1 /* $OpenBSD: src/sys/dev/usb/ubt.c,v 1.11 2008/02/24 21:34:48 uwe Exp $ */
2 /* $NetBSD: ubt.c,v 1.30 2007/12/16 19:01:37 christos Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Iain Hibbert for Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
36 * All rights reserved.
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Lennart Augustsson (lennart@augustsson.net) and
40 * David Sainty (David.Sainty@dtsp.co.nz).
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
71 * This driver originally written by Lennart Augustsson and David Sainty,
72 * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
73 * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
78 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
82 #include <bus/usb/usb.h>
83 #include <bus/usb/usbdi.h>
84 #include <bus/usb/usbdi_util.h>
85 #include <bus/usb/usbdivar.h>
87 #include <netbt/bluetooth.h>
88 #include <netbt/hci.h>
90 /*******************************************************************************
98 int ubt_debug = UBT_DEBUG;
100 #define DPRINTF(fmt, args...) do { \
102 kprintf("%s: "fmt, __func__ , ##args); \
103 } while (/* CONSTCOND */0)
105 #define DPRINTFN(n, fmt, args...) do { \
106 if (ubt_debug > (n)) \
107 kprintf("%s: "fmt, __func__ , ##args); \
108 } while (/* CONSTCOND */0)
112 #define DPRINTFN(...)
115 /*******************************************************************************
117 * ubt softc structure
123 * NB: although ACL packets can extend to 65535 bytes, most devices
124 * have max_acl_size at much less (largest I have seen is 384)
126 #define UBT_BUFSIZ_CMD (HCI_CMD_PKT_SIZE - 1)
127 #define UBT_BUFSIZ_ACL (2048 - 1)
128 #define UBT_BUFSIZ_EVENT (HCI_EVENT_PKT_SIZE - 1)
130 /* Transmit timeouts */
132 #define UBT_CMD_TIMEOUT 100
133 #define UBT_ACL_TIMEOUT 100
136 * Specification says, that is must be 1ms, but it causes kernel panic.
137 * Setting interval to USBD_DEFAULT_INTERVAL is not working for some of
139 * XXX: Make it sysctl.
141 #define UBT_INTR_TIMEOUT USBD_DEFAULT_INTERVAL
146 * xfer buffer size depends on the frame size, and the number
147 * of frames per transfer is fixed, as each frame should be
148 * 1ms worth of data. This keeps the rate that xfers complete
149 * fairly constant. We use multiple xfers to keep the hardware
152 #define UBT_NXFERS 3 /* max xfers to queue */
153 #define UBT_NFRAMES 10 /* frames per xfer */
155 struct ubt_isoc_xfer {
156 struct ubt_softc *softc;
157 usbd_xfer_handle xfer;
159 uint16_t size[UBT_NFRAMES];
165 usbd_device_handle sc_udev;
170 /* Control Interface */
171 usbd_interface_handle sc_iface0;
173 /* Commands (control) */
174 usbd_xfer_handle sc_cmd_xfer;
176 int sc_cmd_busy; /* write active */
177 struct ifqueue sc_cmd_queue; /* output queue */
179 /* Events (interrupt) */
180 int sc_evt_addr; /* endpoint address */
181 usbd_pipe_handle sc_evt_pipe;
186 int sc_aclrd_addr; /* endpoint address */
187 usbd_pipe_handle sc_aclrd_pipe; /* read pipe */
188 usbd_xfer_handle sc_aclrd_xfer; /* read xfer */
189 u_char *sc_aclrd_buf; /* read buffer */
190 int sc_aclrd_busy; /* reading */
193 int sc_aclwr_addr; /* endpoint address */
194 usbd_pipe_handle sc_aclwr_pipe; /* write pipe */
195 usbd_xfer_handle sc_aclwr_xfer; /* write xfer */
196 u_char *sc_aclwr_buf; /* write buffer */
197 int sc_aclwr_busy; /* write active */
198 struct ifqueue sc_aclwr_queue;/* output queue */
201 usbd_interface_handle sc_iface1; /* ISOC interface */
202 struct sysctllog *sc_log; /* sysctl log */
203 int sc_config; /* current config no */
204 int sc_alt_config; /* no of alternates */
207 int sc_scord_addr; /* endpoint address */
208 usbd_pipe_handle sc_scord_pipe; /* read pipe */
209 int sc_scord_size; /* frame length */
210 struct ubt_isoc_xfer sc_scord[UBT_NXFERS];
211 struct mbuf *sc_scord_mbuf; /* current packet */
214 int sc_scowr_addr; /* endpoint address */
215 usbd_pipe_handle sc_scowr_pipe; /* write pipe */
216 int sc_scowr_size; /* frame length */
217 struct ubt_isoc_xfer sc_scowr[UBT_NXFERS];
218 struct mbuf *sc_scowr_mbuf; /* current packet */
219 int sc_scowr_busy; /* write active */
220 struct ifqueue sc_scowr_queue;/* output queue */
222 /* Protocol structure */
223 struct hci_unit *sc_unit;
224 struct bt_stats sc_stats;
226 /* Successfully attached */
229 struct sysctl_ctx_list sysctl_ctx;
230 struct sysctl_oid *sysctl_tree;
234 * Bluetooth unit/USB callback routines
236 int ubt_enable(struct device *);
237 void ubt_disable(struct device *);
239 void ubt_xmit_cmd(struct device *, struct mbuf *);
240 void ubt_xmit_cmd_start(struct ubt_softc *);
241 void ubt_xmit_cmd_complete(usbd_xfer_handle,
242 usbd_private_handle, usbd_status);
244 void ubt_xmit_acl(struct device *, struct mbuf *);
245 void ubt_xmit_acl_start(struct ubt_softc *);
246 void ubt_xmit_acl_complete(usbd_xfer_handle,
247 usbd_private_handle, usbd_status);
249 void ubt_xmit_sco(struct device *, struct mbuf *);
250 void ubt_xmit_sco_start(struct ubt_softc *);
251 void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
252 void ubt_xmit_sco_complete(usbd_xfer_handle,
253 usbd_private_handle, usbd_status);
255 void ubt_recv_event(usbd_xfer_handle,
256 usbd_private_handle, usbd_status);
258 void ubt_recv_acl_start(struct ubt_softc *);
259 void ubt_recv_acl_complete(usbd_xfer_handle,
260 usbd_private_handle, usbd_status);
262 void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
263 void ubt_recv_sco_complete(usbd_xfer_handle,
264 usbd_private_handle, usbd_status);
266 void ubt_stats(struct device *, struct bt_stats *, int);
268 static device_probe_t ubt_match;
269 static device_attach_t ubt_attach;
270 static device_detach_t ubt_detach;
272 static devclass_t ubt_devclass;
274 static device_method_t ubt_methods[] = {
275 DEVMETHOD(device_probe, ubt_match),
276 DEVMETHOD(device_attach, ubt_attach),
277 DEVMETHOD(device_detach, ubt_detach),
282 static driver_t ubt_driver = {
285 sizeof(struct ubt_softc)
288 DRIVER_MODULE(ubt, uhub, ubt_driver, ubt_devclass, usbd_driver_load, NULL);
289 MODULE_DEPEND(ubt, netbt, 1, 1, 1);
291 MODULE_DEPEND(ubt, bthub, 1, 1, 1);
293 MODULE_DEPEND(ubt, usb, 1, 1, 1);
295 const struct hci_if ubt_hci = {
296 .enable = ubt_enable,
297 .disable = ubt_disable,
298 .output_cmd = ubt_xmit_cmd,
299 .output_acl = ubt_xmit_acl,
300 .output_sco = ubt_xmit_sco,
301 .get_stats = ubt_stats,
304 static int ubt_set_isoc_config(struct ubt_softc *);
305 static int ubt_sysctl_config(SYSCTL_HANDLER_ARGS);
306 static void ubt_abortdealloc(struct ubt_softc *);
309 * Match against the whole device, since we want to take
310 * both interfaces. If a device should be ignored then add
312 * { VendorID, ProductID }
314 * to the ubt_ignore list.
316 static const struct usb_devno ubt_ignore[] = {
317 { USB_DEVICE(0x0a5c, 0x2033) }, /* Broadcom BCM2033 */
318 { 0, 0 } /* end of list */
322 ubt_match(device_t self)
324 struct usb_attach_arg *uaa = device_get_ivars(self);
325 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
327 DPRINTFN(50, "ubt_match\n");
329 if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
332 if (dd->bDeviceClass == UDCLASS_WIRELESS
333 && dd->bDeviceSubClass == UDSUBCLASS_RF
334 && dd->bDeviceProtocol == UDPROTO_BLUETOOTH)
335 return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;
340 ubt_attach(device_t self)
342 struct ubt_softc *sc = device_get_softc(self);
343 struct usb_attach_arg *uaa = device_get_ivars(self);
345 usb_config_descriptor_t *cd;
346 usb_endpoint_descriptor_t *ed;
350 DPRINTFN(50, "ubt_attach: sc=%p\n", sc);
352 sc->sc_udev = uaa->device;
356 * Move the device into the configured state
358 err = usbd_set_config_index(sc->sc_udev, 0, 1);
360 kprintf("%s: failed to set configuration idx 0: %s\n",
361 device_get_nameunit(sc->sc_dev), usbd_errstr(err));
367 * Interface 0 must have 3 endpoints
368 * 1) Interrupt endpoint to receive HCI events
369 * 2) Bulk IN endpoint to receive ACL data
370 * 3) Bulk OUT endpoint to send ACL data
372 err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
374 kprintf("%s: Could not get interface 0 handle %s (%d)\n",
375 device_get_nameunit(sc->sc_dev), usbd_errstr(err), err);
380 sc->sc_evt_addr = -1;
381 sc->sc_aclrd_addr = -1;
382 sc->sc_aclwr_addr = -1;
385 (void)usbd_endpoint_count(sc->sc_iface0, &count);
387 for (i = 0 ; i < count ; i++) {
390 ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
392 kprintf("%s: could not read endpoint descriptor %d\n",
393 device_get_nameunit(sc->sc_dev), i);
398 dir = UE_GET_DIR(ed->bEndpointAddress);
399 type = UE_GET_XFERTYPE(ed->bmAttributes);
401 if (dir == UE_DIR_IN && type == UE_INTERRUPT)
402 sc->sc_evt_addr = ed->bEndpointAddress;
403 else if (dir == UE_DIR_IN && type == UE_BULK)
404 sc->sc_aclrd_addr = ed->bEndpointAddress;
405 else if (dir == UE_DIR_OUT && type == UE_BULK)
406 sc->sc_aclwr_addr = ed->bEndpointAddress;
409 if (sc->sc_evt_addr == -1) {
410 kprintf("%s: missing INTERRUPT endpoint on interface 0\n",
411 device_get_nameunit(sc->sc_dev));
415 if (sc->sc_aclrd_addr == -1) {
416 kprintf("%s: missing BULK IN endpoint on interface 0\n",
417 device_get_nameunit(sc->sc_dev));
421 if (sc->sc_aclwr_addr == -1) {
422 kprintf("%s: missing BULK OUT endpoint on interface 0\n",
423 device_get_nameunit(sc->sc_dev));
429 * Interface 1 must have 2 endpoints
430 * 1) Isochronous IN endpoint to receive SCO data
431 * 2) Isochronous OUT endpoint to send SCO data
433 * and will have several configurations, which can be selected
434 * via a sysctl variable. We select config 0 to start, which
435 * means that no SCO data will be available.
437 err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
439 kprintf("%s: Could not get interface 1 handle %s (%d)\n",
440 device_get_nameunit(sc->sc_dev), usbd_errstr(err), err);
445 cd = usbd_get_config_descriptor(sc->sc_udev);
447 kprintf("%s: could not get config descriptor\n",
448 device_get_nameunit(sc->sc_dev));
453 sc->sc_alt_config = usbd_get_no_alts(cd, 1);
455 /* set initial config */
456 err = ubt_set_isoc_config(sc);
458 kprintf("%s: ISOC config failed\n",
459 device_get_nameunit(sc->sc_dev));
465 sc->sc_unit = hci_attach(&ubt_hci, sc->sc_dev, 0);
467 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
472 sysctl_ctx_init(&sc->sysctl_ctx);
473 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
474 SYSCTL_STATIC_CHILDREN(_hw),
476 device_get_nameunit(sc->sc_dev),
479 if (sc->sysctl_tree == NULL) {
480 /* Failure isn't fatal */
481 device_printf(sc->sc_dev, "Unable to create sysctl tree\n");
485 SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
486 OID_AUTO, "config", CTLTYPE_INT|CTLFLAG_RW, (void *)sc,
487 0, ubt_sysctl_config, "I", "Configuration number");
488 SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
489 OID_AUTO, "alt_config", CTLFLAG_RD, &sc->sc_alt_config,
490 0, "Number of alternate configurations");
491 SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
492 OID_AUTO, "sco_rxsize", CTLFLAG_RD, &sc->sc_scord_size,
493 0, "Max SCO receive size");
494 SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
495 OID_AUTO, "sco_wrsize", CTLFLAG_RD, &sc->sc_scowr_size,
496 0, "Max SCO transmit size");
502 ubt_detach(device_t self)
505 struct ubt_softc *sc = device_get_softc(self);
507 DPRINTF("sc=%p \n", sc);
514 /* Detach HCI interface */
516 hci_detach(sc->sc_unit);
521 * Abort all pipes. Causes processes waiting for transfer to wake.
523 * Actually, hci_detach() above will call ubt_disable() which may
524 * call ubt_abortdealloc(), but lets be sure since doing it twice
525 * wont cause an error.
527 ubt_abortdealloc(sc);
529 /* wait for all processes to finish */
530 if (sc->sc_refcnt-- > 0)
531 usb_detach_wait(sc->sc_dev);
533 if (sc->sysctl_tree != NULL) {
534 sc->sysctl_tree = NULL;
535 sysctl_ctx_free(&sc->sysctl_ctx);
538 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
541 DPRINTFN(1, "driver detached\n");
546 /* set ISOC configuration */
548 ubt_set_isoc_config(struct ubt_softc *sc)
550 usb_endpoint_descriptor_t *ed;
551 int rd_addr, wr_addr, rd_size, wr_size;
555 err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
556 if (err != USBD_NORMAL_COMPLETION) {
558 "%s: Could not set config %d on ISOC interface. %s (%d)\n",
559 device_get_nameunit(sc->sc_dev), sc->sc_config, usbd_errstr(err), err);
561 return err == USBD_IN_USE ? EBUSY : EIO;
565 * We wont get past the above if there are any pipes open, so no
566 * need to worry about buf/xfer/pipe deallocation. If we get an
567 * error after this, the frame quantities will be 0 and no SCO
568 * data will be possible.
571 sc->sc_scord_size = rd_size = 0;
572 sc->sc_scord_addr = rd_addr = -1;
574 sc->sc_scowr_size = wr_size = 0;
575 sc->sc_scowr_addr = wr_addr = -1;
578 (void)usbd_endpoint_count(sc->sc_iface1, &count);
580 for (i = 0 ; i < count ; i++) {
581 ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
583 kprintf("%s: could not read endpoint descriptor %d\n",
584 device_get_nameunit(sc->sc_dev), i);
589 DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
590 device_get_nameunit(sc->sc_dev),
591 UE_GET_XFERTYPE(ed->bmAttributes),
592 UE_GET_ISO_TYPE(ed->bmAttributes),
593 ed->bEndpointAddress,
594 UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");
596 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
599 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
600 rd_addr = ed->bEndpointAddress;
601 rd_size = UGETW(ed->wMaxPacketSize);
603 wr_addr = ed->bEndpointAddress;
604 wr_size = UGETW(ed->wMaxPacketSize);
610 "%s: missing ISOC IN endpoint on interface config %d\n",
611 device_get_nameunit(sc->sc_dev), sc->sc_config);
617 "%s: missing ISOC OUT endpoint on interface config %d\n",
618 device_get_nameunit(sc->sc_dev), sc->sc_config);
624 if (rd_size > MLEN) {
625 kprintf("%s: rd_size=%d exceeds MLEN\n",
626 device_get_nameunit(sc->sc_dev), rd_size);
631 if (wr_size > MLEN) {
632 kprintf("%s: wr_size=%d exceeds MLEN\n",
633 device_get_nameunit(sc->sc_dev), wr_size);
639 sc->sc_scord_size = rd_size;
640 sc->sc_scord_addr = rd_addr;
642 sc->sc_scowr_size = wr_size;
643 sc->sc_scowr_addr = wr_addr;
649 ubt_sysctl_config(SYSCTL_HANDLER_ARGS)
651 struct ubt_softc *sc = (struct ubt_softc *)arg1;
655 error = sysctl_handle_int(oidp, &t, sizeof(t), req);
656 if (error || req->newptr == NULL)
659 if (t < 0 || t >= sc->sc_alt_config)
662 /* This may not change when the unit is enabled */
667 return ubt_set_isoc_config(sc);
671 ubt_abortdealloc(struct ubt_softc *sc)
675 DPRINTFN(1, "sc=%p\n", sc);
678 /* Abort all pipes */
679 if (sc->sc_evt_pipe != NULL) {
680 usbd_abort_pipe(sc->sc_evt_pipe);
681 usbd_close_pipe(sc->sc_evt_pipe);
682 sc->sc_evt_pipe = NULL;
685 if (sc->sc_aclrd_pipe != NULL) {
686 usbd_abort_pipe(sc->sc_aclrd_pipe);
687 usbd_close_pipe(sc->sc_aclrd_pipe);
688 sc->sc_aclrd_pipe = NULL;
691 if (sc->sc_aclwr_pipe != NULL) {
692 usbd_abort_pipe(sc->sc_aclwr_pipe);
693 usbd_close_pipe(sc->sc_aclwr_pipe);
694 sc->sc_aclwr_pipe = NULL;
697 if (sc->sc_scord_pipe != NULL) {
698 usbd_abort_pipe(sc->sc_scord_pipe);
699 usbd_close_pipe(sc->sc_scord_pipe);
700 sc->sc_scord_pipe = NULL;
703 if (sc->sc_scowr_pipe != NULL) {
704 usbd_abort_pipe(sc->sc_scowr_pipe);
705 usbd_close_pipe(sc->sc_scowr_pipe);
706 sc->sc_scowr_pipe = NULL;
709 /* Free event buffer */
710 if (sc->sc_evt_buf != NULL) {
711 kfree(sc->sc_evt_buf, M_USBDEV);
712 sc->sc_evt_buf = NULL;
715 /* Free all xfers and xfer buffers (implicit) */
716 if (sc->sc_cmd_xfer != NULL) {
717 usbd_free_xfer(sc->sc_cmd_xfer);
718 sc->sc_cmd_xfer = NULL;
719 sc->sc_cmd_buf = NULL;
722 if (sc->sc_aclrd_xfer != NULL) {
723 usbd_free_xfer(sc->sc_aclrd_xfer);
724 sc->sc_aclrd_xfer = NULL;
725 sc->sc_aclrd_buf = NULL;
728 if (sc->sc_aclwr_xfer != NULL) {
729 usbd_free_xfer(sc->sc_aclwr_xfer);
730 sc->sc_aclwr_xfer = NULL;
731 sc->sc_aclwr_buf = NULL;
734 for (i = 0 ; i < UBT_NXFERS ; i++) {
735 if (sc->sc_scord[i].xfer != NULL) {
736 usbd_free_xfer(sc->sc_scord[i].xfer);
737 sc->sc_scord[i].xfer = NULL;
738 sc->sc_scord[i].buf = NULL;
741 if (sc->sc_scowr[i].xfer != NULL) {
742 usbd_free_xfer(sc->sc_scowr[i].xfer);
743 sc->sc_scowr[i].xfer = NULL;
744 sc->sc_scowr[i].buf = NULL;
748 /* Free partial SCO packets */
749 if (sc->sc_scord_mbuf != NULL) {
750 m_freem(sc->sc_scord_mbuf);
751 sc->sc_scord_mbuf = NULL;
754 if (sc->sc_scowr_mbuf != NULL) {
755 m_freem(sc->sc_scowr_mbuf);
756 sc->sc_scowr_mbuf = NULL;
759 /* Empty mbuf queues */
760 IF_DRAIN(&sc->sc_cmd_queue);
761 IF_DRAIN(&sc->sc_aclwr_queue);
762 IF_DRAIN(&sc->sc_scowr_queue);
767 /*******************************************************************************
769 * Bluetooth Unit/USB callbacks
771 * All of this will be called at the IPL_ we specified above
774 ubt_enable(struct device *self)
776 struct ubt_softc *sc = device_get_softc(self);
780 DPRINTFN(1, "%s: sc=%p\n", __func__, sc);
788 sc->sc_evt_buf = kmalloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
789 if (sc->sc_evt_buf == NULL) {
794 err = usbd_open_pipe_intr(sc->sc_iface0,
803 if (err != USBD_NORMAL_COMPLETION) {
805 kprintf("can't open events pipe_intr\n");
810 sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
811 if (sc->sc_cmd_xfer == NULL) {
812 kprintf("can't allocate cmd_xfer\n");
816 sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
817 if (sc->sc_cmd_buf == NULL) {
818 kprintf("can't allocate cmd_buf\n");
825 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
826 USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
827 if (err != USBD_NORMAL_COMPLETION) {
828 kprintf("can't open aclrd pipe\n");
832 sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
833 if (sc->sc_aclrd_xfer == NULL) {
834 kprintf("can't allocate aclrd_xfer\n");
838 sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
839 if (sc->sc_aclrd_buf == NULL) {
840 kprintf("can't allocate aclrd_buf\n");
844 sc->sc_aclrd_busy = 0;
845 ubt_recv_acl_start(sc);
848 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
849 USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
850 if (err != USBD_NORMAL_COMPLETION) {
851 kprintf("can't open aclwr pipe\n");
855 sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
856 if (sc->sc_aclwr_xfer == NULL) {
857 kprintf("can't allocate aclwr_xfer\n");
861 sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
862 if (sc->sc_aclwr_buf == NULL) {
863 kprintf("can't allocate aclwr_buf\n");
867 sc->sc_aclwr_busy = 0;
870 if (sc->sc_scord_size > 0) {
871 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
872 USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
873 if (err != USBD_NORMAL_COMPLETION) {
878 for (i = 0 ; i < UBT_NXFERS ; i++) {
879 sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
880 if (sc->sc_scord[i].xfer == NULL) {
884 sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
885 sc->sc_scord_size * UBT_NFRAMES);
886 if (sc->sc_scord[i].buf == NULL) {
890 sc->sc_scord[i].softc = sc;
891 sc->sc_scord[i].busy = 0;
892 ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
897 if (sc->sc_scowr_size > 0) {
898 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
899 USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
900 if (err != USBD_NORMAL_COMPLETION) {
905 for (i = 0 ; i < UBT_NXFERS ; i++) {
906 sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
907 if (sc->sc_scowr[i].xfer == NULL) {
911 sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
912 sc->sc_scowr_size * UBT_NFRAMES);
913 if (sc->sc_scowr[i].buf == NULL) {
917 sc->sc_scowr[i].softc = sc;
918 sc->sc_scowr[i].busy = 0;
921 sc->sc_scowr_busy = 0;
929 ubt_abortdealloc(sc);
935 ubt_disable(struct device *self)
937 struct ubt_softc *sc = device_get_softc(self);
939 DPRINTFN(1, "sc=%p\n", sc);
941 if (sc->sc_enabled == 0)
944 ubt_abortdealloc(sc);
949 ubt_xmit_cmd(struct device *self, struct mbuf *m)
951 struct ubt_softc *sc = device_get_softc(self);
953 KKASSERT(sc->sc_enabled);
956 IF_ENQUEUE(&sc->sc_cmd_queue, m);
958 if (sc->sc_cmd_busy == 0)
959 ubt_xmit_cmd_start(sc);
965 ubt_xmit_cmd_start(struct ubt_softc *sc)
967 usb_device_request_t req;
975 if (IF_QEMPTY(&sc->sc_cmd_queue))
978 IF_DEQUEUE(&sc->sc_cmd_queue, m);
980 DPRINTFN(15, " %s: xmit CMD packet (%d bytes)\n",
981 device_get_nameunit(sc->sc_dev), m->m_pkthdr.len);
987 len = m->m_pkthdr.len - 1;
988 m_copydata(m, 1, len, sc->sc_cmd_buf);
991 memset(&req, 0, sizeof(req));
992 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
993 USETW(req.wLength, len);
995 usbd_setup_default_xfer(sc->sc_cmd_xfer,
1002 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1003 ubt_xmit_cmd_complete);
1005 status = usbd_transfer(sc->sc_cmd_xfer);
1007 KKASSERT(status != USBD_NORMAL_COMPLETION);
1009 if (status != USBD_IN_PROGRESS) {
1010 DPRINTF("usbd_transfer status=%s (%d)\n",
1011 usbd_errstr(status), status);
1013 sc->sc_cmd_busy = 0;
1019 ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
1020 usbd_private_handle h, usbd_status status)
1022 struct ubt_softc *sc = h;
1025 DPRINTFN(15, " %s: CMD complete status=%s (%d)\n",
1026 device_get_nameunit(sc->sc_dev), usbd_errstr(status), status);
1028 sc->sc_cmd_busy = 0;
1030 if (--sc->sc_refcnt < 0) {
1031 DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
1032 usb_detach_wakeup(sc->sc_dev);
1037 DPRINTF("sc_dying\n");
1041 if (status != USBD_NORMAL_COMPLETION) {
1042 DPRINTF("status=%s (%d)\n",
1043 usbd_errstr(status), status);
1044 sc->sc_stats.err_tx++;
1048 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1049 sc->sc_stats.cmd_tx++;
1050 sc->sc_stats.byte_tx += count;
1052 ubt_xmit_cmd_start(sc);
1056 ubt_xmit_acl(struct device *self, struct mbuf *m)
1058 struct ubt_softc *sc = device_get_softc(self);
1060 KKASSERT(sc->sc_enabled);
1063 IF_ENQUEUE(&sc->sc_aclwr_queue, m);
1065 if (sc->sc_aclwr_busy == 0)
1066 ubt_xmit_acl_start(sc);
1072 ubt_xmit_acl_start(struct ubt_softc *sc)
1082 if (IF_QEMPTY(&sc->sc_aclwr_queue))
1086 sc->sc_aclwr_busy = 1;
1088 IF_DEQUEUE(&sc->sc_aclwr_queue, m);
1089 KKASSERT(m != NULL);
1091 DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
1092 device_get_nameunit(sc->sc_dev), m->m_pkthdr.len);
1094 len = m->m_pkthdr.len - 1;
1095 if (len > UBT_BUFSIZ_ACL) {
1096 DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
1097 device_get_nameunit(sc->sc_dev), len, UBT_BUFSIZ_ACL);
1099 len = UBT_BUFSIZ_ACL;
1102 m_copydata(m, 1, len, sc->sc_aclwr_buf);
1105 sc->sc_stats.acl_tx++;
1106 sc->sc_stats.byte_tx += len;
1108 usbd_setup_xfer(sc->sc_aclwr_xfer,
1113 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1115 ubt_xmit_acl_complete);
1117 status = usbd_transfer(sc->sc_aclwr_xfer);
1119 KKASSERT(status != USBD_NORMAL_COMPLETION);
1121 if (status != USBD_IN_PROGRESS) {
1122 DPRINTF("usbd_transfer status=%s (%d)\n",
1123 usbd_errstr(status), status);
1126 sc->sc_aclwr_busy = 0;
1132 ubt_xmit_acl_complete(usbd_xfer_handle xfer,
1133 usbd_private_handle h, usbd_status status)
1135 struct ubt_softc *sc = h;
1137 DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
1138 device_get_nameunit(sc->sc_dev), usbd_errstr(status), status);
1140 sc->sc_aclwr_busy = 0;
1142 if (--sc->sc_refcnt < 0) {
1143 usb_detach_wakeup(sc->sc_dev);
1150 if (status != USBD_NORMAL_COMPLETION) {
1151 DPRINTF("status=%s (%d)\n",
1152 usbd_errstr(status), status);
1154 sc->sc_stats.err_tx++;
1156 if (status == USBD_STALLED)
1157 usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
1162 ubt_xmit_acl_start(sc);
1167 ubt_xmit_sco(struct device *self, struct mbuf *m)
1169 struct ubt_softc *sc = device_get_softc(self);
1171 KKASSERT(sc->sc_enabled);
1174 IF_ENQUEUE(&sc->sc_scowr_queue, m);
1176 if (sc->sc_scowr_busy == 0)
1177 ubt_xmit_sco_start(sc);
1183 ubt_xmit_sco_start(struct ubt_softc *sc)
1187 if (sc->sc_dying || sc->sc_scowr_size == 0)
1190 for (i = 0 ; i < UBT_NXFERS ; i++) {
1191 if (sc->sc_scowr[i].busy)
1194 ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
1199 ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1203 int num, len, size, space;
1208 space = sc->sc_scowr_size * UBT_NFRAMES;
1213 * Fill the request buffer with data from the queue,
1214 * keeping any leftover packet on our private hook.
1216 * Complete packets are passed back up to the stack
1217 * for disposal, since we can't rely on the controller
1218 * to tell us when it has finished with them.
1221 m = sc->sc_scowr_mbuf;
1225 IF_DEQUEUE(&sc->sc_scowr_queue, m);
1230 m_adj(m, 1); /* packet type */
1234 if (m->m_pkthdr.len > 0) {
1235 size = MIN(m->m_pkthdr.len, space);
1237 m_copydata(m, 0, size, buf);
1245 if (m->m_pkthdr.len == 0) {
1246 sc->sc_stats.sco_tx++;
1247 if (!hci_complete_sco(sc->sc_unit, m))
1248 sc->sc_stats.err_tx++;
1253 sc->sc_scowr_mbuf = m;
1255 DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
1257 if (len == 0) /* nothing to send */
1262 sc->sc_scowr_busy = 1;
1263 sc->sc_stats.byte_tx += len;
1267 * calculate number of isoc frames and sizes
1270 for (num = 0 ; len > 0 ; num++) {
1271 size = MIN(sc->sc_scowr_size, len);
1273 isoc->size[num] = size;
1277 usbd_setup_isoc_xfer(isoc->xfer,
1282 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1283 ubt_xmit_sco_complete);
1285 usbd_transfer(isoc->xfer);
1289 ubt_xmit_sco_complete(usbd_xfer_handle xfer,
1290 usbd_private_handle h, usbd_status status)
1292 struct ubt_isoc_xfer *isoc = h;
1293 struct ubt_softc *sc;
1296 KKASSERT(xfer == isoc->xfer);
1299 DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
1300 isoc, usbd_errstr(status), status);
1304 for (i = 0 ; ; i++) {
1305 if (i == UBT_NXFERS) {
1306 sc->sc_scowr_busy = 0;
1310 if (sc->sc_scowr[i].busy)
1314 if (--sc->sc_refcnt < 0) {
1315 usb_detach_wakeup(sc->sc_dev);
1322 if (status != USBD_NORMAL_COMPLETION) {
1323 DPRINTF("status=%s (%d)\n",
1324 usbd_errstr(status), status);
1326 sc->sc_stats.err_tx++;
1328 if (status == USBD_STALLED)
1329 usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
1334 ubt_xmit_sco_start(sc);
1338 * Load incoming data into an mbuf with leading type byte.
1340 static struct mbuf *
1341 ubt_mbufload(uint8_t *buf, int count, uint8_t type)
1344 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1345 if (m == NULL) {kprintf(" MGETHDR return NULL\n");
1347 *mtod(m, uint8_t *) = type;
1348 m->m_pkthdr.len = m->m_len = MHLEN;
1349 m_copyback(m, 1, count, buf); /* (extends if necessary)*/
1350 if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
1352 kprintf(" m->m_pkthdr.len != MAX() \n");
1355 m->m_pkthdr.len = count + 1;
1356 m->m_len = MIN(MHLEN, m->m_pkthdr.len);
1361 ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
1363 struct ubt_softc *sc = h;
1368 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1369 sc, usbd_errstr(status), status);
1371 if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
1374 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1376 if (count < sizeof(hci_event_hdr_t) - 1) {
1377 DPRINTF("dumped undersized event (count = %d)\n", count);
1378 sc->sc_stats.err_rx++;
1382 sc->sc_stats.evt_rx++;
1383 sc->sc_stats.byte_rx += count;
1385 m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
1386 if (m == NULL || !hci_input_event(sc->sc_unit, m))
1387 sc->sc_stats.err_rx++;
1391 ubt_recv_acl_start(struct ubt_softc *sc)
1395 DPRINTFN(15, "sc=%p\n", sc);
1397 if (sc->sc_aclrd_busy || sc->sc_dying) {
1398 DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
1406 sc->sc_aclrd_busy = 1;
1408 usbd_setup_xfer(sc->sc_aclrd_xfer,
1413 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1415 ubt_recv_acl_complete);
1417 status = usbd_transfer(sc->sc_aclrd_xfer);
1419 KKASSERT(status != USBD_NORMAL_COMPLETION);
1421 if (status != USBD_IN_PROGRESS) {
1422 DPRINTF("usbd_transfer status=%s (%d)\n",
1423 usbd_errstr(status), status);
1426 sc->sc_aclrd_busy = 0;
1431 ubt_recv_acl_complete(usbd_xfer_handle xfer,
1432 usbd_private_handle h, usbd_status status)
1434 struct ubt_softc *sc = h;
1439 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1440 sc, usbd_errstr(status), status);
1442 sc->sc_aclrd_busy = 0;
1444 if (--sc->sc_refcnt < 0) {
1445 DPRINTF("refcnt = %d\n", sc->sc_refcnt);
1446 usb_detach_wakeup(sc->sc_dev);
1451 DPRINTF("sc_dying\n");
1455 if (status != USBD_NORMAL_COMPLETION) {
1456 DPRINTF("status=%s (%d)\n",
1457 usbd_errstr(status), status);
1459 sc->sc_stats.err_rx++;
1461 if (status == USBD_STALLED)
1462 usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
1466 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1468 if (count < sizeof(hci_acldata_hdr_t) - 1) {
1469 DPRINTF("dumped undersized packet (%d)\n", count);
1470 sc->sc_stats.err_rx++;
1472 sc->sc_stats.acl_rx++;
1473 sc->sc_stats.byte_rx += count;
1475 m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
1476 if (m == NULL || !hci_input_acl(sc->sc_unit, m))
1477 sc->sc_stats.err_rx++;
1482 ubt_recv_acl_start(sc);
1486 ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1490 DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
1492 if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
1494 isoc->busy ? " busy" : "",
1495 sc->sc_dying ? " dying" : "",
1496 sc->sc_scord_size == 0 ? " size=0" : "");
1504 for (i = 0 ; i < UBT_NFRAMES ; i++)
1505 isoc->size[i] = sc->sc_scord_size;
1507 usbd_setup_isoc_xfer(isoc->xfer,
1512 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1513 ubt_recv_sco_complete);
1515 usbd_transfer(isoc->xfer);
1519 ubt_recv_sco_complete(usbd_xfer_handle xfer,
1520 usbd_private_handle h, usbd_status status)
1522 struct ubt_isoc_xfer *isoc = h;
1523 struct ubt_softc *sc;
1526 uint8_t *ptr, *frame;
1527 int i, size, got, want;
1529 KKASSERT(isoc != NULL);
1530 KKASSERT(isoc->xfer == xfer);
1535 if (--sc->sc_refcnt < 0) {
1536 DPRINTF("refcnt=%d\n", sc->sc_refcnt);
1537 usb_detach_wakeup(sc->sc_dev);
1542 DPRINTF("sc_dying\n");
1546 if (status != USBD_NORMAL_COMPLETION) {
1547 DPRINTF("status=%s (%d)\n",
1548 usbd_errstr(status), status);
1550 sc->sc_stats.err_rx++;
1552 if (status == USBD_STALLED) {
1553 usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
1560 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1564 DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
1567 sc->sc_stats.byte_rx += count;
1570 * Extract SCO packets from ISOC frames. The way we have it,
1571 * no SCO packet can be bigger than MHLEN. This is unlikely
1572 * to actually happen, but if we ran out of mbufs and lost
1573 * sync then we may get spurious data that makes it seem that
1574 * way, so we discard data that wont fit. This doesnt really
1575 * help with the lost sync situation alas.
1578 m = sc->sc_scord_mbuf;
1580 sc->sc_scord_mbuf = NULL;
1581 ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
1582 got = m->m_pkthdr.len;
1583 want = sizeof(hci_scodata_hdr_t);
1585 want += mtod(m, hci_scodata_hdr_t *)->length ;
1592 for (i = 0 ; i < UBT_NFRAMES ; i++) {
1593 frame = isoc->buf + (i * sc->sc_scord_size);
1595 while (isoc->size[i] > 0) {
1596 size = isoc->size[i];
1599 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1601 kprintf("%s: out of memory (xfer halted)\n",
1602 device_get_nameunit(sc->sc_dev));
1604 sc->sc_stats.err_rx++;
1605 return; /* lost sync */
1608 ptr = mtod(m, uint8_t *);
1609 *ptr++ = HCI_SCO_DATA_PKT;
1611 want = sizeof(hci_scodata_hdr_t);
1614 if (got + size > want)
1617 if (got + size > MHLEN)
1618 memcpy(ptr, frame, MHLEN - got);
1620 memcpy(ptr, frame, size);
1628 * If we only got a header, add the packet
1629 * length to our want count. Send complete
1630 * packets up to protocol stack.
1632 if (want == sizeof(hci_scodata_hdr_t))
1633 want += mtod(m, hci_scodata_hdr_t *)->length;
1636 m->m_pkthdr.len = m->m_len = got;
1637 sc->sc_stats.sco_rx++;
1638 if (!hci_input_sco(sc->sc_unit, m))
1639 sc->sc_stats.err_rx++;
1645 isoc->size[i] -= size;
1650 m->m_pkthdr.len = m->m_len = got;
1651 sc->sc_scord_mbuf = m;
1654 restart: /* and restart */
1655 ubt_recv_sco_start1(sc, isoc);
1659 ubt_stats(struct device *self, struct bt_stats *dest, int flush)
1661 struct ubt_softc *sc = device_get_softc(self);
1664 memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
1667 memset(&sc->sc_stats, 0, sizeof(struct bt_stats));