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