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$
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 #include <sys/sysctl.h>
41 
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <getopt.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <err.h>
49 
50 #define	N(a)	(sizeof(a)/sizeof(a[0]))
51 
52 const char *progname;
53 
54 /* XXX TODO: include if_ath_debug.h */
55 enum {
56 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
57 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
58 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
59 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
60 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
61 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
62 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
63 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
64 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
65 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
66 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
67 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
68 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
69 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
70 	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
71 	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
72 	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
73 	ATH_DEBUG_LED		= 0x00100000,	/* led management */
74 	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
75 	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
76 	ATH_DEBUG_TDMA		= 0x00800000,	/* TDMA processing */
77 	ATH_DEBUG_TDMA_TIMER	= 0x01000000,	/* TDMA timer processing */
78 	ATH_DEBUG_REGDOMAIN	= 0x02000000,	/* regulatory processing */
79 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
80 	ATH_DEBUG_ANY		= 0xffffffff
81 };
82 
83 static struct {
84 	const char	*name;
85 	uint64_t	bit;
86 } flags[] = {
87 	{ "xmit",	ATH_DEBUG_XMIT },
88 	{ "xmit_desc",	ATH_DEBUG_XMIT_DESC },
89 	{ "recv",	ATH_DEBUG_RECV },
90 	{ "recv_desc",	ATH_DEBUG_RECV_DESC },
91 	{ "rate",	ATH_DEBUG_RATE },
92 	{ "reset",	ATH_DEBUG_RESET },
93 	{ "mode",	ATH_DEBUG_MODE },
94 	{ "beacon",	ATH_DEBUG_BEACON },
95 	{ "watchdog",	ATH_DEBUG_WATCHDOG },
96 	{ "intr",	ATH_DEBUG_INTR },
97 	{ "xmit_proc",	ATH_DEBUG_TX_PROC },
98 	{ "recv_proc",	ATH_DEBUG_RX_PROC },
99 	{ "beacon_proc",ATH_DEBUG_BEACON_PROC },
100 	{ "calibrate",	ATH_DEBUG_CALIBRATE },
101 	{ "keycache",	ATH_DEBUG_KEYCACHE },
102 	{ "state",	ATH_DEBUG_STATE },
103 	{ "node",	ATH_DEBUG_NODE },
104 	{ "led",	ATH_DEBUG_LED },
105 	{ "ff",		ATH_DEBUG_FF },
106 	{ "dfs",	ATH_DEBUG_DFS },
107 	{ "tdma",	ATH_DEBUG_TDMA },
108 	{ "tdma_timer",	ATH_DEBUG_TDMA_TIMER },
109 	{ "regdomain",	ATH_DEBUG_REGDOMAIN },
110 	{ "fatal",	ATH_DEBUG_FATAL },
111 };
112 
113 static uint64_t
114 getflag(const char *name, int len)
115 {
116 	int i;
117 
118 	for (i = 0; i < N(flags); i++)
119 		if (strncasecmp(flags[i].name, name, len) == 0)
120 			return flags[i].bit;
121 	return 0;
122 }
123 
124 static const char *
125 getflagname(u_int flag)
126 {
127 	int i;
128 
129 	for (i = 0; i < N(flags); i++)
130 		if (flags[i].bit == flag)
131 			return flags[i].name;
132 	return "???";
133 }
134 
135 static void
136 usage(void)
137 {
138 	int i;
139 
140 	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
141 	fprintf(stderr, "where flags are:\n");
142 	for (i = 0; i < N(flags); i++)
143 		printf("%s\n", flags[i].name);
144 	exit(-1);
145 }
146 
147 int
148 main(int argc, char *argv[])
149 {
150 	const char *ifname;
151 	const char *cp, *tp;
152 	const char *sep;
153 	int c, op, i;
154 	uint64_t debug, ndebug;
155 	size_t debuglen;
156 	char oid[256];
157 
158 	ifname = getenv("ATH");
159 	if (ifname == NULL)
160 		ifname = "ath0";
161 	progname = argv[0];
162 	if (argc > 1) {
163 		if (strcmp(argv[1], "-i") == 0) {
164 			if (argc < 2)
165 				errx(1, "missing interface name for -i option");
166 			ifname = argv[2];
167 			if (strncmp(ifname, "ath", 3) != 0)
168 				errx(2, "huh, this is for ath devices?");
169 			argc -= 2, argv += 2;
170 		} else if (strcmp(argv[1], "-?") == 0)
171 			usage();
172 	}
173 
174 #ifdef __linux__
175 	snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
176 #else
177 	snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
178 #endif
179 	debuglen = sizeof(debug);
180 	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
181 		err(1, "sysctl-get(%s)", oid);
182 	ndebug = debug;
183 	for (; argc > 1; argc--, argv++) {
184 		cp = argv[1];
185 		do {
186 			u_int bit;
187 
188 			if (*cp == '-') {
189 				cp++;
190 				op = -1;
191 			} else if (*cp == '+') {
192 				cp++;
193 				op = 1;
194 			} else
195 				op = 0;
196 			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
197 				tp++;
198 			bit = getflag(cp, tp-cp);
199 			if (op < 0)
200 				ndebug &= ~bit;
201 			else if (op > 0)
202 				ndebug |= bit;
203 			else {
204 				if (bit == 0) {
205 					if (isdigit(*cp))
206 						bit = strtoul(cp, NULL, 0);
207 					else
208 						errx(1, "unknown flag %.*s",
209 							(int) (tp-cp), cp);
210 				}
211 				ndebug = bit;
212 			}
213 		} while (*(cp = tp) != '\0');
214 	}
215 	if (debug != ndebug) {
216 		printf("%s: 0x%llx => ", oid, (long long) debug);
217 		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
218 			err(1, "sysctl-set(%s)", oid);
219 		printf("0x%llx", (long long) ndebug);
220 		debug = ndebug;
221 	} else
222 		printf("%s: 0x%llx", oid, (long long) debug);
223 	sep = "<";
224 	for (i = 0; i < N(flags); i++)
225 		if (debug & flags[i].bit) {
226 			printf("%s%s", sep, flags[i].name);
227 			sep = ",";
228 		}
229 	printf("%s\n", *sep != '<' ? ">" : "");
230 	return 0;
231 }
232