ath_netbsd.c revision 1.1 1 /*-
2 * Copyright (c) 2003, 2004 David Young
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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/errno.h>
30 #include <sys/systm.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/malloc.h>
34 #include <sys/lock.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/callout.h>
40 #include <machine/bus.h>
41 #include <machine/stdarg.h>
42 #include <sys/endian.h>
43
44 #include <net/if.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_arp.h>
48 #include <net/if_ether.h>
49 #include <net/if_llc.h>
50
51 #include <net80211/ieee80211_netbsd.h>
52 #include <net80211/ieee80211_var.h>
53 #include <dev/ic/ath_netbsd.h>
54 #include <dev/ic/athvar.h>
55
56 void
57 device_printf(struct device dv, const char *fmt, ...)
58 {
59 va_list ap;
60
61 va_start(ap, fmt);
62 printf("%s: ", dv.dv_xname);
63 vprintf(fmt, ap);
64 va_end(ap);
65 return;
66 }
67
68 struct mbuf *
69 m_defrag(struct mbuf *m0, int flags)
70 {
71 struct mbuf *m;
72 MGETHDR(m, flags, MT_DATA);
73 if (m == NULL)
74 return NULL;
75
76 M_COPY_PKTHDR(m, m0);
77 MCLGET(m, flags);
78 if ((m->m_flags & M_EXT) == 0) {
79 m_free(m);
80 return NULL;
81 }
82 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
83 m->m_len = m->m_pkthdr.len;
84 return m;
85 }
86
87 /*
88 * Setup sysctl(3) MIB, ath.*.
89 *
90 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
91 */
92 SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
93 {
94 int rc;
95 const struct sysctlnode *cnode, *rnode;
96
97 if ((rnode = ath_sysctl_treetop(clog)) == NULL)
98 return;
99
100 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "dwell",
101 "channel dwell time (ms) for AP/station scanning",
102 dwelltime)) != 0)
103 goto err;
104
105 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "calibrate",
106 "chip calibration interval (secs)", calinterval)) != 0)
107 goto err;
108
109 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "outdoor",
110 "outdoor operation", outdoor)) != 0)
111 goto err;
112
113 /* country code */
114 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE,
115 "countrycode", "country code", countrycode)) != 0)
116 goto err;
117
118 /* regulatory domain */
119 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "regdomain",
120 "regulatory domain", regdomain)) != 0)
121 goto err;
122
123 if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "debug",
124 "control debugging printfs", debug)) != 0)
125 goto err;
126
127 return;
128 err:
129 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
130 }
131
132 static int
133 ath_sysctl_slottime(SYSCTLFN_ARGS)
134 {
135 struct ath_softc *sc;
136 struct sysctlnode node;
137 u_int slottime;
138 int error;
139
140 node = *rnode;
141 sc = (struct ath_softc *)node.sysctl_data;
142 slottime = ath_hal_getslottime(sc->sc_ah);
143 node.sysctl_data = &slottime;
144 error = sysctl_lookup(SYSCTLFN_CALL(&node));
145 if (error || newp == NULL)
146 return error;
147 return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
148 }
149
150 static int
151 ath_sysctl_acktimeout(SYSCTLFN_ARGS)
152 {
153 struct ath_softc *sc;
154 struct sysctlnode node;
155 u_int acktimeout;
156 int error;
157
158 node = *rnode;
159 sc = (struct ath_softc *)node.sysctl_data;
160 acktimeout = ath_hal_getacktimeout(sc->sc_ah);
161 node.sysctl_data = &acktimeout;
162 error = sysctl_lookup(SYSCTLFN_CALL(&node));
163 if (error || newp == NULL)
164 return error;
165 return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
166 }
167
168 static int
169 ath_sysctl_ctstimeout(SYSCTLFN_ARGS)
170 {
171 struct ath_softc *sc;
172 struct sysctlnode node;
173 u_int ctstimeout;
174 int error;
175
176 node = *rnode;
177 sc = (struct ath_softc *)node.sysctl_data;
178 ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
179 node.sysctl_data = &ctstimeout;
180 error = sysctl_lookup(SYSCTLFN_CALL(&node));
181 if (error || newp == NULL)
182 return error;
183 return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
184 }
185
186 static int
187 ath_sysctl_softled(SYSCTLFN_ARGS)
188 {
189 struct ath_softc *sc;
190 struct sysctlnode node;
191 int softled;
192 int error;
193
194 node = *rnode;
195 sc = (struct ath_softc *)node.sysctl_data;
196 softled = sc->sc_softled;
197 node.sysctl_data = &softled;
198 error = sysctl_lookup(SYSCTLFN_CALL(&node));
199 if (error || newp == NULL)
200 return error;
201 softled = (softled != 0);
202 if (softled != sc->sc_softled) {
203 if (softled) {
204 /* NB: handle any sc_ledpin change */
205 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
206 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
207 !sc->sc_ledon);
208 }
209 sc->sc_softled = softled;
210 }
211 return 0;
212 }
213
214 static int
215 ath_sysctl_rxantenna(SYSCTLFN_ARGS)
216 {
217 struct ath_softc *sc;
218 struct sysctlnode node;
219 u_int defantenna;
220 int error;
221
222 node = *rnode;
223 sc = (struct ath_softc *)node.sysctl_data;
224 defantenna = ath_hal_getdefantenna(sc->sc_ah);
225 node.sysctl_data = &defantenna;
226 error = sysctl_lookup(SYSCTLFN_CALL(&node));
227 if (error || newp == NULL)
228 return error;
229 ath_hal_setdefantenna(sc->sc_ah, defantenna);
230 return 0;
231 }
232
233 static int
234 ath_sysctl_diversity(SYSCTLFN_ARGS)
235 {
236 struct ath_softc *sc;
237 struct sysctlnode node;
238 u_int diversity;
239 int error;
240
241 node = *rnode;
242 sc = (struct ath_softc *)node.sysctl_data;
243 diversity = sc->sc_diversity;
244 node.sysctl_data = &diversity;
245 error = sysctl_lookup(SYSCTLFN_CALL(&node));
246 if (error || newp == NULL)
247 return error;
248 if (!ath_hal_setdiversity(sc->sc_ah, diversity))
249 return EINVAL;
250 sc->sc_diversity = diversity;
251 return 0;
252 }
253
254 static int
255 ath_sysctl_diag(SYSCTLFN_ARGS)
256 {
257 struct ath_softc *sc;
258 struct sysctlnode node;
259 u_int32_t diag;
260 int error;
261
262 node = *rnode;
263 sc = (struct ath_softc *)node.sysctl_data;
264 if (!ath_hal_getdiag(sc->sc_ah, &diag))
265 return EINVAL;
266 node.sysctl_data = &diag;
267 error = sysctl_lookup(SYSCTLFN_CALL(&node));
268 if (error || newp == NULL)
269 return error;
270 return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
271 }
272
273 static int
274 ath_sysctl_tpscale(SYSCTLFN_ARGS)
275 {
276 struct ath_softc *sc;
277 struct sysctlnode node;
278 u_int32_t scale;
279 int error;
280
281 node = *rnode;
282 sc = (struct ath_softc *)node.sysctl_data;
283 ath_hal_gettpscale(sc->sc_ah, &scale);
284 node.sysctl_data = &scale;
285 error = sysctl_lookup(SYSCTLFN_CALL(&node));
286 if (error || newp == NULL)
287 return error;
288 return !ath_hal_settpscale(sc->sc_ah, scale)
289 ? EINVAL
290 : ath_reset(&sc->sc_if);
291 }
292
293 static int
294 ath_sysctl_tpc(SYSCTLFN_ARGS)
295 {
296 struct ath_softc *sc;
297 struct sysctlnode node;
298 u_int tpc;
299 int error;
300
301 node = *rnode;
302 sc = (struct ath_softc *)node.sysctl_data;
303 tpc = ath_hal_gettpc(sc->sc_ah);
304 node.sysctl_data = &tpc;
305 error = sysctl_lookup(SYSCTLFN_CALL(&node));
306 if (error || newp == NULL)
307 return error;
308 return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
309 }
310
311 const struct sysctlnode *
312 ath_sysctl_treetop(struct sysctllog **log)
313 {
314 int rc;
315 const struct sysctlnode *rnode;
316
317 if ((rc = sysctl_createv(log, 0, NULL, &rnode,
318 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
319 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
320 goto err;
321
322 if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
323 CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
324 SYSCTL_DESCR("ath information and options"),
325 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
326 goto err;
327
328 return rnode;
329 err:
330 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
331 return NULL;
332 }
333
334 void
335 ath_sysctlattach(struct ath_softc *sc)
336 {
337 int rc;
338 struct sysctllog **log = &sc->sc_sysctllog;
339 const struct sysctlnode *cnode, *rnode;
340
341 ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
342 ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
343 sc->sc_debug = ath_debug;
344 sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
345
346 if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
347 return;
348
349 if ((rc = SYSCTL_INT(0, countrycode, "EEPROM country code")) != 0)
350 goto err;
351
352 if ((rc = SYSCTL_INT(0, regdomain, "EEPROM regdomain code")) != 0)
353 goto err;
354
355 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, debug,
356 "control debugging printfs")) != 0)
357 goto err;
358
359 #if 0
360 /* channel dwell time (ms) for AP/station scanning */
361 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, dwell,
362 "Channel dwell time (ms) for scanning")) != 0)
363 goto err;
364 #endif
365
366 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, slottime,
367 "802.11 slot time (us)")) != 0)
368 goto err;
369 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, acktimeout,
370 "802.11 ACK timeout (us)")) != 0)
371 goto err;
372 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, ctstimeout,
373 "802.11 CTS timeout (us)")) != 0)
374 goto err;
375 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, softled,
376 "enable/disable software LED support")) != 0)
377 goto err;
378 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledpin,
379 "GPIO pin connected to LED")) != 0)
380 goto err;
381 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledon,
382 "setting to turn LED on")) != 0)
383 goto err;
384 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledidle,
385 "idle time for inactivity LED (ticks)")) != 0)
386 goto err;
387 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txantenna,
388 "tx antenna (0=auto)")) != 0)
389 goto err;
390 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rxantenna,
391 "default/rx antenna")) != 0)
392 goto err;
393 if (sc->sc_hasdiversity) {
394 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diversity,
395 "antenna diversity")) != 0)
396 goto err;
397 }
398 if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txintrperiod,
399 "tx descriptor batching")) != 0)
400 goto err;
401 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diag,
402 "h/w diagnostic control")) != 0)
403 goto err;
404 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpscale,
405 "tx power scaling")) != 0)
406 goto err;
407 if (sc->sc_hastpc) {
408 if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpc,
409 "enable/disable per-packet TPC")) != 0)
410 goto err;
411 }
412 return;
413 err:
414 printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
415 }
416