3ea9d08a272a0da4f06344e2e961edfa8216a85b
[openwrt/svn-archive/archive.git] / package / bcm43xx-mac80211 / src / bcm43xx / bcm43xx_debugfs.c
1 /*
2
3 Broadcom BCM43xx wireless driver
4
5 debugfs driver debugging code
6
7 Copyright (c) 2005 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
27
28 #include <linux/fs.h>
29 #include <linux/debugfs.h>
30 #include <linux/slab.h>
31 #include <linux/netdevice.h>
32 #include <linux/pci.h>
33 #include <linux/mutex.h>
34
35 #include "bcm43xx.h"
36 #include "bcm43xx_main.h"
37 #include "bcm43xx_debugfs.h"
38 #include "bcm43xx_dma.h"
39 #include "bcm43xx_pio.h"
40 #include "bcm43xx_xmit.h"
41
42 #define REALLY_BIG_BUFFER_SIZE (1024*256)
43
44 static struct bcm43xx_debugfs fs;
45 static char big_buffer[1024*256];
46 static DEFINE_MUTEX(big_buffer_mutex);
47
48
49 static ssize_t write_file_dummy(struct file *file, const char __user *buf,
50 size_t count, loff_t *ppos)
51 {
52 return count;
53 }
54
55 static int open_file_generic(struct inode *inode, struct file *file)
56 {
57 file->private_data = inode->i_private;
58 return 0;
59 }
60
61 #define fappend(fmt, x...) pos += snprintf(buf + pos, len - pos, fmt , ##x)
62
63 static ssize_t drvinfo_read_file(struct file *file, char __user *userbuf,
64 size_t count, loff_t *ppos)
65 {
66 const size_t len = ARRAY_SIZE(big_buffer);
67 char *buf = big_buffer;
68 size_t pos = 0;
69 ssize_t res;
70
71 mutex_lock(&big_buffer_mutex);
72 /* This is where the information is written to the "driver" file */
73 fappend(KBUILD_MODNAME " driver\n");
74 fappend("Compiled at: %s %s\n", __DATE__, __TIME__);
75 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
76 mutex_unlock(&big_buffer_mutex);
77
78 return res;
79 }
80
81 static ssize_t tsf_read_file(struct file *file, char __user *userbuf,
82 size_t count, loff_t *ppos)
83 {
84 struct bcm43xx_wldev *dev = file->private_data;
85 const size_t len = ARRAY_SIZE(big_buffer);
86 char *buf = big_buffer;
87 size_t pos = 0;
88 ssize_t res;
89 unsigned long flags;
90 u64 tsf;
91
92 mutex_lock(&big_buffer_mutex);
93 mutex_lock(&dev->wl->mutex);
94 spin_lock_irqsave(&dev->wl->irq_lock, flags);
95 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
96 fappend("Board not initialized.\n");
97 goto out;
98 }
99 bcm43xx_tsf_read(dev, &tsf);
100 fappend("0x%08x%08x\n",
101 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
102 (unsigned int)(tsf & 0xFFFFFFFFULL));
103
104 out:
105 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
106 mutex_unlock(&dev->wl->mutex);
107 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
108 mutex_unlock(&big_buffer_mutex);
109
110 return res;
111 }
112
113 static ssize_t tsf_write_file(struct file *file, const char __user *user_buf,
114 size_t count, loff_t *ppos)
115 {
116 struct bcm43xx_wldev *dev = file->private_data;
117 char *buf = big_buffer;
118 ssize_t buf_size;
119 ssize_t res;
120 unsigned long flags;
121 u64 tsf;
122
123 mutex_lock(&big_buffer_mutex);
124 buf_size = min(count, ARRAY_SIZE(big_buffer) - 1);
125 if (copy_from_user(buf, user_buf, buf_size)) {
126 res = -EFAULT;
127 goto out_unlock_bb;
128 }
129 mutex_lock(&dev->wl->mutex);
130 spin_lock_irqsave(&dev->wl->irq_lock, flags);
131 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
132 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
133 res = -EFAULT;
134 goto out_unlock;
135 }
136 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1) {
137 printk(KERN_INFO PFX "debugfs: invalid values for \"tsf\"\n");
138 res = -EINVAL;
139 goto out_unlock;
140 }
141 bcm43xx_tsf_write(dev, tsf);
142 mmiowb();
143 res = buf_size;
144
145 out_unlock:
146 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
147 mutex_unlock(&dev->wl->mutex);
148 out_unlock_bb:
149 mutex_unlock(&big_buffer_mutex);
150
151 return res;
152 }
153
154 static ssize_t txstat_read_file(struct file *file, char __user *userbuf,
155 size_t count, loff_t *ppos)
156 {
157 struct bcm43xx_wldev *dev = file->private_data;
158 struct bcm43xx_dfsentry *e = dev->dfsentry;
159 struct bcm43xx_txstatus_log *log = &e->txstatlog;
160 unsigned long flags;
161 char *buf = log->printbuf;
162 const size_t len = ARRAY_SIZE(log->printbuf);
163 size_t pos = 0;
164 ssize_t res;
165 int i, idx;
166 struct bcm43xx_txstatus *stat;
167
168 mutex_lock(&big_buffer_mutex);
169 spin_lock_irqsave(&log->lock, flags);
170 if (!log->printing) {
171 log->printing = 1;
172 fappend("bcm43xx TX status reports:\n\n"
173 "index | cookie | seq | phy_stat | frame_count | "
174 "rts_count | supp_reason | pm_indicated | "
175 "intermediate | for_ampdu | acked\n"
176 "---\n");
177 i = log->end + 1;
178 idx = 0;
179 while (1) {
180 if (i == BCM43xx_NR_LOGGED_TXSTATUS)
181 i = 0;
182 stat = &(log->log[i]);
183 if (stat->cookie) {
184 fappend("%03d | "
185 "0x%04X | 0x%04X | 0x%02X | "
186 "0x%X | 0x%X | "
187 "%u | %u | "
188 "%u | %u | %u\n",
189 idx,
190 stat->cookie, stat->seq, stat->phy_stat,
191 stat->frame_count, stat->rts_count,
192 stat->supp_reason, stat->pm_indicated,
193 stat->intermediate, stat->for_ampdu,
194 stat->acked);
195 idx++;
196 }
197 if (i == log->end)
198 break;
199 i++;
200 }
201 log->buf_avail = pos;
202 }
203 memcpy(big_buffer, buf,
204 min(log->buf_avail, ARRAY_SIZE(big_buffer)));
205 spin_unlock_irqrestore(&log->lock, flags);
206
207 res = simple_read_from_buffer(userbuf, count, ppos,
208 big_buffer,
209 log->buf_avail);
210 if (*ppos == log->buf_avail) {
211 spin_lock_irqsave(&log->lock, flags);
212 log->printing = 0;
213 spin_unlock_irqrestore(&log->lock, flags);
214 }
215 mutex_unlock(&big_buffer_mutex);
216
217 return res;
218 }
219
220 static ssize_t restart_write_file(struct file *file, const char __user *user_buf,
221 size_t count, loff_t *ppos)
222 {
223 struct bcm43xx_wldev *dev = file->private_data;
224 char *buf = big_buffer;
225 ssize_t buf_size;
226 ssize_t res;
227 unsigned long flags;
228
229 mutex_lock(&big_buffer_mutex);
230 buf_size = min(count, ARRAY_SIZE(big_buffer) - 1);
231 if (copy_from_user(buf, user_buf, buf_size)) {
232 res = -EFAULT;
233 goto out_unlock_bb;
234 }
235 mutex_lock(&dev->wl->mutex);
236 spin_lock_irqsave(&dev->wl->irq_lock, flags);
237 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
238 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
239 res = -EFAULT;
240 goto out_unlock;
241 }
242 if (count > 0 && buf[0] == '1') {
243 bcm43xx_controller_restart(dev, "manually restarted");
244 res = count;
245 } else
246 res = -EINVAL;
247
248 out_unlock:
249 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
250 mutex_unlock(&dev->wl->mutex);
251 out_unlock_bb:
252 mutex_unlock(&big_buffer_mutex);
253
254 return res;
255 }
256
257 static ssize_t txpower_g_read_file(struct file *file, char __user *userbuf,
258 size_t count, loff_t *ppos)
259 {
260 struct bcm43xx_wldev *dev = file->private_data;
261 const size_t len = ARRAY_SIZE(big_buffer);
262 char *buf = big_buffer;
263 size_t pos = 0;
264 ssize_t res;
265 unsigned long flags;
266
267 mutex_lock(&big_buffer_mutex);
268 mutex_lock(&dev->wl->mutex);
269 spin_lock_irqsave(&dev->wl->irq_lock, flags);
270 if ((bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) ||
271 !dev->started) {
272 fappend("Not initialized\n");
273 goto out;
274 }
275 if (dev->phy.type != BCM43xx_PHYTYPE_G) {
276 fappend("Device is not a G-PHY\n");
277 goto out;
278 }
279 fappend("Control: %s\n", dev->phy.manual_txpower_control ?
280 "MANUAL" : "AUTOMATIC");
281 fappend("Baseband attenuation: %u\n", dev->phy.bbatt.att);
282 fappend("Radio attenuation: %u\n", dev->phy.rfatt.att);
283 fappend("TX Mixer Gain: %s\n", (dev->phy.tx_control & BCM43xx_TXCTL_TXMIX) ?
284 "ON" : "OFF");
285 fappend("PA Gain 2dB: %s\n", (dev->phy.tx_control & BCM43xx_TXCTL_PA2DB) ?
286 "ON" : "OFF");
287 fappend("PA Gain 3dB: %s\n", (dev->phy.tx_control & BCM43xx_TXCTL_PA3DB) ?
288 "ON" : "OFF");
289 fappend("\n\n");
290 fappend("You can write to this file:\n");
291 fappend("Writing \"auto\" enables automatic txpower control.\n");
292 fappend("Writing the attenuation values as \"bbatt rfatt txmix pa2db pa3db\" "
293 "enables manual txpower control.\n");
294 fappend("Example: 5 4 0 0 1\n");
295 fappend("Enables manual control with Baseband attenuation 5, "
296 "Radio attenuation 4, No TX Mixer Gain, "
297 "No PA Gain 2dB, With PA Gain 3dB.\n");
298
299 out:
300 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
301 mutex_unlock(&dev->wl->mutex);
302 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
303 mutex_unlock(&big_buffer_mutex);
304
305 return res;
306 }
307
308 static ssize_t txpower_g_write_file(struct file *file, const char __user *user_buf,
309 size_t count, loff_t *ppos)
310 {
311 struct bcm43xx_wldev *dev = file->private_data;
312 char *buf = big_buffer;
313 ssize_t buf_size;
314 ssize_t res;
315 unsigned long flags, phy_flags;
316
317 mutex_lock(&big_buffer_mutex);
318 buf_size = min(count, ARRAY_SIZE(big_buffer) - 1);
319 if (copy_from_user(buf, user_buf, buf_size)) {
320 res = -EFAULT;
321 goto out_unlock_bb;
322 }
323 mutex_lock(&dev->wl->mutex);
324 spin_lock_irqsave(&dev->wl->irq_lock, flags);
325 if ((bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) ||
326 !dev->started) {
327 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
328 res = -ENODEV;
329 goto out_unlock;
330 }
331 if (dev->phy.type != BCM43xx_PHYTYPE_G) {
332 printk(KERN_ERR PFX "debugfs: Device is not a G-PHY\n");
333 res = -ENODEV;
334 goto out_unlock;
335 }
336 if ((buf_size >= 4) && (memcmp(buf, "auto", 4) == 0)) {
337 /* Automatic control */
338 dev->phy.manual_txpower_control = 0;
339 bcm43xx_phy_xmitpower(dev);
340 } else {
341 int bbatt = 0, rfatt = 0, txmix = 0, pa2db = 0, pa3db = 0;
342 /* Manual control */
343 if (sscanf(buf, "%d %d %d %d %d", &bbatt, &rfatt,
344 &txmix, &pa2db, &pa3db) != 5) {
345 printk(KERN_INFO PFX "debugfs: invalid value for \"tx_power_g\"\n");
346 res = -EINVAL;
347 goto out_unlock;
348 }
349 bcm43xx_put_attenuation_into_ranges(dev, &bbatt, &rfatt);
350 dev->phy.manual_txpower_control = 1;
351 dev->phy.bbatt.att = bbatt;
352 dev->phy.rfatt.att = rfatt;
353 dev->phy.tx_control = 0;
354 if (txmix)
355 dev->phy.tx_control |= BCM43xx_TXCTL_TXMIX;
356 if (pa2db)
357 dev->phy.tx_control |= BCM43xx_TXCTL_PA2DB;
358 if (pa3db)
359 dev->phy.tx_control |= BCM43xx_TXCTL_PA3DB;
360 bcm43xx_phy_lock(dev, phy_flags);
361 bcm43xx_radio_lock(dev);
362 bcm43xx_set_txpower_g(dev, &dev->phy.bbatt,
363 &dev->phy.rfatt, dev->phy.tx_control);
364 bcm43xx_radio_unlock(dev);
365 bcm43xx_phy_unlock(dev, phy_flags);
366 }
367 res = buf_size;
368 out_unlock:
369 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
370 mutex_unlock(&dev->wl->mutex);
371 out_unlock_bb:
372 mutex_unlock(&big_buffer_mutex);
373
374 return res;
375 }
376
377
378 #undef fappend
379
380
381 static struct file_operations drvinfo_fops = {
382 .read = drvinfo_read_file,
383 .write = write_file_dummy,
384 .open = open_file_generic,
385 };
386
387 static struct file_operations tsf_fops = {
388 .read = tsf_read_file,
389 .write = tsf_write_file,
390 .open = open_file_generic,
391 };
392
393 static struct file_operations txstat_fops = {
394 .read = txstat_read_file,
395 .write = write_file_dummy,
396 .open = open_file_generic,
397 };
398
399 static struct file_operations txpower_g_fops = {
400 .read = txpower_g_read_file,
401 .write = txpower_g_write_file,
402 .open = open_file_generic,
403 };
404
405 static struct file_operations restart_fops = {
406 .write = restart_write_file,
407 .open = open_file_generic,
408 };
409
410
411 int bcm43xx_debug(struct bcm43xx_wldev *dev, enum bcm43xx_dyndbg feature)
412 {
413 return !!(dev->dfsentry->dyn_debug[feature]);
414 }
415
416 static void bcm43xx_remove_dynamic_debug(struct bcm43xx_wldev *dev)
417 {
418 struct bcm43xx_dfsentry *e = dev->dfsentry;
419 int i;
420
421 for (i = 0; i < __BCM43xx_NR_DYNDBG; i++)
422 debugfs_remove(e->dyn_debug_dentries[i]);
423 }
424
425 static void bcm43xx_add_dynamic_debug(struct bcm43xx_wldev *dev)
426 {
427 struct bcm43xx_dfsentry *e = dev->dfsentry;
428 struct dentry *d;
429
430 #define add_dyn_dbg(name, id, initstate) do { \
431 e->dyn_debug[id] = (initstate); \
432 d = debugfs_create_bool(name, 0600, e->subdir, \
433 &(e->dyn_debug[id])); \
434 if (!IS_ERR(d)) \
435 e->dyn_debug_dentries[id] = d; \
436 } while (0)
437
438 add_dyn_dbg("debug_xmitpower", BCM43xx_DBG_XMITPOWER, 0);
439 add_dyn_dbg("debug_dmaoverflow", BCM43xx_DBG_DMAOVERFLOW, 0);
440 add_dyn_dbg("debug_pwork_fast", BCM43xx_DBG_PWORK_FAST, 0);
441 add_dyn_dbg("debug_pwork_stop", BCM43xx_DBG_PWORK_STOP, 0);
442
443 #undef add_dyn_dbg
444 }
445
446 void bcm43xx_debugfs_add_device(struct bcm43xx_wldev *dev)
447 {
448 struct bcm43xx_dfsentry *e;
449 struct bcm43xx_txstatus_log *log;
450 char devdir[16];
451
452 assert(dev);
453 e = kzalloc(sizeof(*e), GFP_KERNEL);
454 if (!e) {
455 printk(KERN_ERR PFX "debugfs: add device OOM\n");
456 return;
457 }
458 e->dev = dev;
459 log = &e->txstatlog;
460 log->log = kcalloc(BCM43xx_NR_LOGGED_TXSTATUS,
461 sizeof(struct bcm43xx_txstatus),
462 GFP_KERNEL);
463 if (!log->log) {
464 printk(KERN_ERR PFX "debugfs: add device txstatus OOM\n");
465 kfree(e);
466 return;
467 }
468 log->end = -1;
469 spin_lock_init(&log->lock);
470
471 dev->dfsentry = e;
472
473 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
474 e->subdir = debugfs_create_dir(devdir, fs.root);
475 if (!e->subdir || IS_ERR(e->subdir)) {
476 e->subdir = NULL;
477 kfree(log->log);
478 kfree(e);
479 return;
480 }
481
482 e->dentry_tsf = debugfs_create_file("tsf", 0600, e->subdir,
483 dev, &tsf_fops);
484 if (IS_ERR(e->dentry_tsf))
485 e->dentry_tsf = NULL;
486 e->dentry_txstat = debugfs_create_file("tx_status", 0400, e->subdir,
487 dev, &txstat_fops);
488 if (IS_ERR(e->dentry_txstat))
489 e->dentry_txstat = NULL;
490 e->dentry_txpower_g = debugfs_create_file("tx_power_g", 0600, e->subdir,
491 dev, &txpower_g_fops);
492 if (IS_ERR(e->dentry_txpower_g))
493 e->dentry_txpower_g = NULL;
494 e->dentry_restart = debugfs_create_file("restart", 0200, e->subdir,
495 dev, &restart_fops);
496 if (IS_ERR(e->dentry_restart))
497 e->dentry_restart = NULL;
498
499 bcm43xx_add_dynamic_debug(dev);
500 }
501
502 void bcm43xx_debugfs_remove_device(struct bcm43xx_wldev *dev)
503 {
504 struct bcm43xx_dfsentry *e;
505
506 if (!dev)
507 return;
508 e = dev->dfsentry;
509 if (!e)
510 return;
511 bcm43xx_remove_dynamic_debug(dev);
512 debugfs_remove(e->dentry_tsf);
513 debugfs_remove(e->dentry_txstat);
514 debugfs_remove(e->dentry_restart);
515 debugfs_remove(e->dentry_txpower_g);
516 debugfs_remove(e->subdir);
517 kfree(e->txstatlog.log);
518 kfree(e);
519 }
520
521 void bcm43xx_debugfs_log_txstat(struct bcm43xx_wldev *dev,
522 const struct bcm43xx_txstatus *status)
523 {
524 struct bcm43xx_dfsentry *e = dev->dfsentry;
525 struct bcm43xx_txstatus_log *log;
526 struct bcm43xx_txstatus *cur;
527 int i;
528
529 log = &e->txstatlog;
530 assert(irqs_disabled());
531 spin_lock(&log->lock);
532 i = log->end + 1;
533 if (i == BCM43xx_NR_LOGGED_TXSTATUS)
534 i = 0;
535 log->end = i;
536 cur = &(log->log[i]);
537 memcpy(cur, status, sizeof(*cur));
538 spin_unlock(&log->lock);
539 }
540
541 void bcm43xx_debugfs_init(void)
542 {
543 memset(&fs, 0, sizeof(fs));
544 fs.root = debugfs_create_dir(KBUILD_MODNAME, NULL);
545 if (!fs.root || IS_ERR(fs.root)) {
546 fs.root = NULL;
547 return;
548 }
549 fs.dentry_driverinfo = debugfs_create_file("driver", 0444, fs.root,
550 NULL, &drvinfo_fops);
551 if (IS_ERR(fs.dentry_driverinfo))
552 fs.dentry_driverinfo = NULL;
553 }
554
555 void bcm43xx_debugfs_exit(void)
556 {
557 debugfs_remove(fs.dentry_driverinfo);
558 debugfs_remove(fs.root);
559 }
560
561 void bcm43xx_printk_dump(const char *data,
562 size_t size,
563 const char *description)
564 {
565 unsigned int i;
566 char c;
567
568 printk(KERN_INFO PFX "Data dump (%s, %lu bytes):",
569 description, (unsigned long)size);
570 for (i = 0; i < size; i++) {
571 c = data[i];
572 if (i % 8 == 0)
573 printk("\n" KERN_INFO PFX "0x%08x: 0x%02x, ", i, c & 0xff);
574 else
575 printk("0x%02x, ", c & 0xff);
576 }
577 printk("\n");
578 }
579
580 void bcm43xx_printk_bitdump(const unsigned char *data,
581 size_t bytes, int msb_to_lsb,
582 const char *description)
583 {
584 unsigned int i;
585 int j;
586 const unsigned char *d;
587
588 printk(KERN_INFO PFX "*** Bitdump (%s, %lu bytes, %s) ***",
589 description, (unsigned long)bytes,
590 msb_to_lsb ? "MSB to LSB" : "LSB to MSB");
591 for (i = 0; i < bytes; i++) {
592 d = data + i;
593 if (i % 8 == 0)
594 printk("\n" KERN_INFO PFX "0x%08x: ", i);
595 if (msb_to_lsb) {
596 for (j = 7; j >= 0; j--) {
597 if (*d & (1 << j))
598 printk("1");
599 else
600 printk("0");
601 }
602 } else {
603 for (j = 0; j < 8; j++) {
604 if (*d & (1 << j))
605 printk("1");
606 else
607 printk("0");
608 }
609 }
610 printk(" ");
611 }
612 printk("\n");
613 }