include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / net / wireless / b43legacy / debugfs.c
blob5e28ad0d6d17f8a1c43401594a42f2b040bbdad5
1 /*
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.
26 #include <linux/fs.h>
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"
34 #include "main.h"
35 #include "debugfs.h"
36 #include "dma.h"
37 #include "pio.h"
38 #include "xmit.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? */
51 bool take_irqlock;
54 static inline
55 struct b43legacy_dfs_file * fops_to_dfs_file(struct b43legacy_wldev *dev,
56 const struct b43legacy_debugfs_fops *dfops)
58 void *p;
60 p = dev->dfsentry;
61 p += dfops->file_struct_offset;
63 return p;
67 #define fappend(fmt, x...) \
68 do { \
69 if (bufsize - count) \
70 count += snprintf(buf + count, \
71 bufsize - count, \
72 fmt , ##x); \
73 else \
74 printk(KERN_ERR "b43legacy: fappend overflow\n"); \
75 } while (0)
78 /* wl->irq_lock is locked */
79 static ssize_t tsf_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
81 ssize_t count = 0;
82 u64 tsf;
84 b43legacy_tsf_read(dev, &tsf);
85 fappend("0x%08x%08x\n",
86 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
87 (unsigned int)(tsf & 0xFFFFFFFFULL));
89 return count;
92 /* wl->irq_lock is locked */
93 static int tsf_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
95 u64 tsf;
97 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
98 return -EINVAL;
99 b43legacy_tsf_write(dev, tsf);
101 return 0;
104 /* wl->irq_lock is locked */
105 static ssize_t ucode_regs_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
107 ssize_t count = 0;
108 int i;
110 for (i = 0; i < 64; i++) {
111 fappend("r%d = 0x%04x\n", i,
112 b43legacy_shm_read16(dev, B43legacy_SHM_WIRELESS, i));
115 return count;
118 /* wl->irq_lock is locked */
119 static ssize_t shm_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
121 ssize_t count = 0;
122 int i;
123 u16 tmp;
124 __le16 *le16buf = (__le16 *)buf;
126 for (i = 0; i < 0x1000; i++) {
127 if (bufsize < sizeof(tmp))
128 break;
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);
135 return count;
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;
141 ssize_t count = 0;
142 unsigned long flags;
143 int i, idx;
144 struct b43legacy_txstatus *stat;
146 spin_lock_irqsave(&log->lock, flags);
147 if (log->end < 0) {
148 fappend("Nothing transmitted, yet\n");
149 goto out_unlock;
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");
155 i = log->end + 1;
156 idx = 0;
157 while (1) {
158 if (i == B43legacy_NR_LOGGED_TXSTATUS)
159 i = 0;
160 stat = &(log->log[i]);
161 if (stat->cookie) {
162 fappend("%03d | "
163 "0x%04X | 0x%04X | 0x%02X | "
164 "0x%X | 0x%X | "
165 "%u | %u | "
166 "%u | %u | %u\n",
167 idx,
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,
172 stat->acked);
173 idx++;
175 if (i == log->end)
176 break;
177 i++;
179 out_unlock:
180 spin_unlock_irqrestore(&log->lock, flags);
182 return count;
185 /* wl->irq_lock is locked */
186 static int restart_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
188 int err = 0;
190 if (count > 0 && buf[0] == '1') {
191 b43legacy_controller_restart(dev, "manually restarted");
192 } else
193 err = -EINVAL;
195 return err;
198 #undef fappend
200 static int b43legacy_debugfs_open(struct inode *inode, struct file *file)
202 file->private_data = inode->i_private;
203 return 0;
206 static ssize_t b43legacy_debugfs_read(struct file *file, char __user *userbuf,
207 size_t count, loff_t *ppos)
209 struct b43legacy_wldev *dev;
210 struct b43legacy_debugfs_fops *dfops;
211 struct b43legacy_dfs_file *dfile;
212 ssize_t uninitialized_var(ret);
213 char *buf;
214 const size_t bufsize = 1024 * 16; /* 16 KiB buffer */
215 const size_t buforder = get_order(bufsize);
216 int err = 0;
218 if (!count)
219 return 0;
220 dev = file->private_data;
221 if (!dev)
222 return -ENODEV;
224 mutex_lock(&dev->wl->mutex);
225 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
226 err = -ENODEV;
227 goto out_unlock;
230 dfops = container_of(file->f_op, struct b43legacy_debugfs_fops, fops);
231 if (!dfops->read) {
232 err = -ENOSYS;
233 goto out_unlock;
235 dfile = fops_to_dfs_file(dev, dfops);
237 if (!dfile->buffer) {
238 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
239 if (!buf) {
240 err = -ENOMEM;
241 goto out_unlock;
243 memset(buf, 0, bufsize);
244 if (dfops->take_irqlock) {
245 spin_lock_irq(&dev->wl->irq_lock);
246 ret = dfops->read(dev, buf, bufsize);
247 spin_unlock_irq(&dev->wl->irq_lock);
248 } else
249 ret = dfops->read(dev, buf, bufsize);
250 if (ret <= 0) {
251 free_pages((unsigned long)buf, buforder);
252 err = ret;
253 goto out_unlock;
255 dfile->data_len = ret;
256 dfile->buffer = buf;
259 ret = simple_read_from_buffer(userbuf, count, ppos,
260 dfile->buffer,
261 dfile->data_len);
262 if (*ppos >= dfile->data_len) {
263 free_pages((unsigned long)dfile->buffer, buforder);
264 dfile->buffer = NULL;
265 dfile->data_len = 0;
267 out_unlock:
268 mutex_unlock(&dev->wl->mutex);
270 return err ? err : ret;
273 static ssize_t b43legacy_debugfs_write(struct file *file,
274 const char __user *userbuf,
275 size_t count, loff_t *ppos)
277 struct b43legacy_wldev *dev;
278 struct b43legacy_debugfs_fops *dfops;
279 char *buf;
280 int err = 0;
282 if (!count)
283 return 0;
284 if (count > PAGE_SIZE)
285 return -E2BIG;
286 dev = file->private_data;
287 if (!dev)
288 return -ENODEV;
290 mutex_lock(&dev->wl->mutex);
291 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
292 err = -ENODEV;
293 goto out_unlock;
296 dfops = container_of(file->f_op, struct b43legacy_debugfs_fops, fops);
297 if (!dfops->write) {
298 err = -ENOSYS;
299 goto out_unlock;
302 buf = (char *)get_zeroed_page(GFP_KERNEL);
303 if (!buf) {
304 err = -ENOMEM;
305 goto out_unlock;
307 if (copy_from_user(buf, userbuf, count)) {
308 err = -EFAULT;
309 goto out_freepage;
311 if (dfops->take_irqlock) {
312 spin_lock_irq(&dev->wl->irq_lock);
313 err = dfops->write(dev, buf, count);
314 spin_unlock_irq(&dev->wl->irq_lock);
315 } else
316 err = dfops->write(dev, buf, count);
317 if (err)
318 goto out_freepage;
320 out_freepage:
321 free_page((unsigned long)buf);
322 out_unlock:
323 mutex_unlock(&dev->wl->mutex);
325 return err ? err : count;
329 #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
330 static struct b43legacy_debugfs_fops fops_##name = { \
331 .read = _read, \
332 .write = _write, \
333 .fops = { \
334 .open = b43legacy_debugfs_open, \
335 .read = b43legacy_debugfs_read, \
336 .write = b43legacy_debugfs_write, \
337 .llseek = generic_file_llseek, \
338 }, \
339 .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
340 file_##name), \
341 .take_irqlock = _take_irqlock, \
344 B43legacy_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
345 B43legacy_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
346 B43legacy_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
347 B43legacy_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
348 B43legacy_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
351 int b43legacy_debug(struct b43legacy_wldev *dev, enum b43legacy_dyndbg feature)
353 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
356 static void b43legacy_remove_dynamic_debug(struct b43legacy_wldev *dev)
358 struct b43legacy_dfsentry *e = dev->dfsentry;
359 int i;
361 for (i = 0; i < __B43legacy_NR_DYNDBG; i++)
362 debugfs_remove(e->dyn_debug_dentries[i]);
365 static void b43legacy_add_dynamic_debug(struct b43legacy_wldev *dev)
367 struct b43legacy_dfsentry *e = dev->dfsentry;
368 struct dentry *d;
370 #define add_dyn_dbg(name, id, initstate) do { \
371 e->dyn_debug[id] = (initstate); \
372 d = debugfs_create_bool(name, 0600, e->subdir, \
373 &(e->dyn_debug[id])); \
374 if (!IS_ERR(d)) \
375 e->dyn_debug_dentries[id] = d; \
376 } while (0)
378 add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER, 0);
379 add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW, 0);
380 add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE, 0);
381 add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST, 0);
382 add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP, 0);
384 #undef add_dyn_dbg
387 void b43legacy_debugfs_add_device(struct b43legacy_wldev *dev)
389 struct b43legacy_dfsentry *e;
390 struct b43legacy_txstatus_log *log;
391 char devdir[16];
393 B43legacy_WARN_ON(!dev);
394 e = kzalloc(sizeof(*e), GFP_KERNEL);
395 if (!e) {
396 b43legacyerr(dev->wl, "debugfs: add device OOM\n");
397 return;
399 e->dev = dev;
400 log = &e->txstatlog;
401 log->log = kcalloc(B43legacy_NR_LOGGED_TXSTATUS,
402 sizeof(struct b43legacy_txstatus), GFP_KERNEL);
403 if (!log->log) {
404 b43legacyerr(dev->wl, "debugfs: add device txstatus OOM\n");
405 kfree(e);
406 return;
408 log->end = -1;
409 spin_lock_init(&log->lock);
411 dev->dfsentry = e;
413 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
414 e->subdir = debugfs_create_dir(devdir, rootdir);
415 if (!e->subdir || IS_ERR(e->subdir)) {
416 if (e->subdir == ERR_PTR(-ENODEV)) {
417 b43legacydbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
418 "enabled in kernel config\n");
419 } else {
420 b43legacyerr(dev->wl, "debugfs: cannot create %s directory\n",
421 devdir);
423 dev->dfsentry = NULL;
424 kfree(log->log);
425 kfree(e);
426 return;
429 #define ADD_FILE(name, mode) \
430 do { \
431 struct dentry *d; \
432 d = debugfs_create_file(__stringify(name), \
433 mode, e->subdir, dev, \
434 &fops_##name.fops); \
435 e->file_##name.dentry = NULL; \
436 if (!IS_ERR(d)) \
437 e->file_##name.dentry = d; \
438 } while (0)
441 ADD_FILE(tsf, 0600);
442 ADD_FILE(ucode_regs, 0400);
443 ADD_FILE(shm, 0400);
444 ADD_FILE(txstat, 0400);
445 ADD_FILE(restart, 0200);
447 #undef ADD_FILE
449 b43legacy_add_dynamic_debug(dev);
452 void b43legacy_debugfs_remove_device(struct b43legacy_wldev *dev)
454 struct b43legacy_dfsentry *e;
456 if (!dev)
457 return;
458 e = dev->dfsentry;
459 if (!e)
460 return;
461 b43legacy_remove_dynamic_debug(dev);
463 debugfs_remove(e->file_tsf.dentry);
464 debugfs_remove(e->file_ucode_regs.dentry);
465 debugfs_remove(e->file_shm.dentry);
466 debugfs_remove(e->file_txstat.dentry);
467 debugfs_remove(e->file_restart.dentry);
469 debugfs_remove(e->subdir);
470 kfree(e->txstatlog.log);
471 kfree(e);
474 void b43legacy_debugfs_log_txstat(struct b43legacy_wldev *dev,
475 const struct b43legacy_txstatus *status)
477 struct b43legacy_dfsentry *e = dev->dfsentry;
478 struct b43legacy_txstatus_log *log;
479 struct b43legacy_txstatus *cur;
480 int i;
482 if (!e)
483 return;
484 log = &e->txstatlog;
485 B43legacy_WARN_ON(!irqs_disabled());
486 spin_lock(&log->lock);
487 i = log->end + 1;
488 if (i == B43legacy_NR_LOGGED_TXSTATUS)
489 i = 0;
490 log->end = i;
491 cur = &(log->log[i]);
492 memcpy(cur, status, sizeof(*cur));
493 spin_unlock(&log->lock);
496 void b43legacy_debugfs_init(void)
498 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
499 if (IS_ERR(rootdir))
500 rootdir = NULL;
503 void b43legacy_debugfs_exit(void)
505 debugfs_remove(rootdir);