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