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