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