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