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