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