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