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_LED		= 0x00100000,	/* led management */
75 	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
76 	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
77 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
78 	ATH_DEBUG_ANY		= 0xffffffff
79 };
80 
81 static struct {
82 	const char	*name;
83 	u_int		bit;
84 } flags[] = {
85 	{ "xmit",	ATH_DEBUG_XMIT },
86 	{ "xmit_desc",	ATH_DEBUG_XMIT_DESC },
87 	{ "recv",	ATH_DEBUG_RECV },
88 	{ "recv_desc",	ATH_DEBUG_RECV_DESC },
89 	{ "rate",	ATH_DEBUG_RATE },
90 	{ "reset",	ATH_DEBUG_RESET },
91 	{ "mode",	ATH_DEBUG_MODE },
92 	{ "beacon",	ATH_DEBUG_BEACON },
93 	{ "watchdog",	ATH_DEBUG_WATCHDOG },
94 	{ "intr",	ATH_DEBUG_INTR },
95 	{ "xmit_proc",	ATH_DEBUG_TX_PROC },
96 	{ "recv_proc",	ATH_DEBUG_RX_PROC },
97 	{ "beacon_proc",ATH_DEBUG_BEACON_PROC },
98 	{ "calibrate",	ATH_DEBUG_CALIBRATE },
99 	{ "keycache",	ATH_DEBUG_KEYCACHE },
100 	{ "state",	ATH_DEBUG_STATE },
101 	{ "node",	ATH_DEBUG_NODE },
102 	{ "led",	ATH_DEBUG_LED },
103 	{ "ff",		ATH_DEBUG_FF },
104 	{ "dfs",	ATH_DEBUG_DFS },
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 = "ath0";
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 	progname = argv[0];
154 	if (argc > 1) {
155 		if (strcmp(argv[1], "-i") == 0) {
156 			if (argc < 2)
157 				errx(1, "missing interface name for -i option");
158 			ifname = argv[2];
159 			if (strncmp(ifname, "ath", 3) != 0)
160 				errx(2, "huh, this is for ath devices?");
161 			argc -= 2, argv += 2;
162 		} else if (strcmp(argv[1], "-?") == 0)
163 			usage();
164 	}
165 
166 #ifdef __linux__
167 	snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
168 #else
169 	snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
170 #endif
171 	debuglen = sizeof(debug);
172 	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
173 		err(1, "sysctl-get(%s)", oid);
174 	ndebug = debug;
175 	for (; argc > 1; argc--, argv++) {
176 		cp = argv[1];
177 		do {
178 			u_int bit;
179 
180 			if (*cp == '-') {
181 				cp++;
182 				op = -1;
183 			} else if (*cp == '+') {
184 				cp++;
185 				op = 1;
186 			} else
187 				op = 0;
188 			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
189 				tp++;
190 			bit = getflag(cp, tp-cp);
191 			if (op < 0)
192 				ndebug &= ~bit;
193 			else if (op > 0)
194 				ndebug |= bit;
195 			else {
196 				if (bit == 0) {
197 					if (isdigit(*cp))
198 						bit = strtoul(cp, NULL, 0);
199 					else
200 						errx(1, "unknown flag %.*s",
201 							tp-cp, cp);
202 				}
203 				ndebug = bit;
204 			}
205 		} while (*(cp = tp) != '\0');
206 	}
207 	if (debug != ndebug) {
208 		printf("%s: 0x%x => ", oid, debug);
209 		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
210 			err(1, "sysctl-set(%s)", oid);
211 		printf("0x%x", ndebug);
212 		debug = ndebug;
213 	} else
214 		printf("%s: 0x%x", oid, debug);
215 	sep = "<";
216 	for (i = 0; i < N(flags); i++)
217 		if (debug & flags[i].bit) {
218 			printf("%s%s", sep, flags[i].name);
219 			sep = ",";
220 		}
221 	printf("%s\n", *sep != '<' ? ">" : "");
222 	return 0;
223 }
224