]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - tools/tools/ath/athpoke/athpoke.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / tools / tools / ath / athpoke / athpoke.c
1 /*-
2  * Copyright (c) 2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
16  * NO WARRANTY
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.
28  *
29  * $FreeBSD$
30  */
31 #include "diag.h"
32
33 #include "ah.h"
34 #include "ah_internal.h"
35
36 #include "dumpregs.h"
37
38 #include <getopt.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42
43 typedef struct {
44         HAL_REVS revs;
45 #define MAXREGS 5*1024
46         struct dumpreg *regs[MAXREGS];
47         u_int nregs;
48 } dumpregs_t;
49 static  dumpregs_t state;
50
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);
54
55 static void
56 usage(void)
57 {
58         fprintf(stderr, "usage: athpoke [-i interface] [reg[=value]] ...\n");
59         exit(-1);
60 }
61
62 int
63 main(int argc, char *argv[])
64 {
65         struct ath_diag atd;
66         const char *ifname;
67         int c, s;
68
69         s = socket(AF_INET, SOCK_DGRAM, 0);
70         if (s < 0)
71                 err(1, "socket");
72         ifname = getenv("ATH");
73         if (!ifname)
74                 ifname = ATH_DEFAULT;
75
76         while ((c = getopt(argc, argv, "i:")) != -1)
77                 switch (c) {
78                 case 'i':
79                         ifname = optarg;
80                         break;
81                 default:
82                         usage();
83                         /*NOTREACHED*/
84                 }
85         strncpy(atd.ad_name, ifname, sizeof (atd.ad_name));
86
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)
91                 err(1, atd.ad_name);
92
93         argc -= optind;
94         argv += optind;
95
96         for (; argc > 0; argc--, argv++) {
97                 char *cp;
98                 const struct dumpreg *dr;
99                 uint32_t reg;
100
101                 cp = strchr(argv[0], '=');
102                 if (cp != NULL)
103                         *cp++ = '\0';
104                 dr = reglookup(argv[0]);
105                 reg = (dr != NULL) ? dr->addr : (uint32_t) strtoul(argv[0], NULL, 0);
106                 if (cp != NULL)
107                         regwrite(s, &atd, reg, (uint32_t) strtoul(cp, NULL, 0));
108                 printf("%s = %08x\n", argv[0], regread(s, &atd, reg));
109         }
110         return 0;
111 }
112
113 static uint32_t
114 regread(int s, struct ath_diag *atd, uint32_t r)
115 {
116         HAL_REGRANGE ra;
117         uint32_t v[2];
118
119         ra.start = r;
120         ra.end = 0;
121
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);
129         return v[1];
130 }
131
132 static void
133 regwrite(int s, struct ath_diag *atd, uint32_t r, uint32_t v)
134 {
135         HAL_REGWRITE rw;
136
137         rw.addr = r;
138         rw.value = 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);
144 }
145
146 static int
147 regcompar(const void *a, const void *b)
148 {
149         const struct dumpreg *ra = *(const struct dumpreg **)a;
150         const struct dumpreg *rb = *(const struct dumpreg **)b;
151         return ra->addr - rb->addr;
152 }
153
154 void
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)
157 {
158         const int existing_regs = state.nregs;
159         int i, j;
160
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;
167                 if (nr->phyMin == 0)
168                         nr->phyMin = def_phy_min;
169                 if (nr->phyMax == 0)
170                         nr->phyMax = def_phy_max;
171                 for (j = 0; j < existing_regs; j++) {
172                         struct dumpreg *r = state.regs[j];
173                         /*
174                          * Check if we can just expand the mac+phy
175                          * coverage for the existing entry.
176                          */
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;
185                                         goto skip;
186                                 }
187                                 if (nr->srevMax > r->srevMax &&
188                                     (r->srevMin <= nr->srevMin &&
189                                      nr->srevMin <= r->srevMax)) {
190                                         r->srevMax = nr->srevMax;
191                                         goto skip;
192                                 }
193                         }
194                         if (r->addr > nr->addr)
195                                 break;
196                 }
197                 /*
198                  * New item, add to the end, it'll be sorted below.
199                  */
200                 if (state.nregs == MAXREGS)
201                         errx(-1, "too many registers; bump MAXREGS");
202                 state.regs[state.nregs++] = nr;
203         skip:
204                 ;
205         }
206         qsort(state.regs, state.nregs, sizeof(struct dumpreg *), regcompar);
207 }
208
209 void
210 register_keycache(u_int nslots,
211         int def_srev_min, int def_srev_max, int def_phy_min, int def_phy_max)
212 {
213         /* discard, no use */
214 }
215
216 void
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)
219 {
220         /* discard, no use */
221 }
222
223 static const struct dumpreg *
224 reglookup(const char *v)
225 {
226         const HAL_REVS *revs = &state.revs;
227         int i;
228
229         if (strncasecmp(v, "AR_", 3) == 0)
230                 v += 3;
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)
235                         return dr;
236         }
237         return NULL;
238 }