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