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