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