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