xref: /netbsd-src/sys/dev/ic/ath_netbsd.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ath_netbsd.c,v 1.17 2009/10/19 23:19:39 rmind Exp $ */
2 
3 /*-
4  * Copyright (c) 2003, 2004 David Young
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
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 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ath_netbsd.c,v 1.17 2009/10/19 23:19:39 rmind Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/mbuf.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/sysctl.h>
42 #include <sys/callout.h>
43 #include <sys/bus.h>
44 #include <machine/stdarg.h>
45 #include <sys/endian.h>
46 #include <sys/device.h>
47 
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_arp.h>
52 #include <net/if_ether.h>
53 #include <net/if_llc.h>
54 
55 #include <net80211/ieee80211_netbsd.h>
56 #include <net80211/ieee80211_var.h>
57 #include <dev/ic/ath_netbsd.h>
58 #include <dev/ic/athvar.h>
59 
60 void
61 device_printf(device_t dev, const char *fmt, ...)
62 {
63 	va_list ap;
64 
65 	va_start(ap, fmt);
66 	printf("%s: ", device_xname(dev));
67 	vprintf(fmt, ap);
68 	va_end(ap);
69 	return;
70 }
71 
72 /*
73  * Setup sysctl(3) MIB, hw.ath.*.
74  *
75  * TBD condition CTLFLAG_PERMANENT on being a module or not
76  */
77 SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
78 {
79 	int rc;
80 	const struct sysctlnode *cnode, *rnode;
81 
82 	if ((rnode = ath_sysctl_treetop(clog)) == NULL)
83 		return;
84 
85 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "dwell",
86 	    "channel dwell time (ms) for AP/station scanning",
87 	    dwelltime)) != 0)
88 		goto err;
89 
90 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "calibrate",
91 	    "chip calibration interval (secs)", calinterval)) != 0)
92 		goto err;
93 
94 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "outdoor",
95 	    "outdoor operation", outdoor)) != 0)
96 		goto err;
97 
98 	/* country code */
99 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE,
100 	    "countrycode", "country code", countrycode)) != 0)
101 		goto err;
102 
103 	/* regulatory domain */
104 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "regdomain",
105 	    "EEPROM regdomain code", regdomain)) != 0)
106 		goto err;
107 
108 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "debug",
109 	    "control debugging printfs", debug)) != 0)
110 		goto err;
111 
112 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "rxbuf",
113 	    "rx buffers allocated", rxbuf)) != 0)
114 		goto err;
115 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "txbuf",
116 	    "tx buffers allocated", txbuf)) != 0)
117 		goto err;
118 
119 	return;
120 err:
121 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
122 }
123 
124 static int
125 ath_sysctl_slottime(SYSCTLFN_ARGS)
126 {
127 	struct ath_softc *sc;
128 	struct sysctlnode node;
129 	u_int slottime;
130 	int error;
131 
132 	node = *rnode;
133 	sc = (struct ath_softc *)node.sysctl_data;
134 	slottime = ath_hal_getslottime(sc->sc_ah);
135 	node.sysctl_data = &slottime;
136 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
137 	if (error || newp == NULL)
138 		return error;
139 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
140 }
141 
142 static int
143 ath_sysctl_acktimeout(SYSCTLFN_ARGS)
144 {
145 	struct ath_softc *sc;
146 	struct sysctlnode node;
147 	u_int acktimeout;
148 	int error;
149 
150 	node = *rnode;
151 	sc = (struct ath_softc *)node.sysctl_data;
152 	acktimeout = ath_hal_getacktimeout(sc->sc_ah);
153 	node.sysctl_data = &acktimeout;
154 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
155 	if (error || newp == NULL)
156 		return error;
157 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
158 }
159 
160 static int
161 ath_sysctl_ctstimeout(SYSCTLFN_ARGS)
162 {
163 	struct ath_softc *sc;
164 	struct sysctlnode node;
165 	u_int ctstimeout;
166 	int error;
167 
168 	node = *rnode;
169 	sc = (struct ath_softc *)node.sysctl_data;
170 	ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
171 	node.sysctl_data = &ctstimeout;
172 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
173 	if (error || newp == NULL)
174 		return error;
175 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
176 }
177 
178 static int
179 ath_sysctl_softled(SYSCTLFN_ARGS)
180 {
181 	struct ath_softc *sc;
182 	struct sysctlnode node;
183 	int softled;
184 	int error;
185 
186 	node = *rnode;
187 	sc = (struct ath_softc *)node.sysctl_data;
188 	softled = sc->sc_softled;
189 	node.sysctl_data = &softled;
190 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
191 	if (error || newp == NULL)
192 		return error;
193 	softled = (softled != 0);
194 	if (softled != sc->sc_softled) {
195 		if (softled) {
196 			/* NB: handle any sc_ledpin change */
197 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
198 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
199 				!sc->sc_ledon);
200 		}
201 		sc->sc_softled = softled;
202 	}
203 	return 0;
204 }
205 
206 static int
207 ath_sysctl_rxantenna(SYSCTLFN_ARGS)
208 {
209 	struct ath_softc *sc;
210 	struct sysctlnode node;
211 	u_int defantenna;
212 	int error;
213 
214 	node = *rnode;
215 	sc = (struct ath_softc *)node.sysctl_data;
216 	defantenna = ath_hal_getdefantenna(sc->sc_ah);
217 	node.sysctl_data = &defantenna;
218 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
219 	if (error || newp == NULL)
220 		return error;
221 	ath_hal_setdefantenna(sc->sc_ah, defantenna);
222 	return 0;
223 }
224 
225 static int
226 ath_sysctl_diversity(SYSCTLFN_ARGS)
227 {
228 	struct ath_softc *sc;
229 	struct sysctlnode node;
230 	u_int diversity;
231 	int error;
232 
233 	node = *rnode;
234 	sc = (struct ath_softc *)node.sysctl_data;
235 	diversity = sc->sc_diversity;
236 	node.sysctl_data = &diversity;
237 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
238 	if (error || newp == NULL)
239 		return error;
240 	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
241 		return EINVAL;
242 	sc->sc_diversity = diversity;
243 	return 0;
244 }
245 
246 static int
247 ath_sysctl_diag(SYSCTLFN_ARGS)
248 {
249 	struct ath_softc *sc;
250 	struct sysctlnode node;
251 	u_int32_t diag;
252 	int error;
253 
254 	node = *rnode;
255 	sc = (struct ath_softc *)node.sysctl_data;
256 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
257 		return EINVAL;
258 	node.sysctl_data = &diag;
259 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
260 	if (error || newp == NULL)
261 		return error;
262 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
263 }
264 
265 static int
266 ath_sysctl_tpscale(SYSCTLFN_ARGS)
267 {
268 	struct ath_softc *sc;
269 	struct sysctlnode node;
270 	u_int32_t scale;
271 	int error;
272 
273 	node = *rnode;
274 	sc = (struct ath_softc *)node.sysctl_data;
275 	(void)ath_hal_gettpscale(sc->sc_ah, &scale);
276 	node.sysctl_data = &scale;
277 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
278 	if (error || newp == NULL)
279 		return error;
280 	return !ath_hal_settpscale(sc->sc_ah, scale)
281 	    ? EINVAL
282 	    : ath_reset(&sc->sc_if);
283 }
284 
285 static int
286 ath_sysctl_tpc(SYSCTLFN_ARGS)
287 {
288 	struct ath_softc *sc;
289 	struct sysctlnode node;
290 	u_int tpc;
291 	int error;
292 
293 	node = *rnode;
294 	sc = (struct ath_softc *)node.sysctl_data;
295 	tpc = ath_hal_gettpc(sc->sc_ah);
296 	node.sysctl_data = &tpc;
297 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
298 	if (error || newp == NULL)
299 		return error;
300 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
301 }
302 
303 static int
304 ath_sysctl_rfkill(SYSCTLFN_ARGS)
305 {
306 	struct ath_softc *sc;
307 	struct sysctlnode node;
308 	u_int rfkill;
309 	int error;
310 
311 	node = *rnode;
312 	sc = (struct ath_softc *)node.sysctl_data;
313 	rfkill = ath_hal_getrfkill(sc->sc_ah);
314 	node.sysctl_data = &rfkill;
315 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
316 	if (error || newp == NULL)
317 		return error;
318 	return !ath_hal_setrfkill(sc->sc_ah, rfkill) ? EINVAL : 0;
319 }
320 
321 static int
322 ath_sysctl_rfsilent(SYSCTLFN_ARGS)
323 {
324 	struct ath_softc *sc;
325 	struct sysctlnode node;
326 	u_int rfsilent;
327 	int error;
328 
329 	node = *rnode;
330 	sc = (struct ath_softc *)node.sysctl_data;
331 	(void)ath_hal_getrfsilent(sc->sc_ah, &rfsilent);
332 	node.sysctl_data = &rfsilent;
333 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
334 	if (error || newp == NULL)
335 		return error;
336 	return !ath_hal_setrfsilent(sc->sc_ah, rfsilent) ? EINVAL : 0;
337 }
338 
339 static int
340 ath_sysctl_regdomain(SYSCTLFN_ARGS)
341 {
342 	struct ath_softc *sc;
343 	struct sysctlnode node;
344 	u_int32_t rd;
345 	int error;
346 
347 	node = *rnode;
348 	sc = (struct ath_softc *)node.sysctl_data;
349 	if (!ath_hal_getregdomain(sc->sc_ah, &rd))
350 		return EINVAL;
351 	node.sysctl_data = &rd;
352 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
353 	if (error || newp == NULL)
354 		return error;
355 	return !ath_hal_setregdomain(sc->sc_ah, rd) ? EINVAL : 0;
356 }
357 
358 static int
359 ath_sysctl_tpack(SYSCTLFN_ARGS)
360 {
361 	struct ath_softc *sc;
362 	struct sysctlnode node;
363 	u_int32_t tpack;
364 	int error;
365 
366 	node = *rnode;
367 	sc = (struct ath_softc *)node.sysctl_data;
368 	(void)ath_hal_gettpack(sc->sc_ah, &tpack);
369 	node.sysctl_data = &tpack;
370 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
371 	if (error || newp == NULL)
372 		return error;
373 	return !ath_hal_settpack(sc->sc_ah, tpack) ? EINVAL : 0;
374 }
375 
376 static int
377 ath_sysctl_tpcts(SYSCTLFN_ARGS)
378 {
379 	struct ath_softc *sc;
380 	struct sysctlnode node;
381 	u_int32_t tpcts;
382 	int error;
383 
384 	node = *rnode;
385 	sc = (struct ath_softc *)node.sysctl_data;
386 	(void)ath_hal_gettpcts(sc->sc_ah, &tpcts);
387 	node.sysctl_data = &tpcts;
388 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
389 	if (error || newp == NULL)
390 		return error;
391 	return !ath_hal_settpcts(sc->sc_ah, tpcts) ? EINVAL : 0;
392 }
393 
394 const struct sysctlnode *
395 ath_sysctl_instance(const char *dvname, struct sysctllog **log)
396 {
397 	int rc;
398 	const struct sysctlnode *rnode;
399 
400 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
401 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
402 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
403 		goto err;
404 
405 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
406 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, dvname,
407 	    SYSCTL_DESCR("ath information and options"),
408 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
409 		goto err;
410 
411 	return rnode;
412 err:
413 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
414 	return NULL;
415 }
416 
417 const struct sysctlnode *
418 ath_sysctl_treetop(struct sysctllog **log)
419 {
420 	int rc;
421 	const struct sysctlnode *rnode;
422 
423 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
424 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
425 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
426 		goto err;
427 
428 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
429 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
430 	    SYSCTL_DESCR("ath information and options"),
431 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
432 		goto err;
433 
434 	return rnode;
435 err:
436 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
437 	return NULL;
438 }
439 
440 void
441 ath_sysctlattach(struct ath_softc *sc)
442 {
443 	int rc;
444 	struct sysctllog **log = &sc->sc_sysctllog;
445 	const struct sysctlnode *cnode, *rnode;
446 
447 	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
448 	(void)ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
449 	sc->sc_debug = ath_debug;
450 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
451 
452 	if ((rnode = ath_sysctl_instance(device_xname(sc->sc_dev), log)) == NULL)
453 		return;
454 
455 	if ((rc = SYSCTL_INT(0, countrycode, "EEPROM country code")) != 0)
456 		goto err;
457 
458 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, debug,
459 	    "control debugging printfs")) != 0)
460 		goto err;
461 
462 #if 0
463 	/* channel dwell time (ms) for AP/station scanning */
464 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, dwell,
465 	    "Channel dwell time (ms) for scanning")) != 0)
466 		goto err;
467 #endif
468 
469 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, slottime,
470 	    "802.11 slot time (us)")) != 0)
471 		goto err;
472 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, acktimeout,
473 	    "802.11 ACK timeout (us)")) != 0)
474 		goto err;
475 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, ctstimeout,
476 	    "802.11 CTS timeout (us)")) != 0)
477 		goto err;
478 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, softled,
479 	    "enable/disable software LED support")) != 0)
480 		goto err;
481 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledpin,
482 	    "GPIO pin connected to LED")) != 0)
483 		goto err;
484 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledon,
485 	    "setting to turn LED on")) != 0)
486 		goto err;
487 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledidle,
488 	    "idle time for inactivity LED (ticks)")) != 0)
489 		goto err;
490 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txantenna,
491 	    "tx antenna (0=auto)")) != 0)
492 		goto err;
493 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rxantenna,
494 	    "default/rx antenna")) != 0)
495 		goto err;
496 	if (ath_hal_hasdiversity(sc->sc_ah)) {
497 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diversity,
498 		    "antenna diversity")) != 0)
499 			goto err;
500 	}
501 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txintrperiod,
502 	    "tx descriptor batching")) != 0)
503 		goto err;
504 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diag,
505 	    "h/w diagnostic control")) != 0)
506 		goto err;
507 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpscale,
508 	    "tx power scaling")) != 0)
509 		goto err;
510 	if (ath_hal_hastpc(sc->sc_ah)) {
511 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpc,
512 		    "enable/disable per-packet TPC")) != 0)
513 			goto err;
514 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpack,
515 		    "tx power for ack frames")) != 0)
516 			goto err;
517 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpcts,
518 		    "tx power for cts frames")) != 0)
519 			goto err;
520 	}
521 	if (ath_hal_hasrfsilent(sc->sc_ah)) {
522 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rfsilent,
523 		    "h/w RF silent config")) != 0)
524 			goto err;
525 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rfkill,
526 		    "enable/disable RF kill switch")) != 0)
527 			goto err;
528 	}
529 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, regdomain,
530 	    "EEPROM regdomain code")) != 0)
531 		goto err;
532 	return;
533 err:
534 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
535 }
536