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