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