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