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