2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 #include "ah_internal.h"
47 #define MAXREGS 5*1024
48 struct dumpreg *regs[MAXREGS];
51 static dumpregs_t state;
53 static uint32_t regread(int s, struct ath_diag *atd, uint32_t r);
54 static void regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v);
55 static const struct dumpreg *reglookup(const char *v);
60 fprintf(stderr, "usage: athpoke [-i interface] [reg[=value]] ...\n");
65 main(int argc, char *argv[])
72 s = socket(AF_INET, SOCK_DGRAM, 0);
75 ifname = getenv("ATH");
79 while ((c = getopt(argc, argv, "i:")) != -1)
88 strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
90 atd.ad_id = HAL_DIAG_REVS;
91 atd.ad_out_data = (caddr_t) &state.revs;
92 atd.ad_out_size = sizeof(state.revs);
93 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
99 for (; argc > 0; argc--, argv++) {
101 const struct dumpreg *dr;
104 cp = strchr(argv[0], '=');
107 dr = reglookup(argv[0]);
110 reg = (uint32_t) strtoul(argv[0], &eptr, 0);
111 if (argv[0] == eptr || eptr[0] != '\0')
112 errx(1, "invalid register \"%s\"", argv[0]);
116 regwrite(s, &atd, reg, (uint32_t) strtoul(cp, NULL, 0));
117 printf("%s = %08x\n", argv[0], regread(s, &atd, reg));
123 regread(int s, struct ath_diag *atd, uint32_t r)
131 atd->ad_in_data = (caddr_t) &ra;
132 atd->ad_in_size = sizeof(ra);
133 atd->ad_out_data = (caddr_t) v;
134 atd->ad_out_size = sizeof(v);
135 atd->ad_id = HAL_DIAG_REGS | ATH_DIAG_IN | ATH_DIAG_DYN;
136 if (ioctl(s, SIOCGATHDIAG, atd) < 0)
137 err(1, atd->ad_name);
142 regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v)
148 atd->ad_in_data = (caddr_t) &rw;
149 atd->ad_in_size = sizeof(rw);
150 atd->ad_id = HAL_DIAG_SETREGS | ATH_DIAG_IN;
151 if (ioctl(s, SIOCGATHDIAG, atd) < 0)
152 err(1, atd->ad_name);
156 regcompar(const void *a, const void *b)
158 const struct dumpreg *ra = *(const struct dumpreg **)a;
159 const struct dumpreg *rb = *(const struct dumpreg **)b;
160 return ra->addr - rb->addr;
164 register_regs(struct dumpreg *chipregs, u_int nchipregs,
165 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
167 const int existing_regs = state.nregs;
170 for (i = 0; i < nchipregs; i++) {
171 struct dumpreg *nr = &chipregs[i];
172 if (nr->srevMin == 0)
173 nr->srevMin = def_srev_min;
174 if (nr->srevMax == 0)
175 nr->srevMax = def_srev_max;
177 nr->phyMin = def_phy_min;
179 nr->phyMax = def_phy_max;
180 for (j = 0; j < existing_regs; j++) {
181 struct dumpreg *r = state.regs[j];
183 * Check if we can just expand the mac+phy
184 * coverage for the existing entry.
186 if (nr->addr == r->addr &&
187 (nr->name == r->name ||
188 nr->name != NULL && r->name != NULL &&
189 strcmp(nr->name, r->name) == 0)) {
190 if (nr->srevMin < r->srevMin &&
191 (r->srevMin <= nr->srevMax &&
192 nr->srevMax+1 <= r->srevMax)) {
193 r->srevMin = nr->srevMin;
196 if (nr->srevMax > r->srevMax &&
197 (r->srevMin <= nr->srevMin &&
198 nr->srevMin <= r->srevMax)) {
199 r->srevMax = nr->srevMax;
203 if (r->addr > nr->addr)
207 * New item, add to the end, it'll be sorted below.
209 if (state.nregs == MAXREGS)
210 errx(-1, "too many registers; bump MAXREGS");
211 state.regs[state.nregs++] = nr;
215 qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar);
219 register_keycache(u_int nslots,
220 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
222 /* discard, no use */
226 register_range(u_int brange, u_int erange, int type,
227 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
229 /* discard, no use */
232 static const struct dumpreg *
233 reglookup(const char *v)
235 const HAL_REVS *revs = &state.revs;
238 if (strncasecmp(v, "AR_", 3) == 0)
240 for (i = 0; i < state.nregs; i++) {
241 const struct dumpreg *dr = state.regs[i];
242 if (MAC_MATCH(dr, revs->ah_macVersion, revs->ah_macRev) &&
243 strcasecmp(v, dr->name) == 0)