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