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