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