2174717e4685807b9e6ba5ab560a813751e80e06
[10.03/openwrt.git] / package / libertas / src / 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, struct net_device *dev);
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 | %04ld | %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 static ssize_t lbs_extscan(struct file *file, const char __user *userbuf,
168                                   size_t count, loff_t *ppos)
169 {
170         struct lbs_private *priv = file->private_data;
171         ssize_t res, buf_size;
172         union iwreq_data wrqu;
173         unsigned long addr = get_zeroed_page(GFP_KERNEL);
174         char *buf = (char *)addr;
175
176         buf_size = min(count, len - 1);
177         if (copy_from_user(buf, userbuf, buf_size)) {
178                 res = -EFAULT;
179                 goto out_unlock;
180         }
181
182         lbs_send_specific_ssid_scan(priv, buf, strlen(buf)-1, 0);
183
184         memset(&wrqu, 0, sizeof(union iwreq_data));
185         wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
186
187 out_unlock:
188         free_page(addr);
189         return count;
190 }
191
192 static void lbs_parse_bssid(char *buf, size_t count,
193         struct lbs_ioctl_user_scan_cfg *scan_cfg)
194 {
195         char *hold;
196         unsigned int mac[ETH_ALEN];
197
198         hold = strstr(buf, "bssid=");
199         if (!hold)
200                 return;
201         hold += 6;
202         sscanf(hold, MAC_FMT, mac, mac+1, mac+2, mac+3, mac+4, mac+5);
203         memcpy(scan_cfg->bssid, mac, ETH_ALEN);
204 }
205
206 static void lbs_parse_ssid(char *buf, size_t count,
207         struct lbs_ioctl_user_scan_cfg *scan_cfg)
208 {
209         char *hold, *end;
210         ssize_t size;
211
212         hold = strstr(buf, "ssid=");
213         if (!hold)
214                 return;
215         hold += 5;
216         end = strchr(hold, ' ');
217         if (!end)
218                 end = buf + count - 1;
219
220         size = min((size_t)IW_ESSID_MAX_SIZE, (size_t) (end - hold));
221         strncpy(scan_cfg->ssid, hold, size);
222
223         return;
224 }
225
226 static int lbs_parse_clear(char *buf, size_t count, const char *tag)
227 {
228         char *hold;
229         int val;
230
231         hold = strstr(buf, tag);
232         if (!hold)
233                 return 0;
234         hold += strlen(tag);
235         sscanf(hold, "%d", &val);
236
237         if (val != 0)
238                 val = 1;
239
240         return val;
241 }
242
243 static int lbs_parse_dur(char *buf, size_t count,
244         struct lbs_ioctl_user_scan_cfg *scan_cfg)
245 {
246         char *hold;
247         int val;
248
249         hold = strstr(buf, "dur=");
250         if (!hold)
251                 return 0;
252         hold += 4;
253         sscanf(hold, "%d", &val);
254
255         return val;
256 }
257
258 static void lbs_parse_type(char *buf, size_t count,
259         struct lbs_ioctl_user_scan_cfg *scan_cfg)
260 {
261         char *hold;
262         int val;
263
264         hold = strstr(buf, "type=");
265         if (!hold)
266                 return;
267         hold += 5;
268         sscanf(hold, "%d", &val);
269
270         /* type=1,2 or 3 */
271         if (val < 1 || val > 3)
272                 return;
273
274         scan_cfg->bsstype = val;
275
276         return;
277 }
278
279 static ssize_t lbs_setuserscan(struct file *file,
280                                     const char __user *userbuf,
281                                     size_t count, loff_t *ppos)
282 {
283         struct lbs_private *priv = file->private_data;
284         ssize_t res, buf_size;
285         struct lbs_ioctl_user_scan_cfg *scan_cfg;
286         union iwreq_data wrqu;
287         int dur;
288         char *buf = (char *)get_zeroed_page(GFP_KERNEL);
289
290         if (!buf)
291                 return -ENOMEM;
292
293         buf_size = min(count, len - 1);
294         if (copy_from_user(buf, userbuf, buf_size)) {
295                 res = -EFAULT;
296                 goto out_buf;
297         }
298
299         scan_cfg = kzalloc(sizeof(struct lbs_ioctl_user_scan_cfg), GFP_KERNEL);
300         if (!scan_cfg) {
301                 res = -ENOMEM;
302                 goto out_buf;
303         }
304         res = count;
305
306         scan_cfg->bsstype = LBS_SCAN_BSS_TYPE_ANY;
307
308         dur = lbs_parse_dur(buf, count, scan_cfg);
309         lbs_parse_bssid(buf, count, scan_cfg);
310         scan_cfg->clear_bssid = lbs_parse_clear(buf, count, "clear_bssid=");
311         lbs_parse_ssid(buf, count, scan_cfg);
312         scan_cfg->clear_ssid = lbs_parse_clear(buf, count, "clear_ssid=");
313         lbs_parse_type(buf, count, scan_cfg);
314
315         lbs_scan_networks(priv, scan_cfg, 1);
316         wait_event_interruptible(priv->cmd_pending,
317                                  priv->surpriseremoved || !priv->last_scanned_channel);
318
319         if (priv->surpriseremoved)
320                 goto out_scan_cfg;
321
322         memset(&wrqu, 0x00, sizeof(union iwreq_data));
323         wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
324
325  out_scan_cfg:
326         kfree(scan_cfg);
327  out_buf:
328         free_page((unsigned long)buf);
329         return res;
330 }
331
332
333 /*
334  * When calling CMD_802_11_SUBSCRIBE_EVENT with CMD_ACT_GET, me might
335  * get a bunch of vendor-specific TLVs (a.k.a. IEs) back from the
336  * firmware. Here's an example:
337  *      04 01 02 00 00 00 05 01 02 00 00 00 06 01 02 00
338  *      00 00 07 01 02 00 3c 00 00 00 00 00 00 00 03 03
339  *      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
340  *
341  * The 04 01 is the TLV type (here TLV_TYPE_RSSI_LOW), 02 00 is the length,
342  * 00 00 are the data bytes of this TLV. For this TLV, their meaning is
343  * defined in mrvlietypes_thresholds
344  *
345  * This function searches in this TLV data chunk for a given TLV type
346  * and returns a pointer to the first data byte of the TLV, or to NULL
347  * if the TLV hasn't been found.
348  */
349 static void *lbs_tlv_find(uint16_t tlv_type, const uint8_t *tlv, uint16_t size)
350 {
351         struct mrvlietypesheader *tlv_h;
352         uint16_t length;
353         ssize_t pos = 0;
354
355         while (pos < size) {
356                 tlv_h = (struct mrvlietypesheader *) tlv;
357                 if (!tlv_h->len)
358                         return NULL;
359                 if (tlv_h->type == cpu_to_le16(tlv_type))
360                         return tlv_h;
361                 length = le16_to_cpu(tlv_h->len) + sizeof(*tlv_h);
362                 pos += length;
363                 tlv += length;
364         }
365         return NULL;
366 }
367
368
369 static ssize_t lbs_threshold_read(uint16_t tlv_type, uint16_t event_mask,
370                                   struct file *file, char __user *userbuf,
371                                   size_t count, loff_t *ppos)
372 {
373         struct cmd_ds_802_11_subscribe_event *subscribed;
374         struct mrvlietypes_thresholds *got;
375         struct lbs_private *priv = file->private_data;
376         ssize_t ret = 0;
377         size_t pos = 0;
378         char *buf;
379         u8 value;
380         u8 freq;
381         int events = 0;
382
383         buf = (char *)get_zeroed_page(GFP_KERNEL);
384         if (!buf)
385                 return -ENOMEM;
386
387         subscribed = kzalloc(sizeof(*subscribed), GFP_KERNEL);
388         if (!subscribed) {
389                 ret = -ENOMEM;
390                 goto out_page;
391         }
392
393         subscribed->hdr.size = cpu_to_le16(sizeof(*subscribed));
394         subscribed->action = cpu_to_le16(CMD_ACT_GET);
395
396         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, subscribed);
397         if (ret)
398                 goto out_cmd;
399
400         got = lbs_tlv_find(tlv_type, subscribed->tlv, sizeof(subscribed->tlv));
401         if (got) {
402                 value = got->value;
403                 freq  = got->freq;
404                 events = le16_to_cpu(subscribed->events);
405
406                 pos += snprintf(buf, len, "%d %d %d\n", value, freq,
407                                 !!(events & event_mask));
408         }
409
410         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
411
412  out_cmd:
413         kfree(subscribed);
414
415  out_page:
416         free_page((unsigned long)buf);
417         return ret;
418 }
419
420
421 static ssize_t lbs_threshold_write(uint16_t tlv_type, uint16_t event_mask,
422                                    struct file *file,
423                                    const char __user *userbuf, size_t count,
424                                    loff_t *ppos)
425 {
426         struct cmd_ds_802_11_subscribe_event *events;
427         struct mrvlietypes_thresholds *tlv;
428         struct lbs_private *priv = file->private_data;
429         ssize_t buf_size;
430         int value, freq, new_mask;
431         uint16_t curr_mask;
432         char *buf;
433         int ret;
434
435         buf = (char *)get_zeroed_page(GFP_KERNEL);
436         if (!buf)
437                 return -ENOMEM;
438
439         buf_size = min(count, len - 1);
440         if (copy_from_user(buf, userbuf, buf_size)) {
441                 ret = -EFAULT;
442                 goto out_page;
443         }
444         ret = sscanf(buf, "%d %d %d", &value, &freq, &new_mask);
445         if (ret != 3) {
446                 ret = -EINVAL;
447                 goto out_page;
448         }
449         events = kzalloc(sizeof(*events), GFP_KERNEL);
450         if (!events) {
451                 ret = -ENOMEM;
452                 goto out_page;
453         }
454
455         events->hdr.size = cpu_to_le16(sizeof(*events));
456         events->action = cpu_to_le16(CMD_ACT_GET);
457
458         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
459         if (ret)
460                 goto out_events;
461
462         curr_mask = le16_to_cpu(events->events);
463
464         if (new_mask)
465                 new_mask = curr_mask | event_mask;
466         else
467                 new_mask = curr_mask & ~event_mask;
468
469         /* Now everything is set and we can send stuff down to the firmware */
470
471         tlv = (void *)events->tlv;
472
473         events->action = cpu_to_le16(CMD_ACT_SET);
474         events->events = cpu_to_le16(new_mask);
475         tlv->header.type = cpu_to_le16(tlv_type);
476         tlv->header.len = cpu_to_le16(sizeof(*tlv) - sizeof(tlv->header));
477         tlv->value = value;
478         if (tlv_type != TLV_TYPE_BCNMISS)
479                 tlv->freq = freq;
480
481         /* The command header, the event mask, and the one TLV */
482         events->hdr.size = cpu_to_le16(sizeof(events->hdr) + 2 + sizeof(*tlv));
483
484         ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
485
486         if (!ret)
487                 ret = count;
488  out_events:
489         kfree(events);
490  out_page:
491         free_page((unsigned long)buf);
492         return ret;
493 }
494
495
496 static ssize_t lbs_lowrssi_read(struct file *file, char __user *userbuf,
497                                 size_t count, loff_t *ppos)
498 {
499         return lbs_threshold_read(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
500                                   file, userbuf, count, ppos);
501 }
502
503
504 static ssize_t lbs_lowrssi_write(struct file *file, const char __user *userbuf,
505                                  size_t count, loff_t *ppos)
506 {
507         return lbs_threshold_write(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
508                                    file, userbuf, count, ppos);
509 }
510
511
512 static ssize_t lbs_lowsnr_read(struct file *file, char __user *userbuf,
513                                size_t count, loff_t *ppos)
514 {
515         return lbs_threshold_read(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
516                                   file, userbuf, count, ppos);
517 }
518
519
520 static ssize_t lbs_lowsnr_write(struct file *file, const char __user *userbuf,
521                                 size_t count, loff_t *ppos)
522 {
523         return lbs_threshold_write(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
524                                    file, userbuf, count, ppos);
525 }
526
527
528 static ssize_t lbs_failcount_read(struct file *file, char __user *userbuf,
529                                   size_t count, loff_t *ppos)
530 {
531         return lbs_threshold_read(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
532                                   file, userbuf, count, ppos);
533 }
534
535
536 static ssize_t lbs_failcount_write(struct file *file, const char __user *userbuf,
537                                    size_t count, loff_t *ppos)
538 {
539         return lbs_threshold_write(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
540                                    file, userbuf, count, ppos);
541 }
542
543
544 static ssize_t lbs_highrssi_read(struct file *file, char __user *userbuf,
545                                  size_t count, loff_t *ppos)
546 {
547         return lbs_threshold_read(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
548                                   file, userbuf, count, ppos);
549 }
550
551
552 static ssize_t lbs_highrssi_write(struct file *file, const char __user *userbuf,
553                                   size_t count, loff_t *ppos)
554 {
555         return lbs_threshold_write(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
556                                    file, userbuf, count, ppos);
557 }
558
559
560 static ssize_t lbs_highsnr_read(struct file *file, char __user *userbuf,
561                                 size_t count, loff_t *ppos)
562 {
563         return lbs_threshold_read(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
564                                   file, userbuf, count, ppos);
565 }
566
567
568 static ssize_t lbs_highsnr_write(struct file *file, const char __user *userbuf,
569                                  size_t count, loff_t *ppos)
570 {
571         return lbs_threshold_write(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
572                                    file, userbuf, count, ppos);
573 }
574
575 static ssize_t lbs_bcnmiss_read(struct file *file, char __user *userbuf,
576                                 size_t count, loff_t *ppos)
577 {
578         return lbs_threshold_read(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
579                                   file, userbuf, count, ppos);
580 }
581
582
583 static ssize_t lbs_bcnmiss_write(struct file *file, const char __user *userbuf,
584                                  size_t count, loff_t *ppos)
585 {
586         return lbs_threshold_write(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
587                                    file, userbuf, count, ppos);
588 }
589
590
591
592 static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf,
593                                   size_t count, loff_t *ppos)
594 {
595         struct lbs_private *priv = file->private_data;
596         struct lbs_offset_value offval;
597         ssize_t pos = 0;
598         int ret;
599         unsigned long addr = get_zeroed_page(GFP_KERNEL);
600         char *buf = (char *)addr;
601
602         offval.offset = priv->mac_offset;
603         offval.value = 0;
604
605         ret = lbs_prepare_and_send_command(priv,
606                                 CMD_MAC_REG_ACCESS, 0,
607                                 CMD_OPTION_WAITFORRSP, 0, &offval);
608         mdelay(10);
609         pos += snprintf(buf+pos, len-pos, "MAC[0x%x] = 0x%08x\n",
610                                 priv->mac_offset, priv->offsetvalue.value);
611
612         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
613         free_page(addr);
614         return ret;
615 }
616
617 static ssize_t lbs_rdmac_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->mac_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_wrmac_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_MAC_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 static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf,
675                                   size_t count, loff_t *ppos)
676 {
677         struct lbs_private *priv = file->private_data;
678         struct lbs_offset_value offval;
679         ssize_t pos = 0;
680         int ret;
681         unsigned long addr = get_zeroed_page(GFP_KERNEL);
682         char *buf = (char *)addr;
683
684         offval.offset = priv->bbp_offset;
685         offval.value = 0;
686
687         ret = lbs_prepare_and_send_command(priv,
688                                 CMD_BBP_REG_ACCESS, 0,
689                                 CMD_OPTION_WAITFORRSP, 0, &offval);
690         mdelay(10);
691         pos += snprintf(buf+pos, len-pos, "BBP[0x%x] = 0x%08x\n",
692                                 priv->bbp_offset, priv->offsetvalue.value);
693
694         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
695         free_page(addr);
696
697         return ret;
698 }
699
700 static ssize_t lbs_rdbbp_write(struct file *file,
701                                     const char __user *userbuf,
702                                     size_t count, loff_t *ppos)
703 {
704         struct lbs_private *priv = file->private_data;
705         ssize_t res, buf_size;
706         unsigned long addr = get_zeroed_page(GFP_KERNEL);
707         char *buf = (char *)addr;
708
709         buf_size = min(count, len - 1);
710         if (copy_from_user(buf, userbuf, buf_size)) {
711                 res = -EFAULT;
712                 goto out_unlock;
713         }
714         priv->bbp_offset = simple_strtoul((char *)buf, NULL, 16);
715         res = count;
716 out_unlock:
717         free_page(addr);
718         return res;
719 }
720
721 static ssize_t lbs_wrbbp_write(struct file *file,
722                                     const char __user *userbuf,
723                                     size_t count, loff_t *ppos)
724 {
725
726         struct lbs_private *priv = file->private_data;
727         ssize_t res, buf_size;
728         u32 offset, value;
729         struct lbs_offset_value offval;
730         unsigned long addr = get_zeroed_page(GFP_KERNEL);
731         char *buf = (char *)addr;
732
733         buf_size = min(count, len - 1);
734         if (copy_from_user(buf, userbuf, buf_size)) {
735                 res = -EFAULT;
736                 goto out_unlock;
737         }
738         res = sscanf(buf, "%x %x", &offset, &value);
739         if (res != 2) {
740                 res = -EFAULT;
741                 goto out_unlock;
742         }
743
744         offval.offset = offset;
745         offval.value = value;
746         res = lbs_prepare_and_send_command(priv,
747                                 CMD_BBP_REG_ACCESS, 1,
748                                 CMD_OPTION_WAITFORRSP, 0, &offval);
749         mdelay(10);
750
751         res = count;
752 out_unlock:
753         free_page(addr);
754         return res;
755 }
756
757 static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf,
758                                   size_t count, loff_t *ppos)
759 {
760         struct lbs_private *priv = file->private_data;
761         struct lbs_offset_value offval;
762         ssize_t pos = 0;
763         int ret;
764         unsigned long addr = get_zeroed_page(GFP_KERNEL);
765         char *buf = (char *)addr;
766
767         offval.offset = priv->rf_offset;
768         offval.value = 0;
769
770         ret = lbs_prepare_and_send_command(priv,
771                                 CMD_RF_REG_ACCESS, 0,
772                                 CMD_OPTION_WAITFORRSP, 0, &offval);
773         mdelay(10);
774         pos += snprintf(buf+pos, len-pos, "RF[0x%x] = 0x%08x\n",
775                                 priv->rf_offset, priv->offsetvalue.value);
776
777         ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
778         free_page(addr);
779
780         return ret;
781 }
782
783 static ssize_t lbs_rdrf_write(struct file *file,
784                                     const char __user *userbuf,
785                                     size_t count, loff_t *ppos)
786 {
787         struct lbs_private *priv = file->private_data;
788         ssize_t res, buf_size;
789         unsigned long addr = get_zeroed_page(GFP_KERNEL);
790         char *buf = (char *)addr;
791
792         buf_size = min(count, len - 1);
793         if (copy_from_user(buf, userbuf, buf_size)) {
794                 res = -EFAULT;
795                 goto out_unlock;
796         }
797         priv->rf_offset = simple_strtoul((char *)buf, NULL, 16);
798         res = count;
799 out_unlock:
800         free_page(addr);
801         return res;
802 }
803
804 static ssize_t lbs_wrrf_write(struct file *file,
805                                     const char __user *userbuf,
806                                     size_t count, loff_t *ppos)
807 {
808
809         struct lbs_private *priv = file->private_data;
810         ssize_t res, buf_size;
811         u32 offset, value;
812         struct lbs_offset_value offval;
813         unsigned long addr = get_zeroed_page(GFP_KERNEL);
814         char *buf = (char *)addr;
815
816         buf_size = min(count, len - 1);
817         if (copy_from_user(buf, userbuf, buf_size)) {
818                 res = -EFAULT;
819                 goto out_unlock;
820         }
821         res = sscanf(buf, "%x %x", &offset, &value);
822         if (res != 2) {
823                 res = -EFAULT;
824                 goto out_unlock;
825         }
826
827         offval.offset = offset;
828         offval.value = value;
829         res = lbs_prepare_and_send_command(priv,
830                                 CMD_RF_REG_ACCESS, 1,
831                                 CMD_OPTION_WAITFORRSP, 0, &offval);
832         mdelay(10);
833
834         res = count;
835 out_unlock:
836         free_page(addr);
837         return res;
838 }
839
840 #define FOPS(fread, fwrite) { \
841         .owner = THIS_MODULE, \
842         .open = open_file_generic, \
843         .read = (fread), \
844         .write = (fwrite), \
845 }
846
847 struct lbs_debugfs_files {
848         char *name;
849         int perm;
850         struct file_operations fops;
851 };
852
853 static struct lbs_debugfs_files debugfs_files[] = {
854         { "info", 0444, FOPS(lbs_dev_info, write_file_dummy), },
855         { "getscantable", 0444, FOPS(lbs_getscantable,
856                                         write_file_dummy), },
857         { "sleepparams", 0644, FOPS(lbs_sleepparams_read,
858                                 lbs_sleepparams_write), },
859         { "extscan", 0600, FOPS(NULL, lbs_extscan), },
860         { "setuserscan", 0600, FOPS(NULL, lbs_setuserscan), },
861 };
862
863 static struct lbs_debugfs_files debugfs_events_files[] = {
864         {"low_rssi", 0644, FOPS(lbs_lowrssi_read,
865                                 lbs_lowrssi_write), },
866         {"low_snr", 0644, FOPS(lbs_lowsnr_read,
867                                 lbs_lowsnr_write), },
868         {"failure_count", 0644, FOPS(lbs_failcount_read,
869                                 lbs_failcount_write), },
870         {"beacon_missed", 0644, FOPS(lbs_bcnmiss_read,
871                                 lbs_bcnmiss_write), },
872         {"high_rssi", 0644, FOPS(lbs_highrssi_read,
873                                 lbs_highrssi_write), },
874         {"high_snr", 0644, FOPS(lbs_highsnr_read,
875                                 lbs_highsnr_write), },
876 };
877
878 static struct lbs_debugfs_files debugfs_regs_files[] = {
879         {"rdmac", 0644, FOPS(lbs_rdmac_read, lbs_rdmac_write), },
880         {"wrmac", 0600, FOPS(NULL, lbs_wrmac_write), },
881         {"rdbbp", 0644, FOPS(lbs_rdbbp_read, lbs_rdbbp_write), },
882         {"wrbbp", 0600, FOPS(NULL, lbs_wrbbp_write), },
883         {"rdrf", 0644, FOPS(lbs_rdrf_read, lbs_rdrf_write), },
884         {"wrrf", 0600, FOPS(NULL, lbs_wrrf_write), },
885 };
886
887 void lbs_debugfs_init(void)
888 {
889         if (!lbs_dir)
890                 lbs_dir = debugfs_create_dir("lbs_wireless", NULL);
891
892         return;
893 }
894
895 void lbs_debugfs_remove(void)
896 {
897         if (lbs_dir)
898                  debugfs_remove(lbs_dir);
899         return;
900 }
901
902 void lbs_debugfs_init_one(struct lbs_private *priv, struct net_device *dev)
903 {
904         int i;
905         struct lbs_debugfs_files *files;
906         if (!lbs_dir)
907                 goto exit;
908
909         priv->debugfs_dir = debugfs_create_dir(dev->name, lbs_dir);
910         if (!priv->debugfs_dir)
911                 goto exit;
912
913         for (i=0; i<ARRAY_SIZE(debugfs_files); i++) {
914                 files = &debugfs_files[i];
915                 priv->debugfs_files[i] = debugfs_create_file(files->name,
916                                                              files->perm,
917                                                              priv->debugfs_dir,
918                                                              priv,
919                                                              &files->fops);
920         }
921
922         priv->events_dir = debugfs_create_dir("subscribed_events", priv->debugfs_dir);
923         if (!priv->events_dir)
924                 goto exit;
925
926         for (i=0; i<ARRAY_SIZE(debugfs_events_files); i++) {
927                 files = &debugfs_events_files[i];
928                 priv->debugfs_events_files[i] = debugfs_create_file(files->name,
929                                                              files->perm,
930                                                              priv->events_dir,
931                                                              priv,
932                                                              &files->fops);
933         }
934
935         priv->regs_dir = debugfs_create_dir("registers", priv->debugfs_dir);
936         if (!priv->regs_dir)
937                 goto exit;
938
939         for (i=0; i<ARRAY_SIZE(debugfs_regs_files); i++) {
940                 files = &debugfs_regs_files[i];
941                 priv->debugfs_regs_files[i] = debugfs_create_file(files->name,
942                                                              files->perm,
943                                                              priv->regs_dir,
944                                                              priv,
945                                                              &files->fops);
946         }
947
948 #ifdef PROC_DEBUG
949         lbs_debug_init(priv, dev);
950 #endif
951 exit:
952         return;
953 }
954
955 void lbs_debugfs_remove_one(struct lbs_private *priv)
956 {
957         int i;
958
959         for(i=0; i<ARRAY_SIZE(debugfs_regs_files); i++)
960                 debugfs_remove(priv->debugfs_regs_files[i]);
961
962         debugfs_remove(priv->regs_dir);
963
964         for(i=0; i<ARRAY_SIZE(debugfs_events_files); i++)
965                 debugfs_remove(priv->debugfs_events_files[i]);
966
967         debugfs_remove(priv->events_dir);
968 #ifdef PROC_DEBUG
969         debugfs_remove(priv->debugfs_debug);
970 #endif
971         for(i=0; i<ARRAY_SIZE(debugfs_files); i++)
972                 debugfs_remove(priv->debugfs_files[i]);
973         debugfs_remove(priv->debugfs_dir);
974 }
975
976
977
978 /* debug entry */
979
980 #ifdef PROC_DEBUG
981
982 #define item_size(n)    (FIELD_SIZEOF(struct lbs_private, n))
983 #define item_addr(n)    (offsetof(struct lbs_private, n))
984
985
986 struct debug_data {
987         char name[32];
988         u32 size;
989         size_t addr;
990 };
991
992 /* To debug any member of struct lbs_private, simply add one line here.
993  */
994 static struct debug_data items[] = {
995         {"intcounter", item_size(intcounter), item_addr(intcounter)},
996         {"psmode", item_size(psmode), item_addr(psmode)},
997         {"psstate", item_size(psstate), item_addr(psstate)},
998 };
999
1000 static int num_of_items = ARRAY_SIZE(items);
1001
1002 /**
1003  *  @brief proc read function
1004  *
1005  *  @param page    pointer to buffer
1006  *  @param s       read data starting position
1007  *  @param off     offset
1008  *  @param cnt     counter
1009  *  @param eof     end of file flag
1010  *  @param data    data to output
1011  *  @return        number of output data
1012  */
1013 static ssize_t lbs_debugfs_read(struct file *file, char __user *userbuf,
1014                         size_t count, loff_t *ppos)
1015 {
1016         int val = 0;
1017         size_t pos = 0;
1018         ssize_t res;
1019         char *p;
1020         int i;
1021         struct debug_data *d;
1022         unsigned long addr = get_zeroed_page(GFP_KERNEL);
1023         char *buf = (char *)addr;
1024
1025         p = buf;
1026
1027         d = (struct debug_data *)file->private_data;
1028
1029         for (i = 0; i < num_of_items; i++) {
1030                 if (d[i].size == 1)
1031                         val = *((u8 *) d[i].addr);
1032                 else if (d[i].size == 2)
1033                         val = *((u16 *) d[i].addr);
1034                 else if (d[i].size == 4)
1035                         val = *((u32 *) d[i].addr);
1036                 else if (d[i].size == 8)
1037                         val = *((u64 *) d[i].addr);
1038
1039                 pos += sprintf(p + pos, "%s=%d\n", d[i].name, val);
1040         }
1041
1042         res = simple_read_from_buffer(userbuf, count, ppos, p, pos);
1043
1044         free_page(addr);
1045         return res;
1046 }
1047
1048 /**
1049  *  @brief proc write function
1050  *
1051  *  @param f       file pointer
1052  *  @param buf     pointer to data buffer
1053  *  @param cnt     data number to write
1054  *  @param data    data to write
1055  *  @return        number of data
1056  */
1057 static ssize_t lbs_debugfs_write(struct file *f, const char __user *buf,
1058                             size_t cnt, loff_t *ppos)
1059 {
1060         int r, i;
1061         char *pdata;
1062         char *p;
1063         char *p0;
1064         char *p1;
1065         char *p2;
1066         struct debug_data *d = (struct debug_data *)f->private_data;
1067
1068         pdata = kmalloc(cnt, GFP_KERNEL);
1069         if (pdata == NULL)
1070                 return 0;
1071
1072         if (copy_from_user(pdata, buf, cnt)) {
1073                 lbs_deb_debugfs("Copy from user failed\n");
1074                 kfree(pdata);
1075                 return 0;
1076         }
1077
1078         p0 = pdata;
1079         for (i = 0; i < num_of_items; i++) {
1080                 do {
1081                         p = strstr(p0, d[i].name);
1082                         if (p == NULL)
1083                                 break;
1084                         p1 = strchr(p, '\n');
1085                         if (p1 == NULL)
1086                                 break;
1087                         p0 = p1++;
1088                         p2 = strchr(p, '=');
1089                         if (!p2)
1090                                 break;
1091                         p2++;
1092                         r = simple_strtoul(p2, NULL, 0);
1093                         if (d[i].size == 1)
1094                                 *((u8 *) d[i].addr) = (u8) r;
1095                         else if (d[i].size == 2)
1096                                 *((u16 *) d[i].addr) = (u16) r;
1097                         else if (d[i].size == 4)
1098                                 *((u32 *) d[i].addr) = (u32) r;
1099                         else if (d[i].size == 8)
1100                                 *((u64 *) d[i].addr) = (u64) r;
1101                         break;
1102                 } while (1);
1103         }
1104         kfree(pdata);
1105
1106         return (ssize_t)cnt;
1107 }
1108
1109 static struct file_operations lbs_debug_fops = {
1110         .owner = THIS_MODULE,
1111         .open = open_file_generic,
1112         .write = lbs_debugfs_write,
1113         .read = lbs_debugfs_read,
1114 };
1115
1116 /**
1117  *  @brief create debug proc file
1118  *
1119  *  @param priv    pointer struct lbs_private
1120  *  @param dev     pointer net_device
1121  *  @return        N/A
1122  */
1123 static void lbs_debug_init(struct lbs_private *priv, struct net_device *dev)
1124 {
1125         int i;
1126
1127         if (!priv->debugfs_dir)
1128                 return;
1129
1130         for (i = 0; i < num_of_items; i++)
1131                 items[i].addr += (size_t) priv;
1132
1133         priv->debugfs_debug = debugfs_create_file("debug", 0644,
1134                                                   priv->debugfs_dir, &items[0],
1135                                                   &lbs_debug_fops);
1136 }
1137 #endif