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