if_bwfm_usb.c revision 1.1 1 1.1 jmcneill /* $NetBSD: if_bwfm_usb.c,v 1.1 2017/10/19 23:58:41 jmcneill 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.1 jmcneill int bwfm_usb_txdata(struct bwfm_softc *, struct mbuf *);
203 1.1 jmcneill int bwfm_usb_txctl(struct bwfm_softc *, char *, size_t);
204 1.1 jmcneill int bwfm_usb_rxctl(struct bwfm_softc *, char *, size_t *);
205 1.1 jmcneill
206 1.1 jmcneill void bwfm_usb_rxeof(struct usbd_xfer *, void *, usbd_status);
207 1.1 jmcneill void bwfm_usb_txeof(struct usbd_xfer *, void *, usbd_status);
208 1.1 jmcneill
209 1.1 jmcneill struct bwfm_bus_ops bwfm_usb_bus_ops = {
210 1.1 jmcneill .bs_init = NULL,
211 1.1 jmcneill .bs_stop = NULL,
212 1.1 jmcneill .bs_txdata = bwfm_usb_txdata,
213 1.1 jmcneill .bs_txctl = bwfm_usb_txctl,
214 1.1 jmcneill .bs_rxctl = bwfm_usb_rxctl,
215 1.1 jmcneill };
216 1.1 jmcneill
217 1.1 jmcneill CFATTACH_DECL_NEW(bwfm_usb, sizeof(struct bwfm_usb_softc),
218 1.1 jmcneill bwfm_usb_match, bwfm_usb_attach, bwfm_usb_detach, NULL);
219 1.1 jmcneill
220 1.1 jmcneill int
221 1.1 jmcneill bwfm_usb_match(device_t parent, cfdata_t match, void *aux)
222 1.1 jmcneill {
223 1.1 jmcneill struct usb_attach_arg *uaa = aux;
224 1.1 jmcneill
225 1.1 jmcneill return (usb_lookup(bwfm_usbdevs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
226 1.1 jmcneill UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill void
230 1.1 jmcneill bwfm_usb_attach(device_t parent, device_t self, void *aux)
231 1.1 jmcneill {
232 1.1 jmcneill struct bwfm_usb_softc *sc = device_private(self);
233 1.1 jmcneill struct usb_attach_arg *uaa = aux;
234 1.1 jmcneill usb_device_descriptor_t *dd;
235 1.1 jmcneill usb_interface_descriptor_t *id;
236 1.1 jmcneill usb_endpoint_descriptor_t *ed;
237 1.1 jmcneill char *devinfop;
238 1.1 jmcneill int i;
239 1.1 jmcneill
240 1.1 jmcneill sc->sc_sc.sc_dev = self;
241 1.1 jmcneill sc->sc_udev = uaa->uaa_device;
242 1.1 jmcneill mutex_init(&sc->sc_rx_lock, MUTEX_DEFAULT, IPL_NET);
243 1.1 jmcneill mutex_init(&sc->sc_tx_lock, MUTEX_DEFAULT, IPL_NET);
244 1.1 jmcneill
245 1.1 jmcneill aprint_naive("\n");
246 1.1 jmcneill
247 1.1 jmcneill devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
248 1.1 jmcneill aprint_normal(": %s\n", devinfop);
249 1.1 jmcneill usbd_devinfo_free(devinfop);
250 1.1 jmcneill
251 1.1 jmcneill if (usbd_set_config_no(sc->sc_udev, 1, 1) != 0) {
252 1.1 jmcneill aprint_error_dev(self, "failed to set configuration\n");
253 1.1 jmcneill return;
254 1.1 jmcneill }
255 1.1 jmcneill if (usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface) != 0) {
256 1.1 jmcneill aprint_error_dev(self, "failed to get interface handle\n");
257 1.1 jmcneill return;
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill sc->sc_ifaceno = 0;
261 1.1 jmcneill sc->sc_vendor = uaa->uaa_vendor;
262 1.1 jmcneill sc->sc_product = uaa->uaa_product;
263 1.1 jmcneill sc->sc_sc.sc_bus_ops = &bwfm_usb_bus_ops;
264 1.1 jmcneill sc->sc_sc.sc_proto_ops = &bwfm_proto_bcdc_ops;
265 1.1 jmcneill
266 1.1 jmcneill /* Check number of configurations. */
267 1.1 jmcneill dd = usbd_get_device_descriptor(sc->sc_udev);
268 1.1 jmcneill if (dd->bNumConfigurations != 1) {
269 1.1 jmcneill printf("%s: number of configurations not supported\n",
270 1.1 jmcneill DEVNAME(sc));
271 1.1 jmcneill return;
272 1.1 jmcneill }
273 1.1 jmcneill
274 1.1 jmcneill /* Get endpoints. */
275 1.1 jmcneill id = usbd_get_interface_descriptor(sc->sc_iface);
276 1.1 jmcneill
277 1.1 jmcneill sc->sc_rx_no = sc->sc_tx_no = -1;
278 1.1 jmcneill for (i = 0; i < id->bNumEndpoints; i++) {
279 1.1 jmcneill ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
280 1.1 jmcneill if (ed == NULL) {
281 1.1 jmcneill printf("%s: no endpoint descriptor for iface %d\n",
282 1.1 jmcneill DEVNAME(sc), i);
283 1.1 jmcneill return;
284 1.1 jmcneill }
285 1.1 jmcneill
286 1.1 jmcneill if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
287 1.1 jmcneill UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
288 1.1 jmcneill sc->sc_rx_no == -1)
289 1.1 jmcneill sc->sc_rx_no = ed->bEndpointAddress;
290 1.1 jmcneill else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
291 1.1 jmcneill UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK &&
292 1.1 jmcneill sc->sc_tx_no == -1)
293 1.1 jmcneill sc->sc_tx_no = ed->bEndpointAddress;
294 1.1 jmcneill }
295 1.1 jmcneill if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
296 1.1 jmcneill printf("%s: missing endpoint\n", DEVNAME(sc));
297 1.1 jmcneill return;
298 1.1 jmcneill }
299 1.1 jmcneill
300 1.1 jmcneill config_mountroot(self, bwfm_usb_attachhook);
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill void
304 1.1 jmcneill bwfm_usb_attachhook(device_t self)
305 1.1 jmcneill {
306 1.1 jmcneill struct bwfm_usb_softc *sc = device_private(self);
307 1.1 jmcneill struct bwfm_usb_rx_data *data;
308 1.1 jmcneill const char *name = NULL;
309 1.1 jmcneill struct bootrom_id brom;
310 1.1 jmcneill firmware_handle_t fwh;
311 1.1 jmcneill usbd_status error;
312 1.1 jmcneill u_char *ucode;
313 1.1 jmcneill size_t size;
314 1.1 jmcneill int i;
315 1.1 jmcneill
316 1.1 jmcneill /* Read chip id and chip rev to check the firmware. */
317 1.1 jmcneill memset(&brom, 0, sizeof(brom));
318 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_GETVER, &brom, sizeof(brom));
319 1.1 jmcneill sc->sc_chip = le32toh(brom.chip);
320 1.1 jmcneill sc->sc_chiprev = le32toh(brom.chiprev);
321 1.1 jmcneill
322 1.1 jmcneill /* Setup data pipes */
323 1.1 jmcneill error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
324 1.1 jmcneill &sc->sc_rx_pipeh);
325 1.1 jmcneill if (error != 0) {
326 1.1 jmcneill printf("%s: could not open rx pipe: %s\n",
327 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
328 1.1 jmcneill return;
329 1.1 jmcneill }
330 1.1 jmcneill error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
331 1.1 jmcneill &sc->sc_tx_pipeh);
332 1.1 jmcneill if (error != 0) {
333 1.1 jmcneill printf("%s: could not open tx pipe: %s\n",
334 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
335 1.1 jmcneill return;
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill /* Firmware not yet loaded? */
339 1.1 jmcneill if (sc->sc_chip != BRCMF_POSTBOOT_ID) {
340 1.1 jmcneill switch (sc->sc_chip)
341 1.1 jmcneill {
342 1.1 jmcneill case BRCM_CC_43143_CHIP_ID:
343 1.1 jmcneill name = "brcmfmac43143.bin";
344 1.1 jmcneill break;
345 1.1 jmcneill case BRCM_CC_43235_CHIP_ID:
346 1.1 jmcneill case BRCM_CC_43236_CHIP_ID:
347 1.1 jmcneill case BRCM_CC_43238_CHIP_ID:
348 1.1 jmcneill if (sc->sc_chiprev == 3)
349 1.1 jmcneill name = "brcmfmac43236b.bin";
350 1.1 jmcneill break;
351 1.1 jmcneill case BRCM_CC_43242_CHIP_ID:
352 1.1 jmcneill name = "brcmfmac43242a.bin";
353 1.1 jmcneill break;
354 1.1 jmcneill case BRCM_CC_43566_CHIP_ID:
355 1.1 jmcneill case BRCM_CC_43569_CHIP_ID:
356 1.1 jmcneill name = "brcmfmac43569.bin";
357 1.1 jmcneill break;
358 1.1 jmcneill default:
359 1.1 jmcneill break;
360 1.1 jmcneill }
361 1.1 jmcneill
362 1.1 jmcneill if (name == NULL) {
363 1.1 jmcneill printf("%s: unknown firmware\n", DEVNAME(sc));
364 1.1 jmcneill return;
365 1.1 jmcneill }
366 1.1 jmcneill
367 1.1 jmcneill if (firmware_open("if_bwfm", name, &fwh) != 0) {
368 1.1 jmcneill printf("%s: failed firmware_open of file %s\n",
369 1.1 jmcneill DEVNAME(sc), name);
370 1.1 jmcneill return;
371 1.1 jmcneill }
372 1.1 jmcneill size = firmware_get_size(fwh);
373 1.1 jmcneill ucode = firmware_malloc(size);
374 1.1 jmcneill if (ucode == NULL) {
375 1.1 jmcneill printf("%s: failed to allocate firmware memory\n",
376 1.1 jmcneill DEVNAME(sc));
377 1.1 jmcneill firmware_close(fwh);
378 1.1 jmcneill return;
379 1.1 jmcneill }
380 1.1 jmcneill error = firmware_read(fwh, 0, ucode, size);
381 1.1 jmcneill firmware_close(fwh);
382 1.1 jmcneill if (error != 0) {
383 1.1 jmcneill printf("%s: failed to read firmware (error %d)\n",
384 1.1 jmcneill DEVNAME(sc), error);
385 1.1 jmcneill firmware_free(ucode, size);
386 1.1 jmcneill return;
387 1.1 jmcneill }
388 1.1 jmcneill
389 1.1 jmcneill if (bwfm_usb_load_microcode(sc, ucode, size) != 0) {
390 1.1 jmcneill printf("%s: could not load microcode\n",
391 1.1 jmcneill DEVNAME(sc));
392 1.1 jmcneill return;
393 1.1 jmcneill }
394 1.1 jmcneill
395 1.1 jmcneill firmware_free(ucode, size);
396 1.1 jmcneill
397 1.1 jmcneill for (i = 0; i < 10; i++) {
398 1.1 jmcneill delay(100 * 1000);
399 1.1 jmcneill memset(&brom, 0, sizeof(brom));
400 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_GETVER, &brom, sizeof(brom));
401 1.1 jmcneill if (le32toh(brom.chip) == BRCMF_POSTBOOT_ID)
402 1.1 jmcneill break;
403 1.1 jmcneill }
404 1.1 jmcneill
405 1.1 jmcneill if (le32toh(brom.chip) != BRCMF_POSTBOOT_ID) {
406 1.1 jmcneill printf("%s: firmware did not start up\n",
407 1.1 jmcneill DEVNAME(sc));
408 1.1 jmcneill return;
409 1.1 jmcneill }
410 1.1 jmcneill
411 1.1 jmcneill sc->sc_chip = le32toh(brom.chip);
412 1.1 jmcneill sc->sc_chiprev = le32toh(brom.chiprev);
413 1.1 jmcneill printf("%s: firmware loaded\n", DEVNAME(sc));
414 1.1 jmcneill }
415 1.1 jmcneill
416 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_RESETCFG, &brom, sizeof(brom));
417 1.1 jmcneill
418 1.1 jmcneill if (bwfm_usb_alloc_rx_list(sc) || bwfm_usb_alloc_tx_list(sc)) {
419 1.1 jmcneill printf("%s: cannot allocate rx/tx lists\n", DEVNAME(sc));
420 1.1 jmcneill return;
421 1.1 jmcneill }
422 1.1 jmcneill
423 1.1 jmcneill bwfm_attach(&sc->sc_sc);
424 1.1 jmcneill
425 1.1 jmcneill for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
426 1.1 jmcneill data = &sc->sc_rx_data[i];
427 1.1 jmcneill
428 1.1 jmcneill usbd_setup_xfer(data->xfer, data, data->buf,
429 1.1 jmcneill BWFM_RXBUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
430 1.1 jmcneill bwfm_usb_rxeof);
431 1.1 jmcneill error = usbd_transfer(data->xfer);
432 1.1 jmcneill if (error != 0 && error != USBD_IN_PROGRESS)
433 1.1 jmcneill printf("%s: could not set up new transfer: %s\n",
434 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
435 1.1 jmcneill }
436 1.1 jmcneill }
437 1.1 jmcneill
438 1.1 jmcneill void
439 1.1 jmcneill bwfm_usb_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
440 1.1 jmcneill {
441 1.1 jmcneill struct bwfm_usb_rx_data *data = priv;
442 1.1 jmcneill struct bwfm_usb_softc *sc = data->sc;
443 1.1 jmcneill struct bwfm_proto_bcdc_hdr *hdr;
444 1.1 jmcneill usbd_status error;
445 1.1 jmcneill uint32_t len, off;
446 1.1 jmcneill
447 1.1 jmcneill DPRINTFN(2, ("%s: %s status %s\n", DEVNAME(sc), __func__,
448 1.1 jmcneill usbd_errstr(status)));
449 1.1 jmcneill
450 1.1 jmcneill if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
451 1.1 jmcneill usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
452 1.1 jmcneill if (status != USBD_CANCELLED)
453 1.1 jmcneill goto resubmit;
454 1.1 jmcneill return;
455 1.1 jmcneill }
456 1.1 jmcneill usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
457 1.1 jmcneill
458 1.1 jmcneill off = 0;
459 1.1 jmcneill hdr = (void *)data->buf;
460 1.1 jmcneill if (len < sizeof(*hdr))
461 1.1 jmcneill goto resubmit;
462 1.1 jmcneill len -= sizeof(*hdr);
463 1.1 jmcneill off += sizeof(*hdr);
464 1.1 jmcneill if (len < hdr->data_offset << 2)
465 1.1 jmcneill goto resubmit;
466 1.1 jmcneill len -= hdr->data_offset << 2;
467 1.1 jmcneill off += hdr->data_offset << 2;
468 1.1 jmcneill
469 1.1 jmcneill mutex_enter(&sc->sc_rx_lock);
470 1.1 jmcneill bwfm_rx(&sc->sc_sc, &data->buf[off], len);
471 1.1 jmcneill mutex_exit(&sc->sc_rx_lock);
472 1.1 jmcneill
473 1.1 jmcneill resubmit:
474 1.1 jmcneill usbd_setup_xfer(data->xfer, data, data->buf,
475 1.1 jmcneill BWFM_RXBUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
476 1.1 jmcneill bwfm_usb_rxeof);
477 1.1 jmcneill error = usbd_transfer(data->xfer);
478 1.1 jmcneill if (error != 0 && error != USBD_IN_PROGRESS)
479 1.1 jmcneill printf("%s: could not set up new transfer: %s\n",
480 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
481 1.1 jmcneill }
482 1.1 jmcneill
483 1.1 jmcneill int
484 1.1 jmcneill bwfm_usb_alloc_rx_list(struct bwfm_usb_softc *sc)
485 1.1 jmcneill {
486 1.1 jmcneill struct bwfm_usb_rx_data *data;
487 1.1 jmcneill int i, error = 0;
488 1.1 jmcneill
489 1.1 jmcneill for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
490 1.1 jmcneill data = &sc->sc_rx_data[i];
491 1.1 jmcneill
492 1.1 jmcneill data->sc = sc; /* Backpointer for callbacks. */
493 1.1 jmcneill
494 1.1 jmcneill if (usbd_create_xfer(sc->sc_rx_pipeh, BWFM_RXBUFSZ,
495 1.1 jmcneill USBD_SHORT_XFER_OK, 0, &data->xfer) != 0) {
496 1.1 jmcneill printf("%s: could not create xfer\n",
497 1.1 jmcneill DEVNAME(sc));
498 1.1 jmcneill error = ENOMEM;
499 1.1 jmcneill break;
500 1.1 jmcneill }
501 1.1 jmcneill data->buf = usbd_get_buffer(data->xfer);
502 1.1 jmcneill }
503 1.1 jmcneill if (error != 0)
504 1.1 jmcneill bwfm_usb_free_rx_list(sc);
505 1.1 jmcneill return (error);
506 1.1 jmcneill }
507 1.1 jmcneill
508 1.1 jmcneill void
509 1.1 jmcneill bwfm_usb_free_rx_list(struct bwfm_usb_softc *sc)
510 1.1 jmcneill {
511 1.1 jmcneill int i;
512 1.1 jmcneill
513 1.1 jmcneill /* NB: Caller must abort pipe first. */
514 1.1 jmcneill for (i = 0; i < BWFM_RX_LIST_COUNT; i++) {
515 1.1 jmcneill if (sc->sc_rx_data[i].xfer != NULL)
516 1.1 jmcneill usbd_destroy_xfer(sc->sc_rx_data[i].xfer);
517 1.1 jmcneill sc->sc_rx_data[i].xfer = NULL;
518 1.1 jmcneill }
519 1.1 jmcneill }
520 1.1 jmcneill
521 1.1 jmcneill int
522 1.1 jmcneill bwfm_usb_alloc_tx_list(struct bwfm_usb_softc *sc)
523 1.1 jmcneill {
524 1.1 jmcneill struct bwfm_usb_tx_data *data;
525 1.1 jmcneill int i, error = 0;
526 1.1 jmcneill
527 1.1 jmcneill TAILQ_INIT(&sc->sc_tx_free_list);
528 1.1 jmcneill for (i = 0; i < BWFM_TX_LIST_COUNT; i++) {
529 1.1 jmcneill data = &sc->sc_tx_data[i];
530 1.1 jmcneill
531 1.1 jmcneill data->sc = sc; /* Backpointer for callbacks. */
532 1.1 jmcneill
533 1.1 jmcneill if (usbd_create_xfer(sc->sc_tx_pipeh, BWFM_TXBUFSZ,
534 1.1 jmcneill USBD_FORCE_SHORT_XFER, 0, &data->xfer) != 0) {
535 1.1 jmcneill printf("%s: could not create xfer\n",
536 1.1 jmcneill DEVNAME(sc));
537 1.1 jmcneill error = ENOMEM;
538 1.1 jmcneill break;
539 1.1 jmcneill }
540 1.1 jmcneill data->buf = usbd_get_buffer(data->xfer);
541 1.1 jmcneill
542 1.1 jmcneill /* Append this Tx buffer to our free list. */
543 1.1 jmcneill TAILQ_INSERT_TAIL(&sc->sc_tx_free_list, data, next);
544 1.1 jmcneill }
545 1.1 jmcneill if (error != 0)
546 1.1 jmcneill bwfm_usb_free_tx_list(sc);
547 1.1 jmcneill return (error);
548 1.1 jmcneill }
549 1.1 jmcneill
550 1.1 jmcneill void
551 1.1 jmcneill bwfm_usb_free_tx_list(struct bwfm_usb_softc *sc)
552 1.1 jmcneill {
553 1.1 jmcneill int i;
554 1.1 jmcneill
555 1.1 jmcneill /* NB: Caller must abort pipe first. */
556 1.1 jmcneill for (i = 0; i < BWFM_TX_LIST_COUNT; i++) {
557 1.1 jmcneill if (sc->sc_tx_data[i].xfer != NULL)
558 1.1 jmcneill usbd_destroy_xfer(sc->sc_tx_data[i].xfer);
559 1.1 jmcneill sc->sc_tx_data[i].xfer = NULL;
560 1.1 jmcneill }
561 1.1 jmcneill }
562 1.1 jmcneill
563 1.1 jmcneill void
564 1.1 jmcneill bwfm_usb_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
565 1.1 jmcneill {
566 1.1 jmcneill struct bwfm_usb_tx_data *data = priv;
567 1.1 jmcneill struct bwfm_usb_softc *sc = data->sc;
568 1.1 jmcneill struct ifnet *ifp = sc->sc_sc.sc_ic.ic_ifp;
569 1.1 jmcneill int s;
570 1.1 jmcneill
571 1.1 jmcneill DPRINTFN(2, ("%s: %s status %s\n", DEVNAME(sc), __func__,
572 1.1 jmcneill usbd_errstr(status)));
573 1.1 jmcneill
574 1.1 jmcneill m_freem(data->mbuf);
575 1.1 jmcneill data->mbuf = NULL;
576 1.1 jmcneill
577 1.1 jmcneill mutex_enter(&sc->sc_tx_lock);
578 1.1 jmcneill /* Put this Tx buffer back to our free list. */
579 1.1 jmcneill TAILQ_INSERT_TAIL(&sc->sc_tx_free_list, data, next);
580 1.1 jmcneill mutex_exit(&sc->sc_tx_lock);
581 1.1 jmcneill
582 1.1 jmcneill s = splnet();
583 1.1 jmcneill
584 1.1 jmcneill if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
585 1.1 jmcneill if (status == USBD_CANCELLED)
586 1.1 jmcneill usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
587 1.1 jmcneill ifp->if_oerrors++;
588 1.1 jmcneill splx(s);
589 1.1 jmcneill return;
590 1.1 jmcneill }
591 1.1 jmcneill
592 1.1 jmcneill ifp->if_opackets++;
593 1.1 jmcneill
594 1.1 jmcneill /* We just released a Tx buffer, notify Tx. */
595 1.1 jmcneill if ((ifp->if_flags & IFF_OACTIVE) != 0) {
596 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
597 1.1 jmcneill if_schedule_deferred_start(ifp);
598 1.1 jmcneill }
599 1.1 jmcneill splx(s);
600 1.1 jmcneill }
601 1.1 jmcneill
602 1.1 jmcneill int
603 1.1 jmcneill bwfm_usb_detach(device_t self, int flags)
604 1.1 jmcneill {
605 1.1 jmcneill struct bwfm_usb_softc *sc = device_private(self);
606 1.1 jmcneill
607 1.1 jmcneill bwfm_detach(&sc->sc_sc, flags);
608 1.1 jmcneill
609 1.1 jmcneill if (sc->sc_rx_pipeh != NULL) {
610 1.1 jmcneill usbd_abort_pipe(sc->sc_rx_pipeh);
611 1.1 jmcneill usbd_close_pipe(sc->sc_rx_pipeh);
612 1.1 jmcneill }
613 1.1 jmcneill if (sc->sc_tx_pipeh != NULL) {
614 1.1 jmcneill usbd_abort_pipe(sc->sc_tx_pipeh);
615 1.1 jmcneill usbd_close_pipe(sc->sc_tx_pipeh);
616 1.1 jmcneill }
617 1.1 jmcneill
618 1.1 jmcneill bwfm_usb_free_rx_list(sc);
619 1.1 jmcneill bwfm_usb_free_tx_list(sc);
620 1.1 jmcneill
621 1.1 jmcneill mutex_destroy(&sc->sc_rx_lock);
622 1.1 jmcneill mutex_destroy(&sc->sc_tx_lock);
623 1.1 jmcneill
624 1.1 jmcneill return 0;
625 1.1 jmcneill }
626 1.1 jmcneill
627 1.1 jmcneill int
628 1.1 jmcneill bwfm_usb_dl_cmd(struct bwfm_usb_softc *sc, uByte cmd, void *buf, int len)
629 1.1 jmcneill {
630 1.1 jmcneill usb_device_request_t req;
631 1.1 jmcneill usbd_status error;
632 1.1 jmcneill
633 1.1 jmcneill req.bmRequestType = UT_READ_VENDOR_INTERFACE;
634 1.1 jmcneill req.bRequest = cmd;
635 1.1 jmcneill
636 1.1 jmcneill USETW(req.wValue, 0);
637 1.1 jmcneill USETW(req.wIndex, sc->sc_ifaceno);
638 1.1 jmcneill USETW(req.wLength, len);
639 1.1 jmcneill
640 1.1 jmcneill error = usbd_do_request(sc->sc_udev, &req, buf);
641 1.1 jmcneill if (error != 0) {
642 1.1 jmcneill printf("%s: could not read register: %s\n",
643 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
644 1.1 jmcneill }
645 1.1 jmcneill return error;
646 1.1 jmcneill }
647 1.1 jmcneill
648 1.1 jmcneill int
649 1.1 jmcneill bwfm_usb_load_microcode(struct bwfm_usb_softc *sc, const u_char *ucode, size_t size)
650 1.1 jmcneill {
651 1.1 jmcneill const struct trx_header *trx = (const struct trx_header *)ucode;
652 1.1 jmcneill struct rdl_state state;
653 1.1 jmcneill uint32_t rdlstate, rdlbytes, sent = 0, sendlen = 0;
654 1.1 jmcneill struct usbd_xfer *xfer;
655 1.1 jmcneill usbd_status error;
656 1.1 jmcneill char *buf;
657 1.1 jmcneill
658 1.1 jmcneill if (le32toh(trx->magic) != TRX_MAGIC ||
659 1.1 jmcneill (le32toh(trx->flag_version) & TRX_UNCOMP_IMAGE) == 0) {
660 1.1 jmcneill printf("%s: invalid firmware\n", DEVNAME(sc));
661 1.1 jmcneill return 1;
662 1.1 jmcneill }
663 1.1 jmcneill
664 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_START, &state, sizeof(state));
665 1.1 jmcneill rdlstate = le32toh(state.state);
666 1.1 jmcneill rdlbytes = le32toh(state.bytes);
667 1.1 jmcneill
668 1.1 jmcneill if (rdlstate != DL_WAITING) {
669 1.1 jmcneill printf("%s: cannot start fw download\n", DEVNAME(sc));
670 1.1 jmcneill return 1;
671 1.1 jmcneill }
672 1.1 jmcneill
673 1.1 jmcneill error = usbd_create_xfer(sc->sc_tx_pipeh, TRX_RDL_CHUNK,
674 1.1 jmcneill 0, 0, &xfer);
675 1.1 jmcneill if (error != 0) {
676 1.1 jmcneill printf("%s: cannot create xfer\n", DEVNAME(sc));
677 1.1 jmcneill goto err;
678 1.1 jmcneill }
679 1.1 jmcneill
680 1.1 jmcneill buf = usbd_get_buffer(xfer);
681 1.1 jmcneill
682 1.1 jmcneill while (rdlbytes != size) {
683 1.1 jmcneill sendlen = MIN(size - sent, TRX_RDL_CHUNK);
684 1.1 jmcneill memcpy(buf, ucode + sent, sendlen);
685 1.1 jmcneill
686 1.1 jmcneill usbd_setup_xfer(xfer, NULL, buf, sendlen,
687 1.1 jmcneill USBD_SYNCHRONOUS, USBD_NO_TIMEOUT, NULL);
688 1.1 jmcneill error = usbd_transfer(xfer);
689 1.1 jmcneill if (error != 0 && error != USBD_IN_PROGRESS) {
690 1.1 jmcneill printf("%s: transfer error\n", DEVNAME(sc));
691 1.1 jmcneill goto err;
692 1.1 jmcneill }
693 1.1 jmcneill sent += sendlen;
694 1.1 jmcneill
695 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_GETSTATE, &state, sizeof(state));
696 1.1 jmcneill rdlstate = le32toh(state.state);
697 1.1 jmcneill rdlbytes = le32toh(state.bytes);
698 1.1 jmcneill
699 1.1 jmcneill if (rdlbytes != sent) {
700 1.1 jmcneill printf("%s: device reported different size\n",
701 1.1 jmcneill DEVNAME(sc));
702 1.1 jmcneill goto err;
703 1.1 jmcneill }
704 1.1 jmcneill
705 1.1 jmcneill if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
706 1.1 jmcneill printf("%s: device reported bad hdr/crc\n",
707 1.1 jmcneill DEVNAME(sc));
708 1.1 jmcneill goto err;
709 1.1 jmcneill }
710 1.1 jmcneill }
711 1.1 jmcneill
712 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_GETSTATE, &state, sizeof(state));
713 1.1 jmcneill rdlstate = le32toh(state.state);
714 1.1 jmcneill rdlbytes = le32toh(state.bytes);
715 1.1 jmcneill
716 1.1 jmcneill if (rdlstate != DL_RUNNABLE) {
717 1.1 jmcneill printf("%s: dongle not runnable\n", DEVNAME(sc));
718 1.1 jmcneill goto err;
719 1.1 jmcneill }
720 1.1 jmcneill
721 1.1 jmcneill bwfm_usb_dl_cmd(sc, DL_GO, &state, sizeof(state));
722 1.1 jmcneill
723 1.1 jmcneill usbd_destroy_xfer(xfer);
724 1.1 jmcneill
725 1.1 jmcneill return 0;
726 1.1 jmcneill err:
727 1.1 jmcneill if (sc->sc_tx_pipeh != NULL) {
728 1.1 jmcneill usbd_abort_pipe(sc->sc_tx_pipeh);
729 1.1 jmcneill usbd_close_pipe(sc->sc_tx_pipeh);
730 1.1 jmcneill sc->sc_tx_pipeh = NULL;
731 1.1 jmcneill }
732 1.1 jmcneill if (xfer != NULL)
733 1.1 jmcneill usbd_destroy_xfer(xfer);
734 1.1 jmcneill return 1;
735 1.1 jmcneill }
736 1.1 jmcneill
737 1.1 jmcneill int
738 1.1 jmcneill bwfm_usb_txdata(struct bwfm_softc *bwfm, struct mbuf *m)
739 1.1 jmcneill {
740 1.1 jmcneill struct bwfm_usb_softc *sc = (void *)bwfm;
741 1.1 jmcneill struct bwfm_proto_bcdc_hdr *hdr;
742 1.1 jmcneill struct bwfm_usb_tx_data *data;
743 1.1 jmcneill uint32_t len = 0;
744 1.1 jmcneill int error;
745 1.1 jmcneill
746 1.1 jmcneill DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
747 1.1 jmcneill
748 1.1 jmcneill mutex_enter(&sc->sc_tx_lock);
749 1.1 jmcneill
750 1.1 jmcneill if (TAILQ_EMPTY(&sc->sc_tx_free_list)) {
751 1.1 jmcneill mutex_exit(&sc->sc_tx_lock);
752 1.1 jmcneill return ENOBUFS;
753 1.1 jmcneill }
754 1.1 jmcneill
755 1.1 jmcneill /* Grab a Tx buffer from our free list. */
756 1.1 jmcneill data = TAILQ_FIRST(&sc->sc_tx_free_list);
757 1.1 jmcneill TAILQ_REMOVE(&sc->sc_tx_free_list, data, next);
758 1.1 jmcneill
759 1.1 jmcneill mutex_exit(&sc->sc_tx_lock);
760 1.1 jmcneill
761 1.1 jmcneill hdr = (void *)&data->buf[len];
762 1.1 jmcneill hdr->data_offset = 0;
763 1.1 jmcneill hdr->flags = BWFM_BCDC_FLAG_VER(BWFM_BCDC_FLAG_PROTO_VER);
764 1.1 jmcneill len += sizeof(*hdr);
765 1.1 jmcneill
766 1.1 jmcneill m_copydata(m, 0, m->m_pkthdr.len, &data->buf[len]);
767 1.1 jmcneill len += m->m_pkthdr.len;
768 1.1 jmcneill
769 1.1 jmcneill data->mbuf = m;
770 1.1 jmcneill
771 1.1 jmcneill usbd_setup_xfer(data->xfer, data, data->buf,
772 1.1 jmcneill len, USBD_FORCE_SHORT_XFER, USBD_NO_TIMEOUT,
773 1.1 jmcneill bwfm_usb_txeof);
774 1.1 jmcneill error = usbd_transfer(data->xfer);
775 1.1 jmcneill if (error != 0 && error != USBD_IN_PROGRESS)
776 1.1 jmcneill printf("%s: could not set up new transfer: %s\n",
777 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
778 1.1 jmcneill return 0;
779 1.1 jmcneill }
780 1.1 jmcneill
781 1.1 jmcneill int
782 1.1 jmcneill bwfm_usb_txctl(struct bwfm_softc *bwfm, char *buf, size_t len)
783 1.1 jmcneill {
784 1.1 jmcneill struct bwfm_usb_softc *sc = (void *)bwfm;
785 1.1 jmcneill usb_device_request_t req;
786 1.1 jmcneill usbd_status error;
787 1.1 jmcneill int ret = 1;
788 1.1 jmcneill
789 1.1 jmcneill DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
790 1.1 jmcneill
791 1.1 jmcneill req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
792 1.1 jmcneill req.bRequest = 0;
793 1.1 jmcneill
794 1.1 jmcneill USETW(req.wValue, 0);
795 1.1 jmcneill USETW(req.wIndex, sc->sc_ifaceno);
796 1.1 jmcneill USETW(req.wLength, len);
797 1.1 jmcneill
798 1.1 jmcneill error = usbd_do_request(sc->sc_udev, &req, buf);
799 1.1 jmcneill if (error != 0) {
800 1.1 jmcneill printf("%s: could not read ctl packet: %s\n",
801 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
802 1.1 jmcneill goto err;
803 1.1 jmcneill }
804 1.1 jmcneill
805 1.1 jmcneill ret = 0;
806 1.1 jmcneill err:
807 1.1 jmcneill return ret;
808 1.1 jmcneill }
809 1.1 jmcneill
810 1.1 jmcneill int
811 1.1 jmcneill bwfm_usb_rxctl(struct bwfm_softc *bwfm, char *buf, size_t *len)
812 1.1 jmcneill {
813 1.1 jmcneill struct bwfm_usb_softc *sc = (void *)bwfm;
814 1.1 jmcneill usb_device_request_t req;
815 1.1 jmcneill usbd_status error;
816 1.1 jmcneill uint32_t len32;
817 1.1 jmcneill int ret = 1;
818 1.1 jmcneill
819 1.1 jmcneill DPRINTFN(2, ("%s: %s\n", DEVNAME(sc), __func__));
820 1.1 jmcneill
821 1.1 jmcneill req.bmRequestType = UT_READ_CLASS_INTERFACE;
822 1.1 jmcneill req.bRequest = 1;
823 1.1 jmcneill
824 1.1 jmcneill USETW(req.wValue, 0);
825 1.1 jmcneill USETW(req.wIndex, sc->sc_ifaceno);
826 1.1 jmcneill USETW(req.wLength, *len);
827 1.1 jmcneill
828 1.1 jmcneill error = usbd_do_request_flags(sc->sc_udev, &req, buf, 0,
829 1.1 jmcneill &len32, USBD_DEFAULT_TIMEOUT);
830 1.1 jmcneill if (error != 0) {
831 1.1 jmcneill printf("%s: could not read ctl packet: %s\n",
832 1.1 jmcneill DEVNAME(sc), usbd_errstr(error));
833 1.1 jmcneill goto err;
834 1.1 jmcneill }
835 1.1 jmcneill
836 1.1 jmcneill if (len32 > *len) {
837 1.1 jmcneill printf("%s: broken length\n", DEVNAME(sc));
838 1.1 jmcneill goto err;
839 1.1 jmcneill }
840 1.1 jmcneill
841 1.1 jmcneill *len = len32;
842 1.1 jmcneill ret = 0;
843 1.1 jmcneill err:
844 1.1 jmcneill return ret;
845 1.1 jmcneill }
846