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