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