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 MMIO address that we allow access to from the debugfs files. */
78 #define B43_MAX_MMIO_ACCESS (0xF00 - 1)
80 static ssize_t mmio16read__read_file(struct b43_wldev *dev,
81 char *buf, size_t bufsize)
87 addr = dev->dfsentry->mmio16read_next;
88 if (addr > B43_MAX_MMIO_ACCESS)
91 val = b43_read16(dev, addr);
92 fappend("0x%04X\n", val);
97 static int mmio16read__write_file(struct b43_wldev *dev,
98 const char *buf, size_t count)
103 res = sscanf(buf, "0x%X", &addr);
106 if (addr > B43_MAX_MMIO_ACCESS)
107 return -EADDRNOTAVAIL;
109 dev->dfsentry->mmio16read_next = addr;
114 static int mmio16write__write_file(struct b43_wldev *dev,
115 const char *buf, size_t count)
117 unsigned int addr, val;
120 res = sscanf(buf, "0x%X = 0x%X", &addr, &val);
123 if (addr > B43_MAX_MMIO_ACCESS)
124 return -EADDRNOTAVAIL;
128 b43_write16(dev, addr, val);
133 static ssize_t mmio32read__read_file(struct b43_wldev *dev,
134 char *buf, size_t bufsize)
140 addr = dev->dfsentry->mmio32read_next;
141 if (addr > B43_MAX_MMIO_ACCESS)
142 return -EDESTADDRREQ;
144 val = b43_read32(dev, addr);
145 fappend("0x%08X\n", val);
150 static int mmio32read__write_file(struct b43_wldev *dev,
151 const char *buf, size_t count)
156 res = sscanf(buf, "0x%X", &addr);
159 if (addr > B43_MAX_MMIO_ACCESS)
160 return -EADDRNOTAVAIL;
162 dev->dfsentry->mmio32read_next = addr;
167 static int mmio32write__write_file(struct b43_wldev *dev,
168 const char *buf, size_t count)
170 unsigned int addr, val;
173 res = sscanf(buf, "0x%X = 0x%X", &addr, &val);
176 if (addr > B43_MAX_MMIO_ACCESS)
177 return -EADDRNOTAVAIL;
178 if (val > 0xFFFFFFFF)
181 b43_write32(dev, addr, val);
186 /* wl->irq_lock is locked */
187 static ssize_t tsf_read_file(struct b43_wldev *dev,
188 char *buf, size_t bufsize)
193 b43_tsf_read(dev, &tsf);
194 fappend("0x%08x%08x\n",
195 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
196 (unsigned int)(tsf & 0xFFFFFFFFULL));
201 /* wl->irq_lock is locked */
202 static int tsf_write_file(struct b43_wldev *dev,
203 const char *buf, size_t count)
207 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
209 b43_tsf_write(dev, tsf);
214 /* wl->irq_lock is locked */
215 static ssize_t ucode_regs_read_file(struct b43_wldev *dev,
216 char *buf, size_t bufsize)
221 for (i = 0; i < 64; i++) {
222 fappend("r%d = 0x%04x\n", i,
223 b43_shm_read16(dev, B43_SHM_SCRATCH, i));
229 /* wl->irq_lock is locked */
230 static ssize_t shm_read_file(struct b43_wldev *dev,
231 char *buf, size_t bufsize)
236 __le16 *le16buf = (__le16 *)buf;
238 for (i = 0; i < 0x1000; i++) {
239 if (bufsize < sizeof(tmp))
241 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 2 * i);
242 le16buf[i] = cpu_to_le16(tmp);
243 count += sizeof(tmp);
244 bufsize -= sizeof(tmp);
250 static ssize_t txstat_read_file(struct b43_wldev *dev,
251 char *buf, size_t bufsize)
253 struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
257 struct b43_txstatus *stat;
259 spin_lock_irqsave(&log->lock, flags);
261 fappend("Nothing transmitted, yet\n");
264 fappend("b43 TX status reports:\n\n"
265 "index | cookie | seq | phy_stat | frame_count | "
266 "rts_count | supp_reason | pm_indicated | "
267 "intermediate | for_ampdu | acked\n" "---\n");
271 if (i == B43_NR_LOGGED_TXSTATUS)
273 stat = &(log->log[i]);
276 "0x%04X | 0x%04X | 0x%02X | "
281 stat->cookie, stat->seq, stat->phy_stat,
282 stat->frame_count, stat->rts_count,
283 stat->supp_reason, stat->pm_indicated,
284 stat->intermediate, stat->for_ampdu,
293 spin_unlock_irqrestore(&log->lock, flags);
298 static ssize_t txpower_g_read_file(struct b43_wldev *dev,
299 char *buf, size_t bufsize)
303 if (dev->phy.type != B43_PHYTYPE_G) {
304 fappend("Device is not a G-PHY\n");
307 fappend("Control: %s\n", dev->phy.manual_txpower_control ?
308 "MANUAL" : "AUTOMATIC");
309 fappend("Baseband attenuation: %u\n", dev->phy.bbatt.att);
310 fappend("Radio attenuation: %u\n", dev->phy.rfatt.att);
311 fappend("TX Mixer Gain: %s\n",
312 (dev->phy.tx_control & B43_TXCTL_TXMIX) ? "ON" : "OFF");
313 fappend("PA Gain 2dB: %s\n",
314 (dev->phy.tx_control & B43_TXCTL_PA2DB) ? "ON" : "OFF");
315 fappend("PA Gain 3dB: %s\n",
316 (dev->phy.tx_control & B43_TXCTL_PA3DB) ? "ON" : "OFF");
318 fappend("You can write to this file:\n");
319 fappend("Writing \"auto\" enables automatic txpower control.\n");
321 ("Writing the attenuation values as \"bbatt rfatt txmix pa2db pa3db\" "
322 "enables manual txpower control.\n");
323 fappend("Example: 5 4 0 0 1\n");
324 fappend("Enables manual control with Baseband attenuation 5, "
325 "Radio attenuation 4, No TX Mixer Gain, "
326 "No PA Gain 2dB, With PA Gain 3dB.\n");
331 static int txpower_g_write_file(struct b43_wldev *dev,
332 const char *buf, size_t count)
334 if (dev->phy.type != B43_PHYTYPE_G)
336 if ((count >= 4) && (memcmp(buf, "auto", 4) == 0)) {
337 /* Automatic control */
338 dev->phy.manual_txpower_control = 0;
339 b43_phy_xmitpower(dev);
341 int bbatt = 0, rfatt = 0, txmix = 0, pa2db = 0, pa3db = 0;
343 if (sscanf(buf, "%d %d %d %d %d", &bbatt, &rfatt,
344 &txmix, &pa2db, &pa3db) != 5)
346 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
347 dev->phy.manual_txpower_control = 1;
348 dev->phy.bbatt.att = bbatt;
349 dev->phy.rfatt.att = rfatt;
350 dev->phy.tx_control = 0;
352 dev->phy.tx_control |= B43_TXCTL_TXMIX;
354 dev->phy.tx_control |= B43_TXCTL_PA2DB;
356 dev->phy.tx_control |= B43_TXCTL_PA3DB;
359 b43_set_txpower_g(dev, &dev->phy.bbatt,
360 &dev->phy.rfatt, dev->phy.tx_control);
361 b43_radio_unlock(dev);
368 /* wl->irq_lock is locked */
369 static int restart_write_file(struct b43_wldev *dev,
370 const char *buf, size_t count)
374 if (count > 0 && buf[0] == '1') {
375 b43_controller_restart(dev, "manually restarted");
382 static unsigned long calc_expire_secs(unsigned long now,
384 unsigned long expire)
386 expire = time + expire;
388 if (time_after(now, expire))
389 return 0; /* expired */
391 /* jiffies wrapped */
392 expire -= MAX_JIFFY_OFFSET;
393 now -= MAX_JIFFY_OFFSET;
395 B43_WARN_ON(expire < now);
397 return (expire - now) / HZ;
400 static ssize_t loctls_read_file(struct b43_wldev *dev,
401 char *buf, size_t bufsize)
404 struct b43_txpower_lo_control *lo;
406 struct b43_lo_calib *cal;
407 unsigned long now = jiffies;
408 struct b43_phy *phy = &dev->phy;
410 if (phy->type != B43_PHYTYPE_G) {
411 fappend("Device is not a G-PHY\n");
415 lo = phy->lo_control;
416 fappend("-- Local Oscillator calibration data --\n\n");
417 fappend("HW-power-control enabled: %d\n",
418 dev->phy.hardware_power_control);
419 fappend("TX Bias: 0x%02X, TX Magn: 0x%02X (expire in %lu sec)\n",
420 lo->tx_bias, lo->tx_magn,
421 calc_expire_secs(now, lo->txctl_measured_time,
422 B43_LO_TXCTL_EXPIRE));
423 fappend("Power Vector: 0x%08X%08X (expires in %lu sec)\n",
424 (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
425 (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL),
426 calc_expire_secs(now, lo->pwr_vec_read_time,
427 B43_LO_PWRVEC_EXPIRE));
429 fappend("\nCalibrated settings:\n");
430 list_for_each_entry(cal, &lo->calib_list, list) {
433 active = (b43_compare_bbatt(&cal->bbatt, &phy->bbatt) &&
434 b43_compare_rfatt(&cal->rfatt, &phy->rfatt));
435 fappend("BB(%d), RF(%d,%d) -> I=%d, Q=%d "
436 "(expires in %lu sec)%s\n",
438 cal->rfatt.att, cal->rfatt.with_padmix,
439 cal->ctl.i, cal->ctl.q,
440 calc_expire_secs(now, cal->calib_time,
441 B43_LO_CALIB_EXPIRE),
442 active ? " ACTIVE" : "");
445 fappend("\nUsed RF attenuation values: Value(WithPadmix flag)\n");
446 for (i = 0; i < lo->rfatt_list.len; i++) {
448 lo->rfatt_list.list[i].att,
449 lo->rfatt_list.list[i].with_padmix);
452 fappend("\nUsed Baseband attenuation values:\n");
453 for (i = 0; i < lo->bbatt_list.len; i++) {
455 lo->bbatt_list.list[i].att);
460 return err ? err : count;
465 static int b43_debugfs_open(struct inode *inode, struct file *file)
467 file->private_data = inode->i_private;
471 static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
472 size_t count, loff_t *ppos)
474 struct b43_wldev *dev;
475 struct b43_debugfs_fops *dfops;
476 struct b43_dfs_file *dfile;
477 ssize_t uninitialized_var(ret);
479 const size_t bufsize = 1024 * 16; /* 16 kiB buffer */
480 const size_t buforder = get_order(bufsize);
485 dev = file->private_data;
489 mutex_lock(&dev->wl->mutex);
490 if (b43_status(dev) < B43_STAT_INITIALIZED) {
495 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
500 dfile = fops_to_dfs_file(dev, dfops);
502 if (!dfile->buffer) {
503 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
508 memset(buf, 0, bufsize);
509 if (dfops->take_irqlock) {
510 spin_lock_irq(&dev->wl->irq_lock);
511 ret = dfops->read(dev, buf, bufsize);
512 spin_unlock_irq(&dev->wl->irq_lock);
514 ret = dfops->read(dev, buf, bufsize);
516 free_pages((unsigned long)buf, buforder);
520 dfile->data_len = ret;
524 ret = simple_read_from_buffer(userbuf, count, ppos,
527 if (*ppos >= dfile->data_len) {
528 free_pages((unsigned long)dfile->buffer, buforder);
529 dfile->buffer = NULL;
533 mutex_unlock(&dev->wl->mutex);
535 return err ? err : ret;
538 static ssize_t b43_debugfs_write(struct file *file,
539 const char __user *userbuf,
540 size_t count, loff_t *ppos)
542 struct b43_wldev *dev;
543 struct b43_debugfs_fops *dfops;
549 if (count > PAGE_SIZE)
551 dev = file->private_data;
555 mutex_lock(&dev->wl->mutex);
556 if (b43_status(dev) < B43_STAT_INITIALIZED) {
561 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
567 buf = (char *)get_zeroed_page(GFP_KERNEL);
572 if (copy_from_user(buf, userbuf, count)) {
576 if (dfops->take_irqlock) {
577 spin_lock_irq(&dev->wl->irq_lock);
578 err = dfops->write(dev, buf, count);
579 spin_unlock_irq(&dev->wl->irq_lock);
581 err = dfops->write(dev, buf, count);
586 free_page((unsigned long)buf);
588 mutex_unlock(&dev->wl->mutex);
590 return err ? err : count;
594 #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
595 static struct b43_debugfs_fops fops_##name = { \
599 .open = b43_debugfs_open, \
600 .read = b43_debugfs_read, \
601 .write = b43_debugfs_write, \
603 .file_struct_offset = offsetof(struct b43_dfsentry, \
605 .take_irqlock = _take_irqlock, \
608 B43_DEBUGFS_FOPS(mmio16read, mmio16read__read_file, mmio16read__write_file, 1);
609 B43_DEBUGFS_FOPS(mmio16write, NULL, mmio16write__write_file, 1);
610 B43_DEBUGFS_FOPS(mmio32read, mmio32read__read_file, mmio32read__write_file, 1);
611 B43_DEBUGFS_FOPS(mmio32write, NULL, mmio32write__write_file, 1);
612 B43_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
613 B43_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
614 B43_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
615 B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
616 B43_DEBUGFS_FOPS(txpower_g, txpower_g_read_file, txpower_g_write_file, 0);
617 B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
618 B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
621 int b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
623 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
626 static void b43_remove_dynamic_debug(struct b43_wldev *dev)
628 struct b43_dfsentry *e = dev->dfsentry;
631 for (i = 0; i < __B43_NR_DYNDBG; i++)
632 debugfs_remove(e->dyn_debug_dentries[i]);
635 static void b43_add_dynamic_debug(struct b43_wldev *dev)
637 struct b43_dfsentry *e = dev->dfsentry;
640 #define add_dyn_dbg(name, id, initstate) do { \
641 e->dyn_debug[id] = (initstate); \
642 d = debugfs_create_bool(name, 0600, e->subdir, \
643 &(e->dyn_debug[id])); \
645 e->dyn_debug_dentries[id] = d; \
648 add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
649 add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
650 add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
651 add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
652 add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
653 add_dyn_dbg("debug_lo", B43_DBG_LO, 0);
658 void b43_debugfs_add_device(struct b43_wldev *dev)
660 struct b43_dfsentry *e;
661 struct b43_txstatus_log *log;
665 e = kzalloc(sizeof(*e), GFP_KERNEL);
667 b43err(dev->wl, "debugfs: add device OOM\n");
672 log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
673 sizeof(struct b43_txstatus), GFP_KERNEL);
675 b43err(dev->wl, "debugfs: add device txstatus OOM\n");
680 spin_lock_init(&log->lock);
684 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
685 e->subdir = debugfs_create_dir(devdir, rootdir);
686 if (!e->subdir || IS_ERR(e->subdir)) {
687 if (e->subdir == ERR_PTR(-ENODEV)) {
688 b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
689 "enabled in kernel config\n");
691 b43err(dev->wl, "debugfs: cannot create %s directory\n",
694 dev->dfsentry = NULL;
700 e->mmio16read_next = 0xFFFF; /* invalid address */
701 e->mmio32read_next = 0xFFFF; /* invalid address */
703 #define ADD_FILE(name, mode) \
706 d = debugfs_create_file(__stringify(name), \
707 mode, e->subdir, dev, \
708 &fops_##name.fops); \
709 e->file_##name.dentry = NULL; \
711 e->file_##name.dentry = d; \
715 ADD_FILE(mmio16read, 0600);
716 ADD_FILE(mmio16write, 0200);
717 ADD_FILE(mmio32read, 0600);
718 ADD_FILE(mmio32write, 0200);
720 ADD_FILE(ucode_regs, 0400);
722 ADD_FILE(txstat, 0400);
723 ADD_FILE(txpower_g, 0600);
724 ADD_FILE(restart, 0200);
725 ADD_FILE(loctls, 0400);
729 b43_add_dynamic_debug(dev);
732 void b43_debugfs_remove_device(struct b43_wldev *dev)
734 struct b43_dfsentry *e;
741 b43_remove_dynamic_debug(dev);
743 debugfs_remove(e->file_mmio16read.dentry);
744 debugfs_remove(e->file_mmio16write.dentry);
745 debugfs_remove(e->file_mmio32read.dentry);
746 debugfs_remove(e->file_mmio32write.dentry);
747 debugfs_remove(e->file_tsf.dentry);
748 debugfs_remove(e->file_ucode_regs.dentry);
749 debugfs_remove(e->file_shm.dentry);
750 debugfs_remove(e->file_txstat.dentry);
751 debugfs_remove(e->file_txpower_g.dentry);
752 debugfs_remove(e->file_restart.dentry);
753 debugfs_remove(e->file_loctls.dentry);
755 debugfs_remove(e->subdir);
756 kfree(e->txstatlog.log);
760 /* Called with IRQs disabled. */
761 void b43_debugfs_log_txstat(struct b43_wldev *dev,
762 const struct b43_txstatus *status)
764 struct b43_dfsentry *e = dev->dfsentry;
765 struct b43_txstatus_log *log;
766 struct b43_txstatus *cur;
772 spin_lock(&log->lock); /* IRQs are already disabled. */
774 if (i == B43_NR_LOGGED_TXSTATUS)
777 cur = &(log->log[i]);
778 memcpy(cur, status, sizeof(*cur));
779 spin_unlock(&log->lock);
782 void b43_debugfs_init(void)
784 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
789 void b43_debugfs_exit(void)
791 debugfs_remove(rootdir);