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