mwifiex: debugfs: Fix (sometimes) off-by-1 SSID print
[pandora-kernel.git] / drivers / net / wireless / mwifiex / debugfs.c
1 /*
2  * Marvell Wireless LAN device driver: debugfs
3  *
4  * Copyright (C) 2011, 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         "Unknown",
30         "Ad-hoc",
31         "Managed",
32         "Auto"
33 };
34
35 /* size/addr for mwifiex_debug_info */
36 #define item_size(n)            (FIELD_SIZEOF(struct mwifiex_debug_info, n))
37 #define item_addr(n)            (offsetof(struct mwifiex_debug_info, n))
38
39 /* size/addr for struct mwifiex_adapter */
40 #define adapter_item_size(n)    (FIELD_SIZEOF(struct mwifiex_adapter, n))
41 #define adapter_item_addr(n)    (offsetof(struct mwifiex_adapter, n))
42
43 struct mwifiex_debug_data {
44         char name[32];          /* variable/array name */
45         u32 size;               /* size of the variable/array */
46         size_t addr;            /* address of the variable/array */
47         int num;                /* number of variables in an array */
48 };
49
50 static struct mwifiex_debug_data items[] = {
51         {"int_counter", item_size(int_counter),
52          item_addr(int_counter), 1},
53         {"wmm_ac_vo", item_size(packets_out[WMM_AC_VO]),
54          item_addr(packets_out[WMM_AC_VO]), 1},
55         {"wmm_ac_vi", item_size(packets_out[WMM_AC_VI]),
56          item_addr(packets_out[WMM_AC_VI]), 1},
57         {"wmm_ac_be", item_size(packets_out[WMM_AC_BE]),
58          item_addr(packets_out[WMM_AC_BE]), 1},
59         {"wmm_ac_bk", item_size(packets_out[WMM_AC_BK]),
60          item_addr(packets_out[WMM_AC_BK]), 1},
61         {"max_tx_buf_size", item_size(max_tx_buf_size),
62          item_addr(max_tx_buf_size), 1},
63         {"tx_buf_size", item_size(tx_buf_size),
64          item_addr(tx_buf_size), 1},
65         {"curr_tx_buf_size", item_size(curr_tx_buf_size),
66          item_addr(curr_tx_buf_size), 1},
67         {"ps_mode", item_size(ps_mode),
68          item_addr(ps_mode), 1},
69         {"ps_state", item_size(ps_state),
70          item_addr(ps_state), 1},
71         {"is_deep_sleep", item_size(is_deep_sleep),
72          item_addr(is_deep_sleep), 1},
73         {"wakeup_dev_req", item_size(pm_wakeup_card_req),
74          item_addr(pm_wakeup_card_req), 1},
75         {"wakeup_tries", item_size(pm_wakeup_fw_try),
76          item_addr(pm_wakeup_fw_try), 1},
77         {"hs_configured", item_size(is_hs_configured),
78          item_addr(is_hs_configured), 1},
79         {"hs_activated", item_size(hs_activated),
80          item_addr(hs_activated), 1},
81         {"num_tx_timeout", item_size(num_tx_timeout),
82          item_addr(num_tx_timeout), 1},
83         {"num_cmd_timeout", item_size(num_cmd_timeout),
84          item_addr(num_cmd_timeout), 1},
85         {"timeout_cmd_id", item_size(timeout_cmd_id),
86          item_addr(timeout_cmd_id), 1},
87         {"timeout_cmd_act", item_size(timeout_cmd_act),
88          item_addr(timeout_cmd_act), 1},
89         {"last_cmd_id", item_size(last_cmd_id),
90          item_addr(last_cmd_id), DBG_CMD_NUM},
91         {"last_cmd_act", item_size(last_cmd_act),
92          item_addr(last_cmd_act), DBG_CMD_NUM},
93         {"last_cmd_index", item_size(last_cmd_index),
94          item_addr(last_cmd_index), 1},
95         {"last_cmd_resp_id", item_size(last_cmd_resp_id),
96          item_addr(last_cmd_resp_id), DBG_CMD_NUM},
97         {"last_cmd_resp_index", item_size(last_cmd_resp_index),
98          item_addr(last_cmd_resp_index), 1},
99         {"last_event", item_size(last_event),
100          item_addr(last_event), DBG_CMD_NUM},
101         {"last_event_index", item_size(last_event_index),
102          item_addr(last_event_index), 1},
103         {"num_cmd_h2c_fail", item_size(num_cmd_host_to_card_failure),
104          item_addr(num_cmd_host_to_card_failure), 1},
105         {"num_cmd_sleep_cfm_fail",
106          item_size(num_cmd_sleep_cfm_host_to_card_failure),
107          item_addr(num_cmd_sleep_cfm_host_to_card_failure), 1},
108         {"num_tx_h2c_fail", item_size(num_tx_host_to_card_failure),
109          item_addr(num_tx_host_to_card_failure), 1},
110         {"num_evt_deauth", item_size(num_event_deauth),
111          item_addr(num_event_deauth), 1},
112         {"num_evt_disassoc", item_size(num_event_disassoc),
113          item_addr(num_event_disassoc), 1},
114         {"num_evt_link_lost", item_size(num_event_link_lost),
115          item_addr(num_event_link_lost), 1},
116         {"num_cmd_deauth", item_size(num_cmd_deauth),
117          item_addr(num_cmd_deauth), 1},
118         {"num_cmd_assoc_ok", item_size(num_cmd_assoc_success),
119          item_addr(num_cmd_assoc_success), 1},
120         {"num_cmd_assoc_fail", item_size(num_cmd_assoc_failure),
121          item_addr(num_cmd_assoc_failure), 1},
122         {"cmd_sent", item_size(cmd_sent),
123          item_addr(cmd_sent), 1},
124         {"data_sent", item_size(data_sent),
125          item_addr(data_sent), 1},
126         {"cmd_resp_received", item_size(cmd_resp_received),
127          item_addr(cmd_resp_received), 1},
128         {"event_received", item_size(event_received),
129          item_addr(event_received), 1},
130
131         /* variables defined in struct mwifiex_adapter */
132         {"cmd_pending", adapter_item_size(cmd_pending),
133          adapter_item_addr(cmd_pending), 1},
134         {"tx_pending", adapter_item_size(tx_pending),
135          adapter_item_addr(tx_pending), 1},
136         {"rx_pending", adapter_item_size(rx_pending),
137          adapter_item_addr(rx_pending), 1},
138 };
139
140 static int num_of_items = ARRAY_SIZE(items);
141
142 /*
143  * Generic proc file open handler.
144  *
145  * This function is called every time a file is accessed for read or write.
146  */
147 static int
148 mwifiex_open_generic(struct inode *inode, struct file *file)
149 {
150         file->private_data = inode->i_private;
151         return 0;
152 }
153
154 /*
155  * Proc info file read handler.
156  *
157  * This function is called when the 'info' file is opened for reading.
158  * It prints the following driver related information -
159  *      - Driver name
160  *      - Driver version
161  *      - Driver extended version
162  *      - Interface name
163  *      - BSS mode
164  *      - Media state (connected or disconnected)
165  *      - MAC address
166  *      - Total number of Tx bytes
167  *      - Total number of Rx bytes
168  *      - Total number of Tx packets
169  *      - Total number of Rx packets
170  *      - Total number of dropped Tx packets
171  *      - Total number of dropped Rx packets
172  *      - Total number of corrupted Tx packets
173  *      - Total number of corrupted Rx packets
174  *      - Carrier status (on or off)
175  *      - Tx queue status (started or stopped)
176  *
177  * For STA mode drivers, it also prints the following extra -
178  *      - ESSID
179  *      - BSSID
180  *      - Channel
181  *      - Region code
182  *      - Multicast count
183  *      - Multicast addresses
184  */
185 static ssize_t
186 mwifiex_info_read(struct file *file, char __user *ubuf,
187                   size_t count, loff_t *ppos)
188 {
189         struct mwifiex_private *priv =
190                 (struct mwifiex_private *) file->private_data;
191         struct net_device *netdev = priv->netdev;
192         struct netdev_hw_addr *ha;
193         unsigned long page = get_zeroed_page(GFP_KERNEL);
194         char *p = (char *) page, fmt[64];
195         struct mwifiex_bss_info info;
196         ssize_t ret;
197         int i = 0;
198
199         if (!p)
200                 return -ENOMEM;
201
202         memset(&info, 0, sizeof(info));
203         ret = mwifiex_get_bss_info(priv, &info);
204         if (ret)
205                 goto free_and_exit;
206
207         mwifiex_drv_get_driver_version(priv->adapter, fmt, sizeof(fmt) - 1);
208
209         if (!priv->version_str[0])
210                 mwifiex_get_ver_ext(priv);
211
212         p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
213         p += sprintf(p, "driver_version = %s", fmt);
214         p += sprintf(p, "\nverext = %s", priv->version_str);
215         p += sprintf(p, "\ninterface_name=\"%s\"\n", netdev->name);
216         p += sprintf(p, "bss_mode=\"%s\"\n", bss_modes[info.bss_mode]);
217         p += sprintf(p, "media_state=\"%s\"\n",
218                      (!priv->media_connected ? "Disconnected" : "Connected"));
219         p += sprintf(p, "mac_address=\"%pM\"\n", netdev->dev_addr);
220
221         if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) {
222                 p += sprintf(p, "multicast_count=\"%d\"\n",
223                              netdev_mc_count(netdev));
224                 p += sprintf(p, "essid=\"%.*s\"\n", info.ssid.ssid_len,
225                              info.ssid.ssid);
226                 p += sprintf(p, "bssid=\"%pM\"\n", info.bssid);
227                 p += sprintf(p, "channel=\"%d\"\n", (int) info.bss_chan);
228                 p += sprintf(p, "region_code = \"%02x\"\n", info.region_code);
229
230                 netdev_for_each_mc_addr(ha, netdev)
231                         p += sprintf(p, "multicast_address[%d]=\"%pM\"\n",
232                                         i++, ha->addr);
233         }
234
235         p += sprintf(p, "num_tx_bytes = %lu\n", priv->stats.tx_bytes);
236         p += sprintf(p, "num_rx_bytes = %lu\n", priv->stats.rx_bytes);
237         p += sprintf(p, "num_tx_pkts = %lu\n", priv->stats.tx_packets);
238         p += sprintf(p, "num_rx_pkts = %lu\n", priv->stats.rx_packets);
239         p += sprintf(p, "num_tx_pkts_dropped = %lu\n", priv->stats.tx_dropped);
240         p += sprintf(p, "num_rx_pkts_dropped = %lu\n", priv->stats.rx_dropped);
241         p += sprintf(p, "num_tx_pkts_err = %lu\n", priv->stats.tx_errors);
242         p += sprintf(p, "num_rx_pkts_err = %lu\n", priv->stats.rx_errors);
243         p += sprintf(p, "carrier %s\n", ((netif_carrier_ok(priv->netdev))
244                                          ? "on" : "off"));
245         p += sprintf(p, "tx queue %s\n", ((netif_queue_stopped(priv->netdev))
246                                           ? "stopped" : "started"));
247
248         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
249                                       (unsigned long) p - page);
250
251 free_and_exit:
252         free_page(page);
253         return ret;
254 }
255
256 /*
257  * Proc getlog file read handler.
258  *
259  * This function is called when the 'getlog' file is opened for reading
260  * It prints the following log information -
261  *      - Number of multicast Tx frames
262  *      - Number of failed packets
263  *      - Number of Tx retries
264  *      - Number of multicast Tx retries
265  *      - Number of duplicate frames
266  *      - Number of RTS successes
267  *      - Number of RTS failures
268  *      - Number of ACK failures
269  *      - Number of fragmented Rx frames
270  *      - Number of multicast Rx frames
271  *      - Number of FCS errors
272  *      - Number of Tx frames
273  *      - WEP ICV error counts
274  */
275 static ssize_t
276 mwifiex_getlog_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         unsigned long page = get_zeroed_page(GFP_KERNEL);
282         char *p = (char *) page;
283         ssize_t ret;
284         struct mwifiex_ds_get_stats stats;
285
286         if (!p)
287                 return -ENOMEM;
288
289         memset(&stats, 0, sizeof(stats));
290         ret = mwifiex_get_stats_info(priv, &stats);
291         if (ret)
292                 goto free_and_exit;
293
294         p += sprintf(p, "\n"
295                      "mcasttxframe     %u\n"
296                      "failed           %u\n"
297                      "retry            %u\n"
298                      "multiretry       %u\n"
299                      "framedup         %u\n"
300                      "rtssuccess       %u\n"
301                      "rtsfailure       %u\n"
302                      "ackfailure       %u\n"
303                      "rxfrag           %u\n"
304                      "mcastrxframe     %u\n"
305                      "fcserror         %u\n"
306                      "txframe          %u\n"
307                      "wepicverrcnt-1   %u\n"
308                      "wepicverrcnt-2   %u\n"
309                      "wepicverrcnt-3   %u\n"
310                      "wepicverrcnt-4   %u\n",
311                      stats.mcast_tx_frame,
312                      stats.failed,
313                      stats.retry,
314                      stats.multi_retry,
315                      stats.frame_dup,
316                      stats.rts_success,
317                      stats.rts_failure,
318                      stats.ack_failure,
319                      stats.rx_frag,
320                      stats.mcast_rx_frame,
321                      stats.fcs_error,
322                      stats.tx_frame,
323                      stats.wep_icv_error[0],
324                      stats.wep_icv_error[1],
325                      stats.wep_icv_error[2],
326                      stats.wep_icv_error[3]);
327
328
329         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
330                                       (unsigned long) p - page);
331
332 free_and_exit:
333         free_page(page);
334         return ret;
335 }
336
337 static struct mwifiex_debug_info info;
338
339 /*
340  * Proc debug file read handler.
341  *
342  * This function is called when the 'debug' file is opened for reading
343  * It prints the following log information -
344  *      - Interrupt count
345  *      - WMM AC VO packets count
346  *      - WMM AC VI packets count
347  *      - WMM AC BE packets count
348  *      - WMM AC BK packets count
349  *      - Maximum Tx buffer size
350  *      - Tx buffer size
351  *      - Current Tx buffer size
352  *      - Power Save mode
353  *      - Power Save state
354  *      - Deep Sleep status
355  *      - Device wakeup required status
356  *      - Number of wakeup tries
357  *      - Host Sleep configured status
358  *      - Host Sleep activated status
359  *      - Number of Tx timeouts
360  *      - Number of command timeouts
361  *      - Last timed out command ID
362  *      - Last timed out command action
363  *      - Last command ID
364  *      - Last command action
365  *      - Last command index
366  *      - Last command response ID
367  *      - Last command response index
368  *      - Last event
369  *      - Last event index
370  *      - Number of host to card command failures
371  *      - Number of sleep confirm command failures
372  *      - Number of host to card data failure
373  *      - Number of deauthentication events
374  *      - Number of disassociation events
375  *      - Number of link lost events
376  *      - Number of deauthentication commands
377  *      - Number of association success commands
378  *      - Number of association failure commands
379  *      - Number of commands sent
380  *      - Number of data packets sent
381  *      - Number of command responses received
382  *      - Number of events received
383  *      - Tx BA stream table (TID, RA)
384  *      - Rx reorder table (TID, TA, Start window, Window size, Buffer)
385  */
386 static ssize_t
387 mwifiex_debug_read(struct file *file, char __user *ubuf,
388                    size_t count, loff_t *ppos)
389 {
390         struct mwifiex_private *priv =
391                 (struct mwifiex_private *) file->private_data;
392         struct mwifiex_debug_data *d = &items[0];
393         unsigned long page = get_zeroed_page(GFP_KERNEL);
394         char *p = (char *) page;
395         ssize_t ret;
396         size_t size, addr;
397         long val;
398         int i, j;
399
400         if (!p)
401                 return -ENOMEM;
402
403         ret = mwifiex_get_debug_info(priv, &info);
404         if (ret)
405                 goto free_and_exit;
406
407         for (i = 0; i < num_of_items; i++) {
408                 p += sprintf(p, "%s=", d[i].name);
409
410                 size = d[i].size / d[i].num;
411
412                 if (i < (num_of_items - 3))
413                         addr = d[i].addr + (size_t) &info;
414                 else /* The last 3 items are struct mwifiex_adapter variables */
415                         addr = d[i].addr + (size_t) priv->adapter;
416
417                 for (j = 0; j < d[i].num; j++) {
418                         switch (size) {
419                         case 1:
420                                 val = *((u8 *) addr);
421                                 break;
422                         case 2:
423                                 val = *((u16 *) addr);
424                                 break;
425                         case 4:
426                                 val = *((u32 *) addr);
427                                 break;
428                         case 8:
429                                 val = *((long long *) addr);
430                                 break;
431                         default:
432                                 val = -1;
433                                 break;
434                         }
435
436                         p += sprintf(p, "%#lx ", val);
437                         addr += size;
438                 }
439
440                 p += sprintf(p, "\n");
441         }
442
443         if (info.tx_tbl_num) {
444                 p += sprintf(p, "Tx BA stream table:\n");
445                 for (i = 0; i < info.tx_tbl_num; i++)
446                         p += sprintf(p, "tid = %d, ra = %pM\n",
447                                      info.tx_tbl[i].tid, info.tx_tbl[i].ra);
448         }
449
450         if (info.rx_tbl_num) {
451                 p += sprintf(p, "Rx reorder table:\n");
452                 for (i = 0; i < info.rx_tbl_num; i++) {
453                         p += sprintf(p, "tid = %d, ta = %pM, "
454                                      "start_win = %d, "
455                                      "win_size = %d, buffer: ",
456                                      info.rx_tbl[i].tid,
457                                      info.rx_tbl[i].ta,
458                                      info.rx_tbl[i].start_win,
459                                      info.rx_tbl[i].win_size);
460
461                         for (j = 0; j < info.rx_tbl[i].win_size; j++)
462                                 p += sprintf(p, "%c ",
463                                              info.rx_tbl[i].buffer[j] ?
464                                              '1' : '0');
465
466                         p += sprintf(p, "\n");
467                 }
468         }
469
470         ret = simple_read_from_buffer(ubuf, count, ppos, (char *) page,
471                                       (unsigned long) p - page);
472
473 free_and_exit:
474         free_page(page);
475         return ret;
476 }
477
478 static u32 saved_reg_type, saved_reg_offset, saved_reg_value;
479
480 /*
481  * Proc regrdwr file write handler.
482  *
483  * This function is called when the 'regrdwr' file is opened for writing
484  *
485  * This function can be used to write to a register.
486  */
487 static ssize_t
488 mwifiex_regrdwr_write(struct file *file,
489                       const char __user *ubuf, size_t count, loff_t *ppos)
490 {
491         unsigned long addr = get_zeroed_page(GFP_KERNEL);
492         char *buf = (char *) addr;
493         size_t buf_size = min(count, (size_t) (PAGE_SIZE - 1));
494         int ret;
495         u32 reg_type = 0, reg_offset = 0, reg_value = UINT_MAX;
496
497         if (!buf)
498                 return -ENOMEM;
499
500
501         if (copy_from_user(buf, ubuf, buf_size)) {
502                 ret = -EFAULT;
503                 goto done;
504         }
505
506         sscanf(buf, "%u %x %x", &reg_type, &reg_offset, &reg_value);
507
508         if (reg_type == 0 || reg_offset == 0) {
509                 ret = -EINVAL;
510                 goto done;
511         } else {
512                 saved_reg_type = reg_type;
513                 saved_reg_offset = reg_offset;
514                 saved_reg_value = reg_value;
515                 ret = count;
516         }
517 done:
518         free_page(addr);
519         return ret;
520 }
521
522 /*
523  * Proc regrdwr file read handler.
524  *
525  * This function is called when the 'regrdwr' file is opened for reading
526  *
527  * This function can be used to read from a register.
528  */
529 static ssize_t
530 mwifiex_regrdwr_read(struct file *file, char __user *ubuf,
531                      size_t count, loff_t *ppos)
532 {
533         struct mwifiex_private *priv =
534                 (struct mwifiex_private *) file->private_data;
535         unsigned long addr = get_zeroed_page(GFP_KERNEL);
536         char *buf = (char *) addr;
537         int pos = 0, ret = 0;
538         u32 reg_value;
539
540         if (!buf)
541                 return -ENOMEM;
542
543         if (!saved_reg_type) {
544                 /* No command has been given */
545                 pos += snprintf(buf, PAGE_SIZE, "0");
546                 goto done;
547         }
548         /* Set command has been given */
549         if (saved_reg_value != UINT_MAX) {
550                 ret = mwifiex_reg_write(priv, saved_reg_type, saved_reg_offset,
551                                         saved_reg_value);
552
553                 pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n",
554                                 saved_reg_type, saved_reg_offset,
555                                 saved_reg_value);
556
557                 ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
558
559                 goto done;
560         }
561         /* Get command has been given */
562         ret = mwifiex_reg_read(priv, saved_reg_type,
563                                saved_reg_offset, &reg_value);
564         if (ret) {
565                 ret = -EINVAL;
566                 goto done;
567         }
568
569         pos += snprintf(buf, PAGE_SIZE, "%u 0x%x 0x%x\n", saved_reg_type,
570                         saved_reg_offset, reg_value);
571
572         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
573
574 done:
575         free_page(addr);
576         return ret;
577 }
578
579 static u32 saved_offset = -1, saved_bytes = -1;
580
581 /*
582  * Proc rdeeprom file write handler.
583  *
584  * This function is called when the 'rdeeprom' file is opened for writing
585  *
586  * This function can be used to write to a RDEEPROM location.
587  */
588 static ssize_t
589 mwifiex_rdeeprom_write(struct file *file,
590                        const char __user *ubuf, size_t count, loff_t *ppos)
591 {
592         unsigned long addr = get_zeroed_page(GFP_KERNEL);
593         char *buf = (char *) addr;
594         size_t buf_size = min(count, (size_t) (PAGE_SIZE - 1));
595         int ret = 0;
596         int offset = -1, bytes = -1;
597
598         if (!buf)
599                 return -ENOMEM;
600
601
602         if (copy_from_user(buf, ubuf, buf_size)) {
603                 ret = -EFAULT;
604                 goto done;
605         }
606
607         sscanf(buf, "%d %d", &offset, &bytes);
608
609         if (offset == -1 || bytes == -1) {
610                 ret = -EINVAL;
611                 goto done;
612         } else {
613                 saved_offset = offset;
614                 saved_bytes = bytes;
615                 ret = count;
616         }
617 done:
618         free_page(addr);
619         return ret;
620 }
621
622 /*
623  * Proc rdeeprom read write handler.
624  *
625  * This function is called when the 'rdeeprom' file is opened for reading
626  *
627  * This function can be used to read from a RDEEPROM location.
628  */
629 static ssize_t
630 mwifiex_rdeeprom_read(struct file *file, char __user *ubuf,
631                       size_t count, loff_t *ppos)
632 {
633         struct mwifiex_private *priv =
634                 (struct mwifiex_private *) file->private_data;
635         unsigned long addr = get_zeroed_page(GFP_KERNEL);
636         char *buf = (char *) addr;
637         int pos, ret, i;
638         u8 value[MAX_EEPROM_DATA];
639
640         if (!buf)
641                 return -ENOMEM;
642
643         if (saved_offset == -1) {
644                 /* No command has been given */
645                 pos = snprintf(buf, PAGE_SIZE, "0");
646                 goto done;
647         }
648
649         /* Get command has been given */
650         ret = mwifiex_eeprom_read(priv, (u16) saved_offset,
651                                   (u16) saved_bytes, value);
652         if (ret) {
653                 ret = -EINVAL;
654                 goto out_free;
655         }
656
657         pos = snprintf(buf, PAGE_SIZE, "%d %d ", saved_offset, saved_bytes);
658
659         for (i = 0; i < saved_bytes; i++)
660                 pos += scnprintf(buf + pos, PAGE_SIZE - pos, "%d ", value[i]);
661
662 done:
663         ret = simple_read_from_buffer(ubuf, count, ppos, buf, pos);
664 out_free:
665         free_page(addr);
666         return ret;
667 }
668
669
670 #define MWIFIEX_DFS_ADD_FILE(name) do {                                 \
671         if (!debugfs_create_file(#name, 0644, priv->dfs_dev_dir,        \
672                         priv, &mwifiex_dfs_##name##_fops))              \
673                 return;                                                 \
674 } while (0);
675
676 #define MWIFIEX_DFS_FILE_OPS(name)                                      \
677 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
678         .read = mwifiex_##name##_read,                                  \
679         .write = mwifiex_##name##_write,                                \
680         .open = mwifiex_open_generic,                                   \
681 };
682
683 #define MWIFIEX_DFS_FILE_READ_OPS(name)                                 \
684 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
685         .read = mwifiex_##name##_read,                                  \
686         .open = mwifiex_open_generic,                                   \
687 };
688
689 #define MWIFIEX_DFS_FILE_WRITE_OPS(name)                                \
690 static const struct file_operations mwifiex_dfs_##name##_fops = {       \
691         .write = mwifiex_##name##_write,                                \
692         .open = mwifiex_open_generic,                                   \
693 };
694
695
696 MWIFIEX_DFS_FILE_READ_OPS(info);
697 MWIFIEX_DFS_FILE_READ_OPS(debug);
698 MWIFIEX_DFS_FILE_READ_OPS(getlog);
699 MWIFIEX_DFS_FILE_OPS(regrdwr);
700 MWIFIEX_DFS_FILE_OPS(rdeeprom);
701
702 /*
703  * This function creates the debug FS directory structure and the files.
704  */
705 void
706 mwifiex_dev_debugfs_init(struct mwifiex_private *priv)
707 {
708         if (!mwifiex_dfs_dir || !priv)
709                 return;
710
711         priv->dfs_dev_dir = debugfs_create_dir(priv->netdev->name,
712                                                mwifiex_dfs_dir);
713
714         if (!priv->dfs_dev_dir)
715                 return;
716
717         MWIFIEX_DFS_ADD_FILE(info);
718         MWIFIEX_DFS_ADD_FILE(debug);
719         MWIFIEX_DFS_ADD_FILE(getlog);
720         MWIFIEX_DFS_ADD_FILE(regrdwr);
721         MWIFIEX_DFS_ADD_FILE(rdeeprom);
722 }
723
724 /*
725  * This function removes the debug FS directory structure and the files.
726  */
727 void
728 mwifiex_dev_debugfs_remove(struct mwifiex_private *priv)
729 {
730         if (!priv)
731                 return;
732
733         debugfs_remove_recursive(priv->dfs_dev_dir);
734 }
735
736 /*
737  * This function creates the top level proc directory.
738  */
739 void
740 mwifiex_debugfs_init(void)
741 {
742         if (!mwifiex_dfs_dir)
743                 mwifiex_dfs_dir = debugfs_create_dir("mwifiex", NULL);
744 }
745
746 /*
747  * This function removes the top level proc directory.
748  */
749 void
750 mwifiex_debugfs_remove(void)
751 {
752         if (mwifiex_dfs_dir)
753                 debugfs_remove(mwifiex_dfs_dir);
754 }