Bring in FreeBSD's tools/tools/{ath,net80211}.
[dragonfly.git] / tools / tools / ath / athdebug / athdebug.c
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD: src/tools/tools/ath/athdebug/athdebug.c,v 1.6 2009/01/08 17:12:47 sam Exp $
30  */
31
32 /*
33  * athdebug [-i interface] flags
34  * (default interface is ath0).
35  */
36 #include <sys/types.h>
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <getopt.h>
44 #include <stdlib.h>
45
46 #define N(a)    (sizeof(a)/sizeof(a[0]))
47
48 const char *progname;
49
50 enum {
51         ATH_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
52         ATH_DEBUG_XMIT_DESC     = 0x00000002,   /* xmit descriptors */
53         ATH_DEBUG_RECV          = 0x00000004,   /* basic recv operation */
54         ATH_DEBUG_RECV_DESC     = 0x00000008,   /* recv descriptors */
55         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
56         ATH_DEBUG_RESET         = 0x00000020,   /* reset processing */
57         ATH_DEBUG_MODE          = 0x00000040,   /* mode init/setup */
58         ATH_DEBUG_BEACON        = 0x00000080,   /* beacon handling */
59         ATH_DEBUG_WATCHDOG      = 0x00000100,   /* watchdog timeout */
60         ATH_DEBUG_INTR          = 0x00001000,   /* ISR */
61         ATH_DEBUG_TX_PROC       = 0x00002000,   /* tx ISR proc */
62         ATH_DEBUG_RX_PROC       = 0x00004000,   /* rx ISR proc */
63         ATH_DEBUG_BEACON_PROC   = 0x00008000,   /* beacon ISR proc */
64         ATH_DEBUG_CALIBRATE     = 0x00010000,   /* periodic calibration */
65         ATH_DEBUG_KEYCACHE      = 0x00020000,   /* key cache management */
66         ATH_DEBUG_STATE         = 0x00040000,   /* 802.11 state transitions */
67         ATH_DEBUG_NODE          = 0x00080000,   /* node management */
68         ATH_DEBUG_LED           = 0x00100000,   /* led management */
69         ATH_DEBUG_FF            = 0x00200000,   /* fast frames */
70         ATH_DEBUG_DFS           = 0x00400000,   /* DFS processing */
71         ATH_DEBUG_TDMA          = 0x00800000,   /* TDMA processing */
72         ATH_DEBUG_TDMA_TIMER    = 0x01000000,   /* TDMA timer processing */
73         ATH_DEBUG_REGDOMAIN     = 0x02000000,   /* regulatory processing */
74         ATH_DEBUG_FATAL         = 0x80000000,   /* fatal errors */
75         ATH_DEBUG_ANY           = 0xffffffff
76 };
77
78 static struct {
79         const char      *name;
80         u_int           bit;
81 } flags[] = {
82         { "xmit",       ATH_DEBUG_XMIT },
83         { "xmit_desc",  ATH_DEBUG_XMIT_DESC },
84         { "recv",       ATH_DEBUG_RECV },
85         { "recv_desc",  ATH_DEBUG_RECV_DESC },
86         { "rate",       ATH_DEBUG_RATE },
87         { "reset",      ATH_DEBUG_RESET },
88         { "mode",       ATH_DEBUG_MODE },
89         { "beacon",     ATH_DEBUG_BEACON },
90         { "watchdog",   ATH_DEBUG_WATCHDOG },
91         { "intr",       ATH_DEBUG_INTR },
92         { "xmit_proc",  ATH_DEBUG_TX_PROC },
93         { "recv_proc",  ATH_DEBUG_RX_PROC },
94         { "beacon_proc",ATH_DEBUG_BEACON_PROC },
95         { "calibrate",  ATH_DEBUG_CALIBRATE },
96         { "keycache",   ATH_DEBUG_KEYCACHE },
97         { "state",      ATH_DEBUG_STATE },
98         { "node",       ATH_DEBUG_NODE },
99         { "led",        ATH_DEBUG_LED },
100         { "ff",         ATH_DEBUG_FF },
101         { "dfs",        ATH_DEBUG_DFS },
102         { "tdma",       ATH_DEBUG_TDMA },
103         { "tdma_timer", ATH_DEBUG_TDMA_TIMER },
104         { "regdomain",  ATH_DEBUG_REGDOMAIN },
105         { "fatal",      ATH_DEBUG_FATAL },
106 };
107
108 static u_int
109 getflag(const char *name, int len)
110 {
111         int i;
112
113         for (i = 0; i < N(flags); i++)
114                 if (strncasecmp(flags[i].name, name, len) == 0)
115                         return flags[i].bit;
116         return 0;
117 }
118
119 static const char *
120 getflagname(u_int flag)
121 {
122         int i;
123
124         for (i = 0; i < N(flags); i++)
125                 if (flags[i].bit == flag)
126                         return flags[i].name;
127         return "???";
128 }
129
130 static void
131 usage(void)
132 {
133         int i;
134
135         fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
136         fprintf(stderr, "where flags are:\n");
137         for (i = 0; i < N(flags); i++)
138                 printf("%s\n", flags[i].name);
139         exit(-1);
140 }
141
142 int
143 main(int argc, char *argv[])
144 {
145         const char *ifname;
146         const char *cp, *tp;
147         const char *sep;
148         int c, op, i;
149         u_int32_t debug, ndebug;
150         size_t debuglen;
151         char oid[256];
152
153         ifname = getenv("ATH");
154         if (ifname == NULL)
155                 ifname = "ath0";
156         progname = argv[0];
157         if (argc > 1) {
158                 if (strcmp(argv[1], "-i") == 0) {
159                         if (argc < 2)
160                                 errx(1, "missing interface name for -i option");
161                         ifname = argv[2];
162                         if (strncmp(ifname, "ath", 3) != 0)
163                                 errx(2, "huh, this is for ath devices?");
164                         argc -= 2, argv += 2;
165                 } else if (strcmp(argv[1], "-?") == 0)
166                         usage();
167         }
168
169 #ifdef __linux__
170         snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
171 #else
172         snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
173 #endif
174         debuglen = sizeof(debug);
175         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
176                 err(1, "sysctl-get(%s)", oid);
177         ndebug = debug;
178         for (; argc > 1; argc--, argv++) {
179                 cp = argv[1];
180                 do {
181                         u_int bit;
182
183                         if (*cp == '-') {
184                                 cp++;
185                                 op = -1;
186                         } else if (*cp == '+') {
187                                 cp++;
188                                 op = 1;
189                         } else
190                                 op = 0;
191                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
192                                 tp++;
193                         bit = getflag(cp, tp-cp);
194                         if (op < 0)
195                                 ndebug &= ~bit;
196                         else if (op > 0)
197                                 ndebug |= bit;
198                         else {
199                                 if (bit == 0) {
200                                         if (isdigit(*cp))
201                                                 bit = strtoul(cp, NULL, 0);
202                                         else
203                                                 errx(1, "unknown flag %.*s",
204                                                         tp-cp, cp);
205                                 }
206                                 ndebug = bit;
207                         }
208                 } while (*(cp = tp) != '\0');
209         }
210         if (debug != ndebug) {
211                 printf("%s: 0x%x => ", oid, debug);
212                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
213                         err(1, "sysctl-set(%s)", oid);
214                 printf("0x%x", ndebug);
215                 debug = ndebug;
216         } else
217                 printf("%s: 0x%x", oid, debug);
218         sep = "<";
219         for (i = 0; i < N(flags); i++)
220                 if (debug & flags[i].bit) {
221                         printf("%s%s", sep, flags[i].name);
222                         sep = ",";
223                 }
224         printf("%s\n", *sep != '<' ? ">" : "");
225         return 0;
226 }