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