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