Merge tag 'iwlwifi-next-for-kalle-2014-12-30' of https://git.kernel.org/pub/scm/linux...
[cascardo/linux.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  */
301 static ssize_t
302 mwifiex_getlog_read(struct file *file, char __user *ubuf,
303                     size_t count, loff_t *ppos)
304 {
305         struct mwifiex_private *priv =
306                 (struct mwifiex_private *) file->private_data;
307         unsigned long page = get_zeroed_page(GFP_KERNEL);
308         char *p = (char *) page;
309         ssize_t ret;
310         struct mwifiex_ds_get_stats stats;
311
312         if (!p)
313                 return -ENOMEM;
314
315         memset(&stats, 0, sizeof(stats));
316         ret = mwifiex_get_stats_info(priv, &stats);
317         if (ret)
318                 goto free_and_exit;
319
320         p += sprintf(p, "\n"
321                      "mcasttxframe     %u\n"
322                      "failed           %u\n"
323                      "retry            %u\n"
324                      "multiretry       %u\n"
325                      "framedup         %u\n"
326                      "rtssuccess       %u\n"
327                      "rtsfailure       %u\n"
328                      "ackfailure       %u\n"
329                      "rxfrag           %u\n"
330                      "mcastrxframe     %u\n"
331                      "fcserror         %u\n"
332                      "txframe          %u\n"
333                      "wepicverrcnt-1   %u\n"
334                      "wepicverrcnt-2   %u\n"
335                      "wepicverrcnt-3   %u\n"
336                      "wepicverrcnt-4   %u\n",
337                      stats.mcast_tx_frame,
338                      stats.failed,
339                      stats.retry,
340                      stats.multi_retry,
341                      stats.frame_dup,
342                      stats.rts_success,
343                      stats.rts_failure,
344                      stats.ack_failure,
345                      stats.rx_frag,
346                      stats.mcast_rx_frame,
347                      stats.fcs_error,
348                      stats.tx_frame,
349                      stats.wep_icv_error[0],
350                      stats.wep_icv_error[1],
351                      stats.wep_icv_error[2],
352                      stats.wep_icv_error[3]);
353
354
355         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
356                                       (unsigned long) p - page);
357
358 free_and_exit:
359         free_page(page);
360         return ret;
361 }
362
363 static struct mwifiex_debug_info info;
364
365 /*
366  * Proc debug file read handler.
367  *
368  * This function is called when the 'debug' file is opened for reading
369  * It prints the following log information -
370  *      - Interrupt count
371  *      - WMM AC VO packets count
372  *      - WMM AC VI packets count
373  *      - WMM AC BE packets count
374  *      - WMM AC BK packets count
375  *      - Maximum Tx buffer size
376  *      - Tx buffer size
377  *      - Current Tx buffer size
378  *      - Power Save mode
379  *      - Power Save state
380  *      - Deep Sleep status
381  *      - Device wakeup required status
382  *      - Number of wakeup tries
383  *      - Host Sleep configured status
384  *      - Host Sleep activated status
385  *      - Number of Tx timeouts
386  *      - Number of command timeouts
387  *      - Last timed out command ID
388  *      - Last timed out command action
389  *      - Last command ID
390  *      - Last command action
391  *      - Last command index
392  *      - Last command response ID
393  *      - Last command response index
394  *      - Last event
395  *      - Last event index
396  *      - Number of host to card command failures
397  *      - Number of sleep confirm command failures
398  *      - Number of host to card data failure
399  *      - Number of deauthentication events
400  *      - Number of disassociation events
401  *      - Number of link lost events
402  *      - Number of deauthentication commands
403  *      - Number of association success commands
404  *      - Number of association failure commands
405  *      - Number of commands sent
406  *      - Number of data packets sent
407  *      - Number of command responses received
408  *      - Number of events received
409  *      - Tx BA stream table (TID, RA)
410  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
411  */
412 static ssize_t
413 mwifiex_debug_read(struct file *file, char __user *ubuf,
414                    size_t count, loff_t *ppos)
415 {
416         struct mwifiex_private *priv =
417                 (struct mwifiex_private *) file->private_data;
418         struct mwifiex_debug_data *d = &items[0];
419         unsigned long page = get_zeroed_page(GFP_KERNEL);
420         char *p = (char *) page;
421         ssize_t ret;
422         size_t size, addr;
423         long val;
424         int i, j;
425
426         if (!p)
427                 return -ENOMEM;
428
429         ret = mwifiex_get_debug_info(priv, &info);
430         if (ret)
431                 goto free_and_exit;
432
433         for (i = 0; i < num_of_items; i++) {
434                 p += sprintf(p, "%s=", d[i].name);
435
436                 size = d[i].size / d[i].num;
437
438                 if (i < (num_of_items - 3))
439                         addr = d[i].addr + (size_t) &info;
440                 else /* The last 3 items are struct mwifiex_adapter variables */
441                         addr = d[i].addr + (size_t) priv->adapter;
442
443                 for (j = 0; j < d[i].num; j++) {
444                         switch (size) {
445                         case 1:
446                                 val = *((u8 *) addr);
447                                 break;
448                         case 2:
449                                 val = *((u16 *) addr);
450                                 break;
451                         case 4:
452                                 val = *((u32 *) addr);
453                                 break;
454                         case 8:
455                                 val = *((long long *) addr);
456                                 break;
457                         default:
458                                 val = -1;
459                                 break;
460                         }
461
462                         p += sprintf(p, "%#lx ", val);
463                         addr += size;
464                 }
465
466                 p += sprintf(p, "\n");
467         }
468
469         if (info.tx_tbl_num) {
470                 p += sprintf(p, "Tx BA stream table:\n");
471                 for (i = 0; i < info.tx_tbl_num; i++)
472                         p += sprintf(p, "tid = %d, ra = %pM\n",
473                                      info.tx_tbl[i].tid, info.tx_tbl[i].ra);
474         }
475
476         if (info.rx_tbl_num) {
477                 p += sprintf(p, "Rx reorder table:\n");
478                 for (i = 0; i < info.rx_tbl_num; i++) {
479                         p += sprintf(p, "tid = %d, ta = %pM, "
480                                      "start_win = %d, "
481                                      "win_size = %d, buffer: ",
482                                      info.rx_tbl[i].tid,
483                                      info.rx_tbl[i].ta,
484                                      info.rx_tbl[i].start_win,
485                                      info.rx_tbl[i].win_size);
486
487                         for (j = 0; j < info.rx_tbl[i].win_size; j++)
488                                 p += sprintf(p, "%c ",
489                                              info.rx_tbl[i].buffer[j] ?
490                                              '1' : '0');
491
492                         p += sprintf(p, "\n");
493                 }
494         }
495
496         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
497                                       (unsigned long) p - page);
498
499 free_and_exit:
500         free_page(page);
501         return ret;
502 }
503
504 static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
505
506 /*
507  * Proc regrdwr file write handler.
508  *
509  * This function is called when the 'regrdwr' file is opened for writing
510  *
511  * This function can be used to write to a register.
512  */
513 static ssize_t
514 mwifiex_regrdwr_write(struct file *file,
515                       const char __user *ubuf, size_t count, loff_t *ppos)
516 {
517         unsigned long addr = get_zeroed_page(GFP_KERNEL);
518         char *buf = (char *) addr;
519         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
520         int ret;
521         u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
522
523         if (!buf)
524                 return -ENOMEM;
525
526
527         if (copy_from_user(buf, ubuf, buf_size)) {
528                 ret = -EFAULT;
529                 goto done;
530         }
531
532         sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value);
533
534         if (reg_type == 0 || reg_offset == 0) {
535                 ret = -EINVAL;
536                 goto done;
537         } else {
538                 saved_reg_type = reg_type;
539                 saved_reg_offset = reg_offset;
540                 saved_reg_value = reg_value;
541                 ret = count;
542         }
543 done:
544         free_page(addr);
545         return ret;
546 }
547
548 /*
549  * Proc regrdwr file read handler.
550  *
551  * This function is called when the 'regrdwr' file is opened for reading
552  *
553  * This function can be used to read from a register.
554  */
555 static ssize_t
556 mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
557                      size_t count, loff_t *ppos)
558 {
559         struct mwifiex_private *priv =
560                 (struct mwifiex_private *) file->private_data;
561         unsigned long addr = get_zeroed_page(GFP_KERNEL);
562         char *buf = (char *) addr;
563         int pos = 0, ret = 0;
564         u32 reg_value;
565
566         if (!buf)
567                 return -ENOMEM;
568
569         if (!saved_reg_type) {
570                 /* No command has been given */
571                 pos += snprintf(buf, PAGE_SIZE, "0");
572                 goto done;
573         }
574         /* Set command has been given */
575         if (saved_reg_value != UINT_MAX) {
576                 ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
577                                         saved_reg_value);
578
579                 pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
580                                 saved_reg_type, saved_reg_offset,
581                                 saved_reg_value);
582
583                 ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
584
585                 goto done;
586         }
587         /* Get command has been given */
588         ret = mwifiex_reg_read(priv, saved_reg_type,
589                                saved_reg_offset, &reg_value);
590         if (ret) {
591                 ret = -EINVAL;
592                 goto done;
593         }
594
595         pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
596                         saved_reg_offset, reg_value);
597
598         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
599
600 done:
601         free_page(addr);
602         return ret;
603 }
604
605 static u32 saved_offset = -1, saved_bytes = -1;
606
607 /*
608  * Proc rdeeprom file write handler.
609  *
610  * This function is called when the 'rdeeprom' file is opened for writing
611  *
612  * This function can be used to write to a RDEEPROM location.
613  */
614 static ssize_t
615 mwifiex_rdeeprom_write(struct file *file,
616                        const char __user *ubuf, size_t count, loff_t *ppos)
617 {
618         unsigned long addr = get_zeroed_page(GFP_KERNEL);
619         char *buf = (char *) addr;
620         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
621         int ret = 0;
622         int offset = -1, bytes = -1;
623
624         if (!buf)
625                 return -ENOMEM;
626
627
628         if (copy_from_user(buf, ubuf, buf_size)) {
629                 ret = -EFAULT;
630                 goto done;
631         }
632
633         sscanf(buf, "%d %d", &offset, &bytes);
634
635         if (offset == -1 || bytes == -1) {
636                 ret = -EINVAL;
637                 goto done;
638         } else {
639                 saved_offset = offset;
640                 saved_bytes = bytes;
641                 ret = count;
642         }
643 done:
644         free_page(addr);
645         return ret;
646 }
647
648 /*
649  * Proc rdeeprom read write handler.
650  *
651  * This function is called when the 'rdeeprom' file is opened for reading
652  *
653  * This function can be used to read from a RDEEPROM location.
654  */
655 static ssize_t
656 mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
657                       size_t count, loff_t *ppos)
658 {
659         struct mwifiex_private *priv =
660                 (struct mwifiex_private *) file->private_data;
661         unsigned long addr = get_zeroed_page(GFP_KERNEL);
662         char *buf = (char *) addr;
663         int pos = 0, ret = 0, i;
664         u8 value[MAX_EEPROM_DATA];
665
666         if (!buf)
667                 return -ENOMEM;
668
669         if (saved_offset == -1) {
670                 /* No command has been given */
671                 pos += snprintf(buf, PAGE_SIZE, "0");
672                 goto done;
673         }
674
675         /* Get command has been given */
676         ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
677                                   (u16) saved_bytes, value);
678         if (ret) {
679                 ret = -EINVAL;
680                 goto done;
681         }
682
683         pos += snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
684
685         for (i = 0; i < saved_bytes; i++)
686                 pos += snprintf(buf + strlen(buf), PAGE_SIZE, "%d ", value[i]);
687
688         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
689
690 done:
691         free_page(addr);
692         return ret;
693 }
694
695 /* Proc hscfg file write handler
696  * This function can be used to configure the host sleep parameters.
697  */
698 static ssize_t
699 mwifiex_hscfg_write(struct file *file, const char __user *ubuf,
700                     size_t count, loff_t *ppos)
701 {
702         struct mwifiex_private *priv = (void *)file->private_data;
703         unsigned long addr = get_zeroed_page(GFP_KERNEL);
704         char *buf = (char *)addr;
705         size_t buf_size = min_t(size_t, count, PAGE_SIZE - 1);
706         int ret, arg_num;
707         struct mwifiex_ds_hs_cfg hscfg;
708         int conditions = HS_CFG_COND_DEF;
709         u32 gpio = HS_CFG_GPIO_DEF, gap = HS_CFG_GAP_DEF;
710
711         if (!buf)
712                 return -ENOMEM;
713
714         if (copy_from_user(buf, ubuf, buf_size)) {
715                 ret = -EFAULT;
716                 goto done;
717         }
718
719         arg_num = sscanf(buf, "%d %x %x", &conditions, &gpio, &gap);
720
721         memset(&hscfg, 0, sizeof(struct mwifiex_ds_hs_cfg));
722
723         if (arg_num > 3) {
724                 dev_err(priv->adapter->dev, "Too many arguments\n");
725                 ret = -EINVAL;
726                 goto done;
727         }
728
729         if (arg_num >= 1 && arg_num < 3)
730                 mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
731                                       MWIFIEX_SYNC_CMD, &hscfg);
732
733         if (arg_num) {
734                 if (conditions == HS_CFG_CANCEL) {
735                         mwifiex_cancel_hs(priv, MWIFIEX_ASYNC_CMD);
736                         ret = count;
737                         goto done;
738                 }
739                 hscfg.conditions = conditions;
740         }
741         if (arg_num >= 2)
742                 hscfg.gpio = gpio;
743         if (arg_num == 3)
744                 hscfg.gap = gap;
745
746         hscfg.is_invoke_hostcmd = false;
747         mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_SET,
748                               MWIFIEX_SYNC_CMD, &hscfg);
749
750         mwifiex_enable_hs(priv->adapter);
751         priv->adapter->hs_enabling = false;
752         ret = count;
753 done:
754         free_page(addr);
755         return ret;
756 }
757
758 /* Proc hscfg file read handler
759  * This function can be used to read host sleep configuration
760  * parameters from driver.
761  */
762 static ssize_t
763 mwifiex_hscfg_read(struct file *file, char __user *ubuf,
764                    size_t count, loff_t *ppos)
765 {
766         struct mwifiex_private *priv = (void *)file->private_data;
767         unsigned long addr = get_zeroed_page(GFP_KERNEL);
768         char *buf = (char *)addr;
769         int pos, ret;
770         struct mwifiex_ds_hs_cfg hscfg;
771
772         if (!buf)
773                 return -ENOMEM;
774
775         mwifiex_set_hs_params(priv, HostCmd_ACT_GEN_GET,
776                               MWIFIEX_SYNC_CMD, &hscfg);
777
778         pos = snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", hscfg.conditions,
779                        hscfg.gpio, hscfg.gap);
780
781         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
782
783         free_page(addr);
784         return ret;
785 }
786
787 #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
788         if (!debugfs_create_file(#name, 0644, priv->dfs_dev_dir,        \
789                         priv, &mwifiex_dfs_##name##_fops))              \
790                 return;                                                 \
791 } while (0);
792
793 #define MWIFIEX_DFS_FILE_OPS(name)                                      \
794 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
795         .read = mwifiex_##name##_read,                                  \
796         .write = mwifiex_##name##_write,                                \
797         .open = simple_open,                                            \
798 };
799
800 #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
801 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
802         .read = mwifiex_##name##_read,                                  \
803         .open = simple_open,                                            \
804 };
805
806 #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
807 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
808         .write = mwifiex_##name##_write,                                \
809         .open = simple_open,                                            \
810 };
811
812
813 MWIFIEX_DFS_FILE_READ_OPS(info);
814 MWIFIEX_DFS_FILE_READ_OPS(debug);
815 MWIFIEX_DFS_FILE_READ_OPS(getlog);
816 MWIFIEX_DFS_FILE_READ_OPS(fw_dump);
817 MWIFIEX_DFS_FILE_OPS(regrdwr);
818 MWIFIEX_DFS_FILE_OPS(rdeeprom);
819 MWIFIEX_DFS_FILE_OPS(hscfg);
820
821 /*
822  * This function creates the debug FS directory structure and the files.
823  */
824 void
825 mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
826 {
827         if (!mwifiex_dfs_dir || !priv)
828                 return;
829
830         priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
831                                                mwifiex_dfs_dir);
832
833         if (!priv->dfs_dev_dir)
834                 return;
835
836         MWIFIEX_DFS_ADD_FILE(info);
837         MWIFIEX_DFS_ADD_FILE(debug);
838         MWIFIEX_DFS_ADD_FILE(getlog);
839         MWIFIEX_DFS_ADD_FILE(regrdwr);
840         MWIFIEX_DFS_ADD_FILE(rdeeprom);
841         MWIFIEX_DFS_ADD_FILE(fw_dump);
842         MWIFIEX_DFS_ADD_FILE(hscfg);
843 }
844
845 /*
846  * This function removes the debug FS directory structure and the files.
847  */
848 void
849 mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
850 {
851         if (!priv)
852                 return;
853
854         debugfs_remove_recursive(priv->dfs_dev_dir);
855 }
856
857 /*
858  * This function creates the top level proc directory.
859  */
860 void
861 mwifiex_debugfs_init(void)
862 {
863         if (!mwifiex_dfs_dir)
864                 mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
865 }
866
867 /*
868  * This function removes the top level proc directory.
869  */
870 void
871 mwifiex_debugfs_remove(void)
872 {
873         if (mwifiex_dfs_dir)
874                 debugfs_remove(mwifiex_dfs_dir);
875 }