fe1eb74d6703218a2f6fe79b661edfd2e298d584
[firefly-linux-kernel-4.4.55.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 /* 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
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);
224                 p += sprintf(p, "bssid=\"%pM\"\n", info.bssid);
225                 p += sprintf(p, "channel=\"%d\"\n", (int) info.bss_chan);
226                 p += sprintf(p, "country_code = \"%s\"\n", info.country_code);
227
228                 netdev_for_each_mc_addr(ha, netdev)
229                         p += sprintf(p, "multicast_address[%d]=\"%pM\"\n",
230                                         i++, ha->addr);
231         }
232
233         p += sprintf(p, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
234         p += sprintf(p, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
235         p += sprintf(p, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
236         p += sprintf(p, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
237         p += sprintf(p, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
238         p += sprintf(p, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
239         p += sprintf(p, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
240         p += sprintf(p, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
241         p += sprintf(p, "carrier %s\n", ((netif_carrier_ok(priv->netdev))
242                                          ? "on" : "off"));
243         p += sprintf(p, "tx queue");
244         for (i = 0; i < netdev->num_tx_queues; i++) {
245                 txq = netdev_get_tx_queue(netdev, i);
246                 p += sprintf(p, " %d:%s", i, netif_tx_queue_stopped(txq) ?
247                              "stopped" : "started");
248         }
249         p += sprintf(p, "\n");
250
251         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
252                                       (unsigned long) p - page);
253
254 free_and_exit:
255         free_page(page);
256         return ret;
257 }
258
259 /*
260  * Proc firmware dump read handler.
261  *
262  * This function is called when the 'fw_dump' file is opened for
263  * reading.
264  * This function dumps firmware memory in different files
265  * (ex. DTCM, ITCM, SQRAM etc.) based on the the segments for
266  * debugging.
267  */
268 static ssize_t
269 mwifiex_fw_dump_read(struct file *file, char __user *ubuf,
270                      size_t count, loff_t *ppos)
271 {
272         struct mwifiex_private *priv = file->private_data;
273
274         if (!priv->adapter->if_ops.fw_dump)
275                 return -EIO;
276
277         priv->adapter->if_ops.fw_dump(priv->adapter);
278
279         return 0;
280 }
281
282 /*
283  * Proc getlog file read handler.
284  *
285  * This function is called when the 'getlog' file is opened for reading
286  * It prints the following log information -
287  *      - Number of multicast Tx frames
288  *      - Number of failed packets
289  *      - Number of Tx retries
290  *      - Number of multicast Tx retries
291  *      - Number of duplicate frames
292  *      - Number of RTS successes
293  *      - Number of RTS failures
294  *      - Number of ACK failures
295  *      - Number of fragmented Rx frames
296  *      - Number of multicast Rx frames
297  *      - Number of FCS errors
298  *      - Number of Tx frames
299  *      - WEP ICV error counts
300  *      - Number of received beacons
301  *      - Number of missed beacons
302  */
303 static ssize_t
304 mwifiex_getlog_read(struct file *file, char __user *ubuf,
305                     size_t count, loff_t *ppos)
306 {
307         struct mwifiex_private *priv =
308                 (struct mwifiex_private *) file->private_data;
309         unsigned long page = get_zeroed_page(GFP_KERNEL);
310         char *p = (char *) page;
311         ssize_t ret;
312         struct mwifiex_ds_get_stats stats;
313
314         if (!p)
315                 return -ENOMEM;
316
317         memset(&stats, 0, sizeof(stats));
318         ret = mwifiex_get_stats_info(priv, &stats);
319         if (ret)
320                 goto free_and_exit;
321
322         p += sprintf(p, "\n"
323                      "mcasttxframe     %u\n"
324                      "failed           %u\n"
325                      "retry            %u\n"
326                      "multiretry       %u\n"
327                      "framedup         %u\n"
328                      "rtssuccess       %u\n"
329                      "rtsfailure       %u\n"
330                      "ackfailure       %u\n"
331                      "rxfrag           %u\n"
332                      "mcastrxframe     %u\n"
333                      "fcserror         %u\n"
334                      "txframe          %u\n"
335                      "wepicverrcnt-1   %u\n"
336                      "wepicverrcnt-2   %u\n"
337                      "wepicverrcnt-3   %u\n"
338                      "wepicverrcnt-4   %u\n"
339                      "bcn_rcv_cnt   %u\n"
340                      "bcn_miss_cnt   %u\n",
341                      stats.mcast_tx_frame,
342                      stats.failed,
343                      stats.retry,
344                      stats.multi_retry,
345                      stats.frame_dup,
346                      stats.rts_success,
347                      stats.rts_failure,
348                      stats.ack_failure,
349                      stats.rx_frag,
350                      stats.mcast_rx_frame,
351                      stats.fcs_error,
352                      stats.tx_frame,
353                      stats.wep_icv_error[0],
354                      stats.wep_icv_error[1],
355                      stats.wep_icv_error[2],
356                      stats.wep_icv_error[3],
357                      stats.bcn_rcv_cnt,
358                      stats.bcn_miss_cnt);
359
360
361         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
362                                       (unsigned long) p - page);
363
364 free_and_exit:
365         free_page(page);
366         return ret;
367 }
368
369 /* Sysfs histogram file read handler.
370  *
371  * This function is called when the 'histogram' file is opened for reading
372  * It prints the following histogram information -
373  *      - Number of histogram samples
374  *      - Receive packet number of each rx_rate
375  *      - Receive packet number of each snr
376  *      - Receive packet number of each nosie_flr
377  *      - Receive packet number of each signal streath
378  */
379 static ssize_t
380 mwifiex_histogram_read(struct file *file, char __user *ubuf,
381                        size_t count, loff_t *ppos)
382 {
383         struct mwifiex_private *priv =
384                 (struct mwifiex_private *)file->private_data;
385         ssize_t ret;
386         struct mwifiex_histogram_data *phist_data;
387         int i, value;
388         unsigned long page = get_zeroed_page(GFP_KERNEL);
389         char *p = (char *)page;
390
391         if (!p)
392                 return -ENOMEM;
393
394         if (!priv || !priv->hist_data)
395                 return -EFAULT;
396         phist_data = priv->hist_data;
397
398         p += sprintf(p, "\n"
399                      "total samples = %d\n",
400                      atomic_read(&phist_data->num_samples));
401
402         p += sprintf(p, "rx rates (in Mbps): 0=1M   1=2M");
403         p += sprintf(p, "2=5.5M  3=11M   4=6M   5=9M  6=12M\n");
404         p += sprintf(p, "7=18M  8=24M  9=36M  10=48M  11=54M");
405         p += sprintf(p, "12-27=MCS0-15(BW20) 28-43=MCS0-15(BW40)\n");
406
407         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
408                 p += sprintf(p, "44-53=MCS0-9(VHT:BW20)");
409                 p += sprintf(p, "54-63=MCS0-9(VHT:BW40)");
410                 p += sprintf(p, "64-73=MCS0-9(VHT:BW80)\n\n");
411         } else {
412                 p += sprintf(p, "\n");
413         }
414
415         for (i = 0; i < MWIFIEX_MAX_RX_RATES; i++) {
416                 value = atomic_read(&phist_data->rx_rate[i]);
417                 if (value)
418                         p += sprintf(p, "rx_rate[%02d] = %d\n", i, value);
419         }
420
421         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info)) {
422                 for (i = MWIFIEX_MAX_RX_RATES; i < MWIFIEX_MAX_AC_RX_RATES;
423                      i++) {
424                         value = atomic_read(&phist_data->rx_rate[i]);
425                         if (value)
426                                 p += sprintf(p, "rx_rate[%02d] = %d\n",
427                                            i, value);
428                 }
429         }
430
431         for (i = 0; i < MWIFIEX_MAX_SNR; i++) {
432                 value =  atomic_read(&phist_data->snr[i]);
433                 if (value)
434                         p += sprintf(p, "snr[%02ddB] = %d\n", i, value);
435         }
436         for (i = 0; i < MWIFIEX_MAX_NOISE_FLR; i++) {
437                 value = atomic_read(&phist_data->noise_flr[i]);
438                 if (value)
439                         p += sprintf(p, "noise_flr[-%02ddBm] = %d\n",
440                                 (int)(i-128), value);
441         }
442         for (i = 0; i < MWIFIEX_MAX_SIG_STRENGTH; i++) {
443                 value = atomic_read(&phist_data->sig_str[i]);
444                 if (value)
445                         p += sprintf(p, "sig_strength[-%02ddBm] = %d\n",
446                                 i, value);
447         }
448
449         ret = simple_read_from_buffer(ubuf, count, ppos, (char *)page,
450                                       (unsigned long)p - page);
451
452         return ret;
453 }
454
455 static ssize_t
456 mwifiex_histogram_write(struct file *file, const char __user *ubuf,
457                         size_t count, loff_t *ppos)
458 {
459         struct mwifiex_private *priv = (void *)file->private_data;
460
461         if (priv && priv->hist_data)
462                 mwifiex_hist_data_reset(priv);
463         return 0;
464 }
465
466 static struct mwifiex_debug_info info;
467
468 /*
469  * Proc debug file read handler.
470  *
471  * This function is called when the 'debug' file is opened for reading
472  * It prints the following log information -
473  *      - Interrupt count
474  *      - WMM AC VO packets count
475  *      - WMM AC VI packets count
476  *      - WMM AC BE packets count
477  *      - WMM AC BK packets count
478  *      - Maximum Tx buffer size
479  *      - Tx buffer size
480  *      - Current Tx buffer size
481  *      - Power Save mode
482  *      - Power Save state
483  *      - Deep Sleep status
484  *      - Device wakeup required status
485  *      - Number of wakeup tries
486  *      - Host Sleep configured status
487  *      - Host Sleep activated status
488  *      - Number of Tx timeouts
489  *      - Number of command timeouts
490  *      - Last timed out command ID
491  *      - Last timed out command action
492  *      - Last command ID
493  *      - Last command action
494  *      - Last command index
495  *      - Last command response ID
496  *      - Last command response index
497  *      - Last event
498  *      - Last event index
499  *      - Number of host to card command failures
500  *      - Number of sleep confirm command failures
501  *      - Number of host to card data failure
502  *      - Number of deauthentication events
503  *      - Number of disassociation events
504  *      - Number of link lost events
505  *      - Number of deauthentication commands
506  *      - Number of association success commands
507  *      - Number of association failure commands
508  *      - Number of commands sent
509  *      - Number of data packets sent
510  *      - Number of command responses received
511  *      - Number of events received
512  *      - Tx BA stream table (TID, RA)
513  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
514  */
515 static ssize_t
516 mwifiex_debug_read(struct file *file, char __user *ubuf,
517                    size_t count, loff_t *ppos)
518 {
519         struct mwifiex_private *priv =
520                 (struct mwifiex_private *) file->private_data;
521         struct mwifiex_debug_data *d = &items[0];
522         unsigned long page = get_zeroed_page(GFP_KERNEL);
523         char *p = (char *) page;
524         ssize_t ret;
525         size_t size, addr;
526         long val;
527         int i, j;
528
529         if (!p)
530                 return -ENOMEM;
531
532         ret = mwifiex_get_debug_info(priv, &info);
533         if (ret)
534                 goto free_and_exit;
535
536         for (i = 0; i < num_of_items; i++) {
537                 p += sprintf(p, "%s=", d[i].name);
538
539                 size = d[i].size / d[i].num;
540
541                 if (i < (num_of_items - 3))
542                         addr = d[i].addr + (size_t) &info;
543                 else /* The last 3 items are struct mwifiex_adapter variables */
544                         addr = d[i].addr + (size_t) priv->adapter;
545
546                 for (j = 0; j < d[i].num; j++) {
547                         switch (size) {
548                         case 1:
549                                 val = *((u8 *) addr);
550                                 break;
551                         case 2:
552                                 val = *((u16 *) addr);
553                                 break;
554                         case 4:
555                                 val = *((u32 *) addr);
556                                 break;
557                         case 8:
558                                 val = *((long long *) addr);
559                                 break;
560                         default:
561                                 val = -1;
562                                 break;
563                         }
564
565                         p += sprintf(p, "%#lx ", val);
566                         addr += size;
567                 }
568
569                 p += sprintf(p, "\n");
570         }
571
572         if (info.tx_tbl_num) {
573                 p += sprintf(p, "Tx BA stream table:\n");
574                 for (i = 0; i < info.tx_tbl_num; i++)
575                         p += sprintf(p, "tid = %d, ra = %pM\n",
576                                      info.tx_tbl[i].tid, info.tx_tbl[i].ra);
577         }
578
579         if (info.rx_tbl_num) {
580                 p += sprintf(p, "Rx reorder table:\n");
581                 for (i = 0; i < info.rx_tbl_num; i++) {
582                         p += sprintf(p, "tid = %d, ta = %pM, "
583                                      "start_win = %d, "
584                                      "win_size = %d, buffer: ",
585                                      info.rx_tbl[i].tid,
586                                      info.rx_tbl[i].ta,
587                                      info.rx_tbl[i].start_win,
588                                      info.rx_tbl[i].win_size);
589
590                         for (j = 0; j < info.rx_tbl[i].win_size; j++)
591                                 p += sprintf(p, "%c ",
592                                              info.rx_tbl[i].buffer[j] ?
593                                              '1' : '0');
594
595                         p += sprintf(p, "\n");
596                 }
597         }
598
599         if (info.tdls_peer_num) {
600                 p += sprintf(p, "TDLS peer table:\n");
601                 for (i = 0; i < info.tdls_peer_num; i++) {
602                         p += sprintf(p, "peer = %pM",
603                                      info.tdls_list[i].peer_addr);
604                         p += sprintf(p, "\n");
605                 }
606         }
607
608         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
609                                       (unsigned long) p - page);
610
611 free_and_exit:
612         free_page(page);
613         return ret;
614 }
615
616 static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
617
618 /*
619  * Proc regrdwr file write handler.
620  *
621  * This function is called when the 'regrdwr' file is opened for writing
622  *
623  * This function can be used to write to a register.
624  */
625 static ssize_t
626 mwifiex_regrdwr_write(struct file *file,
627                       const char __user *ubuf, size_t count, loff_t *ppos)
628 {
629         unsigned long addr = get_zeroed_page(GFP_KERNEL);
630         char *buf = (char *) addr;
631         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
632         int ret;
633         u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
634
635         if (!buf)
636                 return -ENOMEM;
637
638
639         if (copy_from_user(buf, ubuf, buf_size)) {
640                 ret = -EFAULT;
641                 goto done;
642         }
643
644         sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value);
645
646         if (reg_type == 0 || reg_offset == 0) {
647                 ret = -EINVAL;
648                 goto done;
649         } else {
650                 saved_reg_type = reg_type;
651                 saved_reg_offset = reg_offset;
652                 saved_reg_value = reg_value;
653                 ret = count;
654         }
655 done:
656         free_page(addr);
657         return ret;
658 }
659
660 /*
661  * Proc regrdwr file read handler.
662  *
663  * This function is called when the 'regrdwr' file is opened for reading
664  *
665  * This function can be used to read from a register.
666  */
667 static ssize_t
668 mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
669                      size_t count, loff_t *ppos)
670 {
671         struct mwifiex_private *priv =
672                 (struct mwifiex_private *) file->private_data;
673         unsigned long addr = get_zeroed_page(GFP_KERNEL);
674         char *buf = (char *) addr;
675         int pos = 0, ret = 0;
676         u32 reg_value;
677
678         if (!buf)
679                 return -ENOMEM;
680
681         if (!saved_reg_type) {
682                 /* No command has been given */
683                 pos += snprintf(buf, PAGE_SIZE, "0");
684                 goto done;
685         }
686         /* Set command has been given */
687         if (saved_reg_value != UINT_MAX) {
688                 ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
689                                         saved_reg_value);
690
691                 pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
692                                 saved_reg_type, saved_reg_offset,
693                                 saved_reg_value);
694
695                 ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
696
697                 goto done;
698         }
699         /* Get command has been given */
700         ret = mwifiex_reg_read(priv, saved_reg_type,
701                                saved_reg_offset, &reg_value);
702         if (ret) {
703                 ret = -EINVAL;
704                 goto done;
705         }
706
707         pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
708                         saved_reg_offset, reg_value);
709
710         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
711
712 done:
713         free_page(addr);
714         return ret;
715 }
716
717 static u32 saved_offset = -1, saved_bytes = -1;
718
719 /*
720  * Proc rdeeprom file write handler.
721  *
722  * This function is called when the 'rdeeprom' file is opened for writing
723  *
724  * This function can be used to write to a RDEEPROM location.
725  */
726 static ssize_t
727 mwifiex_rdeeprom_write(struct file *file,
728                        const char __user *ubuf, size_t count, loff_t *ppos)
729 {
730         unsigned long addr = get_zeroed_page(GFP_KERNEL);
731         char *buf = (char *) addr;
732         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
733         int ret = 0;
734         int offset = -1, bytes = -1;
735
736         if (!buf)
737                 return -ENOMEM;
738
739
740         if (copy_from_user(buf, ubuf, buf_size)) {
741                 ret = -EFAULT;
742                 goto done;
743         }
744
745         sscanf(buf, "%d %d", &offset, &bytes);
746
747         if (offset == -1 || bytes == -1) {
748                 ret = -EINVAL;
749                 goto done;
750         } else {
751                 saved_offset = offset;
752                 saved_bytes = bytes;
753                 ret = count;
754         }
755 done:
756         free_page(addr);
757         return ret;
758 }
759
760 /*
761  * Proc rdeeprom read write handler.
762  *
763  * This function is called when the 'rdeeprom' file is opened for reading
764  *
765  * This function can be used to read from a RDEEPROM location.
766  */
767 static ssize_t
768 mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
769                       size_t count, loff_t *ppos)
770 {
771         struct mwifiex_private *priv =
772                 (struct mwifiex_private *) file->private_data;
773         unsigned long addr = get_zeroed_page(GFP_KERNEL);
774         char *buf = (char *) addr;
775         int pos = 0, ret = 0, i;
776         u8 value[MAX_EEPROM_DATA];
777
778         if (!buf)
779                 return -ENOMEM;
780
781         if (saved_offset == -1) {
782                 /* No command has been given */
783                 pos += snprintf(buf, PAGE_SIZE, "0");
784                 goto done;
785         }
786
787         /* Get command has been given */
788         ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
789                                   (u16) saved_bytes, value);
790         if (ret) {
791                 ret = -EINVAL;
792                 goto done;
793         }
794
795         pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
796
797         for (i = 0; i < saved_bytes; i++)
798                 pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ", value[i]);
799
800         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
801
802 done:
803         free_page(addr);
804         return ret;
805 }
806
807 /* Proc hscfg file write handler
808  * This function can be used to configure the host sleep parameters.
809  */
810 static ssize_t
811 mwifiex_hscfg_write(struct file *file, const char __user *ubuf,
812                     size_t count, loff_t *ppos)
813 {
814         struct mwifiex_private *priv = (void *)file->private_data;
815         unsigned long addr = get_zeroed_page(GFP_KERNEL);
816         char *buf = (char *)addr;
817         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
818         int ret, arg_num;
819         struct mwifiex_ds_hs_cfg hscfg;
820         int conditions = HS_CFG_COND_DEF;
821         u32 gpio = HS_CFG_GPIO_DEF, gap = HS_CFG_GAP_DEF;
822
823         if (!buf)
824                 return -ENOMEM;
825
826         if (copy_from_user(buf, ubuf, buf_size)) {
827                 ret = -EFAULT;
828                 goto done;
829         }
830
831         arg_num = sscanf(buf, "%d %x %x", &conditions, &gpio, &gap);
832
833         memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
834
835         if (arg_num > 3) {
836                 dev_err(priv->adapter->dev, "Too many arguments\n");
837                 ret = -EINVAL;
838                 goto done;
839         }
840
841         if (arg_num >= 1 && arg_num < 3)
842                 mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
843                                       MWIFIEX_SYNC_CMD, &hscfg);
844
845         if (arg_num) {
846                 if (conditions == HS_CFG_CANCEL) {
847                         mwifiex_cancel_hs(priv, MWIFIEX_ASYNC_CMD);
848                         ret = count;
849                         goto done;
850                 }
851                 hscfg.conditions = conditions;
852         }
853         if (arg_num >= 2)
854                 hscfg.gpio = gpio;
855         if (arg_num == 3)
856                 hscfg.gap = gap;
857
858         hscfg.is_invoke_hostcmd = false;
859         mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
860                               MWIFIEX_SYNC_CMD, &hscfg);
861
862         mwifiex_enable_hs(priv->adapter);
863         priv->adapter->hs_enabling = false;
864         ret = count;
865 done:
866         free_page(addr);
867         return ret;
868 }
869
870 /* Proc hscfg file read handler
871  * This function can be used to read host sleep configuration
872  * parameters from driver.
873  */
874 static ssize_t
875 mwifiex_hscfg_read(struct file *file, char __user *ubuf,
876                    size_t count, loff_t *ppos)
877 {
878         struct mwifiex_private *priv = (void *)file->private_data;
879         unsigned long addr = get_zeroed_page(GFP_KERNEL);
880         char *buf = (char *)addr;
881         int pos, ret;
882         struct mwifiex_ds_hs_cfg hscfg;
883
884         if (!buf)
885                 return -ENOMEM;
886
887         mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
888                               MWIFIEX_SYNC_CMD, &hscfg);
889
890         pos = snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", hscfg.conditions,
891                        hscfg.gpio, hscfg.gap);
892
893         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
894
895         free_page(addr);
896         return ret;
897 }
898
899 #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
900         if (!debugfs_create_file(#name, 0644, priv->dfs_dev_dir,        \
901                         priv, &mwifiex_dfs_##name##_fops))              \
902                 return;                                                 \
903 } while (0);
904
905 #define MWIFIEX_DFS_FILE_OPS(name)                                      \
906 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
907         .read = mwifiex_##name##_read,                                  \
908         .write = mwifiex_##name##_write,                                \
909         .open = simple_open,                                            \
910 };
911
912 #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
913 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
914         .read = mwifiex_##name##_read,                                  \
915         .open = simple_open,                                            \
916 };
917
918 #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
919 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
920         .write = mwifiex_##name##_write,                                \
921         .open = simple_open,                                            \
922 };
923
924
925 MWIFIEX_DFS_FILE_READ_OPS(info);
926 MWIFIEX_DFS_FILE_READ_OPS(debug);
927 MWIFIEX_DFS_FILE_READ_OPS(getlog);
928 MWIFIEX_DFS_FILE_READ_OPS(fw_dump);
929 MWIFIEX_DFS_FILE_OPS(regrdwr);
930 MWIFIEX_DFS_FILE_OPS(rdeeprom);
931 MWIFIEX_DFS_FILE_OPS(hscfg);
932 MWIFIEX_DFS_FILE_OPS(histogram);
933
934 /*
935  * This function creates the debug FS directory structure and the files.
936  */
937 void
938 mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
939 {
940         if (!mwifiex_dfs_dir || !priv)
941                 return;
942
943         priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
944                                                mwifiex_dfs_dir);
945
946         if (!priv->dfs_dev_dir)
947                 return;
948
949         MWIFIEX_DFS_ADD_FILE(info);
950         MWIFIEX_DFS_ADD_FILE(debug);
951         MWIFIEX_DFS_ADD_FILE(getlog);
952         MWIFIEX_DFS_ADD_FILE(regrdwr);
953         MWIFIEX_DFS_ADD_FILE(rdeeprom);
954         MWIFIEX_DFS_ADD_FILE(fw_dump);
955         MWIFIEX_DFS_ADD_FILE(hscfg);
956         MWIFIEX_DFS_ADD_FILE(histogram);
957 }
958
959 /*
960  * This function removes the debug FS directory structure and the files.
961  */
962 void
963 mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
964 {
965         if (!priv)
966                 return;
967
968         debugfs_remove_recursive(priv->dfs_dev_dir);
969 }
970
971 /*
972  * This function creates the top level proc directory.
973  */
974 void
975 mwifiex_debugfs_init(void)
976 {
977         if (!mwifiex_dfs_dir)
978                 mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
979 }
980
981 /*
982  * This function removes the top level proc directory.
983  */
984 void
985 mwifiex_debugfs_remove(void)
986 {
987         if (mwifiex_dfs_dir)
988                 debugfs_remove(mwifiex_dfs_dir);
989 }