Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / net / wireless / b43legacy / debugfs.c
1 /*
2
3   Broadcom B43legacy wireless driver
4
5   debugfs driver debugging code
6
7   Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
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 "b43legacy.h"
34 #include "main.h"
35 #include "debugfs.h"
36 #include "dma.h"
37 #include "pio.h"
38 #include "xmit.h"
39
40
41 /* The root directory. */
42 static struct dentry *rootdir;
43
44 struct b43legacy_debugfs_fops {
45         ssize_t (*read)(struct b43legacy_wldev *dev, char *buf, size_t bufsize);
46         int (*write)(struct b43legacy_wldev *dev, const char *buf, size_t count);
47         struct file_operations fops;
48         /* Offset of struct b43legacy_dfs_file in struct b43legacy_dfsentry */
49         size_t file_struct_offset;
50         /* Take wl->irq_lock before calling read/write? */
51         bool take_irqlock;
52 };
53
54 static inline
55 struct b43legacy_dfs_file * fops_to_dfs_file(struct b43legacy_wldev *dev,
56                                        const struct b43legacy_debugfs_fops *dfops)
57 {
58         void *p;
59
60         p = dev->dfsentry;
61         p += dfops->file_struct_offset;
62
63         return p;
64 }
65
66
67 #define fappend(fmt, x...)      \
68         do {                                                    \
69                 if (bufsize - count)                            \
70                         count += snprintf(buf + count,          \
71                                           bufsize - count,      \
72                                           fmt , ##x);           \
73                 else                                            \
74                         printk(KERN_ERR "b43legacy: fappend overflow\n"); \
75         } while (0)
76
77
78 /* wl->irq_lock is locked */
79 static ssize_t tsf_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
80 {
81         ssize_t count = 0;
82         u64 tsf;
83
84         b43legacy_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 b43legacy_wldev *dev, const char *buf, size_t count)
94 {
95         u64 tsf;
96
97         if (sscanf(buf, "%llu", (unsigned long long *)(&tsf)) != 1)
98                 return -EINVAL;
99         b43legacy_tsf_write(dev, tsf);
100
101         return 0;
102 }
103
104 /* wl->irq_lock is locked */
105 static ssize_t ucode_regs_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
106 {
107         ssize_t count = 0;
108         int i;
109
110         for (i = 0; i < 64; i++) {
111                 fappend("r%d = 0x%04x\n", i,
112                         b43legacy_shm_read16(dev, B43legacy_SHM_WIRELESS, i));
113         }
114
115         return count;
116 }
117
118 /* wl->irq_lock is locked */
119 static ssize_t shm_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
120 {
121         ssize_t count = 0;
122         int i;
123         u16 tmp;
124         __le16 *le16buf = (__le16 *)buf;
125
126         for (i = 0; i < 0x1000; i++) {
127                 if (bufsize < sizeof(tmp))
128                         break;
129                 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 2 * i);
130                 le16buf[i] = cpu_to_le16(tmp);
131                 count += sizeof(tmp);
132                 bufsize -= sizeof(tmp);
133         }
134
135         return count;
136 }
137
138 static ssize_t txstat_read_file(struct b43legacy_wldev *dev, char *buf, size_t bufsize)
139 {
140         struct b43legacy_txstatus_log *log = &dev->dfsentry->txstatlog;
141         ssize_t count = 0;
142         unsigned long flags;
143         int i, idx;
144         struct b43legacy_txstatus *stat;
145
146         spin_lock_irqsave(&log->lock, flags);
147         if (log->end < 0) {
148                 fappend("Nothing transmitted, yet\n");
149                 goto out_unlock;
150         }
151         fappend("b43legacy TX status reports:\n\n"
152                 "index | cookie | seq | phy_stat | frame_count | "
153                 "rts_count | supp_reason | pm_indicated | "
154                 "intermediate | for_ampdu | acked\n" "---\n");
155         i = log->end + 1;
156         idx = 0;
157         while (1) {
158                 if (i == B43legacy_NR_LOGGED_TXSTATUS)
159                         i = 0;
160                 stat = &(log->log[i]);
161                 if (stat->cookie) {
162                         fappend("%03d | "
163                                 "0x%04X | 0x%04X | 0x%02X | "
164                                 "0x%X | 0x%X | "
165                                 "%u | %u | "
166                                 "%u | %u | %u\n",
167                                 idx,
168                                 stat->cookie, stat->seq, stat->phy_stat,
169                                 stat->frame_count, stat->rts_count,
170                                 stat->supp_reason, stat->pm_indicated,
171                                 stat->intermediate, stat->for_ampdu,
172                                 stat->acked);
173                         idx++;
174                 }
175                 if (i == log->end)
176                         break;
177                 i++;
178         }
179 out_unlock:
180         spin_unlock_irqrestore(&log->lock, flags);
181
182         return count;
183 }
184
185 /* wl->irq_lock is locked */
186 static int restart_write_file(struct b43legacy_wldev *dev, const char *buf, size_t count)
187 {
188         int err = 0;
189
190         if (count > 0 && buf[0] == '1') {
191                 b43legacy_controller_restart(dev, "manually restarted");
192         } else
193                 err = -EINVAL;
194
195         return err;
196 }
197
198 #undef fappend
199
200 static ssize_t b43legacy_debugfs_read(struct file *file, char __user *userbuf,
201                                 size_t count, loff_t *ppos)
202 {
203         struct b43legacy_wldev *dev;
204         struct b43legacy_debugfs_fops *dfops;
205         struct b43legacy_dfs_file *dfile;
206         ssize_t uninitialized_var(ret);
207         char *buf;
208         const size_t bufsize = 1024 * 16; /* 16 KiB buffer */
209         const size_t buforder = get_order(bufsize);
210         int err = 0;
211
212         if (!count)
213                 return 0;
214         dev = file->private_data;
215         if (!dev)
216                 return -ENODEV;
217
218         mutex_lock(&dev->wl->mutex);
219         if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
220                 err = -ENODEV;
221                 goto out_unlock;
222         }
223
224         dfops = container_of(file->f_op, struct b43legacy_debugfs_fops, fops);
225         if (!dfops->read) {
226                 err = -ENOSYS;
227                 goto out_unlock;
228         }
229         dfile = fops_to_dfs_file(dev, dfops);
230
231         if (!dfile->buffer) {
232                 buf = (char *)__get_free_pages(GFP_KERNEL, buforder);
233                 if (!buf) {
234                         err = -ENOMEM;
235                         goto out_unlock;
236                 }
237                 memset(buf, 0, bufsize);
238                 if (dfops->take_irqlock) {
239                         spin_lock_irq(&dev->wl->irq_lock);
240                         ret = dfops->read(dev, buf, bufsize);
241                         spin_unlock_irq(&dev->wl->irq_lock);
242                 } else
243                         ret = dfops->read(dev, buf, bufsize);
244                 if (ret <= 0) {
245                         free_pages((unsigned long)buf, buforder);
246                         err = ret;
247                         goto out_unlock;
248                 }
249                 dfile->data_len = ret;
250                 dfile->buffer = buf;
251         }
252
253         ret = simple_read_from_buffer(userbuf, count, ppos,
254                                       dfile->buffer,
255                                       dfile->data_len);
256         if (*ppos >= dfile->data_len) {
257                 free_pages((unsigned long)dfile->buffer, buforder);
258                 dfile->buffer = NULL;
259                 dfile->data_len = 0;
260         }
261 out_unlock:
262         mutex_unlock(&dev->wl->mutex);
263
264         return err ? err : ret;
265 }
266
267 static ssize_t b43legacy_debugfs_write(struct file *file,
268                                  const char __user *userbuf,
269                                  size_t count, loff_t *ppos)
270 {
271         struct b43legacy_wldev *dev;
272         struct b43legacy_debugfs_fops *dfops;
273         char *buf;
274         int err = 0;
275
276         if (!count)
277                 return 0;
278         if (count > PAGE_SIZE)
279                 return -E2BIG;
280         dev = file->private_data;
281         if (!dev)
282                 return -ENODEV;
283
284         mutex_lock(&dev->wl->mutex);
285         if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
286                 err = -ENODEV;
287                 goto out_unlock;
288         }
289
290         dfops = container_of(file->f_op, struct b43legacy_debugfs_fops, fops);
291         if (!dfops->write) {
292                 err = -ENOSYS;
293                 goto out_unlock;
294         }
295
296         buf = (char *)get_zeroed_page(GFP_KERNEL);
297         if (!buf) {
298                 err = -ENOMEM;
299                 goto out_unlock;
300         }
301         if (copy_from_user(buf, userbuf, count)) {
302                 err = -EFAULT;
303                 goto out_freepage;
304         }
305         if (dfops->take_irqlock) {
306                 spin_lock_irq(&dev->wl->irq_lock);
307                 err = dfops->write(dev, buf, count);
308                 spin_unlock_irq(&dev->wl->irq_lock);
309         } else
310                 err = dfops->write(dev, buf, count);
311         if (err)
312                 goto out_freepage;
313
314 out_freepage:
315         free_page((unsigned long)buf);
316 out_unlock:
317         mutex_unlock(&dev->wl->mutex);
318
319         return err ? err : count;
320 }
321
322
323 #define B43legacy_DEBUGFS_FOPS(name, _read, _write, _take_irqlock)      \
324         static struct b43legacy_debugfs_fops fops_##name = {            \
325                 .read   = _read,                                \
326                 .write  = _write,                               \
327                 .fops   = {                                     \
328                         .open   = simple_open,                          \
329                         .read   = b43legacy_debugfs_read,               \
330                         .write  = b43legacy_debugfs_write,              \
331                         .llseek = generic_file_llseek,                  \
332                 },                                              \
333                 .file_struct_offset = offsetof(struct b43legacy_dfsentry, \
334                                                file_##name),    \
335                 .take_irqlock   = _take_irqlock,                \
336         }
337
338 B43legacy_DEBUGFS_FOPS(tsf, tsf_read_file, tsf_write_file, 1);
339 B43legacy_DEBUGFS_FOPS(ucode_regs, ucode_regs_read_file, NULL, 1);
340 B43legacy_DEBUGFS_FOPS(shm, shm_read_file, NULL, 1);
341 B43legacy_DEBUGFS_FOPS(txstat, txstat_read_file, NULL, 0);
342 B43legacy_DEBUGFS_FOPS(restart, NULL, restart_write_file, 1);
343
344
345 int b43legacy_debug(struct b43legacy_wldev *dev, enum b43legacy_dyndbg feature)
346 {
347         return !!(dev->dfsentry && dev->dfsentry->dyn_debug[feature]);
348 }
349
350 static void b43legacy_remove_dynamic_debug(struct b43legacy_wldev *dev)
351 {
352         struct b43legacy_dfsentry *e = dev->dfsentry;
353         int i;
354
355         for (i = 0; i < __B43legacy_NR_DYNDBG; i++)
356                 debugfs_remove(e->dyn_debug_dentries[i]);
357 }
358
359 static void b43legacy_add_dynamic_debug(struct b43legacy_wldev *dev)
360 {
361         struct b43legacy_dfsentry *e = dev->dfsentry;
362         struct dentry *d;
363
364 #define add_dyn_dbg(name, id, initstate) do {           \
365         e->dyn_debug[id] = (initstate);                 \
366         d = debugfs_create_bool(name, 0600, e->subdir,  \
367                                 &(e->dyn_debug[id]));   \
368         if (!IS_ERR(d))                                 \
369                 e->dyn_debug_dentries[id] = d;          \
370                                 } while (0)
371
372         add_dyn_dbg("debug_xmitpower", B43legacy_DBG_XMITPOWER, 0);
373         add_dyn_dbg("debug_dmaoverflow", B43legacy_DBG_DMAOVERFLOW, 0);
374         add_dyn_dbg("debug_dmaverbose", B43legacy_DBG_DMAVERBOSE, 0);
375         add_dyn_dbg("debug_pwork_fast", B43legacy_DBG_PWORK_FAST, 0);
376         add_dyn_dbg("debug_pwork_stop", B43legacy_DBG_PWORK_STOP, 0);
377
378 #undef add_dyn_dbg
379 }
380
381 void b43legacy_debugfs_add_device(struct b43legacy_wldev *dev)
382 {
383         struct b43legacy_dfsentry *e;
384         struct b43legacy_txstatus_log *log;
385         char devdir[16];
386
387         B43legacy_WARN_ON(!dev);
388         e = kzalloc(sizeof(*e), GFP_KERNEL);
389         if (!e) {
390                 b43legacyerr(dev->wl, "debugfs: add device OOM\n");
391                 return;
392         }
393         e->dev = dev;
394         log = &e->txstatlog;
395         log->log = kcalloc(B43legacy_NR_LOGGED_TXSTATUS,
396                            sizeof(struct b43legacy_txstatus), GFP_KERNEL);
397         if (!log->log) {
398                 b43legacyerr(dev->wl, "debugfs: add device txstatus OOM\n");
399                 kfree(e);
400                 return;
401         }
402         log->end = -1;
403         spin_lock_init(&log->lock);
404
405         dev->dfsentry = e;
406
407         snprintf(devdir, sizeof(devdir), "%s", wiphy_name(dev->wl->hw->wiphy));
408         e->subdir = debugfs_create_dir(devdir, rootdir);
409         if (!e->subdir || IS_ERR(e->subdir)) {
410                 if (e->subdir == ERR_PTR(-ENODEV)) {
411                         b43legacydbg(dev->wl, "DebugFS (CONFIG_DEBUG_FS) not "
412                                "enabled in kernel config\n");
413                 } else {
414                         b43legacyerr(dev->wl, "debugfs: cannot create %s directory\n",
415                                devdir);
416                 }
417                 dev->dfsentry = NULL;
418                 kfree(log->log);
419                 kfree(e);
420                 return;
421         }
422
423 #define ADD_FILE(name, mode)    \
424         do {                                                    \
425                 struct dentry *d;                               \
426                 d = debugfs_create_file(__stringify(name),      \
427                                         mode, e->subdir, dev,   \
428                                         &fops_##name.fops);     \
429                 e->file_##name.dentry = NULL;                   \
430                 if (!IS_ERR(d))                                 \
431                         e->file_##name.dentry = d;              \
432         } while (0)
433
434
435         ADD_FILE(tsf, 0600);
436         ADD_FILE(ucode_regs, 0400);
437         ADD_FILE(shm, 0400);
438         ADD_FILE(txstat, 0400);
439         ADD_FILE(restart, 0200);
440
441 #undef ADD_FILE
442
443         b43legacy_add_dynamic_debug(dev);
444 }
445
446 void b43legacy_debugfs_remove_device(struct b43legacy_wldev *dev)
447 {
448         struct b43legacy_dfsentry *e;
449
450         if (!dev)
451                 return;
452         e = dev->dfsentry;
453         if (!e)
454                 return;
455         b43legacy_remove_dynamic_debug(dev);
456
457         debugfs_remove(e->file_tsf.dentry);
458         debugfs_remove(e->file_ucode_regs.dentry);
459         debugfs_remove(e->file_shm.dentry);
460         debugfs_remove(e->file_txstat.dentry);
461         debugfs_remove(e->file_restart.dentry);
462
463         debugfs_remove(e->subdir);
464         kfree(e->txstatlog.log);
465         kfree(e);
466 }
467
468 void b43legacy_debugfs_log_txstat(struct b43legacy_wldev *dev,
469                             const struct b43legacy_txstatus *status)
470 {
471         struct b43legacy_dfsentry *e = dev->dfsentry;
472         struct b43legacy_txstatus_log *log;
473         struct b43legacy_txstatus *cur;
474         int i;
475
476         if (!e)
477                 return;
478         log = &e->txstatlog;
479         B43legacy_WARN_ON(!irqs_disabled());
480         spin_lock(&log->lock);
481         i = log->end + 1;
482         if (i == B43legacy_NR_LOGGED_TXSTATUS)
483                 i = 0;
484         log->end = i;
485         cur = &(log->log[i]);
486         memcpy(cur, status, sizeof(*cur));
487         spin_unlock(&log->lock);
488 }
489
490 void b43legacy_debugfs_init(void)
491 {
492         rootdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
493         if (IS_ERR(rootdir))
494                 rootdir = NULL;
495 }
496
497 void b43legacy_debugfs_exit(void)
498 {
499         debugfs_remove(rootdir);
500 }