Home | History | Annotate | Line # | Download | only in ic
wi.c revision 1.105
      1 /*	$NetBSD: wi.c,v 1.105 2002/12/27 07:54:35 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
     37  *
     38  * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
     39  * Electrical Engineering Department
     40  * Columbia University, New York City
     41  */
     42 
     43 /*
     44  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
     45  * from Lucent. Unlike the older cards, the new ones are programmed
     46  * entirely via a firmware-driven controller called the Hermes.
     47  * Unfortunately, Lucent will not release the Hermes programming manual
     48  * without an NDA (if at all). What they do release is an API library
     49  * called the HCF (Hardware Control Functions) which is supposed to
     50  * do the device-specific operations of a device driver for you. The
     51  * publically available version of the HCF library (the 'HCF Light') is
     52  * a) extremely gross, b) lacks certain features, particularly support
     53  * for 802.11 frames, and c) is contaminated by the GNU Public License.
     54  *
     55  * This driver does not use the HCF or HCF Light at all. Instead, it
     56  * programs the Hermes controller directly, using information gleaned
     57  * from the HCF Light code and corresponding documentation.
     58  *
     59  * This driver supports both the PCMCIA and ISA versions of the
     60  * WaveLAN/IEEE cards. Note however that the ISA card isn't really
     61  * anything of the sort: it's actually a PCMCIA bridge adapter
     62  * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
     63  * inserted. Consequently, you need to use the pccard support for
     64  * both the ISA and PCMCIA adapters.
     65  */
     66 
     67 /*
     68  * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
     69  * Oslo IETF plenary meeting.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.105 2002/12/27 07:54:35 dyoung Exp $");
     74 
     75 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
     76 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
     77 
     78 #include "bpfilter.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/callout.h>
     83 #include <sys/device.h>
     84 #include <sys/socket.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/kernel.h>		/* for hz */
     88 #include <sys/proc.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_dl.h>
     92 #include <net/if_media.h>
     93 #include <net/if_ether.h>
     94 #include <net/if_ieee80211.h>
     95 
     96 #if NBPFILTER > 0
     97 #include <net/bpf.h>
     98 #include <net/bpfdesc.h>
     99 #endif
    100 
    101 #include <machine/bus.h>
    102 
    103 #include <dev/ic/wi_ieee.h>
    104 #include <dev/ic/wireg.h>
    105 #include <dev/ic/wivar.h>
    106 
    107 static int  wi_init(struct ifnet *);
    108 static void wi_stop(struct ifnet *, int);
    109 static void wi_start(struct ifnet *);
    110 static int  wi_reset(struct wi_softc *);
    111 static void wi_watchdog(struct ifnet *);
    112 static int  wi_ioctl(struct ifnet *, u_long, caddr_t);
    113 static int  wi_media_change(struct ifnet *);
    114 static void wi_media_status(struct ifnet *, struct ifmediareq *);
    115 
    116 static void wi_rx_intr(struct wi_softc *);
    117 static void wi_tx_intr(struct wi_softc *);
    118 static void wi_info_intr(struct wi_softc *);
    119 
    120 static int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
    121 static int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
    122 static int  wi_write_txrate(struct wi_softc *);
    123 static int  wi_write_wep(struct wi_softc *);
    124 static int  wi_write_multi(struct wi_softc *);
    125 static int  wi_alloc_fid(struct wi_softc *, int, int *);
    126 static void wi_read_nicid(struct wi_softc *);
    127 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
    128 
    129 static int  wi_cmd(struct wi_softc *, int, int, int, int);
    130 static int  wi_seek_bap(struct wi_softc *, int, int);
    131 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
    132 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
    133 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
    134 static int  wi_write_rid(struct wi_softc *, int, void *, int);
    135 
    136 static int  wi_newstate(void *, enum ieee80211_state);
    137 
    138 static int  wi_scan_ap(struct wi_softc *);
    139 static void wi_scan_result(struct wi_softc *, int, int);
    140 
    141 static inline int
    142 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
    143 {
    144 
    145 	val = htole16(val);
    146 	return wi_write_rid(sc, rid, &val, sizeof(val));
    147 }
    148 
    149 #ifdef WI_DEBUG
    150 int wi_debug = 0;
    151 
    152 #define	DPRINTF(X)	if (wi_debug) printf X
    153 #define	DPRINTF2(X)	if (wi_debug > 1) printf X
    154 #else
    155 #define	DPRINTF(X)
    156 #define	DPRINTF2(X)
    157 #endif
    158 
    159 #define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
    160 
    161 struct wi_card_ident
    162 wi_card_ident[] = {
    163 	/* CARD_ID			CARD_NAME		FIRM_TYPE */
    164 	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
    165 	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
    166 	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
    167 	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
    168 	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
    169 	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
    170 	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
    171 	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
    172 	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
    173 	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
    174 	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
    175 	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
    176 	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    177 	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    178 	{ WI_NIC_3842_PCMCIA_ATM_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    179 	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    180 	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    181 	{ WI_NIC_3842_MINI_ATM_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    182 	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    183 	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    184 	{ WI_NIC_3842_PCI_ATM_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    185 	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    186 	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    187 	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    188 	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    189 	{ 0,	NULL,	0 },
    190 };
    191 
    192 int
    193 wi_attach(struct wi_softc *sc)
    194 {
    195 	struct ieee80211com *ic = &sc->sc_ic;
    196 	struct ifnet *ifp = &ic->ic_if;
    197 	int i, nrate, mword, buflen;
    198 	u_int8_t r;
    199 	u_int16_t val;
    200 	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
    201 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
    202 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    203 	};
    204 	int s;
    205 
    206 	s = splnet();
    207 
    208 	/* Make sure interrupts are disabled. */
    209 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    210 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    211 
    212 	/* Reset the NIC. */
    213 	if (wi_reset(sc) != 0) {
    214 		splx(s);
    215 		return 1;
    216 	}
    217 
    218 	buflen = IEEE80211_ADDR_LEN;
    219 	if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 ||
    220 	    IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
    221 		printf(" could not get mac address, attach failed\n");
    222 		splx(s);
    223 		return 1;
    224 	}
    225 
    226 	printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
    227 
    228 	/* Read NIC identification */
    229 	wi_read_nicid(sc);
    230 
    231 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    232 	ifp->if_softc = sc;
    233 	ifp->if_start = wi_start;
    234 	ifp->if_ioctl = wi_ioctl;
    235 	ifp->if_watchdog = wi_watchdog;
    236 	ifp->if_init = wi_init;
    237 	ifp->if_stop = wi_stop;
    238 	ifp->if_flags =
    239 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
    240 	IFQ_SET_READY(&ifp->if_snd);
    241 
    242 	ic->ic_phytype = IEEE80211_T_DS;
    243 	ic->ic_opmode = IEEE80211_M_STA;
    244 	ic->ic_flags = IEEE80211_F_HASPMGT | IEEE80211_F_HASAHDEMO;
    245 	ic->ic_state = IEEE80211_S_INIT;
    246 	ic->ic_newstate = wi_newstate;
    247 
    248 	/* Find available channel */
    249 	buflen = sizeof(val);
    250 	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
    251 		val = htole16(0x1fff);	/* assume 1-11 */
    252 	for (i = 0; i < 16; i++) {
    253 		if (isset((u_int8_t*)&val, i))
    254 			setbit(ic->ic_chan_avail, i + 1);
    255 	}
    256 
    257 	sc->sc_dbm_adjust = 100; /* default */
    258 
    259 	buflen = sizeof(val);
    260 	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
    261 	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
    262 		sc->sc_dbm_adjust = le16toh(val);
    263 	}
    264 
    265 	/* Find default IBSS channel */
    266 	buflen = sizeof(val);
    267 	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0)
    268 		ic->ic_ibss_chan = le16toh(val);
    269 	else {
    270 		/* use lowest available channel */
    271 		for (i = 0; i < 16; i++) {
    272 			if (isset(ic->ic_chan_avail, i))
    273 				break;
    274 		}
    275 		ic->ic_ibss_chan = i;
    276 	}
    277 
    278 	/*
    279 	 * Set flags based on firmware version.
    280 	 */
    281 	switch (sc->sc_firmware_type) {
    282 	case WI_LUCENT:
    283 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    284 #ifdef WI_HERMES_AUTOINC_WAR
    285 		/* XXX: not confirmed, but never seen for recent firmware */
    286 		if (sc->sc_sta_firmware_ver <  40000) {
    287 			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
    288 		}
    289 #endif
    290 		if (sc->sc_sta_firmware_ver >= 60000)
    291 			sc->sc_flags |= WI_FLAGS_HAS_MOR;
    292 		if (sc->sc_sta_firmware_ver >= 60006)
    293 			ic->ic_flags |= IEEE80211_F_HASIBSS;
    294 		sc->sc_ibss_port = 1;
    295 		break;
    296 
    297 	case WI_INTERSIL:
    298 		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
    299 		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
    300 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    301 		if (sc->sc_sta_firmware_ver > 10101)
    302 			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
    303 		if (sc->sc_sta_firmware_ver >= 800) {
    304 			ic->ic_flags |= IEEE80211_F_HASHOSTAP;
    305 			ic->ic_flags |= IEEE80211_F_HASIBSS;
    306 		}
    307 		sc->sc_ibss_port = 0;
    308 		break;
    309 
    310 	case WI_SYMBOL:
    311 		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
    312 		if (sc->sc_sta_firmware_ver >= 20000)
    313 			ic->ic_flags |= IEEE80211_F_HASIBSS;
    314 		sc->sc_ibss_port = 4;
    315 		break;
    316 	}
    317 
    318 	/*
    319 	 * Find out if we support WEP on this card.
    320 	 */
    321 	buflen = sizeof(val);
    322 	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
    323 	    val != htole16(0))
    324 		ic->ic_flags |= IEEE80211_F_HASWEP;
    325 
    326 	/* Find supported rates. */
    327 	buflen = sizeof(ratebuf);
    328 	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
    329 		nrate = le16toh(*(u_int16_t *)ratebuf);
    330 		if (nrate > IEEE80211_RATE_SIZE)
    331 			nrate = IEEE80211_RATE_SIZE;
    332 		memcpy(ic->ic_sup_rates, ratebuf + 2, nrate);
    333 	}
    334 	buflen = sizeof(val);
    335 
    336 	sc->sc_max_datalen = 2304;
    337 	sc->sc_rts_thresh = 2347;
    338 	sc->sc_frag_thresh = 2346;
    339 	sc->sc_system_scale = 1;
    340 	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
    341 	sc->sc_roaming_mode = 1;
    342 
    343 	ifmedia_init(&sc->sc_media, 0, wi_media_change, wi_media_status);
    344 	printf("%s: supported rates: ", sc->sc_dev.dv_xname);
    345 #define	ADD(s, o)	ifmedia_add(&sc->sc_media, \
    346 	IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
    347 	ADD(IFM_AUTO, 0);
    348 	if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
    349 		ADD(IFM_AUTO, IFM_IEEE80211_HOSTAP);
    350 	if (ic->ic_flags & IEEE80211_F_HASIBSS)
    351 		ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
    352 	ADD(IFM_AUTO, IFM_IEEE80211_ADHOC | IFM_FLAG0);
    353 	for (i = 0; i < nrate; i++) {
    354 		r = ic->ic_sup_rates[i];
    355 		mword = ieee80211_rate2media(r, IEEE80211_T_DS);
    356 		if (mword == 0)
    357 			continue;
    358 		printf("%s%d%sMbps", (i != 0 ? " " : ""),
    359 		    (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
    360 		ADD(mword, 0);
    361 		if (ic->ic_flags & IEEE80211_F_HASHOSTAP)
    362 			ADD(mword, IFM_IEEE80211_HOSTAP);
    363 		if (ic->ic_flags & IEEE80211_F_HASIBSS)
    364 			ADD(mword, IFM_IEEE80211_ADHOC);
    365 		ADD(mword, IFM_IEEE80211_ADHOC | IFM_FLAG0);
    366 	}
    367 	printf("\n");
    368 	ifmedia_set(&sc->sc_media, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0));
    369 #undef ADD
    370 
    371 	/*
    372 	 * Call MI attach routines.
    373 	 */
    374 
    375 	if_attach(ifp);
    376 	ieee80211_ifattach(ifp);
    377 
    378 	/* Attach is successful. */
    379 	sc->sc_attached = 1;
    380 
    381 	splx(s);
    382 	return 0;
    383 }
    384 
    385 int
    386 wi_detach(struct wi_softc *sc)
    387 {
    388 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    389 	int s;
    390 
    391 	if (!sc->sc_attached)
    392 		return 0;
    393 
    394 	s = splnet();
    395 
    396 	/* Delete all remaining media. */
    397 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
    398 
    399 	ieee80211_ifdetach(ifp);
    400 	if_detach(ifp);
    401 	if (sc->sc_enabled) {
    402 		if (sc->sc_disable)
    403 			(*sc->sc_disable)(sc);
    404 		sc->sc_enabled = 0;
    405 	}
    406 	splx(s);
    407 	return 0;
    408 }
    409 
    410 int
    411 wi_activate(struct device *self, enum devact act)
    412 {
    413 	struct wi_softc *sc = (struct wi_softc *)self;
    414 	int rv = 0, s;
    415 
    416 	s = splnet();
    417 	switch (act) {
    418 	case DVACT_ACTIVATE:
    419 		rv = EOPNOTSUPP;
    420 		break;
    421 
    422 	case DVACT_DEACTIVATE:
    423 		if_deactivate(&sc->sc_ic.ic_if);
    424 		break;
    425 	}
    426 	splx(s);
    427 	return rv;
    428 }
    429 
    430 void
    431 wi_power(struct wi_softc *sc, int why)
    432 {
    433 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    434 	int s;
    435 
    436 	s = splnet();
    437 	switch (why) {
    438 	case PWR_SUSPEND:
    439 	case PWR_STANDBY:
    440 		wi_stop(ifp, 1);
    441 		break;
    442 	case PWR_RESUME:
    443 		if (ifp->if_flags & IFF_UP) {
    444 			wi_init(ifp);
    445 			(void)wi_intr(sc);
    446 		}
    447 		break;
    448 	case PWR_SOFTSUSPEND:
    449 	case PWR_SOFTSTANDBY:
    450 	case PWR_SOFTRESUME:
    451 		break;
    452 	}
    453 	splx(s);
    454 }
    455 
    456 void
    457 wi_shutdown(struct wi_softc *sc)
    458 {
    459 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    460 
    461 	if (sc->sc_attached)
    462 		wi_stop(ifp, 1);
    463 }
    464 
    465 int
    466 wi_intr(void *arg)
    467 {
    468 	int i;
    469 	struct wi_softc	*sc = arg;
    470 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    471 	u_int16_t status, raw_status, last_status;
    472 
    473 	if (sc->sc_enabled == 0 ||
    474 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
    475 	    (ifp->if_flags & IFF_RUNNING) == 0)
    476 		return 0;
    477 
    478 	if ((ifp->if_flags & IFF_UP) == 0) {
    479 		CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    480 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    481 		return 1;
    482 	}
    483 
    484 	/* maximum 10 loops per interrupt */
    485 	last_status = 0;
    486 	for (i = 0; i < 10; i++) {
    487 		/*
    488 		 * Only believe a status bit when we enter wi_intr, or when
    489 		 * the bit was "off" the last time through the loop. This is
    490 		 * my strategy to avoid racing the hardware/firmware if I
    491 		 * can re-read the event status register more quickly than
    492 		 * it is updated.
    493 		 */
    494 		raw_status = CSR_READ_2(sc, WI_EVENT_STAT);
    495 		status = raw_status & ~last_status;
    496 		if ((status & WI_INTRS) == 0)
    497 			break;
    498 		last_status = raw_status;
    499 
    500 		if (status & WI_EV_RX)
    501 			wi_rx_intr(sc);
    502 
    503 		if (status & WI_EV_ALLOC)
    504 			wi_tx_intr(sc);
    505 
    506 		if (status & WI_EV_INFO)
    507 			wi_info_intr(sc);
    508 
    509 		if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
    510 		    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
    511 		    !IFQ_IS_EMPTY(&ifp->if_snd))
    512 			wi_start(ifp);
    513 	}
    514 
    515 	return 1;
    516 }
    517 
    518 static int
    519 wi_init(struct ifnet *ifp)
    520 {
    521 	struct wi_softc *sc = ifp->if_softc;
    522 	struct ieee80211com *ic = &sc->sc_ic;
    523 	struct wi_joinreq join;
    524 	int i;
    525 	int error = 0, wasenabled;
    526 
    527 	DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
    528 	wasenabled = sc->sc_enabled;
    529 	if (!sc->sc_enabled) {
    530 		if ((error = (*sc->sc_enable)(sc)) != 0)
    531 			goto out;
    532 		sc->sc_enabled = 1;
    533 	} else
    534 		wi_stop(ifp, 0);
    535 
    536 	/* Symbol firmware cannot be initialized more than once */
    537 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
    538 		if ((error = wi_reset(sc)) != 0)
    539 			goto out;
    540 	}
    541 
    542 	/* common 802.11 configuration */
    543 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
    544 	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
    545 	switch (ic->ic_opmode) {
    546 	case IEEE80211_M_STA:
    547 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
    548 		break;
    549 	case IEEE80211_M_IBSS:
    550 		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
    551 		ic->ic_flags |= IEEE80211_F_IBSSON;
    552 		break;
    553 	case IEEE80211_M_AHDEMO:
    554 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
    555 		break;
    556 	case IEEE80211_M_HOSTAP:
    557 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
    558 		break;
    559 	}
    560 
    561 	/* Intersil interprets this RID as joining ESS even in IBSS mode */
    562 	if (sc->sc_firmware_type == WI_LUCENT &&
    563 	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
    564 		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
    565 	else
    566 		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
    567 	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
    568 	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
    569 	    ic->ic_des_esslen);
    570 	wi_write_val(sc, WI_RID_OWN_CHNL, ic->ic_ibss_chan);
    571 	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
    572 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
    573 	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
    574 	wi_write_val(sc, WI_RID_PM_ENABLED,
    575 	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    576 
    577 	/* not yet common 802.11 configuration */
    578 	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
    579 	wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
    580 	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
    581 		wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh);
    582 
    583 	/* driver specific 802.11 configuration */
    584 	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
    585 		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
    586 	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
    587 		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
    588 	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
    589 		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
    590 	wi_write_txrate(sc);
    591 	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
    592 
    593 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    594 	    sc->sc_firmware_type == WI_INTERSIL) {
    595 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
    596 		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
    597 		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
    598 		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
    599 	}
    600 
    601 	/*
    602 	 * Initialize promisc mode.
    603 	 *	Being in the Host-AP mode causes a great
    604 	 *	deal of pain if primisc mode is set.
    605 	 *	Therefore we avoid confusing the firmware
    606 	 *	and always reset promisc mode in Host-AP
    607 	 *	mode.  Host-AP sees all the packets anyway.
    608 	 */
    609 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    610 	    (ifp->if_flags & IFF_PROMISC) != 0) {
    611 		wi_write_val(sc, WI_RID_PROMISC, 1);
    612 	} else {
    613 		wi_write_val(sc, WI_RID_PROMISC, 0);
    614 	}
    615 
    616 	/* Configure WEP. */
    617 	if (ic->ic_flags & IEEE80211_F_HASWEP)
    618 		wi_write_wep(sc);
    619 
    620 	/* Set multicast filter. */
    621 	wi_write_multi(sc);
    622 
    623 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
    624 		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
    625 		if (sc->sc_firmware_type == WI_SYMBOL)
    626 			sc->sc_buflen = 1585;	/* XXX */
    627 		for (i = 0; i < WI_NTXBUF; i++) {
    628 			error = wi_alloc_fid(sc, sc->sc_buflen,
    629 			    &sc->sc_txd[i].d_fid);
    630 			if (error) {
    631 				printf("%s: tx buffer allocation failed\n",
    632 				    sc->sc_dev.dv_xname);
    633 				goto out;
    634 			}
    635 			DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
    636 			    sc->sc_txd[i].d_fid));
    637 			sc->sc_txd[i].d_len = 0;
    638 		}
    639 	}
    640 	sc->sc_txcur = sc->sc_txnext = 0;
    641 
    642 	/* Enable port 0 */
    643 	wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
    644 	ifp->if_flags |= IFF_RUNNING;
    645 	ifp->if_flags &= ~IFF_OACTIVE;
    646 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
    647 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
    648 		wi_newstate(sc, IEEE80211_S_RUN);
    649 
    650 	/* Enable interrupts */
    651 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    652 
    653 	if (!wasenabled &&
    654 	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
    655 	    sc->sc_firmware_type == WI_INTERSIL) {
    656 		/* XXX: some card need to be re-enabled for hostap */
    657 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
    658 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
    659 	}
    660 
    661 	if (ic->ic_opmode == IEEE80211_M_STA &&
    662 	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
    663 	    ic->ic_des_chan != IEEE80211_CHAN_ANY)) {
    664 		memset(&join, 0, sizeof(join));
    665 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    666 			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
    667 		if (ic->ic_des_chan != IEEE80211_CHAN_ANY)
    668 			join.wi_chan = htole16(ic->ic_des_chan);
    669 		wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
    670 	}
    671 
    672  out:
    673 	if (error) {
    674 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    675 		wi_stop(ifp, 0);
    676 	}
    677 	DPRINTF(("wi_init: return %d\n", error));
    678 	return error;
    679 }
    680 
    681 static void
    682 wi_stop(struct ifnet *ifp, int disable)
    683 {
    684 	struct wi_softc	*sc = ifp->if_softc;
    685 
    686 	DPRINTF(("wi_stop: disable %d\n", disable));
    687 	ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
    688 	if (sc->sc_enabled) {
    689 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    690 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
    691 		if (disable) {
    692 			if (sc->sc_disable)
    693 				(*sc->sc_disable)(sc);
    694 			sc->sc_enabled = 0;
    695 		}
    696 	}
    697 
    698 	sc->sc_tx_timer = 0;
    699 	sc->sc_scan_timer = 0;
    700 	sc->sc_naps = 0;
    701 	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
    702 	ifp->if_timer = 0;
    703 }
    704 
    705 static void
    706 wi_start(struct ifnet *ifp)
    707 {
    708 	struct wi_softc	*sc = ifp->if_softc;
    709 	struct ieee80211com *ic = &sc->sc_ic;
    710 	struct ieee80211_node *ni;
    711 	struct ieee80211_frame *wh;
    712 	struct mbuf *m0, *m;
    713 	struct wi_frame frmhdr;
    714 	int cur, fid, off;
    715 
    716 	if (ifp->if_flags & IFF_OACTIVE)
    717 		return;
    718 	if (sc->sc_flags & WI_FLAGS_OUTRANGE)
    719 		return;
    720 
    721 	memset(&frmhdr, 0, sizeof(frmhdr));
    722 	cur = sc->sc_txnext;
    723 	for (;;) {
    724 		IF_POLL(&ic->ic_mgtq, m0);
    725 		if (m0 != NULL) {
    726 			if (sc->sc_txd[cur].d_len != 0) {
    727 				ifp->if_flags |= IFF_OACTIVE;
    728 				break;
    729 			}
    730 			IF_DEQUEUE(&ic->ic_mgtq, m0);
    731 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
    732 			    (caddr_t)&frmhdr.wi_ehdr);
    733 			frmhdr.wi_ehdr.ether_type = 0;
    734                         wh = mtod(m0, struct ieee80211_frame *);
    735 		} else {
    736 			if (ic->ic_state != IEEE80211_S_RUN)
    737 				break;
    738 			IFQ_POLL(&ifp->if_snd, m0);
    739 			if (m0 == NULL)
    740 				break;
    741 			if (sc->sc_txd[cur].d_len != 0) {
    742 				ifp->if_flags |= IFF_OACTIVE;
    743 				break;
    744 			}
    745 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    746 			ifp->if_opackets++;
    747 			m_copydata(m0, 0, ETHER_HDR_LEN,
    748 			    (caddr_t)&frmhdr.wi_ehdr);
    749 #if NBPFILTER > 0
    750 			if (ifp->if_bpf)
    751 				bpf_mtap(ifp->if_bpf, m0);
    752 #endif
    753 
    754 			if ((m0 = ieee80211_encap(ifp, m0)) == NULL) {
    755 				ifp->if_oerrors++;
    756 				continue;
    757 			}
    758                         wh = mtod(m0, struct ieee80211_frame *);
    759 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    760 			    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    761 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    762 			    IEEE80211_FC0_TYPE_DATA &&
    763 			    ((ni = ieee80211_find_node(ic, wh->i_addr1)) ==
    764 			    NULL || ni->ni_associd == 0)) {
    765 				m_freem(m0);
    766 				ifp->if_oerrors++;
    767 				continue;
    768 			}
    769 			if (ic->ic_flags & IEEE80211_F_WEPON)
    770 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
    771 
    772 		}
    773 #if NBPFILTER > 0
    774 		if (ic->ic_rawbpf)
    775 			bpf_mtap(ic->ic_rawbpf, m0);
    776 #endif
    777 		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11);
    778 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    779 		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
    780 			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
    781 				ifp->if_oerrors++;
    782 				continue;
    783 			}
    784 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
    785 		}
    786 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
    787 		    (caddr_t)&frmhdr.wi_whdr);
    788 		m_adj(m0, sizeof(struct ieee80211_frame));
    789 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
    790 #if NBPFILTER > 0
    791 		if (sc->sc_drvbpf) {
    792 			struct mbuf mb;
    793 
    794 			M_COPY_PKTHDR(&mb, m0);
    795 			mb.m_data = (caddr_t)&frmhdr;
    796 			mb.m_len = sizeof(frmhdr);
    797 			mb.m_next = m0;
    798 			mb.m_pkthdr.len += mb.m_len;
    799 			bpf_mtap(sc->sc_drvbpf, &mb);
    800 		}
    801 #endif
    802 		fid = sc->sc_txd[cur].d_fid;
    803 		wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr));
    804 		off = sizeof(frmhdr);
    805 		for (m = m0; m != NULL; m = m->m_next) {
    806 			if (m->m_len == 0)
    807 				continue;
    808 			wi_write_bap(sc, fid, off, m->m_data, m->m_len);
    809 			off += m->m_len;
    810 		}
    811 		m_freem(m0);
    812 		sc->sc_txd[cur].d_len = off;
    813 		if (sc->sc_txcur == cur) {
    814 			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
    815 				printf("%s: xmit failed\n",
    816 				    sc->sc_dev.dv_xname);
    817 				sc->sc_txd[cur].d_len = 0;
    818 				continue;
    819 			}
    820 			sc->sc_tx_timer = 5;
    821 			ifp->if_timer = 1;
    822 		}
    823 		sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
    824 	}
    825 }
    826 
    827 
    828 static int
    829 wi_reset(struct wi_softc *sc)
    830 {
    831 	int i, error;
    832 
    833 	DPRINTF(("wi_reset\n"));
    834 	error = 0;
    835 	for (i = 0; i < 5; i++) {
    836 		DELAY(20*1000);	/* XXX: way too long! */
    837 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
    838 			break;
    839 	}
    840 	if (error) {
    841 		printf("%s: init failed\n", sc->sc_dev.dv_xname);
    842 		return error;
    843 	}
    844 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    845 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    846 
    847 	/* Calibrate timer. */
    848 	wi_write_val(sc, WI_RID_TICK_TIME, 0);
    849 	return 0;
    850 }
    851 
    852 static void
    853 wi_watchdog(struct ifnet *ifp)
    854 {
    855 	struct wi_softc *sc = ifp->if_softc;
    856 
    857 	ifp->if_timer = 0;
    858 	if (!sc->sc_enabled)
    859 		return;
    860 
    861 	if (sc->sc_tx_timer) {
    862 		if (--sc->sc_tx_timer == 0) {
    863 			printf("%s: device timeout\n", ifp->if_xname);
    864 			ifp->if_oerrors++;
    865 			wi_init(ifp);
    866 			return;
    867 		}
    868 		ifp->if_timer = 1;
    869 	}
    870 
    871 	if (sc->sc_scan_timer) {
    872 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
    873 		    sc->sc_firmware_type == WI_INTERSIL) {
    874 			DPRINTF(("wi_watchdog: inquire scan\n"));
    875 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
    876 		}
    877 		if (sc->sc_scan_timer)
    878 			ifp->if_timer = 1;
    879 	}
    880 
    881 	/* TODO: rate control */
    882 	ieee80211_watchdog(ifp);
    883 }
    884 
    885 static int
    886 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    887 {
    888 	struct wi_softc *sc = ifp->if_softc;
    889 	struct ieee80211com *ic = &sc->sc_ic;
    890 	struct ifreq *ifr = (struct ifreq *)data;
    891 	int s, error = 0;
    892 
    893 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    894 		return ENXIO;
    895 
    896 	s = splnet();
    897 
    898 	switch (cmd) {
    899 	case SIOCSIFFLAGS:
    900 		if (ifp->if_flags & IFF_UP) {
    901 			if (sc->sc_enabled) {
    902 				/*
    903 				 * To avoid rescanning another access point,
    904 				 * do not call wi_init() here.  Instead,
    905 				 * only reflect promisc mode settings.
    906 				 */
    907 				if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    908 				    (ifp->if_flags & IFF_PROMISC) != 0)
    909 					wi_write_val(sc, WI_RID_PROMISC, 1);
    910 				else
    911 					wi_write_val(sc, WI_RID_PROMISC, 0);
    912 			} else
    913 				error = wi_init(ifp);
    914 		} else if (sc->sc_enabled)
    915 			wi_stop(ifp, 1);
    916 		break;
    917 	case SIOCSIFMEDIA:
    918 	case SIOCGIFMEDIA:
    919 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    920 		break;
    921 	case SIOCADDMULTI:
    922 	case SIOCDELMULTI:
    923 		error = (cmd == SIOCADDMULTI) ?
    924 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
    925 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
    926 		if (error == ENETRESET) {
    927 			if (sc->sc_enabled) {
    928 				/* do not rescan */
    929 				error = wi_write_multi(sc);
    930 			} else
    931 				error = 0;
    932 		}
    933 		break;
    934 	case SIOCGIFGENERIC:
    935 		error = wi_get_cfg(ifp, cmd, data);
    936 		break;
    937 	case SIOCSIFGENERIC:
    938 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    939 		if (error)
    940 			break;
    941 		error = wi_set_cfg(ifp, cmd, data);
    942 		if (error == ENETRESET) {
    943 			if (sc->sc_enabled)
    944 				error = wi_init(ifp);
    945 			else
    946 				error = 0;
    947 		}
    948 		break;
    949 	default:
    950 		error = ieee80211_ioctl(ifp, cmd, data);
    951 		if (error == ENETRESET) {
    952 			if (sc->sc_enabled)
    953 				error = wi_init(ifp);
    954 			else
    955 				error = 0;
    956 		}
    957 		break;
    958 	}
    959 	splx(s);
    960 	return error;
    961 }
    962 
    963 static int
    964 wi_media_change(struct ifnet *ifp)
    965 {
    966 	struct wi_softc *sc = ifp->if_softc;
    967 	struct ieee80211com *ic = &sc->sc_ic;
    968 	struct ifmedia_entry *ime;
    969 	enum ieee80211_opmode newmode;
    970 	int i, rate, error = 0;
    971 
    972 	ime = sc->sc_media.ifm_cur;
    973 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
    974 		i = -1;
    975 	} else {
    976 		rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS);
    977 		if (rate == 0)
    978 			return EINVAL;
    979 		for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
    980 			if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
    981 				break;
    982 		}
    983 		if (i == IEEE80211_RATE_SIZE)
    984 			return EINVAL;
    985 	}
    986 	if (ic->ic_fixed_rate != i) {
    987 		ic->ic_fixed_rate = i;
    988 		error = ENETRESET;
    989 	}
    990 
    991 	if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
    992 	    (ime->ifm_media & IFM_FLAG0))
    993 		newmode = IEEE80211_M_AHDEMO;
    994 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
    995 		newmode = IEEE80211_M_IBSS;
    996 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    997 		newmode = IEEE80211_M_HOSTAP;
    998 	else
    999 		newmode = IEEE80211_M_STA;
   1000 	if (ic->ic_opmode != newmode) {
   1001 		ic->ic_opmode = newmode;
   1002 		error = ENETRESET;
   1003 	}
   1004 	if (error == ENETRESET) {
   1005 		if (sc->sc_enabled)
   1006 			error = wi_init(ifp);
   1007 		else
   1008 			error = 0;
   1009 	}
   1010 	ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
   1011 
   1012 	return error;
   1013 }
   1014 
   1015 static void
   1016 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1017 {
   1018 	struct wi_softc *sc = ifp->if_softc;
   1019 	struct ieee80211com *ic = &sc->sc_ic;
   1020 	u_int16_t val;
   1021 	int rate, len;
   1022 
   1023 	if (sc->sc_enabled == 0) {
   1024 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   1025 		imr->ifm_status = 0;
   1026 		return;
   1027 	}
   1028 
   1029 	imr->ifm_status = IFM_AVALID;
   1030 	imr->ifm_active = IFM_IEEE80211;
   1031 	if (ic->ic_state == IEEE80211_S_RUN &&
   1032 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
   1033 		imr->ifm_status |= IFM_ACTIVE;
   1034 	len = sizeof(val);
   1035 	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
   1036 		rate = 0;
   1037 	else {
   1038 		/* convert to 802.11 rate */
   1039 		rate = val * 2;
   1040 		if (sc->sc_firmware_type == WI_LUCENT) {
   1041 			if (rate == 10)
   1042 				rate = 11;	/* 5.5Mbps */
   1043 		} else {
   1044 			if (rate == 4*2)
   1045 				rate = 11;	/* 5.5Mbps */
   1046 			else if (rate == 8*2)
   1047 				rate = 22;	/* 11Mbps */
   1048 		}
   1049 	}
   1050 	imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS);
   1051 	switch (ic->ic_opmode) {
   1052 	case IEEE80211_M_STA:
   1053 		break;
   1054 	case IEEE80211_M_IBSS:
   1055 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
   1056 		break;
   1057 	case IEEE80211_M_AHDEMO:
   1058 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   1059 		break;
   1060 	case IEEE80211_M_HOSTAP:
   1061 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
   1062 		break;
   1063 	}
   1064 }
   1065 
   1066 static void
   1067 wi_rx_intr(struct wi_softc *sc)
   1068 {
   1069 	struct ieee80211com *ic = &sc->sc_ic;
   1070 	struct ifnet *ifp = &ic->ic_if;
   1071 	struct wi_frame frmhdr;
   1072 	struct mbuf *m;
   1073 	struct ieee80211_frame *wh;
   1074 	int fid, len, off, rssi;
   1075 	u_int16_t status;
   1076 	u_int32_t rstamp;
   1077 
   1078 	fid = CSR_READ_2(sc, WI_RX_FID);
   1079 
   1080 	/* First read in the frame header */
   1081 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
   1082 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1083 		ifp->if_ierrors++;
   1084 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
   1085 		return;
   1086 	}
   1087 
   1088 	/*
   1089 	 * Drop undecryptable or packets with receive errors here
   1090 	 */
   1091 	status = le16toh(frmhdr.wi_status);
   1092 	if (status & WI_STAT_ERRSTAT) {
   1093 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1094 		ifp->if_ierrors++;
   1095 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
   1096 		return;
   1097 	}
   1098 	rssi = frmhdr.wi_rx_signal;
   1099 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
   1100 	    le16toh(frmhdr.wi_rx_tstamp1);
   1101 
   1102 	len = le16toh(frmhdr.wi_dat_len);
   1103 	off = ALIGN(sizeof(struct ieee80211_frame));
   1104 
   1105 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1106 	if (m == NULL) {
   1107 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1108 		ifp->if_ierrors++;
   1109 		DPRINTF(("wi_rx_intr: MGET failed\n"));
   1110 		return;
   1111 	}
   1112 	if (off + len > MHLEN) {
   1113 		MCLGET(m, M_DONTWAIT);
   1114 		if ((m->m_flags & M_EXT) == 0) {
   1115 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1116 			m_freem(m);
   1117 			ifp->if_ierrors++;
   1118 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
   1119 			return;
   1120 		}
   1121 	}
   1122 
   1123 	m->m_data += off - sizeof(struct ieee80211_frame);
   1124 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
   1125 	wi_read_bap(sc, fid, sizeof(frmhdr),
   1126 	    m->m_data + sizeof(struct ieee80211_frame), len);
   1127 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
   1128 	m->m_pkthdr.rcvif = ifp;
   1129 
   1130 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1131 
   1132 #if NBPFILTER > 0
   1133 	if (sc->sc_drvbpf) {
   1134 		struct mbuf mb;
   1135 
   1136 		M_COPY_PKTHDR(&mb, m);
   1137 		mb.m_data = (caddr_t)&frmhdr;
   1138 		mb.m_len = sizeof(frmhdr);
   1139 		mb.m_next = m;
   1140 		mb.m_pkthdr.len += mb.m_len;
   1141 		bpf_mtap(sc->sc_drvbpf, &mb);
   1142 	}
   1143 #endif
   1144 	wh = mtod(m, struct ieee80211_frame *);
   1145 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1146 		/*
   1147 		 * WEP is decrypted by hardware. Clear WEP bit
   1148 		 * header for ieee80211_input().
   1149 		 */
   1150 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1151 	}
   1152 	ieee80211_input(ifp, m, rssi, rstamp);
   1153 }
   1154 
   1155 static void
   1156 wi_tx_intr(struct wi_softc *sc)
   1157 {
   1158 	struct ieee80211com *ic = &sc->sc_ic;
   1159 	struct ifnet *ifp = &ic->ic_if;
   1160 	int fid, cur;
   1161 
   1162 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
   1163 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   1164 
   1165 	cur = sc->sc_txcur;
   1166 	if (sc->sc_txd[cur].d_fid != fid) {
   1167 		printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
   1168 		    sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
   1169 		    sc->sc_txnext);
   1170 		return;
   1171 	}
   1172 	sc->sc_tx_timer = 0;
   1173 	sc->sc_txd[cur].d_len = 0;
   1174 	sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
   1175 	if (sc->sc_txd[cur].d_len == 0)
   1176 		ifp->if_flags &= ~IFF_OACTIVE;
   1177 	else {
   1178 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
   1179 		    0, 0)) {
   1180 			printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
   1181 			sc->sc_txd[cur].d_len = 0;
   1182 		} else {
   1183 			sc->sc_tx_timer = 5;
   1184 			ifp->if_timer = 1;
   1185 		}
   1186 	}
   1187 }
   1188 
   1189 static void
   1190 wi_info_intr(struct wi_softc *sc)
   1191 {
   1192 	struct ieee80211com *ic = &sc->sc_ic;
   1193 	struct ifnet *ifp = &ic->ic_if;
   1194 	int i, fid, len, off;
   1195 	u_int16_t ltbuf[2];
   1196 	u_int16_t stat;
   1197 	u_int32_t *ptr;
   1198 
   1199 	fid = CSR_READ_2(sc, WI_INFO_FID);
   1200 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
   1201 
   1202 	switch (le16toh(ltbuf[1])) {
   1203 
   1204 	case WI_INFO_LINK_STAT:
   1205 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
   1206 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
   1207 		switch (le16toh(stat)) {
   1208 		case CONNECTED:
   1209 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1210 			if (ic->ic_state == IEEE80211_S_RUN &&
   1211 			    ic->ic_opmode != IEEE80211_M_IBSS)
   1212 				break;
   1213 			/* FALLTHROUGH */
   1214 		case AP_CHANGE:
   1215 			ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
   1216 			break;
   1217 		case AP_IN_RANGE:
   1218 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1219 			break;
   1220 		case AP_OUT_OF_RANGE:
   1221 			if (sc->sc_firmware_type == WI_SYMBOL &&
   1222 			    sc->sc_scan_timer > 0) {
   1223 				if (wi_cmd(sc, WI_CMD_INQUIRE,
   1224 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
   1225 					sc->sc_scan_timer = 0;
   1226 				break;
   1227 			}
   1228 			if (ic->ic_opmode == IEEE80211_M_STA)
   1229 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
   1230 			break;
   1231 		case DISCONNECTED:
   1232 		case ASSOC_FAILED:
   1233 			if (ic->ic_opmode == IEEE80211_M_STA)
   1234 				ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
   1235 			break;
   1236 		}
   1237 		break;
   1238 
   1239 	case WI_INFO_COUNTERS:
   1240 		/* some card versions have a larger stats structure */
   1241 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
   1242 		ptr = (u_int32_t *)&sc->sc_stats;
   1243 		off = sizeof(ltbuf);
   1244 		for (i = 0; i < len; i++, off += 2, ptr++) {
   1245 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
   1246 #ifdef WI_HERMES_STATS_WAR
   1247 			if (stat & 0xf000)
   1248 				stat = ~stat;
   1249 #endif
   1250 			*ptr += stat;
   1251 		}
   1252 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
   1253 		    sc->sc_stats.wi_tx_multi_retries +
   1254 		    sc->sc_stats.wi_tx_retry_limit;
   1255 		break;
   1256 
   1257 	case WI_INFO_SCAN_RESULTS:
   1258 	case WI_INFO_HOST_SCAN_RESULTS:
   1259 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
   1260 		break;
   1261 
   1262 	default:
   1263 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
   1264 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
   1265 		break;
   1266 	}
   1267 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
   1268 }
   1269 
   1270 /*
   1271  * Allocate a region of memory inside the NIC and zero
   1272  * it out.
   1273  */
   1274 static int
   1275 wi_write_multi(struct wi_softc *sc)
   1276 {
   1277 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1278 	int n = 0;
   1279 	struct wi_mcast mlist;
   1280 	struct ether_multi *enm;
   1281 	struct ether_multistep estep;
   1282 
   1283 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1284 allmulti:
   1285 		ifp->if_flags |= IFF_ALLMULTI;
   1286 		memset(&mlist, 0, sizeof(mlist));
   1287 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1288 		    sizeof(mlist));
   1289 	}
   1290 
   1291 	n = 0;
   1292 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1293 	while (enm != NULL) {
   1294 		/* Punt on ranges or too many multicast addresses. */
   1295 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
   1296 		    n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
   1297 			goto allmulti;
   1298 
   1299 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
   1300 		n++;
   1301 		ETHER_NEXT_MULTI(estep, enm);
   1302 	}
   1303 	ifp->if_flags &= ~IFF_ALLMULTI;
   1304 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1305 	    IEEE80211_ADDR_LEN * n);
   1306 }
   1307 
   1308 
   1309 static void
   1310 wi_read_nicid(sc)
   1311 	struct wi_softc *sc;
   1312 {
   1313 	struct wi_card_ident *id;
   1314 	char *p;
   1315 	int len;
   1316 	u_int16_t ver[4];
   1317 
   1318 	/* getting chip identity */
   1319 	memset(ver, 0, sizeof(ver));
   1320 	len = sizeof(ver);
   1321 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
   1322 	printf("%s: using ", sc->sc_dev.dv_xname);
   1323 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1324 
   1325 	sc->sc_firmware_type = WI_NOTYPE;
   1326 	for (id = wi_card_ident; id->card_name != NULL; id++) {
   1327 		if (le16toh(ver[0]) == id->card_id) {
   1328 			printf("%s", id->card_name);
   1329 			sc->sc_firmware_type = id->firm_type;
   1330 			break;
   1331 		}
   1332 	}
   1333 	if (sc->sc_firmware_type == WI_NOTYPE) {
   1334 		if (le16toh(ver[0]) & 0x8000) {
   1335 			printf("Unknown PRISM2 chip");
   1336 			sc->sc_firmware_type = WI_INTERSIL;
   1337 		} else {
   1338 			printf("Unknown Lucent chip");
   1339 			sc->sc_firmware_type = WI_LUCENT;
   1340 		}
   1341 	}
   1342 
   1343 	/* get primary firmware version (Only Prism chips) */
   1344 	if (sc->sc_firmware_type != WI_LUCENT) {
   1345 		memset(ver, 0, sizeof(ver));
   1346 		len = sizeof(ver);
   1347 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
   1348 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
   1349 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1350 DPRINTF2(("wi_read_nicid: PRI_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1351 	}
   1352 
   1353 	/* get station firmware version */
   1354 	memset(ver, 0, sizeof(ver));
   1355 	len = sizeof(ver);
   1356 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
   1357 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
   1358 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1359 DPRINTF2(("wi_read_nicid: STA_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1360 	if (sc->sc_firmware_type == WI_INTERSIL &&
   1361 	    (sc->sc_sta_firmware_ver == 10102 ||
   1362 	     sc->sc_sta_firmware_ver == 20102)) {
   1363 		char ident[12];
   1364 		memset(ident, 0, sizeof(ident));
   1365 		len = sizeof(ident);
   1366 		/* value should be the format like "V2.00-11" */
   1367 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
   1368 		    *(p = (char *)ident) >= 'A' &&
   1369 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
   1370 			sc->sc_firmware_type = WI_SYMBOL;
   1371 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
   1372 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
   1373 			    (p[6] - '0') * 10 + (p[7] - '0');
   1374 		}
   1375 DPRINTF2(("wi_read_nicid: SYMBOL_ID: %x %x %x %x\n", le16toh(ident[0]), le16toh(ident[1]), le16toh(ident[2]), le16toh(ident[3])));
   1376 	}
   1377 
   1378 	printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
   1379 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
   1380 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
   1381 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
   1382 		printf("Primary (%u.%u.%u), ",
   1383 		    sc->sc_pri_firmware_ver / 10000,
   1384 		    (sc->sc_pri_firmware_ver % 10000) / 100,
   1385 		    sc->sc_pri_firmware_ver % 100);
   1386 	printf("Station (%u.%u.%u)\n",
   1387 	    sc->sc_sta_firmware_ver / 10000,
   1388 	    (sc->sc_sta_firmware_ver % 10000) / 100,
   1389 	    sc->sc_sta_firmware_ver % 100);
   1390 }
   1391 
   1392 static int
   1393 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
   1394 {
   1395 	struct wi_ssid ssid;
   1396 
   1397 	if (buflen > IEEE80211_NWID_LEN)
   1398 		return ENOBUFS;
   1399 	memset(&ssid, 0, sizeof(ssid));
   1400 	ssid.wi_len = htole16(buflen);
   1401 	memcpy(ssid.wi_ssid, buf, buflen);
   1402 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
   1403 }
   1404 
   1405 static int
   1406 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1407 {
   1408 	struct wi_softc *sc = ifp->if_softc;
   1409 	struct ieee80211com *ic = &sc->sc_ic;
   1410 	struct ifreq *ifr = (struct ifreq *)data;
   1411 	struct wi_req wreq;
   1412 	int len, n, error;
   1413 
   1414 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1415 	if (error)
   1416 		return error;
   1417 	len = (wreq.wi_len - 1) * 2;
   1418 	if (len < sizeof(u_int16_t))
   1419 		return ENOSPC;
   1420 	if (len > sizeof(wreq.wi_val))
   1421 		len = sizeof(wreq.wi_val);
   1422 
   1423 	switch (wreq.wi_type) {
   1424 
   1425 	case WI_RID_IFACE_STATS:
   1426 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
   1427 		if (len < sizeof(sc->sc_stats))
   1428 			error = ENOSPC;
   1429 		else
   1430 			len = sizeof(sc->sc_stats);
   1431 		break;
   1432 
   1433 	case WI_RID_ENCRYPTION:
   1434 	case WI_RID_TX_CRYPT_KEY:
   1435 	case WI_RID_DEFLT_CRYPT_KEYS:
   1436 	case WI_RID_TX_RATE:
   1437 		return ieee80211_cfgget(ifp, cmd, data);
   1438 
   1439 	case WI_RID_MICROWAVE_OVEN:
   1440 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
   1441 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1442 			    &len);
   1443 			break;
   1444 		}
   1445 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
   1446 		len = sizeof(u_int16_t);
   1447 		break;
   1448 
   1449 	case WI_RID_DBM_ADJUST:
   1450 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
   1451 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1452 			    &len);
   1453 			break;
   1454 		}
   1455 		wreq.wi_val[0] = htole16(sc->sc_dbm_adjust);
   1456 		len = sizeof(u_int16_t);
   1457 		break;
   1458 
   1459 	case WI_RID_ROAMING_MODE:
   1460 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
   1461 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1462 			    &len);
   1463 			break;
   1464 		}
   1465 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
   1466 		len = sizeof(u_int16_t);
   1467 		break;
   1468 
   1469 	case WI_RID_SYSTEM_SCALE:
   1470 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
   1471 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1472 			    &len);
   1473 			break;
   1474 		}
   1475 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
   1476 		len = sizeof(u_int16_t);
   1477 		break;
   1478 
   1479 	case WI_RID_FRAG_THRESH:
   1480 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
   1481 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1482 			    &len);
   1483 			break;
   1484 		}
   1485 		wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1486 		len = sizeof(u_int16_t);
   1487 		break;
   1488 
   1489 	case WI_RID_READ_APS:
   1490 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1491 			return ieee80211_cfgget(ifp, cmd, data);
   1492 		if (sc->sc_scan_timer > 0) {
   1493 			error = EINPROGRESS;
   1494 			break;
   1495 		}
   1496 		n = sc->sc_naps;
   1497 		if (len < sizeof(n)) {
   1498 			error = ENOSPC;
   1499 			break;
   1500 		}
   1501 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
   1502 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
   1503 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
   1504 		memcpy(wreq.wi_val, &n, sizeof(n));
   1505 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
   1506 		    sizeof(struct wi_apinfo) * n);
   1507 		break;
   1508 
   1509 	default:
   1510 		if (sc->sc_enabled) {
   1511 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1512 			    &len);
   1513 			break;
   1514 		}
   1515 		switch (wreq.wi_type) {
   1516 		case WI_RID_MAX_DATALEN:
   1517 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
   1518 			len = sizeof(u_int16_t);
   1519 			break;
   1520 		case WI_RID_RTS_THRESH:
   1521 			wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
   1522 			len = sizeof(u_int16_t);
   1523 			break;
   1524 		case WI_RID_CNFAUTHMODE:
   1525 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
   1526 			len = sizeof(u_int16_t);
   1527 			break;
   1528 		case WI_RID_NODENAME:
   1529 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
   1530 				error = ENOSPC;
   1531 				break;
   1532 			}
   1533 			len = sc->sc_nodelen + sizeof(u_int16_t);
   1534 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
   1535 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
   1536 			    sc->sc_nodelen);
   1537 			break;
   1538 		default:
   1539 			return ieee80211_cfgget(ifp, cmd, data);
   1540 		}
   1541 		break;
   1542 	}
   1543 	if (error)
   1544 		return error;
   1545 	wreq.wi_len = (len + 1) / 2 + 1;
   1546 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
   1547 }
   1548 
   1549 static int
   1550 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1551 {
   1552 	struct wi_softc *sc = ifp->if_softc;
   1553 	struct ieee80211com *ic = &sc->sc_ic;
   1554 	struct ifreq *ifr = (struct ifreq *)data;
   1555 	struct wi_req wreq;
   1556 	struct mbuf *m;
   1557 	int i, len, error;
   1558 
   1559 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1560 	if (error)
   1561 		return error;
   1562 	len = (wreq.wi_len - 1) * 2;
   1563 	switch (wreq.wi_type) {
   1564 	case WI_RID_DBM_ADJUST:
   1565 		return ENODEV;
   1566 
   1567 	case WI_RID_NODENAME:
   1568 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
   1569 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
   1570 			error = ENOSPC;
   1571 			break;
   1572 		}
   1573 		if (sc->sc_enabled) {
   1574 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1575 			    len);
   1576 			if (error)
   1577 				break;
   1578 		}
   1579 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
   1580 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
   1581 		break;
   1582 
   1583 	case WI_RID_MICROWAVE_OVEN:
   1584 	case WI_RID_ROAMING_MODE:
   1585 	case WI_RID_SYSTEM_SCALE:
   1586 	case WI_RID_FRAG_THRESH:
   1587 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
   1588 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
   1589 			break;
   1590 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
   1591 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
   1592 			break;
   1593 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
   1594 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
   1595 			break;
   1596 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
   1597 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
   1598 			break;
   1599 		/* FALLTHROUGH */
   1600 	case WI_RID_RTS_THRESH:
   1601 	case WI_RID_CNFAUTHMODE:
   1602 	case WI_RID_MAX_DATALEN:
   1603 		if (sc->sc_enabled) {
   1604 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1605 			    sizeof(u_int16_t));
   1606 			if (error)
   1607 				break;
   1608 		}
   1609 		switch (wreq.wi_type) {
   1610 		case WI_RID_FRAG_THRESH:
   1611 			sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
   1612 			break;
   1613 		case WI_RID_RTS_THRESH:
   1614 			sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
   1615 			break;
   1616 		case WI_RID_MICROWAVE_OVEN:
   1617 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
   1618 			break;
   1619 		case WI_RID_ROAMING_MODE:
   1620 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
   1621 			break;
   1622 		case WI_RID_SYSTEM_SCALE:
   1623 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
   1624 			break;
   1625 		case WI_RID_CNFAUTHMODE:
   1626 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
   1627 			break;
   1628 		case WI_RID_MAX_DATALEN:
   1629 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
   1630 			break;
   1631 		}
   1632 		break;
   1633 
   1634 	case WI_RID_TX_RATE:
   1635 		switch (le16toh(wreq.wi_val[0])) {
   1636 		case 3:
   1637 			ic->ic_fixed_rate = -1;
   1638 			break;
   1639 		default:
   1640 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   1641 				if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
   1642 				    / 2 == le16toh(wreq.wi_val[0]))
   1643 					break;
   1644 			}
   1645 			if (i == IEEE80211_RATE_SIZE)
   1646 				return EINVAL;
   1647 			ic->ic_fixed_rate = i;
   1648 		}
   1649 		if (sc->sc_enabled)
   1650 			error = wi_write_txrate(sc);
   1651 		break;
   1652 
   1653 	case WI_RID_SCAN_APS:
   1654 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
   1655 			error = wi_scan_ap(sc);
   1656 		break;
   1657 
   1658 	case WI_RID_MGMT_XMIT:
   1659 		if (!sc->sc_enabled) {
   1660 			error = ENETDOWN;
   1661 			break;
   1662 		}
   1663 		if (ic->ic_mgtq.ifq_len > 5) {
   1664 			error = EAGAIN;
   1665 			break;
   1666 		}
   1667 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
   1668 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
   1669 		if (m == NULL) {
   1670 			error = ENOMEM;
   1671 			break;
   1672 		}
   1673 		IF_ENQUEUE(&ic->ic_mgtq, m);
   1674 		break;
   1675 
   1676 	default:
   1677 		if (sc->sc_enabled) {
   1678 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1679 			    len);
   1680 			if (error)
   1681 				break;
   1682 		}
   1683 		error = ieee80211_cfgset(ifp, cmd, data);
   1684 		break;
   1685 	}
   1686 	return error;
   1687 }
   1688 
   1689 static int
   1690 wi_write_txrate(struct wi_softc *sc)
   1691 {
   1692 	struct ieee80211com *ic = &sc->sc_ic;
   1693 	int i;
   1694 	u_int16_t rate;
   1695 
   1696 	if (ic->ic_fixed_rate < 0)
   1697 		rate = 0;	/* auto */
   1698 	else
   1699 		rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
   1700 		    IEEE80211_RATE_VAL) / 2;
   1701 
   1702 	/* rate: 0, 1, 2, 5, 11 */
   1703 
   1704 	switch (sc->sc_firmware_type) {
   1705 	case WI_LUCENT:
   1706 		if (rate == 0)
   1707 			rate = 3;	/* auto */
   1708 		break;
   1709 	default:
   1710 		/* Choose a bit according to this table.
   1711 		 *
   1712 		 * bit | data rate
   1713 		 * ----+-------------------
   1714 		 * 0   | 1Mbps
   1715 		 * 1   | 2Mbps
   1716 		 * 2   | 5.5Mbps
   1717 		 * 3   | 11Mbps
   1718 		 */
   1719 		for (i = 8; i > 0; i >>= 1) {
   1720 			if (rate >= i)
   1721 				break;
   1722 		}
   1723 		if (i == 0)
   1724 			rate = 0xf;	/* auto */
   1725 		else
   1726 			rate = i;
   1727 		break;
   1728 	}
   1729 	return wi_write_val(sc, WI_RID_TX_RATE, rate);
   1730 }
   1731 
   1732 static int
   1733 wi_write_wep(struct wi_softc *sc)
   1734 {
   1735 	struct ieee80211com *ic = &sc->sc_ic;
   1736 	int error = 0;
   1737 	int i, keylen;
   1738 	u_int16_t val;
   1739 	struct wi_key wkey[IEEE80211_WEP_NKID];
   1740 
   1741 	switch (sc->sc_firmware_type) {
   1742 	case WI_LUCENT:
   1743 		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
   1744 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
   1745 		if (error)
   1746 			break;
   1747 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
   1748 		if (error)
   1749 			break;
   1750 		memset(wkey, 0, sizeof(wkey));
   1751 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1752 			keylen = ic->ic_nw_keys[i].wk_len;
   1753 			wkey[i].wi_keylen = htole16(keylen);
   1754 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
   1755 			    keylen);
   1756 		}
   1757 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
   1758 		    wkey, sizeof(wkey));
   1759 		break;
   1760 
   1761 	case WI_INTERSIL:
   1762 	case WI_SYMBOL:
   1763 		if (ic->ic_flags & IEEE80211_F_WEPON) {
   1764 			/*
   1765 			 * ONLY HWB3163 EVAL-CARD Firmware version
   1766 			 * less than 0.8 variant2
   1767 			 *
   1768 			 *   If promiscuous mode disable, Prism2 chip
   1769 			 *  does not work with WEP .
   1770 			 * It is under investigation for details.
   1771 			 * (ichiro (at) netbsd.org)
   1772 			 */
   1773 			if (sc->sc_firmware_type == WI_INTERSIL &&
   1774 			    sc->sc_sta_firmware_ver < 802 ) {
   1775 				/* firm ver < 0.8 variant 2 */
   1776 				wi_write_val(sc, WI_RID_PROMISC, 1);
   1777 			}
   1778 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   1779 			    sc->sc_cnfauthmode);
   1780 			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
   1781 			/*
   1782 			 * Encryption firmware has a bug for HostAP mode.
   1783 			 */
   1784 			if (sc->sc_firmware_type == WI_INTERSIL &&
   1785 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
   1786 				val |= HOST_ENCRYPT;
   1787 		} else {
   1788 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   1789 			    IEEE80211_AUTH_OPEN);
   1790 			val = HOST_ENCRYPT | HOST_DECRYPT;
   1791 		}
   1792 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
   1793 		if (error)
   1794 			break;
   1795 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
   1796 		    ic->ic_wep_txkey);
   1797 		if (error)
   1798 			break;
   1799 		/*
   1800 		 * It seems that the firmware accept 104bit key only if
   1801 		 * all the keys have 104bit length.  We get the length of
   1802 		 * the transmit key and use it for all other keys.
   1803 		 * Perhaps we should use software WEP for such situation.
   1804 		 */
   1805 		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
   1806 		if (keylen > IEEE80211_WEP_KEYLEN)
   1807 			keylen = 13;	/* 104bit keys */
   1808 		else
   1809 			keylen = IEEE80211_WEP_KEYLEN;
   1810 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1811 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
   1812 			    ic->ic_nw_keys[i].wk_key, keylen);
   1813 			if (error)
   1814 				break;
   1815 		}
   1816 		break;
   1817 	}
   1818 	return error;
   1819 }
   1820 
   1821 /* Must be called at proper protection level! */
   1822 static int
   1823 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   1824 {
   1825 	int i, status;
   1826 
   1827 	/* wait for the busy bit to clear */
   1828 	for (i = 0; ; i++) {
   1829 		if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
   1830 			break;
   1831 		if (i == WI_TIMEOUT) {
   1832 			printf("%s: wi_cmd: BUSY did not clear, "
   1833 			    "cmd=0x%x, prev=0x%x\n", sc->sc_dev.dv_xname,
   1834 			    cmd, CSR_READ_2(sc, WI_COMMAND));
   1835 			return EIO;
   1836 		}
   1837 		DELAY(1);
   1838 	}
   1839 
   1840 	CSR_WRITE_2(sc, WI_PARAM0, val0);
   1841 	CSR_WRITE_2(sc, WI_PARAM1, val1);
   1842 	CSR_WRITE_2(sc, WI_PARAM2, val2);
   1843 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
   1844 
   1845 	if (cmd == WI_CMD_INI) {
   1846 		/* XXX: should sleep here. */
   1847 		DELAY(100*1000);
   1848 	}
   1849 	/* wait for the cmd completed bit */
   1850 	for (i = 0; i < WI_TIMEOUT; i++) {
   1851 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
   1852 			break;
   1853 		DELAY(1);
   1854 	}
   1855 
   1856 	status = CSR_READ_2(sc, WI_STATUS);
   1857 
   1858 	/* Ack the command */
   1859 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
   1860 
   1861 	if (i == WI_TIMEOUT) {
   1862 		printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
   1863 		    sc->sc_dev.dv_xname, cmd, val0);
   1864 		return ETIMEDOUT;
   1865 	}
   1866 
   1867 	if (status & WI_STAT_CMD_RESULT) {
   1868 		printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
   1869 		    sc->sc_dev.dv_xname, cmd, val0);
   1870 		return EIO;
   1871 	}
   1872 	return 0;
   1873 }
   1874 
   1875 static int
   1876 wi_seek_bap(struct wi_softc *sc, int id, int off)
   1877 {
   1878 	int i, status;
   1879 
   1880 	CSR_WRITE_2(sc, WI_SEL0, id);
   1881 	CSR_WRITE_2(sc, WI_OFF0, off);
   1882 
   1883 	for (i = 0; ; i++) {
   1884 		status = CSR_READ_2(sc, WI_OFF0);
   1885 		if ((status & WI_OFF_BUSY) == 0)
   1886 			break;
   1887 		if (i == WI_TIMEOUT) {
   1888 			printf("%s: timeout in wi_seek to %x/%x\n",
   1889 			    sc->sc_dev.dv_xname, id, off);
   1890 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   1891 			return ETIMEDOUT;
   1892 		}
   1893 		DELAY(1);
   1894 	}
   1895 	if (status & WI_OFF_ERR) {
   1896 		printf("%s: failed in wi_seek to %x/%x\n",
   1897 		    sc->sc_dev.dv_xname, id, off);
   1898 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   1899 		return EIO;
   1900 	}
   1901 	sc->sc_bap_id = id;
   1902 	sc->sc_bap_off = off;
   1903 	return 0;
   1904 }
   1905 
   1906 static int
   1907 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   1908 {
   1909 	int error, cnt;
   1910 
   1911 	if (buflen == 0)
   1912 		return 0;
   1913 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   1914 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   1915 			return error;
   1916 	}
   1917 	cnt = (buflen + 1) / 2;
   1918 	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   1919 	sc->sc_bap_off += cnt * 2;
   1920 	return 0;
   1921 }
   1922 
   1923 static int
   1924 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   1925 {
   1926 	int error, cnt;
   1927 
   1928 	if (buflen == 0)
   1929 		return 0;
   1930 
   1931 #ifdef WI_HERMES_AUTOINC_WAR
   1932   again:
   1933 #endif
   1934 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   1935 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   1936 			return error;
   1937 	}
   1938 	cnt = (buflen + 1) / 2;
   1939 	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   1940 	sc->sc_bap_off += cnt * 2;
   1941 
   1942 #ifdef WI_HERMES_AUTOINC_WAR
   1943 	/*
   1944 	 * According to the comments in the HCF Light code, there is a bug
   1945 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
   1946 	 * where the chip's internal autoincrement counter gets thrown off
   1947 	 * during data writes:  the autoincrement is missed, causing one
   1948 	 * data word to be overwritten and subsequent words to be written to
   1949 	 * the wrong memory locations. The end result is that we could end
   1950 	 * up transmitting bogus frames without realizing it. The workaround
   1951 	 * for this is to write a couple of extra guard words after the end
   1952 	 * of the transfer, then attempt to read then back. If we fail to
   1953 	 * locate the guard words where we expect them, we preform the
   1954 	 * transfer over again.
   1955 	 */
   1956 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
   1957 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
   1958 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
   1959 		wi_seek_bap(sc, id, sc->sc_bap_off);
   1960 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   1961 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
   1962 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
   1963 			printf("%s: detect auto increment bug, try again\n",
   1964 			    sc->sc_dev.dv_xname);
   1965 			goto again;
   1966 		}
   1967 	}
   1968 #endif
   1969 	return 0;
   1970 }
   1971 
   1972 static int
   1973 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
   1974 {
   1975 	int i;
   1976 
   1977 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
   1978 		printf("%s: failed to allocate %d bytes on NIC\n",
   1979 		    sc->sc_dev.dv_xname, len);
   1980 		return ENOMEM;
   1981 	}
   1982 
   1983 	for (i = 0; i < WI_TIMEOUT; i++) {
   1984 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
   1985 			break;
   1986 		if (i == WI_TIMEOUT) {
   1987 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
   1988 			return ETIMEDOUT;
   1989 		}
   1990 		DELAY(1);
   1991 	}
   1992 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
   1993 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   1994 	return 0;
   1995 }
   1996 
   1997 static int
   1998 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
   1999 {
   2000 	int error, len;
   2001 	u_int16_t ltbuf[2];
   2002 
   2003 	/* Tell the NIC to enter record read mode. */
   2004 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
   2005 	if (error)
   2006 		return error;
   2007 
   2008 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2009 	if (error)
   2010 		return error;
   2011 
   2012 	if (le16toh(ltbuf[1]) != rid) {
   2013 		printf("%s: record read mismatch, rid=%x, got=%x\n",
   2014 		    sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
   2015 		return EIO;
   2016 	}
   2017 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
   2018 	if (*buflenp < len) {
   2019 		printf("%s: record buffer is too small, "
   2020 		    "rid=%x, size=%d, len=%d\n",
   2021 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
   2022 		return ENOSPC;
   2023 	}
   2024 	*buflenp = len;
   2025 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
   2026 }
   2027 
   2028 static int
   2029 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
   2030 {
   2031 	int error;
   2032 	u_int16_t ltbuf[2];
   2033 
   2034 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
   2035 	ltbuf[1] = htole16(rid);
   2036 
   2037 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2038 	if (error)
   2039 		return error;
   2040 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
   2041 	if (error)
   2042 		return error;
   2043 
   2044 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
   2045 }
   2046 
   2047 static int
   2048 wi_newstate(void *arg, enum ieee80211_state nstate)
   2049 {
   2050 	struct wi_softc *sc = arg;
   2051 	struct ieee80211com *ic = &sc->sc_ic;
   2052 	struct ieee80211_node *ni = &ic->ic_bss;
   2053 	int i, buflen;
   2054 	u_int16_t val;
   2055 	struct wi_ssid ssid;
   2056 	enum ieee80211_state ostate;
   2057 #ifdef WI_DEBUG
   2058 	static const char *stname[] =
   2059 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   2060 #endif /* WI_DEBUG */
   2061 
   2062 	ostate = ic->ic_state;
   2063 	DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
   2064 
   2065 	ic->ic_state = nstate;
   2066 	switch (nstate) {
   2067 	case IEEE80211_S_INIT:
   2068 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   2069 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2070 		return 0;
   2071 
   2072 	case IEEE80211_S_RUN:
   2073 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2074 		buflen = IEEE80211_ADDR_LEN;
   2075 		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
   2076 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
   2077 		buflen = sizeof(val);
   2078 		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
   2079 		ni->ni_chan = le16toh(val);
   2080 
   2081 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   2082 			ni->ni_esslen = ic->ic_des_esslen;
   2083 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
   2084 			ni->ni_nrate = 0;
   2085 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   2086 				if (ic->ic_sup_rates[i])
   2087 					ni->ni_rates[ni->ni_nrate++] =
   2088 					    ic->ic_sup_rates[i];
   2089 			}
   2090 			ni->ni_intval = ic->ic_lintval;
   2091 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
   2092 			if (ic->ic_flags & IEEE80211_F_WEPON)
   2093 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2094 		} else {
   2095 			buflen = sizeof(ssid);
   2096 			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
   2097 			ni->ni_esslen = le16toh(ssid.wi_len);
   2098 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
   2099 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
   2100 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
   2101 		}
   2102 		break;
   2103 
   2104 	case IEEE80211_S_SCAN:
   2105 	case IEEE80211_S_AUTH:
   2106 	case IEEE80211_S_ASSOC:
   2107 		break;
   2108 	}
   2109 
   2110 	/* skip standard ieee80211 handling */
   2111 	return EINPROGRESS;
   2112 }
   2113 
   2114 static int
   2115 wi_scan_ap(struct wi_softc *sc)
   2116 {
   2117 	int error = 0;
   2118 	u_int16_t val[2];
   2119 
   2120 	if (!sc->sc_enabled)
   2121 		return ENXIO;
   2122 	switch (sc->sc_firmware_type) {
   2123 	case WI_LUCENT:
   2124 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   2125 		break;
   2126 	case WI_INTERSIL:
   2127 		val[0] = 0x3fff;	/* channel */
   2128 		val[1] = 0x000f;	/* tx rate */
   2129 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
   2130 		break;
   2131 	case WI_SYMBOL:
   2132 		/*
   2133 		 * XXX only supported on 3.x ?
   2134 		 */
   2135 		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
   2136 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
   2137 		    val, sizeof(val[0]));
   2138 		break;
   2139 	}
   2140 	if (error == 0) {
   2141 		sc->sc_scan_timer = WI_SCAN_WAIT;
   2142 		sc->sc_ic.ic_if.if_timer = 1;
   2143 		DPRINTF(("wi_scan_ap: start scanning\n"));
   2144 	}
   2145 	return error;
   2146 }
   2147 
   2148 static void
   2149 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
   2150 {
   2151 	int i, naps, off, szbuf;
   2152 	struct wi_scan_header ws_hdr;	/* Prism2 header */
   2153 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
   2154 	struct wi_apinfo *ap;
   2155 
   2156 	off = sizeof(u_int16_t) * 2;
   2157 	memset(&ws_hdr, 0, sizeof(ws_hdr));
   2158 	switch (sc->sc_firmware_type) {
   2159 	case WI_INTERSIL:
   2160 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
   2161 		off += sizeof(ws_hdr);
   2162 		szbuf = sizeof(struct wi_scan_data_p2);
   2163 		break;
   2164 	case WI_SYMBOL:
   2165 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
   2166 		break;
   2167 	case WI_LUCENT:
   2168 		szbuf = sizeof(struct wi_scan_data);
   2169 		break;
   2170 	}
   2171 	naps = (cnt * 2 + 2 - off) / szbuf;
   2172 	if (naps > MAXAPINFO)
   2173 		naps = MAXAPINFO;
   2174 	sc->sc_naps = naps;
   2175 	/* Read Data */
   2176 	ap = sc->sc_aps;
   2177 	memset(&ws_dat, 0, sizeof(ws_dat));
   2178 	for (i = 0; i < naps; i++, ap++) {
   2179 		wi_read_bap(sc, fid, off, &ws_dat,
   2180 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
   2181 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
   2182 		    ether_sprintf(ws_dat.wi_bssid)));
   2183 		off += szbuf;
   2184 		ap->scanreason = le16toh(ws_hdr.wi_reason);
   2185 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
   2186 		ap->channel = le16toh(ws_dat.wi_chid);
   2187 		ap->signal  = le16toh(ws_dat.wi_signal);
   2188 		ap->noise   = le16toh(ws_dat.wi_noise);
   2189 		ap->quality = ap->signal - ap->noise;
   2190 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
   2191 		ap->interval = le16toh(ws_dat.wi_interval);
   2192 		ap->rate    = le16toh(ws_dat.wi_rate);
   2193 		ap->namelen = le16toh(ws_dat.wi_namelen);
   2194 		if (ap->namelen > sizeof(ap->name))
   2195 			ap->namelen = sizeof(ap->name);
   2196 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
   2197 	}
   2198 	/* Done scanning */
   2199 	sc->sc_scan_timer = 0;
   2200 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
   2201 }
   2202