1 // SPDX-License-Identifier: GPL-2.0-or-later
4 Broadcom B43legacy wireless driver
6 debugfs driver debugging code
14 #include <linux/debugfs.h>
15 #include <linux/slab.h>
16 #include <linux/netdevice.h>
17 #include <linux/pci.h>
18 #include <linux/mutex.h>
20 #include "b43legacy.h"
28 /* The root directory. */
29 static struct dentry *rootdir;
31 struct b43legacy_debugfs_fops {
32 ssize_t (*read)(struct b43legacy_wldev *dev, char *buf, size_t bufsize);
33 int (*write)(struct b43legacy_wldev *dev, const char *buf, size_t count);
34 /* Offset of struct b43legacy_dfs_file in struct b43legacy_dfsentry */
35 size_t file_struct_offset;
36 /* Take wl->irq_lock before calling read/write? */
41 struct b43legacy_dfs_file * fops_to_dfs_file(struct b43legacy_wldev *dev,
42 const struct b43legacy_debugfs_fops *dfops)
47 p += dfops->file_struct_offset;
53 #define fappend(fmt, x...) \
55 if (bufsize - count) \
56 count += scnprintf(buf + count, \
60 printk(KERN_ERR "b43legacy: fappend overflow\n"); \
64 /* wl->irq_lock is locked */
65 static ssize_t tsf_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
70 b43legacy_tsf_read(dev, &tsf);
71 fappend("0x%08x%08x\n",
72 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
73 (unsigned int)(tsf & 0xFFFFFFFFULL));
78 /* wl->irq_lock is locked */
79 static int tsf_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
83 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
85 b43legacy_tsf_write(dev, tsf);
90 /* wl->irq_lock is locked */
91 static ssize_t ucode_regs_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
96 for (i = 0; i < 64; i++) {
97 fappend("r%d = 0x%04x\n", i,
98 b43legacy_shm_read16(dev, B43legacy_SHM_WIRELESS, i));
104 /* wl->irq_lock is locked */
105 static ssize_t shm_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
110 __le16 *le16buf = (__le16 *)buf;
112 for (i = 0; i < 0x1000; i++) {
113 if (bufsize < sizeof(tmp))
115 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 2 * i);
116 le16buf[i] = cpu_to_le16(tmp);
117 count += sizeof(tmp);
118 bufsize -= sizeof(tmp);
124 static ssize_t txstat_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
126 struct b43legacy_txstatus_log *log = &dev->dfsentry->txstatlog;
130 struct b43legacy_txstatus *stat;
132 spin_lock_irqsave(&log->lock, flags);
134 fappend("Nothing transmitted, yet\n");
137 fappend("b43legacy TX status reports:\n\n"
138 "index | cookie | seq | phy_stat | frame_count | "
139 "rts_count | supp_reason | pm_indicated | "
140 "intermediate | for_ampdu | acked\n" "---\n");
144 if (i == B43legacy_NR_LOGGED_TXSTATUS)
146 stat = &(log->log[i]);
149 "0x%04X | 0x%04X | 0x%02X | "
154 stat->cookie, stat->seq, stat->phy_stat,
155 stat->frame_count, stat->rts_count,
156 stat->supp_reason, stat->pm_indicated,
157 stat->intermediate, stat->for_ampdu,
166 spin_unlock_irqrestore(&log->lock, flags);
171 /* wl->irq_lock is locked */
172 static int restart_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
176 if (count > 0 && buf[0] == '1') {
177 b43legacy_controller_restart(dev, "manually restarted");
186 static ssize_t b43legacy_debugfs_read(struct file *file, char __user *userbuf,
187 size_t count, loff_t *ppos)
189 struct b43legacy_wldev *dev;
190 const struct b43legacy_debugfs_fops *dfops;
191 struct b43legacy_dfs_file *dfile;
194 const size_t bufsize = 1024 * 16; /* 16 KiB buffer */
195 const size_t buforder = get_order(bufsize);
200 dev = file->private_data;
204 mutex_lock(&dev->wl->mutex);
205 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
210 dfops = debugfs_get_aux(file);
215 dfile = fops_to_dfs_file(dev, dfops);
217 if (!dfile->buffer) {
218 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
223 memset(buf, 0, bufsize);
224 if (dfops->take_irqlock) {
225 spin_lock_irq(&dev->wl->irq_lock);
226 ret = dfops->read(dev, buf, bufsize);
227 spin_unlock_irq(&dev->wl->irq_lock);
229 ret = dfops->read(dev, buf, bufsize);
231 free_pages((unsigned long)buf, buforder);
235 dfile->data_len = ret;
239 ret = simple_read_from_buffer(userbuf, count, ppos,
242 if (*ppos >= dfile->data_len) {
243 free_pages((unsigned long)dfile->buffer, buforder);
244 dfile->buffer = NULL;
248 mutex_unlock(&dev->wl->mutex);
250 return err ? err : ret;
253 static ssize_t b43legacy_debugfs_write(struct file *file,
254 const char __user *userbuf,
255 size_t count, loff_t *ppos)
257 struct b43legacy_wldev *dev;
258 const struct b43legacy_debugfs_fops *dfops;
264 if (count > PAGE_SIZE)
266 dev = file->private_data;
270 mutex_lock(&dev->wl->mutex);
271 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
276 dfops = debugfs_get_aux(file);
282 buf = (char *)get_zeroed_page(GFP_KERNEL);
287 if (copy_from_user(buf, userbuf, count)) {
291 if (dfops->take_irqlock) {
292 spin_lock_irq(&dev->wl->irq_lock);
293 err = dfops->write(dev, buf, count);
294 spin_unlock_irq(&dev->wl->irq_lock);
296 err = dfops->write(dev, buf, count);
301 free_page((unsigned long)buf);
303 mutex_unlock(&dev->wl->mutex);
305 return err ? err : count;
308 static struct debugfs_short_fops debugfs_ops = {
309 .read = b43legacy_debugfs_read,
310 .write = b43legacy_debugfs_write,
311 .llseek = generic_file_llseek
314 #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
315 static struct b43legacy_debugfs_fops fops_##name = { \
318 .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
320 .take_irqlock = _take_irqlock, \
323 B43legacy_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
324 B43legacy_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
325 B43legacy_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
326 B43legacy_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
327 B43legacy_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
330 int b43legacy_debug(struct b43legacy_wldev *dev, enum b43legacy_dyndbg feature)
332 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
335 static void b43legacy_add_dynamic_debug(struct b43legacy_wldev *dev)
337 struct b43legacy_dfsentry *e = dev->dfsentry;
339 #define add_dyn_dbg(name, id, initstate) do { \
340 e->dyn_debug[id] = (initstate); \
341 debugfs_create_bool(name, 0600, e->subdir, \
342 &(e->dyn_debug[id])); \
345 add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER, false);
346 add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW, false);
347 add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE, false);
348 add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST, false);
349 add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP, false);
354 void b43legacy_debugfs_add_device(struct b43legacy_wldev *dev)
356 struct b43legacy_dfsentry *e;
357 struct b43legacy_txstatus_log *log;
360 B43legacy_WARN_ON(!dev);
361 e = kzalloc(sizeof(*e), GFP_KERNEL);
363 b43legacyerr(dev->wl, "debugfs: add device OOM\n");
368 log->log = kcalloc(B43legacy_NR_LOGGED_TXSTATUS,
369 sizeof(struct b43legacy_txstatus), GFP_KERNEL);
371 b43legacyerr(dev->wl, "debugfs: add device txstatus OOM\n");
376 spin_lock_init(&log->lock);
380 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
381 e->subdir = debugfs_create_dir(devdir, rootdir);
383 #define ADD_FILE(name, mode) \
385 debugfs_create_file_aux(__stringify(name), mode, \
387 &fops_##name, &debugfs_ops); \
392 ADD_FILE(ucode_regs, 0400);
394 ADD_FILE(txstat, 0400);
395 ADD_FILE(restart, 0200);
399 b43legacy_add_dynamic_debug(dev);
402 void b43legacy_debugfs_remove_device(struct b43legacy_wldev *dev)
404 struct b43legacy_dfsentry *e;
412 debugfs_remove(e->subdir);
413 kfree(e->txstatlog.log);
417 void b43legacy_debugfs_log_txstat(struct b43legacy_wldev *dev,
418 const struct b43legacy_txstatus *status)
420 struct b43legacy_dfsentry *e = dev->dfsentry;
421 struct b43legacy_txstatus_log *log;
422 struct b43legacy_txstatus *cur;
428 B43legacy_WARN_ON(!irqs_disabled());
429 spin_lock(&log->lock);
431 if (i == B43legacy_NR_LOGGED_TXSTATUS)
434 cur = &(log->log[i]);
435 memcpy(cur, status, sizeof(*cur));
436 spin_unlock(&log->lock);
439 void b43legacy_debugfs_init(void)
441 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
444 void b43legacy_debugfs_exit(void)
446 debugfs_remove(rootdir);