athdebug.c revision 184370
1153317Ssam/*-
2174245Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3153317Ssam * All rights reserved.
4153317Ssam *
5153317Ssam * Redistribution and use in source and binary forms, with or without
6153317Ssam * modification, are permitted provided that the following conditions
7153317Ssam * are met:
8153317Ssam * 1. Redistributions of source code must retain the above copyright
9153317Ssam *    notice, this list of conditions and the following disclaimer,
10153317Ssam *    without modification.
11153317Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12153317Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13153317Ssam *    redistribution must be conditioned upon including a substantially
14153317Ssam *    similar Disclaimer requirement for further binary redistribution.
15153317Ssam *
16153317Ssam * NO WARRANTY
17153317Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18153317Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19153317Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20153317Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21153317Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22153317Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23153317Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24153317Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25153317Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26153317Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27153317Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28153317Ssam *
29153317Ssam * $FreeBSD: head/tools/tools/ath/athdebug/athdebug.c 184370 2008-10-27 18:47:48Z sam $
30153317Ssam */
31153317Ssam
32153317Ssam/*
33153317Ssam * athdebug [-i interface] flags
34153317Ssam * (default interface is ath0).
35153317Ssam */
36153317Ssam#include <sys/types.h>
37153317Ssam#include <sys/file.h>
38153317Ssam#include <sys/ioctl.h>
39153317Ssam#include <sys/socket.h>
40153317Ssam
41153317Ssam#include <stdio.h>
42153317Ssam#include <ctype.h>
43153317Ssam#include <getopt.h>
44184370Ssam#include <stdlib.h>
45153317Ssam
46153317Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
47153317Ssam
48153317Ssamconst char *progname;
49153317Ssam
50153317Ssamenum {
51153317Ssam	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
52153317Ssam	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
53153317Ssam	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
54153317Ssam	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
55153317Ssam	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
56153317Ssam	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
57153317Ssam	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
58153317Ssam	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
59153317Ssam	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
60153317Ssam	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
61153317Ssam	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
62153317Ssam	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
63153317Ssam	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
64153317Ssam	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
65153317Ssam	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
66153317Ssam	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
67153317Ssam	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
68153391Ssam	ATH_DEBUG_LED		= 0x00100000,	/* led management */
69153391Ssam	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
70153391Ssam	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
71174571Ssam	ATH_DEBUG_TDMA		= 0x00800000,	/* TDMA processing */
72184370Ssam	ATH_DEBUG_REGDOMAIN	= 0x02000000,	/* regulatory processing */
73153317Ssam	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
74153317Ssam	ATH_DEBUG_ANY		= 0xffffffff
75153317Ssam};
76153317Ssam
77153317Ssamstatic struct {
78153317Ssam	const char	*name;
79153317Ssam	u_int		bit;
80153317Ssam} flags[] = {
81153317Ssam	{ "xmit",	ATH_DEBUG_XMIT },
82153317Ssam	{ "xmit_desc",	ATH_DEBUG_XMIT_DESC },
83153317Ssam	{ "recv",	ATH_DEBUG_RECV },
84153317Ssam	{ "recv_desc",	ATH_DEBUG_RECV_DESC },
85153317Ssam	{ "rate",	ATH_DEBUG_RATE },
86153317Ssam	{ "reset",	ATH_DEBUG_RESET },
87153317Ssam	{ "mode",	ATH_DEBUG_MODE },
88153317Ssam	{ "beacon",	ATH_DEBUG_BEACON },
89153317Ssam	{ "watchdog",	ATH_DEBUG_WATCHDOG },
90153317Ssam	{ "intr",	ATH_DEBUG_INTR },
91153317Ssam	{ "xmit_proc",	ATH_DEBUG_TX_PROC },
92153317Ssam	{ "recv_proc",	ATH_DEBUG_RX_PROC },
93153317Ssam	{ "beacon_proc",ATH_DEBUG_BEACON_PROC },
94153317Ssam	{ "calibrate",	ATH_DEBUG_CALIBRATE },
95153317Ssam	{ "keycache",	ATH_DEBUG_KEYCACHE },
96153317Ssam	{ "state",	ATH_DEBUG_STATE },
97153317Ssam	{ "node",	ATH_DEBUG_NODE },
98153391Ssam	{ "led",	ATH_DEBUG_LED },
99153391Ssam	{ "ff",		ATH_DEBUG_FF },
100153391Ssam	{ "dfs",	ATH_DEBUG_DFS },
101174571Ssam	{ "tdma",	ATH_DEBUG_TDMA },
102184370Ssam	{ "regdomain",	ATH_DEBUG_REGDOMAIN },
103153317Ssam	{ "fatal",	ATH_DEBUG_FATAL },
104153317Ssam};
105153317Ssam
106153317Ssamstatic u_int
107153317Ssamgetflag(const char *name, int len)
108153317Ssam{
109153317Ssam	int i;
110153317Ssam
111153317Ssam	for (i = 0; i < N(flags); i++)
112153317Ssam		if (strncasecmp(flags[i].name, name, len) == 0)
113153317Ssam			return flags[i].bit;
114153317Ssam	return 0;
115153317Ssam}
116153317Ssam
117153317Ssamstatic const char *
118153317Ssamgetflagname(u_int flag)
119153317Ssam{
120153317Ssam	int i;
121153317Ssam
122153317Ssam	for (i = 0; i < N(flags); i++)
123153317Ssam		if (flags[i].bit == flag)
124153317Ssam			return flags[i].name;
125153317Ssam	return "???";
126153317Ssam}
127153317Ssam
128153317Ssamstatic void
129153317Ssamusage(void)
130153317Ssam{
131153317Ssam	int i;
132153317Ssam
133153317Ssam	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
134153317Ssam	fprintf(stderr, "where flags are:\n");
135153317Ssam	for (i = 0; i < N(flags); i++)
136153317Ssam		printf("%s\n", flags[i].name);
137153317Ssam	exit(-1);
138153317Ssam}
139153317Ssam
140153317Ssamint
141153317Ssammain(int argc, char *argv[])
142153317Ssam{
143174571Ssam	const char *ifname;
144153317Ssam	const char *cp, *tp;
145153317Ssam	const char *sep;
146153317Ssam	int c, op, i;
147153317Ssam	u_int32_t debug, ndebug;
148153317Ssam	size_t debuglen;
149153317Ssam	char oid[256];
150153317Ssam
151174571Ssam	ifname = getenv("ATH");
152174571Ssam	if (ifname == NULL)
153174571Ssam		ifname = "ath0";
154153317Ssam	progname = argv[0];
155153317Ssam	if (argc > 1) {
156153317Ssam		if (strcmp(argv[1], "-i") == 0) {
157153317Ssam			if (argc < 2)
158153317Ssam				errx(1, "missing interface name for -i option");
159153317Ssam			ifname = argv[2];
160153317Ssam			if (strncmp(ifname, "ath", 3) != 0)
161153317Ssam				errx(2, "huh, this is for ath devices?");
162153317Ssam			argc -= 2, argv += 2;
163153317Ssam		} else if (strcmp(argv[1], "-?") == 0)
164153317Ssam			usage();
165153317Ssam	}
166153317Ssam
167153317Ssam#ifdef __linux__
168153317Ssam	snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
169153317Ssam#else
170153317Ssam	snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
171153317Ssam#endif
172153317Ssam	debuglen = sizeof(debug);
173153317Ssam	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
174153317Ssam		err(1, "sysctl-get(%s)", oid);
175153317Ssam	ndebug = debug;
176153317Ssam	for (; argc > 1; argc--, argv++) {
177153317Ssam		cp = argv[1];
178153317Ssam		do {
179153317Ssam			u_int bit;
180153317Ssam
181153317Ssam			if (*cp == '-') {
182153317Ssam				cp++;
183153317Ssam				op = -1;
184153317Ssam			} else if (*cp == '+') {
185153317Ssam				cp++;
186153317Ssam				op = 1;
187153317Ssam			} else
188153317Ssam				op = 0;
189153317Ssam			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
190153317Ssam				tp++;
191153317Ssam			bit = getflag(cp, tp-cp);
192153317Ssam			if (op < 0)
193153317Ssam				ndebug &= ~bit;
194153317Ssam			else if (op > 0)
195153317Ssam				ndebug |= bit;
196153317Ssam			else {
197153317Ssam				if (bit == 0) {
198153317Ssam					if (isdigit(*cp))
199153317Ssam						bit = strtoul(cp, NULL, 0);
200153317Ssam					else
201153317Ssam						errx(1, "unknown flag %.*s",
202153317Ssam							tp-cp, cp);
203153317Ssam				}
204153317Ssam				ndebug = bit;
205153317Ssam			}
206153317Ssam		} while (*(cp = tp) != '\0');
207153317Ssam	}
208153317Ssam	if (debug != ndebug) {
209153317Ssam		printf("%s: 0x%x => ", oid, debug);
210153317Ssam		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
211153317Ssam			err(1, "sysctl-set(%s)", oid);
212153317Ssam		printf("0x%x", ndebug);
213153317Ssam		debug = ndebug;
214153317Ssam	} else
215153317Ssam		printf("%s: 0x%x", oid, debug);
216153317Ssam	sep = "<";
217153317Ssam	for (i = 0; i < N(flags); i++)
218153317Ssam		if (debug & flags[i].bit) {
219153317Ssam			printf("%s%s", sep, flags[i].name);
220153317Ssam			sep = ",";
221153317Ssam		}
222153317Ssam	printf("%s\n", *sep != '<' ? ">" : "");
223153317Ssam	return 0;
224153317Ssam}
225