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 /* wl->irq_lock is locked */
371 static ssize_t tsf_read_file(struct b43_wldev *dev,
372 char *buf, size_t bufsize)
377 b43_tsf_read(dev, &tsf);
378 fappend("0x%08x%08x\n",
379 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
380 (unsigned int)(tsf & 0xFFFFFFFFULL));
385 /* wl->irq_lock is locked */
386 static int tsf_write_file(struct b43_wldev *dev,
387 const char *buf, size_t count)
391 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
393 b43_tsf_write(dev, tsf);
398 static ssize_t txstat_read_file(struct b43_wldev *dev,
399 char *buf, size_t bufsize)
401 struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
405 struct b43_txstatus *stat;
407 spin_lock_irqsave(&log->lock, flags);
409 fappend("Nothing transmitted, yet\n");
412 fappend("b43 TX status reports:\n\n"
413 "index | cookie | seq | phy_stat | frame_count | "
414 "rts_count | supp_reason | pm_indicated | "
415 "intermediate | for_ampdu | acked\n" "---\n");
419 if (i == B43_NR_LOGGED_TXSTATUS)
421 stat = &(log->log[i]);
424 "0x%04X | 0x%04X | 0x%02X | "
429 stat->cookie, stat->seq, stat->phy_stat,
430 stat->frame_count, stat->rts_count,
431 stat->supp_reason, stat->pm_indicated,
432 stat->intermediate, stat->for_ampdu,
441 spin_unlock_irqrestore(&log->lock, flags);
446 /* wl->irq_lock is locked */
447 static int restart_write_file(struct b43_wldev *dev,
448 const char *buf, size_t count)
452 if (count > 0 && buf[0] == '1') {
453 b43_controller_restart(dev, "manually restarted");
460 static unsigned long calc_expire_secs(unsigned long now,
462 unsigned long expire)
464 expire = time + expire;
466 if (time_after(now, expire))
467 return 0; /* expired */
469 /* jiffies wrapped */
470 expire -= MAX_JIFFY_OFFSET;
471 now -= MAX_JIFFY_OFFSET;
473 B43_WARN_ON(expire < now);
475 return (expire - now) / HZ;
478 static ssize_t loctls_read_file(struct b43_wldev *dev,
479 char *buf, size_t bufsize)
482 struct b43_txpower_lo_control *lo;
484 struct b43_lo_calib *cal;
485 unsigned long now = jiffies;
486 struct b43_phy *phy = &dev->phy;
488 if (phy->type != B43_PHYTYPE_G) {
489 fappend("Device is not a G-PHY\n");
493 lo = phy->g->lo_control;
494 fappend("-- Local Oscillator calibration data --\n\n");
495 fappend("HW-power-control enabled: %d\n",
496 dev->phy.hardware_power_control);
497 fappend("TX Bias: 0x%02X, TX Magn: 0x%02X (expire in %lu sec)\n",
498 lo->tx_bias, lo->tx_magn,
499 calc_expire_secs(now, lo->txctl_measured_time,
500 B43_LO_TXCTL_EXPIRE));
501 fappend("Power Vector: 0x%08X%08X (expires in %lu sec)\n",
502 (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
503 (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL),
504 calc_expire_secs(now, lo->pwr_vec_read_time,
505 B43_LO_PWRVEC_EXPIRE));
507 fappend("\nCalibrated settings:\n");
508 list_for_each_entry(cal, &lo->calib_list, list) {
511 active = (b43_compare_bbatt(&cal->bbatt, &phy->g->bbatt) &&
512 b43_compare_rfatt(&cal->rfatt, &phy->g->rfatt));
513 fappend("BB(%d), RF(%d,%d) -> I=%d, Q=%d "
514 "(expires in %lu sec)%s\n",
516 cal->rfatt.att, cal->rfatt.with_padmix,
517 cal->ctl.i, cal->ctl.q,
518 calc_expire_secs(now, cal->calib_time,
519 B43_LO_CALIB_EXPIRE),
520 active ? " ACTIVE" : "");
523 fappend("\nUsed RF attenuation values: Value(WithPadmix flag)\n");
524 for (i = 0; i < lo->rfatt_list.len; i++) {
526 lo->rfatt_list.list[i].att,
527 lo->rfatt_list.list[i].with_padmix);
530 fappend("\nUsed Baseband attenuation values:\n");
531 for (i = 0; i < lo->bbatt_list.len; i++) {
533 lo->bbatt_list.list[i].att);
538 return err ? err : count;
543 static int b43_debugfs_open(struct inode *inode, struct file *file)
545 file->private_data = inode->i_private;
549 static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
550 size_t count, loff_t *ppos)
552 struct b43_wldev *dev;
553 struct b43_debugfs_fops *dfops;
554 struct b43_dfs_file *dfile;
555 ssize_t uninitialized_var(ret);
557 const size_t bufsize = 1024 * 16; /* 16 kiB buffer */
558 const size_t buforder = get_order(bufsize);
563 dev = file->private_data;
567 mutex_lock(&dev->wl->mutex);
568 if (b43_status(dev) < B43_STAT_INITIALIZED) {
573 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
578 dfile = fops_to_dfs_file(dev, dfops);
580 if (!dfile->buffer) {
581 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
586 memset(buf, 0, bufsize);
587 if (dfops->take_irqlock) {
588 spin_lock_irq(&dev->wl->irq_lock);
589 ret = dfops->read(dev, buf, bufsize);
590 spin_unlock_irq(&dev->wl->irq_lock);
592 ret = dfops->read(dev, buf, bufsize);
594 free_pages((unsigned long)buf, buforder);
598 dfile->data_len = ret;
602 ret = simple_read_from_buffer(userbuf, count, ppos,
605 if (*ppos >= dfile->data_len) {
606 free_pages((unsigned long)dfile->buffer, buforder);
607 dfile->buffer = NULL;
611 mutex_unlock(&dev->wl->mutex);
613 return err ? err : ret;
616 static ssize_t b43_debugfs_write(struct file *file,
617 const char __user *userbuf,
618 size_t count, loff_t *ppos)
620 struct b43_wldev *dev;
621 struct b43_debugfs_fops *dfops;
627 if (count > PAGE_SIZE)
629 dev = file->private_data;
633 mutex_lock(&dev->wl->mutex);
634 if (b43_status(dev) < B43_STAT_INITIALIZED) {
639 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
645 buf = (char *)get_zeroed_page(GFP_KERNEL);
650 if (copy_from_user(buf, userbuf, count)) {
654 if (dfops->take_irqlock) {
655 spin_lock_irq(&dev->wl->irq_lock);
656 err = dfops->write(dev, buf, count);
657 spin_unlock_irq(&dev->wl->irq_lock);
659 err = dfops->write(dev, buf, count);
664 free_page((unsigned long)buf);
666 mutex_unlock(&dev->wl->mutex);
668 return err ? err : count;
672 #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
673 static struct b43_debugfs_fops fops_##name = { \
677 .open = b43_debugfs_open, \
678 .read = b43_debugfs_read, \
679 .write = b43_debugfs_write, \
681 .file_struct_offset = offsetof(struct b43_dfsentry, \
683 .take_irqlock = _take_irqlock, \
686 B43_DEBUGFS_FOPS(shm16read, shm16read__read_file, shm16read__write_file, 1);
687 B43_DEBUGFS_FOPS(shm16write, NULL, shm16write__write_file, 1);
688 B43_DEBUGFS_FOPS(shm32read, shm32read__read_file, shm32read__write_file, 1);
689 B43_DEBUGFS_FOPS(shm32write, NULL, shm32write__write_file, 1);
690 B43_DEBUGFS_FOPS(mmio16read, mmio16read__read_file, mmio16read__write_file, 1);
691 B43_DEBUGFS_FOPS(mmio16write, NULL, mmio16write__write_file, 1);
692 B43_DEBUGFS_FOPS(mmio32read, mmio32read__read_file, mmio32read__write_file, 1);
693 B43_DEBUGFS_FOPS(mmio32write, NULL, mmio32write__write_file, 1);
694 B43_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
695 B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
696 B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
697 B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
700 int b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
702 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
705 static void b43_remove_dynamic_debug(struct b43_wldev *dev)
707 struct b43_dfsentry *e = dev->dfsentry;
710 for (i = 0; i < __B43_NR_DYNDBG; i++)
711 debugfs_remove(e->dyn_debug_dentries[i]);
714 static void b43_add_dynamic_debug(struct b43_wldev *dev)
716 struct b43_dfsentry *e = dev->dfsentry;
719 #define add_dyn_dbg(name, id, initstate) do { \
720 e->dyn_debug[id] = (initstate); \
721 d = debugfs_create_bool(name, 0600, e->subdir, \
722 &(e->dyn_debug[id])); \
724 e->dyn_debug_dentries[id] = d; \
727 add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
728 add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
729 add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
730 add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
731 add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
732 add_dyn_dbg("debug_lo", B43_DBG_LO, 0);
733 add_dyn_dbg("debug_firmware", B43_DBG_FIRMWARE, 0);
738 void b43_debugfs_add_device(struct b43_wldev *dev)
740 struct b43_dfsentry *e;
741 struct b43_txstatus_log *log;
745 e = kzalloc(sizeof(*e), GFP_KERNEL);
747 b43err(dev->wl, "debugfs: add device OOM\n");
752 log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
753 sizeof(struct b43_txstatus), GFP_KERNEL);
755 b43err(dev->wl, "debugfs: add device txstatus OOM\n");
760 spin_lock_init(&log->lock);
764 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
765 e->subdir = debugfs_create_dir(devdir, rootdir);
766 if (!e->subdir || IS_ERR(e->subdir)) {
767 if (e->subdir == ERR_PTR(-ENODEV)) {
768 b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
769 "enabled in kernel config\n");
771 b43err(dev->wl, "debugfs: cannot create %s directory\n",
774 dev->dfsentry = NULL;
780 e->mmio16read_next = 0xFFFF; /* invalid address */
781 e->mmio32read_next = 0xFFFF; /* invalid address */
782 e->shm16read_routing_next = 0xFFFFFFFF; /* invalid routing */
783 e->shm16read_addr_next = 0xFFFFFFFF; /* invalid address */
784 e->shm32read_routing_next = 0xFFFFFFFF; /* invalid routing */
785 e->shm32read_addr_next = 0xFFFFFFFF; /* invalid address */
787 #define ADD_FILE(name, mode) \
790 d = debugfs_create_file(__stringify(name), \
791 mode, e->subdir, dev, \
792 &fops_##name.fops); \
793 e->file_##name.dentry = NULL; \
795 e->file_##name.dentry = d; \
799 ADD_FILE(shm16read, 0600);
800 ADD_FILE(shm16write, 0200);
801 ADD_FILE(shm32read, 0600);
802 ADD_FILE(shm32write, 0200);
803 ADD_FILE(mmio16read, 0600);
804 ADD_FILE(mmio16write, 0200);
805 ADD_FILE(mmio32read, 0600);
806 ADD_FILE(mmio32write, 0200);
808 ADD_FILE(txstat, 0400);
809 ADD_FILE(restart, 0200);
810 ADD_FILE(loctls, 0400);
814 b43_add_dynamic_debug(dev);
817 void b43_debugfs_remove_device(struct b43_wldev *dev)
819 struct b43_dfsentry *e;
826 b43_remove_dynamic_debug(dev);
828 debugfs_remove(e->file_shm16read.dentry);
829 debugfs_remove(e->file_shm16write.dentry);
830 debugfs_remove(e->file_shm32read.dentry);
831 debugfs_remove(e->file_shm32write.dentry);
832 debugfs_remove(e->file_mmio16read.dentry);
833 debugfs_remove(e->file_mmio16write.dentry);
834 debugfs_remove(e->file_mmio32read.dentry);
835 debugfs_remove(e->file_mmio32write.dentry);
836 debugfs_remove(e->file_tsf.dentry);
837 debugfs_remove(e->file_txstat.dentry);
838 debugfs_remove(e->file_restart.dentry);
839 debugfs_remove(e->file_loctls.dentry);
841 debugfs_remove(e->subdir);
842 kfree(e->txstatlog.log);
846 /* Called with IRQs disabled. */
847 void b43_debugfs_log_txstat(struct b43_wldev *dev,
848 const struct b43_txstatus *status)
850 struct b43_dfsentry *e = dev->dfsentry;
851 struct b43_txstatus_log *log;
852 struct b43_txstatus *cur;
858 spin_lock(&log->lock); /* IRQs are already disabled. */
860 if (i == B43_NR_LOGGED_TXSTATUS)
863 cur = &(log->log[i]);
864 memcpy(cur, status, sizeof(*cur));
865 spin_unlock(&log->lock);
868 void b43_debugfs_init(void)
870 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
875 void b43_debugfs_exit(void)
877 debugfs_remove(rootdir);