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