athdebug.c revision 153391
1153317Ssam/*- 2153317Ssam * Copyright (c) 2002-2004 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 * 3. Neither the names of the above-listed copyright holders nor the names 16153317Ssam * of any contributors may be used to endorse or promote products derived 17153317Ssam * from this software without specific prior written permission. 18153317Ssam * 19153317Ssam * Alternatively, this software may be distributed under the terms of the 20153317Ssam * GNU General Public License ("GPL") version 2 as published by the Free 21153317Ssam * Software Foundation. 22153317Ssam * 23153317Ssam * NO WARRANTY 24153317Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25153317Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26153317Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27153317Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28153317Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29153317Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30153317Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31153317Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32153317Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33153317Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34153317Ssam * THE POSSIBILITY OF SUCH DAMAGES. 35153317Ssam * 36153317Ssam * $FreeBSD: head/tools/tools/ath/athdebug/athdebug.c 153391 2005-12-13 22:13:41Z sam $ 37153317Ssam */ 38153317Ssam 39153317Ssam/* 40153317Ssam * athdebug [-i interface] flags 41153317Ssam * (default interface is ath0). 42153317Ssam */ 43153317Ssam#include <sys/types.h> 44153317Ssam#include <sys/file.h> 45153317Ssam#include <sys/ioctl.h> 46153317Ssam#include <sys/socket.h> 47153317Ssam 48153317Ssam#include <stdio.h> 49153317Ssam#include <ctype.h> 50153317Ssam#include <getopt.h> 51153317Ssam 52153317Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 53153317Ssam 54153317Ssamconst char *progname; 55153317Ssam 56153317Ssamenum { 57153317Ssam ATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 58153317Ssam ATH_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */ 59153317Ssam ATH_DEBUG_RECV = 0x00000004, /* basic recv operation */ 60153317Ssam ATH_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */ 61153317Ssam ATH_DEBUG_RATE = 0x00000010, /* rate control */ 62153317Ssam ATH_DEBUG_RESET = 0x00000020, /* reset processing */ 63153317Ssam ATH_DEBUG_MODE = 0x00000040, /* mode init/setup */ 64153317Ssam ATH_DEBUG_BEACON = 0x00000080, /* beacon handling */ 65153317Ssam ATH_DEBUG_WATCHDOG = 0x00000100, /* watchdog timeout */ 66153317Ssam ATH_DEBUG_INTR = 0x00001000, /* ISR */ 67153317Ssam ATH_DEBUG_TX_PROC = 0x00002000, /* tx ISR proc */ 68153317Ssam ATH_DEBUG_RX_PROC = 0x00004000, /* rx ISR proc */ 69153317Ssam ATH_DEBUG_BEACON_PROC = 0x00008000, /* beacon ISR proc */ 70153317Ssam ATH_DEBUG_CALIBRATE = 0x00010000, /* periodic calibration */ 71153317Ssam ATH_DEBUG_KEYCACHE = 0x00020000, /* key cache management */ 72153317Ssam ATH_DEBUG_STATE = 0x00040000, /* 802.11 state transitions */ 73153317Ssam ATH_DEBUG_NODE = 0x00080000, /* node management */ 74153391Ssam ATH_DEBUG_LED = 0x00100000, /* led management */ 75153391Ssam ATH_DEBUG_FF = 0x00200000, /* fast frames */ 76153391Ssam ATH_DEBUG_DFS = 0x00400000, /* DFS processing */ 77153317Ssam ATH_DEBUG_FATAL = 0x80000000, /* fatal errors */ 78153317Ssam ATH_DEBUG_ANY = 0xffffffff 79153317Ssam}; 80153317Ssam 81153317Ssamstatic struct { 82153317Ssam const char *name; 83153317Ssam u_int bit; 84153317Ssam} flags[] = { 85153317Ssam { "xmit", ATH_DEBUG_XMIT }, 86153317Ssam { "xmit_desc", ATH_DEBUG_XMIT_DESC }, 87153317Ssam { "recv", ATH_DEBUG_RECV }, 88153317Ssam { "recv_desc", ATH_DEBUG_RECV_DESC }, 89153317Ssam { "rate", ATH_DEBUG_RATE }, 90153317Ssam { "reset", ATH_DEBUG_RESET }, 91153317Ssam { "mode", ATH_DEBUG_MODE }, 92153317Ssam { "beacon", ATH_DEBUG_BEACON }, 93153317Ssam { "watchdog", ATH_DEBUG_WATCHDOG }, 94153317Ssam { "intr", ATH_DEBUG_INTR }, 95153317Ssam { "xmit_proc", ATH_DEBUG_TX_PROC }, 96153317Ssam { "recv_proc", ATH_DEBUG_RX_PROC }, 97153317Ssam { "beacon_proc",ATH_DEBUG_BEACON_PROC }, 98153317Ssam { "calibrate", ATH_DEBUG_CALIBRATE }, 99153317Ssam { "keycache", ATH_DEBUG_KEYCACHE }, 100153317Ssam { "state", ATH_DEBUG_STATE }, 101153317Ssam { "node", ATH_DEBUG_NODE }, 102153391Ssam { "led", ATH_DEBUG_LED }, 103153391Ssam { "ff", ATH_DEBUG_FF }, 104153391Ssam { "dfs", ATH_DEBUG_DFS }, 105153317Ssam { "fatal", ATH_DEBUG_FATAL }, 106153317Ssam}; 107153317Ssam 108153317Ssamstatic u_int 109153317Ssamgetflag(const char *name, int len) 110153317Ssam{ 111153317Ssam int i; 112153317Ssam 113153317Ssam for (i = 0; i < N(flags); i++) 114153317Ssam if (strncasecmp(flags[i].name, name, len) == 0) 115153317Ssam return flags[i].bit; 116153317Ssam return 0; 117153317Ssam} 118153317Ssam 119153317Ssamstatic const char * 120153317Ssamgetflagname(u_int flag) 121153317Ssam{ 122153317Ssam int i; 123153317Ssam 124153317Ssam for (i = 0; i < N(flags); i++) 125153317Ssam if (flags[i].bit == flag) 126153317Ssam return flags[i].name; 127153317Ssam return "???"; 128153317Ssam} 129153317Ssam 130153317Ssamstatic void 131153317Ssamusage(void) 132153317Ssam{ 133153317Ssam int i; 134153317Ssam 135153317Ssam fprintf(stderr, "usage: %s [-i device] [flags]\n", progname); 136153317Ssam fprintf(stderr, "where flags are:\n"); 137153317Ssam for (i = 0; i < N(flags); i++) 138153317Ssam printf("%s\n", flags[i].name); 139153317Ssam exit(-1); 140153317Ssam} 141153317Ssam 142153317Ssamint 143153317Ssammain(int argc, char *argv[]) 144153317Ssam{ 145153317Ssam const char *ifname = "ath0"; 146153317Ssam const char *cp, *tp; 147153317Ssam const char *sep; 148153317Ssam int c, op, i; 149153317Ssam u_int32_t debug, ndebug; 150153317Ssam size_t debuglen; 151153317Ssam char oid[256]; 152153317Ssam 153153317Ssam progname = argv[0]; 154153317Ssam if (argc > 1) { 155153317Ssam if (strcmp(argv[1], "-i") == 0) { 156153317Ssam if (argc < 2) 157153317Ssam errx(1, "missing interface name for -i option"); 158153317Ssam ifname = argv[2]; 159153317Ssam if (strncmp(ifname, "ath", 3) != 0) 160153317Ssam errx(2, "huh, this is for ath devices?"); 161153317Ssam argc -= 2, argv += 2; 162153317Ssam } else if (strcmp(argv[1], "-?") == 0) 163153317Ssam usage(); 164153317Ssam } 165153317Ssam 166153317Ssam#ifdef __linux__ 167153317Ssam snprintf(oid, sizeof(oid), "dev.%s.debug", ifname); 168153317Ssam#else 169153317Ssam snprintf(oid, sizeof(oid), "dev.ath.%s.debug", ifname+3); 170153317Ssam#endif 171153317Ssam debuglen = sizeof(debug); 172153317Ssam if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0) 173153317Ssam err(1, "sysctl-get(%s)", oid); 174153317Ssam ndebug = debug; 175153317Ssam for (; argc > 1; argc--, argv++) { 176153317Ssam cp = argv[1]; 177153317Ssam do { 178153317Ssam u_int bit; 179153317Ssam 180153317Ssam if (*cp == '-') { 181153317Ssam cp++; 182153317Ssam op = -1; 183153317Ssam } else if (*cp == '+') { 184153317Ssam cp++; 185153317Ssam op = 1; 186153317Ssam } else 187153317Ssam op = 0; 188153317Ssam for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';) 189153317Ssam tp++; 190153317Ssam bit = getflag(cp, tp-cp); 191153317Ssam if (op < 0) 192153317Ssam ndebug &= ~bit; 193153317Ssam else if (op > 0) 194153317Ssam ndebug |= bit; 195153317Ssam else { 196153317Ssam if (bit == 0) { 197153317Ssam if (isdigit(*cp)) 198153317Ssam bit = strtoul(cp, NULL, 0); 199153317Ssam else 200153317Ssam errx(1, "unknown flag %.*s", 201153317Ssam tp-cp, cp); 202153317Ssam } 203153317Ssam ndebug = bit; 204153317Ssam } 205153317Ssam } while (*(cp = tp) != '\0'); 206153317Ssam } 207153317Ssam if (debug != ndebug) { 208153317Ssam printf("%s: 0x%x => ", oid, debug); 209153317Ssam if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0) 210153317Ssam err(1, "sysctl-set(%s)", oid); 211153317Ssam printf("0x%x", ndebug); 212153317Ssam debug = ndebug; 213153317Ssam } else 214153317Ssam printf("%s: 0x%x", oid, debug); 215153317Ssam sep = "<"; 216153317Ssam for (i = 0; i < N(flags); i++) 217153317Ssam if (debug & flags[i].bit) { 218153317Ssam printf("%s%s", sep, flags[i].name); 219153317Ssam sep = ","; 220153317Ssam } 221153317Ssam printf("%s\n", *sep != '<' ? ">" : ""); 222153317Ssam return 0; 223153317Ssam} 224