3 Broadcom B43 wireless driver
5 debugfs driver debugging code
7 Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
22 Boston, MA 02110-1301, USA.
27 #include <linux/debugfs.h>
28 #include <linux/slab.h>
29 #include <linux/netdevice.h>
30 #include <linux/pci.h>
31 #include <linux/mutex.h>
40 /* The root directory. */
41 static struct dentry *rootdir;
43 struct b43_debugfs_fops {
44 ssize_t (*read)(struct b43_wldev *dev, char *buf, size_t bufsize);
45 int (*write)(struct b43_wldev *dev, const char *buf, size_t count);
46 struct file_operations fops;
47 /* Offset of struct b43_dfs_file in struct b43_dfsentry */
48 size_t file_struct_offset;
49 /* Take wl->irq_lock before calling read/write? */
54 struct b43_dfs_file * fops_to_dfs_file(struct b43_wldev *dev,
55 const struct b43_debugfs_fops *dfops)
60 p += dfops->file_struct_offset;
66 #define fappend(fmt, x...) \
68 if (bufsize - count) \
69 count += snprintf(buf + count, \
73 printk(KERN_ERR "b43: fappend overflow\n"); \
77 /* The biggest address values for SHM access from the debugfs files. */
78 #define B43_MAX_SHM_ROUTING 4
79 #define B43_MAX_SHM_ADDR 0xFFFF
81 static ssize_t shm16read__read_file(struct b43_wldev *dev,
82 char *buf, size_t bufsize)
85 unsigned int routing, addr;
88 routing = dev->dfsentry->shm16read_routing_next;
89 addr = dev->dfsentry->shm16read_addr_next;
90 if ((routing > B43_MAX_SHM_ROUTING) ||
91 (addr > B43_MAX_SHM_ADDR))
94 val = b43_shm_read16(dev, routing, addr);
95 fappend("0x%04X\n", val);
100 static int shm16read__write_file(struct b43_wldev *dev,
101 const char *buf, size_t count)
103 unsigned int routing, addr;
106 res = sscanf(buf, "0x%X 0x%X", &routing, &addr);
109 if (routing > B43_MAX_SHM_ROUTING)
110 return -EADDRNOTAVAIL;
111 if (addr > B43_MAX_SHM_ADDR)
112 return -EADDRNOTAVAIL;
113 if (routing == B43_SHM_SHARED) {
115 return -EADDRNOTAVAIL;
118 dev->dfsentry->shm16read_routing_next = routing;
119 dev->dfsentry->shm16read_addr_next = addr;
124 static int shm16write__write_file(struct b43_wldev *dev,
125 const char *buf, size_t count)
127 unsigned int routing, addr, mask, set;
132 res = sscanf(buf, "0x%X 0x%X 0x%X 0x%X",
133 &routing, &addr, &mask, &set);
136 if (routing > B43_MAX_SHM_ROUTING)
137 return -EADDRNOTAVAIL;
138 if (addr > B43_MAX_SHM_ADDR)
139 return -EADDRNOTAVAIL;
140 if (routing == B43_SHM_SHARED) {
142 return -EADDRNOTAVAIL;
144 if ((mask > 0xFFFF) || (set > 0xFFFF))
147 spin_lock_irqsave(&dev->wl->shm_lock, flags);
151 val = __b43_shm_read16(dev, routing, addr);
154 __b43_shm_write16(dev, routing, addr, val);
155 spin_unlock_irqrestore(&dev->wl->shm_lock, flags);
160 static ssize_t shm32read__read_file(struct b43_wldev *dev,
161 char *buf, size_t bufsize)
164 unsigned int routing, addr;
167 routing = dev->dfsentry->shm32read_routing_next;
168 addr = dev->dfsentry->shm32read_addr_next;
169 if ((routing > B43_MAX_SHM_ROUTING) ||
170 (addr > B43_MAX_SHM_ADDR))
171 return -EDESTADDRREQ;
173 val = b43_shm_read32(dev, routing, addr);
174 fappend("0x%08X\n", val);
179 static int shm32read__write_file(struct b43_wldev *dev,
180 const char *buf, size_t count)
182 unsigned int routing, addr;
185 res = sscanf(buf, "0x%X 0x%X", &routing, &addr);
188 if (routing > B43_MAX_SHM_ROUTING)
189 return -EADDRNOTAVAIL;
190 if (addr > B43_MAX_SHM_ADDR)
191 return -EADDRNOTAVAIL;
192 if (routing == B43_SHM_SHARED) {
194 return -EADDRNOTAVAIL;
197 dev->dfsentry->shm32read_routing_next = routing;
198 dev->dfsentry->shm32read_addr_next = addr;
203 static int shm32write__write_file(struct b43_wldev *dev,
204 const char *buf, size_t count)
206 unsigned int routing, addr, mask, set;
211 res = sscanf(buf, "0x%X 0x%X 0x%X 0x%X",
212 &routing, &addr, &mask, &set);
215 if (routing > B43_MAX_SHM_ROUTING)
216 return -EADDRNOTAVAIL;
217 if (addr > B43_MAX_SHM_ADDR)
218 return -EADDRNOTAVAIL;
219 if (routing == B43_SHM_SHARED) {
221 return -EADDRNOTAVAIL;
223 if ((mask > 0xFFFFFFFF) || (set > 0xFFFFFFFF))
226 spin_lock_irqsave(&dev->wl->shm_lock, flags);
230 val = __b43_shm_read32(dev, routing, addr);
233 __b43_shm_write32(dev, routing, addr, val);
234 spin_unlock_irqrestore(&dev->wl->shm_lock, flags);
239 /* The biggest MMIO address that we allow access to from the debugfs files. */
240 #define B43_MAX_MMIO_ACCESS (0xF00 - 1)
242 static ssize_t mmio16read__read_file(struct b43_wldev *dev,
243 char *buf, size_t bufsize)
249 addr = dev->dfsentry->mmio16read_next;
250 if (addr > B43_MAX_MMIO_ACCESS)
251 return -EDESTADDRREQ;
253 val = b43_read16(dev, addr);
254 fappend("0x%04X\n", val);
259 static int mmio16read__write_file(struct b43_wldev *dev,
260 const char *buf, size_t count)
265 res = sscanf(buf, "0x%X", &addr);
268 if (addr > B43_MAX_MMIO_ACCESS)
269 return -EADDRNOTAVAIL;
273 dev->dfsentry->mmio16read_next = addr;
278 static int mmio16write__write_file(struct b43_wldev *dev,
279 const char *buf, size_t count)
281 unsigned int addr, mask, set;
285 res = sscanf(buf, "0x%X 0x%X 0x%X", &addr, &mask, &set);
288 if (addr > B43_MAX_MMIO_ACCESS)
289 return -EADDRNOTAVAIL;
290 if ((mask > 0xFFFF) || (set > 0xFFFF))
298 val = b43_read16(dev, addr);
301 b43_write16(dev, addr, val);
306 static ssize_t mmio32read__read_file(struct b43_wldev *dev,
307 char *buf, size_t bufsize)
313 addr = dev->dfsentry->mmio32read_next;
314 if (addr > B43_MAX_MMIO_ACCESS)
315 return -EDESTADDRREQ;
317 val = b43_read32(dev, addr);
318 fappend("0x%08X\n", val);
323 static int mmio32read__write_file(struct b43_wldev *dev,
324 const char *buf, size_t count)
329 res = sscanf(buf, "0x%X", &addr);
332 if (addr > B43_MAX_MMIO_ACCESS)
333 return -EADDRNOTAVAIL;
337 dev->dfsentry->mmio32read_next = addr;
342 static int mmio32write__write_file(struct b43_wldev *dev,
343 const char *buf, size_t count)
345 unsigned int addr, mask, set;
349 res = sscanf(buf, "0x%X 0x%X 0x%X", &addr, &mask, &set);
352 if (addr > B43_MAX_MMIO_ACCESS)
353 return -EADDRNOTAVAIL;
354 if ((mask > 0xFFFFFFFF) || (set > 0xFFFFFFFF))
362 val = b43_read32(dev, addr);
365 b43_write32(dev, addr, val);
370 static ssize_t txstat_read_file(struct b43_wldev *dev,
371 char *buf, size_t bufsize)
373 struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
377 struct b43_txstatus *stat;
379 spin_lock_irqsave(&log->lock, flags);
381 fappend("Nothing transmitted, yet\n");
384 fappend("b43 TX status reports:\n\n"
385 "index | cookie | seq | phy_stat | frame_count | "
386 "rts_count | supp_reason | pm_indicated | "
387 "intermediate | for_ampdu | acked\n" "---\n");
391 if (i == B43_NR_LOGGED_TXSTATUS)
393 stat = &(log->log[i]);
396 "0x%04X | 0x%04X | 0x%02X | "
401 stat->cookie, stat->seq, stat->phy_stat,
402 stat->frame_count, stat->rts_count,
403 stat->supp_reason, stat->pm_indicated,
404 stat->intermediate, stat->for_ampdu,
413 spin_unlock_irqrestore(&log->lock, flags);
418 /* wl->irq_lock is locked */
419 static int restart_write_file(struct b43_wldev *dev,
420 const char *buf, size_t count)
424 if (count > 0 && buf[0] == '1') {
425 b43_controller_restart(dev, "manually restarted");
432 static unsigned long calc_expire_secs(unsigned long now,
434 unsigned long expire)
436 expire = time + expire;
438 if (time_after(now, expire))
439 return 0; /* expired */
441 /* jiffies wrapped */
442 expire -= MAX_JIFFY_OFFSET;
443 now -= MAX_JIFFY_OFFSET;
445 B43_WARN_ON(expire < now);
447 return (expire - now) / HZ;
450 static ssize_t loctls_read_file(struct b43_wldev *dev,
451 char *buf, size_t bufsize)
454 struct b43_txpower_lo_control *lo;
456 struct b43_lo_calib *cal;
457 unsigned long now = jiffies;
458 struct b43_phy *phy = &dev->phy;
460 if (phy->type != B43_PHYTYPE_G) {
461 fappend("Device is not a G-PHY\n");
465 lo = phy->g->lo_control;
466 fappend("-- Local Oscillator calibration data --\n\n");
467 fappend("HW-power-control enabled: %d\n",
468 dev->phy.hardware_power_control);
469 fappend("TX Bias: 0x%02X, TX Magn: 0x%02X (expire in %lu sec)\n",
470 lo->tx_bias, lo->tx_magn,
471 calc_expire_secs(now, lo->txctl_measured_time,
472 B43_LO_TXCTL_EXPIRE));
473 fappend("Power Vector: 0x%08X%08X (expires in %lu sec)\n",
474 (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
475 (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL),
476 calc_expire_secs(now, lo->pwr_vec_read_time,
477 B43_LO_PWRVEC_EXPIRE));
479 fappend("\nCalibrated settings:\n");
480 list_for_each_entry(cal, &lo->calib_list, list) {
483 active = (b43_compare_bbatt(&cal->bbatt, &phy->g->bbatt) &&
484 b43_compare_rfatt(&cal->rfatt, &phy->g->rfatt));
485 fappend("BB(%d), RF(%d,%d) -> I=%d, Q=%d "
486 "(expires in %lu sec)%s\n",
488 cal->rfatt.att, cal->rfatt.with_padmix,
489 cal->ctl.i, cal->ctl.q,
490 calc_expire_secs(now, cal->calib_time,
491 B43_LO_CALIB_EXPIRE),
492 active ? " ACTIVE" : "");
495 fappend("\nUsed RF attenuation values: Value(WithPadmix flag)\n");
496 for (i = 0; i < lo->rfatt_list.len; i++) {
498 lo->rfatt_list.list[i].att,
499 lo->rfatt_list.list[i].with_padmix);
502 fappend("\nUsed Baseband attenuation values:\n");
503 for (i = 0; i < lo->bbatt_list.len; i++) {
505 lo->bbatt_list.list[i].att);
510 return err ? err : count;
515 static int b43_debugfs_open(struct inode *inode, struct file *file)
517 file->private_data = inode->i_private;
521 static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
522 size_t count, loff_t *ppos)
524 struct b43_wldev *dev;
525 struct b43_debugfs_fops *dfops;
526 struct b43_dfs_file *dfile;
527 ssize_t uninitialized_var(ret);
529 const size_t bufsize = 1024 * 16; /* 16 kiB buffer */
530 const size_t buforder = get_order(bufsize);
535 dev = file->private_data;
539 mutex_lock(&dev->wl->mutex);
540 if (b43_status(dev) < B43_STAT_INITIALIZED) {
545 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
550 dfile = fops_to_dfs_file(dev, dfops);
552 if (!dfile->buffer) {
553 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
558 memset(buf, 0, bufsize);
559 if (dfops->take_irqlock) {
560 spin_lock_irq(&dev->wl->irq_lock);
561 ret = dfops->read(dev, buf, bufsize);
562 spin_unlock_irq(&dev->wl->irq_lock);
564 ret = dfops->read(dev, buf, bufsize);
566 free_pages((unsigned long)buf, buforder);
570 dfile->data_len = ret;
574 ret = simple_read_from_buffer(userbuf, count, ppos,
577 if (*ppos >= dfile->data_len) {
578 free_pages((unsigned long)dfile->buffer, buforder);
579 dfile->buffer = NULL;
583 mutex_unlock(&dev->wl->mutex);
585 return err ? err : ret;
588 static ssize_t b43_debugfs_write(struct file *file,
589 const char __user *userbuf,
590 size_t count, loff_t *ppos)
592 struct b43_wldev *dev;
593 struct b43_debugfs_fops *dfops;
599 if (count > PAGE_SIZE)
601 dev = file->private_data;
605 mutex_lock(&dev->wl->mutex);
606 if (b43_status(dev) < B43_STAT_INITIALIZED) {
611 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
617 buf = (char *)get_zeroed_page(GFP_KERNEL);
622 if (copy_from_user(buf, userbuf, count)) {
626 if (dfops->take_irqlock) {
627 spin_lock_irq(&dev->wl->irq_lock);
628 err = dfops->write(dev, buf, count);
629 spin_unlock_irq(&dev->wl->irq_lock);
631 err = dfops->write(dev, buf, count);
636 free_page((unsigned long)buf);
638 mutex_unlock(&dev->wl->mutex);
640 return err ? err : count;
644 #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
645 static struct b43_debugfs_fops fops_##name = { \
649 .open = b43_debugfs_open, \
650 .read = b43_debugfs_read, \
651 .write = b43_debugfs_write, \
653 .file_struct_offset = offsetof(struct b43_dfsentry, \
655 .take_irqlock = _take_irqlock, \
658 B43_DEBUGFS_FOPS(shm16read, shm16read__read_file, shm16read__write_file, 1);
659 B43_DEBUGFS_FOPS(shm16write, NULL, shm16write__write_file, 1);
660 B43_DEBUGFS_FOPS(shm32read, shm32read__read_file, shm32read__write_file, 1);
661 B43_DEBUGFS_FOPS(shm32write, NULL, shm32write__write_file, 1);
662 B43_DEBUGFS_FOPS(mmio16read, mmio16read__read_file, mmio16read__write_file, 1);
663 B43_DEBUGFS_FOPS(mmio16write, NULL, mmio16write__write_file, 1);
664 B43_DEBUGFS_FOPS(mmio32read, mmio32read__read_file, mmio32read__write_file, 1);
665 B43_DEBUGFS_FOPS(mmio32write, NULL, mmio32write__write_file, 1);
666 B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
667 B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
668 B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
671 bool b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
675 enabled = (dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
676 if (unlikely(enabled)) {
677 /* Force full debugging messages, if the user enabled
678 * some dynamic debugging feature. */
679 b43_modparam_verbose = B43_VERBOSITY_MAX;
685 static void b43_remove_dynamic_debug(struct b43_wldev *dev)
687 struct b43_dfsentry *e = dev->dfsentry;
690 for (i = 0; i < __B43_NR_DYNDBG; i++)
691 debugfs_remove(e->dyn_debug_dentries[i]);
694 static void b43_add_dynamic_debug(struct b43_wldev *dev)
696 struct b43_dfsentry *e = dev->dfsentry;
699 #define add_dyn_dbg(name, id, initstate) do { \
700 e->dyn_debug[id] = (initstate); \
701 d = debugfs_create_bool(name, 0600, e->subdir, \
702 &(e->dyn_debug[id])); \
704 e->dyn_debug_dentries[id] = d; \
707 add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
708 add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
709 add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
710 add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
711 add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
712 add_dyn_dbg("debug_lo", B43_DBG_LO, 0);
713 add_dyn_dbg("debug_firmware", B43_DBG_FIRMWARE, 0);
714 add_dyn_dbg("debug_keys", B43_DBG_KEYS, 0);
719 void b43_debugfs_add_device(struct b43_wldev *dev)
721 struct b43_dfsentry *e;
722 struct b43_txstatus_log *log;
726 e = kzalloc(sizeof(*e), GFP_KERNEL);
728 b43err(dev->wl, "debugfs: add device OOM\n");
733 log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
734 sizeof(struct b43_txstatus), GFP_KERNEL);
736 b43err(dev->wl, "debugfs: add device txstatus OOM\n");
741 spin_lock_init(&log->lock);
745 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
746 e->subdir = debugfs_create_dir(devdir, rootdir);
747 if (!e->subdir || IS_ERR(e->subdir)) {
748 if (e->subdir == ERR_PTR(-ENODEV)) {
749 b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
750 "enabled in kernel config\n");
752 b43err(dev->wl, "debugfs: cannot create %s directory\n",
755 dev->dfsentry = NULL;
761 e->mmio16read_next = 0xFFFF; /* invalid address */
762 e->mmio32read_next = 0xFFFF; /* invalid address */
763 e->shm16read_routing_next = 0xFFFFFFFF; /* invalid routing */
764 e->shm16read_addr_next = 0xFFFFFFFF; /* invalid address */
765 e->shm32read_routing_next = 0xFFFFFFFF; /* invalid routing */
766 e->shm32read_addr_next = 0xFFFFFFFF; /* invalid address */
768 #define ADD_FILE(name, mode) \
771 d = debugfs_create_file(__stringify(name), \
772 mode, e->subdir, dev, \
773 &fops_##name.fops); \
774 e->file_##name.dentry = NULL; \
776 e->file_##name.dentry = d; \
780 ADD_FILE(shm16read, 0600);
781 ADD_FILE(shm16write, 0200);
782 ADD_FILE(shm32read, 0600);
783 ADD_FILE(shm32write, 0200);
784 ADD_FILE(mmio16read, 0600);
785 ADD_FILE(mmio16write, 0200);
786 ADD_FILE(mmio32read, 0600);
787 ADD_FILE(mmio32write, 0200);
788 ADD_FILE(txstat, 0400);
789 ADD_FILE(restart, 0200);
790 ADD_FILE(loctls, 0400);
794 b43_add_dynamic_debug(dev);
797 void b43_debugfs_remove_device(struct b43_wldev *dev)
799 struct b43_dfsentry *e;
806 b43_remove_dynamic_debug(dev);
808 debugfs_remove(e->file_shm16read.dentry);
809 debugfs_remove(e->file_shm16write.dentry);
810 debugfs_remove(e->file_shm32read.dentry);
811 debugfs_remove(e->file_shm32write.dentry);
812 debugfs_remove(e->file_mmio16read.dentry);
813 debugfs_remove(e->file_mmio16write.dentry);
814 debugfs_remove(e->file_mmio32read.dentry);
815 debugfs_remove(e->file_mmio32write.dentry);
816 debugfs_remove(e->file_txstat.dentry);
817 debugfs_remove(e->file_restart.dentry);
818 debugfs_remove(e->file_loctls.dentry);
820 debugfs_remove(e->subdir);
821 kfree(e->txstatlog.log);
825 /* Called with IRQs disabled. */
826 void b43_debugfs_log_txstat(struct b43_wldev *dev,
827 const struct b43_txstatus *status)
829 struct b43_dfsentry *e = dev->dfsentry;
830 struct b43_txstatus_log *log;
831 struct b43_txstatus *cur;
837 spin_lock(&log->lock); /* IRQs are already disabled. */
839 if (i == B43_NR_LOGGED_TXSTATUS)
842 cur = &(log->log[i]);
843 memcpy(cur, status, sizeof(*cur));
844 spin_unlock(&log->lock);
847 void b43_debugfs_init(void)
849 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
854 void b43_debugfs_exit(void)
856 debugfs_remove(rootdir);