Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6] / drivers / net / wireless / bcm43xx / bcm43xx_debugfs.c
1 /*
2
3   Broadcom BCM43xx wireless driver
4
5   debugfs driver debugging code
6
7   Copyright (c) 2005 Michael Buesch <mbuesch@freenet.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 <asm/io.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 really_big_buffer[REALLY_BIG_BUFFER_SIZE];
46 static DECLARE_MUTEX(big_buffer_sem);
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->u.generic_ip;
58         return 0;
59 }
60
61 #define fappend(fmt, x...)      pos += snprintf(buf + pos, len - pos, fmt , ##x)
62
63 static ssize_t devinfo_read_file(struct file *file, char __user *userbuf,
64                                  size_t count, loff_t *ppos)
65 {
66         const size_t len = REALLY_BIG_BUFFER_SIZE;
67
68         struct bcm43xx_private *bcm = file->private_data;
69         char *buf = really_big_buffer;
70         size_t pos = 0;
71         ssize_t res;
72         struct net_device *net_dev;
73         struct pci_dev *pci_dev;
74         unsigned long flags;
75         u16 tmp16;
76         int i;
77
78         down(&big_buffer_sem);
79
80         bcm43xx_lock_irqsafe(bcm, flags);
81         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
82                 fappend("Board not initialized.\n");
83                 goto out;
84         }
85         net_dev = bcm->net_dev;
86         pci_dev = bcm->pci_dev;
87
88         /* This is where the information is written to the "devinfo" file */
89         fappend("*** %s devinfo ***\n", net_dev->name);
90         fappend("vendor:           0x%04x   device:           0x%04x\n",
91                 pci_dev->vendor, pci_dev->device);
92         fappend("subsystem_vendor: 0x%04x   subsystem_device: 0x%04x\n",
93                 pci_dev->subsystem_vendor, pci_dev->subsystem_device);
94         fappend("IRQ: %d\n", bcm->irq);
95         fappend("mmio_addr: 0x%p\n", bcm->mmio_addr);
96         fappend("chip_id: 0x%04x   chip_rev: 0x%02x\n", bcm->chip_id, bcm->chip_rev);
97         if ((bcm->core_80211[0].rev >= 3) && (bcm43xx_read32(bcm, 0x0158) & (1 << 16)))
98                 fappend("Radio disabled by hardware!\n");
99         if ((bcm->core_80211[0].rev < 3) && !(bcm43xx_read16(bcm, 0x049A) & (1 << 4)))
100                 fappend("Radio disabled by hardware!\n");
101         fappend("board_vendor: 0x%04x   board_type: 0x%04x\n", bcm->board_vendor,
102                 bcm->board_type);
103
104         fappend("\nCores:\n");
105 #define fappend_core(name, info) fappend("core \"" name "\" %s, %s, id: 0x%04x, "       \
106                                          "rev: 0x%02x, index: 0x%02x\n",                \
107                                          (info).available                               \
108                                                 ? "available" : "nonavailable",         \
109                                          (info).enabled                                 \
110                                                 ? "enabled" : "disabled",               \
111                                          (info).id, (info).rev, (info).index)
112         fappend_core("CHIPCOMMON", bcm->core_chipcommon);
113         fappend_core("PCI", bcm->core_pci);
114         fappend_core("first 80211", bcm->core_80211[0]);
115         fappend_core("second 80211", bcm->core_80211[1]);
116 #undef fappend_core
117         tmp16 = bcm43xx_read16(bcm, BCM43xx_MMIO_GPIO_CONTROL);
118         fappend("LEDs: ");
119         for (i = 0; i < BCM43xx_NR_LEDS; i++)
120                 fappend("%d ", !!(tmp16 & (1 << i)));
121         fappend("\n");
122
123 out:
124         bcm43xx_unlock_irqsafe(bcm, flags);
125         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
126         up(&big_buffer_sem);
127         return res;
128 }
129
130 static ssize_t drvinfo_read_file(struct file *file, char __user *userbuf,
131                                  size_t count, loff_t *ppos)
132 {
133         const size_t len = REALLY_BIG_BUFFER_SIZE;
134
135         char *buf = really_big_buffer;
136         size_t pos = 0;
137         ssize_t res;
138
139         down(&big_buffer_sem);
140
141         /* This is where the information is written to the "driver" file */
142         fappend(KBUILD_MODNAME " driver\n");
143         fappend("Compiled at: %s %s\n", __DATE__, __TIME__);
144
145         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
146         up(&big_buffer_sem);
147         return res;
148 }
149
150 static ssize_t spromdump_read_file(struct file *file, char __user *userbuf,
151                                  size_t count, loff_t *ppos)
152 {
153         const size_t len = REALLY_BIG_BUFFER_SIZE;
154
155         struct bcm43xx_private *bcm = file->private_data;
156         char *buf = really_big_buffer;
157         size_t pos = 0;
158         ssize_t res;
159         unsigned long flags;
160
161         down(&big_buffer_sem);
162         bcm43xx_lock_irqsafe(bcm, flags);
163         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
164                 fappend("Board not initialized.\n");
165                 goto out;
166         }
167
168         /* This is where the information is written to the "sprom_dump" file */
169         fappend("boardflags: 0x%04x\n", bcm->sprom.boardflags);
170
171 out:
172         bcm43xx_unlock_irqsafe(bcm, flags);
173         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
174         up(&big_buffer_sem);
175         return res;
176 }
177
178 static ssize_t tsf_read_file(struct file *file, char __user *userbuf,
179                              size_t count, loff_t *ppos)
180 {
181         const size_t len = REALLY_BIG_BUFFER_SIZE;
182
183         struct bcm43xx_private *bcm = file->private_data;
184         char *buf = really_big_buffer;
185         size_t pos = 0;
186         ssize_t res;
187         unsigned long flags;
188         u64 tsf;
189
190         down(&big_buffer_sem);
191         bcm43xx_lock_irqsafe(bcm, flags);
192         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
193                 fappend("Board not initialized.\n");
194                 goto out;
195         }
196         bcm43xx_tsf_read(bcm, &tsf);
197         fappend("0x%08x%08x\n",
198                 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
199                 (unsigned int)(tsf & 0xFFFFFFFFULL));
200
201 out:
202         bcm43xx_unlock_irqsafe(bcm, flags);
203         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
204         up(&big_buffer_sem);
205         return res;
206 }
207
208 static ssize_t tsf_write_file(struct file *file, const char __user *user_buf,
209                               size_t count, loff_t *ppos)
210 {
211         struct bcm43xx_private *bcm = file->private_data;
212         char *buf = really_big_buffer;
213         ssize_t buf_size;
214         ssize_t res;
215         unsigned long flags;
216         u64 tsf;
217
218         buf_size = min(count, sizeof (really_big_buffer) - 1);
219         down(&big_buffer_sem);
220         if (copy_from_user(buf, user_buf, buf_size)) {
221                 res = -EFAULT;
222                 goto out_up;
223         }
224         bcm43xx_lock_irqsafe(bcm, flags);
225         if (bcm43xx_status(bcm) != BCM43xx_STAT_INITIALIZED) {
226                 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
227                 res = -EFAULT;
228                 goto out_unlock;
229         }
230         if (sscanf(buf, "%lli", &tsf) != 1) {
231                 printk(KERN_INFO PFX "debugfs: invalid values for \"tsf\"\n");
232                 res = -EINVAL;
233                 goto out_unlock;
234         }
235         bcm43xx_tsf_write(bcm, tsf);
236         mmiowb();
237         res = buf_size;
238         
239 out_unlock:
240         bcm43xx_unlock_irqsafe(bcm, flags);
241 out_up:
242         up(&big_buffer_sem);
243         return res;
244 }
245
246 static ssize_t txstat_read_file(struct file *file, char __user *userbuf,
247                                 size_t count, loff_t *ppos)
248 {
249         const size_t len = REALLY_BIG_BUFFER_SIZE;
250
251         struct bcm43xx_private *bcm = file->private_data;
252         char *buf = really_big_buffer;
253         size_t pos = 0;
254         ssize_t res;
255         unsigned long flags;
256         struct bcm43xx_dfsentry *e;
257         struct bcm43xx_xmitstatus *status;
258         int i, cnt, j = 0;
259
260         down(&big_buffer_sem);
261         bcm43xx_lock_irqsafe(bcm, flags);
262
263         fappend("Last %d logged xmitstatus blobs (Latest first):\n\n",
264                 BCM43xx_NR_LOGGED_XMITSTATUS);
265         e = bcm->dfsentry;
266         if (e->xmitstatus_printing == 0) {
267                 /* At the beginning, make a copy of all data to avoid
268                  * concurrency, as this function is called multiple
269                  * times for big logs. Without copying, the data might
270                  * change between reads. This would result in total trash.
271                  */
272                 e->xmitstatus_printing = 1;
273                 e->saved_xmitstatus_ptr = e->xmitstatus_ptr;
274                 e->saved_xmitstatus_cnt = e->xmitstatus_cnt;
275                 memcpy(e->xmitstatus_print_buffer, e->xmitstatus_buffer,
276                        BCM43xx_NR_LOGGED_XMITSTATUS * sizeof(*(e->xmitstatus_buffer)));
277         }
278         i = e->saved_xmitstatus_ptr - 1;
279         if (i < 0)
280                 i = BCM43xx_NR_LOGGED_XMITSTATUS - 1;
281         cnt = e->saved_xmitstatus_cnt;
282         while (cnt) {
283                 status = e->xmitstatus_print_buffer + i;
284                 fappend("0x%02x:   cookie: 0x%04x,  flags: 0x%02x,  "
285                         "cnt1: 0x%02x,  cnt2: 0x%02x,  seq: 0x%04x,  "
286                         "unk: 0x%04x\n", j,
287                         status->cookie, status->flags,
288                         status->cnt1, status->cnt2, status->seq,
289                         status->unknown);
290                 j++;
291                 cnt--;
292                 i--;
293                 if (i < 0)
294                         i = BCM43xx_NR_LOGGED_XMITSTATUS - 1;
295         }
296
297         bcm43xx_unlock_irqsafe(bcm, flags);
298         res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
299         bcm43xx_lock_irqsafe(bcm, flags);
300         if (*ppos == pos) {
301                 /* Done. Drop the copied data. */
302                 e->xmitstatus_printing = 0;
303         }
304         bcm43xx_unlock_irqsafe(bcm, flags);
305         up(&big_buffer_sem);
306         return res;
307 }
308
309 #undef fappend
310
311
312 static struct file_operations devinfo_fops = {
313         .read = devinfo_read_file,
314         .write = write_file_dummy,
315         .open = open_file_generic,
316 };
317
318 static struct file_operations spromdump_fops = {
319         .read = spromdump_read_file,
320         .write = write_file_dummy,
321         .open = open_file_generic,
322 };
323
324 static struct file_operations drvinfo_fops = {
325         .read = drvinfo_read_file,
326         .write = write_file_dummy,
327         .open = open_file_generic,
328 };
329
330 static struct file_operations tsf_fops = {
331         .read = tsf_read_file,
332         .write = tsf_write_file,
333         .open = open_file_generic,
334 };
335
336 static struct file_operations txstat_fops = {
337         .read = txstat_read_file,
338         .write = write_file_dummy,
339         .open = open_file_generic,
340 };
341
342
343 void bcm43xx_debugfs_add_device(struct bcm43xx_private *bcm)
344 {
345         struct bcm43xx_dfsentry *e;
346         char devdir[IFNAMSIZ];
347
348         assert(bcm);
349         e = kzalloc(sizeof(*e), GFP_KERNEL);
350         if (!e) {
351                 printk(KERN_ERR PFX "out of memory\n");
352                 return;
353         }
354         e->bcm = bcm;
355         e->xmitstatus_buffer = kzalloc(BCM43xx_NR_LOGGED_XMITSTATUS
356                                        * sizeof(*(e->xmitstatus_buffer)),
357                                        GFP_KERNEL);
358         if (!e->xmitstatus_buffer) {
359                 printk(KERN_ERR PFX "out of memory\n");
360                 kfree(e);
361                 return;
362         }
363         e->xmitstatus_print_buffer = kzalloc(BCM43xx_NR_LOGGED_XMITSTATUS
364                                              * sizeof(*(e->xmitstatus_buffer)),
365                                              GFP_KERNEL);
366         if (!e->xmitstatus_print_buffer) {
367                 printk(KERN_ERR PFX "out of memory\n");
368                 kfree(e);
369                 return;
370         }
371
372
373         bcm->dfsentry = e;
374
375         strncpy(devdir, bcm->net_dev->name, ARRAY_SIZE(devdir));
376         e->subdir = debugfs_create_dir(devdir, fs.root);
377         e->dentry_devinfo = debugfs_create_file("devinfo", 0444, e->subdir,
378                                                 bcm, &devinfo_fops);
379         if (!e->dentry_devinfo)
380                 printk(KERN_ERR PFX "debugfs: creating \"devinfo\" for \"%s\" failed!\n", devdir);
381         e->dentry_spromdump = debugfs_create_file("sprom_dump", 0444, e->subdir,
382                                                   bcm, &spromdump_fops);
383         if (!e->dentry_spromdump)
384                 printk(KERN_ERR PFX "debugfs: creating \"sprom_dump\" for \"%s\" failed!\n", devdir);
385         e->dentry_tsf = debugfs_create_file("tsf", 0666, e->subdir,
386                                             bcm, &tsf_fops);
387         if (!e->dentry_tsf)
388                 printk(KERN_ERR PFX "debugfs: creating \"tsf\" for \"%s\" failed!\n", devdir);
389         e->dentry_txstat = debugfs_create_file("tx_status", 0444, e->subdir,
390                                                 bcm, &txstat_fops);
391         if (!e->dentry_txstat)
392                 printk(KERN_ERR PFX "debugfs: creating \"tx_status\" for \"%s\" failed!\n", devdir);
393 }
394
395 void bcm43xx_debugfs_remove_device(struct bcm43xx_private *bcm)
396 {
397         struct bcm43xx_dfsentry *e;
398
399         if (!bcm)
400                 return;
401
402         e = bcm->dfsentry;
403         assert(e);
404         debugfs_remove(e->dentry_spromdump);
405         debugfs_remove(e->dentry_devinfo);
406         debugfs_remove(e->dentry_tsf);
407         debugfs_remove(e->dentry_txstat);
408         debugfs_remove(e->subdir);
409         kfree(e->xmitstatus_buffer);
410         kfree(e->xmitstatus_print_buffer);
411         kfree(e);
412 }
413
414 void bcm43xx_debugfs_log_txstat(struct bcm43xx_private *bcm,
415                                 struct bcm43xx_xmitstatus *status)
416 {
417         struct bcm43xx_dfsentry *e;
418         struct bcm43xx_xmitstatus *savedstatus;
419
420         /* This is protected by bcm->_lock */
421         e = bcm->dfsentry;
422         assert(e);
423         savedstatus = e->xmitstatus_buffer + e->xmitstatus_ptr;
424         memcpy(savedstatus, status, sizeof(*status));
425         e->xmitstatus_ptr++;
426         if (e->xmitstatus_ptr >= BCM43xx_NR_LOGGED_XMITSTATUS)
427                 e->xmitstatus_ptr = 0;
428         if (e->xmitstatus_cnt < BCM43xx_NR_LOGGED_XMITSTATUS)
429                 e->xmitstatus_cnt++;
430 }
431
432 void bcm43xx_debugfs_init(void)
433 {
434         memset(&fs, 0, sizeof(fs));
435         fs.root = debugfs_create_dir(KBUILD_MODNAME, NULL);
436         if (!fs.root)
437                 printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "\" subdir failed!\n");
438         fs.dentry_driverinfo = debugfs_create_file("driver", 0444, fs.root, NULL, &drvinfo_fops);
439         if (!fs.dentry_driverinfo)
440                 printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "/driver\" failed!\n");
441 }
442
443 void bcm43xx_debugfs_exit(void)
444 {
445         debugfs_remove(fs.dentry_driverinfo);
446         debugfs_remove(fs.root);
447 }
448
449 void bcm43xx_printk_dump(const char *data,
450                          size_t size,
451                          const char *description)
452 {
453         size_t i;
454         char c;
455
456         printk(KERN_INFO PFX "Data dump (%s, %zd bytes):",
457                description, size);
458         for (i = 0; i < size; i++) {
459                 c = data[i];
460                 if (i % 8 == 0)
461                         printk("\n" KERN_INFO PFX "0x%08zx:  0x%02x, ", i, c & 0xff);
462                 else
463                         printk("0x%02x, ", c & 0xff);
464         }
465         printk("\n");
466 }
467
468 void bcm43xx_printk_bitdump(const unsigned char *data,
469                             size_t bytes, int msb_to_lsb,
470                             const char *description)
471 {
472         size_t i;
473         int j;
474         const unsigned char *d;
475
476         printk(KERN_INFO PFX "*** Bitdump (%s, %zd bytes, %s) ***",
477                description, bytes, msb_to_lsb ? "MSB to LSB" : "LSB to MSB");
478         for (i = 0; i < bytes; i++) {
479                 d = data + i;
480                 if (i % 8 == 0)
481                         printk("\n" KERN_INFO PFX "0x%08zx:  ", i);
482                 if (msb_to_lsb) {
483                         for (j = 7; j >= 0; j--) {
484                                 if (*d & (1 << j))
485                                         printk("1");
486                                 else
487                                         printk("0");
488                         }
489                 } else {
490                         for (j = 0; j < 8; j++) {
491                                 if (*d & (1 << j))
492                                         printk("1");
493                                 else
494                                         printk("0");
495                         }
496                 }
497                 printk(" ");
498         }
499         printk("\n");
500 }