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