1 #include <linux/dcache.h>
2 #include <linux/debugfs.h>
3 #include <linux/delay.h>
4 #include <linux/hardirq.h>
6 #include <linux/string.h>
7 #include <linux/slab.h>
8 #include <linux/export.h>
14 static struct dentry *lbs_dir;
15 static char *szStates[] = {
21 static void lbs_debug_init(struct lbs_private *priv);
24 static ssize_t write_file_dummy(struct file *file, const char __user *buf,
25 size_t count, loff_t *ppos)
30 static const size_t len = PAGE_SIZE;
32 static ssize_t lbs_dev_info(struct file *file, char __user *userbuf,
33 size_t count, loff_t *ppos)
35 struct lbs_private *priv = file->private_data;
37 unsigned long addr = get_zeroed_page(GFP_KERNEL);
38 char *buf = (char *)addr;
43 pos += snprintf(buf+pos, len-pos, "state = %s\n",
44 szStates[priv->connect_status]);
45 pos += snprintf(buf+pos, len-pos, "region_code = %02x\n",
46 (u32) priv->regioncode);
48 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
54 static ssize_t lbs_sleepparams_write(struct file *file,
55 const char __user *user_buf, size_t count,
58 struct lbs_private *priv = file->private_data;
60 struct sleep_params sp;
61 int p1, p2, p3, p4, p5, p6;
64 buf = memdup_user_nul(user_buf, min(count, len - 1));
68 ret = sscanf(buf, "%d %d %d %d %d %d", &p1, &p2, &p3, &p4, &p5, &p6);
75 sp.sp_stabletime = p3;
76 sp.sp_calcontrol = p4;
77 sp.sp_extsleepclk = p5;
80 ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_SET, &sp);
91 static ssize_t lbs_sleepparams_read(struct file *file, char __user *userbuf,
92 size_t count, loff_t *ppos)
94 struct lbs_private *priv = file->private_data;
97 struct sleep_params sp;
98 unsigned long addr = get_zeroed_page(GFP_KERNEL);
99 char *buf = (char *)addr;
103 ret = lbs_cmd_802_11_sleep_params(priv, CMD_ACT_GET, &sp);
107 pos += snprintf(buf, len, "%d %d %d %d %d %d\n", sp.sp_error,
108 sp.sp_offset, sp.sp_stabletime,
109 sp.sp_calcontrol, sp.sp_extsleepclk,
112 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
119 static ssize_t lbs_host_sleep_write(struct file *file,
120 const char __user *user_buf, size_t count,
123 struct lbs_private *priv = file->private_data;
128 buf = memdup_user_nul(user_buf, min(count, len - 1));
132 ret = sscanf(buf, "%d", &host_sleep);
139 ret = lbs_set_host_sleep(priv, 0);
140 else if (host_sleep == 1) {
141 if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
142 netdev_info(priv->dev,
143 "wake parameters not configured\n");
147 ret = lbs_set_host_sleep(priv, 1);
149 netdev_err(priv->dev, "invalid option\n");
161 static ssize_t lbs_host_sleep_read(struct file *file, char __user *userbuf,
162 size_t count, loff_t *ppos)
164 struct lbs_private *priv = file->private_data;
167 unsigned long addr = get_zeroed_page(GFP_KERNEL);
168 char *buf = (char *)addr;
172 pos += snprintf(buf, len, "%d\n", priv->is_host_sleep_activated);
174 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
181 * When calling CMD_802_11_SUBSCRIBE_EVENT with CMD_ACT_GET, me might
182 * get a bunch of vendor-specific TLVs (a.k.a. IEs) back from the
183 * firmware. Here's an example:
184 * 04 01 02 00 00 00 05 01 02 00 00 00 06 01 02 00
185 * 00 00 07 01 02 00 3c 00 00 00 00 00 00 00 03 03
186 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
188 * The 04 01 is the TLV type (here TLV_TYPE_RSSI_LOW), 02 00 is the length,
189 * 00 00 are the data bytes of this TLV. For this TLV, their meaning is
190 * defined in mrvlietypes_thresholds
192 * This function searches in this TLV data chunk for a given TLV type
193 * and returns a pointer to the first data byte of the TLV, or to NULL
194 * if the TLV hasn't been found.
196 static void *lbs_tlv_find(uint16_t tlv_type, const uint8_t *tlv, uint16_t size)
198 struct mrvl_ie_header *tlv_h;
203 tlv_h = (struct mrvl_ie_header *) tlv;
206 if (tlv_h->type == cpu_to_le16(tlv_type))
208 length = le16_to_cpu(tlv_h->len) + sizeof(*tlv_h);
216 static ssize_t lbs_threshold_read(uint16_t tlv_type, uint16_t event_mask,
217 struct file *file, char __user *userbuf,
218 size_t count, loff_t *ppos)
220 struct cmd_ds_802_11_subscribe_event *subscribed;
221 struct mrvl_ie_thresholds *got;
222 struct lbs_private *priv = file->private_data;
230 buf = (char *)get_zeroed_page(GFP_KERNEL);
234 subscribed = kzalloc(sizeof(*subscribed), GFP_KERNEL);
240 subscribed->hdr.size = cpu_to_le16(sizeof(*subscribed));
241 subscribed->action = cpu_to_le16(CMD_ACT_GET);
243 ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, subscribed);
247 got = lbs_tlv_find(tlv_type, subscribed->tlv, sizeof(subscribed->tlv));
251 events = le16_to_cpu(subscribed->events);
253 pos += snprintf(buf, len, "%d %d %d\n", value, freq,
254 !!(events & event_mask));
257 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
263 free_page((unsigned long)buf);
268 static ssize_t lbs_threshold_write(uint16_t tlv_type, uint16_t event_mask,
270 const char __user *userbuf, size_t count,
273 struct cmd_ds_802_11_subscribe_event *events;
274 struct mrvl_ie_thresholds *tlv;
275 struct lbs_private *priv = file->private_data;
276 int value, freq, new_mask;
281 buf = memdup_user_nul(userbuf, min(count, len - 1));
285 ret = sscanf(buf, "%d %d %d", &value, &freq, &new_mask);
290 events = kzalloc(sizeof(*events), GFP_KERNEL);
296 events->hdr.size = cpu_to_le16(sizeof(*events));
297 events->action = cpu_to_le16(CMD_ACT_GET);
299 ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
303 curr_mask = le16_to_cpu(events->events);
306 new_mask = curr_mask | event_mask;
308 new_mask = curr_mask & ~event_mask;
310 /* Now everything is set and we can send stuff down to the firmware */
312 tlv = (void *)events->tlv;
314 events->action = cpu_to_le16(CMD_ACT_SET);
315 events->events = cpu_to_le16(new_mask);
316 tlv->header.type = cpu_to_le16(tlv_type);
317 tlv->header.len = cpu_to_le16(sizeof(*tlv) - sizeof(tlv->header));
319 if (tlv_type != TLV_TYPE_BCNMISS)
322 /* The command header, the action, the event mask, and one TLV */
323 events->hdr.size = cpu_to_le16(sizeof(events->hdr) + 4 + sizeof(*tlv));
325 ret = lbs_cmd_with_response(priv, CMD_802_11_SUBSCRIBE_EVENT, events);
337 static ssize_t lbs_lowrssi_read(struct file *file, char __user *userbuf,
338 size_t count, loff_t *ppos)
340 return lbs_threshold_read(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
341 file, userbuf, count, ppos);
345 static ssize_t lbs_lowrssi_write(struct file *file, const char __user *userbuf,
346 size_t count, loff_t *ppos)
348 return lbs_threshold_write(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
349 file, userbuf, count, ppos);
353 static ssize_t lbs_lowsnr_read(struct file *file, char __user *userbuf,
354 size_t count, loff_t *ppos)
356 return lbs_threshold_read(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
357 file, userbuf, count, ppos);
361 static ssize_t lbs_lowsnr_write(struct file *file, const char __user *userbuf,
362 size_t count, loff_t *ppos)
364 return lbs_threshold_write(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
365 file, userbuf, count, ppos);
369 static ssize_t lbs_failcount_read(struct file *file, char __user *userbuf,
370 size_t count, loff_t *ppos)
372 return lbs_threshold_read(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
373 file, userbuf, count, ppos);
377 static ssize_t lbs_failcount_write(struct file *file, const char __user *userbuf,
378 size_t count, loff_t *ppos)
380 return lbs_threshold_write(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
381 file, userbuf, count, ppos);
385 static ssize_t lbs_highrssi_read(struct file *file, char __user *userbuf,
386 size_t count, loff_t *ppos)
388 return lbs_threshold_read(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
389 file, userbuf, count, ppos);
393 static ssize_t lbs_highrssi_write(struct file *file, const char __user *userbuf,
394 size_t count, loff_t *ppos)
396 return lbs_threshold_write(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
397 file, userbuf, count, ppos);
401 static ssize_t lbs_highsnr_read(struct file *file, char __user *userbuf,
402 size_t count, loff_t *ppos)
404 return lbs_threshold_read(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
405 file, userbuf, count, ppos);
409 static ssize_t lbs_highsnr_write(struct file *file, const char __user *userbuf,
410 size_t count, loff_t *ppos)
412 return lbs_threshold_write(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
413 file, userbuf, count, ppos);
416 static ssize_t lbs_bcnmiss_read(struct file *file, char __user *userbuf,
417 size_t count, loff_t *ppos)
419 return lbs_threshold_read(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
420 file, userbuf, count, ppos);
424 static ssize_t lbs_bcnmiss_write(struct file *file, const char __user *userbuf,
425 size_t count, loff_t *ppos)
427 return lbs_threshold_write(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
428 file, userbuf, count, ppos);
432 static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf,
433 size_t count, loff_t *ppos)
435 struct lbs_private *priv = file->private_data;
438 unsigned long addr = get_zeroed_page(GFP_KERNEL);
439 char *buf = (char *)addr;
445 ret = lbs_get_reg(priv, CMD_MAC_REG_ACCESS, priv->mac_offset, &val);
448 pos = snprintf(buf, len, "MAC[0x%x] = 0x%08x\n",
449 priv->mac_offset, val);
450 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
456 static ssize_t lbs_rdmac_write(struct file *file,
457 const char __user *userbuf,
458 size_t count, loff_t *ppos)
460 struct lbs_private *priv = file->private_data;
463 buf = memdup_user_nul(userbuf, min(count, len - 1));
467 priv->mac_offset = simple_strtoul(buf, NULL, 16);
472 static ssize_t lbs_wrmac_write(struct file *file,
473 const char __user *userbuf,
474 size_t count, loff_t *ppos)
477 struct lbs_private *priv = file->private_data;
482 buf = memdup_user_nul(userbuf, min(count, len - 1));
486 res = sscanf(buf, "%x %x", &offset, &value);
492 res = lbs_set_reg(priv, CMD_MAC_REG_ACCESS, offset, value);
502 static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf,
503 size_t count, loff_t *ppos)
505 struct lbs_private *priv = file->private_data;
508 unsigned long addr = get_zeroed_page(GFP_KERNEL);
509 char *buf = (char *)addr;
515 ret = lbs_get_reg(priv, CMD_BBP_REG_ACCESS, priv->bbp_offset, &val);
518 pos = snprintf(buf, len, "BBP[0x%x] = 0x%08x\n",
519 priv->bbp_offset, val);
520 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
527 static ssize_t lbs_rdbbp_write(struct file *file,
528 const char __user *userbuf,
529 size_t count, loff_t *ppos)
531 struct lbs_private *priv = file->private_data;
534 buf = memdup_user_nul(userbuf, min(count, len - 1));
538 priv->bbp_offset = simple_strtoul(buf, NULL, 16);
544 static ssize_t lbs_wrbbp_write(struct file *file,
545 const char __user *userbuf,
546 size_t count, loff_t *ppos)
549 struct lbs_private *priv = file->private_data;
554 buf = memdup_user_nul(userbuf, min(count, len - 1));
558 res = sscanf(buf, "%x %x", &offset, &value);
564 res = lbs_set_reg(priv, CMD_BBP_REG_ACCESS, offset, value);
574 static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf,
575 size_t count, loff_t *ppos)
577 struct lbs_private *priv = file->private_data;
580 unsigned long addr = get_zeroed_page(GFP_KERNEL);
581 char *buf = (char *)addr;
587 ret = lbs_get_reg(priv, CMD_RF_REG_ACCESS, priv->rf_offset, &val);
590 pos = snprintf(buf, len, "RF[0x%x] = 0x%08x\n",
591 priv->rf_offset, val);
592 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
599 static ssize_t lbs_rdrf_write(struct file *file,
600 const char __user *userbuf,
601 size_t count, loff_t *ppos)
603 struct lbs_private *priv = file->private_data;
606 buf = memdup_user_nul(userbuf, min(count, len - 1));
610 priv->rf_offset = simple_strtoul(buf, NULL, 16);
615 static ssize_t lbs_wrrf_write(struct file *file,
616 const char __user *userbuf,
617 size_t count, loff_t *ppos)
620 struct lbs_private *priv = file->private_data;
625 buf = memdup_user_nul(userbuf, min(count, len - 1));
629 res = sscanf(buf, "%x %x", &offset, &value);
635 res = lbs_set_reg(priv, CMD_RF_REG_ACCESS, offset, value);
645 #define FOPS(fread, fwrite) { \
646 .owner = THIS_MODULE, \
647 .open = simple_open, \
650 .llseek = generic_file_llseek, \
653 struct lbs_debugfs_files {
656 struct file_operations fops;
659 static const struct lbs_debugfs_files debugfs_files[] = {
660 { "info", 0444, FOPS(lbs_dev_info, write_file_dummy), },
661 { "sleepparams", 0644, FOPS(lbs_sleepparams_read,
662 lbs_sleepparams_write), },
663 { "hostsleep", 0644, FOPS(lbs_host_sleep_read,
664 lbs_host_sleep_write), },
667 static const struct lbs_debugfs_files debugfs_events_files[] = {
668 {"low_rssi", 0644, FOPS(lbs_lowrssi_read,
669 lbs_lowrssi_write), },
670 {"low_snr", 0644, FOPS(lbs_lowsnr_read,
671 lbs_lowsnr_write), },
672 {"failure_count", 0644, FOPS(lbs_failcount_read,
673 lbs_failcount_write), },
674 {"beacon_missed", 0644, FOPS(lbs_bcnmiss_read,
675 lbs_bcnmiss_write), },
676 {"high_rssi", 0644, FOPS(lbs_highrssi_read,
677 lbs_highrssi_write), },
678 {"high_snr", 0644, FOPS(lbs_highsnr_read,
679 lbs_highsnr_write), },
682 static const struct lbs_debugfs_files debugfs_regs_files[] = {
683 {"rdmac", 0644, FOPS(lbs_rdmac_read, lbs_rdmac_write), },
684 {"wrmac", 0600, FOPS(NULL, lbs_wrmac_write), },
685 {"rdbbp", 0644, FOPS(lbs_rdbbp_read, lbs_rdbbp_write), },
686 {"wrbbp", 0600, FOPS(NULL, lbs_wrbbp_write), },
687 {"rdrf", 0644, FOPS(lbs_rdrf_read, lbs_rdrf_write), },
688 {"wrrf", 0600, FOPS(NULL, lbs_wrrf_write), },
691 void lbs_debugfs_init(void)
694 lbs_dir = debugfs_create_dir("lbs_wireless", NULL);
697 void lbs_debugfs_remove(void)
699 debugfs_remove(lbs_dir);
702 void lbs_debugfs_init_one(struct lbs_private *priv, struct net_device *dev)
705 const struct lbs_debugfs_files *files;
709 priv->debugfs_dir = debugfs_create_dir(dev->name, lbs_dir);
710 if (!priv->debugfs_dir)
713 for (i=0; i<ARRAY_SIZE(debugfs_files); i++) {
714 files = &debugfs_files[i];
715 priv->debugfs_files[i] = debugfs_create_file(files->name,
722 priv->events_dir = debugfs_create_dir("subscribed_events", priv->debugfs_dir);
723 if (!priv->events_dir)
726 for (i=0; i<ARRAY_SIZE(debugfs_events_files); i++) {
727 files = &debugfs_events_files[i];
728 priv->debugfs_events_files[i] = debugfs_create_file(files->name,
735 priv->regs_dir = debugfs_create_dir("registers", priv->debugfs_dir);
739 for (i=0; i<ARRAY_SIZE(debugfs_regs_files); i++) {
740 files = &debugfs_regs_files[i];
741 priv->debugfs_regs_files[i] = debugfs_create_file(files->name,
749 lbs_debug_init(priv);
755 void lbs_debugfs_remove_one(struct lbs_private *priv)
759 for(i=0; i<ARRAY_SIZE(debugfs_regs_files); i++)
760 debugfs_remove(priv->debugfs_regs_files[i]);
762 debugfs_remove(priv->regs_dir);
764 for(i=0; i<ARRAY_SIZE(debugfs_events_files); i++)
765 debugfs_remove(priv->debugfs_events_files[i]);
767 debugfs_remove(priv->events_dir);
769 debugfs_remove(priv->debugfs_debug);
771 for(i=0; i<ARRAY_SIZE(debugfs_files); i++)
772 debugfs_remove(priv->debugfs_files[i]);
773 debugfs_remove(priv->debugfs_dir);
782 #define item_size(n) (FIELD_SIZEOF(struct lbs_private, n))
783 #define item_addr(n) (offsetof(struct lbs_private, n))
792 /* To debug any member of struct lbs_private, simply add one line here.
794 static struct debug_data items[] = {
795 {"psmode", item_size(psmode), item_addr(psmode)},
796 {"psstate", item_size(psstate), item_addr(psstate)},
799 static int num_of_items = ARRAY_SIZE(items);
802 * lbs_debugfs_read - proc read function
804 * @file: file to read
805 * @userbuf: pointer to buffer
806 * @count: number of bytes to read
807 * @ppos: read data starting position
809 * returns: amount of data read or negative error code
811 static ssize_t lbs_debugfs_read(struct file *file, char __user *userbuf,
812 size_t count, loff_t *ppos)
819 struct debug_data *d;
820 unsigned long addr = get_zeroed_page(GFP_KERNEL);
821 char *buf = (char *)addr;
827 d = file->private_data;
829 for (i = 0; i < num_of_items; i++) {
831 val = *((u8 *) d[i].addr);
832 else if (d[i].size == 2)
833 val = *((u16 *) d[i].addr);
834 else if (d[i].size == 4)
835 val = *((u32 *) d[i].addr);
836 else if (d[i].size == 8)
837 val = *((u64 *) d[i].addr);
839 pos += sprintf(p + pos, "%s=%d\n", d[i].name, val);
842 res = simple_read_from_buffer(userbuf, count, ppos, p, pos);
849 * lbs_debugfs_write - proc write function
852 * @buf: pointer to data buffer
853 * @cnt: data number to write
854 * @ppos: file position
856 * returns: amount of data written
858 static ssize_t lbs_debugfs_write(struct file *f, const char __user *buf,
859 size_t cnt, loff_t *ppos)
867 struct debug_data *d = f->private_data;
872 pdata = memdup_user_nul(buf, cnt);
874 return PTR_ERR(pdata);
877 for (i = 0; i < num_of_items; i++) {
879 p = strstr(p0, d[i].name);
882 p1 = strchr(p, '\n');
890 r = simple_strtoul(p2, NULL, 0);
892 *((u8 *) d[i].addr) = (u8) r;
893 else if (d[i].size == 2)
894 *((u16 *) d[i].addr) = (u16) r;
895 else if (d[i].size == 4)
896 *((u32 *) d[i].addr) = (u32) r;
897 else if (d[i].size == 8)
898 *((u64 *) d[i].addr) = (u64) r;
907 static const struct file_operations lbs_debug_fops = {
908 .owner = THIS_MODULE,
910 .write = lbs_debugfs_write,
911 .read = lbs_debugfs_read,
912 .llseek = default_llseek,
916 * lbs_debug_init - create debug proc file
918 * @priv: pointer to &struct lbs_private
922 static void lbs_debug_init(struct lbs_private *priv)
926 if (!priv->debugfs_dir)
929 for (i = 0; i < num_of_items; i++)
930 items[i].addr += (size_t) priv;
932 priv->debugfs_debug = debugfs_create_file("debug", 0644,
933 priv->debugfs_dir, &items[0],