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