Home | History | Annotate | Line # | Download | only in ic
wi.c revision 1.199
      1 /*	$NetBSD: wi.c,v 1.199 2005/06/25 03:56:53 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997, 1998, 1999
     41  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Bill Paul.
     54  * 4. Neither the name of the author nor the names of any co-contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     68  * THE POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
     73  *
     74  * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
     75  * Electrical Engineering Department
     76  * Columbia University, New York City
     77  */
     78 
     79 /*
     80  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
     81  * from Lucent. Unlike the older cards, the new ones are programmed
     82  * entirely via a firmware-driven controller called the Hermes.
     83  * Unfortunately, Lucent will not release the Hermes programming manual
     84  * without an NDA (if at all). What they do release is an API library
     85  * called the HCF (Hardware Control Functions) which is supposed to
     86  * do the device-specific operations of a device driver for you. The
     87  * publically available version of the HCF library (the 'HCF Light') is
     88  * a) extremely gross, b) lacks certain features, particularly support
     89  * for 802.11 frames, and c) is contaminated by the GNU Public License.
     90  *
     91  * This driver does not use the HCF or HCF Light at all. Instead, it
     92  * programs the Hermes controller directly, using information gleaned
     93  * from the HCF Light code and corresponding documentation.
     94  *
     95  * This driver supports both the PCMCIA and ISA versions of the
     96  * WaveLAN/IEEE cards. Note however that the ISA card isn't really
     97  * anything of the sort: it's actually a PCMCIA bridge adapter
     98  * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
     99  * inserted. Consequently, you need to use the pccard support for
    100  * both the ISA and PCMCIA adapters.
    101  */
    102 
    103 /*
    104  * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
    105  * Oslo IETF plenary meeting.
    106  */
    107 
    108 #include <sys/cdefs.h>
    109 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.199 2005/06/25 03:56:53 dyoung Exp $");
    110 
    111 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
    112 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
    113 #undef WI_HISTOGRAM
    114 #undef WI_RING_DEBUG
    115 #define STATIC static
    116 
    117 #include "bpfilter.h"
    118 
    119 #include <sys/param.h>
    120 #include <sys/systm.h>
    121 #include <sys/callout.h>
    122 #include <sys/device.h>
    123 #include <sys/socket.h>
    124 #include <sys/mbuf.h>
    125 #include <sys/ioctl.h>
    126 #include <sys/kernel.h>		/* for hz */
    127 #include <sys/proc.h>
    128 
    129 #include <net/if.h>
    130 #include <net/if_dl.h>
    131 #include <net/if_llc.h>
    132 #include <net/if_media.h>
    133 #include <net/if_ether.h>
    134 #include <net/route.h>
    135 
    136 #include <net80211/ieee80211_netbsd.h>
    137 #include <net80211/ieee80211_var.h>
    138 #include <net80211/ieee80211_ioctl.h>
    139 #include <net80211/ieee80211_radiotap.h>
    140 #include <net80211/ieee80211_rssadapt.h>
    141 
    142 #if NBPFILTER > 0
    143 #include <net/bpf.h>
    144 #include <net/bpfdesc.h>
    145 #endif
    146 
    147 #include <machine/bus.h>
    148 
    149 #include <dev/ic/wi_ieee.h>
    150 #include <dev/ic/wireg.h>
    151 #include <dev/ic/wivar.h>
    152 
    153 STATIC int  wi_init(struct ifnet *);
    154 STATIC void wi_stop(struct ifnet *, int);
    155 STATIC void wi_start(struct ifnet *);
    156 STATIC int  wi_reset(struct wi_softc *);
    157 STATIC void wi_watchdog(struct ifnet *);
    158 STATIC int  wi_ioctl(struct ifnet *, u_long, caddr_t);
    159 STATIC int  wi_media_change(struct ifnet *);
    160 STATIC void wi_media_status(struct ifnet *, struct ifmediareq *);
    161 
    162 STATIC struct ieee80211_node *wi_node_alloc(struct ieee80211_node_table *);
    163 STATIC void wi_node_free(struct ieee80211_node *);
    164 
    165 STATIC void wi_raise_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
    166 STATIC void wi_lower_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
    167 STATIC int wi_choose_rate(struct ieee80211com *, struct ieee80211_node *,
    168     struct ieee80211_frame *, u_int);
    169 STATIC void wi_rssadapt_updatestats_cb(void *, struct ieee80211_node *);
    170 STATIC void wi_rssadapt_updatestats(void *);
    171 STATIC void wi_rssdescs_init(struct wi_rssdesc (*)[], wi_rssdescq_t *);
    172 STATIC void wi_rssdescs_reset(struct ieee80211com *, struct wi_rssdesc (*)[],
    173     wi_rssdescq_t *, u_int8_t (*)[]);
    174 STATIC void wi_sync_bssid(struct wi_softc *, u_int8_t new_bssid[]);
    175 
    176 STATIC void wi_rx_intr(struct wi_softc *);
    177 STATIC void wi_txalloc_intr(struct wi_softc *);
    178 STATIC void wi_cmd_intr(struct wi_softc *);
    179 STATIC void wi_tx_intr(struct wi_softc *);
    180 STATIC void wi_tx_ex_intr(struct wi_softc *);
    181 STATIC void wi_info_intr(struct wi_softc *);
    182 
    183 STATIC 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 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    786 	    sc->sc_firmware_type == WI_INTERSIL) {
    787 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
    788 		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
    789 	}
    790 
    791 	if (sc->sc_firmware_type == WI_INTERSIL) {
    792 		struct ieee80211_rateset *rs =
    793 		    &ic->ic_sup_rates[IEEE80211_MODE_11B];
    794 		u_int16_t basic = 0, supported = 0, rate;
    795 
    796 		for (i = 0; i < rs->rs_nrates; i++) {
    797 			switch (rs->rs_rates[i] & IEEE80211_RATE_VAL) {
    798 			case 2:
    799 				rate = 1;
    800 				break;
    801 			case 4:
    802 				rate = 2;
    803 				break;
    804 			case 11:
    805 				rate = 4;
    806 				break;
    807 			case 22:
    808 				rate = 8;
    809 				break;
    810 			default:
    811 				rate = 0;
    812 				break;
    813 			}
    814 			if (rs->rs_rates[i] & IEEE80211_RATE_BASIC)
    815 				basic |= rate;
    816 			supported |= rate;
    817 		}
    818 		wi_write_val(sc, WI_RID_BASIC_RATE, basic);
    819 		wi_write_val(sc, WI_RID_SUPPORT_RATE, supported);
    820 		wi_write_val(sc, WI_RID_ALT_RETRY_COUNT, sc->sc_alt_retry);
    821 	}
    822 
    823 	/*
    824 	 * Initialize promisc mode.
    825 	 *	Being in Host-AP mode causes a great
    826 	 *	deal of pain if promiscuous mode is set.
    827 	 *	Therefore we avoid confusing the firmware
    828 	 *	and always reset promisc mode in Host-AP
    829 	 *	mode.  Host-AP sees all the packets anyway.
    830 	 */
    831 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    832 	    (ifp->if_flags & IFF_PROMISC) != 0) {
    833 		wi_write_val(sc, WI_RID_PROMISC, 1);
    834 	} else {
    835 		wi_write_val(sc, WI_RID_PROMISC, 0);
    836 	}
    837 
    838 	/* Configure WEP. */
    839 	if (ic->ic_caps & IEEE80211_C_WEP) {
    840 		sc->sc_cnfauthmode = ic->ic_bss->ni_authmode;
    841 		wi_write_wep(sc);
    842 	}
    843 
    844 	/* Set multicast filter. */
    845 	wi_write_multi(sc);
    846 
    847 	sc->sc_txalloc = 0;
    848 	sc->sc_txalloced = 0;
    849 	sc->sc_txqueue = 0;
    850 	sc->sc_txqueued = 0;
    851 	sc->sc_txstart = 0;
    852 	sc->sc_txstarted = 0;
    853 
    854 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
    855 		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
    856 		if (sc->sc_firmware_type == WI_SYMBOL)
    857 			sc->sc_buflen = 1585;	/* XXX */
    858 		for (i = 0; i < WI_NTXBUF; i++) {
    859 			error = wi_alloc_fid(sc, sc->sc_buflen,
    860 			    &sc->sc_txd[i].d_fid);
    861 			if (error) {
    862 				printf("%s: tx buffer allocation failed\n",
    863 				    sc->sc_dev.dv_xname);
    864 				goto out;
    865 			}
    866 			DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
    867 			    sc->sc_txd[i].d_fid));
    868 			++sc->sc_txalloced;
    869 		}
    870 	}
    871 
    872 	wi_rssdescs_init(&sc->sc_rssd, &sc->sc_rssdfree);
    873 
    874 	/* Enable desired port */
    875 	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
    876 	ifp->if_flags |= IFF_RUNNING;
    877 	ifp->if_flags &= ~IFF_OACTIVE;
    878 	ic->ic_state = IEEE80211_S_INIT;
    879 
    880 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
    881 	    ic->ic_opmode == IEEE80211_M_IBSS ||
    882 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
    883 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
    884 		ieee80211_create_ibss(ic, ic->ic_ibss_chan);
    885 
    886 	/* Enable interrupts */
    887 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    888 
    889 	if (!wasenabled &&
    890 	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
    891 	    sc->sc_firmware_type == WI_INTERSIL) {
    892 		/* XXX: some card need to be re-enabled for hostap */
    893 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
    894 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
    895 	}
    896 
    897 	if (ic->ic_opmode == IEEE80211_M_STA &&
    898 	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
    899 	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
    900 		memset(&join, 0, sizeof(join));
    901 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    902 			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
    903 		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
    904 			join.wi_chan =
    905 			    htole16(ieee80211_chan2ieee(ic, ic->ic_des_chan));
    906 		/* Lucent firmware does not support the JOIN RID. */
    907 		if (sc->sc_firmware_type != WI_LUCENT)
    908 			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
    909 	}
    910 
    911  out:
    912 	if (error) {
    913 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    914 		wi_stop(ifp, 0);
    915 	}
    916 	DPRINTF(("wi_init: return %d\n", error));
    917 	return error;
    918 }
    919 
    920 STATIC void
    921 wi_txcmd_wait(struct wi_softc *sc)
    922 {
    923 	KASSERT(sc->sc_txcmds == 1);
    924 	if (sc->sc_status & WI_EV_CMD) {
    925 		sc->sc_status &= ~WI_EV_CMD;
    926 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
    927 	} else
    928 		(void)wi_cmd_wait(sc, WI_CMD_TX | WI_RECLAIM, 0);
    929 }
    930 
    931 STATIC void
    932 wi_stop(struct ifnet *ifp, int disable)
    933 {
    934 	struct wi_softc	*sc = ifp->if_softc;
    935 	struct ieee80211com *ic = &sc->sc_ic;
    936 	int s;
    937 
    938 	if (!sc->sc_enabled)
    939 		return;
    940 
    941 	s = splnet();
    942 
    943 	DPRINTF(("wi_stop: disable %d\n", disable));
    944 
    945 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    946 
    947 	/* wait for tx command completion (deassoc, deauth) */
    948 	while (sc->sc_txcmds > 0) {
    949 		wi_txcmd_wait(sc);
    950 		wi_cmd_intr(sc);
    951 	}
    952 
    953 	/* TBD wait for deassoc, deauth tx completion? */
    954 
    955 	if (!sc->sc_invalid) {
    956 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    957 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
    958 	}
    959 
    960 	wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
    961 	    &sc->sc_txpending);
    962 
    963 	sc->sc_tx_timer = 0;
    964 	sc->sc_scan_timer = 0;
    965 	sc->sc_false_syns = 0;
    966 	sc->sc_naps = 0;
    967 	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
    968 	ifp->if_timer = 0;
    969 
    970 	if (disable) {
    971 		if (sc->sc_disable)
    972 			(*sc->sc_disable)(sc);
    973 		sc->sc_enabled = 0;
    974 	}
    975 	splx(s);
    976 }
    977 
    978 /*
    979  * Choose a data rate for a packet len bytes long that suits the packet
    980  * type and the wireless conditions.
    981  *
    982  * TBD Adapt fragmentation threshold.
    983  */
    984 STATIC int
    985 wi_choose_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
    986     struct ieee80211_frame *wh, u_int len)
    987 {
    988 	struct wi_softc	*sc = ic->ic_ifp->if_softc;
    989 	struct wi_node *wn = (void*)ni;
    990 	struct ieee80211_rssadapt *ra = &wn->wn_rssadapt;
    991 	int do_not_adapt, i, rateidx, s;
    992 
    993 	do_not_adapt = (ic->ic_opmode != IEEE80211_M_HOSTAP) &&
    994 	    (sc->sc_flags & WI_FLAGS_RSSADAPTSTA) == 0;
    995 
    996 	s = splnet();
    997 
    998 	rateidx = ieee80211_rssadapt_choose(ra, &ni->ni_rates, wh, len,
    999 	    ic->ic_fixed_rate,
   1000 	    ((ic->ic_ifp->if_flags & IFF_DEBUG) == 0) ? NULL : ic->ic_ifp->if_xname,
   1001 	    do_not_adapt);
   1002 
   1003 	ni->ni_txrate = rateidx;
   1004 
   1005 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
   1006 		/* choose the slowest pending rate so that we don't
   1007 		 * accidentally send a packet on the MAC's queue
   1008 		 * too fast. TBD find out if the MAC labels Tx
   1009 		 * packets w/ rate when enqueued or dequeued.
   1010 		 */
   1011 		for (i = 0; i < rateidx && sc->sc_txpending[i] == 0; i++);
   1012 		rateidx = i;
   1013 	}
   1014 
   1015 	splx(s);
   1016 	return (rateidx);
   1017 }
   1018 
   1019 STATIC void
   1020 wi_raise_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
   1021 {
   1022 	struct wi_node *wn;
   1023 	if (id->id_node == NULL)
   1024 		return;
   1025 
   1026 	wn = (void*)id->id_node;
   1027 	ieee80211_rssadapt_raise_rate(ic, &wn->wn_rssadapt, id);
   1028 }
   1029 
   1030 STATIC void
   1031 wi_lower_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
   1032 {
   1033 	struct ieee80211_node *ni;
   1034 	struct wi_node *wn;
   1035 	int s;
   1036 
   1037 	s = splnet();
   1038 
   1039 	if ((ni = id->id_node) == NULL) {
   1040 		DPRINTF(("wi_lower_rate: missing node\n"));
   1041 		goto out;
   1042 	}
   1043 
   1044 	wn = (void *)ni;
   1045 
   1046 	ieee80211_rssadapt_lower_rate(ic, ni, &wn->wn_rssadapt, id);
   1047 out:
   1048 	splx(s);
   1049 	return;
   1050 }
   1051 
   1052 STATIC void
   1053 wi_start(struct ifnet *ifp)
   1054 {
   1055 	struct wi_softc	*sc = ifp->if_softc;
   1056 	struct ieee80211com *ic = &sc->sc_ic;
   1057 	struct ether_header *eh;
   1058 	struct ieee80211_node *ni;
   1059 	struct ieee80211_frame *wh;
   1060 	struct ieee80211_rateset *rs;
   1061 	struct wi_rssdesc *rd;
   1062 	struct ieee80211_rssdesc *id;
   1063 	struct mbuf *m0;
   1064 	struct wi_frame frmhdr;
   1065 	int cur, fid, off, rateidx;
   1066 
   1067 	if (!sc->sc_enabled || sc->sc_invalid)
   1068 		return;
   1069 	if (sc->sc_flags & WI_FLAGS_OUTRANGE)
   1070 		return;
   1071 
   1072 	memset(&frmhdr, 0, sizeof(frmhdr));
   1073 	cur = sc->sc_txqueue;
   1074 	for (;;) {
   1075 		ni = ic->ic_bss;
   1076 		if (sc->sc_txalloced == 0 || SLIST_EMPTY(&sc->sc_rssdfree)) {
   1077 			ifp->if_flags |= IFF_OACTIVE;
   1078 			break;
   1079 		}
   1080 		if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
   1081 			IF_DEQUEUE(&ic->ic_mgtq, m0);
   1082 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
   1083 			    (caddr_t)&frmhdr.wi_ehdr);
   1084 			frmhdr.wi_ehdr.ether_type = 0;
   1085                         wh = mtod(m0, struct ieee80211_frame *);
   1086 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   1087 			m0->m_pkthdr.rcvif = NULL;
   1088 		} else if (ic->ic_state == IEEE80211_S_RUN) {
   1089 			IFQ_POLL(&ifp->if_snd, m0);
   1090 			if (m0 == NULL)
   1091 				break;
   1092 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   1093 			ifp->if_opackets++;
   1094 			m_copydata(m0, 0, ETHER_HDR_LEN,
   1095 			    (caddr_t)&frmhdr.wi_ehdr);
   1096 #if NBPFILTER > 0
   1097 			if (ifp->if_bpf)
   1098 				bpf_mtap(ifp->if_bpf, m0);
   1099 #endif
   1100 
   1101 			eh = mtod(m0, struct ether_header *);
   1102 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1103 			if (ni == NULL) {
   1104 				ifp->if_oerrors++;
   1105 				continue;
   1106 			}
   1107 			if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
   1108 			    (m0->m_flags & M_PWR_SAV) == 0) {
   1109 				ieee80211_pwrsave(ic, ni, m0);
   1110 				goto next;
   1111 			}
   1112 			if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
   1113 				ieee80211_free_node(ni);
   1114 				ifp->if_oerrors++;
   1115 				continue;
   1116 			}
   1117 			wh = mtod(m0, struct ieee80211_frame *);
   1118 		} else
   1119 			break;
   1120 #if NBPFILTER > 0
   1121 		if (ic->ic_rawbpf)
   1122 			bpf_mtap(ic->ic_rawbpf, m0);
   1123 #endif
   1124 		frmhdr.wi_tx_ctl =
   1125 		    htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX|WI_TXCNTL_TX_OK);
   1126 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1127 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
   1128 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
   1129 		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
   1130 			if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
   1131 				ifp->if_oerrors++;
   1132 				goto next;
   1133 			}
   1134 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
   1135 		}
   1136 
   1137 		rateidx = wi_choose_rate(ic, ni, wh, m0->m_pkthdr.len);
   1138 		rs = &ni->ni_rates;
   1139 
   1140 #if NBPFILTER > 0
   1141 		if (sc->sc_drvbpf) {
   1142 			struct wi_tx_radiotap_header *tap = &sc->sc_txtap;
   1143 
   1144 			tap->wt_rate = rs->rs_rates[rateidx];
   1145 			tap->wt_chan_freq =
   1146 			    htole16(ic->ic_bss->ni_chan->ic_freq);
   1147 			tap->wt_chan_flags =
   1148 			    htole16(ic->ic_bss->ni_chan->ic_flags);
   1149 			/* TBD tap->wt_flags */
   1150 
   1151 			bpf_mtap2(sc->sc_drvbpf, tap, tap->wt_ihdr.it_len, m0);
   1152 		}
   1153 #endif
   1154 
   1155 		rd = SLIST_FIRST(&sc->sc_rssdfree);
   1156 		id = &rd->rd_desc;
   1157 		id->id_len = m0->m_pkthdr.len;
   1158 		id->id_rateidx = ni->ni_txrate;
   1159 		id->id_rssi = ni->ni_rssi;
   1160 
   1161 		frmhdr.wi_tx_idx = rd - sc->sc_rssd;
   1162 
   1163 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1164 			frmhdr.wi_tx_rate = 5 * (rs->rs_rates[rateidx] &
   1165 			    IEEE80211_RATE_VAL);
   1166 		else if (sc->sc_flags & WI_FLAGS_RSSADAPTSTA)
   1167 			(void)wi_write_txrate(sc, rs->rs_rates[rateidx]);
   1168 
   1169 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
   1170 		    (caddr_t)&frmhdr.wi_whdr);
   1171 		m_adj(m0, sizeof(struct ieee80211_frame));
   1172 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
   1173 		if (IFF_DUMPPKTS(ifp))
   1174 			wi_dump_pkt(&frmhdr, ni, -1);
   1175 		fid = sc->sc_txd[cur].d_fid;
   1176 		off = sizeof(frmhdr);
   1177 		if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 ||
   1178 		    wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) {
   1179 			printf("%s: %s write fid %x failed\n",
   1180 			    sc->sc_dev.dv_xname, __func__, fid);
   1181 			ifp->if_oerrors++;
   1182 			m_freem(m0);
   1183 			goto next;
   1184 		}
   1185 		m_freem(m0);
   1186 		sc->sc_txpending[ni->ni_txrate]++;
   1187 		--sc->sc_txalloced;
   1188 		if (sc->sc_txqueued++ == 0) {
   1189 #ifdef DIAGNOSTIC
   1190 			if (cur != sc->sc_txstart)
   1191 				printf("%s: ring is desynchronized\n",
   1192 				    sc->sc_dev.dv_xname);
   1193 #endif
   1194 			wi_push_packet(sc);
   1195 		} else {
   1196 #ifdef WI_RING_DEBUG
   1197 	printf("%s: queue %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
   1198 	    sc->sc_dev.dv_xname, fid,
   1199 	    sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
   1200 	    sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
   1201 #endif
   1202 		}
   1203 		sc->sc_txqueue = cur = (cur + 1) % WI_NTXBUF;
   1204 		SLIST_REMOVE_HEAD(&sc->sc_rssdfree, rd_next);
   1205 		id->id_node = ni;
   1206 		continue;
   1207 next:
   1208 		if (ni != NULL)
   1209 			ieee80211_free_node(ni);
   1210 	}
   1211 }
   1212 
   1213 
   1214 STATIC int
   1215 wi_reset(struct wi_softc *sc)
   1216 {
   1217 	int i, error;
   1218 
   1219 	DPRINTF(("wi_reset\n"));
   1220 
   1221 	if (sc->sc_reset)
   1222 		(*sc->sc_reset)(sc);
   1223 
   1224 	error = 0;
   1225 	for (i = 0; i < 5; i++) {
   1226 		if (sc->sc_invalid)
   1227 			return ENXIO;
   1228 		DELAY(20*1000);	/* XXX: way too long! */
   1229 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
   1230 			break;
   1231 	}
   1232 	if (error) {
   1233 		printf("%s: init failed\n", sc->sc_dev.dv_xname);
   1234 		return error;
   1235 	}
   1236 	CSR_WRITE_2(sc, WI_INT_EN, 0);
   1237 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
   1238 
   1239 	/* Calibrate timer. */
   1240 	wi_write_val(sc, WI_RID_TICK_TIME, 0);
   1241 	return 0;
   1242 }
   1243 
   1244 STATIC void
   1245 wi_watchdog(struct ifnet *ifp)
   1246 {
   1247 	struct wi_softc *sc = ifp->if_softc;
   1248 
   1249 	ifp->if_timer = 0;
   1250 	if (!sc->sc_enabled)
   1251 		return;
   1252 
   1253 	if (sc->sc_tx_timer) {
   1254 		if (--sc->sc_tx_timer == 0) {
   1255 			printf("%s: device timeout\n", ifp->if_xname);
   1256 			ifp->if_oerrors++;
   1257 			wi_init(ifp);
   1258 			return;
   1259 		}
   1260 		ifp->if_timer = 1;
   1261 	}
   1262 
   1263 	if (sc->sc_scan_timer) {
   1264 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
   1265 		    sc->sc_firmware_type == WI_INTERSIL) {
   1266 			DPRINTF(("wi_watchdog: inquire scan\n"));
   1267 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   1268 		}
   1269 		if (sc->sc_scan_timer)
   1270 			ifp->if_timer = 1;
   1271 	}
   1272 
   1273 	/* TODO: rate control */
   1274 	ieee80211_watchdog(&sc->sc_ic);
   1275 }
   1276 
   1277 STATIC int
   1278 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1279 {
   1280 	struct wi_softc *sc = ifp->if_softc;
   1281 	struct ieee80211com *ic = &sc->sc_ic;
   1282 	struct ifreq *ifr = (struct ifreq *)data;
   1283 	int s, error = 0;
   1284 
   1285 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1286 		return ENXIO;
   1287 
   1288 	s = splnet();
   1289 
   1290 	switch (cmd) {
   1291 	case SIOCSIFFLAGS:
   1292 		/*
   1293 		 * Can't do promisc and hostap at the same time.  If all that's
   1294 		 * changing is the promisc flag, try to short-circuit a call to
   1295 		 * wi_init() by just setting PROMISC in the hardware.
   1296 		 */
   1297 		if (ifp->if_flags & IFF_UP) {
   1298 			if (sc->sc_enabled) {
   1299 				if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   1300 				    (ifp->if_flags & IFF_PROMISC) != 0)
   1301 					wi_write_val(sc, WI_RID_PROMISC, 1);
   1302 				else
   1303 					wi_write_val(sc, WI_RID_PROMISC, 0);
   1304 			} else
   1305 				error = wi_init(ifp);
   1306 		} else if (sc->sc_enabled)
   1307 			wi_stop(ifp, 1);
   1308 		break;
   1309 	case SIOCSIFMEDIA:
   1310 	case SIOCGIFMEDIA:
   1311 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   1312 		break;
   1313 	case SIOCADDMULTI:
   1314 	case SIOCDELMULTI:
   1315 		error = (cmd == SIOCADDMULTI) ?
   1316 		    ether_addmulti(ifr, &sc->sc_ec) :
   1317 		    ether_delmulti(ifr, &sc->sc_ec);
   1318 		if (error == ENETRESET) {
   1319 			if (ifp->if_flags & IFF_RUNNING) {
   1320 				/* do not rescan */
   1321 				error = wi_write_multi(sc);
   1322 			} else
   1323 				error = 0;
   1324 		}
   1325 		break;
   1326 	case SIOCGIFGENERIC:
   1327 		error = wi_get_cfg(ifp, cmd, data);
   1328 		break;
   1329 	case SIOCSIFGENERIC:
   1330 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   1331 		if (error)
   1332 			break;
   1333 		error = wi_set_cfg(ifp, cmd, data);
   1334 		if (error == ENETRESET) {
   1335 			if (ifp->if_flags & IFF_RUNNING)
   1336 				error = wi_init(ifp);
   1337 			else
   1338 				error = 0;
   1339 		}
   1340 		break;
   1341 	case SIOCS80211BSSID:
   1342 		if (sc->sc_firmware_type == WI_LUCENT) {
   1343 			error = ENODEV;
   1344 			break;
   1345 		}
   1346 		/* fall through */
   1347 	default:
   1348 		ic->ic_flags = sc->sc_ic_flags;
   1349 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   1350 		sc->sc_ic_flags = ic->ic_flags;
   1351 		wi_mend_flags(sc);
   1352 		if (error == ENETRESET) {
   1353 			if (sc->sc_enabled)
   1354 				error = wi_init(ifp);
   1355 			else
   1356 				error = 0;
   1357 		}
   1358 		break;
   1359 	}
   1360 	splx(s);
   1361 	return error;
   1362 }
   1363 
   1364 STATIC int
   1365 wi_media_change(struct ifnet *ifp)
   1366 {
   1367 	struct wi_softc *sc = ifp->if_softc;
   1368 	struct ieee80211com *ic = &sc->sc_ic;
   1369 	int error;
   1370 
   1371 	error = ieee80211_media_change(ifp);
   1372 	if (error == ENETRESET) {
   1373 		if (sc->sc_enabled)
   1374 			error = wi_init(ifp);
   1375 		else
   1376 			error = 0;
   1377 	}
   1378 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
   1379 
   1380 	return error;
   1381 }
   1382 
   1383 STATIC void
   1384 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1385 {
   1386 	struct wi_softc *sc = ifp->if_softc;
   1387 	struct ieee80211com *ic = &sc->sc_ic;
   1388 	u_int16_t val;
   1389 	int rate;
   1390 
   1391 	if (sc->sc_enabled == 0) {
   1392 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   1393 		imr->ifm_status = 0;
   1394 		return;
   1395 	}
   1396 
   1397 	imr->ifm_status = IFM_AVALID;
   1398 	imr->ifm_active = IFM_IEEE80211;
   1399 	if (ic->ic_state == IEEE80211_S_RUN &&
   1400 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
   1401 		imr->ifm_status |= IFM_ACTIVE;
   1402 	if (wi_read_xrid(sc, WI_RID_CUR_TX_RATE, &val, sizeof(val)) == 0) {
   1403 		/* convert to 802.11 rate */
   1404 		val = le16toh(val);
   1405 		rate = val * 2;
   1406 		if (sc->sc_firmware_type == WI_LUCENT) {
   1407 			if (rate == 10)
   1408 				rate = 11;	/* 5.5Mbps */
   1409 		} else {
   1410 			if (rate == 4*2)
   1411 				rate = 11;	/* 5.5Mbps */
   1412 			else if (rate == 8*2)
   1413 				rate = 22;	/* 11Mbps */
   1414 		}
   1415 	} else
   1416 		rate = 0;
   1417 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
   1418 	switch (ic->ic_opmode) {
   1419 	case IEEE80211_M_STA:
   1420 		break;
   1421 	case IEEE80211_M_IBSS:
   1422 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
   1423 		break;
   1424 	case IEEE80211_M_AHDEMO:
   1425 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   1426 		break;
   1427 	case IEEE80211_M_HOSTAP:
   1428 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
   1429 		break;
   1430 	case IEEE80211_M_MONITOR:
   1431 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
   1432 		break;
   1433 	}
   1434 }
   1435 
   1436 STATIC struct ieee80211_node *
   1437 wi_node_alloc(struct ieee80211_node_table *nt)
   1438 {
   1439 	struct wi_node *wn =
   1440 	    malloc(sizeof(struct wi_node), M_DEVBUF, M_NOWAIT | M_ZERO);
   1441 	return wn ? &wn->wn_node : NULL;
   1442 }
   1443 
   1444 STATIC void
   1445 wi_node_free(struct ieee80211_node *ni)
   1446 {
   1447 	struct wi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
   1448 	int i;
   1449 
   1450 	for (i = 0; i < WI_NTXRSS; i++) {
   1451 		if (sc->sc_rssd[i].rd_desc.id_node == ni)
   1452 			sc->sc_rssd[i].rd_desc.id_node = NULL;
   1453 	}
   1454 	free(ni, M_DEVBUF);
   1455 }
   1456 
   1457 STATIC void
   1458 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
   1459 {
   1460 	struct ieee80211com *ic = &sc->sc_ic;
   1461 	struct ieee80211_node *ni = ic->ic_bss;
   1462 	struct ifnet *ifp = &sc->sc_if;
   1463 
   1464 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
   1465 		return;
   1466 
   1467 	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
   1468 	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
   1469 
   1470 	/* In promiscuous mode, the BSSID field is not a reliable
   1471 	 * indicator of the firmware's BSSID. Damp spurious
   1472 	 * change-of-BSSID indications.
   1473 	 */
   1474 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   1475 	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
   1476 	                 WI_MAX_FALSE_SYNS))
   1477 		return;
   1478 
   1479 	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
   1480 	/*
   1481 	 * XXX hack; we should create a new node with the new bssid
   1482 	 * and replace the existing ic_bss with it but since we don't
   1483 	 * process management frames to collect state we cheat by
   1484 	 * reusing the existing node as we know wi_newstate will be
   1485 	 * called and it will overwrite the node state.
   1486 	 */
   1487         ieee80211_sta_join(ic, ieee80211_ref_node(ni));
   1488 }
   1489 
   1490 static __inline void
   1491 wi_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
   1492     struct ieee80211_frame *wh, int rssi)
   1493 {
   1494 	struct wi_node *wn;
   1495 
   1496 	if (ni == NULL) {
   1497 		printf("%s: null node", __func__);
   1498 		return;
   1499 	}
   1500 
   1501 	wn = (void*)ni;
   1502 	ieee80211_rssadapt_input(ic, ni, &wn->wn_rssadapt, rssi);
   1503 }
   1504 
   1505 STATIC void
   1506 wi_rx_intr(struct wi_softc *sc)
   1507 {
   1508 	struct ieee80211com *ic = &sc->sc_ic;
   1509 	struct ifnet *ifp = &sc->sc_if;
   1510 	struct ieee80211_node *ni;
   1511 	struct wi_frame frmhdr;
   1512 	struct mbuf *m;
   1513 	struct ieee80211_frame *wh;
   1514 	int fid, len, off, rssi;
   1515 	u_int8_t dir;
   1516 	u_int16_t status;
   1517 	u_int32_t rstamp;
   1518 
   1519 	fid = CSR_READ_2(sc, WI_RX_FID);
   1520 
   1521 	/* First read in the frame header */
   1522 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
   1523 		printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
   1524 		    __func__, fid);
   1525 		ifp->if_ierrors++;
   1526 		return;
   1527 	}
   1528 
   1529 	if (IFF_DUMPPKTS(ifp))
   1530 		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
   1531 
   1532 	/*
   1533 	 * Drop undecryptable or packets with receive errors here
   1534 	 */
   1535 	status = le16toh(frmhdr.wi_status);
   1536 	if ((status & WI_STAT_ERRSTAT) != 0 &&
   1537 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
   1538 		ifp->if_ierrors++;
   1539 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
   1540 		return;
   1541 	}
   1542 	rssi = frmhdr.wi_rx_signal;
   1543 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
   1544 	    le16toh(frmhdr.wi_rx_tstamp1);
   1545 
   1546 	len = le16toh(frmhdr.wi_dat_len);
   1547 	off = ALIGN(sizeof(struct ieee80211_frame));
   1548 
   1549 	/* Sometimes the PRISM2.x returns bogusly large frames. Except
   1550 	 * in monitor mode, just throw them away.
   1551 	 */
   1552 	if (off + len > MCLBYTES) {
   1553 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1554 			ifp->if_ierrors++;
   1555 			DPRINTF(("wi_rx_intr: oversized packet\n"));
   1556 			return;
   1557 		} else
   1558 			len = 0;
   1559 	}
   1560 
   1561 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1562 	if (m == NULL) {
   1563 		ifp->if_ierrors++;
   1564 		DPRINTF(("wi_rx_intr: MGET failed\n"));
   1565 		return;
   1566 	}
   1567 	if (off + len > MHLEN) {
   1568 		MCLGET(m, M_DONTWAIT);
   1569 		if ((m->m_flags & M_EXT) == 0) {
   1570 			m_freem(m);
   1571 			ifp->if_ierrors++;
   1572 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
   1573 			return;
   1574 		}
   1575 	}
   1576 
   1577 	m->m_data += off - sizeof(struct ieee80211_frame);
   1578 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
   1579 	wi_read_bap(sc, fid, sizeof(frmhdr),
   1580 	    m->m_data + sizeof(struct ieee80211_frame), len);
   1581 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
   1582 	m->m_pkthdr.rcvif = ifp;
   1583 
   1584 #if NBPFILTER > 0
   1585 	if (sc->sc_drvbpf) {
   1586 		struct wi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1587 
   1588 		tap->wr_rate = frmhdr.wi_rx_rate / 5;
   1589 		tap->wr_antsignal = frmhdr.wi_rx_signal;
   1590 		tap->wr_antnoise = frmhdr.wi_rx_silence;
   1591 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
   1592 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
   1593 		if (frmhdr.wi_status & WI_STAT_PCF)
   1594 			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
   1595 
   1596 		bpf_mtap2(sc->sc_drvbpf, tap, tap->wr_ihdr.it_len, m);
   1597 	}
   1598 #endif
   1599 	wh = mtod(m, struct ieee80211_frame *);
   1600 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1601 		/*
   1602 		 * WEP is decrypted by hardware. Clear WEP bit
   1603 		 * header for ieee80211_input().
   1604 		 */
   1605 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1606 	}
   1607 
   1608 	/* synchronize driver's BSSID with firmware's BSSID */
   1609 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
   1610 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
   1611 		wi_sync_bssid(sc, wh->i_addr3);
   1612 
   1613 	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
   1614 
   1615 	ieee80211_input(ic, m, ni, rssi, rstamp);
   1616 
   1617 	wi_rssadapt_input(ic, ni, wh, rssi);
   1618 
   1619 	/*
   1620 	 * The frame may have caused the node to be marked for
   1621 	 * reclamation (e.g. in response to a DEAUTH message)
   1622 	 * so use release_node here instead of unref_node.
   1623 	 */
   1624 	ieee80211_free_node(ni);
   1625 }
   1626 
   1627 STATIC void
   1628 wi_tx_ex_intr(struct wi_softc *sc)
   1629 {
   1630 	struct ieee80211com *ic = &sc->sc_ic;
   1631 	struct ifnet *ifp = &sc->sc_if;
   1632 	struct ieee80211_node *ni;
   1633 	struct ieee80211_rssdesc *id;
   1634 	struct wi_rssdesc *rssd;
   1635 	struct wi_frame frmhdr;
   1636 	int fid;
   1637 	u_int16_t status;
   1638 
   1639 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
   1640 	/* Read in the frame header */
   1641 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
   1642 		printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
   1643 		    __func__, fid);
   1644 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1645 		    &sc->sc_txpending);
   1646 		goto out;
   1647 	}
   1648 
   1649 	if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
   1650 		printf("%s: %s bad idx %02x\n",
   1651 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1652 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1653 		    &sc->sc_txpending);
   1654 		goto out;
   1655 	}
   1656 
   1657 	status = le16toh(frmhdr.wi_status);
   1658 
   1659 	/*
   1660 	 * Spontaneous station disconnects appear as xmit
   1661 	 * errors.  Don't announce them and/or count them
   1662 	 * as an output error.
   1663 	 */
   1664 	if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
   1665 		printf("%s: tx failed", sc->sc_dev.dv_xname);
   1666 		if (status & WI_TXSTAT_RET_ERR)
   1667 			printf(", retry limit exceeded");
   1668 		if (status & WI_TXSTAT_AGED_ERR)
   1669 			printf(", max transmit lifetime exceeded");
   1670 		if (status & WI_TXSTAT_DISCONNECT)
   1671 			printf(", port disconnected");
   1672 		if (status & WI_TXSTAT_FORM_ERR)
   1673 			printf(", invalid format (data len %u src %s)",
   1674 				le16toh(frmhdr.wi_dat_len),
   1675 				ether_sprintf(frmhdr.wi_ehdr.ether_shost));
   1676 		if (status & ~0xf)
   1677 			printf(", status=0x%x", status);
   1678 		printf("\n");
   1679 	}
   1680 	ifp->if_oerrors++;
   1681 	rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
   1682 	id = &rssd->rd_desc;
   1683 	if ((status & WI_TXSTAT_RET_ERR) != 0)
   1684 		wi_lower_rate(ic, id);
   1685 
   1686 	ni = id->id_node;
   1687 	id->id_node = NULL;
   1688 
   1689 	if (ni == NULL) {
   1690 		printf("%s: %s null node, rssdesc %02x\n",
   1691 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1692 		goto out;
   1693 	}
   1694 
   1695 	if (sc->sc_txpending[id->id_rateidx]-- == 0) {
   1696 	        printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
   1697 		    __func__, id->id_rateidx);
   1698 		sc->sc_txpending[id->id_rateidx] = 0;
   1699 	}
   1700 	if (ni != NULL)
   1701 		ieee80211_free_node(ni);
   1702 	SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
   1703 out:
   1704 	ifp->if_flags &= ~IFF_OACTIVE;
   1705 }
   1706 
   1707 STATIC void
   1708 wi_txalloc_intr(struct wi_softc *sc)
   1709 {
   1710 	int fid, cur;
   1711 
   1712 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
   1713 
   1714 	cur = sc->sc_txalloc;
   1715 #ifdef DIAGNOSTIC
   1716 	if (sc->sc_txstarted == 0) {
   1717 		printf("%s: spurious alloc %x != %x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
   1718 		    sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
   1719 		    sc->sc_txqueue, sc->sc_txstart, sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
   1720 		return;
   1721 	}
   1722 #endif
   1723 	--sc->sc_txstarted;
   1724 	++sc->sc_txalloced;
   1725 	sc->sc_txd[cur].d_fid = fid;
   1726 	sc->sc_txalloc = (cur + 1) % WI_NTXBUF;
   1727 #ifdef WI_RING_DEBUG
   1728 	printf("%s: alloc %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
   1729 	    sc->sc_dev.dv_xname, fid,
   1730 	    sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
   1731 	    sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
   1732 #endif
   1733 }
   1734 
   1735 STATIC void
   1736 wi_cmd_intr(struct wi_softc *sc)
   1737 {
   1738 	struct ifnet *ifp = &sc->sc_if;
   1739 
   1740 	if (sc->sc_invalid)
   1741 		return;
   1742 #ifdef WI_DEBUG
   1743 	if (wi_debug)
   1744 		printf("%s: %d txcmds outstanding\n", __func__, sc->sc_txcmds);
   1745 #endif
   1746 	KASSERT(sc->sc_txcmds > 0);
   1747 
   1748 	--sc->sc_txcmds;
   1749 
   1750 	if (--sc->sc_txqueued == 0) {
   1751 		sc->sc_tx_timer = 0;
   1752 		ifp->if_flags &= ~IFF_OACTIVE;
   1753 #ifdef WI_RING_DEBUG
   1754 	printf("%s: cmd       , alloc %d queue %d start %d alloced %d queued %d started %d\n",
   1755 	    sc->sc_dev.dv_xname,
   1756 	    sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
   1757 	    sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
   1758 #endif
   1759 	} else
   1760 		wi_push_packet(sc);
   1761 }
   1762 
   1763 STATIC void
   1764 wi_push_packet(struct wi_softc *sc)
   1765 {
   1766 	struct ifnet *ifp = &sc->sc_if;
   1767 	int cur, fid;
   1768 
   1769 	cur = sc->sc_txstart;
   1770 	fid = sc->sc_txd[cur].d_fid;
   1771 
   1772 	KASSERT(sc->sc_txcmds == 0);
   1773 
   1774 	if (wi_cmd_start(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
   1775 		printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
   1776 		/* XXX ring might have a hole */
   1777 	}
   1778 
   1779 	if (sc->sc_txcmds++ > 0)
   1780 		printf("%s: %d tx cmds pending!!!\n", __func__, sc->sc_txcmds);
   1781 
   1782 	++sc->sc_txstarted;
   1783 #ifdef DIAGNOSTIC
   1784 	if (sc->sc_txstarted > WI_NTXBUF)
   1785 		printf("%s: too many buffers started\n", sc->sc_dev.dv_xname);
   1786 #endif
   1787 	sc->sc_txstart = (cur + 1) % WI_NTXBUF;
   1788 	sc->sc_tx_timer = 5;
   1789 	ifp->if_timer = 1;
   1790 #ifdef WI_RING_DEBUG
   1791 	printf("%s: push  %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
   1792 	    sc->sc_dev.dv_xname, fid,
   1793 	    sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
   1794 	    sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
   1795 #endif
   1796 }
   1797 
   1798 STATIC void
   1799 wi_tx_intr(struct wi_softc *sc)
   1800 {
   1801 	struct ieee80211com *ic = &sc->sc_ic;
   1802 	struct ifnet *ifp = &sc->sc_if;
   1803 	struct ieee80211_node *ni;
   1804 	struct ieee80211_rssdesc *id;
   1805 	struct wi_rssdesc *rssd;
   1806 	struct wi_frame frmhdr;
   1807 	int fid;
   1808 
   1809 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
   1810 	/* Read in the frame header */
   1811 	if (wi_read_bap(sc, fid, offsetof(struct wi_frame, wi_tx_swsup2),
   1812 	                &frmhdr.wi_tx_swsup2, 2) != 0) {
   1813 		printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
   1814 		    __func__, fid);
   1815 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1816 		    &sc->sc_txpending);
   1817 		goto out;
   1818 	}
   1819 
   1820 	if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
   1821 		printf("%s: %s bad idx %02x\n",
   1822 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1823 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1824 		    &sc->sc_txpending);
   1825 		goto out;
   1826 	}
   1827 
   1828 	rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
   1829 	id = &rssd->rd_desc;
   1830 	wi_raise_rate(ic, id);
   1831 
   1832 	ni = id->id_node;
   1833 	id->id_node = NULL;
   1834 
   1835 	if (ni == NULL) {
   1836 		printf("%s: %s null node, rssdesc %02x\n",
   1837 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1838 		goto out;
   1839 	}
   1840 
   1841 	if (sc->sc_txpending[id->id_rateidx]-- == 0) {
   1842 	        printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
   1843 		    __func__, id->id_rateidx);
   1844 		sc->sc_txpending[id->id_rateidx] = 0;
   1845 	}
   1846 	if (ni != NULL)
   1847 		ieee80211_free_node(ni);
   1848 	SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
   1849 out:
   1850 	ifp->if_flags &= ~IFF_OACTIVE;
   1851 }
   1852 
   1853 STATIC void
   1854 wi_info_intr(struct wi_softc *sc)
   1855 {
   1856 	struct ieee80211com *ic = &sc->sc_ic;
   1857 	struct ifnet *ifp = &sc->sc_if;
   1858 	int i, fid, len, off;
   1859 	u_int16_t ltbuf[2];
   1860 	u_int16_t stat;
   1861 	u_int32_t *ptr;
   1862 
   1863 	fid = CSR_READ_2(sc, WI_INFO_FID);
   1864 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
   1865 
   1866 	switch (le16toh(ltbuf[1])) {
   1867 
   1868 	case WI_INFO_LINK_STAT:
   1869 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
   1870 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
   1871 		switch (le16toh(stat)) {
   1872 		case CONNECTED:
   1873 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1874 			if (ic->ic_state == IEEE80211_S_RUN &&
   1875 			    ic->ic_opmode != IEEE80211_M_IBSS)
   1876 				break;
   1877 			/* FALLTHROUGH */
   1878 		case AP_CHANGE:
   1879 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1880 			break;
   1881 		case AP_IN_RANGE:
   1882 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1883 			break;
   1884 		case AP_OUT_OF_RANGE:
   1885 			if (sc->sc_firmware_type == WI_SYMBOL &&
   1886 			    sc->sc_scan_timer > 0) {
   1887 				if (wi_cmd(sc, WI_CMD_INQUIRE,
   1888 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
   1889 					sc->sc_scan_timer = 0;
   1890 				break;
   1891 			}
   1892 			if (ic->ic_opmode == IEEE80211_M_STA)
   1893 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
   1894 			break;
   1895 		case DISCONNECTED:
   1896 		case ASSOC_FAILED:
   1897 			if (ic->ic_opmode == IEEE80211_M_STA)
   1898 				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1899 			break;
   1900 		}
   1901 		break;
   1902 
   1903 	case WI_INFO_COUNTERS:
   1904 		/* some card versions have a larger stats structure */
   1905 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
   1906 		ptr = (u_int32_t *)&sc->sc_stats;
   1907 		off = sizeof(ltbuf);
   1908 		for (i = 0; i < len; i++, off += 2, ptr++) {
   1909 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
   1910 			stat = le16toh(stat);
   1911 #ifdef WI_HERMES_STATS_WAR
   1912 			if (stat & 0xf000)
   1913 				stat = ~stat;
   1914 #endif
   1915 			*ptr += stat;
   1916 		}
   1917 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
   1918 		    sc->sc_stats.wi_tx_multi_retries +
   1919 		    sc->sc_stats.wi_tx_retry_limit;
   1920 		break;
   1921 
   1922 	case WI_INFO_SCAN_RESULTS:
   1923 	case WI_INFO_HOST_SCAN_RESULTS:
   1924 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
   1925 		break;
   1926 
   1927 	default:
   1928 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
   1929 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
   1930 		break;
   1931 	}
   1932 }
   1933 
   1934 STATIC int
   1935 wi_write_multi(struct wi_softc *sc)
   1936 {
   1937 	struct ifnet *ifp = &sc->sc_if;
   1938 	int n;
   1939 	struct wi_mcast mlist;
   1940 	struct ether_multi *enm;
   1941 	struct ether_multistep estep;
   1942 
   1943 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1944 allmulti:
   1945 		ifp->if_flags |= IFF_ALLMULTI;
   1946 		memset(&mlist, 0, sizeof(mlist));
   1947 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1948 		    sizeof(mlist));
   1949 	}
   1950 
   1951 	n = 0;
   1952 	ETHER_FIRST_MULTI(estep, &sc->sc_ec, enm);
   1953 	while (enm != NULL) {
   1954 		/* Punt on ranges or too many multicast addresses. */
   1955 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
   1956 		    n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
   1957 			goto allmulti;
   1958 
   1959 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
   1960 		n++;
   1961 		ETHER_NEXT_MULTI(estep, enm);
   1962 	}
   1963 	ifp->if_flags &= ~IFF_ALLMULTI;
   1964 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1965 	    IEEE80211_ADDR_LEN * n);
   1966 }
   1967 
   1968 
   1969 STATIC void
   1970 wi_read_nicid(struct wi_softc *sc)
   1971 {
   1972 	struct wi_card_ident *id;
   1973 	char *p;
   1974 	int len;
   1975 	u_int16_t ver[4];
   1976 
   1977 	/* getting chip identity */
   1978 	memset(ver, 0, sizeof(ver));
   1979 	len = sizeof(ver);
   1980 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
   1981 	printf("%s: using ", sc->sc_dev.dv_xname);
   1982 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1983 
   1984 	sc->sc_firmware_type = WI_NOTYPE;
   1985 	for (id = wi_card_ident; id->card_name != NULL; id++) {
   1986 		if (le16toh(ver[0]) == id->card_id) {
   1987 			printf("%s", id->card_name);
   1988 			sc->sc_firmware_type = id->firm_type;
   1989 			break;
   1990 		}
   1991 	}
   1992 	if (sc->sc_firmware_type == WI_NOTYPE) {
   1993 		if (le16toh(ver[0]) & 0x8000) {
   1994 			printf("Unknown PRISM2 chip");
   1995 			sc->sc_firmware_type = WI_INTERSIL;
   1996 		} else {
   1997 			printf("Unknown Lucent chip");
   1998 			sc->sc_firmware_type = WI_LUCENT;
   1999 		}
   2000 	}
   2001 
   2002 	/* get primary firmware version (Only Prism chips) */
   2003 	if (sc->sc_firmware_type != WI_LUCENT) {
   2004 		memset(ver, 0, sizeof(ver));
   2005 		len = sizeof(ver);
   2006 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
   2007 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
   2008 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   2009 	}
   2010 
   2011 	/* get station firmware version */
   2012 	memset(ver, 0, sizeof(ver));
   2013 	len = sizeof(ver);
   2014 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
   2015 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
   2016 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   2017 	if (sc->sc_firmware_type == WI_INTERSIL &&
   2018 	    (sc->sc_sta_firmware_ver == 10102 ||
   2019 	     sc->sc_sta_firmware_ver == 20102)) {
   2020 		char ident[12];
   2021 		memset(ident, 0, sizeof(ident));
   2022 		len = sizeof(ident);
   2023 		/* value should be the format like "V2.00-11" */
   2024 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
   2025 		    *(p = (char *)ident) >= 'A' &&
   2026 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
   2027 			sc->sc_firmware_type = WI_SYMBOL;
   2028 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
   2029 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
   2030 			    (p[6] - '0') * 10 + (p[7] - '0');
   2031 		}
   2032 	}
   2033 
   2034 	printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
   2035 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
   2036 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
   2037 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
   2038 		printf("Primary (%u.%u.%u), ",
   2039 		    sc->sc_pri_firmware_ver / 10000,
   2040 		    (sc->sc_pri_firmware_ver % 10000) / 100,
   2041 		    sc->sc_pri_firmware_ver % 100);
   2042 	printf("Station (%u.%u.%u)\n",
   2043 	    sc->sc_sta_firmware_ver / 10000,
   2044 	    (sc->sc_sta_firmware_ver % 10000) / 100,
   2045 	    sc->sc_sta_firmware_ver % 100);
   2046 }
   2047 
   2048 STATIC int
   2049 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
   2050 {
   2051 	struct wi_ssid ssid;
   2052 
   2053 	if (buflen > IEEE80211_NWID_LEN)
   2054 		return ENOBUFS;
   2055 	memset(&ssid, 0, sizeof(ssid));
   2056 	ssid.wi_len = htole16(buflen);
   2057 	memcpy(ssid.wi_ssid, buf, buflen);
   2058 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
   2059 }
   2060 
   2061 STATIC int
   2062 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   2063 {
   2064 	struct wi_softc *sc = ifp->if_softc;
   2065 	struct ieee80211com *ic = &sc->sc_ic;
   2066 	struct ifreq *ifr = (struct ifreq *)data;
   2067 	struct wi_req wreq;
   2068 	int len, n, error;
   2069 
   2070 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   2071 	if (error)
   2072 		return error;
   2073 	len = (wreq.wi_len - 1) * 2;
   2074 	if (len < sizeof(u_int16_t))
   2075 		return ENOSPC;
   2076 	if (len > sizeof(wreq.wi_val))
   2077 		len = sizeof(wreq.wi_val);
   2078 
   2079 	switch (wreq.wi_type) {
   2080 
   2081 	case WI_RID_IFACE_STATS:
   2082 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
   2083 		if (len < sizeof(sc->sc_stats))
   2084 			error = ENOSPC;
   2085 		else
   2086 			len = sizeof(sc->sc_stats);
   2087 		break;
   2088 
   2089 	case WI_RID_ENCRYPTION:
   2090 	case WI_RID_TX_CRYPT_KEY:
   2091 	case WI_RID_DEFLT_CRYPT_KEYS:
   2092 	case WI_RID_TX_RATE:
   2093 		return ieee80211_cfgget(ic, cmd, data);
   2094 
   2095 	case WI_RID_MICROWAVE_OVEN:
   2096 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
   2097 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2098 			    &len);
   2099 			break;
   2100 		}
   2101 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
   2102 		len = sizeof(u_int16_t);
   2103 		break;
   2104 
   2105 	case WI_RID_DBM_ADJUST:
   2106 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
   2107 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2108 			    &len);
   2109 			break;
   2110 		}
   2111 		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
   2112 		len = sizeof(u_int16_t);
   2113 		break;
   2114 
   2115 	case WI_RID_ROAMING_MODE:
   2116 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
   2117 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2118 			    &len);
   2119 			break;
   2120 		}
   2121 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
   2122 		len = sizeof(u_int16_t);
   2123 		break;
   2124 
   2125 	case WI_RID_SYSTEM_SCALE:
   2126 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
   2127 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2128 			    &len);
   2129 			break;
   2130 		}
   2131 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
   2132 		len = sizeof(u_int16_t);
   2133 		break;
   2134 
   2135 	case WI_RID_FRAG_THRESH:
   2136 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
   2137 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2138 			    &len);
   2139 			break;
   2140 		}
   2141 		wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   2142 		len = sizeof(u_int16_t);
   2143 		break;
   2144 
   2145 	case WI_RID_READ_APS:
   2146 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2147 			return ieee80211_cfgget(ic, cmd, data);
   2148 		if (sc->sc_scan_timer > 0) {
   2149 			error = EINPROGRESS;
   2150 			break;
   2151 		}
   2152 		n = sc->sc_naps;
   2153 		if (len < sizeof(n)) {
   2154 			error = ENOSPC;
   2155 			break;
   2156 		}
   2157 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
   2158 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
   2159 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
   2160 		memcpy(wreq.wi_val, &n, sizeof(n));
   2161 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
   2162 		    sizeof(struct wi_apinfo) * n);
   2163 		break;
   2164 
   2165 	default:
   2166 		if (sc->sc_enabled) {
   2167 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2168 			    &len);
   2169 			break;
   2170 		}
   2171 		switch (wreq.wi_type) {
   2172 		case WI_RID_MAX_DATALEN:
   2173 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
   2174 			len = sizeof(u_int16_t);
   2175 			break;
   2176 		case WI_RID_FRAG_THRESH:
   2177 			wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   2178 			len = sizeof(u_int16_t);
   2179 			break;
   2180 		case WI_RID_RTS_THRESH:
   2181 			wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
   2182 			len = sizeof(u_int16_t);
   2183 			break;
   2184 		case WI_RID_CNFAUTHMODE:
   2185 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
   2186 			len = sizeof(u_int16_t);
   2187 			break;
   2188 		case WI_RID_NODENAME:
   2189 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
   2190 				error = ENOSPC;
   2191 				break;
   2192 			}
   2193 			len = sc->sc_nodelen + sizeof(u_int16_t);
   2194 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
   2195 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
   2196 			    sc->sc_nodelen);
   2197 			break;
   2198 		default:
   2199 			return ieee80211_cfgget(ic, cmd, data);
   2200 		}
   2201 		break;
   2202 	}
   2203 	if (error)
   2204 		return error;
   2205 	wreq.wi_len = (len + 1) / 2 + 1;
   2206 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
   2207 }
   2208 
   2209 STATIC int
   2210 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   2211 {
   2212 	struct wi_softc *sc = ifp->if_softc;
   2213 	struct ieee80211com *ic = &sc->sc_ic;
   2214 	struct ifreq *ifr = (struct ifreq *)data;
   2215 	struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   2216 	struct wi_req wreq;
   2217 	struct mbuf *m;
   2218 	int i, len, error;
   2219 
   2220 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   2221 	if (error)
   2222 		return error;
   2223 	len = (wreq.wi_len - 1) * 2;
   2224 	switch (wreq.wi_type) {
   2225 	case WI_RID_DBM_ADJUST:
   2226 		return ENODEV;
   2227 
   2228 	case WI_RID_NODENAME:
   2229 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
   2230 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
   2231 			error = ENOSPC;
   2232 			break;
   2233 		}
   2234 		if (sc->sc_enabled) {
   2235 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2236 			    len);
   2237 			if (error)
   2238 				break;
   2239 		}
   2240 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
   2241 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
   2242 		break;
   2243 
   2244 	case WI_RID_MICROWAVE_OVEN:
   2245 	case WI_RID_ROAMING_MODE:
   2246 	case WI_RID_SYSTEM_SCALE:
   2247 	case WI_RID_FRAG_THRESH:
   2248 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
   2249 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
   2250 			break;
   2251 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
   2252 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
   2253 			break;
   2254 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
   2255 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
   2256 			break;
   2257 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
   2258 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
   2259 			break;
   2260 		/* FALLTHROUGH */
   2261 	case WI_RID_RTS_THRESH:
   2262 	case WI_RID_CNFAUTHMODE:
   2263 	case WI_RID_MAX_DATALEN:
   2264 		if (sc->sc_enabled) {
   2265 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2266 			    sizeof(u_int16_t));
   2267 			if (error)
   2268 				break;
   2269 		}
   2270 		switch (wreq.wi_type) {
   2271 		case WI_RID_FRAG_THRESH:
   2272 			sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
   2273 			break;
   2274 		case WI_RID_RTS_THRESH:
   2275 			sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
   2276 			break;
   2277 		case WI_RID_MICROWAVE_OVEN:
   2278 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
   2279 			break;
   2280 		case WI_RID_ROAMING_MODE:
   2281 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
   2282 			break;
   2283 		case WI_RID_SYSTEM_SCALE:
   2284 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
   2285 			break;
   2286 		case WI_RID_CNFAUTHMODE:
   2287 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
   2288 			break;
   2289 		case WI_RID_MAX_DATALEN:
   2290 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
   2291 			break;
   2292 		}
   2293 		break;
   2294 
   2295 	case WI_RID_TX_RATE:
   2296 		switch (le16toh(wreq.wi_val[0])) {
   2297 		case 3:
   2298 			ic->ic_fixed_rate = -1;
   2299 			break;
   2300 		default:
   2301 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   2302 				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
   2303 				    / 2 == le16toh(wreq.wi_val[0]))
   2304 					break;
   2305 			}
   2306 			if (i == IEEE80211_RATE_SIZE)
   2307 				return EINVAL;
   2308 			ic->ic_fixed_rate = i;
   2309 		}
   2310 		if (sc->sc_enabled)
   2311 			error = wi_cfg_txrate(sc);
   2312 		break;
   2313 
   2314 	case WI_RID_SCAN_APS:
   2315 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
   2316 			error = wi_scan_ap(sc, 0x3fff, 0x000f);
   2317 		break;
   2318 
   2319 	case WI_RID_MGMT_XMIT:
   2320 		if (!sc->sc_enabled) {
   2321 			error = ENETDOWN;
   2322 			break;
   2323 		}
   2324 		if (ic->ic_mgtq.ifq_len > 5) {
   2325 			error = EAGAIN;
   2326 			break;
   2327 		}
   2328 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
   2329 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
   2330 		if (m == NULL) {
   2331 			error = ENOMEM;
   2332 			break;
   2333 		}
   2334 		IF_ENQUEUE(&ic->ic_mgtq, m);
   2335 		break;
   2336 
   2337 	default:
   2338 		if (sc->sc_enabled) {
   2339 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2340 			    len);
   2341 			if (error)
   2342 				break;
   2343 		}
   2344 		error = ieee80211_cfgset(ic, cmd, data);
   2345 		break;
   2346 	}
   2347 	return error;
   2348 }
   2349 
   2350 /* Rate is 0 for hardware auto-select, otherwise rate is
   2351  * 2, 4, 11, or 22 (units of 500Kbps).
   2352  */
   2353 STATIC int
   2354 wi_write_txrate(struct wi_softc *sc, int rate)
   2355 {
   2356 	u_int16_t hwrate;
   2357 
   2358 	/* rate: 0, 2, 4, 11, 22 */
   2359 	switch (sc->sc_firmware_type) {
   2360 	case WI_LUCENT:
   2361 		switch (rate & IEEE80211_RATE_VAL) {
   2362 		case 2:
   2363 			hwrate = 1;
   2364 			break;
   2365 		case 4:
   2366 			hwrate = 2;
   2367 			break;
   2368 		default:
   2369 			hwrate = 3;	/* auto */
   2370 			break;
   2371 		case 11:
   2372 			hwrate = 4;
   2373 			break;
   2374 		case 22:
   2375 			hwrate = 5;
   2376 			break;
   2377 		}
   2378 		break;
   2379 	default:
   2380 		switch (rate & IEEE80211_RATE_VAL) {
   2381 		case 2:
   2382 			hwrate = 1;
   2383 			break;
   2384 		case 4:
   2385 			hwrate = 2;
   2386 			break;
   2387 		case 11:
   2388 			hwrate = 4;
   2389 			break;
   2390 		case 22:
   2391 			hwrate = 8;
   2392 			break;
   2393 		default:
   2394 			hwrate = 15;	/* auto */
   2395 			break;
   2396 		}
   2397 		break;
   2398 	}
   2399 
   2400 	if (sc->sc_tx_rate == hwrate)
   2401 		return 0;
   2402 
   2403 	if (sc->sc_if.if_flags & IFF_DEBUG)
   2404 		printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate,
   2405 		    hwrate, rate);
   2406 
   2407 	sc->sc_tx_rate = hwrate;
   2408 
   2409 	return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate);
   2410 }
   2411 
   2412 STATIC int
   2413 wi_cfg_txrate(struct wi_softc *sc)
   2414 {
   2415 	struct ieee80211com *ic = &sc->sc_ic;
   2416 	struct ieee80211_rateset *rs;
   2417 	int rate;
   2418 
   2419 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   2420 
   2421 	sc->sc_tx_rate = 0; /* force write to RID */
   2422 
   2423 	if (ic->ic_fixed_rate < 0)
   2424 		rate = 0;	/* auto */
   2425 	else
   2426 		rate = rs->rs_rates[ic->ic_fixed_rate];
   2427 
   2428 	return wi_write_txrate(sc, rate);
   2429 }
   2430 
   2431 STATIC int
   2432 wi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
   2433 {
   2434 	int keyix;
   2435 
   2436 	if (&ic->ic_nw_keys[0] <= k && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])
   2437 		keyix = k - ic->ic_nw_keys;
   2438 	else
   2439 		keyix = IEEE80211_KEYIX_NONE;
   2440 
   2441 	DPRINTF(("%s: alloc key %u\n", __func__, keyix));
   2442 
   2443 	return keyix;
   2444 }
   2445 
   2446 STATIC int
   2447 wi_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
   2448 {
   2449 	struct wi_softc *sc = ic->ic_ifp->if_softc;
   2450 	u_int keyix = k->wk_keyix;
   2451 
   2452 	DPRINTF(("%s: delete key %u\n", __func__, keyix));
   2453 
   2454 	if (keyix >= IEEE80211_WEP_NKID)
   2455 		return 0;
   2456 	if (k->wk_keylen != 0)
   2457 		sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
   2458 
   2459 	return 1;
   2460 }
   2461 
   2462 static int
   2463 wi_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
   2464 	const u_int8_t mac[IEEE80211_ADDR_LEN])
   2465 {
   2466 	struct wi_softc *sc = ic->ic_ifp->if_softc;
   2467 
   2468 	DPRINTF(("%s: set key %u\n", __func__, k->wk_keyix));
   2469 
   2470 	if (k->wk_keyix >= IEEE80211_WEP_NKID)
   2471 		return 0;
   2472 
   2473 	sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
   2474 
   2475 	return 1;
   2476 }
   2477 
   2478 STATIC void
   2479 wi_key_update_begin(struct ieee80211com *ic)
   2480 {
   2481 	DPRINTF(("%s:\n", __func__));
   2482 }
   2483 
   2484 STATIC void
   2485 wi_key_update_end(struct ieee80211com *ic)
   2486 {
   2487 	struct ifnet *ifp = ic->ic_ifp;
   2488 	struct wi_softc *sc = ifp->if_softc;
   2489 
   2490 	DPRINTF(("%s:\n", __func__));
   2491 
   2492 	if ((sc->sc_flags & WI_FLAGS_WEP_VALID) != 0)
   2493 		return;
   2494 	if (ic->ic_caps & IEEE80211_C_WEP)
   2495 		(void)wi_write_wep(sc);
   2496 }
   2497 
   2498 STATIC int
   2499 wi_write_wep(struct wi_softc *sc)
   2500 {
   2501 	struct ifnet *ifp = &sc->sc_if;
   2502 	struct ieee80211com *ic = &sc->sc_ic;
   2503 	int error = 0;
   2504 	int i, keylen;
   2505 	u_int16_t val;
   2506 	struct wi_key wkey[IEEE80211_WEP_NKID];
   2507 
   2508 	if ((ifp->if_flags & IFF_RUNNING) != 0)
   2509 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
   2510 
   2511 	switch (sc->sc_firmware_type) {
   2512 	case WI_LUCENT:
   2513 		val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
   2514 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
   2515 		if (error)
   2516 			break;
   2517 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
   2518 		if (error)
   2519 			break;
   2520 		memset(wkey, 0, sizeof(wkey));
   2521 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2522 			keylen = ic->ic_nw_keys[i].wk_keylen;
   2523 			wkey[i].wi_keylen = htole16(keylen);
   2524 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
   2525 			    keylen);
   2526 		}
   2527 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
   2528 		    wkey, sizeof(wkey));
   2529 		break;
   2530 
   2531 	case WI_INTERSIL:
   2532 	case WI_SYMBOL:
   2533 		if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   2534 			/*
   2535 			 * ONLY HWB3163 EVAL-CARD Firmware version
   2536 			 * less than 0.8 variant2
   2537 			 *
   2538 			 *   If promiscuous mode disable, Prism2 chip
   2539 			 *  does not work with WEP .
   2540 			 * It is under investigation for details.
   2541 			 * (ichiro (at) NetBSD.org)
   2542 			 */
   2543 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2544 			    sc->sc_sta_firmware_ver < 802 ) {
   2545 				/* firm ver < 0.8 variant 2 */
   2546 				wi_write_val(sc, WI_RID_PROMISC, 1);
   2547 			}
   2548 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2549 			    sc->sc_cnfauthmode);
   2550 			val = PRIVACY_INVOKED;
   2551 			if ((sc->sc_ic_flags & IEEE80211_F_DROPUNENC) != 0)
   2552 				val |= EXCLUDE_UNENCRYPTED;
   2553 			/*
   2554 			 * Encryption firmware has a bug for HostAP mode.
   2555 			 */
   2556 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2557 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
   2558 				val |= HOST_ENCRYPT;
   2559 		} else {
   2560 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2561 			    IEEE80211_AUTH_OPEN);
   2562 			val = HOST_ENCRYPT | HOST_DECRYPT;
   2563 		}
   2564 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
   2565 		if (error)
   2566 			break;
   2567 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
   2568 		    ic->ic_def_txkey);
   2569 		if (error)
   2570 			break;
   2571 		/*
   2572 		 * It seems that the firmware accept 104bit key only if
   2573 		 * all the keys have 104bit length.  We get the length of
   2574 		 * the transmit key and use it for all other keys.
   2575 		 * Perhaps we should use software WEP for such situation.
   2576 		 */
   2577 		keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
   2578 		if (keylen > IEEE80211_WEP_KEYLEN)
   2579 			keylen = 13;	/* 104bit keys */
   2580 		else
   2581 			keylen = IEEE80211_WEP_KEYLEN;
   2582 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2583 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
   2584 			    ic->ic_nw_keys[i].wk_key, keylen);
   2585 			if (error)
   2586 				break;
   2587 		}
   2588 		break;
   2589 	}
   2590 	if ((ifp->if_flags & IFF_RUNNING) != 0)
   2591 		wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
   2592 	if (error == 0)
   2593 		sc->sc_flags |= WI_FLAGS_WEP_VALID;
   2594 	return error;
   2595 }
   2596 
   2597 /* Must be called at proper protection level! */
   2598 STATIC int
   2599 wi_cmd_start(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   2600 {
   2601 #ifdef WI_HISTOGRAM
   2602 	static int hist1[11];
   2603 	static int hist1count;
   2604 #endif
   2605 	int i;
   2606 
   2607 	/* wait for the busy bit to clear */
   2608 	for (i = 500; i > 0; i--) {	/* 5s */
   2609 		if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
   2610 			break;
   2611 		if (sc->sc_invalid)
   2612 			return ENXIO;
   2613 		DELAY(1000);	/* 1 m sec */
   2614 	}
   2615 	if (i == 0) {
   2616 		printf("%s: wi_cmd: busy bit won't clear.\n",
   2617 		    sc->sc_dev.dv_xname);
   2618 		return(ETIMEDOUT);
   2619   	}
   2620 #ifdef WI_HISTOGRAM
   2621 	if (i > 490)
   2622 		hist1[500 - i]++;
   2623 	else
   2624 		hist1[10]++;
   2625 	if (++hist1count == 1000) {
   2626 		hist1count = 0;
   2627 		printf("%s: hist1: %d %d %d %d %d %d %d %d %d %d %d\n",
   2628 		    sc->sc_dev.dv_xname,
   2629 		    hist1[0], hist1[1], hist1[2], hist1[3], hist1[4],
   2630 		    hist1[5], hist1[6], hist1[7], hist1[8], hist1[9],
   2631 		    hist1[10]);
   2632 	}
   2633 #endif
   2634 	CSR_WRITE_2(sc, WI_PARAM0, val0);
   2635 	CSR_WRITE_2(sc, WI_PARAM1, val1);
   2636 	CSR_WRITE_2(sc, WI_PARAM2, val2);
   2637 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
   2638 
   2639 	return 0;
   2640 }
   2641 
   2642 STATIC int
   2643 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   2644 {
   2645 	int rc;
   2646 
   2647 #ifdef WI_DEBUG
   2648 	if (wi_debug) {
   2649 		printf("%s: [enter] %d txcmds outstanding\n", __func__,
   2650 		    sc->sc_txcmds);
   2651 	}
   2652 #endif
   2653 	if (sc->sc_txcmds > 0)
   2654 		wi_txcmd_wait(sc);
   2655 
   2656 	if ((rc = wi_cmd_start(sc, cmd, val0, val1, val2)) != 0)
   2657 		return rc;
   2658 
   2659 	if (cmd == WI_CMD_INI) {
   2660 		/* XXX: should sleep here. */
   2661 		if (sc->sc_invalid)
   2662 			return ENXIO;
   2663 		DELAY(100*1000);
   2664 	}
   2665 	rc = wi_cmd_wait(sc, cmd, val0);
   2666 
   2667 #ifdef WI_DEBUG
   2668 	if (wi_debug) {
   2669 		printf("%s: [     ] %d txcmds outstanding\n", __func__,
   2670 		    sc->sc_txcmds);
   2671 	}
   2672 #endif
   2673 	if (sc->sc_txcmds > 0)
   2674 		wi_cmd_intr(sc);
   2675 
   2676 #ifdef WI_DEBUG
   2677 	if (wi_debug) {
   2678 		printf("%s: [leave] %d txcmds outstanding\n", __func__,
   2679 		    sc->sc_txcmds);
   2680 	}
   2681 #endif
   2682 	return rc;
   2683 }
   2684 
   2685 STATIC int
   2686 wi_cmd_wait(struct wi_softc *sc, int cmd, int val0)
   2687 {
   2688 #ifdef WI_HISTOGRAM
   2689 	static int hist2[11];
   2690 	static int hist2count;
   2691 #endif
   2692 	int i, status;
   2693 #ifdef WI_DEBUG
   2694 	if (wi_debug > 1)
   2695 		printf("%s: cmd=%#x, arg=%#x\n", __func__, cmd, val0);
   2696 #endif /* WI_DEBUG */
   2697 
   2698 	/* wait for the cmd completed bit */
   2699 	for (i = 0; i < WI_TIMEOUT; i++) {
   2700 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
   2701 			break;
   2702 		if (sc->sc_invalid)
   2703 			return ENXIO;
   2704 		DELAY(WI_DELAY);
   2705 	}
   2706 
   2707 #ifdef WI_HISTOGRAM
   2708 	if (i < 100)
   2709 		hist2[i/10]++;
   2710 	else
   2711 		hist2[10]++;
   2712 	if (++hist2count == 1000) {
   2713 		hist2count = 0;
   2714 		printf("%s: hist2: %d %d %d %d %d %d %d %d %d %d %d\n",
   2715 		    sc->sc_dev.dv_xname,
   2716 		    hist2[0], hist2[1], hist2[2], hist2[3], hist2[4],
   2717 		    hist2[5], hist2[6], hist2[7], hist2[8], hist2[9],
   2718 		    hist2[10]);
   2719 	}
   2720 #endif
   2721 
   2722 	status = CSR_READ_2(sc, WI_STATUS);
   2723 
   2724 	if (i == WI_TIMEOUT) {
   2725 		printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
   2726 		    sc->sc_dev.dv_xname, cmd, val0);
   2727 		return ETIMEDOUT;
   2728 	}
   2729 
   2730 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
   2731 
   2732 	if (status & WI_STAT_CMD_RESULT) {
   2733 		printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
   2734 		    sc->sc_dev.dv_xname, cmd, val0);
   2735 		return EIO;
   2736 	}
   2737 	return 0;
   2738 }
   2739 
   2740 STATIC int
   2741 wi_seek_bap(struct wi_softc *sc, int id, int off)
   2742 {
   2743 #ifdef WI_HISTOGRAM
   2744 	static int hist4[11];
   2745 	static int hist4count;
   2746 #endif
   2747 	int i, status;
   2748 
   2749 	CSR_WRITE_2(sc, WI_SEL0, id);
   2750 	CSR_WRITE_2(sc, WI_OFF0, off);
   2751 
   2752 	for (i = 0; ; i++) {
   2753 		status = CSR_READ_2(sc, WI_OFF0);
   2754 		if ((status & WI_OFF_BUSY) == 0)
   2755 			break;
   2756 		if (i == WI_TIMEOUT) {
   2757 			printf("%s: timeout in wi_seek to %x/%x\n",
   2758 			    sc->sc_dev.dv_xname, id, off);
   2759 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2760 			return ETIMEDOUT;
   2761 		}
   2762 		if (sc->sc_invalid)
   2763 			return ENXIO;
   2764 		DELAY(2);
   2765 	}
   2766 #ifdef WI_HISTOGRAM
   2767 	if (i < 100)
   2768 		hist4[i/10]++;
   2769 	else
   2770 		hist4[10]++;
   2771 	if (++hist4count == 2500) {
   2772 		hist4count = 0;
   2773 		printf("%s: hist4: %d %d %d %d %d %d %d %d %d %d %d\n",
   2774 		    sc->sc_dev.dv_xname,
   2775 		    hist4[0], hist4[1], hist4[2], hist4[3], hist4[4],
   2776 		    hist4[5], hist4[6], hist4[7], hist4[8], hist4[9],
   2777 		    hist4[10]);
   2778 	}
   2779 #endif
   2780 	if (status & WI_OFF_ERR) {
   2781 		printf("%s: failed in wi_seek to %x/%x\n",
   2782 		    sc->sc_dev.dv_xname, id, off);
   2783 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2784 		return EIO;
   2785 	}
   2786 	sc->sc_bap_id = id;
   2787 	sc->sc_bap_off = off;
   2788 	return 0;
   2789 }
   2790 
   2791 STATIC int
   2792 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2793 {
   2794 	int error, cnt;
   2795 
   2796 	if (buflen == 0)
   2797 		return 0;
   2798 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2799 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2800 			return error;
   2801 	}
   2802 	cnt = (buflen + 1) / 2;
   2803 	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2804 	sc->sc_bap_off += cnt * 2;
   2805 	return 0;
   2806 }
   2807 
   2808 STATIC int
   2809 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2810 {
   2811 	int error, cnt;
   2812 
   2813 	if (buflen == 0)
   2814 		return 0;
   2815 
   2816 #ifdef WI_HERMES_AUTOINC_WAR
   2817   again:
   2818 #endif
   2819 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2820 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2821 			return error;
   2822 	}
   2823 	cnt = (buflen + 1) / 2;
   2824 	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2825 	sc->sc_bap_off += cnt * 2;
   2826 
   2827 #ifdef WI_HERMES_AUTOINC_WAR
   2828 	/*
   2829 	 * According to the comments in the HCF Light code, there is a bug
   2830 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
   2831 	 * where the chip's internal autoincrement counter gets thrown off
   2832 	 * during data writes:  the autoincrement is missed, causing one
   2833 	 * data word to be overwritten and subsequent words to be written to
   2834 	 * the wrong memory locations. The end result is that we could end
   2835 	 * up transmitting bogus frames without realizing it. The workaround
   2836 	 * for this is to write a couple of extra guard words after the end
   2837 	 * of the transfer, then attempt to read then back. If we fail to
   2838 	 * locate the guard words where we expect them, we preform the
   2839 	 * transfer over again.
   2840 	 */
   2841 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
   2842 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
   2843 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
   2844 		wi_seek_bap(sc, id, sc->sc_bap_off);
   2845 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2846 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
   2847 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
   2848 			printf("%s: detect auto increment bug, try again\n",
   2849 			    sc->sc_dev.dv_xname);
   2850 			goto again;
   2851 		}
   2852 	}
   2853 #endif
   2854 	return 0;
   2855 }
   2856 
   2857 STATIC int
   2858 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
   2859 {
   2860 	int error, len;
   2861 	struct mbuf *m;
   2862 
   2863 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
   2864 		if (m->m_len == 0)
   2865 			continue;
   2866 
   2867 		len = min(m->m_len, totlen);
   2868 
   2869 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
   2870 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
   2871 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
   2872 			    totlen);
   2873 		}
   2874 
   2875 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
   2876 			return error;
   2877 
   2878 		off += m->m_len;
   2879 		totlen -= len;
   2880 	}
   2881 	return 0;
   2882 }
   2883 
   2884 STATIC int
   2885 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
   2886 {
   2887 	int i;
   2888 
   2889 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
   2890 		printf("%s: failed to allocate %d bytes on NIC\n",
   2891 		    sc->sc_dev.dv_xname, len);
   2892 		return ENOMEM;
   2893 	}
   2894 
   2895 	for (i = 0; i < WI_TIMEOUT; i++) {
   2896 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
   2897 			break;
   2898 		DELAY(1);
   2899 	}
   2900 	if (i == WI_TIMEOUT) {
   2901 		printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
   2902 		return ETIMEDOUT;
   2903 	}
   2904 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
   2905 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   2906 	return 0;
   2907 }
   2908 
   2909 STATIC int
   2910 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
   2911 {
   2912 	int error, len;
   2913 	u_int16_t ltbuf[2];
   2914 
   2915 	/* Tell the NIC to enter record read mode. */
   2916 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
   2917 	if (error)
   2918 		return error;
   2919 
   2920 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2921 	if (error)
   2922 		return error;
   2923 
   2924 	if (le16toh(ltbuf[0]) == 0)
   2925 		return EOPNOTSUPP;
   2926 	if (le16toh(ltbuf[1]) != rid) {
   2927 		printf("%s: record read mismatch, rid=%x, got=%x\n",
   2928 		    sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
   2929 		return EIO;
   2930 	}
   2931 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
   2932 	if (*buflenp < len) {
   2933 		printf("%s: record buffer is too small, "
   2934 		    "rid=%x, size=%d, len=%d\n",
   2935 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
   2936 		return ENOSPC;
   2937 	}
   2938 	*buflenp = len;
   2939 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
   2940 }
   2941 
   2942 STATIC int
   2943 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
   2944 {
   2945 	int error;
   2946 	u_int16_t ltbuf[2];
   2947 
   2948 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
   2949 	ltbuf[1] = htole16(rid);
   2950 
   2951 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2952 	if (error)
   2953 		return error;
   2954 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
   2955 	if (error)
   2956 		return error;
   2957 
   2958 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
   2959 }
   2960 
   2961 STATIC void
   2962 wi_rssadapt_updatestats_cb(void *arg, struct ieee80211_node *ni)
   2963 {
   2964 	struct wi_node *wn = (void*)ni;
   2965 	ieee80211_rssadapt_updatestats(&wn->wn_rssadapt);
   2966 }
   2967 
   2968 STATIC void
   2969 wi_rssadapt_updatestats(void *arg)
   2970 {
   2971 	struct wi_softc *sc = arg;
   2972 	struct ieee80211com *ic = &sc->sc_ic;
   2973 	ieee80211_iterate_nodes(&ic->ic_sta, wi_rssadapt_updatestats_cb, arg);
   2974 	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
   2975 	    ic->ic_state == IEEE80211_S_RUN)
   2976 		callout_reset(&sc->sc_rssadapt_ch, hz / 10,
   2977 		    wi_rssadapt_updatestats, arg);
   2978 }
   2979 
   2980 /*
   2981  * In HOSTAP mode, restore IEEE80211_F_DROPUNENC when operating
   2982  * with WEP enabled so that the AP drops unencoded frames at the
   2983  * 802.11 layer.
   2984  *
   2985  * In all other modes, clear IEEE80211_F_DROPUNENC when operating
   2986  * with WEP enabled so we don't drop unencoded frames at the 802.11
   2987  * layer.  This is necessary because we must strip the WEP bit from
   2988  * the 802.11 header before passing frames to ieee80211_input
   2989  * because the card has already stripped the WEP crypto header from
   2990  * the packet.
   2991  */
   2992 STATIC void
   2993 wi_mend_flags(struct wi_softc *sc)
   2994 {
   2995 	struct ieee80211com *ic = &sc->sc_ic;
   2996 
   2997 	if (ic->ic_state == IEEE80211_S_RUN &&
   2998 	    (ic->ic_flags & IEEE80211_F_PRIVACY) != 0 &&
   2999 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
   3000 		ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   3001 	else
   3002 		ic->ic_flags = sc->sc_ic_flags;
   3003 }
   3004 
   3005 STATIC int
   3006 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   3007 {
   3008 	struct ifnet *ifp = ic->ic_ifp;
   3009 	struct wi_softc *sc = ifp->if_softc;
   3010 	struct ieee80211_node *ni = ic->ic_bss;
   3011 	u_int16_t val;
   3012 	struct wi_ssid ssid;
   3013 	struct wi_macaddr bssid, old_bssid;
   3014 	enum ieee80211_state ostate;
   3015 #ifdef WI_DEBUG
   3016 	static const char *stname[] =
   3017 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   3018 #endif /* WI_DEBUG */
   3019 
   3020 	ostate = ic->ic_state;
   3021 	DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
   3022 
   3023 	switch (nstate) {
   3024 	case IEEE80211_S_INIT:
   3025 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   3026 			callout_stop(&sc->sc_rssadapt_ch);
   3027 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   3028 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   3029 		break;
   3030 
   3031 	case IEEE80211_S_SCAN:
   3032 	case IEEE80211_S_AUTH:
   3033 	case IEEE80211_S_ASSOC:
   3034 		ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
   3035 		return 0;
   3036 
   3037 	case IEEE80211_S_RUN:
   3038 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   3039 		IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid);
   3040 		wi_read_xrid(sc, WI_RID_CURRENT_BSSID, &bssid,
   3041 		    IEEE80211_ADDR_LEN);
   3042 		IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid);
   3043 		IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid);
   3044 		wi_read_xrid(sc, WI_RID_CURRENT_CHAN, &val, sizeof(val));
   3045 		if (!isset(ic->ic_chan_avail, le16toh(val)))
   3046 			panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname,
   3047 			    le16toh(val));
   3048 		ni->ni_chan = &ic->ic_channels[le16toh(val)];
   3049 
   3050 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   3051 			ni->ni_esslen = ic->ic_des_esslen;
   3052 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
   3053 			ni->ni_rates = ic->ic_sup_rates[
   3054 			    ieee80211_chan2mode(ic, ni->ni_chan)];
   3055 			ni->ni_intval = ic->ic_lintval;
   3056 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
   3057 			if (ic->ic_flags & IEEE80211_F_PRIVACY)
   3058 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
   3059 		} else {
   3060 			wi_read_xrid(sc, WI_RID_CURRENT_SSID, &ssid,
   3061 			    sizeof(ssid));
   3062 			ni->ni_esslen = le16toh(ssid.wi_len);
   3063 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
   3064 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
   3065 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
   3066 			ni->ni_rates = ic->ic_sup_rates[
   3067 			    ieee80211_chan2mode(ic, ni->ni_chan)]; /*XXX*/
   3068 		}
   3069 		wi_mend_flags(sc);
   3070 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   3071 			callout_reset(&sc->sc_rssadapt_ch, hz / 10,
   3072 			    wi_rssadapt_updatestats, sc);
   3073 		/* Trigger routing socket messages. XXX Copied from
   3074 		 * ieee80211_newstate.
   3075 		 */
   3076 		if (ic->ic_opmode == IEEE80211_M_STA)
   3077 			ieee80211_notify_node_join(ic, ic->ic_bss,
   3078 				arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   3079 		break;
   3080 	}
   3081 	return (*sc->sc_newstate)(ic, nstate, arg);
   3082 }
   3083 
   3084 STATIC void
   3085 wi_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
   3086 {
   3087 	struct wi_softc *sc = ic->ic_ifp->if_softc;
   3088 
   3089 	(*sc->sc_set_tim)(ic, ni, set);
   3090 
   3091 	if ((ic->ic_flags & IEEE80211_F_TIMUPDATE) == 0)
   3092 		return;
   3093 
   3094 	ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
   3095 
   3096 	(void)wi_write_val(sc, WI_RID_SET_TIM,
   3097 	    IEEE80211_AID(ni->ni_associd) | (set ? 0x8000 : 0));
   3098 }
   3099 
   3100 STATIC int
   3101 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
   3102 {
   3103 	int error = 0;
   3104 	u_int16_t val[2];
   3105 
   3106 	if (!sc->sc_enabled)
   3107 		return ENXIO;
   3108 	switch (sc->sc_firmware_type) {
   3109 	case WI_LUCENT:
   3110 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   3111 		break;
   3112 	case WI_INTERSIL:
   3113 		val[0] = htole16(chanmask);	/* channel */
   3114 		val[1] = htole16(txrate);	/* tx rate */
   3115 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
   3116 		break;
   3117 	case WI_SYMBOL:
   3118 		/*
   3119 		 * XXX only supported on 3.x ?
   3120 		 */
   3121 		val[0] = htole16(BSCAN_BCAST | BSCAN_ONETIME);
   3122 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
   3123 		    val, sizeof(val[0]));
   3124 		break;
   3125 	}
   3126 	if (error == 0) {
   3127 		sc->sc_scan_timer = WI_SCAN_WAIT;
   3128 		sc->sc_if.if_timer = 1;
   3129 		DPRINTF(("wi_scan_ap: start scanning, "
   3130 			"chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
   3131 	}
   3132 	return error;
   3133 }
   3134 
   3135 STATIC void
   3136 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
   3137 {
   3138 #define	N(a)	(sizeof (a) / sizeof (a[0]))
   3139 	int i, naps, off, szbuf;
   3140 	struct wi_scan_header ws_hdr;	/* Prism2 header */
   3141 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
   3142 	struct wi_apinfo *ap;
   3143 
   3144 	off = sizeof(u_int16_t) * 2;
   3145 	memset(&ws_hdr, 0, sizeof(ws_hdr));
   3146 	switch (sc->sc_firmware_type) {
   3147 	case WI_INTERSIL:
   3148 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
   3149 		off += sizeof(ws_hdr);
   3150 		szbuf = sizeof(struct wi_scan_data_p2);
   3151 		break;
   3152 	case WI_SYMBOL:
   3153 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
   3154 		break;
   3155 	case WI_LUCENT:
   3156 		szbuf = sizeof(struct wi_scan_data);
   3157 		break;
   3158 	default:
   3159 		printf("%s: wi_scan_result: unknown firmware type %u\n",
   3160 		    sc->sc_dev.dv_xname, sc->sc_firmware_type);
   3161 		naps = 0;
   3162 		goto done;
   3163 	}
   3164 	naps = (cnt * 2 + 2 - off) / szbuf;
   3165 	if (naps > N(sc->sc_aps))
   3166 		naps = N(sc->sc_aps);
   3167 	sc->sc_naps = naps;
   3168 	/* Read Data */
   3169 	ap = sc->sc_aps;
   3170 	memset(&ws_dat, 0, sizeof(ws_dat));
   3171 	for (i = 0; i < naps; i++, ap++) {
   3172 		wi_read_bap(sc, fid, off, &ws_dat,
   3173 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
   3174 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
   3175 		    ether_sprintf(ws_dat.wi_bssid)));
   3176 		off += szbuf;
   3177 		ap->scanreason = le16toh(ws_hdr.wi_reason);
   3178 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
   3179 		ap->channel = le16toh(ws_dat.wi_chid);
   3180 		ap->signal  = le16toh(ws_dat.wi_signal);
   3181 		ap->noise   = le16toh(ws_dat.wi_noise);
   3182 		ap->quality = ap->signal - ap->noise;
   3183 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
   3184 		ap->interval = le16toh(ws_dat.wi_interval);
   3185 		ap->rate    = le16toh(ws_dat.wi_rate);
   3186 		ap->namelen = le16toh(ws_dat.wi_namelen);
   3187 		if (ap->namelen > sizeof(ap->name))
   3188 			ap->namelen = sizeof(ap->name);
   3189 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
   3190 	}
   3191 done:
   3192 	/* Done scanning */
   3193 	sc->sc_scan_timer = 0;
   3194 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
   3195 #undef N
   3196 }
   3197 
   3198 STATIC void
   3199 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
   3200 {
   3201 	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
   3202 	    ni	? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL
   3203 		: -1,
   3204 	    rssi);
   3205 	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
   3206 		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
   3207 		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
   3208 	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
   3209 		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
   3210 	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
   3211 		wh->wi_tx_rtry, wh->wi_tx_rate,
   3212 		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
   3213 	printf(" ehdr dst %s src %s type 0x%x\n",
   3214 		ether_sprintf(wh->wi_ehdr.ether_dhost),
   3215 		ether_sprintf(wh->wi_ehdr.ether_shost),
   3216 		wh->wi_ehdr.ether_type);
   3217 }
   3218