Home | History | Annotate | Line # | Download | only in usb
ubt.c revision 1.37
      1 /*	$NetBSD: ubt.c,v 1.37 2009/09/23 19:07:19 plunky Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Iain Hibbert for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
     35  * All rights reserved.
     36  *
     37  * This code is derived from software contributed to The NetBSD Foundation
     38  * by Lennart Augustsson (lennart (at) augustsson.net) and
     39  * David Sainty (David.Sainty (at) dtsp.co.nz).
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  * POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 /*
     63  * This driver originally written by Lennart Augustsson and David Sainty,
     64  * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
     65  * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
     66  * reference.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.37 2009/09/23 19:07:19 plunky Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/device.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/kernel.h>
     76 #include <sys/malloc.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/proc.h>
     79 #include <sys/sysctl.h>
     80 #include <sys/systm.h>
     81 
     82 #include <dev/usb/usb.h>
     83 #include <dev/usb/usbdi.h>
     84 #include <dev/usb/usbdi_util.h>
     85 #include <dev/usb/usbdevs.h>
     86 
     87 #include <netbt/bluetooth.h>
     88 #include <netbt/hci.h>
     89 
     90 /*******************************************************************************
     91  *
     92  *	debugging stuff
     93  */
     94 #undef DPRINTF
     95 #undef DPRINTFN
     96 
     97 #ifdef UBT_DEBUG
     98 int	ubt_debug = 0;
     99 
    100 #define DPRINTF(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 };
    309 
    310 USB_MATCH(ubt)
    311 {
    312 	USB_MATCH_START(ubt, uaa);
    313 
    314 	DPRINTFN(50, "ubt_match\n");
    315 
    316 	if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
    317 		return UMATCH_NONE;
    318 
    319 	if (uaa->class == UDCLASS_WIRELESS
    320 	    && uaa->subclass == UDSUBCLASS_RF
    321 	    && uaa->proto == UDPROTO_BLUETOOTH)
    322 		return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;
    323 
    324 	return UMATCH_NONE;
    325 }
    326 
    327 USB_ATTACH(ubt)
    328 {
    329 	USB_ATTACH_START(ubt, sc, uaa);
    330 	usb_config_descriptor_t *cd;
    331 	usb_endpoint_descriptor_t *ed;
    332 	const struct sysctlnode *node;
    333 	char *devinfop;
    334 	int err;
    335 	uint8_t count, i;
    336 
    337 	DPRINTFN(50, "ubt_attach: sc=%p\n", sc);
    338 
    339 	sc->sc_dev = self;
    340 	sc->sc_udev = uaa->device;
    341 
    342 	MBUFQ_INIT(&sc->sc_cmd_queue);
    343 	MBUFQ_INIT(&sc->sc_aclwr_queue);
    344 	MBUFQ_INIT(&sc->sc_scowr_queue);
    345 
    346 	aprint_naive("\n");
    347 	aprint_normal("\n");
    348 
    349 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    350 	aprint_normal_dev(self, "%s\n", devinfop);
    351 	usbd_devinfo_free(devinfop);
    352 
    353 	/*
    354 	 * Move the device into the configured state
    355 	 */
    356 	err = usbd_set_config_index(sc->sc_udev, 0, 1);
    357 	if (err) {
    358 		aprint_error_dev(self, "failed to set configuration idx 0: %s\n",
    359 		    usbd_errstr(err));
    360 
    361 		USB_ATTACH_ERROR_RETURN;
    362 	}
    363 
    364 	/*
    365 	 * Interface 0 must have 3 endpoints
    366 	 *	1) Interrupt endpoint to receive HCI events
    367 	 *	2) Bulk IN endpoint to receive ACL data
    368 	 *	3) Bulk OUT endpoint to send ACL data
    369 	 */
    370 	err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
    371 	if (err) {
    372 		aprint_error_dev(self, "Could not get interface 0 handle %s (%d)\n",
    373 				usbd_errstr(err), err);
    374 
    375 		USB_ATTACH_ERROR_RETURN;
    376 	}
    377 
    378 	sc->sc_evt_addr = -1;
    379 	sc->sc_aclrd_addr = -1;
    380 	sc->sc_aclwr_addr = -1;
    381 
    382 	count = 0;
    383 	(void)usbd_endpoint_count(sc->sc_iface0, &count);
    384 
    385 	for (i = 0 ; i < count ; i++) {
    386 		int dir, type;
    387 
    388 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
    389 		if (ed == NULL) {
    390 			aprint_error_dev(self,
    391 			    "could not read endpoint descriptor %d\n", i);
    392 
    393 			USB_ATTACH_ERROR_RETURN;
    394 		}
    395 
    396 		dir = UE_GET_DIR(ed->bEndpointAddress);
    397 		type = UE_GET_XFERTYPE(ed->bmAttributes);
    398 
    399 		if (dir == UE_DIR_IN && type == UE_INTERRUPT)
    400 			sc->sc_evt_addr = ed->bEndpointAddress;
    401 		else if (dir == UE_DIR_IN && type == UE_BULK)
    402 			sc->sc_aclrd_addr = ed->bEndpointAddress;
    403 		else if (dir == UE_DIR_OUT && type == UE_BULK)
    404 			sc->sc_aclwr_addr = ed->bEndpointAddress;
    405 	}
    406 
    407 	if (sc->sc_evt_addr == -1) {
    408 		aprint_error_dev(self,
    409 		    "missing INTERRUPT endpoint on interface 0\n");
    410 
    411 		USB_ATTACH_ERROR_RETURN;
    412 	}
    413 	if (sc->sc_aclrd_addr == -1) {
    414 		aprint_error_dev(self,
    415 		    "missing BULK IN endpoint on interface 0\n");
    416 
    417 		USB_ATTACH_ERROR_RETURN;
    418 	}
    419 	if (sc->sc_aclwr_addr == -1) {
    420 		aprint_error_dev(self,
    421 		    "missing BULK OUT endpoint on interface 0\n");
    422 
    423 		USB_ATTACH_ERROR_RETURN;
    424 	}
    425 
    426 	/*
    427 	 * Interface 1 must have 2 endpoints
    428 	 *	1) Isochronous IN endpoint to receive SCO data
    429 	 *	2) Isochronous OUT endpoint to send SCO data
    430 	 *
    431 	 * and will have several configurations, which can be selected
    432 	 * via a sysctl variable. We select config 0 to start, which
    433 	 * means that no SCO data will be available.
    434 	 */
    435 	err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
    436 	if (err) {
    437 		aprint_error_dev(self,
    438 		    "Could not get interface 1 handle %s (%d)\n",
    439 		    usbd_errstr(err), err);
    440 
    441 		USB_ATTACH_ERROR_RETURN;
    442 	}
    443 
    444 	cd = usbd_get_config_descriptor(sc->sc_udev);
    445 	if (cd == NULL) {
    446 		aprint_error_dev(self, "could not get config descriptor\n");
    447 
    448 		USB_ATTACH_ERROR_RETURN;
    449 	}
    450 
    451 	sc->sc_alt_config = usbd_get_no_alts(cd, 1);
    452 
    453 	/* set initial config */
    454 	err = ubt_set_isoc_config(sc);
    455 	if (err) {
    456 		aprint_error_dev(self, "ISOC config failed\n");
    457 
    458 		USB_ATTACH_ERROR_RETURN;
    459 	}
    460 
    461 	/* Attach HCI */
    462 	sc->sc_unit = hci_attach(&ubt_hci, USBDEV(sc->sc_dev), 0);
    463 
    464 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    465 			   USBDEV(sc->sc_dev));
    466 
    467 	/* sysctl set-up for alternate configs */
    468 	sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    469 		CTLFLAG_PERMANENT,
    470 		CTLTYPE_NODE, "hw",
    471 		NULL,
    472 		NULL, 0,
    473 		NULL, 0,
    474 		CTL_HW, CTL_EOL);
    475 
    476 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
    477 		0,
    478 		CTLTYPE_NODE, USBDEVNAME(sc->sc_dev),
    479 		SYSCTL_DESCR("ubt driver information"),
    480 		NULL, 0,
    481 		NULL, 0,
    482 		CTL_HW,
    483 		CTL_CREATE, CTL_EOL);
    484 
    485 	if (node != NULL) {
    486 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    487 			CTLFLAG_READWRITE,
    488 			CTLTYPE_INT, "config",
    489 			SYSCTL_DESCR("configuration number"),
    490 			ubt_sysctl_config, 0,
    491 			sc, 0,
    492 			CTL_HW, node->sysctl_num,
    493 			CTL_CREATE, CTL_EOL);
    494 
    495 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    496 			CTLFLAG_READONLY,
    497 			CTLTYPE_INT, "alt_config",
    498 			SYSCTL_DESCR("number of alternate configurations"),
    499 			NULL, 0,
    500 			&sc->sc_alt_config, sizeof(sc->sc_alt_config),
    501 			CTL_HW, node->sysctl_num,
    502 			CTL_CREATE, CTL_EOL);
    503 
    504 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    505 			CTLFLAG_READONLY,
    506 			CTLTYPE_INT, "sco_rxsize",
    507 			SYSCTL_DESCR("max SCO receive size"),
    508 			NULL, 0,
    509 			&sc->sc_scord_size, sizeof(sc->sc_scord_size),
    510 			CTL_HW, node->sysctl_num,
    511 			CTL_CREATE, CTL_EOL);
    512 
    513 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    514 			CTLFLAG_READONLY,
    515 			CTLTYPE_INT, "sco_txsize",
    516 			SYSCTL_DESCR("max SCO transmit size"),
    517 			NULL, 0,
    518 			&sc->sc_scowr_size, sizeof(sc->sc_scowr_size),
    519 			CTL_HW, node->sysctl_num,
    520 			CTL_CREATE, CTL_EOL);
    521 	}
    522 
    523 	sc->sc_ok = 1;
    524 	if (!pmf_device_register(self, NULL, NULL))
    525 		aprint_error_dev(self, "couldn't establish power handler\n");
    526 	USB_ATTACH_SUCCESS_RETURN;
    527 }
    528 
    529 USB_DETACH(ubt)
    530 {
    531 	USB_DETACH_START(ubt, sc);
    532 	int s;
    533 
    534 	DPRINTF("sc=%p flags=%d\n", sc, flags);
    535 
    536 	if (device_pmf_is_registered(self))
    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 	usbd_abort_default_pipe(sc->sc_udev);
    740 
    741 	if (sc->sc_evt_pipe != NULL) {
    742 		usbd_abort_pipe(sc->sc_evt_pipe);
    743 		usbd_close_pipe(sc->sc_evt_pipe);
    744 		sc->sc_evt_pipe = NULL;
    745 	}
    746 
    747 	if (sc->sc_aclrd_pipe != NULL) {
    748 		usbd_abort_pipe(sc->sc_aclrd_pipe);
    749 		usbd_close_pipe(sc->sc_aclrd_pipe);
    750 		sc->sc_aclrd_pipe = NULL;
    751 	}
    752 
    753 	if (sc->sc_aclwr_pipe != NULL) {
    754 		usbd_abort_pipe(sc->sc_aclwr_pipe);
    755 		usbd_close_pipe(sc->sc_aclwr_pipe);
    756 		sc->sc_aclwr_pipe = NULL;
    757 	}
    758 
    759 	if (sc->sc_scord_pipe != NULL) {
    760 		usbd_abort_pipe(sc->sc_scord_pipe);
    761 		usbd_close_pipe(sc->sc_scord_pipe);
    762 		sc->sc_scord_pipe = NULL;
    763 	}
    764 
    765 	if (sc->sc_scowr_pipe != NULL) {
    766 		usbd_abort_pipe(sc->sc_scowr_pipe);
    767 		usbd_close_pipe(sc->sc_scowr_pipe);
    768 		sc->sc_scowr_pipe = NULL;
    769 	}
    770 
    771 	/* Free event buffer */
    772 	if (sc->sc_evt_buf != NULL) {
    773 		free(sc->sc_evt_buf, M_USBDEV);
    774 		sc->sc_evt_buf = NULL;
    775 	}
    776 
    777 	/* Free all xfers and xfer buffers (implicit) */
    778 	if (sc->sc_cmd_xfer != NULL) {
    779 		usbd_free_xfer(sc->sc_cmd_xfer);
    780 		sc->sc_cmd_xfer = NULL;
    781 		sc->sc_cmd_buf = NULL;
    782 	}
    783 
    784 	if (sc->sc_aclrd_xfer != NULL) {
    785 		usbd_free_xfer(sc->sc_aclrd_xfer);
    786 		sc->sc_aclrd_xfer = NULL;
    787 		sc->sc_aclrd_buf = NULL;
    788 	}
    789 
    790 	if (sc->sc_aclwr_xfer != NULL) {
    791 		usbd_free_xfer(sc->sc_aclwr_xfer);
    792 		sc->sc_aclwr_xfer = NULL;
    793 		sc->sc_aclwr_buf = NULL;
    794 	}
    795 
    796 	for (i = 0 ; i < UBT_NXFERS ; i++) {
    797 		if (sc->sc_scord[i].xfer != NULL) {
    798 			usbd_free_xfer(sc->sc_scord[i].xfer);
    799 			sc->sc_scord[i].xfer = NULL;
    800 			sc->sc_scord[i].buf = NULL;
    801 		}
    802 
    803 		if (sc->sc_scowr[i].xfer != NULL) {
    804 			usbd_free_xfer(sc->sc_scowr[i].xfer);
    805 			sc->sc_scowr[i].xfer = NULL;
    806 			sc->sc_scowr[i].buf = NULL;
    807 		}
    808 	}
    809 
    810 	/* Free partial SCO packets */
    811 	if (sc->sc_scord_mbuf != NULL) {
    812 		m_freem(sc->sc_scord_mbuf);
    813 		sc->sc_scord_mbuf = NULL;
    814 	}
    815 
    816 	if (sc->sc_scowr_mbuf != NULL) {
    817 		m_freem(sc->sc_scowr_mbuf);
    818 		sc->sc_scowr_mbuf = NULL;
    819 	}
    820 
    821 	/* Empty mbuf queues */
    822 	MBUFQ_DRAIN(&sc->sc_cmd_queue);
    823 	MBUFQ_DRAIN(&sc->sc_aclwr_queue);
    824 	MBUFQ_DRAIN(&sc->sc_scowr_queue);
    825 }
    826 
    827 /*******************************************************************************
    828  *
    829  * Bluetooth Unit/USB callbacks
    830  *
    831  */
    832 static int
    833 ubt_enable(device_ptr_t self)
    834 {
    835 	struct ubt_softc *sc = USBGETSOFTC(self);
    836 	usbd_status err;
    837 	int s, i, error;
    838 
    839 	DPRINTFN(1, "sc=%p\n", sc);
    840 
    841 	if (sc->sc_enabled)
    842 		return 0;
    843 
    844 	s = splusb();
    845 
    846 	/* Events */
    847 	sc->sc_evt_buf = malloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
    848 	if (sc->sc_evt_buf == NULL) {
    849 		error = ENOMEM;
    850 		goto bad;
    851 	}
    852 	err = usbd_open_pipe_intr(sc->sc_iface0,
    853 				  sc->sc_evt_addr,
    854 				  USBD_SHORT_XFER_OK,
    855 				  &sc->sc_evt_pipe,
    856 				  sc,
    857 				  sc->sc_evt_buf,
    858 				  UBT_BUFSIZ_EVENT,
    859 				  ubt_recv_event,
    860 				  USBD_DEFAULT_INTERVAL);
    861 	if (err != USBD_NORMAL_COMPLETION) {
    862 		error = EIO;
    863 		goto bad;
    864 	}
    865 
    866 	/* Commands */
    867 	sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
    868 	if (sc->sc_cmd_xfer == NULL) {
    869 		error = ENOMEM;
    870 		goto bad;
    871 	}
    872 	sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
    873 	if (sc->sc_cmd_buf == NULL) {
    874 		error = ENOMEM;
    875 		goto bad;
    876 	}
    877 	sc->sc_cmd_busy = 0;
    878 
    879 	/* ACL read */
    880 	err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
    881 				USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
    882 	if (err != USBD_NORMAL_COMPLETION) {
    883 		error = EIO;
    884 		goto bad;
    885 	}
    886 	sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
    887 	if (sc->sc_aclrd_xfer == NULL) {
    888 		error = ENOMEM;
    889 		goto bad;
    890 	}
    891 	sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
    892 	if (sc->sc_aclrd_buf == NULL) {
    893 		error = ENOMEM;
    894 		goto bad;
    895 	}
    896 	sc->sc_aclrd_busy = 0;
    897 	ubt_recv_acl_start(sc);
    898 
    899 	/* ACL write */
    900 	err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
    901 				USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
    902 	if (err != USBD_NORMAL_COMPLETION) {
    903 		error = EIO;
    904 		goto bad;
    905 	}
    906 	sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
    907 	if (sc->sc_aclwr_xfer == NULL) {
    908 		error = ENOMEM;
    909 		goto bad;
    910 	}
    911 	sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
    912 	if (sc->sc_aclwr_buf == NULL) {
    913 		error = ENOMEM;
    914 		goto bad;
    915 	}
    916 	sc->sc_aclwr_busy = 0;
    917 
    918 	/* SCO read */
    919 	if (sc->sc_scord_size > 0) {
    920 		err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
    921 					USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
    922 		if (err != USBD_NORMAL_COMPLETION) {
    923 			error = EIO;
    924 			goto bad;
    925 		}
    926 
    927 		for (i = 0 ; i < UBT_NXFERS ; i++) {
    928 			sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
    929 			if (sc->sc_scord[i].xfer == NULL) {
    930 				error = ENOMEM;
    931 				goto bad;
    932 			}
    933 			sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
    934 						sc->sc_scord_size * UBT_NFRAMES);
    935 			if (sc->sc_scord[i].buf == NULL) {
    936 				error = ENOMEM;
    937 				goto bad;
    938 			}
    939 			sc->sc_scord[i].softc = sc;
    940 			sc->sc_scord[i].busy = 0;
    941 			ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
    942 		}
    943 	}
    944 
    945 	/* SCO write */
    946 	if (sc->sc_scowr_size > 0) {
    947 		err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
    948 					USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
    949 		if (err != USBD_NORMAL_COMPLETION) {
    950 			error = EIO;
    951 			goto bad;
    952 		}
    953 
    954 		for (i = 0 ; i < UBT_NXFERS ; i++) {
    955 			sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
    956 			if (sc->sc_scowr[i].xfer == NULL) {
    957 				error = ENOMEM;
    958 				goto bad;
    959 			}
    960 			sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
    961 						sc->sc_scowr_size * UBT_NFRAMES);
    962 			if (sc->sc_scowr[i].buf == NULL) {
    963 				error = ENOMEM;
    964 				goto bad;
    965 			}
    966 			sc->sc_scowr[i].softc = sc;
    967 			sc->sc_scowr[i].busy = 0;
    968 		}
    969 
    970 		sc->sc_scowr_busy = 0;
    971 	}
    972 
    973 	sc->sc_enabled = 1;
    974 	splx(s);
    975 	return 0;
    976 
    977 bad:
    978 	ubt_abortdealloc(sc);
    979 	splx(s);
    980 	return error;
    981 }
    982 
    983 static void
    984 ubt_disable(device_ptr_t self)
    985 {
    986 	struct ubt_softc *sc = USBGETSOFTC(self);
    987 	int s;
    988 
    989 	DPRINTFN(1, "sc=%p\n", sc);
    990 
    991 	if (sc->sc_enabled == 0)
    992 		return;
    993 
    994 	s = splusb();
    995 	ubt_abortdealloc(sc);
    996 
    997 	sc->sc_enabled = 0;
    998 	splx(s);
    999 }
   1000 
   1001 static void
   1002 ubt_xmit_cmd(device_ptr_t self, struct mbuf *m)
   1003 {
   1004 	struct ubt_softc *sc = USBGETSOFTC(self);
   1005 	int s;
   1006 
   1007 	KASSERT(sc->sc_enabled);
   1008 
   1009 	s = splusb();
   1010 	MBUFQ_ENQUEUE(&sc->sc_cmd_queue, m);
   1011 
   1012 	if (sc->sc_cmd_busy == 0)
   1013 		ubt_xmit_cmd_start(sc);
   1014 
   1015 	splx(s);
   1016 }
   1017 
   1018 static void
   1019 ubt_xmit_cmd_start(struct ubt_softc *sc)
   1020 {
   1021 	usb_device_request_t req;
   1022 	usbd_status status;
   1023 	struct mbuf *m;
   1024 	int len;
   1025 
   1026 	if (sc->sc_dying)
   1027 		return;
   1028 
   1029 	if (MBUFQ_FIRST(&sc->sc_cmd_queue) == NULL)
   1030 		return;
   1031 
   1032 	MBUFQ_DEQUEUE(&sc->sc_cmd_queue, m);
   1033 	KASSERT(m != NULL);
   1034 
   1035 	DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
   1036 			USBDEVNAME(sc->sc_dev), m->m_pkthdr.len);
   1037 
   1038 	sc->sc_refcnt++;
   1039 	sc->sc_cmd_busy = 1;
   1040 
   1041 	len = m->m_pkthdr.len - 1;
   1042 	m_copydata(m, 1, len, sc->sc_cmd_buf);
   1043 	m_freem(m);
   1044 
   1045 	memset(&req, 0, sizeof(req));
   1046 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
   1047 	USETW(req.wLength, len);
   1048 
   1049 	usbd_setup_default_xfer(sc->sc_cmd_xfer,
   1050 				sc->sc_udev,
   1051 				sc,
   1052 				UBT_CMD_TIMEOUT,
   1053 				&req,
   1054 				sc->sc_cmd_buf,
   1055 				len,
   1056 				USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
   1057 				ubt_xmit_cmd_complete);
   1058 
   1059 	status = usbd_transfer(sc->sc_cmd_xfer);
   1060 
   1061 	KASSERT(status != USBD_NORMAL_COMPLETION);
   1062 
   1063 	if (status != USBD_IN_PROGRESS) {
   1064 		DPRINTF("usbd_transfer status=%s (%d)\n",
   1065 			usbd_errstr(status), status);
   1066 
   1067 		sc->sc_refcnt--;
   1068 		sc->sc_cmd_busy = 0;
   1069 	}
   1070 }
   1071 
   1072 static void
   1073 ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
   1074 			usbd_private_handle h, usbd_status status)
   1075 {
   1076 	struct ubt_softc *sc = h;
   1077 	uint32_t count;
   1078 
   1079 	DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
   1080 			USBDEVNAME(sc->sc_dev), usbd_errstr(status), status);
   1081 
   1082 	sc->sc_cmd_busy = 0;
   1083 
   1084 	if (--sc->sc_refcnt < 0) {
   1085 		DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
   1086 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1087 		return;
   1088 	}
   1089 
   1090 	if (sc->sc_dying) {
   1091 		DPRINTF("sc_dying\n");
   1092 		return;
   1093 	}
   1094 
   1095 	if (status != USBD_NORMAL_COMPLETION) {
   1096 		DPRINTF("status=%s (%d)\n",
   1097 			usbd_errstr(status), status);
   1098 
   1099 		sc->sc_stats.err_tx++;
   1100 		return;
   1101 	}
   1102 
   1103 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1104 	sc->sc_stats.cmd_tx++;
   1105 	sc->sc_stats.byte_tx += count;
   1106 
   1107 	ubt_xmit_cmd_start(sc);
   1108 }
   1109 
   1110 static void
   1111 ubt_xmit_acl(device_ptr_t self, struct mbuf *m)
   1112 {
   1113 	struct ubt_softc *sc = USBGETSOFTC(self);
   1114 	int s;
   1115 
   1116 	KASSERT(sc->sc_enabled);
   1117 
   1118 	s = splusb();
   1119 	MBUFQ_ENQUEUE(&sc->sc_aclwr_queue, m);
   1120 
   1121 	if (sc->sc_aclwr_busy == 0)
   1122 		ubt_xmit_acl_start(sc);
   1123 
   1124 	splx(s);
   1125 }
   1126 
   1127 static void
   1128 ubt_xmit_acl_start(struct ubt_softc *sc)
   1129 {
   1130 	struct mbuf *m;
   1131 	usbd_status status;
   1132 	int len;
   1133 
   1134 	if (sc->sc_dying)
   1135 		return;
   1136 
   1137 	if (MBUFQ_FIRST(&sc->sc_aclwr_queue) == NULL)
   1138 		return;
   1139 
   1140 	sc->sc_refcnt++;
   1141 	sc->sc_aclwr_busy = 1;
   1142 
   1143 	MBUFQ_DEQUEUE(&sc->sc_aclwr_queue, m);
   1144 	KASSERT(m != NULL);
   1145 
   1146 	DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
   1147 			USBDEVNAME(sc->sc_dev), m->m_pkthdr.len);
   1148 
   1149 	len = m->m_pkthdr.len - 1;
   1150 	if (len > UBT_BUFSIZ_ACL) {
   1151 		DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
   1152 			USBDEVNAME(sc->sc_dev), len, UBT_BUFSIZ_ACL);
   1153 
   1154 		len = UBT_BUFSIZ_ACL;
   1155 	}
   1156 
   1157 	m_copydata(m, 1, len, sc->sc_aclwr_buf);
   1158 	m_freem(m);
   1159 
   1160 	sc->sc_stats.acl_tx++;
   1161 	sc->sc_stats.byte_tx += len;
   1162 
   1163 	usbd_setup_xfer(sc->sc_aclwr_xfer,
   1164 			sc->sc_aclwr_pipe,
   1165 			sc,
   1166 			sc->sc_aclwr_buf,
   1167 			len,
   1168 			USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
   1169 			UBT_ACL_TIMEOUT,
   1170 			ubt_xmit_acl_complete);
   1171 
   1172 	status = usbd_transfer(sc->sc_aclwr_xfer);
   1173 
   1174 	KASSERT(status != USBD_NORMAL_COMPLETION);
   1175 
   1176 	if (status != USBD_IN_PROGRESS) {
   1177 		DPRINTF("usbd_transfer status=%s (%d)\n",
   1178 			usbd_errstr(status), status);
   1179 
   1180 		sc->sc_refcnt--;
   1181 		sc->sc_aclwr_busy = 0;
   1182 	}
   1183 }
   1184 
   1185 static void
   1186 ubt_xmit_acl_complete(usbd_xfer_handle xfer,
   1187 		usbd_private_handle h, usbd_status status)
   1188 {
   1189 	struct ubt_softc *sc = h;
   1190 
   1191 	DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
   1192 		USBDEVNAME(sc->sc_dev), usbd_errstr(status), status);
   1193 
   1194 	sc->sc_aclwr_busy = 0;
   1195 
   1196 	if (--sc->sc_refcnt < 0) {
   1197 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1198 		return;
   1199 	}
   1200 
   1201 	if (sc->sc_dying)
   1202 		return;
   1203 
   1204 	if (status != USBD_NORMAL_COMPLETION) {
   1205 		DPRINTF("status=%s (%d)\n",
   1206 			usbd_errstr(status), status);
   1207 
   1208 		sc->sc_stats.err_tx++;
   1209 
   1210 		if (status == USBD_STALLED)
   1211 			usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
   1212 		else
   1213 			return;
   1214 	}
   1215 
   1216 	ubt_xmit_acl_start(sc);
   1217 }
   1218 
   1219 static void
   1220 ubt_xmit_sco(device_ptr_t self, struct mbuf *m)
   1221 {
   1222 	struct ubt_softc *sc = USBGETSOFTC(self);
   1223 	int s;
   1224 
   1225 	KASSERT(sc->sc_enabled);
   1226 
   1227 	s = splusb();
   1228 	MBUFQ_ENQUEUE(&sc->sc_scowr_queue, m);
   1229 
   1230 	if (sc->sc_scowr_busy == 0)
   1231 		ubt_xmit_sco_start(sc);
   1232 
   1233 	splx(s);
   1234 }
   1235 
   1236 static void
   1237 ubt_xmit_sco_start(struct ubt_softc *sc)
   1238 {
   1239 	int i;
   1240 
   1241 	if (sc->sc_dying || sc->sc_scowr_size == 0)
   1242 		return;
   1243 
   1244 	for (i = 0 ; i < UBT_NXFERS ; i++) {
   1245 		if (sc->sc_scowr[i].busy)
   1246 			continue;
   1247 
   1248 		ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
   1249 	}
   1250 }
   1251 
   1252 static void
   1253 ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
   1254 {
   1255 	struct mbuf *m;
   1256 	uint8_t *buf;
   1257 	int num, len, size, space;
   1258 
   1259 	space = sc->sc_scowr_size * UBT_NFRAMES;
   1260 	buf = isoc->buf;
   1261 	len = 0;
   1262 
   1263 	/*
   1264 	 * Fill the request buffer with data from the queue,
   1265 	 * keeping any leftover packet on our private hook.
   1266 	 *
   1267 	 * Complete packets are passed back up to the stack
   1268 	 * for disposal, since we can't rely on the controller
   1269 	 * to tell us when it has finished with them.
   1270 	 */
   1271 
   1272 	m = sc->sc_scowr_mbuf;
   1273 	while (space > 0) {
   1274 		if (m == NULL) {
   1275 			MBUFQ_DEQUEUE(&sc->sc_scowr_queue, m);
   1276 			if (m == NULL)
   1277 				break;
   1278 
   1279 			m_adj(m, 1);	/* packet type */
   1280 		}
   1281 
   1282 		if (m->m_pkthdr.len > 0) {
   1283 			size = MIN(m->m_pkthdr.len, space);
   1284 
   1285 			m_copydata(m, 0, size, buf);
   1286 			m_adj(m, size);
   1287 
   1288 			buf += size;
   1289 			len += size;
   1290 			space -= size;
   1291 		}
   1292 
   1293 		if (m->m_pkthdr.len == 0) {
   1294 			sc->sc_stats.sco_tx++;
   1295 			if (!hci_complete_sco(sc->sc_unit, m))
   1296 				sc->sc_stats.err_tx++;
   1297 
   1298 			m = NULL;
   1299 		}
   1300 	}
   1301 	sc->sc_scowr_mbuf = m;
   1302 
   1303 	DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
   1304 
   1305 	if (len == 0)	/* nothing to send */
   1306 		return;
   1307 
   1308 	sc->sc_refcnt++;
   1309 	sc->sc_scowr_busy = 1;
   1310 	sc->sc_stats.byte_tx += len;
   1311 	isoc->busy = 1;
   1312 
   1313 	/*
   1314 	 * calculate number of isoc frames and sizes
   1315 	 */
   1316 
   1317 	for (num = 0 ; len > 0 ; num++) {
   1318 		size = MIN(sc->sc_scowr_size, len);
   1319 
   1320 		isoc->size[num] = size;
   1321 		len -= size;
   1322 	}
   1323 
   1324 	usbd_setup_isoc_xfer(isoc->xfer,
   1325 			     sc->sc_scowr_pipe,
   1326 			     isoc,
   1327 			     isoc->size,
   1328 			     num,
   1329 			     USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
   1330 			     ubt_xmit_sco_complete);
   1331 
   1332 	usbd_transfer(isoc->xfer);
   1333 }
   1334 
   1335 static void
   1336 ubt_xmit_sco_complete(usbd_xfer_handle xfer,
   1337 		usbd_private_handle h, usbd_status status)
   1338 {
   1339 	struct ubt_isoc_xfer *isoc = h;
   1340 	struct ubt_softc *sc;
   1341 	int i;
   1342 
   1343 	KASSERT(xfer == isoc->xfer);
   1344 	sc = isoc->softc;
   1345 
   1346 	DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
   1347 		isoc, usbd_errstr(status), status);
   1348 
   1349 	isoc->busy = 0;
   1350 
   1351 	for (i = 0 ; ; i++) {
   1352 		if (i == UBT_NXFERS) {
   1353 			sc->sc_scowr_busy = 0;
   1354 			break;
   1355 		}
   1356 
   1357 		if (sc->sc_scowr[i].busy)
   1358 			break;
   1359 	}
   1360 
   1361 	if (--sc->sc_refcnt < 0) {
   1362 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1363 		return;
   1364 	}
   1365 
   1366 	if (sc->sc_dying)
   1367 		return;
   1368 
   1369 	if (status != USBD_NORMAL_COMPLETION) {
   1370 		DPRINTF("status=%s (%d)\n",
   1371 			usbd_errstr(status), status);
   1372 
   1373 		sc->sc_stats.err_tx++;
   1374 
   1375 		if (status == USBD_STALLED)
   1376 			usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
   1377 		else
   1378 			return;
   1379 	}
   1380 
   1381 	ubt_xmit_sco_start(sc);
   1382 }
   1383 
   1384 /*
   1385  * load incoming data into an mbuf with
   1386  * leading type byte
   1387  */
   1388 static struct mbuf *
   1389 ubt_mbufload(uint8_t *buf, int count, uint8_t type)
   1390 {
   1391 	struct mbuf *m;
   1392 
   1393 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1394 	if (m == NULL)
   1395 		return NULL;
   1396 
   1397 	*mtod(m, uint8_t *) = type;
   1398 	m->m_pkthdr.len = m->m_len = MHLEN;
   1399 	m_copyback(m, 1, count, buf);	// (extends if necessary)
   1400 	if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
   1401 		m_free(m);
   1402 		return NULL;
   1403 	}
   1404 
   1405 	m->m_pkthdr.len = count + 1;
   1406 	m->m_len = MIN(MHLEN, m->m_pkthdr.len);
   1407 
   1408 	return m;
   1409 }
   1410 
   1411 static void
   1412 ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
   1413 {
   1414 	struct ubt_softc *sc = h;
   1415 	struct mbuf *m;
   1416 	uint32_t count;
   1417 	void *buf;
   1418 
   1419 	DPRINTFN(15, "sc=%p status=%s (%d)\n",
   1420 		    sc, usbd_errstr(status), status);
   1421 
   1422 	if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
   1423 		return;
   1424 
   1425 	usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
   1426 
   1427 	if (count < sizeof(hci_event_hdr_t) - 1) {
   1428 		DPRINTF("dumped undersized event (count = %d)\n", count);
   1429 		sc->sc_stats.err_rx++;
   1430 		return;
   1431 	}
   1432 
   1433 	sc->sc_stats.evt_rx++;
   1434 	sc->sc_stats.byte_rx += count;
   1435 
   1436 	m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
   1437 	if (m == NULL || !hci_input_event(sc->sc_unit, m))
   1438 		sc->sc_stats.err_rx++;
   1439 }
   1440 
   1441 static void
   1442 ubt_recv_acl_start(struct ubt_softc *sc)
   1443 {
   1444 	usbd_status status;
   1445 
   1446 	DPRINTFN(15, "sc=%p\n", sc);
   1447 
   1448 	if (sc->sc_aclrd_busy || sc->sc_dying) {
   1449 		DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
   1450 			sc->sc_aclrd_busy,
   1451 			sc->sc_dying);
   1452 
   1453 		return;
   1454 	}
   1455 
   1456 	sc->sc_refcnt++;
   1457 	sc->sc_aclrd_busy = 1;
   1458 
   1459 	usbd_setup_xfer(sc->sc_aclrd_xfer,
   1460 			sc->sc_aclrd_pipe,
   1461 			sc,
   1462 			sc->sc_aclrd_buf,
   1463 			UBT_BUFSIZ_ACL,
   1464 			USBD_NO_COPY | USBD_SHORT_XFER_OK,
   1465 			USBD_NO_TIMEOUT,
   1466 			ubt_recv_acl_complete);
   1467 
   1468 	status = usbd_transfer(sc->sc_aclrd_xfer);
   1469 
   1470 	KASSERT(status != USBD_NORMAL_COMPLETION);
   1471 
   1472 	if (status != USBD_IN_PROGRESS) {
   1473 		DPRINTF("usbd_transfer status=%s (%d)\n",
   1474 			usbd_errstr(status), status);
   1475 
   1476 		sc->sc_refcnt--;
   1477 		sc->sc_aclrd_busy = 0;
   1478 	}
   1479 }
   1480 
   1481 static void
   1482 ubt_recv_acl_complete(usbd_xfer_handle xfer,
   1483 		usbd_private_handle h, usbd_status status)
   1484 {
   1485 	struct ubt_softc *sc = h;
   1486 	struct mbuf *m;
   1487 	uint32_t count;
   1488 	void *buf;
   1489 
   1490 	DPRINTFN(15, "sc=%p status=%s (%d)\n",
   1491 			sc, usbd_errstr(status), status);
   1492 
   1493 	sc->sc_aclrd_busy = 0;
   1494 
   1495 	if (--sc->sc_refcnt < 0) {
   1496 		DPRINTF("refcnt = %d\n", sc->sc_refcnt);
   1497 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1498 		return;
   1499 	}
   1500 
   1501 	if (sc->sc_dying) {
   1502 		DPRINTF("sc_dying\n");
   1503 		return;
   1504 	}
   1505 
   1506 	if (status != USBD_NORMAL_COMPLETION) {
   1507 		DPRINTF("status=%s (%d)\n",
   1508 			usbd_errstr(status), status);
   1509 
   1510 		sc->sc_stats.err_rx++;
   1511 
   1512 		if (status == USBD_STALLED)
   1513 			usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
   1514 		else
   1515 			return;
   1516 	} else {
   1517 		usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
   1518 
   1519 		if (count < sizeof(hci_acldata_hdr_t) - 1) {
   1520 			DPRINTF("dumped undersized packet (%d)\n", count);
   1521 			sc->sc_stats.err_rx++;
   1522 		} else {
   1523 			sc->sc_stats.acl_rx++;
   1524 			sc->sc_stats.byte_rx += count;
   1525 
   1526 			m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
   1527 			if (m == NULL || !hci_input_acl(sc->sc_unit, m))
   1528 				sc->sc_stats.err_rx++;
   1529 		}
   1530 	}
   1531 
   1532 	/* and restart */
   1533 	ubt_recv_acl_start(sc);
   1534 }
   1535 
   1536 static void
   1537 ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
   1538 {
   1539 	int i;
   1540 
   1541 	DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
   1542 
   1543 	if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
   1544 		DPRINTF("%s%s%s\n",
   1545 			isoc->busy ? " busy" : "",
   1546 			sc->sc_dying ? " dying" : "",
   1547 			sc->sc_scord_size == 0 ? " size=0" : "");
   1548 
   1549 		return;
   1550 	}
   1551 
   1552 	sc->sc_refcnt++;
   1553 	isoc->busy = 1;
   1554 
   1555 	for (i = 0 ; i < UBT_NFRAMES ; i++)
   1556 		isoc->size[i] = sc->sc_scord_size;
   1557 
   1558 	usbd_setup_isoc_xfer(isoc->xfer,
   1559 			     sc->sc_scord_pipe,
   1560 			     isoc,
   1561 			     isoc->size,
   1562 			     UBT_NFRAMES,
   1563 			     USBD_NO_COPY | USBD_SHORT_XFER_OK,
   1564 			     ubt_recv_sco_complete);
   1565 
   1566 	usbd_transfer(isoc->xfer);
   1567 }
   1568 
   1569 static void
   1570 ubt_recv_sco_complete(usbd_xfer_handle xfer,
   1571 		usbd_private_handle h, usbd_status status)
   1572 {
   1573 	struct ubt_isoc_xfer *isoc = h;
   1574 	struct ubt_softc *sc;
   1575 	struct mbuf *m;
   1576 	uint32_t count;
   1577 	uint8_t *ptr, *frame;
   1578 	int i, size, got, want;
   1579 
   1580 	KASSERT(isoc != NULL);
   1581 	KASSERT(isoc->xfer == xfer);
   1582 
   1583 	sc = isoc->softc;
   1584 	isoc->busy = 0;
   1585 
   1586 	if (--sc->sc_refcnt < 0) {
   1587 		DPRINTF("refcnt=%d\n", sc->sc_refcnt);
   1588 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1589 		return;
   1590 	}
   1591 
   1592 	if (sc->sc_dying) {
   1593 		DPRINTF("sc_dying\n");
   1594 		return;
   1595 	}
   1596 
   1597 	if (status != USBD_NORMAL_COMPLETION) {
   1598 		DPRINTF("status=%s (%d)\n",
   1599 			usbd_errstr(status), status);
   1600 
   1601 		sc->sc_stats.err_rx++;
   1602 
   1603 		if (status == USBD_STALLED) {
   1604 			usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
   1605 			goto restart;
   1606 		}
   1607 
   1608 		return;
   1609 	}
   1610 
   1611 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1612 	if (count == 0)
   1613 		goto restart;
   1614 
   1615 	DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
   1616 			sc, isoc, count);
   1617 
   1618 	sc->sc_stats.byte_rx += count;
   1619 
   1620 	/*
   1621 	 * Extract SCO packets from ISOC frames. The way we have it,
   1622 	 * no SCO packet can be bigger than MHLEN. This is unlikely
   1623 	 * to actually happen, but if we ran out of mbufs and lost
   1624 	 * sync then we may get spurious data that makes it seem that
   1625 	 * way, so we discard data that wont fit. This doesnt really
   1626 	 * help with the lost sync situation alas.
   1627 	 */
   1628 
   1629 	m = sc->sc_scord_mbuf;
   1630 	if (m != NULL) {
   1631 		sc->sc_scord_mbuf = NULL;
   1632 		ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
   1633 		got = m->m_pkthdr.len;
   1634 		want = sizeof(hci_scodata_hdr_t);
   1635 		if (got >= want)
   1636 			want += mtod(m, hci_scodata_hdr_t *)->length ;
   1637 	} else {
   1638 		ptr = NULL;
   1639 		got = 0;
   1640 		want = 0;
   1641 	}
   1642 
   1643 	for (i = 0 ; i < UBT_NFRAMES ; i++) {
   1644 		frame = isoc->buf + (i * sc->sc_scord_size);
   1645 
   1646 		while (isoc->size[i] > 0) {
   1647 			size = isoc->size[i];
   1648 
   1649 			if (m == NULL) {
   1650 				MGETHDR(m, M_DONTWAIT, MT_DATA);
   1651 				if (m == NULL) {
   1652 					aprint_error_dev(sc->sc_dev,
   1653 					    "out of memory (xfer halted)\n");
   1654 
   1655 					sc->sc_stats.err_rx++;
   1656 					return;		/* lost sync */
   1657 				}
   1658 
   1659 				ptr = mtod(m, uint8_t *);
   1660 				*ptr++ = HCI_SCO_DATA_PKT;
   1661 				got = 1;
   1662 				want = sizeof(hci_scodata_hdr_t);
   1663 			}
   1664 
   1665 			if (got + size > want)
   1666 				size = want - got;
   1667 
   1668 			if (got + size > MHLEN)
   1669 				memcpy(ptr, frame, MHLEN - got);
   1670 			else
   1671 				memcpy(ptr, frame, size);
   1672 
   1673 			ptr += size;
   1674 			got += size;
   1675 			frame += size;
   1676 
   1677 			if (got == want) {
   1678 				/*
   1679 				 * If we only got a header, add the packet
   1680 				 * length to our want count. Send complete
   1681 				 * packets up to protocol stack.
   1682 				 */
   1683 				if (want == sizeof(hci_scodata_hdr_t))
   1684 					want += mtod(m, hci_scodata_hdr_t *)->length;
   1685 
   1686 				if (got == want) {
   1687 					m->m_pkthdr.len = m->m_len = got;
   1688 					sc->sc_stats.sco_rx++;
   1689 					if (!hci_input_sco(sc->sc_unit, m))
   1690 						sc->sc_stats.err_rx++;
   1691 
   1692 					m = NULL;
   1693 				}
   1694 			}
   1695 
   1696 			isoc->size[i] -= size;
   1697 		}
   1698 	}
   1699 
   1700 	if (m != NULL) {
   1701 		m->m_pkthdr.len = m->m_len = got;
   1702 		sc->sc_scord_mbuf = m;
   1703 	}
   1704 
   1705 restart: /* and restart */
   1706 	ubt_recv_sco_start1(sc, isoc);
   1707 }
   1708 
   1709 void
   1710 ubt_stats(device_ptr_t self, struct bt_stats *dest, int flush)
   1711 {
   1712 	struct ubt_softc *sc = USBGETSOFTC(self);
   1713 	int s;
   1714 
   1715 	s = splusb();
   1716 	memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
   1717 
   1718 	if (flush)
   1719 		memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
   1720 
   1721 	splx(s);
   1722 }
   1723