Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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 "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 /* The biggest address values for SHM access from the debugfs files. */
78 #define B43_MAX_SHM_ROUTING     4
79 #define B43_MAX_SHM_ADDR        0xFFFF
80
81 static ssize_t shm16read__read_file(struct b43_wldev *dev,
82                                     char *buf, size_t bufsize)
83 {
84         ssize_t count = 0;
85         unsigned int routing, addr;
86         u16 val;
87
88         routing = dev->dfsentry->shm16read_routing_next;
89         addr = dev->dfsentry->shm16read_addr_next;
90         if ((routing > B43_MAX_SHM_ROUTING) ||
91             (addr > B43_MAX_SHM_ADDR))
92                 return -EDESTADDRREQ;
93
94         val = b43_shm_read16(dev, routing, addr);
95         fappend("0x%04X\n", val);
96
97         return count;
98 }
99
100 static int shm16read__write_file(struct b43_wldev *dev,
101                                  const char *buf, size_t count)
102 {
103         unsigned int routing, addr;
104         int res;
105
106         res = sscanf(buf, "0x%X 0x%X", &routing, &addr);
107         if (res != 2)
108                 return -EINVAL;
109         if (routing > B43_MAX_SHM_ROUTING)
110                 return -EADDRNOTAVAIL;
111         if (addr > B43_MAX_SHM_ADDR)
112                 return -EADDRNOTAVAIL;
113         if (routing == B43_SHM_SHARED) {
114                 if ((addr % 2) != 0)
115                         return -EADDRNOTAVAIL;
116         }
117
118         dev->dfsentry->shm16read_routing_next = routing;
119         dev->dfsentry->shm16read_addr_next = addr;
120
121         return 0;
122 }
123
124 static int shm16write__write_file(struct b43_wldev *dev,
125                                   const char *buf, size_t count)
126 {
127         unsigned int routing, addr, mask, set;
128         u16 val;
129         int res;
130         unsigned long flags;
131
132         res = sscanf(buf, "0x%X 0x%X 0x%X 0x%X",
133                      &routing, &addr, &mask, &set);
134         if (res != 4)
135                 return -EINVAL;
136         if (routing > B43_MAX_SHM_ROUTING)
137                 return -EADDRNOTAVAIL;
138         if (addr > B43_MAX_SHM_ADDR)
139                 return -EADDRNOTAVAIL;
140         if (routing == B43_SHM_SHARED) {
141                 if ((addr % 2) != 0)
142                         return -EADDRNOTAVAIL;
143         }
144         if ((mask > 0xFFFF) || (set > 0xFFFF))
145                 return -E2BIG;
146
147         spin_lock_irqsave(&dev->wl->shm_lock, flags);
148         if (mask == 0)
149                 val = 0;
150         else
151                 val = __b43_shm_read16(dev, routing, addr);
152         val &= mask;
153         val |= set;
154         __b43_shm_write16(dev, routing, addr, val);
155         spin_unlock_irqrestore(&dev->wl->shm_lock, flags);
156
157         return 0;
158 }
159
160 static ssize_t shm32read__read_file(struct b43_wldev *dev,
161                                     char *buf, size_t bufsize)
162 {
163         ssize_t count = 0;
164         unsigned int routing, addr;
165         u32 val;
166
167         routing = dev->dfsentry->shm32read_routing_next;
168         addr = dev->dfsentry->shm32read_addr_next;
169         if ((routing > B43_MAX_SHM_ROUTING) ||
170             (addr > B43_MAX_SHM_ADDR))
171                 return -EDESTADDRREQ;
172
173         val = b43_shm_read32(dev, routing, addr);
174         fappend("0x%08X\n", val);
175
176         return count;
177 }
178
179 static int shm32read__write_file(struct b43_wldev *dev,
180                                  const char *buf, size_t count)
181 {
182         unsigned int routing, addr;
183         int res;
184
185         res = sscanf(buf, "0x%X 0x%X", &routing, &addr);
186         if (res != 2)
187                 return -EINVAL;
188         if (routing > B43_MAX_SHM_ROUTING)
189                 return -EADDRNOTAVAIL;
190         if (addr > B43_MAX_SHM_ADDR)
191                 return -EADDRNOTAVAIL;
192         if (routing == B43_SHM_SHARED) {
193                 if ((addr % 2) != 0)
194                         return -EADDRNOTAVAIL;
195         }
196
197         dev->dfsentry->shm32read_routing_next = routing;
198         dev->dfsentry->shm32read_addr_next = addr;
199
200         return 0;
201 }
202
203 static int shm32write__write_file(struct b43_wldev *dev,
204                                   const char *buf, size_t count)
205 {
206         unsigned int routing, addr, mask, set;
207         u32 val;
208         int res;
209         unsigned long flags;
210
211         res = sscanf(buf, "0x%X 0x%X 0x%X 0x%X",
212                      &routing, &addr, &mask, &set);
213         if (res != 4)
214                 return -EINVAL;
215         if (routing > B43_MAX_SHM_ROUTING)
216                 return -EADDRNOTAVAIL;
217         if (addr > B43_MAX_SHM_ADDR)
218                 return -EADDRNOTAVAIL;
219         if (routing == B43_SHM_SHARED) {
220                 if ((addr % 2) != 0)
221                         return -EADDRNOTAVAIL;
222         }
223         if ((mask > 0xFFFFFFFF) || (set > 0xFFFFFFFF))
224                 return -E2BIG;
225
226         spin_lock_irqsave(&dev->wl->shm_lock, flags);
227         if (mask == 0)
228                 val = 0;
229         else
230                 val = __b43_shm_read32(dev, routing, addr);
231         val &= mask;
232         val |= set;
233         __b43_shm_write32(dev, routing, addr, val);
234         spin_unlock_irqrestore(&dev->wl->shm_lock, flags);
235
236         return 0;
237 }
238
239 /* The biggest MMIO address that we allow access to from the debugfs files. */
240 #define B43_MAX_MMIO_ACCESS     (0xF00 - 1)
241
242 static ssize_t mmio16read__read_file(struct b43_wldev *dev,
243                                      char *buf, size_t bufsize)
244 {
245         ssize_t count = 0;
246         unsigned int addr;
247         u16 val;
248
249         addr = dev->dfsentry->mmio16read_next;
250         if (addr > B43_MAX_MMIO_ACCESS)
251                 return -EDESTADDRREQ;
252
253         val = b43_read16(dev, addr);
254         fappend("0x%04X\n", val);
255
256         return count;
257 }
258
259 static int mmio16read__write_file(struct b43_wldev *dev,
260                                   const char *buf, size_t count)
261 {
262         unsigned int addr;
263         int res;
264
265         res = sscanf(buf, "0x%X", &addr);
266         if (res != 1)
267                 return -EINVAL;
268         if (addr > B43_MAX_MMIO_ACCESS)
269                 return -EADDRNOTAVAIL;
270         if ((addr % 2) != 0)
271                 return -EINVAL;
272
273         dev->dfsentry->mmio16read_next = addr;
274
275         return 0;
276 }
277
278 static int mmio16write__write_file(struct b43_wldev *dev,
279                                    const char *buf, size_t count)
280 {
281         unsigned int addr, mask, set;
282         int res;
283         u16 val;
284
285         res = sscanf(buf, "0x%X 0x%X 0x%X", &addr, &mask, &set);
286         if (res != 3)
287                 return -EINVAL;
288         if (addr > B43_MAX_MMIO_ACCESS)
289                 return -EADDRNOTAVAIL;
290         if ((mask > 0xFFFF) || (set > 0xFFFF))
291                 return -E2BIG;
292         if ((addr % 2) != 0)
293                 return -EINVAL;
294
295         if (mask == 0)
296                 val = 0;
297         else
298                 val = b43_read16(dev, addr);
299         val &= mask;
300         val |= set;
301         b43_write16(dev, addr, val);
302
303         return 0;
304 }
305
306 static ssize_t mmio32read__read_file(struct b43_wldev *dev,
307                                      char *buf, size_t bufsize)
308 {
309         ssize_t count = 0;
310         unsigned int addr;
311         u32 val;
312
313         addr = dev->dfsentry->mmio32read_next;
314         if (addr > B43_MAX_MMIO_ACCESS)
315                 return -EDESTADDRREQ;
316
317         val = b43_read32(dev, addr);
318         fappend("0x%08X\n", val);
319
320         return count;
321 }
322
323 static int mmio32read__write_file(struct b43_wldev *dev,
324                                   const char *buf, size_t count)
325 {
326         unsigned int addr;
327         int res;
328
329         res = sscanf(buf, "0x%X", &addr);
330         if (res != 1)
331                 return -EINVAL;
332         if (addr > B43_MAX_MMIO_ACCESS)
333                 return -EADDRNOTAVAIL;
334         if ((addr % 4) != 0)
335                 return -EINVAL;
336
337         dev->dfsentry->mmio32read_next = addr;
338
339         return 0;
340 }
341
342 static int mmio32write__write_file(struct b43_wldev *dev,
343                                    const char *buf, size_t count)
344 {
345         unsigned int addr, mask, set;
346         int res;
347         u32 val;
348
349         res = sscanf(buf, "0x%X 0x%X 0x%X", &addr, &mask, &set);
350         if (res != 3)
351                 return -EINVAL;
352         if (addr > B43_MAX_MMIO_ACCESS)
353                 return -EADDRNOTAVAIL;
354         if ((mask > 0xFFFFFFFF) || (set > 0xFFFFFFFF))
355                 return -E2BIG;
356         if ((addr % 4) != 0)
357                 return -EINVAL;
358
359         if (mask == 0)
360                 val = 0;
361         else
362                 val = b43_read32(dev, addr);
363         val &= mask;
364         val |= set;
365         b43_write32(dev, addr, val);
366
367         return 0;
368 }
369
370 static ssize_t txstat_read_file(struct b43_wldev *dev,
371                                 char *buf, size_t bufsize)
372 {
373         struct b43_txstatus_log *log = &dev->dfsentry->txstatlog;
374         ssize_t count = 0;
375         unsigned long flags;
376         int i, idx;
377         struct b43_txstatus *stat;
378
379         spin_lock_irqsave(&log->lock, flags);
380         if (log->end < 0) {
381                 fappend("Nothing transmitted, yet\n");
382                 goto out_unlock;
383         }
384         fappend("b43 TX status reports:\n\n"
385                 "index | cookie | seq | phy_stat | frame_count | "
386                 "rts_count | supp_reason | pm_indicated | "
387                 "intermediate | for_ampdu | acked\n" "---\n");
388         i = log->end + 1;
389         idx = 0;
390         while (1) {
391                 if (i == B43_NR_LOGGED_TXSTATUS)
392                         i = 0;
393                 stat = &(log->log[i]);
394                 if (stat->cookie) {
395                         fappend("%03d | "
396                                 "0x%04X | 0x%04X | 0x%02X | "
397                                 "0x%X | 0x%X | "
398                                 "%u | %u | "
399                                 "%u | %u | %u\n",
400                                 idx,
401                                 stat->cookie, stat->seq, stat->phy_stat,
402                                 stat->frame_count, stat->rts_count,
403                                 stat->supp_reason, stat->pm_indicated,
404                                 stat->intermediate, stat->for_ampdu,
405                                 stat->acked);
406                         idx++;
407                 }
408                 if (i == log->end)
409                         break;
410                 i++;
411         }
412 out_unlock:
413         spin_unlock_irqrestore(&log->lock, flags);
414
415         return count;
416 }
417
418 /* wl->irq_lock is locked */
419 static int restart_write_file(struct b43_wldev *dev,
420                               const char *buf, size_t count)
421 {
422         int err = 0;
423
424         if (count > 0 && buf[0] == '1') {
425                 b43_controller_restart(dev, "manually restarted");
426         } else
427                 err = -EINVAL;
428
429         return err;
430 }
431
432 static unsigned long calc_expire_secs(unsigned long now,
433                                       unsigned long time,
434                                       unsigned long expire)
435 {
436         expire = time + expire;
437
438         if (time_after(now, expire))
439                 return 0; /* expired */
440         if (expire < now) {
441                 /* jiffies wrapped */
442                 expire -= MAX_JIFFY_OFFSET;
443                 now -= MAX_JIFFY_OFFSET;
444         }
445         B43_WARN_ON(expire < now);
446
447         return (expire - now) / HZ;
448 }
449
450 static ssize_t loctls_read_file(struct b43_wldev *dev,
451                                 char *buf, size_t bufsize)
452 {
453         ssize_t count = 0;
454         struct b43_txpower_lo_control *lo;
455         int i, err = 0;
456         struct b43_lo_calib *cal;
457         unsigned long now = jiffies;
458         struct b43_phy *phy = &dev->phy;
459
460         if (phy->type != B43_PHYTYPE_G) {
461                 fappend("Device is not a G-PHY\n");
462                 err = -ENODEV;
463                 goto out;
464         }
465         lo = phy->g->lo_control;
466         fappend("-- Local Oscillator calibration data --\n\n");
467         fappend("HW-power-control enabled: %d\n",
468                 dev->phy.hardware_power_control);
469         fappend("TX Bias: 0x%02X,  TX Magn: 0x%02X  (expire in %lu sec)\n",
470                 lo->tx_bias, lo->tx_magn,
471                 calc_expire_secs(now, lo->txctl_measured_time,
472                                  B43_LO_TXCTL_EXPIRE));
473         fappend("Power Vector: 0x%08X%08X  (expires in %lu sec)\n",
474                 (unsigned int)((lo->power_vector & 0xFFFFFFFF00000000ULL) >> 32),
475                 (unsigned int)(lo->power_vector & 0x00000000FFFFFFFFULL),
476                 calc_expire_secs(now, lo->pwr_vec_read_time,
477                                  B43_LO_PWRVEC_EXPIRE));
478
479         fappend("\nCalibrated settings:\n");
480         list_for_each_entry(cal, &lo->calib_list, list) {
481                 bool active;
482
483                 active = (b43_compare_bbatt(&cal->bbatt, &phy->g->bbatt) &&
484                           b43_compare_rfatt(&cal->rfatt, &phy->g->rfatt));
485                 fappend("BB(%d), RF(%d,%d)  ->  I=%d, Q=%d  "
486                         "(expires in %lu sec)%s\n",
487                         cal->bbatt.att,
488                         cal->rfatt.att, cal->rfatt.with_padmix,
489                         cal->ctl.i, cal->ctl.q,
490                         calc_expire_secs(now, cal->calib_time,
491                                          B43_LO_CALIB_EXPIRE),
492                         active ? "  ACTIVE" : "");
493         }
494
495         fappend("\nUsed RF attenuation values:  Value(WithPadmix flag)\n");
496         for (i = 0; i < lo->rfatt_list.len; i++) {
497                 fappend("%u(%d), ",
498                         lo->rfatt_list.list[i].att,
499                         lo->rfatt_list.list[i].with_padmix);
500         }
501         fappend("\n");
502         fappend("\nUsed Baseband attenuation values:\n");
503         for (i = 0; i < lo->bbatt_list.len; i++) {
504                 fappend("%u, ",
505                         lo->bbatt_list.list[i].att);
506         }
507         fappend("\n");
508
509 out:
510         return err ? err : count;
511 }
512
513 #undef fappend
514
515 static int b43_debugfs_open(struct inode *inode, struct file *file)
516 {
517         file->private_data = inode->i_private;
518         return 0;
519 }
520
521 static ssize_t b43_debugfs_read(struct file *file, char __user *userbuf,
522                                 size_t count, loff_t *ppos)
523 {
524         struct b43_wldev *dev;
525         struct b43_debugfs_fops *dfops;
526         struct b43_dfs_file *dfile;
527         ssize_t uninitialized_var(ret);
528         char *buf;
529         const size_t bufsize = 1024 * 16; /* 16 kiB buffer */
530         const size_t buforder = get_order(bufsize);
531         int err = 0;
532
533         if (!count)
534                 return 0;
535         dev = file->private_data;
536         if (!dev)
537                 return -ENODEV;
538
539         mutex_lock(&dev->wl->mutex);
540         if (b43_status(dev) < B43_STAT_INITIALIZED) {
541                 err = -ENODEV;
542                 goto out_unlock;
543         }
544
545         dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
546         if (!dfops->read) {
547                 err = -ENOSYS;
548                 goto out_unlock;
549         }
550         dfile = fops_to_dfs_file(dev, dfops);
551
552         if (!dfile->buffer) {
553                 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
554                 if (!buf) {
555                         err = -ENOMEM;
556                         goto out_unlock;
557                 }
558                 memset(buf, 0, bufsize);
559                 if (dfops->take_irqlock) {
560                         spin_lock_irq(&dev->wl->irq_lock);
561                         ret = dfops->read(dev, buf, bufsize);
562                         spin_unlock_irq(&dev->wl->irq_lock);
563                 } else
564                         ret = dfops->read(dev, buf, bufsize);
565                 if (ret <= 0) {
566                         free_pages((unsigned long)buf, buforder);
567                         err = ret;
568                         goto out_unlock;
569                 }
570                 dfile->data_len = ret;
571                 dfile->buffer = buf;
572         }
573
574         ret = simple_read_from_buffer(userbuf, count, ppos,
575                                       dfile->buffer,
576                                       dfile->data_len);
577         if (*ppos >= dfile->data_len) {
578                 free_pages((unsigned long)dfile->buffer, buforder);
579                 dfile->buffer = NULL;
580                 dfile->data_len = 0;
581         }
582 out_unlock:
583         mutex_unlock(&dev->wl->mutex);
584
585         return err ? err : ret;
586 }
587
588 static ssize_t b43_debugfs_write(struct file *file,
589                                  const char __user *userbuf,
590                                  size_t count, loff_t *ppos)
591 {
592         struct b43_wldev *dev;
593         struct b43_debugfs_fops *dfops;
594         char *buf;
595         int err = 0;
596
597         if (!count)
598                 return 0;
599         if (count > PAGE_SIZE)
600                 return -E2BIG;
601         dev = file->private_data;
602         if (!dev)
603                 return -ENODEV;
604
605         mutex_lock(&dev->wl->mutex);
606         if (b43_status(dev) < B43_STAT_INITIALIZED) {
607                 err = -ENODEV;
608                 goto out_unlock;
609         }
610
611         dfops = container_of(file->f_op, struct b43_debugfs_fops, fops);
612         if (!dfops->write) {
613                 err = -ENOSYS;
614                 goto out_unlock;
615         }
616
617         buf = (char *)get_zeroed_page(GFP_KERNEL);
618         if (!buf) {
619                 err = -ENOMEM;
620                 goto out_unlock;
621         }
622         if (copy_from_user(buf, userbuf, count)) {
623                 err = -EFAULT;
624                 goto out_freepage;
625         }
626         if (dfops->take_irqlock) {
627                 spin_lock_irq(&dev->wl->irq_lock);
628                 err = dfops->write(dev, buf, count);
629                 spin_unlock_irq(&dev->wl->irq_lock);
630         } else
631                 err = dfops->write(dev, buf, count);
632         if (err)
633                 goto out_freepage;
634
635 out_freepage:
636         free_page((unsigned long)buf);
637 out_unlock:
638         mutex_unlock(&dev->wl->mutex);
639
640         return err ? err : count;
641 }
642
643
644 #define B43_DEBUGFS_FOPS(name, _read, _write, _take_irqlock)    \
645         static struct b43_debugfs_fops fops_##name = {          \
646                 .read   = _read,                                \
647                 .write  = _write,                               \
648                 .fops   = {                                     \
649                         .open   = b43_debugfs_open,             \
650                         .read   = b43_debugfs_read,             \
651                         .write  = b43_debugfs_write,            \
652                 },                                              \
653                 .file_struct_offset = offsetof(struct b43_dfsentry, \
654                                                file_##name),    \
655                 .take_irqlock   = _take_irqlock,                \
656         }
657
658 B43_DEBUGFS_FOPS(shm16read, shm16read__read_file, shm16read__write_file, 1);
659 B43_DEBUGFS_FOPS(shm16write, NULL, shm16write__write_file, 1);
660 B43_DEBUGFS_FOPS(shm32read, shm32read__read_file, shm32read__write_file, 1);
661 B43_DEBUGFS_FOPS(shm32write, NULL, shm32write__write_file, 1);
662 B43_DEBUGFS_FOPS(mmio16read, mmio16read__read_file, mmio16read__write_file, 1);
663 B43_DEBUGFS_FOPS(mmio16write, NULL, mmio16write__write_file, 1);
664 B43_DEBUGFS_FOPS(mmio32read, mmio32read__read_file, mmio32read__write_file, 1);
665 B43_DEBUGFS_FOPS(mmio32write, NULL, mmio32write__write_file, 1);
666 B43_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
667 B43_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
668 B43_DEBUGFS_FOPS(loctls, loctls_read_file, NULL, 0);
669
670
671 bool b43_debug(struct b43_wldev *dev, enum b43_dyndbg feature)
672 {
673         bool enabled;
674
675         enabled = (dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
676         if (unlikely(enabled)) {
677                 /* Force full debugging messages, if the user enabled
678                  * some dynamic debugging feature. */
679                 b43_modparam_verbose = B43_VERBOSITY_MAX;
680         }
681
682         return enabled;
683 }
684
685 static void b43_remove_dynamic_debug(struct b43_wldev *dev)
686 {
687         struct b43_dfsentry *e = dev->dfsentry;
688         int i;
689
690         for (i = 0; i < __B43_NR_DYNDBG; i++)
691                 debugfs_remove(e->dyn_debug_dentries[i]);
692 }
693
694 static void b43_add_dynamic_debug(struct b43_wldev *dev)
695 {
696         struct b43_dfsentry *e = dev->dfsentry;
697         struct dentry *d;
698
699 #define add_dyn_dbg(name, id, initstate) do {           \
700         e->dyn_debug[id] = (initstate);                 \
701         d = debugfs_create_bool(name, 0600, e->subdir,  \
702                                 &(e->dyn_debug[id]));   \
703         if (!IS_ERR(d))                                 \
704                 e->dyn_debug_dentries[id] = d;          \
705                                 } while (0)
706
707         add_dyn_dbg("debug_xmitpower", B43_DBG_XMITPOWER, 0);
708         add_dyn_dbg("debug_dmaoverflow", B43_DBG_DMAOVERFLOW, 0);
709         add_dyn_dbg("debug_dmaverbose", B43_DBG_DMAVERBOSE, 0);
710         add_dyn_dbg("debug_pwork_fast", B43_DBG_PWORK_FAST, 0);
711         add_dyn_dbg("debug_pwork_stop", B43_DBG_PWORK_STOP, 0);
712         add_dyn_dbg("debug_lo", B43_DBG_LO, 0);
713         add_dyn_dbg("debug_firmware", B43_DBG_FIRMWARE, 0);
714         add_dyn_dbg("debug_keys", B43_DBG_KEYS, 0);
715
716 #undef add_dyn_dbg
717 }
718
719 void b43_debugfs_add_device(struct b43_wldev *dev)
720 {
721         struct b43_dfsentry *e;
722         struct b43_txstatus_log *log;
723         char devdir[16];
724
725         B43_WARN_ON(!dev);
726         e = kzalloc(sizeof(*e), GFP_KERNEL);
727         if (!e) {
728                 b43err(dev->wl, "debugfs: add device OOM\n");
729                 return;
730         }
731         e->dev = dev;
732         log = &e->txstatlog;
733         log->log = kcalloc(B43_NR_LOGGED_TXSTATUS,
734                            sizeof(struct b43_txstatus), GFP_KERNEL);
735         if (!log->log) {
736                 b43err(dev->wl, "debugfs: add device txstatus OOM\n");
737                 kfree(e);
738                 return;
739         }
740         log->end = -1;
741         spin_lock_init(&log->lock);
742
743         dev->dfsentry = e;
744
745         snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
746         e->subdir = debugfs_create_dir(devdir, rootdir);
747         if (!e->subdir || IS_ERR(e->subdir)) {
748                 if (e->subdir == ERR_PTR(-ENODEV)) {
749                         b43dbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
750                                "enabled in kernel config\n");
751                 } else {
752                         b43err(dev->wl, "debugfs: cannot create %s directory\n",
753                                devdir);
754                 }
755                 dev->dfsentry = NULL;
756                 kfree(log->log);
757                 kfree(e);
758                 return;
759         }
760
761         e->mmio16read_next = 0xFFFF; /* invalid address */
762         e->mmio32read_next = 0xFFFF; /* invalid address */
763         e->shm16read_routing_next = 0xFFFFFFFF; /* invalid routing */
764         e->shm16read_addr_next = 0xFFFFFFFF; /* invalid address */
765         e->shm32read_routing_next = 0xFFFFFFFF; /* invalid routing */
766         e->shm32read_addr_next = 0xFFFFFFFF; /* invalid address */
767
768 #define ADD_FILE(name, mode)    \
769         do {                                                    \
770                 struct dentry *d;                               \
771                 d = debugfs_create_file(__stringify(name),      \
772                                         mode, e->subdir, dev,   \
773                                         &fops_##name.fops);     \
774                 e->file_##name.dentry = NULL;                   \
775                 if (!IS_ERR(d))                                 \
776                         e->file_##name.dentry = d;              \
777         } while (0)
778
779
780         ADD_FILE(shm16read, 0600);
781         ADD_FILE(shm16write, 0200);
782         ADD_FILE(shm32read, 0600);
783         ADD_FILE(shm32write, 0200);
784         ADD_FILE(mmio16read, 0600);
785         ADD_FILE(mmio16write, 0200);
786         ADD_FILE(mmio32read, 0600);
787         ADD_FILE(mmio32write, 0200);
788         ADD_FILE(txstat, 0400);
789         ADD_FILE(restart, 0200);
790         ADD_FILE(loctls, 0400);
791
792 #undef ADD_FILE
793
794         b43_add_dynamic_debug(dev);
795 }
796
797 void b43_debugfs_remove_device(struct b43_wldev *dev)
798 {
799         struct b43_dfsentry *e;
800
801         if (!dev)
802                 return;
803         e = dev->dfsentry;
804         if (!e)
805                 return;
806         b43_remove_dynamic_debug(dev);
807
808         debugfs_remove(e->file_shm16read.dentry);
809         debugfs_remove(e->file_shm16write.dentry);
810         debugfs_remove(e->file_shm32read.dentry);
811         debugfs_remove(e->file_shm32write.dentry);
812         debugfs_remove(e->file_mmio16read.dentry);
813         debugfs_remove(e->file_mmio16write.dentry);
814         debugfs_remove(e->file_mmio32read.dentry);
815         debugfs_remove(e->file_mmio32write.dentry);
816         debugfs_remove(e->file_txstat.dentry);
817         debugfs_remove(e->file_restart.dentry);
818         debugfs_remove(e->file_loctls.dentry);
819
820         debugfs_remove(e->subdir);
821         kfree(e->txstatlog.log);
822         kfree(e);
823 }
824
825 /* Called with IRQs disabled. */
826 void b43_debugfs_log_txstat(struct b43_wldev *dev,
827                             const struct b43_txstatus *status)
828 {
829         struct b43_dfsentry *e = dev->dfsentry;
830         struct b43_txstatus_log *log;
831         struct b43_txstatus *cur;
832         int i;
833
834         if (!e)
835                 return;
836         log = &e->txstatlog;
837         spin_lock(&log->lock); /* IRQs are already disabled. */
838         i = log->end + 1;
839         if (i == B43_NR_LOGGED_TXSTATUS)
840                 i = 0;
841         log->end = i;
842         cur = &(log->log[i]);
843         memcpy(cur, status, sizeof(*cur));
844         spin_unlock(&log->lock);
845 }
846
847 void b43_debugfs_init(void)
848 {
849         rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
850         if (IS_ERR(rootdir))
851                 rootdir = NULL;
852 }
853
854 void b43_debugfs_exit(void)
855 {
856         debugfs_remove(rootdir);
857 }