iwlwifi: fix strict_strtoul error checking
[linux-2.6] / drivers / net / wireless / libertas / debugfs.c
1 #include <linux/module.h>
2 #include <linux/dcache.h>
3 #include <linux/debugfs.h>
4 #include <linux/delay.h>
5 #include <linux/mm.h>
6 #include <linux/string.h>
7 #include <net/iw_handler.h>
8
9 #include "dev.h"
10 #include "decl.h"
11 #include "host.h"
12 #include "debugfs.h"
13 #include "cmd.h"
14
15 static struct dentry *lbs_dir;
16 static char *szStates[] = {
17         "Connected",
18         "Disconnected"
19 };
20
21 #ifdef PROC_DEBUG
22 static void lbs_debug_init(struct lbs_private *priv);
23 #endif
24
25 static int open_file_generic(struct inode *inode, struct file *file)
26 {
27         file->private_data = inode->i_private;
28         return 0;
29 }
30
31 static ssize_t write_file_dummy(struct file *file, const char __user *buf,
32                                 size_t count, loff_t *ppos)
33 {
34         return -EINVAL;
35 }
36
37 static const size_t len = PAGE_SIZE;
38
39 static ssize_t lbs_dev_info(struct file *file, char __user *userbuf,
40                                   size_t count, loff_t *ppos)
41 {
42         struct lbs_private *priv = file->private_data;
43         size_t pos = 0;
44         unsigned long addr = get_zeroed_page(GFP_KERNEL);
45         char *buf = (char *)addr;
46         ssize_t res;
47
48         pos += snprintf(buf+pos, len-pos, "state = %s\n",
49                                 szStates[priv->connect_status]);
50         pos += snprintf(buf+pos, len-pos, "region_code = %02x\n",
51                                 (u32) priv->regioncode);
52
53         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
54
55         free_page(addr);
56         return res;
57 }
58
59
60 static ssize_t lbs_getscantable(struct file *file, char __user *userbuf,
61                                   size_t count, loff_t *ppos)
62 {
63         struct lbs_private *priv = file->private_data;
64         size_t pos = 0;
65         int numscansdone = 0, res;
66         unsigned long addr = get_zeroed_page(GFP_KERNEL);
67         char *buf = (char *)addr;
68         DECLARE_MAC_BUF(mac);
69         struct bss_descriptor * iter_bss;
70
71         pos += snprintf(buf+pos, len-pos,
72                 "# | ch  | rssi |       bssid       |   cap    | Qual | SSID \n");
73
74         mutex_lock(&priv->lock);
75         list_for_each_entry (iter_bss, &priv->network_list, list) {
76                 u16 ibss = (iter_bss->capability & WLAN_CAPABILITY_IBSS);
77                 u16 privacy = (iter_bss->capability & WLAN_CAPABILITY_PRIVACY);
78                 u16 spectrum_mgmt = (iter_bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT);
79
80                 pos += snprintf(buf+pos, len-pos,
81                         "%02u| %03d | %04d | %s |",
82                         numscansdone, iter_bss->channel, iter_bss->rssi,
83                         print_mac(mac, iter_bss->bssid));
84                 pos += snprintf(buf+pos, len-pos, " %04x-", iter_bss->capability);
85                 pos += snprintf(buf+pos, len-pos, "%c%c%c |",
86                                 ibss ? 'A' : 'I', privacy ? 'P' : ' ',
87                                 spectrum_mgmt ? 'S' : ' ');
88                 pos += snprintf(buf+pos, len-pos, " %04d |", SCAN_RSSI(iter_bss->rssi));
89                 pos += snprintf(buf+pos, len-pos, " %s\n",
90                                 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
91
92                 numscansdone++;
93         }
94         mutex_unlock(&priv->lock);
95
96         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
97
98         free_page(addr);
99         return res;
100 }
101
102 static ssize_t lbs_sleepparams_write(struct file *file,
103                                 const char __user *user_buf, size_t count,
104                                 loff_t *ppos)
105 {
106         struct lbs_private *priv = file->private_data;
107         ssize_t buf_size, ret;
108         struct sleep_params sp;
109         int p1, p2, p3, p4, p5, p6;
110         unsigned long addr = get_zeroed_page(GFP_KERNEL);
111         char *buf = (char *)addr;
112
113         buf_size = min(count, len - 1);
114         if (copy_from_user(buf, user_buf, buf_size)) {
115                 ret = -EFAULT;
116                 goto out_unlock;
117         }
118         ret = sscanf(buf, "%d %d %d %d %d %d", &p1, &p2, &p3, &p4, &p5, &p6);
119         if (ret != 6) {
120                 ret = -EINVAL;
121                 goto out_unlock;
122         }
123         sp.sp_error = p1;
124         sp.sp_offset = p2;
125         sp.sp_stabletime = p3;
126         sp.sp_calcontrol = p4;
127         sp.sp_extsleepclk = p5;
128         sp.sp_reserved = p6;
129
130         ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_SET, &sp);
131         if (!ret)
132                 ret = count;
133         else if (ret > 0)
134                 ret = -EINVAL;
135
136 out_unlock:
137         free_page(addr);
138         return ret;
139 }
140
141 static ssize_t lbs_sleepparams_read(struct file *file, char __user *userbuf,
142                                   size_t count, loff_t *ppos)
143 {
144         struct lbs_private *priv = file->private_data;
145         ssize_t ret;
146         size_t pos = 0;
147         struct sleep_params sp;
148         unsigned long addr = get_zeroed_page(GFP_KERNEL);
149         char *buf = (char *)addr;
150
151         ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_GET, &sp);
152         if (ret)
153                 goto out_unlock;
154
155         pos += snprintf(buf, len, "%d %d %d %d %d %d\n", sp.sp_error,
156                         sp.sp_offset, sp.sp_stabletime,
157                         sp.sp_calcontrol, sp.sp_extsleepclk,
158                         sp.sp_reserved);
159
160         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
161
162 out_unlock:
163         free_page(addr);
164         return ret;
165 }
166
167 /*
168  * When calling CMD_802_11_SUBSCRIBE_EVENT with CMD_ACT_GET, me might
169  * get a bunch of vendor-specific TLVs (a.k.a. IEs) back from the
170  * firmware. Here's an example:
171  *      04 01 02 00 00 00 05 01 02 00 00 00 06 01 02 00
172  *      00 00 07 01 02 00 3c 00 00 00 00 00 00 00 03 03
173  *      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
174  *
175  * The 04 01 is the TLV type (here TLV_TYPE_RSSI_LOW), 02 00 is the length,
176  * 00 00 are the data bytes of this TLV. For this TLV, their meaning is
177  * defined in mrvlietypes_thresholds
178  *
179  * This function searches in this TLV data chunk for a given TLV type
180  * and returns a pointer to the first data byte of the TLV, or to NULL
181  * if the TLV hasn't been found.
182  */
183 static void *lbs_tlv_find(uint16_t tlv_type, const uint8_t *tlv, uint16_t size)
184 {
185         struct mrvlietypesheader *tlv_h;
186         uint16_t length;
187         ssize_t pos = 0;
188
189         while (pos < size) {
190                 tlv_h = (struct mrvlietypesheader *) tlv;
191                 if (!tlv_h->len)
192                         return NULL;
193                 if (tlv_h->type == cpu_to_le16(tlv_type))
194                         return tlv_h;
195                 length = le16_to_cpu(tlv_h->len) + sizeof(*tlv_h);
196                 pos += length;
197                 tlv += length;
198         }
199         return NULL;
200 }
201
202
203 static ssize_t lbs_threshold_read(uint16_t tlv_type, uint16_t event_mask,
204                                   struct file *file, char __user *userbuf,
205                                   size_t count, loff_t *ppos)
206 {
207         struct cmd_ds_802_11_subscribe_event *subscribed;
208         struct mrvlietypes_thresholds *got;
209         struct lbs_private *priv = file->private_data;
210         ssize_t ret = 0;
211         size_t pos = 0;
212         char *buf;
213         u8 value;
214         u8 freq;
215         int events = 0;
216
217         buf = (char *)get_zeroed_page(GFP_KERNEL);
218         if (!buf)
219                 return -ENOMEM;
220
221         subscribed = kzalloc(sizeof(*subscribed), GFP_KERNEL);
222         if (!subscribed) {
223                 ret = -ENOMEM;
224                 goto out_page;
225         }
226
227         subscribed->hdr.size = cpu_to_le16(sizeof(*subscribed));
228         subscribed->action = cpu_to_le16(CMD_ACT_GET);
229
230         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, subscribed);
231         if (ret)
232                 goto out_cmd;
233
234         got = lbs_tlv_find(tlv_type, subscribed->tlv, sizeof(subscribed->tlv));
235         if (got) {
236                 value = got->value;
237                 freq  = got->freq;
238                 events = le16_to_cpu(subscribed->events);
239
240                 pos += snprintf(buf, len, "%d %d %d\n", value, freq,
241                                 !!(events & event_mask));
242         }
243
244         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
245
246  out_cmd:
247         kfree(subscribed);
248
249  out_page:
250         free_page((unsigned long)buf);
251         return ret;
252 }
253
254
255 static ssize_t lbs_threshold_write(uint16_t tlv_type, uint16_t event_mask,
256                                    struct file *file,
257                                    const char __user *userbuf, size_t count,
258                                    loff_t *ppos)
259 {
260         struct cmd_ds_802_11_subscribe_event *events;
261         struct mrvlietypes_thresholds *tlv;
262         struct lbs_private *priv = file->private_data;
263         ssize_t buf_size;
264         int value, freq, new_mask;
265         uint16_t curr_mask;
266         char *buf;
267         int ret;
268
269         buf = (char *)get_zeroed_page(GFP_KERNEL);
270         if (!buf)
271                 return -ENOMEM;
272
273         buf_size = min(count, len - 1);
274         if (copy_from_user(buf, userbuf, buf_size)) {
275                 ret = -EFAULT;
276                 goto out_page;
277         }
278         ret = sscanf(buf, "%d %d %d", &value, &freq, &new_mask);
279         if (ret != 3) {
280                 ret = -EINVAL;
281                 goto out_page;
282         }
283         events = kzalloc(sizeof(*events), GFP_KERNEL);
284         if (!events) {
285                 ret = -ENOMEM;
286                 goto out_page;
287         }
288
289         events->hdr.size = cpu_to_le16(sizeof(*events));
290         events->action = cpu_to_le16(CMD_ACT_GET);
291
292         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
293         if (ret)
294                 goto out_events;
295
296         curr_mask = le16_to_cpu(events->events);
297
298         if (new_mask)
299                 new_mask = curr_mask | event_mask;
300         else
301                 new_mask = curr_mask & ~event_mask;
302
303         /* Now everything is set and we can send stuff down to the firmware */
304
305         tlv = (void *)events->tlv;
306
307         events->action = cpu_to_le16(CMD_ACT_SET);
308         events->events = cpu_to_le16(new_mask);
309         tlv->header.type = cpu_to_le16(tlv_type);
310         tlv->header.len = cpu_to_le16(sizeof(*tlv) - sizeof(tlv->header));
311         tlv->value = value;
312         if (tlv_type != TLV_TYPE_BCNMISS)
313                 tlv->freq = freq;
314
315         /* The command header, the action, the event mask, and one TLV */
316         events->hdr.size = cpu_to_le16(sizeof(events->hdr) + 4 + sizeof(*tlv));
317
318         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
319
320         if (!ret)
321                 ret = count;
322  out_events:
323         kfree(events);
324  out_page:
325         free_page((unsigned long)buf);
326         return ret;
327 }
328
329
330 static ssize_t lbs_lowrssi_read(struct file *file, char __user *userbuf,
331                                 size_t count, loff_t *ppos)
332 {
333         return lbs_threshold_read(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
334                                   file, userbuf, count, ppos);
335 }
336
337
338 static ssize_t lbs_lowrssi_write(struct file *file, const char __user *userbuf,
339                                  size_t count, loff_t *ppos)
340 {
341         return lbs_threshold_write(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
342                                    file, userbuf, count, ppos);
343 }
344
345
346 static ssize_t lbs_lowsnr_read(struct file *file, char __user *userbuf,
347                                size_t count, loff_t *ppos)
348 {
349         return lbs_threshold_read(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
350                                   file, userbuf, count, ppos);
351 }
352
353
354 static ssize_t lbs_lowsnr_write(struct file *file, const char __user *userbuf,
355                                 size_t count, loff_t *ppos)
356 {
357         return lbs_threshold_write(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
358                                    file, userbuf, count, ppos);
359 }
360
361
362 static ssize_t lbs_failcount_read(struct file *file, char __user *userbuf,
363                                   size_t count, loff_t *ppos)
364 {
365         return lbs_threshold_read(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
366                                   file, userbuf, count, ppos);
367 }
368
369
370 static ssize_t lbs_failcount_write(struct file *file, const char __user *userbuf,
371                                    size_t count, loff_t *ppos)
372 {
373         return lbs_threshold_write(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
374                                    file, userbuf, count, ppos);
375 }
376
377
378 static ssize_t lbs_highrssi_read(struct file *file, char __user *userbuf,
379                                  size_t count, loff_t *ppos)
380 {
381         return lbs_threshold_read(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
382                                   file, userbuf, count, ppos);
383 }
384
385
386 static ssize_t lbs_highrssi_write(struct file *file, const char __user *userbuf,
387                                   size_t count, loff_t *ppos)
388 {
389         return lbs_threshold_write(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
390                                    file, userbuf, count, ppos);
391 }
392
393
394 static ssize_t lbs_highsnr_read(struct file *file, char __user *userbuf,
395                                 size_t count, loff_t *ppos)
396 {
397         return lbs_threshold_read(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
398                                   file, userbuf, count, ppos);
399 }
400
401
402 static ssize_t lbs_highsnr_write(struct file *file, const char __user *userbuf,
403                                  size_t count, loff_t *ppos)
404 {
405         return lbs_threshold_write(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
406                                    file, userbuf, count, ppos);
407 }
408
409 static ssize_t lbs_bcnmiss_read(struct file *file, char __user *userbuf,
410                                 size_t count, loff_t *ppos)
411 {
412         return lbs_threshold_read(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
413                                   file, userbuf, count, ppos);
414 }
415
416
417 static ssize_t lbs_bcnmiss_write(struct file *file, const char __user *userbuf,
418                                  size_t count, loff_t *ppos)
419 {
420         return lbs_threshold_write(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
421                                    file, userbuf, count, ppos);
422 }
423
424
425
426 static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf,
427                                   size_t count, loff_t *ppos)
428 {
429         struct lbs_private *priv = file->private_data;
430         struct lbs_offset_value offval;
431         ssize_t pos = 0;
432         int ret;
433         unsigned long addr = get_zeroed_page(GFP_KERNEL);
434         char *buf = (char *)addr;
435
436         offval.offset = priv->mac_offset;
437         offval.value = 0;
438
439         ret = lbs_prepare_and_send_command(priv,
440                                 CMD_MAC_REG_ACCESS, 0,
441                                 CMD_OPTION_WAITFORRSP, 0, &offval);
442         mdelay(10);
443         pos += snprintf(buf+pos, len-pos, "MAC[0x%x] = 0x%08x\n",
444                                 priv->mac_offset, priv->offsetvalue.value);
445
446         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
447         free_page(addr);
448         return ret;
449 }
450
451 static ssize_t lbs_rdmac_write(struct file *file,
452                                     const char __user *userbuf,
453                                     size_t count, loff_t *ppos)
454 {
455         struct lbs_private *priv = file->private_data;
456         ssize_t res, buf_size;
457         unsigned long addr = get_zeroed_page(GFP_KERNEL);
458         char *buf = (char *)addr;
459
460         buf_size = min(count, len - 1);
461         if (copy_from_user(buf, userbuf, buf_size)) {
462                 res = -EFAULT;
463                 goto out_unlock;
464         }
465         priv->mac_offset = simple_strtoul((char *)buf, NULL, 16);
466         res = count;
467 out_unlock:
468         free_page(addr);
469         return res;
470 }
471
472 static ssize_t lbs_wrmac_write(struct file *file,
473                                     const char __user *userbuf,
474                                     size_t count, loff_t *ppos)
475 {
476
477         struct lbs_private *priv = file->private_data;
478         ssize_t res, buf_size;
479         u32 offset, value;
480         struct lbs_offset_value offval;
481         unsigned long addr = get_zeroed_page(GFP_KERNEL);
482         char *buf = (char *)addr;
483
484         buf_size = min(count, len - 1);
485         if (copy_from_user(buf, userbuf, buf_size)) {
486                 res = -EFAULT;
487                 goto out_unlock;
488         }
489         res = sscanf(buf, "%x %x", &offset, &value);
490         if (res != 2) {
491                 res = -EFAULT;
492                 goto out_unlock;
493         }
494
495         offval.offset = offset;
496         offval.value = value;
497         res = lbs_prepare_and_send_command(priv,
498                                 CMD_MAC_REG_ACCESS, 1,
499                                 CMD_OPTION_WAITFORRSP, 0, &offval);
500         mdelay(10);
501
502         res = count;
503 out_unlock:
504         free_page(addr);
505         return res;
506 }
507
508 static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf,
509                                   size_t count, loff_t *ppos)
510 {
511         struct lbs_private *priv = file->private_data;
512         struct lbs_offset_value offval;
513         ssize_t pos = 0;
514         int ret;
515         unsigned long addr = get_zeroed_page(GFP_KERNEL);
516         char *buf = (char *)addr;
517
518         offval.offset = priv->bbp_offset;
519         offval.value = 0;
520
521         ret = lbs_prepare_and_send_command(priv,
522                                 CMD_BBP_REG_ACCESS, 0,
523                                 CMD_OPTION_WAITFORRSP, 0, &offval);
524         mdelay(10);
525         pos += snprintf(buf+pos, len-pos, "BBP[0x%x] = 0x%08x\n",
526                                 priv->bbp_offset, priv->offsetvalue.value);
527
528         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
529         free_page(addr);
530
531         return ret;
532 }
533
534 static ssize_t lbs_rdbbp_write(struct file *file,
535                                     const char __user *userbuf,
536                                     size_t count, loff_t *ppos)
537 {
538         struct lbs_private *priv = file->private_data;
539         ssize_t res, buf_size;
540         unsigned long addr = get_zeroed_page(GFP_KERNEL);
541         char *buf = (char *)addr;
542
543         buf_size = min(count, len - 1);
544         if (copy_from_user(buf, userbuf, buf_size)) {
545                 res = -EFAULT;
546                 goto out_unlock;
547         }
548         priv->bbp_offset = simple_strtoul((char *)buf, NULL, 16);
549         res = count;
550 out_unlock:
551         free_page(addr);
552         return res;
553 }
554
555 static ssize_t lbs_wrbbp_write(struct file *file,
556                                     const char __user *userbuf,
557                                     size_t count, loff_t *ppos)
558 {
559
560         struct lbs_private *priv = file->private_data;
561         ssize_t res, buf_size;
562         u32 offset, value;
563         struct lbs_offset_value offval;
564         unsigned long addr = get_zeroed_page(GFP_KERNEL);
565         char *buf = (char *)addr;
566
567         buf_size = min(count, len - 1);
568         if (copy_from_user(buf, userbuf, buf_size)) {
569                 res = -EFAULT;
570                 goto out_unlock;
571         }
572         res = sscanf(buf, "%x %x", &offset, &value);
573         if (res != 2) {
574                 res = -EFAULT;
575                 goto out_unlock;
576         }
577
578         offval.offset = offset;
579         offval.value = value;
580         res = lbs_prepare_and_send_command(priv,
581                                 CMD_BBP_REG_ACCESS, 1,
582                                 CMD_OPTION_WAITFORRSP, 0, &offval);
583         mdelay(10);
584
585         res = count;
586 out_unlock:
587         free_page(addr);
588         return res;
589 }
590
591 static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf,
592                                   size_t count, loff_t *ppos)
593 {
594         struct lbs_private *priv = file->private_data;
595         struct lbs_offset_value offval;
596         ssize_t pos = 0;
597         int ret;
598         unsigned long addr = get_zeroed_page(GFP_KERNEL);
599         char *buf = (char *)addr;
600
601         offval.offset = priv->rf_offset;
602         offval.value = 0;
603
604         ret = lbs_prepare_and_send_command(priv,
605                                 CMD_RF_REG_ACCESS, 0,
606                                 CMD_OPTION_WAITFORRSP, 0, &offval);
607         mdelay(10);
608         pos += snprintf(buf+pos, len-pos, "RF[0x%x] = 0x%08x\n",
609                                 priv->rf_offset, priv->offsetvalue.value);
610
611         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
612         free_page(addr);
613
614         return ret;
615 }
616
617 static ssize_t lbs_rdrf_write(struct file *file,
618                                     const char __user *userbuf,
619                                     size_t count, loff_t *ppos)
620 {
621         struct lbs_private *priv = file->private_data;
622         ssize_t res, buf_size;
623         unsigned long addr = get_zeroed_page(GFP_KERNEL);
624         char *buf = (char *)addr;
625
626         buf_size = min(count, len - 1);
627         if (copy_from_user(buf, userbuf, buf_size)) {
628                 res = -EFAULT;
629                 goto out_unlock;
630         }
631         priv->rf_offset = simple_strtoul((char *)buf, NULL, 16);
632         res = count;
633 out_unlock:
634         free_page(addr);
635         return res;
636 }
637
638 static ssize_t lbs_wrrf_write(struct file *file,
639                                     const char __user *userbuf,
640                                     size_t count, loff_t *ppos)
641 {
642
643         struct lbs_private *priv = file->private_data;
644         ssize_t res, buf_size;
645         u32 offset, value;
646         struct lbs_offset_value offval;
647         unsigned long addr = get_zeroed_page(GFP_KERNEL);
648         char *buf = (char *)addr;
649
650         buf_size = min(count, len - 1);
651         if (copy_from_user(buf, userbuf, buf_size)) {
652                 res = -EFAULT;
653                 goto out_unlock;
654         }
655         res = sscanf(buf, "%x %x", &offset, &value);
656         if (res != 2) {
657                 res = -EFAULT;
658                 goto out_unlock;
659         }
660
661         offval.offset = offset;
662         offval.value = value;
663         res = lbs_prepare_and_send_command(priv,
664                                 CMD_RF_REG_ACCESS, 1,
665                                 CMD_OPTION_WAITFORRSP, 0, &offval);
666         mdelay(10);
667
668         res = count;
669 out_unlock:
670         free_page(addr);
671         return res;
672 }
673
674 #define FOPS(fread, fwrite) { \
675         .owner = THIS_MODULE, \
676         .open = open_file_generic, \
677         .read = (fread), \
678         .write = (fwrite), \
679 }
680
681 struct lbs_debugfs_files {
682         char *name;
683         int perm;
684         struct file_operations fops;
685 };
686
687 static struct lbs_debugfs_files debugfs_files[] = {
688         { "info", 0444, FOPS(lbs_dev_info, write_file_dummy), },
689         { "getscantable", 0444, FOPS(lbs_getscantable,
690                                         write_file_dummy), },
691         { "sleepparams", 0644, FOPS(lbs_sleepparams_read,
692                                 lbs_sleepparams_write), },
693 };
694
695 static struct lbs_debugfs_files debugfs_events_files[] = {
696         {"low_rssi", 0644, FOPS(lbs_lowrssi_read,
697                                 lbs_lowrssi_write), },
698         {"low_snr", 0644, FOPS(lbs_lowsnr_read,
699                                 lbs_lowsnr_write), },
700         {"failure_count", 0644, FOPS(lbs_failcount_read,
701                                 lbs_failcount_write), },
702         {"beacon_missed", 0644, FOPS(lbs_bcnmiss_read,
703                                 lbs_bcnmiss_write), },
704         {"high_rssi", 0644, FOPS(lbs_highrssi_read,
705                                 lbs_highrssi_write), },
706         {"high_snr", 0644, FOPS(lbs_highsnr_read,
707                                 lbs_highsnr_write), },
708 };
709
710 static struct lbs_debugfs_files debugfs_regs_files[] = {
711         {"rdmac", 0644, FOPS(lbs_rdmac_read, lbs_rdmac_write), },
712         {"wrmac", 0600, FOPS(NULL, lbs_wrmac_write), },
713         {"rdbbp", 0644, FOPS(lbs_rdbbp_read, lbs_rdbbp_write), },
714         {"wrbbp", 0600, FOPS(NULL, lbs_wrbbp_write), },
715         {"rdrf", 0644, FOPS(lbs_rdrf_read, lbs_rdrf_write), },
716         {"wrrf", 0600, FOPS(NULL, lbs_wrrf_write), },
717 };
718
719 void lbs_debugfs_init(void)
720 {
721         if (!lbs_dir)
722                 lbs_dir = debugfs_create_dir("lbs_wireless", NULL);
723
724         return;
725 }
726
727 void lbs_debugfs_remove(void)
728 {
729         if (lbs_dir)
730                  debugfs_remove(lbs_dir);
731         return;
732 }
733
734 void lbs_debugfs_init_one(struct lbs_private *priv, struct net_device *dev)
735 {
736         int i;
737         struct lbs_debugfs_files *files;
738         if (!lbs_dir)
739                 goto exit;
740
741         priv->debugfs_dir = debugfs_create_dir(dev->name, lbs_dir);
742         if (!priv->debugfs_dir)
743                 goto exit;
744
745         for (i=0; i<ARRAY_SIZE(debugfs_files); i++) {
746                 files = &debugfs_files[i];
747                 priv->debugfs_files[i] = debugfs_create_file(files->name,
748                                                              files->perm,
749                                                              priv->debugfs_dir,
750                                                              priv,
751                                                              &files->fops);
752         }
753
754         priv->events_dir = debugfs_create_dir("subscribed_events", priv->debugfs_dir);
755         if (!priv->events_dir)
756                 goto exit;
757
758         for (i=0; i<ARRAY_SIZE(debugfs_events_files); i++) {
759                 files = &debugfs_events_files[i];
760                 priv->debugfs_events_files[i] = debugfs_create_file(files->name,
761                                                              files->perm,
762                                                              priv->events_dir,
763                                                              priv,
764                                                              &files->fops);
765         }
766
767         priv->regs_dir = debugfs_create_dir("registers", priv->debugfs_dir);
768         if (!priv->regs_dir)
769                 goto exit;
770
771         for (i=0; i<ARRAY_SIZE(debugfs_regs_files); i++) {
772                 files = &debugfs_regs_files[i];
773                 priv->debugfs_regs_files[i] = debugfs_create_file(files->name,
774                                                              files->perm,
775                                                              priv->regs_dir,
776                                                              priv,
777                                                              &files->fops);
778         }
779
780 #ifdef PROC_DEBUG
781         lbs_debug_init(priv);
782 #endif
783 exit:
784         return;
785 }
786
787 void lbs_debugfs_remove_one(struct lbs_private *priv)
788 {
789         int i;
790
791         for(i=0; i<ARRAY_SIZE(debugfs_regs_files); i++)
792                 debugfs_remove(priv->debugfs_regs_files[i]);
793
794         debugfs_remove(priv->regs_dir);
795
796         for(i=0; i<ARRAY_SIZE(debugfs_events_files); i++)
797                 debugfs_remove(priv->debugfs_events_files[i]);
798
799         debugfs_remove(priv->events_dir);
800 #ifdef PROC_DEBUG
801         debugfs_remove(priv->debugfs_debug);
802 #endif
803         for(i=0; i<ARRAY_SIZE(debugfs_files); i++)
804                 debugfs_remove(priv->debugfs_files[i]);
805         debugfs_remove(priv->debugfs_dir);
806 }
807
808
809
810 /* debug entry */
811
812 #ifdef PROC_DEBUG
813
814 #define item_size(n)    (FIELD_SIZEOF(struct lbs_private, n))
815 #define item_addr(n)    (offsetof(struct lbs_private, n))
816
817
818 struct debug_data {
819         char name[32];
820         u32 size;
821         size_t addr;
822 };
823
824 /* To debug any member of struct lbs_private, simply add one line here.
825  */
826 static struct debug_data items[] = {
827         {"psmode", item_size(psmode), item_addr(psmode)},
828         {"psstate", item_size(psstate), item_addr(psstate)},
829 };
830
831 static int num_of_items = ARRAY_SIZE(items);
832
833 /**
834  *  @brief proc read function
835  *
836  *  @param page    pointer to buffer
837  *  @param s       read data starting position
838  *  @param off     offset
839  *  @param cnt     counter
840  *  @param eof     end of file flag
841  *  @param data    data to output
842  *  @return        number of output data
843  */
844 static ssize_t lbs_debugfs_read(struct file *file, char __user *userbuf,
845                         size_t count, loff_t *ppos)
846 {
847         int val = 0;
848         size_t pos = 0;
849         ssize_t res;
850         char *p;
851         int i;
852         struct debug_data *d;
853         unsigned long addr = get_zeroed_page(GFP_KERNEL);
854         char *buf = (char *)addr;
855
856         p = buf;
857
858         d = (struct debug_data *)file->private_data;
859
860         for (i = 0; i < num_of_items; i++) {
861                 if (d[i].size == 1)
862                         val = *((u8 *) d[i].addr);
863                 else if (d[i].size == 2)
864                         val = *((u16 *) d[i].addr);
865                 else if (d[i].size == 4)
866                         val = *((u32 *) d[i].addr);
867                 else if (d[i].size == 8)
868                         val = *((u64 *) d[i].addr);
869
870                 pos += sprintf(p + pos, "%s=%d\n", d[i].name, val);
871         }
872
873         res = simple_read_from_buffer(userbuf, count, ppos, p, pos);
874
875         free_page(addr);
876         return res;
877 }
878
879 /**
880  *  @brief proc write function
881  *
882  *  @param f       file pointer
883  *  @param buf     pointer to data buffer
884  *  @param cnt     data number to write
885  *  @param data    data to write
886  *  @return        number of data
887  */
888 static ssize_t lbs_debugfs_write(struct file *f, const char __user *buf,
889                             size_t cnt, loff_t *ppos)
890 {
891         int r, i;
892         char *pdata;
893         char *p;
894         char *p0;
895         char *p1;
896         char *p2;
897         struct debug_data *d = (struct debug_data *)f->private_data;
898
899         pdata = kmalloc(cnt, GFP_KERNEL);
900         if (pdata == NULL)
901                 return 0;
902
903         if (copy_from_user(pdata, buf, cnt)) {
904                 lbs_deb_debugfs("Copy from user failed\n");
905                 kfree(pdata);
906                 return 0;
907         }
908
909         p0 = pdata;
910         for (i = 0; i < num_of_items; i++) {
911                 do {
912                         p = strstr(p0, d[i].name);
913                         if (p == NULL)
914                                 break;
915                         p1 = strchr(p, '\n');
916                         if (p1 == NULL)
917                                 break;
918                         p0 = p1++;
919                         p2 = strchr(p, '=');
920                         if (!p2)
921                                 break;
922                         p2++;
923                         r = simple_strtoul(p2, NULL, 0);
924                         if (d[i].size == 1)
925                                 *((u8 *) d[i].addr) = (u8) r;
926                         else if (d[i].size == 2)
927                                 *((u16 *) d[i].addr) = (u16) r;
928                         else if (d[i].size == 4)
929                                 *((u32 *) d[i].addr) = (u32) r;
930                         else if (d[i].size == 8)
931                                 *((u64 *) d[i].addr) = (u64) r;
932                         break;
933                 } while (1);
934         }
935         kfree(pdata);
936
937         return (ssize_t)cnt;
938 }
939
940 static struct file_operations lbs_debug_fops = {
941         .owner = THIS_MODULE,
942         .open = open_file_generic,
943         .write = lbs_debugfs_write,
944         .read = lbs_debugfs_read,
945 };
946
947 /**
948  *  @brief create debug proc file
949  *
950  *  @param priv    pointer struct lbs_private
951  *  @param dev     pointer net_device
952  *  @return        N/A
953  */
954 static void lbs_debug_init(struct lbs_private *priv)
955 {
956         int i;
957
958         if (!priv->debugfs_dir)
959                 return;
960
961         for (i = 0; i < num_of_items; i++)
962                 items[i].addr += (size_t) priv;
963
964         priv->debugfs_debug = debugfs_create_file("debug", 0644,
965                                                   priv->debugfs_dir, &items[0],
966                                                   &lbs_debug_fops);
967 }
968 #endif