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