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