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