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