Home | History | Annotate | Line # | Download | only in ic
wi.c revision 1.112
      1 /*	$NetBSD: wi.c,v 1.112 2003/02/25 01:57:35 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
     37  *
     38  * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
     39  * Electrical Engineering Department
     40  * Columbia University, New York City
     41  */
     42 
     43 /*
     44  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
     45  * from Lucent. Unlike the older cards, the new ones are programmed
     46  * entirely via a firmware-driven controller called the Hermes.
     47  * Unfortunately, Lucent will not release the Hermes programming manual
     48  * without an NDA (if at all). What they do release is an API library
     49  * called the HCF (Hardware Control Functions) which is supposed to
     50  * do the device-specific operations of a device driver for you. The
     51  * publically available version of the HCF library (the 'HCF Light') is
     52  * a) extremely gross, b) lacks certain features, particularly support
     53  * for 802.11 frames, and c) is contaminated by the GNU Public License.
     54  *
     55  * This driver does not use the HCF or HCF Light at all. Instead, it
     56  * programs the Hermes controller directly, using information gleaned
     57  * from the HCF Light code and corresponding documentation.
     58  *
     59  * This driver supports both the PCMCIA and ISA versions of the
     60  * WaveLAN/IEEE cards. Note however that the ISA card isn't really
     61  * anything of the sort: it's actually a PCMCIA bridge adapter
     62  * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
     63  * inserted. Consequently, you need to use the pccard support for
     64  * both the ISA and PCMCIA adapters.
     65  */
     66 
     67 /*
     68  * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
     69  * Oslo IETF plenary meeting.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.112 2003/02/25 01:57:35 dyoung Exp $");
     74 
     75 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
     76 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
     77 
     78 #include "bpfilter.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/callout.h>
     83 #include <sys/device.h>
     84 #include <sys/socket.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/kernel.h>		/* for hz */
     88 #include <sys/proc.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_dl.h>
     92 #include <net/if_media.h>
     93 #include <net/if_ether.h>
     94 #include <net/if_ieee80211.h>
     95 
     96 #if NBPFILTER > 0
     97 #include <net/bpf.h>
     98 #include <net/bpfdesc.h>
     99 #endif
    100 
    101 #include <machine/bus.h>
    102 
    103 #include <dev/ic/wi_ieee.h>
    104 #include <dev/ic/wireg.h>
    105 #include <dev/ic/wivar.h>
    106 
    107 static int  wi_init(struct ifnet *);
    108 static void wi_stop(struct ifnet *, int);
    109 static void wi_start(struct ifnet *);
    110 static int  wi_reset(struct wi_softc *);
    111 static void wi_watchdog(struct ifnet *);
    112 static int  wi_ioctl(struct ifnet *, u_long, caddr_t);
    113 static int  wi_media_change(struct ifnet *);
    114 static void wi_media_status(struct ifnet *, struct ifmediareq *);
    115 
    116 static void wi_rx_intr(struct wi_softc *);
    117 static void wi_tx_intr(struct wi_softc *);
    118 static void wi_info_intr(struct wi_softc *);
    119 
    120 static int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
    121 static int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
    122 static int  wi_write_txrate(struct wi_softc *);
    123 static int  wi_write_wep(struct wi_softc *);
    124 static int  wi_write_multi(struct wi_softc *);
    125 static int  wi_alloc_fid(struct wi_softc *, int, int *);
    126 static void wi_read_nicid(struct wi_softc *);
    127 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
    128 
    129 static int  wi_cmd(struct wi_softc *, int, int, int, int);
    130 static int  wi_seek_bap(struct wi_softc *, int, int);
    131 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
    132 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
    133 static int  wi_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 	error = 0;
    850 	for (i = 0; i < 5; i++) {
    851 		DELAY(20*1000);	/* XXX: way too long! */
    852 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
    853 			break;
    854 	}
    855 	if (error) {
    856 		printf("%s: init failed\n", sc->sc_dev.dv_xname);
    857 		return error;
    858 	}
    859 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    860 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    861 
    862 	/* Calibrate timer. */
    863 	wi_write_val(sc, WI_RID_TICK_TIME, 0);
    864 	return 0;
    865 }
    866 
    867 static void
    868 wi_watchdog(struct ifnet *ifp)
    869 {
    870 	struct wi_softc *sc = ifp->if_softc;
    871 
    872 	ifp->if_timer = 0;
    873 	if (!sc->sc_enabled)
    874 		return;
    875 
    876 	if (sc->sc_tx_timer) {
    877 		if (--sc->sc_tx_timer == 0) {
    878 			printf("%s: device timeout\n", ifp->if_xname);
    879 			ifp->if_oerrors++;
    880 			wi_init(ifp);
    881 			return;
    882 		}
    883 		ifp->if_timer = 1;
    884 	}
    885 
    886 	if (sc->sc_scan_timer) {
    887 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
    888 		    sc->sc_firmware_type == WI_INTERSIL) {
    889 			DPRINTF(("wi_watchdog: inquire scan\n"));
    890 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
    891 		}
    892 		if (sc->sc_scan_timer)
    893 			ifp->if_timer = 1;
    894 	}
    895 
    896 	if (sc->sc_syn_timer) {
    897 		if (--sc->sc_syn_timer == 0) {
    898 			DPRINTF2(("%s: %d false syns\n",
    899 			    sc->sc_dev.dv_xname, sc->sc_false_syns));
    900 			sc->sc_false_syns = 0;
    901 			ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
    902 			sc->sc_syn_timer = 5;
    903 		}
    904 		ifp->if_timer = 1;
    905 	}
    906 
    907 	/* TODO: rate control */
    908 	ieee80211_watchdog(ifp);
    909 }
    910 
    911 static int
    912 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    913 {
    914 	struct wi_softc *sc = ifp->if_softc;
    915 	struct ieee80211com *ic = &sc->sc_ic;
    916 	struct ifreq *ifr = (struct ifreq *)data;
    917 	int s, error = 0;
    918 
    919 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    920 		return ENXIO;
    921 
    922 	s = splnet();
    923 
    924 	switch (cmd) {
    925 	case SIOCSIFFLAGS:
    926 		if (ifp->if_flags & IFF_UP) {
    927 			if (sc->sc_enabled) {
    928 				/*
    929 				 * To avoid rescanning another access point,
    930 				 * do not call wi_init() here.  Instead,
    931 				 * only reflect promisc mode settings.
    932 				 */
    933 				if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    934 				    (ifp->if_flags & IFF_PROMISC) != 0)
    935 					wi_write_val(sc, WI_RID_PROMISC, 1);
    936 				else
    937 					wi_write_val(sc, WI_RID_PROMISC, 0);
    938 			} else
    939 				error = wi_init(ifp);
    940 		} else if (sc->sc_enabled)
    941 			wi_stop(ifp, 1);
    942 		break;
    943 	case SIOCSIFMEDIA:
    944 	case SIOCGIFMEDIA:
    945 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    946 		break;
    947 	case SIOCADDMULTI:
    948 	case SIOCDELMULTI:
    949 		error = (cmd == SIOCADDMULTI) ?
    950 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
    951 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
    952 		if (error == ENETRESET) {
    953 			if (sc->sc_enabled) {
    954 				/* do not rescan */
    955 				error = wi_write_multi(sc);
    956 			} else
    957 				error = 0;
    958 		}
    959 		break;
    960 	case SIOCGIFGENERIC:
    961 		error = wi_get_cfg(ifp, cmd, data);
    962 		break;
    963 	case SIOCSIFGENERIC:
    964 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    965 		if (error)
    966 			break;
    967 		error = wi_set_cfg(ifp, cmd, data);
    968 		if (error == ENETRESET) {
    969 			if (sc->sc_enabled)
    970 				error = wi_init(ifp);
    971 			else
    972 				error = 0;
    973 		}
    974 		break;
    975 	case SIOCS80211BSSID:
    976 		/* No use pretending that Lucent firmware supports
    977 		 * 802.11 MLME-JOIN.request.
    978 		 */
    979 		if (sc->sc_firmware_type == WI_LUCENT) {
    980 			error = ENODEV;
    981 			break;
    982 		}
    983 		/* fall through */
    984 	default:
    985 		error = ieee80211_ioctl(ifp, cmd, data);
    986 		if (error == ENETRESET) {
    987 			if (sc->sc_enabled)
    988 				error = wi_init(ifp);
    989 			else
    990 				error = 0;
    991 		}
    992 		break;
    993 	}
    994 	splx(s);
    995 	return error;
    996 }
    997 
    998 static int
    999 wi_media_change(struct ifnet *ifp)
   1000 {
   1001 	struct wi_softc *sc = ifp->if_softc;
   1002 	struct ieee80211com *ic = &sc->sc_ic;
   1003 	struct ifmedia_entry *ime;
   1004 	enum ieee80211_opmode newmode;
   1005 	int i, rate, error = 0;
   1006 
   1007 	ime = sc->sc_media.ifm_cur;
   1008 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
   1009 		i = -1;
   1010 	} else {
   1011 		rate = ieee80211_media2rate(ime->ifm_media, IEEE80211_T_DS);
   1012 		if (rate == 0)
   1013 			return EINVAL;
   1014 		for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   1015 			if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
   1016 				break;
   1017 		}
   1018 		if (i == IEEE80211_RATE_SIZE)
   1019 			return EINVAL;
   1020 	}
   1021 	if (ic->ic_fixed_rate != i) {
   1022 		ic->ic_fixed_rate = i;
   1023 		error = ENETRESET;
   1024 	}
   1025 
   1026 	if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
   1027 	    (ime->ifm_media & IFM_FLAG0))
   1028 		newmode = IEEE80211_M_AHDEMO;
   1029 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
   1030 		newmode = IEEE80211_M_IBSS;
   1031 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
   1032 		newmode = IEEE80211_M_HOSTAP;
   1033 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
   1034 		newmode = IEEE80211_M_MONITOR;
   1035 	else
   1036 		newmode = IEEE80211_M_STA;
   1037 	if (ic->ic_opmode != newmode) {
   1038 		ic->ic_opmode = newmode;
   1039 		error = ENETRESET;
   1040 	}
   1041 	if (error == ENETRESET) {
   1042 		if (sc->sc_enabled)
   1043 			error = wi_init(ifp);
   1044 		else
   1045 			error = 0;
   1046 	}
   1047 	ifp->if_baudrate = ifmedia_baudrate(sc->sc_media.ifm_cur->ifm_media);
   1048 
   1049 	return error;
   1050 }
   1051 
   1052 static void
   1053 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1054 {
   1055 	struct wi_softc *sc = ifp->if_softc;
   1056 	struct ieee80211com *ic = &sc->sc_ic;
   1057 	u_int16_t val;
   1058 	int rate, len;
   1059 
   1060 	if (sc->sc_enabled == 0) {
   1061 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   1062 		imr->ifm_status = 0;
   1063 		return;
   1064 	}
   1065 
   1066 	imr->ifm_status = IFM_AVALID;
   1067 	imr->ifm_active = IFM_IEEE80211;
   1068 	if (ic->ic_state == IEEE80211_S_RUN &&
   1069 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
   1070 		imr->ifm_status |= IFM_ACTIVE;
   1071 	len = sizeof(val);
   1072 	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
   1073 		rate = 0;
   1074 	else {
   1075 		/* convert to 802.11 rate */
   1076 		rate = val * 2;
   1077 		if (sc->sc_firmware_type == WI_LUCENT) {
   1078 			if (rate == 10)
   1079 				rate = 11;	/* 5.5Mbps */
   1080 		} else {
   1081 			if (rate == 4*2)
   1082 				rate = 11;	/* 5.5Mbps */
   1083 			else if (rate == 8*2)
   1084 				rate = 22;	/* 11Mbps */
   1085 		}
   1086 	}
   1087 	imr->ifm_active |= ieee80211_rate2media(rate, IEEE80211_T_DS);
   1088 	switch (ic->ic_opmode) {
   1089 	case IEEE80211_M_STA:
   1090 		break;
   1091 	case IEEE80211_M_IBSS:
   1092 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
   1093 		break;
   1094 	case IEEE80211_M_AHDEMO:
   1095 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   1096 		break;
   1097 	case IEEE80211_M_HOSTAP:
   1098 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
   1099 		break;
   1100 	case IEEE80211_M_MONITOR:
   1101 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
   1102 		break;
   1103 	}
   1104 }
   1105 
   1106 static void
   1107 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
   1108 {
   1109 	struct ieee80211com *ic = &sc->sc_ic;
   1110 	struct ieee80211_node *ni = &ic->ic_bss;
   1111 	struct ifnet *ifp = &ic->ic_if;
   1112 
   1113 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
   1114 		return;
   1115 
   1116 	DPRINTF(("%s: bssid %s -> ", sc->sc_dev.dv_xname,
   1117 	    ether_sprintf(ni->ni_bssid)));
   1118 	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
   1119 
   1120 	/* In promiscuous mode, the BSSID field is not a reliable
   1121 	 * indicator of the firmware's BSSID. Damp spurious
   1122 	 * change-of-BSSID indications.
   1123 	 */
   1124 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   1125 	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
   1126 		return;
   1127 
   1128 	ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
   1129 }
   1130 
   1131 static void
   1132 wi_rx_intr(struct wi_softc *sc)
   1133 {
   1134 	struct ieee80211com *ic = &sc->sc_ic;
   1135 	struct ifnet *ifp = &ic->ic_if;
   1136 	struct wi_frame frmhdr;
   1137 	struct mbuf *m;
   1138 	struct ieee80211_frame *wh;
   1139 	int fid, len, off, rssi;
   1140 	u_int8_t dir;
   1141 	u_int16_t status;
   1142 	u_int32_t rstamp;
   1143 
   1144 	fid = CSR_READ_2(sc, WI_RX_FID);
   1145 
   1146 	/* First read in the frame header */
   1147 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
   1148 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1149 		ifp->if_ierrors++;
   1150 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
   1151 		return;
   1152 	}
   1153 
   1154 	/*
   1155 	 * Drop undecryptable or packets with receive errors here
   1156 	 */
   1157 	status = le16toh(frmhdr.wi_status);
   1158 	if (status & WI_STAT_ERRSTAT) {
   1159 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1160 		ifp->if_ierrors++;
   1161 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
   1162 		return;
   1163 	}
   1164 	rssi = frmhdr.wi_rx_signal;
   1165 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
   1166 	    le16toh(frmhdr.wi_rx_tstamp1);
   1167 
   1168 	len = le16toh(frmhdr.wi_dat_len);
   1169 	off = ALIGN(sizeof(struct ieee80211_frame));
   1170 
   1171 	/* Sometimes the PRISM2.x returns bogusly large frames. Except
   1172 	 * in monitor mode, just throw them away.
   1173 	 */
   1174 	if (off + len > MCLBYTES) {
   1175 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1176 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1177 			ifp->if_ierrors++;
   1178 			DPRINTF(("wi_rx_intr: oversized packet\n"));
   1179 			return;
   1180 		} else
   1181 			len = 0;
   1182 	}
   1183 
   1184 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1185 	if (m == NULL) {
   1186 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1187 		ifp->if_ierrors++;
   1188 		DPRINTF(("wi_rx_intr: MGET failed\n"));
   1189 		return;
   1190 	}
   1191 	if (off + len > MHLEN) {
   1192 		MCLGET(m, M_DONTWAIT);
   1193 		if ((m->m_flags & M_EXT) == 0) {
   1194 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1195 			m_freem(m);
   1196 			ifp->if_ierrors++;
   1197 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
   1198 			return;
   1199 		}
   1200 	}
   1201 
   1202 	m->m_data += off - sizeof(struct ieee80211_frame);
   1203 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
   1204 	wi_read_bap(sc, fid, sizeof(frmhdr),
   1205 	    m->m_data + sizeof(struct ieee80211_frame), len);
   1206 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
   1207 	m->m_pkthdr.rcvif = ifp;
   1208 
   1209 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1210 
   1211 #if NBPFILTER > 0
   1212 	if (sc->sc_drvbpf) {
   1213 		struct mbuf mb;
   1214 
   1215 		M_COPY_PKTHDR(&mb, m);
   1216 		mb.m_data = (caddr_t)&frmhdr;
   1217 		mb.m_len = sizeof(frmhdr);
   1218 		mb.m_next = m;
   1219 		mb.m_pkthdr.len += mb.m_len;
   1220 		bpf_mtap(sc->sc_drvbpf, &mb);
   1221 	}
   1222 #endif
   1223 	wh = mtod(m, struct ieee80211_frame *);
   1224 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1225 		/*
   1226 		 * WEP is decrypted by hardware. Clear WEP bit
   1227 		 * header for ieee80211_input().
   1228 		 */
   1229 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1230 	}
   1231 
   1232 	/* synchronize driver's BSSID with firmware's BSSID */
   1233 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
   1234 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
   1235 		wi_sync_bssid(sc, wh->i_addr3);
   1236 
   1237 	ieee80211_input(ifp, m, rssi, rstamp);
   1238 }
   1239 
   1240 static void
   1241 wi_tx_intr(struct wi_softc *sc)
   1242 {
   1243 	struct ieee80211com *ic = &sc->sc_ic;
   1244 	struct ifnet *ifp = &ic->ic_if;
   1245 	int fid, cur;
   1246 
   1247 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
   1248 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   1249 
   1250 	cur = sc->sc_txcur;
   1251 	if (sc->sc_txd[cur].d_fid != fid) {
   1252 		printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
   1253 		    sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
   1254 		    sc->sc_txnext);
   1255 		return;
   1256 	}
   1257 	sc->sc_tx_timer = 0;
   1258 	sc->sc_txd[cur].d_len = 0;
   1259 	sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
   1260 	if (sc->sc_txd[cur].d_len == 0)
   1261 		ifp->if_flags &= ~IFF_OACTIVE;
   1262 	else {
   1263 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
   1264 		    0, 0)) {
   1265 			printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
   1266 			sc->sc_txd[cur].d_len = 0;
   1267 		} else {
   1268 			sc->sc_tx_timer = 5;
   1269 			ifp->if_timer = 1;
   1270 		}
   1271 	}
   1272 }
   1273 
   1274 static void
   1275 wi_info_intr(struct wi_softc *sc)
   1276 {
   1277 	struct ieee80211com *ic = &sc->sc_ic;
   1278 	struct ifnet *ifp = &ic->ic_if;
   1279 	int i, fid, len, off;
   1280 	u_int16_t ltbuf[2];
   1281 	u_int16_t stat;
   1282 	u_int32_t *ptr;
   1283 
   1284 	fid = CSR_READ_2(sc, WI_INFO_FID);
   1285 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
   1286 
   1287 	switch (le16toh(ltbuf[1])) {
   1288 
   1289 	case WI_INFO_LINK_STAT:
   1290 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
   1291 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
   1292 		switch (le16toh(stat)) {
   1293 		case CONNECTED:
   1294 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1295 			if (ic->ic_state == IEEE80211_S_RUN &&
   1296 			    ic->ic_opmode != IEEE80211_M_IBSS)
   1297 				break;
   1298 			/* FALLTHROUGH */
   1299 		case AP_CHANGE:
   1300 			ieee80211_new_state(ifp, IEEE80211_S_RUN, -1);
   1301 			break;
   1302 		case AP_IN_RANGE:
   1303 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1304 			break;
   1305 		case AP_OUT_OF_RANGE:
   1306 			if (sc->sc_firmware_type == WI_SYMBOL &&
   1307 			    sc->sc_scan_timer > 0) {
   1308 				if (wi_cmd(sc, WI_CMD_INQUIRE,
   1309 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
   1310 					sc->sc_scan_timer = 0;
   1311 				break;
   1312 			}
   1313 			if (ic->ic_opmode == IEEE80211_M_STA)
   1314 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
   1315 			break;
   1316 		case DISCONNECTED:
   1317 		case ASSOC_FAILED:
   1318 			if (ic->ic_opmode == IEEE80211_M_STA)
   1319 				ieee80211_new_state(ifp, IEEE80211_S_INIT, -1);
   1320 			break;
   1321 		}
   1322 		break;
   1323 
   1324 	case WI_INFO_COUNTERS:
   1325 		/* some card versions have a larger stats structure */
   1326 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
   1327 		ptr = (u_int32_t *)&sc->sc_stats;
   1328 		off = sizeof(ltbuf);
   1329 		for (i = 0; i < len; i++, off += 2, ptr++) {
   1330 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
   1331 #ifdef WI_HERMES_STATS_WAR
   1332 			if (stat & 0xf000)
   1333 				stat = ~stat;
   1334 #endif
   1335 			*ptr += stat;
   1336 		}
   1337 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
   1338 		    sc->sc_stats.wi_tx_multi_retries +
   1339 		    sc->sc_stats.wi_tx_retry_limit;
   1340 		break;
   1341 
   1342 	case WI_INFO_SCAN_RESULTS:
   1343 	case WI_INFO_HOST_SCAN_RESULTS:
   1344 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
   1345 		break;
   1346 
   1347 	default:
   1348 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
   1349 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
   1350 		break;
   1351 	}
   1352 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
   1353 }
   1354 
   1355 /*
   1356  * Allocate a region of memory inside the NIC and zero
   1357  * it out.
   1358  */
   1359 static int
   1360 wi_write_multi(struct wi_softc *sc)
   1361 {
   1362 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1363 	int n = 0;
   1364 	struct wi_mcast mlist;
   1365 	struct ether_multi *enm;
   1366 	struct ether_multistep estep;
   1367 
   1368 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1369 allmulti:
   1370 		ifp->if_flags |= IFF_ALLMULTI;
   1371 		memset(&mlist, 0, sizeof(mlist));
   1372 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1373 		    sizeof(mlist));
   1374 	}
   1375 
   1376 	n = 0;
   1377 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1378 	while (enm != NULL) {
   1379 		/* Punt on ranges or too many multicast addresses. */
   1380 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
   1381 		    n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
   1382 			goto allmulti;
   1383 
   1384 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
   1385 		n++;
   1386 		ETHER_NEXT_MULTI(estep, enm);
   1387 	}
   1388 	ifp->if_flags &= ~IFF_ALLMULTI;
   1389 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1390 	    IEEE80211_ADDR_LEN * n);
   1391 }
   1392 
   1393 
   1394 static void
   1395 wi_read_nicid(sc)
   1396 	struct wi_softc *sc;
   1397 {
   1398 	struct wi_card_ident *id;
   1399 	char *p;
   1400 	int len;
   1401 	u_int16_t ver[4];
   1402 
   1403 	/* getting chip identity */
   1404 	memset(ver, 0, sizeof(ver));
   1405 	len = sizeof(ver);
   1406 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
   1407 	printf("%s: using ", sc->sc_dev.dv_xname);
   1408 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1409 
   1410 	sc->sc_firmware_type = WI_NOTYPE;
   1411 	for (id = wi_card_ident; id->card_name != NULL; id++) {
   1412 		if (le16toh(ver[0]) == id->card_id) {
   1413 			printf("%s", id->card_name);
   1414 			sc->sc_firmware_type = id->firm_type;
   1415 			break;
   1416 		}
   1417 	}
   1418 	if (sc->sc_firmware_type == WI_NOTYPE) {
   1419 		if (le16toh(ver[0]) & 0x8000) {
   1420 			printf("Unknown PRISM2 chip");
   1421 			sc->sc_firmware_type = WI_INTERSIL;
   1422 		} else {
   1423 			printf("Unknown Lucent chip");
   1424 			sc->sc_firmware_type = WI_LUCENT;
   1425 		}
   1426 	}
   1427 
   1428 	/* get primary firmware version (Only Prism chips) */
   1429 	if (sc->sc_firmware_type != WI_LUCENT) {
   1430 		memset(ver, 0, sizeof(ver));
   1431 		len = sizeof(ver);
   1432 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
   1433 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
   1434 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1435 DPRINTF2(("wi_read_nicid: PRI_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1436 	}
   1437 
   1438 	/* get station firmware version */
   1439 	memset(ver, 0, sizeof(ver));
   1440 	len = sizeof(ver);
   1441 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
   1442 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
   1443 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1444 DPRINTF2(("wi_read_nicid: STA_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1445 	if (sc->sc_firmware_type == WI_INTERSIL &&
   1446 	    (sc->sc_sta_firmware_ver == 10102 ||
   1447 	     sc->sc_sta_firmware_ver == 20102)) {
   1448 		char ident[12];
   1449 		memset(ident, 0, sizeof(ident));
   1450 		len = sizeof(ident);
   1451 		/* value should be the format like "V2.00-11" */
   1452 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
   1453 		    *(p = (char *)ident) >= 'A' &&
   1454 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
   1455 			sc->sc_firmware_type = WI_SYMBOL;
   1456 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
   1457 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
   1458 			    (p[6] - '0') * 10 + (p[7] - '0');
   1459 		}
   1460 DPRINTF2(("wi_read_nicid: SYMBOL_ID: %x %x %x %x\n", le16toh(ident[0]), le16toh(ident[1]), le16toh(ident[2]), le16toh(ident[3])));
   1461 	}
   1462 
   1463 	printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
   1464 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
   1465 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
   1466 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
   1467 		printf("Primary (%u.%u.%u), ",
   1468 		    sc->sc_pri_firmware_ver / 10000,
   1469 		    (sc->sc_pri_firmware_ver % 10000) / 100,
   1470 		    sc->sc_pri_firmware_ver % 100);
   1471 	printf("Station (%u.%u.%u)\n",
   1472 	    sc->sc_sta_firmware_ver / 10000,
   1473 	    (sc->sc_sta_firmware_ver % 10000) / 100,
   1474 	    sc->sc_sta_firmware_ver % 100);
   1475 }
   1476 
   1477 static int
   1478 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
   1479 {
   1480 	struct wi_ssid ssid;
   1481 
   1482 	if (buflen > IEEE80211_NWID_LEN)
   1483 		return ENOBUFS;
   1484 	memset(&ssid, 0, sizeof(ssid));
   1485 	ssid.wi_len = htole16(buflen);
   1486 	memcpy(ssid.wi_ssid, buf, buflen);
   1487 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
   1488 }
   1489 
   1490 static int
   1491 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1492 {
   1493 	struct wi_softc *sc = ifp->if_softc;
   1494 	struct ieee80211com *ic = &sc->sc_ic;
   1495 	struct ifreq *ifr = (struct ifreq *)data;
   1496 	struct wi_req wreq;
   1497 	int len, n, error;
   1498 
   1499 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1500 	if (error)
   1501 		return error;
   1502 	len = (wreq.wi_len - 1) * 2;
   1503 	if (len < sizeof(u_int16_t))
   1504 		return ENOSPC;
   1505 	if (len > sizeof(wreq.wi_val))
   1506 		len = sizeof(wreq.wi_val);
   1507 
   1508 	switch (wreq.wi_type) {
   1509 
   1510 	case WI_RID_IFACE_STATS:
   1511 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
   1512 		if (len < sizeof(sc->sc_stats))
   1513 			error = ENOSPC;
   1514 		else
   1515 			len = sizeof(sc->sc_stats);
   1516 		break;
   1517 
   1518 	case WI_RID_ENCRYPTION:
   1519 	case WI_RID_TX_CRYPT_KEY:
   1520 	case WI_RID_DEFLT_CRYPT_KEYS:
   1521 	case WI_RID_TX_RATE:
   1522 		return ieee80211_cfgget(ifp, cmd, data);
   1523 
   1524 	case WI_RID_MICROWAVE_OVEN:
   1525 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
   1526 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1527 			    &len);
   1528 			break;
   1529 		}
   1530 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
   1531 		len = sizeof(u_int16_t);
   1532 		break;
   1533 
   1534 	case WI_RID_DBM_ADJUST:
   1535 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
   1536 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1537 			    &len);
   1538 			break;
   1539 		}
   1540 		wreq.wi_val[0] = htole16(sc->sc_dbm_adjust);
   1541 		len = sizeof(u_int16_t);
   1542 		break;
   1543 
   1544 	case WI_RID_ROAMING_MODE:
   1545 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
   1546 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1547 			    &len);
   1548 			break;
   1549 		}
   1550 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
   1551 		len = sizeof(u_int16_t);
   1552 		break;
   1553 
   1554 	case WI_RID_SYSTEM_SCALE:
   1555 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
   1556 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1557 			    &len);
   1558 			break;
   1559 		}
   1560 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
   1561 		len = sizeof(u_int16_t);
   1562 		break;
   1563 
   1564 	case WI_RID_FRAG_THRESH:
   1565 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
   1566 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1567 			    &len);
   1568 			break;
   1569 		}
   1570 		wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1571 		len = sizeof(u_int16_t);
   1572 		break;
   1573 
   1574 	case WI_RID_READ_APS:
   1575 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1576 			return ieee80211_cfgget(ifp, cmd, data);
   1577 		if (sc->sc_scan_timer > 0) {
   1578 			error = EINPROGRESS;
   1579 			break;
   1580 		}
   1581 		n = sc->sc_naps;
   1582 		if (len < sizeof(n)) {
   1583 			error = ENOSPC;
   1584 			break;
   1585 		}
   1586 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
   1587 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
   1588 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
   1589 		memcpy(wreq.wi_val, &n, sizeof(n));
   1590 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
   1591 		    sizeof(struct wi_apinfo) * n);
   1592 		break;
   1593 
   1594 	default:
   1595 		if (sc->sc_enabled) {
   1596 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1597 			    &len);
   1598 			break;
   1599 		}
   1600 		switch (wreq.wi_type) {
   1601 		case WI_RID_MAX_DATALEN:
   1602 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
   1603 			len = sizeof(u_int16_t);
   1604 			break;
   1605 		case WI_RID_FRAG_THRESH:
   1606 			wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1607 			len = sizeof(u_int16_t);
   1608 			break;
   1609 		case WI_RID_RTS_THRESH:
   1610 			wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
   1611 			len = sizeof(u_int16_t);
   1612 			break;
   1613 		case WI_RID_CNFAUTHMODE:
   1614 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
   1615 			len = sizeof(u_int16_t);
   1616 			break;
   1617 		case WI_RID_NODENAME:
   1618 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
   1619 				error = ENOSPC;
   1620 				break;
   1621 			}
   1622 			len = sc->sc_nodelen + sizeof(u_int16_t);
   1623 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
   1624 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
   1625 			    sc->sc_nodelen);
   1626 			break;
   1627 		default:
   1628 			return ieee80211_cfgget(ifp, cmd, data);
   1629 		}
   1630 		break;
   1631 	}
   1632 	if (error)
   1633 		return error;
   1634 	wreq.wi_len = (len + 1) / 2 + 1;
   1635 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
   1636 }
   1637 
   1638 static int
   1639 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1640 {
   1641 	struct wi_softc *sc = ifp->if_softc;
   1642 	struct ieee80211com *ic = &sc->sc_ic;
   1643 	struct ifreq *ifr = (struct ifreq *)data;
   1644 	struct wi_req wreq;
   1645 	struct mbuf *m;
   1646 	int i, len, error;
   1647 
   1648 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1649 	if (error)
   1650 		return error;
   1651 	len = (wreq.wi_len - 1) * 2;
   1652 	switch (wreq.wi_type) {
   1653 	case WI_RID_DBM_ADJUST:
   1654 		return ENODEV;
   1655 
   1656 	case WI_RID_NODENAME:
   1657 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
   1658 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
   1659 			error = ENOSPC;
   1660 			break;
   1661 		}
   1662 		if (sc->sc_enabled) {
   1663 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1664 			    len);
   1665 			if (error)
   1666 				break;
   1667 		}
   1668 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
   1669 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
   1670 		break;
   1671 
   1672 	case WI_RID_MICROWAVE_OVEN:
   1673 	case WI_RID_ROAMING_MODE:
   1674 	case WI_RID_SYSTEM_SCALE:
   1675 	case WI_RID_FRAG_THRESH:
   1676 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
   1677 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
   1678 			break;
   1679 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
   1680 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
   1681 			break;
   1682 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
   1683 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
   1684 			break;
   1685 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
   1686 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
   1687 			break;
   1688 		/* FALLTHROUGH */
   1689 	case WI_RID_RTS_THRESH:
   1690 	case WI_RID_CNFAUTHMODE:
   1691 	case WI_RID_MAX_DATALEN:
   1692 		if (sc->sc_enabled) {
   1693 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1694 			    sizeof(u_int16_t));
   1695 			if (error)
   1696 				break;
   1697 		}
   1698 		switch (wreq.wi_type) {
   1699 		case WI_RID_FRAG_THRESH:
   1700 			sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
   1701 			break;
   1702 		case WI_RID_RTS_THRESH:
   1703 			sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
   1704 			break;
   1705 		case WI_RID_MICROWAVE_OVEN:
   1706 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
   1707 			break;
   1708 		case WI_RID_ROAMING_MODE:
   1709 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
   1710 			break;
   1711 		case WI_RID_SYSTEM_SCALE:
   1712 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
   1713 			break;
   1714 		case WI_RID_CNFAUTHMODE:
   1715 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
   1716 			break;
   1717 		case WI_RID_MAX_DATALEN:
   1718 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
   1719 			break;
   1720 		}
   1721 		break;
   1722 
   1723 	case WI_RID_TX_RATE:
   1724 		switch (le16toh(wreq.wi_val[0])) {
   1725 		case 3:
   1726 			ic->ic_fixed_rate = -1;
   1727 			break;
   1728 		default:
   1729 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   1730 				if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL)
   1731 				    / 2 == le16toh(wreq.wi_val[0]))
   1732 					break;
   1733 			}
   1734 			if (i == IEEE80211_RATE_SIZE)
   1735 				return EINVAL;
   1736 			ic->ic_fixed_rate = i;
   1737 		}
   1738 		if (sc->sc_enabled)
   1739 			error = wi_write_txrate(sc);
   1740 		break;
   1741 
   1742 	case WI_RID_SCAN_APS:
   1743 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
   1744 			error = wi_scan_ap(sc);
   1745 		break;
   1746 
   1747 	case WI_RID_MGMT_XMIT:
   1748 		if (!sc->sc_enabled) {
   1749 			error = ENETDOWN;
   1750 			break;
   1751 		}
   1752 		if (ic->ic_mgtq.ifq_len > 5) {
   1753 			error = EAGAIN;
   1754 			break;
   1755 		}
   1756 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
   1757 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
   1758 		if (m == NULL) {
   1759 			error = ENOMEM;
   1760 			break;
   1761 		}
   1762 		IF_ENQUEUE(&ic->ic_mgtq, m);
   1763 		break;
   1764 
   1765 	default:
   1766 		if (sc->sc_enabled) {
   1767 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1768 			    len);
   1769 			if (error)
   1770 				break;
   1771 		}
   1772 		error = ieee80211_cfgset(ifp, cmd, data);
   1773 		break;
   1774 	}
   1775 	return error;
   1776 }
   1777 
   1778 static int
   1779 wi_write_txrate(struct wi_softc *sc)
   1780 {
   1781 	struct ieee80211com *ic = &sc->sc_ic;
   1782 	int i;
   1783 	u_int16_t rate;
   1784 
   1785 	if (ic->ic_fixed_rate < 0)
   1786 		rate = 0;	/* auto */
   1787 	else
   1788 		rate = (ic->ic_sup_rates[ic->ic_fixed_rate] &
   1789 		    IEEE80211_RATE_VAL) / 2;
   1790 
   1791 	/* rate: 0, 1, 2, 5, 11 */
   1792 
   1793 	switch (sc->sc_firmware_type) {
   1794 	case WI_LUCENT:
   1795 		if (rate == 0)
   1796 			rate = 3;	/* auto */
   1797 		break;
   1798 	default:
   1799 		/* Choose a bit according to this table.
   1800 		 *
   1801 		 * bit | data rate
   1802 		 * ----+-------------------
   1803 		 * 0   | 1Mbps
   1804 		 * 1   | 2Mbps
   1805 		 * 2   | 5.5Mbps
   1806 		 * 3   | 11Mbps
   1807 		 */
   1808 		for (i = 8; i > 0; i >>= 1) {
   1809 			if (rate >= i)
   1810 				break;
   1811 		}
   1812 		if (i == 0)
   1813 			rate = 0xf;	/* auto */
   1814 		else
   1815 			rate = i;
   1816 		break;
   1817 	}
   1818 	return wi_write_val(sc, WI_RID_TX_RATE, rate);
   1819 }
   1820 
   1821 static int
   1822 wi_write_wep(struct wi_softc *sc)
   1823 {
   1824 	struct ieee80211com *ic = &sc->sc_ic;
   1825 	int error = 0;
   1826 	int i, keylen;
   1827 	u_int16_t val;
   1828 	struct wi_key wkey[IEEE80211_WEP_NKID];
   1829 
   1830 	switch (sc->sc_firmware_type) {
   1831 	case WI_LUCENT:
   1832 		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
   1833 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
   1834 		if (error)
   1835 			break;
   1836 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
   1837 		if (error)
   1838 			break;
   1839 		memset(wkey, 0, sizeof(wkey));
   1840 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1841 			keylen = ic->ic_nw_keys[i].wk_len;
   1842 			wkey[i].wi_keylen = htole16(keylen);
   1843 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
   1844 			    keylen);
   1845 		}
   1846 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
   1847 		    wkey, sizeof(wkey));
   1848 		break;
   1849 
   1850 	case WI_INTERSIL:
   1851 	case WI_SYMBOL:
   1852 		if (ic->ic_flags & IEEE80211_F_WEPON) {
   1853 			/*
   1854 			 * ONLY HWB3163 EVAL-CARD Firmware version
   1855 			 * less than 0.8 variant2
   1856 			 *
   1857 			 *   If promiscuous mode disable, Prism2 chip
   1858 			 *  does not work with WEP .
   1859 			 * It is under investigation for details.
   1860 			 * (ichiro (at) netbsd.org)
   1861 			 */
   1862 			if (sc->sc_firmware_type == WI_INTERSIL &&
   1863 			    sc->sc_sta_firmware_ver < 802 ) {
   1864 				/* firm ver < 0.8 variant 2 */
   1865 				wi_write_val(sc, WI_RID_PROMISC, 1);
   1866 			}
   1867 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   1868 			    sc->sc_cnfauthmode);
   1869 			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
   1870 			/*
   1871 			 * Encryption firmware has a bug for HostAP mode.
   1872 			 */
   1873 			if (sc->sc_firmware_type == WI_INTERSIL &&
   1874 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
   1875 				val |= HOST_ENCRYPT;
   1876 		} else {
   1877 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   1878 			    IEEE80211_AUTH_OPEN);
   1879 			val = HOST_ENCRYPT | HOST_DECRYPT;
   1880 		}
   1881 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
   1882 		if (error)
   1883 			break;
   1884 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
   1885 		    ic->ic_wep_txkey);
   1886 		if (error)
   1887 			break;
   1888 		/*
   1889 		 * It seems that the firmware accept 104bit key only if
   1890 		 * all the keys have 104bit length.  We get the length of
   1891 		 * the transmit key and use it for all other keys.
   1892 		 * Perhaps we should use software WEP for such situation.
   1893 		 */
   1894 		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
   1895 		if (keylen > IEEE80211_WEP_KEYLEN)
   1896 			keylen = 13;	/* 104bit keys */
   1897 		else
   1898 			keylen = IEEE80211_WEP_KEYLEN;
   1899 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1900 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
   1901 			    ic->ic_nw_keys[i].wk_key, keylen);
   1902 			if (error)
   1903 				break;
   1904 		}
   1905 		break;
   1906 	}
   1907 	return error;
   1908 }
   1909 
   1910 /* Must be called at proper protection level! */
   1911 static int
   1912 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   1913 {
   1914 	int i, status;
   1915 
   1916 	/* wait for the busy bit to clear */
   1917 	for (i = 0; ; i++) {
   1918 		if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
   1919 			break;
   1920 		if (i == WI_TIMEOUT) {
   1921 			printf("%s: wi_cmd: BUSY did not clear, "
   1922 			    "cmd=0x%x, prev=0x%x\n", sc->sc_dev.dv_xname,
   1923 			    cmd, CSR_READ_2(sc, WI_COMMAND));
   1924 			return EIO;
   1925 		}
   1926 		DELAY(1);
   1927 	}
   1928 
   1929 	CSR_WRITE_2(sc, WI_PARAM0, val0);
   1930 	CSR_WRITE_2(sc, WI_PARAM1, val1);
   1931 	CSR_WRITE_2(sc, WI_PARAM2, val2);
   1932 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
   1933 
   1934 	if (cmd == WI_CMD_INI) {
   1935 		/* XXX: should sleep here. */
   1936 		DELAY(100*1000);
   1937 	}
   1938 	/* wait for the cmd completed bit */
   1939 	for (i = 0; i < WI_TIMEOUT; i++) {
   1940 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
   1941 			break;
   1942 		DELAY(1);
   1943 	}
   1944 
   1945 	status = CSR_READ_2(sc, WI_STATUS);
   1946 
   1947 	/* Ack the command */
   1948 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
   1949 
   1950 	if (i == WI_TIMEOUT) {
   1951 		printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
   1952 		    sc->sc_dev.dv_xname, cmd, val0);
   1953 		return ETIMEDOUT;
   1954 	}
   1955 
   1956 	if (status & WI_STAT_CMD_RESULT) {
   1957 		printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
   1958 		    sc->sc_dev.dv_xname, cmd, val0);
   1959 		return EIO;
   1960 	}
   1961 	return 0;
   1962 }
   1963 
   1964 static int
   1965 wi_seek_bap(struct wi_softc *sc, int id, int off)
   1966 {
   1967 	int i, status;
   1968 
   1969 	CSR_WRITE_2(sc, WI_SEL0, id);
   1970 	CSR_WRITE_2(sc, WI_OFF0, off);
   1971 
   1972 	for (i = 0; ; i++) {
   1973 		status = CSR_READ_2(sc, WI_OFF0);
   1974 		if ((status & WI_OFF_BUSY) == 0)
   1975 			break;
   1976 		if (i == WI_TIMEOUT) {
   1977 			printf("%s: timeout in wi_seek to %x/%x\n",
   1978 			    sc->sc_dev.dv_xname, id, off);
   1979 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   1980 			return ETIMEDOUT;
   1981 		}
   1982 		DELAY(1);
   1983 	}
   1984 	if (status & WI_OFF_ERR) {
   1985 		printf("%s: failed in wi_seek to %x/%x\n",
   1986 		    sc->sc_dev.dv_xname, id, off);
   1987 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   1988 		return EIO;
   1989 	}
   1990 	sc->sc_bap_id = id;
   1991 	sc->sc_bap_off = off;
   1992 	return 0;
   1993 }
   1994 
   1995 static int
   1996 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   1997 {
   1998 	int error, cnt;
   1999 
   2000 	if (buflen == 0)
   2001 		return 0;
   2002 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2003 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2004 			return error;
   2005 	}
   2006 	cnt = (buflen + 1) / 2;
   2007 	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2008 	sc->sc_bap_off += cnt * 2;
   2009 	return 0;
   2010 }
   2011 
   2012 static int
   2013 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2014 {
   2015 	int error, cnt;
   2016 
   2017 	if (buflen == 0)
   2018 		return 0;
   2019 
   2020 #ifdef WI_HERMES_AUTOINC_WAR
   2021   again:
   2022 #endif
   2023 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2024 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2025 			return error;
   2026 	}
   2027 	cnt = (buflen + 1) / 2;
   2028 	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2029 	sc->sc_bap_off += cnt * 2;
   2030 
   2031 #ifdef WI_HERMES_AUTOINC_WAR
   2032 	/*
   2033 	 * According to the comments in the HCF Light code, there is a bug
   2034 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
   2035 	 * where the chip's internal autoincrement counter gets thrown off
   2036 	 * during data writes:  the autoincrement is missed, causing one
   2037 	 * data word to be overwritten and subsequent words to be written to
   2038 	 * the wrong memory locations. The end result is that we could end
   2039 	 * up transmitting bogus frames without realizing it. The workaround
   2040 	 * for this is to write a couple of extra guard words after the end
   2041 	 * of the transfer, then attempt to read then back. If we fail to
   2042 	 * locate the guard words where we expect them, we preform the
   2043 	 * transfer over again.
   2044 	 */
   2045 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
   2046 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
   2047 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
   2048 		wi_seek_bap(sc, id, sc->sc_bap_off);
   2049 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2050 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
   2051 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
   2052 			printf("%s: detect auto increment bug, try again\n",
   2053 			    sc->sc_dev.dv_xname);
   2054 			goto again;
   2055 		}
   2056 	}
   2057 #endif
   2058 	return 0;
   2059 }
   2060 
   2061 static int
   2062 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
   2063 {
   2064 	int error, len;
   2065 	struct mbuf *m;
   2066 
   2067 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
   2068 		if (m->m_len == 0)
   2069 			continue;
   2070 
   2071 		len = min(m->m_len, totlen);
   2072 
   2073 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
   2074 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
   2075 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
   2076 			    totlen);
   2077 		}
   2078 
   2079 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
   2080 			return error;
   2081 
   2082 		off += m->m_len;
   2083 		totlen -= len;
   2084 	}
   2085 	return 0;
   2086 }
   2087 
   2088 static int
   2089 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
   2090 {
   2091 	int i;
   2092 
   2093 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
   2094 		printf("%s: failed to allocate %d bytes on NIC\n",
   2095 		    sc->sc_dev.dv_xname, len);
   2096 		return ENOMEM;
   2097 	}
   2098 
   2099 	for (i = 0; i < WI_TIMEOUT; i++) {
   2100 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
   2101 			break;
   2102 		if (i == WI_TIMEOUT) {
   2103 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
   2104 			return ETIMEDOUT;
   2105 		}
   2106 		DELAY(1);
   2107 	}
   2108 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
   2109 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   2110 	return 0;
   2111 }
   2112 
   2113 static int
   2114 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
   2115 {
   2116 	int error, len;
   2117 	u_int16_t ltbuf[2];
   2118 
   2119 	/* Tell the NIC to enter record read mode. */
   2120 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
   2121 	if (error)
   2122 		return error;
   2123 
   2124 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2125 	if (error)
   2126 		return error;
   2127 
   2128 	if (le16toh(ltbuf[1]) != rid) {
   2129 		printf("%s: record read mismatch, rid=%x, got=%x\n",
   2130 		    sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
   2131 		return EIO;
   2132 	}
   2133 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
   2134 	if (*buflenp < len) {
   2135 		printf("%s: record buffer is too small, "
   2136 		    "rid=%x, size=%d, len=%d\n",
   2137 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
   2138 		return ENOSPC;
   2139 	}
   2140 	*buflenp = len;
   2141 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
   2142 }
   2143 
   2144 static int
   2145 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
   2146 {
   2147 	int error;
   2148 	u_int16_t ltbuf[2];
   2149 
   2150 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
   2151 	ltbuf[1] = htole16(rid);
   2152 
   2153 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2154 	if (error)
   2155 		return error;
   2156 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
   2157 	if (error)
   2158 		return error;
   2159 
   2160 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
   2161 }
   2162 
   2163 static int
   2164 wi_newstate(void *arg, enum ieee80211_state nstate)
   2165 {
   2166 	struct wi_softc *sc = arg;
   2167 	struct ieee80211com *ic = &sc->sc_ic;
   2168 	struct ieee80211_node *ni = &ic->ic_bss;
   2169 	int i, buflen;
   2170 	u_int16_t val;
   2171 	struct wi_ssid ssid;
   2172 	u_int8_t old_bssid[IEEE80211_ADDR_LEN];
   2173 	enum ieee80211_state ostate;
   2174 #ifdef WI_DEBUG
   2175 	static const char *stname[] =
   2176 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   2177 #endif /* WI_DEBUG */
   2178 
   2179 	ostate = ic->ic_state;
   2180 	DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
   2181 
   2182 	ic->ic_state = nstate;
   2183 	switch (nstate) {
   2184 	case IEEE80211_S_INIT:
   2185 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   2186 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2187 		return 0;
   2188 
   2189 	case IEEE80211_S_RUN:
   2190 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2191 		buflen = IEEE80211_ADDR_LEN;
   2192 		IEEE80211_ADDR_COPY(old_bssid, ni->ni_bssid);
   2193 		wi_read_rid(sc, WI_RID_CURRENT_BSSID, ni->ni_bssid, &buflen);
   2194 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
   2195 		buflen = sizeof(val);
   2196 		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
   2197 		ni->ni_chan = le16toh(val);
   2198 
   2199 		if (IEEE80211_ADDR_EQ(old_bssid, ni->ni_bssid))
   2200 			sc->sc_false_syns++;
   2201 		else
   2202 			sc->sc_false_syns = 0;
   2203 
   2204 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   2205 			ni->ni_esslen = ic->ic_des_esslen;
   2206 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
   2207 			ni->ni_nrate = 0;
   2208 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   2209 				if (ic->ic_sup_rates[i])
   2210 					ni->ni_rates[ni->ni_nrate++] =
   2211 					    ic->ic_sup_rates[i];
   2212 			}
   2213 			ni->ni_intval = ic->ic_lintval;
   2214 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
   2215 			if (ic->ic_flags & IEEE80211_F_WEPON)
   2216 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2217 		} else {
   2218 			buflen = sizeof(ssid);
   2219 			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
   2220 			ni->ni_esslen = le16toh(ssid.wi_len);
   2221 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
   2222 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
   2223 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
   2224 		}
   2225 		break;
   2226 
   2227 	case IEEE80211_S_SCAN:
   2228 	case IEEE80211_S_AUTH:
   2229 	case IEEE80211_S_ASSOC:
   2230 		break;
   2231 	}
   2232 
   2233 	/* skip standard ieee80211 handling */
   2234 	return EINPROGRESS;
   2235 }
   2236 
   2237 static int
   2238 wi_scan_ap(struct wi_softc *sc)
   2239 {
   2240 	int error = 0;
   2241 	u_int16_t val[2];
   2242 
   2243 	if (!sc->sc_enabled)
   2244 		return ENXIO;
   2245 	switch (sc->sc_firmware_type) {
   2246 	case WI_LUCENT:
   2247 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   2248 		break;
   2249 	case WI_INTERSIL:
   2250 		val[0] = 0x3fff;	/* channel */
   2251 		val[1] = 0x000f;	/* tx rate */
   2252 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
   2253 		break;
   2254 	case WI_SYMBOL:
   2255 		/*
   2256 		 * XXX only supported on 3.x ?
   2257 		 */
   2258 		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
   2259 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
   2260 		    val, sizeof(val[0]));
   2261 		break;
   2262 	}
   2263 	if (error == 0) {
   2264 		sc->sc_scan_timer = WI_SCAN_WAIT;
   2265 		sc->sc_ic.ic_if.if_timer = 1;
   2266 		DPRINTF(("wi_scan_ap: start scanning\n"));
   2267 	}
   2268 	return error;
   2269 }
   2270 
   2271 static void
   2272 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
   2273 {
   2274 	int i, naps, off, szbuf;
   2275 	struct wi_scan_header ws_hdr;	/* Prism2 header */
   2276 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
   2277 	struct wi_apinfo *ap;
   2278 
   2279 	off = sizeof(u_int16_t) * 2;
   2280 	memset(&ws_hdr, 0, sizeof(ws_hdr));
   2281 	switch (sc->sc_firmware_type) {
   2282 	case WI_INTERSIL:
   2283 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
   2284 		off += sizeof(ws_hdr);
   2285 		szbuf = sizeof(struct wi_scan_data_p2);
   2286 		break;
   2287 	case WI_SYMBOL:
   2288 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
   2289 		break;
   2290 	case WI_LUCENT:
   2291 		szbuf = sizeof(struct wi_scan_data);
   2292 		break;
   2293 	}
   2294 	naps = (cnt * 2 + 2 - off) / szbuf;
   2295 	if (naps > MAXAPINFO)
   2296 		naps = MAXAPINFO;
   2297 	sc->sc_naps = naps;
   2298 	/* Read Data */
   2299 	ap = sc->sc_aps;
   2300 	memset(&ws_dat, 0, sizeof(ws_dat));
   2301 	for (i = 0; i < naps; i++, ap++) {
   2302 		wi_read_bap(sc, fid, off, &ws_dat,
   2303 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
   2304 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
   2305 		    ether_sprintf(ws_dat.wi_bssid)));
   2306 		off += szbuf;
   2307 		ap->scanreason = le16toh(ws_hdr.wi_reason);
   2308 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
   2309 		ap->channel = le16toh(ws_dat.wi_chid);
   2310 		ap->signal  = le16toh(ws_dat.wi_signal);
   2311 		ap->noise   = le16toh(ws_dat.wi_noise);
   2312 		ap->quality = ap->signal - ap->noise;
   2313 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
   2314 		ap->interval = le16toh(ws_dat.wi_interval);
   2315 		ap->rate    = le16toh(ws_dat.wi_rate);
   2316 		ap->namelen = le16toh(ws_dat.wi_namelen);
   2317 		if (ap->namelen > sizeof(ap->name))
   2318 			ap->namelen = sizeof(ap->name);
   2319 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
   2320 	}
   2321 	/* Done scanning */
   2322 	sc->sc_scan_timer = 0;
   2323 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
   2324 }
   2325