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