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