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