ubt.c revision 1.42 1 /* $NetBSD: ubt.c,v 1.42 2011/12/23 00:51:45 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Lennart Augustsson (lennart (at) augustsson.net) and
39 * David Sainty (David.Sainty (at) dtsp.co.nz).
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62 /*
63 * This driver originally written by Lennart Augustsson and David Sainty,
64 * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
65 * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
66 * reference.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.42 2011/12/23 00:51:45 jakllsch Exp $");
71
72 #include <sys/param.h>
73 #include <sys/device.h>
74 #include <sys/ioctl.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/proc.h>
79 #include <sys/sysctl.h>
80 #include <sys/systm.h>
81
82 #include <dev/usb/usb.h>
83 #include <dev/usb/usbdi.h>
84 #include <dev/usb/usbdi_util.h>
85 #include <dev/usb/usbdevs.h>
86
87 #include <netbt/bluetooth.h>
88 #include <netbt/hci.h>
89
90 /*******************************************************************************
91 *
92 * debugging stuff
93 */
94 #undef DPRINTF
95 #undef DPRINTFN
96
97 #ifdef UBT_DEBUG
98 int ubt_debug = 0;
99
100 #define DPRINTF(...) do { \
101 if (ubt_debug) { \
102 printf("%s: ", __func__); \
103 printf(__VA_ARGS__); \
104 } \
105 } while (/* CONSTCOND */0)
106
107 #define DPRINTFN(n, ...) do { \
108 if (ubt_debug > (n)) { \
109 printf("%s: ", __func__); \
110 printf(__VA_ARGS__); \
111 } \
112 } while (/* CONSTCOND */0)
113
114 SYSCTL_SETUP(sysctl_hw_ubt_debug_setup, "sysctl hw.ubt_debug setup")
115 {
116
117 sysctl_createv(NULL, 0, NULL, NULL,
118 CTLFLAG_PERMANENT,
119 CTLTYPE_NODE, "hw",
120 NULL,
121 NULL, 0,
122 NULL, 0,
123 CTL_HW, CTL_EOL);
124
125 sysctl_createv(NULL, 0, NULL, NULL,
126 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
127 CTLTYPE_INT, "ubt_debug",
128 SYSCTL_DESCR("ubt debug level"),
129 NULL, 0,
130 &ubt_debug, sizeof(ubt_debug),
131 CTL_HW, CTL_CREATE, CTL_EOL);
132 }
133 #else
134 #define DPRINTF(...)
135 #define DPRINTFN(...)
136 #endif
137
138 /*******************************************************************************
139 *
140 * ubt softc structure
141 *
142 */
143
144 /* buffer sizes */
145 /*
146 * NB: although ACL packets can extend to 65535 bytes, most devices
147 * have max_acl_size at much less (largest I have seen is 384)
148 */
149 #define UBT_BUFSIZ_CMD (HCI_CMD_PKT_SIZE - 1)
150 #define UBT_BUFSIZ_ACL (2048 - 1)
151 #define UBT_BUFSIZ_EVENT (HCI_EVENT_PKT_SIZE - 1)
152
153 /* Transmit timeouts */
154 #define UBT_CMD_TIMEOUT USBD_DEFAULT_TIMEOUT
155 #define UBT_ACL_TIMEOUT USBD_DEFAULT_TIMEOUT
156
157 /*
158 * ISOC transfers
159 *
160 * xfer buffer size depends on the frame size, and the number
161 * of frames per transfer is fixed, as each frame should be
162 * 1ms worth of data. This keeps the rate that xfers complete
163 * fairly constant. We use multiple xfers to keep the hardware
164 * busy
165 */
166 #define UBT_NXFERS 3 /* max xfers to queue */
167 #define UBT_NFRAMES 10 /* frames per xfer */
168
169 struct ubt_isoc_xfer {
170 struct ubt_softc *softc;
171 usbd_xfer_handle xfer;
172 uint8_t *buf;
173 uint16_t size[UBT_NFRAMES];
174 int busy;
175 };
176
177 struct ubt_softc {
178 device_t sc_dev;
179 usbd_device_handle sc_udev;
180 int sc_refcnt;
181 int sc_dying;
182 int sc_enabled;
183
184 /* Control Interface */
185 usbd_interface_handle sc_iface0;
186
187 /* Commands (control) */
188 usbd_xfer_handle sc_cmd_xfer;
189 uint8_t *sc_cmd_buf;
190 int sc_cmd_busy; /* write active */
191 MBUFQ_HEAD() sc_cmd_queue; /* output queue */
192
193 /* Events (interrupt) */
194 int sc_evt_addr; /* endpoint address */
195 usbd_pipe_handle sc_evt_pipe;
196 uint8_t *sc_evt_buf;
197
198 /* ACL data (in) */
199 int sc_aclrd_addr; /* endpoint address */
200 usbd_pipe_handle sc_aclrd_pipe; /* read pipe */
201 usbd_xfer_handle sc_aclrd_xfer; /* read xfer */
202 uint8_t *sc_aclrd_buf; /* read buffer */
203 int sc_aclrd_busy; /* reading */
204
205 /* ACL data (out) */
206 int sc_aclwr_addr; /* endpoint address */
207 usbd_pipe_handle sc_aclwr_pipe; /* write pipe */
208 usbd_xfer_handle sc_aclwr_xfer; /* write xfer */
209 uint8_t *sc_aclwr_buf; /* write buffer */
210 int sc_aclwr_busy; /* write active */
211 MBUFQ_HEAD() sc_aclwr_queue;/* output queue */
212
213 /* ISOC interface */
214 usbd_interface_handle sc_iface1; /* ISOC interface */
215 struct sysctllog *sc_log; /* sysctl log */
216 int sc_config; /* current config no */
217 int sc_alt_config; /* no of alternates */
218
219 /* SCO data (in) */
220 int sc_scord_addr; /* endpoint address */
221 usbd_pipe_handle sc_scord_pipe; /* read pipe */
222 int sc_scord_size; /* frame length */
223 struct ubt_isoc_xfer sc_scord[UBT_NXFERS];
224 struct mbuf *sc_scord_mbuf; /* current packet */
225
226 /* SCO data (out) */
227 int sc_scowr_addr; /* endpoint address */
228 usbd_pipe_handle sc_scowr_pipe; /* write pipe */
229 int sc_scowr_size; /* frame length */
230 struct ubt_isoc_xfer sc_scowr[UBT_NXFERS];
231 struct mbuf *sc_scowr_mbuf; /* current packet */
232 int sc_scowr_busy; /* write active */
233 MBUFQ_HEAD() sc_scowr_queue;/* output queue */
234
235 /* Protocol structure */
236 struct hci_unit *sc_unit;
237 struct bt_stats sc_stats;
238
239 /* Successfully attached */
240 int sc_ok;
241 };
242
243 /*******************************************************************************
244 *
245 * Bluetooth unit/USB callback routines
246 *
247 */
248 static int ubt_enable(device_t);
249 static void ubt_disable(device_t);
250
251 static void ubt_xmit_cmd(device_t, struct mbuf *);
252 static void ubt_xmit_cmd_start(struct ubt_softc *);
253 static void ubt_xmit_cmd_complete(usbd_xfer_handle,
254 usbd_private_handle, usbd_status);
255
256 static void ubt_xmit_acl(device_t, struct mbuf *);
257 static void ubt_xmit_acl_start(struct ubt_softc *);
258 static void ubt_xmit_acl_complete(usbd_xfer_handle,
259 usbd_private_handle, usbd_status);
260
261 static void ubt_xmit_sco(device_t, struct mbuf *);
262 static void ubt_xmit_sco_start(struct ubt_softc *);
263 static void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
264 static void ubt_xmit_sco_complete(usbd_xfer_handle,
265 usbd_private_handle, usbd_status);
266
267 static void ubt_recv_event(usbd_xfer_handle,
268 usbd_private_handle, usbd_status);
269
270 static void ubt_recv_acl_start(struct ubt_softc *);
271 static void ubt_recv_acl_complete(usbd_xfer_handle,
272 usbd_private_handle, usbd_status);
273
274 static void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
275 static void ubt_recv_sco_complete(usbd_xfer_handle,
276 usbd_private_handle, usbd_status);
277
278 static void ubt_stats(device_t, struct bt_stats *, int);
279
280 static const struct hci_if ubt_hci = {
281 .enable = ubt_enable,
282 .disable = ubt_disable,
283 .output_cmd = ubt_xmit_cmd,
284 .output_acl = ubt_xmit_acl,
285 .output_sco = ubt_xmit_sco,
286 .get_stats = ubt_stats,
287 .ipl = IPL_USB, /* IPL_SOFTUSB ??? */
288 };
289
290 /*******************************************************************************
291 *
292 * USB Autoconfig stuff
293 *
294 */
295
296 int ubt_match(device_t, cfdata_t, void *);
297 void ubt_attach(device_t, device_t, void *);
298 int ubt_detach(device_t, int);
299 int ubt_activate(device_t, enum devact);
300 extern struct cfdriver ubt_cd;
301 CFATTACH_DECL_NEW(ubt, sizeof(struct ubt_softc), ubt_match, ubt_attach, ubt_detach, ubt_activate);
302
303 static int ubt_set_isoc_config(struct ubt_softc *);
304 static int ubt_sysctl_config(SYSCTLFN_PROTO);
305 static void ubt_abortdealloc(struct ubt_softc *);
306
307 /*
308 * Match against the whole device, since we want to take
309 * both interfaces. If a device should be ignored then add
310 *
311 * { VendorID, ProductID }
312 *
313 * to the ubt_ignore list.
314 */
315 static const struct usb_devno ubt_ignore[] = {
316 { USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033NF },
317 };
318
319 int
320 ubt_match(device_t parent, cfdata_t match, void *aux)
321 {
322 struct usb_attach_arg *uaa = aux;
323
324 DPRINTFN(50, "ubt_match\n");
325
326 if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
327 return UMATCH_NONE;
328
329 if (uaa->class == UDCLASS_WIRELESS
330 && uaa->subclass == UDSUBCLASS_RF
331 && uaa->proto == UDPROTO_BLUETOOTH)
332 return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;
333
334 return UMATCH_NONE;
335 }
336
337 void
338 ubt_attach(device_t parent, device_t self, void *aux)
339 {
340 struct ubt_softc *sc = device_private(self);
341 struct usb_attach_arg *uaa = aux;
342 usb_config_descriptor_t *cd;
343 usb_endpoint_descriptor_t *ed;
344 const struct sysctlnode *node;
345 char *devinfop;
346 int err;
347 uint8_t count, i;
348
349 DPRINTFN(50, "ubt_attach: sc=%p\n", sc);
350
351 sc->sc_dev = self;
352 sc->sc_udev = uaa->device;
353
354 MBUFQ_INIT(&sc->sc_cmd_queue);
355 MBUFQ_INIT(&sc->sc_aclwr_queue);
356 MBUFQ_INIT(&sc->sc_scowr_queue);
357
358 aprint_naive("\n");
359 aprint_normal("\n");
360
361 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
362 aprint_normal_dev(self, "%s\n", devinfop);
363 usbd_devinfo_free(devinfop);
364
365 /*
366 * Move the device into the configured state
367 */
368 err = usbd_set_config_index(sc->sc_udev, 0, 1);
369 if (err) {
370 aprint_error_dev(self, "failed to set configuration idx 0: %s\n",
371 usbd_errstr(err));
372
373 return;
374 }
375
376 /*
377 * Interface 0 must have 3 endpoints
378 * 1) Interrupt endpoint to receive HCI events
379 * 2) Bulk IN endpoint to receive ACL data
380 * 3) Bulk OUT endpoint to send ACL data
381 */
382 err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
383 if (err) {
384 aprint_error_dev(self, "Could not get interface 0 handle %s (%d)\n",
385 usbd_errstr(err), err);
386
387 return;
388 }
389
390 sc->sc_evt_addr = -1;
391 sc->sc_aclrd_addr = -1;
392 sc->sc_aclwr_addr = -1;
393
394 count = 0;
395 (void)usbd_endpoint_count(sc->sc_iface0, &count);
396
397 for (i = 0 ; i < count ; i++) {
398 int dir, type;
399
400 ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
401 if (ed == NULL) {
402 aprint_error_dev(self,
403 "could not read endpoint descriptor %d\n", i);
404
405 return;
406 }
407
408 dir = UE_GET_DIR(ed->bEndpointAddress);
409 type = UE_GET_XFERTYPE(ed->bmAttributes);
410
411 if (dir == UE_DIR_IN && type == UE_INTERRUPT)
412 sc->sc_evt_addr = ed->bEndpointAddress;
413 else if (dir == UE_DIR_IN && type == UE_BULK)
414 sc->sc_aclrd_addr = ed->bEndpointAddress;
415 else if (dir == UE_DIR_OUT && type == UE_BULK)
416 sc->sc_aclwr_addr = ed->bEndpointAddress;
417 }
418
419 if (sc->sc_evt_addr == -1) {
420 aprint_error_dev(self,
421 "missing INTERRUPT endpoint on interface 0\n");
422
423 return;
424 }
425 if (sc->sc_aclrd_addr == -1) {
426 aprint_error_dev(self,
427 "missing BULK IN endpoint on interface 0\n");
428
429 return;
430 }
431 if (sc->sc_aclwr_addr == -1) {
432 aprint_error_dev(self,
433 "missing BULK OUT endpoint on interface 0\n");
434
435 return;
436 }
437
438 /*
439 * Interface 1 must have 2 endpoints
440 * 1) Isochronous IN endpoint to receive SCO data
441 * 2) Isochronous OUT endpoint to send SCO data
442 *
443 * and will have several configurations, which can be selected
444 * via a sysctl variable. We select config 0 to start, which
445 * means that no SCO data will be available.
446 */
447 err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
448 if (err) {
449 aprint_error_dev(self,
450 "Could not get interface 1 handle %s (%d)\n",
451 usbd_errstr(err), err);
452
453 return;
454 }
455
456 cd = usbd_get_config_descriptor(sc->sc_udev);
457 if (cd == NULL) {
458 aprint_error_dev(self, "could not get config descriptor\n");
459
460 return;
461 }
462
463 sc->sc_alt_config = usbd_get_no_alts(cd, 1);
464
465 /* set initial config */
466 err = ubt_set_isoc_config(sc);
467 if (err) {
468 aprint_error_dev(self, "ISOC config failed\n");
469
470 return;
471 }
472
473 /* Attach HCI */
474 sc->sc_unit = hci_attach(&ubt_hci, sc->sc_dev, 0);
475
476 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
477 sc->sc_dev);
478
479 /* sysctl set-up for alternate configs */
480 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
481 CTLFLAG_PERMANENT,
482 CTLTYPE_NODE, "hw",
483 NULL,
484 NULL, 0,
485 NULL, 0,
486 CTL_HW, CTL_EOL);
487
488 sysctl_createv(&sc->sc_log, 0, NULL, &node,
489 0,
490 CTLTYPE_NODE, device_xname(sc->sc_dev),
491 SYSCTL_DESCR("ubt driver information"),
492 NULL, 0,
493 NULL, 0,
494 CTL_HW,
495 CTL_CREATE, CTL_EOL);
496
497 if (node != NULL) {
498 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
499 CTLFLAG_READWRITE,
500 CTLTYPE_INT, "config",
501 SYSCTL_DESCR("configuration number"),
502 ubt_sysctl_config, 0,
503 sc, 0,
504 CTL_HW, node->sysctl_num,
505 CTL_CREATE, CTL_EOL);
506
507 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
508 CTLFLAG_READONLY,
509 CTLTYPE_INT, "alt_config",
510 SYSCTL_DESCR("number of alternate configurations"),
511 NULL, 0,
512 &sc->sc_alt_config, sizeof(sc->sc_alt_config),
513 CTL_HW, node->sysctl_num,
514 CTL_CREATE, CTL_EOL);
515
516 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
517 CTLFLAG_READONLY,
518 CTLTYPE_INT, "sco_rxsize",
519 SYSCTL_DESCR("max SCO receive size"),
520 NULL, 0,
521 &sc->sc_scord_size, sizeof(sc->sc_scord_size),
522 CTL_HW, node->sysctl_num,
523 CTL_CREATE, CTL_EOL);
524
525 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
526 CTLFLAG_READONLY,
527 CTLTYPE_INT, "sco_txsize",
528 SYSCTL_DESCR("max SCO transmit size"),
529 NULL, 0,
530 &sc->sc_scowr_size, sizeof(sc->sc_scowr_size),
531 CTL_HW, node->sysctl_num,
532 CTL_CREATE, CTL_EOL);
533 }
534
535 sc->sc_ok = 1;
536 if (!pmf_device_register(self, NULL, NULL))
537 aprint_error_dev(self, "couldn't establish power handler\n");
538 return;
539 }
540
541 int
542 ubt_detach(device_t self, int flags)
543 {
544 struct ubt_softc *sc = device_private(self);
545 int s;
546
547 DPRINTF("sc=%p flags=%d\n", sc, flags);
548
549 if (device_pmf_is_registered(self))
550 pmf_device_deregister(self);
551
552 sc->sc_dying = 1;
553
554 if (!sc->sc_ok)
555 return 0;
556
557 /* delete sysctl nodes */
558 sysctl_teardown(&sc->sc_log);
559
560 /* Detach HCI interface */
561 if (sc->sc_unit) {
562 hci_detach(sc->sc_unit);
563 sc->sc_unit = NULL;
564 }
565
566 /*
567 * Abort all pipes. Causes processes waiting for transfer to wake.
568 *
569 * Actually, hci_detach() above will call ubt_disable() which may
570 * call ubt_abortdealloc(), but lets be sure since doing it twice
571 * wont cause an error.
572 */
573 ubt_abortdealloc(sc);
574
575 /* wait for all processes to finish */
576 s = splusb();
577 if (sc->sc_refcnt-- > 0)
578 usb_detach_wait(sc->sc_dev);
579
580 splx(s);
581
582 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
583 sc->sc_dev);
584
585 DPRINTFN(1, "driver detached\n");
586
587 return 0;
588 }
589
590 int
591 ubt_activate(device_t self, enum devact act)
592 {
593 struct ubt_softc *sc = device_private(self);
594
595 DPRINTFN(1, "sc=%p, act=%d\n", sc, act);
596
597 switch (act) {
598 case DVACT_DEACTIVATE:
599 sc->sc_dying = 1;
600 return 0;
601 default:
602 return EOPNOTSUPP;
603 }
604 }
605
606 /* set ISOC configuration */
607 static int
608 ubt_set_isoc_config(struct ubt_softc *sc)
609 {
610 usb_endpoint_descriptor_t *ed;
611 int rd_addr, wr_addr, rd_size, wr_size;
612 uint8_t count, i;
613 int err;
614
615 err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
616 if (err != USBD_NORMAL_COMPLETION) {
617 aprint_error_dev(sc->sc_dev,
618 "Could not set config %d on ISOC interface. %s (%d)\n",
619 sc->sc_config, usbd_errstr(err), err);
620
621 return err == USBD_IN_USE ? EBUSY : EIO;
622 }
623
624 /*
625 * We wont get past the above if there are any pipes open, so no
626 * need to worry about buf/xfer/pipe deallocation. If we get an
627 * error after this, the frame quantities will be 0 and no SCO
628 * data will be possible.
629 */
630
631 sc->sc_scord_size = rd_size = 0;
632 sc->sc_scord_addr = rd_addr = -1;
633
634 sc->sc_scowr_size = wr_size = 0;
635 sc->sc_scowr_addr = wr_addr = -1;
636
637 count = 0;
638 (void)usbd_endpoint_count(sc->sc_iface1, &count);
639
640 for (i = 0 ; i < count ; i++) {
641 ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
642 if (ed == NULL) {
643 aprint_error_dev(sc->sc_dev,
644 "could not read endpoint descriptor %d\n", i);
645
646 return EIO;
647 }
648
649 DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
650 device_xname(sc->sc_dev),
651 UE_GET_XFERTYPE(ed->bmAttributes),
652 UE_GET_ISO_TYPE(ed->bmAttributes),
653 ed->bEndpointAddress,
654 UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");
655
656 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
657 continue;
658
659 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
660 rd_addr = ed->bEndpointAddress;
661 rd_size = UGETW(ed->wMaxPacketSize);
662 } else {
663 wr_addr = ed->bEndpointAddress;
664 wr_size = UGETW(ed->wMaxPacketSize);
665 }
666 }
667
668 if (rd_addr == -1) {
669 aprint_error_dev(sc->sc_dev,
670 "missing ISOC IN endpoint on interface config %d\n",
671 sc->sc_config);
672
673 return ENOENT;
674 }
675 if (wr_addr == -1) {
676 aprint_error_dev(sc->sc_dev,
677 "missing ISOC OUT endpoint on interface config %d\n",
678 sc->sc_config);
679
680 return ENOENT;
681 }
682
683 #ifdef DIAGNOSTIC
684 if (rd_size > MLEN) {
685 aprint_error_dev(sc->sc_dev, "rd_size=%d exceeds MLEN\n",
686 rd_size);
687
688 return EOVERFLOW;
689 }
690
691 if (wr_size > MLEN) {
692 aprint_error_dev(sc->sc_dev, "wr_size=%d exceeds MLEN\n",
693 wr_size);
694
695 return EOVERFLOW;
696 }
697 #endif
698
699 sc->sc_scord_size = rd_size;
700 sc->sc_scord_addr = rd_addr;
701
702 sc->sc_scowr_size = wr_size;
703 sc->sc_scowr_addr = wr_addr;
704
705 return 0;
706 }
707
708 /* sysctl helper to set alternate configurations */
709 static int
710 ubt_sysctl_config(SYSCTLFN_ARGS)
711 {
712 struct sysctlnode node;
713 struct ubt_softc *sc;
714 int t, error;
715
716 node = *rnode;
717 sc = node.sysctl_data;
718
719 t = sc->sc_config;
720 node.sysctl_data = &t;
721 error = sysctl_lookup(SYSCTLFN_CALL(&node));
722 if (error || newp == NULL)
723 return error;
724
725 if (t < 0 || t >= sc->sc_alt_config)
726 return EINVAL;
727
728 /* This may not change when the unit is enabled */
729 if (sc->sc_enabled)
730 return EBUSY;
731
732 sc->sc_config = t;
733 return ubt_set_isoc_config(sc);
734 }
735
736 static void
737 ubt_abortdealloc(struct ubt_softc *sc)
738 {
739 int i;
740
741 DPRINTFN(1, "sc=%p\n", sc);
742
743 /* Abort all pipes */
744 usbd_abort_default_pipe(sc->sc_udev);
745
746 if (sc->sc_evt_pipe != NULL) {
747 usbd_abort_pipe(sc->sc_evt_pipe);
748 usbd_close_pipe(sc->sc_evt_pipe);
749 sc->sc_evt_pipe = NULL;
750 }
751
752 if (sc->sc_aclrd_pipe != NULL) {
753 usbd_abort_pipe(sc->sc_aclrd_pipe);
754 usbd_close_pipe(sc->sc_aclrd_pipe);
755 sc->sc_aclrd_pipe = NULL;
756 }
757
758 if (sc->sc_aclwr_pipe != NULL) {
759 usbd_abort_pipe(sc->sc_aclwr_pipe);
760 usbd_close_pipe(sc->sc_aclwr_pipe);
761 sc->sc_aclwr_pipe = NULL;
762 }
763
764 if (sc->sc_scord_pipe != NULL) {
765 usbd_abort_pipe(sc->sc_scord_pipe);
766 usbd_close_pipe(sc->sc_scord_pipe);
767 sc->sc_scord_pipe = NULL;
768 }
769
770 if (sc->sc_scowr_pipe != NULL) {
771 usbd_abort_pipe(sc->sc_scowr_pipe);
772 usbd_close_pipe(sc->sc_scowr_pipe);
773 sc->sc_scowr_pipe = NULL;
774 }
775
776 /* Free event buffer */
777 if (sc->sc_evt_buf != NULL) {
778 free(sc->sc_evt_buf, M_USBDEV);
779 sc->sc_evt_buf = NULL;
780 }
781
782 /* Free all xfers and xfer buffers (implicit) */
783 if (sc->sc_cmd_xfer != NULL) {
784 usbd_free_xfer(sc->sc_cmd_xfer);
785 sc->sc_cmd_xfer = NULL;
786 sc->sc_cmd_buf = NULL;
787 }
788
789 if (sc->sc_aclrd_xfer != NULL) {
790 usbd_free_xfer(sc->sc_aclrd_xfer);
791 sc->sc_aclrd_xfer = NULL;
792 sc->sc_aclrd_buf = NULL;
793 }
794
795 if (sc->sc_aclwr_xfer != NULL) {
796 usbd_free_xfer(sc->sc_aclwr_xfer);
797 sc->sc_aclwr_xfer = NULL;
798 sc->sc_aclwr_buf = NULL;
799 }
800
801 for (i = 0 ; i < UBT_NXFERS ; i++) {
802 if (sc->sc_scord[i].xfer != NULL) {
803 usbd_free_xfer(sc->sc_scord[i].xfer);
804 sc->sc_scord[i].xfer = NULL;
805 sc->sc_scord[i].buf = NULL;
806 }
807
808 if (sc->sc_scowr[i].xfer != NULL) {
809 usbd_free_xfer(sc->sc_scowr[i].xfer);
810 sc->sc_scowr[i].xfer = NULL;
811 sc->sc_scowr[i].buf = NULL;
812 }
813 }
814
815 /* Free partial SCO packets */
816 if (sc->sc_scord_mbuf != NULL) {
817 m_freem(sc->sc_scord_mbuf);
818 sc->sc_scord_mbuf = NULL;
819 }
820
821 if (sc->sc_scowr_mbuf != NULL) {
822 m_freem(sc->sc_scowr_mbuf);
823 sc->sc_scowr_mbuf = NULL;
824 }
825
826 /* Empty mbuf queues */
827 MBUFQ_DRAIN(&sc->sc_cmd_queue);
828 MBUFQ_DRAIN(&sc->sc_aclwr_queue);
829 MBUFQ_DRAIN(&sc->sc_scowr_queue);
830 }
831
832 /*******************************************************************************
833 *
834 * Bluetooth Unit/USB callbacks
835 *
836 */
837 static int
838 ubt_enable(device_t self)
839 {
840 struct ubt_softc *sc = device_private(self);
841 usbd_status err;
842 int s, i, error;
843
844 DPRINTFN(1, "sc=%p\n", sc);
845
846 if (sc->sc_enabled)
847 return 0;
848
849 s = splusb();
850
851 /* Events */
852 sc->sc_evt_buf = malloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
853 if (sc->sc_evt_buf == NULL) {
854 error = ENOMEM;
855 goto bad;
856 }
857 err = usbd_open_pipe_intr(sc->sc_iface0,
858 sc->sc_evt_addr,
859 USBD_SHORT_XFER_OK,
860 &sc->sc_evt_pipe,
861 sc,
862 sc->sc_evt_buf,
863 UBT_BUFSIZ_EVENT,
864 ubt_recv_event,
865 USBD_DEFAULT_INTERVAL);
866 if (err != USBD_NORMAL_COMPLETION) {
867 error = EIO;
868 goto bad;
869 }
870
871 /* Commands */
872 sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
873 if (sc->sc_cmd_xfer == NULL) {
874 error = ENOMEM;
875 goto bad;
876 }
877 sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
878 if (sc->sc_cmd_buf == NULL) {
879 error = ENOMEM;
880 goto bad;
881 }
882 sc->sc_cmd_busy = 0;
883
884 /* ACL read */
885 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
886 USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
887 if (err != USBD_NORMAL_COMPLETION) {
888 error = EIO;
889 goto bad;
890 }
891 sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
892 if (sc->sc_aclrd_xfer == NULL) {
893 error = ENOMEM;
894 goto bad;
895 }
896 sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
897 if (sc->sc_aclrd_buf == NULL) {
898 error = ENOMEM;
899 goto bad;
900 }
901 sc->sc_aclrd_busy = 0;
902 ubt_recv_acl_start(sc);
903
904 /* ACL write */
905 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
906 USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
907 if (err != USBD_NORMAL_COMPLETION) {
908 error = EIO;
909 goto bad;
910 }
911 sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
912 if (sc->sc_aclwr_xfer == NULL) {
913 error = ENOMEM;
914 goto bad;
915 }
916 sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
917 if (sc->sc_aclwr_buf == NULL) {
918 error = ENOMEM;
919 goto bad;
920 }
921 sc->sc_aclwr_busy = 0;
922
923 /* SCO read */
924 if (sc->sc_scord_size > 0) {
925 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
926 USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
927 if (err != USBD_NORMAL_COMPLETION) {
928 error = EIO;
929 goto bad;
930 }
931
932 for (i = 0 ; i < UBT_NXFERS ; i++) {
933 sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
934 if (sc->sc_scord[i].xfer == NULL) {
935 error = ENOMEM;
936 goto bad;
937 }
938 sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
939 sc->sc_scord_size * UBT_NFRAMES);
940 if (sc->sc_scord[i].buf == NULL) {
941 error = ENOMEM;
942 goto bad;
943 }
944 sc->sc_scord[i].softc = sc;
945 sc->sc_scord[i].busy = 0;
946 ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
947 }
948 }
949
950 /* SCO write */
951 if (sc->sc_scowr_size > 0) {
952 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
953 USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
954 if (err != USBD_NORMAL_COMPLETION) {
955 error = EIO;
956 goto bad;
957 }
958
959 for (i = 0 ; i < UBT_NXFERS ; i++) {
960 sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
961 if (sc->sc_scowr[i].xfer == NULL) {
962 error = ENOMEM;
963 goto bad;
964 }
965 sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
966 sc->sc_scowr_size * UBT_NFRAMES);
967 if (sc->sc_scowr[i].buf == NULL) {
968 error = ENOMEM;
969 goto bad;
970 }
971 sc->sc_scowr[i].softc = sc;
972 sc->sc_scowr[i].busy = 0;
973 }
974
975 sc->sc_scowr_busy = 0;
976 }
977
978 sc->sc_enabled = 1;
979 splx(s);
980 return 0;
981
982 bad:
983 ubt_abortdealloc(sc);
984 splx(s);
985 return error;
986 }
987
988 static void
989 ubt_disable(device_t self)
990 {
991 struct ubt_softc *sc = device_private(self);
992 int s;
993
994 DPRINTFN(1, "sc=%p\n", sc);
995
996 if (sc->sc_enabled == 0)
997 return;
998
999 s = splusb();
1000 ubt_abortdealloc(sc);
1001
1002 sc->sc_enabled = 0;
1003 splx(s);
1004 }
1005
1006 static void
1007 ubt_xmit_cmd(device_t self, struct mbuf *m)
1008 {
1009 struct ubt_softc *sc = device_private(self);
1010 int s;
1011
1012 KASSERT(sc->sc_enabled);
1013
1014 s = splusb();
1015 MBUFQ_ENQUEUE(&sc->sc_cmd_queue, m);
1016
1017 if (sc->sc_cmd_busy == 0)
1018 ubt_xmit_cmd_start(sc);
1019
1020 splx(s);
1021 }
1022
1023 static void
1024 ubt_xmit_cmd_start(struct ubt_softc *sc)
1025 {
1026 usb_device_request_t req;
1027 usbd_status status;
1028 struct mbuf *m;
1029 int len;
1030
1031 if (sc->sc_dying)
1032 return;
1033
1034 if (MBUFQ_FIRST(&sc->sc_cmd_queue) == NULL)
1035 return;
1036
1037 MBUFQ_DEQUEUE(&sc->sc_cmd_queue, m);
1038 KASSERT(m != NULL);
1039
1040 DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
1041 device_xname(sc->sc_dev), m->m_pkthdr.len);
1042
1043 sc->sc_refcnt++;
1044 sc->sc_cmd_busy = 1;
1045
1046 len = m->m_pkthdr.len - 1;
1047 m_copydata(m, 1, len, sc->sc_cmd_buf);
1048 m_freem(m);
1049
1050 memset(&req, 0, sizeof(req));
1051 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1052 USETW(req.wLength, len);
1053
1054 usbd_setup_default_xfer(sc->sc_cmd_xfer,
1055 sc->sc_udev,
1056 sc,
1057 UBT_CMD_TIMEOUT,
1058 &req,
1059 sc->sc_cmd_buf,
1060 len,
1061 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1062 ubt_xmit_cmd_complete);
1063
1064 status = usbd_transfer(sc->sc_cmd_xfer);
1065
1066 KASSERT(status != USBD_NORMAL_COMPLETION);
1067
1068 if (status != USBD_IN_PROGRESS) {
1069 DPRINTF("usbd_transfer status=%s (%d)\n",
1070 usbd_errstr(status), status);
1071
1072 sc->sc_refcnt--;
1073 sc->sc_cmd_busy = 0;
1074 }
1075 }
1076
1077 static void
1078 ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
1079 usbd_private_handle h, usbd_status status)
1080 {
1081 struct ubt_softc *sc = h;
1082 uint32_t count;
1083
1084 DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
1085 device_xname(sc->sc_dev), usbd_errstr(status), status);
1086
1087 sc->sc_cmd_busy = 0;
1088
1089 if (--sc->sc_refcnt < 0) {
1090 DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
1091 usb_detach_wakeup(sc->sc_dev);
1092 return;
1093 }
1094
1095 if (sc->sc_dying) {
1096 DPRINTF("sc_dying\n");
1097 return;
1098 }
1099
1100 if (status != USBD_NORMAL_COMPLETION) {
1101 DPRINTF("status=%s (%d)\n",
1102 usbd_errstr(status), status);
1103
1104 sc->sc_stats.err_tx++;
1105 return;
1106 }
1107
1108 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1109 sc->sc_stats.cmd_tx++;
1110 sc->sc_stats.byte_tx += count;
1111
1112 ubt_xmit_cmd_start(sc);
1113 }
1114
1115 static void
1116 ubt_xmit_acl(device_t self, struct mbuf *m)
1117 {
1118 struct ubt_softc *sc = device_private(self);
1119 int s;
1120
1121 KASSERT(sc->sc_enabled);
1122
1123 s = splusb();
1124 MBUFQ_ENQUEUE(&sc->sc_aclwr_queue, m);
1125
1126 if (sc->sc_aclwr_busy == 0)
1127 ubt_xmit_acl_start(sc);
1128
1129 splx(s);
1130 }
1131
1132 static void
1133 ubt_xmit_acl_start(struct ubt_softc *sc)
1134 {
1135 struct mbuf *m;
1136 usbd_status status;
1137 int len;
1138
1139 if (sc->sc_dying)
1140 return;
1141
1142 if (MBUFQ_FIRST(&sc->sc_aclwr_queue) == NULL)
1143 return;
1144
1145 sc->sc_refcnt++;
1146 sc->sc_aclwr_busy = 1;
1147
1148 MBUFQ_DEQUEUE(&sc->sc_aclwr_queue, m);
1149 KASSERT(m != NULL);
1150
1151 DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
1152 device_xname(sc->sc_dev), m->m_pkthdr.len);
1153
1154 len = m->m_pkthdr.len - 1;
1155 if (len > UBT_BUFSIZ_ACL) {
1156 DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
1157 device_xname(sc->sc_dev), len, UBT_BUFSIZ_ACL);
1158
1159 len = UBT_BUFSIZ_ACL;
1160 }
1161
1162 m_copydata(m, 1, len, sc->sc_aclwr_buf);
1163 m_freem(m);
1164
1165 sc->sc_stats.acl_tx++;
1166 sc->sc_stats.byte_tx += len;
1167
1168 usbd_setup_xfer(sc->sc_aclwr_xfer,
1169 sc->sc_aclwr_pipe,
1170 sc,
1171 sc->sc_aclwr_buf,
1172 len,
1173 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1174 UBT_ACL_TIMEOUT,
1175 ubt_xmit_acl_complete);
1176
1177 status = usbd_transfer(sc->sc_aclwr_xfer);
1178
1179 KASSERT(status != USBD_NORMAL_COMPLETION);
1180
1181 if (status != USBD_IN_PROGRESS) {
1182 DPRINTF("usbd_transfer status=%s (%d)\n",
1183 usbd_errstr(status), status);
1184
1185 sc->sc_refcnt--;
1186 sc->sc_aclwr_busy = 0;
1187 }
1188 }
1189
1190 static void
1191 ubt_xmit_acl_complete(usbd_xfer_handle xfer,
1192 usbd_private_handle h, usbd_status status)
1193 {
1194 struct ubt_softc *sc = h;
1195
1196 DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
1197 device_xname(sc->sc_dev), usbd_errstr(status), status);
1198
1199 sc->sc_aclwr_busy = 0;
1200
1201 if (--sc->sc_refcnt < 0) {
1202 usb_detach_wakeup(sc->sc_dev);
1203 return;
1204 }
1205
1206 if (sc->sc_dying)
1207 return;
1208
1209 if (status != USBD_NORMAL_COMPLETION) {
1210 DPRINTF("status=%s (%d)\n",
1211 usbd_errstr(status), status);
1212
1213 sc->sc_stats.err_tx++;
1214
1215 if (status == USBD_STALLED)
1216 usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
1217 else
1218 return;
1219 }
1220
1221 ubt_xmit_acl_start(sc);
1222 }
1223
1224 static void
1225 ubt_xmit_sco(device_t self, struct mbuf *m)
1226 {
1227 struct ubt_softc *sc = device_private(self);
1228 int s;
1229
1230 KASSERT(sc->sc_enabled);
1231
1232 s = splusb();
1233 MBUFQ_ENQUEUE(&sc->sc_scowr_queue, m);
1234
1235 if (sc->sc_scowr_busy == 0)
1236 ubt_xmit_sco_start(sc);
1237
1238 splx(s);
1239 }
1240
1241 static void
1242 ubt_xmit_sco_start(struct ubt_softc *sc)
1243 {
1244 int i;
1245
1246 if (sc->sc_dying || sc->sc_scowr_size == 0)
1247 return;
1248
1249 for (i = 0 ; i < UBT_NXFERS ; i++) {
1250 if (sc->sc_scowr[i].busy)
1251 continue;
1252
1253 ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
1254 }
1255 }
1256
1257 static void
1258 ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1259 {
1260 struct mbuf *m;
1261 uint8_t *buf;
1262 int num, len, size, space;
1263
1264 space = sc->sc_scowr_size * UBT_NFRAMES;
1265 buf = isoc->buf;
1266 len = 0;
1267
1268 /*
1269 * Fill the request buffer with data from the queue,
1270 * keeping any leftover packet on our private hook.
1271 *
1272 * Complete packets are passed back up to the stack
1273 * for disposal, since we can't rely on the controller
1274 * to tell us when it has finished with them.
1275 */
1276
1277 m = sc->sc_scowr_mbuf;
1278 while (space > 0) {
1279 if (m == NULL) {
1280 MBUFQ_DEQUEUE(&sc->sc_scowr_queue, m);
1281 if (m == NULL)
1282 break;
1283
1284 m_adj(m, 1); /* packet type */
1285 }
1286
1287 if (m->m_pkthdr.len > 0) {
1288 size = MIN(m->m_pkthdr.len, space);
1289
1290 m_copydata(m, 0, size, buf);
1291 m_adj(m, size);
1292
1293 buf += size;
1294 len += size;
1295 space -= size;
1296 }
1297
1298 if (m->m_pkthdr.len == 0) {
1299 sc->sc_stats.sco_tx++;
1300 if (!hci_complete_sco(sc->sc_unit, m))
1301 sc->sc_stats.err_tx++;
1302
1303 m = NULL;
1304 }
1305 }
1306 sc->sc_scowr_mbuf = m;
1307
1308 DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
1309
1310 if (len == 0) /* nothing to send */
1311 return;
1312
1313 sc->sc_refcnt++;
1314 sc->sc_scowr_busy = 1;
1315 sc->sc_stats.byte_tx += len;
1316 isoc->busy = 1;
1317
1318 /*
1319 * calculate number of isoc frames and sizes
1320 */
1321
1322 for (num = 0 ; len > 0 ; num++) {
1323 size = MIN(sc->sc_scowr_size, len);
1324
1325 isoc->size[num] = size;
1326 len -= size;
1327 }
1328
1329 usbd_setup_isoc_xfer(isoc->xfer,
1330 sc->sc_scowr_pipe,
1331 isoc,
1332 isoc->size,
1333 num,
1334 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1335 ubt_xmit_sco_complete);
1336
1337 usbd_transfer(isoc->xfer);
1338 }
1339
1340 static void
1341 ubt_xmit_sco_complete(usbd_xfer_handle xfer,
1342 usbd_private_handle h, usbd_status status)
1343 {
1344 struct ubt_isoc_xfer *isoc = h;
1345 struct ubt_softc *sc;
1346 int i;
1347
1348 KASSERT(xfer == isoc->xfer);
1349 sc = isoc->softc;
1350
1351 DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
1352 isoc, usbd_errstr(status), status);
1353
1354 isoc->busy = 0;
1355
1356 for (i = 0 ; ; i++) {
1357 if (i == UBT_NXFERS) {
1358 sc->sc_scowr_busy = 0;
1359 break;
1360 }
1361
1362 if (sc->sc_scowr[i].busy)
1363 break;
1364 }
1365
1366 if (--sc->sc_refcnt < 0) {
1367 usb_detach_wakeup(sc->sc_dev);
1368 return;
1369 }
1370
1371 if (sc->sc_dying)
1372 return;
1373
1374 if (status != USBD_NORMAL_COMPLETION) {
1375 DPRINTF("status=%s (%d)\n",
1376 usbd_errstr(status), status);
1377
1378 sc->sc_stats.err_tx++;
1379
1380 if (status == USBD_STALLED)
1381 usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
1382 else
1383 return;
1384 }
1385
1386 ubt_xmit_sco_start(sc);
1387 }
1388
1389 /*
1390 * load incoming data into an mbuf with
1391 * leading type byte
1392 */
1393 static struct mbuf *
1394 ubt_mbufload(uint8_t *buf, int count, uint8_t type)
1395 {
1396 struct mbuf *m;
1397
1398 MGETHDR(m, M_DONTWAIT, MT_DATA);
1399 if (m == NULL)
1400 return NULL;
1401
1402 *mtod(m, uint8_t *) = type;
1403 m->m_pkthdr.len = m->m_len = MHLEN;
1404 m_copyback(m, 1, count, buf); // (extends if necessary)
1405 if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
1406 m_free(m);
1407 return NULL;
1408 }
1409
1410 m->m_pkthdr.len = count + 1;
1411 m->m_len = MIN(MHLEN, m->m_pkthdr.len);
1412
1413 return m;
1414 }
1415
1416 static void
1417 ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
1418 {
1419 struct ubt_softc *sc = h;
1420 struct mbuf *m;
1421 uint32_t count;
1422 void *buf;
1423
1424 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1425 sc, usbd_errstr(status), status);
1426
1427 if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
1428 return;
1429
1430 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1431
1432 if (count < sizeof(hci_event_hdr_t) - 1) {
1433 DPRINTF("dumped undersized event (count = %d)\n", count);
1434 sc->sc_stats.err_rx++;
1435 return;
1436 }
1437
1438 sc->sc_stats.evt_rx++;
1439 sc->sc_stats.byte_rx += count;
1440
1441 m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
1442 if (m == NULL || !hci_input_event(sc->sc_unit, m))
1443 sc->sc_stats.err_rx++;
1444 }
1445
1446 static void
1447 ubt_recv_acl_start(struct ubt_softc *sc)
1448 {
1449 usbd_status status;
1450
1451 DPRINTFN(15, "sc=%p\n", sc);
1452
1453 if (sc->sc_aclrd_busy || sc->sc_dying) {
1454 DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
1455 sc->sc_aclrd_busy,
1456 sc->sc_dying);
1457
1458 return;
1459 }
1460
1461 sc->sc_refcnt++;
1462 sc->sc_aclrd_busy = 1;
1463
1464 usbd_setup_xfer(sc->sc_aclrd_xfer,
1465 sc->sc_aclrd_pipe,
1466 sc,
1467 sc->sc_aclrd_buf,
1468 UBT_BUFSIZ_ACL,
1469 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1470 USBD_NO_TIMEOUT,
1471 ubt_recv_acl_complete);
1472
1473 status = usbd_transfer(sc->sc_aclrd_xfer);
1474
1475 KASSERT(status != USBD_NORMAL_COMPLETION);
1476
1477 if (status != USBD_IN_PROGRESS) {
1478 DPRINTF("usbd_transfer status=%s (%d)\n",
1479 usbd_errstr(status), status);
1480
1481 sc->sc_refcnt--;
1482 sc->sc_aclrd_busy = 0;
1483 }
1484 }
1485
1486 static void
1487 ubt_recv_acl_complete(usbd_xfer_handle xfer,
1488 usbd_private_handle h, usbd_status status)
1489 {
1490 struct ubt_softc *sc = h;
1491 struct mbuf *m;
1492 uint32_t count;
1493 void *buf;
1494
1495 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1496 sc, usbd_errstr(status), status);
1497
1498 sc->sc_aclrd_busy = 0;
1499
1500 if (--sc->sc_refcnt < 0) {
1501 DPRINTF("refcnt = %d\n", sc->sc_refcnt);
1502 usb_detach_wakeup(sc->sc_dev);
1503 return;
1504 }
1505
1506 if (sc->sc_dying) {
1507 DPRINTF("sc_dying\n");
1508 return;
1509 }
1510
1511 if (status != USBD_NORMAL_COMPLETION) {
1512 DPRINTF("status=%s (%d)\n",
1513 usbd_errstr(status), status);
1514
1515 sc->sc_stats.err_rx++;
1516
1517 if (status == USBD_STALLED)
1518 usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
1519 else
1520 return;
1521 } else {
1522 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1523
1524 if (count < sizeof(hci_acldata_hdr_t) - 1) {
1525 DPRINTF("dumped undersized packet (%d)\n", count);
1526 sc->sc_stats.err_rx++;
1527 } else {
1528 sc->sc_stats.acl_rx++;
1529 sc->sc_stats.byte_rx += count;
1530
1531 m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
1532 if (m == NULL || !hci_input_acl(sc->sc_unit, m))
1533 sc->sc_stats.err_rx++;
1534 }
1535 }
1536
1537 /* and restart */
1538 ubt_recv_acl_start(sc);
1539 }
1540
1541 static void
1542 ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1543 {
1544 int i;
1545
1546 DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
1547
1548 if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
1549 DPRINTF("%s%s%s\n",
1550 isoc->busy ? " busy" : "",
1551 sc->sc_dying ? " dying" : "",
1552 sc->sc_scord_size == 0 ? " size=0" : "");
1553
1554 return;
1555 }
1556
1557 sc->sc_refcnt++;
1558 isoc->busy = 1;
1559
1560 for (i = 0 ; i < UBT_NFRAMES ; i++)
1561 isoc->size[i] = sc->sc_scord_size;
1562
1563 usbd_setup_isoc_xfer(isoc->xfer,
1564 sc->sc_scord_pipe,
1565 isoc,
1566 isoc->size,
1567 UBT_NFRAMES,
1568 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1569 ubt_recv_sco_complete);
1570
1571 usbd_transfer(isoc->xfer);
1572 }
1573
1574 static void
1575 ubt_recv_sco_complete(usbd_xfer_handle xfer,
1576 usbd_private_handle h, usbd_status status)
1577 {
1578 struct ubt_isoc_xfer *isoc = h;
1579 struct ubt_softc *sc;
1580 struct mbuf *m;
1581 uint32_t count;
1582 uint8_t *ptr, *frame;
1583 int i, size, got, want;
1584
1585 KASSERT(isoc != NULL);
1586 KASSERT(isoc->xfer == xfer);
1587
1588 sc = isoc->softc;
1589 isoc->busy = 0;
1590
1591 if (--sc->sc_refcnt < 0) {
1592 DPRINTF("refcnt=%d\n", sc->sc_refcnt);
1593 usb_detach_wakeup(sc->sc_dev);
1594 return;
1595 }
1596
1597 if (sc->sc_dying) {
1598 DPRINTF("sc_dying\n");
1599 return;
1600 }
1601
1602 if (status != USBD_NORMAL_COMPLETION) {
1603 DPRINTF("status=%s (%d)\n",
1604 usbd_errstr(status), status);
1605
1606 sc->sc_stats.err_rx++;
1607
1608 if (status == USBD_STALLED) {
1609 usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
1610 goto restart;
1611 }
1612
1613 return;
1614 }
1615
1616 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1617 if (count == 0)
1618 goto restart;
1619
1620 DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
1621 sc, isoc, count);
1622
1623 sc->sc_stats.byte_rx += count;
1624
1625 /*
1626 * Extract SCO packets from ISOC frames. The way we have it,
1627 * no SCO packet can be bigger than MHLEN. This is unlikely
1628 * to actually happen, but if we ran out of mbufs and lost
1629 * sync then we may get spurious data that makes it seem that
1630 * way, so we discard data that wont fit. This doesnt really
1631 * help with the lost sync situation alas.
1632 */
1633
1634 m = sc->sc_scord_mbuf;
1635 if (m != NULL) {
1636 sc->sc_scord_mbuf = NULL;
1637 ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
1638 got = m->m_pkthdr.len;
1639 want = sizeof(hci_scodata_hdr_t);
1640 if (got >= want)
1641 want += mtod(m, hci_scodata_hdr_t *)->length ;
1642 } else {
1643 ptr = NULL;
1644 got = 0;
1645 want = 0;
1646 }
1647
1648 for (i = 0 ; i < UBT_NFRAMES ; i++) {
1649 frame = isoc->buf + (i * sc->sc_scord_size);
1650
1651 while (isoc->size[i] > 0) {
1652 size = isoc->size[i];
1653
1654 if (m == NULL) {
1655 MGETHDR(m, M_DONTWAIT, MT_DATA);
1656 if (m == NULL) {
1657 aprint_error_dev(sc->sc_dev,
1658 "out of memory (xfer halted)\n");
1659
1660 sc->sc_stats.err_rx++;
1661 return; /* lost sync */
1662 }
1663
1664 ptr = mtod(m, uint8_t *);
1665 *ptr++ = HCI_SCO_DATA_PKT;
1666 got = 1;
1667 want = sizeof(hci_scodata_hdr_t);
1668 }
1669
1670 if (got + size > want)
1671 size = want - got;
1672
1673 if (got + size > MHLEN)
1674 memcpy(ptr, frame, MHLEN - got);
1675 else
1676 memcpy(ptr, frame, size);
1677
1678 ptr += size;
1679 got += size;
1680 frame += size;
1681
1682 if (got == want) {
1683 /*
1684 * If we only got a header, add the packet
1685 * length to our want count. Send complete
1686 * packets up to protocol stack.
1687 */
1688 if (want == sizeof(hci_scodata_hdr_t))
1689 want += mtod(m, hci_scodata_hdr_t *)->length;
1690
1691 if (got == want) {
1692 m->m_pkthdr.len = m->m_len = got;
1693 sc->sc_stats.sco_rx++;
1694 if (!hci_input_sco(sc->sc_unit, m))
1695 sc->sc_stats.err_rx++;
1696
1697 m = NULL;
1698 }
1699 }
1700
1701 isoc->size[i] -= size;
1702 }
1703 }
1704
1705 if (m != NULL) {
1706 m->m_pkthdr.len = m->m_len = got;
1707 sc->sc_scord_mbuf = m;
1708 }
1709
1710 restart: /* and restart */
1711 ubt_recv_sco_start1(sc, isoc);
1712 }
1713
1714 void
1715 ubt_stats(device_t self, struct bt_stats *dest, int flush)
1716 {
1717 struct ubt_softc *sc = device_private(self);
1718 int s;
1719
1720 s = splusb();
1721 memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
1722
1723 if (flush)
1724 memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
1725
1726 splx(s);
1727 }
1728