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