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