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