Home | History | Annotate | Line # | Download | only in usb
if_otus.c revision 1.1
      1  1.1  christos /*	$OpenBSD: if_otus.c,v 1.18 2010/08/27 17:08:00 jsg Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
      5  1.1  christos  *
      6  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      7  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      8  1.1  christos  * copyright notice and this permission notice appear in all copies.
      9  1.1  christos  *
     10  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.1  christos /*-
     20  1.1  christos  * Driver for Atheros AR9001U chipset.
     21  1.1  christos  * http://www.atheros.com/pt/bulletins/AR9001USBBulletin.pdf
     22  1.1  christos  */
     23  1.1  christos 
     24  1.1  christos #include "bpfilter.h"
     25  1.1  christos 
     26  1.1  christos #include <sys/param.h>
     27  1.1  christos #include <sys/sockio.h>
     28  1.1  christos #include <sys/mbuf.h>
     29  1.1  christos #include <sys/kernel.h>
     30  1.1  christos #include <sys/socket.h>
     31  1.1  christos #include <sys/systm.h>
     32  1.1  christos #include <sys/timeout.h>
     33  1.1  christos #include <sys/conf.h>
     34  1.1  christos #include <sys/device.h>
     35  1.1  christos 
     36  1.1  christos #include <machine/bus.h>
     37  1.1  christos #include <machine/endian.h>
     38  1.1  christos #include <machine/intr.h>
     39  1.1  christos 
     40  1.1  christos #if NBPFILTER > 0
     41  1.1  christos #include <net/bpf.h>
     42  1.1  christos #endif
     43  1.1  christos #include <net/if.h>
     44  1.1  christos #include <net/if_arp.h>
     45  1.1  christos #include <net/if_dl.h>
     46  1.1  christos #include <net/if_media.h>
     47  1.1  christos #include <net/if_types.h>
     48  1.1  christos 
     49  1.1  christos #include <netinet/in.h>
     50  1.1  christos #include <netinet/in_systm.h>
     51  1.1  christos #include <netinet/in_var.h>
     52  1.1  christos #include <netinet/if_ether.h>
     53  1.1  christos #include <netinet/ip.h>
     54  1.1  christos 
     55  1.1  christos #include <net80211/ieee80211_var.h>
     56  1.1  christos #include <net80211/ieee80211_amrr.h>
     57  1.1  christos #include <net80211/ieee80211_radiotap.h>
     58  1.1  christos 
     59  1.1  christos #include <dev/usb/usb.h>
     60  1.1  christos #include <dev/usb/usbdi.h>
     61  1.1  christos #include <dev/usb/usbdi_util.h>
     62  1.1  christos #include <dev/usb/usbdevs.h>
     63  1.1  christos 
     64  1.1  christos #include <dev/usb/if_otusreg.h>
     65  1.1  christos 
     66  1.1  christos #ifdef USB_DEBUG
     67  1.1  christos #define OTUS_DEBUG
     68  1.1  christos #endif
     69  1.1  christos 
     70  1.1  christos #ifdef OTUS_DEBUG
     71  1.1  christos #define DPRINTF(x)	do { if (otus_debug) printf x; } while (0)
     72  1.1  christos #define DPRINTFN(n, x)	do { if (otus_debug >= (n)) printf x; } while (0)
     73  1.1  christos int otus_debug = 1;
     74  1.1  christos #else
     75  1.1  christos #define DPRINTF(x)
     76  1.1  christos #define DPRINTFN(n, x)
     77  1.1  christos #endif
     78  1.1  christos 
     79  1.1  christos static const struct usb_devno otus_devs[] = {
     80  1.1  christos 	{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_WN7512 },
     81  1.1  christos 	{ USB_VENDOR_ATHEROS2,		USB_PRODUCT_ATHEROS2_3CRUSBN275 },
     82  1.1  christos 	{ USB_VENDOR_ATHEROS2,		USB_PRODUCT_ATHEROS2_TG121N },
     83  1.1  christos 	{ USB_VENDOR_ATHEROS2,		USB_PRODUCT_ATHEROS2_AR9170 },
     84  1.1  christos 	{ USB_VENDOR_ATHEROS2,		USB_PRODUCT_ATHEROS2_WN612 },
     85  1.1  christos 	{ USB_VENDOR_ATHEROS2,		USB_PRODUCT_ATHEROS2_WN821NV2 },
     86  1.1  christos 	{ USB_VENDOR_AVM,		USB_PRODUCT_AVM_FRITZWLAN },
     87  1.1  christos 	{ USB_VENDOR_CACE,		USB_PRODUCT_CACE_AIRPCAPNX },
     88  1.1  christos 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA130D1 },
     89  1.1  christos 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA160A1 },
     90  1.1  christos 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA160A2 },
     91  1.1  christos 	{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_WNGDNUS2 },
     92  1.1  christos 	{ USB_VENDOR_NEC,		USB_PRODUCT_NEC_WL300NUG },
     93  1.1  christos 	{ USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_WN111V2 },
     94  1.1  christos 	{ USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_WNA1000 },
     95  1.1  christos 	{ USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_WNDA3100 },
     96  1.1  christos 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GW_US300 },
     97  1.1  christos 	{ USB_VENDOR_WISTRONNEWEB,	USB_PRODUCT_WISTRONNEWEB_O8494 },
     98  1.1  christos 	{ USB_VENDOR_WISTRONNEWEB,	USB_PRODUCT_WISTRONNEWEB_WNC0600 },
     99  1.1  christos 	{ USB_VENDOR_ZCOM,		USB_PRODUCT_ZCOM_UB81 },
    100  1.1  christos 	{ USB_VENDOR_ZCOM,		USB_PRODUCT_ZCOM_UB82 },
    101  1.1  christos 	{ USB_VENDOR_ZYDAS,		USB_PRODUCT_ZYDAS_ZD1221 },
    102  1.1  christos 	{ USB_VENDOR_ZYXEL,		USB_PRODUCT_ZYXEL_NWD271N }
    103  1.1  christos };
    104  1.1  christos 
    105  1.1  christos int		otus_match(struct device *, void *, void *);
    106  1.1  christos void		otus_attach(struct device *, struct device *, void *);
    107  1.1  christos int		otus_detach(struct device *, int);
    108  1.1  christos void		otus_attachhook(void *);
    109  1.1  christos void		otus_get_chanlist(struct otus_softc *);
    110  1.1  christos int		otus_load_firmware(struct otus_softc *, const char *,
    111  1.1  christos 		    uint32_t);
    112  1.1  christos int		otus_open_pipes(struct otus_softc *);
    113  1.1  christos void		otus_close_pipes(struct otus_softc *);
    114  1.1  christos int		otus_alloc_tx_cmd(struct otus_softc *);
    115  1.1  christos void		otus_free_tx_cmd(struct otus_softc *);
    116  1.1  christos int		otus_alloc_tx_data_list(struct otus_softc *);
    117  1.1  christos void		otus_free_tx_data_list(struct otus_softc *);
    118  1.1  christos int		otus_alloc_rx_data_list(struct otus_softc *);
    119  1.1  christos void		otus_free_rx_data_list(struct otus_softc *);
    120  1.1  christos void		otus_next_scan(void *);
    121  1.1  christos void		otus_task(void *);
    122  1.1  christos void		otus_do_async(struct otus_softc *,
    123  1.1  christos 		    void (*)(struct otus_softc *, void *), void *, int);
    124  1.1  christos int		otus_newstate(struct ieee80211com *, enum ieee80211_state,
    125  1.1  christos 		    int);
    126  1.1  christos void		otus_newstate_cb(struct otus_softc *, void *);
    127  1.1  christos int		otus_cmd(struct otus_softc *, uint8_t, const void *, int,
    128  1.1  christos 		    void *);
    129  1.1  christos void		otus_write(struct otus_softc *, uint32_t, uint32_t);
    130  1.1  christos int		otus_write_barrier(struct otus_softc *);
    131  1.1  christos struct		ieee80211_node *otus_node_alloc(struct ieee80211com *);
    132  1.1  christos int		otus_media_change(struct ifnet *);
    133  1.1  christos int		otus_read_eeprom(struct otus_softc *);
    134  1.1  christos void		otus_newassoc(struct ieee80211com *, struct ieee80211_node *,
    135  1.1  christos 		    int);
    136  1.1  christos void		otus_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    137  1.1  christos void		otus_cmd_rxeof(struct otus_softc *, uint8_t *, int);
    138  1.1  christos void		otus_sub_rxeof(struct otus_softc *, uint8_t *, int);
    139  1.1  christos void		otus_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    140  1.1  christos void		otus_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    141  1.1  christos int		otus_tx(struct otus_softc *, struct mbuf *,
    142  1.1  christos 		    struct ieee80211_node *);
    143  1.1  christos void		otus_start(struct ifnet *);
    144  1.1  christos void		otus_watchdog(struct ifnet *);
    145  1.1  christos int		otus_ioctl(struct ifnet *, u_long, caddr_t);
    146  1.1  christos int		otus_set_multi(struct otus_softc *);
    147  1.1  christos void		otus_updateedca(struct ieee80211com *);
    148  1.1  christos void		otus_updateedca_cb(struct otus_softc *, void *);
    149  1.1  christos void		otus_updateslot(struct ieee80211com *);
    150  1.1  christos void		otus_updateslot_cb(struct otus_softc *, void *);
    151  1.1  christos int		otus_init_mac(struct otus_softc *);
    152  1.1  christos uint32_t	otus_phy_get_def(struct otus_softc *, uint32_t);
    153  1.1  christos int		otus_set_board_values(struct otus_softc *,
    154  1.1  christos 		    struct ieee80211_channel *);
    155  1.1  christos int		otus_program_phy(struct otus_softc *,
    156  1.1  christos 		    struct ieee80211_channel *);
    157  1.1  christos int		otus_set_rf_bank4(struct otus_softc *,
    158  1.1  christos 		    struct ieee80211_channel *);
    159  1.1  christos void		otus_get_delta_slope(uint32_t, uint32_t *, uint32_t *);
    160  1.1  christos int		otus_set_chan(struct otus_softc *, struct ieee80211_channel *,
    161  1.1  christos 		    int);
    162  1.1  christos int		otus_set_key(struct ieee80211com *, struct ieee80211_node *,
    163  1.1  christos 		    struct ieee80211_key *);
    164  1.1  christos void		otus_set_key_cb(struct otus_softc *, void *);
    165  1.1  christos void		otus_delete_key(struct ieee80211com *, struct ieee80211_node *,
    166  1.1  christos 		    struct ieee80211_key *);
    167  1.1  christos void		otus_delete_key_cb(struct otus_softc *, void *);
    168  1.1  christos void		otus_calibrate_to(void *);
    169  1.1  christos int		otus_set_bssid(struct otus_softc *, const uint8_t *);
    170  1.1  christos int		otus_set_macaddr(struct otus_softc *, const uint8_t *);
    171  1.1  christos void		otus_led_newstate_type1(struct otus_softc *);
    172  1.1  christos void		otus_led_newstate_type2(struct otus_softc *);
    173  1.1  christos void		otus_led_newstate_type3(struct otus_softc *);
    174  1.1  christos int		otus_init(struct ifnet *);
    175  1.1  christos void		otus_stop(struct ifnet *);
    176  1.1  christos 
    177  1.1  christos struct cfdriver otus_cd = {
    178  1.1  christos 	NULL, "otus", DV_IFNET
    179  1.1  christos };
    180  1.1  christos 
    181  1.1  christos const struct cfattach otus_ca = {
    182  1.1  christos 	sizeof (struct otus_softc), otus_match, otus_attach, otus_detach
    183  1.1  christos };
    184  1.1  christos 
    185  1.1  christos int
    186  1.1  christos otus_match(struct device *parent, void *match, void *aux)
    187  1.1  christos {
    188  1.1  christos 	struct usb_attach_arg *uaa = aux;
    189  1.1  christos 
    190  1.1  christos 	if (uaa->iface != NULL)
    191  1.1  christos 		return UMATCH_NONE;
    192  1.1  christos 
    193  1.1  christos 	return (usb_lookup(otus_devs, uaa->vendor, uaa->product) != NULL) ?
    194  1.1  christos 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    195  1.1  christos }
    196  1.1  christos 
    197  1.1  christos void
    198  1.1  christos otus_attach(struct device *parent, struct device *self, void *aux)
    199  1.1  christos {
    200  1.1  christos 	struct otus_softc *sc = (struct otus_softc *)self;
    201  1.1  christos 	struct usb_attach_arg *uaa = aux;
    202  1.1  christos 	int error;
    203  1.1  christos 
    204  1.1  christos 	sc->sc_udev = uaa->device;
    205  1.1  christos 
    206  1.1  christos 	usb_init_task(&sc->sc_task, otus_task, sc);
    207  1.1  christos 	timeout_set(&sc->scan_to, otus_next_scan, sc);
    208  1.1  christos 	timeout_set(&sc->calib_to, otus_calibrate_to, sc);
    209  1.1  christos 
    210  1.1  christos 	sc->amrr.amrr_min_success_threshold =  1;
    211  1.1  christos 	sc->amrr.amrr_max_success_threshold = 10;
    212  1.1  christos 
    213  1.1  christos 	if (usbd_set_config_no(sc->sc_udev, 1, 0) != 0) {
    214  1.1  christos 		printf("%s: could not set configuration no\n",
    215  1.1  christos 		    sc->sc_dev.dv_xname);
    216  1.1  christos 		return;
    217  1.1  christos 	}
    218  1.1  christos 
    219  1.1  christos 	/* Get the first interface handle. */
    220  1.1  christos 	error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
    221  1.1  christos 	if (error != 0) {
    222  1.1  christos 		printf("%s: could not get interface handle\n",
    223  1.1  christos 		    sc->sc_dev.dv_xname);
    224  1.1  christos 		return;
    225  1.1  christos 	}
    226  1.1  christos 
    227  1.1  christos 	if ((error = otus_open_pipes(sc)) != 0) {
    228  1.1  christos 		printf("%s: could not open pipes\n", sc->sc_dev.dv_xname);
    229  1.1  christos 		return;
    230  1.1  christos 	}
    231  1.1  christos 
    232  1.1  christos 	if (rootvp == NULL)
    233  1.1  christos 		mountroothook_establish(otus_attachhook, sc);
    234  1.1  christos 	else
    235  1.1  christos 		otus_attachhook(sc);
    236  1.1  christos 
    237  1.1  christos 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, &sc->sc_dev);
    238  1.1  christos }
    239  1.1  christos 
    240  1.1  christos int
    241  1.1  christos otus_detach(struct device *self, int flags)
    242  1.1  christos {
    243  1.1  christos 	struct otus_softc *sc = (struct otus_softc *)self;
    244  1.1  christos 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    245  1.1  christos 	int s;
    246  1.1  christos 
    247  1.1  christos 	s = splnet();
    248  1.1  christos 
    249  1.1  christos 	/* Wait for all queued asynchronous commands to complete. */
    250  1.1  christos 	while (sc->cmdq.queued > 0)
    251  1.1  christos 		tsleep(&sc->cmdq, 0, "cmdq", 0);
    252  1.1  christos 
    253  1.1  christos 	timeout_del(&sc->scan_to);
    254  1.1  christos 	timeout_del(&sc->calib_to);
    255  1.1  christos 
    256  1.1  christos 	if (ifp->if_flags != 0) {	/* if_attach() has been called. */
    257  1.1  christos 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    258  1.1  christos 		ieee80211_ifdetach(ifp);
    259  1.1  christos 		if_detach(ifp);
    260  1.1  christos 	}
    261  1.1  christos 
    262  1.1  christos 	otus_close_pipes(sc);
    263  1.1  christos 
    264  1.1  christos 	splx(s);
    265  1.1  christos 
    266  1.1  christos 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev);
    267  1.1  christos 
    268  1.1  christos 	return 0;
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos void
    272  1.1  christos otus_attachhook(void *xsc)
    273  1.1  christos {
    274  1.1  christos 	struct otus_softc *sc = xsc;
    275  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    276  1.1  christos 	struct ifnet *ifp = &ic->ic_if;
    277  1.1  christos 	usb_device_request_t req;
    278  1.1  christos 	uint32_t in, out;
    279  1.1  christos 	int error;
    280  1.1  christos 
    281  1.1  christos 	error = otus_load_firmware(sc, "otus-init", AR_FW_INIT_ADDR);
    282  1.1  christos 	if (error != 0) {
    283  1.1  christos 		printf("%s: could not load %s firmware\n",
    284  1.1  christos 		    sc->sc_dev.dv_xname, "init");
    285  1.1  christos 		return;
    286  1.1  christos 	}
    287  1.1  christos 
    288  1.1  christos 	usbd_delay_ms(sc->sc_udev, 1000);
    289  1.1  christos 
    290  1.1  christos 	error = otus_load_firmware(sc, "otus-main", AR_FW_MAIN_ADDR);
    291  1.1  christos 	if (error != 0) {
    292  1.1  christos 		printf("%s: could not load %s firmware\n",
    293  1.1  christos 		    sc->sc_dev.dv_xname, "main");
    294  1.1  christos 		return;
    295  1.1  christos 	}
    296  1.1  christos 
    297  1.1  christos 	/* Tell device that firmware transfer is complete. */
    298  1.1  christos 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    299  1.1  christos 	req.bRequest = AR_FW_DOWNLOAD_COMPLETE;
    300  1.1  christos 	USETW(req.wValue, 0);
    301  1.1  christos 	USETW(req.wIndex, 0);
    302  1.1  christos 	USETW(req.wLength, 0);
    303  1.1  christos 	if (usbd_do_request(sc->sc_udev, &req, NULL) != 0) {
    304  1.1  christos 		printf("%s: firmware initialization failed\n",
    305  1.1  christos 		    sc->sc_dev.dv_xname);
    306  1.1  christos 		return;
    307  1.1  christos 	}
    308  1.1  christos 
    309  1.1  christos 	/* Send an ECHO command to check that everything is settled. */
    310  1.1  christos 	in = 0xbadc0ffe;
    311  1.1  christos 	if (otus_cmd(sc, AR_CMD_ECHO, &in, sizeof in, &out) != 0) {
    312  1.1  christos 		printf("%s: echo command failed\n", sc->sc_dev.dv_xname);
    313  1.1  christos 		return;
    314  1.1  christos 	}
    315  1.1  christos 	if (in != out) {
    316  1.1  christos 		printf("%s: echo reply mismatch: 0x%08x!=0x%08x\n",
    317  1.1  christos 		    sc->sc_dev.dv_xname, in, out);
    318  1.1  christos 		return;
    319  1.1  christos 	}
    320  1.1  christos 
    321  1.1  christos 	/* Read entire EEPROM. */
    322  1.1  christos 	if (otus_read_eeprom(sc) != 0) {
    323  1.1  christos 		printf("%s: could not read EEPROM\n", sc->sc_dev.dv_xname);
    324  1.1  christos 		return;
    325  1.1  christos 	}
    326  1.1  christos 
    327  1.1  christos 	sc->txmask = sc->eeprom.baseEepHeader.txMask;
    328  1.1  christos 	sc->rxmask = sc->eeprom.baseEepHeader.rxMask;
    329  1.1  christos 	sc->capflags = sc->eeprom.baseEepHeader.opCapFlags;
    330  1.1  christos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->eeprom.baseEepHeader.macAddr);
    331  1.1  christos 	sc->sc_led_newstate = otus_led_newstate_type3;	/* XXX */
    332  1.1  christos 
    333  1.1  christos 	printf("%s: MAC/BBP AR9170, RF AR%X, MIMO %dT%dR, address %s\n",
    334  1.1  christos 	    sc->sc_dev.dv_xname, (sc->capflags & AR5416_OPFLAGS_11A) ?
    335  1.1  christos 	        0x9104 : ((sc->txmask == 0x5) ? 0x9102 : 0x9101),
    336  1.1  christos 	    (sc->txmask == 0x5) ? 2 : 1, (sc->rxmask == 0x5) ? 2 : 1,
    337  1.1  christos 	    ether_sprintf(ic->ic_myaddr));
    338  1.1  christos 
    339  1.1  christos 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
    340  1.1  christos 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
    341  1.1  christos 	ic->ic_state = IEEE80211_S_INIT;
    342  1.1  christos 
    343  1.1  christos 	/* Set device capabilities. */
    344  1.1  christos 	ic->ic_caps =
    345  1.1  christos 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    346  1.1  christos 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    347  1.1  christos 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
    348  1.1  christos 	    IEEE80211_C_WEP |		/* WEP */
    349  1.1  christos 	    IEEE80211_C_RSN;		/* WPA/RSN */
    350  1.1  christos 
    351  1.1  christos 	if (sc->eeprom.baseEepHeader.opCapFlags & AR5416_OPFLAGS_11G) {
    352  1.1  christos 		/* Set supported .11b and .11g rates. */
    353  1.1  christos 		ic->ic_sup_rates[IEEE80211_MODE_11B] =
    354  1.1  christos 		    ieee80211_std_rateset_11b;
    355  1.1  christos 		ic->ic_sup_rates[IEEE80211_MODE_11G] =
    356  1.1  christos 		    ieee80211_std_rateset_11g;
    357  1.1  christos 	}
    358  1.1  christos 	if (sc->eeprom.baseEepHeader.opCapFlags & AR5416_OPFLAGS_11A) {
    359  1.1  christos 		/* Set supported .11a rates. */
    360  1.1  christos 		ic->ic_sup_rates[IEEE80211_MODE_11A] =
    361  1.1  christos 		    ieee80211_std_rateset_11a;
    362  1.1  christos 	}
    363  1.1  christos 
    364  1.1  christos 	/* Build the list of supported channels. */
    365  1.1  christos 	otus_get_chanlist(sc);
    366  1.1  christos 
    367  1.1  christos 	ifp->if_softc = sc;
    368  1.1  christos 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    369  1.1  christos 	ifp->if_ioctl = otus_ioctl;
    370  1.1  christos 	ifp->if_start = otus_start;
    371  1.1  christos 	ifp->if_watchdog = otus_watchdog;
    372  1.1  christos 	IFQ_SET_READY(&ifp->if_snd);
    373  1.1  christos 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    374  1.1  christos 
    375  1.1  christos 	if_attach(ifp);
    376  1.1  christos 	ieee80211_ifattach(ifp);
    377  1.1  christos 	ic->ic_node_alloc = otus_node_alloc;
    378  1.1  christos 	ic->ic_newassoc = otus_newassoc;
    379  1.1  christos 	ic->ic_updateslot = otus_updateslot;
    380  1.1  christos 	ic->ic_updateedca = otus_updateedca;
    381  1.1  christos #ifdef notyet
    382  1.1  christos 	ic->ic_set_key = otus_set_key;
    383  1.1  christos 	ic->ic_delete_key = otus_delete_key;
    384  1.1  christos #endif
    385  1.1  christos 	/* Override state transition machine. */
    386  1.1  christos 	sc->sc_newstate = ic->ic_newstate;
    387  1.1  christos 	ic->ic_newstate = otus_newstate;
    388  1.1  christos 	ieee80211_media_init(ifp, otus_media_change, ieee80211_media_status);
    389  1.1  christos 
    390  1.1  christos #if NBPFILTER > 0
    391  1.1  christos 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
    392  1.1  christos 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
    393  1.1  christos 
    394  1.1  christos 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    395  1.1  christos 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    396  1.1  christos 	sc->sc_rxtap.wr_ihdr.it_present = htole32(OTUS_RX_RADIOTAP_PRESENT);
    397  1.1  christos 
    398  1.1  christos 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    399  1.1  christos 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    400  1.1  christos 	sc->sc_txtap.wt_ihdr.it_present = htole32(OTUS_TX_RADIOTAP_PRESENT);
    401  1.1  christos #endif
    402  1.1  christos }
    403  1.1  christos 
    404  1.1  christos void
    405  1.1  christos otus_get_chanlist(struct otus_softc *sc)
    406  1.1  christos {
    407  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    408  1.1  christos 	uint16_t domain;
    409  1.1  christos 	uint8_t chan;
    410  1.1  christos 	int i;
    411  1.1  christos 
    412  1.1  christos 	/* XXX regulatory domain. */
    413  1.1  christos 	domain = letoh16(sc->eeprom.baseEepHeader.regDmn[0]);
    414  1.1  christos 	DPRINTF(("regdomain=0x%04x\n", domain));
    415  1.1  christos 
    416  1.1  christos 	if (sc->eeprom.baseEepHeader.opCapFlags & AR5416_OPFLAGS_11G) {
    417  1.1  christos 		for (i = 0; i < 14; i++) {
    418  1.1  christos 			chan = ar_chans[i];
    419  1.1  christos 			ic->ic_channels[chan].ic_freq =
    420  1.1  christos 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
    421  1.1  christos 			ic->ic_channels[chan].ic_flags =
    422  1.1  christos 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    423  1.1  christos 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    424  1.1  christos 		}
    425  1.1  christos 	}
    426  1.1  christos 	if (sc->eeprom.baseEepHeader.opCapFlags & AR5416_OPFLAGS_11A) {
    427  1.1  christos 		for (i = 14; i < nitems(ar_chans); i++) {
    428  1.1  christos 			chan = ar_chans[i];
    429  1.1  christos 			ic->ic_channels[chan].ic_freq =
    430  1.1  christos 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
    431  1.1  christos 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
    432  1.1  christos 		}
    433  1.1  christos 	}
    434  1.1  christos }
    435  1.1  christos 
    436  1.1  christos int
    437  1.1  christos otus_load_firmware(struct otus_softc *sc, const char *name, uint32_t addr)
    438  1.1  christos {
    439  1.1  christos 	usb_device_request_t req;
    440  1.1  christos 	size_t size;
    441  1.1  christos 	u_char *fw, *ptr;
    442  1.1  christos 	int mlen, error;
    443  1.1  christos 
    444  1.1  christos 	/* Read firmware image from the filesystem. */
    445  1.1  christos 	if ((error = loadfirmware(name, &fw, &size)) != 0) {
    446  1.1  christos 		printf("%s: failed loadfirmware of file %s (error %d)\n",
    447  1.1  christos 		    sc->sc_dev.dv_xname, name, error);
    448  1.1  christos 		return error;
    449  1.1  christos 	}
    450  1.1  christos 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    451  1.1  christos 	req.bRequest = AR_FW_DOWNLOAD;
    452  1.1  christos 	USETW(req.wIndex, 0);
    453  1.1  christos 
    454  1.1  christos 	ptr = fw;
    455  1.1  christos 	addr >>= 8;
    456  1.1  christos 	while (size > 0) {
    457  1.1  christos 		mlen = MIN(size, 4096);
    458  1.1  christos 
    459  1.1  christos 		USETW(req.wValue, addr);
    460  1.1  christos 		USETW(req.wLength, mlen);
    461  1.1  christos 		if (usbd_do_request(sc->sc_udev, &req, ptr) != 0) {
    462  1.1  christos 			error = EIO;
    463  1.1  christos 			break;
    464  1.1  christos 		}
    465  1.1  christos 		addr += mlen >> 8;
    466  1.1  christos 		ptr  += mlen;
    467  1.1  christos 		size -= mlen;
    468  1.1  christos 	}
    469  1.1  christos 	free(fw, M_DEVBUF);
    470  1.1  christos 	return error;
    471  1.1  christos }
    472  1.1  christos 
    473  1.1  christos int
    474  1.1  christos otus_open_pipes(struct otus_softc *sc)
    475  1.1  christos {
    476  1.1  christos 	usb_endpoint_descriptor_t *ed;
    477  1.1  christos 	int i, isize, error;
    478  1.1  christos 
    479  1.1  christos 	error = usbd_open_pipe(sc->sc_iface, AR_EPT_BULK_RX_NO, 0,
    480  1.1  christos 	    &sc->data_rx_pipe);
    481  1.1  christos 	if (error != 0) {
    482  1.1  christos 		printf("%s: could not open Rx bulk pipe\n",
    483  1.1  christos 		    sc->sc_dev.dv_xname);
    484  1.1  christos 		goto fail;
    485  1.1  christos 	}
    486  1.1  christos 
    487  1.1  christos 	ed = usbd_get_endpoint_descriptor(sc->sc_iface, AR_EPT_INTR_RX_NO);
    488  1.1  christos 	if (ed == NULL) {
    489  1.1  christos 		printf("%s: could not retrieve Rx intr pipe descriptor\n",
    490  1.1  christos 		    sc->sc_dev.dv_xname);
    491  1.1  christos 		goto fail;
    492  1.1  christos 	}
    493  1.1  christos 	isize = UGETW(ed->wMaxPacketSize);
    494  1.1  christos 	if (isize == 0) {
    495  1.1  christos 		printf("%s: invalid Rx intr pipe descriptor\n",
    496  1.1  christos 		    sc->sc_dev.dv_xname);
    497  1.1  christos 		goto fail;
    498  1.1  christos 	}
    499  1.1  christos 	sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
    500  1.1  christos 	if (sc->ibuf == NULL) {
    501  1.1  christos 		printf("%s: could not allocate Rx intr buffer\n",
    502  1.1  christos 		    sc->sc_dev.dv_xname);
    503  1.1  christos 		goto fail;
    504  1.1  christos 	}
    505  1.1  christos 	error = usbd_open_pipe_intr(sc->sc_iface, AR_EPT_INTR_RX_NO,
    506  1.1  christos 	    USBD_SHORT_XFER_OK, &sc->cmd_rx_pipe, sc, sc->ibuf, isize,
    507  1.1  christos 	    otus_intr, USBD_DEFAULT_INTERVAL);
    508  1.1  christos 	if (error != 0) {
    509  1.1  christos 		printf("%s: could not open Rx intr pipe\n",
    510  1.1  christos 		    sc->sc_dev.dv_xname);
    511  1.1  christos 		goto fail;
    512  1.1  christos 	}
    513  1.1  christos 
    514  1.1  christos 	error = usbd_open_pipe(sc->sc_iface, AR_EPT_BULK_TX_NO, 0,
    515  1.1  christos 	    &sc->data_tx_pipe);
    516  1.1  christos 	if (error != 0) {
    517  1.1  christos 		printf("%s: could not open Tx bulk pipe\n",
    518  1.1  christos 		    sc->sc_dev.dv_xname);
    519  1.1  christos 		goto fail;
    520  1.1  christos 	}
    521  1.1  christos 
    522  1.1  christos 	error = usbd_open_pipe(sc->sc_iface, AR_EPT_INTR_TX_NO, 0,
    523  1.1  christos 	    &sc->cmd_tx_pipe);
    524  1.1  christos 	if (error != 0) {
    525  1.1  christos 		printf("%s: could not open Tx intr pipe\n",
    526  1.1  christos 		    sc->sc_dev.dv_xname);
    527  1.1  christos 		goto fail;
    528  1.1  christos 	}
    529  1.1  christos 
    530  1.1  christos 	if (otus_alloc_tx_cmd(sc) != 0) {
    531  1.1  christos 		printf("%s: could not allocate command xfer\n",
    532  1.1  christos 		    sc->sc_dev.dv_xname);
    533  1.1  christos 		goto fail;
    534  1.1  christos 	}
    535  1.1  christos 
    536  1.1  christos 	if (otus_alloc_tx_data_list(sc) != 0) {
    537  1.1  christos 		printf("%s: could not allocate Tx xfers\n",
    538  1.1  christos 		    sc->sc_dev.dv_xname);
    539  1.1  christos 		goto fail;
    540  1.1  christos 	}
    541  1.1  christos 
    542  1.1  christos 	if (otus_alloc_rx_data_list(sc) != 0) {
    543  1.1  christos 		printf("%s: could not allocate Rx xfers\n",
    544  1.1  christos 		    sc->sc_dev.dv_xname);
    545  1.1  christos 		goto fail;
    546  1.1  christos 	}
    547  1.1  christos 
    548  1.1  christos 	for (i = 0; i < OTUS_RX_DATA_LIST_COUNT; i++) {
    549  1.1  christos 		struct otus_rx_data *data = &sc->rx_data[i];
    550  1.1  christos 
    551  1.1  christos 		usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf,
    552  1.1  christos 		    OTUS_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    553  1.1  christos 		    USBD_NO_TIMEOUT, otus_rxeof);
    554  1.1  christos 		error = usbd_transfer(data->xfer);
    555  1.1  christos 		if (error != USBD_IN_PROGRESS && error != 0) {
    556  1.1  christos 			printf("%s: could not queue Rx xfer\n",
    557  1.1  christos 			    sc->sc_dev.dv_xname);
    558  1.1  christos 			goto fail;
    559  1.1  christos 		}
    560  1.1  christos 	}
    561  1.1  christos 	return 0;
    562  1.1  christos 
    563  1.1  christos  fail:	otus_close_pipes(sc);
    564  1.1  christos 	return error;
    565  1.1  christos }
    566  1.1  christos 
    567  1.1  christos void
    568  1.1  christos otus_close_pipes(struct otus_softc *sc)
    569  1.1  christos {
    570  1.1  christos 	otus_free_tx_cmd(sc);
    571  1.1  christos 	otus_free_tx_data_list(sc);
    572  1.1  christos 	otus_free_rx_data_list(sc);
    573  1.1  christos 
    574  1.1  christos 	if (sc->data_rx_pipe != NULL)
    575  1.1  christos 		usbd_close_pipe(sc->data_rx_pipe);
    576  1.1  christos 	if (sc->cmd_rx_pipe != NULL) {
    577  1.1  christos 		usbd_abort_pipe(sc->cmd_rx_pipe);
    578  1.1  christos 		usbd_close_pipe(sc->cmd_rx_pipe);
    579  1.1  christos 	}
    580  1.1  christos 	if (sc->ibuf != NULL)
    581  1.1  christos 		free(sc->ibuf, M_USBDEV);
    582  1.1  christos 	if (sc->data_tx_pipe != NULL)
    583  1.1  christos 		usbd_close_pipe(sc->data_tx_pipe);
    584  1.1  christos 	if (sc->cmd_tx_pipe != NULL)
    585  1.1  christos 		usbd_close_pipe(sc->cmd_tx_pipe);
    586  1.1  christos }
    587  1.1  christos 
    588  1.1  christos int
    589  1.1  christos otus_alloc_tx_cmd(struct otus_softc *sc)
    590  1.1  christos {
    591  1.1  christos 	struct otus_tx_cmd *cmd = &sc->tx_cmd;
    592  1.1  christos 
    593  1.1  christos 	cmd->xfer = usbd_alloc_xfer(sc->sc_udev);
    594  1.1  christos 	if (cmd->xfer == NULL) {
    595  1.1  christos 		printf("%s: could not allocate xfer\n",
    596  1.1  christos 		    sc->sc_dev.dv_xname);
    597  1.1  christos 		return ENOMEM;
    598  1.1  christos 	}
    599  1.1  christos 	cmd->buf = usbd_alloc_buffer(cmd->xfer, OTUS_MAX_TXCMDSZ);
    600  1.1  christos 	if (cmd->buf == NULL) {
    601  1.1  christos 		printf("%s: could not allocate xfer buffer\n",
    602  1.1  christos 		    sc->sc_dev.dv_xname);
    603  1.1  christos 		usbd_free_xfer(cmd->xfer);
    604  1.1  christos 		return ENOMEM;
    605  1.1  christos 	}
    606  1.1  christos 	return 0;
    607  1.1  christos }
    608  1.1  christos 
    609  1.1  christos void
    610  1.1  christos otus_free_tx_cmd(struct otus_softc *sc)
    611  1.1  christos {
    612  1.1  christos 	/* Make sure no transfers are pending. */
    613  1.1  christos 	usbd_abort_pipe(sc->cmd_tx_pipe);
    614  1.1  christos 
    615  1.1  christos 	if (sc->tx_cmd.xfer != NULL)
    616  1.1  christos 		usbd_free_xfer(sc->tx_cmd.xfer);
    617  1.1  christos }
    618  1.1  christos 
    619  1.1  christos int
    620  1.1  christos otus_alloc_tx_data_list(struct otus_softc *sc)
    621  1.1  christos {
    622  1.1  christos 	struct otus_tx_data *data;
    623  1.1  christos 	int i, error;
    624  1.1  christos 
    625  1.1  christos 	for (i = 0; i < OTUS_TX_DATA_LIST_COUNT; i++) {
    626  1.1  christos 		data = &sc->tx_data[i];
    627  1.1  christos 
    628  1.1  christos 		data->sc = sc;  /* Backpointer for callbacks. */
    629  1.1  christos 
    630  1.1  christos 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    631  1.1  christos 		if (data->xfer == NULL) {
    632  1.1  christos 			printf("%s: could not allocate xfer\n",
    633  1.1  christos 			    sc->sc_dev.dv_xname);
    634  1.1  christos 			error = ENOMEM;
    635  1.1  christos 			goto fail;
    636  1.1  christos 		}
    637  1.1  christos 		data->buf = usbd_alloc_buffer(data->xfer, OTUS_TXBUFSZ);
    638  1.1  christos 		if (data->buf == NULL) {
    639  1.1  christos 			printf("%s: could not allocate xfer buffer\n",
    640  1.1  christos 			    sc->sc_dev.dv_xname);
    641  1.1  christos 			error = ENOMEM;
    642  1.1  christos 			goto fail;
    643  1.1  christos 		}
    644  1.1  christos 	}
    645  1.1  christos 	return 0;
    646  1.1  christos 
    647  1.1  christos fail:	otus_free_tx_data_list(sc);
    648  1.1  christos 	return error;
    649  1.1  christos }
    650  1.1  christos 
    651  1.1  christos void
    652  1.1  christos otus_free_tx_data_list(struct otus_softc *sc)
    653  1.1  christos {
    654  1.1  christos 	int i;
    655  1.1  christos 
    656  1.1  christos 	/* Make sure no transfers are pending. */
    657  1.1  christos 	usbd_abort_pipe(sc->data_tx_pipe);
    658  1.1  christos 
    659  1.1  christos 	for (i = 0; i < OTUS_TX_DATA_LIST_COUNT; i++)
    660  1.1  christos 		if (sc->tx_data[i].xfer != NULL)
    661  1.1  christos 			usbd_free_xfer(sc->tx_data[i].xfer);
    662  1.1  christos }
    663  1.1  christos 
    664  1.1  christos int
    665  1.1  christos otus_alloc_rx_data_list(struct otus_softc *sc)
    666  1.1  christos {
    667  1.1  christos 	struct otus_rx_data *data;
    668  1.1  christos 	int i, error;
    669  1.1  christos 
    670  1.1  christos 	for (i = 0; i < OTUS_RX_DATA_LIST_COUNT; i++) {
    671  1.1  christos 		data = &sc->rx_data[i];
    672  1.1  christos 
    673  1.1  christos 		data->sc = sc;	/* Backpointer for callbacks. */
    674  1.1  christos 
    675  1.1  christos 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    676  1.1  christos 		if (data->xfer == NULL) {
    677  1.1  christos 			printf("%s: could not allocate xfer\n",
    678  1.1  christos 			    sc->sc_dev.dv_xname);
    679  1.1  christos 			error = ENOMEM;
    680  1.1  christos 			goto fail;
    681  1.1  christos 		}
    682  1.1  christos 		data->buf = usbd_alloc_buffer(data->xfer, OTUS_RXBUFSZ);
    683  1.1  christos 		if (data->buf == NULL) {
    684  1.1  christos 			printf("%s: could not allocate xfer buffer\n",
    685  1.1  christos 			    sc->sc_dev.dv_xname);
    686  1.1  christos 			error = ENOMEM;
    687  1.1  christos 			goto fail;
    688  1.1  christos 		}
    689  1.1  christos 	}
    690  1.1  christos 	return 0;
    691  1.1  christos 
    692  1.1  christos fail:	otus_free_rx_data_list(sc);
    693  1.1  christos 	return error;
    694  1.1  christos }
    695  1.1  christos 
    696  1.1  christos void
    697  1.1  christos otus_free_rx_data_list(struct otus_softc *sc)
    698  1.1  christos {
    699  1.1  christos 	int i;
    700  1.1  christos 
    701  1.1  christos 	/* Make sure no transfers are pending. */
    702  1.1  christos 	usbd_abort_pipe(sc->data_rx_pipe);
    703  1.1  christos 
    704  1.1  christos 	for (i = 0; i < OTUS_RX_DATA_LIST_COUNT; i++)
    705  1.1  christos 		if (sc->rx_data[i].xfer != NULL)
    706  1.1  christos 			usbd_free_xfer(sc->rx_data[i].xfer);
    707  1.1  christos }
    708  1.1  christos 
    709  1.1  christos void
    710  1.1  christos otus_next_scan(void *arg)
    711  1.1  christos {
    712  1.1  christos 	struct otus_softc *sc = arg;
    713  1.1  christos 
    714  1.1  christos 	if (sc->sc_ic.ic_state == IEEE80211_S_SCAN)
    715  1.1  christos 		ieee80211_next_scan(&sc->sc_ic.ic_if);
    716  1.1  christos }
    717  1.1  christos 
    718  1.1  christos void
    719  1.1  christos otus_task(void *arg)
    720  1.1  christos {
    721  1.1  christos 	struct otus_softc *sc = arg;
    722  1.1  christos 	struct otus_host_cmd_ring *ring = &sc->cmdq;
    723  1.1  christos 	struct otus_host_cmd *cmd;
    724  1.1  christos 	int s;
    725  1.1  christos 
    726  1.1  christos 	/* Process host commands. */
    727  1.1  christos 	s = splusb();
    728  1.1  christos 	while (ring->next != ring->cur) {
    729  1.1  christos 		cmd = &ring->cmd[ring->next];
    730  1.1  christos 		splx(s);
    731  1.1  christos 		/* Callback. */
    732  1.1  christos 		cmd->cb(sc, cmd->data);
    733  1.1  christos 		s = splusb();
    734  1.1  christos 		ring->queued--;
    735  1.1  christos 		ring->next = (ring->next + 1) % OTUS_HOST_CMD_RING_COUNT;
    736  1.1  christos 	}
    737  1.1  christos 	wakeup(ring);
    738  1.1  christos 	splx(s);
    739  1.1  christos }
    740  1.1  christos 
    741  1.1  christos void
    742  1.1  christos otus_do_async(struct otus_softc *sc, void (*cb)(struct otus_softc *, void *),
    743  1.1  christos     void *arg, int len)
    744  1.1  christos {
    745  1.1  christos 	struct otus_host_cmd_ring *ring = &sc->cmdq;
    746  1.1  christos 	struct otus_host_cmd *cmd;
    747  1.1  christos 	int s;
    748  1.1  christos 
    749  1.1  christos 	s = splusb();
    750  1.1  christos 	cmd = &ring->cmd[ring->cur];
    751  1.1  christos 	cmd->cb = cb;
    752  1.1  christos 	KASSERT(len <= sizeof (cmd->data));
    753  1.1  christos 	memcpy(cmd->data, arg, len);
    754  1.1  christos 	ring->cur = (ring->cur + 1) % OTUS_HOST_CMD_RING_COUNT;
    755  1.1  christos 
    756  1.1  christos 	/* If there is no pending command already, schedule a task. */
    757  1.1  christos 	if (++ring->queued == 1)
    758  1.1  christos 		usb_add_task(sc->sc_udev, &sc->sc_task);
    759  1.1  christos 	splx(s);
    760  1.1  christos }
    761  1.1  christos 
    762  1.1  christos int
    763  1.1  christos otus_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    764  1.1  christos {
    765  1.1  christos 	struct otus_softc *sc = ic->ic_softc;
    766  1.1  christos 	struct otus_cmd_newstate cmd;
    767  1.1  christos 
    768  1.1  christos 	/* Do it in a process context. */
    769  1.1  christos 	cmd.state = nstate;
    770  1.1  christos 	cmd.arg = arg;
    771  1.1  christos 	otus_do_async(sc, otus_newstate_cb, &cmd, sizeof cmd);
    772  1.1  christos 	return 0;
    773  1.1  christos }
    774  1.1  christos 
    775  1.1  christos void
    776  1.1  christos otus_newstate_cb(struct otus_softc *sc, void *arg)
    777  1.1  christos {
    778  1.1  christos 	struct otus_cmd_newstate *cmd = arg;
    779  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    780  1.1  christos 	struct ieee80211_node *ni;
    781  1.1  christos 	int s;
    782  1.1  christos 
    783  1.1  christos 	s = splnet();
    784  1.1  christos 
    785  1.1  christos 	switch (cmd->state) {
    786  1.1  christos 	case IEEE80211_S_INIT:
    787  1.1  christos 		break;
    788  1.1  christos 
    789  1.1  christos 	case IEEE80211_S_SCAN:
    790  1.1  christos 		(void)otus_set_chan(sc, ic->ic_bss->ni_chan, 0);
    791  1.1  christos 		timeout_add_msec(&sc->scan_to, 200);
    792  1.1  christos 		break;
    793  1.1  christos 
    794  1.1  christos 	case IEEE80211_S_AUTH:
    795  1.1  christos 	case IEEE80211_S_ASSOC:
    796  1.1  christos 		(void)otus_set_chan(sc, ic->ic_bss->ni_chan, 0);
    797  1.1  christos 		break;
    798  1.1  christos 
    799  1.1  christos 	case IEEE80211_S_RUN:
    800  1.1  christos 		(void)otus_set_chan(sc, ic->ic_bss->ni_chan, 1);
    801  1.1  christos 
    802  1.1  christos 		ni = ic->ic_bss;
    803  1.1  christos 
    804  1.1  christos 		if (ic->ic_opmode == IEEE80211_M_STA) {
    805  1.1  christos 			otus_updateslot(ic);
    806  1.1  christos 			otus_set_bssid(sc, ni->ni_bssid);
    807  1.1  christos 
    808  1.1  christos 			/* Fake a join to init the Tx rate. */
    809  1.1  christos 			otus_newassoc(ic, ni, 1);
    810  1.1  christos 
    811  1.1  christos 			/* Start calibration timer. */
    812  1.1  christos 			timeout_add_sec(&sc->calib_to, 1);
    813  1.1  christos 		}
    814  1.1  christos 		break;
    815  1.1  christos 	}
    816  1.1  christos 
    817  1.1  christos 	sc->sc_led_newstate(sc);
    818  1.1  christos 	(void)sc->sc_newstate(ic, cmd->state, cmd->arg);
    819  1.1  christos 
    820  1.1  christos 	splx(s);
    821  1.1  christos }
    822  1.1  christos 
    823  1.1  christos int
    824  1.1  christos otus_cmd(struct otus_softc *sc, uint8_t code, const void *idata, int ilen,
    825  1.1  christos     void *odata)
    826  1.1  christos {
    827  1.1  christos 	struct otus_tx_cmd *cmd = &sc->tx_cmd;
    828  1.1  christos 	struct ar_cmd_hdr *hdr;
    829  1.1  christos 	int s, xferlen, error;
    830  1.1  christos 
    831  1.1  christos 	/* Always bulk-out a multiple of 4 bytes. */
    832  1.1  christos 	xferlen = (sizeof (*hdr) + ilen + 3) & ~3;
    833  1.1  christos 
    834  1.1  christos 	hdr = (struct ar_cmd_hdr *)cmd->buf;
    835  1.1  christos 	hdr->code  = code;
    836  1.1  christos 	hdr->len   = ilen;
    837  1.1  christos 	hdr->token = ++cmd->token;	/* Don't care about endianness. */
    838  1.1  christos 	memcpy((uint8_t *)&hdr[1], idata, ilen);
    839  1.1  christos 
    840  1.1  christos 	DPRINTFN(2, ("sending command code=0x%02x len=%d token=%d\n",
    841  1.1  christos 	    code, ilen, hdr->token));
    842  1.1  christos 
    843  1.1  christos 	s = splusb();
    844  1.1  christos 	cmd->odata = odata;
    845  1.1  christos 	cmd->done = 0;
    846  1.1  christos 
    847  1.1  christos 	usbd_setup_xfer(cmd->xfer, sc->cmd_tx_pipe, cmd, cmd->buf, xferlen,
    848  1.1  christos 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, OTUS_CMD_TIMEOUT, NULL);
    849  1.1  christos 	error = usbd_sync_transfer(cmd->xfer);
    850  1.1  christos 	if (error != 0) {
    851  1.1  christos 		splx(s);
    852  1.1  christos 		printf("%s: could not send command 0x%x (error=%s)\n",
    853  1.1  christos 		    sc->sc_dev.dv_xname, code, usbd_errstr(error));
    854  1.1  christos 		return EIO;
    855  1.1  christos 	}
    856  1.1  christos 	if (!cmd->done)
    857  1.1  christos 		error = tsleep(cmd, PCATCH, "otuscmd", hz);
    858  1.1  christos 	cmd->odata = NULL;	/* In case answer is received too late. */
    859  1.1  christos 	splx(s);
    860  1.1  christos 	if (error != 0) {
    861  1.1  christos 		printf("%s: timeout waiting for command 0x%02x reply\n",
    862  1.1  christos 		    sc->sc_dev.dv_xname, code);
    863  1.1  christos 	}
    864  1.1  christos 	return error;
    865  1.1  christos }
    866  1.1  christos 
    867  1.1  christos void
    868  1.1  christos otus_write(struct otus_softc *sc, uint32_t reg, uint32_t val)
    869  1.1  christos {
    870  1.1  christos 	sc->write_buf[sc->write_idx].reg = htole32(reg);
    871  1.1  christos 	sc->write_buf[sc->write_idx].val = htole32(val);
    872  1.1  christos 
    873  1.1  christos 	if (++sc->write_idx > AR_MAX_WRITE_IDX)
    874  1.1  christos 		(void)otus_write_barrier(sc);
    875  1.1  christos }
    876  1.1  christos 
    877  1.1  christos int
    878  1.1  christos otus_write_barrier(struct otus_softc *sc)
    879  1.1  christos {
    880  1.1  christos 	int error;
    881  1.1  christos 
    882  1.1  christos 	if (sc->write_idx == 0)
    883  1.1  christos 		return 0;	/* Nothing to flush. */
    884  1.1  christos 
    885  1.1  christos 	error = otus_cmd(sc, AR_CMD_WREG, sc->write_buf,
    886  1.1  christos 	    sizeof (sc->write_buf[0]) * sc->write_idx, NULL);
    887  1.1  christos 	sc->write_idx = 0;
    888  1.1  christos 	return error;
    889  1.1  christos }
    890  1.1  christos 
    891  1.1  christos struct ieee80211_node *
    892  1.1  christos otus_node_alloc(struct ieee80211com *ic)
    893  1.1  christos {
    894  1.1  christos 	return malloc(sizeof (struct otus_node), M_DEVBUF, M_NOWAIT | M_ZERO);
    895  1.1  christos }
    896  1.1  christos 
    897  1.1  christos int
    898  1.1  christos otus_media_change(struct ifnet *ifp)
    899  1.1  christos {
    900  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
    901  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    902  1.1  christos 	uint8_t rate, ridx;
    903  1.1  christos 	int error;
    904  1.1  christos 
    905  1.1  christos 	error = ieee80211_media_change(ifp);
    906  1.1  christos 	if (error != ENETRESET)
    907  1.1  christos 		return error;
    908  1.1  christos 
    909  1.1  christos 	if (ic->ic_fixed_rate != -1) {
    910  1.1  christos 		rate = ic->ic_sup_rates[ic->ic_curmode].
    911  1.1  christos 		    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    912  1.1  christos 		for (ridx = 0; ridx <= OTUS_RIDX_MAX; ridx++)
    913  1.1  christos 			if (otus_rates[ridx].rate == rate)
    914  1.1  christos 				break;
    915  1.1  christos 		sc->fixed_ridx = ridx;
    916  1.1  christos 	}
    917  1.1  christos 
    918  1.1  christos 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    919  1.1  christos 		error = otus_init(ifp);
    920  1.1  christos 
    921  1.1  christos 	return error;
    922  1.1  christos }
    923  1.1  christos 
    924  1.1  christos int
    925  1.1  christos otus_read_eeprom(struct otus_softc *sc)
    926  1.1  christos {
    927  1.1  christos 	uint32_t regs[8], reg;
    928  1.1  christos 	uint8_t *eep;
    929  1.1  christos 	int i, j, error;
    930  1.1  christos 
    931  1.1  christos 	/* Read EEPROM by blocks of 32 bytes. */
    932  1.1  christos 	eep = (uint8_t *)&sc->eeprom;
    933  1.1  christos 	reg = AR_EEPROM_OFFSET;
    934  1.1  christos 	for (i = 0; i < sizeof (sc->eeprom) / 32; i++) {
    935  1.1  christos 		for (j = 0; j < 8; j++, reg += 4)
    936  1.1  christos 			regs[j] = htole32(reg);
    937  1.1  christos 		error = otus_cmd(sc, AR_CMD_RREG, regs, sizeof regs, eep);
    938  1.1  christos 		if (error != 0)
    939  1.1  christos 			break;
    940  1.1  christos 		eep += 32;
    941  1.1  christos 	}
    942  1.1  christos 	return error;
    943  1.1  christos }
    944  1.1  christos 
    945  1.1  christos void
    946  1.1  christos otus_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
    947  1.1  christos {
    948  1.1  christos 	struct otus_softc *sc = ic->ic_softc;
    949  1.1  christos 	struct otus_node *on = (void *)ni;
    950  1.1  christos 	struct ieee80211_rateset *rs = &ni->ni_rates;
    951  1.1  christos 	uint8_t rate;
    952  1.1  christos 	int ridx, i;
    953  1.1  christos 
    954  1.1  christos 	DPRINTF(("new assoc isnew=%d addr=%s\n",
    955  1.1  christos 	    isnew, ether_sprintf(ni->ni_macaddr)));
    956  1.1  christos 
    957  1.1  christos 	ieee80211_amrr_node_init(&sc->amrr, &on->amn);
    958  1.1  christos 	/* Start at lowest available bit-rate, AMRR will raise. */
    959  1.1  christos 	ni->ni_txrate = 0;
    960  1.1  christos 
    961  1.1  christos 	for (i = 0; i < rs->rs_nrates; i++) {
    962  1.1  christos 		rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
    963  1.1  christos 		/* Convert 802.11 rate to hardware rate index. */
    964  1.1  christos 		for (ridx = 0; ridx <= OTUS_RIDX_MAX; ridx++)
    965  1.1  christos 			if (otus_rates[ridx].rate == rate)
    966  1.1  christos 				break;
    967  1.1  christos 		on->ridx[i] = ridx;
    968  1.1  christos 		DPRINTF(("rate=0x%02x ridx=%d\n",
    969  1.1  christos 		    rs->rs_rates[i], on->ridx[i]));
    970  1.1  christos 	}
    971  1.1  christos }
    972  1.1  christos 
    973  1.1  christos /* ARGSUSED */
    974  1.1  christos void
    975  1.1  christos otus_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    976  1.1  christos {
    977  1.1  christos #if 0
    978  1.1  christos 	struct otus_softc *sc = priv;
    979  1.1  christos 	int len;
    980  1.1  christos 
    981  1.1  christos 	/*
    982  1.1  christos 	 * The Rx intr pipe is unused with current firmware.  Notifications
    983  1.1  christos 	 * and replies to commands are sent through the Rx bulk pipe instead
    984  1.1  christos 	 * (with a magic PLCP header.)
    985  1.1  christos 	 */
    986  1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
    987  1.1  christos 		DPRINTF(("intr status=%d\n", status));
    988  1.1  christos 		if (status == USBD_STALLED)
    989  1.1  christos 			usbd_clear_endpoint_stall_async(sc->cmd_rx_pipe);
    990  1.1  christos 		return;
    991  1.1  christos 	}
    992  1.1  christos 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    993  1.1  christos 
    994  1.1  christos 	otus_cmd_rxeof(sc, sc->ibuf, len);
    995  1.1  christos #endif
    996  1.1  christos }
    997  1.1  christos 
    998  1.1  christos void
    999  1.1  christos otus_cmd_rxeof(struct otus_softc *sc, uint8_t *buf, int len)
   1000  1.1  christos {
   1001  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1002  1.1  christos 	struct otus_tx_cmd *cmd;
   1003  1.1  christos 	struct ar_cmd_hdr *hdr;
   1004  1.1  christos 	int s;
   1005  1.1  christos 
   1006  1.1  christos 	if (__predict_false(len < sizeof (*hdr))) {
   1007  1.1  christos 		DPRINTF(("cmd too small %d\n", len));
   1008  1.1  christos 		return;
   1009  1.1  christos 	}
   1010  1.1  christos 	hdr = (struct ar_cmd_hdr *)buf;
   1011  1.1  christos 	if (__predict_false(sizeof (*hdr) + hdr->len > len ||
   1012  1.1  christos 	    sizeof (*hdr) + hdr->len > 64)) {
   1013  1.1  christos 		DPRINTF(("cmd too large %d\n", hdr->len));
   1014  1.1  christos 		return;
   1015  1.1  christos 	}
   1016  1.1  christos 
   1017  1.1  christos 	if ((hdr->code & 0xc0) != 0xc0) {
   1018  1.1  christos 		DPRINTFN(2, ("received reply code=0x%02x len=%d token=%d\n",
   1019  1.1  christos 		    hdr->code, hdr->len, hdr->token));
   1020  1.1  christos 		cmd = &sc->tx_cmd;
   1021  1.1  christos 		if (__predict_false(hdr->token != cmd->token))
   1022  1.1  christos 			return;
   1023  1.1  christos 		/* Copy answer into caller's supplied buffer. */
   1024  1.1  christos 		if (cmd->odata != NULL)
   1025  1.1  christos 			memcpy(cmd->odata, &hdr[1], hdr->len);
   1026  1.1  christos 		cmd->done = 1;
   1027  1.1  christos 		wakeup(cmd);
   1028  1.1  christos 		return;
   1029  1.1  christos 	}
   1030  1.1  christos 
   1031  1.1  christos 	/* Received unsolicited notification. */
   1032  1.1  christos 	DPRINTF(("received notification code=0x%02x len=%d\n",
   1033  1.1  christos 	    hdr->code, hdr->len));
   1034  1.1  christos 	switch (hdr->code & 0x3f) {
   1035  1.1  christos 	case AR_EVT_BEACON:
   1036  1.1  christos 		break;
   1037  1.1  christos 	case AR_EVT_TX_COMP:
   1038  1.1  christos 	{
   1039  1.1  christos 		struct ar_evt_tx_comp *tx = (struct ar_evt_tx_comp *)&hdr[1];
   1040  1.1  christos 		struct ieee80211_node *ni;
   1041  1.1  christos 		struct otus_node *on;
   1042  1.1  christos 
   1043  1.1  christos 		DPRINTF(("tx completed %s status=%d phy=0x%x\n",
   1044  1.1  christos 		    ether_sprintf(tx->macaddr), letoh16(tx->status),
   1045  1.1  christos 		    letoh32(tx->phy)));
   1046  1.1  christos 		s = splnet();
   1047  1.1  christos #ifdef notyet
   1048  1.1  christos #ifndef IEEE80211_STA_ONLY
   1049  1.1  christos 		if (ic->ic_opmode != IEEE80211_M_STA) {
   1050  1.1  christos 			ni = ieee80211_find_node(ic, tx->macaddr);
   1051  1.1  christos 			if (__predict_false(ni == NULL)) {
   1052  1.1  christos 				splx(s);
   1053  1.1  christos 				break;
   1054  1.1  christos 			}
   1055  1.1  christos 		} else
   1056  1.1  christos #endif
   1057  1.1  christos #endif
   1058  1.1  christos 			ni = ic->ic_bss;
   1059  1.1  christos 		/* Update rate control statistics. */
   1060  1.1  christos 		on = (void *)ni;
   1061  1.1  christos 		/* NB: we do not set the TX_MAC_RATE_PROBING flag. */
   1062  1.1  christos 		if (__predict_true(tx->status != 0))
   1063  1.1  christos 			on->amn.amn_retrycnt++;
   1064  1.1  christos 		splx(s);
   1065  1.1  christos 		break;
   1066  1.1  christos 	}
   1067  1.1  christos 	case AR_EVT_TBTT:
   1068  1.1  christos 		break;
   1069  1.1  christos 	}
   1070  1.1  christos }
   1071  1.1  christos 
   1072  1.1  christos void
   1073  1.1  christos otus_sub_rxeof(struct otus_softc *sc, uint8_t *buf, int len)
   1074  1.1  christos {
   1075  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1076  1.1  christos 	struct ifnet *ifp = &ic->ic_if;
   1077  1.1  christos 	struct ieee80211_rxinfo rxi;
   1078  1.1  christos 	struct ieee80211_node *ni;
   1079  1.1  christos 	struct ar_rx_tail *tail;
   1080  1.1  christos 	struct ieee80211_frame *wh;
   1081  1.1  christos 	struct mbuf *m;
   1082  1.1  christos 	uint8_t *plcp;
   1083  1.1  christos 	int s, mlen, align;
   1084  1.1  christos 
   1085  1.1  christos 	if (__predict_false(len < AR_PLCP_HDR_LEN)) {
   1086  1.1  christos 		DPRINTF(("sub-xfer too short %d\n", len));
   1087  1.1  christos 		return;
   1088  1.1  christos 	}
   1089  1.1  christos 	plcp = buf;
   1090  1.1  christos 
   1091  1.1  christos 	/* All bits in the PLCP header are set to 1 for non-MPDU. */
   1092  1.1  christos 	if (memcmp(plcp, AR_PLCP_HDR_INTR, AR_PLCP_HDR_LEN) == 0) {
   1093  1.1  christos 		otus_cmd_rxeof(sc, plcp + AR_PLCP_HDR_LEN,
   1094  1.1  christos 		    len - AR_PLCP_HDR_LEN);
   1095  1.1  christos 		return;
   1096  1.1  christos 	}
   1097  1.1  christos 
   1098  1.1  christos 	/* Received MPDU. */
   1099  1.1  christos 	if (__predict_false(len < AR_PLCP_HDR_LEN + sizeof (*tail))) {
   1100  1.1  christos 		DPRINTF(("MPDU too short %d\n", len));
   1101  1.1  christos 		ifp->if_ierrors++;
   1102  1.1  christos 		return;
   1103  1.1  christos 	}
   1104  1.1  christos 	tail = (struct ar_rx_tail *)(plcp + len - sizeof (*tail));
   1105  1.1  christos 
   1106  1.1  christos 	/* Discard error frames. */
   1107  1.1  christos 	if (__predict_false(tail->error != 0)) {
   1108  1.1  christos 		DPRINTF(("error frame 0x%02x\n", tail->error));
   1109  1.1  christos 		if (tail->error & AR_RX_ERROR_FCS) {
   1110  1.1  christos 			DPRINTFN(3, ("bad FCS\n"));
   1111  1.1  christos 		} else if (tail->error & AR_RX_ERROR_MMIC) {
   1112  1.1  christos 			/* Report Michael MIC failures to net80211. */
   1113  1.1  christos 			ic->ic_stats.is_rx_locmicfail++;
   1114  1.1  christos 			ieee80211_michael_mic_failure(ic, 0);
   1115  1.1  christos 		}
   1116  1.1  christos 		ifp->if_ierrors++;
   1117  1.1  christos 		return;
   1118  1.1  christos 	}
   1119  1.1  christos 	/* Compute MPDU's length. */
   1120  1.1  christos 	mlen = len - AR_PLCP_HDR_LEN - sizeof (*tail);
   1121  1.1  christos 	/* Make sure there's room for an 802.11 header + FCS. */
   1122  1.1  christos 	if (__predict_false(mlen < IEEE80211_MIN_LEN)) {
   1123  1.1  christos 		ifp->if_ierrors++;
   1124  1.1  christos 		return;
   1125  1.1  christos 	}
   1126  1.1  christos 	mlen -= IEEE80211_CRC_LEN;	/* strip 802.11 FCS */
   1127  1.1  christos 
   1128  1.1  christos 	wh = (struct ieee80211_frame *)(plcp + AR_PLCP_HDR_LEN);
   1129  1.1  christos 	/* Provide a 32-bit aligned protocol header to the stack. */
   1130  1.1  christos 	align = (ieee80211_has_qos(wh) ^ ieee80211_has_addr4(wh)) ? 2 : 0;
   1131  1.1  christos 
   1132  1.1  christos 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1133  1.1  christos 	if (__predict_false(m == NULL)) {
   1134  1.1  christos 		ifp->if_ierrors++;
   1135  1.1  christos 		return;
   1136  1.1  christos 	}
   1137  1.1  christos 	if (align + mlen > MHLEN) {
   1138  1.1  christos 		MCLGET(m, M_DONTWAIT);
   1139  1.1  christos 		if (__predict_false(!(m->m_flags & M_EXT))) {
   1140  1.1  christos 			ifp->if_ierrors++;
   1141  1.1  christos 			m_freem(m);
   1142  1.1  christos 			return;
   1143  1.1  christos 		}
   1144  1.1  christos 	}
   1145  1.1  christos 	/* Finalize mbuf. */
   1146  1.1  christos 	m->m_pkthdr.rcvif = ifp;
   1147  1.1  christos 	m->m_data += align;
   1148  1.1  christos 	memcpy(mtod(m, caddr_t), wh, mlen);
   1149  1.1  christos 	m->m_pkthdr.len = m->m_len = mlen;
   1150  1.1  christos 
   1151  1.1  christos #if NBPFILTER > 0
   1152  1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   1153  1.1  christos 		struct otus_rx_radiotap_header *tap = &sc->sc_rxtap;
   1154  1.1  christos 		struct mbuf mb;
   1155  1.1  christos 
   1156  1.1  christos 		tap->wr_flags = 0;
   1157  1.1  christos 		tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
   1158  1.1  christos 		tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
   1159  1.1  christos 		tap->wr_antsignal = tail->rssi;
   1160  1.1  christos 		tap->wr_rate = 2;	/* In case it can't be found below. */
   1161  1.1  christos 		switch (tail->status & AR_RX_STATUS_MT_MASK) {
   1162  1.1  christos 		case AR_RX_STATUS_MT_CCK:
   1163  1.1  christos 			switch (plcp[0]) {
   1164  1.1  christos 			case  10: tap->wr_rate =   2; break;
   1165  1.1  christos 			case  20: tap->wr_rate =   4; break;
   1166  1.1  christos 			case  55: tap->wr_rate =  11; break;
   1167  1.1  christos 			case 110: tap->wr_rate =  22; break;
   1168  1.1  christos 			}
   1169  1.1  christos 			if (tail->status & AR_RX_STATUS_SHPREAMBLE)
   1170  1.1  christos 				tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1171  1.1  christos 			break;
   1172  1.1  christos 		case AR_RX_STATUS_MT_OFDM:
   1173  1.1  christos 			switch (plcp[0] & 0xf) {
   1174  1.1  christos 			case 0xb: tap->wr_rate =  12; break;
   1175  1.1  christos 			case 0xf: tap->wr_rate =  18; break;
   1176  1.1  christos 			case 0xa: tap->wr_rate =  24; break;
   1177  1.1  christos 			case 0xe: tap->wr_rate =  36; break;
   1178  1.1  christos 			case 0x9: tap->wr_rate =  48; break;
   1179  1.1  christos 			case 0xd: tap->wr_rate =  72; break;
   1180  1.1  christos 			case 0x8: tap->wr_rate =  96; break;
   1181  1.1  christos 			case 0xc: tap->wr_rate = 108; break;
   1182  1.1  christos 			}
   1183  1.1  christos 			break;
   1184  1.1  christos 		}
   1185  1.1  christos 		mb.m_data = (caddr_t)tap;
   1186  1.1  christos 		mb.m_len = sc->sc_rxtap_len;
   1187  1.1  christos 		mb.m_next = m;
   1188  1.1  christos 		mb.m_nextpkt = NULL;
   1189  1.1  christos 		mb.m_type = 0;
   1190  1.1  christos 		mb.m_flags = 0;
   1191  1.1  christos 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
   1192  1.1  christos 	}
   1193  1.1  christos #endif
   1194  1.1  christos 
   1195  1.1  christos 	s = splnet();
   1196  1.1  christos 	ni = ieee80211_find_rxnode(ic, wh);
   1197  1.1  christos 	rxi.rxi_flags = 0;
   1198  1.1  christos 	rxi.rxi_rssi = tail->rssi;
   1199  1.1  christos 	rxi.rxi_tstamp = 0;	/* unused */
   1200  1.1  christos 	ieee80211_input(ifp, m, ni, &rxi);
   1201  1.1  christos 
   1202  1.1  christos 	/* Node is no longer needed. */
   1203  1.1  christos 	ieee80211_release_node(ic, ni);
   1204  1.1  christos 	splx(s);
   1205  1.1  christos }
   1206  1.1  christos 
   1207  1.1  christos void
   1208  1.1  christos otus_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1209  1.1  christos {
   1210  1.1  christos 	struct otus_rx_data *data = priv;
   1211  1.1  christos 	struct otus_softc *sc = data->sc;
   1212  1.1  christos 	caddr_t buf = data->buf;
   1213  1.1  christos 	struct ar_rx_head *head;
   1214  1.1  christos 	uint16_t hlen;
   1215  1.1  christos 	int len;
   1216  1.1  christos 
   1217  1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   1218  1.1  christos 		DPRINTF(("RX status=%d\n", status));
   1219  1.1  christos 		if (status == USBD_STALLED)
   1220  1.1  christos 			usbd_clear_endpoint_stall_async(sc->data_rx_pipe);
   1221  1.1  christos 		if (status != USBD_CANCELLED)
   1222  1.1  christos 			goto resubmit;
   1223  1.1  christos 		return;
   1224  1.1  christos 	}
   1225  1.1  christos 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   1226  1.1  christos 
   1227  1.1  christos 	while (len >= sizeof (*head)) {
   1228  1.1  christos 		head = (struct ar_rx_head *)buf;
   1229  1.1  christos 		if (__predict_false(head->tag != htole16(AR_RX_HEAD_TAG))) {
   1230  1.1  christos 			DPRINTF(("tag not valid 0x%x\n", letoh16(head->tag)));
   1231  1.1  christos 			break;
   1232  1.1  christos 		}
   1233  1.1  christos 		hlen = letoh16(head->len);
   1234  1.1  christos 		if (__predict_false(sizeof (*head) + hlen > len)) {
   1235  1.1  christos 			DPRINTF(("xfer too short %d/%d\n", len, hlen));
   1236  1.1  christos 			break;
   1237  1.1  christos 		}
   1238  1.1  christos 		/* Process sub-xfer. */
   1239  1.1  christos 		otus_sub_rxeof(sc, (uint8_t *)&head[1], hlen);
   1240  1.1  christos 
   1241  1.1  christos 		/* Next sub-xfer is aligned on a 32-bit boundary. */
   1242  1.1  christos 		hlen = (sizeof (*head) + hlen + 3) & ~3;
   1243  1.1  christos 		buf += hlen;
   1244  1.1  christos 		len -= hlen;
   1245  1.1  christos 	}
   1246  1.1  christos 
   1247  1.1  christos  resubmit:
   1248  1.1  christos 	usbd_setup_xfer(xfer, sc->data_rx_pipe, data, data->buf, OTUS_RXBUFSZ,
   1249  1.1  christos 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, otus_rxeof);
   1250  1.1  christos 	(void)usbd_transfer(data->xfer);
   1251  1.1  christos }
   1252  1.1  christos 
   1253  1.1  christos void
   1254  1.1  christos otus_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1255  1.1  christos {
   1256  1.1  christos 	struct otus_tx_data *data = priv;
   1257  1.1  christos 	struct otus_softc *sc = data->sc;
   1258  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1259  1.1  christos 	struct ifnet *ifp = &ic->ic_if;
   1260  1.1  christos 	int s;
   1261  1.1  christos 
   1262  1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   1263  1.1  christos 		DPRINTF(("TX status=%d\n", status));
   1264  1.1  christos 		if (status == USBD_STALLED)
   1265  1.1  christos 			usbd_clear_endpoint_stall_async(sc->data_tx_pipe);
   1266  1.1  christos 		ifp->if_oerrors++;
   1267  1.1  christos 		return;
   1268  1.1  christos 	}
   1269  1.1  christos 	s = splnet();
   1270  1.1  christos 	sc->tx_queued--;
   1271  1.1  christos 	sc->sc_tx_timer = 0;
   1272  1.1  christos 	ifp->if_flags &= ~IFF_OACTIVE;
   1273  1.1  christos 	otus_start(ifp);
   1274  1.1  christos 	splx(s);
   1275  1.1  christos }
   1276  1.1  christos 
   1277  1.1  christos int
   1278  1.1  christos otus_tx(struct otus_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
   1279  1.1  christos {
   1280  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1281  1.1  christos 	struct otus_node *on = (void *)ni;
   1282  1.1  christos 	struct otus_tx_data *data;
   1283  1.1  christos 	struct ieee80211_frame *wh;
   1284  1.1  christos 	struct ieee80211_key *k;
   1285  1.1  christos 	struct ar_tx_head *head;
   1286  1.1  christos 	uint32_t phyctl;
   1287  1.1  christos 	uint16_t macctl, qos;
   1288  1.1  christos 	uint8_t tid, qid;
   1289  1.1  christos 	int error, ridx, hasqos, xferlen;
   1290  1.1  christos 
   1291  1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1292  1.1  christos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
   1293  1.1  christos 		k = ieee80211_get_txkey(ic, wh, ni);
   1294  1.1  christos 		if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
   1295  1.1  christos 			return ENOBUFS;
   1296  1.1  christos 		wh = mtod(m, struct ieee80211_frame *);
   1297  1.1  christos 	}
   1298  1.1  christos 
   1299  1.1  christos 	if ((hasqos = ieee80211_has_qos(wh))) {
   1300  1.1  christos 		qos = ieee80211_get_qos(wh);
   1301  1.1  christos 		tid = qos & IEEE80211_QOS_TID;
   1302  1.1  christos 		qid = ieee80211_up_to_ac(ic, tid);
   1303  1.1  christos 	} else
   1304  1.1  christos 		qid = EDCA_AC_BE;
   1305  1.1  christos 
   1306  1.1  christos 	/* Pickup a rate index. */
   1307  1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1308  1.1  christos 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA)
   1309  1.1  christos 		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1310  1.1  christos 		    OTUS_RIDX_OFDM6 : OTUS_RIDX_CCK1;
   1311  1.1  christos 	else if (ic->ic_fixed_rate != -1)
   1312  1.1  christos 		ridx = sc->fixed_ridx;
   1313  1.1  christos 	else
   1314  1.1  christos 		ridx = on->ridx[ni->ni_txrate];
   1315  1.1  christos 
   1316  1.1  christos 	phyctl = 0;
   1317  1.1  christos 	macctl = AR_TX_MAC_BACKOFF | AR_TX_MAC_HW_DUR | AR_TX_MAC_QID(qid);
   1318  1.1  christos 
   1319  1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1320  1.1  christos 	    (hasqos && ((qos & IEEE80211_QOS_ACK_POLICY_MASK) ==
   1321  1.1  christos 	     IEEE80211_QOS_ACK_POLICY_NOACK)))
   1322  1.1  christos 		macctl |= AR_TX_MAC_NOACK;
   1323  1.1  christos 
   1324  1.1  christos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1325  1.1  christos 		if (m->m_pkthdr.len + IEEE80211_CRC_LEN >= ic->ic_rtsthreshold)
   1326  1.1  christos 			macctl |= AR_TX_MAC_RTS;
   1327  1.1  christos 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
   1328  1.1  christos 		    ridx >= OTUS_RIDX_OFDM6) {
   1329  1.1  christos 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   1330  1.1  christos 				macctl |= AR_TX_MAC_CTS;
   1331  1.1  christos 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   1332  1.1  christos 				macctl |= AR_TX_MAC_RTS;
   1333  1.1  christos 		}
   1334  1.1  christos 	}
   1335  1.1  christos 
   1336  1.1  christos 	phyctl |= AR_TX_PHY_MCS(otus_rates[ridx].mcs);
   1337  1.1  christos 	if (ridx >= OTUS_RIDX_OFDM6) {
   1338  1.1  christos 		phyctl |= AR_TX_PHY_MT_OFDM;
   1339  1.1  christos 		if (ridx <= OTUS_RIDX_OFDM24)
   1340  1.1  christos 			phyctl |= AR_TX_PHY_ANTMSK(sc->txmask);
   1341  1.1  christos 		else
   1342  1.1  christos 			phyctl |= AR_TX_PHY_ANTMSK(1);
   1343  1.1  christos 	} else {	/* CCK */
   1344  1.1  christos 		phyctl |= AR_TX_PHY_MT_CCK;
   1345  1.1  christos 		phyctl |= AR_TX_PHY_ANTMSK(sc->txmask);
   1346  1.1  christos 	}
   1347  1.1  christos 
   1348  1.1  christos 	/* Update rate control stats for frames that are ACK'ed. */
   1349  1.1  christos 	if (!(macctl & AR_TX_MAC_NOACK))
   1350  1.1  christos 		((struct otus_node *)ni)->amn.amn_txcnt++;
   1351  1.1  christos 
   1352  1.1  christos 	data = &sc->tx_data[sc->tx_cur];
   1353  1.1  christos 	/* Fill Tx descriptor. */
   1354  1.1  christos 	head = (struct ar_tx_head *)data->buf;
   1355  1.1  christos 	head->len = htole16(m->m_pkthdr.len + IEEE80211_CRC_LEN);
   1356  1.1  christos 	head->macctl = htole16(macctl);
   1357  1.1  christos 	head->phyctl = htole32(phyctl);
   1358  1.1  christos 
   1359  1.1  christos #if NBPFILTER > 0
   1360  1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   1361  1.1  christos 		struct otus_tx_radiotap_header *tap = &sc->sc_txtap;
   1362  1.1  christos 		struct mbuf mb;
   1363  1.1  christos 
   1364  1.1  christos 		tap->wt_flags = 0;
   1365  1.1  christos 		tap->wt_rate = otus_rates[ridx].rate;
   1366  1.1  christos 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
   1367  1.1  christos 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
   1368  1.1  christos 
   1369  1.1  christos 		mb.m_data = (caddr_t)tap;
   1370  1.1  christos 		mb.m_len = sc->sc_txtap_len;
   1371  1.1  christos 		mb.m_next = m;
   1372  1.1  christos 		mb.m_nextpkt = NULL;
   1373  1.1  christos 		mb.m_type = 0;
   1374  1.1  christos 		mb.m_flags = 0;
   1375  1.1  christos 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
   1376  1.1  christos 	}
   1377  1.1  christos #endif
   1378  1.1  christos 
   1379  1.1  christos 	xferlen = sizeof (*head) + m->m_pkthdr.len;
   1380  1.1  christos 	m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&head[1]);
   1381  1.1  christos 	m_freem(m);
   1382  1.1  christos 	ieee80211_release_node(ic, ni);
   1383  1.1  christos 
   1384  1.1  christos 	DPRINTFN(5, ("tx queued=%d len=%d mac=0x%04x phy=0x%08x rate=%d\n",
   1385  1.1  christos 	    sc->tx_queued, head->len, head->macctl, head->phyctl,
   1386  1.1  christos 	    otus_rates[ridx].rate));
   1387  1.1  christos 	usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf, xferlen,
   1388  1.1  christos 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, OTUS_TX_TIMEOUT, otus_txeof);
   1389  1.1  christos 	error = usbd_transfer(data->xfer);
   1390  1.1  christos 	if (__predict_false(error != USBD_IN_PROGRESS && error != 0))
   1391  1.1  christos 		return error;
   1392  1.1  christos 
   1393  1.1  christos 	sc->tx_queued++;
   1394  1.1  christos 	sc->tx_cur = (sc->tx_cur + 1) % OTUS_TX_DATA_LIST_COUNT;
   1395  1.1  christos 
   1396  1.1  christos 	return 0;
   1397  1.1  christos }
   1398  1.1  christos 
   1399  1.1  christos void
   1400  1.1  christos otus_start(struct ifnet *ifp)
   1401  1.1  christos {
   1402  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
   1403  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1404  1.1  christos 	struct ieee80211_node *ni;
   1405  1.1  christos 	struct mbuf *m;
   1406  1.1  christos 
   1407  1.1  christos 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1408  1.1  christos 		return;
   1409  1.1  christos 
   1410  1.1  christos 	for (;;) {
   1411  1.1  christos 		if (sc->tx_queued >= OTUS_TX_DATA_LIST_COUNT) {
   1412  1.1  christos 			ifp->if_flags |= IFF_OACTIVE;
   1413  1.1  christos 			break;
   1414  1.1  christos 		}
   1415  1.1  christos 		/* Send pending management frames first. */
   1416  1.1  christos 		IF_DEQUEUE(&ic->ic_mgtq, m);
   1417  1.1  christos 		if (m != NULL) {
   1418  1.1  christos 			ni = (void *)m->m_pkthdr.rcvif;
   1419  1.1  christos 			goto sendit;
   1420  1.1  christos 		}
   1421  1.1  christos 		if (ic->ic_state != IEEE80211_S_RUN)
   1422  1.1  christos 			break;
   1423  1.1  christos 
   1424  1.1  christos 		/* Encapsulate and send data frames. */
   1425  1.1  christos 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1426  1.1  christos 		if (m == NULL)
   1427  1.1  christos 			break;
   1428  1.1  christos #if NBPFILTER > 0
   1429  1.1  christos 		if (ifp->if_bpf != NULL)
   1430  1.1  christos 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
   1431  1.1  christos #endif
   1432  1.1  christos 		if ((m = ieee80211_encap(ifp, m, &ni)) == NULL)
   1433  1.1  christos 			continue;
   1434  1.1  christos sendit:
   1435  1.1  christos #if NBPFILTER > 0
   1436  1.1  christos 		if (ic->ic_rawbpf != NULL)
   1437  1.1  christos 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
   1438  1.1  christos #endif
   1439  1.1  christos 		if (otus_tx(sc, m, ni) != 0) {
   1440  1.1  christos 			ieee80211_release_node(ic, ni);
   1441  1.1  christos 			ifp->if_oerrors++;
   1442  1.1  christos 			continue;
   1443  1.1  christos 		}
   1444  1.1  christos 
   1445  1.1  christos 		sc->sc_tx_timer = 5;
   1446  1.1  christos 		ifp->if_timer = 1;
   1447  1.1  christos 	}
   1448  1.1  christos }
   1449  1.1  christos 
   1450  1.1  christos void
   1451  1.1  christos otus_watchdog(struct ifnet *ifp)
   1452  1.1  christos {
   1453  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
   1454  1.1  christos 
   1455  1.1  christos 	ifp->if_timer = 0;
   1456  1.1  christos 
   1457  1.1  christos 	if (sc->sc_tx_timer > 0) {
   1458  1.1  christos 		if (--sc->sc_tx_timer == 0) {
   1459  1.1  christos 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1460  1.1  christos 			/* otus_init(ifp); XXX needs a process context! */
   1461  1.1  christos 			ifp->if_oerrors++;
   1462  1.1  christos 			return;
   1463  1.1  christos 		}
   1464  1.1  christos 		ifp->if_timer = 1;
   1465  1.1  christos 	}
   1466  1.1  christos 	ieee80211_watchdog(ifp);
   1467  1.1  christos }
   1468  1.1  christos 
   1469  1.1  christos int
   1470  1.1  christos otus_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1471  1.1  christos {
   1472  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
   1473  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1474  1.1  christos 	struct ifaddr *ifa;
   1475  1.1  christos 	struct ifreq *ifr;
   1476  1.1  christos 	int s, error = 0;
   1477  1.1  christos 
   1478  1.1  christos 	s = splnet();
   1479  1.1  christos 
   1480  1.1  christos 	switch (cmd) {
   1481  1.1  christos 	case SIOCSIFADDR:
   1482  1.1  christos 		ifa = (struct ifaddr *)data;
   1483  1.1  christos 		ifp->if_flags |= IFF_UP;
   1484  1.1  christos #ifdef INET
   1485  1.1  christos 		if (ifa->ifa_addr->sa_family == AF_INET)
   1486  1.1  christos 			arp_ifinit(&ic->ic_ac, ifa);
   1487  1.1  christos #endif
   1488  1.1  christos 		/* FALLTHROUGH */
   1489  1.1  christos 	case SIOCSIFFLAGS:
   1490  1.1  christos 		if (ifp->if_flags & IFF_UP) {
   1491  1.1  christos 			if ((ifp->if_flags & IFF_RUNNING) &&
   1492  1.1  christos 			    ((ifp->if_flags ^ sc->sc_if_flags) &
   1493  1.1  christos 			     (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
   1494  1.1  christos 				otus_set_multi(sc);
   1495  1.1  christos 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1496  1.1  christos 				otus_init(ifp);
   1497  1.1  christos 
   1498  1.1  christos 		} else if (ifp->if_flags & IFF_RUNNING)
   1499  1.1  christos 			otus_stop(ifp);
   1500  1.1  christos 
   1501  1.1  christos 		sc->sc_if_flags = ifp->if_flags;
   1502  1.1  christos 		break;
   1503  1.1  christos 	case SIOCADDMULTI:
   1504  1.1  christos 	case SIOCDELMULTI:
   1505  1.1  christos 		ifr = (struct ifreq *)data;
   1506  1.1  christos 		error = (cmd == SIOCADDMULTI) ?
   1507  1.1  christos 		    ether_addmulti(ifr, &ic->ic_ac) :
   1508  1.1  christos 		    ether_delmulti(ifr, &ic->ic_ac);
   1509  1.1  christos 		if (error == ENETRESET)
   1510  1.1  christos 			error = 0;
   1511  1.1  christos 		break;
   1512  1.1  christos 	case SIOCS80211CHANNEL:
   1513  1.1  christos 		error = ieee80211_ioctl(ifp, cmd, data);
   1514  1.1  christos 		if (error == ENETRESET &&
   1515  1.1  christos 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
   1516  1.1  christos 			if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1517  1.1  christos 			    (IFF_UP | IFF_RUNNING))
   1518  1.1  christos 				otus_set_chan(sc, ic->ic_ibss_chan, 0);
   1519  1.1  christos 			error = 0;
   1520  1.1  christos 		}
   1521  1.1  christos 		break;
   1522  1.1  christos 	default:
   1523  1.1  christos 		error = ieee80211_ioctl(ifp, cmd, data);
   1524  1.1  christos 	}
   1525  1.1  christos 
   1526  1.1  christos 	if (error == ENETRESET) {
   1527  1.1  christos 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1528  1.1  christos 		    (IFF_UP | IFF_RUNNING))
   1529  1.1  christos 			otus_init(ifp);
   1530  1.1  christos 		error = 0;
   1531  1.1  christos 	}
   1532  1.1  christos 
   1533  1.1  christos 	splx(s);
   1534  1.1  christos 	return error;
   1535  1.1  christos }
   1536  1.1  christos 
   1537  1.1  christos int
   1538  1.1  christos otus_set_multi(struct otus_softc *sc)
   1539  1.1  christos {
   1540  1.1  christos 	struct arpcom *ac = &sc->sc_ic.ic_ac;
   1541  1.1  christos 	struct ifnet *ifp = &ac->ac_if;
   1542  1.1  christos 	struct ether_multi *enm;
   1543  1.1  christos 	struct ether_multistep step;
   1544  1.1  christos 	uint32_t lo, hi;
   1545  1.1  christos 	uint8_t bit;
   1546  1.1  christos 
   1547  1.1  christos 	if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
   1548  1.1  christos 		lo = hi = 0xffffffff;
   1549  1.1  christos 		goto done;
   1550  1.1  christos 	}
   1551  1.1  christos 	lo = hi = 0;
   1552  1.1  christos 	ETHER_FIRST_MULTI(step, ac, enm);
   1553  1.1  christos 	while (enm != NULL) {
   1554  1.1  christos 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1555  1.1  christos 			ifp->if_flags |= IFF_ALLMULTI;
   1556  1.1  christos 			lo = hi = 0xffffffff;
   1557  1.1  christos 			goto done;
   1558  1.1  christos 		}
   1559  1.1  christos 		bit = enm->enm_addrlo[5] >> 2;
   1560  1.1  christos 		if (bit < 32)
   1561  1.1  christos 			lo |= 1 << bit;
   1562  1.1  christos 		else
   1563  1.1  christos 			hi |= 1 << (bit - 32);
   1564  1.1  christos 		ETHER_NEXT_MULTI(step, enm);
   1565  1.1  christos 	}
   1566  1.1  christos  done:
   1567  1.1  christos 	hi |= 1 << 31;	/* Make sure the broadcast bit is set. */
   1568  1.1  christos 	otus_write(sc, AR_MAC_REG_GROUP_HASH_TBL_L, lo);
   1569  1.1  christos 	otus_write(sc, AR_MAC_REG_GROUP_HASH_TBL_H, hi);
   1570  1.1  christos 	return otus_write_barrier(sc);
   1571  1.1  christos }
   1572  1.1  christos 
   1573  1.1  christos void
   1574  1.1  christos otus_updateedca(struct ieee80211com *ic)
   1575  1.1  christos {
   1576  1.1  christos 	/* Do it in a process context. */
   1577  1.1  christos 	otus_do_async(ic->ic_softc, otus_updateedca_cb, NULL, 0);
   1578  1.1  christos }
   1579  1.1  christos 
   1580  1.1  christos /* ARGSUSED */
   1581  1.1  christos void
   1582  1.1  christos otus_updateedca_cb(struct otus_softc *sc, void *arg)
   1583  1.1  christos {
   1584  1.1  christos #define EXP2(val)	((1 << (val)) - 1)
   1585  1.1  christos #define AIFS(val)	((val) * 9 + 10)
   1586  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1587  1.1  christos 	const struct ieee80211_edca_ac_params *edca;
   1588  1.1  christos 	int s;
   1589  1.1  christos 
   1590  1.1  christos 	s = splnet();
   1591  1.1  christos 
   1592  1.1  christos 	edca = (ic->ic_flags & IEEE80211_F_QOS) ?
   1593  1.1  christos 	    ic->ic_edca_ac : otus_edca_def;
   1594  1.1  christos 
   1595  1.1  christos 	/* Set CWmin/CWmax values. */
   1596  1.1  christos 	otus_write(sc, AR_MAC_REG_AC0_CW,
   1597  1.1  christos 	    EXP2(edca[EDCA_AC_BE].ac_ecwmax) << 16 |
   1598  1.1  christos 	    EXP2(edca[EDCA_AC_BE].ac_ecwmin));
   1599  1.1  christos 	otus_write(sc, AR_MAC_REG_AC1_CW,
   1600  1.1  christos 	    EXP2(edca[EDCA_AC_BK].ac_ecwmax) << 16 |
   1601  1.1  christos 	    EXP2(edca[EDCA_AC_BK].ac_ecwmin));
   1602  1.1  christos 	otus_write(sc, AR_MAC_REG_AC2_CW,
   1603  1.1  christos 	    EXP2(edca[EDCA_AC_VI].ac_ecwmax) << 16 |
   1604  1.1  christos 	    EXP2(edca[EDCA_AC_VI].ac_ecwmin));
   1605  1.1  christos 	otus_write(sc, AR_MAC_REG_AC3_CW,
   1606  1.1  christos 	    EXP2(edca[EDCA_AC_VO].ac_ecwmax) << 16 |
   1607  1.1  christos 	    EXP2(edca[EDCA_AC_VO].ac_ecwmin));
   1608  1.1  christos 	otus_write(sc, AR_MAC_REG_AC4_CW,		/* Special TXQ. */
   1609  1.1  christos 	    EXP2(edca[EDCA_AC_VO].ac_ecwmax) << 16 |
   1610  1.1  christos 	    EXP2(edca[EDCA_AC_VO].ac_ecwmin));
   1611  1.1  christos 
   1612  1.1  christos 	/* Set AIFSN values. */
   1613  1.1  christos 	otus_write(sc, AR_MAC_REG_AC1_AC0_AIFS,
   1614  1.1  christos 	    AIFS(edca[EDCA_AC_VI].ac_aifsn) << 24 |
   1615  1.1  christos 	    AIFS(edca[EDCA_AC_BK].ac_aifsn) << 12 |
   1616  1.1  christos 	    AIFS(edca[EDCA_AC_BE].ac_aifsn));
   1617  1.1  christos 	otus_write(sc, AR_MAC_REG_AC3_AC2_AIFS,
   1618  1.1  christos 	    AIFS(edca[EDCA_AC_VO].ac_aifsn) << 16 |	/* Special TXQ. */
   1619  1.1  christos 	    AIFS(edca[EDCA_AC_VO].ac_aifsn) <<  4 |
   1620  1.1  christos 	    AIFS(edca[EDCA_AC_VI].ac_aifsn) >>  8);
   1621  1.1  christos 
   1622  1.1  christos 	/* Set TXOP limit. */
   1623  1.1  christos 	otus_write(sc, AR_MAC_REG_AC1_AC0_TXOP,
   1624  1.1  christos 	    edca[EDCA_AC_BK].ac_txoplimit << 16 |
   1625  1.1  christos 	    edca[EDCA_AC_BE].ac_txoplimit);
   1626  1.1  christos 	otus_write(sc, AR_MAC_REG_AC3_AC2_TXOP,
   1627  1.1  christos 	    edca[EDCA_AC_VO].ac_txoplimit << 16 |
   1628  1.1  christos 	    edca[EDCA_AC_VI].ac_txoplimit);
   1629  1.1  christos 
   1630  1.1  christos 	splx(s);
   1631  1.1  christos 
   1632  1.1  christos 	(void)otus_write_barrier(sc);
   1633  1.1  christos #undef AIFS
   1634  1.1  christos #undef EXP2
   1635  1.1  christos }
   1636  1.1  christos 
   1637  1.1  christos void
   1638  1.1  christos otus_updateslot(struct ieee80211com *ic)
   1639  1.1  christos {
   1640  1.1  christos 	/* Do it in a process context. */
   1641  1.1  christos 	otus_do_async(ic->ic_softc, otus_updateslot_cb, NULL, 0);
   1642  1.1  christos }
   1643  1.1  christos 
   1644  1.1  christos /* ARGSUSED */
   1645  1.1  christos void
   1646  1.1  christos otus_updateslot_cb(struct otus_softc *sc, void *arg)
   1647  1.1  christos {
   1648  1.1  christos 	uint32_t slottime;
   1649  1.1  christos 
   1650  1.1  christos 	slottime = (sc->sc_ic.ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
   1651  1.1  christos 	otus_write(sc, AR_MAC_REG_SLOT_TIME, slottime << 10);
   1652  1.1  christos 	(void)otus_write_barrier(sc);
   1653  1.1  christos }
   1654  1.1  christos 
   1655  1.1  christos int
   1656  1.1  christos otus_init_mac(struct otus_softc *sc)
   1657  1.1  christos {
   1658  1.1  christos 	int error;
   1659  1.1  christos 
   1660  1.1  christos 	otus_write(sc, AR_MAC_REG_ACK_EXTENSION, 0x40);
   1661  1.1  christos 	otus_write(sc, AR_MAC_REG_RETRY_MAX, 0);
   1662  1.1  christos 	otus_write(sc, AR_MAC_REG_SNIFFER, 0x2000000);
   1663  1.1  christos 	otus_write(sc, AR_MAC_REG_RX_THRESHOLD, 0xc1f80);
   1664  1.1  christos 	otus_write(sc, AR_MAC_REG_RX_PE_DELAY, 0x70);
   1665  1.1  christos 	otus_write(sc, AR_MAC_REG_EIFS_AND_SIFS, 0xa144000);
   1666  1.1  christos 	otus_write(sc, AR_MAC_REG_SLOT_TIME, 9 << 10);
   1667  1.1  christos 	otus_write(sc, 0x1c3b2c, 0x19000000);
   1668  1.1  christos 	/* NAV protects ACK only (in TXOP). */
   1669  1.1  christos 	otus_write(sc, 0x1c3b38, 0x201);
   1670  1.1  christos 	/* Set beacon Tx power to 0x7. */
   1671  1.1  christos 	otus_write(sc, AR_MAC_REG_BCN_HT1, 0x8000170);
   1672  1.1  christos 	otus_write(sc, AR_MAC_REG_BACKOFF_PROTECT, 0x105);
   1673  1.1  christos 	otus_write(sc, 0x1c3b9c, 0x10000a);
   1674  1.1  christos 	/* Filter any control frames, BAR is bit 24. */
   1675  1.1  christos 	otus_write(sc, 0x1c368c, 0x0500ffff);
   1676  1.1  christos 	otus_write(sc, 0x1c3c40, 0x1);
   1677  1.1  christos 	otus_write(sc, AR_MAC_REG_BASIC_RATE, 0x150f);
   1678  1.1  christos 	otus_write(sc, AR_MAC_REG_MANDATORY_RATE, 0x150f);
   1679  1.1  christos 	otus_write(sc, AR_MAC_REG_RTS_CTS_RATE, 0x10b01bb);
   1680  1.1  christos 	otus_write(sc, 0x1c3694, 0x4003c1e);
   1681  1.1  christos 	/* Enable LED0 and LED1. */
   1682  1.1  christos 	otus_write(sc, 0x1d0100, 0x3);
   1683  1.1  christos 	otus_write(sc, 0x1d0104, 0x3);
   1684  1.1  christos 	/* Switch MAC to OTUS interface. */
   1685  1.1  christos 	otus_write(sc, 0x1c3600, 0x3);
   1686  1.1  christos 	otus_write(sc, 0x1c3c50, 0xffff);
   1687  1.1  christos 	otus_write(sc, 0x1c3680, 0xf00008);
   1688  1.1  christos 	/* Disable Rx timeout (workaround). */
   1689  1.1  christos 	otus_write(sc, 0x1c362c, 0);
   1690  1.1  christos 
   1691  1.1  christos 	/* Set USB Rx stream mode maximum frame number to 2. */
   1692  1.1  christos 	otus_write(sc, 0x1e1110, 0x4);
   1693  1.1  christos 	/* Set USB Rx stream mode timeout to 10us. */
   1694  1.1  christos 	otus_write(sc, 0x1e1114, 0x80);
   1695  1.1  christos 
   1696  1.1  christos 	/* Set clock frequency to 88/80MHz. */
   1697  1.1  christos 	otus_write(sc, 0x1d4008, 0x73);
   1698  1.1  christos 	/* Set WLAN DMA interrupt mode: generate intr per packet. */
   1699  1.1  christos 	otus_write(sc, 0x1c3d7c, 0x110011);
   1700  1.1  christos 	otus_write(sc, 0x1c3bb0, 0x4);
   1701  1.1  christos 	otus_write(sc, AR_MAC_REG_TXOP_NOT_ENOUGH_INDICATION, 0x141e0f48);
   1702  1.1  christos 
   1703  1.1  christos 	/* Disable HW decryption for now. */
   1704  1.1  christos 	otus_write(sc, 0x1c3678, 0x78);
   1705  1.1  christos 
   1706  1.1  christos 	if ((error = otus_write_barrier(sc)) != 0)
   1707  1.1  christos 		return error;
   1708  1.1  christos 
   1709  1.1  christos 	/* Set default EDCA parameters. */
   1710  1.1  christos 	otus_updateedca_cb(sc, NULL);
   1711  1.1  christos 
   1712  1.1  christos 	return 0;
   1713  1.1  christos }
   1714  1.1  christos 
   1715  1.1  christos /*
   1716  1.1  christos  * Return default value for PHY register based on current operating mode.
   1717  1.1  christos  */
   1718  1.1  christos uint32_t
   1719  1.1  christos otus_phy_get_def(struct otus_softc *sc, uint32_t reg)
   1720  1.1  christos {
   1721  1.1  christos 	int i;
   1722  1.1  christos 
   1723  1.1  christos 	for (i = 0; i < nitems(ar5416_phy_regs); i++)
   1724  1.1  christos 		if (AR_PHY(ar5416_phy_regs[i]) == reg)
   1725  1.1  christos 			return sc->phy_vals[i];
   1726  1.1  christos 	return 0;	/* Register not found. */
   1727  1.1  christos }
   1728  1.1  christos 
   1729  1.1  christos /*
   1730  1.1  christos  * Update PHY's programming based on vendor-specific data stored in EEPROM.
   1731  1.1  christos  * This is for FEM-type devices only.
   1732  1.1  christos  */
   1733  1.1  christos int
   1734  1.1  christos otus_set_board_values(struct otus_softc *sc, struct ieee80211_channel *c)
   1735  1.1  christos {
   1736  1.1  christos 	const struct ModalEepHeader *eep;
   1737  1.1  christos 	uint32_t tmp, offset;
   1738  1.1  christos 
   1739  1.1  christos 	if (IEEE80211_IS_CHAN_5GHZ(c))
   1740  1.1  christos 		eep = &sc->eeprom.modalHeader[0];
   1741  1.1  christos 	else
   1742  1.1  christos 		eep = &sc->eeprom.modalHeader[1];
   1743  1.1  christos 
   1744  1.1  christos 	/* Offset of chain 2. */
   1745  1.1  christos 	offset = 2 * 0x1000;
   1746  1.1  christos 
   1747  1.1  christos 	tmp = letoh32(eep->antCtrlCommon);
   1748  1.1  christos 	otus_write(sc, AR_PHY_SWITCH_COM, tmp);
   1749  1.1  christos 
   1750  1.1  christos 	tmp = letoh32(eep->antCtrlChain[0]);
   1751  1.1  christos 	otus_write(sc, AR_PHY_SWITCH_CHAIN_0, tmp);
   1752  1.1  christos 
   1753  1.1  christos 	tmp = letoh32(eep->antCtrlChain[1]);
   1754  1.1  christos 	otus_write(sc, AR_PHY_SWITCH_CHAIN_0 + offset, tmp);
   1755  1.1  christos 
   1756  1.1  christos 	if (1 /* sc->sc_sco == AR_SCO_SCN */) {
   1757  1.1  christos 		tmp = otus_phy_get_def(sc, AR_PHY_SETTLING);
   1758  1.1  christos 		tmp &= ~(0x7f << 7);
   1759  1.1  christos 		tmp |= (eep->switchSettling & 0x7f) << 7;
   1760  1.1  christos 		otus_write(sc, AR_PHY_SETTLING, tmp);
   1761  1.1  christos 	}
   1762  1.1  christos 
   1763  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_DESIRED_SZ);
   1764  1.1  christos 	tmp &= ~0xffff;
   1765  1.1  christos 	tmp |= eep->pgaDesiredSize << 8 | eep->adcDesiredSize;
   1766  1.1  christos 	otus_write(sc, AR_PHY_DESIRED_SZ, tmp);
   1767  1.1  christos 
   1768  1.1  christos 	tmp = eep->txEndToXpaOff << 24 | eep->txEndToXpaOff << 16 |
   1769  1.1  christos 	      eep->txFrameToXpaOn << 8 | eep->txFrameToXpaOn;
   1770  1.1  christos 	otus_write(sc, AR_PHY_RF_CTL4, tmp);
   1771  1.1  christos 
   1772  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_RF_CTL3);
   1773  1.1  christos 	tmp &= ~(0xff << 16);
   1774  1.1  christos 	tmp |= eep->txEndToRxOn << 16;
   1775  1.1  christos 	otus_write(sc, AR_PHY_RF_CTL3, tmp);
   1776  1.1  christos 
   1777  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_CCA);
   1778  1.1  christos 	tmp &= ~(0x7f << 12);
   1779  1.1  christos 	tmp |= (eep->thresh62 & 0x7f) << 12;
   1780  1.1  christos 	otus_write(sc, AR_PHY_CCA, tmp);
   1781  1.1  christos 
   1782  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_RXGAIN);
   1783  1.1  christos 	tmp &= ~(0x3f << 12);
   1784  1.1  christos 	tmp |= (eep->txRxAttenCh[0] & 0x3f) << 12;
   1785  1.1  christos 	otus_write(sc, AR_PHY_RXGAIN, tmp);
   1786  1.1  christos 
   1787  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_RXGAIN + offset);
   1788  1.1  christos 	tmp &= ~(0x3f << 12);
   1789  1.1  christos 	tmp |= (eep->txRxAttenCh[1] & 0x3f) << 12;
   1790  1.1  christos 	otus_write(sc, AR_PHY_RXGAIN + offset, tmp);
   1791  1.1  christos 
   1792  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_GAIN_2GHZ);
   1793  1.1  christos 	tmp &= ~(0x3f << 18);
   1794  1.1  christos 	tmp |= (eep->rxTxMarginCh[0] & 0x3f) << 18;
   1795  1.1  christos 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
   1796  1.1  christos 		tmp &= ~(0xf << 10);
   1797  1.1  christos 		tmp |= (eep->bswMargin[0] & 0xf) << 10;
   1798  1.1  christos 	}
   1799  1.1  christos 	otus_write(sc, AR_PHY_GAIN_2GHZ, tmp);
   1800  1.1  christos 
   1801  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_GAIN_2GHZ + offset);
   1802  1.1  christos 	tmp &= ~(0x3f << 18);
   1803  1.1  christos 	tmp |= (eep->rxTxMarginCh[1] & 0x3f) << 18;
   1804  1.1  christos 	otus_write(sc, AR_PHY_GAIN_2GHZ + offset, tmp);
   1805  1.1  christos 
   1806  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_TIMING_CTRL4);
   1807  1.1  christos 	tmp &= ~(0x3f << 5 | 0x1f);
   1808  1.1  christos 	tmp |= (eep->iqCalICh[0] & 0x3f) << 5 | (eep->iqCalQCh[0] & 0x1f);
   1809  1.1  christos 	otus_write(sc, AR_PHY_TIMING_CTRL4, tmp);
   1810  1.1  christos 
   1811  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_TIMING_CTRL4 + offset);
   1812  1.1  christos 	tmp &= ~(0x3f << 5 | 0x1f);
   1813  1.1  christos 	tmp |= (eep->iqCalICh[1] & 0x3f) << 5 | (eep->iqCalQCh[1] & 0x1f);
   1814  1.1  christos 	otus_write(sc, AR_PHY_TIMING_CTRL4 + offset, tmp);
   1815  1.1  christos 
   1816  1.1  christos 	tmp = otus_phy_get_def(sc, AR_PHY_TPCRG1);
   1817  1.1  christos 	tmp &= ~(0xf << 16);
   1818  1.1  christos 	tmp |= (eep->xpd & 0xf) << 16;
   1819  1.1  christos 	otus_write(sc, AR_PHY_TPCRG1, tmp);
   1820  1.1  christos 
   1821  1.1  christos 	return otus_write_barrier(sc);
   1822  1.1  christos }
   1823  1.1  christos 
   1824  1.1  christos int
   1825  1.1  christos otus_program_phy(struct otus_softc *sc, struct ieee80211_channel *c)
   1826  1.1  christos {
   1827  1.1  christos 	const uint32_t *vals;
   1828  1.1  christos 	int error, i;
   1829  1.1  christos 
   1830  1.1  christos 	/* Select PHY programming based on band and bandwidth. */
   1831  1.1  christos 	if (IEEE80211_IS_CHAN_2GHZ(c))
   1832  1.1  christos 		vals = ar5416_phy_vals_2ghz_20mhz;
   1833  1.1  christos 	else
   1834  1.1  christos 		vals = ar5416_phy_vals_5ghz_20mhz;
   1835  1.1  christos 	for (i = 0; i < nitems(ar5416_phy_regs); i++)
   1836  1.1  christos 		otus_write(sc, AR_PHY(ar5416_phy_regs[i]), vals[i]);
   1837  1.1  christos 	sc->phy_vals = vals;
   1838  1.1  christos 
   1839  1.1  christos 	if (sc->eeprom.baseEepHeader.deviceType == 0x80)	/* FEM */
   1840  1.1  christos 		if ((error = otus_set_board_values(sc, c)) != 0)
   1841  1.1  christos 			return error;
   1842  1.1  christos 
   1843  1.1  christos 	/* Initial Tx power settings. */
   1844  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE_MAX, 0x7f);
   1845  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE1, 0x3f3f3f3f);
   1846  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE2, 0x3f3f3f3f);
   1847  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE3, 0x3f3f3f3f);
   1848  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE4, 0x3f3f3f3f);
   1849  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE5, 0x3f3f3f3f);
   1850  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE6, 0x3f3f3f3f);
   1851  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE7, 0x3f3f3f3f);
   1852  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE8, 0x3f3f3f3f);
   1853  1.1  christos 	otus_write(sc, AR_PHY_POWER_TX_RATE9, 0x3f3f3f3f);
   1854  1.1  christos 
   1855  1.1  christos 	if (IEEE80211_IS_CHAN_2GHZ(c))
   1856  1.1  christos 		otus_write(sc, 0x1d4014, 0x5163);
   1857  1.1  christos 	else
   1858  1.1  christos 		otus_write(sc, 0x1d4014, 0x5143);
   1859  1.1  christos 
   1860  1.1  christos 	return otus_write_barrier(sc);
   1861  1.1  christos }
   1862  1.1  christos 
   1863  1.1  christos static __inline uint8_t
   1864  1.1  christos otus_reverse_bits(uint8_t v)
   1865  1.1  christos {
   1866  1.1  christos 	v = ((v >> 1) & 0x55) | ((v & 0x55) << 1);
   1867  1.1  christos 	v = ((v >> 2) & 0x33) | ((v & 0x33) << 2);
   1868  1.1  christos 	v = ((v >> 4) & 0x0f) | ((v & 0x0f) << 4);
   1869  1.1  christos 	return v;
   1870  1.1  christos }
   1871  1.1  christos 
   1872  1.1  christos int
   1873  1.1  christos otus_set_rf_bank4(struct otus_softc *sc, struct ieee80211_channel *c)
   1874  1.1  christos {
   1875  1.1  christos 	uint8_t chansel, d0, d1;
   1876  1.1  christos 	uint16_t data;
   1877  1.1  christos 	int error;
   1878  1.1  christos 
   1879  1.1  christos 	d0 = 0;
   1880  1.1  christos 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
   1881  1.1  christos 		chansel = (c->ic_freq - 4800) / 5;
   1882  1.1  christos 		if (chansel & 1)
   1883  1.1  christos 			d0 |= AR_BANK4_AMODE_REFSEL(2);
   1884  1.1  christos 		else
   1885  1.1  christos 			d0 |= AR_BANK4_AMODE_REFSEL(1);
   1886  1.1  christos 	} else {
   1887  1.1  christos 		d0 |= AR_BANK4_AMODE_REFSEL(2);
   1888  1.1  christos 		if (c->ic_freq == 2484) {	/* CH 14 */
   1889  1.1  christos 			d0 |= AR_BANK4_BMODE_LF_SYNTH_FREQ;
   1890  1.1  christos 			chansel = 10 + (c->ic_freq - 2274) / 5;
   1891  1.1  christos 		} else
   1892  1.1  christos 			chansel = 16 + (c->ic_freq - 2272) / 5;
   1893  1.1  christos 		chansel <<= 2;
   1894  1.1  christos 	}
   1895  1.1  christos 	d0 |= AR_BANK4_ADDR(1) | AR_BANK4_CHUP;
   1896  1.1  christos 	d1 = otus_reverse_bits(chansel);
   1897  1.1  christos 
   1898  1.1  christos 	/* Write bits 0-4 of d0 and d1. */
   1899  1.1  christos 	data = (d1 & 0x1f) << 5 | (d0 & 0x1f);
   1900  1.1  christos 	otus_write(sc, AR_PHY(44), data);
   1901  1.1  christos 	/* Write bits 5-7 of d0 and d1. */
   1902  1.1  christos 	data = (d1 >> 5) << 5 | (d0 >> 5);
   1903  1.1  christos 	otus_write(sc, AR_PHY(58), data);
   1904  1.1  christos 
   1905  1.1  christos 	if ((error = otus_write_barrier(sc)) == 0)
   1906  1.1  christos 		usbd_delay_ms(sc->sc_udev, 10);
   1907  1.1  christos 	return error;
   1908  1.1  christos }
   1909  1.1  christos 
   1910  1.1  christos void
   1911  1.1  christos otus_get_delta_slope(uint32_t coeff, uint32_t *exponent, uint32_t *mantissa)
   1912  1.1  christos {
   1913  1.1  christos #define COEFF_SCALE_SHIFT	24
   1914  1.1  christos 	uint32_t exp, man;
   1915  1.1  christos 
   1916  1.1  christos 	/* exponent = 14 - floor(log2(coeff)) */
   1917  1.1  christos 	for (exp = 31; exp > 0; exp--)
   1918  1.1  christos 		if (coeff & (1 << exp))
   1919  1.1  christos 			break;
   1920  1.1  christos 	KASSERT(exp != 0);
   1921  1.1  christos 	exp = 14 - (exp - COEFF_SCALE_SHIFT);
   1922  1.1  christos 
   1923  1.1  christos 	/* mantissa = floor(coeff * 2^exponent + 0.5) */
   1924  1.1  christos 	man = coeff + (1 << (COEFF_SCALE_SHIFT - exp - 1));
   1925  1.1  christos 
   1926  1.1  christos 	*mantissa = man >> (COEFF_SCALE_SHIFT - exp);
   1927  1.1  christos 	*exponent = exp - 16;
   1928  1.1  christos #undef COEFF_SCALE_SHIFT
   1929  1.1  christos }
   1930  1.1  christos 
   1931  1.1  christos int
   1932  1.1  christos otus_set_chan(struct otus_softc *sc, struct ieee80211_channel *c, int assoc)
   1933  1.1  christos {
   1934  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1935  1.1  christos 	struct ar_cmd_frequency cmd;
   1936  1.1  christos 	struct ar_rsp_frequency rsp;
   1937  1.1  christos 	const uint32_t *vals;
   1938  1.1  christos 	uint32_t coeff, exp, man, tmp;
   1939  1.1  christos 	uint8_t code;
   1940  1.1  christos 	int error, chan, i;
   1941  1.1  christos 
   1942  1.1  christos 	chan = ieee80211_chan2ieee(ic, c);
   1943  1.1  christos 	DPRINTF(("setting channel %d (%dMHz)\n", chan, c->ic_freq));
   1944  1.1  christos 
   1945  1.1  christos 	tmp = IEEE80211_IS_CHAN_2GHZ(c) ? 0x105 : 0x104;
   1946  1.1  christos 	otus_write(sc, AR_MAC_REG_DYNAMIC_SIFS_ACK, tmp);
   1947  1.1  christos 	if ((error = otus_write_barrier(sc)) != 0)
   1948  1.1  christos 		return error;
   1949  1.1  christos 
   1950  1.1  christos 	/* Disable BB Heavy Clip. */
   1951  1.1  christos 	otus_write(sc, AR_PHY_HEAVY_CLIP_ENABLE, 0x200);
   1952  1.1  christos 	if ((error = otus_write_barrier(sc)) != 0)
   1953  1.1  christos 		return error;
   1954  1.1  christos 
   1955  1.1  christos 	/* XXX Is that FREQ_START ? */
   1956  1.1  christos 	error = otus_cmd(sc, AR_CMD_FREQ_STRAT, NULL, 0, NULL);
   1957  1.1  christos 	if (error != 0)
   1958  1.1  christos 		return error;
   1959  1.1  christos 
   1960  1.1  christos 	/* Reprogram PHY and RF on channel band or bandwidth changes. */
   1961  1.1  christos 	if (sc->bb_reset || c->ic_flags != sc->sc_curchan->ic_flags) {
   1962  1.1  christos 		DPRINTF(("band switch\n"));
   1963  1.1  christos 
   1964  1.1  christos 		/* Cold/Warm reset BB/ADDA. */
   1965  1.1  christos 		otus_write(sc, 0x1d4004, sc->bb_reset ? 0x800 : 0x400);
   1966  1.1  christos 		if ((error = otus_write_barrier(sc)) != 0)
   1967  1.1  christos 			return error;
   1968  1.1  christos 		otus_write(sc, 0x1d4004, 0);
   1969  1.1  christos 		if ((error = otus_write_barrier(sc)) != 0)
   1970  1.1  christos 			return error;
   1971  1.1  christos 		sc->bb_reset = 0;
   1972  1.1  christos 
   1973  1.1  christos 		if ((error = otus_program_phy(sc, c)) != 0) {
   1974  1.1  christos 			printf("%s: could not program PHY\n",
   1975  1.1  christos 			    sc->sc_dev.dv_xname);
   1976  1.1  christos 			return error;
   1977  1.1  christos 		}
   1978  1.1  christos 
   1979  1.1  christos 		/* Select RF programming based on band. */
   1980  1.1  christos 		if (IEEE80211_IS_CHAN_5GHZ(c))
   1981  1.1  christos 			vals = ar5416_banks_vals_5ghz;
   1982  1.1  christos 		else
   1983  1.1  christos 			vals = ar5416_banks_vals_2ghz;
   1984  1.1  christos 		for (i = 0; i < nitems(ar5416_banks_regs); i++)
   1985  1.1  christos 			otus_write(sc, AR_PHY(ar5416_banks_regs[i]), vals[i]);
   1986  1.1  christos 		if ((error = otus_write_barrier(sc)) != 0) {
   1987  1.1  christos 			printf("%s: could not program RF\n",
   1988  1.1  christos 			    sc->sc_dev.dv_xname);
   1989  1.1  christos 			return error;
   1990  1.1  christos 		}
   1991  1.1  christos 		code = AR_CMD_RF_INIT;
   1992  1.1  christos 	} else {
   1993  1.1  christos 		code = AR_CMD_FREQUENCY;
   1994  1.1  christos 	}
   1995  1.1  christos 
   1996  1.1  christos 	if ((error = otus_set_rf_bank4(sc, c)) != 0)
   1997  1.1  christos 		return error;
   1998  1.1  christos 
   1999  1.1  christos 	tmp = (sc->txmask == 0x5) ? 0x340 : 0x240;
   2000  1.1  christos 	otus_write(sc, AR_PHY_TURBO, tmp);
   2001  1.1  christos 	if ((error = otus_write_barrier(sc)) != 0)
   2002  1.1  christos 		return error;
   2003  1.1  christos 
   2004  1.1  christos 	/* Send firmware command to set channel. */
   2005  1.1  christos 	cmd.freq = htole32((uint32_t)c->ic_freq * 1000);
   2006  1.1  christos 	cmd.dynht2040 = htole32(0);
   2007  1.1  christos 	cmd.htena = htole32(1);
   2008  1.1  christos 	/* Set Delta Slope (exponent and mantissa). */
   2009  1.1  christos 	coeff = (100 << 24) / c->ic_freq;
   2010  1.1  christos 	otus_get_delta_slope(coeff, &exp, &man);
   2011  1.1  christos 	cmd.dsc_exp = htole32(exp);
   2012  1.1  christos 	cmd.dsc_man = htole32(man);
   2013  1.1  christos 	DPRINTF(("ds coeff=%u exp=%u man=%u\n", coeff, exp, man));
   2014  1.1  christos 	/* For Short GI, coeff is 9/10 that of normal coeff. */
   2015  1.1  christos 	coeff = (9 * coeff) / 10;
   2016  1.1  christos 	otus_get_delta_slope(coeff, &exp, &man);
   2017  1.1  christos 	cmd.dsc_shgi_exp = htole32(exp);
   2018  1.1  christos 	cmd.dsc_shgi_man = htole32(man);
   2019  1.1  christos 	DPRINTF(("ds shgi coeff=%u exp=%u man=%u\n", coeff, exp, man));
   2020  1.1  christos 	/* Set wait time for AGC and noise calibration (100 or 200ms). */
   2021  1.1  christos 	cmd.check_loop_count = assoc ? htole32(2000) : htole32(1000);
   2022  1.1  christos 	DPRINTF(("%s\n", (code == AR_CMD_RF_INIT) ? "RF_INIT" : "FREQUENCY"));
   2023  1.1  christos 	error = otus_cmd(sc, code, &cmd, sizeof cmd, &rsp);
   2024  1.1  christos 	if (error != 0)
   2025  1.1  christos 		return error;
   2026  1.1  christos 	if ((rsp.status & htole32(AR_CAL_ERR_AGC | AR_CAL_ERR_NF_VAL)) != 0) {
   2027  1.1  christos 		DPRINTF(("status=0x%x\n", letoh32(rsp.status)));
   2028  1.1  christos 		/* Force cold reset on next channel. */
   2029  1.1  christos 		sc->bb_reset = 1;
   2030  1.1  christos 	}
   2031  1.1  christos #ifdef OTUS_DEBUG
   2032  1.1  christos 	if (otus_debug) {
   2033  1.1  christos 		printf("calibration status=0x%x\n", letoh32(rsp.status));
   2034  1.1  christos 		for (i = 0; i < 2; i++) {	/* 2 Rx chains */
   2035  1.1  christos 			/* Sign-extend 9-bit NF values. */
   2036  1.1  christos 			printf("noisefloor chain %d=%d\n", i,
   2037  1.1  christos 			    (((int32_t)letoh32(rsp.nf[i])) << 4) >> 23);
   2038  1.1  christos 			printf("noisefloor ext chain %d=%d\n", i,
   2039  1.1  christos 			    ((int32_t)letoh32(rsp.nf_ext[i])) >> 23);
   2040  1.1  christos 		}
   2041  1.1  christos 	}
   2042  1.1  christos #endif
   2043  1.1  christos 	sc->sc_curchan = c;
   2044  1.1  christos 	return 0;
   2045  1.1  christos }
   2046  1.1  christos 
   2047  1.1  christos #ifdef notyet
   2048  1.1  christos int
   2049  1.1  christos otus_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   2050  1.1  christos     struct ieee80211_key *k)
   2051  1.1  christos {
   2052  1.1  christos 	struct otus_softc *sc = ic->ic_softc;
   2053  1.1  christos 	struct otus_cmd_key cmd;
   2054  1.1  christos 
   2055  1.1  christos 	/* Defer setting of WEP keys until interface is brought up. */
   2056  1.1  christos 	if ((ic->ic_if.if_flags & (IFF_UP | IFF_RUNNING)) !=
   2057  1.1  christos 	    (IFF_UP | IFF_RUNNING))
   2058  1.1  christos 		return 0;
   2059  1.1  christos 
   2060  1.1  christos 	/* Do it in a process context. */
   2061  1.1  christos 	cmd.key = *k;
   2062  1.1  christos 	cmd.associd = (ni != NULL) ? ni->ni_associd : 0;
   2063  1.1  christos 	otus_do_async(sc, otus_set_key_cb, &cmd, sizeof cmd);
   2064  1.1  christos 	return 0;
   2065  1.1  christos }
   2066  1.1  christos 
   2067  1.1  christos void
   2068  1.1  christos otus_set_key_cb(struct otus_softc *sc, void *arg)
   2069  1.1  christos {
   2070  1.1  christos 	struct otus_cmd_key *cmd = arg;
   2071  1.1  christos 	struct ieee80211_key *k = &cmd->key;
   2072  1.1  christos 	struct ar_cmd_ekey key;
   2073  1.1  christos 	uint16_t cipher;
   2074  1.1  christos 	int error;
   2075  1.1  christos 
   2076  1.1  christos 	memset(&key, 0, sizeof key);
   2077  1.1  christos 	if (k->k_flags & IEEE80211_KEY_GROUP) {
   2078  1.1  christos 		key.uid = htole16(k->k_id);
   2079  1.1  christos 		IEEE80211_ADDR_COPY(key.macaddr, sc->sc_ic.ic_myaddr);
   2080  1.1  christos 		key.macaddr[0] |= 0x80;
   2081  1.1  christos 	} else {
   2082  1.1  christos 		key.uid = htole16(OTUS_UID(cmd->associd));
   2083  1.1  christos 		IEEE80211_ADDR_COPY(key.macaddr, ni->ni_macaddr);
   2084  1.1  christos 	}
   2085  1.1  christos 	key.kix = htole16(0);
   2086  1.1  christos 	/* Map net80211 cipher to hardware. */
   2087  1.1  christos 	switch (k->k_cipher) {
   2088  1.1  christos 	case IEEE80211_CIPHER_WEP40:
   2089  1.1  christos 		cipher = AR_CIPHER_WEP64;
   2090  1.1  christos 		break;
   2091  1.1  christos 	case IEEE80211_CIPHER_WEP104:
   2092  1.1  christos 		cipher = AR_CIPHER_WEP128;
   2093  1.1  christos 		break;
   2094  1.1  christos 	case IEEE80211_CIPHER_TKIP:
   2095  1.1  christos 		cipher = AR_CIPHER_TKIP;
   2096  1.1  christos 		break;
   2097  1.1  christos 	case IEEE80211_CIPHER_CCMP:
   2098  1.1  christos 		cipher = AR_CIPHER_AES;
   2099  1.1  christos 		break;
   2100  1.1  christos 	default:
   2101  1.1  christos 		return;
   2102  1.1  christos 	}
   2103  1.1  christos 	key.cipher = htole16(cipher);
   2104  1.1  christos 	memcpy(key.key, k->k_key, MIN(k->k_len, 16));
   2105  1.1  christos 	error = otus_cmd(sc, AR_CMD_EKEY, &key, sizeof key, NULL);
   2106  1.1  christos 	if (error != 0 || k->k_cipher != IEEE80211_CIPHER_TKIP)
   2107  1.1  christos 		return;
   2108  1.1  christos 
   2109  1.1  christos 	/* TKIP: set Tx/Rx MIC Key. */
   2110  1.1  christos 	key.kix = htole16(1);
   2111  1.1  christos 	memcpy(key.key, k->k_key + 16, 16);
   2112  1.1  christos 	(void)otus_cmd(sc, AR_CMD_EKEY, &key, sizeof key, NULL);
   2113  1.1  christos }
   2114  1.1  christos 
   2115  1.1  christos void
   2116  1.1  christos otus_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   2117  1.1  christos     struct ieee80211_key *k)
   2118  1.1  christos {
   2119  1.1  christos 	struct otus_softc *sc = ic->ic_softc;
   2120  1.1  christos 	struct otus_cmd_key cmd;
   2121  1.1  christos 
   2122  1.1  christos 	if (!(ic->ic_if.if_flags & IFF_RUNNING) ||
   2123  1.1  christos 	    ic->ic_state != IEEE80211_S_RUN)
   2124  1.1  christos 		return;	/* Nothing to do. */
   2125  1.1  christos 
   2126  1.1  christos 	/* Do it in a process context. */
   2127  1.1  christos 	cmd.key = *k;
   2128  1.1  christos 	cmd.associd = (ni != NULL) ? ni->ni_associd : 0;
   2129  1.1  christos 	otus_do_async(sc, otus_delete_key_cb, &cmd, sizeof cmd);
   2130  1.1  christos }
   2131  1.1  christos 
   2132  1.1  christos void
   2133  1.1  christos otus_delete_key_cb(struct otus_softc *sc, void *arg)
   2134  1.1  christos {
   2135  1.1  christos 	struct otus_cmd_key *cmd = arg;
   2136  1.1  christos 	struct ieee80211_key *k = &cmd->key;
   2137  1.1  christos 	uint32_t uid;
   2138  1.1  christos 
   2139  1.1  christos 	if (k->k_flags & IEEE80211_KEY_GROUP)
   2140  1.1  christos 		uid = htole32(k->k_id);
   2141  1.1  christos 	else
   2142  1.1  christos 		uid = htole32(OTUS_UID(cmd->associd));
   2143  1.1  christos 	(void)otus_cmd(sc, AR_CMD_DKEY, &uid, sizeof uid, NULL);
   2144  1.1  christos }
   2145  1.1  christos #endif
   2146  1.1  christos 
   2147  1.1  christos void
   2148  1.1  christos otus_calibrate_to(void *arg)
   2149  1.1  christos {
   2150  1.1  christos 	struct otus_softc *sc = arg;
   2151  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2152  1.1  christos 	struct ieee80211_node *ni;
   2153  1.1  christos 	int s;
   2154  1.1  christos 
   2155  1.1  christos 	s = splnet();
   2156  1.1  christos 	ni = ic->ic_bss;
   2157  1.1  christos 	ieee80211_amrr_choose(&sc->amrr, ni, &((struct otus_node *)ni)->amn);
   2158  1.1  christos 	splx(s);
   2159  1.1  christos 
   2160  1.1  christos 	timeout_add_sec(&sc->calib_to, 1);
   2161  1.1  christos }
   2162  1.1  christos 
   2163  1.1  christos int
   2164  1.1  christos otus_set_bssid(struct otus_softc *sc, const uint8_t *bssid)
   2165  1.1  christos {
   2166  1.1  christos 	otus_write(sc, AR_MAC_REG_BSSID_L,
   2167  1.1  christos 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
   2168  1.1  christos 	otus_write(sc, AR_MAC_REG_BSSID_H,
   2169  1.1  christos 	    bssid[4] | bssid[5] << 8);
   2170  1.1  christos 	return otus_write_barrier(sc);
   2171  1.1  christos }
   2172  1.1  christos 
   2173  1.1  christos int
   2174  1.1  christos otus_set_macaddr(struct otus_softc *sc, const uint8_t *addr)
   2175  1.1  christos {
   2176  1.1  christos 	otus_write(sc, AR_MAC_REG_MAC_ADDR_L,
   2177  1.1  christos 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
   2178  1.1  christos 	otus_write(sc, AR_MAC_REG_MAC_ADDR_H,
   2179  1.1  christos 	    addr[4] | addr[5] << 8);
   2180  1.1  christos 	return otus_write_barrier(sc);
   2181  1.1  christos }
   2182  1.1  christos 
   2183  1.1  christos /* Default single-LED. */
   2184  1.1  christos void
   2185  1.1  christos otus_led_newstate_type1(struct otus_softc *sc)
   2186  1.1  christos {
   2187  1.1  christos 	/* TBD */
   2188  1.1  christos }
   2189  1.1  christos 
   2190  1.1  christos /* NETGEAR, dual-LED. */
   2191  1.1  christos void
   2192  1.1  christos otus_led_newstate_type2(struct otus_softc *sc)
   2193  1.1  christos {
   2194  1.1  christos 	/* TBD */
   2195  1.1  christos }
   2196  1.1  christos 
   2197  1.1  christos /* NETGEAR, single-LED/3 colors (blue, red, purple.) */
   2198  1.1  christos void
   2199  1.1  christos otus_led_newstate_type3(struct otus_softc *sc)
   2200  1.1  christos {
   2201  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2202  1.1  christos 	uint32_t state = sc->led_state;
   2203  1.1  christos 
   2204  1.1  christos 	if (ic->ic_state == IEEE80211_S_INIT) {
   2205  1.1  christos 		state = 0;	/* LED off. */
   2206  1.1  christos 	} else if (ic->ic_state == IEEE80211_S_RUN) {
   2207  1.1  christos 		/* Associated, LED always on. */
   2208  1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan))
   2209  1.1  christos 			state = AR_LED0_ON;	/* 2GHz=>Red. */
   2210  1.1  christos 		else
   2211  1.1  christos 			state = AR_LED1_ON;	/* 5GHz=>Blue. */
   2212  1.1  christos 	} else {
   2213  1.1  christos 		/* Scanning, blink LED. */
   2214  1.1  christos 		state ^= AR_LED0_ON | AR_LED1_ON;
   2215  1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan))
   2216  1.1  christos 			state &= ~AR_LED1_ON;
   2217  1.1  christos 		else
   2218  1.1  christos 			state &= ~AR_LED0_ON;
   2219  1.1  christos 	}
   2220  1.1  christos 	if (state != sc->led_state) {
   2221  1.1  christos 		otus_write(sc, 0x1d0104, state);
   2222  1.1  christos 		if (otus_write_barrier(sc) == 0)
   2223  1.1  christos 			sc->led_state = state;
   2224  1.1  christos 	}
   2225  1.1  christos }
   2226  1.1  christos 
   2227  1.1  christos int
   2228  1.1  christos otus_init(struct ifnet *ifp)
   2229  1.1  christos {
   2230  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
   2231  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2232  1.1  christos 	int error;
   2233  1.1  christos 
   2234  1.1  christos 	/* Init host command ring. */
   2235  1.1  christos 	sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0;
   2236  1.1  christos 
   2237  1.1  christos 	if ((error = otus_init_mac(sc)) != 0) {
   2238  1.1  christos 		printf("%s: could not initialize MAC\n", sc->sc_dev.dv_xname);
   2239  1.1  christos 		return error;
   2240  1.1  christos 	}
   2241  1.1  christos 
   2242  1.1  christos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   2243  1.1  christos 	(void)otus_set_macaddr(sc, ic->ic_myaddr);
   2244  1.1  christos 
   2245  1.1  christos 	switch (ic->ic_opmode) {
   2246  1.1  christos #ifdef notyet
   2247  1.1  christos #ifndef IEEE80211_STA_ONLY
   2248  1.1  christos 	case IEEE80211_M_HOSTAP:
   2249  1.1  christos 		otus_write(sc, 0x1c3700, 0x0f0000a1);
   2250  1.1  christos 		otus_write(sc, 0x1c3c40, 0x1);
   2251  1.1  christos 		break;
   2252  1.1  christos 	case IEEE80211_M_IBSS:
   2253  1.1  christos 		otus_write(sc, 0x1c3700, 0x0f000000);
   2254  1.1  christos 		otus_write(sc, 0x1c3c40, 0x1);
   2255  1.1  christos 		break;
   2256  1.1  christos #endif
   2257  1.1  christos #endif
   2258  1.1  christos 	case IEEE80211_M_STA:
   2259  1.1  christos 		otus_write(sc, 0x1c3700, 0x0f000002);
   2260  1.1  christos 		otus_write(sc, 0x1c3c40, 0x1);
   2261  1.1  christos 		break;
   2262  1.1  christos 	default:
   2263  1.1  christos 		break;
   2264  1.1  christos 	}
   2265  1.1  christos 	otus_write(sc, AR_MAC_REG_SNIFFER,
   2266  1.1  christos 	    (ic->ic_opmode == IEEE80211_M_MONITOR) ? 0x2000001 : 0x2000000);
   2267  1.1  christos 	(void)otus_write_barrier(sc);
   2268  1.1  christos 
   2269  1.1  christos 	sc->bb_reset = 1;	/* Force cold reset. */
   2270  1.1  christos 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   2271  1.1  christos 	if ((error = otus_set_chan(sc, ic->ic_ibss_chan, 0)) != 0) {
   2272  1.1  christos 		printf("%s: could not set channel\n", sc->sc_dev.dv_xname);
   2273  1.1  christos 		return error;
   2274  1.1  christos 	}
   2275  1.1  christos 
   2276  1.1  christos 	/* Start Rx. */
   2277  1.1  christos 	otus_write(sc, 0x1c3d30, 0x100);
   2278  1.1  christos 	(void)otus_write_barrier(sc);
   2279  1.1  christos 
   2280  1.1  christos 	ifp->if_flags &= ~IFF_OACTIVE;
   2281  1.1  christos 	ifp->if_flags |= IFF_RUNNING;
   2282  1.1  christos 
   2283  1.1  christos 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2284  1.1  christos 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2285  1.1  christos 	else
   2286  1.1  christos 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2287  1.1  christos 
   2288  1.1  christos 	return 0;
   2289  1.1  christos }
   2290  1.1  christos 
   2291  1.1  christos void
   2292  1.1  christos otus_stop(struct ifnet *ifp)
   2293  1.1  christos {
   2294  1.1  christos 	struct otus_softc *sc = ifp->if_softc;
   2295  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2296  1.1  christos 	int s;
   2297  1.1  christos 
   2298  1.1  christos 	sc->sc_tx_timer = 0;
   2299  1.1  christos 	ifp->if_timer = 0;
   2300  1.1  christos 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2301  1.1  christos 
   2302  1.1  christos 	timeout_del(&sc->scan_to);
   2303  1.1  christos 	timeout_del(&sc->calib_to);
   2304  1.1  christos 
   2305  1.1  christos 	s = splusb();
   2306  1.1  christos 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2307  1.1  christos 	/* Wait for all queued asynchronous commands to complete. */
   2308  1.1  christos 	while (sc->cmdq.queued > 0)
   2309  1.1  christos 		tsleep(&sc->cmdq, 0, "cmdq", 0);
   2310  1.1  christos 	splx(s);
   2311  1.1  christos 
   2312  1.1  christos 	/* Stop Rx. */
   2313  1.1  christos 	otus_write(sc, 0x1c3d30, 0);
   2314  1.1  christos 	(void)otus_write_barrier(sc);
   2315  1.1  christos 
   2316  1.1  christos 	sc->tx_queued = 0;
   2317  1.1  christos }
   2318