1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: debugfs
4  *
5  * Copyright 2011-2020 NXP
6  */
7 
8 #include <linux/debugfs.h>
9 
10 #include "main.h"
11 #include "11n.h"
12 
13 
14 static struct dentry *mwifiex_dfs_dir;
15 
16 static char *bss_modes[] = {
17 	"UNSPECIFIED",
18 	"ADHOC",
19 	"STATION",
20 	"AP",
21 	"AP_VLAN",
22 	"WDS",
23 	"MONITOR",
24 	"MESH_POINT",
25 	"P2P_CLIENT",
26 	"P2P_GO",
27 	"P2P_DEVICE",
28 };
29 
30 /*
31  * Proc info file read handler.
32  *
33  * This function is called when the 'info' file is opened for reading.
34  * It prints the following driver related information -
35  *      - Driver name
36  *      - Driver version
37  *      - Driver extended version
38  *      - Interface name
39  *      - BSS mode
40  *      - Media state (connected or disconnected)
41  *      - MAC address
42  *      - Total number of Tx bytes
43  *      - Total number of Rx bytes
44  *      - Total number of Tx packets
45  *      - Total number of Rx packets
46  *      - Total number of dropped Tx packets
47  *      - Total number of dropped Rx packets
48  *      - Total number of corrupted Tx packets
49  *      - Total number of corrupted Rx packets
50  *      - Carrier status (on or off)
51  *      - Tx queue status (started or stopped)
52  *
53  * For STA mode drivers, it also prints the following extra -
54  *      - ESSID
55  *      - BSSID
56  *      - Channel
57  *      - Region code
58  *      - Multicast count
59  *      - Multicast addresses
60  */
61 static ssize_t
mwifiex_info_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)62 mwifiex_info_read(struct file *file, char __user *ubuf,
63 		  size_t count, loff_t *ppos)
64 {
65 	struct mwifiex_private *priv =
66 		(struct mwifiex_private *) file->private_data;
67 	struct net_device *netdev = priv->netdev;
68 	struct netdev_hw_addr *ha;
69 	struct netdev_queue *txq;
70 	unsigned long page = get_zeroed_page(GFP_KERNEL);
71 	char *p = (char *) page, fmt[64];
72 	struct mwifiex_bss_info info;
73 	ssize_t ret;
74 	int i = 0;
75 
76 	if (!p)
77 		return -ENOMEM;
78 
79 	memset(&info, 0, sizeof(info));
80 	ret = mwifiex_get_bss_info(priv, &info);
81 	if (ret)
82 		goto free_and_exit;
83 
84 	mwifiex_drv_get_driver_version(priv->adapter, fmt, sizeof(fmt) - 1);
85 
86 	mwifiex_get_ver_ext(priv, 0);
87 
88 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
89 	p += sprintf(p, "driver_version = %s", fmt);
90 	p += sprintf(p, "\nverext = %s", priv->version_str);
91 	p += sprintf(p, "\ninterface_name=\"%s\"\n", netdev->name);
92 
93 	if (info.bss_mode >= ARRAY_SIZE(bss_modes))
94 		p += sprintf(p, "bss_mode=\"%d\"\n", info.bss_mode);
95 	else
96 		p += sprintf(p, "bss_mode=\"%s\"\n", bss_modes[info.bss_mode]);
97 
98 	p += sprintf(p, "media_state=\"%s\"\n",
99 		     (!priv->media_connected ? "Disconnected" : "Connected"));
100 	p += sprintf(p, "mac_address=\"%pM\"\n", netdev->dev_addr);
101 
102 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
103 		p += sprintf(p, "multicast_count=\"%d\"\n",
104 			     netdev_mc_count(netdev));
105 		p += sprintf(p, "essid=\"%.*s\"\n", info.ssid.ssid_len,
106 			     info.ssid.ssid);
107 		p += sprintf(p, "bssid=\"%pM\"\n", info.bssid);
108 		p += sprintf(p, "channel=\"%d\"\n", (int) info.bss_chan);
109 		p += sprintf(p, "country_code = \"%s\"\n", info.country_code);
110 		p += sprintf(p, "region_code=\"0x%x\"\n",
111 			     priv->adapter->region_code);
112 
113 		netdev_for_each_mc_addr(ha, netdev)
114 			p += sprintf(p, "multicast_address[%d]=\"%pM\"\n",
115 					i++, ha->addr);
116 	}
117 
118 	p += sprintf(p, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
119 	p += sprintf(p, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
120 	p += sprintf(p, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
121 	p += sprintf(p, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
122 	p += sprintf(p, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
123 	p += sprintf(p, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
124 	p += sprintf(p, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
125 	p += sprintf(p, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
126 	p += sprintf(p, "carrier %s\n", ((netif_carrier_ok(priv->netdev))
127 					 ? "on" : "off"));
128 	p += sprintf(p, "tx queue");
129 	for (i = 0; i < netdev->num_tx_queues; i++) {
130 		txq = netdev_get_tx_queue(netdev, i);
131 		p += sprintf(p, " %d:%s", i, netif_tx_queue_stopped(txq) ?
132 			     "stopped" : "started");
133 	}
134 	p += sprintf(p, "\n");
135 
136 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
137 				      (unsigned long) p - page);
138 
139 free_and_exit:
140 	free_page(page);
141 	return ret;
142 }
143 
144 /*
145  * Proc getlog file read handler.
146  *
147  * This function is called when the 'getlog' file is opened for reading
148  * It prints the following log information -
149  *      - Number of multicast Tx frames
150  *      - Number of failed packets
151  *      - Number of Tx retries
152  *      - Number of multicast Tx retries
153  *      - Number of duplicate frames
154  *      - Number of RTS successes
155  *      - Number of RTS failures
156  *      - Number of ACK failures
157  *      - Number of fragmented Rx frames
158  *      - Number of multicast Rx frames
159  *      - Number of FCS errors
160  *      - Number of Tx frames
161  *      - WEP ICV error counts
162  *      - Number of received beacons
163  *      - Number of missed beacons
164  */
165 static ssize_t
mwifiex_getlog_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)166 mwifiex_getlog_read(struct file *file, char __user *ubuf,
167 		    size_t count, loff_t *ppos)
168 {
169 	struct mwifiex_private *priv =
170 		(struct mwifiex_private *) file->private_data;
171 	unsigned long page = get_zeroed_page(GFP_KERNEL);
172 	char *p = (char *) page;
173 	ssize_t ret;
174 	struct mwifiex_ds_get_stats stats;
175 
176 	if (!p)
177 		return -ENOMEM;
178 
179 	memset(&stats, 0, sizeof(stats));
180 	ret = mwifiex_get_stats_info(priv, &stats);
181 	if (ret)
182 		goto free_and_exit;
183 
184 	p += sprintf(p, "\n"
185 		     "mcasttxframe     %u\n"
186 		     "failed           %u\n"
187 		     "retry            %u\n"
188 		     "multiretry       %u\n"
189 		     "framedup         %u\n"
190 		     "rtssuccess       %u\n"
191 		     "rtsfailure       %u\n"
192 		     "ackfailure       %u\n"
193 		     "rxfrag           %u\n"
194 		     "mcastrxframe     %u\n"
195 		     "fcserror         %u\n"
196 		     "txframe          %u\n"
197 		     "wepicverrcnt-1   %u\n"
198 		     "wepicverrcnt-2   %u\n"
199 		     "wepicverrcnt-3   %u\n"
200 		     "wepicverrcnt-4   %u\n"
201 		     "bcn_rcv_cnt   %u\n"
202 		     "bcn_miss_cnt   %u\n",
203 		     stats.mcast_tx_frame,
204 		     stats.failed,
205 		     stats.retry,
206 		     stats.multi_retry,
207 		     stats.frame_dup,
208 		     stats.rts_success,
209 		     stats.rts_failure,
210 		     stats.ack_failure,
211 		     stats.rx_frag,
212 		     stats.mcast_rx_frame,
213 		     stats.fcs_error,
214 		     stats.tx_frame,
215 		     stats.wep_icv_error[0],
216 		     stats.wep_icv_error[1],
217 		     stats.wep_icv_error[2],
218 		     stats.wep_icv_error[3],
219 		     stats.bcn_rcv_cnt,
220 		     stats.bcn_miss_cnt);
221 
222 
223 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
224 				      (unsigned long) p - page);
225 
226 free_and_exit:
227 	free_page(page);
228 	return ret;
229 }
230 
231 /* Sysfs histogram file read handler.
232  *
233  * This function is called when the 'histogram' file is opened for reading
234  * It prints the following histogram information -
235  *      - Number of histogram samples
236  *      - Receive packet number of each rx_rate
237  *      - Receive packet number of each snr
238  *      - Receive packet number of each nosie_flr
239  *      - Receive packet number of each signal streath
240  */
241 static ssize_t
mwifiex_histogram_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)242 mwifiex_histogram_read(struct file *file, char __user *ubuf,
243 		       size_t count, loff_t *ppos)
244 {
245 	struct mwifiex_private *priv =
246 		(struct mwifiex_private *)file->private_data;
247 	ssize_t ret;
248 	struct mwifiex_histogram_data *phist_data;
249 	int i, value;
250 	unsigned long page = get_zeroed_page(GFP_KERNEL);
251 	char *p = (char *)page;
252 
253 	if (!p)
254 		return -ENOMEM;
255 
256 	if (!priv || !priv->hist_data) {
257 		ret = -EFAULT;
258 		goto free_and_exit;
259 	}
260 
261 	phist_data = priv->hist_data;
262 
263 	p += sprintf(p, "\n"
264 		     "total samples = %d\n",
265 		     atomic_read(&phist_data->num_samples));
266 
267 	p += sprintf(p,
268 		     "rx rates (in Mbps): 0=1M   1=2M 2=5.5M  3=11M   4=6M   5=9M  6=12M\n"
269 		     "7=18M  8=24M  9=36M  10=48M  11=54M 12-27=MCS0-15(BW20) 28-43=MCS0-15(BW40)\n");
270 
271 	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
272 		p += sprintf(p,
273 			     "44-53=MCS0-9(VHT:BW20) 54-63=MCS0-9(VHT:BW40) 64-73=MCS0-9(VHT:BW80)\n\n");
274 	} else {
275 		p += sprintf(p, "\n");
276 	}
277 
278 	for (i = 0; i < MWIFIEX_MAX_RX_RATES; i++) {
279 		value = atomic_read(&phist_data->rx_rate[i]);
280 		if (value)
281 			p += sprintf(p, "rx_rate[%02d] = %d\n", i, value);
282 	}
283 
284 	if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
285 		for (i = MWIFIEX_MAX_RX_RATES; i < MWIFIEX_MAX_AC_RX_RATES;
286 		     i++) {
287 			value = atomic_read(&phist_data->rx_rate[i]);
288 			if (value)
289 				p += sprintf(p, "rx_rate[%02d] = %d\n",
290 					   i, value);
291 		}
292 	}
293 
294 	for (i = 0; i < MWIFIEX_MAX_SNR; i++) {
295 		value =  atomic_read(&phist_data->snr[i]);
296 		if (value)
297 			p += sprintf(p, "snr[%02ddB] = %d\n", i, value);
298 	}
299 	for (i = 0; i < MWIFIEX_MAX_NOISE_FLR; i++) {
300 		value = atomic_read(&phist_data->noise_flr[i]);
301 		if (value)
302 			p += sprintf(p, "noise_flr[%02ddBm] = %d\n",
303 				(int)(i-128), value);
304 	}
305 	for (i = 0; i < MWIFIEX_MAX_SIG_STRENGTH; i++) {
306 		value = atomic_read(&phist_data->sig_str[i]);
307 		if (value)
308 			p += sprintf(p, "sig_strength[-%02ddBm] = %d\n",
309 				i, value);
310 	}
311 
312 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *)page,
313 				      (unsigned long)p - page);
314 
315 free_and_exit:
316 	free_page(page);
317 	return ret;
318 }
319 
320 static ssize_t
mwifiex_histogram_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)321 mwifiex_histogram_write(struct file *file, const char __user *ubuf,
322 			size_t count, loff_t *ppos)
323 {
324 	struct mwifiex_private *priv = (void *)file->private_data;
325 
326 	if (priv && priv->hist_data)
327 		mwifiex_hist_data_reset(priv);
328 	return 0;
329 }
330 
331 static struct mwifiex_debug_info info;
332 
333 /*
334  * Proc debug file read handler.
335  *
336  * This function is called when the 'debug' file is opened for reading
337  * It prints the following log information -
338  *      - Interrupt count
339  *      - WMM AC VO packets count
340  *      - WMM AC VI packets count
341  *      - WMM AC BE packets count
342  *      - WMM AC BK packets count
343  *      - Maximum Tx buffer size
344  *      - Tx buffer size
345  *      - Current Tx buffer size
346  *      - Power Save mode
347  *      - Power Save state
348  *      - Deep Sleep status
349  *      - Device wakeup required status
350  *      - Number of wakeup tries
351  *      - Host Sleep configured status
352  *      - Host Sleep activated status
353  *      - Number of Tx timeouts
354  *      - Number of command timeouts
355  *      - Last timed out command ID
356  *      - Last timed out command action
357  *      - Last command ID
358  *      - Last command action
359  *      - Last command index
360  *      - Last command response ID
361  *      - Last command response index
362  *      - Last event
363  *      - Last event index
364  *      - Number of host to card command failures
365  *      - Number of sleep confirm command failures
366  *      - Number of host to card data failure
367  *      - Number of deauthentication events
368  *      - Number of disassociation events
369  *      - Number of link lost events
370  *      - Number of deauthentication commands
371  *      - Number of association success commands
372  *      - Number of association failure commands
373  *      - Number of commands sent
374  *      - Number of data packets sent
375  *      - Number of command responses received
376  *      - Number of events received
377  *      - Tx BA stream table (TID, RA)
378  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
379  */
380 static ssize_t
mwifiex_debug_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)381 mwifiex_debug_read(struct file *file, char __user *ubuf,
382 		   size_t count, loff_t *ppos)
383 {
384 	struct mwifiex_private *priv =
385 		(struct mwifiex_private *) file->private_data;
386 	unsigned long page = get_zeroed_page(GFP_KERNEL);
387 	char *p = (char *) page;
388 	ssize_t ret;
389 
390 	if (!p)
391 		return -ENOMEM;
392 
393 	ret = mwifiex_get_debug_info(priv, &info);
394 	if (ret)
395 		goto free_and_exit;
396 
397 	p += mwifiex_debug_info_to_buffer(priv, p, &info);
398 
399 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
400 				      (unsigned long) p - page);
401 
402 free_and_exit:
403 	free_page(page);
404 	return ret;
405 }
406 
407 static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
408 
409 /*
410  * Proc regrdwr file write handler.
411  *
412  * This function is called when the 'regrdwr' file is opened for writing
413  *
414  * This function can be used to write to a register.
415  */
416 static ssize_t
mwifiex_regrdwr_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)417 mwifiex_regrdwr_write(struct file *file,
418 		      const char __user *ubuf, size_t count, loff_t *ppos)
419 {
420 	char *buf;
421 	int ret;
422 	u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
423 
424 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
425 	if (IS_ERR(buf))
426 		return PTR_ERR(buf);
427 
428 	if (sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value) != 3) {
429 		ret = -EINVAL;
430 		goto done;
431 	}
432 
433 	if (reg_type == 0 || reg_offset == 0) {
434 		ret = -EINVAL;
435 		goto done;
436 	} else {
437 		saved_reg_type = reg_type;
438 		saved_reg_offset = reg_offset;
439 		saved_reg_value = reg_value;
440 		ret = count;
441 	}
442 done:
443 	kfree(buf);
444 	return ret;
445 }
446 
447 /*
448  * Proc regrdwr file read handler.
449  *
450  * This function is called when the 'regrdwr' file is opened for reading
451  *
452  * This function can be used to read from a register.
453  */
454 static ssize_t
mwifiex_regrdwr_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)455 mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
456 		     size_t count, loff_t *ppos)
457 {
458 	struct mwifiex_private *priv =
459 		(struct mwifiex_private *) file->private_data;
460 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
461 	char *buf = (char *) addr;
462 	int pos = 0, ret = 0;
463 	u32 reg_value;
464 
465 	if (!buf)
466 		return -ENOMEM;
467 
468 	if (!saved_reg_type) {
469 		/* No command has been given */
470 		pos += snprintf(buf, PAGE_SIZE, "0");
471 		goto done;
472 	}
473 	/* Set command has been given */
474 	if (saved_reg_value != UINT_MAX) {
475 		ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
476 					saved_reg_value);
477 
478 		pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
479 				saved_reg_type, saved_reg_offset,
480 				saved_reg_value);
481 
482 		ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
483 
484 		goto done;
485 	}
486 	/* Get command has been given */
487 	ret = mwifiex_reg_read(priv, saved_reg_type,
488 			       saved_reg_offset, &reg_value);
489 	if (ret) {
490 		ret = -EINVAL;
491 		goto done;
492 	}
493 
494 	pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
495 			saved_reg_offset, reg_value);
496 
497 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
498 
499 done:
500 	free_page(addr);
501 	return ret;
502 }
503 
504 /* Proc debug_mask file read handler.
505  * This function is called when the 'debug_mask' file is opened for reading
506  * This function can be used read driver debugging mask value.
507  */
508 static ssize_t
mwifiex_debug_mask_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)509 mwifiex_debug_mask_read(struct file *file, char __user *ubuf,
510 			size_t count, loff_t *ppos)
511 {
512 	struct mwifiex_private *priv =
513 		(struct mwifiex_private *)file->private_data;
514 	unsigned long page = get_zeroed_page(GFP_KERNEL);
515 	char *buf = (char *)page;
516 	size_t ret = 0;
517 	int pos = 0;
518 
519 	if (!buf)
520 		return -ENOMEM;
521 
522 	pos += snprintf(buf, PAGE_SIZE, "debug mask=0x%08x\n",
523 			priv->adapter->debug_mask);
524 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
525 
526 	free_page(page);
527 	return ret;
528 }
529 
530 /* Proc debug_mask file read handler.
531  * This function is called when the 'debug_mask' file is opened for reading
532  * This function can be used read driver debugging mask value.
533  */
534 static ssize_t
mwifiex_debug_mask_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)535 mwifiex_debug_mask_write(struct file *file, const char __user *ubuf,
536 			 size_t count, loff_t *ppos)
537 {
538 	int ret;
539 	unsigned long debug_mask;
540 	struct mwifiex_private *priv = (void *)file->private_data;
541 	char *buf;
542 
543 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
544 	if (IS_ERR(buf))
545 		return PTR_ERR(buf);
546 
547 	if (kstrtoul(buf, 0, &debug_mask)) {
548 		ret = -EINVAL;
549 		goto done;
550 	}
551 
552 	priv->adapter->debug_mask = debug_mask;
553 	ret = count;
554 done:
555 	kfree(buf);
556 	return ret;
557 }
558 
559 /* debugfs verext file write handler.
560  * This function is called when the 'verext' file is opened for write
561  */
562 static ssize_t
mwifiex_verext_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)563 mwifiex_verext_write(struct file *file, const char __user *ubuf,
564 		     size_t count, loff_t *ppos)
565 {
566 	int ret;
567 	u32 versionstrsel;
568 	struct mwifiex_private *priv = (void *)file->private_data;
569 	char buf[16];
570 
571 	memset(buf, 0, sizeof(buf));
572 
573 	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
574 		return -EFAULT;
575 
576 	ret = kstrtou32(buf, 10, &versionstrsel);
577 	if (ret)
578 		return ret;
579 
580 	priv->versionstrsel = versionstrsel;
581 
582 	return count;
583 }
584 
585 /* Proc verext file read handler.
586  * This function is called when the 'verext' file is opened for reading
587  * This function can be used read driver exteneed verion string.
588  */
589 static ssize_t
mwifiex_verext_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)590 mwifiex_verext_read(struct file *file, char __user *ubuf,
591 		    size_t count, loff_t *ppos)
592 {
593 	struct mwifiex_private *priv =
594 		(struct mwifiex_private *)file->private_data;
595 	char buf[256];
596 	int ret;
597 
598 	mwifiex_get_ver_ext(priv, priv->versionstrsel);
599 	ret = snprintf(buf, sizeof(buf), "version string: %s\n",
600 		       priv->version_str);
601 
602 	return simple_read_from_buffer(ubuf, count, ppos, buf, ret);
603 }
604 
605 /* Proc memrw file write handler.
606  * This function is called when the 'memrw' file is opened for writing
607  * This function can be used to write to a memory location.
608  */
609 static ssize_t
mwifiex_memrw_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)610 mwifiex_memrw_write(struct file *file, const char __user *ubuf, size_t count,
611 		    loff_t *ppos)
612 {
613 	int ret;
614 	char cmd;
615 	struct mwifiex_ds_mem_rw mem_rw;
616 	u16 cmd_action;
617 	struct mwifiex_private *priv = (void *)file->private_data;
618 	char *buf;
619 
620 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
621 	if (IS_ERR(buf))
622 		return PTR_ERR(buf);
623 
624 	ret = sscanf(buf, "%c %x %x", &cmd, &mem_rw.addr, &mem_rw.value);
625 	if (ret != 3) {
626 		ret = -EINVAL;
627 		goto done;
628 	}
629 
630 	if ((cmd == 'r') || (cmd == 'R')) {
631 		cmd_action = HostCmd_ACT_GEN_GET;
632 		mem_rw.value = 0;
633 	} else if ((cmd == 'w') || (cmd == 'W')) {
634 		cmd_action = HostCmd_ACT_GEN_SET;
635 	} else {
636 		ret = -EINVAL;
637 		goto done;
638 	}
639 
640 	memcpy(&priv->mem_rw, &mem_rw, sizeof(mem_rw));
641 	if (mwifiex_send_cmd(priv, HostCmd_CMD_MEM_ACCESS, cmd_action, 0,
642 			     &mem_rw, true))
643 		ret = -1;
644 	else
645 		ret = count;
646 
647 done:
648 	kfree(buf);
649 	return ret;
650 }
651 
652 /* Proc memrw file read handler.
653  * This function is called when the 'memrw' file is opened for reading
654  * This function can be used to read from a memory location.
655  */
656 static ssize_t
mwifiex_memrw_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)657 mwifiex_memrw_read(struct file *file, char __user *ubuf,
658 		   size_t count, loff_t *ppos)
659 {
660 	struct mwifiex_private *priv = (void *)file->private_data;
661 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
662 	char *buf = (char *)addr;
663 	int ret, pos = 0;
664 
665 	if (!buf)
666 		return -ENOMEM;
667 
668 	pos += snprintf(buf, PAGE_SIZE, "0x%x 0x%x\n", priv->mem_rw.addr,
669 			priv->mem_rw.value);
670 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
671 
672 	free_page(addr);
673 	return ret;
674 }
675 
676 static u32 saved_offset = -1, saved_bytes = -1;
677 
678 /*
679  * Proc rdeeprom file write handler.
680  *
681  * This function is called when the 'rdeeprom' file is opened for writing
682  *
683  * This function can be used to write to a RDEEPROM location.
684  */
685 static ssize_t
mwifiex_rdeeprom_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)686 mwifiex_rdeeprom_write(struct file *file,
687 		       const char __user *ubuf, size_t count, loff_t *ppos)
688 {
689 	char *buf;
690 	int ret = 0;
691 	int offset = -1, bytes = -1;
692 
693 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
694 	if (IS_ERR(buf))
695 		return PTR_ERR(buf);
696 
697 	if (sscanf(buf, "%d %d", &offset, &bytes) != 2) {
698 		ret = -EINVAL;
699 		goto done;
700 	}
701 
702 	if (offset == -1 || bytes == -1) {
703 		ret = -EINVAL;
704 		goto done;
705 	} else {
706 		saved_offset = offset;
707 		saved_bytes = bytes;
708 		ret = count;
709 	}
710 done:
711 	kfree(buf);
712 	return ret;
713 }
714 
715 /*
716  * Proc rdeeprom read write handler.
717  *
718  * This function is called when the 'rdeeprom' file is opened for reading
719  *
720  * This function can be used to read from a RDEEPROM location.
721  */
722 static ssize_t
mwifiex_rdeeprom_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)723 mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
724 		      size_t count, loff_t *ppos)
725 {
726 	struct mwifiex_private *priv =
727 		(struct mwifiex_private *) file->private_data;
728 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
729 	char *buf = (char *) addr;
730 	int pos, ret, i;
731 	u8 value[MAX_EEPROM_DATA];
732 
733 	if (!buf)
734 		return -ENOMEM;
735 
736 	if (saved_offset == -1) {
737 		/* No command has been given */
738 		pos = snprintf(buf, PAGE_SIZE, "0");
739 		goto done;
740 	}
741 
742 	/* Get command has been given */
743 	ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
744 				  (u16) saved_bytes, value);
745 	if (ret) {
746 		ret = -EINVAL;
747 		goto out_free;
748 	}
749 
750 	pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
751 
752 	for (i = 0; i < saved_bytes; i++)
753 		pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ", value[i]);
754 
755 done:
756 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
757 out_free:
758 	free_page(addr);
759 	return ret;
760 }
761 
762 /* Proc hscfg file write handler
763  * This function can be used to configure the host sleep parameters.
764  */
765 static ssize_t
mwifiex_hscfg_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)766 mwifiex_hscfg_write(struct file *file, const char __user *ubuf,
767 		    size_t count, loff_t *ppos)
768 {
769 	struct mwifiex_private *priv = (void *)file->private_data;
770 	char *buf;
771 	int ret, arg_num;
772 	struct mwifiex_ds_hs_cfg hscfg;
773 	int conditions = HS_CFG_COND_DEF;
774 	u32 gpio = HS_CFG_GPIO_DEF, gap = HS_CFG_GAP_DEF;
775 
776 	buf = memdup_user_nul(ubuf, min(count, (size_t)(PAGE_SIZE - 1)));
777 	if (IS_ERR(buf))
778 		return PTR_ERR(buf);
779 
780 	arg_num = sscanf(buf, "%d %x %x", &conditions, &gpio, &gap);
781 
782 	memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
783 
784 	if (arg_num > 3) {
785 		mwifiex_dbg(priv->adapter, ERROR,
786 			    "Too many arguments\n");
787 		ret = -EINVAL;
788 		goto done;
789 	}
790 
791 	if (arg_num >= 1 && arg_num < 3)
792 		mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
793 				      MWIFIEX_SYNC_CMD, &hscfg);
794 
795 	if (arg_num) {
796 		if (conditions == HS_CFG_CANCEL) {
797 			mwifiex_cancel_hs(priv, MWIFIEX_ASYNC_CMD);
798 			ret = count;
799 			goto done;
800 		}
801 		hscfg.conditions = conditions;
802 	}
803 	if (arg_num >= 2)
804 		hscfg.gpio = gpio;
805 	if (arg_num == 3)
806 		hscfg.gap = gap;
807 
808 	hscfg.is_invoke_hostcmd = false;
809 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
810 			      MWIFIEX_SYNC_CMD, &hscfg);
811 
812 	mwifiex_enable_hs(priv->adapter);
813 	clear_bit(MWIFIEX_IS_HS_ENABLING, &priv->adapter->work_flags);
814 	ret = count;
815 done:
816 	kfree(buf);
817 	return ret;
818 }
819 
820 /* Proc hscfg file read handler
821  * This function can be used to read host sleep configuration
822  * parameters from driver.
823  */
824 static ssize_t
mwifiex_hscfg_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)825 mwifiex_hscfg_read(struct file *file, char __user *ubuf,
826 		   size_t count, loff_t *ppos)
827 {
828 	struct mwifiex_private *priv = (void *)file->private_data;
829 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
830 	char *buf = (char *)addr;
831 	int pos, ret;
832 	struct mwifiex_ds_hs_cfg hscfg;
833 
834 	if (!buf)
835 		return -ENOMEM;
836 
837 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
838 			      MWIFIEX_SYNC_CMD, &hscfg);
839 
840 	pos = snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", hscfg.conditions,
841 		       hscfg.gpio, hscfg.gap);
842 
843 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
844 
845 	free_page(addr);
846 	return ret;
847 }
848 
849 static ssize_t
mwifiex_timeshare_coex_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)850 mwifiex_timeshare_coex_read(struct file *file, char __user *ubuf,
851 			    size_t count, loff_t *ppos)
852 {
853 	struct mwifiex_private *priv = file->private_data;
854 	char buf[3];
855 	bool timeshare_coex;
856 	int ret;
857 	unsigned int len;
858 
859 	if (priv->adapter->fw_api_ver != MWIFIEX_FW_V15)
860 		return -EOPNOTSUPP;
861 
862 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_ROBUST_COEX,
863 			       HostCmd_ACT_GEN_GET, 0, &timeshare_coex, true);
864 	if (ret)
865 		return ret;
866 
867 	len = sprintf(buf, "%d\n", timeshare_coex);
868 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
869 }
870 
871 static ssize_t
mwifiex_timeshare_coex_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)872 mwifiex_timeshare_coex_write(struct file *file, const char __user *ubuf,
873 			     size_t count, loff_t *ppos)
874 {
875 	bool timeshare_coex;
876 	struct mwifiex_private *priv = file->private_data;
877 	char kbuf[16];
878 	int ret;
879 
880 	if (priv->adapter->fw_api_ver != MWIFIEX_FW_V15)
881 		return -EOPNOTSUPP;
882 
883 	memset(kbuf, 0, sizeof(kbuf));
884 
885 	if (copy_from_user(&kbuf, ubuf, min_t(size_t, sizeof(kbuf) - 1, count)))
886 		return -EFAULT;
887 
888 	if (kstrtobool(kbuf, &timeshare_coex))
889 		return -EINVAL;
890 
891 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_ROBUST_COEX,
892 			       HostCmd_ACT_GEN_SET, 0, &timeshare_coex, true);
893 	if (ret)
894 		return ret;
895 	else
896 		return count;
897 }
898 
899 static ssize_t
mwifiex_reset_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)900 mwifiex_reset_write(struct file *file,
901 		    const char __user *ubuf, size_t count, loff_t *ppos)
902 {
903 	struct mwifiex_private *priv = file->private_data;
904 	struct mwifiex_adapter *adapter = priv->adapter;
905 	bool result;
906 	int rc;
907 
908 	rc = kstrtobool_from_user(ubuf, count, &result);
909 	if (rc)
910 		return rc;
911 
912 	if (!result)
913 		return -EINVAL;
914 
915 	if (adapter->if_ops.card_reset) {
916 		dev_info(adapter->dev, "Resetting per request\n");
917 		adapter->if_ops.card_reset(adapter);
918 	}
919 
920 	return count;
921 }
922 
923 #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
924 	debugfs_create_file(#name, 0644, priv->dfs_dev_dir, priv,       \
925 			    &mwifiex_dfs_##name##_fops);                \
926 } while (0);
927 
928 #define MWIFIEX_DFS_FILE_OPS(name)                                      \
929 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
930 	.read = mwifiex_##name##_read,                                  \
931 	.write = mwifiex_##name##_write,                                \
932 	.open = simple_open,                                            \
933 };
934 
935 #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
936 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
937 	.read = mwifiex_##name##_read,                                  \
938 	.open = simple_open,                                            \
939 };
940 
941 #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
942 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
943 	.write = mwifiex_##name##_write,                                \
944 	.open = simple_open,                                            \
945 };
946 
947 
948 MWIFIEX_DFS_FILE_READ_OPS(info);
949 MWIFIEX_DFS_FILE_READ_OPS(debug);
950 MWIFIEX_DFS_FILE_READ_OPS(getlog);
951 MWIFIEX_DFS_FILE_OPS(regrdwr);
952 MWIFIEX_DFS_FILE_OPS(rdeeprom);
953 MWIFIEX_DFS_FILE_OPS(memrw);
954 MWIFIEX_DFS_FILE_OPS(hscfg);
955 MWIFIEX_DFS_FILE_OPS(histogram);
956 MWIFIEX_DFS_FILE_OPS(debug_mask);
957 MWIFIEX_DFS_FILE_OPS(timeshare_coex);
958 MWIFIEX_DFS_FILE_WRITE_OPS(reset);
959 MWIFIEX_DFS_FILE_OPS(verext);
960 
961 /*
962  * This function creates the debug FS directory structure and the files.
963  */
964 void
mwifiex_dev_debugfs_init(struct mwifiex_private * priv)965 mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
966 {
967 	if (!mwifiex_dfs_dir || !priv)
968 		return;
969 
970 	priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
971 					       mwifiex_dfs_dir);
972 
973 	if (!priv->dfs_dev_dir)
974 		return;
975 
976 	MWIFIEX_DFS_ADD_FILE(info);
977 	MWIFIEX_DFS_ADD_FILE(debug);
978 	MWIFIEX_DFS_ADD_FILE(getlog);
979 	MWIFIEX_DFS_ADD_FILE(regrdwr);
980 	MWIFIEX_DFS_ADD_FILE(rdeeprom);
981 
982 	MWIFIEX_DFS_ADD_FILE(memrw);
983 	MWIFIEX_DFS_ADD_FILE(hscfg);
984 	MWIFIEX_DFS_ADD_FILE(histogram);
985 	MWIFIEX_DFS_ADD_FILE(debug_mask);
986 	MWIFIEX_DFS_ADD_FILE(timeshare_coex);
987 	MWIFIEX_DFS_ADD_FILE(reset);
988 	MWIFIEX_DFS_ADD_FILE(verext);
989 }
990 
991 /*
992  * This function removes the debug FS directory structure and the files.
993  */
994 void
mwifiex_dev_debugfs_remove(struct mwifiex_private * priv)995 mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
996 {
997 	if (!priv)
998 		return;
999 
1000 	debugfs_remove_recursive(priv->dfs_dev_dir);
1001 }
1002 
1003 /*
1004  * This function creates the top level proc directory.
1005  */
1006 void
mwifiex_debugfs_init(void)1007 mwifiex_debugfs_init(void)
1008 {
1009 	if (!mwifiex_dfs_dir)
1010 		mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
1011 }
1012 
1013 /*
1014  * This function removes the top level proc directory.
1015  */
1016 void
mwifiex_debugfs_remove(void)1017 mwifiex_debugfs_remove(void)
1018 {
1019 	debugfs_remove(mwifiex_dfs_dir);
1020 }
1021