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