xref: /netbsd-src/sys/dev/ic/ath_netbsd.c (revision fd8cfc5edb3ad8dade159e425635a85153a56292)
1 /*	$NetBSD: ath_netbsd.c,v 1.6 2006/02/05 06:03:26 xtraeme 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.6 2006/02/05 06:03:26 xtraeme 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 <machine/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(struct device dv, const char *fmt, ...)
65 {
66         va_list ap;
67 
68         va_start(ap, fmt);
69         printf("%s: ", dv.dv_xname);
70         vprintf(fmt, ap);
71         va_end(ap);
72 	return;
73 }
74 
75 struct mbuf *
76 m_defrag(struct mbuf *m0, int flags)
77 {
78 	struct mbuf *m;
79 	MGETHDR(m, flags, MT_DATA);
80 	if (m == NULL)
81 		return NULL;
82 
83 	M_COPY_PKTHDR(m, m0);
84 	MCLGET(m, flags);
85 	if ((m->m_flags & M_EXT) == 0) {
86 		m_free(m);
87 		return NULL;
88 	}
89 	m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
90 	m->m_len = m->m_pkthdr.len;
91 	return m;
92 }
93 
94 /*
95  * Setup sysctl(3) MIB, ath.*.
96  *
97  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
98  */
99 SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
100 {
101 	int rc;
102 	const struct sysctlnode *cnode, *rnode;
103 
104 	if ((rnode = ath_sysctl_treetop(clog)) == NULL)
105 		return;
106 
107 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "dwell",
108 	    "channel dwell time (ms) for AP/station scanning",
109 	    dwelltime)) != 0)
110 		goto err;
111 
112 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "calibrate",
113 	    "chip calibration interval (secs)", calinterval)) != 0)
114 		goto err;
115 
116 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "outdoor",
117 	    "outdoor operation", outdoor)) != 0)
118 		goto err;
119 
120 	/* country code */
121 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE,
122 	    "countrycode", "country code", countrycode)) != 0)
123 		goto err;
124 
125 	/* regulatory domain */
126 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READONLY, "regdomain",
127 	    "regulatory domain", regdomain)) != 0)
128 		goto err;
129 
130 	if ((rc = SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "debug",
131 	    "control debugging printfs", debug)) != 0)
132 		goto err;
133 
134 	return;
135 err:
136 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
137 }
138 
139 static int
140 ath_sysctl_slottime(SYSCTLFN_ARGS)
141 {
142 	struct ath_softc *sc;
143 	struct sysctlnode node;
144 	u_int slottime;
145 	int error;
146 
147 	node = *rnode;
148 	sc = (struct ath_softc *)node.sysctl_data;
149 	slottime = ath_hal_getslottime(sc->sc_ah);
150 	node.sysctl_data = &slottime;
151 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
152 	if (error || newp == NULL)
153 		return error;
154 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
155 }
156 
157 static int
158 ath_sysctl_acktimeout(SYSCTLFN_ARGS)
159 {
160 	struct ath_softc *sc;
161 	struct sysctlnode node;
162 	u_int acktimeout;
163 	int error;
164 
165 	node = *rnode;
166 	sc = (struct ath_softc *)node.sysctl_data;
167 	acktimeout = ath_hal_getacktimeout(sc->sc_ah);
168 	node.sysctl_data = &acktimeout;
169 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
170 	if (error || newp == NULL)
171 		return error;
172 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
173 }
174 
175 static int
176 ath_sysctl_ctstimeout(SYSCTLFN_ARGS)
177 {
178 	struct ath_softc *sc;
179 	struct sysctlnode node;
180 	u_int ctstimeout;
181 	int error;
182 
183 	node = *rnode;
184 	sc = (struct ath_softc *)node.sysctl_data;
185 	ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
186 	node.sysctl_data = &ctstimeout;
187 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
188 	if (error || newp == NULL)
189 		return error;
190 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
191 }
192 
193 static int
194 ath_sysctl_softled(SYSCTLFN_ARGS)
195 {
196 	struct ath_softc *sc;
197 	struct sysctlnode node;
198 	int softled;
199 	int error;
200 
201 	node = *rnode;
202 	sc = (struct ath_softc *)node.sysctl_data;
203 	softled = sc->sc_softled;
204 	node.sysctl_data = &softled;
205 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
206 	if (error || newp == NULL)
207 		return error;
208 	softled = (softled != 0);
209 	if (softled != sc->sc_softled) {
210 		if (softled) {
211 			/* NB: handle any sc_ledpin change */
212 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
213 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
214 				!sc->sc_ledon);
215 		}
216 		sc->sc_softled = softled;
217 	}
218 	return 0;
219 }
220 
221 static int
222 ath_sysctl_rxantenna(SYSCTLFN_ARGS)
223 {
224 	struct ath_softc *sc;
225 	struct sysctlnode node;
226 	u_int defantenna;
227 	int error;
228 
229 	node = *rnode;
230 	sc = (struct ath_softc *)node.sysctl_data;
231 	defantenna = ath_hal_getdefantenna(sc->sc_ah);
232 	node.sysctl_data = &defantenna;
233 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
234 	if (error || newp == NULL)
235 		return error;
236 	ath_hal_setdefantenna(sc->sc_ah, defantenna);
237 	return 0;
238 }
239 
240 static int
241 ath_sysctl_diversity(SYSCTLFN_ARGS)
242 {
243 	struct ath_softc *sc;
244 	struct sysctlnode node;
245 	u_int diversity;
246 	int error;
247 
248 	node = *rnode;
249 	sc = (struct ath_softc *)node.sysctl_data;
250 	diversity = sc->sc_diversity;
251 	node.sysctl_data = &diversity;
252 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
253 	if (error || newp == NULL)
254 		return error;
255 	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
256 		return EINVAL;
257 	sc->sc_diversity = diversity;
258 	return 0;
259 }
260 
261 static int
262 ath_sysctl_diag(SYSCTLFN_ARGS)
263 {
264 	struct ath_softc *sc;
265 	struct sysctlnode node;
266 	u_int32_t diag;
267 	int error;
268 
269 	node = *rnode;
270 	sc = (struct ath_softc *)node.sysctl_data;
271 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
272 		return EINVAL;
273 	node.sysctl_data = &diag;
274 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
275 	if (error || newp == NULL)
276 		return error;
277 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
278 }
279 
280 static int
281 ath_sysctl_tpscale(SYSCTLFN_ARGS)
282 {
283 	struct ath_softc *sc;
284 	struct sysctlnode node;
285 	u_int32_t scale;
286 	int error;
287 
288 	node = *rnode;
289 	sc = (struct ath_softc *)node.sysctl_data;
290 	ath_hal_gettpscale(sc->sc_ah, &scale);
291 	node.sysctl_data = &scale;
292 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
293 	if (error || newp == NULL)
294 		return error;
295 	return !ath_hal_settpscale(sc->sc_ah, scale)
296 	    ? EINVAL
297 	    : ath_reset(&sc->sc_if);
298 }
299 
300 static int
301 ath_sysctl_tpc(SYSCTLFN_ARGS)
302 {
303 	struct ath_softc *sc;
304 	struct sysctlnode node;
305 	u_int tpc;
306 	int error;
307 
308 	node = *rnode;
309 	sc = (struct ath_softc *)node.sysctl_data;
310 	tpc = ath_hal_gettpc(sc->sc_ah);
311 	node.sysctl_data = &tpc;
312 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
313 	if (error || newp == NULL)
314 		return error;
315 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
316 }
317 
318 const struct sysctlnode *
319 ath_sysctl_instance(const char *dvname, struct sysctllog **log)
320 {
321 	int rc;
322 	const struct sysctlnode *rnode;
323 
324 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
325 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
326 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
327 		goto err;
328 
329 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
330 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, dvname,
331 	    SYSCTL_DESCR("ath information and options"),
332 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
333 		goto err;
334 
335 	return rnode;
336 err:
337 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
338 	return NULL;
339 }
340 
341 const struct sysctlnode *
342 ath_sysctl_treetop(struct sysctllog **log)
343 {
344 	int rc;
345 	const struct sysctlnode *rnode;
346 
347 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
348 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
349 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
350 		goto err;
351 
352 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
353 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
354 	    SYSCTL_DESCR("ath information and options"),
355 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
356 		goto err;
357 
358 	return rnode;
359 err:
360 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
361 	return NULL;
362 }
363 
364 void
365 ath_sysctlattach(struct ath_softc *sc)
366 {
367 	int rc;
368 	struct sysctllog **log = &sc->sc_sysctllog;
369 	const struct sysctlnode *cnode, *rnode;
370 
371 	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
372 	ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
373 	sc->sc_debug = ath_debug;
374 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
375 
376 	if ((rnode = ath_sysctl_instance(sc->sc_dev.dv_xname, log)) == NULL)
377 		return;
378 
379 	if ((rc = SYSCTL_INT(0, countrycode, "EEPROM country code")) != 0)
380 		goto err;
381 
382 	if ((rc = SYSCTL_INT(0, regdomain, "EEPROM regdomain code")) != 0)
383 		goto err;
384 
385 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, debug,
386 	    "control debugging printfs")) != 0)
387 		goto err;
388 
389 #if 0
390 	/* channel dwell time (ms) for AP/station scanning */
391 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, dwell,
392 	    "Channel dwell time (ms) for scanning")) != 0)
393 		goto err;
394 #endif
395 
396 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, slottime,
397 	    "802.11 slot time (us)")) != 0)
398 		goto err;
399 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, acktimeout,
400 	    "802.11 ACK timeout (us)")) != 0)
401 		goto err;
402 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, ctstimeout,
403 	    "802.11 CTS timeout (us)")) != 0)
404 		goto err;
405 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, softled,
406 	    "enable/disable software LED support")) != 0)
407 		goto err;
408 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledpin,
409 	    "GPIO pin connected to LED")) != 0)
410 		goto err;
411 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledon,
412 	    "setting to turn LED on")) != 0)
413 		goto err;
414 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, ledidle,
415 	    "idle time for inactivity LED (ticks)")) != 0)
416 		goto err;
417 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txantenna,
418 	    "tx antenna (0=auto)")) != 0)
419 		goto err;
420 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, rxantenna,
421 	    "default/rx antenna")) != 0)
422 		goto err;
423 	if (ath_hal_hasdiversity(sc->sc_ah)) {
424 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diversity,
425 		    "antenna diversity")) != 0)
426 			goto err;
427 	}
428 	if ((rc = SYSCTL_INT(CTLFLAG_READWRITE, txintrperiod,
429 	    "tx descriptor batching")) != 0)
430 		goto err;
431 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, diag,
432 	    "h/w diagnostic control")) != 0)
433 		goto err;
434 	if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpscale,
435 	    "tx power scaling")) != 0)
436 		goto err;
437 	if (ath_hal_hastpc(sc->sc_ah)) {
438 		if ((rc = SYSCTL_INT_SUBR(CTLFLAG_READWRITE, tpc,
439 		    "enable/disable per-packet TPC")) != 0)
440 			goto err;
441 	}
442 	return;
443 err:
444 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
445 }
446