xref: /netbsd/sys/dev/usb/ubt.c (revision c4a72b64)
1 /*	$NetBSD: ubt.c,v 1.3 2002/08/24 17:31:19 augustss 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@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.3 2002/08/24 17:31:19 augustss 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 #define UBT_DEBUG
62 
63 #ifdef UBT_DEBUG
64 #define DPRINTF(x)	if (ubtdebug) logprintf x
65 #define DPRINTFN(n,x)	if (ubtdebug>(n)) logprintf x
66 int	ubtdebug = 1;
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71 
72 /*
73  * Protocol related definitions
74  */
75 
76 struct ubt_softc {
77  	USBBASEDEVICE		sc_dev;
78 	usbd_device_handle	sc_udev;
79 	usbd_interface_handle	sc_ctl_iface;
80 	usbd_interface_handle	sc_isoc_iface;
81 
82 	int			sc_rd_addr;
83 	usbd_pipe_handle	sc_rd_pipe;
84 
85 	int			sc_wr_addr;
86 	usbd_pipe_handle	sc_wr_pipe;
87 
88 	struct device		*sc_child;
89 	int			sc_refcnt;
90 	char			sc_dying;
91 };
92 
93 USB_DECLARE_DRIVER(ubt);
94 
95 USB_MATCH(ubt)
96 {
97 	USB_MATCH_START(ubt, uaa);
98 	usb_interface_descriptor_t *id;
99 
100 	DPRINTFN(50,("ubt_match\n"));
101 
102 	if (uaa->iface == NULL)
103 		return (UMATCH_NONE);
104 
105 	id = usbd_get_interface_descriptor(uaa->iface);
106 	if (id != NULL &&
107 	    id->bInterfaceClass == UICLASS_WIRELESS &&
108 	    id->bInterfaceSubClass == UISUBCLASS_RF &&
109 	    id->bInterfaceProtocol == UIPROTO_BLUETOOTH)
110 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
111 	return (UMATCH_NONE);
112 }
113 
114 USB_ATTACH(ubt)
115 {
116 	USB_ATTACH_START(ubt, sc, uaa);
117 	usbd_device_handle	dev = uaa->device;
118 	usbd_interface_handle	iface = uaa->iface;
119 	struct bt_attach_args	bt;
120 	usb_interface_descriptor_t *id;
121 	char			devinfo[1024];
122 	usb_endpoint_descriptor_t *ed;
123 	u_int8_t		epcount;
124 	int			i;
125 
126 	DPRINTFN(10,("ubt_attach: sc=%p\n", sc));
127 
128 	usbd_devinfo(dev, 0, devinfo);
129 	USB_ATTACH_SETUP;
130 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
131 
132 	sc->sc_udev = dev;
133 	sc->sc_ctl_iface = iface;
134 
135 	/*
136 	 * The control interface comes before the isoc interface
137 	 * according to the spec, so we find it first.
138 	 */
139 	epcount = 0;
140 	(void)usbd_endpoint_count(iface, &epcount);
141 	sc->sc_rd_addr = -1;
142 	sc->sc_wr_addr = -1;
143 	for (i = 0; i < epcount; i++) {
144 		ed = usbd_interface2endpoint_descriptor(iface, i);
145 		if (ed == NULL) {
146 			printf("%s: couldn't get ep %d\n",
147 			    USBDEVNAME(sc->sc_dev), i);
148 			USB_ATTACH_ERROR_RETURN;
149 		}
150 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
151 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
152 			sc->sc_rd_addr = ed->bEndpointAddress;
153 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
154 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
155 			sc->sc_wr_addr = ed->bEndpointAddress;
156 		}
157 	}
158 #if 0
159 	if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
160 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
161 		USB_ATTACH_ERROR_RETURN;
162 	}
163 #endif
164 
165 	/* XXX works because isoc comes after ctl */
166 	/* Grab isoc interface as well. */
167 	for (i = 0; i < uaa->nifaces; i++) {
168 		if (uaa->ifaces[i] == NULL)
169 			continue;
170 		id = usbd_get_interface_descriptor(uaa->ifaces[i]);
171 		if (id != NULL &&
172 		    id->bInterfaceClass == UICLASS_WIRELESS &&
173 		    id->bInterfaceSubClass == UISUBCLASS_RF &&
174 		    id->bInterfaceProtocol == UIPROTO_BLUETOOTH) {
175 			sc->sc_isoc_iface = uaa->ifaces[i];
176 			uaa->ifaces[i] = NULL;
177 		}
178 	}
179 
180 	printf("%s: has%s isoc data\n", USBDEVNAME(sc->sc_dev),
181 	       sc->sc_isoc_iface != NULL ? "" : " no");
182 
183 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
184 			   USBDEV(sc->sc_dev));
185 
186 	sc->sc_child = config_found(self, &bt, bt_print);
187 
188 	USB_ATTACH_SUCCESS_RETURN;
189 }
190 
191 USB_DETACH(ubt)
192 {
193 	USB_DETACH_START(ubt, sc);
194 	int s;
195 	int rv = 0;
196 
197 	DPRINTF(("ubt_detach: sc=%p flags=%d\n", sc, flags));
198 
199 	sc->sc_dying = 1;
200 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
201 	if (sc->sc_rd_pipe != NULL) {
202 		usbd_abort_pipe(sc->sc_rd_pipe);
203 		usbd_close_pipe(sc->sc_rd_pipe);
204 		sc->sc_rd_pipe = NULL;
205 	}
206 	if (sc->sc_wr_pipe != NULL) {
207 		usbd_abort_pipe(sc->sc_wr_pipe);
208 		usbd_close_pipe(sc->sc_wr_pipe);
209 		sc->sc_wr_pipe = NULL;
210 	}
211 #if 0
212 	wakeup(&sc->sc_rd_count);
213 #endif
214 
215 	s = splusb();
216 	if (--sc->sc_refcnt >= 0) {
217 		/* Wait for processes to go away. */
218 		usb_detach_wait(USBDEV(sc->sc_dev));
219 	}
220 	splx(s);
221 
222 	if (sc->sc_child != NULL) {
223 		rv = config_detach(sc->sc_child, flags);
224 		sc->sc_child = NULL;
225 	}
226 
227 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
228 			   USBDEV(sc->sc_dev));
229 
230 	return (rv);
231 }
232 
233 int
234 ubt_activate(device_ptr_t self, enum devact act)
235 {
236 	struct ubt_softc *sc = (struct ubt_softc *)self;
237 	int error = 0;
238 
239 	switch (act) {
240 	case DVACT_ACTIVATE:
241 		return (EOPNOTSUPP);
242 		break;
243 
244 	case DVACT_DEACTIVATE:
245 		sc->sc_dying = 1;
246 		if (sc->sc_child != NULL)
247 			error = config_deactivate(sc->sc_child);
248 		break;
249 	}
250 	return (error);
251 }
252