Home | History | Annotate | Line # | Download | only in usb
ubt.c revision 1.4
      1 /*	$NetBSD: ubt.c,v 1.4 2003/01/05 03:48:18 dsainty Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.4 2003/01/05 03:48:18 dsainty Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #include <sys/lock.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/conf.h>
     49 #include <sys/file.h>
     50 #include <sys/poll.h>
     51 #include <sys/select.h>
     52 #include <sys/proc.h>
     53 
     54 #include <dev/usb/usb.h>
     55 #include <dev/usb/usbdi.h>
     56 #include <dev/usb/usbdi_util.h>
     57 #include <dev/usb/usbdevs.h>
     58 
     59 #include <dev/bluetooth/bluetooth.h>
     60 
     61 #ifdef UBT_DEBUG
     62 #define DPRINTF(x)	if (ubtdebug) logprintf x
     63 #define DPRINTFN(n,x)	if (ubtdebug>(n)) logprintf x
     64 int	ubtdebug = 1;
     65 #else
     66 #define DPRINTF(x)
     67 #define DPRINTFN(n,x)
     68 #endif
     69 
     70 /*
     71  * Protocol related definitions
     72  */
     73 
     74 struct ubt_softc {
     75  	USBBASEDEVICE		sc_dev;
     76 	usbd_device_handle	sc_udev;
     77 	usbd_interface_handle	sc_ctl_iface;
     78 	usbd_interface_handle	sc_isoc_iface;
     79 
     80 	int			sc_rd_addr;
     81 	usbd_pipe_handle	sc_rd_pipe;
     82 
     83 	int			sc_wr_addr;
     84 	usbd_pipe_handle	sc_wr_pipe;
     85 
     86 	struct device		*sc_child;
     87 	int			sc_refcnt;
     88 	char			sc_dying;
     89 };
     90 
     91 USB_DECLARE_DRIVER(ubt);
     92 
     93 USB_MATCH(ubt)
     94 {
     95 	USB_MATCH_START(ubt, uaa);
     96 	usb_interface_descriptor_t *id;
     97 
     98 	DPRINTFN(50,("ubt_match\n"));
     99 
    100 	if (uaa->iface == NULL)
    101 		return (UMATCH_NONE);
    102 
    103 	id = usbd_get_interface_descriptor(uaa->iface);
    104 	if (id != NULL &&
    105 	    id->bInterfaceClass == UICLASS_WIRELESS &&
    106 	    id->bInterfaceSubClass == UISUBCLASS_RF &&
    107 	    id->bInterfaceProtocol == UIPROTO_BLUETOOTH)
    108 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    109 	return (UMATCH_NONE);
    110 }
    111 
    112 USB_ATTACH(ubt)
    113 {
    114 	USB_ATTACH_START(ubt, sc, uaa);
    115 	usbd_device_handle	dev = uaa->device;
    116 	usbd_interface_handle	iface = uaa->iface;
    117 	struct bt_attach_args	bt;
    118 	usb_interface_descriptor_t *id;
    119 	char			devinfo[1024];
    120 	usb_endpoint_descriptor_t *ed;
    121 	u_int8_t		epcount;
    122 	int			i;
    123 
    124 	DPRINTFN(10,("ubt_attach: sc=%p\n", sc));
    125 
    126 	usbd_devinfo(dev, 0, devinfo);
    127 	USB_ATTACH_SETUP;
    128 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    129 
    130 	sc->sc_udev = dev;
    131 	sc->sc_ctl_iface = iface;
    132 
    133 	/*
    134 	 * The control interface comes before the isoc interface
    135 	 * according to the spec, so we find it first.
    136 	 */
    137 	epcount = 0;
    138 	(void)usbd_endpoint_count(iface, &epcount);
    139 	sc->sc_rd_addr = -1;
    140 	sc->sc_wr_addr = -1;
    141 	for (i = 0; i < epcount; i++) {
    142 		ed = usbd_interface2endpoint_descriptor(iface, i);
    143 		if (ed == NULL) {
    144 			printf("%s: couldn't get ep %d\n",
    145 			    USBDEVNAME(sc->sc_dev), i);
    146 			USB_ATTACH_ERROR_RETURN;
    147 		}
    148 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    149 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    150 			sc->sc_rd_addr = ed->bEndpointAddress;
    151 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    152 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    153 			sc->sc_wr_addr = ed->bEndpointAddress;
    154 		}
    155 	}
    156 #if 0
    157 	if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
    158 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    159 		USB_ATTACH_ERROR_RETURN;
    160 	}
    161 #endif
    162 
    163 	/* XXX works because isoc comes after ctl */
    164 	/* Grab isoc interface as well. */
    165 	for (i = 0; i < uaa->nifaces; i++) {
    166 		if (uaa->ifaces[i] == NULL)
    167 			continue;
    168 		id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    169 		if (id != NULL &&
    170 		    id->bInterfaceClass == UICLASS_WIRELESS &&
    171 		    id->bInterfaceSubClass == UISUBCLASS_RF &&
    172 		    id->bInterfaceProtocol == UIPROTO_BLUETOOTH) {
    173 			sc->sc_isoc_iface = uaa->ifaces[i];
    174 			uaa->ifaces[i] = NULL;
    175 		}
    176 	}
    177 
    178 	printf("%s: has%s isoc data\n", USBDEVNAME(sc->sc_dev),
    179 	       sc->sc_isoc_iface != NULL ? "" : " no");
    180 
    181 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    182 			   USBDEV(sc->sc_dev));
    183 
    184 	sc->sc_child = config_found(self, &bt, bt_print);
    185 
    186 	USB_ATTACH_SUCCESS_RETURN;
    187 }
    188 
    189 USB_DETACH(ubt)
    190 {
    191 	USB_DETACH_START(ubt, sc);
    192 	int s;
    193 	int rv = 0;
    194 
    195 	DPRINTF(("ubt_detach: sc=%p flags=%d\n", sc, flags));
    196 
    197 	sc->sc_dying = 1;
    198 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    199 	if (sc->sc_rd_pipe != NULL) {
    200 		usbd_abort_pipe(sc->sc_rd_pipe);
    201 		usbd_close_pipe(sc->sc_rd_pipe);
    202 		sc->sc_rd_pipe = NULL;
    203 	}
    204 	if (sc->sc_wr_pipe != NULL) {
    205 		usbd_abort_pipe(sc->sc_wr_pipe);
    206 		usbd_close_pipe(sc->sc_wr_pipe);
    207 		sc->sc_wr_pipe = NULL;
    208 	}
    209 #if 0
    210 	wakeup(&sc->sc_rd_count);
    211 #endif
    212 
    213 	s = splusb();
    214 	if (--sc->sc_refcnt >= 0) {
    215 		/* Wait for processes to go away. */
    216 		usb_detach_wait(USBDEV(sc->sc_dev));
    217 	}
    218 	splx(s);
    219 
    220 	if (sc->sc_child != NULL) {
    221 		rv = config_detach(sc->sc_child, flags);
    222 		sc->sc_child = NULL;
    223 	}
    224 
    225 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    226 			   USBDEV(sc->sc_dev));
    227 
    228 	return (rv);
    229 }
    230 
    231 int
    232 ubt_activate(device_ptr_t self, enum devact act)
    233 {
    234 	struct ubt_softc *sc = (struct ubt_softc *)self;
    235 	int error = 0;
    236 
    237 	switch (act) {
    238 	case DVACT_ACTIVATE:
    239 		return (EOPNOTSUPP);
    240 		break;
    241 
    242 	case DVACT_DEACTIVATE:
    243 		sc->sc_dying = 1;
    244 		if (sc->sc_child != NULL)
    245 			error = config_deactivate(sc->sc_child);
    246 		break;
    247 	}
    248 	return (error);
    249 }
    250