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