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