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