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