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