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