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