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