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