]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/tools/tools/ath/athdebug/athdebug.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / tools / tools / ath / athdebug / athdebug.c
1 /*-
2  * Copyright (c) 2002-2004 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  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD$
37  */
38
39 /*
40  * athdebug [-i interface] flags
41  * (default interface is ath0).
42  */
43 #include <sys/types.h>
44 #include <sys/file.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <getopt.h>
51
52 #define N(a)    (sizeof(a)/sizeof(a[0]))
53
54 const char *progname;
55
56 enum {
57         ATH_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
58         ATH_DEBUG_XMIT_DESC     = 0x00000002,   /* xmit descriptors */
59         ATH_DEBUG_RECV          = 0x00000004,   /* basic recv operation */
60         ATH_DEBUG_RECV_DESC     = 0x00000008,   /* recv descriptors */
61         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
62         ATH_DEBUG_RESET         = 0x00000020,   /* reset processing */
63         ATH_DEBUG_MODE          = 0x00000040,   /* mode init/setup */
64         ATH_DEBUG_BEACON        = 0x00000080,   /* beacon handling */
65         ATH_DEBUG_WATCHDOG      = 0x00000100,   /* watchdog timeout */
66         ATH_DEBUG_INTR          = 0x00001000,   /* ISR */
67         ATH_DEBUG_TX_PROC       = 0x00002000,   /* tx ISR proc */
68         ATH_DEBUG_RX_PROC       = 0x00004000,   /* rx ISR proc */
69         ATH_DEBUG_BEACON_PROC   = 0x00008000,   /* beacon ISR proc */
70         ATH_DEBUG_CALIBRATE     = 0x00010000,   /* periodic calibration */
71         ATH_DEBUG_KEYCACHE      = 0x00020000,   /* key cache management */
72         ATH_DEBUG_STATE         = 0x00040000,   /* 802.11 state transitions */
73         ATH_DEBUG_NODE          = 0x00080000,   /* node management */
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         { "fatal",      ATH_DEBUG_FATAL },
100 };
101
102 static u_int
103 getflag(const char *name, int len)
104 {
105         int i;
106
107         for (i = 0; i < N(flags); i++)
108                 if (strncasecmp(flags[i].name, name, len) == 0)
109                         return flags[i].bit;
110         return 0;
111 }
112
113 static const char *
114 getflagname(u_int flag)
115 {
116         int i;
117
118         for (i = 0; i < N(flags); i++)
119                 if (flags[i].bit == flag)
120                         return flags[i].name;
121         return "???";
122 }
123
124 static void
125 usage(void)
126 {
127         int i;
128
129         fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
130         fprintf(stderr, "where flags are:\n");
131         for (i = 0; i < N(flags); i++)
132                 printf("%s\n", flags[i].name);
133         exit(-1);
134 }
135
136 int
137 main(int argc, char *argv[])
138 {
139         const char *ifname = "ath0";
140         const char *cp, *tp;
141         const char *sep;
142         int c, op, i;
143         u_int32_t debug, ndebug;
144         size_t debuglen;
145         char oid[256];
146
147         progname = argv[0];
148         if (argc > 1) {
149                 if (strcmp(argv[1], "-i") == 0) {
150                         if (argc < 2)
151                                 errx(1, "missing interface name for -i option");
152                         ifname = argv[2];
153                         if (strncmp(ifname, "ath", 3) != 0)
154                                 errx(2, "huh, this is for ath devices?");
155                         argc -= 2, argv += 2;
156                 } else if (strcmp(argv[1], "-?") == 0)
157                         usage();
158         }
159
160 #ifdef __linux__
161         snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
162 #else
163         snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
164 #endif
165         debuglen = sizeof(debug);
166         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
167                 err(1, "sysctl-get(%s)", oid);
168         ndebug = debug;
169         for (; argc > 1; argc--, argv++) {
170                 cp = argv[1];
171                 do {
172                         u_int bit;
173
174                         if (*cp == '-') {
175                                 cp++;
176                                 op = -1;
177                         } else if (*cp == '+') {
178                                 cp++;
179                                 op = 1;
180                         } else
181                                 op = 0;
182                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
183                                 tp++;
184                         bit = getflag(cp, tp-cp);
185                         if (op < 0)
186                                 ndebug &= ~bit;
187                         else if (op > 0)
188                                 ndebug |= bit;
189                         else {
190                                 if (bit == 0) {
191                                         if (isdigit(*cp))
192                                                 bit = strtoul(cp, NULL, 0);
193                                         else
194                                                 errx(1, "unknown flag %.*s",
195                                                         tp-cp, cp);
196                                 }
197                                 ndebug = bit;
198                         }
199                 } while (*(cp = tp) != '\0');
200         }
201         if (debug != ndebug) {
202                 printf("%s: 0x%x => ", oid, debug);
203                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
204                         err(1, "sysctl-set(%s)", oid);
205                 printf("0x%x", ndebug);
206                 debug = ndebug;
207         } else
208                 printf("%s: 0x%x", oid, debug);
209         sep = "<";
210         for (i = 0; i < N(flags); i++)
211                 if (debug & flags[i].bit) {
212                         printf("%s%s", sep, flags[i].name);
213                         sep = ",";
214                 }
215         printf("%s\n", *sep != '<' ? ">" : "");
216         return 0;
217 }