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