e623cfd05e16f1a4a218ce456c1950666dfc2530
[openwrt/staging/yousong.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_dmaverbose", BCM43xx_DBG_DMAVERBOSE, 0);
441 add_dyn_dbg("debug_pwork_fast", BCM43xx_DBG_PWORK_FAST, 0);
442 add_dyn_dbg("debug_pwork_stop", BCM43xx_DBG_PWORK_STOP, 0);
443
444 #undef add_dyn_dbg
445 }
446
447 void bcm43xx_debugfs_add_device(struct bcm43xx_wldev *dev)
448 {
449 struct bcm43xx_dfsentry *e;
450 struct bcm43xx_txstatus_log *log;
451 char devdir[16];
452
453 assert(dev);
454 e = kzalloc(sizeof(*e), GFP_KERNEL);
455 if (!e) {
456 printk(KERN_ERR PFX "debugfs: add device OOM\n");
457 return;
458 }
459 e->dev = dev;
460 log = &e->txstatlog;
461 log->log = kcalloc(BCM43xx_NR_LOGGED_TXSTATUS,
462 sizeof(struct bcm43xx_txstatus),
463 GFP_KERNEL);
464 if (!log->log) {
465 printk(KERN_ERR PFX "debugfs: add device txstatus OOM\n");
466 kfree(e);
467 return;
468 }
469 log->end = -1;
470 spin_lock_init(&log->lock);
471
472 dev->dfsentry = e;
473
474 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
475 e->subdir = debugfs_create_dir(devdir, fs.root);
476 if (!e->subdir || IS_ERR(e->subdir)) {
477 e->subdir = NULL;
478 kfree(log->log);
479 kfree(e);
480 return;
481 }
482
483 e->dentry_tsf = debugfs_create_file("tsf", 0600, e->subdir,
484 dev, &tsf_fops);
485 if (IS_ERR(e->dentry_tsf))
486 e->dentry_tsf = NULL;
487 e->dentry_txstat = debugfs_create_file("tx_status", 0400, e->subdir,
488 dev, &txstat_fops);
489 if (IS_ERR(e->dentry_txstat))
490 e->dentry_txstat = NULL;
491 e->dentry_txpower_g = debugfs_create_file("tx_power_g", 0600, e->subdir,
492 dev, &txpower_g_fops);
493 if (IS_ERR(e->dentry_txpower_g))
494 e->dentry_txpower_g = NULL;
495 e->dentry_restart = debugfs_create_file("restart", 0200, e->subdir,
496 dev, &restart_fops);
497 if (IS_ERR(e->dentry_restart))
498 e->dentry_restart = NULL;
499
500 bcm43xx_add_dynamic_debug(dev);
501 }
502
503 void bcm43xx_debugfs_remove_device(struct bcm43xx_wldev *dev)
504 {
505 struct bcm43xx_dfsentry *e;
506
507 if (!dev)
508 return;
509 e = dev->dfsentry;
510 if (!e)
511 return;
512 bcm43xx_remove_dynamic_debug(dev);
513 debugfs_remove(e->dentry_tsf);
514 debugfs_remove(e->dentry_txstat);
515 debugfs_remove(e->dentry_restart);
516 debugfs_remove(e->dentry_txpower_g);
517 debugfs_remove(e->subdir);
518 kfree(e->txstatlog.log);
519 kfree(e);
520 }
521
522 void bcm43xx_debugfs_log_txstat(struct bcm43xx_wldev *dev,
523 const struct bcm43xx_txstatus *status)
524 {
525 struct bcm43xx_dfsentry *e = dev->dfsentry;
526 struct bcm43xx_txstatus_log *log;
527 struct bcm43xx_txstatus *cur;
528 int i;
529
530 log = &e->txstatlog;
531 assert(irqs_disabled());
532 spin_lock(&log->lock);
533 i = log->end + 1;
534 if (i == BCM43xx_NR_LOGGED_TXSTATUS)
535 i = 0;
536 log->end = i;
537 cur = &(log->log[i]);
538 memcpy(cur, status, sizeof(*cur));
539 spin_unlock(&log->lock);
540 }
541
542 void bcm43xx_debugfs_init(void)
543 {
544 memset(&fs, 0, sizeof(fs));
545 fs.root = debugfs_create_dir(KBUILD_MODNAME, NULL);
546 if (!fs.root || IS_ERR(fs.root)) {
547 fs.root = NULL;
548 return;
549 }
550 fs.dentry_driverinfo = debugfs_create_file("driver", 0444, fs.root,
551 NULL, &drvinfo_fops);
552 if (IS_ERR(fs.dentry_driverinfo))
553 fs.dentry_driverinfo = NULL;
554 }
555
556 void bcm43xx_debugfs_exit(void)
557 {
558 debugfs_remove(fs.dentry_driverinfo);
559 debugfs_remove(fs.root);
560 }
561
562 void bcm43xx_printk_dump(const char *data,
563 size_t size,
564 const char *description)
565 {
566 unsigned int i;
567 char c;
568
569 printk(KERN_INFO PFX "Data dump (%s, %lu bytes):",
570 description, (unsigned long)size);
571 for (i = 0; i < size; i++) {
572 c = data[i];
573 if (i % 8 == 0)
574 printk("\n" KERN_INFO PFX "0x%08x: 0x%02x, ", i, c & 0xff);
575 else
576 printk("0x%02x, ", c & 0xff);
577 }
578 printk("\n");
579 }
580
581 void bcm43xx_printk_bitdump(const unsigned char *data,
582 size_t bytes, int msb_to_lsb,
583 const char *description)
584 {
585 unsigned int i;
586 int j;
587 const unsigned char *d;
588
589 printk(KERN_INFO PFX "*** Bitdump (%s, %lu bytes, %s) ***",
590 description, (unsigned long)bytes,
591 msb_to_lsb ? "MSB to LSB" : "LSB to MSB");
592 for (i = 0; i < bytes; i++) {
593 d = data + i;
594 if (i % 8 == 0)
595 printk("\n" KERN_INFO PFX "0x%08x: ", i);
596 if (msb_to_lsb) {
597 for (j = 7; j >= 0; j--) {
598 if (*d & (1 << j))
599 printk("1");
600 else
601 printk("0");
602 }
603 } else {
604 for (j = 0; j < 8; j++) {
605 if (*d & (1 << j))
606 printk("1");
607 else
608 printk("0");
609 }
610 }
611 printk(" ");
612 }
613 printk("\n");
614 }