b24bf63c927f1843881c6e5ba8a0a01edd1ec5d2
[openwrt/openwrt.git] / package / bcm43xx-mac80211 / src / bcm43xx / bcm43xx_debugfs.c
1 /*
2
3 Broadcom BCM43xx wireless driver
4
5 debugfs driver debugging code
6
7 Copyright (c) 2005 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
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 <linux/mutex.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 big_buffer[1024*256];
46 static DEFINE_MUTEX(big_buffer_mutex);
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->i_private;
58 return 0;
59 }
60
61 #define fappend(fmt, x...) pos += snprintf(buf + pos, len - pos, fmt , ##x)
62
63 static ssize_t drvinfo_read_file(struct file *file, char __user *userbuf,
64 size_t count, loff_t *ppos)
65 {
66 const size_t len = ARRAY_SIZE(big_buffer);
67 char *buf = big_buffer;
68 size_t pos = 0;
69 ssize_t res;
70
71 mutex_lock(&big_buffer_mutex);
72 /* This is where the information is written to the "driver" file */
73 fappend(KBUILD_MODNAME " driver\n");
74 fappend("Compiled at: %s %s\n", __DATE__, __TIME__);
75 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
76 mutex_unlock(&big_buffer_mutex);
77
78 return res;
79 }
80
81 static ssize_t tsf_read_file(struct file *file, char __user *userbuf,
82 size_t count, loff_t *ppos)
83 {
84 struct bcm43xx_wldev *dev = file->private_data;
85 const size_t len = ARRAY_SIZE(big_buffer);
86 char *buf = big_buffer;
87 size_t pos = 0;
88 ssize_t res;
89 unsigned long flags;
90 u64 tsf;
91
92 mutex_lock(&big_buffer_mutex);
93 mutex_lock(&dev->wl->mutex);
94 spin_lock_irqsave(&dev->wl->irq_lock, flags);
95 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
96 fappend("Board not initialized.\n");
97 goto out;
98 }
99 bcm43xx_tsf_read(dev, &tsf);
100 fappend("0x%08x%08x\n",
101 (unsigned int)((tsf & 0xFFFFFFFF00000000ULL) >> 32),
102 (unsigned int)(tsf & 0xFFFFFFFFULL));
103
104 out:
105 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
106 mutex_unlock(&dev->wl->mutex);
107 res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
108 mutex_unlock(&big_buffer_mutex);
109
110 return res;
111 }
112
113 static ssize_t tsf_write_file(struct file *file, const char __user *user_buf,
114 size_t count, loff_t *ppos)
115 {
116 struct bcm43xx_wldev *dev = file->private_data;
117 char *buf = big_buffer;
118 ssize_t buf_size;
119 ssize_t res;
120 unsigned long flags;
121 u64 tsf;
122
123 mutex_lock(&big_buffer_mutex);
124 buf_size = min(count, ARRAY_SIZE(big_buffer) - 1);
125 if (copy_from_user(buf, user_buf, buf_size)) {
126 res = -EFAULT;
127 goto out_unlock_bb;
128 }
129 mutex_lock(&dev->wl->mutex);
130 spin_lock_irqsave(&dev->wl->irq_lock, flags);
131 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
132 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
133 res = -EFAULT;
134 goto out_unlock;
135 }
136 if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1) {
137 printk(KERN_INFO PFX "debugfs: invalid values for \"tsf\"\n");
138 res = -EINVAL;
139 goto out_unlock;
140 }
141 bcm43xx_tsf_write(dev, tsf);
142 mmiowb();
143 res = buf_size;
144
145 out_unlock:
146 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
147 mutex_unlock(&dev->wl->mutex);
148 out_unlock_bb:
149 mutex_unlock(&big_buffer_mutex);
150
151 return res;
152 }
153
154 static ssize_t txstat_read_file(struct file *file, char __user *userbuf,
155 size_t count, loff_t *ppos)
156 {
157 struct bcm43xx_wldev *dev = file->private_data;
158 struct bcm43xx_dfsentry *e = dev->dfsentry;
159 struct bcm43xx_txstatus_log *log = &e->txstatlog;
160 unsigned long flags;
161 char *buf = log->printbuf;
162 const size_t len = ARRAY_SIZE(log->printbuf);
163 size_t pos = 0;
164 ssize_t res;
165 int i, idx;
166 struct bcm43xx_txstatus *stat;
167
168 mutex_lock(&big_buffer_mutex);
169 spin_lock_irqsave(&log->lock, flags);
170 if (!log->printing) {
171 log->printing = 1;
172 fappend("bcm43xx TX status reports:\n\n"
173 "index | cookie | seq | phy_stat | frame_count | "
174 "rts_count | supp_reason | pm_indicated | "
175 "intermediate | for_ampdu | acked\n"
176 "---\n");
177 i = log->end + 1;
178 idx = 0;
179 while (1) {
180 if (i == BCM43xx_NR_LOGGED_TXSTATUS)
181 i = 0;
182 stat = &(log->log[i]);
183 if (stat->cookie) {
184 fappend("%03d | "
185 "0x%04X | 0x%04X | 0x%02X | "
186 "0x%X | 0x%X | "
187 "%u | %u | "
188 "%u | %u | %u\n",
189 idx,
190 stat->cookie, stat->seq, stat->phy_stat,
191 stat->frame_count, stat->rts_count,
192 stat->supp_reason, stat->pm_indicated,
193 stat->intermediate, stat->for_ampdu,
194 stat->acked);
195 idx++;
196 }
197 if (i == log->end)
198 break;
199 i++;
200 }
201 log->buf_avail = pos;
202 }
203 memcpy(big_buffer, buf,
204 min(log->buf_avail, ARRAY_SIZE(big_buffer)));
205 spin_unlock_irqrestore(&log->lock, flags);
206
207 res = simple_read_from_buffer(userbuf, count, ppos,
208 big_buffer,
209 log->buf_avail);
210 if (*ppos == log->buf_avail) {
211 spin_lock_irqsave(&log->lock, flags);
212 log->printing = 0;
213 spin_unlock_irqrestore(&log->lock, flags);
214 }
215 mutex_unlock(&big_buffer_mutex);
216
217 return res;
218 }
219
220 static ssize_t restart_write_file(struct file *file, const char __user *user_buf,
221 size_t count, loff_t *ppos)
222 {
223 struct bcm43xx_wldev *dev = file->private_data;
224 char *buf = big_buffer;
225 ssize_t buf_size;
226 ssize_t res;
227 unsigned long flags;
228
229 mutex_lock(&big_buffer_mutex);
230 buf_size = min(count, ARRAY_SIZE(big_buffer) - 1);
231 if (copy_from_user(buf, user_buf, buf_size)) {
232 res = -EFAULT;
233 goto out_unlock_bb;
234 }
235 mutex_lock(&dev->wl->mutex);
236 spin_lock_irqsave(&dev->wl->irq_lock, flags);
237 if (bcm43xx_status(dev) != BCM43xx_STAT_INITIALIZED) {
238 printk(KERN_INFO PFX "debugfs: Board not initialized.\n");
239 res = -EFAULT;
240 goto out_unlock;
241 }
242 if (count > 0 && buf[0] == '1') {
243 bcm43xx_controller_restart(dev, "manually restarted");
244 res = count;
245 } else
246 res = -EINVAL;
247
248 out_unlock:
249 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
250 mutex_unlock(&dev->wl->mutex);
251 out_unlock_bb:
252 mutex_unlock(&big_buffer_mutex);
253
254 return res;
255 }
256
257 #undef fappend
258
259
260 static struct file_operations drvinfo_fops = {
261 .read = drvinfo_read_file,
262 .write = write_file_dummy,
263 .open = open_file_generic,
264 };
265
266 static struct file_operations tsf_fops = {
267 .read = tsf_read_file,
268 .write = tsf_write_file,
269 .open = open_file_generic,
270 };
271
272 static struct file_operations txstat_fops = {
273 .read = txstat_read_file,
274 .write = write_file_dummy,
275 .open = open_file_generic,
276 };
277
278 static struct file_operations restart_fops = {
279 .write = restart_write_file,
280 .open = open_file_generic,
281 };
282
283
284 void bcm43xx_debugfs_add_device(struct bcm43xx_wldev *dev)
285 {
286 struct bcm43xx_dfsentry *e;
287 struct bcm43xx_txstatus_log *log;
288 char devdir[16];
289
290 assert(dev);
291 e = kzalloc(sizeof(*e), GFP_KERNEL);
292 if (!e) {
293 printk(KERN_ERR PFX "out of memory\n");
294 return;
295 }
296 e->dev = dev;
297 log = &e->txstatlog;
298 log->log = kcalloc(BCM43xx_NR_LOGGED_TXSTATUS,
299 sizeof(struct bcm43xx_txstatus),
300 GFP_KERNEL);
301 if (!log->log) {
302 printk(KERN_ERR PFX "debugfs txstatus log OOM\n");
303 kfree(e);
304 return;
305 }
306 log->end = -1;
307 spin_lock_init(&log->lock);
308
309 dev->dfsentry = e;
310
311 snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
312 e->subdir = debugfs_create_dir(devdir, fs.root);
313 e->dentry_tsf = debugfs_create_file("tsf", 0666, e->subdir,
314 dev, &tsf_fops);
315 if (!e->dentry_tsf)
316 printk(KERN_ERR PFX "debugfs: creating \"tsf\" for \"%s\" failed!\n", devdir);
317 e->dentry_txstat = debugfs_create_file("tx_status", 0444, e->subdir,
318 dev, &txstat_fops);
319 if (!e->dentry_txstat)
320 printk(KERN_ERR PFX "debugfs: creating \"tx_status\" for \"%s\" failed!\n", devdir);
321 e->dentry_restart = debugfs_create_file("restart", 0222, e->subdir,
322 dev, &restart_fops);
323 if (!e->dentry_restart)
324 printk(KERN_ERR PFX "debugfs: creating \"restart\" for \"%s\" failed!\n", devdir);
325 }
326
327 void bcm43xx_debugfs_remove_device(struct bcm43xx_wldev *dev)
328 {
329 struct bcm43xx_dfsentry *e;
330
331 if (!dev)
332 return;
333
334 e = dev->dfsentry;
335 assert(e);
336 debugfs_remove(e->dentry_tsf);
337 debugfs_remove(e->dentry_txstat);
338 debugfs_remove(e->dentry_restart);
339 debugfs_remove(e->subdir);
340 kfree(e->txstatlog.log);
341 kfree(e);
342 }
343
344 void bcm43xx_debugfs_log_txstat(struct bcm43xx_wldev *dev,
345 const struct bcm43xx_txstatus *status)
346 {
347 struct bcm43xx_dfsentry *e = dev->dfsentry;
348 struct bcm43xx_txstatus_log *log;
349 struct bcm43xx_txstatus *cur;
350 int i;
351
352 log = &e->txstatlog;
353 assert(irqs_disabled());
354 spin_lock(&log->lock);
355 i = log->end + 1;
356 if (i == BCM43xx_NR_LOGGED_TXSTATUS)
357 i = 0;
358 log->end = i;
359 cur = &(log->log[i]);
360 memcpy(cur, status, sizeof(*cur));
361 spin_unlock(&log->lock);
362 }
363
364 void bcm43xx_debugfs_init(void)
365 {
366 memset(&fs, 0, sizeof(fs));
367 fs.root = debugfs_create_dir(KBUILD_MODNAME, NULL);
368 if (!fs.root)
369 printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "\" subdir failed!\n");
370 fs.dentry_driverinfo = debugfs_create_file("driver", 0444, fs.root, NULL, &drvinfo_fops);
371 if (!fs.dentry_driverinfo)
372 printk(KERN_ERR PFX "debugfs: creating \"" KBUILD_MODNAME "/driver\" failed!\n");
373 }
374
375 void bcm43xx_debugfs_exit(void)
376 {
377 debugfs_remove(fs.dentry_driverinfo);
378 debugfs_remove(fs.root);
379 }
380
381 void bcm43xx_printk_dump(const char *data,
382 size_t size,
383 const char *description)
384 {
385 unsigned int i;
386 char c;
387
388 printk(KERN_INFO PFX "Data dump (%s, %lu bytes):",
389 description, (unsigned long)size);
390 for (i = 0; i < size; i++) {
391 c = data[i];
392 if (i % 8 == 0)
393 printk("\n" KERN_INFO PFX "0x%08x: 0x%02x, ", i, c & 0xff);
394 else
395 printk("0x%02x, ", c & 0xff);
396 }
397 printk("\n");
398 }
399
400 void bcm43xx_printk_bitdump(const unsigned char *data,
401 size_t bytes, int msb_to_lsb,
402 const char *description)
403 {
404 unsigned int i;
405 int j;
406 const unsigned char *d;
407
408 printk(KERN_INFO PFX "*** Bitdump (%s, %lu bytes, %s) ***",
409 description, (unsigned long)bytes,
410 msb_to_lsb ? "MSB to LSB" : "LSB to MSB");
411 for (i = 0; i < bytes; i++) {
412 d = data + i;
413 if (i % 8 == 0)
414 printk("\n" KERN_INFO PFX "0x%08x: ", i);
415 if (msb_to_lsb) {
416 for (j = 7; j >= 0; j--) {
417 if (*d & (1 << j))
418 printk("1");
419 else
420 printk("0");
421 }
422 } else {
423 for (j = 0; j < 8; j++) {
424 if (*d & (1 << j))
425 printk("1");
426 else
427 printk("0");
428 }
429 }
430 printk(" ");
431 }
432 printk("\n");
433 }