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