3aafde9f08a8b0c5c6d65ab12f598140369d1788
[deliverable/linux.git] / drivers / net / wireless / b43 / debugfs.c
1 /*
2
3 Broadcom B43 wireless driver
4
5 debugfs driver debugging code
6
7 Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
8
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.
13
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.
18
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.
23
24 */
25
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>
32
33 #include "b43.h"
34 #include "main.h"
35 #include "debugfs.h"
36 #include "dma.h"
37 #include "pio.h"
38 #include "xmit.h"
39
40
41 /* The root directory. */
42 static struct dentry *rootdir;
43
44 struct b43_debugfs_fops {
45 ssize_t (*read)(struct b43_wldev *dev, char *buf, size_t bufsize);
46 int (*write)(struct b43_wldev *dev, const char *buf, size_t count);
47 struct file_operations fops;
48 /* Offset of struct b43_dfs_file in struct b43_dfsentry */
49 size_t file_struct_offset;
50 /* Take wl->irq_lock before calling read/write? */
51 bool take_irqlock;
52 };
53
54 static inline
55 struct b43_dfs_file * fops_to_dfs_file(struct b43_wldev *dev,
56 const struct b43_debugfs_fops *dfops)
57 {
58 void *p;
59
60 p = dev->dfsentry;
61 p += dfops->file_struct_offset;
62
63 return p;
64 }
65
66
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 "b43: fappend overflow\n"); \
75 } while (0)
76
77
78 /* wl->irq_lock is locked */
79 static ssize_t tsf_read_file(struct b43_wldev *dev,
80 char *buf, size_t bufsize)
81 {
82 ssize_t count = 0;
83 u64 tsf;
84
85 b43_tsf_read(dev, &tsf);
86 fappend("0x%08x%08x\n",
87 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
88 (unsigned int)(tsf & 0xFFFFFFFFULL));
89
90 return count;
91 }
92
93 /* wl->irq_lock is locked */
94 static int tsf_write_file(struct b43_wldev *dev,
95 const char *buf, size_t count)
96 {
97 u64 tsf;
98
99 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
100 return -EINVAL;
101 b43_tsf_write(dev, tsf);
102
103 return 0;
104 }
105
106 /* wl->irq_lock is locked */
107 static ssize_t ucode_regs_read_file(struct b43_wldev *dev,
108 char *buf, size_t bufsize)
109 {
110 ssize_t count = 0;
111 int i;
112
113 for (i = 0; i < 64; i++) {
114 fappend("r%d = 0x%04x\n", i,
115 b43_shm_read16(dev, B43_SHM_SCRATCH, i));
116 }
117
118 return count;
119 }
120
121 /* wl->irq_lock is locked */
122 static ssize_t shm_read_file(struct b43_wldev *dev,
123 char *buf, size_t bufsize)
124 {
125 ssize_t count = 0;
126 int i;
127 u16 tmp;
128 __le16 *le16buf = (__le16 *)buf;
129
130 for (i = 0; i < 0x1000; i++) {
131 if (bufsize <= 0)
132 break;
133 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 2 * i);
134 le16buf[i] = cpu_to_le16(tmp);
135 count += sizeof(tmp);
136 bufsize -= sizeof(tmp);
137 }
138
139 return count;
140 }
141
142 static ssize_t txstat_read_file(struct b43_wldev *dev,
143 char *buf, size_t bufsize)
144 {
145 struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
146 ssize_t count = 0;
147 unsigned long flags;
148 int i, idx;
149 struct b43_txstatus *stat;
150
151 spin_lock_irqsave(&log->lock, flags);
152 if (log->end < 0) {
153 fappend("Nothing transmitted, yet\n");
154 goto out_unlock;
155 }
156 fappend("b43 TX status reports:\n\n"
157 "index | cookie | seq | phy_stat | frame_count | "
158 "rts_count | supp_reason | pm_indicated | "
159 "intermediate | for_ampdu | acked\n" "---\n");
160 i = log->end + 1;
161 idx = 0;
162 while (1) {
163 if (i == B43_NR_LOGGED_TXSTATUS)
164 i = 0;
165 stat = &(log->log[i]);
166 if (stat->cookie) {
167 fappend("%03d | "
168 "0x%04X | 0x%04X | 0x%02X | "
169 "0x%X | 0x%X | "
170 "%u | %u | "
171 "%u | %u | %u\n",
172 idx,
173 stat->cookie, stat->seq, stat->phy_stat,
174 stat->frame_count, stat->rts_count,
175 stat->supp_reason, stat->pm_indicated,
176 stat->intermediate, stat->for_ampdu,
177 stat->acked);
178 idx++;
179 }
180 if (i == log->end)
181 break;
182 i++;
183 }
184 out_unlock:
185 spin_unlock_irqrestore(&log->lock, flags);
186
187 return count;
188 }
189
190 static ssize_t txpower_g_read_file(struct b43_wldev *dev,
191 char *buf, size_t bufsize)
192 {
193 ssize_t count = 0;
194
195 if (dev->phy.type != B43_PHYTYPE_G) {
196 fappend("Device is not a G-PHY\n");
197 goto out;
198 }
199 fappend("Control: %s\n", dev->phy.manual_txpower_control ?
200 "MANUAL" : "AUTOMATIC");
201 fappend("Baseband attenuation: %u\n", dev->phy.bbatt.att);
202 fappend("Radio attenuation: %u\n", dev->phy.rfatt.att);
203 fappend("TX Mixer Gain: %s\n",
204 (dev->phy.tx_control & B43_TXCTL_TXMIX) ? "ON" : "OFF");
205 fappend("PA Gain 2dB: %s\n",
206 (dev->phy.tx_control & B43_TXCTL_PA2DB) ? "ON" : "OFF");
207 fappend("PA Gain 3dB: %s\n",
208 (dev->phy.tx_control & B43_TXCTL_PA3DB) ? "ON" : "OFF");
209 fappend("\n\n");
210 fappend("You can write to this file:\n");
211 fappend("Writing \"auto\" enables automatic txpower control.\n");
212 fappend
213 ("Writing the attenuation values as \"bbatt rfatt txmix pa2db pa3db\" "
214 "enables manual txpower control.\n");
215 fappend("Example: 5 4 0 0 1\n");
216 fappend("Enables manual control with Baseband attenuation 5, "
217 "Radio attenuation 4, No TX Mixer Gain, "
218 "No PA Gain 2dB, With PA Gain 3dB.\n");
219 out:
220 return count;
221 }
222
223 static int txpower_g_write_file(struct b43_wldev *dev,
224 const char *buf, size_t count)
225 {
226 unsigned long flags;
227 unsigned long phy_flags;
228 int err = 0;
229
230 spin_lock_irqsave(&dev->wl->irq_lock, flags);
231 if (dev->phy.type != B43_PHYTYPE_G) {
232 err = -ENODEV;
233 goto out_unlock;
234 }
235 if ((count >= 4) && (memcmp(buf, "auto", 4) == 0)) {
236 /* Automatic control */
237 dev->phy.manual_txpower_control = 0;
238 b43_phy_xmitpower(dev);
239 } else {
240 int bbatt = 0, rfatt = 0, txmix = 0, pa2db = 0, pa3db = 0;
241 /* Manual control */
242 if (sscanf(buf, "%d %d %d %d %d", &bbatt, &rfatt,
243 &txmix, &pa2db, &pa3db) != 5) {
244 err = -EINVAL;
245 goto out_unlock;
246 }
247 b43_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
248 dev->phy.manual_txpower_control = 1;
249 dev->phy.bbatt.att = bbatt;
250 dev->phy.rfatt.att = rfatt;
251 dev->phy.tx_control = 0;
252 if (txmix)
253 dev->phy.tx_control |= B43_TXCTL_TXMIX;
254 if (pa2db)
255 dev->phy.tx_control |= B43_TXCTL_PA2DB;
256 if (pa3db)
257 dev->phy.tx_control |= B43_TXCTL_PA3DB;
258 b43_phy_lock(dev, phy_flags);
259 b43_radio_lock(dev);
260 b43_set_txpower_g(dev, &dev->phy.bbatt,
261 &dev->phy.rfatt, dev->phy.tx_control);
262 b43_radio_unlock(dev);
263 b43_phy_unlock(dev, phy_flags);
264 }
265 out_unlock:
266 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
267
268 return err;
269 }
270
271 /* wl->irq_lock is locked */
272 static int restart_write_file(struct b43_wldev *dev,
273 const char *buf, size_t count)
274 {
275 int err = 0;
276
277 if (count > 0 && buf[0] == '1') {
278 b43_controller_restart(dev, "manually restarted");
279 } else
280 err = -EINVAL;
281
282 return err;
283 }
284
285 static ssize_t append_lo_table(ssize_t count, char *buf, const size_t bufsize,
286 struct b43_loctl table[B43_NR_BB][B43_NR_RF])
287 {
288 unsigned int i, j;
289 struct b43_loctl *ctl;
290
291 for (i = 0; i < B43_NR_BB; i++) {
292 for (j = 0; j < B43_NR_RF; j++) {
293 ctl = &(table[i][j]);
294 fappend("(bbatt %2u, rfatt %2u) -> "
295 "(I %+3d, Q %+3d, Used: %d, Calibrated: %d)\n",
296 i, j, ctl->i, ctl->q,
297 ctl->used,
298 b43_loctl_is_calibrated(ctl));
299 }
300 }
301
302 return count;
303 }
304
305 static ssize_t loctls_read_file(struct b43_wldev *dev,
306 char *buf, size_t bufsize)
307 {
308 ssize_t count = 0;
309 struct b43_txpower_lo_control *lo;
310 int i, err = 0;
311
312 if (dev->phy.type != B43_PHYTYPE_G) {
313 fappend("Device is not a G-PHY\n");
314 err = -ENODEV;
315 goto out;
316 }
317 lo = dev->phy.lo_control;
318 fappend("-- Local Oscillator calibration data --\n\n");
319 fappend("Measured: %d, Rebuild: %d, HW-power-control: %d\n",
320 lo->lo_measured,
321 lo->rebuild,
322 dev->phy.hardware_power_control);
323 fappend("TX Bias: 0x%02X, TX Magn: 0x%02X\n",
324 lo->tx_bias, lo->tx_magn);
325 fappend("Power Vector: 0x%08X%08X\n",
326 (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
327 (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL));
328 fappend("\nControl table WITH PADMIX:\n");
329 count = append_lo_table(count, buf, bufsize, lo->with_padmix);
330 fappend("\nControl table WITHOUT PADMIX:\n");
331 count = append_lo_table(count, buf, bufsize, lo->no_padmix);
332 fappend("\nUsed RF attenuation values: Value(WithPadmix flag)\n");
333 for (i = 0; i < lo->rfatt_list.len; i++) {
334 fappend("%u(%d), ",
335 lo->rfatt_list.list[i].att,
336 lo->rfatt_list.list[i].with_padmix);
337 }
338 fappend("\n");
339 fappend("\nUsed Baseband attenuation values:\n");
340 for (i = 0; i < lo->bbatt_list.len; i++) {
341 fappend("%u, ",
342 lo->bbatt_list.list[i].att);
343 }
344 fappend("\n");
345
346 out:
347 return err ? err : count;
348 }
349
350 #undef fappend
351
352 static int b43_debugfs_open(struct inode *inode, struct file *file)
353 {
354 file->private_data = inode->i_private;
355 return 0;
356 }
357
358 static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
359 size_t count, loff_t *ppos)
360 {
361 struct b43_wldev *dev;
362 struct b43_debugfs_fops *dfops;
363 struct b43_dfs_file *dfile;
364 ssize_t ret;
365 char *buf;
366 const size_t bufsize = 1024 * 128;
367 const size_t buforder = get_order(bufsize);
368 int err = 0;
369
370 if (!count)
371 return 0;
372 dev = file->private_data;
373 if (!dev)
374 return -ENODEV;
375
376 mutex_lock(&dev->wl->mutex);
377 if (b43_status(dev) < B43_STAT_INITIALIZED) {
378 err = -ENODEV;
379 goto out_unlock;
380 }
381
382 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
383 if (!dfops->read) {
384 err = -ENOSYS;
385 goto out_unlock;
386 }
387 dfile = fops_to_dfs_file(dev, dfops);
388
389 if (!dfile->buffer) {
390 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
391 if (!buf) {
392 err = -ENOMEM;
393 goto out_unlock;
394 }
395 /* Sparse warns about the following memset, because it has a big
396 * size value. That warning is bogus, so I will ignore it. --mb */
397 memset(buf, 0, bufsize);
398 if (dfops->take_irqlock) {
399 spin_lock_irq(&dev->wl->irq_lock);
400 ret = dfops->read(dev, buf, bufsize);
401 spin_unlock_irq(&dev->wl->irq_lock);
402 } else
403 ret = dfops->read(dev, buf, bufsize);
404 if (ret <= 0) {
405 free_pages((unsigned long)buf, buforder);
406 err = ret;
407 goto out_unlock;
408 }
409 dfile->data_len = ret;
410 dfile->buffer = buf;
411 }
412
413 ret = simple_read_from_buffer(userbuf, count, ppos,
414 dfile->buffer,
415 dfile->data_len);
416 if (*ppos >= dfile->data_len) {
417 free_pages((unsigned long)dfile->buffer, buforder);
418 dfile->buffer = NULL;
419 dfile->data_len = 0;
420 }
421 out_unlock:
422 mutex_unlock(&dev->wl->mutex);
423
424 return err ? err : ret;
425 }
426
427 static ssize_t b43_debugfs_write(struct file *file,
428 const char __user *userbuf,
429 size_t count, loff_t *ppos)
430 {
431 struct b43_wldev *dev;
432 struct b43_debugfs_fops *dfops;
433 char *buf;
434 int err = 0;
435
436 if (!count)
437 return 0;
438 if (count > PAGE_SIZE)
439 return -E2BIG;
440 dev = file->private_data;
441 if (!dev)
442 return -ENODEV;
443
444 mutex_lock(&dev->wl->mutex);
445 if (b43_status(dev) < B43_STAT_INITIALIZED) {
446 err = -ENODEV;
447 goto out_unlock;
448 }
449
450 dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
451 if (!dfops->write) {
452 err = -ENOSYS;
453 goto out_unlock;
454 }
455
456 buf = (char *)get_zeroed_page(GFP_KERNEL);
457 if (!buf) {
458 err = -ENOMEM;
459 goto out_unlock;
460 }
461 if (copy_from_user(buf, userbuf, count)) {
462 err = -EFAULT;
463 goto out_freepage;
464 }
465 if (dfops->take_irqlock) {
466 spin_lock_irq(&dev->wl->irq_lock);
467 err = dfops->write(dev, buf, count);
468 spin_unlock_irq(&dev->wl->irq_lock);
469 } else
470 err = dfops->write(dev, buf, count);
471 if (err)
472 goto out_freepage;
473
474 out_freepage:
475 free_page((unsigned long)buf);
476 out_unlock:
477 mutex_unlock(&dev->wl->mutex);
478
479 return err ? err : count;
480 }
481
482
483 #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock) \
484 static struct b43_debugfs_fops fops_##name = { \
485 .read = _read, \
486 .write = _write, \
487 .fops = { \
488 .open = b43_debugfs_open, \
489 .read = b43_debugfs_read, \
490 .write = b43_debugfs_write, \
491 }, \
492 .file_struct_offset = offsetof(struct b43_dfsentry, \
493 file_##name), \
494 .take_irqlock = _take_irqlock, \
495 }
496
497 B43_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
498 B43_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
499 B43_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
500 B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
501 B43_DEBUGFS_FOPS(txpower_g, txpower_g_read_file, txpower_g_write_file, 0);
502 B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
503 B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
504
505
506 int b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
507 {
508 return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
509 }
510
511 static void b43_remove_dynamic_debug(struct b43_wldev *dev)
512 {
513 struct b43_dfsentry *e = dev->dfsentry;
514 int i;
515
516 for (i = 0; i < __B43_NR_DYNDBG; i++)
517 debugfs_remove(e->dyn_debug_dentries[i]);
518 }
519
520 static void b43_add_dynamic_debug(struct b43_wldev *dev)
521 {
522 struct b43_dfsentry *e = dev->dfsentry;
523 struct dentry *d;
524
525 #define add_dyn_dbg(name, id, initstate) do { \
526 e->dyn_debug[id] = (initstate); \
527 d = debugfs_create_bool(name, 0600, e->subdir, \
528 &(e->dyn_debug[id])); \
529 if (!IS_ERR(d)) \
530 e->dyn_debug_dentries[id] = d; \
531 } while (0)
532
533 add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
534 add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
535 add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
536 add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
537 add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
538
539 #undef add_dyn_dbg
540 }
541
542 void b43_debugfs_add_device(struct b43_wldev *dev)
543 {
544 struct b43_dfsentry *e;
545 struct b43_txstatus_log *log;
546 char devdir[16];
547
548 B43_WARN_ON(!dev);
549 e = kzalloc(sizeof(*e), GFP_KERNEL);
550 if (!e) {
551 b43err(dev->wl, "debugfs: add device OOM\n");
552 return;
553 }
554 e->dev = dev;
555 log = &e->txstatlog;
556 log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
557 sizeof(struct b43_txstatus), GFP_KERNEL);
558 if (!log->log) {
559 b43err(dev->wl, "debugfs: add device txstatus OOM\n");
560 kfree(e);
561 return;
562 }
563 log->end = -1;
564 spin_lock_init(&log->lock);
565
566 dev->dfsentry = e;
567
568 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
569 e->subdir = debugfs_create_dir(devdir, rootdir);
570 if (!e->subdir || IS_ERR(e->subdir)) {
571 if (e->subdir == ERR_PTR(-ENODEV)) {
572 b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
573 "enabled in kernel config\n");
574 } else {
575 b43err(dev->wl, "debugfs: cannot create %s directory\n",
576 devdir);
577 }
578 dev->dfsentry = NULL;
579 kfree(log->log);
580 kfree(e);
581 return;
582 }
583
584 #define ADD_FILE(name, mode) \
585 do { \
586 struct dentry *d; \
587 d = debugfs_create_file(__stringify(name), \
588 mode, e->subdir, dev, \
589 &fops_##name.fops); \
590 e->file_##name.dentry = NULL; \
591 if (!IS_ERR(d)) \
592 e->file_##name.dentry = d; \
593 } while (0)
594
595
596 ADD_FILE(tsf, 0600);
597 ADD_FILE(ucode_regs, 0400);
598 ADD_FILE(shm, 0400);
599 ADD_FILE(txstat, 0400);
600 ADD_FILE(txpower_g, 0600);
601 ADD_FILE(restart, 0200);
602 ADD_FILE(loctls, 0400);
603
604 #undef ADD_FILE
605
606 b43_add_dynamic_debug(dev);
607 }
608
609 void b43_debugfs_remove_device(struct b43_wldev *dev)
610 {
611 struct b43_dfsentry *e;
612
613 if (!dev)
614 return;
615 e = dev->dfsentry;
616 if (!e)
617 return;
618 b43_remove_dynamic_debug(dev);
619
620 debugfs_remove(e->file_tsf.dentry);
621 debugfs_remove(e->file_ucode_regs.dentry);
622 debugfs_remove(e->file_shm.dentry);
623 debugfs_remove(e->file_txstat.dentry);
624 debugfs_remove(e->file_txpower_g.dentry);
625 debugfs_remove(e->file_restart.dentry);
626 debugfs_remove(e->file_loctls.dentry);
627
628 debugfs_remove(e->subdir);
629 kfree(e->txstatlog.log);
630 kfree(e);
631 }
632
633 void b43_debugfs_log_txstat(struct b43_wldev *dev,
634 const struct b43_txstatus *status)
635 {
636 struct b43_dfsentry *e = dev->dfsentry;
637 struct b43_txstatus_log *log;
638 struct b43_txstatus *cur;
639 int i;
640
641 if (!e)
642 return;
643 log = &e->txstatlog;
644 B43_WARN_ON(!irqs_disabled());
645 spin_lock(&log->lock);
646 i = log->end + 1;
647 if (i == B43_NR_LOGGED_TXSTATUS)
648 i = 0;
649 log->end = i;
650 cur = &(log->log[i]);
651 memcpy(cur, status, sizeof(*cur));
652 spin_unlock(&log->lock);
653 }
654
655 void b43_debugfs_init(void)
656 {
657 rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
658 if (IS_ERR(rootdir))
659 rootdir = NULL;
660 }
661
662 void b43_debugfs_exit(void)
663 {
664 debugfs_remove(rootdir);
665 }
This page took 0.044734 seconds and 4 git commands to generate.