athdebug.c revision 174571
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 174571 2007-12-13 02:01:01Z 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>
44153317Ssam
45153317Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
46153317Ssam
47153317Ssamconst char *progname;
48153317Ssam
49153317Ssamenum {
50153317Ssam	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
51153317Ssam	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
52153317Ssam	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
53153317Ssam	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
54153317Ssam	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
55153317Ssam	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
56153317Ssam	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
57153317Ssam	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
58153317Ssam	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
59153317Ssam	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
60153317Ssam	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
61153317Ssam	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
62153317Ssam	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
63153317Ssam	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
64153317Ssam	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
65153317Ssam	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
66153317Ssam	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
67153391Ssam	ATH_DEBUG_LED		= 0x00100000,	/* led management */
68153391Ssam	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
69153391Ssam	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
70174571Ssam	ATH_DEBUG_TDMA		= 0x00800000,	/* TDMA processing */
71153317Ssam	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
72153317Ssam	ATH_DEBUG_ANY		= 0xffffffff
73153317Ssam};
74153317Ssam
75153317Ssamstatic struct {
76153317Ssam	const char	*name;
77153317Ssam	u_int		bit;
78153317Ssam} flags[] = {
79153317Ssam	{ "xmit",	ATH_DEBUG_XMIT },
80153317Ssam	{ "xmit_desc",	ATH_DEBUG_XMIT_DESC },
81153317Ssam	{ "recv",	ATH_DEBUG_RECV },
82153317Ssam	{ "recv_desc",	ATH_DEBUG_RECV_DESC },
83153317Ssam	{ "rate",	ATH_DEBUG_RATE },
84153317Ssam	{ "reset",	ATH_DEBUG_RESET },
85153317Ssam	{ "mode",	ATH_DEBUG_MODE },
86153317Ssam	{ "beacon",	ATH_DEBUG_BEACON },
87153317Ssam	{ "watchdog",	ATH_DEBUG_WATCHDOG },
88153317Ssam	{ "intr",	ATH_DEBUG_INTR },
89153317Ssam	{ "xmit_proc",	ATH_DEBUG_TX_PROC },
90153317Ssam	{ "recv_proc",	ATH_DEBUG_RX_PROC },
91153317Ssam	{ "beacon_proc",ATH_DEBUG_BEACON_PROC },
92153317Ssam	{ "calibrate",	ATH_DEBUG_CALIBRATE },
93153317Ssam	{ "keycache",	ATH_DEBUG_KEYCACHE },
94153317Ssam	{ "state",	ATH_DEBUG_STATE },
95153317Ssam	{ "node",	ATH_DEBUG_NODE },
96153391Ssam	{ "led",	ATH_DEBUG_LED },
97153391Ssam	{ "ff",		ATH_DEBUG_FF },
98153391Ssam	{ "dfs",	ATH_DEBUG_DFS },
99174571Ssam	{ "tdma",	ATH_DEBUG_TDMA },
100153317Ssam	{ "fatal",	ATH_DEBUG_FATAL },
101153317Ssam};
102153317Ssam
103153317Ssamstatic u_int
104153317Ssamgetflag(const char *name, int len)
105153317Ssam{
106153317Ssam	int i;
107153317Ssam
108153317Ssam	for (i = 0; i < N(flags); i++)
109153317Ssam		if (strncasecmp(flags[i].name, name, len) == 0)
110153317Ssam			return flags[i].bit;
111153317Ssam	return 0;
112153317Ssam}
113153317Ssam
114153317Ssamstatic const char *
115153317Ssamgetflagname(u_int flag)
116153317Ssam{
117153317Ssam	int i;
118153317Ssam
119153317Ssam	for (i = 0; i < N(flags); i++)
120153317Ssam		if (flags[i].bit == flag)
121153317Ssam			return flags[i].name;
122153317Ssam	return "???";
123153317Ssam}
124153317Ssam
125153317Ssamstatic void
126153317Ssamusage(void)
127153317Ssam{
128153317Ssam	int i;
129153317Ssam
130153317Ssam	fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
131153317Ssam	fprintf(stderr, "where flags are:\n");
132153317Ssam	for (i = 0; i < N(flags); i++)
133153317Ssam		printf("%s\n", flags[i].name);
134153317Ssam	exit(-1);
135153317Ssam}
136153317Ssam
137153317Ssamint
138153317Ssammain(int argc, char *argv[])
139153317Ssam{
140174571Ssam	const char *ifname;
141153317Ssam	const char *cp, *tp;
142153317Ssam	const char *sep;
143153317Ssam	int c, op, i;
144153317Ssam	u_int32_t debug, ndebug;
145153317Ssam	size_t debuglen;
146153317Ssam	char oid[256];
147153317Ssam
148174571Ssam	ifname = getenv("ATH");
149174571Ssam	if (ifname == NULL)
150174571Ssam		ifname = "ath0";
151153317Ssam	progname = argv[0];
152153317Ssam	if (argc > 1) {
153153317Ssam		if (strcmp(argv[1], "-i") == 0) {
154153317Ssam			if (argc < 2)
155153317Ssam				errx(1, "missing interface name for -i option");
156153317Ssam			ifname = argv[2];
157153317Ssam			if (strncmp(ifname, "ath", 3) != 0)
158153317Ssam				errx(2, "huh, this is for ath devices?");
159153317Ssam			argc -= 2, argv += 2;
160153317Ssam		} else if (strcmp(argv[1], "-?") == 0)
161153317Ssam			usage();
162153317Ssam	}
163153317Ssam
164153317Ssam#ifdef __linux__
165153317Ssam	snprintf(oid, sizeof(oid), "dev.%s.debug", ifname);
166153317Ssam#else
167153317Ssam	snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3);
168153317Ssam#endif
169153317Ssam	debuglen = sizeof(debug);
170153317Ssam	if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
171153317Ssam		err(1, "sysctl-get(%s)", oid);
172153317Ssam	ndebug = debug;
173153317Ssam	for (; argc > 1; argc--, argv++) {
174153317Ssam		cp = argv[1];
175153317Ssam		do {
176153317Ssam			u_int bit;
177153317Ssam
178153317Ssam			if (*cp == '-') {
179153317Ssam				cp++;
180153317Ssam				op = -1;
181153317Ssam			} else if (*cp == '+') {
182153317Ssam				cp++;
183153317Ssam				op = 1;
184153317Ssam			} else
185153317Ssam				op = 0;
186153317Ssam			for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
187153317Ssam				tp++;
188153317Ssam			bit = getflag(cp, tp-cp);
189153317Ssam			if (op < 0)
190153317Ssam				ndebug &= ~bit;
191153317Ssam			else if (op > 0)
192153317Ssam				ndebug |= bit;
193153317Ssam			else {
194153317Ssam				if (bit == 0) {
195153317Ssam					if (isdigit(*cp))
196153317Ssam						bit = strtoul(cp, NULL, 0);
197153317Ssam					else
198153317Ssam						errx(1, "unknown flag %.*s",
199153317Ssam							tp-cp, cp);
200153317Ssam				}
201153317Ssam				ndebug = bit;
202153317Ssam			}
203153317Ssam		} while (*(cp = tp) != '\0');
204153317Ssam	}
205153317Ssam	if (debug != ndebug) {
206153317Ssam		printf("%s: 0x%x => ", oid, debug);
207153317Ssam		if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
208153317Ssam			err(1, "sysctl-set(%s)", oid);
209153317Ssam		printf("0x%x", ndebug);
210153317Ssam		debug = ndebug;
211153317Ssam	} else
212153317Ssam		printf("%s: 0x%x", oid, debug);
213153317Ssam	sep = "<";
214153317Ssam	for (i = 0; i < N(flags); i++)
215153317Ssam		if (debug & flags[i].bit) {
216153317Ssam			printf("%s%s", sep, flags[i].name);
217153317Ssam			sep = ",";
218153317Ssam		}
219153317Ssam	printf("%s\n", *sep != '<' ? ">" : "");
220153317Ssam	return 0;
221153317Ssam}
222