Home | History | Annotate | Line # | Download | only in ic
      1 /*	$NetBSD: awi.c,v 1.104 2025/07/09 21:25:35 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999,2000,2001 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Driver for AMD 802.11 firmware.
     33  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     34  *
     35  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
     36  */
     37 
     38 /*
     39  * todo:
     40  *	- flush tx queue on resynch.
     41  *	- clear oactive on "down".
     42  *	- rewrite copy-into-mbuf code
     43  *	- mgmt state machine gets stuck retransmitting assoc requests.
     44  *	- multicast filter.
     45  *	- fix device reset so it's more likely to work
     46  *	- show status goo through ifmedia.
     47  *
     48  * more todo:
     49  *	- deal with more 802.11 frames.
     50  *		- send reassoc request
     51  *		- deal with reassoc response
     52  *		- send/deal with disassociation
     53  *	- deal with "full" access points (no room for me).
     54  *	- power save mode
     55  *
     56  * later:
     57  *	- SSID preferences
     58  *	- need ioctls for poking at the MIBs
     59  *	- implement ad-hoc mode (including bss creation).
     60  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
     61  *		(focus on inf. mode since that will be needed for ietf)
     62  *	- deal with DH vs. FH versions of the card
     63  *	- deal with faster cards (2mb/s)
     64  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
     65  *	- ifmedia revision.
     66  *	- common 802.11 mibish things.
     67  *	- common 802.11 media layer.
     68  */
     69 
     70 /*
     71  * Driver for AMD 802.11 PCnetMobile firmware.
     72  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     73  *
     74  * The initial version of the driver was written by
     75  * Bill Sommerfeld <sommerfeld (at) NetBSD.org>.
     76  * Then the driver module completely rewritten to support cards with DS phy
     77  * and to support adhoc mode by Atsushi Onoe <onoe (at) NetBSD.org>
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.104 2025/07/09 21:25:35 andvar Exp $");
     82 
     83 #include "opt_inet.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/proc.h>
     90 #include <sys/socket.h>
     91 #include <sys/sockio.h>
     92 #include <sys/errno.h>
     93 #include <sys/endian.h>
     94 #include <sys/device.h>
     95 #include <sys/cpu.h>
     96 #include <sys/bus.h>
     97 
     98 #include <net/if.h>
     99 #include <net/if_dl.h>
    100 #include <net/if_ether.h>
    101 #include <net/if_media.h>
    102 #include <net/if_llc.h>
    103 #include <net/bpf.h>
    104 
    105 #include <net80211/ieee80211_netbsd.h>
    106 #include <net80211/ieee80211_var.h>
    107 
    108 #include <dev/ic/am79c930reg.h>
    109 #include <dev/ic/am79c930var.h>
    110 #include <dev/ic/awireg.h>
    111 #include <dev/ic/awivar.h>
    112 
    113 static void awi_softintr(void *);
    114 static int  awi_init(struct ifnet *);
    115 static void awi_stop(struct ifnet *, int);
    116 static void awi_start(struct ifnet *);
    117 static void awi_watchdog(struct ifnet *);
    118 static int  awi_ioctl(struct ifnet *, u_long, void *);
    119 static int  awi_media_change(struct ifnet *);
    120 static void awi_media_status(struct ifnet *, struct ifmediareq *);
    121 static int  awi_mode_init(struct awi_softc *);
    122 static void awi_rx_int(struct awi_softc *);
    123 static void awi_tx_int(struct awi_softc *);
    124 static struct mbuf *awi_devget(struct awi_softc *, uint32_t, uint16_t);
    125 static int  awi_hw_init(struct awi_softc *);
    126 static int  awi_init_mibs(struct awi_softc *);
    127 static int  awi_mib(struct awi_softc *, uint8_t, uint8_t, int);
    128 static int  awi_cmd(struct awi_softc *, uint8_t, int);
    129 static int  awi_cmd_wait(struct awi_softc *);
    130 static void awi_cmd_done(struct awi_softc *);
    131 static int  awi_next_txd(struct awi_softc *, int, uint32_t *, uint32_t *);
    132 static int  awi_lock(struct awi_softc *);
    133 static void awi_unlock(struct awi_softc *);
    134 static int  awi_intr_lock(struct awi_softc *);
    135 static void awi_intr_unlock(struct awi_softc *);
    136 static int  awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    137 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
    138     struct ieee80211_node *, int, int, uint32_t);
    139 static int  awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
    140     int);
    141 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
    142 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
    143 
    144 /* Unaligned little endian access */
    145 #define LE_READ_2(p)							\
    146 	((((uint8_t *)(p))[0]      ) | (((uint8_t *)(p))[1] <<  8))
    147 #define LE_READ_4(p)							\
    148 	((((uint8_t *)(p))[0]      ) | (((uint8_t *)(p))[1] <<  8) |	\
    149 	 (((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24))
    150 #define LE_WRITE_2(p, v)						\
    151 	((((uint8_t *)(p))[0] = (((uint32_t)(v)      ) & 0xff)),	\
    152 	 (((uint8_t *)(p))[1] = (((uint32_t)(v) >>  8) & 0xff)))
    153 #define LE_WRITE_4(p, v)						\
    154 	((((uint8_t *)(p))[0] = (((uint32_t)(v)      ) & 0xff)),	\
    155 	 (((uint8_t *)(p))[1] = (((uint32_t)(v) >>  8) & 0xff)),	\
    156 	 (((uint8_t *)(p))[2] = (((uint32_t)(v) >> 16) & 0xff)),	\
    157 	 (((uint8_t *)(p))[3] = (((uint32_t)(v) >> 24) & 0xff)))
    158 
    159 static const struct awi_chanset awi_chanset[] = {
    160     /* PHY type        domain            min max def */
    161     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP,  6, 17,  6 },
    162     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES,  0, 26,  1 },
    163     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR,  0, 32,  1 },
    164     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US,  0, 77,  1 },
    165     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA,  0, 77,  1 },
    166     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU,  0, 77,  1 },
    167     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
    168     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
    169     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
    170     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US,  1, 11,  3 },
    171     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA,  1, 11,  3 },
    172     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU,  1, 13,  3 },
    173     { 0, 0, 0, 0, 0 }
    174 };
    175 
    176 #ifdef AWI_DEBUG
    177 int awi_debug = 0;
    178 
    179 #define	DPRINTF(X)	if (awi_debug) printf X
    180 #define	DPRINTF2(X)	if (awi_debug > 1) printf X
    181 #else
    182 #define	DPRINTF(X)
    183 #define	DPRINTF2(X)
    184 #endif
    185 
    186 int
    187 awi_attach(struct awi_softc *sc)
    188 {
    189 	struct ieee80211com *ic = &sc->sc_ic;
    190 	struct ifnet *ifp = &sc->sc_if;
    191 	int s, i, error, nrate;
    192 	int mword;
    193 	enum ieee80211_phymode mode;
    194 
    195 	s = splnet();
    196 	sc->sc_busy = 1;
    197 	sc->sc_attached = 0;
    198 	sc->sc_substate = AWI_ST_NONE;
    199 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, awi_softintr, sc);
    200 	if (sc->sc_soft_ih == NULL) {
    201 		config_deactivate(sc->sc_dev);
    202 		splx(s);
    203 		return ENOMEM;
    204 	}
    205 	if ((error = awi_hw_init(sc)) != 0) {
    206 		config_deactivate(sc->sc_dev);
    207 		splx(s);
    208 		return error;
    209 	}
    210 	error = awi_init_mibs(sc);
    211 	if (error != 0) {
    212 		config_deactivate(sc->sc_dev);
    213 		splx(s);
    214 		return error;
    215 	}
    216 	ifp->if_softc = sc;
    217 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    218 	ifp->if_ioctl = awi_ioctl;
    219 	ifp->if_start = awi_start;
    220 	ifp->if_watchdog = awi_watchdog;
    221 	ifp->if_init = awi_init;
    222 	ifp->if_stop = awi_stop;
    223 	IFQ_SET_READY(&ifp->if_snd);
    224 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    225 
    226 	ic->ic_ifp = ifp;
    227 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
    228 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
    229 		ic->ic_phytype = IEEE80211_T_FH;
    230 		mode = IEEE80211_MODE_FH;
    231 	} else {
    232 		ic->ic_phytype = IEEE80211_T_DS;
    233 		ic->ic_caps |= IEEE80211_C_AHDEMO;
    234 		mode = IEEE80211_MODE_11B;
    235 	}
    236 	ic->ic_opmode = IEEE80211_M_STA;
    237 	nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
    238 	memcpy(ic->ic_sup_rates[mode].rs_rates,
    239 	    sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
    240 	ic->ic_sup_rates[mode].rs_nrates = nrate;
    241 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
    242 
    243 	printf("%s: IEEE802.11 %s (firmware %s)\n", ifp->if_xname,
    244 	    (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
    245 	printf("%s: 802.11 address: %s\n", ifp->if_xname,
    246 	    ether_sprintf(ic->ic_myaddr));
    247 
    248 	if_attach(ifp);
    249 	ieee80211_ifattach(ic);
    250 
    251 	sc->sc_newstate = ic->ic_newstate;
    252 	ic->ic_newstate = awi_newstate;
    253 
    254 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
    255 	ic->ic_recv_mgmt = awi_recv_mgmt;
    256 
    257 	sc->sc_send_mgmt = ic->ic_send_mgmt;
    258 	ic->ic_send_mgmt = awi_send_mgmt;
    259 
    260 	ieee80211_media_init(ic, awi_media_change, awi_media_status);
    261 
    262 	/* Melco compatibility mode. */
    263 #define	ADD(s, o)	ifmedia_add(&ic->ic_media, \
    264 	IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
    265 	ADD(IFM_AUTO, IFM_FLAG0);
    266 
    267 	for (i = 0; i < nrate; i++) {
    268 		mword = ieee80211_rate2media(ic,
    269 		    ic->ic_sup_rates[mode].rs_rates[i], mode);
    270 		if (mword == 0)
    271 			continue;
    272 		ADD(mword, IFM_FLAG0);
    273 	}
    274 #undef	ADD
    275 
    276 	if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
    277 		printf("%s: WARNING: unable to establish shutdown hook\n",
    278 		    ifp->if_xname);
    279 
    280 	pmf_device_register(sc->sc_dev, NULL, NULL);
    281 	pmf_class_network_register(sc->sc_dev, ifp);
    282 	sc->sc_attached = 1;
    283 	splx(s);
    284 
    285 	/* Ready to accept ioctl */
    286 	awi_unlock(sc);
    287 
    288 	return 0;
    289 }
    290 
    291 int
    292 awi_detach(struct awi_softc *sc)
    293 {
    294 	struct ieee80211com *ic = &sc->sc_ic;
    295 	struct ifnet *ifp = &sc->sc_if;
    296 	int s;
    297 
    298 	if (!sc->sc_attached)
    299 		return 0;
    300 
    301 	s = splnet();
    302 	awi_stop(ifp, 1);
    303 
    304 	while (sc->sc_sleep_cnt > 0) {
    305 		wakeup(sc);
    306 		(void)tsleep(sc, PWAIT, "awidet", 1);
    307 	}
    308 	sc->sc_attached = 0;
    309 	ieee80211_ifdetach(ic);
    310 	if_detach(ifp);
    311 	shutdownhook_disestablish(sc->sc_sdhook);
    312 	pmf_device_deregister(sc->sc_dev);
    313 	softint_disestablish(sc->sc_soft_ih);
    314 	splx(s);
    315 	return 0;
    316 }
    317 
    318 int
    319 awi_activate(device_t self, enum devact act)
    320 {
    321 	struct awi_softc *sc = device_private(self);
    322 
    323 	switch (act) {
    324 	case DVACT_DEACTIVATE:
    325 		if_deactivate(&sc->sc_if);
    326 		return 0;
    327 	default:
    328 		return EOPNOTSUPP;
    329 	}
    330 }
    331 
    332 void
    333 awi_shutdown(void *arg)
    334 {
    335 	struct awi_softc *sc = arg;
    336 	struct ifnet *ifp = &sc->sc_if;
    337 
    338 	if (sc->sc_attached)
    339 		awi_stop(ifp, 1);
    340 }
    341 
    342 int
    343 awi_intr(void *arg)
    344 {
    345 	struct awi_softc *sc = arg;
    346 
    347 	if (!sc->sc_enabled || !sc->sc_enab_intr ||
    348 	    !device_is_active(sc->sc_dev)) {
    349 		DPRINTF(("awi_intr: stray interrupt: "
    350 		    "enabled %d enab_intr %d invalid %d\n",
    351 		    sc->sc_enabled, sc->sc_enab_intr,
    352 		    !device_is_active(sc->sc_dev)));
    353 		return 0;
    354 	}
    355 
    356 	softint_schedule(sc->sc_soft_ih);
    357 	return 1;
    358 }
    359 
    360 static void
    361 awi_softintr(void *arg)
    362 {
    363 	struct awi_softc *sc = arg;
    364 	uint16_t status;
    365 	int ocansleep;
    366 	int s;
    367 #ifdef AWI_DEBUG
    368 	static const char *intname[] = {
    369 	    "CMD", "RX", "TX", "SCAN_CMPLT",
    370 	    "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
    371 	    "TXDATA", "TXBCAST", "TXPS", "TXCF",
    372 	    "TXMGT", "#13", "RXDATA", "RXMGT"
    373 	};
    374 #endif
    375 
    376 	s = splnet();
    377 	am79c930_gcr_setbits(&sc->sc_chip,
    378 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
    379 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
    380 	ocansleep = sc->sc_cansleep;
    381 	sc->sc_cansleep = 0;
    382 
    383 	for (;;) {
    384 		if (awi_intr_lock(sc) != 0)
    385 			break;
    386 		status = awi_read_1(sc, AWI_INTSTAT);
    387 		awi_write_1(sc, AWI_INTSTAT, 0);
    388 		awi_write_1(sc, AWI_INTSTAT, 0);
    389 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
    390 		awi_write_1(sc, AWI_INTSTAT2, 0);
    391 		DELAY(10);
    392 		awi_intr_unlock(sc);
    393 		if (!sc->sc_cmd_inprog)
    394 			status &= ~AWI_INT_CMD;	/* make sure */
    395 		if (status == 0)
    396 			break;
    397 #ifdef AWI_DEBUG
    398 		if (awi_debug > 1) {
    399 			int i;
    400 
    401 			printf("awi_intr: status 0x%04x", status);
    402 			for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
    403 			    i++) {
    404 				if (status & (1 << i))
    405 					printf(" %s", intname[i]);
    406 			}
    407 			printf("\n");
    408 		}
    409 #endif
    410 		if (status & AWI_INT_RX)
    411 			awi_rx_int(sc);
    412 		if (status & AWI_INT_TX)
    413 			awi_tx_int(sc);
    414 		if (status & AWI_INT_CMD)
    415 			awi_cmd_done(sc);
    416 		if (status & AWI_INT_SCAN_CMPLT) {
    417 			if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
    418 			    sc->sc_substate == AWI_ST_NONE)
    419 				ieee80211_next_scan(&sc->sc_ic);
    420 		}
    421 	}
    422 
    423 	sc->sc_cansleep = ocansleep;
    424 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
    425 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
    426 	splx(s);
    427 }
    428 
    429 
    430 static int
    431 awi_init(struct ifnet *ifp)
    432 {
    433 	struct awi_softc *sc = ifp->if_softc;
    434 	struct ieee80211com *ic = &sc->sc_ic;
    435 	struct ieee80211_node *ni = ic->ic_bss;
    436 	struct ieee80211_rateset *rs;
    437 	int error, rate, i;
    438 
    439 	DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
    440 	if (sc->sc_enabled) {
    441 		awi_stop(ifp, 0);
    442 	} else {
    443 		if (sc->sc_enable)
    444 			(*sc->sc_enable)(sc);
    445 		sc->sc_enabled = 1;
    446 		if ((error = awi_hw_init(sc)) != 0) {
    447 			if (sc->sc_disable)
    448 				(*sc->sc_disable)(sc);
    449 			sc->sc_enabled = 0;
    450 			return error;
    451 		}
    452 	}
    453 	ic->ic_state = IEEE80211_S_INIT;
    454 
    455 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
    456 	switch (ic->ic_opmode) {
    457 	case IEEE80211_M_STA:
    458 		sc->sc_mib_local.Network_Mode = 1;
    459 		sc->sc_mib_local.Acting_as_AP = 0;
    460 		break;
    461 	case IEEE80211_M_IBSS:
    462 		ic->ic_flags |= IEEE80211_F_IBSSON;
    463 		/* FALLTHRU */
    464 	case IEEE80211_M_AHDEMO:
    465 		sc->sc_mib_local.Network_Mode = 0;
    466 		sc->sc_mib_local.Acting_as_AP = 0;
    467 		break;
    468 	case IEEE80211_M_HOSTAP:
    469 		sc->sc_mib_local.Network_Mode = 1;
    470 		sc->sc_mib_local.Acting_as_AP = 1;
    471 		break;
    472 	case IEEE80211_M_MONITOR:
    473 		return ENODEV;
    474 	}
    475 #if 0
    476 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
    477 #endif
    478 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
    479 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
    480 	sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen;
    481 	memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid,
    482 	    ic->ic_des_esslen);
    483 
    484 	/* Configure basic rate */
    485 	if (ic->ic_phytype == IEEE80211_T_FH)
    486 		rs = &ic->ic_sup_rates[IEEE80211_MODE_FH];
    487 	else
    488 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
    489 	if (ic->ic_fixed_rate != -1) {
    490 		rate = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    491 	} else {
    492 		rate = 0;
    493 		for (i = 0; i < rs->rs_nrates; i++) {
    494 			if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
    495 			    rate < (rs->rs_rates[i] & IEEE80211_RATE_VAL))
    496 				rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
    497 		}
    498 	}
    499 	rate *= 5;
    500 	LE_WRITE_2(&sc->sc_mib_mac.aStation_Basic_Rate, rate);
    501 
    502 	if ((error = awi_mode_init(sc)) != 0) {
    503 		DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
    504 		awi_stop(ifp, 1);
    505 		return error;
    506 	}
    507 
    508 	/* Start transmitter */
    509 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
    510 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
    511 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
    512 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
    513 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
    514 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
    515 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
    516 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
    517 	awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
    518 	awi_write_4(sc, AWI_CA_TX_MGT, 0);
    519 	awi_write_4(sc, AWI_CA_TX_BCAST, 0);
    520 	awi_write_4(sc, AWI_CA_TX_PS, 0);
    521 	awi_write_4(sc, AWI_CA_TX_CF, 0);
    522 	if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
    523 		DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
    524 		awi_stop(ifp, 1);
    525 		return error;
    526 	}
    527 
    528 	/* Start receiver */
    529 	if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
    530 		DPRINTF(("awi_init: failed to start receiver: %d\n", error));
    531 		awi_stop(ifp, 1);
    532 		return error;
    533 	}
    534 	sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
    535 	sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
    536 
    537 	ifp->if_flags |= IFF_RUNNING;
    538 	ifp->if_flags &= ~IFF_OACTIVE;
    539 	ic->ic_state = IEEE80211_S_INIT;
    540 
    541 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
    542 	    ic->ic_opmode == IEEE80211_M_HOSTAP) {
    543 		ni->ni_chan = ic->ic_ibss_chan;
    544 		ni->ni_intval = ic->ic_lintval;
    545 		ni->ni_rssi = 0;
    546 		ni->ni_rstamp = 0;
    547 		memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
    548 		ni->ni_rates =
    549 		    ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    550 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
    551 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    552 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
    553 			ni->ni_esslen = ic->ic_des_esslen;
    554 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
    555 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
    556 			if (ic->ic_phytype == IEEE80211_T_FH) {
    557 				ni->ni_fhdwell = 200;	/* XXX */
    558 				ni->ni_fhindex = 1;
    559 			}
    560 		} else {
    561 			ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
    562 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
    563 			ni->ni_esslen = 0;
    564 		}
    565 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    566 			ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
    567 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
    568 			ic->ic_flags |= IEEE80211_F_SIBSS;
    569 		ic->ic_state = IEEE80211_S_SCAN;	/*XXX*/
    570 		sc->sc_substate = AWI_ST_NONE;
    571 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    572 	} else {
    573 		/* XXX check sc->sc_cur_chan */
    574 		ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
    575 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    576 	}
    577 	return 0;
    578 }
    579 
    580 static void
    581 awi_stop(struct ifnet *ifp, int disable)
    582 {
    583 	struct awi_softc *sc = ifp->if_softc;
    584 
    585 	if (!sc->sc_enabled)
    586 		return;
    587 
    588 	DPRINTF(("awi_stop(%d)\n", disable));
    589 
    590 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
    591 
    592 	if (device_is_active(sc->sc_dev)) {
    593 		if (sc->sc_cmd_inprog)
    594 			(void)awi_cmd_wait(sc);
    595 		(void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
    596 		sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
    597 		awi_write_1(sc, AWI_CA_FTX_DATA, 1);
    598 		awi_write_1(sc, AWI_CA_FTX_MGT, 0);
    599 		awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
    600 		awi_write_1(sc, AWI_CA_FTX_PS, 0);
    601 		awi_write_1(sc, AWI_CA_FTX_CF, 0);
    602 		(void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
    603 	}
    604 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    605 	ifp->if_timer = 0;
    606 	sc->sc_tx_timer = sc->sc_rx_timer = 0;
    607 	m_freem(sc->sc_rxpend);
    608 	sc->sc_rxpend = NULL;
    609 	IFQ_PURGE(&ifp->if_snd);
    610 
    611 	if (disable) {
    612 		if (device_is_active(sc->sc_dev))
    613 			am79c930_gcr_setbits(&sc->sc_chip,
    614 			    AM79C930_GCR_CORESET);
    615 		if (sc->sc_disable)
    616 			(*sc->sc_disable)(sc);
    617 		sc->sc_enabled = 0;
    618 	}
    619 }
    620 
    621 static void
    622 awi_start(struct ifnet *ifp)
    623 {
    624 	struct awi_softc *sc = ifp->if_softc;
    625 	struct ieee80211com *ic = &sc->sc_ic;
    626 	struct ether_header *eh;
    627 	struct ieee80211_node *ni;
    628 	struct ieee80211_frame *wh;
    629 	struct mbuf *m, *m0;
    630 	int len, dowep;
    631 	uint32_t txd, frame, ntxd;
    632 	uint8_t rate;
    633 
    634 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev))
    635 		return;
    636 
    637 	for (;;) {
    638 		txd = sc->sc_txnext;
    639 		IF_POLL(&ic->ic_mgtq, m0);
    640 		dowep = 0;
    641 		if (m0 != NULL) {
    642 			len = m0->m_pkthdr.len;
    643 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
    644 				ifp->if_flags |= IFF_OACTIVE;
    645 				break;
    646 			}
    647 			IF_DEQUEUE(&ic->ic_mgtq, m0);
    648 			ni = M_GETCTX(m0, struct ieee80211_node *);
    649 		} else {
    650 			if (ic->ic_state != IEEE80211_S_RUN)
    651 				break;
    652 			IFQ_POLL(&ifp->if_snd, m0);
    653 			if (m0 == NULL)
    654 				break;
    655 			/*
    656 			 * Need to calculate the real length to determine
    657 			 * if the transmit buffer has a room for the packet.
    658 			 */
    659 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
    660 			if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
    661 				len += sizeof(struct llc) -
    662 				    sizeof(struct ether_header);
    663 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    664 				dowep = 1;
    665 				len += IEEE80211_WEP_IVLEN +
    666 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
    667 			}
    668 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
    669 				ifp->if_flags |= IFF_OACTIVE;
    670 				break;
    671 			}
    672 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    673 			if_statinc(ifp, if_opackets);
    674 			bpf_mtap(ifp, m0, BPF_D_OUT);
    675 			eh = mtod(m0, struct ether_header *);
    676 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
    677 			if (ni == NULL) {
    678 				if_statinc(ifp, if_oerrors);
    679 				continue;
    680 			}
    681 			if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
    682 				m0 = awi_ether_encap(sc, m0);
    683 			else {
    684 				m0 = ieee80211_encap(ic, m0, ni);
    685 			}
    686 			if (m0 == NULL) {
    687 				ieee80211_free_node(ni);
    688 				if_statinc(ifp, if_oerrors);
    689 				continue;
    690 			}
    691 			wh = mtod(m0, struct ieee80211_frame *);
    692 			if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    693 			    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
    694 			     ic->ic_opmode == IEEE80211_M_IBSS) &&
    695 			    sc->sc_adhoc_ap == 0 &&
    696 			    (ifp->if_flags & IFF_LINK0) == 0 &&
    697 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    698 			    IEEE80211_FC0_TYPE_DATA) {
    699 				m_freem(m0);
    700 				ieee80211_free_node(ni);
    701 				if_statinc(ifp, if_oerrors);
    702 				continue;
    703 			}
    704 		}
    705 		bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
    706 		if (dowep) {
    707 			if ((ieee80211_crypto_encap(ic, ni, m0)) == NULL) {
    708 				m_freem(m0);
    709 				ieee80211_free_node(ni);
    710 				if_statinc(ifp, if_oerrors);
    711 				continue;
    712 			}
    713 		}
    714 		ieee80211_free_node(ni);
    715 #ifdef DIAGNOSTIC
    716 		if (m0->m_pkthdr.len != len) {
    717 			printf("%s: length %d should be %d\n",
    718 			    sc->sc_if.if_xname, m0->m_pkthdr.len, len);
    719 			m_freem(m0);
    720 			if_statinc(ifp, if_oerrors);
    721 			continue;
    722 		}
    723 #endif
    724 
    725 		if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
    726 			ieee80211_dump_pkt(m0->m_data, m0->m_len,
    727 			    ic->ic_bss->ni_rates.
    728 				rs_rates[ic->ic_bss->ni_txrate] &
    729 			    IEEE80211_RATE_VAL, -1);
    730 
    731 		for (m = m0, len = 0; m != NULL; m = m->m_next) {
    732 			awi_write_bytes(sc, frame + len, mtod(m, uint8_t *),
    733 			    m->m_len);
    734 			len += m->m_len;
    735 		}
    736 		m_freem(m0);
    737 		rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
    738 		    IEEE80211_RATE_VAL) * 5;
    739 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
    740 		awi_write_4(sc, txd + AWI_TXD_START, frame);
    741 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
    742 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
    743 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
    744 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
    745 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
    746 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
    747 		sc->sc_txnext = ntxd;
    748 
    749 		sc->sc_tx_timer = 5;
    750 		ifp->if_timer = 1;
    751 	}
    752 }
    753 
    754 static void
    755 awi_watchdog(struct ifnet *ifp)
    756 {
    757 	struct awi_softc *sc = ifp->if_softc;
    758 	uint32_t prevdone;
    759 	int ocansleep;
    760 
    761 	ifp->if_timer = 0;
    762 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev))
    763 		return;
    764 
    765 	ocansleep = sc->sc_cansleep;
    766 	sc->sc_cansleep = 0;
    767 	if (sc->sc_tx_timer) {
    768 		if (--sc->sc_tx_timer == 0) {
    769 			printf("%s: device timeout\n", ifp->if_xname);
    770 			prevdone = sc->sc_txdone;
    771 			awi_tx_int(sc);
    772 			if (sc->sc_txdone == prevdone) {
    773 				if_statinc(ifp, if_oerrors);
    774 				awi_init(ifp);
    775 				goto out;
    776 			}
    777 		}
    778 		ifp->if_timer = 1;
    779 	}
    780 	if (sc->sc_rx_timer) {
    781 		if (--sc->sc_rx_timer == 0) {
    782 			if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
    783 				ieee80211_new_state(&sc->sc_ic,
    784 				    IEEE80211_S_SCAN, -1);
    785 				goto out;
    786 			}
    787 		} else
    788 			ifp->if_timer = 1;
    789 	}
    790 	/* TODO: rate control */
    791 	ieee80211_watchdog(&sc->sc_ic);
    792   out:
    793 	sc->sc_cansleep = ocansleep;
    794 }
    795 
    796 static int
    797 awi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    798 {
    799 	struct awi_softc *sc = ifp->if_softc;
    800 	int s, error;
    801 
    802 	s = splnet();
    803 	/* Serialize ioctl, since we may sleep */
    804 	if ((error = awi_lock(sc)) != 0)
    805 		goto cantlock;
    806 
    807 	switch (cmd) {
    808 	case SIOCSIFFLAGS:
    809 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    810 			break;
    811 		if (ifp->if_flags & IFF_UP) {
    812 			if (sc->sc_enabled) {
    813 				/*
    814 				 * To avoid rescanning another access point,
    815 				 * do not call awi_init() here.  Instead,
    816 				 * only reflect promisc mode settings.
    817 				 */
    818 				error = awi_mode_init(sc);
    819 			} else
    820 				error = awi_init(ifp);
    821 		} else if (sc->sc_enabled)
    822 			awi_stop(ifp, 1);
    823 		break;
    824 	case SIOCADDMULTI:
    825 	case SIOCDELMULTI:
    826 		error = ether_ioctl(ifp, cmd, data);
    827 		if (error == ENETRESET) {
    828 			/* Do not rescan */
    829 			if (ifp->if_flags & IFF_RUNNING)
    830 				error = awi_mode_init(sc);
    831 			else
    832 				error = 0;
    833 		}
    834 		break;
    835 	default:
    836 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
    837 		if (error == ENETRESET) {
    838 			if (sc->sc_enabled)
    839 				error = awi_init(ifp);
    840 			else
    841 				error = 0;
    842 		}
    843 		break;
    844 	}
    845 	awi_unlock(sc);
    846   cantlock:
    847 	splx(s);
    848 	return error;
    849 }
    850 
    851 /*
    852  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
    853  *
    854  * TBD factor with ieee80211_media_change
    855  */
    856 static int
    857 awi_media_change(struct ifnet *ifp)
    858 {
    859 	struct awi_softc *sc = ifp->if_softc;
    860 	struct ieee80211com *ic = &sc->sc_ic;
    861 	struct ifmedia_entry *ime;
    862 	enum ieee80211_opmode newmode;
    863 	int i, rate, newadhoc_ap, error = 0;
    864 
    865 	ime = ic->ic_media.ifm_cur;
    866 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
    867 		i = -1;
    868 	} else {
    869 		struct ieee80211_rateset *rs =
    870 		    &ic->ic_sup_rates[(ic->ic_phytype == IEEE80211_T_FH)
    871 		    ? IEEE80211_MODE_FH : IEEE80211_MODE_11B];
    872 		rate = ieee80211_media2rate(ime->ifm_media);
    873 		if (rate == 0)
    874 			return EINVAL;
    875 		for (i = 0; i < rs->rs_nrates; i++) {
    876 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
    877 				break;
    878 		}
    879 		if (i == rs->rs_nrates)
    880 			return EINVAL;
    881 	}
    882 	if (ic->ic_fixed_rate != i) {
    883 		ic->ic_fixed_rate = i;
    884 		error = ENETRESET;
    885 	}
    886 
    887 	/*
    888 	 * Combination of mediaopt
    889 	 *
    890 	 * hostap adhoc flag0	opmode  adhoc_ap	comment
    891 	 *   +      -     -	HOSTAP      0		HostAP
    892 	 *   -      +     -	IBSS        0		IBSS
    893 	 *   -      +     +	AHDEMO      0		WaveLAN adhoc
    894 	 *   -      -     +	IBSS        1		Melco old Sta
    895 	 *							also LINK0
    896 	 *   -      -     -	STA         0		Infra Station
    897 	 */
    898 	newadhoc_ap = 0;
    899 	if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
    900 		newmode = IEEE80211_M_HOSTAP;
    901 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
    902 		if (ic->ic_phytype == IEEE80211_T_DS &&
    903 		    (ime->ifm_media & IFM_FLAG0))
    904 			newmode = IEEE80211_M_AHDEMO;
    905 		else
    906 			newmode = IEEE80211_M_IBSS;
    907 	} else if (ime->ifm_media & IFM_FLAG0) {
    908 		newmode = IEEE80211_M_IBSS;
    909 		newadhoc_ap = 1;
    910 	} else
    911 		newmode = IEEE80211_M_STA;
    912 	if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
    913 		ic->ic_opmode = newmode;
    914 		sc->sc_adhoc_ap = newadhoc_ap;
    915 		error = ENETRESET;
    916 	}
    917 
    918 	if (error == ENETRESET) {
    919 		if (sc->sc_enabled)
    920 			error = awi_init(ifp);
    921 		else
    922 			error = 0;
    923 	}
    924 	return error;
    925 }
    926 
    927 static void
    928 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    929 {
    930 	struct awi_softc *sc = ifp->if_softc;
    931 	struct ieee80211com *ic = &sc->sc_ic;
    932 	int rate;
    933 	enum ieee80211_phymode mode;
    934 
    935 	imr->ifm_status = IFM_AVALID;
    936 	if (ic->ic_state == IEEE80211_S_RUN)
    937 		imr->ifm_status |= IFM_ACTIVE;
    938 	imr->ifm_active = IFM_IEEE80211;
    939 	if (ic->ic_phytype == IEEE80211_T_FH)
    940 		mode = IEEE80211_MODE_FH;
    941 	else
    942 		mode = IEEE80211_MODE_11B;
    943 	if (ic->ic_state == IEEE80211_S_RUN) {
    944 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
    945 		    IEEE80211_RATE_VAL;
    946 	} else {
    947 		if (ic->ic_fixed_rate == -1)
    948 			rate = 0;
    949 		else
    950 			rate = ic->ic_sup_rates[mode].
    951 			    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    952 	}
    953 	imr->ifm_active |= ieee80211_rate2media(ic, rate, mode);
    954 	switch (ic->ic_opmode) {
    955 	case IEEE80211_M_MONITOR: /* We should never reach here */
    956 		break;
    957 	case IEEE80211_M_STA:
    958 		break;
    959 	case IEEE80211_M_IBSS:
    960 		if (sc->sc_adhoc_ap)
    961 			imr->ifm_active |= IFM_FLAG0;
    962 		else
    963 			imr->ifm_active |= IFM_IEEE80211_ADHOC;
    964 		break;
    965 	case IEEE80211_M_AHDEMO:
    966 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
    967 		break;
    968 	case IEEE80211_M_HOSTAP:
    969 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
    970 		break;
    971 	}
    972 }
    973 
    974 static int
    975 awi_mode_init(struct awi_softc *sc)
    976 {
    977 	struct ethercom *ec = &sc->sc_ec;
    978 	struct ifnet *ifp = &sc->sc_if;
    979 	int n, error;
    980 	struct ether_multi *enm;
    981 	struct ether_multistep step;
    982 
    983 	/* Reinitialize multicast filter */
    984 	n = 0;
    985 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
    986 	if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
    987 	    (ifp->if_flags & IFF_PROMISC)) {
    988 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
    989 		goto set_mib;
    990 	}
    991 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
    992 	ETHER_LOCK(ec);
    993 	ETHER_FIRST_MULTI(step, ec, enm);
    994 	while (enm != NULL) {
    995 		if (n == AWI_GROUP_ADDR_SIZE ||
    996 		    !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
    997 			ETHER_UNLOCK(ec);
    998 			goto set_mib;
    999 		}
   1000 		IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
   1001 		    enm->enm_addrlo);
   1002 		n++;
   1003 		ETHER_NEXT_MULTI(step, enm);
   1004 	}
   1005 	ETHER_UNLOCK(ec);
   1006 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
   1007 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
   1008 		    IEEE80211_ADDR_LEN);
   1009 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
   1010 
   1011   set_mib:
   1012 	if (sc->sc_mib_local.Accept_All_Multicast_Dis)
   1013 		ifp->if_flags &= ~IFF_ALLMULTI;
   1014 	else
   1015 		ifp->if_flags |= IFF_ALLMULTI;
   1016 	sc->sc_mib_mgt.Wep_Required =
   1017 	    (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? AWI_WEP_ON : AWI_WEP_OFF;
   1018 
   1019 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
   1020 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
   1021 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
   1022 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
   1023 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
   1024 		DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
   1025 		return error;
   1026 	}
   1027 	return 0;
   1028 }
   1029 
   1030 static void
   1031 awi_rx_int(struct awi_softc *sc)
   1032 {
   1033 	struct ieee80211com *ic = &sc->sc_ic;
   1034 	struct ifnet *ifp = &sc->sc_if;
   1035 	struct ieee80211_frame_min *wh;
   1036 	struct ieee80211_node *ni;
   1037 	uint8_t state, rate, rssi;
   1038 	uint16_t len;
   1039 	uint32_t frame, next, rstamp, rxoff;
   1040 	struct mbuf *m;
   1041 
   1042 	rxoff = sc->sc_rxdoff;
   1043 	for (;;) {
   1044 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
   1045 		if (state & AWI_RXD_ST_OWN)
   1046 			break;
   1047 		if (!(state & AWI_RXD_ST_CONSUMED)) {
   1048 			if (sc->sc_substate != AWI_ST_NONE)
   1049 				goto rx_next;
   1050 			if (state & AWI_RXD_ST_RXERROR) {
   1051 				if_statinc(ifp, if_ierrors);
   1052 				goto rx_next;
   1053 			}
   1054 			len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
   1055 			rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
   1056 			rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
   1057 			frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
   1058 			    0x7fff;
   1059 			rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
   1060 			m = awi_devget(sc, frame, len);
   1061 			if (m == NULL) {
   1062 				if_statinc(ifp, if_ierrors);
   1063 				goto rx_next;
   1064 			}
   1065 			if (state & AWI_RXD_ST_LF) {
   1066 				/* TODO check my bss */
   1067 				if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
   1068 				    sc->sc_ic.ic_state == IEEE80211_S_RUN) {
   1069 					sc->sc_rx_timer = 10;
   1070 					ifp->if_timer = 1;
   1071 				}
   1072 				if ((ifp->if_flags & IFF_DEBUG) &&
   1073 				    (ifp->if_flags & IFF_LINK2))
   1074 					ieee80211_dump_pkt(m->m_data, m->m_len,
   1075 					    rate / 5, rssi);
   1076 				if ((ifp->if_flags & IFF_LINK0) ||
   1077 				    sc->sc_adhoc_ap)
   1078 					m = awi_ether_modcap(sc, m);
   1079 				else
   1080 					m = m_pullup(m, sizeof(*wh));
   1081 				if (m == NULL) {
   1082 					if_statinc(ifp, if_ierrors);
   1083 					goto rx_next;
   1084 				}
   1085 				wh = mtod(m, struct ieee80211_frame_min *);
   1086 				ni = ieee80211_find_rxnode(ic, wh);
   1087 				ieee80211_input(ic, m, ni, rssi, rstamp);
   1088 				/*
   1089 				 * The frame may have caused the
   1090 				 * node to be marked for reclamation
   1091 				 * (e.g. in response to a DEAUTH
   1092 				 * message) so use release_node here
   1093 				 * instead of unref_node.
   1094 				 */
   1095 				ieee80211_free_node(ni);
   1096 			} else
   1097 				sc->sc_rxpend = m;
   1098   rx_next:
   1099 			state |= AWI_RXD_ST_CONSUMED;
   1100 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1101 		}
   1102 		next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
   1103 		if (next & AWI_RXD_NEXT_LAST)
   1104 			break;
   1105 		/* Make sure the next pointer is correct */
   1106 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
   1107 			break;
   1108 		state |= AWI_RXD_ST_OWN;
   1109 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1110 		rxoff = next & 0x7fff;
   1111 	}
   1112 	sc->sc_rxdoff = rxoff;
   1113 }
   1114 
   1115 static void
   1116 awi_tx_int(struct awi_softc *sc)
   1117 {
   1118 	struct ifnet *ifp = &sc->sc_if;
   1119 	uint8_t flags;
   1120 
   1121 	while (sc->sc_txdone != sc->sc_txnext) {
   1122 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
   1123 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
   1124 			break;
   1125 		if (flags & AWI_TXD_ST_ERROR)
   1126 			if_statinc(ifp, if_oerrors);
   1127 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
   1128 		    0x7fff;
   1129 	}
   1130 	DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
   1131 	    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
   1132 	sc->sc_tx_timer = 0;
   1133 	ifp->if_flags &= ~IFF_OACTIVE;
   1134 	awi_start(ifp); /* in softint */
   1135 }
   1136 
   1137 static struct mbuf *
   1138 awi_devget(struct awi_softc *sc, uint32_t off, uint16_t len)
   1139 {
   1140 	struct ifnet *ifp = &sc->sc_if;
   1141 	struct mbuf *m;
   1142 	struct mbuf *top, **mp;
   1143 	u_int tlen;
   1144 
   1145 	top = sc->sc_rxpend;
   1146 	mp = &top;
   1147 	if (top != NULL) {
   1148 		sc->sc_rxpend = NULL;
   1149 		top->m_pkthdr.len += len;
   1150 		m = top;
   1151 		while (*mp != NULL) {
   1152 			m = *mp;
   1153 			mp = &m->m_next;
   1154 		}
   1155 		if (m->m_flags & M_EXT)
   1156 			tlen = m->m_ext.ext_size;
   1157 		else if (m->m_flags & M_PKTHDR)
   1158 			tlen = MHLEN;
   1159 		else
   1160 			tlen = MLEN;
   1161 		tlen -= m->m_len;
   1162 		if (tlen > len)
   1163 			tlen = len;
   1164 		awi_read_bytes(sc, off, mtod(m, uint8_t *) + m->m_len, tlen);
   1165 		off += tlen;
   1166 		len -= tlen;
   1167 	}
   1168 
   1169 	while (len > 0) {
   1170 		if (top == NULL) {
   1171 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1172 			if (m == NULL)
   1173 				return NULL;
   1174 			m_set_rcvif(m, ifp);
   1175 			m->m_pkthdr.len = len;
   1176 			m->m_len = MHLEN;
   1177 			m->m_flags |= M_HASFCS;
   1178 		} else {
   1179 			MGET(m, M_DONTWAIT, MT_DATA);
   1180 			if (m == NULL) {
   1181 				m_freem(top);
   1182 				return NULL;
   1183 			}
   1184 			m->m_len = MLEN;
   1185 		}
   1186 		if (len >= MINCLSIZE) {
   1187 			MCLGET(m, M_DONTWAIT);
   1188 			if (m->m_flags & M_EXT)
   1189 				m->m_len = m->m_ext.ext_size;
   1190 		}
   1191 		if (top == NULL) {
   1192 			int hdrlen = sizeof(struct ieee80211_frame) +
   1193 			    sizeof(struct llc);
   1194 			char *newdata = (char *)
   1195 			    ALIGN(m->m_data + hdrlen) - hdrlen;
   1196 			m->m_len -= newdata - m->m_data;
   1197 			m->m_data = newdata;
   1198 		}
   1199 		if (m->m_len > len)
   1200 			m->m_len = len;
   1201 		awi_read_bytes(sc, off, mtod(m, uint8_t *), m->m_len);
   1202 		off += m->m_len;
   1203 		len -= m->m_len;
   1204 		*mp = m;
   1205 		mp = &m->m_next;
   1206 	}
   1207 	return top;
   1208 }
   1209 
   1210 /*
   1211  * Initialize hardware and start firmware to accept commands.
   1212  * Called everytime after power on firmware.
   1213  */
   1214 
   1215 static int
   1216 awi_hw_init(struct awi_softc *sc)
   1217 {
   1218 	uint8_t status;
   1219 	uint16_t intmask;
   1220 	int i, error;
   1221 
   1222 	sc->sc_enab_intr = 0;
   1223 	awi_drvstate(sc, AWI_DRV_RESET);
   1224 
   1225 	/* Reset firmware */
   1226 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1227 	DELAY(100);
   1228 	awi_write_1(sc, AWI_SELFTEST, 0);
   1229 	awi_write_1(sc, AWI_CMD, 0);
   1230 	awi_write_1(sc, AWI_BANNER, 0);
   1231 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1232 	DELAY(100);
   1233 
   1234 	/* Wait for selftest completion */
   1235 	for (i = 0; ; i++) {
   1236 		if (!device_is_active(sc->sc_dev))
   1237 			return ENXIO;
   1238 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
   1239 			printf("%s: failed to complete selftest (timeout)\n",
   1240 			    sc->sc_if.if_xname);
   1241 			return ENXIO;
   1242 		}
   1243 		status = awi_read_1(sc, AWI_SELFTEST);
   1244 		if ((status & 0xf0) == 0xf0)
   1245 			break;
   1246 		if (sc->sc_cansleep) {
   1247 			sc->sc_sleep_cnt++;
   1248 			(void)tsleep(sc, PWAIT, "awitst", 1);
   1249 			sc->sc_sleep_cnt--;
   1250 		} else {
   1251 			DELAY(1000*1000/hz);
   1252 		}
   1253 	}
   1254 	if (status != AWI_SELFTEST_PASSED) {
   1255 		printf("%s: failed to complete selftest (code %x)\n",
   1256 		    sc->sc_if.if_xname, status);
   1257 		return ENXIO;
   1258 	}
   1259 
   1260 	/* Check banner to confirm firmware write it */
   1261 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
   1262 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
   1263 		printf("%s: failed to complete selftest (bad banner)\n",
   1264 		    sc->sc_if.if_xname);
   1265 		for (i = 0; i < AWI_BANNER_LEN; i++)
   1266 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
   1267 		printf("\n");
   1268 		return ENXIO;
   1269 	}
   1270 
   1271 	/* Initializing interrupt */
   1272 	sc->sc_enab_intr = 1;
   1273 	error = awi_intr_lock(sc);
   1274 	if (error)
   1275 		return error;
   1276 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
   1277 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
   1278 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
   1279 	awi_write_1(sc, AWI_INTMASK2, 0);
   1280 	awi_write_1(sc, AWI_INTSTAT, 0);
   1281 	awi_write_1(sc, AWI_INTSTAT2, 0);
   1282 	awi_intr_unlock(sc);
   1283 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
   1284 
   1285 	/* Issuing interface test command */
   1286 	error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
   1287 	if (error) {
   1288 		printf("%s: failed to complete selftest",
   1289 		    sc->sc_if.if_xname);
   1290 		if (error == ENXIO)
   1291 			printf(" (no hardware)\n");
   1292 		else if (error != EWOULDBLOCK)
   1293 			printf(" (error %d)\n", error);
   1294 		else if (sc->sc_cansleep)
   1295 			printf(" (lost interrupt)\n");
   1296 		else
   1297 			printf(" (command timeout)\n");
   1298 		return error;
   1299 	}
   1300 
   1301 	/* Initialize VBM */
   1302 	awi_write_1(sc, AWI_VBM_OFFSET, 0);
   1303 	awi_write_1(sc, AWI_VBM_LENGTH, 1);
   1304 	awi_write_1(sc, AWI_VBM_BITMAP, 0);
   1305 	return 0;
   1306 }
   1307 
   1308 /*
   1309  * Extract the factory default MIB value from firmware and assign the driver
   1310  * default value.
   1311  * Called once at attaching the interface.
   1312  */
   1313 
   1314 static int
   1315 awi_init_mibs(struct awi_softc *sc)
   1316 {
   1317 	int chan, i, error;
   1318 	struct ieee80211com *ic = &sc->sc_ic;
   1319 	const struct awi_chanset *cs;
   1320 
   1321 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
   1322 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
   1323 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
   1324 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
   1325 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
   1326 		printf("%s: failed to get default mib value (error %d)\n",
   1327 		    sc->sc_if.if_xname, error);
   1328 		return error;
   1329 	}
   1330 
   1331 	memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
   1332 	for (cs = awi_chanset; ; cs++) {
   1333 		if (cs->cs_type == 0) {
   1334 			printf("%s: failed to set available channel\n",
   1335 			    sc->sc_if.if_xname);
   1336 			return ENXIO;
   1337 		}
   1338 		if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
   1339 		    cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
   1340 			break;
   1341 	}
   1342 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1343 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
   1344 			chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
   1345 			setbit(sc->sc_ic.ic_chan_avail, chan);
   1346 			/* XXX for FHSS, does frequency matter? */
   1347 			ic->ic_channels[chan].ic_freq = 0;
   1348 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
   1349 			/*
   1350 			 * According to the IEEE 802.11 specification,
   1351 			 * hop pattern parameter for FH phy should be
   1352 			 * incremented by 3 for given hop chanset, i.e.,
   1353 			 * the chanset parameter is calculated for given
   1354 			 * hop patter.  However, BayStack 650 Access Points
   1355 			 * apparently use fixed hop chanset parameter value
   1356 			 * 1 for any hop pattern.  So we also try this
   1357 			 * combination of hop chanset and pattern.
   1358 			 */
   1359 			chan = IEEE80211_FH_CHAN(1, i);
   1360 			setbit(sc->sc_ic.ic_chan_avail, chan);
   1361 			ic->ic_channels[chan].ic_freq = 0; /* XXX */
   1362 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
   1363 		}
   1364 	} else {
   1365 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
   1366 			setbit(sc->sc_ic.ic_chan_avail, i);
   1367 			ic->ic_channels[i].ic_freq =
   1368 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
   1369 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
   1370 		}
   1371 	}
   1372 	sc->sc_cur_chan = cs->cs_def;
   1373 	ic->ic_ibss_chan = &ic->ic_channels[cs->cs_def];
   1374 
   1375 	sc->sc_mib_local.Fragmentation_Dis = 1;
   1376 	sc->sc_mib_local.Add_PLCP_Dis = 0;
   1377 	sc->sc_mib_local.MAC_Hdr_Prsv = 0;
   1378 	sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
   1379 	sc->sc_mib_local.Re_Assembly_Dis = 1;
   1380 	sc->sc_mib_local.Strip_PLCP_Dis = 0;
   1381 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
   1382 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
   1383 	sc->sc_mib_local.Check_Seq_Cntl_Dis = 0;
   1384 	sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
   1385 	sc->sc_mib_local.Network_Mode = 1;
   1386 	sc->sc_mib_local.PWD_Lvl = 0;
   1387 	sc->sc_mib_local.CFP_Mode = 0;
   1388 
   1389 	/* Allocate buffers */
   1390 	sc->sc_txbase = AWI_BUFFERS;
   1391 	sc->sc_txend = sc->sc_txbase +
   1392 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
   1393 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
   1394 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
   1395 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
   1396 	    sc->sc_txend - sc->sc_txbase);
   1397 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
   1398 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
   1399 	    AWI_BUFFERS_END - sc->sc_txend);
   1400 	sc->sc_mib_local.Acting_as_AP = 0;
   1401 	sc->sc_mib_local.Fill_CFP = 0;
   1402 
   1403 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
   1404 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
   1405 
   1406 	sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
   1407 	sc->sc_mib_mgt.aDTIM_Period = 1;
   1408 	LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
   1409 	return 0;
   1410 }
   1411 
   1412 static int
   1413 awi_mib(struct awi_softc *sc, uint8_t cmd, uint8_t mib, int wflag)
   1414 {
   1415 	int error;
   1416 	uint8_t size, *ptr;
   1417 
   1418 	switch (mib) {
   1419 	case AWI_MIB_LOCAL:
   1420 		ptr = (uint8_t *)&sc->sc_mib_local;
   1421 		size = sizeof(sc->sc_mib_local);
   1422 		break;
   1423 	case AWI_MIB_ADDR:
   1424 		ptr = (uint8_t *)&sc->sc_mib_addr;
   1425 		size = sizeof(sc->sc_mib_addr);
   1426 		break;
   1427 	case AWI_MIB_MAC:
   1428 		ptr = (uint8_t *)&sc->sc_mib_mac;
   1429 		size = sizeof(sc->sc_mib_mac);
   1430 		break;
   1431 	case AWI_MIB_STAT:
   1432 		ptr = (uint8_t *)&sc->sc_mib_stat;
   1433 		size = sizeof(sc->sc_mib_stat);
   1434 		break;
   1435 	case AWI_MIB_MGT:
   1436 		ptr = (uint8_t *)&sc->sc_mib_mgt;
   1437 		size = sizeof(sc->sc_mib_mgt);
   1438 		break;
   1439 	case AWI_MIB_PHY:
   1440 		ptr = (uint8_t *)&sc->sc_mib_phy;
   1441 		size = sizeof(sc->sc_mib_phy);
   1442 		break;
   1443 	default:
   1444 		return EINVAL;
   1445 	}
   1446 	if (sc->sc_cmd_inprog) {
   1447 		if ((error = awi_cmd_wait(sc)) != 0) {
   1448 			if (error == EWOULDBLOCK) {
   1449 				DPRINTF(("awi_mib: cmd %d inprog",
   1450 				    sc->sc_cmd_inprog));
   1451 			}
   1452 			return error;
   1453 		}
   1454 	}
   1455 	sc->sc_cmd_inprog = cmd;
   1456 	if (cmd == AWI_CMD_SET_MIB)
   1457 		awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
   1458 	awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
   1459 	awi_write_1(sc, AWI_CA_MIB_SIZE, size);
   1460 	awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
   1461 	if ((error = awi_cmd(sc, cmd, wflag)) != 0)
   1462 		return error;
   1463 	if (cmd == AWI_CMD_GET_MIB) {
   1464 		awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
   1465 #ifdef AWI_DEBUG
   1466 		if (awi_debug) {
   1467 			int i;
   1468 
   1469 			printf("awi_mib: #%d:", mib);
   1470 			for (i = 0; i < size; i++)
   1471 				printf(" %02x", ptr[i]);
   1472 			printf("\n");
   1473 		}
   1474 #endif
   1475 	}
   1476 	return 0;
   1477 }
   1478 
   1479 static int
   1480 awi_cmd(struct awi_softc *sc, uint8_t cmd, int wflag)
   1481 {
   1482 	uint8_t status;
   1483 	int error = 0;
   1484 #ifdef AWI_DEBUG
   1485 	static const char *cmdname[] = {
   1486 	    "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
   1487 	    "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
   1488 	};
   1489 #endif
   1490 
   1491 #ifdef AWI_DEBUG
   1492 	if (awi_debug > 1) {
   1493 		if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
   1494 			printf("awi_cmd: #%d", cmd);
   1495 		else
   1496 			printf("awi_cmd: %s", cmdname[cmd]);
   1497 		printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
   1498 	}
   1499 #endif
   1500 	sc->sc_cmd_inprog = cmd;
   1501 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
   1502 	awi_write_1(sc, AWI_CMD, cmd);
   1503 	if (wflag == AWI_NOWAIT)
   1504 		return EINPROGRESS;
   1505 	if ((error = awi_cmd_wait(sc)) != 0)
   1506 		return error;
   1507 	status = awi_read_1(sc, AWI_CMD_STATUS);
   1508 	awi_write_1(sc, AWI_CMD, 0);
   1509 	switch (status) {
   1510 	case AWI_STAT_OK:
   1511 		break;
   1512 	case AWI_STAT_BADPARM:
   1513 		return EINVAL;
   1514 	default:
   1515 		printf("%s: command %d failed %x\n",
   1516 		    sc->sc_if.if_xname, cmd, status);
   1517 		return ENXIO;
   1518 	}
   1519 	return 0;
   1520 }
   1521 
   1522 static int
   1523 awi_cmd_wait(struct awi_softc *sc)
   1524 {
   1525 	int i, error = 0;
   1526 
   1527 	i = 0;
   1528 	while (sc->sc_cmd_inprog) {
   1529 		if (!device_is_active(sc->sc_dev))
   1530 			return ENXIO;
   1531 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
   1532 			printf("%s: failed to access hardware\n",
   1533 			    sc->sc_if.if_xname);
   1534 			config_deactivate(sc->sc_dev);
   1535 			return ENXIO;
   1536 		}
   1537 		if (sc->sc_cansleep) {
   1538 			sc->sc_sleep_cnt++;
   1539 			error = tsleep(sc, PWAIT, "awicmd",
   1540 			    AWI_CMD_TIMEOUT*hz/1000);
   1541 			sc->sc_sleep_cnt--;
   1542 		} else {
   1543 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
   1544 				awi_cmd_done(sc);
   1545 				break;
   1546 			}
   1547 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
   1548 				error = EWOULDBLOCK;
   1549 			else
   1550 				DELAY(10);
   1551 		}
   1552 		if (error)
   1553 			break;
   1554 	}
   1555 	if (error) {
   1556 		DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
   1557 		    sc->sc_cmd_inprog, error));
   1558 	}
   1559 	return error;
   1560 }
   1561 
   1562 static void
   1563 awi_cmd_done(struct awi_softc *sc)
   1564 {
   1565 	uint8_t cmd, status;
   1566 
   1567 	status = awi_read_1(sc, AWI_CMD_STATUS);
   1568 	if (status == AWI_STAT_IDLE)
   1569 		return;		/* stray interrupt */
   1570 
   1571 	cmd = sc->sc_cmd_inprog;
   1572 	sc->sc_cmd_inprog = 0;
   1573 	wakeup(sc);
   1574 	awi_write_1(sc, AWI_CMD, 0);
   1575 
   1576 	if (status != AWI_STAT_OK) {
   1577 		printf("%s: command %d failed %x\n",
   1578 		    sc->sc_if.if_xname, cmd, status);
   1579 		sc->sc_substate = AWI_ST_NONE;
   1580 		return;
   1581 	}
   1582 	if (sc->sc_substate != AWI_ST_NONE)
   1583 		(void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
   1584 }
   1585 
   1586 static int
   1587 awi_next_txd(struct awi_softc *sc, int len, uint32_t *framep, uint32_t *ntxdp)
   1588 {
   1589 	uint32_t txd, ntxd, frame;
   1590 
   1591 	txd = sc->sc_txnext;
   1592 	frame = txd + AWI_TXD_SIZE;
   1593 	if (frame + len > sc->sc_txend)
   1594 		frame = sc->sc_txbase;
   1595 	ntxd = frame + len;
   1596 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
   1597 		ntxd = sc->sc_txbase;
   1598 	*framep = frame;
   1599 	*ntxdp = ntxd;
   1600 	/*
   1601 	 * Determine if there are any room in ring buffer.
   1602 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
   1603 	 *   base........................end
   1604 	 *	   done----txd=====ntxd		OK
   1605 	 *	 --txd=====done++++ntxd--	full
   1606 	 *	 --txd=====ntxd    done--	OK
   1607 	 *	 ==ntxd    done----txd===	OK
   1608 	 *	 ==done++++ntxd----txd===	full
   1609 	 *	 ++ntxd    txd=====done++	full
   1610 	 */
   1611 	if (txd < ntxd) {
   1612 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   1613 			return ENOBUFS;
   1614 	} else {
   1615 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   1616 			return ENOBUFS;
   1617 	}
   1618 	return 0;
   1619 }
   1620 
   1621 static int
   1622 awi_lock(struct awi_softc *sc)
   1623 {
   1624 	int error = 0;
   1625 
   1626 	if (curlwp == NULL) {
   1627 		/*
   1628 		 * XXX
   1629 		 * Though driver ioctl should be called with context,
   1630 		 * KAME ipv6 stack calls ioctl in interrupt for now.
   1631 		 * We simply abort the request if there are other
   1632 		 * ioctl requests in progress.
   1633 		 */
   1634 		if (sc->sc_busy) {
   1635 			if (!device_is_active(sc->sc_dev))
   1636 				return ENXIO;
   1637 			return EWOULDBLOCK;
   1638 		}
   1639 		sc->sc_busy = 1;
   1640 		sc->sc_cansleep = 0;
   1641 		return 0;
   1642 	}
   1643 	while (sc->sc_busy) {
   1644 		if (!device_is_active(sc->sc_dev))
   1645 			return ENXIO;
   1646 		sc->sc_sleep_cnt++;
   1647 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
   1648 		sc->sc_sleep_cnt--;
   1649 		if (error)
   1650 			return error;
   1651 	}
   1652 	sc->sc_busy = 1;
   1653 	sc->sc_cansleep = 1;
   1654 	return 0;
   1655 }
   1656 
   1657 static void
   1658 awi_unlock(struct awi_softc *sc)
   1659 {
   1660 	sc->sc_busy = 0;
   1661 	sc->sc_cansleep = 0;
   1662 	if (sc->sc_sleep_cnt)
   1663 		wakeup(sc);
   1664 }
   1665 
   1666 static int
   1667 awi_intr_lock(struct awi_softc *sc)
   1668 {
   1669 	uint8_t status;
   1670 	int i, retry;
   1671 
   1672 	status = 1;
   1673 	for (retry = 0; retry < 10; retry++) {
   1674 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
   1675 			if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
   1676 				break;
   1677 			DELAY(5);
   1678 		}
   1679 		if (status != 0)
   1680 			break;
   1681 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
   1682 		if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
   1683 			break;
   1684 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   1685 	}
   1686 	if (status != 0) {
   1687 		printf("%s: failed to lock interrupt\n",
   1688 		    sc->sc_if.if_xname);
   1689 		return ENXIO;
   1690 	}
   1691 	return 0;
   1692 }
   1693 
   1694 static void
   1695 awi_intr_unlock(struct awi_softc *sc)
   1696 {
   1697 
   1698 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   1699 }
   1700 
   1701 static int
   1702 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   1703 {
   1704 	struct ifnet *ifp = ic->ic_ifp;
   1705 	struct awi_softc *sc = ifp->if_softc;
   1706 	struct ieee80211_node *ni;
   1707 	int error;
   1708 	uint8_t newmode;
   1709 	enum ieee80211_state ostate;
   1710 #ifdef AWI_DEBUG
   1711 	static const char *stname[] =
   1712 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   1713 	static const char *substname[] =
   1714 	    { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
   1715 	      "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
   1716 #endif /* AWI_DEBUG */
   1717 
   1718 	ostate = ic->ic_state;
   1719 	DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
   1720 	    stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
   1721 
   1722 	/* Set LED */
   1723 	switch (nstate) {
   1724 	case IEEE80211_S_INIT:
   1725 		awi_drvstate(sc, AWI_DRV_RESET);
   1726 		break;
   1727 	case IEEE80211_S_SCAN:
   1728 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1729 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
   1730 			awi_drvstate(sc, AWI_DRV_ADHSC);
   1731 		else
   1732 			awi_drvstate(sc, AWI_DRV_INFSY);
   1733 		break;
   1734 	case IEEE80211_S_AUTH:
   1735 		awi_drvstate(sc, AWI_DRV_INFSY);
   1736 		break;
   1737 	case IEEE80211_S_ASSOC:
   1738 		awi_drvstate(sc, AWI_DRV_INFAUTH);
   1739 		break;
   1740 	case IEEE80211_S_RUN:
   1741 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1742 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
   1743 			awi_drvstate(sc, AWI_DRV_ADHSY);
   1744 		else
   1745 			awi_drvstate(sc, AWI_DRV_INFASSOC);
   1746 		break;
   1747 	}
   1748 
   1749 	if (nstate == IEEE80211_S_INIT) {
   1750 		sc->sc_substate = AWI_ST_NONE;
   1751 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   1752 		return (*sc->sc_newstate)(ic, nstate, arg);
   1753 	}
   1754 
   1755 	/* State transition */
   1756 	if (nstate == IEEE80211_S_SCAN) {
   1757 		/* SCAN substate */
   1758 		if (sc->sc_substate == AWI_ST_NONE) {
   1759 			sc->sc_nstate = nstate;	/* next state in transition */
   1760 			sc->sc_substate = AWI_ST_SCAN_INIT;
   1761 		}
   1762 		switch (sc->sc_substate) {
   1763 		case AWI_ST_SCAN_INIT:
   1764 			sc->sc_substate = AWI_ST_SCAN_SETMIB;
   1765 			switch (ostate) {
   1766 			case IEEE80211_S_RUN:
   1767 				/* Beacon miss */
   1768 				if (ifp->if_flags & IFF_DEBUG)
   1769 					printf("%s: no recent beacons from %s;"
   1770 					    " rescanning\n",
   1771 					    ifp->if_xname,
   1772 					    ether_sprintf(ic->ic_bss->ni_bssid));
   1773 				/* FALLTHRU */
   1774 			case IEEE80211_S_AUTH:
   1775 			case IEEE80211_S_ASSOC:
   1776 			case IEEE80211_S_INIT:
   1777 				ieee80211_begin_scan(ic, 1);
   1778 				/* FALLTHRU */
   1779 			case IEEE80211_S_SCAN:
   1780 				/* Scan next */
   1781 				break;
   1782 			}
   1783 			if (ic->ic_flags & IEEE80211_F_ASCAN)
   1784 				newmode = AWI_SCAN_ACTIVE;
   1785 			else
   1786 				newmode = AWI_SCAN_PASSIVE;
   1787 			if (sc->sc_mib_mgt.aScan_Mode != newmode) {
   1788 				sc->sc_mib_mgt.aScan_Mode = newmode;
   1789 				if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
   1790 				    AWI_MIB_MGT, AWI_NOWAIT)) != 0)
   1791 					break;
   1792 			}
   1793 			/* FALLTHRU */
   1794 		case AWI_ST_SCAN_SETMIB:
   1795 			sc->sc_substate = AWI_ST_SCAN_SCCMD;
   1796 			if (sc->sc_cmd_inprog) {
   1797 				if ((error = awi_cmd_wait(sc)) != 0)
   1798 					break;
   1799 			}
   1800 			sc->sc_cmd_inprog = AWI_CMD_SCAN;
   1801 			ni = ic->ic_bss;
   1802 			awi_write_2(sc, AWI_CA_SCAN_DURATION,
   1803 			    (ic->ic_flags & IEEE80211_F_ASCAN) ?
   1804 			    AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
   1805 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1806 				awi_write_1(sc, AWI_CA_SCAN_SET,
   1807 				    IEEE80211_FH_CHANSET(
   1808 					ieee80211_chan2ieee(ic, ni->ni_chan)));
   1809 				awi_write_1(sc, AWI_CA_SCAN_PATTERN,
   1810 				    IEEE80211_FH_CHANPAT(
   1811 					ieee80211_chan2ieee(ic, ni->ni_chan)));
   1812 				awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
   1813 			} else {
   1814 				awi_write_1(sc, AWI_CA_SCAN_SET,
   1815 				    ieee80211_chan2ieee(ic, ni->ni_chan));
   1816 				awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
   1817 				awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
   1818 			}
   1819 			awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
   1820 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   1821 			if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
   1822 			    != 0)
   1823 				break;
   1824 			/* FALLTHRU */
   1825 		case AWI_ST_SCAN_SCCMD:
   1826 			ic->ic_state = nstate;
   1827 			sc->sc_substate = AWI_ST_NONE;
   1828 			error = EINPROGRESS;
   1829 			break;
   1830 		default:
   1831 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
   1832 			    stname[nstate], substname[sc->sc_substate]));
   1833 			sc->sc_substate = AWI_ST_NONE;
   1834 			error = EIO;
   1835 			break;
   1836 		}
   1837 		goto out;
   1838 	}
   1839 
   1840 	if (ostate == IEEE80211_S_SCAN) {
   1841 		/* Set SSID and channel */
   1842 		/* substate */
   1843 		if (sc->sc_substate == AWI_ST_NONE) {
   1844 			sc->sc_nstate = nstate;	/* Next state in transition */
   1845 			sc->sc_substate = AWI_ST_SUB_INIT;
   1846 		}
   1847 		ni = ic->ic_bss;
   1848 		switch (sc->sc_substate) {
   1849 		case AWI_ST_SUB_INIT:
   1850 			sc->sc_substate = AWI_ST_SUB_SETSS;
   1851 			IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
   1852 			    ni->ni_bssid);
   1853 			memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
   1854 			    AWI_ESS_ID_SIZE);
   1855 			sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
   1856 			    IEEE80211_ELEMID_SSID;
   1857 			sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
   1858 			memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
   1859 			    ni->ni_essid, ni->ni_esslen);
   1860 			LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
   1861 			    ni->ni_intval);
   1862 			if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
   1863 			    AWI_NOWAIT)) != 0)
   1864 				break;
   1865 			/* FALLTHRU */
   1866 		case AWI_ST_SUB_SETSS:
   1867 			sc->sc_substate = AWI_ST_SUB_SYNC;
   1868 			if (sc->sc_cmd_inprog) {
   1869 				if ((error = awi_cmd_wait(sc)) != 0)
   1870 					break;
   1871 			}
   1872 			sc->sc_cmd_inprog = AWI_CMD_SYNC;
   1873 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1874 				awi_write_1(sc, AWI_CA_SYNC_SET,
   1875 				    IEEE80211_FH_CHANSET(
   1876 					ieee80211_chan2ieee(ic, ni->ni_chan)));
   1877 				awi_write_1(sc, AWI_CA_SYNC_PATTERN,
   1878 				    IEEE80211_FH_CHANPAT(
   1879 					ieee80211_chan2ieee(ic, ni->ni_chan)));
   1880 				awi_write_1(sc, AWI_CA_SYNC_IDX,
   1881 				    ni->ni_fhindex);
   1882 				awi_write_2(sc, AWI_CA_SYNC_DWELL,
   1883 				    ni->ni_fhdwell);
   1884 			} else {
   1885 				awi_write_1(sc, AWI_CA_SYNC_SET,
   1886 				    ieee80211_chan2ieee(ic, ni->ni_chan));
   1887 				awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
   1888 				awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
   1889 				awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
   1890 			}
   1891 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
   1892 				memset(&ni->ni_tstamp, 0,
   1893 				    sizeof(ni->ni_tstamp));
   1894 				ni->ni_rstamp = 0;
   1895 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
   1896 			} else
   1897 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
   1898 			awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
   1899 			awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
   1900 			    ni->ni_tstamp.data, sizeof(ni->ni_tstamp.data));
   1901 			awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
   1902 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   1903 			if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
   1904 			    != 0)
   1905 				break;
   1906 			/* FALLTHRU */
   1907 		case AWI_ST_SUB_SYNC:
   1908 			sc->sc_substate = AWI_ST_NONE;
   1909 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
   1910 				if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
   1911 				    AWI_MIB_MGT, AWI_WAIT)) != 0)
   1912 					break;
   1913 				IEEE80211_ADDR_COPY(ni->ni_bssid,
   1914 				    &sc->sc_mib_mgt.aCurrent_BSS_ID);
   1915 			} else {
   1916 				if (nstate == IEEE80211_S_RUN) {
   1917 					sc->sc_rx_timer = 10;
   1918 					ifp->if_timer = 1;
   1919 				}
   1920 			}
   1921 			error = 0;
   1922 			break;
   1923 		default:
   1924 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
   1925 			    stname[nstate], substname[sc->sc_substate]));
   1926 			sc->sc_substate = AWI_ST_NONE;
   1927 			error = EIO;
   1928 			break;
   1929 		}
   1930 		goto out;
   1931 	}
   1932 
   1933 	sc->sc_substate = AWI_ST_NONE;
   1934 
   1935 	return (*sc->sc_newstate)(ic, nstate, arg);
   1936 out:
   1937 	if (error != 0) {
   1938 		if (error == EINPROGRESS)
   1939 			error = 0;
   1940 		return error;
   1941 	}
   1942 	return (*sc->sc_newstate)(ic, nstate, arg);
   1943 }
   1944 
   1945 static void
   1946 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
   1947 	struct ieee80211_node *ni,
   1948 	int subtype, int rssi, uint32_t rstamp)
   1949 {
   1950 	struct awi_softc *sc = ic->ic_ifp->if_softc;
   1951 
   1952 	/* probe request is handled by hardware */
   1953 	if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
   1954 		return;
   1955 	(*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
   1956 }
   1957 
   1958 static int
   1959 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
   1960 	int type, int arg)
   1961 {
   1962 	struct awi_softc *sc = ic->ic_ifp->if_softc;
   1963 
   1964 	/* Probe request is handled by hardware */
   1965 	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
   1966 		return 0;
   1967 	return (*sc->sc_send_mgmt)(ic, ni, type, arg);
   1968 }
   1969 
   1970 static struct mbuf *
   1971 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
   1972 {
   1973 	struct ieee80211com *ic = &sc->sc_ic;
   1974 	struct ieee80211_node *ni = ic->ic_bss;
   1975 	struct ether_header *eh;
   1976 	struct ieee80211_frame *wh;
   1977 
   1978 	if (m->m_len < sizeof(struct ether_header)) {
   1979 		m = m_pullup(m, sizeof(struct ether_header));
   1980 		if (m == NULL)
   1981 			return NULL;
   1982 	}
   1983 	eh = mtod(m, struct ether_header *);
   1984 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
   1985 	if (m == NULL)
   1986 		return NULL;
   1987 	wh = mtod(m, struct ieee80211_frame *);
   1988 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
   1989 	*(uint16_t *)wh->i_dur = 0;
   1990 	*(uint16_t *)wh->i_seq =
   1991 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
   1992 	ni->ni_txseqs[0]++;
   1993 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1994 	    ic->ic_opmode == IEEE80211_M_AHDEMO) {
   1995 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1996 		if (sc->sc_adhoc_ap)
   1997 			IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
   1998 		else
   1999 			IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
   2000 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
   2001 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
   2002 	} else {
   2003 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
   2004 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
   2005 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
   2006 		IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
   2007 	}
   2008 	return m;
   2009 }
   2010 
   2011 static struct mbuf *
   2012 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
   2013 {
   2014 	struct ieee80211com *ic = &sc->sc_ic;
   2015 	struct ether_header eh;
   2016 	struct ieee80211_frame wh;
   2017 	struct llc *llc;
   2018 
   2019 	if (m->m_len < sizeof(wh) + sizeof(eh)) {
   2020 		m = m_pullup(m, sizeof(wh) + sizeof(eh));
   2021 		if (m == NULL)
   2022 			return NULL;
   2023 	}
   2024 	memcpy(&wh, mtod(m, void *), sizeof(wh));
   2025 	if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
   2026 		return m;
   2027 	memcpy(&eh, mtod(m, char *) + sizeof(wh), sizeof(eh));
   2028 	m_adj(m, sizeof(eh) - sizeof(*llc));
   2029 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
   2030 	    ic->ic_opmode == IEEE80211_M_AHDEMO)
   2031 		IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
   2032 	memcpy(mtod(m, void *), &wh, sizeof(wh));
   2033 	llc = (struct llc *)(mtod(m, char *) + sizeof(wh));
   2034 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
   2035 	llc->llc_control = LLC_UI;
   2036 	llc->llc_snap.org_code[0] = 0;
   2037 	llc->llc_snap.org_code[1] = 0;
   2038 	llc->llc_snap.org_code[2] = 0;
   2039 	llc->llc_snap.ether_type = eh.ether_type;
   2040 	return m;
   2041 }
   2042