Home | History | Annotate | Line # | Download | only in usb
ubt.c revision 1.7.2.4
      1  1.7.2.4     skrll /*	$NetBSD: ubt.c,v 1.7.2.4 2005/11/10 14:08:05 skrll Exp $	*/
      2      1.1  augustss 
      3      1.1  augustss /*
      4      1.5   dsainty  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5      1.1  augustss  * All rights reserved.
      6      1.1  augustss  *
      7      1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8      1.5   dsainty  * by Lennart Augustsson (lennart (at) augustsson.net) and
      9      1.5   dsainty  * David Sainty (David.Sainty (at) dtsp.co.nz).
     10      1.1  augustss  *
     11      1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12      1.1  augustss  * modification, are permitted provided that the following conditions
     13      1.1  augustss  * are met:
     14      1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15      1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16      1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18      1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19      1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     20      1.1  augustss  *    must display the following acknowledgement:
     21      1.1  augustss  *        This product includes software developed by the NetBSD
     22      1.1  augustss  *        Foundation, Inc. and its contributors.
     23      1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1  augustss  *    contributors may be used to endorse or promote products derived
     25      1.1  augustss  *    from this software without specific prior written permission.
     26      1.1  augustss  *
     27      1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1  augustss  */
     39      1.1  augustss 
     40      1.1  augustss #include <sys/cdefs.h>
     41  1.7.2.4     skrll __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.7.2.4 2005/11/10 14:08:05 skrll Exp $");
     42      1.1  augustss 
     43      1.1  augustss #include <sys/param.h>
     44      1.1  augustss #include <sys/systm.h>
     45      1.1  augustss #include <sys/kernel.h>
     46      1.5   dsainty #include <sys/malloc.h>
     47      1.1  augustss #include <sys/device.h>
     48      1.1  augustss #include <sys/lock.h>
     49      1.1  augustss #include <sys/ioctl.h>
     50      1.1  augustss #include <sys/conf.h>
     51      1.1  augustss #include <sys/file.h>
     52      1.1  augustss #include <sys/poll.h>
     53      1.1  augustss #include <sys/select.h>
     54      1.1  augustss #include <sys/proc.h>
     55      1.1  augustss 
     56      1.5   dsainty #include <dev/usb/ubtreg.h>
     57      1.1  augustss #include <dev/usb/usb.h>
     58      1.1  augustss #include <dev/usb/usbdi.h>
     59      1.1  augustss #include <dev/usb/usbdi_util.h>
     60      1.1  augustss #include <dev/usb/usbdevs.h>
     61      1.1  augustss 
     62      1.3  augustss #include <dev/bluetooth/bluetooth.h>
     63      1.3  augustss 
     64      1.1  augustss #ifdef UBT_DEBUG
     65      1.1  augustss #define DPRINTF(x)	if (ubtdebug) logprintf x
     66      1.1  augustss #define DPRINTFN(n,x)	if (ubtdebug>(n)) logprintf x
     67      1.7  augustss int	ubtdebug = 49;
     68      1.1  augustss #else
     69      1.1  augustss #define DPRINTF(x)
     70      1.1  augustss #define DPRINTFN(n,x)
     71      1.1  augustss #endif
     72      1.1  augustss 
     73      1.1  augustss /*
     74      1.1  augustss  * Protocol related definitions
     75      1.1  augustss  */
     76      1.1  augustss 
     77      1.1  augustss struct ubt_softc {
     78      1.1  augustss  	USBBASEDEVICE		sc_dev;
     79      1.1  augustss 	usbd_device_handle	sc_udev;
     80      1.3  augustss 	usbd_interface_handle	sc_ctl_iface;
     81      1.3  augustss 	usbd_interface_handle	sc_isoc_iface;
     82      1.1  augustss 
     83      1.5   dsainty 	/* Control */
     84      1.5   dsainty 	usbd_pipe_handle        sc_ctl_pipe;
     85      1.5   dsainty 	usbd_xfer_handle        sc_ctl_xfer;
     86      1.5   dsainty 	u_int8_t                *sc_ctl_buf;
     87      1.5   dsainty 
     88      1.5   dsainty 	/* Events */
     89      1.5   dsainty 	int			sc_evt_addr;
     90      1.5   dsainty 	usbd_pipe_handle	sc_evt_pipe;
     91      1.5   dsainty 	usbd_xfer_handle	sc_evt_xfer;
     92      1.5   dsainty 	u_int8_t		*sc_evt_buf;
     93      1.5   dsainty 
     94      1.5   dsainty 	/* ACL data (in) */
     95      1.5   dsainty 	int			sc_aclrd_addr;
     96      1.5   dsainty 	usbd_pipe_handle	sc_aclrd_pipe;
     97      1.5   dsainty 	usbd_xfer_handle	sc_aclrd_xfer;
     98      1.5   dsainty 	u_int8_t		*sc_aclrd_buf;
     99      1.5   dsainty 	int			sc_aclrd_running;
    100      1.5   dsainty 
    101      1.5   dsainty 	/* ACL data (out) */
    102      1.5   dsainty 	int			sc_aclwr_addr;
    103      1.5   dsainty 	usbd_pipe_handle	sc_aclwr_pipe;
    104      1.5   dsainty 	usbd_xfer_handle	sc_aclwr_xfer;
    105      1.5   dsainty 	u_int8_t		*sc_aclwr_buf;
    106      1.1  augustss 
    107      1.5   dsainty 	struct device		*sc_child;
    108      1.5   dsainty 	struct btframe_callback_methods const *sc_cb;
    109      1.5   dsainty 
    110      1.1  augustss 	int			sc_refcnt;
    111      1.1  augustss 	char			sc_dying;
    112      1.1  augustss };
    113      1.1  augustss 
    114      1.5   dsainty static int ubt_open(void *h, int flag, int mode, usb_proc_ptr p);
    115      1.5   dsainty static int ubt_close(void *h, int flag, int mode, usb_proc_ptr p);
    116      1.5   dsainty 
    117      1.6   dsainty static u_int8_t* ubt_alloc_control(void*, size_t, struct btframe_buffer**);
    118      1.6   dsainty static int ubt_send_control(void*, struct btframe_buffer*, size_t);
    119      1.6   dsainty 
    120      1.6   dsainty static u_int8_t* ubt_alloc_acldata(void*, size_t, struct btframe_buffer**);
    121      1.6   dsainty static int ubt_send_acldata(void*, struct btframe_buffer*, size_t);
    122      1.6   dsainty 
    123      1.6   dsainty static u_int8_t* ubt_alloc_scodata(void*, size_t, struct btframe_buffer**);
    124      1.6   dsainty static int ubt_send_scodata(void*, struct btframe_buffer*, size_t);
    125      1.5   dsainty 
    126      1.5   dsainty static int ubt_splraise(void);
    127      1.5   dsainty 
    128      1.5   dsainty static void ubt_event_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    129      1.5   dsainty static void ubt_aclrd_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    130      1.5   dsainty static void ubt_aclrd_request(struct ubt_softc *);
    131      1.5   dsainty 
    132      1.5   dsainty static struct btframe_methods const ubt_methods = {
    133      1.6   dsainty 	ubt_open, ubt_close,
    134      1.6   dsainty 	{ubt_alloc_control, ubt_send_control},
    135      1.6   dsainty 	{ubt_alloc_acldata, ubt_send_acldata},
    136      1.6   dsainty 	{ubt_alloc_scodata, ubt_send_scodata},
    137  1.7.2.1     skrll 	ubt_splraise
    138      1.5   dsainty };
    139      1.5   dsainty 
    140      1.1  augustss USB_DECLARE_DRIVER(ubt);
    141      1.1  augustss 
    142      1.1  augustss USB_MATCH(ubt)
    143      1.1  augustss {
    144      1.1  augustss 	USB_MATCH_START(ubt, uaa);
    145      1.1  augustss 	usb_interface_descriptor_t *id;
    146      1.1  augustss 
    147      1.1  augustss 	DPRINTFN(50,("ubt_match\n"));
    148      1.1  augustss 
    149      1.1  augustss 	if (uaa->iface == NULL)
    150      1.1  augustss 		return (UMATCH_NONE);
    151      1.1  augustss 
    152      1.1  augustss 	id = usbd_get_interface_descriptor(uaa->iface);
    153      1.1  augustss 	if (id != NULL &&
    154      1.1  augustss 	    id->bInterfaceClass == UICLASS_WIRELESS &&
    155      1.1  augustss 	    id->bInterfaceSubClass == UISUBCLASS_RF &&
    156      1.1  augustss 	    id->bInterfaceProtocol == UIPROTO_BLUETOOTH)
    157      1.1  augustss 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    158      1.1  augustss 	return (UMATCH_NONE);
    159      1.1  augustss }
    160      1.1  augustss 
    161      1.1  augustss USB_ATTACH(ubt)
    162      1.1  augustss {
    163      1.1  augustss 	USB_ATTACH_START(ubt, sc, uaa);
    164      1.1  augustss 	usbd_device_handle	dev = uaa->device;
    165      1.1  augustss 	usbd_interface_handle	iface = uaa->iface;
    166      1.3  augustss 	struct bt_attach_args	bt;
    167      1.5   dsainty 	usb_interface_descriptor_t const *id;
    168  1.7.2.4     skrll 	char			*devinfop;
    169      1.5   dsainty 	usb_endpoint_descriptor_t const *ed;
    170      1.1  augustss 	u_int8_t		epcount;
    171      1.1  augustss 	int			i;
    172      1.1  augustss 
    173      1.1  augustss 	DPRINTFN(10,("ubt_attach: sc=%p\n", sc));
    174      1.1  augustss 
    175  1.7.2.4     skrll 	devinfop = usbd_devinfo_alloc(dev, 0);
    176      1.1  augustss 	USB_ATTACH_SETUP;
    177  1.7.2.4     skrll 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    178  1.7.2.4     skrll 	usbd_devinfo_free(devinfop);
    179      1.1  augustss 
    180      1.1  augustss 	sc->sc_udev = dev;
    181      1.3  augustss 	sc->sc_ctl_iface = iface;
    182      1.1  augustss 
    183      1.5   dsainty 	/*
    184      1.3  augustss 	 * The control interface comes before the isoc interface
    185      1.3  augustss 	 * according to the spec, so we find it first.
    186      1.3  augustss 	 */
    187      1.1  augustss 	epcount = 0;
    188      1.1  augustss 	(void)usbd_endpoint_count(iface, &epcount);
    189      1.5   dsainty 
    190      1.5   dsainty 	sc->sc_evt_addr = -1;
    191      1.5   dsainty 	sc->sc_aclrd_addr = -1;
    192      1.5   dsainty 	sc->sc_aclwr_addr = -1;
    193      1.5   dsainty 
    194      1.1  augustss 	for (i = 0; i < epcount; i++) {
    195      1.1  augustss 		ed = usbd_interface2endpoint_descriptor(iface, i);
    196      1.1  augustss 		if (ed == NULL) {
    197      1.1  augustss 			printf("%s: couldn't get ep %d\n",
    198      1.1  augustss 			    USBDEVNAME(sc->sc_dev), i);
    199      1.1  augustss 			USB_ATTACH_ERROR_RETURN;
    200      1.1  augustss 		}
    201      1.5   dsainty 
    202      1.5   dsainty 		DPRINTFN(10, ("%s: addr=%d attr=%d\n", __func__,
    203      1.5   dsainty 			      (int)ed->bEndpointAddress,
    204      1.5   dsainty 			      (int)ed->bmAttributes));
    205      1.5   dsainty 
    206      1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    207      1.5   dsainty 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    208      1.5   dsainty 			sc->sc_evt_addr = ed->bEndpointAddress;
    209      1.5   dsainty 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    210      1.5   dsainty 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    211      1.5   dsainty 			sc->sc_aclrd_addr = ed->bEndpointAddress;
    212      1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    213      1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    214      1.5   dsainty 			sc->sc_aclwr_addr = ed->bEndpointAddress;
    215      1.1  augustss 		}
    216      1.1  augustss 	}
    217      1.5   dsainty 
    218      1.5   dsainty 	if (sc->sc_evt_addr == -1 ||
    219      1.5   dsainty 	    sc->sc_aclrd_addr == -1 || sc->sc_aclwr_addr == -1) {
    220      1.1  augustss 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    221      1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    222      1.1  augustss 	}
    223      1.1  augustss 
    224      1.3  augustss 	/* XXX works because isoc comes after ctl */
    225      1.3  augustss 	/* Grab isoc interface as well. */
    226      1.3  augustss 	for (i = 0; i < uaa->nifaces; i++) {
    227      1.3  augustss 		if (uaa->ifaces[i] == NULL)
    228      1.3  augustss 			continue;
    229      1.3  augustss 		id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    230      1.3  augustss 		if (id != NULL &&
    231      1.3  augustss 		    id->bInterfaceClass == UICLASS_WIRELESS &&
    232      1.3  augustss 		    id->bInterfaceSubClass == UISUBCLASS_RF &&
    233      1.3  augustss 		    id->bInterfaceProtocol == UIPROTO_BLUETOOTH) {
    234      1.3  augustss 			sc->sc_isoc_iface = uaa->ifaces[i];
    235      1.3  augustss 			uaa->ifaces[i] = NULL;
    236      1.3  augustss 		}
    237      1.3  augustss 	}
    238      1.3  augustss 
    239      1.3  augustss 	printf("%s: has%s isoc data\n", USBDEVNAME(sc->sc_dev),
    240      1.3  augustss 	       sc->sc_isoc_iface != NULL ? "" : " no");
    241      1.3  augustss 
    242      1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    243      1.1  augustss 			   USBDEV(sc->sc_dev));
    244      1.1  augustss 
    245      1.5   dsainty 	bt.bt_methods = &ubt_methods;
    246      1.5   dsainty 	bt.bt_cb = &sc->sc_cb;
    247  1.7.2.1     skrll 	bt.bt_handle = sc;
    248      1.5   dsainty 
    249      1.3  augustss 	sc->sc_child = config_found(self, &bt, bt_print);
    250      1.1  augustss 
    251      1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    252      1.1  augustss }
    253      1.1  augustss 
    254      1.5   dsainty static void
    255      1.5   dsainty ubt_abortdealloc(struct ubt_softc *sc)
    256      1.5   dsainty {
    257      1.5   dsainty 	DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
    258      1.5   dsainty 
    259      1.5   dsainty 	if (sc->sc_evt_pipe != NULL) {
    260      1.5   dsainty 		usbd_abort_pipe(sc->sc_evt_pipe);
    261      1.5   dsainty 		usbd_close_pipe(sc->sc_evt_pipe);
    262      1.5   dsainty 		sc->sc_evt_pipe = NULL;
    263      1.5   dsainty 	}
    264      1.5   dsainty 	if (sc->sc_evt_buf != NULL) {
    265      1.5   dsainty 		free(sc->sc_evt_buf, M_USBDEV);
    266      1.5   dsainty 		sc->sc_evt_buf = NULL;
    267      1.5   dsainty 	}
    268      1.5   dsainty 	if (sc->sc_aclrd_pipe != NULL) {
    269      1.5   dsainty 		usbd_abort_pipe(sc->sc_aclrd_pipe);
    270      1.5   dsainty 		usbd_close_pipe(sc->sc_aclrd_pipe);
    271      1.5   dsainty 		sc->sc_aclrd_pipe = NULL;
    272      1.5   dsainty 	}
    273      1.5   dsainty 	if (sc->sc_aclwr_pipe != NULL) {
    274      1.5   dsainty 		usbd_abort_pipe(sc->sc_aclwr_pipe);
    275      1.5   dsainty 		usbd_close_pipe(sc->sc_aclwr_pipe);
    276      1.5   dsainty 		sc->sc_aclwr_pipe = NULL;
    277      1.5   dsainty 	}
    278      1.5   dsainty 	if (sc->sc_aclrd_xfer != NULL) {
    279      1.5   dsainty 		usbd_free_xfer(sc->sc_aclrd_xfer);
    280      1.5   dsainty 		sc->sc_aclrd_xfer = NULL;
    281      1.5   dsainty 		sc->sc_aclrd_buf = NULL;
    282      1.5   dsainty 	}
    283      1.5   dsainty 	if (sc->sc_aclwr_xfer != NULL) {
    284      1.5   dsainty 		usbd_free_xfer(sc->sc_aclwr_xfer);
    285      1.5   dsainty 		sc->sc_aclwr_xfer = NULL;
    286      1.5   dsainty 		sc->sc_aclwr_buf = NULL;
    287      1.5   dsainty 	}
    288      1.5   dsainty }
    289      1.5   dsainty 
    290      1.1  augustss USB_DETACH(ubt)
    291      1.1  augustss {
    292      1.1  augustss 	USB_DETACH_START(ubt, sc);
    293      1.1  augustss 	int s;
    294      1.1  augustss 	int rv = 0;
    295      1.1  augustss 
    296      1.5   dsainty 	DPRINTF(("%s: sc=%p flags=%d\n", __func__, sc, flags));
    297      1.1  augustss 
    298      1.1  augustss 	sc->sc_dying = 1;
    299      1.5   dsainty 
    300      1.1  augustss 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    301      1.5   dsainty 	ubt_abortdealloc(sc);
    302      1.1  augustss 
    303      1.5   dsainty 	DPRINTFN(1, ("%s: waiting for USB detach\n", __func__));
    304      1.1  augustss 	s = splusb();
    305      1.1  augustss 	if (--sc->sc_refcnt >= 0) {
    306      1.1  augustss 		/* Wait for processes to go away. */
    307      1.1  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    308      1.1  augustss 	}
    309      1.1  augustss 	splx(s);
    310      1.5   dsainty 	DPRINTFN(1, ("%s: USB detach complete\n", __func__));
    311      1.1  augustss 
    312      1.1  augustss 	if (sc->sc_child != NULL) {
    313      1.5   dsainty 		DPRINTFN(1, ("%s: waiting for child detach\n", __func__));
    314      1.1  augustss 		rv = config_detach(sc->sc_child, flags);
    315      1.1  augustss 		sc->sc_child = NULL;
    316      1.5   dsainty 		DPRINTFN(1, ("%s: child detach complete\n", __func__));
    317      1.1  augustss 	}
    318      1.1  augustss 
    319      1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    320      1.1  augustss 			   USBDEV(sc->sc_dev));
    321      1.1  augustss 
    322      1.5   dsainty 	DPRINTFN(1, ("%s: driver detached\n", __func__));
    323      1.5   dsainty 
    324      1.1  augustss 	return (rv);
    325      1.1  augustss }
    326      1.1  augustss 
    327      1.1  augustss int
    328      1.1  augustss ubt_activate(device_ptr_t self, enum devact act)
    329      1.1  augustss {
    330      1.1  augustss 	struct ubt_softc *sc = (struct ubt_softc *)self;
    331      1.1  augustss 	int error = 0;
    332      1.1  augustss 
    333      1.1  augustss 	switch (act) {
    334      1.1  augustss 	case DVACT_ACTIVATE:
    335      1.1  augustss 		return (EOPNOTSUPP);
    336      1.1  augustss 		break;
    337      1.1  augustss 
    338      1.1  augustss 	case DVACT_DEACTIVATE:
    339      1.1  augustss 		sc->sc_dying = 1;
    340      1.1  augustss 		if (sc->sc_child != NULL)
    341      1.1  augustss 			error = config_deactivate(sc->sc_child);
    342      1.1  augustss 		break;
    343      1.1  augustss 	}
    344      1.1  augustss 	return (error);
    345      1.5   dsainty }
    346      1.5   dsainty 
    347      1.5   dsainty static int
    348      1.5   dsainty ubt_open(void *h, int flag, int mode, usb_proc_ptr p)
    349      1.5   dsainty {
    350      1.5   dsainty 	struct ubt_softc *sc = h;
    351      1.5   dsainty 	int error;
    352      1.5   dsainty 	usbd_status err;
    353      1.5   dsainty 
    354      1.5   dsainty 	DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
    355      1.5   dsainty 
    356      1.5   dsainty 	sc->sc_evt_buf = malloc(BTHCI_EVENT_MAX_LEN, M_USBDEV, M_NOWAIT);
    357      1.5   dsainty 	if (sc->sc_evt_buf == NULL) {
    358      1.5   dsainty 		error = ENOMEM;
    359      1.5   dsainty 		goto bad0;
    360      1.5   dsainty 	}
    361      1.5   dsainty 
    362      1.5   dsainty 	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_evt_addr,
    363      1.5   dsainty 				  USBD_SHORT_XFER_OK, &sc->sc_evt_pipe,
    364      1.5   dsainty 				  sc, sc->sc_evt_buf, BTHCI_EVENT_MAX_LEN,
    365      1.5   dsainty 				  ubt_event_cb, UBT_EVENT_EP_INTERVAL);
    366      1.5   dsainty 	if (err != USBD_NORMAL_COMPLETION) {
    367      1.5   dsainty 		error = EIO;
    368      1.5   dsainty 		goto bad1;
    369      1.5   dsainty 	}
    370      1.5   dsainty 
    371      1.5   dsainty 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_aclrd_addr,
    372      1.5   dsainty 			     0, &sc->sc_aclrd_pipe);
    373      1.5   dsainty 	if (err != USBD_NORMAL_COMPLETION) {
    374      1.5   dsainty 		error = EIO;
    375      1.5   dsainty 		goto bad2;
    376      1.5   dsainty 	}
    377      1.5   dsainty 
    378      1.5   dsainty 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_aclwr_addr,
    379      1.5   dsainty 			     0, &sc->sc_aclwr_pipe);
    380      1.5   dsainty 	if (err != USBD_NORMAL_COMPLETION) {
    381      1.5   dsainty 		error = EIO;
    382      1.5   dsainty 		goto bad3;
    383      1.5   dsainty 	}
    384      1.5   dsainty 
    385      1.5   dsainty 	sc->sc_ctl_xfer = usbd_alloc_xfer(sc->sc_udev);
    386      1.5   dsainty 	if (sc->sc_ctl_xfer == NULL) {
    387      1.5   dsainty 		error = ENOMEM;
    388      1.5   dsainty 		goto bad4;
    389      1.5   dsainty 	}
    390      1.5   dsainty 	sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
    391      1.5   dsainty 	if (sc->sc_aclrd_xfer == NULL) {
    392      1.5   dsainty 		error = ENOMEM;
    393      1.5   dsainty 		goto bad5;
    394      1.5   dsainty 	}
    395      1.5   dsainty 	sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
    396      1.5   dsainty 	if (sc->sc_aclwr_xfer == NULL) {
    397      1.5   dsainty 		error = ENOMEM;
    398      1.5   dsainty 		goto bad6;
    399      1.5   dsainty 	}
    400      1.5   dsainty 
    401      1.5   dsainty 	/* Buffers */
    402      1.5   dsainty 	sc->sc_ctl_buf = usbd_alloc_buffer(sc->sc_ctl_xfer,
    403      1.5   dsainty 					   BTHCI_COMMAND_MAX_LEN);
    404      1.5   dsainty 	if (sc->sc_ctl_buf == NULL) {
    405      1.5   dsainty 		error = ENOMEM;
    406      1.5   dsainty 		goto bad7;
    407      1.5   dsainty 	}
    408      1.5   dsainty 	sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer,
    409      1.5   dsainty 					     BTHCI_ACL_DATA_MAX_LEN);
    410      1.5   dsainty 	if (sc->sc_aclrd_buf == NULL) {
    411      1.5   dsainty 		error = ENOMEM;
    412      1.5   dsainty 		goto bad7;
    413      1.5   dsainty 	}
    414      1.5   dsainty 	sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer,
    415      1.5   dsainty 					     BTHCI_ACL_DATA_MAX_LEN);
    416      1.5   dsainty 	if (sc->sc_aclwr_buf == NULL) {
    417      1.5   dsainty 		error = ENOMEM;
    418      1.5   dsainty 		goto bad7;
    419      1.5   dsainty 	}
    420      1.5   dsainty 
    421      1.5   dsainty 	/* Start reading */
    422      1.5   dsainty 	ubt_aclrd_request(sc);
    423      1.5   dsainty 
    424      1.5   dsainty 	return 0;
    425      1.5   dsainty 
    426      1.5   dsainty  bad7:
    427      1.5   dsainty 	usbd_free_xfer(sc->sc_aclwr_xfer);
    428      1.5   dsainty 	sc->sc_aclwr_xfer = NULL;
    429      1.5   dsainty  bad6:
    430      1.5   dsainty 	usbd_free_xfer(sc->sc_aclrd_xfer);
    431      1.5   dsainty 	sc->sc_aclrd_xfer = NULL;
    432      1.5   dsainty  bad5:
    433      1.5   dsainty 	usbd_free_xfer(sc->sc_ctl_xfer);
    434      1.5   dsainty 	sc->sc_ctl_xfer = NULL;
    435      1.5   dsainty  bad4:
    436      1.5   dsainty 	usbd_close_pipe(sc->sc_aclwr_pipe);
    437      1.5   dsainty 	sc->sc_aclwr_pipe = NULL;
    438      1.5   dsainty  bad3:
    439      1.5   dsainty 	usbd_close_pipe(sc->sc_aclrd_pipe);
    440      1.5   dsainty 	sc->sc_aclrd_pipe = NULL;
    441      1.5   dsainty  bad2:
    442      1.5   dsainty 	usbd_close_pipe(sc->sc_evt_pipe);
    443      1.5   dsainty 	sc->sc_evt_pipe = NULL;
    444      1.5   dsainty  bad1:
    445      1.5   dsainty 	free(sc->sc_evt_buf, M_USBDEV);
    446      1.5   dsainty 	sc->sc_evt_buf = NULL;
    447      1.5   dsainty  bad0:
    448      1.5   dsainty 	return error;
    449      1.5   dsainty }
    450      1.5   dsainty 
    451      1.5   dsainty static int
    452      1.5   dsainty ubt_close(void *h, int flag, int mode, usb_proc_ptr p)
    453      1.5   dsainty {
    454      1.5   dsainty 	struct ubt_softc *sc = h;
    455      1.5   dsainty 
    456      1.5   dsainty 	DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
    457      1.5   dsainty 
    458      1.5   dsainty 	ubt_abortdealloc(sc);
    459      1.5   dsainty 
    460      1.5   dsainty 	return 0;
    461      1.5   dsainty }
    462      1.5   dsainty 
    463      1.6   dsainty static u_int8_t*
    464      1.6   dsainty ubt_alloc_control(void *h, size_t len, struct btframe_buffer **buf)
    465      1.6   dsainty {
    466      1.6   dsainty 	struct ubt_softc *sc = h;
    467      1.6   dsainty 
    468      1.6   dsainty 	/*
    469      1.6   dsainty 	 * We should be catching this earlier, but at the moment a
    470      1.6   dsainty 	 * user request can generate oversized allocations.
    471      1.6   dsainty 	 */
    472      1.6   dsainty 	if (len > BTHCI_COMMAND_MAX_LEN)
    473      1.6   dsainty 		return NULL;
    474      1.6   dsainty 
    475      1.6   dsainty 	*buf = (struct btframe_buffer*)sc->sc_ctl_buf;
    476      1.6   dsainty 	return sc->sc_ctl_buf;
    477      1.6   dsainty }
    478      1.6   dsainty 
    479      1.5   dsainty static int
    480      1.6   dsainty ubt_send_control(void *h, struct btframe_buffer *buf, size_t len)
    481      1.5   dsainty {
    482      1.5   dsainty 	struct ubt_softc *sc = h;
    483      1.5   dsainty 	usb_device_request_t req;
    484      1.5   dsainty 	usbd_status status;
    485      1.5   dsainty 
    486      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    487      1.5   dsainty 
    488      1.6   dsainty #ifdef DIAGNOSTIC
    489      1.6   dsainty 	if ((u_int8_t*)buf != sc->sc_ctl_buf)
    490      1.6   dsainty 		panic("ubt_control() called with wrong buffer");
    491      1.6   dsainty #endif
    492      1.6   dsainty 
    493      1.5   dsainty 	if (sc->sc_dying)
    494      1.5   dsainty 		return EIO;
    495      1.5   dsainty 
    496      1.5   dsainty 	if (len < BTHCI_COMMAND_MIN_LEN || len > BTHCI_COMMAND_MAX_LEN)
    497      1.5   dsainty 		return EINVAL;
    498      1.5   dsainty 
    499      1.5   dsainty 	sc->sc_refcnt++;
    500      1.5   dsainty 
    501      1.5   dsainty 	memset(&req, 0, sizeof(req));
    502      1.5   dsainty 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    503      1.5   dsainty 	USETW(req.wLength, len);
    504      1.5   dsainty 
    505      1.5   dsainty 	usbd_setup_default_xfer(sc->sc_ctl_xfer,
    506      1.5   dsainty 				sc->sc_udev,
    507      1.5   dsainty 				sc,
    508      1.5   dsainty 				USBD_DEFAULT_TIMEOUT,
    509      1.5   dsainty 				&req, sc->sc_ctl_buf, len,
    510      1.5   dsainty 				USBD_SYNCHRONOUS | USBD_NO_COPY, NULL);
    511      1.5   dsainty 
    512      1.5   dsainty 	status = usbd_transfer(sc->sc_ctl_xfer);
    513      1.5   dsainty 
    514      1.5   dsainty 	if (--sc->sc_refcnt < 0)
    515      1.5   dsainty 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    516      1.5   dsainty 
    517      1.5   dsainty 	if (status != USBD_NORMAL_COMPLETION)
    518      1.5   dsainty 		return EIO;
    519      1.5   dsainty 
    520      1.5   dsainty 	return 0;
    521      1.5   dsainty }
    522      1.5   dsainty 
    523      1.6   dsainty static u_int8_t*
    524      1.6   dsainty ubt_alloc_acldata(void *h, size_t len, struct btframe_buffer **buf)
    525      1.6   dsainty {
    526      1.6   dsainty 	struct ubt_softc *sc = h;
    527      1.6   dsainty 
    528      1.6   dsainty 	/*
    529      1.6   dsainty 	 * We should be catching this earlier, but at the moment a
    530      1.6   dsainty 	 * user request can generate oversized allocations.
    531      1.6   dsainty 	 */
    532      1.6   dsainty 	if (len > BTHCI_ACL_DATA_MAX_LEN)
    533      1.6   dsainty 		return NULL;
    534      1.6   dsainty 
    535      1.6   dsainty 	*buf = (struct btframe_buffer*)sc->sc_aclwr_buf;
    536      1.6   dsainty 	return sc->sc_aclwr_buf;
    537      1.6   dsainty }
    538      1.6   dsainty 
    539      1.5   dsainty static int
    540      1.6   dsainty ubt_send_acldata(void *h, struct btframe_buffer *buf, size_t len)
    541      1.5   dsainty {
    542      1.5   dsainty 	struct ubt_softc *sc = h;
    543      1.5   dsainty 	usbd_status status;
    544      1.5   dsainty 
    545      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    546      1.5   dsainty 
    547      1.6   dsainty #ifdef DIAGNOSTIC
    548      1.6   dsainty 	if ((u_int8_t*)buf != sc->sc_aclwr_buf)
    549      1.6   dsainty 		panic("ubt_sendacldata() called with wrong buffer");
    550      1.6   dsainty #endif
    551      1.6   dsainty 
    552      1.5   dsainty 	if (sc->sc_dying)
    553      1.5   dsainty 		return EIO;
    554      1.5   dsainty 
    555      1.5   dsainty 	if (len < BTHCI_ACL_DATA_MIN_LEN || len > BTHCI_ACL_DATA_MAX_LEN)
    556      1.5   dsainty 		return EINVAL;
    557      1.5   dsainty 
    558      1.5   dsainty 	sc->sc_refcnt++;
    559      1.5   dsainty 
    560      1.5   dsainty 	usbd_setup_xfer(sc->sc_aclwr_xfer,
    561      1.5   dsainty 			sc->sc_aclwr_pipe,
    562      1.5   dsainty 			(usbd_private_handle)sc,
    563      1.5   dsainty 			sc->sc_aclwr_buf, len,
    564      1.5   dsainty 			USBD_SYNCHRONOUS | USBD_NO_COPY,
    565      1.5   dsainty  			USBD_DEFAULT_TIMEOUT,
    566      1.5   dsainty 			NULL);
    567      1.5   dsainty 
    568      1.5   dsainty 	status = usbd_transfer(sc->sc_aclwr_xfer);
    569      1.5   dsainty 
    570      1.5   dsainty 	if (--sc->sc_refcnt < 0)
    571      1.5   dsainty 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    572      1.5   dsainty 
    573      1.5   dsainty 	if (status != USBD_NORMAL_COMPLETION)
    574      1.5   dsainty 		return EIO;
    575      1.5   dsainty 
    576      1.5   dsainty 	return 0;
    577      1.5   dsainty }
    578      1.5   dsainty 
    579      1.6   dsainty static u_int8_t*
    580      1.6   dsainty ubt_alloc_scodata(void *h, size_t len, struct btframe_buffer **buf)
    581      1.6   dsainty {
    582      1.6   dsainty 	return NULL;
    583      1.6   dsainty }
    584      1.6   dsainty 
    585      1.5   dsainty static int
    586      1.6   dsainty ubt_send_scodata(void *h, struct btframe_buffer *buf, size_t len)
    587      1.5   dsainty {
    588      1.5   dsainty 	struct ubt_softc *sc = h;
    589      1.5   dsainty 
    590      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    591      1.5   dsainty 
    592      1.5   dsainty 	if (sc->sc_dying)
    593      1.5   dsainty 		return EIO;
    594      1.5   dsainty 
    595      1.5   dsainty 	return ENXIO;
    596      1.5   dsainty }
    597      1.5   dsainty 
    598      1.5   dsainty static void
    599      1.5   dsainty ubt_event_cb(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
    600      1.5   dsainty {
    601      1.5   dsainty 	struct ubt_softc *sc = h;
    602      1.5   dsainty 	void *buf;
    603      1.5   dsainty 	u_int32_t size;
    604      1.5   dsainty 
    605      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p status=%s\n", __func__, sc,
    606      1.5   dsainty 		    usbd_errstr(status)));
    607      1.5   dsainty 
    608      1.5   dsainty 	if (status != USBD_NORMAL_COMPLETION || sc->sc_dying ||
    609      1.5   dsainty 	    sc->sc_child == NULL)
    610      1.5   dsainty 		return;
    611      1.5   dsainty 
    612      1.5   dsainty 	usbd_get_xfer_status(xfer, NULL, &buf, &size, NULL);
    613      1.5   dsainty 
    614      1.5   dsainty 	sc->sc_cb->bt_recveventdata(sc->sc_child, buf, (size_t)size);
    615      1.5   dsainty }
    616      1.5   dsainty 
    617      1.5   dsainty static void
    618      1.5   dsainty ubt_aclrd_request(struct ubt_softc *sc)
    619      1.5   dsainty {
    620      1.5   dsainty 	usbd_status status;
    621      1.5   dsainty 	int s;
    622      1.5   dsainty 
    623      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
    624      1.5   dsainty 
    625      1.5   dsainty 	if (sc->sc_dying)
    626      1.5   dsainty 		return;
    627      1.5   dsainty 
    628      1.5   dsainty 	s = splusb();
    629      1.5   dsainty 	if (sc->sc_aclrd_running) {
    630      1.5   dsainty 		splx(s);
    631      1.5   dsainty 		return;
    632      1.5   dsainty 	}
    633      1.5   dsainty 	sc->sc_aclrd_running = 1;
    634      1.5   dsainty 	splx(s);
    635      1.6   dsainty 
    636      1.5   dsainty 	usbd_setup_xfer(sc->sc_aclrd_xfer, sc->sc_aclrd_pipe,
    637      1.5   dsainty 			sc, sc->sc_aclrd_buf, BTHCI_ACL_DATA_MAX_LEN,
    638      1.5   dsainty 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
    639      1.5   dsainty 			USBD_NO_TIMEOUT, ubt_aclrd_cb);
    640      1.5   dsainty 
    641      1.5   dsainty 	status = usbd_transfer(sc->sc_aclrd_xfer);
    642  1.7.2.1     skrll 
    643  1.7.2.1     skrll 	/* Cancellation is normal on device shutdown */
    644  1.7.2.1     skrll 	if (status == USBD_IN_PROGRESS || status == USBD_CANCELLED)
    645      1.5   dsainty 		return;
    646      1.5   dsainty 
    647      1.5   dsainty 	DPRINTFN(1,("%s: read request failed: %s\n", __func__,
    648      1.5   dsainty 		    usbd_errstr(status)));
    649      1.5   dsainty 
    650      1.5   dsainty 	sc->sc_aclrd_running = 0;
    651  1.7.2.1     skrll 	/* XXX now what!? */
    652      1.5   dsainty }
    653      1.5   dsainty 
    654      1.5   dsainty static void
    655      1.5   dsainty ubt_aclrd_cb(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
    656      1.5   dsainty {
    657      1.5   dsainty 	struct ubt_softc *sc = h;
    658      1.5   dsainty 	void *buf;
    659      1.5   dsainty 	u_int32_t size;
    660      1.5   dsainty 
    661      1.5   dsainty 	DPRINTFN(1,("%s: sc=%p status=%s\n", __func__, sc,
    662      1.5   dsainty 		    usbd_errstr(status)));
    663      1.5   dsainty 
    664      1.5   dsainty 	sc->sc_aclrd_running = 0;
    665      1.5   dsainty 
    666      1.5   dsainty 	if (status != USBD_NORMAL_COMPLETION || sc->sc_dying ||
    667      1.5   dsainty 	    sc->sc_child == NULL)
    668      1.5   dsainty 		return;
    669      1.5   dsainty 
    670      1.5   dsainty 	usbd_get_xfer_status(xfer, NULL, &buf, &size, NULL);
    671      1.5   dsainty 
    672      1.5   dsainty 	sc->sc_cb->bt_recvacldata(sc->sc_child, buf, (size_t)size);
    673      1.5   dsainty 
    674  1.7.2.1     skrll 	/* Re-issue the request */
    675  1.7.2.1     skrll 	if (!sc->sc_dying)
    676      1.5   dsainty 		ubt_aclrd_request(sc);
    677      1.5   dsainty }
    678      1.5   dsainty 
    679      1.5   dsainty static int
    680      1.5   dsainty ubt_splraise(void)
    681      1.5   dsainty {
    682      1.5   dsainty 	return splusb();
    683      1.1  augustss }
    684