Home | History | Annotate | Line # | Download | only in ic
awi.c revision 1.19.2.4
      1  1.19.2.4      onoe /*	$NetBSD: awi.c,v 1.19.2.4 2000/07/21 18:55:58 onoe Exp $	*/
      2       1.3  sommerfe 
      3      1.19      onoe /*-
      4      1.19      onoe  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5       1.1  sommerfe  * All rights reserved.
      6       1.1  sommerfe  *
      7       1.1  sommerfe  * This code is derived from software contributed to The NetBSD Foundation
      8      1.19      onoe  * by Bill Sommerfeld
      9       1.1  sommerfe  *
     10       1.1  sommerfe  * Redistribution and use in source and binary forms, with or without
     11       1.1  sommerfe  * modification, are permitted provided that the following conditions
     12       1.1  sommerfe  * are met:
     13       1.1  sommerfe  * 1. Redistributions of source code must retain the above copyright
     14       1.1  sommerfe  *    notice, this list of conditions and the following disclaimer.
     15       1.1  sommerfe  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  sommerfe  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  sommerfe  *    documentation and/or other materials provided with the distribution.
     18       1.1  sommerfe  * 3. All advertising materials mentioning features or use of this software
     19       1.1  sommerfe  *    must display the following acknowledgement:
     20      1.19      onoe  *        This product includes software developed by the NetBSD
     21      1.19      onoe  *        Foundation, Inc. and its contributors.
     22       1.1  sommerfe  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  sommerfe  *    contributors may be used to endorse or promote products derived
     24       1.1  sommerfe  *    from this software without specific prior written permission.
     25       1.1  sommerfe  *
     26       1.1  sommerfe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  sommerfe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  sommerfe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  sommerfe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  sommerfe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  sommerfe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  sommerfe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  sommerfe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  sommerfe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  sommerfe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  sommerfe  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  sommerfe  */
     38      1.19      onoe /*
     39      1.19      onoe  * Driver for AMD 802.11 firmware.
     40      1.19      onoe  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     41      1.19      onoe  *
     42      1.19      onoe  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
     43      1.19      onoe  */
     44      1.19      onoe 
     45      1.19      onoe /*
     46      1.19      onoe  * todo:
     47      1.19      onoe  *	- flush tx queue on resynch.
     48      1.19      onoe  *	- clear oactive on "down".
     49      1.19      onoe  *	- rewrite copy-into-mbuf code
     50      1.19      onoe  *	- mgmt state machine gets stuck retransmitting assoc requests.
     51      1.19      onoe  *	- multicast filter.
     52      1.19      onoe  *	- fix device reset so it's more likely to work
     53      1.19      onoe  *	- show status goo through ifmedia.
     54      1.19      onoe  *
     55      1.19      onoe  * more todo:
     56      1.19      onoe  *	- deal with more 802.11 frames.
     57      1.19      onoe  *		- send reassoc request
     58      1.19      onoe  *		- deal with reassoc response
     59      1.19      onoe  *		- send/deal with disassociation
     60      1.19      onoe  *	- deal with "full" access points (no room for me).
     61      1.19      onoe  *	- power save mode
     62      1.19      onoe  *
     63      1.19      onoe  * later:
     64      1.19      onoe  *	- SSID preferences
     65      1.19      onoe  *	- need ioctls for poking at the MIBs
     66      1.19      onoe  *	- implement ad-hoc mode (including bss creation).
     67      1.19      onoe  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
     68      1.19      onoe  *		(focus on inf. mode since that will be needed for ietf)
     69      1.19      onoe  *	- deal with DH vs. FH versions of the card
     70      1.19      onoe  *	- deal with faster cards (2mb/s)
     71      1.19      onoe  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
     72      1.19      onoe  *	- ifmedia revision.
     73      1.19      onoe  *	- common 802.11 mibish things.
     74      1.19      onoe  *	- common 802.11 media layer.
     75      1.19      onoe  */
     76      1.10      onoe 
     77       1.1  sommerfe /*
     78      1.10      onoe  * Driver for AMD 802.11 PCnetMobile firmware.
     79       1.1  sommerfe  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     80       1.1  sommerfe  *
     81      1.10      onoe  * The initial version of the driver was written by
     82      1.10      onoe  * Bill Sommerfeld <sommerfeld (at) netbsd.org>.
     83      1.10      onoe  * Then the driver module completely rewritten to support cards with DS phy
     84      1.10      onoe  * and to support adhoc mode by Atsushi Onoe <onoe (at) netbsd.org>
     85       1.1  sommerfe  */
     86       1.1  sommerfe 
     87       1.1  sommerfe #include "opt_inet.h"
     88      1.18      onoe #if defined(__FreeBSD__) && __FreeBSD__ >= 4
     89  1.19.2.1      onoe #define	NBPFILTER	1
     90  1.19.2.1      onoe #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
     91      1.10      onoe #include "bpf.h"
     92      1.10      onoe #define	NBPFILTER	NBPF
     93      1.10      onoe #else
     94      1.10      onoe #include "bpfilter.h"
     95      1.10      onoe #endif
     96       1.1  sommerfe 
     97       1.1  sommerfe #include <sys/param.h>
     98       1.1  sommerfe #include <sys/systm.h>
     99       1.1  sommerfe #include <sys/kernel.h>
    100       1.1  sommerfe #include <sys/mbuf.h>
    101      1.10      onoe #include <sys/malloc.h>
    102      1.10      onoe #include <sys/proc.h>
    103       1.1  sommerfe #include <sys/socket.h>
    104      1.10      onoe #include <sys/sockio.h>
    105       1.1  sommerfe #include <sys/errno.h>
    106       1.1  sommerfe #include <sys/syslog.h>
    107      1.10      onoe #if defined(__FreeBSD__) && __FreeBSD__ >= 4
    108      1.10      onoe #include <sys/bus.h>
    109      1.10      onoe #else
    110       1.1  sommerfe #include <sys/device.h>
    111      1.10      onoe #endif
    112       1.1  sommerfe 
    113       1.1  sommerfe #include <net/if.h>
    114       1.1  sommerfe #include <net/if_dl.h>
    115      1.10      onoe #ifdef __FreeBSD__
    116      1.10      onoe #include <net/ethernet.h>
    117      1.10      onoe #else
    118       1.1  sommerfe #include <net/if_ether.h>
    119      1.10      onoe #endif
    120       1.1  sommerfe #include <net/if_media.h>
    121      1.10      onoe #include <net/if_llc.h>
    122      1.10      onoe #include <net/if_ieee80211.h>
    123       1.1  sommerfe 
    124       1.1  sommerfe #ifdef INET
    125       1.1  sommerfe #include <netinet/in.h>
    126       1.1  sommerfe #include <netinet/in_systm.h>
    127       1.1  sommerfe #include <netinet/in_var.h>
    128       1.1  sommerfe #include <netinet/ip.h>
    129      1.10      onoe #ifdef __NetBSD__
    130       1.1  sommerfe #include <netinet/if_inarp.h>
    131      1.10      onoe #else
    132      1.10      onoe #include <netinet/if_ether.h>
    133      1.10      onoe #endif
    134       1.1  sommerfe #endif
    135       1.1  sommerfe 
    136       1.1  sommerfe #if NBPFILTER > 0
    137       1.1  sommerfe #include <net/bpf.h>
    138       1.1  sommerfe #include <net/bpfdesc.h>
    139       1.1  sommerfe #endif
    140       1.1  sommerfe 
    141       1.1  sommerfe #include <machine/cpu.h>
    142       1.1  sommerfe #include <machine/bus.h>
    143      1.10      onoe #ifdef __NetBSD__
    144       1.1  sommerfe #include <machine/intr.h>
    145      1.10      onoe #endif
    146      1.10      onoe #ifdef __FreeBSD__
    147      1.10      onoe #include <machine/clock.h>
    148      1.10      onoe #endif
    149       1.1  sommerfe 
    150      1.10      onoe #ifdef __NetBSD__
    151       1.1  sommerfe #include <dev/ic/am79c930reg.h>
    152       1.1  sommerfe #include <dev/ic/am79c930var.h>
    153       1.1  sommerfe #include <dev/ic/awireg.h>
    154       1.1  sommerfe #include <dev/ic/awivar.h>
    155      1.10      onoe #endif
    156      1.10      onoe #ifdef __FreeBSD__
    157      1.10      onoe #include <dev/awi/am79c930reg.h>
    158      1.10      onoe #include <dev/awi/am79c930var.h>
    159      1.10      onoe #include <dev/awi/awireg.h>
    160      1.10      onoe #include <dev/awi/awivar.h>
    161      1.10      onoe #endif
    162      1.10      onoe 
    163      1.10      onoe static int awi_ioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
    164      1.10      onoe #ifdef IFM_IEEE80211
    165      1.10      onoe static int awi_media_rate2opt __P((struct awi_softc *sc, int rate));
    166      1.10      onoe static int awi_media_opt2rate __P((struct awi_softc *sc, int opt));
    167      1.10      onoe static int awi_media_change __P((struct ifnet *ifp));
    168      1.10      onoe static void awi_media_status __P((struct ifnet *ifp, struct ifmediareq *imr));
    169      1.10      onoe #endif
    170      1.10      onoe static void awi_watchdog __P((struct ifnet *ifp));
    171      1.10      onoe static void awi_start __P((struct ifnet *ifp));
    172      1.10      onoe static void awi_txint __P((struct awi_softc *sc));
    173      1.10      onoe static struct mbuf * awi_fix_txhdr __P((struct awi_softc *sc, struct mbuf *m0));
    174      1.10      onoe static struct mbuf * awi_fix_rxhdr __P((struct awi_softc *sc, struct mbuf *m0));
    175      1.10      onoe static void awi_input __P((struct awi_softc *sc, struct mbuf *m, u_int32_t rxts, u_int8_t rssi));
    176      1.10      onoe static void awi_rxint __P((struct awi_softc *sc));
    177      1.18      onoe static struct mbuf * awi_devget __P((struct awi_softc *sc, u_int32_t off, u_int16_t len));
    178      1.10      onoe static int awi_init_hw __P((struct awi_softc *sc));
    179      1.10      onoe static int awi_init_mibs __P((struct awi_softc *sc));
    180      1.10      onoe static int awi_init_txrx __P((struct awi_softc *sc));
    181      1.10      onoe static void awi_stop_txrx __P((struct awi_softc *sc));
    182      1.10      onoe static int awi_start_scan __P((struct awi_softc *sc));
    183      1.10      onoe static int awi_next_scan __P((struct awi_softc *sc));
    184      1.10      onoe static void awi_stop_scan __P((struct awi_softc *sc));
    185      1.10      onoe static void awi_recv_beacon __P((struct awi_softc *sc, struct mbuf *m0, u_int32_t rxts, u_int8_t rssi));
    186      1.10      onoe static int awi_set_ss __P((struct awi_softc *sc));
    187      1.10      onoe static void awi_try_sync __P((struct awi_softc *sc));
    188      1.10      onoe static void awi_sync_done __P((struct awi_softc *sc));
    189      1.10      onoe static void awi_send_deauth __P((struct awi_softc *sc));
    190  1.19.2.1      onoe static void awi_send_auth __P((struct awi_softc *sc, int seq));
    191      1.10      onoe static void awi_recv_auth __P((struct awi_softc *sc, struct mbuf *m0));
    192      1.10      onoe static void awi_send_asreq __P((struct awi_softc *sc, int reassoc));
    193      1.10      onoe static void awi_recv_asresp __P((struct awi_softc *sc, struct mbuf *m0));
    194      1.10      onoe static int awi_mib __P((struct awi_softc *sc, u_int8_t cmd, u_int8_t mib));
    195      1.10      onoe static int awi_cmd_scan __P((struct awi_softc *sc));
    196      1.10      onoe static int awi_cmd __P((struct awi_softc *sc, u_int8_t cmd));
    197      1.10      onoe static void awi_cmd_done __P((struct awi_softc *sc));
    198      1.10      onoe static int awi_next_txd __P((struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t*ntxdp));
    199      1.10      onoe static int awi_lock __P((struct awi_softc *sc));
    200      1.10      onoe static void awi_unlock __P((struct awi_softc *sc));
    201      1.10      onoe static int awi_intr_lock __P((struct awi_softc *sc));
    202      1.10      onoe static void awi_intr_unlock __P((struct awi_softc *sc));
    203      1.10      onoe static int awi_cmd_wait __P((struct awi_softc *sc));
    204  1.19.2.1      onoe static void awi_print_essid __P((u_int8_t *essid));
    205      1.10      onoe 
    206      1.10      onoe #ifdef AWI_DEBUG
    207  1.19.2.1      onoe static void awi_dump_pkt __P((struct awi_softc *sc, struct mbuf *m, int rssi));
    208      1.10      onoe int awi_verbose = 0;
    209      1.10      onoe int awi_dump = 0;
    210      1.10      onoe #define	AWI_DUMP_MASK(fc0)  (1 << (((fc0) & IEEE80211_FC0_SUBTYPE_MASK) >> 4))
    211      1.10      onoe int awi_dump_mask = AWI_DUMP_MASK(IEEE80211_FC0_SUBTYPE_BEACON);
    212      1.10      onoe int awi_dump_hdr = 0;
    213      1.10      onoe int awi_dump_len = 28;
    214      1.10      onoe #endif
    215       1.1  sommerfe 
    216      1.10      onoe #if NBPFILTER > 0
    217      1.10      onoe #define	AWI_BPF_NORM	0
    218      1.10      onoe #define	AWI_BPF_RAW	1
    219      1.10      onoe #ifdef __FreeBSD__
    220      1.10      onoe #define	AWI_BPF_MTAP(sc, m, raw) do {					\
    221      1.10      onoe 	if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw))		\
    222      1.10      onoe 		bpf_mtap((sc)->sc_ifp, (m));				\
    223      1.10      onoe } while (0);
    224      1.10      onoe #else
    225      1.10      onoe #define	AWI_BPF_MTAP(sc, m, raw) do {					\
    226      1.10      onoe 	if ((sc)->sc_ifp->if_bpf && (sc)->sc_rawbpf == (raw))		\
    227      1.10      onoe 		bpf_mtap((sc)->sc_ifp->if_bpf, (m));			\
    228      1.10      onoe } while (0);
    229      1.10      onoe #endif
    230      1.10      onoe #else
    231      1.10      onoe #define	AWI_BPF_MTAP(sc, m, raw)
    232      1.10      onoe #endif
    233       1.1  sommerfe 
    234      1.10      onoe #ifndef llc_snap
    235      1.10      onoe #define llc_snap              llc_un.type_snap
    236      1.10      onoe #endif
    237       1.8  sommerfe 
    238      1.10      onoe #ifdef __FreeBSD__
    239      1.10      onoe #if __FreeBSD__ >= 4
    240      1.10      onoe devclass_t awi_devclass;
    241      1.10      onoe #endif
    242       1.1  sommerfe 
    243      1.10      onoe /* NetBSD compatible functions  */
    244      1.10      onoe static char * ether_sprintf __P((u_int8_t *));
    245       1.5  sommerfe 
    246      1.10      onoe static char *
    247      1.10      onoe ether_sprintf(enaddr)
    248      1.10      onoe 	u_int8_t *enaddr;
    249      1.10      onoe {
    250      1.10      onoe 	static char strbuf[18];
    251       1.1  sommerfe 
    252      1.10      onoe 	sprintf(strbuf, "%6D", enaddr, ":");
    253      1.10      onoe 	return strbuf;
    254      1.10      onoe }
    255      1.10      onoe #endif
    256       1.1  sommerfe 
    257      1.10      onoe int
    258      1.10      onoe awi_attach(sc)
    259       1.1  sommerfe 	struct awi_softc *sc;
    260       1.1  sommerfe {
    261       1.1  sommerfe 	struct ifnet *ifp = sc->sc_ifp;
    262      1.18      onoe 	int s;
    263      1.18      onoe 	int error;
    264      1.10      onoe #ifdef IFM_IEEE80211
    265      1.10      onoe 	int i;
    266      1.10      onoe 	u_int8_t *phy_rates;
    267      1.10      onoe 	int mword;
    268      1.10      onoe 	struct ifmediareq imr;
    269      1.10      onoe #endif
    270       1.1  sommerfe 
    271      1.10      onoe 	s = splnet();
    272      1.10      onoe 	/*
    273      1.10      onoe 	 * Even if we can sleep in initialization state,
    274      1.10      onoe 	 * all other processes (e.g. ifconfig) have to wait for
    275      1.10      onoe 	 * completion of attaching interface.
    276      1.10      onoe 	 */
    277      1.10      onoe 	sc->sc_busy = 1;
    278      1.10      onoe 	sc->sc_status = AWI_ST_INIT;
    279      1.10      onoe 	TAILQ_INIT(&sc->sc_scan);
    280      1.10      onoe 	error = awi_init_hw(sc);
    281      1.10      onoe 	if (error) {
    282      1.10      onoe 		sc->sc_invalid = 1;
    283      1.10      onoe 		splx(s);
    284      1.10      onoe 		return error;
    285      1.10      onoe 	}
    286      1.10      onoe 	error = awi_init_mibs(sc);
    287      1.10      onoe 	splx(s);
    288      1.10      onoe 	if (error) {
    289      1.10      onoe 		sc->sc_invalid = 1;
    290      1.10      onoe 		return error;
    291      1.10      onoe 	}
    292       1.1  sommerfe 
    293      1.10      onoe 	ifp->if_softc = sc;
    294      1.10      onoe 	ifp->if_start = awi_start;
    295      1.10      onoe 	ifp->if_ioctl = awi_ioctl;
    296      1.10      onoe 	ifp->if_watchdog = awi_watchdog;
    297      1.10      onoe 	ifp->if_mtu = ETHERMTU;
    298      1.10      onoe 	ifp->if_hdrlen = sizeof(struct ieee80211_frame) +
    299      1.10      onoe 	    sizeof(struct ether_header);
    300      1.10      onoe 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    301      1.10      onoe #ifdef IFF_NOTRAILERS
    302      1.10      onoe 	ifp->if_flags |= IFF_NOTRAILERS;
    303      1.10      onoe #endif
    304      1.10      onoe #ifdef __NetBSD__
    305      1.10      onoe 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    306      1.10      onoe #endif
    307      1.10      onoe #ifdef __FreeBSD__
    308      1.10      onoe 	ifp->if_output = ether_output;
    309      1.10      onoe 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
    310      1.10      onoe 	memcpy(sc->sc_ec.ac_enaddr, sc->sc_mib_addr.aMAC_Address,
    311      1.10      onoe 	    ETHER_ADDR_LEN);
    312      1.10      onoe #endif
    313       1.1  sommerfe 
    314      1.18      onoe 	printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
    315      1.10      onoe 	    sc->sc_dev.dv_xname,
    316      1.10      onoe 	    sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
    317      1.18      onoe 	    sc->sc_tx_rate / 10, sc->sc_banner);
    318      1.18      onoe 	printf("%s: address %s\n",
    319      1.18      onoe 	    sc->sc_dev.dv_xname,  ether_sprintf(sc->sc_mib_addr.aMAC_Address));
    320      1.10      onoe 	if_attach(ifp);
    321      1.10      onoe #ifdef __FreeBSD__
    322      1.10      onoe 	ether_ifattach(ifp);
    323      1.10      onoe #if NBPFILTER > 0
    324      1.10      onoe 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    325      1.10      onoe #endif
    326      1.10      onoe #else
    327      1.10      onoe 	ether_ifattach(ifp, sc->sc_mib_addr.aMAC_Address);
    328      1.10      onoe #if NBPFILTER > 0
    329      1.10      onoe 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    330      1.10      onoe #endif
    331      1.10      onoe #endif
    332       1.1  sommerfe 
    333      1.10      onoe #ifdef IFM_IEEE80211
    334      1.10      onoe 	ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
    335      1.10      onoe 	phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
    336      1.10      onoe 	for (i = 0; i < phy_rates[1]; i++) {
    337      1.10      onoe 		mword = awi_media_rate2opt(sc, AWI_80211_RATE(phy_rates[2 + i]));
    338      1.10      onoe 		if (mword == 0)
    339      1.10      onoe 			continue;
    340      1.10      onoe 		mword |= IFM_IEEE80211;
    341      1.10      onoe 		ifmedia_add(&sc->sc_media, mword, 0, NULL);
    342      1.10      onoe 		ifmedia_add(&sc->sc_media,
    343      1.10      onoe 		    mword | IFM_IEEE80211_ADHOC, 0, NULL);
    344  1.19.2.1      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
    345  1.19.2.1      onoe 			ifmedia_add(&sc->sc_media,
    346  1.19.2.1      onoe 			    mword | IFM_IEEE80211_ADHOC | IFM_FLAG0, 0, NULL);
    347       1.1  sommerfe 	}
    348      1.10      onoe 	awi_media_status(ifp, &imr);
    349      1.10      onoe 	ifmedia_set(&sc->sc_media, imr.ifm_active);
    350       1.1  sommerfe #endif
    351       1.1  sommerfe 
    352      1.10      onoe 	/* ready to accept ioctl */
    353      1.10      onoe 	awi_unlock(sc);
    354      1.17     jhawk 
    355      1.17     jhawk 	/* Attach is successful. */
    356      1.17     jhawk 	sc->sc_attached = 1;
    357      1.10      onoe 	return 0;
    358       1.1  sommerfe }
    359       1.1  sommerfe 
    360      1.10      onoe #ifdef __NetBSD__
    361      1.10      onoe int
    362      1.10      onoe awi_detach(sc)
    363       1.1  sommerfe 	struct awi_softc *sc;
    364       1.1  sommerfe {
    365      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
    366      1.10      onoe 	int s;
    367      1.17     jhawk 
    368      1.17     jhawk 	/* Succeed if there is no work to do. */
    369      1.17     jhawk 	if (!sc->sc_attached)
    370      1.17     jhawk 		return (0);
    371       1.1  sommerfe 
    372      1.10      onoe 	s = splnet();
    373      1.10      onoe 	sc->sc_invalid = 1;
    374      1.10      onoe 	awi_stop(sc);
    375      1.10      onoe 	while (sc->sc_sleep_cnt > 0) {
    376      1.10      onoe 		wakeup(sc);
    377      1.10      onoe 		(void)tsleep(sc, PWAIT, "awidet", 1);
    378       1.1  sommerfe 	}
    379      1.18      onoe 	if (sc->sc_wep_ctx != NULL)
    380      1.18      onoe 		free(sc->sc_wep_ctx, M_DEVBUF);
    381       1.1  sommerfe #if NBPFILTER > 0
    382      1.10      onoe 	bpfdetach(ifp);
    383      1.10      onoe #endif
    384      1.10      onoe #ifdef IFM_IEEE80211
    385      1.10      onoe 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
    386       1.1  sommerfe #endif
    387      1.10      onoe 	ether_ifdetach(ifp);
    388      1.10      onoe 	if_detach(ifp);
    389      1.10      onoe 	if (sc->sc_enabled) {
    390      1.10      onoe 		if (sc->sc_disable)
    391      1.10      onoe 			(*sc->sc_disable)(sc);
    392      1.10      onoe 		sc->sc_enabled = 0;
    393       1.1  sommerfe 	}
    394      1.10      onoe 	splx(s);
    395      1.10      onoe 	return 0;
    396       1.1  sommerfe }
    397       1.1  sommerfe 
    398       1.1  sommerfe int
    399      1.10      onoe awi_activate(self, act)
    400      1.10      onoe 	struct device *self;
    401      1.10      onoe 	enum devact act;
    402       1.1  sommerfe {
    403      1.10      onoe 	struct awi_softc *sc = (struct awi_softc *)self;
    404      1.10      onoe 	int s, error = 0;
    405      1.10      onoe 
    406      1.10      onoe 	s = splnet();
    407      1.10      onoe 	switch (act) {
    408      1.10      onoe 	case DVACT_ACTIVATE:
    409      1.10      onoe 		error = EOPNOTSUPP;
    410      1.10      onoe 		break;
    411      1.10      onoe 
    412      1.10      onoe 	case DVACT_DEACTIVATE:
    413      1.10      onoe 		sc->sc_invalid = 1;
    414      1.13    itojun 		if (sc->sc_ifp)
    415      1.13    itojun 			if_deactivate(sc->sc_ifp);
    416      1.10      onoe 		break;
    417       1.1  sommerfe 	}
    418      1.10      onoe 	splx(s);
    419      1.10      onoe 
    420      1.10      onoe 	return error;
    421       1.1  sommerfe }
    422       1.1  sommerfe 
    423       1.1  sommerfe void
    424      1.18      onoe awi_power(sc, why)
    425       1.1  sommerfe 	struct awi_softc *sc;
    426      1.18      onoe 	int why;
    427       1.1  sommerfe {
    428      1.10      onoe 	int s;
    429      1.18      onoe 	int ocansleep;
    430      1.10      onoe 
    431      1.10      onoe 	if (!sc->sc_enabled)
    432      1.10      onoe 		return;
    433      1.18      onoe 
    434      1.10      onoe 	s = splnet();
    435      1.18      onoe 	ocansleep = sc->sc_cansleep;
    436      1.18      onoe 	sc->sc_cansleep = 0;
    437      1.18      onoe #ifdef needtobefixed	/*ONOE*/
    438      1.18      onoe 	if (why == PWR_RESUME) {
    439      1.18      onoe 		sc->sc_enabled = 0;
    440      1.18      onoe 		awi_init(sc);
    441      1.18      onoe 		(void)awi_intr(sc);
    442      1.18      onoe 	} else {
    443      1.18      onoe 		awi_stop(sc);
    444      1.18      onoe 		if (sc->sc_disable)
    445      1.18      onoe 			(*sc->sc_disable)(sc);
    446      1.18      onoe 	}
    447      1.18      onoe #endif
    448      1.18      onoe 	sc->sc_cansleep = ocansleep;
    449      1.10      onoe 	splx(s);
    450       1.1  sommerfe }
    451      1.18      onoe #endif /* __NetBSD__ */
    452       1.1  sommerfe 
    453      1.10      onoe static int
    454      1.10      onoe awi_ioctl(ifp, cmd, data)
    455      1.10      onoe 	struct ifnet *ifp;
    456      1.10      onoe 	u_long cmd;
    457      1.10      onoe 	caddr_t data;
    458      1.10      onoe {
    459      1.10      onoe 	struct awi_softc *sc = ifp->if_softc;
    460      1.10      onoe 	struct ifreq *ifr = (struct ifreq *)data;
    461      1.10      onoe 	struct ifaddr *ifa = (struct ifaddr *)data;
    462      1.10      onoe 	int s, error;
    463  1.19.2.2      onoe 	struct ieee80211_nwid nwid;
    464      1.10      onoe 	u_int8_t *p;
    465       1.1  sommerfe 
    466      1.10      onoe 	s = splnet();
    467       1.1  sommerfe 
    468      1.10      onoe 	/* serialize ioctl */
    469      1.10      onoe 	error = awi_lock(sc);
    470      1.10      onoe 	if (error)
    471      1.10      onoe 		goto cantlock;
    472      1.10      onoe 	switch (cmd) {
    473      1.10      onoe 	case SIOCSIFADDR:
    474      1.10      onoe 		ifp->if_flags |= IFF_UP;
    475      1.10      onoe 		switch (ifa->ifa_addr->sa_family) {
    476      1.10      onoe #ifdef INET
    477      1.10      onoe 		case AF_INET:
    478      1.10      onoe 			arp_ifinit((void *)ifp, ifa);
    479      1.10      onoe 			break;
    480      1.10      onoe #endif
    481       1.1  sommerfe 		}
    482      1.10      onoe 		/* FALLTHROUGH */
    483      1.10      onoe 	case SIOCSIFFLAGS:
    484      1.10      onoe 		sc->sc_format_llc = !(ifp->if_flags & IFF_LINK0);
    485      1.10      onoe 		if (!(ifp->if_flags & IFF_UP)) {
    486      1.10      onoe 			if (sc->sc_enabled) {
    487      1.10      onoe 				awi_stop(sc);
    488      1.10      onoe 				if (sc->sc_disable)
    489      1.10      onoe 					(*sc->sc_disable)(sc);
    490      1.10      onoe 				sc->sc_enabled = 0;
    491      1.10      onoe 			}
    492       1.1  sommerfe 			break;
    493      1.10      onoe 		}
    494      1.10      onoe 		error = awi_init(sc);
    495      1.10      onoe 		break;
    496       1.1  sommerfe 
    497      1.10      onoe 	case SIOCADDMULTI:
    498      1.10      onoe 	case SIOCDELMULTI:
    499      1.10      onoe #ifdef __FreeBSD__
    500      1.10      onoe 		error = ENETRESET;	/*XXX*/
    501      1.10      onoe #else
    502      1.10      onoe 		error = (cmd == SIOCADDMULTI) ?
    503      1.10      onoe 		    ether_addmulti(ifr, &sc->sc_ec) :
    504      1.10      onoe 		    ether_delmulti(ifr, &sc->sc_ec);
    505      1.10      onoe #endif
    506      1.18      onoe 		/*
    507      1.18      onoe 		 * Do not rescan BSS.  Rather, just reset multicast filter.
    508      1.18      onoe 		 */
    509      1.10      onoe 		if (error == ENETRESET) {
    510      1.10      onoe 			if (sc->sc_enabled)
    511      1.10      onoe 				error = awi_init(sc);
    512      1.10      onoe 			else
    513      1.10      onoe 				error = 0;
    514      1.10      onoe 		}
    515      1.10      onoe 		break;
    516      1.10      onoe 	case SIOCSIFMTU:
    517      1.10      onoe 		if (ifr->ifr_mtu > ETHERMTU)
    518      1.10      onoe 			error = EINVAL;
    519      1.10      onoe 		else
    520      1.10      onoe 			ifp->if_mtu = ifr->ifr_mtu;
    521      1.10      onoe 		break;
    522      1.10      onoe 	case SIOCS80211NWID:
    523  1.19.2.2      onoe 		error = copyin(ifr->ifr_data, &nwid, sizeof(nwid));
    524      1.10      onoe 		if (error)
    525      1.10      onoe 			break;
    526  1.19.2.2      onoe 		if (nwid.i_len > IEEE80211_NWID_LEN) {
    527      1.10      onoe 			error = EINVAL;
    528      1.10      onoe 			break;
    529      1.10      onoe 		}
    530  1.19.2.2      onoe 		if (sc->sc_mib_mac.aDesired_ESS_ID[1] == nwid.i_len &&
    531  1.19.2.2      onoe 		    memcmp(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
    532  1.19.2.2      onoe 		    nwid.i_len) == 0)
    533       1.1  sommerfe 			break;
    534      1.10      onoe 		memset(sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
    535      1.10      onoe 		sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
    536  1.19.2.2      onoe 		sc->sc_mib_mac.aDesired_ESS_ID[1] = nwid.i_len;
    537  1.19.2.2      onoe 		memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], nwid.i_nwid,
    538  1.19.2.2      onoe 		    nwid.i_len);
    539      1.10      onoe 		if (sc->sc_enabled) {
    540      1.10      onoe 			awi_stop(sc);
    541      1.10      onoe 			error = awi_init(sc);
    542      1.10      onoe 		}
    543      1.10      onoe 		break;
    544      1.10      onoe 	case SIOCG80211NWID:
    545      1.10      onoe 		if (ifp->if_flags & IFF_RUNNING)
    546      1.10      onoe 			p = sc->sc_bss.essid;
    547      1.10      onoe 		else
    548      1.10      onoe 			p = sc->sc_mib_mac.aDesired_ESS_ID;
    549  1.19.2.2      onoe 		error = copyout(p + 1, ifr->ifr_data, 1 + IEEE80211_NWID_LEN);
    550  1.19.2.4      onoe 		break;
    551  1.19.2.4      onoe 	case SIOCS80211NWKEY:
    552  1.19.2.4      onoe 		error = awi_wep_setnwkey(sc, (struct ieee80211_nwkey *)data);
    553  1.19.2.4      onoe 		break;
    554  1.19.2.4      onoe 	case SIOCG80211NWKEY:
    555  1.19.2.4      onoe 		error = awi_wep_getnwkey(sc, (struct ieee80211_nwkey *)data);
    556      1.10      onoe 		break;
    557      1.10      onoe #ifdef IFM_IEEE80211
    558      1.10      onoe 	case SIOCSIFMEDIA:
    559      1.10      onoe 	case SIOCGIFMEDIA:
    560      1.10      onoe 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    561      1.10      onoe 		break;
    562      1.10      onoe #endif
    563      1.10      onoe 	default:
    564      1.18      onoe 		error = awi_wicfg(ifp, cmd, data);
    565      1.10      onoe 		break;
    566       1.1  sommerfe 	}
    567      1.10      onoe 	awi_unlock(sc);
    568      1.10      onoe   cantlock:
    569      1.10      onoe 	splx(s);
    570      1.10      onoe 	return error;
    571       1.1  sommerfe }
    572       1.1  sommerfe 
    573      1.10      onoe #ifdef IFM_IEEE80211
    574      1.10      onoe static int
    575      1.10      onoe awi_media_rate2opt(sc, rate)
    576       1.1  sommerfe 	struct awi_softc *sc;
    577      1.10      onoe 	int rate;
    578       1.1  sommerfe {
    579      1.10      onoe 	int mword;
    580      1.10      onoe 
    581      1.10      onoe 	mword = 0;
    582      1.10      onoe 	switch (rate) {
    583      1.10      onoe 	case 10:
    584      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
    585      1.10      onoe 			mword = IFM_IEEE80211_FH1;
    586      1.10      onoe 		else
    587      1.10      onoe 			mword = IFM_IEEE80211_DS1;
    588      1.10      onoe 		break;
    589      1.10      onoe 	case 20:
    590      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
    591      1.10      onoe 			mword = IFM_IEEE80211_FH2;
    592      1.10      onoe 		else
    593      1.10      onoe 			mword = IFM_IEEE80211_DS2;
    594      1.10      onoe 		break;
    595      1.10      onoe 	case 55:
    596      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
    597      1.10      onoe 			mword = IFM_IEEE80211_DS5;
    598      1.10      onoe 		break;
    599      1.10      onoe 	case 110:
    600      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
    601      1.10      onoe 			mword = IFM_IEEE80211_DS11;
    602      1.10      onoe 		break;
    603      1.10      onoe 	}
    604      1.10      onoe 	return mword;
    605       1.1  sommerfe }
    606       1.1  sommerfe 
    607      1.10      onoe static int
    608      1.10      onoe awi_media_opt2rate(sc, opt)
    609       1.1  sommerfe 	struct awi_softc *sc;
    610      1.10      onoe 	int opt;
    611       1.1  sommerfe {
    612      1.10      onoe 	int rate;
    613       1.1  sommerfe 
    614      1.10      onoe 	rate = 0;
    615      1.10      onoe 	switch (IFM_SUBTYPE(opt)) {
    616      1.10      onoe 	case IFM_IEEE80211_FH1:
    617      1.10      onoe 	case IFM_IEEE80211_FH2:
    618      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
    619      1.10      onoe 			return 0;
    620      1.10      onoe 		break;
    621      1.10      onoe 	case IFM_IEEE80211_DS1:
    622      1.10      onoe 	case IFM_IEEE80211_DS2:
    623      1.10      onoe 	case IFM_IEEE80211_DS5:
    624      1.10      onoe 	case IFM_IEEE80211_DS11:
    625      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
    626      1.10      onoe 			return 0;
    627      1.10      onoe 		break;
    628      1.10      onoe 	}
    629       1.1  sommerfe 
    630      1.10      onoe 	switch (IFM_SUBTYPE(opt)) {
    631      1.10      onoe 	case IFM_IEEE80211_FH1:
    632      1.10      onoe 	case IFM_IEEE80211_DS1:
    633      1.10      onoe 		rate = 10;
    634      1.10      onoe 		break;
    635      1.10      onoe 	case IFM_IEEE80211_FH2:
    636      1.10      onoe 	case IFM_IEEE80211_DS2:
    637      1.10      onoe 		rate = 20;
    638      1.10      onoe 		break;
    639      1.10      onoe 	case IFM_IEEE80211_DS5:
    640      1.10      onoe 		rate = 55;
    641      1.10      onoe 		break;
    642      1.10      onoe 	case IFM_IEEE80211_DS11:
    643      1.10      onoe 		rate = 110;
    644      1.10      onoe 		break;
    645      1.10      onoe 	}
    646      1.10      onoe 	return rate;
    647       1.1  sommerfe }
    648       1.1  sommerfe 
    649      1.10      onoe /*
    650      1.10      onoe  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
    651      1.10      onoe  */
    652      1.10      onoe static int
    653      1.10      onoe awi_media_change(ifp)
    654      1.10      onoe 	struct ifnet *ifp;
    655       1.1  sommerfe {
    656      1.10      onoe 	struct awi_softc *sc = ifp->if_softc;
    657      1.10      onoe 	struct ifmedia_entry *ime;
    658      1.10      onoe 	u_int8_t *phy_rates;
    659      1.10      onoe 	int i, rate, error;
    660      1.10      onoe 
    661      1.10      onoe 	error = 0;
    662      1.10      onoe 	ime = sc->sc_media.ifm_cur;
    663      1.10      onoe 	rate = awi_media_opt2rate(sc, ime->ifm_media);
    664      1.10      onoe 	if (rate == 0)
    665      1.10      onoe 		return EINVAL;
    666      1.10      onoe 	if (rate != sc->sc_tx_rate) {
    667      1.10      onoe 		phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
    668      1.10      onoe 		for (i = 0; i < phy_rates[1]; i++) {
    669      1.10      onoe 			if (rate == AWI_80211_RATE(phy_rates[2 + i]))
    670      1.10      onoe 				break;
    671      1.10      onoe 		}
    672      1.10      onoe 		if (i == phy_rates[1])
    673      1.10      onoe 			return EINVAL;
    674       1.1  sommerfe 	}
    675      1.10      onoe 	if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
    676      1.10      onoe 		sc->sc_mib_local.Network_Mode = 0;
    677  1.19.2.1      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
    678  1.19.2.1      onoe 			sc->sc_no_bssid = 0;
    679  1.19.2.1      onoe 		else
    680  1.19.2.1      onoe 			sc->sc_no_bssid = (ime->ifm_media & IFM_FLAG0) ? 1 : 0;
    681      1.10      onoe 	} else {
    682      1.10      onoe 		sc->sc_mib_local.Network_Mode = 1;
    683       1.1  sommerfe 	}
    684      1.10      onoe 	if (sc->sc_enabled) {
    685      1.10      onoe 		awi_stop(sc);
    686      1.10      onoe 		error = awi_init(sc);
    687       1.1  sommerfe 	}
    688      1.10      onoe 	return error;
    689       1.1  sommerfe }
    690       1.1  sommerfe 
    691      1.10      onoe static void
    692      1.10      onoe awi_media_status(ifp, imr)
    693      1.10      onoe 	struct ifnet *ifp;
    694      1.10      onoe 	struct ifmediareq *imr;
    695       1.1  sommerfe {
    696      1.10      onoe 	struct awi_softc *sc = ifp->if_softc;
    697       1.1  sommerfe 
    698      1.10      onoe 	imr->ifm_status = IFM_AVALID;
    699      1.10      onoe 	if (ifp->if_flags & IFF_RUNNING)
    700      1.10      onoe 		imr->ifm_status |= IFM_ACTIVE;
    701      1.10      onoe 	imr->ifm_active = IFM_IEEE80211;
    702      1.10      onoe 	imr->ifm_active |= awi_media_rate2opt(sc, sc->sc_tx_rate);
    703      1.10      onoe 	if (sc->sc_mib_local.Network_Mode == 0) {
    704      1.10      onoe 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    705      1.10      onoe 		if (sc->sc_no_bssid)
    706      1.10      onoe 			imr->ifm_active |= IFM_FLAG0;
    707       1.6  sommerfe 	}
    708       1.1  sommerfe }
    709      1.10      onoe #endif /* IFM_IEEE80211 */
    710       1.1  sommerfe 
    711       1.1  sommerfe int
    712       1.1  sommerfe awi_intr(arg)
    713       1.1  sommerfe 	void *arg;
    714       1.1  sommerfe {
    715       1.1  sommerfe 	struct awi_softc *sc = arg;
    716      1.10      onoe 	u_int16_t status;
    717      1.10      onoe 	int error, handled = 0, ocansleep;
    718       1.1  sommerfe 
    719      1.15      onoe 	if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
    720      1.10      onoe 		return 0;
    721       1.1  sommerfe 
    722      1.10      onoe 	am79c930_gcr_setbits(&sc->sc_chip,
    723      1.10      onoe 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
    724       1.1  sommerfe 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
    725      1.10      onoe 	ocansleep = sc->sc_cansleep;
    726      1.10      onoe 	sc->sc_cansleep = 0;
    727      1.10      onoe 
    728       1.1  sommerfe 	for (;;) {
    729      1.10      onoe 		error = awi_intr_lock(sc);
    730      1.10      onoe 		if (error)
    731      1.10      onoe 			break;
    732      1.10      onoe 		status = awi_read_1(sc, AWI_INTSTAT);
    733      1.10      onoe 		awi_write_1(sc, AWI_INTSTAT, 0);
    734      1.10      onoe 		awi_write_1(sc, AWI_INTSTAT, 0);
    735      1.10      onoe 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
    736      1.10      onoe 		awi_write_1(sc, AWI_INTSTAT2, 0);
    737      1.10      onoe 		DELAY(10);
    738      1.10      onoe 		awi_intr_unlock(sc);
    739      1.10      onoe 		if (!sc->sc_cmd_inprog)
    740      1.10      onoe 			status &= ~AWI_INT_CMD;	/* make sure */
    741      1.10      onoe 		if (status == 0)
    742       1.1  sommerfe 			break;
    743       1.1  sommerfe 		handled = 1;
    744      1.10      onoe 		if (status & AWI_INT_RX)
    745       1.1  sommerfe 			awi_rxint(sc);
    746      1.10      onoe 		if (status & AWI_INT_TX)
    747       1.1  sommerfe 			awi_txint(sc);
    748      1.10      onoe 		if (status & AWI_INT_CMD)
    749      1.10      onoe 			awi_cmd_done(sc);
    750      1.10      onoe 		if (status & AWI_INT_SCAN_CMPLT) {
    751      1.10      onoe 			if (sc->sc_status == AWI_ST_SCAN &&
    752      1.10      onoe 			    sc->sc_mgt_timer > 0)
    753      1.10      onoe 				(void)awi_next_scan(sc);
    754       1.1  sommerfe 		}
    755       1.1  sommerfe 	}
    756      1.10      onoe 	sc->sc_cansleep = ocansleep;
    757       1.1  sommerfe 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
    758       1.1  sommerfe 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
    759       1.1  sommerfe 	return handled;
    760       1.1  sommerfe }
    761       1.1  sommerfe 
    762      1.18      onoe int
    763      1.10      onoe awi_init(sc)
    764       1.8  sommerfe 	struct awi_softc *sc;
    765       1.8  sommerfe {
    766      1.10      onoe 	int error, ostatus;
    767      1.10      onoe 	int n;
    768       1.8  sommerfe 	struct ifnet *ifp = sc->sc_ifp;
    769      1.10      onoe #ifdef __FreeBSD__
    770      1.10      onoe 	struct ifmultiaddr *ifma;
    771      1.10      onoe #else
    772      1.10      onoe 	struct ether_multi *enm;
    773      1.10      onoe 	struct ether_multistep step;
    774      1.10      onoe #endif
    775       1.8  sommerfe 
    776      1.16      onoe 	/* reinitialize muticast filter */
    777      1.10      onoe 	n = 0;
    778      1.10      onoe 	ifp->if_flags |= IFF_ALLMULTI;
    779      1.10      onoe 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
    780      1.10      onoe 	if (ifp->if_flags & IFF_PROMISC) {
    781      1.10      onoe 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
    782      1.10      onoe 		goto set_mib;
    783      1.10      onoe 	}
    784      1.10      onoe 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
    785      1.10      onoe #ifdef __FreeBSD__
    786      1.10      onoe 	if (ifp->if_amcount != 0)
    787      1.10      onoe 		goto set_mib;
    788      1.10      onoe 	for (ifma = LIST_FIRST(&ifp->if_multiaddrs); ifma != NULL;
    789      1.10      onoe 	    ifma = LIST_NEXT(ifma, ifma_link)) {
    790      1.10      onoe 		if (ifma->ifma_addr->sa_family != AF_LINK)
    791      1.10      onoe 			continue;
    792      1.10      onoe 		if (n == AWI_GROUP_ADDR_SIZE)
    793      1.10      onoe 			goto set_mib;
    794      1.10      onoe 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n],
    795      1.10      onoe 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
    796      1.10      onoe 		    ETHER_ADDR_LEN);
    797      1.10      onoe 		n++;
    798      1.10      onoe 	}
    799      1.10      onoe #else
    800      1.10      onoe 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    801      1.10      onoe 	while (enm != NULL) {
    802      1.10      onoe 		if (n == AWI_GROUP_ADDR_SIZE ||
    803      1.10      onoe 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)
    804      1.10      onoe 		    != 0)
    805      1.10      onoe 			goto set_mib;
    806      1.10      onoe 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
    807      1.10      onoe 		    ETHER_ADDR_LEN);
    808      1.10      onoe 		n++;
    809      1.10      onoe 		ETHER_NEXT_MULTI(step, enm);
    810      1.10      onoe 	}
    811      1.10      onoe #endif
    812      1.16      onoe 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
    813      1.16      onoe 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, ETHER_ADDR_LEN);
    814      1.10      onoe 	ifp->if_flags &= ~IFF_ALLMULTI;
    815      1.10      onoe 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
    816      1.10      onoe 
    817      1.10      onoe   set_mib:
    818  1.19.2.1      onoe #ifdef notdef	/* allow non-encrypted frame for receiving. */
    819  1.19.2.1      onoe 	sc->sc_mib_mgt.Wep_Required = sc->sc_wep_algo != NULL ? 1 : 0;
    820  1.19.2.1      onoe #endif
    821      1.10      onoe 	if (!sc->sc_enabled) {
    822      1.10      onoe 		sc->sc_enabled = 1;
    823      1.10      onoe 		if (sc->sc_enable)
    824      1.10      onoe 			(*sc->sc_enable)(sc);
    825      1.10      onoe 		sc->sc_status = AWI_ST_INIT;
    826      1.10      onoe 		error = awi_init_hw(sc);
    827      1.10      onoe 		if (error)
    828      1.10      onoe 			return error;
    829      1.10      onoe 	}
    830      1.10      onoe 	ostatus = sc->sc_status;
    831      1.10      onoe 	sc->sc_status = AWI_ST_INIT;
    832      1.10      onoe 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL)) != 0 ||
    833      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR)) != 0 ||
    834      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC)) != 0 ||
    835      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT)) != 0 ||
    836      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY)) != 0) {
    837      1.10      onoe 		awi_stop(sc);
    838      1.10      onoe 		return error;
    839      1.10      onoe 	}
    840      1.10      onoe 	if (ifp->if_flags & IFF_RUNNING)
    841      1.10      onoe 		sc->sc_status = AWI_ST_RUNNING;
    842      1.10      onoe 	else {
    843      1.10      onoe 		if (ostatus == AWI_ST_INIT) {
    844      1.10      onoe 			error = awi_init_txrx(sc);
    845      1.10      onoe 			if (error)
    846      1.10      onoe 				return error;
    847      1.10      onoe 		}
    848      1.10      onoe 		error = awi_start_scan(sc);
    849      1.10      onoe 	}
    850      1.10      onoe 	return error;
    851       1.8  sommerfe }
    852       1.8  sommerfe 
    853      1.18      onoe void
    854       1.1  sommerfe awi_stop(sc)
    855       1.1  sommerfe 	struct awi_softc *sc;
    856       1.1  sommerfe {
    857       1.1  sommerfe 	struct ifnet *ifp = sc->sc_ifp;
    858      1.10      onoe 	struct awi_bss *bp;
    859      1.10      onoe 	struct mbuf *m;
    860       1.8  sommerfe 
    861      1.10      onoe 	sc->sc_status = AWI_ST_INIT;
    862      1.10      onoe 	if (!sc->sc_invalid) {
    863      1.10      onoe 		(void)awi_cmd_wait(sc);
    864      1.10      onoe 		if (sc->sc_mib_local.Network_Mode &&
    865      1.10      onoe 		    sc->sc_status > AWI_ST_AUTH)
    866      1.10      onoe 			awi_send_deauth(sc);
    867      1.10      onoe 		awi_stop_txrx(sc);
    868      1.10      onoe 	}
    869      1.10      onoe 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
    870       1.1  sommerfe 	ifp->if_timer = 0;
    871      1.10      onoe 	sc->sc_tx_timer = sc->sc_rx_timer = sc->sc_mgt_timer = 0;
    872      1.10      onoe 	for (;;) {
    873      1.10      onoe 		IF_DEQUEUE(&sc->sc_mgtq, m);
    874      1.10      onoe 		if (m == NULL)
    875      1.10      onoe 			break;
    876      1.10      onoe 		m_freem(m);
    877      1.10      onoe 	}
    878      1.10      onoe 	for (;;) {
    879      1.10      onoe 		IF_DEQUEUE(&ifp->if_snd, m);
    880      1.10      onoe 		if (m == NULL)
    881      1.10      onoe 			break;
    882      1.10      onoe 		m_freem(m);
    883      1.10      onoe 	}
    884      1.18      onoe 	while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
    885      1.10      onoe 		TAILQ_REMOVE(&sc->sc_scan, bp, list);
    886      1.18      onoe 		free(bp, M_DEVBUF);
    887      1.18      onoe 	}
    888       1.1  sommerfe }
    889       1.1  sommerfe 
    890      1.10      onoe static void
    891       1.1  sommerfe awi_watchdog(ifp)
    892       1.1  sommerfe 	struct ifnet *ifp;
    893       1.1  sommerfe {
    894       1.1  sommerfe 	struct awi_softc *sc = ifp->if_softc;
    895      1.10      onoe 	int ocansleep;
    896       1.1  sommerfe 
    897      1.10      onoe 	if (sc->sc_invalid) {
    898      1.10      onoe 		ifp->if_timer = 0;
    899       1.1  sommerfe 		return;
    900       1.1  sommerfe 	}
    901       1.1  sommerfe 
    902      1.10      onoe 	ocansleep = sc->sc_cansleep;
    903      1.10      onoe 	sc->sc_cansleep = 0;
    904      1.10      onoe 	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
    905      1.10      onoe 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    906      1.10      onoe 		awi_txint(sc);
    907      1.10      onoe 	}
    908      1.10      onoe 	if (sc->sc_rx_timer && --sc->sc_rx_timer == 0) {
    909  1.19.2.3      onoe 		if (ifp->if_flags & IFF_DEBUG) {
    910  1.19.2.3      onoe 			printf("%s: no recent beacons from %s; rescanning\n",
    911  1.19.2.3      onoe 			    sc->sc_dev.dv_xname,
    912  1.19.2.3      onoe 			    ether_sprintf(sc->sc_bss.bssid));
    913  1.19.2.3      onoe 		}
    914  1.19.2.1      onoe 		ifp->if_flags &= ~IFF_RUNNING;
    915      1.10      onoe 		awi_start_scan(sc);
    916      1.10      onoe 	}
    917      1.10      onoe 	if (sc->sc_mgt_timer && --sc->sc_mgt_timer == 0) {
    918      1.10      onoe 		switch (sc->sc_status) {
    919      1.10      onoe 		case AWI_ST_SCAN:
    920      1.10      onoe 			awi_stop_scan(sc);
    921      1.10      onoe 			break;
    922      1.10      onoe 		case AWI_ST_AUTH:
    923      1.10      onoe 		case AWI_ST_ASSOC:
    924      1.10      onoe 			/* restart scan */
    925      1.10      onoe 			awi_start_scan(sc);
    926      1.10      onoe 			break;
    927      1.10      onoe 		default:
    928      1.10      onoe 			break;
    929       1.1  sommerfe 		}
    930       1.1  sommerfe 	}
    931       1.1  sommerfe 
    932      1.10      onoe 	if (sc->sc_tx_timer == 0 && sc->sc_rx_timer == 0 &&
    933      1.10      onoe 	    sc->sc_mgt_timer == 0)
    934      1.10      onoe 		ifp->if_timer = 0;
    935      1.10      onoe 	else
    936      1.10      onoe 		ifp->if_timer = 1;
    937      1.10      onoe 	sc->sc_cansleep = ocansleep;
    938       1.1  sommerfe }
    939       1.1  sommerfe 
    940      1.10      onoe static void
    941      1.10      onoe awi_start(ifp)
    942      1.10      onoe 	struct ifnet *ifp;
    943       1.1  sommerfe {
    944       1.1  sommerfe 	struct awi_softc *sc = ifp->if_softc;
    945      1.10      onoe 	struct mbuf *m0, *m;
    946      1.10      onoe 	u_int32_t txd, frame, ntxd;
    947      1.10      onoe 	u_int8_t rate;
    948      1.10      onoe 	int len, sent = 0;
    949       1.1  sommerfe 
    950      1.10      onoe 	for (;;) {
    951      1.10      onoe 		txd = sc->sc_txnext;
    952      1.10      onoe 		IF_DEQUEUE(&sc->sc_mgtq, m0);
    953      1.10      onoe 		if (m0 != NULL) {
    954      1.10      onoe 			if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
    955      1.10      onoe 				IF_PREPEND(&sc->sc_mgtq, m0);
    956      1.10      onoe 				ifp->if_flags |= IFF_OACTIVE;
    957      1.10      onoe 				break;
    958      1.10      onoe 			}
    959      1.10      onoe 		} else {
    960      1.10      onoe 			if (!(ifp->if_flags & IFF_RUNNING))
    961      1.10      onoe 				break;
    962      1.10      onoe 			IF_DEQUEUE(&ifp->if_snd, m0);
    963      1.10      onoe 			if (m0 == NULL)
    964      1.10      onoe 				break;
    965      1.18      onoe 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
    966      1.18      onoe 			if (sc->sc_format_llc)
    967      1.18      onoe 				len += sizeof(struct llc) -
    968      1.18      onoe 				    sizeof(struct ether_header);
    969      1.18      onoe 			if (sc->sc_wep_algo != NULL)
    970      1.18      onoe 				len += IEEE80211_WEP_IVLEN +
    971      1.18      onoe 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
    972      1.18      onoe 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
    973      1.10      onoe 				IF_PREPEND(&ifp->if_snd, m0);
    974      1.10      onoe 				ifp->if_flags |= IFF_OACTIVE;
    975       1.1  sommerfe 				break;
    976      1.10      onoe 			}
    977      1.10      onoe 			AWI_BPF_MTAP(sc, m0, AWI_BPF_NORM);
    978      1.10      onoe 			m0 = awi_fix_txhdr(sc, m0);
    979      1.18      onoe 			if (sc->sc_wep_algo != NULL && m0 != NULL)
    980      1.18      onoe 				m0 = awi_wep_encrypt(sc, m0, 1);
    981      1.10      onoe 			if (m0 == NULL) {
    982      1.10      onoe 				ifp->if_oerrors++;
    983      1.10      onoe 				continue;
    984      1.10      onoe 			}
    985      1.10      onoe 			ifp->if_opackets++;
    986       1.1  sommerfe 		}
    987  1.19.2.1      onoe #ifdef AWI_DEBUG
    988  1.19.2.1      onoe 		if (awi_dump)
    989  1.19.2.1      onoe 			awi_dump_pkt(sc, m0, -1);
    990  1.19.2.1      onoe #endif
    991      1.10      onoe 		AWI_BPF_MTAP(sc, m0, AWI_BPF_RAW);
    992      1.10      onoe 		len = 0;
    993      1.10      onoe 		for (m = m0; m != NULL; m = m->m_next) {
    994      1.10      onoe 			awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
    995      1.10      onoe 			    m->m_len);
    996      1.10      onoe 			len += m->m_len;
    997       1.4  sommerfe 		}
    998      1.10      onoe 		m_freem(m0);
    999      1.10      onoe 		rate = sc->sc_tx_rate;	/*XXX*/
   1000      1.10      onoe 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
   1001      1.10      onoe 		awi_write_4(sc, txd + AWI_TXD_START, frame);
   1002      1.10      onoe 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
   1003      1.10      onoe 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
   1004      1.10      onoe 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
   1005      1.10      onoe 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
   1006      1.10      onoe 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
   1007      1.10      onoe 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
   1008      1.10      onoe 		sc->sc_txnext = ntxd;
   1009      1.10      onoe 		sent++;
   1010      1.10      onoe 	}
   1011      1.10      onoe 	if (sent) {
   1012      1.10      onoe 		if (sc->sc_tx_timer == 0)
   1013      1.10      onoe 			sc->sc_tx_timer = 5;
   1014      1.10      onoe 		ifp->if_timer = 1;
   1015      1.10      onoe #ifdef AWI_DEBUG
   1016      1.10      onoe 		if (awi_verbose)
   1017      1.10      onoe 			printf("awi_start: sent %d txdone %d txnext %d txbase %d txend %d\n", sent, sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
   1018      1.10      onoe #endif
   1019       1.1  sommerfe 	}
   1020       1.1  sommerfe }
   1021       1.1  sommerfe 
   1022      1.10      onoe static void
   1023      1.10      onoe awi_txint(sc)
   1024      1.10      onoe 	struct awi_softc *sc;
   1025       1.1  sommerfe {
   1026       1.9  sommerfe 	struct ifnet *ifp = sc->sc_ifp;
   1027      1.10      onoe 	u_int8_t flags;
   1028       1.9  sommerfe 
   1029      1.10      onoe 	while (sc->sc_txdone != sc->sc_txnext) {
   1030      1.10      onoe 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
   1031      1.10      onoe 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
   1032      1.10      onoe 			break;
   1033      1.10      onoe 		if (flags & AWI_TXD_ST_ERROR)
   1034      1.10      onoe 			ifp->if_oerrors++;
   1035      1.10      onoe 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
   1036      1.10      onoe 		    0x7fff;
   1037      1.10      onoe 	}
   1038      1.10      onoe 	sc->sc_tx_timer = 0;
   1039      1.10      onoe 	ifp->if_flags &= ~IFF_OACTIVE;
   1040      1.10      onoe #ifdef AWI_DEBUG
   1041      1.10      onoe 	if (awi_verbose)
   1042      1.10      onoe 		printf("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
   1043      1.10      onoe 		    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend);
   1044      1.10      onoe #endif
   1045      1.10      onoe 	awi_start(ifp);
   1046      1.10      onoe }
   1047       1.9  sommerfe 
   1048      1.10      onoe static struct mbuf *
   1049      1.10      onoe awi_fix_txhdr(sc, m0)
   1050      1.10      onoe 	struct awi_softc *sc;
   1051      1.10      onoe 	struct mbuf *m0;
   1052      1.10      onoe {
   1053      1.10      onoe 	struct ether_header eh;
   1054      1.10      onoe 	struct ieee80211_frame *wh;
   1055      1.10      onoe 	struct llc *llc;
   1056      1.10      onoe 
   1057      1.10      onoe 	if (m0->m_len < sizeof(eh)) {
   1058      1.10      onoe 		m0 = m_pullup(m0, sizeof(eh));
   1059      1.10      onoe 		if (m0 == NULL)
   1060      1.10      onoe 			return NULL;
   1061      1.10      onoe 	}
   1062      1.10      onoe 	memcpy(&eh, mtod(m0, caddr_t), sizeof(eh));
   1063      1.10      onoe 	if (sc->sc_format_llc) {
   1064      1.10      onoe 		m_adj(m0, sizeof(struct ether_header) - sizeof(struct llc));
   1065      1.10      onoe 		llc = mtod(m0, struct llc *);
   1066      1.10      onoe 		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
   1067      1.10      onoe 		llc->llc_control = LLC_UI;
   1068      1.10      onoe 		llc->llc_snap.org_code[0] = llc->llc_snap.org_code[1] =
   1069      1.10      onoe 		    llc->llc_snap.org_code[2] = 0;
   1070      1.10      onoe 		llc->llc_snap.ether_type = eh.ether_type;
   1071      1.10      onoe 	}
   1072      1.10      onoe 	M_PREPEND(m0, sizeof(struct ieee80211_frame), M_DONTWAIT);
   1073      1.10      onoe 	if (m0 == NULL)
   1074      1.10      onoe 		return NULL;
   1075      1.10      onoe 	wh = mtod(m0, struct ieee80211_frame *);
   1076       1.1  sommerfe 
   1077      1.10      onoe 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
   1078      1.10      onoe 	LE_WRITE_2(wh->i_dur, 0);
   1079      1.10      onoe 	LE_WRITE_2(wh->i_seq, 0);
   1080      1.10      onoe 	if (sc->sc_mib_local.Network_Mode) {
   1081      1.10      onoe 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
   1082      1.10      onoe 		memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   1083      1.10      onoe 		memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
   1084      1.10      onoe 		memcpy(wh->i_addr3, eh.ether_dhost, ETHER_ADDR_LEN);
   1085      1.10      onoe 	} else {
   1086      1.10      onoe 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1087      1.10      onoe 		memcpy(wh->i_addr1, eh.ether_dhost, ETHER_ADDR_LEN);
   1088      1.10      onoe 		memcpy(wh->i_addr2, eh.ether_shost, ETHER_ADDR_LEN);
   1089      1.10      onoe 		memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   1090       1.1  sommerfe 	}
   1091      1.10      onoe 	return m0;
   1092      1.10      onoe }
   1093       1.9  sommerfe 
   1094      1.10      onoe static struct mbuf *
   1095      1.10      onoe awi_fix_rxhdr(sc, m0)
   1096      1.10      onoe 	struct awi_softc *sc;
   1097      1.10      onoe 	struct mbuf *m0;
   1098      1.10      onoe {
   1099      1.10      onoe 	struct ieee80211_frame wh;
   1100      1.10      onoe 	struct ether_header *eh;
   1101      1.10      onoe 	struct llc *llc;
   1102       1.1  sommerfe 
   1103      1.10      onoe 	if (m0->m_len < sizeof(wh)) {
   1104      1.10      onoe 		m_freem(m0);
   1105      1.10      onoe 		return NULL;
   1106      1.10      onoe 	}
   1107      1.10      onoe 	llc = (struct llc *)(mtod(m0, caddr_t) + sizeof(wh));
   1108      1.10      onoe 	if (llc->llc_dsap == LLC_SNAP_LSAP &&
   1109      1.10      onoe 	    llc->llc_ssap == LLC_SNAP_LSAP &&
   1110      1.10      onoe 	    llc->llc_control == LLC_UI &&
   1111      1.10      onoe 	    llc->llc_snap.org_code[0] == 0 &&
   1112      1.10      onoe 	    llc->llc_snap.org_code[1] == 0 &&
   1113      1.10      onoe 	    llc->llc_snap.org_code[2] == 0) {
   1114      1.10      onoe 		memcpy(&wh, mtod(m0, caddr_t), sizeof(wh));
   1115      1.10      onoe 		m_adj(m0, sizeof(wh) + sizeof(*llc) - sizeof(*eh));
   1116      1.10      onoe 		eh = mtod(m0, struct ether_header *);
   1117      1.10      onoe 		switch (wh.i_fc[1] & IEEE80211_FC1_DIR_MASK) {
   1118      1.10      onoe 		case IEEE80211_FC1_DIR_NODS:
   1119      1.10      onoe 			memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
   1120      1.10      onoe 			memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
   1121      1.10      onoe 			break;
   1122      1.10      onoe 		case IEEE80211_FC1_DIR_TODS:
   1123      1.10      onoe 			memcpy(eh->ether_dhost, wh.i_addr3, ETHER_ADDR_LEN);
   1124      1.10      onoe 			memcpy(eh->ether_shost, wh.i_addr2, ETHER_ADDR_LEN);
   1125      1.10      onoe 			break;
   1126      1.10      onoe 		case IEEE80211_FC1_DIR_FROMDS:
   1127      1.10      onoe 			memcpy(eh->ether_dhost, wh.i_addr1, ETHER_ADDR_LEN);
   1128      1.10      onoe 			memcpy(eh->ether_shost, wh.i_addr3, ETHER_ADDR_LEN);
   1129      1.10      onoe 			break;
   1130      1.10      onoe 		case IEEE80211_FC1_DIR_DSTODS:
   1131      1.10      onoe 			m_freem(m0);
   1132      1.10      onoe 			return NULL;
   1133      1.10      onoe 		}
   1134      1.10      onoe 	} else {
   1135      1.10      onoe 		/* assuming ethernet encapsulation, just strip 802.11 header */
   1136      1.10      onoe 		m_adj(m0, sizeof(wh));
   1137      1.10      onoe 	}
   1138  1.19.2.1      onoe 	if (ALIGN(mtod(m0, caddr_t) + sizeof(struct ether_header)) !=
   1139  1.19.2.1      onoe 	    (u_int)(mtod(m0, caddr_t) + sizeof(struct ether_header))) {
   1140  1.19.2.1      onoe 		/* XXX: we loose to estimate the type of encapsulation */
   1141  1.19.2.1      onoe 		struct mbuf *n, *n0, **np;
   1142  1.19.2.1      onoe 		caddr_t newdata;
   1143  1.19.2.1      onoe 		int off;
   1144  1.19.2.1      onoe 
   1145  1.19.2.1      onoe 		n0 = NULL;
   1146  1.19.2.1      onoe 		np = &n0;
   1147  1.19.2.1      onoe 		off = 0;
   1148  1.19.2.1      onoe 		while (m0->m_pkthdr.len > off) {
   1149  1.19.2.1      onoe 			if (n0 == NULL) {
   1150  1.19.2.1      onoe 				MGETHDR(n, M_DONTWAIT, MT_DATA);
   1151  1.19.2.1      onoe 				if (n == NULL) {
   1152  1.19.2.1      onoe 					m_freem(m0);
   1153  1.19.2.1      onoe 					return NULL;
   1154  1.19.2.1      onoe 				}
   1155  1.19.2.1      onoe 				M_COPY_PKTHDR(n, m0);
   1156  1.19.2.1      onoe 				n->m_len = MHLEN;
   1157  1.19.2.1      onoe 			} else {
   1158  1.19.2.1      onoe 				MGET(n, M_DONTWAIT, MT_DATA);
   1159  1.19.2.1      onoe 				if (n == NULL) {
   1160  1.19.2.1      onoe 					m_freem(m0);
   1161  1.19.2.1      onoe 					m_freem(n0);
   1162  1.19.2.1      onoe 					return NULL;
   1163  1.19.2.1      onoe 				}
   1164  1.19.2.1      onoe 				n->m_len = MLEN;
   1165  1.19.2.1      onoe 			}
   1166  1.19.2.1      onoe 			if (m0->m_pkthdr.len - off >= MINCLSIZE) {
   1167  1.19.2.1      onoe 				MCLGET(n, M_DONTWAIT);
   1168  1.19.2.1      onoe 				if (n->m_flags & M_EXT)
   1169  1.19.2.1      onoe 					n->m_len = n->m_ext.ext_size;
   1170  1.19.2.1      onoe 			}
   1171  1.19.2.1      onoe 			if (n0 == NULL) {
   1172  1.19.2.1      onoe 				newdata = (caddr_t)
   1173  1.19.2.1      onoe 				    ALIGN(n->m_data
   1174  1.19.2.1      onoe 				    + sizeof(struct ether_header))
   1175  1.19.2.1      onoe 				    - sizeof(struct ether_header);
   1176  1.19.2.1      onoe 				n->m_len -= newdata - n->m_data;
   1177  1.19.2.1      onoe 				n->m_data = newdata;
   1178  1.19.2.1      onoe 			}
   1179  1.19.2.1      onoe 			if (n->m_len > m0->m_pkthdr.len - off)
   1180  1.19.2.1      onoe 				n->m_len = m0->m_pkthdr.len - off;
   1181  1.19.2.1      onoe 			m_copydata(m0, off, n->m_len, mtod(n, caddr_t));
   1182  1.19.2.1      onoe 			off += n->m_len;
   1183  1.19.2.1      onoe 			*np = n;
   1184  1.19.2.1      onoe 			np = &n->m_next;
   1185  1.19.2.1      onoe 		}
   1186  1.19.2.1      onoe 		m_freem(m0);
   1187  1.19.2.1      onoe 		m0 = n0;
   1188  1.19.2.1      onoe 	}
   1189      1.10      onoe 	return m0;
   1190       1.1  sommerfe }
   1191       1.1  sommerfe 
   1192      1.10      onoe static void
   1193      1.10      onoe awi_input(sc, m, rxts, rssi)
   1194       1.1  sommerfe 	struct awi_softc *sc;
   1195      1.10      onoe 	struct mbuf *m;
   1196      1.10      onoe 	u_int32_t rxts;
   1197      1.10      onoe 	u_int8_t rssi;
   1198       1.1  sommerfe {
   1199      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   1200      1.10      onoe 	struct ieee80211_frame *wh;
   1201      1.10      onoe #ifndef __NetBSD__
   1202      1.10      onoe 	struct ether_header *eh;
   1203      1.10      onoe #endif
   1204       1.1  sommerfe 
   1205      1.18      onoe 	/* trim CRC here for WEP can find its own CRC at the end of packet. */
   1206      1.18      onoe 	m_adj(m, -ETHER_CRC_LEN);
   1207      1.10      onoe 	AWI_BPF_MTAP(sc, m, AWI_BPF_RAW);
   1208      1.10      onoe 	wh = mtod(m, struct ieee80211_frame *);
   1209      1.10      onoe 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
   1210      1.10      onoe 	    IEEE80211_FC0_VERSION_0) {
   1211      1.10      onoe 		printf("%s; receive packet with wrong version: %x\n",
   1212      1.10      onoe 		    sc->sc_dev.dv_xname, wh->i_fc[0]);
   1213      1.10      onoe 		m_freem(m);
   1214      1.18      onoe 		ifp->if_ierrors++;
   1215      1.10      onoe 		return;
   1216      1.10      onoe 	}
   1217      1.18      onoe 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1218      1.18      onoe 		m = awi_wep_encrypt(sc, m, 0);
   1219      1.18      onoe 		if (m == NULL) {
   1220      1.18      onoe 			ifp->if_ierrors++;
   1221      1.18      onoe 			return;
   1222      1.18      onoe 		}
   1223      1.18      onoe 		wh = mtod(m, struct ieee80211_frame *);
   1224      1.18      onoe 	}
   1225      1.10      onoe #ifdef AWI_DEBUG
   1226      1.10      onoe 	if (awi_dump)
   1227      1.10      onoe 		awi_dump_pkt(sc, m, rssi);
   1228      1.10      onoe #endif
   1229      1.10      onoe 
   1230      1.10      onoe 	if ((sc->sc_mib_local.Network_Mode || !sc->sc_no_bssid) &&
   1231      1.10      onoe 	    sc->sc_status == AWI_ST_RUNNING) {
   1232      1.10      onoe 		if (memcmp(wh->i_addr2, sc->sc_bss.bssid, ETHER_ADDR_LEN) == 0) {
   1233      1.10      onoe 			sc->sc_rx_timer = 10;
   1234      1.10      onoe 			sc->sc_bss.rssi = rssi;
   1235      1.10      onoe 		}
   1236      1.10      onoe 	}
   1237      1.10      onoe 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
   1238      1.10      onoe 	case IEEE80211_FC0_TYPE_DATA:
   1239      1.10      onoe 		if (sc->sc_mib_local.Network_Mode) {
   1240      1.10      onoe 			if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
   1241      1.10      onoe 			    IEEE80211_FC1_DIR_FROMDS) {
   1242      1.10      onoe 				m_freem(m);
   1243      1.10      onoe 				return;
   1244      1.10      onoe 			}
   1245      1.10      onoe 		} else {
   1246      1.10      onoe 			if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
   1247      1.10      onoe 			    IEEE80211_FC1_DIR_NODS) {
   1248      1.10      onoe 				m_freem(m);
   1249      1.10      onoe 				return;
   1250      1.10      onoe 			}
   1251      1.10      onoe 		}
   1252      1.10      onoe 		m = awi_fix_rxhdr(sc, m);
   1253      1.10      onoe 		if (m == NULL) {
   1254      1.10      onoe 			ifp->if_ierrors++;
   1255      1.10      onoe 			break;
   1256      1.10      onoe 		}
   1257      1.10      onoe 		ifp->if_ipackets++;
   1258      1.18      onoe #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
   1259      1.10      onoe 		AWI_BPF_MTAP(sc, m, AWI_BPF_NORM);
   1260      1.18      onoe #endif
   1261      1.10      onoe #ifdef __NetBSD__
   1262      1.10      onoe 		(*ifp->if_input)(ifp, m);
   1263      1.10      onoe #else
   1264      1.10      onoe 		eh = mtod(m, struct ether_header *);
   1265      1.10      onoe 		m_adj(m, sizeof(*eh));
   1266      1.10      onoe 		ether_input(ifp, eh, m);
   1267       1.1  sommerfe #endif
   1268      1.10      onoe 		break;
   1269      1.10      onoe 	case IEEE80211_FC0_TYPE_MGT:
   1270      1.10      onoe 		if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
   1271      1.10      onoe 		   IEEE80211_FC1_DIR_NODS) {
   1272      1.10      onoe 			m_freem(m);
   1273      1.10      onoe 			return;
   1274      1.10      onoe 		}
   1275      1.10      onoe 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
   1276      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   1277      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_BEACON:
   1278      1.10      onoe 			awi_recv_beacon(sc, m, rxts, rssi);
   1279      1.10      onoe 			break;
   1280      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_AUTH:
   1281      1.10      onoe 			awi_recv_auth(sc, m);
   1282      1.10      onoe 			break;
   1283      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
   1284      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
   1285      1.10      onoe 			awi_recv_asresp(sc, m);
   1286      1.10      onoe 			break;
   1287      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
   1288      1.10      onoe 			if (sc->sc_mib_local.Network_Mode)
   1289  1.19.2.1      onoe 				awi_send_auth(sc, 1);
   1290      1.10      onoe 			break;
   1291      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
   1292      1.10      onoe 			if (sc->sc_mib_local.Network_Mode)
   1293      1.10      onoe 				awi_send_asreq(sc, 1);
   1294      1.10      onoe 			break;
   1295      1.10      onoe 		}
   1296      1.10      onoe 		m_freem(m);
   1297      1.10      onoe 		break;
   1298      1.10      onoe 	case IEEE80211_FC0_TYPE_CTL:
   1299      1.10      onoe 	default:
   1300      1.10      onoe 		/* should not come here */
   1301      1.10      onoe 		m_freem(m);
   1302      1.10      onoe 		break;
   1303      1.10      onoe 	}
   1304       1.1  sommerfe }
   1305       1.9  sommerfe 
   1306      1.10      onoe static void
   1307      1.10      onoe awi_rxint(sc)
   1308       1.9  sommerfe 	struct awi_softc *sc;
   1309       1.9  sommerfe {
   1310      1.10      onoe 	u_int8_t state, rate, rssi;
   1311      1.10      onoe 	u_int16_t len;
   1312      1.10      onoe 	u_int32_t frame, next, rxts, rxoff;
   1313      1.10      onoe 	struct mbuf *m;
   1314      1.10      onoe 
   1315      1.10      onoe 	rxoff = sc->sc_rxdoff;
   1316      1.10      onoe 	for (;;) {
   1317      1.10      onoe 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
   1318      1.10      onoe 		if (state & AWI_RXD_ST_OWN)
   1319      1.10      onoe 			break;
   1320      1.10      onoe 		if (!(state & AWI_RXD_ST_CONSUMED)) {
   1321      1.10      onoe 			if (state & AWI_RXD_ST_RXERROR)
   1322      1.10      onoe 				sc->sc_ifp->if_ierrors++;
   1323      1.10      onoe 			else {
   1324      1.10      onoe 				len   = awi_read_2(sc, rxoff + AWI_RXD_LEN);
   1325      1.10      onoe 				rate  = awi_read_1(sc, rxoff + AWI_RXD_RATE);
   1326      1.10      onoe 				rssi  = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
   1327      1.10      onoe 				frame = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) & 0x7fff;
   1328      1.10      onoe 				rxts  = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
   1329      1.10      onoe 				m = awi_devget(sc, frame, len);
   1330      1.10      onoe 				if (state & AWI_RXD_ST_LF)
   1331      1.10      onoe 					awi_input(sc, m, rxts, rssi);
   1332      1.10      onoe 				else
   1333      1.10      onoe 					sc->sc_rxpend = m;
   1334      1.10      onoe 			}
   1335      1.10      onoe 			state |= AWI_RXD_ST_CONSUMED;
   1336      1.10      onoe 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1337      1.10      onoe 		}
   1338      1.10      onoe 		next  = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
   1339      1.10      onoe 		if (next & AWI_RXD_NEXT_LAST)
   1340      1.10      onoe 			break;
   1341      1.10      onoe 		/* make sure the next pointer is correct */
   1342      1.10      onoe 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
   1343      1.10      onoe 			break;
   1344      1.10      onoe 		state |= AWI_RXD_ST_OWN;
   1345      1.10      onoe 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1346      1.10      onoe 		rxoff = next & 0x7fff;
   1347      1.10      onoe 	}
   1348      1.10      onoe 	sc->sc_rxdoff = rxoff;
   1349       1.9  sommerfe }
   1350       1.9  sommerfe 
   1351      1.18      onoe static struct mbuf *
   1352      1.10      onoe awi_devget(sc, off, len)
   1353       1.1  sommerfe 	struct awi_softc *sc;
   1354      1.10      onoe 	u_int32_t off;
   1355      1.10      onoe 	u_int16_t len;
   1356       1.1  sommerfe {
   1357      1.10      onoe 	struct mbuf *m;
   1358      1.18      onoe 	struct mbuf *top, **mp;
   1359      1.10      onoe 	u_int tlen;
   1360      1.10      onoe 
   1361      1.10      onoe 	top = sc->sc_rxpend;
   1362      1.18      onoe 	mp = &top;
   1363      1.10      onoe 	if (top != NULL) {
   1364      1.10      onoe 		sc->sc_rxpend = NULL;
   1365      1.10      onoe 		top->m_pkthdr.len += len;
   1366  1.19.2.1      onoe 		m = top;
   1367      1.18      onoe 		while (*mp != NULL) {
   1368      1.18      onoe 			m = *mp;
   1369      1.10      onoe 			mp = &m->m_next;
   1370      1.18      onoe 		}
   1371      1.10      onoe 		if (m->m_flags & M_EXT)
   1372      1.10      onoe 			tlen = m->m_ext.ext_size;
   1373      1.10      onoe 		else if (m->m_flags & M_PKTHDR)
   1374      1.10      onoe 			tlen = MHLEN;
   1375      1.10      onoe 		else
   1376      1.10      onoe 			tlen = MLEN;
   1377      1.10      onoe 		tlen -= m->m_len;
   1378      1.10      onoe 		if (tlen > len)
   1379      1.10      onoe 			tlen = len;
   1380      1.10      onoe 		awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
   1381      1.10      onoe 		off += tlen;
   1382      1.10      onoe 		len -= tlen;
   1383      1.10      onoe 	}
   1384      1.10      onoe 
   1385      1.10      onoe 	while (len > 0) {
   1386      1.10      onoe 		if (top == NULL) {
   1387      1.10      onoe 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1388      1.10      onoe 			if (m == NULL)
   1389      1.10      onoe 				return NULL;
   1390      1.10      onoe 			m->m_pkthdr.rcvif = sc->sc_ifp;
   1391      1.10      onoe 			m->m_pkthdr.len = len;
   1392      1.10      onoe 			m->m_len = MHLEN;
   1393      1.10      onoe 		} else {
   1394      1.10      onoe 			MGET(m, M_DONTWAIT, MT_DATA);
   1395      1.10      onoe 			if (m == NULL) {
   1396      1.10      onoe 				m_freem(top);
   1397      1.10      onoe 				return NULL;
   1398      1.10      onoe 			}
   1399      1.10      onoe 			m->m_len = MLEN;
   1400      1.10      onoe 		}
   1401      1.10      onoe 		if (len >= MINCLSIZE) {
   1402      1.10      onoe 			MCLGET(m, M_DONTWAIT);
   1403      1.10      onoe 			if (m->m_flags & M_EXT)
   1404      1.10      onoe 				m->m_len = m->m_ext.ext_size;
   1405      1.10      onoe 		}
   1406  1.19.2.1      onoe 		if (top == NULL) {
   1407  1.19.2.1      onoe 			int hdrlen = sizeof(struct ieee80211_frame) +
   1408  1.19.2.1      onoe 			    (sc->sc_format_llc ? sizeof(struct llc) :
   1409  1.19.2.1      onoe 			    sizeof(struct ether_header));
   1410  1.19.2.1      onoe 			caddr_t newdata = (caddr_t)
   1411  1.19.2.1      onoe 			    ALIGN(m->m_data + hdrlen) - hdrlen;
   1412  1.19.2.1      onoe 			m->m_len -= newdata - m->m_data;
   1413  1.19.2.1      onoe 			m->m_data = newdata;
   1414  1.19.2.1      onoe 		}
   1415      1.10      onoe 		if (m->m_len > len)
   1416      1.10      onoe 			m->m_len = len;
   1417      1.10      onoe 		awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
   1418      1.10      onoe 		off += m->m_len;
   1419      1.10      onoe 		len -= m->m_len;
   1420      1.10      onoe 		*mp = m;
   1421      1.10      onoe 		mp = &m->m_next;
   1422      1.10      onoe 	}
   1423      1.10      onoe 	return top;
   1424       1.1  sommerfe }
   1425       1.1  sommerfe 
   1426      1.10      onoe /*
   1427      1.10      onoe  * Initialize hardware and start firmware to accept commands.
   1428      1.10      onoe  * Called everytime after power on firmware.
   1429      1.10      onoe  */
   1430      1.10      onoe 
   1431      1.10      onoe static int
   1432      1.10      onoe awi_init_hw(sc)
   1433       1.1  sommerfe 	struct awi_softc *sc;
   1434       1.1  sommerfe {
   1435      1.10      onoe 	u_int8_t status;
   1436      1.10      onoe 	u_int16_t intmask;
   1437      1.10      onoe 	int i, error;
   1438       1.1  sommerfe 
   1439      1.15      onoe 	sc->sc_enab_intr = 0;
   1440  1.19.2.1      onoe 	sc->sc_invalid = 0;	/* XXX: really? */
   1441      1.10      onoe 	awi_drvstate(sc, AWI_DRV_RESET);
   1442       1.1  sommerfe 
   1443      1.10      onoe 	/* reset firmware */
   1444      1.10      onoe 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1445      1.10      onoe 	DELAY(100);
   1446      1.10      onoe 	awi_write_1(sc, AWI_SELFTEST, 0);
   1447  1.19.2.1      onoe 	awi_write_1(sc, AWI_CMD, 0);
   1448  1.19.2.1      onoe 	awi_write_1(sc, AWI_BANNER, 0);
   1449      1.10      onoe 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1450      1.10      onoe 	DELAY(100);
   1451      1.10      onoe 
   1452      1.10      onoe 	/* wait for selftest completion */
   1453      1.10      onoe 	for (i = 0; ; i++) {
   1454      1.10      onoe 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
   1455      1.10      onoe 			printf("%s: failed to complete selftest (timeout)\n",
   1456      1.10      onoe 			    sc->sc_dev.dv_xname);
   1457      1.10      onoe 			return ENXIO;
   1458      1.10      onoe 		}
   1459      1.10      onoe 		status = awi_read_1(sc, AWI_SELFTEST);
   1460      1.10      onoe 		if ((status & 0xf0) == 0xf0)
   1461      1.10      onoe 			break;
   1462      1.10      onoe 		if (sc->sc_cansleep) {
   1463      1.10      onoe 			sc->sc_sleep_cnt++;
   1464      1.10      onoe 			(void)tsleep(sc, PWAIT, "awitst", 1);
   1465      1.10      onoe 			sc->sc_sleep_cnt--;
   1466      1.10      onoe 		} else {
   1467      1.10      onoe 			DELAY(1000*1000/hz);
   1468      1.10      onoe 		}
   1469      1.10      onoe 	}
   1470      1.10      onoe 	if (status != AWI_SELFTEST_PASSED) {
   1471      1.10      onoe 		printf("%s: failed to complete selftest (code %x)\n",
   1472      1.10      onoe 		    sc->sc_dev.dv_xname, status);
   1473      1.10      onoe 		return ENXIO;
   1474      1.10      onoe 	}
   1475       1.1  sommerfe 
   1476      1.10      onoe 	/* check banner to confirm firmware write it */
   1477      1.18      onoe 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
   1478      1.18      onoe 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
   1479      1.10      onoe 		printf("%s: failed to complete selftest (bad banner)\n",
   1480      1.10      onoe 		    sc->sc_dev.dv_xname);
   1481      1.10      onoe 		for (i = 0; i < AWI_BANNER_LEN; i++)
   1482      1.18      onoe 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
   1483      1.10      onoe 		printf("\n");
   1484      1.10      onoe 		return ENXIO;
   1485      1.10      onoe 	}
   1486       1.1  sommerfe 
   1487      1.10      onoe 	/* initializing interrupt */
   1488      1.15      onoe 	sc->sc_enab_intr = 1;
   1489      1.10      onoe 	error = awi_intr_lock(sc);
   1490      1.10      onoe 	if (error)
   1491      1.10      onoe 		return error;
   1492      1.10      onoe 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
   1493      1.10      onoe 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
   1494      1.10      onoe 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
   1495      1.10      onoe 	awi_write_1(sc, AWI_INTMASK2, 0);
   1496      1.10      onoe 	awi_write_1(sc, AWI_INTSTAT, 0);
   1497      1.10      onoe 	awi_write_1(sc, AWI_INTSTAT2, 0);
   1498      1.10      onoe 	awi_intr_unlock(sc);
   1499      1.10      onoe 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
   1500       1.1  sommerfe 
   1501      1.10      onoe 	/* issueing interface test command */
   1502      1.10      onoe 	error = awi_cmd(sc, AWI_CMD_NOP);
   1503      1.10      onoe 	if (error) {
   1504      1.10      onoe 		printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
   1505  1.19.2.1      onoe 		if (error == ENXIO)
   1506  1.19.2.1      onoe 			printf(" (no hardware)\n");
   1507  1.19.2.1      onoe 		else if (error != EWOULDBLOCK)
   1508      1.10      onoe 			printf(" (error %d)\n", error);
   1509      1.10      onoe 		else if (sc->sc_cansleep)
   1510      1.10      onoe 			printf(" (lost interrupt)\n");
   1511      1.10      onoe 		else
   1512      1.10      onoe 			printf(" (command timeout)\n");
   1513      1.10      onoe 	}
   1514      1.10      onoe 	return error;
   1515      1.10      onoe }
   1516       1.1  sommerfe 
   1517      1.10      onoe /*
   1518      1.10      onoe  * Extract the factory default MIB value from firmware and assign the driver
   1519      1.10      onoe  * default value.
   1520      1.10      onoe  * Called once at attaching the interface.
   1521      1.10      onoe  */
   1522       1.1  sommerfe 
   1523      1.10      onoe static int
   1524      1.10      onoe awi_init_mibs(sc)
   1525      1.10      onoe 	struct awi_softc *sc;
   1526      1.10      onoe {
   1527      1.10      onoe 	int i, error;
   1528      1.10      onoe 	u_int8_t *rate;
   1529       1.1  sommerfe 
   1530      1.10      onoe 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL)) != 0 ||
   1531      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR)) != 0 ||
   1532      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC)) != 0 ||
   1533      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT)) != 0 ||
   1534      1.10      onoe 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY)) != 0) {
   1535      1.10      onoe 		printf("%s: failed to get default mib value (error %d)\n",
   1536      1.10      onoe 		    sc->sc_dev.dv_xname, error);
   1537      1.10      onoe 		return error;
   1538      1.10      onoe 	}
   1539      1.10      onoe 
   1540      1.10      onoe 	rate = sc->sc_mib_phy.aSuprt_Data_Rates;
   1541      1.10      onoe 	sc->sc_tx_rate = AWI_RATE_1MBIT;
   1542      1.10      onoe 	for (i = 0; i < rate[1]; i++) {
   1543      1.10      onoe 		if (AWI_80211_RATE(rate[2 + i]) > sc->sc_tx_rate)
   1544      1.10      onoe 			sc->sc_tx_rate = AWI_80211_RATE(rate[2 + i]);
   1545      1.10      onoe 	}
   1546      1.10      onoe 	awi_init_region(sc);
   1547      1.10      onoe 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
   1548      1.10      onoe 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
   1549      1.10      onoe 	sc->sc_mib_local.Fragmentation_Dis = 1;
   1550      1.10      onoe 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
   1551  1.19.2.1      onoe 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
   1552      1.10      onoe 
   1553      1.10      onoe 	/* allocate buffers */
   1554      1.10      onoe 	sc->sc_txbase = AWI_BUFFERS;
   1555      1.10      onoe 	sc->sc_txend = sc->sc_txbase +
   1556      1.10      onoe 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
   1557      1.10      onoe 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
   1558      1.10      onoe 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
   1559      1.10      onoe 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
   1560      1.10      onoe 	    sc->sc_txend - sc->sc_txbase);
   1561      1.10      onoe 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
   1562      1.10      onoe 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
   1563      1.10      onoe 	    AWI_BUFFERS_END - sc->sc_txend);
   1564      1.10      onoe 	sc->sc_mib_local.Network_Mode = 1;
   1565      1.10      onoe 	sc->sc_mib_local.Acting_as_AP = 0;
   1566      1.10      onoe 	return 0;
   1567       1.1  sommerfe }
   1568       1.1  sommerfe 
   1569      1.10      onoe /*
   1570      1.10      onoe  * Start transmitter and receiver of firmware
   1571      1.10      onoe  * Called after awi_init_hw() to start operation.
   1572      1.10      onoe  */
   1573      1.10      onoe 
   1574      1.10      onoe static int
   1575      1.10      onoe awi_init_txrx(sc)
   1576       1.1  sommerfe 	struct awi_softc *sc;
   1577       1.1  sommerfe {
   1578      1.10      onoe 	int error;
   1579       1.1  sommerfe 
   1580      1.10      onoe 	/* start transmitter */
   1581      1.10      onoe 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
   1582      1.10      onoe 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
   1583      1.10      onoe 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
   1584      1.10      onoe 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
   1585      1.10      onoe 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
   1586      1.10      onoe 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
   1587      1.10      onoe 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
   1588      1.10      onoe 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
   1589      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_DATA, sc->sc_txbase);
   1590      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_MGT, 0);
   1591      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_BCAST, 0);
   1592      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_PS, 0);
   1593      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_TX_CF, 0);
   1594      1.10      onoe 	error = awi_cmd(sc, AWI_CMD_INIT_TX);
   1595      1.10      onoe 	if (error)
   1596      1.10      onoe 		return error;
   1597      1.10      onoe 
   1598      1.10      onoe 	/* start receiver */
   1599      1.10      onoe 	if (sc->sc_rxpend) {
   1600      1.10      onoe 		m_freem(sc->sc_rxpend);
   1601      1.10      onoe 		sc->sc_rxpend = NULL;
   1602      1.10      onoe 	}
   1603      1.10      onoe 	error = awi_cmd(sc, AWI_CMD_INIT_RX);
   1604      1.10      onoe 	if (error)
   1605      1.10      onoe 		return error;
   1606      1.10      onoe 	sc->sc_rxdoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_DATA_DESC);
   1607      1.10      onoe 	sc->sc_rxmoff = awi_read_4(sc, AWI_CMD_PARAMS+AWI_CA_IRX_PS_DESC);
   1608      1.10      onoe 	return 0;
   1609       1.1  sommerfe }
   1610       1.1  sommerfe 
   1611      1.10      onoe static void
   1612      1.10      onoe awi_stop_txrx(sc)
   1613       1.1  sommerfe 	struct awi_softc *sc;
   1614       1.1  sommerfe {
   1615      1.10      onoe 
   1616  1.19.2.1      onoe 	if (sc->sc_cmd_inprog)
   1617  1.19.2.1      onoe 		(void)awi_cmd_wait(sc);
   1618      1.10      onoe 	(void)awi_cmd(sc, AWI_CMD_KILL_RX);
   1619  1.19.2.1      onoe 	(void)awi_cmd_wait(sc);
   1620  1.19.2.1      onoe 	sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
   1621      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_DATA, 1);
   1622      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_MGT, 0);
   1623      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_BCAST, 0);
   1624      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_PS, 0);
   1625      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_FTX_CF, 0);
   1626      1.10      onoe 	(void)awi_cmd(sc, AWI_CMD_FLUSH_TX);
   1627  1.19.2.1      onoe 	(void)awi_cmd_wait(sc);
   1628       1.1  sommerfe }
   1629       1.1  sommerfe 
   1630      1.18      onoe int
   1631      1.10      onoe awi_init_region(sc)
   1632       1.1  sommerfe 	struct awi_softc *sc;
   1633       1.1  sommerfe {
   1634       1.1  sommerfe 
   1635      1.10      onoe 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1636      1.10      onoe 		switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
   1637      1.10      onoe 		case AWI_REG_DOMAIN_US:
   1638      1.10      onoe 		case AWI_REG_DOMAIN_CA:
   1639      1.10      onoe 		case AWI_REG_DOMAIN_EU:
   1640      1.10      onoe 			sc->sc_scan_min = 0;
   1641      1.10      onoe 			sc->sc_scan_max = 77;
   1642      1.10      onoe 			break;
   1643      1.10      onoe 		case AWI_REG_DOMAIN_ES:
   1644      1.10      onoe 			sc->sc_scan_min = 0;
   1645      1.10      onoe 			sc->sc_scan_max = 26;
   1646      1.10      onoe 			break;
   1647      1.10      onoe 		case AWI_REG_DOMAIN_FR:
   1648      1.10      onoe 			sc->sc_scan_min = 0;
   1649      1.10      onoe 			sc->sc_scan_max = 32;
   1650      1.10      onoe 			break;
   1651      1.10      onoe 		case AWI_REG_DOMAIN_JP:
   1652      1.10      onoe 			sc->sc_scan_min = 6;
   1653      1.10      onoe 			sc->sc_scan_max = 17;
   1654      1.10      onoe 			break;
   1655      1.10      onoe 		default:
   1656      1.10      onoe 			return EINVAL;
   1657      1.10      onoe 		}
   1658      1.10      onoe 		sc->sc_scan_set = sc->sc_scan_cur % 3 + 1;
   1659      1.10      onoe 	} else {
   1660      1.10      onoe 		switch (sc->sc_mib_phy.aCurrent_Reg_Domain) {
   1661      1.10      onoe 		case AWI_REG_DOMAIN_US:
   1662      1.10      onoe 		case AWI_REG_DOMAIN_CA:
   1663      1.10      onoe 			sc->sc_scan_min = 1;
   1664      1.10      onoe 			sc->sc_scan_max = 11;
   1665      1.10      onoe 			sc->sc_scan_cur = 3;
   1666      1.10      onoe 			break;
   1667      1.10      onoe 		case AWI_REG_DOMAIN_EU:
   1668      1.10      onoe 			sc->sc_scan_min = 1;
   1669      1.10      onoe 			sc->sc_scan_max = 13;
   1670      1.10      onoe 			sc->sc_scan_cur = 3;
   1671      1.10      onoe 			break;
   1672      1.10      onoe 		case AWI_REG_DOMAIN_ES:
   1673      1.10      onoe 			sc->sc_scan_min = 10;
   1674      1.10      onoe 			sc->sc_scan_max = 11;
   1675      1.10      onoe 			sc->sc_scan_cur = 10;
   1676      1.10      onoe 			break;
   1677      1.10      onoe 		case AWI_REG_DOMAIN_FR:
   1678      1.10      onoe 			sc->sc_scan_min = 10;
   1679      1.10      onoe 			sc->sc_scan_max = 13;
   1680      1.10      onoe 			sc->sc_scan_cur = 10;
   1681      1.10      onoe 			break;
   1682      1.10      onoe 		case AWI_REG_DOMAIN_JP:
   1683      1.10      onoe 			sc->sc_scan_min = 14;
   1684      1.10      onoe 			sc->sc_scan_max = 14;
   1685      1.10      onoe 			sc->sc_scan_cur = 14;
   1686      1.10      onoe 			break;
   1687      1.10      onoe 		default:
   1688      1.10      onoe 			return EINVAL;
   1689      1.10      onoe 		}
   1690      1.10      onoe 	}
   1691      1.18      onoe 	sc->sc_ownch = sc->sc_scan_cur;
   1692      1.10      onoe 	return 0;
   1693       1.1  sommerfe }
   1694       1.1  sommerfe 
   1695      1.10      onoe static int
   1696      1.10      onoe awi_start_scan(sc)
   1697       1.1  sommerfe 	struct awi_softc *sc;
   1698       1.1  sommerfe {
   1699      1.10      onoe 	int error = 0;
   1700      1.18      onoe 	struct awi_bss *bp;
   1701      1.10      onoe 
   1702      1.18      onoe 	while ((bp = TAILQ_FIRST(&sc->sc_scan)) != NULL) {
   1703      1.18      onoe 		TAILQ_REMOVE(&sc->sc_scan, bp, list);
   1704      1.18      onoe 		free(bp, M_DEVBUF);
   1705      1.18      onoe 	}
   1706      1.10      onoe 	if (!sc->sc_mib_local.Network_Mode && sc->sc_no_bssid) {
   1707      1.10      onoe 		memset(&sc->sc_bss, 0, sizeof(sc->sc_bss));
   1708  1.19.2.1      onoe 		sc->sc_bss.essid[0] = IEEE80211_ELEMID_SSID;
   1709      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1710      1.18      onoe 			sc->sc_bss.chanset = sc->sc_ownch % 3 + 1;
   1711      1.18      onoe 			sc->sc_bss.pattern = sc->sc_ownch;
   1712      1.10      onoe 			sc->sc_bss.index = 1;
   1713  1.19.2.1      onoe 			sc->sc_bss.dwell_time = 200;	/*XXX*/
   1714      1.10      onoe 		} else
   1715      1.18      onoe 			sc->sc_bss.chanset = sc->sc_ownch;
   1716      1.10      onoe 		sc->sc_status = AWI_ST_SETSS;
   1717      1.10      onoe 		error = awi_set_ss(sc);
   1718      1.10      onoe 	} else {
   1719      1.10      onoe 		if (sc->sc_mib_local.Network_Mode)
   1720      1.10      onoe 			awi_drvstate(sc, AWI_DRV_INFSC);
   1721      1.10      onoe 		else
   1722      1.10      onoe 			awi_drvstate(sc, AWI_DRV_ADHSC);
   1723      1.10      onoe 		sc->sc_start_bss = 0;
   1724      1.10      onoe 		sc->sc_active_scan = 1;
   1725      1.10      onoe 		sc->sc_mgt_timer = AWI_ASCAN_WAIT / 1000;
   1726      1.10      onoe 		sc->sc_ifp->if_timer = 1;
   1727      1.10      onoe 		sc->sc_status = AWI_ST_SCAN;
   1728      1.10      onoe 		error = awi_cmd_scan(sc);
   1729      1.10      onoe 	}
   1730      1.10      onoe 	return error;
   1731       1.1  sommerfe }
   1732       1.1  sommerfe 
   1733      1.10      onoe static int
   1734      1.10      onoe awi_next_scan(sc)
   1735      1.10      onoe 	struct awi_softc *sc;
   1736      1.10      onoe {
   1737      1.10      onoe 	int error;
   1738       1.1  sommerfe 
   1739      1.10      onoe 	for (;;) {
   1740      1.10      onoe 		/*
   1741      1.10      onoe 		 * The pattern parameter for FH phy should be incremented
   1742      1.10      onoe 		 * by 3.  But BayStack 650 Access Points apparently always
   1743      1.10      onoe 		 * assign hop pattern set parameter to 1 for any pattern.
   1744      1.10      onoe 		 * So we try all combinations of pattern/set parameters.
   1745      1.10      onoe 		 * Since this causes no error, it may be a bug of
   1746      1.10      onoe 		 * PCnetMobile firmware.
   1747      1.10      onoe 		 */
   1748      1.10      onoe 		sc->sc_scan_cur++;
   1749      1.10      onoe 		if (sc->sc_scan_cur > sc->sc_scan_max) {
   1750      1.10      onoe 			sc->sc_scan_cur = sc->sc_scan_min;
   1751      1.10      onoe 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
   1752  1.19.2.1      onoe 				sc->sc_scan_set = sc->sc_scan_set % 3 + 1;
   1753      1.10      onoe 		}
   1754      1.10      onoe 		error = awi_cmd_scan(sc);
   1755      1.10      onoe 		if (error != EINVAL)
   1756      1.10      onoe 			break;
   1757      1.10      onoe 	}
   1758      1.10      onoe 	return error;
   1759      1.10      onoe }
   1760       1.1  sommerfe 
   1761      1.10      onoe static void
   1762      1.10      onoe awi_stop_scan(sc)
   1763       1.1  sommerfe 	struct awi_softc *sc;
   1764       1.1  sommerfe {
   1765       1.1  sommerfe 	struct ifnet *ifp = sc->sc_ifp;
   1766      1.10      onoe 	struct awi_bss *bp, *sbp;
   1767  1.19.2.1      onoe 	int fail;
   1768      1.10      onoe 
   1769      1.10      onoe 	bp = TAILQ_FIRST(&sc->sc_scan);
   1770      1.10      onoe 	if (bp == NULL) {
   1771      1.10      onoe   notfound:
   1772      1.10      onoe 		if (sc->sc_active_scan) {
   1773      1.10      onoe 			if (ifp->if_flags & IFF_DEBUG)
   1774      1.10      onoe 				printf("%s: entering passive scan mode\n",
   1775      1.10      onoe 				    sc->sc_dev.dv_xname);
   1776      1.10      onoe 			sc->sc_active_scan = 0;
   1777      1.10      onoe 		}
   1778      1.10      onoe 		sc->sc_mgt_timer = AWI_PSCAN_WAIT / 1000;
   1779      1.10      onoe 		ifp->if_timer = 1;
   1780      1.10      onoe 		(void)awi_next_scan(sc);
   1781      1.10      onoe 		return;
   1782      1.10      onoe 	}
   1783      1.10      onoe 	sbp = NULL;
   1784  1.19.2.1      onoe 	if (ifp->if_flags & IFF_DEBUG)
   1785  1.19.2.1      onoe 		printf("%s:\tmacaddr     ch/pat   sig flag  wep  essid\n",
   1786  1.19.2.1      onoe 		    sc->sc_dev.dv_xname);
   1787      1.10      onoe 	for (; bp != NULL; bp = TAILQ_NEXT(bp, list)) {
   1788      1.10      onoe 		if (bp->fails) {
   1789      1.10      onoe 			/*
   1790      1.10      onoe 			 * The configuration of the access points may change
   1791      1.10      onoe 			 * during my scan.  So we retries to associate with
   1792      1.10      onoe 			 * it unless there are any suitable AP.
   1793      1.10      onoe 			 */
   1794      1.18      onoe 			if (bp->fails++ < 3)
   1795      1.10      onoe 				continue;
   1796      1.10      onoe 			bp->fails = 0;
   1797      1.10      onoe 		}
   1798  1.19.2.1      onoe 		fail = 0;
   1799      1.10      onoe 		/*
   1800      1.10      onoe 		 * Since the firmware apparently scans not only the specified
   1801      1.10      onoe 		 * channel of SCAN command but all available channel within
   1802      1.10      onoe 		 * the region, we should filter out unnecessary responses here.
   1803      1.10      onoe 		 */
   1804      1.10      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1805      1.10      onoe 			if (bp->pattern < sc->sc_scan_min ||
   1806      1.10      onoe 			    bp->pattern > sc->sc_scan_max)
   1807  1.19.2.1      onoe 				fail |= 0x01;
   1808      1.10      onoe 		} else {
   1809      1.10      onoe 			if (bp->chanset < sc->sc_scan_min ||
   1810      1.10      onoe 			    bp->chanset > sc->sc_scan_max)
   1811  1.19.2.1      onoe 				fail |= 0x01;
   1812  1.19.2.1      onoe 		}
   1813  1.19.2.1      onoe 		if (sc->sc_mib_local.Network_Mode) {
   1814  1.19.2.1      onoe 			if (!(bp->capinfo & IEEE80211_CAPINFO_ESS) ||
   1815  1.19.2.1      onoe 			    (bp->capinfo & IEEE80211_CAPINFO_IBSS))
   1816  1.19.2.1      onoe 				fail |= 0x02;
   1817  1.19.2.1      onoe 		} else {
   1818  1.19.2.1      onoe 			if ((bp->capinfo & IEEE80211_CAPINFO_ESS) ||
   1819  1.19.2.1      onoe 			    !(bp->capinfo & IEEE80211_CAPINFO_IBSS))
   1820  1.19.2.1      onoe 				fail |= 0x02;
   1821  1.19.2.1      onoe 		}
   1822  1.19.2.1      onoe 		if (sc->sc_wep_algo == NULL) {
   1823  1.19.2.1      onoe 			if (bp->capinfo & IEEE80211_CAPINFO_PRIVACY)
   1824  1.19.2.1      onoe 				fail |= 0x04;
   1825  1.19.2.1      onoe 		} else {
   1826  1.19.2.1      onoe 			if (!(bp->capinfo & IEEE80211_CAPINFO_PRIVACY))
   1827  1.19.2.1      onoe 				fail |= 0x04;
   1828  1.19.2.1      onoe 		}
   1829  1.19.2.1      onoe 		if (sc->sc_mib_mac.aDesired_ESS_ID[1] != 0 &&
   1830  1.19.2.1      onoe 		    memcmp(&sc->sc_mib_mac.aDesired_ESS_ID, bp->essid,
   1831  1.19.2.1      onoe 		    sizeof(bp->essid)) != 0)
   1832  1.19.2.1      onoe 			fail |= 0x08;
   1833  1.19.2.1      onoe 		if (ifp->if_flags & IFF_DEBUG) {
   1834  1.19.2.1      onoe 			printf(" %c %s", fail ? '-' : '+',
   1835  1.19.2.1      onoe 			    ether_sprintf(bp->esrc));
   1836  1.19.2.1      onoe 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
   1837  1.19.2.1      onoe 				printf("  %2d/%d%c", bp->pattern, bp->chanset,
   1838  1.19.2.1      onoe 				    fail & 0x01 ? '!' : ' ');
   1839  1.19.2.1      onoe 			else
   1840  1.19.2.1      onoe 				printf("  %4d%c", bp->chanset,
   1841  1.19.2.1      onoe 				    fail & 0x01 ? '!' : ' ');
   1842  1.19.2.1      onoe 			printf(" %+4d", bp->rssi);
   1843  1.19.2.1      onoe 			printf(" %4s%c",
   1844  1.19.2.1      onoe 			    (bp->capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
   1845  1.19.2.1      onoe 			    (bp->capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
   1846  1.19.2.1      onoe 			    "????",
   1847  1.19.2.1      onoe 			    fail & 0x02 ? '!' : ' ');
   1848  1.19.2.1      onoe 			printf(" %3s%c ",
   1849  1.19.2.1      onoe 			    (bp->capinfo & IEEE80211_CAPINFO_PRIVACY) ? "wep" :
   1850  1.19.2.1      onoe 			    "no",
   1851  1.19.2.1      onoe 			    fail & 0x04 ? '!' : ' ');
   1852  1.19.2.1      onoe 			awi_print_essid(bp->essid);
   1853  1.19.2.1      onoe 			printf("%s\n", fail & 0x08 ? "!" : "");
   1854  1.19.2.1      onoe 		}
   1855  1.19.2.1      onoe 		if (!fail) {
   1856  1.19.2.1      onoe 			if (sbp == NULL || bp->rssi > sbp->rssi)
   1857  1.19.2.1      onoe 				sbp = bp;
   1858      1.10      onoe 		}
   1859      1.10      onoe 	}
   1860      1.10      onoe 	if (sbp == NULL)
   1861      1.10      onoe 		goto notfound;
   1862      1.10      onoe 	sc->sc_bss = *sbp;
   1863      1.10      onoe 	(void)awi_set_ss(sc);
   1864      1.10      onoe }
   1865       1.1  sommerfe 
   1866      1.10      onoe static void
   1867      1.10      onoe awi_recv_beacon(sc, m0, rxts, rssi)
   1868      1.10      onoe 	struct awi_softc *sc;
   1869      1.10      onoe 	struct mbuf *m0;
   1870      1.10      onoe 	u_int32_t rxts;
   1871      1.10      onoe 	u_int8_t rssi;
   1872      1.10      onoe {
   1873      1.10      onoe 	struct ieee80211_frame *wh;
   1874      1.10      onoe 	struct awi_bss *bp;
   1875      1.10      onoe 	u_int8_t *frame, *eframe;
   1876  1.19.2.1      onoe 	u_int8_t *tstamp, *bintval, *capinfo, *ssid, *rates, *parms;
   1877       1.1  sommerfe 
   1878      1.10      onoe 	if (sc->sc_status != AWI_ST_SCAN)
   1879      1.10      onoe 		return;
   1880      1.10      onoe 	wh = mtod(m0, struct ieee80211_frame *);
   1881       1.1  sommerfe 
   1882      1.10      onoe 	frame = (u_int8_t *)&wh[1];
   1883      1.10      onoe 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
   1884      1.10      onoe 	/*
   1885      1.10      onoe 	 * XXX:
   1886      1.10      onoe 	 *	timestamp [8]
   1887      1.10      onoe 	 *	beacon interval [2]
   1888      1.10      onoe 	 *	capability information [2]
   1889      1.10      onoe 	 *	ssid [tlv]
   1890      1.10      onoe 	 *	supported rates [tlv]
   1891      1.10      onoe 	 *	parameter set [tlv]
   1892      1.10      onoe 	 *	...
   1893      1.10      onoe 	 */
   1894      1.10      onoe 	if (frame + 12 > eframe) {
   1895      1.10      onoe #ifdef AWI_DEBUG
   1896      1.10      onoe 		if (awi_verbose)
   1897      1.10      onoe 			printf("awi_recv_beacon: frame too short \n");
   1898      1.10      onoe #endif
   1899      1.10      onoe 		return;
   1900      1.10      onoe 	}
   1901      1.10      onoe 	tstamp = frame;
   1902      1.10      onoe 	frame += 8;
   1903  1.19.2.1      onoe 	bintval = frame;
   1904      1.10      onoe 	frame += 2;
   1905      1.10      onoe 	capinfo = frame;
   1906      1.10      onoe 	frame += 2;
   1907      1.10      onoe 
   1908      1.10      onoe 	ssid = rates = parms = NULL;
   1909      1.10      onoe 	while (frame < eframe) {
   1910      1.10      onoe 		switch (*frame) {
   1911      1.10      onoe 		case IEEE80211_ELEMID_SSID:
   1912      1.10      onoe 			ssid = frame;
   1913      1.10      onoe 			break;
   1914      1.10      onoe 		case IEEE80211_ELEMID_RATES:
   1915      1.10      onoe 			rates = frame;
   1916      1.10      onoe 			break;
   1917      1.10      onoe 		case IEEE80211_ELEMID_FHPARMS:
   1918      1.10      onoe 		case IEEE80211_ELEMID_DSPARMS:
   1919      1.10      onoe 			parms = frame;
   1920      1.10      onoe 			break;
   1921      1.10      onoe 		}
   1922      1.10      onoe 		frame += frame[1] + 2;
   1923      1.10      onoe 	}
   1924      1.10      onoe 	if (ssid == NULL || rates == NULL || parms == NULL) {
   1925      1.10      onoe #ifdef AWI_DEBUG
   1926      1.10      onoe 		if (awi_verbose)
   1927      1.10      onoe 			printf("awi_recv_beacon: ssid=%p, rates=%p, parms=%p\n",
   1928      1.10      onoe 			    ssid, rates, parms);
   1929      1.10      onoe #endif
   1930      1.10      onoe 		return;
   1931      1.10      onoe 	}
   1932      1.10      onoe 	if (ssid[1] > IEEE80211_NWID_LEN) {
   1933      1.10      onoe #ifdef AWI_DEBUG
   1934      1.10      onoe 		if (awi_verbose)
   1935      1.10      onoe 			printf("awi_recv_beacon: bad ssid len: %d from %s\n",
   1936      1.10      onoe 			    ssid[1], ether_sprintf(wh->i_addr2));
   1937      1.10      onoe #endif
   1938       1.1  sommerfe 		return;
   1939       1.1  sommerfe 	}
   1940      1.10      onoe 
   1941      1.10      onoe 	for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
   1942      1.10      onoe 	    bp = TAILQ_NEXT(bp, list)) {
   1943      1.10      onoe 		if (memcmp(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN) == 0 &&
   1944      1.10      onoe 		    memcmp(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN) == 0)
   1945      1.10      onoe 			break;
   1946       1.1  sommerfe 	}
   1947      1.10      onoe 	if (bp == NULL) {
   1948      1.10      onoe 		bp = malloc(sizeof(struct awi_bss), M_DEVBUF, M_NOWAIT);
   1949      1.10      onoe 		if (bp == NULL)
   1950      1.10      onoe 			return;
   1951      1.10      onoe 		TAILQ_INSERT_TAIL(&sc->sc_scan, bp, list);
   1952      1.10      onoe 		memcpy(bp->esrc, wh->i_addr2, ETHER_ADDR_LEN);
   1953      1.10      onoe 		memcpy(bp->bssid, wh->i_addr3, ETHER_ADDR_LEN);
   1954      1.10      onoe 		memset(bp->essid, 0, sizeof(bp->essid));
   1955      1.10      onoe 		memcpy(bp->essid, ssid, 2 + ssid[1]);
   1956      1.10      onoe 	}
   1957      1.10      onoe 	bp->rssi = rssi;
   1958      1.10      onoe 	bp->rxtime = rxts;
   1959      1.10      onoe 	memcpy(bp->timestamp, tstamp, sizeof(bp->timestamp));
   1960  1.19.2.1      onoe 	bp->interval = LE_READ_2(bintval);
   1961  1.19.2.1      onoe 	bp->capinfo = LE_READ_2(capinfo);
   1962      1.10      onoe 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1963      1.10      onoe 		bp->chanset = parms[4];
   1964      1.10      onoe 		bp->pattern = parms[5];
   1965      1.10      onoe 		bp->index = parms[6];
   1966      1.10      onoe 		bp->dwell_time = LE_READ_2(parms + 2);
   1967       1.1  sommerfe 	} else {
   1968      1.10      onoe 		bp->chanset = parms[2];
   1969      1.10      onoe 		bp->pattern = 0;
   1970      1.10      onoe 		bp->index = 0;
   1971      1.10      onoe 		bp->dwell_time = 0;
   1972       1.1  sommerfe 	}
   1973      1.10      onoe 	if (sc->sc_mgt_timer == 0)
   1974      1.10      onoe 		awi_stop_scan(sc);
   1975       1.1  sommerfe }
   1976       1.1  sommerfe 
   1977      1.10      onoe static int
   1978      1.10      onoe awi_set_ss(sc)
   1979       1.1  sommerfe 	struct awi_softc *sc;
   1980       1.1  sommerfe {
   1981      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   1982      1.10      onoe 	struct awi_bss *bp;
   1983      1.10      onoe 	int error;
   1984       1.1  sommerfe 
   1985      1.10      onoe 	sc->sc_status = AWI_ST_SETSS;
   1986      1.10      onoe 	bp = &sc->sc_bss;
   1987      1.10      onoe 	if (ifp->if_flags & IFF_DEBUG) {
   1988  1.19.2.1      onoe 		printf("%s: ch %d pat %d id %d dw %d iv %d bss %s ssid ",
   1989      1.10      onoe 		    sc->sc_dev.dv_xname, bp->chanset,
   1990      1.10      onoe 		    bp->pattern, bp->index, bp->dwell_time, bp->interval,
   1991  1.19.2.1      onoe 		    ether_sprintf(bp->bssid));
   1992  1.19.2.1      onoe 		awi_print_essid(bp->essid);
   1993  1.19.2.1      onoe 		printf("\n");
   1994      1.10      onoe 	}
   1995      1.10      onoe 	memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bp->bssid, ETHER_ADDR_LEN);
   1996      1.10      onoe 	memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID, bp->essid,
   1997      1.10      onoe 	    AWI_ESS_ID_SIZE);
   1998      1.10      onoe 	LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period, bp->interval);
   1999      1.10      onoe 	error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
   2000      1.10      onoe 	return error;
   2001       1.1  sommerfe }
   2002       1.1  sommerfe 
   2003      1.10      onoe static void
   2004      1.10      onoe awi_try_sync(sc)
   2005       1.1  sommerfe 	struct awi_softc *sc;
   2006       1.1  sommerfe {
   2007      1.10      onoe 	struct awi_bss *bp;
   2008      1.10      onoe 
   2009      1.10      onoe 	sc->sc_status = AWI_ST_SYNC;
   2010      1.10      onoe 	bp = &sc->sc_bss;
   2011      1.10      onoe 
   2012      1.10      onoe 	if (sc->sc_cmd_inprog) {
   2013      1.10      onoe 		if (awi_cmd_wait(sc))
   2014      1.10      onoe 			return;
   2015       1.1  sommerfe 	}
   2016  1.19.2.1      onoe 	sc->sc_cmd_inprog = AWI_CMD_SYNC;
   2017      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_SET, bp->chanset);
   2018      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_PATTERN, bp->pattern);
   2019      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_IDX, bp->index);
   2020      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_STARTBSS,
   2021      1.10      onoe 	    sc->sc_start_bss ? 1 : 0);
   2022      1.10      onoe 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_DWELL, bp->dwell_time);
   2023      1.10      onoe 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_MBZ, 0);
   2024      1.10      onoe 	awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_TIMESTAMP,
   2025      1.10      onoe 	    bp->timestamp, 8);
   2026      1.10      onoe 	awi_write_4(sc, AWI_CMD_PARAMS+AWI_CA_SYNC_REFTIME, bp->rxtime);
   2027      1.10      onoe 	(void)awi_cmd(sc, AWI_CMD_SYNC);
   2028       1.1  sommerfe }
   2029       1.1  sommerfe 
   2030      1.10      onoe static void
   2031      1.10      onoe awi_sync_done(sc)
   2032       1.1  sommerfe 	struct awi_softc *sc;
   2033       1.1  sommerfe {
   2034      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   2035       1.1  sommerfe 
   2036      1.10      onoe 	if (sc->sc_mib_local.Network_Mode) {
   2037      1.10      onoe 		awi_drvstate(sc, AWI_DRV_INFSY);
   2038  1.19.2.1      onoe 		awi_send_auth(sc, 1);
   2039      1.10      onoe 	} else {
   2040  1.19.2.3      onoe 		if (ifp->if_flags & IFF_DEBUG) {
   2041  1.19.2.3      onoe 			printf("%s: synced with", sc->sc_dev.dv_xname);
   2042  1.19.2.3      onoe 			if (sc->sc_no_bssid)
   2043  1.19.2.3      onoe 				printf(" no-bssid");
   2044  1.19.2.3      onoe 			else {
   2045  1.19.2.3      onoe 				printf(" %s ssid ",
   2046  1.19.2.3      onoe 				    ether_sprintf(sc->sc_bss.bssid));
   2047  1.19.2.3      onoe 				awi_print_essid(sc->sc_bss.essid);
   2048  1.19.2.3      onoe 			}
   2049  1.19.2.3      onoe 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
   2050  1.19.2.3      onoe 				printf(" at chanset %d pattern %d\n",
   2051  1.19.2.3      onoe 				    sc->sc_bss.chanset, sc->sc_bss.pattern);
   2052  1.19.2.3      onoe 			else
   2053  1.19.2.3      onoe 				printf(" at channel %d\n", sc->sc_bss.chanset);
   2054  1.19.2.1      onoe 		}
   2055      1.10      onoe 		awi_drvstate(sc, AWI_DRV_ADHSY);
   2056      1.10      onoe 		sc->sc_status = AWI_ST_RUNNING;
   2057      1.10      onoe 		ifp->if_flags |= IFF_RUNNING;
   2058      1.10      onoe 		awi_start(ifp);
   2059       1.1  sommerfe 	}
   2060       1.1  sommerfe }
   2061       1.1  sommerfe 
   2062      1.10      onoe static void
   2063      1.10      onoe awi_send_deauth(sc)
   2064       1.1  sommerfe 	struct awi_softc *sc;
   2065       1.1  sommerfe {
   2066      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   2067      1.10      onoe 	struct mbuf *m;
   2068      1.10      onoe 	struct ieee80211_frame *wh;
   2069      1.10      onoe 	u_int8_t *deauth;
   2070      1.10      onoe 
   2071      1.10      onoe 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2072      1.10      onoe 	if (m == NULL)
   2073      1.10      onoe 		return;
   2074      1.10      onoe 	if (ifp->if_flags & IFF_DEBUG)
   2075      1.10      onoe 		printf("%s: sending deauth to %s\n", sc->sc_dev.dv_xname,
   2076      1.10      onoe 		    ether_sprintf(sc->sc_bss.bssid));
   2077      1.10      onoe 
   2078      1.10      onoe 	wh = mtod(m, struct ieee80211_frame *);
   2079      1.10      onoe 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   2080      1.10      onoe 	    IEEE80211_FC0_SUBTYPE_AUTH;
   2081      1.10      onoe 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2082      1.10      onoe 	LE_WRITE_2(wh->i_dur, 0);
   2083      1.10      onoe 	LE_WRITE_2(wh->i_seq, 0);
   2084      1.10      onoe 	memcpy(wh->i_addr1, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   2085      1.10      onoe 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
   2086      1.10      onoe 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   2087      1.10      onoe 
   2088      1.10      onoe 	deauth = (u_int8_t *)&wh[1];
   2089      1.10      onoe 	LE_WRITE_2(deauth, IEEE80211_REASON_AUTH_LEAVE);
   2090      1.10      onoe 	deauth += 2;
   2091       1.1  sommerfe 
   2092      1.10      onoe 	m->m_pkthdr.len = m->m_len = deauth - mtod(m, u_int8_t *);
   2093      1.10      onoe 	IF_ENQUEUE(&sc->sc_mgtq, m);
   2094      1.10      onoe 	awi_start(ifp);
   2095      1.10      onoe 	awi_drvstate(sc, AWI_DRV_INFTOSS);
   2096       1.1  sommerfe }
   2097       1.1  sommerfe 
   2098      1.10      onoe static void
   2099  1.19.2.1      onoe awi_send_auth(sc, seq)
   2100       1.1  sommerfe 	struct awi_softc *sc;
   2101  1.19.2.1      onoe 	int seq;
   2102       1.1  sommerfe {
   2103      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   2104      1.10      onoe 	struct mbuf *m;
   2105      1.10      onoe 	struct ieee80211_frame *wh;
   2106      1.10      onoe 	u_int8_t *auth;
   2107      1.10      onoe 
   2108      1.10      onoe 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2109      1.10      onoe 	if (m == NULL)
   2110       1.1  sommerfe 		return;
   2111      1.10      onoe 	sc->sc_status = AWI_ST_AUTH;
   2112      1.10      onoe 	if (ifp->if_flags & IFF_DEBUG)
   2113      1.10      onoe 		printf("%s: sending auth to %s\n", sc->sc_dev.dv_xname,
   2114      1.10      onoe 		    ether_sprintf(sc->sc_bss.bssid));
   2115      1.10      onoe 
   2116      1.10      onoe 	wh = mtod(m, struct ieee80211_frame *);
   2117      1.10      onoe 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   2118      1.10      onoe 	    IEEE80211_FC0_SUBTYPE_AUTH;
   2119      1.10      onoe 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2120      1.10      onoe 	LE_WRITE_2(wh->i_dur, 0);
   2121      1.10      onoe 	LE_WRITE_2(wh->i_seq, 0);
   2122  1.19.2.1      onoe 	memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
   2123      1.10      onoe 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
   2124      1.10      onoe 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   2125      1.10      onoe 
   2126      1.10      onoe 	auth = (u_int8_t *)&wh[1];
   2127      1.10      onoe 	/* algorithm number */
   2128      1.10      onoe 	LE_WRITE_2(auth, IEEE80211_AUTH_ALG_OPEN);
   2129      1.10      onoe 	auth += 2;
   2130      1.10      onoe 	/* sequence number */
   2131  1.19.2.1      onoe 	LE_WRITE_2(auth, seq);
   2132      1.10      onoe 	auth += 2;
   2133      1.10      onoe 	/* status */
   2134      1.10      onoe 	LE_WRITE_2(auth, 0);
   2135      1.10      onoe 	auth += 2;
   2136      1.10      onoe 
   2137      1.10      onoe 	m->m_pkthdr.len = m->m_len = auth - mtod(m, u_int8_t *);
   2138      1.10      onoe 	IF_ENQUEUE(&sc->sc_mgtq, m);
   2139      1.10      onoe 	awi_start(ifp);
   2140      1.10      onoe 
   2141      1.10      onoe 	sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
   2142      1.10      onoe 	ifp->if_timer = 1;
   2143       1.1  sommerfe }
   2144       1.1  sommerfe 
   2145      1.10      onoe static void
   2146      1.10      onoe awi_recv_auth(sc, m0)
   2147       1.1  sommerfe 	struct awi_softc *sc;
   2148      1.10      onoe 	struct mbuf *m0;
   2149       1.1  sommerfe {
   2150      1.10      onoe 	struct ieee80211_frame *wh;
   2151      1.10      onoe 	u_int8_t *auth, *eframe;
   2152      1.10      onoe 	struct awi_bss *bp;
   2153      1.10      onoe 	u_int16_t status;
   2154      1.10      onoe 
   2155      1.10      onoe 	wh = mtod(m0, struct ieee80211_frame *);
   2156      1.10      onoe 	auth = (u_int8_t *)&wh[1];
   2157      1.10      onoe 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
   2158      1.10      onoe 	if (sc->sc_ifp->if_flags & IFF_DEBUG)
   2159      1.10      onoe 		printf("%s: receive auth from %s\n", sc->sc_dev.dv_xname,
   2160      1.10      onoe 		    ether_sprintf(wh->i_addr2));
   2161       1.1  sommerfe 
   2162  1.19.2.1      onoe 	/* algorithm number */
   2163  1.19.2.1      onoe 	if (LE_READ_2(auth) != IEEE80211_AUTH_ALG_OPEN)
   2164  1.19.2.1      onoe 		return;
   2165  1.19.2.1      onoe 	auth += 2;
   2166      1.10      onoe 	if (!sc->sc_mib_local.Network_Mode) {
   2167  1.19.2.1      onoe 		if (sc->sc_status != AWI_ST_RUNNING)
   2168  1.19.2.1      onoe 			return;
   2169  1.19.2.1      onoe 		if (LE_READ_2(auth) == 1)
   2170  1.19.2.1      onoe 			awi_send_auth(sc, 2);
   2171      1.10      onoe 		return;
   2172      1.10      onoe 	}
   2173      1.10      onoe 	if (sc->sc_status != AWI_ST_AUTH)
   2174      1.10      onoe 		return;
   2175      1.10      onoe 	/* sequence number */
   2176      1.10      onoe 	if (LE_READ_2(auth) != 2)
   2177      1.10      onoe 		return;
   2178      1.10      onoe 	auth += 2;
   2179      1.10      onoe 	/* status */
   2180      1.10      onoe 	status = LE_READ_2(auth);
   2181      1.10      onoe 	if (status != 0) {
   2182      1.10      onoe 		printf("%s: authentication failed (reason %d)\n",
   2183      1.10      onoe 		    sc->sc_dev.dv_xname, status);
   2184      1.10      onoe 		for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
   2185      1.10      onoe 		    bp = TAILQ_NEXT(bp, list)) {
   2186      1.10      onoe 			if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
   2187      1.10      onoe 			    == 0) {
   2188      1.10      onoe 				bp->fails++;
   2189      1.10      onoe 				break;
   2190      1.10      onoe 			}
   2191      1.10      onoe 		}
   2192       1.1  sommerfe 		return;
   2193       1.1  sommerfe 	}
   2194      1.10      onoe 	sc->sc_mgt_timer = 0;
   2195      1.10      onoe 	awi_drvstate(sc, AWI_DRV_INFAUTH);
   2196      1.10      onoe 	awi_send_asreq(sc, 0);
   2197       1.1  sommerfe }
   2198       1.1  sommerfe 
   2199      1.10      onoe static void
   2200      1.10      onoe awi_send_asreq(sc, reassoc)
   2201       1.1  sommerfe 	struct awi_softc *sc;
   2202      1.10      onoe 	int reassoc;
   2203       1.1  sommerfe {
   2204      1.10      onoe 	struct ifnet *ifp = sc->sc_ifp;
   2205      1.10      onoe 	struct mbuf *m;
   2206      1.10      onoe 	struct ieee80211_frame *wh;
   2207      1.10      onoe 	u_int16_t lintval;
   2208      1.10      onoe 	u_int8_t *asreq;
   2209       1.1  sommerfe 
   2210      1.10      onoe 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2211      1.10      onoe 	if (m == NULL)
   2212       1.1  sommerfe 		return;
   2213      1.10      onoe 	sc->sc_status = AWI_ST_ASSOC;
   2214      1.10      onoe 	if (ifp->if_flags & IFF_DEBUG)
   2215      1.10      onoe 		printf("%s: sending %sassoc req to %s\n", sc->sc_dev.dv_xname,
   2216      1.10      onoe 		    reassoc ? "re" : "",
   2217      1.10      onoe 		    ether_sprintf(sc->sc_bss.bssid));
   2218      1.10      onoe 
   2219      1.10      onoe 	wh = mtod(m, struct ieee80211_frame *);
   2220      1.10      onoe 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT;
   2221      1.10      onoe 	if (reassoc)
   2222      1.10      onoe 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_REASSOC_REQ;
   2223      1.10      onoe 	else
   2224      1.10      onoe 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_ASSOC_REQ;
   2225      1.10      onoe 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2226      1.10      onoe 	LE_WRITE_2(wh->i_dur, 0);
   2227      1.10      onoe 	LE_WRITE_2(wh->i_seq, 0);
   2228  1.19.2.1      onoe 	memcpy(wh->i_addr1, sc->sc_bss.esrc, ETHER_ADDR_LEN);
   2229      1.10      onoe 	memcpy(wh->i_addr2, sc->sc_mib_addr.aMAC_Address, ETHER_ADDR_LEN);
   2230      1.10      onoe 	memcpy(wh->i_addr3, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   2231      1.10      onoe 
   2232      1.10      onoe 	asreq = (u_int8_t *)&wh[1];
   2233      1.10      onoe 
   2234      1.10      onoe 	/* capability info */
   2235  1.19.2.1      onoe 	if (sc->sc_wep_algo == NULL)
   2236  1.19.2.1      onoe 		LE_WRITE_2(asreq, IEEE80211_CAPINFO_CF_POLLABLE);
   2237  1.19.2.1      onoe 	else
   2238  1.19.2.1      onoe 		LE_WRITE_2(asreq,
   2239  1.19.2.1      onoe 		    IEEE80211_CAPINFO_CF_POLLABLE | IEEE80211_CAPINFO_PRIVACY);
   2240      1.10      onoe 	asreq += 2;
   2241      1.10      onoe 	/* listen interval */
   2242      1.10      onoe 	lintval = LE_READ_2(&sc->sc_mib_mgt.aListen_Interval);
   2243      1.10      onoe 	LE_WRITE_2(asreq, lintval);
   2244      1.10      onoe 	asreq += 2;
   2245      1.10      onoe 	if (reassoc) {
   2246      1.10      onoe 		/* current AP address */
   2247      1.10      onoe 		memcpy(asreq, sc->sc_bss.bssid, ETHER_ADDR_LEN);
   2248      1.10      onoe 		asreq += ETHER_ADDR_LEN;
   2249      1.10      onoe 	}
   2250      1.10      onoe 	/* ssid */
   2251      1.10      onoe 	memcpy(asreq, sc->sc_bss.essid, 2 + sc->sc_bss.essid[1]);
   2252      1.10      onoe 	asreq += 2 + asreq[1];
   2253      1.10      onoe 	/* supported rates */
   2254      1.10      onoe 	memcpy(asreq, &sc->sc_mib_phy.aSuprt_Data_Rates, 4);
   2255      1.10      onoe 	asreq += 2 + asreq[1];
   2256       1.1  sommerfe 
   2257      1.10      onoe 	m->m_pkthdr.len = m->m_len = asreq - mtod(m, u_int8_t *);
   2258      1.10      onoe 	IF_ENQUEUE(&sc->sc_mgtq, m);
   2259      1.10      onoe 	awi_start(ifp);
   2260       1.1  sommerfe 
   2261      1.10      onoe 	sc->sc_mgt_timer = AWI_TRANS_TIMEOUT / 1000;
   2262      1.10      onoe 	ifp->if_timer = 1;
   2263       1.1  sommerfe }
   2264       1.1  sommerfe 
   2265      1.10      onoe static void
   2266      1.10      onoe awi_recv_asresp(sc, m0)
   2267       1.1  sommerfe 	struct awi_softc *sc;
   2268      1.10      onoe 	struct mbuf *m0;
   2269       1.1  sommerfe {
   2270      1.10      onoe 	struct ieee80211_frame *wh;
   2271      1.10      onoe 	u_int8_t *asresp, *eframe;
   2272      1.10      onoe 	u_int16_t status;
   2273      1.10      onoe 	u_int8_t rate, *phy_rates;
   2274      1.10      onoe 	struct awi_bss *bp;
   2275      1.10      onoe 	int i, j;
   2276      1.10      onoe 
   2277      1.10      onoe 	wh = mtod(m0, struct ieee80211_frame *);
   2278      1.10      onoe 	asresp = (u_int8_t *)&wh[1];
   2279      1.10      onoe 	eframe = mtod(m0, u_int8_t *) + m0->m_len;
   2280      1.10      onoe 	if (sc->sc_ifp->if_flags & IFF_DEBUG)
   2281      1.10      onoe 		printf("%s: receive assoc resp from %s\n", sc->sc_dev.dv_xname,
   2282      1.10      onoe 		    ether_sprintf(wh->i_addr2));
   2283      1.10      onoe 
   2284      1.10      onoe 	if (!sc->sc_mib_local.Network_Mode)
   2285      1.10      onoe 		return;
   2286       1.1  sommerfe 
   2287      1.10      onoe 	if (sc->sc_status != AWI_ST_ASSOC)
   2288      1.10      onoe 		return;
   2289      1.10      onoe 	/* capability info */
   2290      1.10      onoe 	asresp += 2;
   2291      1.10      onoe 	/* status */
   2292      1.10      onoe 	status = LE_READ_2(asresp);
   2293      1.10      onoe 	if (status != 0) {
   2294      1.10      onoe 		printf("%s: association failed (reason %d)\n",
   2295      1.10      onoe 		    sc->sc_dev.dv_xname, status);
   2296      1.10      onoe 		for (bp = TAILQ_FIRST(&sc->sc_scan); bp != NULL;
   2297      1.10      onoe 		    bp = TAILQ_NEXT(bp, list)) {
   2298      1.10      onoe 			if (memcmp(bp->esrc, sc->sc_bss.esrc, ETHER_ADDR_LEN)
   2299      1.10      onoe 			    == 0) {
   2300      1.10      onoe 				bp->fails++;
   2301      1.10      onoe 				break;
   2302      1.10      onoe 			}
   2303      1.10      onoe 		}
   2304       1.1  sommerfe 		return;
   2305       1.1  sommerfe 	}
   2306      1.10      onoe 	asresp += 2;
   2307      1.10      onoe 	/* association id */
   2308      1.10      onoe 	asresp += 2;
   2309      1.10      onoe 	/* supported rates */
   2310      1.10      onoe 	rate = AWI_RATE_1MBIT;
   2311      1.10      onoe 	for (i = 0; i < asresp[1]; i++) {
   2312      1.10      onoe 		if (AWI_80211_RATE(asresp[2 + i]) <= rate)
   2313      1.10      onoe 			continue;
   2314      1.10      onoe 		phy_rates = sc->sc_mib_phy.aSuprt_Data_Rates;
   2315      1.10      onoe 		for (j = 0; j < phy_rates[1]; j++) {
   2316      1.10      onoe 			if (AWI_80211_RATE(asresp[2 + i]) ==
   2317      1.10      onoe 			    AWI_80211_RATE(phy_rates[2 + j]))
   2318      1.10      onoe 				rate = AWI_80211_RATE(asresp[2 + i]);
   2319      1.10      onoe 		}
   2320      1.10      onoe 	}
   2321  1.19.2.3      onoe 	if (sc->sc_ifp->if_flags & IFF_DEBUG) {
   2322  1.19.2.3      onoe 		printf("%s: associated with %s ssid ",
   2323  1.19.2.3      onoe 		    sc->sc_dev.dv_xname, ether_sprintf(sc->sc_bss.bssid));
   2324  1.19.2.3      onoe 		awi_print_essid(sc->sc_bss.essid);
   2325  1.19.2.3      onoe 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
   2326  1.19.2.3      onoe 			printf(" chanset %d pattern %d\n",
   2327  1.19.2.3      onoe 			    sc->sc_bss.chanset, sc->sc_bss.pattern);
   2328  1.19.2.3      onoe 		else
   2329  1.19.2.3      onoe 			printf(" channel %d\n", sc->sc_bss.chanset);
   2330  1.19.2.3      onoe 	}
   2331      1.10      onoe 	sc->sc_tx_rate = rate;
   2332      1.10      onoe 	sc->sc_mgt_timer = 0;
   2333      1.10      onoe 	sc->sc_rx_timer = 10;
   2334      1.10      onoe 	sc->sc_ifp->if_timer = 1;
   2335      1.10      onoe 	sc->sc_status = AWI_ST_RUNNING;
   2336      1.10      onoe 	sc->sc_ifp->if_flags |= IFF_RUNNING;
   2337      1.10      onoe 	awi_drvstate(sc, AWI_DRV_INFASSOC);
   2338      1.10      onoe 	awi_start(sc->sc_ifp);
   2339       1.1  sommerfe }
   2340       1.1  sommerfe 
   2341      1.10      onoe static int
   2342      1.10      onoe awi_mib(sc, cmd, mib)
   2343       1.1  sommerfe 	struct awi_softc *sc;
   2344      1.10      onoe 	u_int8_t cmd;
   2345      1.10      onoe 	u_int8_t mib;
   2346       1.1  sommerfe {
   2347      1.10      onoe 	int error;
   2348      1.10      onoe 	u_int8_t size, *ptr;
   2349       1.1  sommerfe 
   2350      1.10      onoe 	switch (mib) {
   2351      1.10      onoe 	case AWI_MIB_LOCAL:
   2352      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_local;
   2353      1.10      onoe 		size = sizeof(sc->sc_mib_local);
   2354      1.10      onoe 		break;
   2355      1.10      onoe 	case AWI_MIB_ADDR:
   2356      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_addr;
   2357      1.10      onoe 		size = sizeof(sc->sc_mib_addr);
   2358      1.10      onoe 		break;
   2359      1.10      onoe 	case AWI_MIB_MAC:
   2360      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_mac;
   2361      1.10      onoe 		size = sizeof(sc->sc_mib_mac);
   2362      1.10      onoe 		break;
   2363      1.10      onoe 	case AWI_MIB_STAT:
   2364      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_stat;
   2365      1.10      onoe 		size = sizeof(sc->sc_mib_stat);
   2366      1.10      onoe 		break;
   2367      1.10      onoe 	case AWI_MIB_MGT:
   2368      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_mgt;
   2369      1.10      onoe 		size = sizeof(sc->sc_mib_mgt);
   2370      1.10      onoe 		break;
   2371      1.10      onoe 	case AWI_MIB_PHY:
   2372      1.10      onoe 		ptr = (u_int8_t *)&sc->sc_mib_phy;
   2373      1.10      onoe 		size = sizeof(sc->sc_mib_phy);
   2374      1.10      onoe 		break;
   2375      1.10      onoe 	default:
   2376      1.10      onoe 		return EINVAL;
   2377       1.1  sommerfe 	}
   2378      1.10      onoe 	if (sc->sc_cmd_inprog) {
   2379      1.10      onoe 		error = awi_cmd_wait(sc);
   2380      1.10      onoe 		if (error) {
   2381  1.19.2.1      onoe 			if (error == EWOULDBLOCK)
   2382  1.19.2.1      onoe 				printf("awi_mib: cmd %d inprog",
   2383  1.19.2.1      onoe 				    sc->sc_cmd_inprog);
   2384      1.10      onoe 			return error;
   2385      1.10      onoe 		}
   2386      1.10      onoe 	}
   2387  1.19.2.1      onoe 	sc->sc_cmd_inprog = cmd;
   2388      1.10      onoe 	if (cmd == AWI_CMD_SET_MIB)
   2389      1.10      onoe 		awi_write_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
   2390      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_TYPE, mib);
   2391      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_SIZE, size);
   2392      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_MIB_INDEX, 0);
   2393      1.10      onoe 	error = awi_cmd(sc, cmd);
   2394      1.10      onoe 	if (error)
   2395      1.10      onoe 		return error;
   2396      1.10      onoe 	if (cmd == AWI_CMD_GET_MIB) {
   2397      1.10      onoe 		awi_read_bytes(sc, AWI_CMD_PARAMS+AWI_CA_MIB_DATA, ptr, size);
   2398      1.10      onoe #ifdef AWI_DEBUG
   2399      1.10      onoe 		if (awi_verbose) {
   2400      1.10      onoe 			int i;
   2401       1.1  sommerfe 
   2402      1.10      onoe 			printf("awi_mib: #%d:", mib);
   2403      1.10      onoe 			for (i = 0; i < size; i++)
   2404      1.10      onoe 				printf(" %02x", ptr[i]);
   2405      1.10      onoe 			printf("\n");
   2406      1.10      onoe 		}
   2407       1.1  sommerfe #endif
   2408      1.10      onoe 	}
   2409      1.10      onoe 	return 0;
   2410       1.1  sommerfe }
   2411       1.1  sommerfe 
   2412      1.10      onoe static int
   2413      1.10      onoe awi_cmd_scan(sc)
   2414       1.1  sommerfe 	struct awi_softc *sc;
   2415       1.1  sommerfe {
   2416      1.10      onoe 	int error;
   2417      1.10      onoe 	u_int8_t scan_mode;
   2418      1.10      onoe 
   2419      1.10      onoe 	if (sc->sc_active_scan)
   2420      1.10      onoe 		scan_mode = AWI_SCAN_ACTIVE;
   2421      1.10      onoe 	else
   2422      1.10      onoe 		scan_mode = AWI_SCAN_PASSIVE;
   2423      1.10      onoe 	if (sc->sc_mib_mgt.aScan_Mode != scan_mode) {
   2424      1.10      onoe 		sc->sc_mib_mgt.aScan_Mode = scan_mode;
   2425      1.10      onoe 		error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT);
   2426      1.10      onoe 		return error;
   2427      1.10      onoe 	}
   2428      1.10      onoe 
   2429      1.10      onoe 	if (sc->sc_cmd_inprog) {
   2430      1.10      onoe 		error = awi_cmd_wait(sc);
   2431      1.10      onoe 		if (error)
   2432      1.10      onoe 			return error;
   2433      1.10      onoe 	}
   2434  1.19.2.1      onoe 	sc->sc_cmd_inprog = AWI_CMD_SCAN;
   2435      1.10      onoe 	awi_write_2(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_DURATION,
   2436      1.10      onoe 	    sc->sc_active_scan ? AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
   2437      1.10      onoe 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   2438      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
   2439      1.10      onoe 		    sc->sc_scan_set);
   2440      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN,
   2441      1.10      onoe 		    sc->sc_scan_cur);
   2442      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 1);
   2443      1.10      onoe 	} else {
   2444      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SET,
   2445      1.10      onoe 		    sc->sc_scan_cur);
   2446      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_PATTERN, 0);
   2447      1.10      onoe 		awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_IDX, 0);
   2448       1.6  sommerfe 	}
   2449      1.10      onoe 	awi_write_1(sc, AWI_CMD_PARAMS+AWI_CA_SCAN_SUSP, 0);
   2450      1.10      onoe 	return awi_cmd(sc, AWI_CMD_SCAN);
   2451       1.1  sommerfe }
   2452       1.1  sommerfe 
   2453      1.10      onoe static int
   2454      1.10      onoe awi_cmd(sc, cmd)
   2455       1.1  sommerfe 	struct awi_softc *sc;
   2456      1.10      onoe 	u_int8_t cmd;
   2457       1.1  sommerfe {
   2458      1.10      onoe 	u_int8_t status;
   2459      1.10      onoe 	int error = 0;
   2460      1.10      onoe 
   2461  1.19.2.1      onoe 	sc->sc_cmd_inprog = cmd;
   2462      1.10      onoe 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
   2463      1.10      onoe 	awi_write_1(sc, AWI_CMD, cmd);
   2464      1.10      onoe 	if (sc->sc_status != AWI_ST_INIT)
   2465      1.10      onoe 		return 0;
   2466      1.10      onoe 	error = awi_cmd_wait(sc);
   2467      1.10      onoe 	if (error)
   2468      1.10      onoe 		return error;
   2469      1.10      onoe 	status = awi_read_1(sc, AWI_CMD_STATUS);
   2470      1.10      onoe 	awi_write_1(sc, AWI_CMD, 0);
   2471      1.10      onoe 	switch (status) {
   2472      1.10      onoe 	case AWI_STAT_OK:
   2473      1.10      onoe 		break;
   2474      1.10      onoe 	case AWI_STAT_BADPARM:
   2475      1.10      onoe 		return EINVAL;
   2476      1.10      onoe 	default:
   2477      1.10      onoe 		printf("%s: command %d failed %x\n",
   2478      1.10      onoe 		    sc->sc_dev.dv_xname, cmd, status);
   2479      1.10      onoe 		return ENXIO;
   2480      1.10      onoe 	}
   2481      1.10      onoe 	return 0;
   2482       1.1  sommerfe }
   2483       1.1  sommerfe 
   2484      1.10      onoe static void
   2485      1.10      onoe awi_cmd_done(sc)
   2486       1.1  sommerfe 	struct awi_softc *sc;
   2487       1.1  sommerfe {
   2488      1.10      onoe 	u_int8_t cmd, status;
   2489      1.10      onoe 
   2490      1.10      onoe 	status = awi_read_1(sc, AWI_CMD_STATUS);
   2491      1.10      onoe 	if (status == AWI_STAT_IDLE)
   2492      1.10      onoe 		return;		/* stray interrupt */
   2493      1.10      onoe 
   2494  1.19.2.1      onoe 	cmd = sc->sc_cmd_inprog;
   2495      1.10      onoe 	sc->sc_cmd_inprog = 0;
   2496      1.10      onoe 	if (sc->sc_status == AWI_ST_INIT) {
   2497      1.10      onoe 		wakeup(sc);
   2498      1.10      onoe 		return;
   2499      1.10      onoe 	}
   2500      1.10      onoe 	awi_write_1(sc, AWI_CMD, 0);
   2501      1.10      onoe 
   2502      1.10      onoe 	if (status != AWI_STAT_OK) {
   2503      1.10      onoe 		printf("%s: command %d failed %x\n",
   2504      1.10      onoe 		    sc->sc_dev.dv_xname, cmd, status);
   2505      1.10      onoe 		return;
   2506      1.10      onoe 	}
   2507      1.10      onoe 	switch (sc->sc_status) {
   2508      1.10      onoe 	case AWI_ST_SCAN:
   2509      1.10      onoe 		if (cmd == AWI_CMD_SET_MIB)
   2510      1.10      onoe 			awi_cmd_scan(sc);	/* retry */
   2511      1.10      onoe 		break;
   2512      1.10      onoe 	case AWI_ST_SETSS:
   2513      1.10      onoe 		awi_try_sync(sc);
   2514      1.10      onoe 		break;
   2515      1.10      onoe 	case AWI_ST_SYNC:
   2516      1.10      onoe 		awi_sync_done(sc);
   2517      1.10      onoe 		break;
   2518      1.10      onoe 	default:
   2519      1.10      onoe 		break;
   2520      1.10      onoe 	}
   2521       1.1  sommerfe }
   2522       1.1  sommerfe 
   2523      1.10      onoe static int
   2524      1.10      onoe awi_next_txd(sc, len, framep, ntxdp)
   2525       1.1  sommerfe 	struct awi_softc *sc;
   2526      1.10      onoe 	int len;
   2527      1.10      onoe 	u_int32_t *framep, *ntxdp;
   2528       1.1  sommerfe {
   2529      1.10      onoe 	u_int32_t txd, ntxd, frame;
   2530       1.1  sommerfe 
   2531      1.10      onoe 	txd = sc->sc_txnext;
   2532      1.10      onoe 	frame = txd + AWI_TXD_SIZE;
   2533      1.10      onoe 	if (frame + len > sc->sc_txend)
   2534      1.10      onoe 		frame = sc->sc_txbase;
   2535      1.10      onoe 	ntxd = frame + len;
   2536      1.10      onoe 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
   2537      1.10      onoe 		ntxd = sc->sc_txbase;
   2538      1.10      onoe 	*framep = frame;
   2539      1.10      onoe 	*ntxdp = ntxd;
   2540      1.10      onoe 	/*
   2541      1.10      onoe 	 * Determine if there are any room in ring buffer.
   2542      1.10      onoe 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
   2543      1.10      onoe 	 *   base........................end
   2544      1.10      onoe 	 *	   done----txd=====ntxd		OK
   2545      1.10      onoe 	 *	 --txd=====done++++ntxd--	full
   2546      1.10      onoe 	 *	 --txd=====ntxd    done--	OK
   2547      1.10      onoe 	 *	 ==ntxd    done----txd===	OK
   2548      1.10      onoe 	 *	 ==done++++ntxd----txd===	full
   2549      1.10      onoe 	 *	 ++ntxd    txd=====done++	full
   2550      1.10      onoe 	 */
   2551      1.10      onoe 	if (txd < ntxd) {
   2552      1.10      onoe 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   2553      1.10      onoe 			return ENOBUFS;
   2554      1.10      onoe 	} else {
   2555      1.10      onoe 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   2556      1.10      onoe 			return ENOBUFS;
   2557       1.1  sommerfe 	}
   2558      1.10      onoe 	return 0;
   2559       1.1  sommerfe }
   2560       1.1  sommerfe 
   2561      1.10      onoe static int
   2562      1.10      onoe awi_lock(sc)
   2563       1.1  sommerfe 	struct awi_softc *sc;
   2564       1.1  sommerfe {
   2565      1.10      onoe 	int error = 0;
   2566       1.1  sommerfe 
   2567      1.10      onoe 	if (curproc == NULL) {
   2568      1.10      onoe 		/*
   2569      1.10      onoe 		 * XXX
   2570      1.10      onoe 		 * Though driver ioctl should be called with context,
   2571      1.10      onoe 		 * KAME ipv6 stack calls ioctl in interrupt for now.
   2572      1.10      onoe 		 * We simply abort the request if there are other
   2573      1.10      onoe 		 * ioctl requests in progress.
   2574      1.10      onoe 		 */
   2575  1.19.2.1      onoe 		if (sc->sc_busy) {
   2576      1.10      onoe 			return EWOULDBLOCK;
   2577  1.19.2.1      onoe 			if (sc->sc_invalid)
   2578  1.19.2.1      onoe 				return ENXIO;
   2579  1.19.2.1      onoe 		}
   2580      1.10      onoe 		sc->sc_busy = 1;
   2581      1.10      onoe 		sc->sc_cansleep = 0;
   2582      1.10      onoe 		return 0;
   2583       1.1  sommerfe 	}
   2584      1.10      onoe 	while (sc->sc_busy) {
   2585  1.19.2.1      onoe 		if (sc->sc_invalid)
   2586  1.19.2.1      onoe 			return ENXIO;
   2587      1.10      onoe 		sc->sc_sleep_cnt++;
   2588      1.10      onoe 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
   2589      1.10      onoe 		sc->sc_sleep_cnt--;
   2590      1.10      onoe 		if (error)
   2591      1.10      onoe 			return error;
   2592       1.6  sommerfe 	}
   2593      1.10      onoe 	sc->sc_busy = 1;
   2594      1.10      onoe 	sc->sc_cansleep = 1;
   2595      1.10      onoe 	return 0;
   2596      1.10      onoe }
   2597       1.1  sommerfe 
   2598      1.10      onoe static void
   2599      1.10      onoe awi_unlock(sc)
   2600      1.10      onoe 	struct awi_softc *sc;
   2601      1.10      onoe {
   2602      1.10      onoe 	sc->sc_busy = 0;
   2603      1.10      onoe 	sc->sc_cansleep = 0;
   2604      1.10      onoe 	if (sc->sc_sleep_cnt)
   2605      1.10      onoe 		wakeup(sc);
   2606       1.1  sommerfe }
   2607       1.1  sommerfe 
   2608      1.10      onoe static int
   2609      1.10      onoe awi_intr_lock(sc)
   2610       1.1  sommerfe 	struct awi_softc *sc;
   2611      1.10      onoe {
   2612       1.1  sommerfe 	u_int8_t status;
   2613      1.10      onoe 	int i, retry;
   2614      1.10      onoe 
   2615      1.10      onoe 	status = 1;
   2616      1.10      onoe 	for (retry = 0; retry < 10; retry++) {
   2617      1.10      onoe 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
   2618      1.10      onoe 			status = awi_read_1(sc, AWI_LOCKOUT_HOST);
   2619      1.10      onoe 			if (status == 0)
   2620      1.10      onoe 				break;
   2621      1.10      onoe 			DELAY(5);
   2622      1.10      onoe 		}
   2623      1.10      onoe 		if (status != 0)
   2624      1.10      onoe 			break;
   2625      1.10      onoe 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
   2626      1.10      onoe 		status = awi_read_1(sc, AWI_LOCKOUT_HOST);
   2627      1.10      onoe 		if (status == 0)
   2628      1.10      onoe 			break;
   2629      1.10      onoe 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   2630       1.1  sommerfe 	}
   2631      1.10      onoe 	if (status != 0) {
   2632      1.10      onoe 		printf("%s: failed to lock interrupt\n",
   2633       1.6  sommerfe 		    sc->sc_dev.dv_xname);
   2634      1.10      onoe 		return ENXIO;
   2635       1.6  sommerfe 	}
   2636      1.10      onoe 	return 0;
   2637       1.1  sommerfe }
   2638       1.1  sommerfe 
   2639      1.10      onoe static void
   2640      1.10      onoe awi_intr_unlock(sc)
   2641       1.1  sommerfe 	struct awi_softc *sc;
   2642       1.1  sommerfe {
   2643       1.1  sommerfe 
   2644      1.10      onoe 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   2645       1.1  sommerfe }
   2646       1.1  sommerfe 
   2647      1.10      onoe static int
   2648      1.10      onoe awi_cmd_wait(sc)
   2649       1.1  sommerfe 	struct awi_softc *sc;
   2650       1.1  sommerfe {
   2651      1.10      onoe 	int i, error = 0;
   2652      1.10      onoe 
   2653      1.10      onoe 	i = 0;
   2654      1.10      onoe 	while (sc->sc_cmd_inprog) {
   2655      1.10      onoe 		if (sc->sc_invalid)
   2656      1.10      onoe 			return ENXIO;
   2657  1.19.2.1      onoe 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
   2658  1.19.2.1      onoe 			printf("%s: failed to access hardware\n",
   2659  1.19.2.1      onoe 			    sc->sc_dev.dv_xname);
   2660  1.19.2.1      onoe 			sc->sc_invalid = 1;
   2661  1.19.2.1      onoe 			return ENXIO;
   2662  1.19.2.1      onoe 		}
   2663      1.10      onoe 		if (sc->sc_cansleep) {
   2664      1.10      onoe 			sc->sc_sleep_cnt++;
   2665      1.10      onoe 			error = tsleep(sc, PWAIT, "awicmd",
   2666      1.10      onoe 			    AWI_CMD_TIMEOUT*hz/1000);
   2667      1.10      onoe 			sc->sc_sleep_cnt--;
   2668      1.10      onoe 		} else {
   2669      1.10      onoe 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
   2670      1.10      onoe 				awi_cmd_done(sc);
   2671      1.10      onoe 				break;
   2672      1.10      onoe 			}
   2673      1.10      onoe 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
   2674      1.10      onoe 				error = EWOULDBLOCK;
   2675      1.10      onoe 			else
   2676      1.10      onoe 				DELAY(10);
   2677      1.10      onoe 		}
   2678      1.10      onoe 		if (error)
   2679      1.10      onoe 			break;
   2680       1.1  sommerfe 	}
   2681      1.10      onoe 	return error;
   2682       1.1  sommerfe }
   2683       1.1  sommerfe 
   2684  1.19.2.1      onoe static void
   2685  1.19.2.1      onoe awi_print_essid(essid)
   2686  1.19.2.1      onoe 	u_int8_t *essid;
   2687  1.19.2.1      onoe {
   2688  1.19.2.1      onoe 	int i, len;
   2689  1.19.2.1      onoe 	u_int8_t *p;
   2690  1.19.2.1      onoe 
   2691  1.19.2.1      onoe 	len = essid[1];
   2692  1.19.2.1      onoe 	if (len > IEEE80211_NWID_LEN)
   2693  1.19.2.1      onoe 		len = IEEE80211_NWID_LEN;	/*XXX*/
   2694  1.19.2.1      onoe 	/* determine printable or not */
   2695  1.19.2.1      onoe 	for (i = 0, p = essid + 2; i < len; i++, p++) {
   2696  1.19.2.1      onoe 		if (*p < ' ' || *p > 0x7e)
   2697  1.19.2.1      onoe 			break;
   2698  1.19.2.1      onoe 	}
   2699  1.19.2.1      onoe 	if (i == len) {
   2700  1.19.2.1      onoe 		printf("\"");
   2701  1.19.2.1      onoe 		for (i = 0, p = essid + 2; i < len; i++, p++)
   2702  1.19.2.1      onoe 			printf("%c", *p);
   2703  1.19.2.1      onoe 		printf("\"");
   2704  1.19.2.1      onoe 	} else {
   2705  1.19.2.1      onoe 		printf("0x");
   2706  1.19.2.1      onoe 		for (i = 0, p = essid + 2; i < len; i++, p++)
   2707  1.19.2.1      onoe 			printf("%02x", *p);
   2708  1.19.2.1      onoe 	}
   2709  1.19.2.1      onoe }
   2710  1.19.2.1      onoe 
   2711      1.10      onoe #ifdef AWI_DEBUG
   2712      1.10      onoe static void
   2713      1.10      onoe awi_dump_pkt(sc, m, rssi)
   2714       1.1  sommerfe 	struct awi_softc *sc;
   2715      1.10      onoe 	struct mbuf *m;
   2716  1.19.2.1      onoe 	int rssi;
   2717       1.1  sommerfe {
   2718      1.10      onoe 	struct ieee80211_frame *wh;
   2719      1.10      onoe 	int i, l;
   2720       1.1  sommerfe 
   2721      1.10      onoe 	wh = mtod(m, struct ieee80211_frame *);
   2722       1.1  sommerfe 
   2723      1.10      onoe 	if (awi_dump_mask != 0 &&
   2724      1.10      onoe 	    ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK)==IEEE80211_FC1_DIR_NODS) &&
   2725      1.10      onoe 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_MGT)) {
   2726      1.10      onoe 		if ((AWI_DUMP_MASK(wh->i_fc[0]) & awi_dump_mask) != 0)
   2727      1.10      onoe 			return;
   2728      1.10      onoe 	}
   2729      1.10      onoe 	if (awi_dump_mask < 0 &&
   2730      1.10      onoe 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK)==IEEE80211_FC0_TYPE_DATA)
   2731      1.10      onoe 		return;
   2732       1.1  sommerfe 
   2733  1.19.2.1      onoe 	if (rssi < 0)
   2734  1.19.2.1      onoe 		printf("tx: ");
   2735  1.19.2.1      onoe 	else
   2736  1.19.2.1      onoe 		printf("rx: ");
   2737      1.10      onoe 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
   2738      1.10      onoe 	case IEEE80211_FC1_DIR_NODS:
   2739  1.19.2.1      onoe 		printf("NODS %s", ether_sprintf(wh->i_addr2));
   2740      1.10      onoe 		printf("->%s", ether_sprintf(wh->i_addr1));
   2741      1.10      onoe 		printf("(%s)", ether_sprintf(wh->i_addr3));
   2742      1.10      onoe 		break;
   2743      1.10      onoe 	case IEEE80211_FC1_DIR_TODS:
   2744  1.19.2.1      onoe 		printf("TODS %s", ether_sprintf(wh->i_addr2));
   2745      1.10      onoe 		printf("->%s", ether_sprintf(wh->i_addr3));
   2746      1.10      onoe 		printf("(%s)", ether_sprintf(wh->i_addr1));
   2747      1.10      onoe 		break;
   2748      1.10      onoe 	case IEEE80211_FC1_DIR_FROMDS:
   2749  1.19.2.1      onoe 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
   2750      1.10      onoe 		printf("->%s", ether_sprintf(wh->i_addr1));
   2751      1.10      onoe 		printf("(%s)", ether_sprintf(wh->i_addr2));
   2752      1.10      onoe 		break;
   2753      1.10      onoe 	case IEEE80211_FC1_DIR_DSTODS:
   2754  1.19.2.1      onoe 		printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
   2755      1.10      onoe 		printf("->%s", ether_sprintf(wh->i_addr3));
   2756      1.10      onoe 		printf("(%s", ether_sprintf(wh->i_addr2));
   2757      1.10      onoe 		printf("->%s)", ether_sprintf(wh->i_addr1));
   2758      1.10      onoe 		break;
   2759      1.10      onoe 	}
   2760      1.10      onoe 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
   2761      1.10      onoe 	case IEEE80211_FC0_TYPE_DATA:
   2762      1.10      onoe 		printf(" data");
   2763      1.10      onoe 		break;
   2764      1.10      onoe 	case IEEE80211_FC0_TYPE_MGT:
   2765      1.10      onoe 		switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
   2766      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
   2767      1.10      onoe 			printf(" probe_req");
   2768      1.10      onoe 			break;
   2769      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   2770      1.10      onoe 			printf(" probe_resp");
   2771      1.10      onoe 			break;
   2772      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_BEACON:
   2773      1.10      onoe 			printf(" beacon");
   2774      1.10      onoe 			break;
   2775      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_AUTH:
   2776      1.10      onoe 			printf(" auth");
   2777      1.10      onoe 			break;
   2778      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
   2779      1.10      onoe 			printf(" assoc_req");
   2780      1.10      onoe 			break;
   2781      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
   2782      1.10      onoe 			printf(" assoc_resp");
   2783      1.10      onoe 			break;
   2784      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
   2785      1.10      onoe 			printf(" reassoc_req");
   2786      1.10      onoe 			break;
   2787      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
   2788      1.10      onoe 			printf(" reassoc_resp");
   2789      1.10      onoe 			break;
   2790      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_DEAUTH:
   2791      1.10      onoe 			printf(" deauth");
   2792      1.10      onoe 			break;
   2793      1.10      onoe 		case IEEE80211_FC0_SUBTYPE_DISASSOC:
   2794      1.10      onoe 			printf(" disassoc");
   2795      1.10      onoe 			break;
   2796      1.10      onoe 		default:
   2797      1.10      onoe 			printf(" mgt#%d",
   2798      1.10      onoe 			    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
   2799      1.10      onoe 			break;
   2800      1.10      onoe 		}
   2801      1.10      onoe 		break;
   2802      1.10      onoe 	default:
   2803      1.10      onoe 		printf(" type#%d",
   2804      1.10      onoe 		    wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
   2805      1.10      onoe 		break;
   2806      1.10      onoe 	}
   2807  1.19.2.1      onoe 	if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   2808  1.19.2.1      onoe 		printf(" WEP");
   2809  1.19.2.1      onoe 	if (rssi >= 0)
   2810  1.19.2.1      onoe 		printf(" +%d", rssi);
   2811  1.19.2.1      onoe 	printf("\n");
   2812      1.10      onoe 	if (awi_dump_len > 0) {
   2813      1.10      onoe 		l = m->m_len;
   2814      1.10      onoe 		if (l > awi_dump_len + sizeof(*wh))
   2815      1.10      onoe 			l = awi_dump_len + sizeof(*wh);
   2816      1.10      onoe 		i = sizeof(*wh);
   2817      1.10      onoe 		if (awi_dump_hdr)
   2818      1.10      onoe 			i = 0;
   2819      1.10      onoe 		for (; i < l; i++) {
   2820      1.10      onoe 			if ((i & 1) == 0)
   2821      1.10      onoe 				printf(" ");
   2822      1.10      onoe 			printf("%02x", mtod(m, u_int8_t *)[i]);
   2823      1.10      onoe 		}
   2824      1.10      onoe 		printf("\n");
   2825      1.10      onoe 	}
   2826       1.1  sommerfe }
   2827      1.10      onoe #endif
   2828