• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Marvell Wireless LAN device driver: debugfs
3  *
4  * Copyright (C) 2011-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include <linux/debugfs.h>
21 
22 #include "main.h"
23 #include "11n.h"
24 
25 
26 static struct dentry *mwifiex_dfs_dir;
27 
28 static char *bss_modes[] = {
29 	"UNSPECIFIED",
30 	"ADHOC",
31 	"STATION",
32 	"AP",
33 	"AP_VLAN",
34 	"WDS",
35 	"MONITOR",
36 	"MESH_POINT",
37 	"P2P_CLIENT",
38 	"P2P_GO",
39 	"P2P_DEVICE",
40 };
41 
42 /* size/addr for mwifiex_debug_info */
43 #define item_size(n)		(FIELD_SIZEOF(struct mwifiex_debug_info, n))
44 #define item_addr(n)		(offsetof(struct mwifiex_debug_info, n))
45 
46 /* size/addr for struct mwifiex_adapter */
47 #define adapter_item_size(n)	(FIELD_SIZEOF(struct mwifiex_adapter, n))
48 #define adapter_item_addr(n)	(offsetof(struct mwifiex_adapter, n))
49 
50 struct mwifiex_debug_data {
51 	char name[32];		/* variable/array name */
52 	u32 size;		/* size of the variable/array */
53 	size_t addr;		/* address of the variable/array */
54 	int num;		/* number of variables in an array */
55 };
56 
57 static struct mwifiex_debug_data items[] = {
58 	{"int_counter", item_size(int_counter),
59 	 item_addr(int_counter), 1},
60 	{"wmm_ac_vo", item_size(packets_out[WMM_AC_VO]),
61 	 item_addr(packets_out[WMM_AC_VO]), 1},
62 	{"wmm_ac_vi", item_size(packets_out[WMM_AC_VI]),
63 	 item_addr(packets_out[WMM_AC_VI]), 1},
64 	{"wmm_ac_be", item_size(packets_out[WMM_AC_BE]),
65 	 item_addr(packets_out[WMM_AC_BE]), 1},
66 	{"wmm_ac_bk", item_size(packets_out[WMM_AC_BK]),
67 	 item_addr(packets_out[WMM_AC_BK]), 1},
68 	{"tx_buf_size", item_size(tx_buf_size),
69 	 item_addr(tx_buf_size), 1},
70 	{"curr_tx_buf_size", item_size(curr_tx_buf_size),
71 	 item_addr(curr_tx_buf_size), 1},
72 	{"ps_mode", item_size(ps_mode),
73 	 item_addr(ps_mode), 1},
74 	{"ps_state", item_size(ps_state),
75 	 item_addr(ps_state), 1},
76 	{"is_deep_sleep", item_size(is_deep_sleep),
77 	 item_addr(is_deep_sleep), 1},
78 	{"wakeup_dev_req", item_size(pm_wakeup_card_req),
79 	 item_addr(pm_wakeup_card_req), 1},
80 	{"wakeup_tries", item_size(pm_wakeup_fw_try),
81 	 item_addr(pm_wakeup_fw_try), 1},
82 	{"hs_configured", item_size(is_hs_configured),
83 	 item_addr(is_hs_configured), 1},
84 	{"hs_activated", item_size(hs_activated),
85 	 item_addr(hs_activated), 1},
86 	{"num_tx_timeout", item_size(num_tx_timeout),
87 	 item_addr(num_tx_timeout), 1},
88 	{"is_cmd_timedout", item_size(is_cmd_timedout),
89 	 item_addr(is_cmd_timedout), 1},
90 	{"timeout_cmd_id", item_size(timeout_cmd_id),
91 	 item_addr(timeout_cmd_id), 1},
92 	{"timeout_cmd_act", item_size(timeout_cmd_act),
93 	 item_addr(timeout_cmd_act), 1},
94 	{"last_cmd_id", item_size(last_cmd_id),
95 	 item_addr(last_cmd_id), DBG_CMD_NUM},
96 	{"last_cmd_act", item_size(last_cmd_act),
97 	 item_addr(last_cmd_act), DBG_CMD_NUM},
98 	{"last_cmd_index", item_size(last_cmd_index),
99 	 item_addr(last_cmd_index), 1},
100 	{"last_cmd_resp_id", item_size(last_cmd_resp_id),
101 	 item_addr(last_cmd_resp_id), DBG_CMD_NUM},
102 	{"last_cmd_resp_index", item_size(last_cmd_resp_index),
103 	 item_addr(last_cmd_resp_index), 1},
104 	{"last_event", item_size(last_event),
105 	 item_addr(last_event), DBG_CMD_NUM},
106 	{"last_event_index", item_size(last_event_index),
107 	 item_addr(last_event_index), 1},
108 	{"num_cmd_h2c_fail", item_size(num_cmd_host_to_card_failure),
109 	 item_addr(num_cmd_host_to_card_failure), 1},
110 	{"num_cmd_sleep_cfm_fail",
111 	 item_size(num_cmd_sleep_cfm_host_to_card_failure),
112 	 item_addr(num_cmd_sleep_cfm_host_to_card_failure), 1},
113 	{"num_tx_h2c_fail", item_size(num_tx_host_to_card_failure),
114 	 item_addr(num_tx_host_to_card_failure), 1},
115 	{"num_evt_deauth", item_size(num_event_deauth),
116 	 item_addr(num_event_deauth), 1},
117 	{"num_evt_disassoc", item_size(num_event_disassoc),
118 	 item_addr(num_event_disassoc), 1},
119 	{"num_evt_link_lost", item_size(num_event_link_lost),
120 	 item_addr(num_event_link_lost), 1},
121 	{"num_cmd_deauth", item_size(num_cmd_deauth),
122 	 item_addr(num_cmd_deauth), 1},
123 	{"num_cmd_assoc_ok", item_size(num_cmd_assoc_success),
124 	 item_addr(num_cmd_assoc_success), 1},
125 	{"num_cmd_assoc_fail", item_size(num_cmd_assoc_failure),
126 	 item_addr(num_cmd_assoc_failure), 1},
127 	{"cmd_sent", item_size(cmd_sent),
128 	 item_addr(cmd_sent), 1},
129 	{"data_sent", item_size(data_sent),
130 	 item_addr(data_sent), 1},
131 	{"cmd_resp_received", item_size(cmd_resp_received),
132 	 item_addr(cmd_resp_received), 1},
133 	{"event_received", item_size(event_received),
134 	 item_addr(event_received), 1},
135 
136 	/* variables defined in struct mwifiex_adapter */
137 	{"cmd_pending", adapter_item_size(cmd_pending),
138 	 adapter_item_addr(cmd_pending), 1},
139 	{"tx_pending", adapter_item_size(tx_pending),
140 	 adapter_item_addr(tx_pending), 1},
141 	{"rx_pending", adapter_item_size(rx_pending),
142 	 adapter_item_addr(rx_pending), 1},
143 };
144 
145 static int num_of_items = ARRAY_SIZE(items);
146 
147 /*
148  * Proc info file read handler.
149  *
150  * This function is called when the 'info' file is opened for reading.
151  * It prints the following driver related information -
152  *      - Driver name
153  *      - Driver version
154  *      - Driver extended version
155  *      - Interface name
156  *      - BSS mode
157  *      - Media state (connected or disconnected)
158  *      - MAC address
159  *      - Total number of Tx bytes
160  *      - Total number of Rx bytes
161  *      - Total number of Tx packets
162  *      - Total number of Rx packets
163  *      - Total number of dropped Tx packets
164  *      - Total number of dropped Rx packets
165  *      - Total number of corrupted Tx packets
166  *      - Total number of corrupted Rx packets
167  *      - Carrier status (on or off)
168  *      - Tx queue status (started or stopped)
169  *
170  * For STA mode drivers, it also prints the following extra -
171  *      - ESSID
172  *      - BSSID
173  *      - Channel
174  *      - Region code
175  *      - Multicast count
176  *      - Multicast addresses
177  */
178 static ssize_t
mwifiex_info_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)179 mwifiex_info_read(struct file *file, char __user *ubuf,
180 		  size_t count, loff_t *ppos)
181 {
182 	struct mwifiex_private *priv =
183 		(struct mwifiex_private *) file->private_data;
184 	struct net_device *netdev = priv->netdev;
185 	struct netdev_hw_addr *ha;
186 	struct netdev_queue *txq;
187 	unsigned long page = get_zeroed_page(GFP_KERNEL);
188 	char *p = (char *) page, fmt[64];
189 	struct mwifiex_bss_info info;
190 	ssize_t ret;
191 	int i = 0;
192 
193 	if (!p)
194 		return -ENOMEM;
195 
196 	memset(&info, 0, sizeof(info));
197 	ret = mwifiex_get_bss_info(priv, &info);
198 	if (ret)
199 		goto free_and_exit;
200 
201 	mwifiex_drv_get_driver_version(priv->adapter, fmt, sizeof(fmt) - 1);
202 
203 	if (!priv->version_str[0])
204 		mwifiex_get_ver_ext(priv);
205 
206 	p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
207 	p += sprintf(p, "driver_version = %s", fmt);
208 	p += sprintf(p, "\nverext = %s", priv->version_str);
209 	p += sprintf(p, "\ninterface_name=\"%s\"\n", netdev->name);
210 
211 	if (info.bss_mode >= ARRAY_SIZE(bss_modes))
212 		p += sprintf(p, "bss_mode=\"%d\"\n", info.bss_mode);
213 	else
214 		p += sprintf(p, "bss_mode=\"%s\"\n", bss_modes[info.bss_mode]);
215 
216 	p += sprintf(p, "media_state=\"%s\"\n",
217 		     (!priv->media_connected ? "Disconnected" : "Connected"));
218 	p += sprintf(p, "mac_address=\"%pM\"\n", netdev->dev_addr);
219 
220 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
221 		p += sprintf(p, "multicast_count=\"%d\"\n",
222 			     netdev_mc_count(netdev));
223 		p += sprintf(p, "essid=\"%.*s\"\n", info.ssid.ssid_len,
224 			     info.ssid.ssid);
225 		p += sprintf(p, "bssid=\"%pM\"\n", info.bssid);
226 		p += sprintf(p, "channel=\"%d\"\n", (int) info.bss_chan);
227 		p += sprintf(p, "country_code = \"%s\"\n", info.country_code);
228 
229 		netdev_for_each_mc_addr(ha, netdev)
230 			p += sprintf(p, "multicast_address[%d]=\"%pM\"\n",
231 					i++, ha->addr);
232 	}
233 
234 	p += sprintf(p, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
235 	p += sprintf(p, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
236 	p += sprintf(p, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
237 	p += sprintf(p, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
238 	p += sprintf(p, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
239 	p += sprintf(p, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
240 	p += sprintf(p, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
241 	p += sprintf(p, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
242 	p += sprintf(p, "carrier %s\n", ((netif_carrier_ok(priv->netdev))
243 					 ? "on" : "off"));
244 	p += sprintf(p, "tx queue");
245 	for (i = 0; i < netdev->num_tx_queues; i++) {
246 		txq = netdev_get_tx_queue(netdev, i);
247 		p += sprintf(p, " %d:%s", i, netif_tx_queue_stopped(txq) ?
248 			     "stopped" : "started");
249 	}
250 	p += sprintf(p, "\n");
251 
252 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
253 				      (unsigned long) p - page);
254 
255 free_and_exit:
256 	free_page(page);
257 	return ret;
258 }
259 
260 /*
261  * Proc firmware dump read handler.
262  *
263  * This function is called when the 'fw_dump' file is opened for
264  * reading.
265  * This function dumps firmware memory in different files
266  * (ex. DTCM, ITCM, SQRAM etc.) based on the the segments for
267  * debugging.
268  */
269 static ssize_t
mwifiex_fw_dump_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)270 mwifiex_fw_dump_read(struct file *file, char __user *ubuf,
271 		     size_t count, loff_t *ppos)
272 {
273 	struct mwifiex_private *priv = file->private_data;
274 
275 	if (!priv->adapter->if_ops.fw_dump)
276 		return -EIO;
277 
278 	priv->adapter->if_ops.fw_dump(priv->adapter);
279 
280 	return 0;
281 }
282 
283 /*
284  * Proc getlog file read handler.
285  *
286  * This function is called when the 'getlog' file is opened for reading
287  * It prints the following log information -
288  *      - Number of multicast Tx frames
289  *      - Number of failed packets
290  *      - Number of Tx retries
291  *      - Number of multicast Tx retries
292  *      - Number of duplicate frames
293  *      - Number of RTS successes
294  *      - Number of RTS failures
295  *      - Number of ACK failures
296  *      - Number of fragmented Rx frames
297  *      - Number of multicast Rx frames
298  *      - Number of FCS errors
299  *      - Number of Tx frames
300  *      - WEP ICV error counts
301  */
302 static ssize_t
mwifiex_getlog_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)303 mwifiex_getlog_read(struct file *file, char __user *ubuf,
304 		    size_t count, loff_t *ppos)
305 {
306 	struct mwifiex_private *priv =
307 		(struct mwifiex_private *) file->private_data;
308 	unsigned long page = get_zeroed_page(GFP_KERNEL);
309 	char *p = (char *) page;
310 	ssize_t ret;
311 	struct mwifiex_ds_get_stats stats;
312 
313 	if (!p)
314 		return -ENOMEM;
315 
316 	memset(&stats, 0, sizeof(stats));
317 	ret = mwifiex_get_stats_info(priv, &stats);
318 	if (ret)
319 		goto free_and_exit;
320 
321 	p += sprintf(p, "\n"
322 		     "mcasttxframe     %u\n"
323 		     "failed           %u\n"
324 		     "retry            %u\n"
325 		     "multiretry       %u\n"
326 		     "framedup         %u\n"
327 		     "rtssuccess       %u\n"
328 		     "rtsfailure       %u\n"
329 		     "ackfailure       %u\n"
330 		     "rxfrag           %u\n"
331 		     "mcastrxframe     %u\n"
332 		     "fcserror         %u\n"
333 		     "txframe          %u\n"
334 		     "wepicverrcnt-1   %u\n"
335 		     "wepicverrcnt-2   %u\n"
336 		     "wepicverrcnt-3   %u\n"
337 		     "wepicverrcnt-4   %u\n",
338 		     stats.mcast_tx_frame,
339 		     stats.failed,
340 		     stats.retry,
341 		     stats.multi_retry,
342 		     stats.frame_dup,
343 		     stats.rts_success,
344 		     stats.rts_failure,
345 		     stats.ack_failure,
346 		     stats.rx_frag,
347 		     stats.mcast_rx_frame,
348 		     stats.fcs_error,
349 		     stats.tx_frame,
350 		     stats.wep_icv_error[0],
351 		     stats.wep_icv_error[1],
352 		     stats.wep_icv_error[2],
353 		     stats.wep_icv_error[3]);
354 
355 
356 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
357 				      (unsigned long) p - page);
358 
359 free_and_exit:
360 	free_page(page);
361 	return ret;
362 }
363 
364 static struct mwifiex_debug_info info;
365 
366 /*
367  * Proc debug file read handler.
368  *
369  * This function is called when the 'debug' file is opened for reading
370  * It prints the following log information -
371  *      - Interrupt count
372  *      - WMM AC VO packets count
373  *      - WMM AC VI packets count
374  *      - WMM AC BE packets count
375  *      - WMM AC BK packets count
376  *      - Maximum Tx buffer size
377  *      - Tx buffer size
378  *      - Current Tx buffer size
379  *      - Power Save mode
380  *      - Power Save state
381  *      - Deep Sleep status
382  *      - Device wakeup required status
383  *      - Number of wakeup tries
384  *      - Host Sleep configured status
385  *      - Host Sleep activated status
386  *      - Number of Tx timeouts
387  *      - Number of command timeouts
388  *      - Last timed out command ID
389  *      - Last timed out command action
390  *      - Last command ID
391  *      - Last command action
392  *      - Last command index
393  *      - Last command response ID
394  *      - Last command response index
395  *      - Last event
396  *      - Last event index
397  *      - Number of host to card command failures
398  *      - Number of sleep confirm command failures
399  *      - Number of host to card data failure
400  *      - Number of deauthentication events
401  *      - Number of disassociation events
402  *      - Number of link lost events
403  *      - Number of deauthentication commands
404  *      - Number of association success commands
405  *      - Number of association failure commands
406  *      - Number of commands sent
407  *      - Number of data packets sent
408  *      - Number of command responses received
409  *      - Number of events received
410  *      - Tx BA stream table (TID, RA)
411  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
412  */
413 static ssize_t
mwifiex_debug_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)414 mwifiex_debug_read(struct file *file, char __user *ubuf,
415 		   size_t count, loff_t *ppos)
416 {
417 	struct mwifiex_private *priv =
418 		(struct mwifiex_private *) file->private_data;
419 	struct mwifiex_debug_data *d = &items[0];
420 	unsigned long page = get_zeroed_page(GFP_KERNEL);
421 	char *p = (char *) page;
422 	ssize_t ret;
423 	size_t size, addr;
424 	long val;
425 	int i, j;
426 
427 	if (!p)
428 		return -ENOMEM;
429 
430 	ret = mwifiex_get_debug_info(priv, &info);
431 	if (ret)
432 		goto free_and_exit;
433 
434 	for (i = 0; i < num_of_items; i++) {
435 		p += sprintf(p, "%s=", d[i].name);
436 
437 		size = d[i].size / d[i].num;
438 
439 		if (i < (num_of_items - 3))
440 			addr = d[i].addr + (size_t) &info;
441 		else /* The last 3 items are struct mwifiex_adapter variables */
442 			addr = d[i].addr + (size_t) priv->adapter;
443 
444 		for (j = 0; j < d[i].num; j++) {
445 			switch (size) {
446 			case 1:
447 				val = *((u8 *) addr);
448 				break;
449 			case 2:
450 				val = *((u16 *) addr);
451 				break;
452 			case 4:
453 				val = *((u32 *) addr);
454 				break;
455 			case 8:
456 				val = *((long long *) addr);
457 				break;
458 			default:
459 				val = -1;
460 				break;
461 			}
462 
463 			p += sprintf(p, "%#lx ", val);
464 			addr += size;
465 		}
466 
467 		p += sprintf(p, "\n");
468 	}
469 
470 	if (info.tx_tbl_num) {
471 		p += sprintf(p, "Tx BA stream table:\n");
472 		for (i = 0; i < info.tx_tbl_num; i++)
473 			p += sprintf(p, "tid = %d, ra = %pM\n",
474 				     info.tx_tbl[i].tid, info.tx_tbl[i].ra);
475 	}
476 
477 	if (info.rx_tbl_num) {
478 		p += sprintf(p, "Rx reorder table:\n");
479 		for (i = 0; i < info.rx_tbl_num; i++) {
480 			p += sprintf(p, "tid = %d, ta = %pM, "
481 				     "start_win = %d, "
482 				     "win_size = %d, buffer: ",
483 				     info.rx_tbl[i].tid,
484 				     info.rx_tbl[i].ta,
485 				     info.rx_tbl[i].start_win,
486 				     info.rx_tbl[i].win_size);
487 
488 			for (j = 0; j < info.rx_tbl[i].win_size; j++)
489 				p += sprintf(p, "%c ",
490 					     info.rx_tbl[i].buffer[j] ?
491 					     '1' : '0');
492 
493 			p += sprintf(p, "\n");
494 		}
495 	}
496 
497 	ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
498 				      (unsigned long) p - page);
499 
500 free_and_exit:
501 	free_page(page);
502 	return ret;
503 }
504 
505 static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
506 
507 /*
508  * Proc regrdwr file write handler.
509  *
510  * This function is called when the 'regrdwr' file is opened for writing
511  *
512  * This function can be used to write to a register.
513  */
514 static ssize_t
mwifiex_regrdwr_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)515 mwifiex_regrdwr_write(struct file *file,
516 		      const char __user *ubuf, size_t count, loff_t *ppos)
517 {
518 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
519 	char *buf = (char *) addr;
520 	size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
521 	int ret;
522 	u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
523 
524 	if (!buf)
525 		return -ENOMEM;
526 
527 
528 	if (copy_from_user(buf, ubuf, buf_size)) {
529 		ret = -EFAULT;
530 		goto done;
531 	}
532 
533 	sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value);
534 
535 	if (reg_type == 0 || reg_offset == 0) {
536 		ret = -EINVAL;
537 		goto done;
538 	} else {
539 		saved_reg_type = reg_type;
540 		saved_reg_offset = reg_offset;
541 		saved_reg_value = reg_value;
542 		ret = count;
543 	}
544 done:
545 	free_page(addr);
546 	return ret;
547 }
548 
549 /*
550  * Proc regrdwr file read handler.
551  *
552  * This function is called when the 'regrdwr' file is opened for reading
553  *
554  * This function can be used to read from a register.
555  */
556 static ssize_t
mwifiex_regrdwr_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)557 mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
558 		     size_t count, loff_t *ppos)
559 {
560 	struct mwifiex_private *priv =
561 		(struct mwifiex_private *) file->private_data;
562 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
563 	char *buf = (char *) addr;
564 	int pos = 0, ret = 0;
565 	u32 reg_value;
566 
567 	if (!buf)
568 		return -ENOMEM;
569 
570 	if (!saved_reg_type) {
571 		/* No command has been given */
572 		pos += snprintf(buf, PAGE_SIZE, "0");
573 		goto done;
574 	}
575 	/* Set command has been given */
576 	if (saved_reg_value != UINT_MAX) {
577 		ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
578 					saved_reg_value);
579 
580 		pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
581 				saved_reg_type, saved_reg_offset,
582 				saved_reg_value);
583 
584 		ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
585 
586 		goto done;
587 	}
588 	/* Get command has been given */
589 	ret = mwifiex_reg_read(priv, saved_reg_type,
590 			       saved_reg_offset, &reg_value);
591 	if (ret) {
592 		ret = -EINVAL;
593 		goto done;
594 	}
595 
596 	pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
597 			saved_reg_offset, reg_value);
598 
599 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
600 
601 done:
602 	free_page(addr);
603 	return ret;
604 }
605 
606 static u32 saved_offset = -1, saved_bytes = -1;
607 
608 /*
609  * Proc rdeeprom file write handler.
610  *
611  * This function is called when the 'rdeeprom' file is opened for writing
612  *
613  * This function can be used to write to a RDEEPROM location.
614  */
615 static ssize_t
mwifiex_rdeeprom_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)616 mwifiex_rdeeprom_write(struct file *file,
617 		       const char __user *ubuf, size_t count, loff_t *ppos)
618 {
619 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
620 	char *buf = (char *) addr;
621 	size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
622 	int ret = 0;
623 	int offset = -1, bytes = -1;
624 
625 	if (!buf)
626 		return -ENOMEM;
627 
628 
629 	if (copy_from_user(buf, ubuf, buf_size)) {
630 		ret = -EFAULT;
631 		goto done;
632 	}
633 
634 	sscanf(buf, "%d %d", &offset, &bytes);
635 
636 	if (offset == -1 || bytes == -1) {
637 		ret = -EINVAL;
638 		goto done;
639 	} else {
640 		saved_offset = offset;
641 		saved_bytes = bytes;
642 		ret = count;
643 	}
644 done:
645 	free_page(addr);
646 	return ret;
647 }
648 
649 /*
650  * Proc rdeeprom read write handler.
651  *
652  * This function is called when the 'rdeeprom' file is opened for reading
653  *
654  * This function can be used to read from a RDEEPROM location.
655  */
656 static ssize_t
mwifiex_rdeeprom_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)657 mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
658 		      size_t count, loff_t *ppos)
659 {
660 	struct mwifiex_private *priv =
661 		(struct mwifiex_private *) file->private_data;
662 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
663 	char *buf = (char *) addr;
664 	int pos = 0, ret = 0, i;
665 	u8 value[MAX_EEPROM_DATA];
666 
667 	if (!buf)
668 		return -ENOMEM;
669 
670 	if (saved_offset == -1) {
671 		/* No command has been given */
672 		pos += snprintf(buf, PAGE_SIZE, "0");
673 		goto done;
674 	}
675 
676 	/* Get command has been given */
677 	ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
678 				  (u16) saved_bytes, value);
679 	if (ret) {
680 		ret = -EINVAL;
681 		goto done;
682 	}
683 
684 	pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
685 
686 	for (i = 0; i < saved_bytes; i++)
687 		pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ", value[i]);
688 
689 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
690 
691 done:
692 	free_page(addr);
693 	return ret;
694 }
695 
696 /* Proc hscfg file write handler
697  * This function can be used to configure the host sleep parameters.
698  */
699 static ssize_t
mwifiex_hscfg_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)700 mwifiex_hscfg_write(struct file *file, const char __user *ubuf,
701 		    size_t count, loff_t *ppos)
702 {
703 	struct mwifiex_private *priv = (void *)file->private_data;
704 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
705 	char *buf = (char *)addr;
706 	size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
707 	int ret, arg_num;
708 	struct mwifiex_ds_hs_cfg hscfg;
709 	int conditions = HS_CFG_COND_DEF;
710 	u32 gpio = HS_CFG_GPIO_DEF, gap = HS_CFG_GAP_DEF;
711 
712 	if (!buf)
713 		return -ENOMEM;
714 
715 	if (copy_from_user(buf, ubuf, buf_size)) {
716 		ret = -EFAULT;
717 		goto done;
718 	}
719 
720 	arg_num = sscanf(buf, "%d %x %x", &conditions, &gpio, &gap);
721 
722 	memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
723 
724 	if (arg_num > 3) {
725 		dev_err(priv->adapter->dev, "Too many arguments\n");
726 		ret = -EINVAL;
727 		goto done;
728 	}
729 
730 	if (arg_num >= 1 && arg_num < 3)
731 		mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
732 				      MWIFIEX_SYNC_CMD, &hscfg);
733 
734 	if (arg_num) {
735 		if (conditions == HS_CFG_CANCEL) {
736 			mwifiex_cancel_hs(priv, MWIFIEX_ASYNC_CMD);
737 			ret = count;
738 			goto done;
739 		}
740 		hscfg.conditions = conditions;
741 	}
742 	if (arg_num >= 2)
743 		hscfg.gpio = gpio;
744 	if (arg_num == 3)
745 		hscfg.gap = gap;
746 
747 	hscfg.is_invoke_hostcmd = false;
748 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
749 			      MWIFIEX_SYNC_CMD, &hscfg);
750 
751 	mwifiex_enable_hs(priv->adapter);
752 	priv->adapter->hs_enabling = false;
753 	ret = count;
754 done:
755 	free_page(addr);
756 	return ret;
757 }
758 
759 /* Proc hscfg file read handler
760  * This function can be used to read host sleep configuration
761  * parameters from driver.
762  */
763 static ssize_t
mwifiex_hscfg_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)764 mwifiex_hscfg_read(struct file *file, char __user *ubuf,
765 		   size_t count, loff_t *ppos)
766 {
767 	struct mwifiex_private *priv = (void *)file->private_data;
768 	unsigned long addr = get_zeroed_page(GFP_KERNEL);
769 	char *buf = (char *)addr;
770 	int pos, ret;
771 	struct mwifiex_ds_hs_cfg hscfg;
772 
773 	if (!buf)
774 		return -ENOMEM;
775 
776 	mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
777 			      MWIFIEX_SYNC_CMD, &hscfg);
778 
779 	pos = snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", hscfg.conditions,
780 		       hscfg.gpio, hscfg.gap);
781 
782 	ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
783 
784 	free_page(addr);
785 	return ret;
786 }
787 
788 #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
789 	if (!debugfs_create_file(#name, 0644, priv->dfs_dev_dir,        \
790 			priv, &mwifiex_dfs_##name##_fops))              \
791 		return;                                                 \
792 } while (0);
793 
794 #define MWIFIEX_DFS_FILE_OPS(name)                                      \
795 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
796 	.read = mwifiex_##name##_read,                                  \
797 	.write = mwifiex_##name##_write,                                \
798 	.open = simple_open,                                            \
799 };
800 
801 #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
802 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
803 	.read = mwifiex_##name##_read,                                  \
804 	.open = simple_open,                                            \
805 };
806 
807 #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
808 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
809 	.write = mwifiex_##name##_write,                                \
810 	.open = simple_open,                                            \
811 };
812 
813 
814 MWIFIEX_DFS_FILE_READ_OPS(info);
815 MWIFIEX_DFS_FILE_READ_OPS(debug);
816 MWIFIEX_DFS_FILE_READ_OPS(getlog);
817 MWIFIEX_DFS_FILE_READ_OPS(fw_dump);
818 MWIFIEX_DFS_FILE_OPS(regrdwr);
819 MWIFIEX_DFS_FILE_OPS(rdeeprom);
820 MWIFIEX_DFS_FILE_OPS(hscfg);
821 
822 /*
823  * This function creates the debug FS directory structure and the files.
824  */
825 void
mwifiex_dev_debugfs_init(struct mwifiex_private * priv)826 mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
827 {
828 	if (!mwifiex_dfs_dir || !priv)
829 		return;
830 
831 	priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
832 					       mwifiex_dfs_dir);
833 
834 	if (!priv->dfs_dev_dir)
835 		return;
836 
837 	MWIFIEX_DFS_ADD_FILE(info);
838 	MWIFIEX_DFS_ADD_FILE(debug);
839 	MWIFIEX_DFS_ADD_FILE(getlog);
840 	MWIFIEX_DFS_ADD_FILE(regrdwr);
841 	MWIFIEX_DFS_ADD_FILE(rdeeprom);
842 	MWIFIEX_DFS_ADD_FILE(fw_dump);
843 	MWIFIEX_DFS_ADD_FILE(hscfg);
844 }
845 
846 /*
847  * This function removes the debug FS directory structure and the files.
848  */
849 void
mwifiex_dev_debugfs_remove(struct mwifiex_private * priv)850 mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
851 {
852 	if (!priv)
853 		return;
854 
855 	debugfs_remove_recursive(priv->dfs_dev_dir);
856 }
857 
858 /*
859  * This function creates the top level proc directory.
860  */
861 void
mwifiex_debugfs_init(void)862 mwifiex_debugfs_init(void)
863 {
864 	if (!mwifiex_dfs_dir)
865 		mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
866 }
867 
868 /*
869  * This function removes the top level proc directory.
870  */
871 void
mwifiex_debugfs_remove(void)872 mwifiex_debugfs_remove(void)
873 {
874 	if (mwifiex_dfs_dir)
875 		debugfs_remove(mwifiex_dfs_dir);
876 }
877