3 Broadcom B43legacy wireless driver
5 debugfs driver debugging code
7 Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
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>
33 #include "b43legacy.h"
41 /* The root directory. */
42 static struct dentry
*rootdir
;
44 struct b43legacy_debugfs_fops
{
45 ssize_t (*read
)(struct b43legacy_wldev
*dev
, char *buf
, size_t bufsize
);
46 int (*write
)(struct b43legacy_wldev
*dev
, const char *buf
, size_t count
);
47 struct file_operations fops
;
48 /* Offset of struct b43legacy_dfs_file in struct b43legacy_dfsentry */
49 size_t file_struct_offset
;
50 /* Take wl->irq_lock before calling read/write? */
55 struct b43legacy_dfs_file
* fops_to_dfs_file(struct b43legacy_wldev
*dev
,
56 const struct b43legacy_debugfs_fops
*dfops
)
61 p
+= dfops
->file_struct_offset
;
67 #define fappend(fmt, x...) \
69 if (bufsize - count) \
70 count += snprintf(buf + count, \
74 printk(KERN_ERR "b43legacy: fappend overflow\n"); \
78 /* wl->irq_lock is locked */
79 static ssize_t
tsf_read_file(struct b43legacy_wldev
*dev
, char *buf
, size_t bufsize
)
84 b43legacy_tsf_read(dev
, &tsf
);
85 fappend("0x%08x%08x\n",
86 (unsigned int)((tsf
& 0xFFFFFFFF00000000ULL
) >> 32),
87 (unsigned int)(tsf
& 0xFFFFFFFFULL
));
92 /* wl->irq_lock is locked */
93 static int tsf_write_file(struct b43legacy_wldev
*dev
, const char *buf
, size_t count
)
97 if (sscanf(buf
, "%llu", (unsigned long long *)(&tsf
)) != 1)
99 b43legacy_tsf_write(dev
, tsf
);
104 /* wl->irq_lock is locked */
105 static ssize_t
ucode_regs_read_file(struct b43legacy_wldev
*dev
, char *buf
, size_t bufsize
)
110 for (i
= 0; i
< 64; i
++) {
111 fappend("r%d = 0x%04x\n", i
,
112 b43legacy_shm_read16(dev
, B43legacy_SHM_WIRELESS
, i
));
118 /* wl->irq_lock is locked */
119 static ssize_t
shm_read_file(struct b43legacy_wldev
*dev
, char *buf
, size_t bufsize
)
124 __le16
*le16buf
= (__le16
*)buf
;
126 for (i
= 0; i
< 0x1000; i
++) {
127 if (bufsize
< sizeof(tmp
))
129 tmp
= b43legacy_shm_read16(dev
, B43legacy_SHM_SHARED
, 2 * i
);
130 le16buf
[i
] = cpu_to_le16(tmp
);
131 count
+= sizeof(tmp
);
132 bufsize
-= sizeof(tmp
);
138 static ssize_t
txstat_read_file(struct b43legacy_wldev
*dev
, char *buf
, size_t bufsize
)
140 struct b43legacy_txstatus_log
*log
= &dev
->dfsentry
->txstatlog
;
144 struct b43legacy_txstatus
*stat
;
146 spin_lock_irqsave(&log
->lock
, flags
);
148 fappend("Nothing transmitted, yet\n");
151 fappend("b43legacy TX status reports:\n\n"
152 "index | cookie | seq | phy_stat | frame_count | "
153 "rts_count | supp_reason | pm_indicated | "
154 "intermediate | for_ampdu | acked\n" "---\n");
158 if (i
== B43legacy_NR_LOGGED_TXSTATUS
)
160 stat
= &(log
->log
[i
]);
163 "0x%04X | 0x%04X | 0x%02X | "
168 stat
->cookie
, stat
->seq
, stat
->phy_stat
,
169 stat
->frame_count
, stat
->rts_count
,
170 stat
->supp_reason
, stat
->pm_indicated
,
171 stat
->intermediate
, stat
->for_ampdu
,
180 spin_unlock_irqrestore(&log
->lock
, flags
);
185 /* wl->irq_lock is locked */
186 static int restart_write_file(struct b43legacy_wldev
*dev
, const char *buf
, size_t count
)
190 if (count
> 0 && buf
[0] == '1') {
191 b43legacy_controller_restart(dev
, "manually restarted");
200 static ssize_t
b43legacy_debugfs_read(struct file
*file
, char __user
*userbuf
,
201 size_t count
, loff_t
*ppos
)
203 struct b43legacy_wldev
*dev
;
204 struct b43legacy_debugfs_fops
*dfops
;
205 struct b43legacy_dfs_file
*dfile
;
206 ssize_t
uninitialized_var(ret
);
208 const size_t bufsize
= 1024 * 16; /* 16 KiB buffer */
209 const size_t buforder
= get_order(bufsize
);
214 dev
= file
->private_data
;
218 mutex_lock(&dev
->wl
->mutex
);
219 if (b43legacy_status(dev
) < B43legacy_STAT_INITIALIZED
) {
224 dfops
= container_of(debugfs_real_fops(file
),
225 struct b43legacy_debugfs_fops
, fops
);
230 dfile
= fops_to_dfs_file(dev
, dfops
);
232 if (!dfile
->buffer
) {
233 buf
= (char *)__get_free_pages(GFP_KERNEL
, buforder
);
238 memset(buf
, 0, bufsize
);
239 if (dfops
->take_irqlock
) {
240 spin_lock_irq(&dev
->wl
->irq_lock
);
241 ret
= dfops
->read(dev
, buf
, bufsize
);
242 spin_unlock_irq(&dev
->wl
->irq_lock
);
244 ret
= dfops
->read(dev
, buf
, bufsize
);
246 free_pages((unsigned long)buf
, buforder
);
250 dfile
->data_len
= ret
;
254 ret
= simple_read_from_buffer(userbuf
, count
, ppos
,
257 if (*ppos
>= dfile
->data_len
) {
258 free_pages((unsigned long)dfile
->buffer
, buforder
);
259 dfile
->buffer
= NULL
;
263 mutex_unlock(&dev
->wl
->mutex
);
265 return err
? err
: ret
;
268 static ssize_t
b43legacy_debugfs_write(struct file
*file
,
269 const char __user
*userbuf
,
270 size_t count
, loff_t
*ppos
)
272 struct b43legacy_wldev
*dev
;
273 struct b43legacy_debugfs_fops
*dfops
;
279 if (count
> PAGE_SIZE
)
281 dev
= file
->private_data
;
285 mutex_lock(&dev
->wl
->mutex
);
286 if (b43legacy_status(dev
) < B43legacy_STAT_INITIALIZED
) {
291 dfops
= container_of(debugfs_real_fops(file
),
292 struct b43legacy_debugfs_fops
, fops
);
298 buf
= (char *)get_zeroed_page(GFP_KERNEL
);
303 if (copy_from_user(buf
, userbuf
, count
)) {
307 if (dfops
->take_irqlock
) {
308 spin_lock_irq(&dev
->wl
->irq_lock
);
309 err
= dfops
->write(dev
, buf
, count
);
310 spin_unlock_irq(&dev
->wl
->irq_lock
);
312 err
= dfops
->write(dev
, buf
, count
);
317 free_page((unsigned long)buf
);
319 mutex_unlock(&dev
->wl
->mutex
);
321 return err
? err
: count
;
325 #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
326 static struct b43legacy_debugfs_fops fops_##name = { \
330 .open = simple_open, \
331 .read = b43legacy_debugfs_read, \
332 .write = b43legacy_debugfs_write, \
333 .llseek = generic_file_llseek, \
335 .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
337 .take_irqlock = _take_irqlock, \
340 B43legacy_DEBUGFS_FOPS(tsf
, tsf_read_file
, tsf_write_file
, 1);
341 B43legacy_DEBUGFS_FOPS(ucode_regs
, ucode_regs_read_file
, NULL
, 1);
342 B43legacy_DEBUGFS_FOPS(shm
, shm_read_file
, NULL
, 1);
343 B43legacy_DEBUGFS_FOPS(txstat
, txstat_read_file
, NULL
, 0);
344 B43legacy_DEBUGFS_FOPS(restart
, NULL
, restart_write_file
, 1);
347 int b43legacy_debug(struct b43legacy_wldev
*dev
, enum b43legacy_dyndbg feature
)
349 return !!(dev
->dfsentry
&& dev
->dfsentry
->dyn_debug
[feature
]);
352 static void b43legacy_remove_dynamic_debug(struct b43legacy_wldev
*dev
)
354 struct b43legacy_dfsentry
*e
= dev
->dfsentry
;
357 for (i
= 0; i
< __B43legacy_NR_DYNDBG
; i
++)
358 debugfs_remove(e
->dyn_debug_dentries
[i
]);
361 static void b43legacy_add_dynamic_debug(struct b43legacy_wldev
*dev
)
363 struct b43legacy_dfsentry
*e
= dev
->dfsentry
;
366 #define add_dyn_dbg(name, id, initstate) do { \
367 e->dyn_debug[id] = (initstate); \
368 d = debugfs_create_bool(name, 0600, e->subdir, \
369 &(e->dyn_debug[id])); \
371 e->dyn_debug_dentries[id] = d; \
374 add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER
, false);
375 add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW
, false);
376 add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE
, false);
377 add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST
, false);
378 add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP
, false);
383 void b43legacy_debugfs_add_device(struct b43legacy_wldev
*dev
)
385 struct b43legacy_dfsentry
*e
;
386 struct b43legacy_txstatus_log
*log
;
389 B43legacy_WARN_ON(!dev
);
390 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
392 b43legacyerr(dev
->wl
, "debugfs: add device OOM\n");
397 log
->log
= kcalloc(B43legacy_NR_LOGGED_TXSTATUS
,
398 sizeof(struct b43legacy_txstatus
), GFP_KERNEL
);
400 b43legacyerr(dev
->wl
, "debugfs: add device txstatus OOM\n");
405 spin_lock_init(&log
->lock
);
409 snprintf(devdir
, sizeof(devdir
), "%s", wiphy_name(dev
->wl
->hw
->wiphy
));
410 e
->subdir
= debugfs_create_dir(devdir
, rootdir
);
411 if (!e
->subdir
|| IS_ERR(e
->subdir
)) {
412 if (e
->subdir
== ERR_PTR(-ENODEV
)) {
413 b43legacydbg(dev
->wl
, "DebugFS (CONFIG_DEBUG_FS) not "
414 "enabled in kernel config\n");
416 b43legacyerr(dev
->wl
, "debugfs: cannot create %s directory\n",
419 dev
->dfsentry
= NULL
;
425 #define ADD_FILE(name, mode) \
428 d = debugfs_create_file(__stringify(name), \
429 mode, e->subdir, dev, \
430 &fops_##name.fops); \
431 e->file_##name.dentry = NULL; \
433 e->file_##name.dentry = d; \
438 ADD_FILE(ucode_regs
, 0400);
440 ADD_FILE(txstat
, 0400);
441 ADD_FILE(restart
, 0200);
445 b43legacy_add_dynamic_debug(dev
);
448 void b43legacy_debugfs_remove_device(struct b43legacy_wldev
*dev
)
450 struct b43legacy_dfsentry
*e
;
457 b43legacy_remove_dynamic_debug(dev
);
459 debugfs_remove(e
->file_tsf
.dentry
);
460 debugfs_remove(e
->file_ucode_regs
.dentry
);
461 debugfs_remove(e
->file_shm
.dentry
);
462 debugfs_remove(e
->file_txstat
.dentry
);
463 debugfs_remove(e
->file_restart
.dentry
);
465 debugfs_remove(e
->subdir
);
466 kfree(e
->txstatlog
.log
);
470 void b43legacy_debugfs_log_txstat(struct b43legacy_wldev
*dev
,
471 const struct b43legacy_txstatus
*status
)
473 struct b43legacy_dfsentry
*e
= dev
->dfsentry
;
474 struct b43legacy_txstatus_log
*log
;
475 struct b43legacy_txstatus
*cur
;
481 B43legacy_WARN_ON(!irqs_disabled());
482 spin_lock(&log
->lock
);
484 if (i
== B43legacy_NR_LOGGED_TXSTATUS
)
487 cur
= &(log
->log
[i
]);
488 memcpy(cur
, status
, sizeof(*cur
));
489 spin_unlock(&log
->lock
);
492 void b43legacy_debugfs_init(void)
494 rootdir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
499 void b43legacy_debugfs_exit(void)
501 debugfs_remove(rootdir
);