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