Home | History | Annotate | Line # | Download | only in ic
wi.c revision 1.147
      1 /*	$NetBSD: wi.c,v 1.147 2003/12/04 13:57:30 keihan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
     37  *
     38  * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
     39  * Electrical Engineering Department
     40  * Columbia University, New York City
     41  */
     42 
     43 /*
     44  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
     45  * from Lucent. Unlike the older cards, the new ones are programmed
     46  * entirely via a firmware-driven controller called the Hermes.
     47  * Unfortunately, Lucent will not release the Hermes programming manual
     48  * without an NDA (if at all). What they do release is an API library
     49  * called the HCF (Hardware Control Functions) which is supposed to
     50  * do the device-specific operations of a device driver for you. The
     51  * publically available version of the HCF library (the 'HCF Light') is
     52  * a) extremely gross, b) lacks certain features, particularly support
     53  * for 802.11 frames, and c) is contaminated by the GNU Public License.
     54  *
     55  * This driver does not use the HCF or HCF Light at all. Instead, it
     56  * programs the Hermes controller directly, using information gleaned
     57  * from the HCF Light code and corresponding documentation.
     58  *
     59  * This driver supports both the PCMCIA and ISA versions of the
     60  * WaveLAN/IEEE cards. Note however that the ISA card isn't really
     61  * anything of the sort: it's actually a PCMCIA bridge adapter
     62  * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
     63  * inserted. Consequently, you need to use the pccard support for
     64  * both the ISA and PCMCIA adapters.
     65  */
     66 
     67 /*
     68  * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
     69  * Oslo IETF plenary meeting.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.147 2003/12/04 13:57:30 keihan Exp $");
     74 
     75 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
     76 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
     77 
     78 #include "bpfilter.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/callout.h>
     83 #include <sys/device.h>
     84 #include <sys/socket.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/kernel.h>		/* for hz */
     88 #include <sys/proc.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_dl.h>
     92 #include <net/if_llc.h>
     93 #include <net/if_media.h>
     94 #include <net/if_ether.h>
     95 
     96 #include <net80211/ieee80211_var.h>
     97 #include <net80211/ieee80211_compat.h>
     98 #include <net80211/ieee80211_ioctl.h>
     99 #include <net80211/ieee80211_radiotap.h>
    100 
    101 #if NBPFILTER > 0
    102 #include <net/bpf.h>
    103 #include <net/bpfdesc.h>
    104 #endif
    105 
    106 #include <machine/bus.h>
    107 
    108 #include <dev/ic/wi_ieee.h>
    109 #include <dev/ic/wireg.h>
    110 #include <dev/ic/wivar.h>
    111 
    112 static int  wi_init(struct ifnet *);
    113 static void wi_stop(struct ifnet *, int);
    114 static void wi_start(struct ifnet *);
    115 static int  wi_reset(struct wi_softc *);
    116 static void wi_watchdog(struct ifnet *);
    117 static int  wi_ioctl(struct ifnet *, u_long, caddr_t);
    118 static int  wi_media_change(struct ifnet *);
    119 static void wi_media_status(struct ifnet *, struct ifmediareq *);
    120 
    121 static void wi_rx_intr(struct wi_softc *);
    122 static void wi_tx_intr(struct wi_softc *);
    123 static void wi_tx_ex_intr(struct wi_softc *);
    124 static void wi_info_intr(struct wi_softc *);
    125 
    126 static int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
    127 static int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
    128 static int  wi_cfg_txrate(struct wi_softc *);
    129 static int  wi_write_txrate(struct wi_softc *, int);
    130 static int  wi_write_wep(struct wi_softc *);
    131 static int  wi_write_multi(struct wi_softc *);
    132 static int  wi_alloc_fid(struct wi_softc *, int, int *);
    133 static void wi_read_nicid(struct wi_softc *);
    134 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
    135 
    136 static int  wi_cmd(struct wi_softc *, int, int, int, int);
    137 static int  wi_seek_bap(struct wi_softc *, int, int);
    138 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
    139 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
    140 static int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
    141 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
    142 static int  wi_write_rid(struct wi_softc *, int, void *, int);
    143 
    144 static int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    145 static int  wi_set_tim(struct ieee80211com *, int, int);
    146 
    147 static int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
    148 static void wi_scan_result(struct wi_softc *, int, int);
    149 
    150 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
    151 
    152 static inline int
    153 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
    154 {
    155 
    156 	val = htole16(val);
    157 	return wi_write_rid(sc, rid, &val, sizeof(val));
    158 }
    159 
    160 static	struct timeval lasttxerror;	/* time of last tx error msg */
    161 static	int curtxeps = 0;		/* current tx error msgs/sec */
    162 static	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
    163 
    164 #ifdef WI_DEBUG
    165 int wi_debug = 0;
    166 
    167 #define	DPRINTF(X)	if (wi_debug) printf X
    168 #define	DPRINTF2(X)	if (wi_debug > 1) printf X
    169 #define	IFF_DUMPPKTS(_ifp) \
    170 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    171 #else
    172 #define	DPRINTF(X)
    173 #define	DPRINTF2(X)
    174 #define	IFF_DUMPPKTS(_ifp)	0
    175 #endif
    176 
    177 #define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
    178 
    179 struct wi_card_ident
    180 wi_card_ident[] = {
    181 	/* CARD_ID			CARD_NAME		FIRM_TYPE */
    182 	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
    183 	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
    184 	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
    185 	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
    186 	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
    187 	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
    188 	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
    189 	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
    190 	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
    191 	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
    192 	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
    193 	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
    194 	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    195 	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    196 	{ WI_NIC_3842_PCMCIA_ATM_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    197 	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    198 	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    199 	{ WI_NIC_3842_MINI_ATM_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    200 	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    201 	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    202 	{ WI_NIC_3842_PCI_ATM_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    203 	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    204 	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    205 	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    206 	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    207 	{ 0,	NULL,	0 },
    208 };
    209 
    210 int
    211 wi_attach(struct wi_softc *sc)
    212 {
    213 	struct ieee80211com *ic = &sc->sc_ic;
    214 	struct ifnet *ifp = &ic->ic_if;
    215 	int chan, nrate, buflen;
    216 	u_int16_t val, chanavail;
    217  	struct {
    218  		u_int16_t nrates;
    219  		char rates[IEEE80211_RATE_SIZE];
    220  	} ratebuf;
    221 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
    222 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    223 	};
    224 	int s;
    225 
    226 	s = splnet();
    227 
    228 	/* Make sure interrupts are disabled. */
    229 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    230 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    231 
    232 	sc->sc_invalid = 0;
    233 
    234 	/* Reset the NIC. */
    235 	if (wi_reset(sc) != 0) {
    236 		sc->sc_invalid = 1;
    237 		splx(s);
    238 		return 1;
    239 	}
    240 
    241 	buflen = IEEE80211_ADDR_LEN;
    242 	if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 ||
    243 	    IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
    244 		printf(" could not get mac address, attach failed\n");
    245 		splx(s);
    246 		return 1;
    247 	}
    248 
    249 	printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
    250 
    251 	/* Read NIC identification */
    252 	wi_read_nicid(sc);
    253 
    254 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    255 	ifp->if_softc = sc;
    256 	ifp->if_start = wi_start;
    257 	ifp->if_ioctl = wi_ioctl;
    258 	ifp->if_watchdog = wi_watchdog;
    259 	ifp->if_init = wi_init;
    260 	ifp->if_stop = wi_stop;
    261 	ifp->if_flags =
    262 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
    263 	IFQ_SET_READY(&ifp->if_snd);
    264 
    265 	ic->ic_phytype = IEEE80211_T_DS;
    266 	ic->ic_opmode = IEEE80211_M_STA;
    267 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
    268 	ic->ic_state = IEEE80211_S_INIT;
    269 	ic->ic_max_aid = WI_MAX_AID;
    270 
    271 	/* Find available channel */
    272 	buflen = sizeof(chanavail);
    273 	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &chanavail, &buflen) != 0)
    274 		chanavail = htole16(0x1fff);	/* assume 1-11 */
    275 	for (chan = 16; chan > 0; chan--) {
    276 		if (!isset((u_int8_t*)&chanavail, chan - 1))
    277 			continue;
    278 		ic->ic_ibss_chan = &ic->ic_channels[chan];
    279 		ic->ic_channels[chan].ic_freq =
    280 		    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
    281 		ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
    282 	}
    283 
    284 	/* Find default IBSS channel */
    285 	buflen = sizeof(val);
    286 	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
    287 		chan = le16toh(val);
    288 		if (isset((u_int8_t*)&chanavail, chan - 1))
    289 			ic->ic_ibss_chan = &ic->ic_channels[chan];
    290 	}
    291 	if (ic->ic_ibss_chan == NULL)
    292 		panic("%s: no available channel\n", sc->sc_dev.dv_xname);
    293 
    294 	if (sc->sc_firmware_type == WI_LUCENT) {
    295 		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
    296 	} else {
    297 		buflen = sizeof(val);
    298 		if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
    299 		    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0)
    300 			sc->sc_dbm_offset = le16toh(val);
    301 		else
    302 			sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
    303 	}
    304 
    305 	/*
    306 	 * Set flags based on firmware version.
    307 	 */
    308 	switch (sc->sc_firmware_type) {
    309 	case WI_LUCENT:
    310 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    311 #ifdef WI_HERMES_AUTOINC_WAR
    312 		/* XXX: not confirmed, but never seen for recent firmware */
    313 		if (sc->sc_sta_firmware_ver <  40000) {
    314 			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
    315 		}
    316 #endif
    317 		if (sc->sc_sta_firmware_ver >= 60000)
    318 			sc->sc_flags |= WI_FLAGS_HAS_MOR;
    319 		if (sc->sc_sta_firmware_ver >= 60006) {
    320 			ic->ic_caps |= IEEE80211_C_IBSS;
    321 			ic->ic_caps |= IEEE80211_C_MONITOR;
    322 		}
    323 		sc->sc_ibss_port = 1;
    324 		break;
    325 
    326 	case WI_INTERSIL:
    327 		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
    328 		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
    329 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    330 		if (sc->sc_sta_firmware_ver > 10101)
    331 			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
    332 		if (sc->sc_sta_firmware_ver >= 800) {
    333 			if (sc->sc_sta_firmware_ver != 10402)
    334 				ic->ic_caps |= IEEE80211_C_HOSTAP;
    335 			ic->ic_caps |= IEEE80211_C_IBSS;
    336 			ic->ic_caps |= IEEE80211_C_MONITOR;
    337 		}
    338 		sc->sc_ibss_port = 0;
    339 		break;
    340 
    341 	case WI_SYMBOL:
    342 		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
    343 		if (sc->sc_sta_firmware_ver >= 20000)
    344 			ic->ic_caps |= IEEE80211_C_IBSS;
    345 		sc->sc_ibss_port = 4;
    346 		break;
    347 	}
    348 
    349 	/*
    350 	 * Find out if we support WEP on this card.
    351 	 */
    352 	buflen = sizeof(val);
    353 	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
    354 	    val != htole16(0))
    355 		ic->ic_caps |= IEEE80211_C_WEP;
    356 
    357 	/* Find supported rates. */
    358 	buflen = sizeof(ratebuf);
    359 	if (wi_read_rid(sc, WI_RID_DATA_RATES, &ratebuf, &buflen) == 0) {
    360 		nrate = le16toh(ratebuf.nrates);
    361 		if (nrate > IEEE80211_RATE_SIZE)
    362 			nrate = IEEE80211_RATE_SIZE;
    363 		memcpy(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates,
    364 		    &ratebuf.rates[0], nrate);
    365 		ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
    366 	}
    367 	buflen = sizeof(val);
    368 
    369 	sc->sc_max_datalen = 2304;
    370 	sc->sc_rts_thresh = 2347;
    371 	sc->sc_frag_thresh = 2346;
    372 	sc->sc_system_scale = 1;
    373 	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
    374 	sc->sc_roaming_mode = 1;
    375 
    376 	/*
    377 	 * Call MI attach routines.
    378 	 */
    379 	if_attach(ifp);
    380 	ieee80211_ifattach(ifp);
    381 
    382 	sc->sc_newstate = ic->ic_newstate;
    383 	ic->ic_newstate = wi_newstate;
    384 	ic->ic_set_tim = wi_set_tim;
    385 
    386 	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
    387 
    388 #if NBPFILTER > 0
    389 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    390 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    391 #endif
    392 
    393 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
    394 	sc->sc_rxtap.wr_ihdr.it_len = sizeof(sc->sc_rxtapu);
    395 	sc->sc_rxtap.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT;
    396 
    397 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
    398 	sc->sc_txtap.wt_ihdr.it_len = sizeof(sc->sc_txtapu);
    399 	sc->sc_txtap.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
    400 
    401 	/* Attach is successful. */
    402 	sc->sc_attached = 1;
    403 
    404 	splx(s);
    405 	return 0;
    406 }
    407 
    408 int
    409 wi_detach(struct wi_softc *sc)
    410 {
    411 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    412 	int s;
    413 
    414 	if (!sc->sc_attached)
    415 		return 0;
    416 
    417 	s = splnet();
    418 
    419 	sc->sc_invalid = 1;
    420 	wi_stop(ifp, 1);
    421 
    422 	/* Delete all remaining media. */
    423 	ifmedia_delete_instance(&sc->sc_ic.ic_media, IFM_INST_ANY);
    424 
    425 	ieee80211_ifdetach(ifp);
    426 	if_detach(ifp);
    427 	splx(s);
    428 	return 0;
    429 }
    430 
    431 #ifdef __NetBSD__
    432 int
    433 wi_activate(struct device *self, enum devact act)
    434 {
    435 	struct wi_softc *sc = (struct wi_softc *)self;
    436 	int rv = 0, s;
    437 
    438 	s = splnet();
    439 	switch (act) {
    440 	case DVACT_ACTIVATE:
    441 		rv = EOPNOTSUPP;
    442 		break;
    443 
    444 	case DVACT_DEACTIVATE:
    445 		if_deactivate(&sc->sc_ic.ic_if);
    446 		break;
    447 	}
    448 	splx(s);
    449 	return rv;
    450 }
    451 
    452 void
    453 wi_power(struct wi_softc *sc, int why)
    454 {
    455 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    456 	int s;
    457 
    458 	s = splnet();
    459 	switch (why) {
    460 	case PWR_SUSPEND:
    461 	case PWR_STANDBY:
    462 		wi_stop(ifp, 1);
    463 		break;
    464 	case PWR_RESUME:
    465 		if (ifp->if_flags & IFF_UP) {
    466 			wi_init(ifp);
    467 			(void)wi_intr(sc);
    468 		}
    469 		break;
    470 	case PWR_SOFTSUSPEND:
    471 	case PWR_SOFTSTANDBY:
    472 	case PWR_SOFTRESUME:
    473 		break;
    474 	}
    475 	splx(s);
    476 }
    477 #endif /* __NetBSD__ */
    478 
    479 void
    480 wi_shutdown(struct wi_softc *sc)
    481 {
    482 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    483 
    484 	if (sc->sc_attached)
    485 		wi_stop(ifp, 1);
    486 }
    487 
    488 int
    489 wi_intr(void *arg)
    490 {
    491 	int i;
    492 	struct wi_softc	*sc = arg;
    493 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    494 	u_int16_t status;
    495 
    496 	if (sc->sc_enabled == 0 ||
    497 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
    498 	    (ifp->if_flags & IFF_RUNNING) == 0)
    499 		return 0;
    500 
    501 	if ((ifp->if_flags & IFF_UP) == 0) {
    502 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    503 		CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    504 		return 1;
    505 	}
    506 
    507 	/* This is superfluous on Prism, but Lucent breaks if we
    508 	 * do not disable interrupts.
    509 	 */
    510 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    511 
    512 	/* maximum 10 loops per interrupt */
    513 	for (i = 0; i < 10; i++) {
    514 		/*
    515 		 * Only believe a status bit when we enter wi_intr, or when
    516 		 * the bit was "off" the last time through the loop. This is
    517 		 * my strategy to avoid racing the hardware/firmware if I
    518 		 * can re-read the event status register more quickly than
    519 		 * it is updated.
    520 		 */
    521 		status = CSR_READ_2(sc, WI_EVENT_STAT);
    522 		if ((status & WI_INTRS) == 0)
    523 			break;
    524 
    525 		if (status & WI_EV_RX)
    526 			wi_rx_intr(sc);
    527 
    528 		if (status & WI_EV_ALLOC)
    529 			wi_tx_intr(sc);
    530 
    531 		if (status & WI_EV_TX_EXC)
    532 			wi_tx_ex_intr(sc);
    533 
    534 		if (status & WI_EV_INFO)
    535 			wi_info_intr(sc);
    536 
    537 		if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
    538 		    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
    539 		    !IFQ_IS_EMPTY(&ifp->if_snd))
    540 			wi_start(ifp);
    541 	}
    542 
    543 	/* re-enable interrupts */
    544 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    545 
    546 	return 1;
    547 }
    548 
    549 static int
    550 wi_init(struct ifnet *ifp)
    551 {
    552 	struct wi_softc *sc = ifp->if_softc;
    553 	struct ieee80211com *ic = &sc->sc_ic;
    554 	struct wi_joinreq join;
    555 	int i;
    556 	int error = 0, wasenabled;
    557 
    558 	DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
    559 	wasenabled = sc->sc_enabled;
    560 	if (!sc->sc_enabled) {
    561 		if ((error = (*sc->sc_enable)(sc)) != 0)
    562 			goto out;
    563 		sc->sc_enabled = 1;
    564 	} else
    565 		wi_stop(ifp, 0);
    566 
    567 	/* Symbol firmware cannot be initialized more than once */
    568 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled)
    569 		if ((error = wi_reset(sc)) != 0)
    570 			goto out;
    571 
    572 	/* common 802.11 configuration */
    573 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
    574 	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
    575 	switch (ic->ic_opmode) {
    576 	case IEEE80211_M_STA:
    577 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
    578 		break;
    579 	case IEEE80211_M_IBSS:
    580 		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
    581 		ic->ic_flags |= IEEE80211_F_IBSSON;
    582 		sc->sc_syn_timer = 5;
    583 		ifp->if_timer = 1;
    584 		break;
    585 	case IEEE80211_M_AHDEMO:
    586 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
    587 		break;
    588 	case IEEE80211_M_HOSTAP:
    589 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
    590 		break;
    591 	case IEEE80211_M_MONITOR:
    592 		if (sc->sc_firmware_type == WI_LUCENT)
    593 			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
    594 		wi_cmd(sc, WI_CMD_TEST | (WI_TEST_MONITOR << 8), 0, 0, 0);
    595 		break;
    596 	}
    597 
    598 	/* Intersil interprets this RID as joining ESS even in IBSS mode */
    599 	if (sc->sc_firmware_type == WI_LUCENT &&
    600 	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
    601 		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
    602 	else
    603 		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
    604 	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
    605 	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
    606 	    ic->ic_des_esslen);
    607 	wi_write_val(sc, WI_RID_OWN_CHNL,
    608 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
    609 	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
    610 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
    611 	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
    612 	wi_write_val(sc, WI_RID_PM_ENABLED,
    613 	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    614 
    615 	/* not yet common 802.11 configuration */
    616 	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
    617 	wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
    618 	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
    619 		wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh);
    620 
    621 	/* driver specific 802.11 configuration */
    622 	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
    623 		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
    624 	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
    625 		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
    626 	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
    627 		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
    628 	wi_cfg_txrate(sc);
    629 	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
    630 
    631 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    632 	    sc->sc_firmware_type == WI_INTERSIL) {
    633 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
    634 		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
    635 		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
    636 		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
    637 	}
    638 
    639 	/*
    640 	 * Initialize promisc mode.
    641 	 *	Being in Host-AP mode causes a great
    642 	 *	deal of pain if promiscuous mode is set.
    643 	 *	Therefore we avoid confusing the firmware
    644 	 *	and always reset promisc mode in Host-AP
    645 	 *	mode.  Host-AP sees all the packets anyway.
    646 	 */
    647 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    648 	    (ifp->if_flags & IFF_PROMISC) != 0) {
    649 		wi_write_val(sc, WI_RID_PROMISC, 1);
    650 	} else {
    651 		wi_write_val(sc, WI_RID_PROMISC, 0);
    652 	}
    653 
    654 	/* Configure WEP. */
    655 	if (ic->ic_caps & IEEE80211_C_WEP)
    656 		wi_write_wep(sc);
    657 
    658 	/* Set multicast filter. */
    659 	wi_write_multi(sc);
    660 
    661 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
    662 		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
    663 		if (sc->sc_firmware_type == WI_SYMBOL)
    664 			sc->sc_buflen = 1585;	/* XXX */
    665 		for (i = 0; i < WI_NTXBUF; i++) {
    666 			error = wi_alloc_fid(sc, sc->sc_buflen,
    667 			    &sc->sc_txd[i].d_fid);
    668 			if (error) {
    669 				printf("%s: tx buffer allocation failed\n",
    670 				    sc->sc_dev.dv_xname);
    671 				goto out;
    672 			}
    673 			DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
    674 			    sc->sc_txd[i].d_fid));
    675 			sc->sc_txd[i].d_len = 0;
    676 		}
    677 	}
    678 	sc->sc_txcur = sc->sc_txnext = 0;
    679 
    680 	/* Enable desired port */
    681 	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
    682 	ifp->if_flags |= IFF_RUNNING;
    683 	ifp->if_flags &= ~IFF_OACTIVE;
    684 	ic->ic_state = IEEE80211_S_INIT;
    685 
    686 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
    687 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
    688 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
    689 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    690 
    691 	/* Enable interrupts */
    692 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    693 
    694 	if (!wasenabled &&
    695 	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
    696 	    sc->sc_firmware_type == WI_INTERSIL) {
    697 		/* XXX: some card need to be re-enabled for hostap */
    698 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
    699 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
    700 	}
    701 
    702 	if (ic->ic_opmode == IEEE80211_M_STA &&
    703 	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
    704 	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
    705 		memset(&join, 0, sizeof(join));
    706 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    707 			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
    708 		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
    709 			join.wi_chan =
    710 			    htole16(ieee80211_chan2ieee(ic, ic->ic_des_chan));
    711 		/* Lucent firmware does not support the JOIN RID. */
    712 		if (sc->sc_firmware_type != WI_LUCENT)
    713 			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
    714 	}
    715 
    716  out:
    717 	if (error) {
    718 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    719 		wi_stop(ifp, 0);
    720 	}
    721 	DPRINTF(("wi_init: return %d\n", error));
    722 	return error;
    723 }
    724 
    725 static void
    726 wi_stop(struct ifnet *ifp, int disable)
    727 {
    728 	struct wi_softc	*sc = ifp->if_softc;
    729 	struct ieee80211com *ic = &sc->sc_ic;
    730 	int s;
    731 
    732 	if (!sc->sc_enabled)
    733 		return;
    734 
    735 	s = splnet();
    736 
    737 	DPRINTF(("wi_stop: disable %d\n", disable));
    738 
    739 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    740 	if (!sc->sc_invalid) {
    741 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    742 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
    743 	}
    744 
    745 	sc->sc_tx_timer = 0;
    746 	sc->sc_scan_timer = 0;
    747 	sc->sc_syn_timer = 0;
    748 	sc->sc_false_syns = 0;
    749 	sc->sc_naps = 0;
    750 	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
    751 	ifp->if_timer = 0;
    752 
    753 	if (disable) {
    754 		if (sc->sc_disable)
    755 			(*sc->sc_disable)(sc);
    756 		sc->sc_enabled = 0;
    757 	}
    758 	splx(s);
    759 }
    760 
    761 static void
    762 wi_start(struct ifnet *ifp)
    763 {
    764 	struct wi_softc	*sc = ifp->if_softc;
    765 	struct ieee80211com *ic = &sc->sc_ic;
    766 	struct ieee80211_node *ni;
    767 	struct ieee80211_frame *wh;
    768 	struct mbuf *m0;
    769 	struct wi_frame frmhdr;
    770 	int cur, fid, off;
    771 
    772 	if (!sc->sc_enabled || sc->sc_invalid)
    773 		return;
    774 	if (sc->sc_flags & WI_FLAGS_OUTRANGE)
    775 		return;
    776 
    777 	memset(&frmhdr, 0, sizeof(frmhdr));
    778 	cur = sc->sc_txnext;
    779 	for (;;) {
    780 		ni = ic->ic_bss;
    781 		if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
    782 			if (sc->sc_txd[cur].d_len != 0) {
    783 				ifp->if_flags |= IFF_OACTIVE;
    784 				break;
    785 			}
    786 			IF_DEQUEUE(&ic->ic_mgtq, m0);
    787 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
    788 			    (caddr_t)&frmhdr.wi_ehdr);
    789 			frmhdr.wi_ehdr.ether_type = 0;
    790                         wh = mtod(m0, struct ieee80211_frame *);
    791 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
    792 			m0->m_pkthdr.rcvif = NULL;
    793 		} else if (!IF_IS_EMPTY(&ic->ic_pwrsaveq)) {
    794 			struct llc *llc;
    795 
    796 			/*
    797 			 * Should these packets be processed after the
    798 			 * regular packets or before?  Since they are being
    799 			 * probed for, they are probably less time critical
    800 			 * than other packets, but, on the other hand,
    801 			 * we want the power saving nodes to go back to
    802 			 * sleep as quickly as possible to save power...
    803 			 */
    804 
    805 			if (ic->ic_state != IEEE80211_S_RUN)
    806 				break;
    807 
    808 			if (sc->sc_txd[cur].d_len != 0) {
    809 				ifp->if_flags |= IFF_OACTIVE;
    810 				break;
    811 			}
    812 			IF_DEQUEUE(&ic->ic_pwrsaveq, m0);
    813                         wh = mtod(m0, struct ieee80211_frame *);
    814 			llc = (struct llc *) (wh + 1);
    815 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
    816 			    (caddr_t)&frmhdr.wi_ehdr);
    817 			frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
    818 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
    819 			m0->m_pkthdr.rcvif = NULL;
    820 		} else {
    821 			if (ic->ic_state != IEEE80211_S_RUN) {
    822 				break;
    823 			}
    824 			IFQ_POLL(&ifp->if_snd, m0);
    825 			if (m0 == NULL) {
    826 				break;
    827 			}
    828 			if (sc->sc_txd[cur].d_len != 0) {
    829 				ifp->if_flags |= IFF_OACTIVE;
    830 				break;
    831 			}
    832 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    833 			ifp->if_opackets++;
    834 			m_copydata(m0, 0, ETHER_HDR_LEN,
    835 			    (caddr_t)&frmhdr.wi_ehdr);
    836 #if NBPFILTER > 0
    837 			if (ifp->if_bpf)
    838 				bpf_mtap(ifp->if_bpf, m0);
    839 #endif
    840 
    841 			if ((m0 = ieee80211_encap(ifp, m0, &ni)) == NULL) {
    842 				ifp->if_oerrors++;
    843 				continue;
    844 			}
    845                         wh = mtod(m0, struct ieee80211_frame *);
    846 			if (ic->ic_flags & IEEE80211_F_WEPON)
    847 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
    848 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    849 			    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    850 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    851 			    IEEE80211_FC0_TYPE_DATA) {
    852 				if (ni->ni_associd == 0) {
    853 					m_freem(m0);
    854 					ifp->if_oerrors++;
    855 					goto next;
    856 				}
    857 				if (ni->ni_pwrsave & IEEE80211_PS_SLEEP) {
    858 					ieee80211_pwrsave(ic, ni, m0);
    859 					continue; /* don't free node. */
    860 				}
    861 			}
    862 		}
    863 #if NBPFILTER > 0
    864 		if (ic->ic_rawbpf)
    865 			bpf_mtap(ic->ic_rawbpf, m0);
    866 #endif
    867 		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
    868 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    869 		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
    870 			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
    871 				ifp->if_oerrors++;
    872 				goto next;
    873 			}
    874 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
    875 		}
    876 #if NBPFILTER > 0
    877 		if (sc->sc_drvbpf) {
    878 			struct mbuf mb;
    879 
    880 			struct wi_tx_radiotap_header *tap = &sc->sc_txtap;
    881 
    882 			tap->wt_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
    883 			tap->wt_chan_freq = ic->ic_bss->ni_chan->ic_freq;
    884 
    885 			/* TBD tap->wt_flags, tap->wt_chan_flags */
    886 
    887 			M_COPY_PKTHDR(&mb, m0);
    888 			mb.m_data = (caddr_t)tap;
    889 			mb.m_len = tap->wt_ihdr.it_len;
    890 			mb.m_next = m0;
    891 			mb.m_pkthdr.len += mb.m_len;
    892 			bpf_mtap(sc->sc_drvbpf, &mb);
    893 		}
    894 #endif
    895 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
    896 		    (caddr_t)&frmhdr.wi_whdr);
    897 		m_adj(m0, sizeof(struct ieee80211_frame));
    898 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
    899 		if (IFF_DUMPPKTS(ifp))
    900 			wi_dump_pkt(&frmhdr, ni, -1);
    901 		fid = sc->sc_txd[cur].d_fid;
    902 		off = sizeof(frmhdr);
    903 		if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 ||
    904 		    wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) {
    905 			ifp->if_oerrors++;
    906 			m_freem(m0);
    907 			goto next;
    908 		}
    909 		m_freem(m0);
    910 		sc->sc_txd[cur].d_len = off;
    911 		if (sc->sc_txcur == cur) {
    912 			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
    913 				printf("%s: xmit failed\n",
    914 				    sc->sc_dev.dv_xname);
    915 				sc->sc_txd[cur].d_len = 0;
    916 				goto next;
    917 			}
    918 			sc->sc_tx_timer = 5;
    919 			ifp->if_timer = 1;
    920 		}
    921 		sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
    922 next:
    923 		if (ni != NULL && ni != ic->ic_bss)
    924 			ieee80211_free_node(ic, ni);
    925 	}
    926 }
    927 
    928 
    929 static int
    930 wi_reset(struct wi_softc *sc)
    931 {
    932 	int i, error;
    933 
    934 	DPRINTF(("wi_reset\n"));
    935 
    936 	if (sc->sc_reset)
    937 		(*sc->sc_reset)(sc);
    938 
    939 	error = 0;
    940 	for (i = 0; i < 5; i++) {
    941 		DELAY(20*1000);	/* XXX: way too long! */
    942 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
    943 			break;
    944 	}
    945 	if (error) {
    946 		printf("%s: init failed\n", sc->sc_dev.dv_xname);
    947 		return error;
    948 	}
    949 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    950 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    951 
    952 	/* Calibrate timer. */
    953 	wi_write_val(sc, WI_RID_TICK_TIME, 0);
    954 	return 0;
    955 }
    956 
    957 static void
    958 wi_watchdog(struct ifnet *ifp)
    959 {
    960 	struct wi_softc *sc = ifp->if_softc;
    961 	struct ieee80211com *ic = &sc->sc_ic;
    962 
    963 	ifp->if_timer = 0;
    964 	if (!sc->sc_enabled)
    965 		return;
    966 
    967 	if (sc->sc_tx_timer) {
    968 		if (--sc->sc_tx_timer == 0) {
    969 			printf("%s: device timeout\n", ifp->if_xname);
    970 			ifp->if_oerrors++;
    971 			wi_init(ifp);
    972 			return;
    973 		}
    974 		ifp->if_timer = 1;
    975 	}
    976 
    977 	if (sc->sc_scan_timer) {
    978 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
    979 		    sc->sc_firmware_type == WI_INTERSIL) {
    980 			DPRINTF(("wi_watchdog: inquire scan\n"));
    981 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
    982 		}
    983 		if (sc->sc_scan_timer)
    984 			ifp->if_timer = 1;
    985 	}
    986 
    987 	if (sc->sc_syn_timer) {
    988 		if (--sc->sc_syn_timer == 0) {
    989 			DPRINTF2(("%s: %d false syns\n",
    990 			    sc->sc_dev.dv_xname, sc->sc_false_syns));
    991 			sc->sc_false_syns = 0;
    992 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    993 			sc->sc_syn_timer = 5;
    994 		}
    995 		ifp->if_timer = 1;
    996 	}
    997 
    998 	/* TODO: rate control */
    999 	ieee80211_watchdog(ifp);
   1000 }
   1001 
   1002 static int
   1003 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1004 {
   1005 	struct wi_softc *sc = ifp->if_softc;
   1006 	struct ieee80211com *ic = &sc->sc_ic;
   1007 	struct ifreq *ifr = (struct ifreq *)data;
   1008 	int s, error = 0;
   1009 
   1010 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1011 		return ENXIO;
   1012 
   1013 	s = splnet();
   1014 
   1015 	switch (cmd) {
   1016 	case SIOCSIFFLAGS:
   1017 		/*
   1018 		 * Can't do promisc and hostap at the same time.  If all that's
   1019 		 * changing is the promisc flag, try to short-circuit a call to
   1020 		 * wi_init() by just setting PROMISC in the hardware.
   1021 		 */
   1022 		if (ifp->if_flags & IFF_UP) {
   1023 			if (sc->sc_enabled) {
   1024 				if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   1025 				    (ifp->if_flags & IFF_PROMISC) != 0)
   1026 					wi_write_val(sc, WI_RID_PROMISC, 1);
   1027 				else
   1028 					wi_write_val(sc, WI_RID_PROMISC, 0);
   1029 			} else
   1030 				error = wi_init(ifp);
   1031 		} else if (sc->sc_enabled)
   1032 			wi_stop(ifp, 1);
   1033 		break;
   1034 	case SIOCSIFMEDIA:
   1035 	case SIOCGIFMEDIA:
   1036 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   1037 		break;
   1038 	case SIOCADDMULTI:
   1039 	case SIOCDELMULTI:
   1040 		error = (cmd == SIOCADDMULTI) ?
   1041 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
   1042 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
   1043 		if (error == ENETRESET) {
   1044 			if (sc->sc_enabled) {
   1045 				/* do not rescan */
   1046 				error = wi_write_multi(sc);
   1047 			} else
   1048 				error = 0;
   1049 		}
   1050 		break;
   1051 	case SIOCGIFGENERIC:
   1052 		error = wi_get_cfg(ifp, cmd, data);
   1053 		break;
   1054 	case SIOCSIFGENERIC:
   1055 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   1056 		if (error)
   1057 			break;
   1058 		error = wi_set_cfg(ifp, cmd, data);
   1059 		if (error == ENETRESET) {
   1060 			if (sc->sc_enabled)
   1061 				error = wi_init(ifp);
   1062 			else
   1063 				error = 0;
   1064 		}
   1065 		break;
   1066 	case SIOCS80211BSSID:
   1067 		if (sc->sc_firmware_type == WI_LUCENT) {
   1068 			error = ENODEV;
   1069 			break;
   1070 		}
   1071 		/* fall through */
   1072 	default:
   1073 		error = ieee80211_ioctl(ifp, cmd, data);
   1074 		if (error == ENETRESET) {
   1075 			if (sc->sc_enabled)
   1076 				error = wi_init(ifp);
   1077 			else
   1078 				error = 0;
   1079 		}
   1080 		break;
   1081 	}
   1082 	splx(s);
   1083 	return error;
   1084 }
   1085 
   1086 /* TBD factor with ieee80211_media_change */
   1087 static int
   1088 wi_media_change(struct ifnet *ifp)
   1089 {
   1090 	struct wi_softc *sc = ifp->if_softc;
   1091 	struct ieee80211com *ic = &sc->sc_ic;
   1092 	struct ifmedia_entry *ime;
   1093 	enum ieee80211_opmode newmode;
   1094 	int i, rate, error = 0;
   1095 
   1096 	ime = ic->ic_media.ifm_cur;
   1097 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
   1098 		i = -1;
   1099 	} else {
   1100 		struct ieee80211_rateset *rs =
   1101 		    &ic->ic_sup_rates[ieee80211_chan2mode(ic,
   1102 		        ic->ic_bss->ni_chan)];
   1103 		rate = ieee80211_media2rate(ime->ifm_media);
   1104 		if (rate == 0)
   1105 			return EINVAL;
   1106 		for (i = 0; i < rs->rs_nrates; i++) {
   1107 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
   1108 				break;
   1109 		}
   1110 		if (i == rs->rs_nrates)
   1111 			return EINVAL;
   1112 	}
   1113 	if (ic->ic_fixed_rate != i) {
   1114 		ic->ic_fixed_rate = i;
   1115 		error = ENETRESET;
   1116 	}
   1117 
   1118 	if ((ime->ifm_media & IFM_IEEE80211_ADHOC) &&
   1119 	    (ime->ifm_media & IFM_FLAG0))
   1120 		newmode = IEEE80211_M_AHDEMO;
   1121 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC)
   1122 		newmode = IEEE80211_M_IBSS;
   1123 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
   1124 		newmode = IEEE80211_M_HOSTAP;
   1125 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
   1126 		newmode = IEEE80211_M_MONITOR;
   1127 	else
   1128 		newmode = IEEE80211_M_STA;
   1129 	if (ic->ic_opmode != newmode) {
   1130 		ic->ic_opmode = newmode;
   1131 		error = ENETRESET;
   1132 	}
   1133 	if (error == ENETRESET) {
   1134 		if (sc->sc_enabled)
   1135 			error = wi_init(ifp);
   1136 		else
   1137 			error = 0;
   1138 	}
   1139 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
   1140 
   1141 	return error;
   1142 }
   1143 
   1144 static void
   1145 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1146 {
   1147 	struct wi_softc *sc = ifp->if_softc;
   1148 	struct ieee80211com *ic = &sc->sc_ic;
   1149 	u_int16_t val;
   1150 	int rate, len;
   1151 
   1152 	if (sc->sc_enabled == 0) {
   1153 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   1154 		imr->ifm_status = 0;
   1155 		return;
   1156 	}
   1157 
   1158 	imr->ifm_status = IFM_AVALID;
   1159 	imr->ifm_active = IFM_IEEE80211;
   1160 	if (ic->ic_state == IEEE80211_S_RUN &&
   1161 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
   1162 		imr->ifm_status |= IFM_ACTIVE;
   1163 	len = sizeof(val);
   1164 	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
   1165 		rate = 0;
   1166 	else {
   1167 		/* convert to 802.11 rate */
   1168 		val = le16toh(val);
   1169 		rate = val * 2;
   1170 		if (sc->sc_firmware_type == WI_LUCENT) {
   1171 			if (rate == 10)
   1172 				rate = 11;	/* 5.5Mbps */
   1173 		} else {
   1174 			if (rate == 4*2)
   1175 				rate = 11;	/* 5.5Mbps */
   1176 			else if (rate == 8*2)
   1177 				rate = 22;	/* 11Mbps */
   1178 		}
   1179 	}
   1180 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
   1181 	switch (ic->ic_opmode) {
   1182 	case IEEE80211_M_STA:
   1183 		break;
   1184 	case IEEE80211_M_IBSS:
   1185 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
   1186 		break;
   1187 	case IEEE80211_M_AHDEMO:
   1188 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   1189 		break;
   1190 	case IEEE80211_M_HOSTAP:
   1191 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
   1192 		break;
   1193 	case IEEE80211_M_MONITOR:
   1194 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
   1195 		break;
   1196 	}
   1197 }
   1198 
   1199 static void
   1200 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
   1201 {
   1202 	struct ieee80211com *ic = &sc->sc_ic;
   1203 	struct ieee80211_node *ni = ic->ic_bss;
   1204 	struct ifnet *ifp = &ic->ic_if;
   1205 
   1206 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
   1207 		return;
   1208 
   1209 	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
   1210 	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
   1211 
   1212 	/* In promiscuous mode, the BSSID field is not a reliable
   1213 	 * indicator of the firmware's BSSID. Damp spurious
   1214 	 * change-of-BSSID indications.
   1215 	 */
   1216 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   1217 	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
   1218 		return;
   1219 
   1220 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1221 }
   1222 
   1223 static void
   1224 wi_rx_intr(struct wi_softc *sc)
   1225 {
   1226 	struct ieee80211com *ic = &sc->sc_ic;
   1227 	struct ifnet *ifp = &ic->ic_if;
   1228 	struct ieee80211_node *ni;
   1229 	struct wi_frame frmhdr;
   1230 	struct mbuf *m;
   1231 	struct ieee80211_frame *wh;
   1232 	int fid, len, off, rssi;
   1233 	u_int8_t dir;
   1234 	u_int16_t status;
   1235 	u_int32_t rstamp;
   1236 
   1237 	fid = CSR_READ_2(sc, WI_RX_FID);
   1238 
   1239 	/* First read in the frame header */
   1240 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
   1241 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1242 		ifp->if_ierrors++;
   1243 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
   1244 		return;
   1245 	}
   1246 
   1247 	if (IFF_DUMPPKTS(ifp))
   1248 		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
   1249 
   1250 	/*
   1251 	 * Drop undecryptable or packets with receive errors here
   1252 	 */
   1253 	status = le16toh(frmhdr.wi_status);
   1254 	if (status & WI_STAT_ERRSTAT) {
   1255 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1256 		ifp->if_ierrors++;
   1257 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
   1258 		return;
   1259 	}
   1260 	rssi = frmhdr.wi_rx_signal;
   1261 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
   1262 	    le16toh(frmhdr.wi_rx_tstamp1);
   1263 
   1264 	len = le16toh(frmhdr.wi_dat_len);
   1265 	off = ALIGN(sizeof(struct ieee80211_frame));
   1266 
   1267 	/* Sometimes the PRISM2.x returns bogusly large frames. Except
   1268 	 * in monitor mode, just throw them away.
   1269 	 */
   1270 	if (off + len > MCLBYTES) {
   1271 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1272 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1273 			ifp->if_ierrors++;
   1274 			DPRINTF(("wi_rx_intr: oversized packet\n"));
   1275 			return;
   1276 		} else
   1277 			len = 0;
   1278 	}
   1279 
   1280 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1281 	if (m == NULL) {
   1282 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1283 		ifp->if_ierrors++;
   1284 		DPRINTF(("wi_rx_intr: MGET failed\n"));
   1285 		return;
   1286 	}
   1287 	if (off + len > MHLEN) {
   1288 		MCLGET(m, M_DONTWAIT);
   1289 		if ((m->m_flags & M_EXT) == 0) {
   1290 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1291 			m_freem(m);
   1292 			ifp->if_ierrors++;
   1293 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
   1294 			return;
   1295 		}
   1296 	}
   1297 
   1298 	m->m_data += off - sizeof(struct ieee80211_frame);
   1299 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
   1300 	wi_read_bap(sc, fid, sizeof(frmhdr),
   1301 	    m->m_data + sizeof(struct ieee80211_frame), len);
   1302 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
   1303 	m->m_pkthdr.rcvif = ifp;
   1304 
   1305 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1306 
   1307 #if NBPFILTER > 0
   1308 	if (sc->sc_drvbpf) {
   1309 		struct mbuf mb;
   1310 		struct wi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1311 
   1312 		tap->wr_rate = frmhdr.wi_rx_rate / 5;
   1313 		tap->wr_antsignal = WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_signal);
   1314 		tap->wr_antnoise = WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_silence);
   1315 
   1316 		tap->wr_chan_freq = ic->ic_bss->ni_chan->ic_freq;
   1317 		if (frmhdr.wi_status & WI_STAT_PCF)
   1318 			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
   1319 
   1320 		M_COPY_PKTHDR(&mb, m);
   1321 		mb.m_data = (caddr_t)tap;
   1322 		mb.m_len = tap->wr_ihdr.it_len;
   1323 		mb.m_next = m;
   1324 		mb.m_pkthdr.len += mb.m_len;
   1325 		bpf_mtap(sc->sc_drvbpf, &mb);
   1326 	}
   1327 #endif
   1328 	wh = mtod(m, struct ieee80211_frame *);
   1329 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1330 		/*
   1331 		 * WEP is decrypted by hardware. Clear WEP bit
   1332 		 * header for ieee80211_input().
   1333 		 */
   1334 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1335 	}
   1336 
   1337 	/* synchronize driver's BSSID with firmware's BSSID */
   1338 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
   1339 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
   1340 		wi_sync_bssid(sc, wh->i_addr3);
   1341 
   1342 	ni = ieee80211_find_rxnode(ic, wh);
   1343 
   1344 	ieee80211_input(ifp, m, ni, rssi, rstamp);
   1345 
   1346 	/*
   1347 	 * The frame may have caused the node to be marked for
   1348 	 * reclamation (e.g. in response to a DEAUTH message)
   1349 	 * so use free_node here instead of unref_node.
   1350 	 */
   1351 	if (ni == ic->ic_bss)
   1352 		ieee80211_unref_node(&ni);
   1353 	else
   1354 		ieee80211_free_node(ic, ni);
   1355 }
   1356 
   1357 static void
   1358 wi_tx_ex_intr(struct wi_softc *sc)
   1359 {
   1360 	struct ieee80211com *ic = &sc->sc_ic;
   1361 	struct ifnet *ifp = &ic->ic_if;
   1362 	struct wi_frame frmhdr;
   1363 	int fid;
   1364 
   1365 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
   1366 	/* Read in the frame header */
   1367 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
   1368 		u_int16_t status = le16toh(frmhdr.wi_status);
   1369 
   1370 		/*
   1371 		 * Spontaneous station disconnects appear as xmit
   1372 		 * errors.  Don't announce them and/or count them
   1373 		 * as an output error.
   1374 		 */
   1375 		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
   1376 			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
   1377 				curtxeps = 0;
   1378 				printf("%s: tx failed", sc->sc_dev.dv_xname);
   1379 				if (status & WI_TXSTAT_RET_ERR)
   1380 					printf(", retry limit exceeded");
   1381 				if (status & WI_TXSTAT_AGED_ERR)
   1382 					printf(", max transmit lifetime exceeded");
   1383 				if (status & WI_TXSTAT_DISCONNECT)
   1384 					printf(", port disconnected");
   1385 				if (status & WI_TXSTAT_FORM_ERR)
   1386 					printf(", invalid format (data len %u src %s)",
   1387 						le16toh(frmhdr.wi_dat_len),
   1388 						ether_sprintf(frmhdr.wi_ehdr.ether_shost));
   1389 				if (status & ~0xf)
   1390 					printf(", status=0x%x", status);
   1391 				printf("\n");
   1392 			}
   1393 			ifp->if_oerrors++;
   1394 		} else {
   1395 			DPRINTF(("port disconnected\n"));
   1396 			ifp->if_collisions++;	/* XXX */
   1397 		}
   1398 	} else
   1399 		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
   1400 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
   1401 }
   1402 
   1403 static void
   1404 wi_tx_intr(struct wi_softc *sc)
   1405 {
   1406 	struct ieee80211com *ic = &sc->sc_ic;
   1407 	struct ifnet *ifp = &ic->ic_if;
   1408 	int fid, cur;
   1409 
   1410 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
   1411 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   1412 
   1413 	cur = sc->sc_txcur;
   1414 	if (sc->sc_txd[cur].d_fid != fid) {
   1415 		printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
   1416 		    sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
   1417 		    sc->sc_txnext);
   1418 		return;
   1419 	}
   1420 	sc->sc_tx_timer = 0;
   1421 	sc->sc_txd[cur].d_len = 0;
   1422 	sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
   1423 	if (sc->sc_txd[cur].d_len == 0)
   1424 		ifp->if_flags &= ~IFF_OACTIVE;
   1425 	else {
   1426 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
   1427 		    0, 0)) {
   1428 			printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
   1429 			sc->sc_txd[cur].d_len = 0;
   1430 		} else {
   1431 			sc->sc_tx_timer = 5;
   1432 			ifp->if_timer = 1;
   1433 		}
   1434 	}
   1435 }
   1436 
   1437 static void
   1438 wi_info_intr(struct wi_softc *sc)
   1439 {
   1440 	struct ieee80211com *ic = &sc->sc_ic;
   1441 	struct ifnet *ifp = &ic->ic_if;
   1442 	int i, fid, len, off;
   1443 	u_int16_t ltbuf[2];
   1444 	u_int16_t stat;
   1445 	u_int32_t *ptr;
   1446 
   1447 	fid = CSR_READ_2(sc, WI_INFO_FID);
   1448 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
   1449 
   1450 	switch (le16toh(ltbuf[1])) {
   1451 
   1452 	case WI_INFO_LINK_STAT:
   1453 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
   1454 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
   1455 		switch (le16toh(stat)) {
   1456 		case CONNECTED:
   1457 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1458 			if (ic->ic_state == IEEE80211_S_RUN &&
   1459 			    ic->ic_opmode != IEEE80211_M_IBSS)
   1460 				break;
   1461 			/* FALLTHROUGH */
   1462 		case AP_CHANGE:
   1463 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1464 			break;
   1465 		case AP_IN_RANGE:
   1466 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1467 			break;
   1468 		case AP_OUT_OF_RANGE:
   1469 			if (sc->sc_firmware_type == WI_SYMBOL &&
   1470 			    sc->sc_scan_timer > 0) {
   1471 				if (wi_cmd(sc, WI_CMD_INQUIRE,
   1472 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
   1473 					sc->sc_scan_timer = 0;
   1474 				break;
   1475 			}
   1476 			if (ic->ic_opmode == IEEE80211_M_STA)
   1477 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
   1478 			break;
   1479 		case DISCONNECTED:
   1480 		case ASSOC_FAILED:
   1481 			if (ic->ic_opmode == IEEE80211_M_STA)
   1482 				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1483 			break;
   1484 		}
   1485 		break;
   1486 
   1487 	case WI_INFO_COUNTERS:
   1488 		/* some card versions have a larger stats structure */
   1489 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
   1490 		ptr = (u_int32_t *)&sc->sc_stats;
   1491 		off = sizeof(ltbuf);
   1492 		for (i = 0; i < len; i++, off += 2, ptr++) {
   1493 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
   1494 			stat = le16toh(stat);
   1495 #ifdef WI_HERMES_STATS_WAR
   1496 			if (stat & 0xf000)
   1497 				stat = ~stat;
   1498 #endif
   1499 			*ptr += stat;
   1500 		}
   1501 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
   1502 		    sc->sc_stats.wi_tx_multi_retries +
   1503 		    sc->sc_stats.wi_tx_retry_limit;
   1504 		break;
   1505 
   1506 	case WI_INFO_SCAN_RESULTS:
   1507 	case WI_INFO_HOST_SCAN_RESULTS:
   1508 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
   1509 		break;
   1510 
   1511 	default:
   1512 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
   1513 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
   1514 		break;
   1515 	}
   1516 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
   1517 }
   1518 
   1519 static int
   1520 wi_write_multi(struct wi_softc *sc)
   1521 {
   1522 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1523 	int n;
   1524 	struct wi_mcast mlist;
   1525 	struct ether_multi *enm;
   1526 	struct ether_multistep estep;
   1527 
   1528 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1529 allmulti:
   1530 		ifp->if_flags |= IFF_ALLMULTI;
   1531 		memset(&mlist, 0, sizeof(mlist));
   1532 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1533 		    sizeof(mlist));
   1534 	}
   1535 
   1536 	n = 0;
   1537 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1538 	while (enm != NULL) {
   1539 		/* Punt on ranges or too many multicast addresses. */
   1540 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
   1541 		    n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
   1542 			goto allmulti;
   1543 
   1544 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
   1545 		n++;
   1546 		ETHER_NEXT_MULTI(estep, enm);
   1547 	}
   1548 	ifp->if_flags &= ~IFF_ALLMULTI;
   1549 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1550 	    IEEE80211_ADDR_LEN * n);
   1551 }
   1552 
   1553 
   1554 static void
   1555 wi_read_nicid(struct wi_softc *sc)
   1556 {
   1557 	struct wi_card_ident *id;
   1558 	char *p;
   1559 	int len;
   1560 	u_int16_t ver[4];
   1561 
   1562 	/* getting chip identity */
   1563 	memset(ver, 0, sizeof(ver));
   1564 	len = sizeof(ver);
   1565 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
   1566 	printf("%s: using ", sc->sc_dev.dv_xname);
   1567 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1568 
   1569 	sc->sc_firmware_type = WI_NOTYPE;
   1570 	for (id = wi_card_ident; id->card_name != NULL; id++) {
   1571 		if (le16toh(ver[0]) == id->card_id) {
   1572 			printf("%s", id->card_name);
   1573 			sc->sc_firmware_type = id->firm_type;
   1574 			break;
   1575 		}
   1576 	}
   1577 	if (sc->sc_firmware_type == WI_NOTYPE) {
   1578 		if (le16toh(ver[0]) & 0x8000) {
   1579 			printf("Unknown PRISM2 chip");
   1580 			sc->sc_firmware_type = WI_INTERSIL;
   1581 		} else {
   1582 			printf("Unknown Lucent chip");
   1583 			sc->sc_firmware_type = WI_LUCENT;
   1584 		}
   1585 	}
   1586 
   1587 	/* get primary firmware version (Only Prism chips) */
   1588 	if (sc->sc_firmware_type != WI_LUCENT) {
   1589 		memset(ver, 0, sizeof(ver));
   1590 		len = sizeof(ver);
   1591 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
   1592 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
   1593 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1594 	}
   1595 
   1596 	/* get station firmware version */
   1597 	memset(ver, 0, sizeof(ver));
   1598 	len = sizeof(ver);
   1599 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
   1600 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
   1601 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1602 	if (sc->sc_firmware_type == WI_INTERSIL &&
   1603 	    (sc->sc_sta_firmware_ver == 10102 ||
   1604 	     sc->sc_sta_firmware_ver == 20102)) {
   1605 		char ident[12];
   1606 		memset(ident, 0, sizeof(ident));
   1607 		len = sizeof(ident);
   1608 		/* value should be the format like "V2.00-11" */
   1609 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
   1610 		    *(p = (char *)ident) >= 'A' &&
   1611 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
   1612 			sc->sc_firmware_type = WI_SYMBOL;
   1613 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
   1614 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
   1615 			    (p[6] - '0') * 10 + (p[7] - '0');
   1616 		}
   1617 	}
   1618 
   1619 	printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
   1620 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
   1621 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
   1622 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
   1623 		printf("Primary (%u.%u.%u), ",
   1624 		    sc->sc_pri_firmware_ver / 10000,
   1625 		    (sc->sc_pri_firmware_ver % 10000) / 100,
   1626 		    sc->sc_pri_firmware_ver % 100);
   1627 	printf("Station (%u.%u.%u)\n",
   1628 	    sc->sc_sta_firmware_ver / 10000,
   1629 	    (sc->sc_sta_firmware_ver % 10000) / 100,
   1630 	    sc->sc_sta_firmware_ver % 100);
   1631 }
   1632 
   1633 static int
   1634 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
   1635 {
   1636 	struct wi_ssid ssid;
   1637 
   1638 	if (buflen > IEEE80211_NWID_LEN)
   1639 		return ENOBUFS;
   1640 	memset(&ssid, 0, sizeof(ssid));
   1641 	ssid.wi_len = htole16(buflen);
   1642 	memcpy(ssid.wi_ssid, buf, buflen);
   1643 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
   1644 }
   1645 
   1646 static int
   1647 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1648 {
   1649 	struct wi_softc *sc = ifp->if_softc;
   1650 	struct ieee80211com *ic = &sc->sc_ic;
   1651 	struct ifreq *ifr = (struct ifreq *)data;
   1652 	struct wi_req wreq;
   1653 	int len, n, error;
   1654 
   1655 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1656 	if (error)
   1657 		return error;
   1658 	len = (wreq.wi_len - 1) * 2;
   1659 	if (len < sizeof(u_int16_t))
   1660 		return ENOSPC;
   1661 	if (len > sizeof(wreq.wi_val))
   1662 		len = sizeof(wreq.wi_val);
   1663 
   1664 	switch (wreq.wi_type) {
   1665 
   1666 	case WI_RID_IFACE_STATS:
   1667 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
   1668 		if (len < sizeof(sc->sc_stats))
   1669 			error = ENOSPC;
   1670 		else
   1671 			len = sizeof(sc->sc_stats);
   1672 		break;
   1673 
   1674 	case WI_RID_ENCRYPTION:
   1675 	case WI_RID_TX_CRYPT_KEY:
   1676 	case WI_RID_DEFLT_CRYPT_KEYS:
   1677 	case WI_RID_TX_RATE:
   1678 		return ieee80211_cfgget(ifp, cmd, data);
   1679 
   1680 	case WI_RID_MICROWAVE_OVEN:
   1681 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
   1682 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1683 			    &len);
   1684 			break;
   1685 		}
   1686 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
   1687 		len = sizeof(u_int16_t);
   1688 		break;
   1689 
   1690 	case WI_RID_DBM_ADJUST:
   1691 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
   1692 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1693 			    &len);
   1694 			break;
   1695 		}
   1696 		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
   1697 		len = sizeof(u_int16_t);
   1698 		break;
   1699 
   1700 	case WI_RID_ROAMING_MODE:
   1701 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
   1702 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1703 			    &len);
   1704 			break;
   1705 		}
   1706 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
   1707 		len = sizeof(u_int16_t);
   1708 		break;
   1709 
   1710 	case WI_RID_SYSTEM_SCALE:
   1711 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
   1712 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1713 			    &len);
   1714 			break;
   1715 		}
   1716 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
   1717 		len = sizeof(u_int16_t);
   1718 		break;
   1719 
   1720 	case WI_RID_FRAG_THRESH:
   1721 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
   1722 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1723 			    &len);
   1724 			break;
   1725 		}
   1726 		wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1727 		len = sizeof(u_int16_t);
   1728 		break;
   1729 
   1730 	case WI_RID_READ_APS:
   1731 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1732 			return ieee80211_cfgget(ifp, cmd, data);
   1733 		if (sc->sc_scan_timer > 0) {
   1734 			error = EINPROGRESS;
   1735 			break;
   1736 		}
   1737 		n = sc->sc_naps;
   1738 		if (len < sizeof(n)) {
   1739 			error = ENOSPC;
   1740 			break;
   1741 		}
   1742 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
   1743 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
   1744 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
   1745 		memcpy(wreq.wi_val, &n, sizeof(n));
   1746 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
   1747 		    sizeof(struct wi_apinfo) * n);
   1748 		break;
   1749 
   1750 	default:
   1751 		if (sc->sc_enabled) {
   1752 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1753 			    &len);
   1754 			break;
   1755 		}
   1756 		switch (wreq.wi_type) {
   1757 		case WI_RID_MAX_DATALEN:
   1758 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
   1759 			len = sizeof(u_int16_t);
   1760 			break;
   1761 		case WI_RID_FRAG_THRESH:
   1762 			wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1763 			len = sizeof(u_int16_t);
   1764 			break;
   1765 		case WI_RID_RTS_THRESH:
   1766 			wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
   1767 			len = sizeof(u_int16_t);
   1768 			break;
   1769 		case WI_RID_CNFAUTHMODE:
   1770 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
   1771 			len = sizeof(u_int16_t);
   1772 			break;
   1773 		case WI_RID_NODENAME:
   1774 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
   1775 				error = ENOSPC;
   1776 				break;
   1777 			}
   1778 			len = sc->sc_nodelen + sizeof(u_int16_t);
   1779 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
   1780 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
   1781 			    sc->sc_nodelen);
   1782 			break;
   1783 		default:
   1784 			return ieee80211_cfgget(ifp, cmd, data);
   1785 		}
   1786 		break;
   1787 	}
   1788 	if (error)
   1789 		return error;
   1790 	wreq.wi_len = (len + 1) / 2 + 1;
   1791 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
   1792 }
   1793 
   1794 static int
   1795 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1796 {
   1797 	struct wi_softc *sc = ifp->if_softc;
   1798 	struct ieee80211com *ic = &sc->sc_ic;
   1799 	struct ifreq *ifr = (struct ifreq *)data;
   1800 	struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   1801 	struct wi_req wreq;
   1802 	struct mbuf *m;
   1803 	int i, len, error;
   1804 
   1805 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1806 	if (error)
   1807 		return error;
   1808 	len = (wreq.wi_len - 1) * 2;
   1809 	switch (wreq.wi_type) {
   1810 	case WI_RID_DBM_ADJUST:
   1811 		return ENODEV;
   1812 
   1813 	case WI_RID_NODENAME:
   1814 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
   1815 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
   1816 			error = ENOSPC;
   1817 			break;
   1818 		}
   1819 		if (sc->sc_enabled) {
   1820 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1821 			    len);
   1822 			if (error)
   1823 				break;
   1824 		}
   1825 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
   1826 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
   1827 		break;
   1828 
   1829 	case WI_RID_MICROWAVE_OVEN:
   1830 	case WI_RID_ROAMING_MODE:
   1831 	case WI_RID_SYSTEM_SCALE:
   1832 	case WI_RID_FRAG_THRESH:
   1833 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
   1834 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
   1835 			break;
   1836 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
   1837 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
   1838 			break;
   1839 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
   1840 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
   1841 			break;
   1842 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
   1843 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
   1844 			break;
   1845 		/* FALLTHROUGH */
   1846 	case WI_RID_RTS_THRESH:
   1847 	case WI_RID_CNFAUTHMODE:
   1848 	case WI_RID_MAX_DATALEN:
   1849 		if (sc->sc_enabled) {
   1850 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1851 			    sizeof(u_int16_t));
   1852 			if (error)
   1853 				break;
   1854 		}
   1855 		switch (wreq.wi_type) {
   1856 		case WI_RID_FRAG_THRESH:
   1857 			sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
   1858 			break;
   1859 		case WI_RID_RTS_THRESH:
   1860 			sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
   1861 			break;
   1862 		case WI_RID_MICROWAVE_OVEN:
   1863 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
   1864 			break;
   1865 		case WI_RID_ROAMING_MODE:
   1866 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
   1867 			break;
   1868 		case WI_RID_SYSTEM_SCALE:
   1869 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
   1870 			break;
   1871 		case WI_RID_CNFAUTHMODE:
   1872 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
   1873 			break;
   1874 		case WI_RID_MAX_DATALEN:
   1875 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
   1876 			break;
   1877 		}
   1878 		break;
   1879 
   1880 	case WI_RID_TX_RATE:
   1881 		switch (le16toh(wreq.wi_val[0])) {
   1882 		case 3:
   1883 			ic->ic_fixed_rate = -1;
   1884 			break;
   1885 		default:
   1886 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   1887 				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
   1888 				    / 2 == le16toh(wreq.wi_val[0]))
   1889 					break;
   1890 			}
   1891 			if (i == IEEE80211_RATE_SIZE)
   1892 				return EINVAL;
   1893 			ic->ic_fixed_rate = i;
   1894 		}
   1895 		if (sc->sc_enabled)
   1896 			error = wi_cfg_txrate(sc);
   1897 		break;
   1898 
   1899 	case WI_RID_SCAN_APS:
   1900 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
   1901 			error = wi_scan_ap(sc, 0x3fff, 0x000f);
   1902 		break;
   1903 
   1904 	case WI_RID_MGMT_XMIT:
   1905 		if (!sc->sc_enabled) {
   1906 			error = ENETDOWN;
   1907 			break;
   1908 		}
   1909 		if (ic->ic_mgtq.ifq_len > 5) {
   1910 			error = EAGAIN;
   1911 			break;
   1912 		}
   1913 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
   1914 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
   1915 		if (m == NULL) {
   1916 			error = ENOMEM;
   1917 			break;
   1918 		}
   1919 		IF_ENQUEUE(&ic->ic_mgtq, m);
   1920 		break;
   1921 
   1922 	default:
   1923 		if (sc->sc_enabled) {
   1924 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   1925 			    len);
   1926 			if (error)
   1927 				break;
   1928 		}
   1929 		error = ieee80211_cfgset(ifp, cmd, data);
   1930 		break;
   1931 	}
   1932 	return error;
   1933 }
   1934 
   1935 /* Rate is 0 for hardware auto-select, otherwise rate is
   1936  * 2, 4, 11, or 22 (units of 500Kbps).
   1937  */
   1938 static int
   1939 wi_write_txrate(struct wi_softc *sc, int rate)
   1940 {
   1941 	u_int16_t hwrate;
   1942 	int i;
   1943 
   1944 	rate = (rate & IEEE80211_RATE_VAL) / 2;
   1945 
   1946 	/* rate: 0, 1, 2, 5, 11 */
   1947 	switch (sc->sc_firmware_type) {
   1948 	case WI_LUCENT:
   1949 		switch (rate) {
   1950 		case 0:
   1951 			hwrate = 3;	/* auto */
   1952 			break;
   1953 		case 5:
   1954 			hwrate = 4;
   1955 			break;
   1956 		case 11:
   1957 			hwrate = 5;
   1958 			break;
   1959 		default:
   1960 			hwrate = rate;
   1961 			break;
   1962 		}
   1963 		break;
   1964 	default:
   1965 		/* Choose a bit according to this table.
   1966 		 *
   1967 		 * bit | data rate
   1968 		 * ----+-------------------
   1969 		 * 0   | 1Mbps
   1970 		 * 1   | 2Mbps
   1971 		 * 2   | 5.5Mbps
   1972 		 * 3   | 11Mbps
   1973 		 */
   1974 		for (i = 8; i > 0; i >>= 1) {
   1975 			if (rate >= i)
   1976 				break;
   1977 		}
   1978 		if (i == 0)
   1979 			hwrate = 0xf;	/* auto */
   1980 		else
   1981 			hwrate = i;
   1982 		break;
   1983 	}
   1984 
   1985 	if (sc->sc_tx_rate == hwrate)
   1986 		return 0;
   1987 
   1988 	if (sc->sc_if.if_flags & IFF_DEBUG)
   1989 		printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate,
   1990 		    hwrate, rate);
   1991 
   1992 	sc->sc_tx_rate = hwrate;
   1993 
   1994 	return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate);
   1995 }
   1996 
   1997 static int
   1998 wi_cfg_txrate(struct wi_softc *sc)
   1999 {
   2000 	struct ieee80211com *ic = &sc->sc_ic;
   2001 	struct ieee80211_rateset *rs;
   2002 	int rate;
   2003 
   2004 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   2005 
   2006 	sc->sc_tx_rate = 0; /* force write to RID */
   2007 
   2008 	if (ic->ic_fixed_rate < 0)
   2009 		rate = 0;	/* auto */
   2010 	else
   2011 		rate = rs->rs_rates[ic->ic_fixed_rate];
   2012 
   2013 	return wi_write_txrate(sc, rate);
   2014 }
   2015 
   2016 static int
   2017 wi_write_wep(struct wi_softc *sc)
   2018 {
   2019 	struct ieee80211com *ic = &sc->sc_ic;
   2020 	int error = 0;
   2021 	int i, keylen;
   2022 	u_int16_t val;
   2023 	struct wi_key wkey[IEEE80211_WEP_NKID];
   2024 
   2025 	switch (sc->sc_firmware_type) {
   2026 	case WI_LUCENT:
   2027 		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
   2028 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
   2029 		if (error)
   2030 			break;
   2031 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
   2032 		if (error)
   2033 			break;
   2034 		memset(wkey, 0, sizeof(wkey));
   2035 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2036 			keylen = ic->ic_nw_keys[i].wk_len;
   2037 			wkey[i].wi_keylen = htole16(keylen);
   2038 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
   2039 			    keylen);
   2040 		}
   2041 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
   2042 		    wkey, sizeof(wkey));
   2043 		break;
   2044 
   2045 	case WI_INTERSIL:
   2046 	case WI_SYMBOL:
   2047 		if (ic->ic_flags & IEEE80211_F_WEPON) {
   2048 			/*
   2049 			 * ONLY HWB3163 EVAL-CARD Firmware version
   2050 			 * less than 0.8 variant2
   2051 			 *
   2052 			 *   If promiscuous mode disable, Prism2 chip
   2053 			 *  does not work with WEP .
   2054 			 * It is under investigation for details.
   2055 			 * (ichiro (at) NetBSD.org)
   2056 			 */
   2057 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2058 			    sc->sc_sta_firmware_ver < 802 ) {
   2059 				/* firm ver < 0.8 variant 2 */
   2060 				wi_write_val(sc, WI_RID_PROMISC, 1);
   2061 			}
   2062 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2063 			    sc->sc_cnfauthmode);
   2064 			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
   2065 			/*
   2066 			 * Encryption firmware has a bug for HostAP mode.
   2067 			 */
   2068 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2069 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
   2070 				val |= HOST_ENCRYPT;
   2071 		} else {
   2072 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2073 			    IEEE80211_AUTH_OPEN);
   2074 			val = HOST_ENCRYPT | HOST_DECRYPT;
   2075 		}
   2076 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
   2077 		if (error)
   2078 			break;
   2079 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
   2080 		    ic->ic_wep_txkey);
   2081 		if (error)
   2082 			break;
   2083 		/*
   2084 		 * It seems that the firmware accept 104bit key only if
   2085 		 * all the keys have 104bit length.  We get the length of
   2086 		 * the transmit key and use it for all other keys.
   2087 		 * Perhaps we should use software WEP for such situation.
   2088 		 */
   2089 		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
   2090 		if (keylen > IEEE80211_WEP_KEYLEN)
   2091 			keylen = 13;	/* 104bit keys */
   2092 		else
   2093 			keylen = IEEE80211_WEP_KEYLEN;
   2094 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2095 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
   2096 			    ic->ic_nw_keys[i].wk_key, keylen);
   2097 			if (error)
   2098 				break;
   2099 		}
   2100 		break;
   2101 	}
   2102 	return error;
   2103 }
   2104 
   2105 /* Must be called at proper protection level! */
   2106 static int
   2107 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   2108 {
   2109 	int i, status;
   2110 
   2111 	/* wait for the busy bit to clear */
   2112 	for (i = 500; i > 0; i--) {	/* 5s */
   2113 		if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
   2114 			break;
   2115 		DELAY(10*1000);	/* 10 m sec */
   2116 	}
   2117 	if (i == 0) {
   2118 		printf("%s: wi_cmd: busy bit won't clear.\n",
   2119 		    sc->sc_dev.dv_xname);
   2120 		return(ETIMEDOUT);
   2121   	}
   2122 	CSR_WRITE_2(sc, WI_PARAM0, val0);
   2123 	CSR_WRITE_2(sc, WI_PARAM1, val1);
   2124 	CSR_WRITE_2(sc, WI_PARAM2, val2);
   2125 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
   2126 
   2127 	if (cmd == WI_CMD_INI) {
   2128 		/* XXX: should sleep here. */
   2129 		DELAY(100*1000);
   2130 	}
   2131 	/* wait for the cmd completed bit */
   2132 	for (i = 0; i < WI_TIMEOUT; i++) {
   2133 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
   2134 			break;
   2135 		DELAY(WI_DELAY);
   2136 	}
   2137 
   2138 	status = CSR_READ_2(sc, WI_STATUS);
   2139 
   2140 	/* Ack the command */
   2141 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
   2142 
   2143 	if (i == WI_TIMEOUT) {
   2144 		printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
   2145 		    sc->sc_dev.dv_xname, cmd, val0);
   2146 		return ETIMEDOUT;
   2147 	}
   2148 
   2149 	if (status & WI_STAT_CMD_RESULT) {
   2150 		printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
   2151 		    sc->sc_dev.dv_xname, cmd, val0);
   2152 		return EIO;
   2153 	}
   2154 	return 0;
   2155 }
   2156 
   2157 static int
   2158 wi_seek_bap(struct wi_softc *sc, int id, int off)
   2159 {
   2160 	int i, status;
   2161 
   2162 	CSR_WRITE_2(sc, WI_SEL0, id);
   2163 	CSR_WRITE_2(sc, WI_OFF0, off);
   2164 
   2165 	for (i = 0; ; i++) {
   2166 		status = CSR_READ_2(sc, WI_OFF0);
   2167 		if ((status & WI_OFF_BUSY) == 0)
   2168 			break;
   2169 		if (i == WI_TIMEOUT) {
   2170 			printf("%s: timeout in wi_seek to %x/%x\n",
   2171 			    sc->sc_dev.dv_xname, id, off);
   2172 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2173 			return ETIMEDOUT;
   2174 		}
   2175 		DELAY(1);
   2176 	}
   2177 	if (status & WI_OFF_ERR) {
   2178 		printf("%s: failed in wi_seek to %x/%x\n",
   2179 		    sc->sc_dev.dv_xname, id, off);
   2180 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2181 		return EIO;
   2182 	}
   2183 	sc->sc_bap_id = id;
   2184 	sc->sc_bap_off = off;
   2185 	return 0;
   2186 }
   2187 
   2188 static int
   2189 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2190 {
   2191 	int error, cnt;
   2192 
   2193 	if (buflen == 0)
   2194 		return 0;
   2195 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2196 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2197 			return error;
   2198 	}
   2199 	cnt = (buflen + 1) / 2;
   2200 	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2201 	sc->sc_bap_off += cnt * 2;
   2202 	return 0;
   2203 }
   2204 
   2205 static int
   2206 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2207 {
   2208 	int error, cnt;
   2209 
   2210 	if (buflen == 0)
   2211 		return 0;
   2212 
   2213 #ifdef WI_HERMES_AUTOINC_WAR
   2214   again:
   2215 #endif
   2216 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2217 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2218 			return error;
   2219 	}
   2220 	cnt = (buflen + 1) / 2;
   2221 	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2222 	sc->sc_bap_off += cnt * 2;
   2223 
   2224 #ifdef WI_HERMES_AUTOINC_WAR
   2225 	/*
   2226 	 * According to the comments in the HCF Light code, there is a bug
   2227 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
   2228 	 * where the chip's internal autoincrement counter gets thrown off
   2229 	 * during data writes:  the autoincrement is missed, causing one
   2230 	 * data word to be overwritten and subsequent words to be written to
   2231 	 * the wrong memory locations. The end result is that we could end
   2232 	 * up transmitting bogus frames without realizing it. The workaround
   2233 	 * for this is to write a couple of extra guard words after the end
   2234 	 * of the transfer, then attempt to read then back. If we fail to
   2235 	 * locate the guard words where we expect them, we preform the
   2236 	 * transfer over again.
   2237 	 */
   2238 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
   2239 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
   2240 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
   2241 		wi_seek_bap(sc, id, sc->sc_bap_off);
   2242 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2243 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
   2244 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
   2245 			printf("%s: detect auto increment bug, try again\n",
   2246 			    sc->sc_dev.dv_xname);
   2247 			goto again;
   2248 		}
   2249 	}
   2250 #endif
   2251 	return 0;
   2252 }
   2253 
   2254 static int
   2255 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
   2256 {
   2257 	int error, len;
   2258 	struct mbuf *m;
   2259 
   2260 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
   2261 		if (m->m_len == 0)
   2262 			continue;
   2263 
   2264 		len = min(m->m_len, totlen);
   2265 
   2266 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
   2267 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
   2268 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
   2269 			    totlen);
   2270 		}
   2271 
   2272 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
   2273 			return error;
   2274 
   2275 		off += m->m_len;
   2276 		totlen -= len;
   2277 	}
   2278 	return 0;
   2279 }
   2280 
   2281 static int
   2282 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
   2283 {
   2284 	int i;
   2285 
   2286 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
   2287 		printf("%s: failed to allocate %d bytes on NIC\n",
   2288 		    sc->sc_dev.dv_xname, len);
   2289 		return ENOMEM;
   2290 	}
   2291 
   2292 	for (i = 0; i < WI_TIMEOUT; i++) {
   2293 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
   2294 			break;
   2295 		if (i == WI_TIMEOUT) {
   2296 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
   2297 			return ETIMEDOUT;
   2298 		}
   2299 		DELAY(1);
   2300 	}
   2301 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
   2302 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   2303 	return 0;
   2304 }
   2305 
   2306 static int
   2307 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
   2308 {
   2309 	int error, len;
   2310 	u_int16_t ltbuf[2];
   2311 
   2312 	/* Tell the NIC to enter record read mode. */
   2313 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
   2314 	if (error)
   2315 		return error;
   2316 
   2317 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2318 	if (error)
   2319 		return error;
   2320 
   2321 	if (le16toh(ltbuf[1]) != rid) {
   2322 		printf("%s: record read mismatch, rid=%x, got=%x\n",
   2323 		    sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
   2324 		return EIO;
   2325 	}
   2326 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
   2327 	if (*buflenp < len) {
   2328 		printf("%s: record buffer is too small, "
   2329 		    "rid=%x, size=%d, len=%d\n",
   2330 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
   2331 		return ENOSPC;
   2332 	}
   2333 	*buflenp = len;
   2334 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
   2335 }
   2336 
   2337 static int
   2338 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
   2339 {
   2340 	int error;
   2341 	u_int16_t ltbuf[2];
   2342 
   2343 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
   2344 	ltbuf[1] = htole16(rid);
   2345 
   2346 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2347 	if (error)
   2348 		return error;
   2349 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
   2350 	if (error)
   2351 		return error;
   2352 
   2353 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
   2354 }
   2355 
   2356 static int
   2357 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2358 {
   2359 	struct wi_softc *sc = ic->ic_softc;
   2360 	struct ieee80211_node *ni = ic->ic_bss;
   2361 	int buflen;
   2362 	u_int16_t val;
   2363 	struct wi_ssid ssid;
   2364 	struct wi_macaddr bssid, old_bssid;
   2365 	enum ieee80211_state ostate;
   2366 #ifdef WI_DEBUG
   2367 	static const char *stname[] =
   2368 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   2369 #endif /* WI_DEBUG */
   2370 
   2371 	ostate = ic->ic_state;
   2372 	DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
   2373 
   2374 	switch (nstate) {
   2375 	case IEEE80211_S_INIT:
   2376 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   2377 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2378 		return (*sc->sc_newstate)(ic, nstate, arg);
   2379 
   2380 	case IEEE80211_S_RUN:
   2381 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2382 		buflen = IEEE80211_ADDR_LEN;
   2383 		IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid);
   2384 		wi_read_rid(sc, WI_RID_CURRENT_BSSID, &bssid, &buflen);
   2385 		IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid);
   2386 		IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid);
   2387 		buflen = sizeof(val);
   2388 		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
   2389 		if (!isset(ic->ic_chan_avail, le16toh(val)))
   2390 			panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname,
   2391 			    le16toh(val));
   2392 		ni->ni_chan = &ic->ic_channels[le16toh(val)];
   2393 
   2394 		if (IEEE80211_ADDR_EQ(old_bssid.wi_mac_addr, ni->ni_bssid))
   2395 			sc->sc_false_syns++;
   2396 		else
   2397 			sc->sc_false_syns = 0;
   2398 
   2399 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   2400 			ni->ni_esslen = ic->ic_des_esslen;
   2401 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
   2402 			ni->ni_rates = ic->ic_sup_rates[
   2403 			    ieee80211_chan2mode(ic, ni->ni_chan)];
   2404 			ni->ni_intval = ic->ic_lintval;
   2405 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
   2406 			if (ic->ic_flags & IEEE80211_F_WEPON)
   2407 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2408 		} else {
   2409 			buflen = sizeof(ssid);
   2410 			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
   2411 			ni->ni_esslen = le16toh(ssid.wi_len);
   2412 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
   2413 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
   2414 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
   2415 		}
   2416 		break;
   2417 
   2418 	case IEEE80211_S_SCAN:
   2419 	case IEEE80211_S_AUTH:
   2420 	case IEEE80211_S_ASSOC:
   2421 		break;
   2422 	}
   2423 
   2424 	ic->ic_state = nstate;
   2425 	/* skip standard ieee80211 handling */
   2426 	return 0;
   2427 }
   2428 
   2429 static int
   2430 wi_set_tim(struct ieee80211com *ic, int aid, int which)
   2431 {
   2432 	struct wi_softc *sc = ic->ic_softc;
   2433 
   2434 	aid &= ~0xc000;
   2435 	if (which)
   2436 		aid |= 0x8000;
   2437 
   2438 	return wi_write_val(sc, WI_RID_SET_TIM, aid);
   2439 }
   2440 
   2441 static int
   2442 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
   2443 {
   2444 	int error = 0;
   2445 	u_int16_t val[2];
   2446 
   2447 	if (!sc->sc_enabled)
   2448 		return ENXIO;
   2449 	switch (sc->sc_firmware_type) {
   2450 	case WI_LUCENT:
   2451 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   2452 		break;
   2453 	case WI_INTERSIL:
   2454 		val[0] = htole16(chanmask);	/* channel */
   2455 		val[1] = htole16(txrate);	/* tx rate */
   2456 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
   2457 		break;
   2458 	case WI_SYMBOL:
   2459 		/*
   2460 		 * XXX only supported on 3.x ?
   2461 		 */
   2462 		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
   2463 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
   2464 		    val, sizeof(val[0]));
   2465 		break;
   2466 	}
   2467 	if (error == 0) {
   2468 		sc->sc_scan_timer = WI_SCAN_WAIT;
   2469 		sc->sc_ic.ic_if.if_timer = 1;
   2470 		DPRINTF(("wi_scan_ap: start scanning, "
   2471 			"chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
   2472 	}
   2473 	return error;
   2474 }
   2475 
   2476 static void
   2477 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
   2478 {
   2479 #define	N(a)	(sizeof (a) / sizeof (a[0]))
   2480 	int i, naps, off, szbuf;
   2481 	struct wi_scan_header ws_hdr;	/* Prism2 header */
   2482 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
   2483 	struct wi_apinfo *ap;
   2484 
   2485 	off = sizeof(u_int16_t) * 2;
   2486 	memset(&ws_hdr, 0, sizeof(ws_hdr));
   2487 	switch (sc->sc_firmware_type) {
   2488 	case WI_INTERSIL:
   2489 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
   2490 		off += sizeof(ws_hdr);
   2491 		szbuf = sizeof(struct wi_scan_data_p2);
   2492 		break;
   2493 	case WI_SYMBOL:
   2494 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
   2495 		break;
   2496 	case WI_LUCENT:
   2497 		szbuf = sizeof(struct wi_scan_data);
   2498 		break;
   2499 	default:
   2500 		printf("%s: wi_scan_result: unknown firmware type %u\n",
   2501 		    sc->sc_dev.dv_xname, sc->sc_firmware_type);
   2502 		naps = 0;
   2503 		goto done;
   2504 	}
   2505 	naps = (cnt * 2 + 2 - off) / szbuf;
   2506 	if (naps > N(sc->sc_aps))
   2507 		naps = N(sc->sc_aps);
   2508 	sc->sc_naps = naps;
   2509 	/* Read Data */
   2510 	ap = sc->sc_aps;
   2511 	memset(&ws_dat, 0, sizeof(ws_dat));
   2512 	for (i = 0; i < naps; i++, ap++) {
   2513 		wi_read_bap(sc, fid, off, &ws_dat,
   2514 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
   2515 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
   2516 		    ether_sprintf(ws_dat.wi_bssid)));
   2517 		off += szbuf;
   2518 		ap->scanreason = le16toh(ws_hdr.wi_reason);
   2519 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
   2520 		ap->channel = le16toh(ws_dat.wi_chid);
   2521 		ap->signal  = le16toh(ws_dat.wi_signal);
   2522 		ap->noise   = le16toh(ws_dat.wi_noise);
   2523 		ap->quality = ap->signal - ap->noise;
   2524 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
   2525 		ap->interval = le16toh(ws_dat.wi_interval);
   2526 		ap->rate    = le16toh(ws_dat.wi_rate);
   2527 		ap->namelen = le16toh(ws_dat.wi_namelen);
   2528 		if (ap->namelen > sizeof(ap->name))
   2529 			ap->namelen = sizeof(ap->name);
   2530 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
   2531 	}
   2532 done:
   2533 	/* Done scanning */
   2534 	sc->sc_scan_timer = 0;
   2535 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
   2536 #undef N
   2537 }
   2538 
   2539 static void
   2540 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
   2541 {
   2542 	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
   2543 	    ni	? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL
   2544 		: -1,
   2545 	    rssi);
   2546 	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
   2547 		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
   2548 		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
   2549 	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
   2550 		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
   2551 	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
   2552 		wh->wi_tx_rtry, wh->wi_tx_rate,
   2553 		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
   2554 	printf(" ehdr dst %s src %s type 0x%x\n",
   2555 		ether_sprintf(wh->wi_ehdr.ether_dhost),
   2556 		ether_sprintf(wh->wi_ehdr.ether_shost),
   2557 		wh->wi_ehdr.ether_type);
   2558 }
   2559