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