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