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