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"
45 #define MAXREGS 5*1024
46 struct dumpreg *regs[MAXREGS];
49 static dumpregs_t state;
51 static uint32_t regread(int s, struct ath_diag *atd, uint32_t r);
52 static void regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v);
53 static const struct dumpreg *reglookup(const char *v);
58 fprintf(stderr, "usage: athpoke [-i interface] [reg[=value]] ...\n");
63 main(int argc, char *argv[])
69 s = socket(AF_INET, SOCK_DGRAM, 0);
72 ifname = getenv("ATH");
76 while ((c = getopt(argc, argv, "i:")) != -1)
85 strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
87 atd.ad_id = HAL_DIAG_REVS;
88 atd.ad_out_data = (caddr_t) &state.revs;
89 atd.ad_out_size = sizeof(state.revs);
90 if (ioctl(s, SIOCGATHDIAG, &atd) < 0)
96 for (; argc > 0; argc--, argv++) {
98 const struct dumpreg *dr;
101 cp = strchr(argv[0], '=');
104 dr = reglookup(argv[0]);
105 reg = (dr != NULL) ? dr->addr : (uint32_t) strtoul(argv[0], NULL, 0);
107 regwrite(s, &atd, reg, (uint32_t) strtoul(cp, NULL, 0));
108 printf("%s = %08x\n", argv[0], regread(s, &atd, reg));
114 regread(int s, struct ath_diag *atd, uint32_t r)
122 atd->ad_in_data = (caddr_t) &ra;
123 atd->ad_in_size = sizeof(ra);
124 atd->ad_out_data = (caddr_t) v;
125 atd->ad_out_size = sizeof(v);
126 atd->ad_id = HAL_DIAG_REGS | ATH_DIAG_IN | ATH_DIAG_DYN;
127 if (ioctl(s, SIOCGATHDIAG, atd) < 0)
128 err(1, atd->ad_name);
133 regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v)
139 atd->ad_in_data = (caddr_t) &rw;
140 atd->ad_in_size = sizeof(rw);
141 atd->ad_id = HAL_DIAG_SETREGS | ATH_DIAG_IN;
142 if (ioctl(s, SIOCGATHDIAG, atd) < 0)
143 err(1, atd->ad_name);
147 regcompar(const void *a, const void *b)
149 const struct dumpreg *ra = *(const struct dumpreg **)a;
150 const struct dumpreg *rb = *(const struct dumpreg **)b;
151 return ra->addr - rb->addr;
155 register_regs(struct dumpreg *chipregs, u_int nchipregs,
156 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
158 const int existing_regs = state.nregs;
161 for (i = 0; i < nchipregs; i++) {
162 struct dumpreg *nr = &chipregs[i];
163 if (nr->srevMin == 0)
164 nr->srevMin = def_srev_min;
165 if (nr->srevMax == 0)
166 nr->srevMax = def_srev_max;
168 nr->phyMin = def_phy_min;
170 nr->phyMax = def_phy_max;
171 for (j = 0; j < existing_regs; j++) {
172 struct dumpreg *r = state.regs[j];
174 * Check if we can just expand the mac+phy
175 * coverage for the existing entry.
177 if (nr->addr == r->addr &&
178 (nr->name == r->name ||
179 nr->name != NULL && r->name != NULL &&
180 strcmp(nr->name, r->name) == 0)) {
181 if (nr->srevMin < r->srevMin &&
182 (r->srevMin <= nr->srevMax &&
183 nr->srevMax+1 <= r->srevMax)) {
184 r->srevMin = nr->srevMin;
187 if (nr->srevMax > r->srevMax &&
188 (r->srevMin <= nr->srevMin &&
189 nr->srevMin <= r->srevMax)) {
190 r->srevMax = nr->srevMax;
194 if (r->addr > nr->addr)
198 * New item, add to the end, it'll be sorted below.
200 if (state.nregs == MAXREGS)
201 errx(-1, "too many registers; bump MAXREGS");
202 state.regs[state.nregs++] = nr;
206 qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar);
210 register_keycache(u_int nslots,
211 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
213 /* discard, no use */
217 register_range(u_int brange, u_int erange, int type,
218 int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
220 /* discard, no use */
223 static const struct dumpreg *
224 reglookup(const char *v)
226 const HAL_REVS *revs = &state.revs;
229 if (strncasecmp(v, "AR_", 3) == 0)
231 for (i = 0; i < state.nregs; i++) {
232 const struct dumpreg *dr = state.regs[i];
233 if (MAC_MATCH(dr, revs->ah_macVersion, revs->ah_macRev) &&
234 strcasecmp(v, dr->name) == 0)