Home | History | Annotate | Line # | Download | only in usb
if_athn_usb.c revision 1.14
      1  1.14     skrll /*	$NetBSD: if_athn_usb.c,v 1.14 2016/12/04 10:12:35 skrll Exp $	*/
      2   1.1  christos /*	$OpenBSD: if_athn_usb.c,v 1.12 2013/01/14 09:50:31 jsing Exp $	*/
      3   1.1  christos 
      4   1.1  christos /*-
      5   1.1  christos  * Copyright (c) 2011 Damien Bergamini <damien.bergamini (at) free.fr>
      6   1.1  christos  *
      7   1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  christos  * copyright notice and this permission notice appear in all copies.
     10   1.1  christos  *
     11   1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  christos  */
     19   1.1  christos 
     20   1.1  christos /*
     21   1.1  christos  * USB front-end for Atheros AR9271 and AR7010 chipsets.
     22   1.1  christos  */
     23   1.1  christos 
     24   1.1  christos #include <sys/cdefs.h>
     25  1.14     skrll __KERNEL_RCSID(0, "$NetBSD: if_athn_usb.c,v 1.14 2016/12/04 10:12:35 skrll Exp $");
     26   1.1  christos 
     27   1.1  christos #ifdef	_KERNEL_OPT
     28   1.1  christos #include "opt_inet.h"
     29   1.1  christos #endif
     30   1.1  christos 
     31   1.1  christos #include <sys/param.h>
     32   1.1  christos #include <sys/callout.h>
     33   1.1  christos #include <sys/conf.h>
     34   1.1  christos #include <sys/device.h>
     35   1.1  christos #include <sys/kernel.h>
     36   1.1  christos #include <sys/mbuf.h>
     37   1.1  christos #include <sys/module.h>
     38   1.1  christos #include <sys/proc.h>
     39   1.1  christos #include <sys/socket.h>
     40   1.1  christos #include <sys/sockio.h>
     41   1.1  christos #include <sys/systm.h>
     42   1.9     skrll #include <sys/kmem.h>
     43   1.1  christos 
     44   1.1  christos #include <sys/bus.h>
     45   1.1  christos #include <sys/endian.h>
     46   1.1  christos #include <sys/intr.h>
     47   1.1  christos 
     48   1.1  christos #include <net/bpf.h>
     49   1.1  christos #include <net/if.h>
     50   1.1  christos #include <net/if_arp.h>
     51   1.1  christos #include <net/if_dl.h>
     52   1.1  christos #include <net/if_ether.h>
     53   1.1  christos #include <net/if_media.h>
     54   1.1  christos #include <net/if_types.h>
     55   1.1  christos 
     56   1.1  christos #include <netinet/if_inarp.h>
     57   1.1  christos #include <netinet/in.h>
     58   1.1  christos #include <netinet/in_systm.h>
     59   1.1  christos #include <netinet/in_var.h>
     60   1.1  christos #include <netinet/ip.h>
     61   1.1  christos 
     62   1.1  christos #include <net80211/ieee80211_var.h>
     63   1.1  christos #include <net80211/ieee80211_amrr.h>
     64   1.1  christos #include <net80211/ieee80211_radiotap.h>
     65   1.1  christos 
     66   1.1  christos #include <dev/firmload.h>
     67   1.1  christos 
     68   1.1  christos #include <dev/usb/usb.h>
     69   1.1  christos #include <dev/usb/usbdevs.h>
     70   1.1  christos #include <dev/usb/usbdi.h>
     71   1.1  christos #include <dev/usb/usbdi_util.h>
     72   1.1  christos 
     73   1.1  christos #include <dev/ic/athnreg.h>
     74   1.1  christos #include <dev/ic/athnvar.h>
     75   1.1  christos #include <dev/ic/arn9285.h>
     76   1.1  christos #include <dev/usb/if_athn_usb.h>
     77   1.1  christos 
     78   1.1  christos #define ATHN_USB_SOFTC(sc)	((struct athn_usb_softc *)(sc))
     79   1.1  christos #define ATHN_USB_NODE(ni)	((struct athn_usb_node *)(ni))
     80   1.1  christos 
     81   1.1  christos #define IS_UP_AND_RUNNING(ifp) \
     82   1.1  christos 	(((ifp)->if_flags & IFF_UP) && ((ifp)->if_flags & IFF_RUNNING))
     83   1.1  christos 
     84   1.1  christos #define athn_usb_wmi_cmd(sc, cmd_id) \
     85   1.1  christos 	athn_usb_wmi_xcmd(sc, cmd_id, NULL, 0, NULL)
     86   1.1  christos 
     87   1.1  christos Static int	athn_usb_activate(device_t, enum devact);
     88   1.1  christos Static int	athn_usb_detach(device_t, int);
     89   1.1  christos Static int	athn_usb_match(device_t, cfdata_t, void *);
     90   1.1  christos Static void	athn_usb_attach(device_t, device_t, void *);
     91   1.1  christos 
     92   1.1  christos CFATTACH_DECL_NEW(athn_usb, sizeof(struct athn_usb_softc), athn_usb_match,
     93   1.1  christos     athn_usb_attach, athn_usb_detach, athn_usb_activate);
     94   1.1  christos 
     95   1.1  christos Static int	athn_usb_alloc_rx_list(struct athn_usb_softc *);
     96   1.1  christos Static int	athn_usb_alloc_tx_cmd(struct athn_usb_softc *);
     97   1.1  christos Static int	athn_usb_alloc_tx_list(struct athn_usb_softc *);
     98   1.1  christos Static void	athn_usb_attachhook(device_t);
     99   1.9     skrll Static void	athn_usb_bcneof(struct usbd_xfer *, void *,
    100   1.1  christos 		    usbd_status);
    101   1.9     skrll Static void	athn_usb_abort_pipes(struct athn_usb_softc *);
    102   1.1  christos Static void	athn_usb_close_pipes(struct athn_usb_softc *);
    103   1.1  christos Static int	athn_usb_create_hw_node(struct athn_usb_softc *,
    104   1.1  christos 		    struct ar_htc_target_sta *);
    105   1.1  christos Static int	athn_usb_create_node(struct athn_usb_softc *,
    106   1.1  christos 		    struct ieee80211_node *);
    107   1.1  christos Static void	athn_usb_do_async(struct athn_usb_softc *,
    108   1.1  christos 		    void (*)(struct athn_usb_softc *, void *), void *, int);
    109   1.1  christos Static void	athn_usb_free_rx_list(struct athn_usb_softc *);
    110   1.1  christos Static void	athn_usb_free_tx_cmd(struct athn_usb_softc *);
    111   1.1  christos Static void	athn_usb_free_tx_list(struct athn_usb_softc *);
    112   1.1  christos Static int	athn_usb_htc_connect_svc(struct athn_usb_softc *, uint16_t,
    113   1.1  christos 		    uint8_t, uint8_t, uint8_t *);
    114   1.1  christos Static int	athn_usb_htc_msg(struct athn_usb_softc *, uint16_t, void *,
    115   1.1  christos 		    int);
    116   1.1  christos Static int	athn_usb_htc_setup(struct athn_usb_softc *);
    117   1.1  christos Static int	athn_usb_init(struct ifnet *);
    118   1.9     skrll Static void	athn_usb_intr(struct usbd_xfer *, void *,
    119   1.1  christos 		    usbd_status);
    120   1.1  christos Static int	athn_usb_ioctl(struct ifnet *, u_long, void *);
    121   1.1  christos Static int	athn_usb_load_firmware(struct athn_usb_softc *);
    122   1.1  christos Static const struct athn_usb_type *
    123   1.1  christos 		athn_usb_lookup(int, int);
    124   1.1  christos Static int	athn_usb_media_change(struct ifnet *);
    125   1.1  christos Static void	athn_usb_newassoc(struct ieee80211_node *, int);
    126   1.1  christos Static void	athn_usb_newassoc_cb(struct athn_usb_softc *, void *);
    127   1.1  christos Static int	athn_usb_newstate(struct ieee80211com *, enum ieee80211_state,
    128   1.1  christos 		    int);
    129   1.1  christos Static void	athn_usb_newstate_cb(struct athn_usb_softc *, void *);
    130   1.1  christos Static void	athn_usb_node_cleanup(struct ieee80211_node *);
    131   1.1  christos Static void	athn_usb_node_cleanup_cb(struct athn_usb_softc *, void *);
    132   1.1  christos Static int	athn_usb_open_pipes(struct athn_usb_softc *);
    133   1.1  christos Static uint32_t	athn_usb_read(struct athn_softc *, uint32_t);
    134   1.1  christos Static int	athn_usb_remove_hw_node(struct athn_usb_softc *, uint8_t *);
    135   1.1  christos Static void	athn_usb_rx_enable(struct athn_softc *);
    136   1.1  christos Static void	athn_usb_rx_frame(struct athn_usb_softc *, struct mbuf *);
    137   1.1  christos Static void	athn_usb_rx_radiotap(struct athn_softc *, struct mbuf *,
    138   1.1  christos 		    struct ar_rx_status *);
    139   1.3  christos Static void	athn_usb_rx_wmi_ctrl(struct athn_usb_softc *, uint8_t *, size_t);
    140   1.9     skrll Static void	athn_usb_rxeof(struct usbd_xfer *, void *,
    141   1.1  christos 		    usbd_status);
    142   1.1  christos Static void	athn_usb_start(struct ifnet *);
    143   1.1  christos Static void	athn_usb_stop(struct ifnet *);
    144   1.1  christos Static void	athn_usb_swba(struct athn_usb_softc *);
    145   1.1  christos Static int	athn_usb_switch_chan(struct athn_softc *,
    146   1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
    147   1.1  christos Static void	athn_usb_task(void *);
    148   1.1  christos Static int	athn_usb_tx(struct athn_softc *, struct mbuf *,
    149   1.1  christos 		    struct ieee80211_node *, struct athn_usb_tx_data *);
    150   1.9     skrll Static void	athn_usb_txeof(struct usbd_xfer *, void *,
    151   1.1  christos 		    usbd_status);
    152   1.1  christos Static void	athn_usb_updateslot(struct ifnet *);
    153   1.1  christos Static void	athn_usb_updateslot_cb(struct athn_usb_softc *, void *);
    154   1.1  christos Static void	athn_usb_wait_async(struct athn_usb_softc *);
    155   1.1  christos Static void	athn_usb_wait_cmd(struct athn_usb_softc *);
    156   1.1  christos Static void	athn_usb_wait_msg(struct athn_usb_softc *);
    157   1.1  christos Static void	athn_usb_wait_wmi(struct athn_usb_softc *);
    158   1.1  christos Static void	athn_usb_watchdog(struct ifnet *);
    159   1.1  christos Static int	athn_usb_wmi_xcmd(struct athn_usb_softc *, uint16_t, void *,
    160   1.1  christos 		    int, void *);
    161   1.9     skrll Static void	athn_usb_wmieof(struct usbd_xfer *, void *,
    162   1.1  christos 		    usbd_status);
    163   1.1  christos Static void	athn_usb_write(struct athn_softc *, uint32_t, uint32_t);
    164   1.1  christos Static void	athn_usb_write_barrier(struct athn_softc *);
    165   1.1  christos 
    166   1.1  christos /************************************************************************
    167   1.1  christos  * unused/notyet declarations
    168   1.1  christos  */
    169   1.1  christos #ifdef unused
    170   1.1  christos Static int	athn_usb_read_rom(struct athn_softc *);
    171   1.1  christos #endif /* unused */
    172   1.1  christos 
    173   1.1  christos #ifdef notyet_edca
    174   1.1  christos Static void	athn_usb_updateedca(struct ieee80211com *);
    175   1.1  christos Static void	athn_usb_updateedca_cb(struct athn_usb_softc *, void *);
    176   1.1  christos #endif /* notyet_edca */
    177   1.1  christos 
    178   1.1  christos #ifdef notyet
    179   1.1  christos Static int	athn_usb_ampdu_tx_start(struct ieee80211com *,
    180   1.1  christos 		    struct ieee80211_node *, uint8_t);
    181   1.1  christos Static void	athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *, void *);
    182   1.1  christos Static void	athn_usb_ampdu_tx_stop(struct ieee80211com *,
    183   1.1  christos 		    struct ieee80211_node *, uint8_t);
    184   1.1  christos Static void	athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *, void *);
    185   1.1  christos Static void	athn_usb_delete_key(struct ieee80211com *,
    186   1.1  christos 		    struct ieee80211_node *, struct ieee80211_key *);
    187   1.1  christos Static void	athn_usb_delete_key_cb(struct athn_usb_softc *, void *);
    188   1.1  christos Static int	athn_usb_set_key(struct ieee80211com *,
    189   1.1  christos 		    struct ieee80211_node *, struct ieee80211_key *);
    190   1.1  christos Static void	athn_usb_set_key_cb(struct athn_usb_softc *, void *);
    191   1.1  christos #endif /* notyet */
    192   1.1  christos /************************************************************************/
    193   1.1  christos 
    194   1.1  christos struct athn_usb_type {
    195   1.1  christos 	struct usb_devno	devno;
    196   1.1  christos 	u_int			flags;
    197   1.1  christos };
    198   1.1  christos 
    199   1.1  christos Static const struct athn_usb_type *
    200   1.1  christos athn_usb_lookup(int vendor, int product)
    201   1.1  christos {
    202   1.1  christos 	static const struct athn_usb_type athn_usb_devs[] = {
    203   1.1  christos #define _D(v,p,f) \
    204   1.1  christos 		{{ USB_VENDOR_##v, USB_PRODUCT_##p }, ATHN_USB_FLAG_##f }
    205   1.1  christos 
    206   1.1  christos 		_D( ACCTON,	ACCTON_AR9280,		AR7010 ),
    207   1.1  christos 		_D( ACTIONTEC,	ACTIONTEC_AR9287,	AR7010 ),
    208   1.1  christos 		_D( ATHEROS2,	ATHEROS2_AR9271_1,	NONE ),
    209   1.1  christos 		_D( ATHEROS2,	ATHEROS2_AR9271_2,	NONE ),
    210   1.1  christos 		_D( ATHEROS2,	ATHEROS2_AR9271_3,	NONE ),
    211   1.1  christos 		_D( ATHEROS2,	ATHEROS2_AR9280,	AR7010 ),
    212   1.1  christos 		_D( ATHEROS2,	ATHEROS2_AR9287,	AR7010 ),
    213   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_1,	NONE ),
    214   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_2,	NONE ),
    215   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_3,	NONE ),
    216   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_4,	NONE ),
    217   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_5,	NONE ),
    218   1.1  christos 		_D( AZUREWAVE,	AZUREWAVE_AR9271_6,	NONE ),
    219   1.1  christos 		_D( DLINK2,	DLINK2_AR9271,	  	NONE ),
    220   1.1  christos 		_D( LITEON,	LITEON_AR9271,	  	NONE ),
    221   1.1  christos 		_D( NETGEAR,	NETGEAR_WNA1100,	NONE ),
    222   1.1  christos 		_D( NETGEAR,	NETGEAR_WNDA3200,	AR7010 ),
    223   1.1  christos 		_D( VIA,	VIA_AR9271,		NONE )
    224   1.1  christos #undef _D
    225   1.1  christos 	};
    226   1.1  christos 
    227   1.1  christos 	return (const void *)usb_lookup(athn_usb_devs, vendor, product);
    228   1.1  christos }
    229   1.1  christos 
    230   1.1  christos Static int
    231   1.1  christos athn_usb_match(device_t parent, cfdata_t match, void *aux)
    232   1.1  christos {
    233   1.1  christos 	struct usb_attach_arg *uaa = aux;
    234   1.1  christos 
    235   1.9     skrll 	return athn_usb_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    236   1.1  christos 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    237   1.1  christos }
    238   1.1  christos 
    239   1.1  christos Static void
    240   1.1  christos athn_usb_attach(device_t parent, device_t self, void *aux)
    241   1.1  christos {
    242   1.1  christos 	struct athn_usb_softc *usc;
    243   1.1  christos 	struct athn_softc *sc;
    244   1.1  christos 	struct usb_attach_arg *uaa;
    245   1.1  christos 	int error;
    246   1.1  christos 
    247   1.1  christos 	usc = device_private(self);
    248   1.1  christos 	sc = &usc->usc_sc;
    249   1.1  christos 	uaa = aux;
    250   1.1  christos 	sc->sc_dev = self;
    251   1.9     skrll 	usc->usc_udev = uaa->uaa_device;
    252   1.1  christos 
    253   1.1  christos 	aprint_naive("\n");
    254   1.1  christos 	aprint_normal("\n");
    255   1.1  christos 
    256   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
    257   1.1  christos 
    258   1.1  christos 	usc->usc_athn_attached = 0;
    259   1.9     skrll 	usc->usc_flags = athn_usb_lookup(uaa->uaa_vendor, uaa->uaa_product)->flags;
    260   1.1  christos 	sc->sc_flags |= ATHN_FLAG_USB;
    261   1.1  christos #ifdef notyet
    262   1.1  christos 	/* Check if it is a combo WiFi+Bluetooth (WB193) device. */
    263   1.1  christos 	if (strncmp(product, "wb193", 5) == 0)
    264   1.1  christos 		sc->sc_flags |= ATHN_FLAG_BTCOEX3WIRE;
    265   1.1  christos #endif
    266   1.1  christos 
    267   1.1  christos 	sc->sc_ops.read = athn_usb_read;
    268   1.1  christos 	sc->sc_ops.write = athn_usb_write;
    269   1.1  christos 	sc->sc_ops.write_barrier = athn_usb_write_barrier;
    270   1.1  christos 
    271   1.9     skrll 	cv_init(&usc->usc_task_cv, "athntsk");
    272   1.1  christos 	mutex_init(&usc->usc_task_mtx, MUTEX_DEFAULT, IPL_NET);
    273   1.1  christos 	mutex_init(&usc->usc_tx_mtx, MUTEX_DEFAULT, IPL_NONE);
    274   1.1  christos 
    275   1.1  christos 	usb_init_task(&usc->usc_task, athn_usb_task, usc, 0);
    276   1.1  christos 
    277   1.1  christos 	if (usbd_set_config_no(usc->usc_udev, 1, 0) != 0) {
    278   1.1  christos 		aprint_error_dev(sc->sc_dev,
    279   1.1  christos 		    "could not set configuration no\n");
    280   1.1  christos 		goto fail;
    281   1.1  christos 	}
    282   1.1  christos 
    283   1.1  christos 	/* Get the first interface handle. */
    284   1.1  christos 	error = usbd_device2interface_handle(usc->usc_udev, 0, &usc->usc_iface);
    285   1.1  christos 	if (error != 0) {
    286   1.1  christos 		aprint_error_dev(sc->sc_dev,
    287   1.1  christos 		    "could not get interface handle\n");
    288   1.1  christos 		goto fail;
    289   1.1  christos 	}
    290   1.1  christos 
    291   1.1  christos 	if (athn_usb_open_pipes(usc) != 0)
    292   1.1  christos 		goto fail;
    293   1.1  christos 
    294   1.1  christos 	/* Allocate xfer for firmware commands. */
    295   1.1  christos 	if (athn_usb_alloc_tx_cmd(usc) != 0)
    296   1.1  christos 		goto fail;
    297   1.1  christos 
    298   1.9     skrll 	/* Allocate Tx/Rx buffers. */
    299   1.9     skrll 	error = athn_usb_alloc_rx_list(usc);
    300   1.9     skrll 	if (error != 0)
    301   1.9     skrll 		goto fail;
    302   1.9     skrll 	error = athn_usb_alloc_tx_list(usc);
    303   1.9     skrll 	if (error != 0)
    304   1.9     skrll 		goto fail;
    305   1.9     skrll 
    306   1.1  christos 	config_mountroot(self, athn_usb_attachhook);
    307   1.1  christos 
    308   1.1  christos 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, usc->usc_udev, sc->sc_dev);
    309   1.1  christos 	return;
    310   1.1  christos 
    311   1.1  christos  fail:
    312   1.9     skrll 	/* Free Tx/Rx buffers. */
    313   1.9     skrll 	athn_usb_abort_pipes(usc);
    314   1.9     skrll 	athn_usb_free_tx_list(usc);
    315   1.9     skrll 	athn_usb_free_rx_list(usc);
    316   1.1  christos 	athn_usb_free_tx_cmd(usc);
    317   1.1  christos 	athn_usb_close_pipes(usc);
    318   1.1  christos 	usb_rem_task(usc->usc_udev, &usc->usc_task);
    319   1.1  christos 	mutex_destroy(&usc->usc_tx_mtx);
    320   1.1  christos 	mutex_destroy(&usc->usc_task_mtx);
    321   1.1  christos }
    322   1.1  christos 
    323   1.1  christos Static void
    324   1.1  christos athn_usb_node_cleanup_cb(struct athn_usb_softc *usc, void *arg)
    325   1.1  christos {
    326   1.1  christos 	uint8_t sta_index = *(uint8_t *)arg;
    327   1.1  christos 
    328   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    329   1.1  christos 	DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
    330   1.1  christos 	athn_usb_remove_hw_node(usc, &sta_index);
    331   1.1  christos }
    332   1.1  christos 
    333   1.1  christos Static void
    334   1.1  christos athn_usb_node_cleanup(struct ieee80211_node *ni)
    335   1.1  christos {
    336   1.1  christos 	struct athn_usb_softc *usc;
    337   1.1  christos 	struct ieee80211com *ic;
    338   1.1  christos 	uint8_t sta_index;
    339   1.1  christos 
    340   1.1  christos 	usc = ATHN_USB_SOFTC(ni->ni_ic->ic_ifp->if_softc);
    341   1.1  christos 	ic = &ATHN_SOFTC(usc)->sc_ic;
    342   1.1  christos 
    343   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    344   1.1  christos 
    345   1.1  christos 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    346   1.1  christos 		sta_index = ATHN_NODE(ni)->sta_index;
    347   1.1  christos 		if (sta_index != 0)
    348   1.1  christos 			athn_usb_do_async(usc, athn_usb_node_cleanup_cb,
    349   1.1  christos 			    &sta_index, sizeof(sta_index));
    350   1.1  christos 	}
    351   1.1  christos 	usc->usc_node_cleanup(ni);
    352   1.1  christos }
    353   1.1  christos 
    354   1.1  christos Static void
    355   1.1  christos athn_usb_attachhook(device_t arg)
    356   1.1  christos {
    357   1.1  christos 	struct athn_usb_softc *usc = device_private(arg);
    358   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
    359   1.1  christos 	struct athn_ops *ops = &sc->sc_ops;
    360   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    361   1.1  christos 	struct ifnet *ifp = &sc->sc_if;
    362   1.1  christos 	size_t i;
    363   1.1  christos 	int s, error;
    364   1.1  christos 
    365   1.1  christos 	if (usc->usc_dying)
    366   1.1  christos 		return;
    367   1.1  christos 
    368   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    369   1.1  christos 
    370   1.1  christos 	/* Load firmware. */
    371   1.1  christos 	error = athn_usb_load_firmware(usc);
    372   1.1  christos 	if (error != 0) {
    373   1.1  christos 		aprint_error_dev(sc->sc_dev,
    374   1.1  christos 		    "could not load firmware (%d)\n", error);
    375   1.1  christos 		return;
    376   1.1  christos 	}
    377   1.1  christos 
    378   1.1  christos 	/* Setup the host transport communication interface. */
    379   1.1  christos 	error = athn_usb_htc_setup(usc);
    380   1.1  christos 	if (error != 0)
    381   1.1  christos 		return;
    382   1.1  christos 
    383   1.1  christos 	/* We're now ready to attach the bus agnostic driver. */
    384   1.1  christos 	s = splnet();
    385   1.1  christos 	ic->ic_ifp = ifp;
    386   1.1  christos 	ic->ic_updateslot = athn_usb_updateslot;
    387   1.1  christos 	sc->sc_max_aid = AR_USB_MAX_STA;  /* Firmware is limited to 8 STA */
    388   1.1  christos 	sc->sc_media_change = athn_usb_media_change;
    389   1.1  christos 	error = athn_attach(sc);
    390   1.1  christos 	if (error != 0) {
    391   1.1  christos 		splx(s);
    392   1.1  christos 		return;
    393   1.1  christos 	}
    394   1.1  christos 	usc->usc_athn_attached = 1;
    395   1.1  christos 
    396   1.1  christos 	/* Override some operations for USB. */
    397   1.1  christos 	ifp->if_init = athn_usb_init;
    398   1.1  christos 	ifp->if_ioctl = athn_usb_ioctl;
    399   1.1  christos 	ifp->if_start = athn_usb_start;
    400   1.1  christos 	ifp->if_watchdog = athn_usb_watchdog;
    401   1.1  christos 
    402   1.1  christos 	/* hooks for HostAP association and disassociation */
    403   1.1  christos 	ic->ic_newassoc = athn_usb_newassoc;
    404   1.1  christos 	usc->usc_node_cleanup = ic->ic_node_cleanup;
    405   1.1  christos 	ic->ic_node_cleanup = athn_usb_node_cleanup;
    406   1.1  christos 
    407   1.1  christos #ifdef notyet_edca
    408   1.1  christos 	ic->ic_updateedca = athn_usb_updateedca;
    409   1.1  christos #endif
    410   1.1  christos #ifdef notyet
    411   1.1  christos 	ic->ic_set_key = athn_usb_set_key;
    412   1.1  christos 	ic->ic_delete_key = athn_usb_delete_key;
    413   1.1  christos 	ic->ic_ampdu_tx_start = athn_usb_ampdu_tx_start;
    414   1.1  christos 	ic->ic_ampdu_tx_stop = athn_usb_ampdu_tx_stop;
    415   1.1  christos #endif
    416   1.1  christos 	ic->ic_newstate = athn_usb_newstate;
    417   1.1  christos 
    418   1.1  christos 	ops->rx_enable = athn_usb_rx_enable;
    419   1.1  christos 	splx(s);
    420   1.1  christos 
    421   1.1  christos 	/* Reset HW key cache entries. */
    422   1.1  christos 	for (i = 0; i < sc->sc_kc_entries; i++)
    423   1.1  christos 		athn_reset_key(sc, i);
    424   1.1  christos 
    425   1.1  christos 	ops->enable_antenna_diversity(sc);
    426   1.1  christos 
    427   1.1  christos #ifdef ATHN_BT_COEXISTENCE
    428   1.1  christos 	/* Configure bluetooth coexistence for combo chips. */
    429   1.1  christos 	if (sc->sc_flags & ATHN_FLAG_BTCOEX)
    430   1.1  christos 		athn_btcoex_init(sc);
    431   1.1  christos #endif
    432   1.1  christos 	/* Configure LED. */
    433   1.1  christos 	athn_led_init(sc);
    434   1.1  christos 
    435   1.1  christos 	ieee80211_announce(ic);
    436   1.1  christos }
    437   1.1  christos 
    438   1.1  christos Static int
    439   1.1  christos athn_usb_detach(device_t self, int flags)
    440   1.1  christos {
    441   1.1  christos 	struct athn_usb_softc *usc = device_private(self);
    442   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
    443   1.1  christos 	int s;
    444   1.1  christos 
    445   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    446   1.1  christos 
    447   1.1  christos 	s = splusb();
    448   1.1  christos 	usc->usc_dying = 1;
    449   1.1  christos 
    450   1.1  christos 	athn_usb_wait_wmi(usc);
    451   1.1  christos 	athn_usb_wait_cmd(usc);
    452   1.1  christos 	athn_usb_wait_msg(usc);
    453   1.1  christos 	athn_usb_wait_async(usc);
    454   1.1  christos 
    455   1.1  christos 	usb_rem_task(usc->usc_udev, &usc->usc_task);
    456   1.1  christos 
    457   1.1  christos 	if (usc->usc_athn_attached) {
    458   1.1  christos 		usc->usc_athn_attached = 0;
    459   1.1  christos 		athn_detach(sc);
    460   1.1  christos 	}
    461   1.9     skrll 	/* Abort Tx/Rx pipes. */
    462   1.9     skrll 	athn_usb_abort_pipes(usc);
    463   1.1  christos 	splx(s);
    464   1.1  christos 
    465   1.1  christos 	/* Free Tx/Rx buffers. */
    466   1.1  christos 	athn_usb_free_rx_list(usc);
    467   1.1  christos 	athn_usb_free_tx_list(usc);
    468   1.1  christos 	athn_usb_free_tx_cmd(usc);
    469   1.1  christos 
    470   1.9     skrll 	/* Close Tx/Rx pipes. */
    471   1.9     skrll 	athn_usb_close_pipes(usc);
    472   1.9     skrll 
    473   1.1  christos 	mutex_destroy(&usc->usc_tx_mtx);
    474   1.1  christos 	mutex_destroy(&usc->usc_task_mtx);
    475   1.9     skrll 	cv_destroy(&usc->usc_task_cv);
    476   1.1  christos 
    477   1.1  christos 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, usc->usc_udev, sc->sc_dev);
    478   1.1  christos 	return 0;
    479   1.1  christos }
    480   1.1  christos 
    481   1.1  christos Static int
    482   1.1  christos athn_usb_activate(device_t self, enum devact act)
    483   1.1  christos {
    484   1.1  christos 	struct athn_usb_softc *usc = device_private(self);
    485   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
    486   1.1  christos 
    487   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    488   1.1  christos 
    489   1.1  christos 	switch (act) {
    490   1.1  christos 	case DVACT_DEACTIVATE:
    491   1.1  christos 		if_deactivate(sc->sc_ic.ic_ifp);
    492   1.1  christos 		usc->usc_dying = 1;
    493   1.1  christos 		return 0;
    494   1.1  christos 	default:
    495   1.1  christos 		return EOPNOTSUPP;
    496   1.1  christos 	}
    497   1.1  christos }
    498   1.1  christos 
    499   1.1  christos Static int
    500   1.1  christos athn_usb_open_pipes(struct athn_usb_softc *usc)
    501   1.1  christos {
    502   1.1  christos 	usb_endpoint_descriptor_t *ed;
    503   1.9     skrll 	int error;
    504   1.1  christos 
    505   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    506   1.1  christos 
    507   1.1  christos 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_DATA, 0,
    508   1.1  christos 	    &usc->usc_tx_data_pipe);
    509   1.1  christos 	if (error != 0) {
    510   1.1  christos 		aprint_error_dev(usc->usc_dev,
    511   1.1  christos 		    "could not open Tx bulk pipe\n");
    512   1.1  christos 		goto fail;
    513   1.1  christos 	}
    514   1.1  christos 
    515   1.1  christos 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_RX_DATA, 0,
    516   1.1  christos 	    &usc->usc_rx_data_pipe);
    517   1.1  christos 	if (error != 0) {
    518   1.1  christos 		aprint_error_dev(usc->usc_dev,
    519   1.1  christos 		    "could not open Rx bulk pipe\n");
    520   1.1  christos 		goto fail;
    521   1.1  christos 	}
    522   1.1  christos 
    523   1.1  christos 	ed = usbd_get_endpoint_descriptor(usc->usc_iface, AR_PIPE_RX_INTR);
    524   1.1  christos 	if (ed == NULL) {
    525   1.1  christos 		aprint_error_dev(usc->usc_dev,
    526   1.1  christos 		    "could not retrieve Rx intr pipe descriptor\n");
    527   1.1  christos 		goto fail;
    528   1.1  christos 	}
    529   1.9     skrll 	usc->usc_ibufsize = UGETW(ed->wMaxPacketSize);
    530   1.9     skrll 	if (usc->usc_ibufsize == 0) {
    531   1.1  christos 		aprint_error_dev(usc->usc_dev,
    532   1.1  christos 		    "invalid Rx intr pipe descriptor\n");
    533   1.1  christos 		goto fail;
    534   1.1  christos 	}
    535   1.9     skrll 	usc->usc_ibuf = kmem_alloc(usc->usc_ibufsize, KM_SLEEP);
    536   1.1  christos 	if (usc->usc_ibuf == NULL) {
    537   1.1  christos 		aprint_error_dev(usc->usc_dev,
    538   1.1  christos 		    "could not allocate Rx intr buffer\n");
    539   1.1  christos 		goto fail;
    540   1.1  christos 	}
    541   1.9     skrll 
    542   1.1  christos 	error = usbd_open_pipe_intr(usc->usc_iface, AR_PIPE_RX_INTR,
    543   1.9     skrll 	    USBD_SHORT_XFER_OK, &usc->usc_rx_intr_pipe, usc, usc->usc_ibuf,
    544   1.9     skrll 	    usc->usc_ibufsize, athn_usb_intr, USBD_DEFAULT_INTERVAL);
    545   1.1  christos 	if (error != 0) {
    546   1.1  christos 		aprint_error_dev(usc->usc_dev,
    547   1.1  christos 		    "could not open Rx intr pipe\n");
    548   1.1  christos 		goto fail;
    549   1.1  christos 	}
    550   1.1  christos 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_INTR, 0,
    551   1.1  christos 	    &usc->usc_tx_intr_pipe);
    552   1.1  christos 	if (error != 0) {
    553   1.1  christos 		aprint_error_dev(usc->usc_dev,
    554   1.1  christos 		    "could not open Tx intr pipe\n");
    555   1.1  christos 		goto fail;
    556   1.1  christos 	}
    557   1.1  christos 	return 0;
    558   1.1  christos  fail:
    559   1.9     skrll 	athn_usb_abort_pipes(usc);
    560   1.1  christos 	athn_usb_close_pipes(usc);
    561   1.1  christos 	return error;
    562   1.1  christos }
    563   1.1  christos 
    564   1.1  christos static inline void
    565   1.9     skrll athn_usb_kill_pipe(struct usbd_pipe **pipeptr)
    566   1.1  christos {
    567   1.9     skrll 	struct usbd_pipe *pipe;
    568   1.1  christos 
    569   1.1  christos 	CTASSERT(sizeof(pipe) == sizeof(void *));
    570   1.1  christos 	pipe = atomic_swap_ptr(pipeptr, NULL);
    571   1.1  christos 	if (pipe != NULL) {
    572   1.1  christos 		usbd_close_pipe(pipe);
    573   1.1  christos 	}
    574   1.1  christos }
    575   1.1  christos 
    576   1.1  christos Static void
    577   1.9     skrll athn_usb_abort_pipes(struct athn_usb_softc *usc)
    578   1.9     skrll {
    579   1.9     skrll 	DPRINTFN(DBG_FN, usc, "\n");
    580   1.9     skrll 
    581   1.9     skrll 	if (usc->usc_tx_data_pipe != NULL)
    582   1.9     skrll 		usbd_abort_pipe(usc->usc_tx_data_pipe);
    583   1.9     skrll 	if (usc->usc_rx_data_pipe != NULL)
    584   1.9     skrll 		usbd_abort_pipe(usc->usc_rx_data_pipe);
    585   1.9     skrll 	if (usc->usc_tx_intr_pipe != NULL)
    586   1.9     skrll 		usbd_abort_pipe(usc->usc_tx_intr_pipe);
    587   1.9     skrll 	if (usc->usc_rx_intr_pipe != NULL)
    588   1.9     skrll 		usbd_abort_pipe(usc->usc_rx_intr_pipe);
    589   1.9     skrll }
    590   1.9     skrll 
    591   1.9     skrll Static void
    592   1.1  christos athn_usb_close_pipes(struct athn_usb_softc *usc)
    593   1.1  christos {
    594   1.1  christos 	uint8_t *ibuf;
    595   1.1  christos 
    596   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    597   1.1  christos 
    598   1.1  christos 	athn_usb_kill_pipe(&usc->usc_tx_data_pipe);
    599   1.1  christos 	athn_usb_kill_pipe(&usc->usc_rx_data_pipe);
    600   1.1  christos 	athn_usb_kill_pipe(&usc->usc_tx_intr_pipe);
    601   1.1  christos 	athn_usb_kill_pipe(&usc->usc_rx_intr_pipe);
    602   1.1  christos 	ibuf = atomic_swap_ptr(&usc->usc_ibuf, NULL);
    603   1.1  christos 	if (ibuf != NULL)
    604   1.9     skrll 		kmem_free(ibuf, usc->usc_ibufsize);
    605   1.1  christos }
    606   1.1  christos 
    607   1.1  christos Static int
    608   1.1  christos athn_usb_alloc_rx_list(struct athn_usb_softc *usc)
    609   1.1  christos {
    610   1.1  christos 	struct athn_usb_rx_data *data;
    611   1.1  christos 	size_t i;
    612   1.1  christos 	int error = 0;
    613   1.1  christos 
    614   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    615   1.1  christos 
    616   1.1  christos 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
    617   1.1  christos 		data = &usc->usc_rx_data[i];
    618   1.1  christos 
    619   1.1  christos 		data->sc = usc;	/* Backpointer for callbacks. */
    620   1.1  christos 
    621   1.9     skrll 		error = usbd_create_xfer(usc->usc_rx_data_pipe,
    622   1.9     skrll 		    ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK, 0, &data->xfer);
    623   1.9     skrll 		if (error) {
    624   1.1  christos 			aprint_error_dev(usc->usc_dev,
    625   1.1  christos 			    "could not allocate xfer\n");
    626   1.1  christos 			break;
    627   1.1  christos 		}
    628   1.9     skrll 		data->buf = usbd_get_buffer(data->xfer);
    629   1.1  christos 	}
    630   1.1  christos 	if (error != 0)
    631   1.1  christos 		athn_usb_free_rx_list(usc);
    632   1.1  christos 	return error;
    633   1.1  christos }
    634   1.1  christos 
    635   1.1  christos Static void
    636   1.1  christos athn_usb_free_rx_list(struct athn_usb_softc *usc)
    637   1.1  christos {
    638   1.9     skrll 	struct usbd_xfer *xfer;
    639   1.1  christos 	size_t i;
    640   1.1  christos 
    641   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    642   1.1  christos 
    643   1.1  christos 	/* NB: Caller must abort pipe first. */
    644   1.1  christos 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
    645   1.1  christos 		CTASSERT(sizeof(xfer) == sizeof(void *));
    646   1.1  christos 		xfer = atomic_swap_ptr(&usc->usc_rx_data[i].xfer, NULL);
    647   1.1  christos 		if (xfer != NULL)
    648   1.9     skrll 			usbd_destroy_xfer(xfer);
    649   1.1  christos 	}
    650   1.1  christos }
    651   1.1  christos 
    652   1.1  christos Static int
    653   1.1  christos athn_usb_alloc_tx_list(struct athn_usb_softc *usc)
    654   1.1  christos {
    655   1.1  christos 	struct athn_usb_tx_data *data;
    656   1.1  christos 	size_t i;
    657   1.1  christos 	int error = 0;
    658   1.1  christos 
    659   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    660   1.1  christos 
    661   1.1  christos 	mutex_enter(&usc->usc_tx_mtx);
    662   1.1  christos 	TAILQ_INIT(&usc->usc_tx_free_list);
    663   1.1  christos 	for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
    664   1.1  christos 		data = &usc->usc_tx_data[i];
    665   1.1  christos 
    666   1.1  christos 		data->sc = usc;	/* Backpointer for callbacks. */
    667   1.1  christos 
    668   1.9     skrll 		error = usbd_create_xfer(usc->usc_tx_data_pipe,
    669   1.9     skrll 		    ATHN_USB_TXBUFSZ, USBD_SHORT_XFER_OK, 0, &data->xfer);
    670   1.9     skrll 		if (error) {
    671   1.1  christos 			aprint_error_dev(usc->usc_dev,
    672   1.9     skrll 			    "could not create xfer on TX pipe\n");
    673   1.1  christos 			break;
    674   1.1  christos 		}
    675   1.9     skrll 		data->buf = usbd_get_buffer(data->xfer);
    676   1.9     skrll 
    677   1.1  christos 		/* Append this Tx buffer to our free list. */
    678   1.1  christos 		TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
    679   1.1  christos 	}
    680   1.1  christos 	if (error != 0)
    681   1.1  christos 		athn_usb_free_tx_list(usc);
    682   1.1  christos 	mutex_exit(&usc->usc_tx_mtx);
    683   1.1  christos 	return error;
    684   1.1  christos }
    685   1.1  christos 
    686   1.1  christos Static void
    687   1.1  christos athn_usb_free_tx_list(struct athn_usb_softc *usc)
    688   1.1  christos {
    689   1.9     skrll 	struct usbd_xfer *xfer;
    690   1.1  christos 	size_t i;
    691   1.1  christos 
    692   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    693   1.1  christos 
    694   1.1  christos 	/* NB: Caller must abort pipe first. */
    695   1.1  christos 	for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
    696   1.1  christos 		CTASSERT(sizeof(xfer) == sizeof(void *));
    697   1.1  christos 		xfer = atomic_swap_ptr(&usc->usc_tx_data[i].xfer, NULL);
    698   1.1  christos 		if (xfer != NULL)
    699   1.9     skrll 			usbd_destroy_xfer(xfer);
    700   1.1  christos 	}
    701   1.1  christos }
    702   1.1  christos 
    703   1.1  christos Static int
    704   1.1  christos athn_usb_alloc_tx_cmd(struct athn_usb_softc *usc)
    705   1.1  christos {
    706   1.1  christos 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
    707   1.1  christos 
    708   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    709   1.1  christos 
    710   1.1  christos 	data->sc = usc;	/* Backpointer for callbacks. */
    711   1.1  christos 
    712   1.9     skrll 	int err = usbd_create_xfer(usc->usc_tx_intr_pipe, ATHN_USB_TXCMDSZ,
    713   1.9     skrll 	    0, 0, &data->xfer);
    714   1.9     skrll 	if (err) {
    715   1.1  christos 		aprint_error_dev(usc->usc_dev,
    716   1.9     skrll 		    "could not allocate command xfer\n");
    717   1.9     skrll 		return err;
    718   1.1  christos 	}
    719  1.13     skrll 	data->buf = usbd_get_buffer(data->xfer);
    720   1.9     skrll 
    721   1.1  christos 	return 0;
    722   1.1  christos }
    723   1.1  christos 
    724   1.1  christos Static void
    725   1.1  christos athn_usb_free_tx_cmd(struct athn_usb_softc *usc)
    726   1.1  christos {
    727   1.9     skrll 	struct usbd_xfer *xfer;
    728   1.1  christos 
    729   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    730   1.1  christos 
    731   1.1  christos 	CTASSERT(sizeof(xfer) == sizeof(void *));
    732   1.1  christos 	xfer = atomic_swap_ptr(&usc->usc_tx_cmd.xfer, NULL);
    733   1.1  christos 	if (xfer != NULL)
    734   1.9     skrll 		usbd_destroy_xfer(xfer);
    735   1.1  christos }
    736   1.1  christos 
    737   1.1  christos Static void
    738   1.1  christos athn_usb_task(void *arg)
    739   1.1  christos {
    740   1.1  christos 	struct athn_usb_softc *usc = arg;
    741   1.1  christos 	struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
    742   1.1  christos 	struct athn_usb_host_cmd *cmd;
    743   1.1  christos 	int s;
    744   1.1  christos 
    745   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    746   1.1  christos 
    747   1.1  christos 	/* Process host commands. */
    748   1.1  christos 	s = splusb();
    749   1.1  christos 	mutex_spin_enter(&usc->usc_task_mtx);
    750   1.1  christos 	while (ring->next != ring->cur) {
    751   1.1  christos 		cmd = &ring->cmd[ring->next];
    752   1.1  christos 		mutex_spin_exit(&usc->usc_task_mtx);
    753   1.1  christos 		splx(s);
    754   1.1  christos 
    755   1.1  christos 		/* Invoke callback. */
    756   1.1  christos 		if (!usc->usc_dying)
    757   1.1  christos 			cmd->cb(usc, cmd->data);
    758   1.1  christos 
    759   1.1  christos 		s = splusb();
    760   1.1  christos 		mutex_spin_enter(&usc->usc_task_mtx);
    761   1.1  christos 		ring->queued--;
    762   1.1  christos 		ring->next = (ring->next + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
    763   1.1  christos 	}
    764   1.1  christos 	mutex_spin_exit(&usc->usc_task_mtx);
    765   1.1  christos 	wakeup(ring);
    766   1.1  christos 	splx(s);
    767   1.1  christos }
    768   1.1  christos 
    769   1.1  christos Static void
    770   1.1  christos athn_usb_do_async(struct athn_usb_softc *usc,
    771   1.1  christos     void (*cb)(struct athn_usb_softc *, void *), void *arg, int len)
    772   1.1  christos {
    773   1.1  christos 	struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
    774   1.1  christos 	struct athn_usb_host_cmd *cmd;
    775   1.1  christos 	int s;
    776   1.1  christos 
    777   1.1  christos 	if (usc->usc_dying)
    778   1.1  christos 		return;
    779   1.1  christos 
    780   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    781   1.1  christos 
    782   1.1  christos 	s = splusb();
    783   1.1  christos 	mutex_spin_enter(&usc->usc_task_mtx);
    784   1.1  christos 	cmd = &ring->cmd[ring->cur];
    785   1.1  christos 	cmd->cb = cb;
    786   1.1  christos 	KASSERT(len <= sizeof(cmd->data));
    787   1.1  christos 	memcpy(cmd->data, arg, len);
    788   1.1  christos 	ring->cur = (ring->cur + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
    789   1.1  christos 
    790   1.1  christos 	/* If there is no pending command already, schedule a task. */
    791   1.1  christos 	if (++ring->queued == 1) {
    792   1.1  christos 		mutex_spin_exit(&usc->usc_task_mtx);
    793   1.1  christos 		usb_add_task(usc->usc_udev, &usc->usc_task, USB_TASKQ_DRIVER);
    794   1.1  christos 	}
    795   1.1  christos 	else
    796   1.1  christos 		mutex_spin_exit(&usc->usc_task_mtx);
    797   1.1  christos 	splx(s);
    798   1.1  christos }
    799   1.1  christos 
    800   1.1  christos Static void
    801   1.1  christos athn_usb_wait_async(struct athn_usb_softc *usc)
    802   1.1  christos {
    803   1.1  christos 
    804   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    805   1.1  christos 
    806   1.1  christos 	/* Wait for all queued asynchronous commands to complete. */
    807   1.9     skrll 	mutex_spin_enter(&usc->usc_task_mtx);
    808   1.1  christos 	while (usc->usc_cmdq.queued > 0)
    809   1.9     skrll 		cv_wait(&usc->usc_task_cv, &usc->usc_task_mtx);
    810   1.9     skrll 	mutex_spin_exit(&usc->usc_task_mtx);
    811   1.1  christos }
    812   1.1  christos 
    813   1.1  christos Static int
    814   1.1  christos athn_usb_load_firmware(struct athn_usb_softc *usc)
    815   1.1  christos {
    816   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
    817   1.1  christos 	firmware_handle_t fwh;
    818   1.1  christos 	usb_device_descriptor_t *dd;
    819   1.1  christos 	usb_device_request_t req;
    820   1.1  christos 	const char *name;
    821   1.1  christos 	u_char *fw, *ptr;
    822   1.8    nonaka 	size_t size, remain;
    823   1.1  christos 	uint32_t addr;
    824   1.1  christos 	int s, mlen, error;
    825   1.1  christos 
    826   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
    827   1.1  christos 
    828   1.1  christos 	/* Determine which firmware image to load. */
    829   1.1  christos 	if (usc->usc_flags & ATHN_USB_FLAG_AR7010) {
    830   1.1  christos 		dd = usbd_get_device_descriptor(usc->usc_udev);
    831   1.1  christos 		if (UGETW(dd->bcdDevice) == 0x0202)
    832   1.1  christos 			name = "athn-ar7010-11";
    833   1.1  christos 		else
    834   1.1  christos 			name = "athn-ar7010";
    835   1.1  christos 	}
    836   1.1  christos 	else
    837   1.1  christos 		name = "athn-ar9271";
    838   1.1  christos 
    839   1.1  christos 	/* Read firmware image from the filesystem. */
    840   1.4  christos 	if ((error = firmware_open("if_athn", name, &fwh)) != 0) {
    841   1.1  christos 		aprint_error_dev(sc->sc_dev,
    842   1.1  christos 		    "failed to open firmware file %s (%d)\n", name, error);
    843   1.1  christos 		return error;
    844   1.1  christos 	}
    845   1.1  christos 	size = firmware_get_size(fwh);
    846   1.1  christos 	fw = firmware_malloc(size);
    847   1.1  christos 	if (fw == NULL) {
    848   1.1  christos 		aprint_error_dev(usc->usc_dev,
    849   1.1  christos 		    "failed to allocate firmware memory\n");
    850   1.1  christos 		firmware_close(fwh);
    851   1.1  christos 		return ENOMEM;
    852   1.1  christos 	}
    853   1.1  christos 	error = firmware_read(fwh, 0, fw, size);
    854   1.1  christos 	firmware_close(fwh);
    855   1.1  christos 	if (error != 0) {
    856   1.1  christos 		aprint_error_dev(usc->usc_dev,
    857   1.1  christos 		    "failed to read firmware (error %d)\n", error);
    858   1.7     ozaki 		firmware_free(fw, size);
    859   1.1  christos 		return error;
    860   1.1  christos 	}
    861   1.1  christos 
    862   1.1  christos 	/* Load firmware image. */
    863   1.1  christos 	ptr = fw;
    864   1.1  christos 	addr = AR9271_FIRMWARE >> 8;
    865   1.1  christos 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    866   1.1  christos 	req.bRequest = AR_FW_DOWNLOAD;
    867   1.1  christos 	USETW(req.wIndex, 0);
    868   1.8    nonaka 	remain = size;
    869   1.8    nonaka 	while (remain > 0) {
    870   1.8    nonaka 		mlen = MIN(remain, 4096);
    871   1.1  christos 
    872   1.1  christos 		USETW(req.wValue, addr);
    873   1.1  christos 		USETW(req.wLength, mlen);
    874   1.1  christos 		error = usbd_do_request(usc->usc_udev, &req, ptr);
    875   1.1  christos 		if (error != 0) {
    876   1.7     ozaki 			firmware_free(fw, size);
    877   1.1  christos 			return error;
    878   1.1  christos 		}
    879   1.8    nonaka 		addr   += mlen >> 8;
    880   1.8    nonaka 		ptr    += mlen;
    881   1.8    nonaka 		remain -= mlen;
    882   1.1  christos 	}
    883   1.7     ozaki 	firmware_free(fw, size);
    884   1.1  christos 
    885   1.1  christos 	/* Start firmware. */
    886   1.1  christos 	if (usc->usc_flags & ATHN_USB_FLAG_AR7010)
    887   1.1  christos 		addr = AR7010_FIRMWARE_TEXT >> 8;
    888   1.1  christos 	else
    889   1.1  christos 		addr = AR9271_FIRMWARE_TEXT >> 8;
    890   1.1  christos 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    891   1.1  christos 	req.bRequest = AR_FW_DOWNLOAD_COMP;
    892   1.1  christos 	USETW(req.wIndex, 0);
    893   1.1  christos 	USETW(req.wValue, addr);
    894   1.1  christos 	USETW(req.wLength, 0);
    895   1.1  christos 
    896   1.1  christos 	s = splusb();
    897   1.1  christos 	usc->usc_wait_msg_id = AR_HTC_MSG_READY;
    898   1.1  christos 	error = usbd_do_request(usc->usc_udev, &req, NULL);
    899   1.1  christos 	/* Wait at most 1 second for firmware to boot. */
    900   1.1  christos 	if (error == 0 && usc->usc_wait_msg_id != 0)
    901   1.1  christos 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnfw", hz);
    902   1.1  christos 	usc->usc_wait_msg_id = 0;
    903   1.1  christos 	splx(s);
    904   1.1  christos 	return error;
    905   1.1  christos }
    906   1.1  christos 
    907   1.1  christos Static int
    908   1.1  christos athn_usb_htc_msg(struct athn_usb_softc *usc, uint16_t msg_id, void *buf,
    909   1.1  christos     int len)
    910   1.1  christos {
    911   1.1  christos 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
    912   1.1  christos 	struct ar_htc_frame_hdr *htc;
    913   1.1  christos 	struct ar_htc_msg_hdr *msg;
    914   1.1  christos 
    915   1.1  christos 	if (usc->usc_dying)
    916   1.1  christos 		return USBD_CANCELLED;
    917   1.1  christos 
    918   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
    919   1.1  christos 
    920   1.1  christos 	htc = (struct ar_htc_frame_hdr *)data->buf;
    921   1.1  christos 	memset(htc, 0, sizeof(*htc));
    922   1.1  christos 	htc->endpoint_id = 0;
    923   1.1  christos 	htc->payload_len = htobe16(sizeof(*msg) + len);
    924   1.1  christos 
    925   1.1  christos 	msg = (struct ar_htc_msg_hdr *)&htc[1];
    926   1.1  christos 	msg->msg_id = htobe16(msg_id);
    927   1.1  christos 
    928   1.1  christos 	memcpy(&msg[1], buf, len);
    929   1.1  christos 
    930   1.9     skrll 	usbd_setup_xfer(data->xfer, NULL, data->buf,
    931   1.1  christos 	    sizeof(*htc) + sizeof(*msg) + len,
    932   1.9     skrll 	    USBD_SHORT_XFER_OK, ATHN_USB_CMD_TIMEOUT, NULL);
    933   1.1  christos 	return usbd_sync_transfer(data->xfer);
    934   1.1  christos }
    935   1.1  christos 
    936   1.1  christos Static int
    937   1.1  christos athn_usb_htc_setup(struct athn_usb_softc *usc)
    938   1.1  christos {
    939   1.1  christos 	struct ar_htc_msg_config_pipe cfg;
    940   1.1  christos 	int s, error;
    941   1.1  christos 
    942   1.1  christos 	/*
    943   1.1  christos 	 * Connect WMI services to USB pipes.
    944   1.1  christos 	 */
    945   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CONTROL,
    946   1.1  christos 	    AR_PIPE_TX_INTR, AR_PIPE_RX_INTR, &usc->usc_ep_ctrl);
    947   1.1  christos 	if (error != 0)
    948   1.1  christos 		return error;
    949   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_BEACON,
    950   1.1  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_bcn);
    951   1.1  christos 	if (error != 0)
    952   1.1  christos 		return error;
    953   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CAB,
    954   1.1  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_cab);
    955   1.1  christos 	if (error != 0)
    956   1.1  christos 		return error;
    957   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_UAPSD,
    958   1.1  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_uapsd);
    959   1.1  christos 	if (error != 0)
    960   1.1  christos 		return error;
    961   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_MGMT,
    962   1.1  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_mgmt);
    963   1.1  christos 	if (error != 0)
    964   1.1  christos 		return error;
    965   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BE,
    966   1.3  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BE]);
    967   1.1  christos 	if (error != 0)
    968   1.1  christos 		return error;
    969   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BK,
    970   1.3  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BK]);
    971   1.1  christos 	if (error != 0)
    972   1.1  christos 		return error;
    973   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VI,
    974   1.3  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VI]);
    975   1.1  christos 	if (error != 0)
    976   1.1  christos 		return error;
    977   1.1  christos 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VO,
    978   1.3  christos 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VO]);
    979   1.1  christos 	if (error != 0)
    980   1.1  christos 		return error;
    981   1.1  christos 
    982   1.1  christos 	/* Set credits for WLAN Tx pipe. */
    983   1.1  christos 	memset(&cfg, 0, sizeof(cfg));
    984   1.1  christos 	cfg.pipe_id = UE_GET_ADDR(AR_PIPE_TX_DATA);
    985   1.1  christos 	cfg.credits = (usc->usc_flags & ATHN_USB_FLAG_AR7010) ? 45 : 33;
    986   1.1  christos 
    987   1.1  christos 	s = splusb();
    988   1.1  christos 
    989   1.1  christos 	usc->usc_wait_msg_id = AR_HTC_MSG_CONF_PIPE_RSP;
    990   1.1  christos 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONF_PIPE, &cfg, sizeof(cfg));
    991   1.1  christos 	if (error == 0 && usc->usc_wait_msg_id != 0)
    992   1.1  christos 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
    993   1.1  christos 	usc->usc_wait_msg_id = 0;
    994   1.1  christos 
    995   1.1  christos 	splx(s);
    996   1.1  christos 
    997   1.1  christos 	if (error != 0) {
    998   1.1  christos 		aprint_error_dev(usc->usc_dev, "could not configure pipe\n");
    999   1.1  christos 		return error;
   1000   1.1  christos 	}
   1001   1.1  christos 
   1002   1.1  christos 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_SETUP_COMPLETE, NULL, 0);
   1003   1.1  christos 	if (error != 0) {
   1004   1.1  christos 		aprint_error_dev(usc->usc_dev, "could not complete setup\n");
   1005   1.1  christos 		return error;
   1006   1.1  christos 	}
   1007   1.1  christos 	return 0;
   1008   1.1  christos }
   1009   1.1  christos 
   1010   1.1  christos Static int
   1011   1.1  christos athn_usb_htc_connect_svc(struct athn_usb_softc *usc, uint16_t svc_id,
   1012   1.1  christos     uint8_t ul_pipe, uint8_t dl_pipe, uint8_t *endpoint_id)
   1013   1.1  christos {
   1014   1.1  christos 	struct ar_htc_msg_conn_svc msg;
   1015   1.1  christos 	struct ar_htc_msg_conn_svc_rsp rsp;
   1016   1.1  christos 	int s, error;
   1017   1.1  christos 
   1018   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1019   1.1  christos 
   1020   1.1  christos 	memset(&msg, 0, sizeof(msg));
   1021   1.1  christos 	msg.svc_id = htobe16(svc_id);
   1022   1.1  christos 	msg.dl_pipeid = UE_GET_ADDR(dl_pipe);
   1023   1.1  christos 	msg.ul_pipeid = UE_GET_ADDR(ul_pipe);
   1024   1.1  christos 	s = splusb();
   1025   1.1  christos 
   1026   1.1  christos 	usc->usc_msg_conn_svc_rsp = &rsp;
   1027   1.1  christos 
   1028   1.1  christos 	usc->usc_wait_msg_id = AR_HTC_MSG_CONN_SVC_RSP;
   1029   1.1  christos 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONN_SVC, &msg, sizeof(msg));
   1030   1.1  christos 	if (error == 0 && usc->usc_wait_msg_id != 0)
   1031   1.1  christos 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
   1032   1.1  christos 	usc->usc_wait_msg_id = 0;
   1033   1.1  christos 
   1034   1.1  christos 	splx(s);
   1035   1.1  christos 	if (error != 0) {
   1036   1.1  christos 		aprint_error_dev(usc->usc_dev,
   1037   1.1  christos 		    "error waiting for service %d connection\n", svc_id);
   1038   1.1  christos 		return error;
   1039   1.1  christos 	}
   1040   1.1  christos 	if (rsp.status != AR_HTC_SVC_SUCCESS) {
   1041   1.1  christos 		aprint_error_dev(usc->usc_dev,
   1042   1.1  christos 		    "service %d connection failed, error %d\n",
   1043   1.1  christos 		    svc_id, rsp.status);
   1044   1.1  christos 		return EIO;
   1045   1.1  christos 	}
   1046   1.1  christos 	DPRINTFN(DBG_INIT, usc,
   1047   1.1  christos 	    "service %d successfully connected to endpoint %d\n",
   1048   1.1  christos 	    svc_id, rsp.endpoint_id);
   1049   1.1  christos 
   1050   1.1  christos 	/* Return endpoint id. */
   1051   1.1  christos 	*endpoint_id = rsp.endpoint_id;
   1052   1.1  christos 	return 0;
   1053   1.1  christos }
   1054   1.1  christos 
   1055   1.1  christos Static void
   1056   1.1  christos athn_usb_wait_msg(struct athn_usb_softc *usc)
   1057   1.1  christos {
   1058   1.1  christos 
   1059   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1060   1.1  christos 
   1061   1.1  christos 	while (__predict_false(usc->usc_wait_msg_id))
   1062   1.1  christos 		tsleep(&usc->usc_wait_msg_id, 0, "athnmsg", hz);
   1063   1.1  christos }
   1064   1.1  christos 
   1065   1.1  christos Static void
   1066   1.1  christos athn_usb_wait_cmd(struct athn_usb_softc *usc)
   1067   1.1  christos {
   1068   1.1  christos 
   1069   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1070   1.1  christos 
   1071   1.1  christos 	while (__predict_false(usc->usc_wait_cmd_id))
   1072   1.1  christos 		tsleep(&usc->usc_wait_cmd_id, 0, "athncmd", hz);
   1073   1.1  christos }
   1074   1.1  christos 
   1075   1.1  christos Static void
   1076   1.9     skrll athn_usb_wmieof(struct usbd_xfer *xfer, void * priv,
   1077   1.1  christos     usbd_status status)
   1078   1.1  christos {
   1079   1.1  christos 	struct athn_usb_softc *usc = priv;
   1080   1.1  christos 
   1081   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1082   1.1  christos 
   1083   1.1  christos 	if (__predict_false(status == USBD_STALLED))
   1084   1.1  christos 		usbd_clear_endpoint_stall_async(usc->usc_tx_intr_pipe);
   1085   1.1  christos 
   1086   1.1  christos 	usc->usc_wmi_done = 1;
   1087   1.1  christos 	wakeup(&usc->usc_wmi_done);
   1088   1.1  christos }
   1089   1.1  christos 
   1090   1.1  christos Static int
   1091   1.1  christos athn_usb_wmi_xcmd(struct athn_usb_softc *usc, uint16_t cmd_id, void *ibuf,
   1092   1.1  christos     int ilen, void *obuf)
   1093   1.1  christos {
   1094   1.1  christos 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
   1095   1.1  christos 	struct ar_htc_frame_hdr *htc;
   1096   1.1  christos 	struct ar_wmi_cmd_hdr *wmi;
   1097   1.1  christos 	int s, error;
   1098   1.1  christos 
   1099   1.1  christos 	if (usc->usc_dying)
   1100   1.1  christos 		return EIO;
   1101   1.1  christos 
   1102   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1103   1.1  christos 
   1104   1.1  christos 	htc = (struct ar_htc_frame_hdr *)data->buf;
   1105   1.1  christos 	memset(htc, 0, sizeof(*htc));
   1106   1.1  christos 	htc->endpoint_id = usc->usc_ep_ctrl;
   1107   1.1  christos 	htc->payload_len = htobe16(sizeof(*wmi) + ilen);
   1108   1.1  christos 
   1109   1.1  christos 	wmi = (struct ar_wmi_cmd_hdr *)&htc[1];
   1110   1.1  christos 	wmi->cmd_id = htobe16(cmd_id);
   1111   1.1  christos 	usc->usc_wmi_seq_no++;
   1112   1.1  christos 	wmi->seq_no = htobe16(usc->usc_wmi_seq_no);
   1113   1.1  christos 
   1114   1.1  christos 	memcpy(&wmi[1], ibuf, ilen);
   1115   1.1  christos 
   1116   1.9     skrll 	usbd_setup_xfer(data->xfer, usc, data->buf,
   1117   1.1  christos 	    sizeof(*htc) + sizeof(*wmi) + ilen,
   1118   1.9     skrll 	    USBD_SHORT_XFER_OK, ATHN_USB_CMD_TIMEOUT,
   1119   1.1  christos 	    athn_usb_wmieof);
   1120   1.1  christos 
   1121   1.1  christos 	s = splusb();
   1122   1.1  christos 	usc->usc_wmi_done = 0;
   1123   1.1  christos 	usc->usc_wait_cmd_id = cmd_id;
   1124   1.1  christos 	error = usbd_transfer(data->xfer);
   1125   1.1  christos 	if (__predict_true(error == 0 || error == USBD_IN_PROGRESS)) {
   1126   1.1  christos 		usc->usc_obuf = obuf;
   1127   1.1  christos 
   1128   1.1  christos 		/* Wait for WMI command to complete. */
   1129   1.1  christos 		error = tsleep(&usc->usc_wait_cmd_id, 0, "athnwmi", hz);
   1130   1.1  christos 		usc->usc_wait_cmd_id = 0;
   1131   1.1  christos 		athn_usb_wait_wmi(usc);
   1132   1.1  christos 	}
   1133   1.1  christos 	splx(s);
   1134   1.1  christos 	return error;
   1135   1.1  christos }
   1136   1.1  christos 
   1137   1.1  christos Static void
   1138   1.1  christos athn_usb_wait_wmi(struct athn_usb_softc *usc)
   1139   1.1  christos {
   1140   1.1  christos 
   1141   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1142   1.1  christos 
   1143   1.1  christos 	while (__predict_false(!usc->usc_wmi_done))
   1144   1.1  christos 		tsleep(&usc->usc_wmi_done, 0, "athnwmi", 0);
   1145   1.1  christos }
   1146   1.1  christos 
   1147   1.1  christos #ifdef unused
   1148   1.1  christos Static int
   1149   1.1  christos athn_usb_read_rom(struct athn_softc *sc)
   1150   1.1  christos {
   1151   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1152   1.1  christos 	uint32_t addrs[8], vals[8], addr;
   1153   1.1  christos 	uint16_t *eep;
   1154   1.1  christos 	size_t i, j;
   1155   1.1  christos 	int error = 0;
   1156   1.1  christos 
   1157   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1158   1.1  christos 
   1159   1.1  christos 	/* Read EEPROM by blocks of 16 bytes. */
   1160   1.1  christos 	eep = sc->sc_eep;
   1161   1.1  christos 	addr = AR_EEPROM_OFFSET(sc->sc_eep_base);
   1162   1.1  christos 	for (i = 0; i < sc->sc_eep_size / 16; i++) {
   1163   1.1  christos 		for (j = 0; j < 8; j++, addr += 4)
   1164   1.1  christos 			addrs[j] = htobe32(addr);
   1165   1.1  christos 		error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
   1166   1.1  christos 		    addrs, sizeof(addrs), vals);
   1167   1.1  christos 		if (error != 0)
   1168   1.1  christos 			break;
   1169   1.1  christos 		for (j = 0; j < 8; j++)
   1170   1.1  christos 			*eep++ = be32toh(vals[j]);
   1171   1.1  christos 	}
   1172   1.1  christos 	return error;
   1173   1.1  christos }
   1174   1.1  christos #endif /* unused */
   1175   1.1  christos 
   1176   1.1  christos Static uint32_t
   1177   1.1  christos athn_usb_read(struct athn_softc *sc, uint32_t addr)
   1178   1.1  christos {
   1179   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1180   1.1  christos 	uint32_t val;
   1181   1.1  christos 	int error;
   1182   1.1  christos 
   1183   1.1  christos 	if (usc->usc_dying)
   1184   1.1  christos 		return 0;
   1185   1.1  christos 
   1186   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1187   1.1  christos 
   1188   1.1  christos 	/* Flush pending writes for strict consistency. */
   1189   1.1  christos 	athn_usb_write_barrier(sc);
   1190   1.1  christos 
   1191   1.1  christos 	addr = htobe32(addr);
   1192   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
   1193   1.1  christos 	    &addr, sizeof(addr), &val);
   1194   1.1  christos 	if (error != 0)
   1195   1.1  christos 		return 0xdeadbeef;
   1196   1.1  christos 	return be32toh(val);
   1197   1.1  christos }
   1198   1.1  christos 
   1199   1.1  christos Static void
   1200   1.1  christos athn_usb_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
   1201   1.1  christos {
   1202   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1203   1.1  christos 
   1204   1.1  christos 	if (usc->usc_dying)
   1205   1.1  christos 		return;
   1206   1.1  christos 
   1207   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1208   1.1  christos 
   1209   1.1  christos 	usc->usc_wbuf[usc->usc_wcount].addr = htobe32(addr);
   1210   1.1  christos 	usc->usc_wbuf[usc->usc_wcount].val  = htobe32(val);
   1211   1.1  christos 	if (++usc->usc_wcount == AR_MAX_WRITE_COUNT)
   1212   1.1  christos 		athn_usb_write_barrier(sc);
   1213   1.1  christos }
   1214   1.1  christos 
   1215   1.1  christos Static void
   1216   1.1  christos athn_usb_write_barrier(struct athn_softc *sc)
   1217   1.1  christos {
   1218   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1219   1.1  christos 
   1220   1.1  christos 	if (usc->usc_dying)
   1221   1.1  christos 		goto done;
   1222   1.1  christos 
   1223   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1224   1.1  christos 
   1225   1.1  christos 	if (usc->usc_wcount == 0)
   1226   1.1  christos 		return;
   1227   1.1  christos 
   1228   1.1  christos 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_WRITE,
   1229   1.1  christos 	    usc->usc_wbuf, usc->usc_wcount * sizeof(usc->usc_wbuf[0]), NULL);
   1230   1.1  christos  done:
   1231   1.1  christos 	usc->usc_wcount = 0;	/* Always flush buffer. */
   1232   1.1  christos }
   1233   1.1  christos 
   1234   1.1  christos Static int
   1235   1.1  christos athn_usb_media_change(struct ifnet *ifp)
   1236   1.1  christos {
   1237   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   1238   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1239   1.1  christos 	int error;
   1240   1.1  christos 
   1241   1.1  christos 	if (usc->usc_dying)
   1242   1.1  christos 		return EIO;
   1243   1.1  christos 
   1244   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1245   1.1  christos 
   1246   1.1  christos 	error = ieee80211_media_change(ifp);
   1247   1.1  christos 	if (error == ENETRESET && IS_UP_AND_RUNNING(ifp)) {
   1248   1.1  christos 		athn_usb_stop(ifp);
   1249   1.1  christos 		error = athn_usb_init(ifp);
   1250   1.1  christos 	}
   1251   1.1  christos 	return error;
   1252   1.1  christos }
   1253   1.1  christos 
   1254   1.1  christos Static int
   1255   1.1  christos athn_usb_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
   1256   1.1  christos     int arg)
   1257   1.1  christos {
   1258   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1259   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1260   1.1  christos 	struct athn_usb_cmd_newstate cmd;
   1261   1.1  christos 
   1262   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1263   1.1  christos 
   1264   1.1  christos 	/* Do it in a process context. */
   1265   1.1  christos 	cmd.state = nstate;
   1266   1.1  christos 	cmd.arg = arg;
   1267   1.1  christos 	athn_usb_do_async(usc, athn_usb_newstate_cb, &cmd, sizeof(cmd));
   1268   1.1  christos 	return 0;
   1269   1.1  christos }
   1270   1.1  christos 
   1271   1.1  christos Static void
   1272   1.1  christos athn_usb_newstate_cb(struct athn_usb_softc *usc, void *arg)
   1273   1.1  christos {
   1274   1.1  christos 	struct athn_usb_cmd_newstate *cmd = arg;
   1275   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
   1276   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1277   1.1  christos 	enum ieee80211_state ostate, nstate;
   1278   1.1  christos 	uint32_t reg, imask;
   1279   1.1  christos 	int s;
   1280   1.1  christos 
   1281   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1282   1.1  christos 
   1283   1.1  christos 	callout_stop(&sc->sc_calib_to);
   1284   1.1  christos 
   1285   1.1  christos 	s = splnet();
   1286   1.1  christos 
   1287   1.1  christos 	ostate = ic->ic_state;
   1288   1.1  christos 	nstate = cmd->state;
   1289   1.1  christos 	DPRINTFN(DBG_STM, usc, "newstate %s(%d) -> %s(%d)\n",
   1290   1.1  christos 		    ieee80211_state_name[ostate], ostate,
   1291   1.1  christos 		    ieee80211_state_name[nstate], nstate);
   1292   1.1  christos 
   1293   1.1  christos 	if (ostate == IEEE80211_S_RUN) {
   1294   1.1  christos 		uint8_t sta_index;
   1295   1.1  christos 
   1296   1.1  christos 		sta_index = ATHN_NODE(ic->ic_bss)->sta_index;
   1297   1.1  christos 		DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
   1298   1.1  christos 		athn_usb_remove_hw_node(usc, &sta_index);
   1299   1.1  christos 	}
   1300   1.1  christos 
   1301   1.1  christos 	switch (nstate) {
   1302   1.1  christos 	case IEEE80211_S_INIT:
   1303   1.1  christos 		athn_set_led(sc, 0);
   1304   1.1  christos 		break;
   1305   1.1  christos 	case IEEE80211_S_SCAN:
   1306   1.1  christos 		/* Make the LED blink while scanning. */
   1307   1.1  christos 		athn_set_led(sc, !sc->sc_led_state);
   1308   1.1  christos 		(void)athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
   1309   1.1  christos 		if (!usc->usc_dying)
   1310   1.1  christos 			callout_schedule(&sc->sc_scan_to, hz / 5);
   1311   1.1  christos 		break;
   1312   1.1  christos 	case IEEE80211_S_AUTH:
   1313   1.1  christos 		athn_set_led(sc, 0);
   1314   1.1  christos 		athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
   1315   1.1  christos 		break;
   1316   1.1  christos 	case IEEE80211_S_ASSOC:
   1317   1.1  christos 		break;
   1318   1.1  christos 	case IEEE80211_S_RUN:
   1319   1.1  christos 		athn_set_led(sc, 1);
   1320   1.1  christos 
   1321   1.1  christos 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
   1322   1.1  christos 			break;
   1323   1.1  christos 
   1324   1.1  christos 		/* Create node entry for our BSS. */
   1325   1.1  christos 		DPRINTFN(DBG_NODES, sc, "create node for AID=0x%x\n",
   1326   1.1  christos 		    ic->ic_bss->ni_associd);
   1327   1.1  christos 		athn_usb_create_node(usc, ic->ic_bss);	/* XXX: handle error? */
   1328   1.1  christos 
   1329   1.1  christos 		athn_set_bss(sc, ic->ic_bss);
   1330   1.1  christos 		athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
   1331   1.1  christos #ifndef IEEE80211_STA_ONLY
   1332   1.1  christos 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   1333   1.1  christos 			athn_set_hostap_timers(sc);
   1334   1.1  christos 			/* Enable software beacon alert interrupts. */
   1335   1.1  christos 			imask = htobe32(AR_IMR_SWBA);
   1336   1.1  christos 		}
   1337   1.1  christos 		else
   1338   1.1  christos #endif
   1339   1.1  christos 		{
   1340   1.1  christos 			athn_set_sta_timers(sc);
   1341   1.1  christos 			/* Enable beacon miss interrupts. */
   1342   1.1  christos 			imask = htobe32(AR_IMR_BMISS);
   1343   1.1  christos 
   1344   1.1  christos 			/* Stop receiving beacons from other BSS. */
   1345   1.1  christos 			reg = AR_READ(sc, AR_RX_FILTER);
   1346   1.1  christos 			reg = (reg & ~AR_RX_FILTER_BEACON) |
   1347   1.1  christos 			    AR_RX_FILTER_MYBEACON;
   1348   1.1  christos 			AR_WRITE(sc, AR_RX_FILTER, reg);
   1349   1.1  christos 			AR_WRITE_BARRIER(sc);
   1350   1.1  christos 		}
   1351   1.1  christos 		athn_usb_wmi_xcmd(usc, AR_WMI_CMD_ENABLE_INTR,
   1352   1.1  christos 		    &imask, sizeof(imask), NULL);
   1353   1.1  christos 		break;
   1354   1.1  christos 	}
   1355   1.1  christos 	if (!usc->usc_dying)
   1356   1.1  christos 		(void)sc->sc_newstate(ic, nstate, cmd->arg);
   1357   1.1  christos 	splx(s);
   1358   1.1  christos }
   1359   1.1  christos 
   1360   1.1  christos Static void
   1361   1.1  christos athn_usb_newassoc(struct ieee80211_node *ni, int isnew)
   1362   1.1  christos {
   1363   1.1  christos 	struct ieee80211com *ic = ni->ni_ic;
   1364   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1365   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1366   1.1  christos 
   1367   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1368   1.1  christos 
   1369   1.1  christos 	if (ic->ic_opmode != IEEE80211_M_HOSTAP || !isnew)
   1370   1.1  christos 		return;
   1371   1.1  christos 
   1372   1.1  christos 	/* Do it in a process context. */
   1373   1.1  christos 	ieee80211_ref_node(ni);
   1374   1.1  christos 	athn_usb_do_async(usc, athn_usb_newassoc_cb, &ni, sizeof(ni));
   1375   1.1  christos }
   1376   1.1  christos 
   1377   1.1  christos Static void
   1378   1.1  christos athn_usb_newassoc_cb(struct athn_usb_softc *usc, void *arg)
   1379   1.1  christos {
   1380   1.1  christos 	struct ieee80211_node *ni = *(void **)arg;
   1381   1.1  christos 	int s;
   1382   1.1  christos 
   1383   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1384   1.1  christos 
   1385   1.1  christos 	s = splnet();
   1386   1.1  christos 	/* NB: Node may have left before we got scheduled. */
   1387   1.1  christos 	if (ni->ni_associd != 0) {
   1388   1.1  christos 		DPRINTFN(DBG_NODES, usc, "creating node for AID=0x%x\n",
   1389   1.1  christos 		    ni->ni_associd);
   1390   1.1  christos 		(void)athn_usb_create_node(usc, ni);	/* XXX: handle error? */
   1391   1.1  christos 	}
   1392   1.1  christos 	ieee80211_free_node(ni);
   1393   1.1  christos 	splx(s);
   1394   1.1  christos }
   1395   1.1  christos 
   1396   1.1  christos #ifdef notyet
   1397   1.1  christos Static int
   1398   1.1  christos athn_usb_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
   1399   1.1  christos     uint8_t tid)
   1400   1.1  christos {
   1401   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1402   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1403   1.1  christos 	struct athn_node *an = ATHN_NODE(ni);
   1404   1.1  christos 	struct athn_usb_aggr_cmd cmd;
   1405   1.1  christos 
   1406   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1407   1.1  christos 
   1408   1.1  christos 	/* Do it in a process context. */
   1409   1.1  christos 	cmd.sta_index = an->sta_index;
   1410   1.1  christos 	cmd.tid = tid;
   1411   1.1  christos 	athn_usb_do_async(usc, athn_usb_ampdu_tx_start_cb, &cmd, sizeof(cmd));
   1412   1.1  christos 	return 0;
   1413   1.1  christos }
   1414   1.1  christos 
   1415   1.1  christos Static void
   1416   1.1  christos athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *usc, void *arg)
   1417   1.1  christos {
   1418   1.1  christos 	struct athn_usb_aggr_cmd *cmd = arg;
   1419   1.1  christos 	struct ar_htc_target_aggr aggr;
   1420   1.1  christos 
   1421   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1422   1.1  christos 
   1423   1.1  christos 	memset(&aggr, 0, sizeof(aggr));
   1424   1.1  christos 	aggr.sta_index = cmd->sta_index;
   1425   1.1  christos 	aggr.tidno = cmd->tid;
   1426   1.1  christos 	aggr.aggr_enable = 1;
   1427   1.1  christos 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
   1428   1.1  christos 	    &aggr, sizeof(aggr), NULL);
   1429   1.1  christos }
   1430   1.1  christos 
   1431   1.1  christos Static void
   1432   1.1  christos athn_usb_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
   1433   1.1  christos     uint8_t tid)
   1434   1.1  christos {
   1435   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1436   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1437   1.1  christos 	struct athn_node *an = ATHN_NODE(ni);
   1438   1.1  christos 	struct athn_usb_aggr_cmd cmd;
   1439   1.1  christos 
   1440   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1441   1.1  christos 
   1442   1.1  christos 	/* Do it in a process context. */
   1443   1.1  christos 	cmd.sta_index = an->sta_index;
   1444   1.1  christos 	cmd.tid = tid;
   1445   1.1  christos 	athn_usb_do_async(usc, athn_usb_ampdu_tx_stop_cb, &cmd, sizeof(cmd));
   1446   1.1  christos }
   1447   1.1  christos 
   1448   1.1  christos Static void
   1449   1.1  christos athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *usc, void *arg)
   1450   1.1  christos {
   1451   1.1  christos 	struct athn_usb_aggr_cmd *cmd = arg;
   1452   1.1  christos 	struct ar_htc_target_aggr aggr;
   1453   1.1  christos 
   1454   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1455   1.1  christos 
   1456   1.1  christos 	memset(&aggr, 0, sizeof(aggr));
   1457   1.1  christos 	aggr.sta_index = cmd->sta_index;
   1458   1.1  christos 	aggr.tidno = cmd->tid;
   1459   1.1  christos 	aggr.aggr_enable = 0;
   1460   1.1  christos 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
   1461   1.1  christos 	    &aggr, sizeof(aggr), NULL);
   1462   1.1  christos }
   1463   1.1  christos #endif /* notyet */
   1464   1.1  christos 
   1465   1.1  christos Static int
   1466   1.1  christos athn_usb_remove_hw_node(struct athn_usb_softc *usc, uint8_t *sta_idx)
   1467   1.1  christos {
   1468   1.1  christos 	int error;
   1469   1.1  christos 
   1470   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1471   1.1  christos 
   1472   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE,
   1473   1.1  christos 	    sta_idx, sizeof(*sta_idx), NULL);
   1474   1.1  christos 
   1475   1.1  christos 	DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
   1476   1.1  christos 	    *sta_idx, error);
   1477   1.1  christos 	return error;
   1478   1.1  christos }
   1479   1.1  christos 
   1480   1.1  christos Static int
   1481   1.1  christos athn_usb_create_hw_node(struct athn_usb_softc *usc,
   1482   1.1  christos     struct ar_htc_target_sta *sta)
   1483   1.1  christos {
   1484   1.1  christos 	int error;
   1485   1.1  christos 
   1486   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1487   1.1  christos 
   1488   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_CREATE,
   1489   1.1  christos 	    sta, sizeof(*sta), NULL);
   1490   1.1  christos 
   1491   1.1  christos 	DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
   1492   1.1  christos 	    sta->sta_index, error);
   1493   1.1  christos 
   1494   1.1  christos 	return error;
   1495   1.1  christos }
   1496   1.1  christos 
   1497   1.1  christos Static int
   1498   1.1  christos athn_usb_create_node(struct athn_usb_softc *usc, struct ieee80211_node *ni)
   1499   1.1  christos {
   1500   1.1  christos 	struct athn_node *an = ATHN_NODE(ni);
   1501   1.1  christos 	struct ar_htc_target_sta sta;
   1502   1.1  christos 	struct ar_htc_target_rate rate;
   1503   1.1  christos 	int error;
   1504   1.1  christos 
   1505   1.1  christos 	DPRINTFN(DBG_FN | DBG_NODES, usc, "AID=0x%x\n", ni->ni_associd);
   1506   1.1  christos 
   1507   1.1  christos 	/*
   1508   1.1  christos 	 * NB: this is called by ic_newstate and (in HOSTAP mode by)
   1509   1.1  christos 	 * ic_newassoc.
   1510   1.1  christos 	 *
   1511   1.1  christos 	 * The firmware has a limit of 8 nodes.  In HOSTAP mode, we
   1512   1.1  christos 	 * limit the AID to < 8 and use that value to index the
   1513   1.1  christos 	 * firmware node table.  Node zero is used for the BSS.
   1514   1.1  christos 	 *
   1515   1.1  christos 	 * In STA mode, we simply use node 1 for the BSS.
   1516   1.1  christos 	 */
   1517   1.1  christos 	if (ATHN_SOFTC(usc)->sc_ic.ic_opmode == IEEE80211_M_HOSTAP)
   1518   1.1  christos 		an->sta_index = IEEE80211_NODE_AID(ni);
   1519   1.1  christos 	else
   1520   1.1  christos 		an->sta_index = 1;
   1521   1.1  christos 
   1522   1.1  christos 	/* Create node entry on target. */
   1523   1.1  christos 	memset(&sta, 0, sizeof(sta));
   1524   1.1  christos 	IEEE80211_ADDR_COPY(sta.macaddr, ni->ni_macaddr);
   1525   1.1  christos 	IEEE80211_ADDR_COPY(sta.bssid, ni->ni_bssid);
   1526   1.1  christos 
   1527   1.1  christos 	sta.associd = htobe16(ni->ni_associd);
   1528   1.1  christos 	sta.valid = 1;
   1529   1.1  christos 	sta.sta_index = an->sta_index;
   1530   1.1  christos 
   1531   1.1  christos 	sta.maxampdu = 0xffff;
   1532   1.1  christos #ifndef IEEE80211_NO_HT
   1533   1.1  christos 	if (ni->ni_flags & IEEE80211_NODE_HT)
   1534   1.1  christos 		sta.flags |= htobe16(AR_HTC_STA_HT);
   1535   1.1  christos #endif
   1536   1.1  christos 	error = athn_usb_create_hw_node(usc, &sta);
   1537   1.1  christos 	if (error)
   1538   1.1  christos 		return error;
   1539   1.1  christos 
   1540   1.1  christos 	/* Setup supported rates. */
   1541   1.1  christos 	memset(&rate, 0, sizeof(rate));
   1542   1.1  christos 	rate.sta_index = sta.sta_index;
   1543   1.1  christos 	rate.isnew = 1;
   1544   1.1  christos 	rate.lg_rates.rs_nrates = ni->ni_rates.rs_nrates;
   1545   1.1  christos 	memcpy(rate.lg_rates.rs_rates, ni->ni_rates.rs_rates,
   1546   1.1  christos 	    ni->ni_rates.rs_nrates);
   1547   1.1  christos 
   1548   1.1  christos #ifndef IEEE80211_NO_HT
   1549   1.1  christos 	if (ni->ni_flags & IEEE80211_NODE_HT) {
   1550   1.1  christos 		rate.capflags |= htobe32(AR_RC_HT_FLAG);
   1551   1.1  christos #ifdef notyet
   1552   1.1  christos 		/* XXX setup HT rates */
   1553   1.1  christos 		if (ni->ni_htcaps & IEEE80211_HTCAP_CBW20_40)
   1554   1.1  christos 			rate.capflags |= htobe32(AR_RC_40_FLAG);
   1555   1.1  christos 		if (ni->ni_htcaps & IEEE80211_HTCAP_SGI40)
   1556   1.1  christos 			rate.capflags |= htobe32(AR_RC_SGI_FLAG);
   1557   1.1  christos 		if (ni->ni_htcaps & IEEE80211_HTCAP_SGI20)
   1558   1.1  christos 			rate.capflags |= htobe32(AR_RC_SGI_FLAG);
   1559   1.1  christos #endif
   1560   1.1  christos 	}
   1561   1.1  christos #endif
   1562   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_RC_RATE_UPDATE,
   1563   1.1  christos 	    &rate, sizeof(rate), NULL);
   1564   1.1  christos 	return error;
   1565   1.1  christos }
   1566   1.1  christos 
   1567   1.1  christos Static void
   1568   1.1  christos athn_usb_rx_enable(struct athn_softc *sc)
   1569   1.1  christos {
   1570   1.1  christos 
   1571   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1572   1.1  christos 
   1573   1.1  christos 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
   1574   1.1  christos 	AR_WRITE_BARRIER(sc);
   1575   1.1  christos }
   1576   1.1  christos 
   1577   1.1  christos Static int
   1578   1.1  christos athn_usb_switch_chan(struct athn_softc *sc, struct ieee80211_channel *curchan,
   1579   1.1  christos     struct ieee80211_channel *extchan)
   1580   1.1  christos {
   1581   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1582   1.1  christos 	uint16_t mode;
   1583   1.1  christos 	int error;
   1584   1.1  christos 
   1585   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1586   1.1  christos 
   1587   1.1  christos 	/* Disable interrupts. */
   1588   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
   1589   1.1  christos 	if (error != 0)
   1590   1.1  christos 		goto reset;
   1591   1.1  christos 	/* Stop all Tx queues. */
   1592   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
   1593   1.1  christos 	if (error != 0)
   1594   1.1  christos 		goto reset;
   1595   1.1  christos 	/* Stop Rx. */
   1596   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
   1597   1.1  christos 	if (error != 0)
   1598   1.1  christos 		goto reset;
   1599   1.1  christos 
   1600   1.1  christos 	/* If band or bandwidth changes, we need to do a full reset. */
   1601   1.1  christos 	if (curchan->ic_flags != sc->sc_curchan->ic_flags ||
   1602   1.1  christos 	    ((extchan != NULL) ^ (sc->sc_curchanext != NULL))) {
   1603   1.1  christos 		DPRINTFN(DBG_RF, sc, "channel band switch\n");
   1604   1.1  christos 		goto reset;
   1605   1.1  christos 	}
   1606   1.1  christos 
   1607   1.1  christos 	error = athn_set_chan(sc, curchan, extchan);
   1608   1.1  christos 	if (AR_SREV_9271(sc) && error == 0)
   1609   1.1  christos 		ar9271_load_ani(sc);
   1610   1.1  christos 	if (error != 0) {
   1611   1.1  christos  reset:		/* Error found, try a full reset. */
   1612   1.1  christos 		DPRINTFN(DBG_RF, sc, "needs a full reset\n");
   1613   1.1  christos 		error = athn_hw_reset(sc, curchan, extchan, 0);
   1614   1.1  christos 		if (error != 0)	/* Hopeless case. */
   1615   1.1  christos 			return error;
   1616   1.1  christos 	}
   1617   1.1  christos 
   1618   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
   1619   1.1  christos 	if (error != 0)
   1620   1.1  christos 		return error;
   1621   1.1  christos 	athn_rx_start(sc);
   1622   1.1  christos 
   1623   1.1  christos 	mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
   1624   1.1  christos 	    AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
   1625   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
   1626   1.1  christos 	    &mode, sizeof(mode), NULL);
   1627   1.1  christos 	if (error != 0)
   1628   1.1  christos 		return error;
   1629   1.1  christos 
   1630   1.1  christos 	/* Re-enable interrupts. */
   1631   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ENABLE_INTR);
   1632   1.1  christos 	return error;
   1633   1.1  christos }
   1634   1.1  christos 
   1635   1.1  christos #ifdef notyet_edca
   1636   1.1  christos Static void
   1637   1.1  christos athn_usb_updateedca(struct ieee80211com *ic)
   1638   1.1  christos {
   1639   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1640   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1641   1.1  christos 
   1642   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1643   1.1  christos 
   1644   1.1  christos 	/* Do it in a process context. */
   1645   1.1  christos 	athn_usb_do_async(usc, athn_usb_updateedca_cb, NULL, 0);
   1646   1.1  christos }
   1647   1.1  christos 
   1648   1.1  christos Static void
   1649   1.1  christos athn_usb_updateedca_cb(struct athn_usb_softc *usc, void *arg)
   1650   1.1  christos {
   1651   1.1  christos 	int s;
   1652   1.1  christos 
   1653   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1654   1.1  christos 
   1655   1.1  christos 	s = splnet();
   1656   1.1  christos 	athn_updateedca(&usc->usc_sc.sc_ic);
   1657   1.1  christos 	splx(s);
   1658   1.1  christos }
   1659   1.1  christos #endif /* notyet_edca */
   1660   1.1  christos 
   1661   1.1  christos Static void
   1662   1.1  christos athn_usb_updateslot(struct ifnet *ifp)
   1663   1.1  christos {
   1664   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   1665   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1666   1.1  christos 
   1667   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1668   1.1  christos 
   1669   1.1  christos 	/*
   1670   1.1  christos 	 * NB: athn_updateslog() needs to be done in a process context
   1671   1.1  christos 	 * to avoid being called by ieee80211_reset_erp() inside a
   1672   1.1  christos 	 * spinlock held by ieee80211_free_allnodes().
   1673   1.1  christos 	 *
   1674   1.1  christos 	 * XXX: calling this during the athn_attach() causes
   1675   1.1  christos 	 * usb_insert_transfer() to produce a bunch of "not busy"
   1676   1.1  christos 	 * messages.  Why?
   1677   1.1  christos 	 */
   1678   1.1  christos 	if (usc->usc_athn_attached)
   1679   1.1  christos 		athn_usb_do_async(usc, athn_usb_updateslot_cb, NULL, 0);
   1680   1.1  christos }
   1681   1.1  christos 
   1682   1.1  christos Static void
   1683   1.1  christos athn_usb_updateslot_cb(struct athn_usb_softc *usc, void *arg)
   1684   1.1  christos {
   1685   1.1  christos 	int s;
   1686   1.1  christos 
   1687   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1688   1.1  christos 
   1689   1.1  christos 	s = splnet();
   1690   1.1  christos 	athn_updateslot(&usc->usc_sc.sc_if);
   1691   1.1  christos 	splx(s);
   1692   1.1  christos }
   1693   1.1  christos 
   1694   1.1  christos #ifdef notyet
   1695   1.1  christos Static int
   1696   1.1  christos athn_usb_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   1697   1.1  christos     struct ieee80211_key *k)
   1698   1.1  christos {
   1699   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1700   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1701   1.1  christos 	struct ifnet *ifp = &usc->usc_sc.sc_if;
   1702   1.1  christos 	struct athn_usb_cmd_key cmd;
   1703   1.1  christos 
   1704   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1705   1.1  christos 
   1706   1.1  christos 	/* Defer setting of WEP keys until interface is brought up. */
   1707   1.1  christos 	if (!IS_UP_AND_RUNNING(ifp))
   1708   1.1  christos 		return 0;
   1709   1.1  christos 
   1710   1.1  christos 	/* Do it in a process context. */
   1711   1.1  christos 	cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
   1712   1.1  christos 	cmd.key = k;
   1713   1.1  christos 	athn_usb_do_async(usc, athn_usb_set_key_cb, &cmd, sizeof(cmd));
   1714   1.1  christos 	return 0;
   1715   1.1  christos }
   1716   1.1  christos 
   1717   1.1  christos Static void
   1718   1.1  christos athn_usb_set_key_cb(struct athn_usb_softc *usc, void *arg)
   1719   1.1  christos {
   1720   1.1  christos 	struct ieee80211com *ic = &usc->usc_sc.sc_ic;
   1721   1.1  christos 	struct athn_usb_cmd_key *cmd = arg;
   1722   1.1  christos 	int s;
   1723   1.1  christos 
   1724   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1725   1.1  christos 
   1726   1.1  christos 	s = splnet();
   1727   1.1  christos 	athn_set_key(ic, cmd->ni, cmd->key);
   1728   1.1  christos 	if (cmd->ni != NULL)
   1729   1.1  christos 		ieee80211_free_node(cmd->ni);
   1730   1.1  christos 	splx(s);
   1731   1.1  christos }
   1732   1.1  christos 
   1733   1.1  christos Static void
   1734   1.1  christos athn_usb_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
   1735   1.1  christos     struct ieee80211_key *k)
   1736   1.1  christos {
   1737   1.1  christos 	struct athn_softc *sc = ic->ic_ifp->if_softc;
   1738   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   1739   1.1  christos 	struct ifnet *ifp = &usc->usc_sc.sc_if;
   1740   1.1  christos 	struct athn_usb_cmd_key cmd;
   1741   1.1  christos 
   1742   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1743   1.1  christos 
   1744   1.1  christos 	if (!(ifp->if_flags & IFF_RUNNING) ||
   1745   1.1  christos 	    ic->ic_state != IEEE80211_S_RUN)
   1746   1.1  christos 		return;	/* Nothing to do. */
   1747   1.1  christos 
   1748   1.1  christos 	/* Do it in a process context. */
   1749   1.1  christos 	cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
   1750   1.1  christos 	cmd.key = k;
   1751   1.1  christos 	athn_usb_do_async(usc, athn_usb_delete_key_cb, &cmd, sizeof(cmd));
   1752   1.1  christos }
   1753   1.1  christos 
   1754   1.1  christos Static void
   1755   1.1  christos athn_usb_delete_key_cb(struct athn_usb_softc *usc, void *arg)
   1756   1.1  christos {
   1757   1.1  christos 	struct ieee80211com *ic = &usc->usc_sc.sc_ic;
   1758   1.1  christos 	struct athn_usb_cmd_key *cmd = arg;
   1759   1.1  christos 	int s;
   1760   1.1  christos 
   1761   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1762   1.1  christos 
   1763   1.1  christos 	s = splnet();
   1764   1.1  christos 	athn_delete_key(ic, cmd->ni, cmd->key);
   1765   1.1  christos 	if (cmd->ni != NULL)
   1766   1.1  christos 		ieee80211_free_node(cmd->ni);
   1767   1.1  christos 	splx(s);
   1768   1.1  christos }
   1769   1.1  christos #endif /* notyet */
   1770   1.1  christos 
   1771   1.1  christos #ifndef IEEE80211_STA_ONLY
   1772   1.1  christos Static void
   1773   1.9     skrll athn_usb_bcneof(struct usbd_xfer *xfer, void * priv,
   1774   1.1  christos     usbd_status status)
   1775   1.1  christos {
   1776   1.1  christos 	struct athn_usb_tx_data *data = priv;
   1777   1.1  christos 	struct athn_usb_softc *usc = data->sc;
   1778   1.1  christos 
   1779   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1780   1.1  christos 
   1781   1.1  christos 	if (__predict_false(status == USBD_STALLED))
   1782   1.1  christos 		usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
   1783   1.1  christos 	usc->usc_tx_bcn = data;
   1784   1.1  christos }
   1785   1.1  christos 
   1786   1.1  christos /*
   1787   1.1  christos  * Process Software Beacon Alert interrupts.
   1788   1.1  christos  */
   1789   1.1  christos Static void
   1790   1.1  christos athn_usb_swba(struct athn_usb_softc *usc)
   1791   1.1  christos {
   1792   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
   1793   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1794   1.1  christos 	struct athn_usb_tx_data *data;
   1795   1.1  christos 	struct ieee80211_frame *wh;
   1796   1.1  christos 	struct ieee80211_beacon_offsets bo;
   1797   1.1  christos 	struct ar_stream_hdr *hdr;
   1798   1.1  christos 	struct ar_htc_frame_hdr *htc;
   1799   1.1  christos 	struct ar_tx_bcn *bcn;
   1800   1.1  christos 	struct mbuf *m;
   1801   1.1  christos 	int error;
   1802   1.1  christos 
   1803   1.1  christos 	if (usc->usc_dying)
   1804   1.1  christos 		return;
   1805   1.1  christos 
   1806   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   1807   1.1  christos 
   1808   1.1  christos 	if (ic->ic_dtim_count == 0)
   1809   1.1  christos 		ic->ic_dtim_count = ic->ic_dtim_period - 1;
   1810   1.1  christos 	else
   1811   1.1  christos 		ic->ic_dtim_count--;
   1812   1.1  christos 
   1813   1.1  christos 	/* Make sure previous beacon has been sent. */
   1814   1.1  christos 	if (usc->usc_tx_bcn == NULL)
   1815   1.1  christos 		return;
   1816   1.1  christos 	data = usc->usc_tx_bcn;
   1817   1.1  christos 
   1818   1.1  christos 	/* Get new beacon. */
   1819   1.1  christos #ifdef ATHN_DEBUG
   1820   1.1  christos 	memset(&bo, 0, sizeof(bo));
   1821   1.1  christos #endif
   1822   1.1  christos 	m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
   1823   1.1  christos 	if (__predict_false(m == NULL))
   1824   1.1  christos 		return;
   1825   1.1  christos 	/* Assign sequence number. */
   1826   1.1  christos 	/* XXX: use non-QoS tid? */
   1827   1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1828   1.1  christos 	*(uint16_t *)&wh->i_seq[0] =
   1829   1.1  christos 	    htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
   1830   1.1  christos 	ic->ic_bss->ni_txseqs[0]++;
   1831   1.1  christos 
   1832   1.1  christos 	hdr = (struct ar_stream_hdr *)data->buf;
   1833   1.1  christos 	hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
   1834   1.1  christos 	hdr->len = htole16(sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len);
   1835   1.1  christos 
   1836   1.1  christos 	htc = (struct ar_htc_frame_hdr *)&hdr[1];
   1837   1.1  christos 	memset(htc, 0, sizeof(*htc));
   1838   1.1  christos 	htc->endpoint_id = usc->usc_ep_bcn;
   1839   1.1  christos 	htc->payload_len = htobe16(sizeof(*bcn) + m->m_pkthdr.len);
   1840   1.1  christos 
   1841   1.1  christos 	bcn = (struct ar_tx_bcn *)&htc[1];
   1842   1.1  christos 	memset(bcn, 0, sizeof(*bcn));
   1843   1.1  christos 	bcn->vif_idx = 0;
   1844   1.1  christos 
   1845   1.1  christos 	m_copydata(m, 0, m->m_pkthdr.len, (void *)&bcn[1]);
   1846   1.1  christos 
   1847   1.9     skrll 	usbd_setup_xfer(data->xfer, data, data->buf,
   1848   1.1  christos 	    sizeof(*hdr) + sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len,
   1849   1.9     skrll 	    USBD_SHORT_XFER_OK, ATHN_USB_TX_TIMEOUT,
   1850   1.1  christos 	    athn_usb_bcneof);
   1851   1.1  christos 
   1852   1.1  christos 	m_freem(m);
   1853   1.1  christos 	usc->usc_tx_bcn = NULL;
   1854   1.1  christos 	error = usbd_transfer(data->xfer);
   1855   1.1  christos 	if (__predict_false(error != USBD_IN_PROGRESS && error != 0))
   1856   1.1  christos 		usc->usc_tx_bcn = data;
   1857   1.1  christos }
   1858   1.1  christos #endif
   1859   1.1  christos 
   1860   1.1  christos Static void
   1861   1.3  christos athn_usb_rx_wmi_ctrl(struct athn_usb_softc *usc, uint8_t *buf, size_t len)
   1862   1.1  christos {
   1863   1.1  christos #ifdef ATHN_DEBUG
   1864   1.1  christos 	struct ar_wmi_evt_txrate *txrate;
   1865   1.1  christos #endif
   1866   1.1  christos 	struct ar_wmi_cmd_hdr *wmi;
   1867   1.1  christos 	uint16_t cmd_id;
   1868   1.1  christos 
   1869   1.1  christos 	if (usc->usc_dying)
   1870   1.1  christos 		return;
   1871   1.1  christos 
   1872   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1873   1.1  christos 
   1874   1.3  christos 	if (__predict_false(len < sizeof(*wmi)))
   1875   1.1  christos 		return;
   1876   1.1  christos 	wmi = (struct ar_wmi_cmd_hdr *)buf;
   1877   1.1  christos 	cmd_id = be16toh(wmi->cmd_id);
   1878   1.1  christos 
   1879   1.1  christos 	if (!(cmd_id & AR_WMI_EVT_FLAG)) {
   1880   1.1  christos 		if (usc->usc_wait_cmd_id != cmd_id)
   1881   1.1  christos 			return;	/* Unexpected reply. */
   1882   1.1  christos 		if (usc->usc_obuf != NULL) {
   1883   1.1  christos 			/* Copy answer into caller supplied buffer. */
   1884   1.1  christos 			memcpy(usc->usc_obuf, &wmi[1], len - sizeof(*wmi));
   1885   1.1  christos 		}
   1886   1.1  christos 		/* Notify caller of completion. */
   1887   1.1  christos 		usc->usc_wait_cmd_id = 0;
   1888   1.1  christos 		wakeup(&usc->usc_wait_cmd_id);
   1889   1.1  christos 		return;
   1890   1.1  christos 	}
   1891   1.1  christos 	/*
   1892   1.1  christos 	 * XXX: the Linux 2.6 and 3.7.4 kernels differ on the event numbers!
   1893   1.1  christos 	 * See the alternate defines in if_athn_usb.h.
   1894   1.1  christos 	 */
   1895   1.1  christos 	switch (cmd_id & 0xfff) {
   1896   1.1  christos #ifndef IEEE80211_STA_ONLY
   1897   1.1  christos 	case AR_WMI_EVT_SWBA:
   1898   1.1  christos 		athn_usb_swba(usc);
   1899   1.1  christos 		break;
   1900   1.1  christos #endif
   1901   1.1  christos 	case AR_WMI_EVT_FATAL:
   1902   1.1  christos 		aprint_error_dev(usc->usc_dev, "fatal firmware error\n");
   1903   1.1  christos 		break;
   1904   1.1  christos 	case AR_WMI_EVT_TXRATE:
   1905   1.1  christos #ifdef ATHN_DEBUG
   1906   1.1  christos 		txrate = (struct ar_wmi_evt_txrate *)&wmi[1];
   1907   1.1  christos 		DPRINTFN(DBG_TX, usc, "txrate=%d\n", be32toh(txrate->txrate));
   1908   1.1  christos #endif
   1909   1.1  christos 		break;
   1910   1.1  christos 	default:
   1911   1.1  christos 		DPRINTFN(DBG_TX, usc, "WMI event 0x%x (%d) ignored\n", cmd_id, cmd_id);
   1912   1.1  christos 		break;
   1913   1.1  christos 	}
   1914   1.1  christos }
   1915   1.1  christos 
   1916   1.1  christos Static void
   1917   1.9     skrll athn_usb_intr(struct usbd_xfer *xfer, void * priv,
   1918   1.1  christos     usbd_status status)
   1919   1.1  christos {
   1920   1.1  christos 	struct athn_usb_softc *usc = priv;
   1921   1.1  christos 	struct ar_htc_frame_hdr *htc;
   1922   1.1  christos 	struct ar_htc_msg_hdr *msg;
   1923   1.1  christos 	uint8_t *buf = usc->usc_ibuf;
   1924   1.1  christos 	uint16_t msg_id;
   1925   1.1  christos 	int len;
   1926   1.1  christos 
   1927   1.1  christos 	if (usc->usc_dying)
   1928   1.1  christos 		return;
   1929   1.1  christos 
   1930   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   1931   1.1  christos 
   1932   1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   1933   1.1  christos 		DPRINTFN(DBG_INTR, usc, "intr status=%d\n", status);
   1934   1.1  christos 		if (status == USBD_STALLED)
   1935   1.1  christos 			usbd_clear_endpoint_stall_async(usc->usc_rx_intr_pipe);
   1936   1.1  christos 		return;
   1937   1.1  christos 	}
   1938   1.1  christos 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   1939   1.1  christos 
   1940   1.1  christos 	/* Skip watchdog pattern if present. */
   1941   1.1  christos 	if (len >= 4 && *(uint32_t *)buf == htobe32(0x00c60000)) {
   1942   1.1  christos 		buf += 4;
   1943   1.1  christos 		len -= 4;
   1944   1.1  christos 	}
   1945   1.1  christos 	if (__predict_false(len < (int)sizeof(*htc)))
   1946   1.1  christos 		return;
   1947   1.1  christos 	htc = (struct ar_htc_frame_hdr *)buf;
   1948   1.1  christos 	/* Skip HTC header. */
   1949   1.1  christos 	buf += sizeof(*htc);
   1950   1.1  christos 	len -= sizeof(*htc);
   1951   1.1  christos 
   1952   1.1  christos 	if (htc->endpoint_id != 0) {
   1953   1.1  christos 		if (__predict_false(htc->endpoint_id != usc->usc_ep_ctrl))
   1954   1.1  christos 			return;
   1955   1.1  christos 		/* Remove trailer if present. */
   1956   1.1  christos 		if (htc->flags & AR_HTC_FLAG_TRAILER) {
   1957   1.1  christos 			if (__predict_false(len < htc->control[0]))
   1958   1.1  christos 				return;
   1959   1.1  christos 			len -= htc->control[0];
   1960   1.1  christos 		}
   1961   1.1  christos 		athn_usb_rx_wmi_ctrl(usc, buf, len);
   1962   1.1  christos 		return;
   1963   1.1  christos 	}
   1964   1.1  christos 
   1965   1.1  christos 	/*
   1966   1.1  christos 	 * Endpoint 0 carries HTC messages.
   1967   1.1  christos 	 */
   1968   1.1  christos 	if (__predict_false(len < (int)sizeof(*msg)))
   1969   1.1  christos 		return;
   1970   1.1  christos 	msg = (struct ar_htc_msg_hdr *)buf;
   1971   1.1  christos 	msg_id = be16toh(msg->msg_id);
   1972   1.1  christos 	DPRINTFN(DBG_RX, usc, "Rx HTC message %d\n", msg_id);
   1973   1.1  christos 	switch (msg_id) {
   1974   1.1  christos 	case AR_HTC_MSG_READY:
   1975   1.1  christos 	case AR_HTC_MSG_CONF_PIPE_RSP:
   1976   1.1  christos 		if (usc->usc_wait_msg_id != msg_id)
   1977   1.1  christos 			break;
   1978   1.1  christos 		usc->usc_wait_msg_id = 0;
   1979   1.1  christos 		wakeup(&usc->usc_wait_msg_id);
   1980   1.1  christos 		break;
   1981   1.1  christos 	case AR_HTC_MSG_CONN_SVC_RSP:
   1982   1.1  christos 		if (usc->usc_wait_msg_id != msg_id)
   1983   1.1  christos 			break;
   1984   1.1  christos 		if (usc->usc_msg_conn_svc_rsp != NULL) {
   1985   1.1  christos 			memcpy(usc->usc_msg_conn_svc_rsp, &msg[1],
   1986   1.1  christos 			    sizeof(*usc->usc_msg_conn_svc_rsp));
   1987   1.1  christos 		}
   1988   1.1  christos 		usc->usc_wait_msg_id = 0;
   1989   1.1  christos 		wakeup(&usc->usc_wait_msg_id);
   1990   1.1  christos 		break;
   1991   1.1  christos 	default:
   1992   1.1  christos 		DPRINTFN(DBG_RX, usc, "HTC message %d ignored\n", msg_id);
   1993   1.1  christos 		break;
   1994   1.1  christos 	}
   1995   1.1  christos }
   1996   1.1  christos 
   1997   1.1  christos Static void
   1998   1.1  christos athn_usb_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
   1999   1.1  christos     struct ar_rx_status *rs)
   2000   1.1  christos {
   2001   1.1  christos 	struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
   2002   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2003   1.1  christos 	uint8_t rate;
   2004   1.1  christos 
   2005   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2006   1.1  christos 
   2007   1.1  christos 	tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
   2008   1.1  christos 	tap->wr_tsft = htole64(be64toh(rs->rs_tstamp));
   2009   1.1  christos 	tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2010   1.1  christos 	tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2011   1.1  christos 	tap->wr_dbm_antsignal = rs->rs_rssi;
   2012   1.1  christos 	/* XXX noise. */
   2013   1.1  christos 	tap->wr_antenna = rs->rs_antenna;
   2014   1.1  christos 	rate = rs->rs_rate;
   2015   1.1  christos 	if (rate & 0x80) {		/* HT. */
   2016   1.1  christos 		/* Bit 7 set means HT MCS instead of rate. */
   2017   1.1  christos 		tap->wr_rate = rate;
   2018   1.1  christos 		if (!(rs->rs_flags & AR_RXS_FLAG_GI))
   2019   1.1  christos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
   2020   1.1  christos 	}
   2021   1.1  christos 	else if (rate & 0x10) {	/* CCK. */
   2022   1.1  christos 		if (rate & 0x04)
   2023   1.1  christos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   2024   1.1  christos 		switch (rate & ~0x14) {
   2025   1.1  christos 		case 0xb: tap->wr_rate =   2; break;
   2026   1.1  christos 		case 0xa: tap->wr_rate =   4; break;
   2027   1.1  christos 		case 0x9: tap->wr_rate =  11; break;
   2028   1.1  christos 		case 0x8: tap->wr_rate =  22; break;
   2029   1.1  christos 		default:  tap->wr_rate =   0; break;
   2030   1.1  christos 		}
   2031   1.1  christos 	}
   2032   1.1  christos 	else {			/* OFDM. */
   2033   1.1  christos 		switch (rate) {
   2034   1.1  christos 		case 0xb: tap->wr_rate =  12; break;
   2035   1.1  christos 		case 0xf: tap->wr_rate =  18; break;
   2036   1.1  christos 		case 0xa: tap->wr_rate =  24; break;
   2037   1.1  christos 		case 0xe: tap->wr_rate =  36; break;
   2038   1.1  christos 		case 0x9: tap->wr_rate =  48; break;
   2039   1.1  christos 		case 0xd: tap->wr_rate =  72; break;
   2040   1.1  christos 		case 0x8: tap->wr_rate =  96; break;
   2041   1.1  christos 		case 0xc: tap->wr_rate = 108; break;
   2042   1.1  christos 		default:  tap->wr_rate =   0; break;
   2043   1.1  christos 		}
   2044   1.1  christos 	}
   2045   1.1  christos 	bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   2046   1.1  christos }
   2047   1.1  christos 
   2048   1.1  christos Static void
   2049   1.1  christos athn_usb_rx_frame(struct athn_usb_softc *usc, struct mbuf *m)
   2050   1.1  christos {
   2051   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
   2052   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2053   1.1  christos 	struct ifnet *ifp = &sc->sc_if;
   2054   1.1  christos 	struct ieee80211_frame *wh;
   2055   1.1  christos 	struct ieee80211_node *ni;
   2056   1.1  christos 	struct ar_htc_frame_hdr *htc;
   2057   1.1  christos 	struct ar_rx_status *rs;
   2058   1.1  christos 	uint16_t datalen;
   2059   1.1  christos 	int s;
   2060   1.1  christos 
   2061   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2062   1.1  christos 
   2063   1.1  christos 	if (__predict_false(m->m_len < (int)sizeof(*htc)))
   2064   1.1  christos 		goto skip;
   2065   1.1  christos 	htc = mtod(m, struct ar_htc_frame_hdr *);
   2066   1.1  christos 	if (__predict_false(htc->endpoint_id == 0)) {
   2067   1.1  christos 		DPRINTFN(DBG_RX, sc, "bad endpoint %d\n", htc->endpoint_id);
   2068   1.1  christos 		goto skip;
   2069   1.1  christos 	}
   2070   1.1  christos 	if (htc->flags & AR_HTC_FLAG_TRAILER) {
   2071   1.1  christos 		if (m->m_len < htc->control[0])
   2072   1.1  christos 			goto skip;
   2073   1.1  christos 		m_adj(m, -(int)htc->control[0]);
   2074   1.1  christos 	}
   2075   1.1  christos 	m_adj(m, sizeof(*htc));	/* Strip HTC header. */
   2076   1.1  christos 
   2077   1.1  christos 	if (__predict_false(m->m_len < (int)sizeof(*rs)))
   2078   1.1  christos 		goto skip;
   2079   1.1  christos 	rs = mtod(m, struct ar_rx_status *);
   2080   1.1  christos 
   2081   1.1  christos 	/* Make sure that payload fits. */
   2082   1.1  christos 	datalen = be16toh(rs->rs_datalen);
   2083   1.1  christos 	if (__predict_false(m->m_len < (int)sizeof(*rs) + datalen))
   2084   1.1  christos 		goto skip;
   2085   1.1  christos 
   2086   1.1  christos 	/* Ignore runt frames.  Let ACKs be seen by bpf */
   2087   1.1  christos 	if (__predict_false(datalen <
   2088   1.1  christos 		sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN))
   2089   1.1  christos 		goto skip;
   2090   1.1  christos 
   2091   1.1  christos 	m_adj(m, sizeof(*rs));	/* Strip Rx status. */
   2092  1.12     ozaki 	m_set_rcvif(m, ifp);
   2093   1.1  christos 
   2094   1.1  christos 	s = splnet();
   2095   1.1  christos 
   2096   1.1  christos 	/* Grab a reference to the source node. */
   2097   1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   2098   1.1  christos 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   2099   1.1  christos 
   2100   1.1  christos 	/* Remove any HW padding after the 802.11 header. */
   2101   1.1  christos 	if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
   2102   1.1  christos 		u_int hdrlen = ieee80211_anyhdrsize(wh);
   2103   1.1  christos 		if (hdrlen & 3) {
   2104   1.1  christos 			ovbcopy(wh, (uint8_t *)wh + 2, hdrlen);
   2105   1.1  christos 			m_adj(m, 2);
   2106   1.1  christos 		}
   2107   1.1  christos 	}
   2108   1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL))
   2109   1.1  christos 		athn_usb_rx_radiotap(sc, m, rs);
   2110   1.1  christos 
   2111   1.1  christos 	/* Trim 802.11 FCS after radiotap. */
   2112   1.1  christos 	m_adj(m, -IEEE80211_CRC_LEN);
   2113   1.1  christos 
   2114   1.1  christos 	/* Send the frame to the 802.11 layer. */
   2115   1.1  christos 	ieee80211_input(ic, m, ni, rs->rs_rssi + AR_USB_DEFAULT_NF, 0);
   2116   1.1  christos 
   2117   1.1  christos 	/* Node is no longer needed. */
   2118   1.1  christos 	ieee80211_free_node(ni);
   2119   1.1  christos 	splx(s);
   2120   1.1  christos 	return;
   2121   1.1  christos  skip:
   2122   1.1  christos 	m_freem(m);
   2123   1.1  christos }
   2124   1.1  christos 
   2125   1.1  christos Static void
   2126   1.9     skrll athn_usb_rxeof(struct usbd_xfer *xfer, void * priv,
   2127   1.1  christos     usbd_status status)
   2128   1.1  christos {
   2129   1.1  christos 	struct athn_usb_rx_data *data = priv;
   2130   1.1  christos 	struct athn_usb_softc *usc = data->sc;
   2131   1.1  christos 	struct athn_usb_rx_stream *stream = &usc->usc_rx_stream;
   2132   1.1  christos 	uint8_t *buf = data->buf;
   2133   1.1  christos 	struct ar_stream_hdr *hdr;
   2134   1.1  christos 	struct mbuf *m;
   2135   1.1  christos 	uint16_t pktlen;
   2136   1.1  christos 	int off, len;
   2137   1.1  christos 
   2138   1.1  christos 	if (usc->usc_dying)
   2139   1.1  christos 		return;
   2140   1.1  christos 
   2141   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   2142   1.1  christos 
   2143   1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   2144   1.1  christos 		DPRINTFN(DBG_RX, usc, "RX status=%d\n", status);
   2145   1.1  christos 		if (status == USBD_STALLED)
   2146   1.1  christos 			usbd_clear_endpoint_stall_async(usc->usc_rx_data_pipe);
   2147   1.1  christos 		if (status != USBD_CANCELLED)
   2148   1.1  christos 			goto resubmit;
   2149   1.1  christos 		return;
   2150   1.1  christos 	}
   2151   1.1  christos 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   2152   1.1  christos 
   2153   1.1  christos 	if (stream->left > 0) {
   2154   1.1  christos 		if (len >= stream->left) {
   2155   1.1  christos 			/* We have all our pktlen bytes now. */
   2156   1.1  christos 			if (__predict_true(stream->m != NULL)) {
   2157   1.1  christos 				memcpy(mtod(stream->m, uint8_t *) +
   2158   1.1  christos 				    stream->moff, buf, stream->left);
   2159   1.1  christos 				athn_usb_rx_frame(usc, stream->m);
   2160   1.1  christos 				stream->m = NULL;
   2161   1.1  christos 			}
   2162   1.1  christos 			/* Next header is 32-bit aligned. */
   2163   1.1  christos 			off = (stream->left + 3) & ~3;
   2164   1.1  christos 			buf += off;
   2165   1.1  christos 			len -= off;
   2166   1.1  christos 			stream->left = 0;
   2167   1.1  christos 		}
   2168   1.1  christos 		else {
   2169   1.1  christos 			/* Still need more bytes, save what we have. */
   2170   1.1  christos 			if (__predict_true(stream->m != NULL)) {
   2171   1.1  christos 				memcpy(mtod(stream->m, uint8_t *) +
   2172   1.1  christos 				    stream->moff, buf, len);
   2173   1.1  christos 				stream->moff += len;
   2174   1.1  christos 			}
   2175   1.1  christos 			stream->left -= len;
   2176   1.1  christos 			goto resubmit;
   2177   1.1  christos 		}
   2178   1.1  christos 	}
   2179   1.1  christos 	KASSERT(stream->left == 0);
   2180   1.1  christos 	while (len >= (int)sizeof(*hdr)) {
   2181   1.1  christos 		hdr = (struct ar_stream_hdr *)buf;
   2182   1.1  christos 		if (hdr->tag != htole16(AR_USB_RX_STREAM_TAG)) {
   2183   1.1  christos 			DPRINTFN(DBG_RX, usc, "invalid tag 0x%x\n", hdr->tag);
   2184   1.1  christos 			break;
   2185   1.1  christos 		}
   2186   1.1  christos 		pktlen = le16toh(hdr->len);
   2187   1.1  christos 		buf += sizeof(*hdr);
   2188   1.1  christos 		len -= sizeof(*hdr);
   2189   1.1  christos 
   2190   1.1  christos 		if (__predict_true(pktlen <= MCLBYTES)) {
   2191   1.1  christos 			/* Allocate an mbuf to store the next pktlen bytes. */
   2192   1.1  christos 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   2193   1.1  christos 			if (__predict_true(m != NULL)) {
   2194   1.1  christos 				m->m_pkthdr.len = m->m_len = pktlen;
   2195   1.1  christos 				if (pktlen > MHLEN) {
   2196   1.1  christos 					MCLGET(m, M_DONTWAIT);
   2197   1.1  christos 					if (!(m->m_flags & M_EXT)) {
   2198   1.1  christos 						m_free(m);
   2199   1.1  christos 						m = NULL;
   2200   1.1  christos 					}
   2201   1.1  christos 				}
   2202   1.1  christos 			}
   2203   1.1  christos 		}
   2204   1.1  christos 		else	/* Drop frames larger than MCLBYTES. */
   2205   1.1  christos 			m = NULL;
   2206   1.1  christos 		/*
   2207   1.1  christos 		 * NB: m can be NULL, in which case the next pktlen bytes
   2208   1.1  christos 		 * will be discarded from the Rx stream.
   2209   1.1  christos 		 */
   2210   1.1  christos 		if (pktlen > len) {
   2211   1.1  christos 			/* Need more bytes, save what we have. */
   2212   1.1  christos 			stream->m = m;	/* NB: m can be NULL. */
   2213   1.1  christos 			if (__predict_true(stream->m != NULL)) {
   2214   1.1  christos 				memcpy(mtod(stream->m, uint8_t *), buf, len);
   2215   1.1  christos 				stream->moff = len;
   2216   1.1  christos 			}
   2217   1.1  christos 			stream->left = pktlen - len;
   2218   1.1  christos 			goto resubmit;
   2219   1.1  christos 		}
   2220   1.1  christos 		if (__predict_true(m != NULL)) {
   2221   1.1  christos 			/* We have all the pktlen bytes in this xfer. */
   2222   1.1  christos 			memcpy(mtod(m, uint8_t *), buf, pktlen);
   2223   1.1  christos 			athn_usb_rx_frame(usc, m);
   2224   1.1  christos 		}
   2225   1.1  christos 
   2226   1.1  christos 		/* Next header is 32-bit aligned. */
   2227   1.1  christos 		off = (pktlen + 3) & ~3;
   2228   1.1  christos 		buf += off;
   2229   1.1  christos 		len -= off;
   2230   1.1  christos 	}
   2231   1.1  christos 
   2232   1.1  christos  resubmit:
   2233   1.1  christos 	/* Setup a new transfer. */
   2234   1.9     skrll 	usbd_setup_xfer(xfer, data, data->buf, ATHN_USB_RXBUFSZ,
   2235   1.9     skrll 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, athn_usb_rxeof);
   2236   1.1  christos 	(void)usbd_transfer(xfer);
   2237   1.1  christos }
   2238   1.1  christos 
   2239   1.1  christos Static void
   2240   1.9     skrll athn_usb_txeof(struct usbd_xfer *xfer, void * priv,
   2241   1.1  christos     usbd_status status)
   2242   1.1  christos {
   2243   1.1  christos 	struct athn_usb_tx_data *data = priv;
   2244   1.1  christos 	struct athn_usb_softc *usc = data->sc;
   2245   1.1  christos 	struct athn_softc *sc = &usc->usc_sc;
   2246   1.1  christos 	struct ifnet *ifp = &sc->sc_if;
   2247   1.1  christos 	int s;
   2248   1.1  christos 
   2249   1.1  christos 	if (usc->usc_dying)
   2250   1.1  christos 		return;
   2251   1.1  christos 
   2252   1.1  christos 	DPRINTFN(DBG_FN, usc, "\n");
   2253   1.1  christos 
   2254   1.1  christos 	s = splnet();
   2255   1.1  christos 	/* Put this Tx buffer back to our free list. */
   2256   1.1  christos 	mutex_enter(&usc->usc_tx_mtx);
   2257   1.1  christos 	TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
   2258   1.1  christos 	mutex_exit(&usc->usc_tx_mtx);
   2259   1.1  christos 
   2260   1.1  christos 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   2261   1.1  christos 		DPRINTFN(DBG_TX, sc, "TX status=%d\n", status);
   2262   1.1  christos 		if (status == USBD_STALLED)
   2263   1.1  christos 			usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
   2264   1.1  christos 		ifp->if_oerrors++;
   2265   1.1  christos 		splx(s);
   2266   1.1  christos 		/* XXX Why return? */
   2267   1.1  christos 		return;
   2268   1.1  christos 	}
   2269   1.1  christos 	sc->sc_tx_timer = 0;
   2270   1.1  christos 	ifp->if_opackets++;
   2271   1.1  christos 
   2272   1.1  christos 	/* We just released a Tx buffer, notify Tx. */
   2273   1.1  christos 	if (ifp->if_flags & IFF_OACTIVE) {
   2274   1.1  christos 		ifp->if_flags &= ~IFF_OACTIVE;
   2275   1.1  christos 		ifp->if_start(ifp);
   2276   1.1  christos 	}
   2277   1.1  christos 	splx(s);
   2278   1.1  christos }
   2279   1.1  christos 
   2280   1.1  christos Static int
   2281   1.1  christos athn_usb_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
   2282  1.14     skrll     struct athn_usb_tx_data *data)
   2283   1.1  christos {
   2284   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   2285   1.1  christos 	struct athn_node *an = ATHN_NODE(ni);
   2286   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2287   1.1  christos 	struct ieee80211_frame *wh;
   2288   1.1  christos 	struct ieee80211_key *k = NULL;
   2289   1.1  christos 	struct ar_stream_hdr *hdr;
   2290   1.1  christos 	struct ar_htc_frame_hdr *htc;
   2291   1.1  christos 	struct ar_tx_frame *txf;
   2292   1.1  christos 	struct ar_tx_mgmt *txm;
   2293   1.1  christos 	uint8_t *frm;
   2294   1.1  christos 	uint8_t sta_index, qid, tid;
   2295   1.1  christos 	int error, s, xferlen;
   2296   1.1  christos 
   2297   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2298   1.1  christos 
   2299   1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   2300   1.1  christos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
   2301   1.1  christos 		k = ieee80211_crypto_encap(ic, ni, m);
   2302   1.1  christos 		if (k == NULL)
   2303   1.1  christos 			return ENOBUFS;
   2304   1.1  christos 
   2305   1.1  christos 		/* packet header may have moved, reset our local pointer */
   2306   1.1  christos 		wh = mtod(m, struct ieee80211_frame *);
   2307   1.1  christos 	}
   2308   1.1  christos #ifdef notyet_edca
   2309   1.1  christos 	if (ieee80211_has_qos(wh)) {
   2310   1.1  christos 		uint16_t qos;
   2311   1.1  christos 
   2312   1.1  christos 		qos = ieee80211_get_qos(wh);
   2313   1.1  christos 		tid = qos & IEEE80211_QOS_TID;
   2314   1.1  christos 		qid = ieee80211_up_to_ac(ic, tid);
   2315   1.1  christos 	}
   2316   1.1  christos 	else
   2317   1.1  christos #endif /* notyet_edca */
   2318   1.1  christos 	{
   2319   1.1  christos 		tid = 0;
   2320   1.3  christos 		qid = WME_AC_BE;
   2321   1.1  christos 	}
   2322   1.1  christos 
   2323   1.1  christos 	/* XXX Change radiotap Tx header for USB (no txrate). */
   2324   1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   2325   1.1  christos 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
   2326   1.1  christos 
   2327   1.1  christos 		tap->wt_flags = 0;
   2328   1.1  christos 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2329   1.1  christos 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2330   1.1  christos 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
   2331   1.1  christos 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   2332   1.1  christos 
   2333   1.1  christos 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
   2334   1.1  christos 	}
   2335   1.1  christos 	sta_index = an->sta_index;
   2336   1.1  christos 
   2337   1.1  christos 	/* NB: We don't take advantage of USB Tx stream mode for now. */
   2338   1.1  christos 	hdr = (struct ar_stream_hdr *)data->buf;
   2339   1.1  christos 	hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
   2340   1.1  christos 
   2341   1.1  christos 	htc = (struct ar_htc_frame_hdr *)&hdr[1];
   2342   1.1  christos 	memset(htc, 0, sizeof(*htc));
   2343   1.1  christos 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   2344   1.1  christos 	    IEEE80211_FC0_TYPE_DATA) {
   2345   1.1  christos 		htc->endpoint_id = usc->usc_ep_data[qid];
   2346   1.1  christos 
   2347   1.1  christos 		txf = (struct ar_tx_frame *)&htc[1];
   2348   1.1  christos 		memset(txf, 0, sizeof(*txf));
   2349   1.1  christos 		txf->data_type = AR_HTC_NORMAL;
   2350   1.1  christos 		txf->node_idx = sta_index;
   2351   1.1  christos 		txf->vif_idx = 0;
   2352   1.1  christos 		txf->tid = tid;
   2353   1.1  christos 		if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
   2354   1.1  christos 			txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
   2355   1.1  christos 		else if (ic->ic_flags & IEEE80211_F_USEPROT) {
   2356   1.1  christos 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2357   1.1  christos 				txf->flags |= htobe32(AR_HTC_TX_CTSONLY);
   2358   1.1  christos 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2359   1.1  christos 				txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
   2360   1.1  christos 		}
   2361   1.1  christos 		txf->key_idx = 0xff;
   2362   1.1  christos 		frm = (uint8_t *)&txf[1];
   2363   1.1  christos 	}
   2364   1.1  christos 	else {
   2365   1.1  christos 		htc->endpoint_id = usc->usc_ep_mgmt;
   2366   1.1  christos 
   2367   1.1  christos 		txm = (struct ar_tx_mgmt *)&htc[1];
   2368   1.1  christos 		memset(txm, 0, sizeof(*txm));
   2369   1.1  christos 		txm->node_idx = sta_index;
   2370   1.1  christos 		txm->vif_idx = 0;
   2371   1.1  christos 		txm->key_idx = 0xff;
   2372   1.1  christos 		frm = (uint8_t *)&txm[1];
   2373   1.1  christos 	}
   2374   1.1  christos 	/* Copy payload. */
   2375   1.1  christos 	m_copydata(m, 0, m->m_pkthdr.len, (void *)frm);
   2376   1.1  christos 	frm += m->m_pkthdr.len;
   2377   1.1  christos 
   2378   1.1  christos 	/* Finalize headers. */
   2379   1.1  christos 	htc->payload_len = htobe16(frm - (uint8_t *)&htc[1]);
   2380   1.1  christos 	hdr->len = htole16(frm - (uint8_t *)&hdr[1]);
   2381   1.1  christos 	xferlen = frm - data->buf;
   2382   1.1  christos 
   2383   1.1  christos 	s = splnet();
   2384   1.9     skrll 	usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
   2385   1.9     skrll 	    USBD_FORCE_SHORT_XFER, ATHN_USB_TX_TIMEOUT, athn_usb_txeof);
   2386   1.1  christos 	error = usbd_transfer(data->xfer);
   2387   1.1  christos 	if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) {
   2388   1.1  christos 		splx(s);
   2389   1.1  christos 		return error;
   2390   1.1  christos 	}
   2391   1.1  christos 	splx(s);
   2392   1.1  christos 	return 0;
   2393   1.1  christos }
   2394   1.1  christos 
   2395   1.1  christos Static void
   2396   1.1  christos athn_usb_start(struct ifnet *ifp)
   2397   1.1  christos {
   2398   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   2399   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   2400   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2401   1.1  christos 	struct athn_usb_tx_data *data;
   2402   1.1  christos 	struct ether_header *eh;
   2403   1.1  christos 	struct ieee80211_node *ni;
   2404   1.1  christos 	struct mbuf *m;
   2405   1.1  christos 
   2406   1.1  christos 	if (usc->usc_dying)
   2407   1.1  christos 		return;
   2408   1.1  christos 
   2409   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2410   1.1  christos 
   2411   1.1  christos 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   2412   1.1  christos 		return;
   2413   1.1  christos 
   2414   1.1  christos 	data = NULL;
   2415   1.1  christos 	for (;;) {
   2416   1.1  christos 		mutex_enter(&usc->usc_tx_mtx);
   2417   1.1  christos 		if (data == NULL && !TAILQ_EMPTY(&usc->usc_tx_free_list)) {
   2418   1.1  christos 			data = TAILQ_FIRST(&usc->usc_tx_free_list);
   2419   1.1  christos 			TAILQ_REMOVE(&usc->usc_tx_free_list, data, next);
   2420   1.1  christos 		}
   2421   1.1  christos 		mutex_exit(&usc->usc_tx_mtx);
   2422   1.1  christos 
   2423   1.1  christos 		if (data == NULL) {
   2424   1.1  christos 			ifp->if_flags |= IFF_OACTIVE;
   2425   1.1  christos 			return;
   2426   1.1  christos 		}
   2427   1.1  christos 
   2428   1.1  christos 		/* Send pending management frames first. */
   2429   1.1  christos 		IF_DEQUEUE(&ic->ic_mgtq, m);
   2430   1.1  christos 		if (m != NULL) {
   2431  1.10     ozaki 			ni = M_GETCTX(m, struct ieee80211_node *);
   2432  1.11     ozaki 			M_CLEARCTX(m);
   2433   1.1  christos 			goto sendit;
   2434   1.1  christos 		}
   2435   1.1  christos 		if (ic->ic_state != IEEE80211_S_RUN)
   2436   1.1  christos 			break;
   2437   1.1  christos 
   2438   1.1  christos 		/* Encapsulate and send data frames. */
   2439   1.1  christos 		IFQ_DEQUEUE(&ifp->if_snd, m);
   2440   1.1  christos 		if (m == NULL)
   2441   1.1  christos 			break;
   2442   1.1  christos 
   2443   1.1  christos 		if (m->m_len < (int)sizeof(*eh) &&
   2444   1.1  christos 		    (m = m_pullup(m, sizeof(*eh))) == NULL) {
   2445   1.1  christos 			ifp->if_oerrors++;
   2446   1.1  christos 			continue;
   2447   1.1  christos 		}
   2448   1.1  christos 		eh = mtod(m, struct ether_header *);
   2449   1.1  christos 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   2450   1.1  christos 		if (ni == NULL) {
   2451   1.1  christos 			m_freem(m);
   2452   1.1  christos 			ifp->if_oerrors++;
   2453   1.1  christos 			continue;
   2454   1.1  christos 		}
   2455   1.1  christos 
   2456   1.1  christos 		bpf_mtap(ifp, m);
   2457   1.1  christos 
   2458   1.1  christos 		if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
   2459   1.1  christos 			ieee80211_free_node(ni);
   2460   1.1  christos 			ifp->if_oerrors++;
   2461   1.1  christos 			continue;
   2462   1.1  christos 		}
   2463   1.1  christos  sendit:
   2464   1.1  christos 		bpf_mtap3(ic->ic_rawbpf, m);
   2465   1.1  christos 
   2466   1.1  christos 		if (athn_usb_tx(sc, m, ni, data) != 0) {
   2467   1.5  christos 			m_freem(m);
   2468   1.1  christos 			ieee80211_free_node(ni);
   2469   1.1  christos 			ifp->if_oerrors++;
   2470   1.1  christos 			continue;
   2471   1.1  christos 		}
   2472   1.1  christos 		data = NULL;
   2473   1.5  christos 		m_freem(m);
   2474   1.1  christos 		ieee80211_free_node(ni);
   2475   1.1  christos 		sc->sc_tx_timer = 5;
   2476   1.1  christos 		ifp->if_timer = 1;
   2477   1.1  christos 	}
   2478   1.1  christos 
   2479   1.1  christos 	/* Return the Tx buffer to the free list */
   2480   1.1  christos 	mutex_enter(&usc->usc_tx_mtx);
   2481   1.1  christos 	TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
   2482   1.1  christos 	mutex_exit(&usc->usc_tx_mtx);
   2483   1.1  christos }
   2484   1.1  christos 
   2485   1.1  christos Static void
   2486   1.1  christos athn_usb_watchdog(struct ifnet *ifp)
   2487   1.1  christos {
   2488   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   2489   1.1  christos 
   2490   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2491   1.1  christos 
   2492   1.1  christos 	ifp->if_timer = 0;
   2493   1.1  christos 
   2494   1.1  christos 	if (sc->sc_tx_timer > 0) {
   2495   1.1  christos 		if (--sc->sc_tx_timer == 0) {
   2496   1.1  christos 			aprint_error_dev(sc->sc_dev, "device timeout\n");
   2497   1.1  christos 			/* athn_usb_init(ifp); XXX needs a process context! */
   2498   1.1  christos 			ifp->if_oerrors++;
   2499   1.1  christos 			return;
   2500   1.1  christos 		}
   2501   1.1  christos 		ifp->if_timer = 1;
   2502   1.1  christos 	}
   2503   1.1  christos 	ieee80211_watchdog(&sc->sc_ic);
   2504   1.1  christos }
   2505   1.1  christos 
   2506   1.1  christos Static int
   2507   1.1  christos athn_usb_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2508   1.1  christos {
   2509   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   2510   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   2511   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2512   1.1  christos 	int s, error = 0;
   2513   1.1  christos 
   2514   1.1  christos 	if (usc->usc_dying)
   2515   1.1  christos 		return EIO;
   2516   1.1  christos 
   2517   1.1  christos 	DPRINTFN(DBG_FN, sc, "cmd=0x%08lx\n", cmd);
   2518   1.1  christos 
   2519   1.1  christos 	s = splnet();
   2520   1.1  christos 
   2521   1.1  christos 	switch (cmd) {
   2522   1.1  christos 	case SIOCSIFFLAGS:
   2523   1.1  christos 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   2524   1.1  christos 			break;
   2525   1.1  christos 
   2526   1.1  christos 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   2527   1.1  christos 		case IFF_UP | IFF_RUNNING:
   2528   1.1  christos 			break;
   2529   1.1  christos 		case IFF_UP:
   2530   1.1  christos 			error = athn_usb_init(ifp);
   2531   1.1  christos 			break;
   2532   1.1  christos 		case IFF_RUNNING:
   2533   1.1  christos 			athn_usb_stop(ifp);
   2534   1.1  christos 			break;
   2535   1.1  christos 		case 0:
   2536   1.1  christos 		default:
   2537   1.1  christos 			break;
   2538   1.1  christos 		}
   2539   1.1  christos 		break;
   2540   1.1  christos 
   2541   1.1  christos 	case SIOCADDMULTI:
   2542   1.1  christos 	case SIOCDELMULTI:
   2543   1.1  christos 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   2544   1.1  christos 			/* setup multicast filter, etc */
   2545   1.1  christos 			error = 0;
   2546   1.1  christos 		}
   2547   1.1  christos 		break;
   2548   1.1  christos 
   2549   1.1  christos 	case SIOCS80211CHANNEL:
   2550   1.1  christos 		error = ieee80211_ioctl(ic, cmd, data);
   2551   1.1  christos 		if (error == ENETRESET &&
   2552   1.1  christos 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
   2553   1.1  christos 			if (IS_UP_AND_RUNNING(ifp))
   2554   1.1  christos 				athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
   2555   1.1  christos 			error = 0;
   2556   1.1  christos 		}
   2557   1.1  christos 		break;
   2558   1.1  christos 
   2559   1.1  christos 	default:
   2560   1.1  christos 		error = ieee80211_ioctl(ic, cmd, data);
   2561   1.1  christos 		break;
   2562   1.1  christos 	}
   2563   1.1  christos 	if (error == ENETRESET) {
   2564   1.1  christos 		error = 0;
   2565   1.1  christos 		if (IS_UP_AND_RUNNING(ifp) &&
   2566   1.1  christos 		    ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
   2567   1.1  christos 			athn_usb_stop(ifp);
   2568   1.1  christos 			error = athn_usb_init(ifp);
   2569   1.1  christos 		}
   2570   1.1  christos 	}
   2571   1.1  christos 	splx(s);
   2572   1.1  christos 	return error;
   2573   1.1  christos }
   2574   1.1  christos 
   2575   1.1  christos Static int
   2576   1.1  christos athn_usb_init(struct ifnet *ifp)
   2577   1.1  christos {
   2578   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   2579   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   2580   1.1  christos 	struct athn_ops *ops = &sc->sc_ops;
   2581   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2582   1.1  christos 	struct ieee80211_channel *curchan, *extchan;
   2583   1.1  christos 	struct athn_usb_rx_data *data;
   2584   1.1  christos 	struct ar_htc_target_vif hvif;
   2585   1.1  christos 	struct ar_htc_target_sta sta;
   2586   1.1  christos 	struct ar_htc_cap_target hic;
   2587   1.1  christos 	uint16_t mode;
   2588   1.1  christos 	size_t i;
   2589   1.1  christos 	int error;
   2590   1.1  christos 
   2591   1.1  christos 	if (usc->usc_dying)
   2592   1.1  christos 		return USBD_CANCELLED;
   2593   1.1  christos 
   2594   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2595   1.1  christos 
   2596   1.1  christos 	/* Init host async commands ring. */
   2597   1.1  christos 	mutex_spin_enter(&usc->usc_task_mtx);
   2598   1.1  christos 	usc->usc_cmdq.cur = usc->usc_cmdq.next = usc->usc_cmdq.queued = 0;
   2599   1.1  christos 	mutex_spin_exit(&usc->usc_task_mtx);
   2600   1.1  christos 
   2601   1.1  christos 	/* Steal one buffer for beacons. */
   2602   1.1  christos 	mutex_enter(&usc->usc_tx_mtx);
   2603   1.1  christos 	usc->usc_tx_bcn = TAILQ_FIRST(&usc->usc_tx_free_list);
   2604   1.1  christos 	TAILQ_REMOVE(&usc->usc_tx_free_list, usc->usc_tx_bcn, next);
   2605   1.1  christos 	mutex_exit(&usc->usc_tx_mtx);
   2606   1.1  christos 
   2607   1.1  christos 	curchan = ic->ic_curchan;
   2608   1.1  christos 	extchan = NULL;
   2609   1.1  christos 
   2610   1.1  christos 	/* In case a new MAC address has been configured. */
   2611   1.1  christos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   2612   1.1  christos 
   2613   1.1  christos 	error = athn_set_power_awake(sc);
   2614   1.1  christos 	if (error != 0)
   2615   1.1  christos 		goto fail;
   2616   1.1  christos 
   2617   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_FLUSH_RECV);
   2618   1.1  christos 	if (error != 0)
   2619   1.1  christos 		goto fail;
   2620   1.1  christos 
   2621   1.1  christos 	error = athn_hw_reset(sc, curchan, extchan, 1);
   2622   1.1  christos 	if (error != 0)
   2623   1.1  christos 		goto fail;
   2624   1.1  christos 
   2625   1.1  christos 	ops->set_txpower(sc, curchan, extchan);
   2626   1.1  christos 
   2627   1.1  christos 	mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
   2628   1.1  christos 	    AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
   2629   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
   2630   1.1  christos 	    &mode, sizeof(mode), NULL);
   2631   1.1  christos 	if (error != 0)
   2632   1.1  christos 		goto fail;
   2633   1.1  christos 
   2634   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ATH_INIT);
   2635   1.1  christos 	if (error != 0)
   2636   1.1  christos 		goto fail;
   2637   1.1  christos 
   2638   1.1  christos 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
   2639   1.1  christos 	if (error != 0)
   2640   1.1  christos 		goto fail;
   2641   1.1  christos 
   2642   1.1  christos 	athn_rx_start(sc);
   2643   1.1  christos 
   2644   1.1  christos 	/* Create main interface on target. */
   2645   1.1  christos 	memset(&hvif, 0, sizeof(hvif));
   2646   1.1  christos 	hvif.index = 0;
   2647   1.1  christos 	IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
   2648   1.1  christos 	switch (ic->ic_opmode) {
   2649   1.1  christos 	case IEEE80211_M_STA:
   2650   1.1  christos 		hvif.opmode = htobe32(AR_HTC_M_STA);
   2651   1.1  christos 		break;
   2652   1.1  christos 	case IEEE80211_M_MONITOR:
   2653   1.1  christos 		hvif.opmode = htobe32(AR_HTC_M_MONITOR);
   2654   1.1  christos 		break;
   2655   1.1  christos #ifndef IEEE80211_STA_ONLY
   2656   1.1  christos 	case IEEE80211_M_IBSS:
   2657   1.1  christos 		hvif.opmode = htobe32(AR_HTC_M_IBSS);
   2658   1.1  christos 		break;
   2659   1.1  christos 	case IEEE80211_M_AHDEMO:
   2660   1.1  christos 		hvif.opmode = htobe32(AR_HTC_M_AHDEMO);
   2661   1.1  christos 		break;
   2662   1.1  christos 	case IEEE80211_M_HOSTAP:
   2663   1.1  christos 		hvif.opmode = htobe32(AR_HTC_M_HOSTAP);
   2664   1.1  christos 		break;
   2665   1.1  christos #endif
   2666   1.1  christos 	}
   2667   1.1  christos 	hvif.rtsthreshold = htobe16(ic->ic_rtsthreshold);
   2668   1.1  christos 	DPRINTFN(DBG_INIT, sc, "creating VAP\n");
   2669   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_CREATE,
   2670   1.1  christos 	    &hvif, sizeof(hvif), NULL);
   2671   1.1  christos 	if (error != 0)
   2672   1.1  christos 		goto fail;
   2673   1.1  christos 
   2674   1.1  christos 	/* Create a fake node to send management frames before assoc. */
   2675   1.1  christos 	memset(&sta, 0, sizeof(sta));
   2676   1.1  christos 	IEEE80211_ADDR_COPY(sta.macaddr, ic->ic_myaddr);
   2677   1.1  christos 	sta.sta_index = 0;
   2678   1.1  christos 	sta.is_vif_sta = 1;
   2679   1.1  christos 	sta.vif_index = hvif.index;
   2680   1.1  christos 	sta.maxampdu = 0xffff;
   2681   1.1  christos 
   2682   1.1  christos 	DPRINTFN(DBG_INIT | DBG_NODES, sc, "creating default node %u\n",
   2683   1.1  christos 	    sta.sta_index);
   2684   1.1  christos 	error = athn_usb_create_hw_node(usc, &sta);
   2685   1.1  christos 	if (error != 0)
   2686   1.1  christos 		goto fail;
   2687   1.1  christos 
   2688   1.1  christos 	/* Update target capabilities. */
   2689   1.1  christos 	memset(&hic, 0, sizeof(hic));
   2690   1.1  christos 	hic.flags = htobe32(0x400c2400);
   2691   1.1  christos 	hic.flags_ext = htobe32(0x00106080);
   2692   1.1  christos 	hic.ampdu_limit = htobe32(0x0000ffff);
   2693   1.1  christos 	hic.ampdu_subframes = 20;
   2694   1.1  christos 	hic.protmode = 1;	/* XXX */
   2695   1.1  christos 	hic.lg_txchainmask = sc->sc_txchainmask;
   2696   1.1  christos 	hic.ht_txchainmask = sc->sc_txchainmask;
   2697   1.1  christos 	DPRINTFN(DBG_INIT, sc, "updating target configuration\n");
   2698   1.1  christos 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TARGET_IC_UPDATE,
   2699   1.1  christos 	    &hic, sizeof(hic), NULL);
   2700   1.1  christos 	if (error != 0)
   2701   1.1  christos 		goto fail;
   2702   1.1  christos 
   2703   1.1  christos 	/* Queue Rx xfers. */
   2704   1.1  christos 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
   2705   1.1  christos 		data = &usc->usc_rx_data[i];
   2706   1.1  christos 
   2707   1.9     skrll 		usbd_setup_xfer(data->xfer, data, data->buf,
   2708   1.9     skrll 		    ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK,
   2709   1.1  christos 		    USBD_NO_TIMEOUT, athn_usb_rxeof);
   2710   1.1  christos 		error = usbd_transfer(data->xfer);
   2711   1.1  christos 		if (error != 0 && error != USBD_IN_PROGRESS)
   2712   1.1  christos 			goto fail;
   2713   1.1  christos 	}
   2714   1.1  christos 	/* We're ready to go. */
   2715   1.1  christos 	ifp->if_flags &= ~IFF_OACTIVE;
   2716   1.1  christos 	ifp->if_flags |= IFF_RUNNING;
   2717   1.1  christos 
   2718   1.1  christos #ifdef notyet
   2719   1.1  christos 	if (ic->ic_flags & IEEE80211_F_WEPON) {
   2720   1.1  christos 		/* Install WEP keys. */
   2721   1.1  christos 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
   2722   1.1  christos 			athn_usb_set_key(ic, NULL, &ic->ic_nw_keys[i]);
   2723   1.1  christos 	}
   2724   1.1  christos #endif
   2725   1.1  christos 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2726   1.1  christos 		ic->ic_max_aid = AR_USB_MAX_STA;  /* Firmware is limited to 8 STA */
   2727   1.1  christos 	else
   2728   1.1  christos 		ic->ic_max_aid = sc->sc_max_aid;
   2729   1.1  christos 
   2730   1.1  christos 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2731   1.1  christos 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2732   1.1  christos 	else
   2733   1.1  christos 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2734   1.1  christos 	athn_usb_wait_async(usc);
   2735   1.1  christos 	return 0;
   2736   1.1  christos  fail:
   2737   1.1  christos 	athn_usb_stop(ifp);
   2738   1.1  christos 	return error;
   2739   1.1  christos }
   2740   1.1  christos 
   2741   1.1  christos Static void
   2742   1.1  christos athn_usb_stop(struct ifnet *ifp)
   2743   1.1  christos {
   2744   1.1  christos 	struct athn_softc *sc = ifp->if_softc;
   2745   1.1  christos 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
   2746   1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   2747   1.1  christos 	struct ar_htc_target_vif hvif;
   2748   1.1  christos 	struct mbuf *m;
   2749   1.1  christos 	uint8_t sta_index;
   2750   1.6  christos 	int s;
   2751   1.1  christos 
   2752   1.1  christos 	DPRINTFN(DBG_FN, sc, "\n");
   2753   1.1  christos 
   2754   1.1  christos 	s = splusb();
   2755   1.1  christos 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2756   1.1  christos 	athn_usb_wait_async(usc);
   2757   1.1  christos 	splx(s);
   2758   1.1  christos 
   2759   1.1  christos 	sc->sc_tx_timer = 0;
   2760   1.1  christos 	ifp->if_timer = 0;
   2761   1.1  christos 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2762   1.1  christos 
   2763   1.1  christos 	callout_stop(&sc->sc_scan_to);
   2764   1.1  christos 	callout_stop(&sc->sc_calib_to);
   2765   1.1  christos 
   2766   1.1  christos 	/* Abort Tx/Rx. */
   2767   1.1  christos 	usbd_abort_pipe(usc->usc_tx_data_pipe);
   2768   1.1  christos 	usbd_abort_pipe(usc->usc_rx_data_pipe);
   2769   1.1  christos 
   2770   1.1  christos 	/* Free Tx/Rx buffers. */
   2771   1.1  christos 	athn_usb_free_tx_list(usc);
   2772   1.1  christos 	athn_usb_free_rx_list(usc);
   2773   1.1  christos 
   2774   1.1  christos 	/* Flush Rx stream. */
   2775   1.1  christos 	CTASSERT(sizeof(m) == sizeof(void *));
   2776   1.1  christos 	m = atomic_swap_ptr(&usc->usc_rx_stream.m, NULL);
   2777   1.1  christos 	m_freem(m);
   2778   1.1  christos 	usc->usc_rx_stream.left = 0;
   2779   1.1  christos 
   2780   1.1  christos 	/* Remove main interface. */
   2781   1.1  christos 	memset(&hvif, 0, sizeof(hvif));
   2782   1.1  christos 	hvif.index = 0;
   2783   1.1  christos 	IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
   2784   1.1  christos 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_REMOVE,
   2785   1.1  christos 	    &hvif, sizeof(hvif), NULL);
   2786   1.1  christos 
   2787   1.1  christos 	/* Remove default node. */
   2788   1.1  christos 	sta_index = 0;
   2789   1.1  christos 	DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
   2790   1.6  christos 	(void)athn_usb_remove_hw_node(usc, &sta_index);
   2791   1.1  christos 
   2792   1.1  christos 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
   2793   1.1  christos 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
   2794   1.1  christos 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
   2795   1.1  christos 
   2796   1.1  christos 	athn_reset(sc, 0);
   2797   1.1  christos 	athn_init_pll(sc, NULL);
   2798   1.1  christos 	athn_set_power_awake(sc);
   2799   1.1  christos 	athn_reset(sc, 1);
   2800   1.1  christos 	athn_init_pll(sc, NULL);
   2801   1.1  christos 	athn_set_power_sleep(sc);
   2802   1.1  christos }
   2803   1.1  christos 
   2804   1.1  christos MODULE(MODULE_CLASS_DRIVER, if_athn_usb, "bpf");
   2805   1.1  christos 
   2806   1.1  christos #ifdef _MODULE
   2807   1.1  christos #include "ioconf.c"
   2808   1.1  christos #endif
   2809   1.1  christos 
   2810   1.1  christos static int
   2811   1.1  christos if_athn_usb_modcmd(modcmd_t cmd, void *aux)
   2812   1.1  christos {
   2813   1.1  christos 	int error = 0;
   2814   1.1  christos 
   2815   1.1  christos 	switch (cmd) {
   2816   1.1  christos 	case MODULE_CMD_INIT:
   2817   1.1  christos #ifdef _MODULE
   2818   1.1  christos 		error = config_init_component(cfdriver_ioconf_if_athn_usb,
   2819   1.1  christos 		    cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
   2820   1.1  christos #endif
   2821   1.1  christos 		return error;
   2822   1.1  christos 	case MODULE_CMD_FINI:
   2823   1.1  christos #ifdef _MODULE
   2824   1.1  christos 		error = config_fini_component(cfdriver_ioconf_if_athn_usb,
   2825   1.1  christos 		    cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
   2826   1.1  christos #endif
   2827   1.1  christos 		return error;
   2828   1.1  christos 	default:
   2829   1.1  christos 		return ENOTTY;
   2830   1.1  christos 	}
   2831   1.1  christos }
   2832