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