Home | History | Annotate | Line # | Download | only in usb
if_bwfm_usb.c revision 1.15
      1  1.15  riastrad /* $NetBSD: if_bwfm_usb.c,v 1.15 2020/07/22 17:18:10 riastradh Exp $ */
      2   1.1  jmcneill /* $OpenBSD: if_bwfm_usb.c,v 1.2 2017/10/15 14:55:13 patrick Exp $ */
      3   1.1  jmcneill /*
      4   1.1  jmcneill  * Copyright (c) 2010-2016 Broadcom Corporation
      5   1.1  jmcneill  * Copyright (c) 2016,2017 Patrick Wildt <patrick (at) blueri.se>
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Permission to use, copy, modify, and/or distribute this software for any
      8   1.1  jmcneill  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  jmcneill  * copyright notice and this permission notice appear in all copies.
     10   1.1  jmcneill  *
     11   1.1  jmcneill  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  jmcneill  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  jmcneill  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  jmcneill  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  jmcneill  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  jmcneill  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  jmcneill  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  jmcneill  */
     19   1.1  jmcneill 
     20   1.7       rin #include <sys/cdefs.h>
     21  1.15  riastrad __KERNEL_RCSID(0, "$NetBSD: if_bwfm_usb.c,v 1.15 2020/07/22 17:18:10 riastradh Exp $");
     22   1.7       rin 
     23   1.1  jmcneill #include <sys/param.h>
     24  1.15  riastrad #include <sys/types.h>
     25  1.15  riastrad 
     26   1.1  jmcneill #include <sys/buf.h>
     27  1.15  riastrad #include <sys/device.h>
     28   1.1  jmcneill #include <sys/kernel.h>
     29   1.1  jmcneill #include <sys/malloc.h>
     30  1.15  riastrad #include <sys/mutex.h>
     31   1.1  jmcneill #include <sys/queue.h>
     32   1.1  jmcneill #include <sys/socket.h>
     33  1.15  riastrad #include <sys/systm.h>
     34   1.1  jmcneill #include <sys/workqueue.h>
     35   1.1  jmcneill 
     36   1.1  jmcneill #include <net/bpf.h>
     37   1.1  jmcneill #include <net/if.h>
     38   1.1  jmcneill #include <net/if_dl.h>
     39  1.15  riastrad #include <net/if_ether.h>
     40   1.1  jmcneill #include <net/if_media.h>
     41   1.1  jmcneill 
     42   1.1  jmcneill #include <netinet/in.h>
     43   1.1  jmcneill 
     44   1.1  jmcneill #include <net80211/ieee80211_var.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill #include <dev/usb/usb.h>
     47  1.15  riastrad #include <dev/usb/usbdevs.h>
     48   1.1  jmcneill #include <dev/usb/usbdi.h>
     49   1.1  jmcneill #include <dev/usb/usbdi_util.h>
     50   1.1  jmcneill #include <dev/usb/usbdivar.h>
     51   1.1  jmcneill 
     52  1.12  jdolecek #include <dev/ic/bwfmreg.h>
     53   1.1  jmcneill #include <dev/ic/bwfmvar.h>
     54   1.1  jmcneill 
     55  1.11   thorpej static const struct bwfm_firmware_selector bwfm_usb_fwtab[] = {
     56  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43143_CHIP_ID,
     57  1.11   thorpej 		      BWFM_FWSEL_ALLREVS, "brcmfmac43143"),
     58  1.15  riastrad 
     59  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43235_CHIP_ID,
     60  1.11   thorpej 		      BWFM_FWSEL_REV_EQ(3), "brcmfmac43236b"),
     61  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43236_CHIP_ID,
     62  1.11   thorpej 		      BWFM_FWSEL_REV_EQ(3), "brcmfmac43236b"),
     63  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43238_CHIP_ID,
     64  1.11   thorpej 		      BWFM_FWSEL_REV_EQ(3), "brcmfmac43236b"),
     65  1.11   thorpej 
     66  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43242_CHIP_ID,
     67  1.11   thorpej 		      BWFM_FWSEL_ALLREVS, "brcmfmac43242a"),
     68  1.15  riastrad 
     69  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43566_CHIP_ID,
     70  1.11   thorpej 		      BWFM_FWSEL_ALLREVS, "brcmfmac43569"),
     71  1.11   thorpej 	BWFM_FW_ENTRY(BRCM_CC_43569_CHIP_ID,
     72  1.11   thorpej 		      BWFM_FWSEL_ALLREVS, "brcmfmac43569"),
     73  1.15  riastrad 
     74  1.11   thorpej 	BWFM_FW_ENTRY(CY_CC_4373_CHIP_ID,
     75  1.11   thorpej 		      BWFM_FWSEL_ALLREVS, "brcmfmac4373"),
     76  1.11   thorpej 
     77  1.11   thorpej 	BWFM_FW_ENTRY_END
     78  1.11   thorpej };
     79  1.11   thorpej 
     80   1.1  jmcneill /*
     81   1.1  jmcneill  * Various supported device vendors/products.
     82   1.1  jmcneill  */
     83   1.1  jmcneill static const struct usb_devno bwfm_usbdevs[] = {
     84   1.1  jmcneill 	{ USB_VENDOR_BROADCOM,	USB_PRODUCT_BROADCOM_BCM43143 },
     85   1.1  jmcneill 	{ USB_VENDOR_BROADCOM,	USB_PRODUCT_BROADCOM_BCM43236 },
     86   1.1  jmcneill 	{ USB_VENDOR_BROADCOM,	USB_PRODUCT_BROADCOM_BCM43242 },
     87   1.1  jmcneill 	{ USB_VENDOR_BROADCOM,	USB_PRODUCT_BROADCOM_BCM43569 },
     88   1.1  jmcneill 	{ USB_VENDOR_BROADCOM,	USB_PRODUCT_BROADCOM_BCMFW },
     89   1.1  jmcneill };
     90   1.1  jmcneill 
     91   1.1  jmcneill #ifdef BWFM_DEBUG
     92   1.1  jmcneill #define DPRINTF(x)	do { if (bwfm_debug > 0) printf x; } while (0)
     93   1.1  jmcneill #define DPRINTFN(n, x)	do { if (bwfm_debug >= (n)) printf x; } while (0)
     94   1.1  jmcneill static int bwfm_debug = 2;
     95   1.1  jmcneill #else
     96   1.1  jmcneill #define DPRINTF(x)	do { ; } while (0)
     97   1.1  jmcneill #define DPRINTFN(n, x)	do { ; } while (0)
     98   1.1  jmcneill #endif
     99   1.1  jmcneill 
    100   1.1  jmcneill #define DEVNAME(sc)	device_xname((sc)->sc_sc.sc_dev)
    101   1.1  jmcneill 
    102   1.1  jmcneill #define BRCMF_POSTBOOT_ID	0xA123	/* ID to detect if dongle
    103   1.1  jmcneill 					 * has boot up
    104   1.1  jmcneill 					 */
    105   1.1  jmcneill 
    106   1.1  jmcneill #define TRX_MAGIC		0x30524448	/* "HDR0" */
    107   1.1  jmcneill #define TRX_MAX_OFFSET		3		/* Max number of file offsets */
    108   1.1  jmcneill #define TRX_UNCOMP_IMAGE	0x20		/* Trx holds uncompressed img */
    109   1.1  jmcneill #define TRX_RDL_CHUNK		1500		/* size of each dl transfer */
    110   1.1  jmcneill #define TRX_OFFSETS_DLFWLEN_IDX	0
    111   1.1  jmcneill 
    112   1.1  jmcneill /* Control messages: bRequest values */
    113   1.1  jmcneill #define DL_GETSTATE	0	/* returns the rdl_state_t struct */
    114   1.1  jmcneill #define DL_CHECK_CRC	1	/* currently unused */
    115   1.1  jmcneill #define DL_GO		2	/* execute downloaded image */
    116   1.1  jmcneill #define DL_START	3	/* initialize dl state */
    117   1.1  jmcneill #define DL_REBOOT	4	/* reboot the device in 2 seconds */
    118   1.1  jmcneill #define DL_GETVER	5	/* returns the bootrom_id_t struct */
    119   1.1  jmcneill #define DL_GO_PROTECTED	6	/* execute the downloaded code and set reset
    120   1.1  jmcneill 				 * event to occur in 2 seconds.  It is the
    121   1.1  jmcneill 				 * responsibility of the downloaded code to
    122   1.1  jmcneill 				 * clear this event
    123   1.1  jmcneill 				 */
    124   1.1  jmcneill #define DL_EXEC		7	/* jump to a supplied address */
    125   1.1  jmcneill #define DL_RESETCFG	8	/* To support single enum on dongle
    126   1.1  jmcneill 				 * - Not used by bootloader
    127   1.1  jmcneill 				 */
    128   1.1  jmcneill #define DL_DEFER_RESP_OK 9	/* Potentially defer the response to setup
    129   1.1  jmcneill 				 * if resp unavailable
    130   1.1  jmcneill 				 */
    131   1.1  jmcneill 
    132   1.1  jmcneill /* states */
    133   1.1  jmcneill #define DL_WAITING	0	/* waiting to rx first pkt */
    134   1.1  jmcneill #define DL_READY	1	/* hdr was good, waiting for more of the
    135   1.1  jmcneill 				 * compressed image
    136   1.1  jmcneill 				 */
    137   1.1  jmcneill #define DL_BAD_HDR	2	/* hdr was corrupted */
    138   1.1  jmcneill #define DL_BAD_CRC	3	/* compressed image was corrupted */
    139   1.1  jmcneill #define DL_RUNNABLE	4	/* download was successful,waiting for go cmd */
    140   1.1  jmcneill #define DL_START_FAIL	5	/* failed to initialize correctly */
    141   1.1  jmcneill #define DL_NVRAM_TOOBIG	6	/* host specified nvram data exceeds DL_NVRAM
    142   1.1  jmcneill 				 * value
    143   1.1  jmcneill 				 */
    144   1.1  jmcneill #define DL_IMAGE_TOOBIG	7	/* firmware image too big */
    145   1.1  jmcneill 
    146   1.1  jmcneill 
    147   1.1  jmcneill struct trx_header {
    148   1.1  jmcneill 	uint32_t	magic;			/* "HDR0" */
    149   1.1  jmcneill 	uint32_t	len;			/* Length of file including header */
    150   1.1  jmcneill 	uint32_t	crc32;			/* CRC from flag_version to end of file */
    151   1.1  jmcneill 	uint32_t	flag_version;		/* 0:15 flags, 16:31 version */
    152   1.1  jmcneill 	uint32_t	offsets[TRX_MAX_OFFSET];/* Offsets of partitions from start of
    153   1.1  jmcneill 						 * header
    154   1.1  jmcneill 						 */
    155   1.1  jmcneill };
    156   1.1  jmcneill 
    157   1.1  jmcneill struct rdl_state {
    158   1.1  jmcneill 	uint32_t	state;
    159   1.1  jmcneill 	uint32_t	bytes;
    160   1.1  jmcneill };
    161   1.1  jmcneill 
    162   1.1  jmcneill struct bootrom_id {
    163   1.1  jmcneill 	uint32_t	chip;		/* Chip id */
    164   1.1  jmcneill 	uint32_t	chiprev;	/* Chip rev */
    165   1.1  jmcneill 	uint32_t	ramsize;	/* Size of  RAM */
    166   1.1  jmcneill 	uint32_t	remapbase;	/* Current remap base address */
    167   1.1  jmcneill 	uint32_t	boardtype;	/* Type of board */
    168   1.1  jmcneill 	uint32_t	boardrev;	/* Board revision */
    169   1.1  jmcneill };
    170   1.1  jmcneill 
    171   1.1  jmcneill struct bwfm_usb_rx_data {
    172   1.1  jmcneill 	struct bwfm_usb_softc		*sc;
    173   1.1  jmcneill 	struct usbd_xfer		*xfer;
    174   1.1  jmcneill 	uint8_t				*buf;
    175   1.1  jmcneill };
    176   1.1  jmcneill 
    177   1.1  jmcneill struct bwfm_usb_tx_data {
    178   1.1  jmcneill 	struct bwfm_usb_softc		*sc;
    179   1.1  jmcneill 	struct usbd_xfer		*xfer;
    180   1.1  jmcneill 	uint8_t				*buf;
    181   1.1  jmcneill 	struct mbuf			*mbuf;
    182   1.1  jmcneill 	TAILQ_ENTRY(bwfm_usb_tx_data)	 next;
    183   1.1  jmcneill };
    184   1.1  jmcneill 
    185   1.1  jmcneill #define BWFM_RX_LIST_COUNT		50
    186   1.1  jmcneill #define BWFM_TX_LIST_COUNT		50
    187   1.1  jmcneill #define BWFM_RXBUFSZ			1600
    188   1.1  jmcneill #define BWFM_TXBUFSZ			1600
    189   1.1  jmcneill struct bwfm_usb_softc {
    190   1.1  jmcneill 	struct bwfm_softc	 sc_sc;
    191   1.1  jmcneill 	struct usbd_device	*sc_udev;
    192   1.1  jmcneill 	struct usbd_interface	*sc_iface;
    193   1.1  jmcneill 	uint8_t			 sc_ifaceno;
    194   1.1  jmcneill 
    195   1.1  jmcneill 	uint16_t		 sc_vendor;
    196   1.1  jmcneill 	uint16_t		 sc_product;
    197   1.1  jmcneill 
    198   1.1  jmcneill 	uint32_t		 sc_chip;
    199   1.1  jmcneill 	uint32_t		 sc_chiprev;
    200   1.1  jmcneill 
    201   1.1  jmcneill 	int			 sc_rx_no;
    202   1.1  jmcneill 	int			 sc_tx_no;
    203   1.1  jmcneill 
    204   1.1  jmcneill 	struct usbd_pipe	*sc_rx_pipeh;
    205   1.1  jmcneill 	struct usbd_pipe	*sc_tx_pipeh;
    206   1.1  jmcneill 
    207   1.1  jmcneill 	struct bwfm_usb_rx_data	 sc_rx_data[BWFM_RX_LIST_COUNT];
    208   1.1  jmcneill 	struct bwfm_usb_tx_data	 sc_tx_data[BWFM_TX_LIST_COUNT];
    209   1.1  jmcneill 	TAILQ_HEAD(, bwfm_usb_tx_data) sc_tx_free_list;
    210   1.1  jmcneill 
    211   1.1  jmcneill 	kmutex_t		 sc_rx_lock;
    212   1.1  jmcneill 	kmutex_t		 sc_tx_lock;
    213   1.1  jmcneill };
    214   1.1  jmcneill 
    215   1.1  jmcneill int		 bwfm_usb_match(device_t, cfdata_t, void *);
    216   1.1  jmcneill void		 bwfm_usb_attachhook(device_t);
    217   1.1  jmcneill void		 bwfm_usb_attach(device_t, device_t, void *);
    218   1.1  jmcneill int		 bwfm_usb_detach(device_t, int);
    219   1.1  jmcneill 
    220   1.1  jmcneill int		 bwfm_usb_dl_cmd(struct bwfm_usb_softc *, uint8_t, void *, int);
    221   1.1  jmcneill int		 bwfm_usb_load_microcode(struct bwfm_usb_softc *, const u_char *,
    222   1.1  jmcneill 		     size_t);
    223   1.1  jmcneill 
    224   1.1  jmcneill int		 bwfm_usb_alloc_rx_list(struct bwfm_usb_softc *);
    225   1.1  jmcneill void		 bwfm_usb_free_rx_list(struct bwfm_usb_softc *);
    226   1.1  jmcneill int		 bwfm_usb_alloc_tx_list(struct bwfm_usb_softc *);
    227   1.1  jmcneill void		 bwfm_usb_free_tx_list(struct bwfm_usb_softc *);
    228   1.1  jmcneill 
    229   1.5      maya int		 bwfm_usb_txcheck(struct bwfm_softc *);
    230   1.8  riastrad int		 bwfm_usb_txdata(struct bwfm_softc *, struct mbuf **);
    231   1.1  jmcneill int		 bwfm_usb_txctl(struct bwfm_softc *, char *, size_t);
    232   1.1  jmcneill int		 bwfm_usb_rxctl(struct bwfm_softc *, char *, size_t *);
    233   1.1  jmcneill 
    234   1.5      maya struct mbuf *	 bwfm_usb_newbuf(void);
    235   1.1  jmcneill void		 bwfm_usb_rxeof(struct usbd_xfer *, void *, usbd_status);
    236   1.1  jmcneill void		 bwfm_usb_txeof(struct usbd_xfer *, void *, usbd_status);
    237   1.1  jmcneill 
    238  1.13  jdolecek static const struct bwfm_bus_ops bwfm_usb_bus_ops = {
    239   1.1  jmcneill 	.bs_init = NULL,
    240   1.1  jmcneill 	.bs_stop = NULL,
    241   1.5      maya 	.bs_txcheck = bwfm_usb_txcheck,
    242   1.1  jmcneill 	.bs_txdata = bwfm_usb_txdata,
    243   1.1  jmcneill 	.bs_txctl = bwfm_usb_txctl,
    244   1.1  jmcneill 	.bs_rxctl = bwfm_usb_rxctl,
    245   1.1  jmcneill };
    246   1.1  jmcneill 
    247   1.1  jmcneill CFATTACH_DECL_NEW(bwfm_usb, sizeof(struct bwfm_usb_softc),
    248   1.1  jmcneill     bwfm_usb_match, bwfm_usb_attach, bwfm_usb_detach, NULL);
    249   1.1  jmcneill 
    250   1.1  jmcneill int
    251   1.1  jmcneill bwfm_usb_match(device_t parent, cfdata_t match, void *aux)
    252   1.1  jmcneill {
    253   1.1  jmcneill 	struct usb_attach_arg *uaa = aux;
    254   1.1  jmcneill 
    255   1.1  jmcneill 	return (usb_lookup(bwfm_usbdevs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
    256   1.1  jmcneill 	    UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
    257   1.1  jmcneill }
    258   1.1  jmcneill 
    259   1.1  jmcneill void
    260   1.1  jmcneill bwfm_usb_attach(device_t parent, device_t self, void *aux)
    261   1.1  jmcneill {
    262   1.1  jmcneill 	struct bwfm_usb_softc *sc = device_private(self);
    263   1.1  jmcneill 	struct usb_attach_arg *uaa = aux;
    264   1.1  jmcneill 	usb_device_descriptor_t *dd;
    265   1.1  jmcneill 	usb_interface_descriptor_t *id;
    266   1.1  jmcneill 	usb_endpoint_descriptor_t *ed;
    267   1.1  jmcneill 	char *devinfop;
    268   1.1  jmcneill 	int i;
    269   1.1  jmcneill 
    270   1.1  jmcneill 	sc->sc_sc.sc_dev = self;
    271   1.1  jmcneill 	sc->sc_udev = uaa->uaa_device;
    272   1.1  jmcneill 	mutex_init(&sc->sc_rx_lock, MUTEX_DEFAULT, IPL_NET);
    273   1.1  jmcneill 	mutex_init(&sc->sc_tx_lock, MUTEX_DEFAULT, IPL_NET);
    274   1.1  jmcneill 
    275   1.1  jmcneill 	aprint_naive("\n");
    276   1.1  jmcneill 
    277   1.1  jmcneill 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    278   1.1  jmcneill 	aprint_normal(": %s\n", devinfop);
    279   1.1  jmcneill 	usbd_devinfo_free(devinfop);
    280   1.1  jmcneill 
    281   1.1  jmcneill 	if (usbd_set_config_no(sc->sc_udev, 1, 1) != 0) {
    282   1.1  jmcneill 		aprint_error_dev(self, "failed to set configuration\n");
    283   1.1  jmcneill 		return;
    284   1.1  jmcneill 	}
    285   1.1  jmcneill 	if (usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface) != 0) {
    286   1.1  jmcneill 		aprint_error_dev(self, "failed to get interface handle\n");
    287   1.1  jmcneill 		return;
    288   1.1  jmcneill 	}
    289   1.1  jmcneill 
    290   1.1  jmcneill 	sc->sc_ifaceno = 0;
    291   1.1  jmcneill 	sc->sc_vendor = uaa->uaa_vendor;
    292   1.1  jmcneill 	sc->sc_product = uaa->uaa_product;
    293   1.1  jmcneill 	sc->sc_sc.sc_bus_ops = &bwfm_usb_bus_ops;
    294   1.1  jmcneill 	sc->sc_sc.sc_proto_ops = &bwfm_proto_bcdc_ops;
    295   1.1  jmcneill 
    296   1.1  jmcneill 	/* Check number of configurations. */
    297   1.1  jmcneill 	dd = usbd_get_device_descriptor(sc->sc_udev);
    298   1.1  jmcneill 	if (dd->bNumConfigurations != 1) {
    299   1.1  jmcneill 		printf("%s: number of configurations not supported\n",
    300   1.1  jmcneill 		    DEVNAME(sc));
    301   1.1  jmcneill 		return;
    302   1.1  jmcneill 	}
    303   1.1  jmcneill 
    304   1.1  jmcneill 	/* Get endpoints. */
    305   1.1  jmcneill 	id = usbd_get_interface_descriptor(sc->sc_iface);
    306   1.1  jmcneill 
    307   1.1  jmcneill 	sc->sc_rx_no = sc->sc_tx_no = -1;
    308   1.1  jmcneill 	for (i = 0; i < id->bNumEndpoints; i++) {
    309   1.1  jmcneill 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    310   1.1  jmcneill 		if (ed == NULL) {
    311   1.1  jmcneill 			printf("%s: no endpoint descriptor for iface %d\n",
    312   1.1  jmcneill 			    DEVNAME(sc), i);
    313   1.1  jmcneill 			return;
    314   1.1  jmcneill 		}
    315   1.1  jmcneill 
    316   1.1  jmcneill 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    317   1.1  jmcneill 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
    318   1.1  jmcneill 		    sc->sc_rx_no == -1)
    319   1.1  jmcneill 			sc->sc_rx_no = ed->bEndpointAddress;
    320   1.1  jmcneill 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    321   1.1  jmcneill 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
    322   1.1  jmcneill 		    sc->sc_tx_no == -1)
    323   1.1  jmcneill 			sc->sc_tx_no = ed->bEndpointAddress;
    324   1.1  jmcneill 	}
    325   1.1  jmcneill 	if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
    326   1.1  jmcneill 		printf("%s: missing endpoint\n", DEVNAME(sc));
    327   1.1  jmcneill 		return;
    328   1.1  jmcneill 	}
    329   1.1  jmcneill 
    330   1.1  jmcneill 	config_mountroot(self, bwfm_usb_attachhook);
    331   1.1  jmcneill }
    332   1.1  jmcneill 
    333   1.1  jmcneill void
    334   1.1  jmcneill bwfm_usb_attachhook(device_t self)
    335   1.1  jmcneill {
    336   1.1  jmcneill 	struct bwfm_usb_softc *sc = device_private(self);
    337  1.11   thorpej 	struct bwfm_softc *bwfm = &sc->sc_sc;
    338   1.1  jmcneill 	struct bwfm_usb_rx_data *data;
    339   1.1  jmcneill 	struct bootrom_id brom;
    340  1.11   thorpej 	struct bwfm_firmware_context fwctx;
    341   1.1  jmcneill 	usbd_status error;
    342   1.1  jmcneill 	u_char *ucode;
    343  1.11   thorpej 	size_t ucsize;
    344   1.1  jmcneill 	int i;
    345   1.1  jmcneill 
    346   1.1  jmcneill 	/* Read chip id and chip rev to check the firmware. */
    347   1.1  jmcneill 	memset(&brom, 0, sizeof(brom));
    348   1.1  jmcneill 	bwfm_usb_dl_cmd(sc, DL_GETVER, &brom, sizeof(brom));
    349   1.1  jmcneill 	sc->sc_chip = le32toh(brom.chip);
    350   1.1  jmcneill 	sc->sc_chiprev = le32toh(brom.chiprev);
    351   1.1  jmcneill 
    352   1.1  jmcneill 	/* Setup data pipes */
    353   1.1  jmcneill 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
    354   1.1  jmcneill 	    &sc->sc_rx_pipeh);
    355   1.1  jmcneill 	if (error != 0) {
    356  1.11   thorpej 		aprint_error_dev(bwfm->sc_dev, "could not open rx pipe: %s\n",
    357  1.11   thorpej 		    usbd_errstr(error));
    358   1.1  jmcneill 		return;
    359   1.1  jmcneill 	}
    360   1.1  jmcneill 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
    361   1.1  jmcneill 	    &sc->sc_tx_pipeh);
    362   1.1  jmcneill 	if (error != 0) {
    363  1.11   thorpej 		aprint_error_dev(bwfm->sc_dev, "could not open tx pipe: %s\n",
    364  1.11   thorpej 		    usbd_errstr(error));
    365   1.1  jmcneill 		return;
    366   1.1  jmcneill 	}
    367   1.1  jmcneill 
    368   1.1  jmcneill 	/* Firmware not yet loaded? */
    369   1.1  jmcneill 	if (sc->sc_chip != BRCMF_POSTBOOT_ID) {
    370  1.11   thorpej 		bwfm_firmware_context_init(&fwctx,
    371  1.11   thorpej 		    sc->sc_chip, sc->sc_chiprev, NULL,
    372  1.11   thorpej 		    BWFM_FWREQ(BWFM_FILETYPE_UCODE));
    373   1.1  jmcneill 
    374  1.11   thorpej 		if (!bwfm_firmware_open(bwfm, bwfm_usb_fwtab, &fwctx)) {
    375  1.11   thorpej 			/* Error message already displayed. */
    376   1.1  jmcneill 			return;
    377   1.1  jmcneill 		}
    378   1.1  jmcneill 
    379  1.11   thorpej 		ucode = bwfm_firmware_data(&fwctx, BWFM_FILETYPE_UCODE,
    380  1.11   thorpej 		    &ucsize);
    381  1.11   thorpej 		KASSERT(ucode != NULL);
    382  1.11   thorpej 
    383  1.11   thorpej 		if (bwfm_usb_load_microcode(sc, ucode, ucsize) != 0) {
    384  1.11   thorpej 			aprint_error_dev(bwfm->sc_dev,
    385  1.11   thorpej 			    "could not load microcode\n");
    386  1.11   thorpej 			bwfm_firmware_close(&fwctx);
    387   1.1  jmcneill 			return;
    388   1.1  jmcneill 		}
    389   1.1  jmcneill 
    390  1.11   thorpej 		bwfm_firmware_close(&fwctx);
    391   1.1  jmcneill 
    392   1.1  jmcneill 		for (i = 0; i < 10; i++) {
    393   1.1  jmcneill 			delay(100 * 1000);
    394   1.1  jmcneill 			memset(&brom, 0, sizeof(brom));
    395   1.1  jmcneill 			bwfm_usb_dl_cmd(sc, DL_GETVER, &brom, sizeof(brom));
    396   1.1  jmcneill 			if (le32toh(brom.chip) == BRCMF_POSTBOOT_ID)
    397   1.1  jmcneill 				break;
    398   1.1  jmcneill 		}
    399   1.1  jmcneill 
    400   1.1  jmcneill 		if (le32toh(brom.chip) != BRCMF_POSTBOOT_ID) {
    401  1.11   thorpej 			aprint_error_dev(bwfm->sc_dev,
    402  1.11   thorpej 			    "firmware did not start up\n");
    403   1.1  jmcneill 			return;
    404   1.1  jmcneill 		}
    405   1.1  jmcneill 
    406   1.1  jmcneill 		sc->sc_chip = le32toh(brom.chip);
    407   1.1  jmcneill 		sc->sc_chiprev = le32toh(brom.chiprev);
    408   1.1  jmcneill 	}
    409   1.1  jmcneill 
    410   1.1  jmcneill 	bwfm_usb_dl_cmd(sc, DL_RESETCFG, &brom, sizeof(brom));
    411   1.1  jmcneill 
    412   1.1  jmcneill 	if (bwfm_usb_alloc_rx_list(sc) || bwfm_usb_alloc_tx_list(sc)) {
    413   1.1  jmcneill 		printf("%s: cannot allocate rx/tx lists\n", DEVNAME(sc));
    414   1.1  jmcneill 		return;
    415   1.1  jmcneill 	}
    416   1.1  jmcneill 
    417   1.1  jmcneill 	bwfm_attach(&sc->sc_sc);
    418   1.1  jmcneill 
    419   1.1  jmcneill 	for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
    420   1.1  jmcneill 		data = &sc->sc_rx_data[i];
    421   1.1  jmcneill 
    422   1.1  jmcneill 		usbd_setup_xfer(data->xfer, data, data->buf,
    423   1.1  jmcneill 		    BWFM_RXBUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
    424   1.1  jmcneill 		    bwfm_usb_rxeof);
    425   1.1  jmcneill 		error = usbd_transfer(data->xfer);
    426   1.1  jmcneill 		if (error != 0 && error != USBD_IN_PROGRESS)
    427  1.11   thorpej 			aprint_error_dev(bwfm->sc_dev,
    428  1.11   thorpej 			    "could not set up new transfer: %s\n",
    429  1.11   thorpej 			    usbd_errstr(error));
    430   1.1  jmcneill 	}
    431   1.1  jmcneill }
    432   1.1  jmcneill 
    433   1.5      maya struct mbuf *
    434   1.5      maya bwfm_usb_newbuf(void)
    435   1.5      maya {
    436   1.5      maya 	struct mbuf *m;
    437   1.5      maya 
    438   1.5      maya 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    439   1.5      maya 	if (m == NULL)
    440   1.5      maya 		return (NULL);
    441   1.5      maya 
    442   1.5      maya 	MCLGET(m, M_DONTWAIT);
    443   1.5      maya 	if (!(m->m_flags & M_EXT)) {
    444   1.5      maya 		m_freem(m);
    445   1.5      maya 		return (NULL);
    446   1.5      maya 	}
    447   1.5      maya 
    448   1.5      maya 	m->m_len = m->m_pkthdr.len = MCLBYTES;
    449   1.5      maya 
    450   1.5      maya 	return (m);
    451   1.5      maya }
    452   1.5      maya 
    453   1.1  jmcneill void
    454   1.1  jmcneill bwfm_usb_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    455   1.1  jmcneill {
    456   1.1  jmcneill 	struct bwfm_usb_rx_data *data = priv;
    457   1.1  jmcneill 	struct bwfm_usb_softc *sc = data->sc;
    458   1.1  jmcneill 	struct bwfm_proto_bcdc_hdr *hdr;
    459   1.1  jmcneill 	usbd_status error;
    460   1.1  jmcneill 	uint32_t len, off;
    461   1.5      maya 	struct mbuf *m;
    462   1.1  jmcneill 
    463   1.1  jmcneill 	DPRINTFN(2, ("%s: %s status %s\n", DEVNAME(sc), __func__,
    464   1.1  jmcneill 	    usbd_errstr(status)));
    465   1.1  jmcneill 
    466   1.1  jmcneill 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
    467   1.1  jmcneill 		usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
    468   1.1  jmcneill 		if (status != USBD_CANCELLED)
    469   1.1  jmcneill 			goto resubmit;
    470   1.1  jmcneill 		return;
    471   1.1  jmcneill 	}
    472   1.1  jmcneill 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    473   1.1  jmcneill 
    474   1.1  jmcneill 	off = 0;
    475   1.1  jmcneill 	hdr = (void *)data->buf;
    476   1.1  jmcneill 	if (len < sizeof(*hdr))
    477   1.1  jmcneill 		goto resubmit;
    478   1.1  jmcneill 	len -= sizeof(*hdr);
    479   1.1  jmcneill 	off += sizeof(*hdr);
    480   1.6  jmcneill 	if (len <= hdr->data_offset << 2)
    481   1.1  jmcneill 		goto resubmit;
    482   1.1  jmcneill 	len -= hdr->data_offset << 2;
    483   1.1  jmcneill 	off += hdr->data_offset << 2;
    484   1.1  jmcneill 
    485   1.5      maya 	m = bwfm_usb_newbuf();
    486   1.5      maya 	if (m == NULL)
    487   1.5      maya 		goto resubmit;
    488   1.5      maya 
    489   1.5      maya 	memcpy(mtod(m, char *), data->buf + off, len);
    490   1.5      maya 	m->m_len = m->m_pkthdr.len = len;
    491   1.5      maya 	mutex_enter(&sc->sc_rx_lock); /* XXX */
    492   1.5      maya 	bwfm_rx(&sc->sc_sc, m);
    493   1.1  jmcneill 	mutex_exit(&sc->sc_rx_lock);
    494   1.1  jmcneill 
    495   1.1  jmcneill resubmit:
    496   1.1  jmcneill 	usbd_setup_xfer(data->xfer, data, data->buf,
    497   1.1  jmcneill 	    BWFM_RXBUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
    498   1.1  jmcneill 	    bwfm_usb_rxeof);
    499   1.1  jmcneill 	error = usbd_transfer(data->xfer);
    500   1.1  jmcneill 	if (error != 0 && error != USBD_IN_PROGRESS)
    501   1.1  jmcneill 		printf("%s: could not set up new transfer: %s\n",
    502   1.1  jmcneill 		    DEVNAME(sc), usbd_errstr(error));
    503   1.1  jmcneill }
    504   1.1  jmcneill 
    505   1.1  jmcneill int
    506   1.1  jmcneill bwfm_usb_alloc_rx_list(struct bwfm_usb_softc *sc)
    507   1.1  jmcneill {
    508   1.1  jmcneill 	struct bwfm_usb_rx_data *data;
    509   1.1  jmcneill 	int i, error = 0;
    510   1.1  jmcneill 
    511   1.1  jmcneill 	for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
    512   1.1  jmcneill 		data = &sc->sc_rx_data[i];
    513   1.1  jmcneill 
    514   1.1  jmcneill 		data->sc = sc; /* Backpointer for callbacks. */
    515   1.1  jmcneill 
    516   1.1  jmcneill 		if (usbd_create_xfer(sc->sc_rx_pipeh, BWFM_RXBUFSZ,
    517   1.4     skrll 		    0, 0, &data->xfer) != 0) {
    518   1.1  jmcneill 			printf("%s: could not create xfer\n",
    519   1.1  jmcneill 			    DEVNAME(sc));
    520   1.1  jmcneill 			error = ENOMEM;
    521   1.1  jmcneill 			break;
    522   1.1  jmcneill 		}
    523   1.1  jmcneill 		data->buf = usbd_get_buffer(data->xfer);
    524   1.1  jmcneill 	}
    525   1.1  jmcneill 	if (error != 0)
    526   1.1  jmcneill 		bwfm_usb_free_rx_list(sc);
    527   1.1  jmcneill 	return (error);
    528   1.1  jmcneill }
    529   1.1  jmcneill 
    530   1.1  jmcneill void
    531   1.1  jmcneill bwfm_usb_free_rx_list(struct bwfm_usb_softc *sc)
    532   1.1  jmcneill {
    533   1.1  jmcneill 	int i;
    534   1.1  jmcneill 
    535   1.1  jmcneill 	/* NB: Caller must abort pipe first. */
    536   1.1  jmcneill 	for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
    537   1.1  jmcneill 		if (sc->sc_rx_data[i].xfer != NULL)
    538   1.1  jmcneill 			usbd_destroy_xfer(sc->sc_rx_data[i].xfer);
    539   1.1  jmcneill 		sc->sc_rx_data[i].xfer = NULL;
    540   1.1  jmcneill 	}
    541   1.1  jmcneill }
    542   1.1  jmcneill 
    543   1.1  jmcneill int
    544   1.1  jmcneill bwfm_usb_alloc_tx_list(struct bwfm_usb_softc *sc)
    545   1.1  jmcneill {
    546   1.1  jmcneill 	struct bwfm_usb_tx_data *data;
    547   1.1  jmcneill 	int i, error = 0;
    548   1.1  jmcneill 
    549   1.1  jmcneill 	TAILQ_INIT(&sc->sc_tx_free_list);
    550   1.1  jmcneill 	for (i = 0; i < BWFM_TX_LIST_COUNT; i++) {
    551   1.1  jmcneill 		data = &sc->sc_tx_data[i];
    552   1.1  jmcneill 
    553   1.1  jmcneill 		data->sc = sc; /* Backpointer for callbacks. */
    554   1.1  jmcneill 
    555   1.1  jmcneill 		if (usbd_create_xfer(sc->sc_tx_pipeh, BWFM_TXBUFSZ,
    556   1.1  jmcneill 		    USBD_FORCE_SHORT_XFER, 0, &data->xfer) != 0) {
    557   1.1  jmcneill 			printf("%s: could not create xfer\n",
    558   1.1  jmcneill 			    DEVNAME(sc));
    559   1.1  jmcneill 			error = ENOMEM;
    560   1.1  jmcneill 			break;
    561   1.1  jmcneill 		}
    562   1.1  jmcneill 		data->buf = usbd_get_buffer(data->xfer);
    563   1.1  jmcneill 
    564   1.1  jmcneill 		/* Append this Tx buffer to our free list. */
    565   1.1  jmcneill 		TAILQ_INSERT_TAIL(&sc->sc_tx_free_list, data, next);
    566   1.1  jmcneill 	}
    567   1.1  jmcneill 	if (error != 0)
    568   1.1  jmcneill 		bwfm_usb_free_tx_list(sc);
    569   1.1  jmcneill 	return (error);
    570   1.1  jmcneill }
    571   1.1  jmcneill 
    572   1.1  jmcneill void
    573   1.1  jmcneill bwfm_usb_free_tx_list(struct bwfm_usb_softc *sc)
    574   1.1  jmcneill {
    575   1.1  jmcneill 	int i;
    576   1.1  jmcneill 
    577   1.1  jmcneill 	/* NB: Caller must abort pipe first. */
    578   1.1  jmcneill 	for (i = 0; i < BWFM_TX_LIST_COUNT; i++) {
    579   1.1  jmcneill 		if (sc->sc_tx_data[i].xfer != NULL)
    580   1.1  jmcneill 			usbd_destroy_xfer(sc->sc_tx_data[i].xfer);
    581   1.1  jmcneill 		sc->sc_tx_data[i].xfer = NULL;
    582   1.1  jmcneill 	}
    583   1.1  jmcneill }
    584   1.1  jmcneill 
    585   1.1  jmcneill void
    586   1.1  jmcneill bwfm_usb_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    587   1.1  jmcneill {
    588   1.1  jmcneill 	struct bwfm_usb_tx_data *data = priv;
    589   1.1  jmcneill 	struct bwfm_usb_softc *sc = data->sc;
    590   1.1  jmcneill 	struct ifnet *ifp = sc->sc_sc.sc_ic.ic_ifp;
    591   1.1  jmcneill 	int s;
    592   1.1  jmcneill 
    593   1.1  jmcneill 	DPRINTFN(2, ("%s: %s status %s\n", DEVNAME(sc), __func__,
    594   1.1  jmcneill 	    usbd_errstr(status)));
    595   1.1  jmcneill 
    596   1.1  jmcneill 	m_freem(data->mbuf);
    597   1.1  jmcneill 	data->mbuf = NULL;
    598   1.1  jmcneill 
    599   1.1  jmcneill 	mutex_enter(&sc->sc_tx_lock);
    600   1.1  jmcneill 	/* Put this Tx buffer back to our free list. */
    601   1.1  jmcneill 	TAILQ_INSERT_TAIL(&sc->sc_tx_free_list, data, next);
    602   1.1  jmcneill 	mutex_exit(&sc->sc_tx_lock);
    603   1.1  jmcneill 
    604   1.1  jmcneill 	s = splnet();
    605   1.1  jmcneill 
    606   1.1  jmcneill 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
    607   1.1  jmcneill 		if (status == USBD_CANCELLED)
    608   1.1  jmcneill 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
    609  1.10   thorpej 		if_statinc(ifp, if_oerrors);
    610   1.1  jmcneill 		splx(s);
    611   1.1  jmcneill 		return;
    612   1.1  jmcneill 	}
    613   1.1  jmcneill 
    614  1.10   thorpej 	if_statinc(ifp, if_opackets);
    615   1.1  jmcneill 
    616   1.1  jmcneill 	/* We just released a Tx buffer, notify Tx. */
    617   1.1  jmcneill 	if ((ifp->if_flags & IFF_OACTIVE) != 0) {
    618   1.1  jmcneill 		ifp->if_flags &= ~IFF_OACTIVE;
    619   1.1  jmcneill 		if_schedule_deferred_start(ifp);
    620   1.1  jmcneill 	}
    621   1.1  jmcneill 	splx(s);
    622   1.1  jmcneill }
    623   1.1  jmcneill 
    624   1.1  jmcneill int
    625   1.1  jmcneill bwfm_usb_detach(device_t self, int flags)
    626   1.1  jmcneill {
    627   1.1  jmcneill 	struct bwfm_usb_softc *sc = device_private(self);
    628   1.1  jmcneill 
    629   1.1  jmcneill 	bwfm_detach(&sc->sc_sc, flags);
    630   1.1  jmcneill 
    631   1.1  jmcneill 	if (sc->sc_rx_pipeh != NULL) {
    632   1.1  jmcneill 		usbd_abort_pipe(sc->sc_rx_pipeh);
    633   1.1  jmcneill 		usbd_close_pipe(sc->sc_rx_pipeh);
    634   1.1  jmcneill 	}
    635   1.1  jmcneill 	if (sc->sc_tx_pipeh != NULL) {
    636   1.1  jmcneill 		usbd_abort_pipe(sc->sc_tx_pipeh);
    637   1.1  jmcneill 		usbd_close_pipe(sc->sc_tx_pipeh);
    638   1.1  jmcneill 	}
    639   1.1  jmcneill 
    640   1.1  jmcneill 	bwfm_usb_free_rx_list(sc);
    641   1.1  jmcneill 	bwfm_usb_free_tx_list(sc);
    642   1.1  jmcneill 
    643   1.1  jmcneill 	mutex_destroy(&sc->sc_rx_lock);
    644   1.1  jmcneill 	mutex_destroy(&sc->sc_tx_lock);
    645   1.1  jmcneill 
    646   1.1  jmcneill 	return 0;
    647   1.1  jmcneill }
    648   1.1  jmcneill 
    649   1.1  jmcneill int
    650   1.1  jmcneill bwfm_usb_dl_cmd(struct bwfm_usb_softc *sc, uByte cmd, void *buf, int len)
    651   1.1  jmcneill {
    652   1.1  jmcneill 	usb_device_request_t req;
    653   1.1  jmcneill 	usbd_status error;
    654   1.1  jmcneill 
    655   1.1  jmcneill 	req.bmRequestType = UT_READ_VENDOR_INTERFACE;
    656   1.1  jmcneill 	req.bRequest = cmd;
    657   1.1  jmcneill 
    658   1.1  jmcneill 	USETW(req.wValue, 0);
    659   1.1  jmcneill 	USETW(req.wIndex, sc->sc_ifaceno);
    660   1.1  jmcneill 	USETW(req.wLength, len);
    661   1.1  jmcneill 
    662   1.1  jmcneill 	error = usbd_do_request(sc->sc_udev, &req, buf);
    663   1.1  jmcneill 	if (error != 0) {
    664   1.1  jmcneill 		printf("%s: could not read register: %s\n",
    665   1.1  jmcneill 		    DEVNAME(sc), usbd_errstr(error));
    666   1.1  jmcneill 	}
    667   1.1  jmcneill 	return error;
    668   1.1  jmcneill }
    669   1.1  jmcneill 
    670   1.1  jmcneill int
    671   1.1  jmcneill bwfm_usb_load_microcode(struct bwfm_usb_softc *sc, const u_char *ucode, size_t size)
    672   1.1  jmcneill {
    673   1.1  jmcneill 	const struct trx_header *trx = (const struct trx_header *)ucode;
    674   1.1  jmcneill 	struct rdl_state state;
    675   1.1  jmcneill 	uint32_t rdlstate, rdlbytes, sent = 0, sendlen = 0;
    676   1.1  jmcneill 	struct usbd_xfer *xfer;
    677   1.1  jmcneill 	usbd_status error;
    678   1.1  jmcneill 	char *buf;
    679   1.1  jmcneill 
    680   1.1  jmcneill 	if (le32toh(trx->magic) != TRX_MAGIC ||
    681   1.1  jmcneill 	    (le32toh(trx->flag_version) & TRX_UNCOMP_IMAGE) == 0) {
    682   1.1  jmcneill 		printf("%s: invalid firmware\n", DEVNAME(sc));
    683   1.1  jmcneill 		return 1;
    684   1.1  jmcneill 	}
    685   1.1  jmcneill 
    686   1.1  jmcneill 	bwfm_usb_dl_cmd(sc, DL_START, &state, sizeof(state));
    687   1.1  jmcneill 	rdlstate = le32toh(state.state);
    688   1.1  jmcneill 	rdlbytes = le32toh(state.bytes);
    689   1.1  jmcneill 
    690   1.1  jmcneill 	if (rdlstate != DL_WAITING) {
    691   1.1  jmcneill 		printf("%s: cannot start fw download\n", DEVNAME(sc));
    692   1.1  jmcneill 		return 1;
    693   1.1  jmcneill 	}
    694   1.1  jmcneill 
    695   1.1  jmcneill 	error = usbd_create_xfer(sc->sc_tx_pipeh, TRX_RDL_CHUNK,
    696   1.1  jmcneill 	    0, 0, &xfer);
    697   1.1  jmcneill 	if (error != 0) {
    698   1.1  jmcneill 		printf("%s: cannot create xfer\n", DEVNAME(sc));
    699   1.1  jmcneill 		goto err;
    700   1.1  jmcneill 	}
    701   1.1  jmcneill 
    702   1.1  jmcneill 	buf = usbd_get_buffer(xfer);
    703   1.1  jmcneill 
    704   1.1  jmcneill 	while (rdlbytes != size) {
    705   1.1  jmcneill 		sendlen = MIN(size - sent, TRX_RDL_CHUNK);
    706   1.1  jmcneill 		memcpy(buf, ucode + sent, sendlen);
    707   1.1  jmcneill 
    708   1.1  jmcneill 		usbd_setup_xfer(xfer, NULL, buf, sendlen,
    709   1.1  jmcneill 		    USBD_SYNCHRONOUS, USBD_NO_TIMEOUT, NULL);
    710   1.1  jmcneill 		error = usbd_transfer(xfer);
    711   1.1  jmcneill 		if (error != 0 && error != USBD_IN_PROGRESS) {
    712   1.1  jmcneill 			printf("%s: transfer error\n", DEVNAME(sc));
    713   1.1  jmcneill 			goto err;
    714   1.1  jmcneill 		}
    715   1.1  jmcneill 		sent += sendlen;
    716   1.1  jmcneill 
    717   1.1  jmcneill 		bwfm_usb_dl_cmd(sc, DL_GETSTATE, &state, sizeof(state));
    718   1.1  jmcneill 		rdlstate = le32toh(state.state);
    719   1.1  jmcneill 		rdlbytes = le32toh(state.bytes);
    720   1.1  jmcneill 
    721   1.1  jmcneill 		if (rdlbytes != sent) {
    722   1.1  jmcneill 			printf("%s: device reported different size\n",
    723   1.1  jmcneill 			    DEVNAME(sc));
    724   1.1  jmcneill 			goto err;
    725   1.1  jmcneill 		}
    726   1.1  jmcneill 
    727   1.1  jmcneill 		if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
    728   1.1  jmcneill 			printf("%s: device reported bad hdr/crc\n",
    729   1.1  jmcneill 			    DEVNAME(sc));
    730   1.1  jmcneill 			goto err;
    731   1.1  jmcneill 		}
    732   1.1  jmcneill 	}
    733   1.1  jmcneill 
    734   1.1  jmcneill 	bwfm_usb_dl_cmd(sc, DL_GETSTATE, &state, sizeof(state));
    735   1.1  jmcneill 	rdlstate = le32toh(state.state);
    736   1.1  jmcneill 	rdlbytes = le32toh(state.bytes);
    737   1.1  jmcneill 
    738   1.1  jmcneill 	if (rdlstate != DL_RUNNABLE) {
    739   1.1  jmcneill 		printf("%s: dongle not runnable\n", DEVNAME(sc));
    740   1.1  jmcneill 		goto err;
    741   1.1  jmcneill 	}
    742   1.1  jmcneill 
    743   1.1  jmcneill 	bwfm_usb_dl_cmd(sc, DL_GO, &state, sizeof(state));
    744   1.1  jmcneill 
    745   1.1  jmcneill 	usbd_destroy_xfer(xfer);
    746   1.1  jmcneill 
    747   1.1  jmcneill 	return 0;
    748   1.1  jmcneill err:
    749   1.1  jmcneill 	if (sc->sc_tx_pipeh != NULL) {
    750   1.1  jmcneill 		usbd_abort_pipe(sc->sc_tx_pipeh);
    751   1.1  jmcneill 		usbd_close_pipe(sc->sc_tx_pipeh);
    752   1.1  jmcneill 		sc->sc_tx_pipeh = NULL;
    753   1.1  jmcneill 	}
    754   1.1  jmcneill 	if (xfer != NULL)
    755   1.1  jmcneill 		usbd_destroy_xfer(xfer);
    756   1.1  jmcneill 	return 1;
    757   1.1  jmcneill }
    758   1.1  jmcneill 
    759   1.1  jmcneill int
    760   1.5      maya bwfm_usb_txcheck(struct bwfm_softc *bwfm)
    761   1.5      maya {
    762   1.5      maya 	struct bwfm_usb_softc *sc = (void *)bwfm;
    763   1.5      maya 
    764   1.5      maya 	mutex_enter(&sc->sc_tx_lock);
    765   1.5      maya 
    766   1.5      maya 	if (TAILQ_EMPTY(&sc->sc_tx_free_list)) {
    767   1.5      maya 		mutex_exit(&sc->sc_tx_lock);
    768   1.5      maya 		return ENOBUFS;
    769   1.5      maya 	}
    770   1.5      maya 
    771   1.5      maya 	mutex_exit(&sc->sc_tx_lock);
    772   1.5      maya 	return 0;
    773   1.5      maya }
    774   1.5      maya 
    775   1.5      maya 
    776   1.5      maya int
    777   1.8  riastrad bwfm_usb_txdata(struct bwfm_softc *bwfm, struct mbuf **mp)
    778   1.1  jmcneill {
    779   1.1  jmcneill 	struct bwfm_usb_softc *sc = (void *)bwfm;
    780   1.8  riastrad 	struct mbuf *m = *mp;
    781   1.1  jmcneill 	struct bwfm_proto_bcdc_hdr *hdr;
    782   1.1  jmcneill 	struct bwfm_usb_tx_data *data;
    783   1.3  jmcneill 	struct ether_header *eh;
    784   1.1  jmcneill 	uint32_t len = 0;
    785   1.3  jmcneill 	int error, ac;
    786   1.1  jmcneill 
    787   1.1  jmcneill 	DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
    788   1.1  jmcneill 
    789   1.1  jmcneill 	mutex_enter(&sc->sc_tx_lock);
    790   1.1  jmcneill 
    791   1.1  jmcneill 	if (TAILQ_EMPTY(&sc->sc_tx_free_list)) {
    792   1.1  jmcneill 		mutex_exit(&sc->sc_tx_lock);
    793   1.1  jmcneill 		return ENOBUFS;
    794   1.1  jmcneill 	}
    795   1.1  jmcneill 
    796   1.3  jmcneill 	/* No QoS for EAPOL frames. */
    797   1.3  jmcneill 	eh = mtod(m, struct ether_header *);
    798   1.3  jmcneill 	ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
    799   1.3  jmcneill 	    M_WME_GETAC(m) : WME_AC_BE;
    800   1.3  jmcneill 
    801   1.1  jmcneill 	/* Grab a Tx buffer from our free list. */
    802   1.1  jmcneill 	data = TAILQ_FIRST(&sc->sc_tx_free_list);
    803   1.1  jmcneill 	TAILQ_REMOVE(&sc->sc_tx_free_list, data, next);
    804   1.1  jmcneill 
    805   1.1  jmcneill 	mutex_exit(&sc->sc_tx_lock);
    806   1.1  jmcneill 
    807   1.1  jmcneill 	hdr = (void *)&data->buf[len];
    808   1.1  jmcneill 	hdr->data_offset = 0;
    809   1.3  jmcneill 	hdr->priority = ac;
    810   1.1  jmcneill 	hdr->flags = BWFM_BCDC_FLAG_VER(BWFM_BCDC_FLAG_PROTO_VER);
    811   1.2  jmcneill 	hdr->flags2 = 0;
    812   1.1  jmcneill 	len += sizeof(*hdr);
    813   1.1  jmcneill 
    814   1.1  jmcneill 	m_copydata(m, 0, m->m_pkthdr.len, &data->buf[len]);
    815   1.1  jmcneill 	len += m->m_pkthdr.len;
    816   1.1  jmcneill 
    817   1.1  jmcneill 	data->mbuf = m;
    818   1.1  jmcneill 
    819   1.1  jmcneill 	usbd_setup_xfer(data->xfer, data, data->buf,
    820   1.1  jmcneill 	    len, USBD_FORCE_SHORT_XFER, USBD_NO_TIMEOUT,
    821   1.1  jmcneill 	    bwfm_usb_txeof);
    822   1.1  jmcneill 	error = usbd_transfer(data->xfer);
    823   1.1  jmcneill 	if (error != 0 && error != USBD_IN_PROGRESS)
    824   1.1  jmcneill 		printf("%s: could not set up new transfer: %s\n",
    825   1.1  jmcneill 		    DEVNAME(sc), usbd_errstr(error));
    826   1.1  jmcneill 	return 0;
    827   1.1  jmcneill }
    828   1.1  jmcneill 
    829   1.1  jmcneill int
    830   1.1  jmcneill bwfm_usb_txctl(struct bwfm_softc *bwfm, char *buf, size_t len)
    831   1.1  jmcneill {
    832   1.1  jmcneill 	struct bwfm_usb_softc *sc = (void *)bwfm;
    833   1.1  jmcneill 	usb_device_request_t req;
    834   1.1  jmcneill 	usbd_status error;
    835   1.1  jmcneill 	int ret = 1;
    836   1.1  jmcneill 
    837   1.1  jmcneill 	DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
    838   1.1  jmcneill 
    839   1.1  jmcneill 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    840   1.1  jmcneill 	req.bRequest = 0;
    841   1.1  jmcneill 
    842   1.1  jmcneill 	USETW(req.wValue, 0);
    843   1.1  jmcneill 	USETW(req.wIndex, sc->sc_ifaceno);
    844   1.1  jmcneill 	USETW(req.wLength, len);
    845   1.1  jmcneill 
    846   1.1  jmcneill 	error = usbd_do_request(sc->sc_udev, &req, buf);
    847   1.1  jmcneill 	if (error != 0) {
    848   1.1  jmcneill 		printf("%s: could not read ctl packet: %s\n",
    849   1.1  jmcneill 		    DEVNAME(sc), usbd_errstr(error));
    850   1.1  jmcneill 		goto err;
    851   1.1  jmcneill 	}
    852   1.1  jmcneill 
    853   1.1  jmcneill 	ret = 0;
    854   1.1  jmcneill err:
    855   1.1  jmcneill 	return ret;
    856   1.1  jmcneill }
    857   1.1  jmcneill 
    858   1.1  jmcneill int
    859   1.1  jmcneill bwfm_usb_rxctl(struct bwfm_softc *bwfm, char *buf, size_t *len)
    860   1.1  jmcneill {
    861   1.1  jmcneill 	struct bwfm_usb_softc *sc = (void *)bwfm;
    862   1.1  jmcneill 	usb_device_request_t req;
    863   1.1  jmcneill 	usbd_status error;
    864   1.1  jmcneill 	uint32_t len32;
    865   1.1  jmcneill 	int ret = 1;
    866   1.1  jmcneill 
    867   1.1  jmcneill 	DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
    868   1.1  jmcneill 
    869   1.1  jmcneill 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    870   1.1  jmcneill 	req.bRequest = 1;
    871   1.1  jmcneill 
    872   1.1  jmcneill 	USETW(req.wValue, 0);
    873   1.1  jmcneill 	USETW(req.wIndex, sc->sc_ifaceno);
    874   1.1  jmcneill 	USETW(req.wLength, *len);
    875   1.1  jmcneill 
    876   1.1  jmcneill 	error = usbd_do_request_flags(sc->sc_udev, &req, buf, 0,
    877   1.1  jmcneill 	    &len32, USBD_DEFAULT_TIMEOUT);
    878   1.1  jmcneill 	if (error != 0) {
    879   1.1  jmcneill 		printf("%s: could not read ctl packet: %s\n",
    880   1.1  jmcneill 		    DEVNAME(sc), usbd_errstr(error));
    881   1.1  jmcneill 		goto err;
    882   1.1  jmcneill 	}
    883   1.1  jmcneill 
    884   1.1  jmcneill 	if (len32 > *len) {
    885   1.1  jmcneill 		printf("%s: broken length\n", DEVNAME(sc));
    886   1.1  jmcneill 		goto err;
    887   1.1  jmcneill 	}
    888   1.1  jmcneill 
    889   1.1  jmcneill 	*len = len32;
    890   1.1  jmcneill 	ret = 0;
    891   1.1  jmcneill err:
    892   1.1  jmcneill 	return ret;
    893   1.1  jmcneill }
    894