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