]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/ath/ath5k/debug.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / wireless / ath / ath5k / debug.c
1 /*
2  * Copyright (c) 2007-2008 Bruno Randolf <bruno@thinktube.com>
3  *
4  *  This file is free software: you may copy, redistribute and/or modify it
5  *  under the terms of the GNU General Public License as published by the
6  *  Free Software Foundation, either version 2 of the License, or (at your
7  *  option) any later version.
8  *
9  *  This file is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  *
18  * This file incorporates work covered by the following copyright and
19  * permission notice:
20  *
21  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
22  * Copyright (c) 2004-2005 Atheros Communications, Inc.
23  * Copyright (c) 2006 Devicescape Software, Inc.
24  * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
25  * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
26  *
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer,
34  *    without modification.
35  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
36  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
37  *    redistribution must be conditioned upon including a substantially
38  *    similar Disclaimer requirement for further binary redistribution.
39  * 3. Neither the names of the above-listed copyright holders nor the names
40  *    of any contributors may be used to endorse or promote products derived
41  *    from this software without specific prior written permission.
42  *
43  * Alternatively, this software may be distributed under the terms of the
44  * GNU General Public License ("GPL") version 2 as published by the Free
45  * Software Foundation.
46  *
47  * NO WARRANTY
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
51  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
52  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
53  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
56  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58  * THE POSSIBILITY OF SUCH DAMAGES.
59  */
60
61 #include "base.h"
62 #include "debug.h"
63
64 static unsigned int ath5k_debug;
65 module_param_named(debug, ath5k_debug, uint, 0);
66
67
68 #ifdef CONFIG_ATH5K_DEBUG
69
70 #include <linux/seq_file.h>
71 #include "reg.h"
72 #include "ani.h"
73
74 static struct dentry *ath5k_global_debugfs;
75
76 static int ath5k_debugfs_open(struct inode *inode, struct file *file)
77 {
78         file->private_data = inode->i_private;
79         return 0;
80 }
81
82
83 /* debugfs: registers */
84
85 struct reg {
86         const char *name;
87         int addr;
88 };
89
90 #define REG_STRUCT_INIT(r) { #r, r }
91
92 /* just a few random registers, might want to add more */
93 static const struct reg regs[] = {
94         REG_STRUCT_INIT(AR5K_CR),
95         REG_STRUCT_INIT(AR5K_RXDP),
96         REG_STRUCT_INIT(AR5K_CFG),
97         REG_STRUCT_INIT(AR5K_IER),
98         REG_STRUCT_INIT(AR5K_BCR),
99         REG_STRUCT_INIT(AR5K_RTSD0),
100         REG_STRUCT_INIT(AR5K_RTSD1),
101         REG_STRUCT_INIT(AR5K_TXCFG),
102         REG_STRUCT_INIT(AR5K_RXCFG),
103         REG_STRUCT_INIT(AR5K_RXJLA),
104         REG_STRUCT_INIT(AR5K_MIBC),
105         REG_STRUCT_INIT(AR5K_TOPS),
106         REG_STRUCT_INIT(AR5K_RXNOFRM),
107         REG_STRUCT_INIT(AR5K_TXNOFRM),
108         REG_STRUCT_INIT(AR5K_RPGTO),
109         REG_STRUCT_INIT(AR5K_RFCNT),
110         REG_STRUCT_INIT(AR5K_MISC),
111         REG_STRUCT_INIT(AR5K_QCUDCU_CLKGT),
112         REG_STRUCT_INIT(AR5K_ISR),
113         REG_STRUCT_INIT(AR5K_PISR),
114         REG_STRUCT_INIT(AR5K_SISR0),
115         REG_STRUCT_INIT(AR5K_SISR1),
116         REG_STRUCT_INIT(AR5K_SISR2),
117         REG_STRUCT_INIT(AR5K_SISR3),
118         REG_STRUCT_INIT(AR5K_SISR4),
119         REG_STRUCT_INIT(AR5K_IMR),
120         REG_STRUCT_INIT(AR5K_PIMR),
121         REG_STRUCT_INIT(AR5K_SIMR0),
122         REG_STRUCT_INIT(AR5K_SIMR1),
123         REG_STRUCT_INIT(AR5K_SIMR2),
124         REG_STRUCT_INIT(AR5K_SIMR3),
125         REG_STRUCT_INIT(AR5K_SIMR4),
126         REG_STRUCT_INIT(AR5K_DCM_ADDR),
127         REG_STRUCT_INIT(AR5K_DCCFG),
128         REG_STRUCT_INIT(AR5K_CCFG),
129         REG_STRUCT_INIT(AR5K_CPC0),
130         REG_STRUCT_INIT(AR5K_CPC1),
131         REG_STRUCT_INIT(AR5K_CPC2),
132         REG_STRUCT_INIT(AR5K_CPC3),
133         REG_STRUCT_INIT(AR5K_CPCOVF),
134         REG_STRUCT_INIT(AR5K_RESET_CTL),
135         REG_STRUCT_INIT(AR5K_SLEEP_CTL),
136         REG_STRUCT_INIT(AR5K_INTPEND),
137         REG_STRUCT_INIT(AR5K_SFR),
138         REG_STRUCT_INIT(AR5K_PCICFG),
139         REG_STRUCT_INIT(AR5K_GPIOCR),
140         REG_STRUCT_INIT(AR5K_GPIODO),
141         REG_STRUCT_INIT(AR5K_SREV),
142 };
143
144 static void *reg_start(struct seq_file *seq, loff_t *pos)
145 {
146         return *pos < ARRAY_SIZE(regs) ? (void *)&regs[*pos] : NULL;
147 }
148
149 static void reg_stop(struct seq_file *seq, void *p)
150 {
151         /* nothing to do */
152 }
153
154 static void *reg_next(struct seq_file *seq, void *p, loff_t *pos)
155 {
156         ++*pos;
157         return *pos < ARRAY_SIZE(regs) ? (void *)&regs[*pos] : NULL;
158 }
159
160 static int reg_show(struct seq_file *seq, void *p)
161 {
162         struct ath5k_softc *sc = seq->private;
163         struct reg *r = p;
164         seq_printf(seq, "%-25s0x%08x\n", r->name,
165                 ath5k_hw_reg_read(sc->ah, r->addr));
166         return 0;
167 }
168
169 static const struct seq_operations register_seq_ops = {
170         .start = reg_start,
171         .next  = reg_next,
172         .stop  = reg_stop,
173         .show  = reg_show
174 };
175
176 static int open_file_registers(struct inode *inode, struct file *file)
177 {
178         struct seq_file *s;
179         int res;
180         res = seq_open(file, &register_seq_ops);
181         if (res == 0) {
182                 s = file->private_data;
183                 s->private = inode->i_private;
184         }
185         return res;
186 }
187
188 static const struct file_operations fops_registers = {
189         .open = open_file_registers,
190         .read    = seq_read,
191         .llseek  = seq_lseek,
192         .release = seq_release,
193         .owner = THIS_MODULE,
194 };
195
196
197 /* debugfs: beacons */
198
199 static ssize_t read_file_beacon(struct file *file, char __user *user_buf,
200                                    size_t count, loff_t *ppos)
201 {
202         struct ath5k_softc *sc = file->private_data;
203         struct ath5k_hw *ah = sc->ah;
204         char buf[500];
205         unsigned int len = 0;
206         unsigned int v;
207         u64 tsf;
208
209         v = ath5k_hw_reg_read(sc->ah, AR5K_BEACON);
210         len += snprintf(buf+len, sizeof(buf)-len,
211                 "%-24s0x%08x\tintval: %d\tTIM: 0x%x\n",
212                 "AR5K_BEACON", v, v & AR5K_BEACON_PERIOD,
213                 (v & AR5K_BEACON_TIM) >> AR5K_BEACON_TIM_S);
214
215         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\n",
216                 "AR5K_LAST_TSTP", ath5k_hw_reg_read(sc->ah, AR5K_LAST_TSTP));
217
218         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\n\n",
219                 "AR5K_BEACON_CNT", ath5k_hw_reg_read(sc->ah, AR5K_BEACON_CNT));
220
221         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER0);
222         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
223                 "AR5K_TIMER0 (TBTT)", v, v);
224
225         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER1);
226         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
227                 "AR5K_TIMER1 (DMA)", v, v >> 3);
228
229         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER2);
230         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
231                 "AR5K_TIMER2 (SWBA)", v, v >> 3);
232
233         v = ath5k_hw_reg_read(sc->ah, AR5K_TIMER3);
234         len += snprintf(buf+len, sizeof(buf)-len, "%-24s0x%08x\tTU: %08x\n",
235                 "AR5K_TIMER3 (ATIM)", v, v);
236
237         tsf = ath5k_hw_get_tsf64(sc->ah);
238         len += snprintf(buf+len, sizeof(buf)-len,
239                 "TSF\t\t0x%016llx\tTU: %08x\n",
240                 (unsigned long long)tsf, TSF_TO_TU(tsf));
241
242         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
243 }
244
245 static ssize_t write_file_beacon(struct file *file,
246                                  const char __user *userbuf,
247                                  size_t count, loff_t *ppos)
248 {
249         struct ath5k_softc *sc = file->private_data;
250         struct ath5k_hw *ah = sc->ah;
251         char buf[20];
252
253         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
254                 return -EFAULT;
255
256         if (strncmp(buf, "disable", 7) == 0) {
257                 AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
258                 printk(KERN_INFO "debugfs disable beacons\n");
259         } else if (strncmp(buf, "enable", 6) == 0) {
260                 AR5K_REG_ENABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE);
261                 printk(KERN_INFO "debugfs enable beacons\n");
262         }
263         return count;
264 }
265
266 static const struct file_operations fops_beacon = {
267         .read = read_file_beacon,
268         .write = write_file_beacon,
269         .open = ath5k_debugfs_open,
270         .owner = THIS_MODULE,
271 };
272
273
274 /* debugfs: reset */
275
276 static ssize_t write_file_reset(struct file *file,
277                                  const char __user *userbuf,
278                                  size_t count, loff_t *ppos)
279 {
280         struct ath5k_softc *sc = file->private_data;
281         tasklet_schedule(&sc->restq);
282         return count;
283 }
284
285 static const struct file_operations fops_reset = {
286         .write = write_file_reset,
287         .open = ath5k_debugfs_open,
288         .owner = THIS_MODULE,
289 };
290
291
292 /* debugfs: debug level */
293
294 static const struct {
295         enum ath5k_debug_level level;
296         const char *name;
297         const char *desc;
298 } dbg_info[] = {
299         { ATH5K_DEBUG_RESET,    "reset",        "reset and initialization" },
300         { ATH5K_DEBUG_INTR,     "intr",         "interrupt handling" },
301         { ATH5K_DEBUG_MODE,     "mode",         "mode init/setup" },
302         { ATH5K_DEBUG_XMIT,     "xmit",         "basic xmit operation" },
303         { ATH5K_DEBUG_BEACON,   "beacon",       "beacon handling" },
304         { ATH5K_DEBUG_CALIBRATE, "calib",       "periodic calibration" },
305         { ATH5K_DEBUG_TXPOWER,  "txpower",      "transmit power setting" },
306         { ATH5K_DEBUG_LED,      "led",          "LED management" },
307         { ATH5K_DEBUG_DUMP_RX,  "dumprx",       "print received skb content" },
308         { ATH5K_DEBUG_DUMP_TX,  "dumptx",       "print transmit skb content" },
309         { ATH5K_DEBUG_DUMPBANDS, "dumpbands",   "dump bands" },
310         { ATH5K_DEBUG_ANI,      "ani",          "adaptive noise immunity" },
311         { ATH5K_DEBUG_ANY,      "all",          "show all debug levels" },
312 };
313
314 static ssize_t read_file_debug(struct file *file, char __user *user_buf,
315                                    size_t count, loff_t *ppos)
316 {
317         struct ath5k_softc *sc = file->private_data;
318         char buf[700];
319         unsigned int len = 0;
320         unsigned int i;
321
322         len += snprintf(buf+len, sizeof(buf)-len,
323                 "DEBUG LEVEL: 0x%08x\n\n", sc->debug.level);
324
325         for (i = 0; i < ARRAY_SIZE(dbg_info) - 1; i++) {
326                 len += snprintf(buf+len, sizeof(buf)-len,
327                         "%10s %c 0x%08x - %s\n", dbg_info[i].name,
328                         sc->debug.level & dbg_info[i].level ? '+' : ' ',
329                         dbg_info[i].level, dbg_info[i].desc);
330         }
331         len += snprintf(buf+len, sizeof(buf)-len,
332                 "%10s %c 0x%08x - %s\n", dbg_info[i].name,
333                 sc->debug.level == dbg_info[i].level ? '+' : ' ',
334                 dbg_info[i].level, dbg_info[i].desc);
335
336         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
337 }
338
339 static ssize_t write_file_debug(struct file *file,
340                                  const char __user *userbuf,
341                                  size_t count, loff_t *ppos)
342 {
343         struct ath5k_softc *sc = file->private_data;
344         unsigned int i;
345         char buf[20];
346
347         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
348                 return -EFAULT;
349
350         for (i = 0; i < ARRAY_SIZE(dbg_info); i++) {
351                 if (strncmp(buf, dbg_info[i].name,
352                                         strlen(dbg_info[i].name)) == 0) {
353                         sc->debug.level ^= dbg_info[i].level; /* toggle bit */
354                         break;
355                 }
356         }
357         return count;
358 }
359
360 static const struct file_operations fops_debug = {
361         .read = read_file_debug,
362         .write = write_file_debug,
363         .open = ath5k_debugfs_open,
364         .owner = THIS_MODULE,
365 };
366
367
368 /* debugfs: antenna */
369
370 static ssize_t read_file_antenna(struct file *file, char __user *user_buf,
371                                    size_t count, loff_t *ppos)
372 {
373         struct ath5k_softc *sc = file->private_data;
374         char buf[700];
375         unsigned int len = 0;
376         unsigned int i;
377         unsigned int v;
378
379         len += snprintf(buf+len, sizeof(buf)-len, "antenna mode\t%d\n",
380                 sc->ah->ah_ant_mode);
381         len += snprintf(buf+len, sizeof(buf)-len, "default antenna\t%d\n",
382                 sc->ah->ah_def_ant);
383         len += snprintf(buf+len, sizeof(buf)-len, "tx antenna\t%d\n",
384                 sc->ah->ah_tx_ant);
385
386         len += snprintf(buf+len, sizeof(buf)-len, "\nANTENNA\t\tRX\tTX\n");
387         for (i = 1; i < ARRAY_SIZE(sc->stats.antenna_rx); i++) {
388                 len += snprintf(buf+len, sizeof(buf)-len,
389                         "[antenna %d]\t%d\t%d\n",
390                         i, sc->stats.antenna_rx[i], sc->stats.antenna_tx[i]);
391         }
392         len += snprintf(buf+len, sizeof(buf)-len, "[invalid]\t%d\t%d\n",
393                         sc->stats.antenna_rx[0], sc->stats.antenna_tx[0]);
394
395         v = ath5k_hw_reg_read(sc->ah, AR5K_DEFAULT_ANTENNA);
396         len += snprintf(buf+len, sizeof(buf)-len,
397                         "\nAR5K_DEFAULT_ANTENNA\t0x%08x\n", v);
398
399         v = ath5k_hw_reg_read(sc->ah, AR5K_STA_ID1);
400         len += snprintf(buf+len, sizeof(buf)-len,
401                 "AR5K_STA_ID1_DEFAULT_ANTENNA\t%d\n",
402                 (v & AR5K_STA_ID1_DEFAULT_ANTENNA) != 0);
403         len += snprintf(buf+len, sizeof(buf)-len,
404                 "AR5K_STA_ID1_DESC_ANTENNA\t%d\n",
405                 (v & AR5K_STA_ID1_DESC_ANTENNA) != 0);
406         len += snprintf(buf+len, sizeof(buf)-len,
407                 "AR5K_STA_ID1_RTS_DEF_ANTENNA\t%d\n",
408                 (v & AR5K_STA_ID1_RTS_DEF_ANTENNA) != 0);
409         len += snprintf(buf+len, sizeof(buf)-len,
410                 "AR5K_STA_ID1_SELFGEN_DEF_ANT\t%d\n",
411                 (v & AR5K_STA_ID1_SELFGEN_DEF_ANT) != 0);
412
413         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_AGCCTL);
414         len += snprintf(buf+len, sizeof(buf)-len,
415                 "\nAR5K_PHY_AGCCTL_OFDM_DIV_DIS\t%d\n",
416                 (v & AR5K_PHY_AGCCTL_OFDM_DIV_DIS) != 0);
417
418         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_RESTART);
419         len += snprintf(buf+len, sizeof(buf)-len,
420                 "AR5K_PHY_RESTART_DIV_GC\t\t%x\n",
421                 (v & AR5K_PHY_RESTART_DIV_GC) >> AR5K_PHY_RESTART_DIV_GC_S);
422
423         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_FAST_ANT_DIV);
424         len += snprintf(buf+len, sizeof(buf)-len,
425                 "AR5K_PHY_FAST_ANT_DIV_EN\t%d\n",
426                 (v & AR5K_PHY_FAST_ANT_DIV_EN) != 0);
427
428         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_ANT_SWITCH_TABLE_0);
429         len += snprintf(buf+len, sizeof(buf)-len,
430                         "\nAR5K_PHY_ANT_SWITCH_TABLE_0\t0x%08x\n", v);
431         v = ath5k_hw_reg_read(sc->ah, AR5K_PHY_ANT_SWITCH_TABLE_1);
432         len += snprintf(buf+len, sizeof(buf)-len,
433                         "AR5K_PHY_ANT_SWITCH_TABLE_1\t0x%08x\n", v);
434
435         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
436 }
437
438 static ssize_t write_file_antenna(struct file *file,
439                                  const char __user *userbuf,
440                                  size_t count, loff_t *ppos)
441 {
442         struct ath5k_softc *sc = file->private_data;
443         unsigned int i;
444         char buf[20];
445
446         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
447                 return -EFAULT;
448
449         if (strncmp(buf, "diversity", 9) == 0) {
450                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
451                 printk(KERN_INFO "ath5k debug: enable diversity\n");
452         } else if (strncmp(buf, "fixed-a", 7) == 0) {
453                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
454                 printk(KERN_INFO "ath5k debugfs: fixed antenna A\n");
455         } else if (strncmp(buf, "fixed-b", 7) == 0) {
456                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
457                 printk(KERN_INFO "ath5k debug: fixed antenna B\n");
458         } else if (strncmp(buf, "clear", 5) == 0) {
459                 for (i = 0; i < ARRAY_SIZE(sc->stats.antenna_rx); i++) {
460                         sc->stats.antenna_rx[i] = 0;
461                         sc->stats.antenna_tx[i] = 0;
462                 }
463                 printk(KERN_INFO "ath5k debug: cleared antenna stats\n");
464         }
465         return count;
466 }
467
468 static const struct file_operations fops_antenna = {
469         .read = read_file_antenna,
470         .write = write_file_antenna,
471         .open = ath5k_debugfs_open,
472         .owner = THIS_MODULE,
473 };
474
475
476 /* debugfs: frameerrors */
477
478 static ssize_t read_file_frameerrors(struct file *file, char __user *user_buf,
479                                    size_t count, loff_t *ppos)
480 {
481         struct ath5k_softc *sc = file->private_data;
482         struct ath5k_statistics *st = &sc->stats;
483         char buf[700];
484         unsigned int len = 0;
485         int i;
486
487         len += snprintf(buf+len, sizeof(buf)-len,
488                         "RX\n---------------------\n");
489         len += snprintf(buf+len, sizeof(buf)-len, "CRC\t%d\t(%d%%)\n",
490                         st->rxerr_crc,
491                         st->rx_all_count > 0 ?
492                                 st->rxerr_crc*100/st->rx_all_count : 0);
493         len += snprintf(buf+len, sizeof(buf)-len, "PHY\t%d\t(%d%%)\n",
494                         st->rxerr_phy,
495                         st->rx_all_count > 0 ?
496                                 st->rxerr_phy*100/st->rx_all_count : 0);
497         for (i = 0; i < 32; i++) {
498                 if (st->rxerr_phy_code[i])
499                         len += snprintf(buf+len, sizeof(buf)-len,
500                                 " phy_err[%d]\t%d\n",
501                                 i, st->rxerr_phy_code[i]);
502         }
503
504         len += snprintf(buf+len, sizeof(buf)-len, "FIFO\t%d\t(%d%%)\n",
505                         st->rxerr_fifo,
506                         st->rx_all_count > 0 ?
507                                 st->rxerr_fifo*100/st->rx_all_count : 0);
508         len += snprintf(buf+len, sizeof(buf)-len, "decrypt\t%d\t(%d%%)\n",
509                         st->rxerr_decrypt,
510                         st->rx_all_count > 0 ?
511                                 st->rxerr_decrypt*100/st->rx_all_count : 0);
512         len += snprintf(buf+len, sizeof(buf)-len, "MIC\t%d\t(%d%%)\n",
513                         st->rxerr_mic,
514                         st->rx_all_count > 0 ?
515                                 st->rxerr_mic*100/st->rx_all_count : 0);
516         len += snprintf(buf+len, sizeof(buf)-len, "process\t%d\t(%d%%)\n",
517                         st->rxerr_proc,
518                         st->rx_all_count > 0 ?
519                                 st->rxerr_proc*100/st->rx_all_count : 0);
520         len += snprintf(buf+len, sizeof(buf)-len, "jumbo\t%d\t(%d%%)\n",
521                         st->rxerr_jumbo,
522                         st->rx_all_count > 0 ?
523                                 st->rxerr_jumbo*100/st->rx_all_count : 0);
524         len += snprintf(buf+len, sizeof(buf)-len, "[RX all\t%d]\n",
525                         st->rx_all_count);
526
527         len += snprintf(buf+len, sizeof(buf)-len,
528                         "\nTX\n---------------------\n");
529         len += snprintf(buf+len, sizeof(buf)-len, "retry\t%d\t(%d%%)\n",
530                         st->txerr_retry,
531                         st->tx_all_count > 0 ?
532                                 st->txerr_retry*100/st->tx_all_count : 0);
533         len += snprintf(buf+len, sizeof(buf)-len, "FIFO\t%d\t(%d%%)\n",
534                         st->txerr_fifo,
535                         st->tx_all_count > 0 ?
536                                 st->txerr_fifo*100/st->tx_all_count : 0);
537         len += snprintf(buf+len, sizeof(buf)-len, "filter\t%d\t(%d%%)\n",
538                         st->txerr_filt,
539                         st->tx_all_count > 0 ?
540                                 st->txerr_filt*100/st->tx_all_count : 0);
541         len += snprintf(buf+len, sizeof(buf)-len, "[TX all\t%d]\n",
542                         st->tx_all_count);
543
544         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
545 }
546
547 static ssize_t write_file_frameerrors(struct file *file,
548                                  const char __user *userbuf,
549                                  size_t count, loff_t *ppos)
550 {
551         struct ath5k_softc *sc = file->private_data;
552         struct ath5k_statistics *st = &sc->stats;
553         char buf[20];
554
555         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
556                 return -EFAULT;
557
558         if (strncmp(buf, "clear", 5) == 0) {
559                 st->rxerr_crc = 0;
560                 st->rxerr_phy = 0;
561                 st->rxerr_fifo = 0;
562                 st->rxerr_decrypt = 0;
563                 st->rxerr_mic = 0;
564                 st->rxerr_proc = 0;
565                 st->rxerr_jumbo = 0;
566                 st->rx_all_count = 0;
567                 st->txerr_retry = 0;
568                 st->txerr_fifo = 0;
569                 st->txerr_filt = 0;
570                 st->tx_all_count = 0;
571                 printk(KERN_INFO "ath5k debug: cleared frameerrors stats\n");
572         }
573         return count;
574 }
575
576 static const struct file_operations fops_frameerrors = {
577         .read = read_file_frameerrors,
578         .write = write_file_frameerrors,
579         .open = ath5k_debugfs_open,
580         .owner = THIS_MODULE,
581 };
582
583
584 /* debugfs: ani */
585
586 static ssize_t read_file_ani(struct file *file, char __user *user_buf,
587                                    size_t count, loff_t *ppos)
588 {
589         struct ath5k_softc *sc = file->private_data;
590         struct ath5k_statistics *st = &sc->stats;
591         struct ath5k_ani_state *as = &sc->ani_state;
592
593         char buf[700];
594         unsigned int len = 0;
595
596         len += snprintf(buf+len, sizeof(buf)-len,
597                         "HW has PHY error counters:\t%s\n",
598                         sc->ah->ah_capabilities.cap_has_phyerr_counters ?
599                         "yes" : "no");
600         len += snprintf(buf+len, sizeof(buf)-len,
601                         "HW max spur immunity level:\t%d\n",
602                         as->max_spur_level);
603         len += snprintf(buf+len, sizeof(buf)-len,
604                 "\nANI state\n--------------------------------------------\n");
605         len += snprintf(buf+len, sizeof(buf)-len, "operating mode:\t\t\t");
606         switch (as->ani_mode) {
607         case ATH5K_ANI_MODE_OFF:
608                 len += snprintf(buf+len, sizeof(buf)-len, "OFF\n");
609                 break;
610         case ATH5K_ANI_MODE_MANUAL_LOW:
611                 len += snprintf(buf+len, sizeof(buf)-len,
612                         "MANUAL LOW\n");
613                 break;
614         case ATH5K_ANI_MODE_MANUAL_HIGH:
615                 len += snprintf(buf+len, sizeof(buf)-len,
616                         "MANUAL HIGH\n");
617                 break;
618         case ATH5K_ANI_MODE_AUTO:
619                 len += snprintf(buf+len, sizeof(buf)-len, "AUTO\n");
620                 break;
621         default:
622                 len += snprintf(buf+len, sizeof(buf)-len,
623                         "??? (not good)\n");
624                 break;
625         }
626         len += snprintf(buf+len, sizeof(buf)-len,
627                         "noise immunity level:\t\t%d\n",
628                         as->noise_imm_level);
629         len += snprintf(buf+len, sizeof(buf)-len,
630                         "spur immunity level:\t\t%d\n",
631                         as->spur_level);
632         len += snprintf(buf+len, sizeof(buf)-len, "firstep level:\t\t\t%d\n",
633                         as->firstep_level);
634         len += snprintf(buf+len, sizeof(buf)-len,
635                         "OFDM weak signal detection:\t%s\n",
636                         as->ofdm_weak_sig ? "on" : "off");
637         len += snprintf(buf+len, sizeof(buf)-len,
638                         "CCK weak signal detection:\t%s\n",
639                         as->cck_weak_sig ? "on" : "off");
640
641         len += snprintf(buf+len, sizeof(buf)-len,
642                         "\nMIB INTERRUPTS:\t\t%u\n",
643                         st->mib_intr);
644         len += snprintf(buf+len, sizeof(buf)-len,
645                         "beacon RSSI average:\t%d\n",
646                         sc->ah->ah_beacon_rssi_avg.avg);
647         len += snprintf(buf+len, sizeof(buf)-len, "profcnt tx\t\t%u\t(%d%%)\n",
648                         as->pfc_tx,
649                         as->pfc_cycles > 0 ?
650                         as->pfc_tx*100/as->pfc_cycles : 0);
651         len += snprintf(buf+len, sizeof(buf)-len, "profcnt rx\t\t%u\t(%d%%)\n",
652                         as->pfc_rx,
653                         as->pfc_cycles > 0 ?
654                         as->pfc_rx*100/as->pfc_cycles : 0);
655         len += snprintf(buf+len, sizeof(buf)-len, "profcnt busy\t\t%u\t(%d%%)\n",
656                         as->pfc_busy,
657                         as->pfc_cycles > 0 ?
658                         as->pfc_busy*100/as->pfc_cycles : 0);
659         len += snprintf(buf+len, sizeof(buf)-len, "profcnt cycles\t\t%u\n",
660                         as->pfc_cycles);
661         len += snprintf(buf+len, sizeof(buf)-len,
662                         "listen time\t\t%d\tlast: %d\n",
663                         as->listen_time, as->last_listen);
664         len += snprintf(buf+len, sizeof(buf)-len,
665                         "OFDM errors\t\t%u\tlast: %u\tsum: %u\n",
666                         as->ofdm_errors, as->last_ofdm_errors,
667                         as->sum_ofdm_errors);
668         len += snprintf(buf+len, sizeof(buf)-len,
669                         "CCK errors\t\t%u\tlast: %u\tsum: %u\n",
670                         as->cck_errors, as->last_cck_errors,
671                         as->sum_cck_errors);
672         len += snprintf(buf+len, sizeof(buf)-len,
673                         "AR5K_PHYERR_CNT1\t%x\t(=%d)\n",
674                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT1),
675                         ATH5K_ANI_OFDM_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX -
676                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT1)));
677         len += snprintf(buf+len, sizeof(buf)-len,
678                         "AR5K_PHYERR_CNT2\t%x\t(=%d)\n",
679                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT2),
680                         ATH5K_ANI_CCK_TRIG_HIGH - (ATH5K_PHYERR_CNT_MAX -
681                         ath5k_hw_reg_read(sc->ah, AR5K_PHYERR_CNT2)));
682
683         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
684 }
685
686 static ssize_t write_file_ani(struct file *file,
687                                  const char __user *userbuf,
688                                  size_t count, loff_t *ppos)
689 {
690         struct ath5k_softc *sc = file->private_data;
691         char buf[20];
692
693         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
694                 return -EFAULT;
695
696         if (strncmp(buf, "sens-low", 8) == 0) {
697                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_MANUAL_HIGH);
698         } else if (strncmp(buf, "sens-high", 9) == 0) {
699                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_MANUAL_LOW);
700         } else if (strncmp(buf, "ani-off", 7) == 0) {
701                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_OFF);
702         } else if (strncmp(buf, "ani-on", 6) == 0) {
703                 ath5k_ani_init(sc->ah, ATH5K_ANI_MODE_AUTO);
704         } else if (strncmp(buf, "noise-low", 9) == 0) {
705                 ath5k_ani_set_noise_immunity_level(sc->ah, 0);
706         } else if (strncmp(buf, "noise-high", 10) == 0) {
707                 ath5k_ani_set_noise_immunity_level(sc->ah,
708                                                    ATH5K_ANI_MAX_NOISE_IMM_LVL);
709         } else if (strncmp(buf, "spur-low", 8) == 0) {
710                 ath5k_ani_set_spur_immunity_level(sc->ah, 0);
711         } else if (strncmp(buf, "spur-high", 9) == 0) {
712                 ath5k_ani_set_spur_immunity_level(sc->ah,
713                                                   sc->ani_state.max_spur_level);
714         } else if (strncmp(buf, "fir-low", 7) == 0) {
715                 ath5k_ani_set_firstep_level(sc->ah, 0);
716         } else if (strncmp(buf, "fir-high", 8) == 0) {
717                 ath5k_ani_set_firstep_level(sc->ah, ATH5K_ANI_MAX_FIRSTEP_LVL);
718         } else if (strncmp(buf, "ofdm-off", 8) == 0) {
719                 ath5k_ani_set_ofdm_weak_signal_detection(sc->ah, false);
720         } else if (strncmp(buf, "ofdm-on", 7) == 0) {
721                 ath5k_ani_set_ofdm_weak_signal_detection(sc->ah, true);
722         } else if (strncmp(buf, "cck-off", 7) == 0) {
723                 ath5k_ani_set_cck_weak_signal_detection(sc->ah, false);
724         } else if (strncmp(buf, "cck-on", 6) == 0) {
725                 ath5k_ani_set_cck_weak_signal_detection(sc->ah, true);
726         }
727         return count;
728 }
729
730 static const struct file_operations fops_ani = {
731         .read = read_file_ani,
732         .write = write_file_ani,
733         .open = ath5k_debugfs_open,
734         .owner = THIS_MODULE,
735 };
736
737
738 /* debugfs: queues etc */
739
740 static ssize_t read_file_queue(struct file *file, char __user *user_buf,
741                                    size_t count, loff_t *ppos)
742 {
743         struct ath5k_softc *sc = file->private_data;
744         char buf[700];
745         unsigned int len = 0;
746
747         struct ath5k_txq *txq;
748         struct ath5k_buf *bf, *bf0;
749         int i, n = 0;
750
751         len += snprintf(buf+len, sizeof(buf)-len,
752                         "available txbuffers: %d\n", sc->txbuf_len);
753
754         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
755                 txq = &sc->txqs[i];
756
757                 len += snprintf(buf+len, sizeof(buf)-len,
758                         "%02d: %ssetup\n", i, txq->setup ? "" : "not ");
759
760                 if (!txq->setup)
761                         continue;
762
763                 list_for_each_entry_safe(bf, bf0, &txq->q, list)
764                         n++;
765                 len += snprintf(buf+len, sizeof(buf)-len, "  len: %d\n", n);
766         }
767
768         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
769 }
770
771 static ssize_t write_file_queue(struct file *file,
772                                  const char __user *userbuf,
773                                  size_t count, loff_t *ppos)
774 {
775         struct ath5k_softc *sc = file->private_data;
776         char buf[20];
777
778         if (copy_from_user(buf, userbuf, min(count, sizeof(buf))))
779                 return -EFAULT;
780
781         if (strncmp(buf, "start", 5) == 0)
782                 ieee80211_wake_queues(sc->hw);
783         else if (strncmp(buf, "stop", 4) == 0)
784                 ieee80211_stop_queues(sc->hw);
785
786         return count;
787 }
788
789
790 static const struct file_operations fops_queue = {
791         .read = read_file_queue,
792         .write = write_file_queue,
793         .open = ath5k_debugfs_open,
794         .owner = THIS_MODULE,
795 };
796
797
798 /* init */
799
800 void
801 ath5k_debug_init(void)
802 {
803         ath5k_global_debugfs = debugfs_create_dir("ath5k", NULL);
804 }
805
806 void
807 ath5k_debug_init_device(struct ath5k_softc *sc)
808 {
809         sc->debug.level = ath5k_debug;
810
811         sc->debug.debugfs_phydir = debugfs_create_dir(wiphy_name(sc->hw->wiphy),
812                                 ath5k_global_debugfs);
813
814         sc->debug.debugfs_debug = debugfs_create_file("debug",
815                                 S_IWUSR | S_IRUSR,
816                                 sc->debug.debugfs_phydir, sc, &fops_debug);
817
818         sc->debug.debugfs_registers = debugfs_create_file("registers", S_IRUSR,
819                                 sc->debug.debugfs_phydir, sc, &fops_registers);
820
821         sc->debug.debugfs_beacon = debugfs_create_file("beacon",
822                                 S_IWUSR | S_IRUSR,
823                                 sc->debug.debugfs_phydir, sc, &fops_beacon);
824
825         sc->debug.debugfs_reset = debugfs_create_file("reset", S_IWUSR,
826                                 sc->debug.debugfs_phydir, sc, &fops_reset);
827
828         sc->debug.debugfs_antenna = debugfs_create_file("antenna",
829                                 S_IWUSR | S_IRUSR,
830                                 sc->debug.debugfs_phydir, sc, &fops_antenna);
831
832         sc->debug.debugfs_frameerrors = debugfs_create_file("frameerrors",
833                                 S_IWUSR | S_IRUSR,
834                                 sc->debug.debugfs_phydir, sc,
835                                 &fops_frameerrors);
836
837         sc->debug.debugfs_ani = debugfs_create_file("ani",
838                                 S_IWUSR | S_IRUSR,
839                                 sc->debug.debugfs_phydir, sc,
840                                 &fops_ani);
841
842         sc->debug.debugfs_queue = debugfs_create_file("queue",
843                                 S_IWUSR | S_IRUSR,
844                                 sc->debug.debugfs_phydir, sc,
845                                 &fops_queue);
846 }
847
848 void
849 ath5k_debug_finish(void)
850 {
851         debugfs_remove(ath5k_global_debugfs);
852 }
853
854 void
855 ath5k_debug_finish_device(struct ath5k_softc *sc)
856 {
857         debugfs_remove(sc->debug.debugfs_debug);
858         debugfs_remove(sc->debug.debugfs_registers);
859         debugfs_remove(sc->debug.debugfs_beacon);
860         debugfs_remove(sc->debug.debugfs_reset);
861         debugfs_remove(sc->debug.debugfs_antenna);
862         debugfs_remove(sc->debug.debugfs_frameerrors);
863         debugfs_remove(sc->debug.debugfs_ani);
864         debugfs_remove(sc->debug.debugfs_queue);
865         debugfs_remove(sc->debug.debugfs_phydir);
866 }
867
868
869 /* functions used in other places */
870
871 void
872 ath5k_debug_dump_bands(struct ath5k_softc *sc)
873 {
874         unsigned int b, i;
875
876         if (likely(!(sc->debug.level & ATH5K_DEBUG_DUMPBANDS)))
877                 return;
878
879         BUG_ON(!sc->sbands);
880
881         for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
882                 struct ieee80211_supported_band *band = &sc->sbands[b];
883                 char bname[6];
884                 switch (band->band) {
885                 case IEEE80211_BAND_2GHZ:
886                         strcpy(bname, "2 GHz");
887                         break;
888                 case IEEE80211_BAND_5GHZ:
889                         strcpy(bname, "5 GHz");
890                         break;
891                 default:
892                         printk(KERN_DEBUG "Band not supported: %d\n",
893                                 band->band);
894                         return;
895                 }
896                 printk(KERN_DEBUG "Band %s: channels %d, rates %d\n", bname,
897                                 band->n_channels, band->n_bitrates);
898                 printk(KERN_DEBUG " channels:\n");
899                 for (i = 0; i < band->n_channels; i++)
900                         printk(KERN_DEBUG "  %3d %d %.4x %.4x\n",
901                                         ieee80211_frequency_to_channel(
902                                                 band->channels[i].center_freq),
903                                         band->channels[i].center_freq,
904                                         band->channels[i].hw_value,
905                                         band->channels[i].flags);
906                 printk(KERN_DEBUG " rates:\n");
907                 for (i = 0; i < band->n_bitrates; i++)
908                         printk(KERN_DEBUG "  %4d %.4x %.4x %.4x\n",
909                                         band->bitrates[i].bitrate,
910                                         band->bitrates[i].hw_value,
911                                         band->bitrates[i].flags,
912                                         band->bitrates[i].hw_value_short);
913         }
914 }
915
916 static inline void
917 ath5k_debug_printrxbuf(struct ath5k_buf *bf, int done,
918                        struct ath5k_rx_status *rs)
919 {
920         struct ath5k_desc *ds = bf->desc;
921         struct ath5k_hw_all_rx_desc *rd = &ds->ud.ds_rx;
922
923         printk(KERN_DEBUG "R (%p %llx) %08x %08x %08x %08x %08x %08x %c\n",
924                 ds, (unsigned long long)bf->daddr,
925                 ds->ds_link, ds->ds_data,
926                 rd->rx_ctl.rx_control_0, rd->rx_ctl.rx_control_1,
927                 rd->u.rx_stat.rx_status_0, rd->u.rx_stat.rx_status_0,
928                 !done ? ' ' : (rs->rs_status == 0) ? '*' : '!');
929 }
930
931 void
932 ath5k_debug_printrxbuffs(struct ath5k_softc *sc, struct ath5k_hw *ah)
933 {
934         struct ath5k_desc *ds;
935         struct ath5k_buf *bf;
936         struct ath5k_rx_status rs = {};
937         int status;
938
939         if (likely(!(sc->debug.level & ATH5K_DEBUG_RESET)))
940                 return;
941
942         printk(KERN_DEBUG "rx queue %x, link %p\n",
943                 ath5k_hw_get_rxdp(ah), sc->rxlink);
944
945         spin_lock_bh(&sc->rxbuflock);
946         list_for_each_entry(bf, &sc->rxbuf, list) {
947                 ds = bf->desc;
948                 status = ah->ah_proc_rx_desc(ah, ds, &rs);
949                 if (!status)
950                         ath5k_debug_printrxbuf(bf, status == 0, &rs);
951         }
952         spin_unlock_bh(&sc->rxbuflock);
953 }
954
955 void
956 ath5k_debug_dump_skb(struct ath5k_softc *sc,
957                         struct sk_buff *skb, const char *prefix, int tx)
958 {
959         char buf[16];
960
961         if (likely(!((tx && (sc->debug.level & ATH5K_DEBUG_DUMP_TX)) ||
962                      (!tx && (sc->debug.level & ATH5K_DEBUG_DUMP_RX)))))
963                 return;
964
965         snprintf(buf, sizeof(buf), "%s %s", wiphy_name(sc->hw->wiphy), prefix);
966
967         print_hex_dump_bytes(buf, DUMP_PREFIX_NONE, skb->data,
968                 min(200U, skb->len));
969
970         printk(KERN_DEBUG "\n");
971 }
972
973 void
974 ath5k_debug_printtxbuf(struct ath5k_softc *sc, struct ath5k_buf *bf)
975 {
976         struct ath5k_desc *ds = bf->desc;
977         struct ath5k_hw_5212_tx_desc *td = &ds->ud.ds_tx5212;
978         struct ath5k_tx_status ts = {};
979         int done;
980
981         if (likely(!(sc->debug.level & ATH5K_DEBUG_RESET)))
982                 return;
983
984         done = sc->ah->ah_proc_tx_desc(sc->ah, bf->desc, &ts);
985
986         printk(KERN_DEBUG "T (%p %llx) %08x %08x %08x %08x %08x %08x %08x "
987                 "%08x %c\n", ds, (unsigned long long)bf->daddr, ds->ds_link,
988                 ds->ds_data, td->tx_ctl.tx_control_0, td->tx_ctl.tx_control_1,
989                 td->tx_ctl.tx_control_2, td->tx_ctl.tx_control_3,
990                 td->tx_stat.tx_status_0, td->tx_stat.tx_status_1,
991                 done ? ' ' : (ts.ts_status == 0) ? '*' : '!');
992 }
993
994 #endif /* ifdef CONFIG_ATH5K_DEBUG */