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