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