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