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