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