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