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