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