ubt.c revision 1.3.2.4 1 1.3.2.4 thorpej /* $NetBSD: ubt.c,v 1.3.2.4 2003/01/15 18:44:24 thorpej Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*
4 1.3.2.3 thorpej * Copyright (c) 2002, 2003 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.3 thorpej * by Lennart Augustsson (lennart (at) augustsson.net) and
9 1.3.2.3 thorpej * David Sainty (David.Sainty (at) dtsp.co.nz).
10 1.3.2.2 nathanw *
11 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
12 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
13 1.3.2.2 nathanw * are met:
14 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
15 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
16 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
18 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
19 1.3.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
20 1.3.2.2 nathanw * must display the following acknowledgement:
21 1.3.2.2 nathanw * This product includes software developed by the NetBSD
22 1.3.2.2 nathanw * Foundation, Inc. and its contributors.
23 1.3.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.3.2.2 nathanw * contributors may be used to endorse or promote products derived
25 1.3.2.2 nathanw * from this software without specific prior written permission.
26 1.3.2.2 nathanw *
27 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.3.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.3.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.3.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.3.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.3.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.3.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.3.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.3.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.3.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.3.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
38 1.3.2.2 nathanw */
39 1.3.2.2 nathanw
40 1.3.2.2 nathanw #include <sys/cdefs.h>
41 1.3.2.4 thorpej __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.3.2.4 2003/01/15 18:44:24 thorpej Exp $");
42 1.3.2.2 nathanw
43 1.3.2.2 nathanw #include <sys/param.h>
44 1.3.2.2 nathanw #include <sys/systm.h>
45 1.3.2.2 nathanw #include <sys/kernel.h>
46 1.3.2.3 thorpej #include <sys/malloc.h>
47 1.3.2.2 nathanw #include <sys/device.h>
48 1.3.2.2 nathanw #include <sys/lock.h>
49 1.3.2.2 nathanw #include <sys/ioctl.h>
50 1.3.2.2 nathanw #include <sys/conf.h>
51 1.3.2.2 nathanw #include <sys/file.h>
52 1.3.2.2 nathanw #include <sys/poll.h>
53 1.3.2.2 nathanw #include <sys/select.h>
54 1.3.2.2 nathanw #include <sys/proc.h>
55 1.3.2.2 nathanw
56 1.3.2.3 thorpej #include <dev/usb/ubtreg.h>
57 1.3.2.2 nathanw #include <dev/usb/usb.h>
58 1.3.2.2 nathanw #include <dev/usb/usbdi.h>
59 1.3.2.2 nathanw #include <dev/usb/usbdi_util.h>
60 1.3.2.2 nathanw #include <dev/usb/usbdevs.h>
61 1.3.2.2 nathanw
62 1.3.2.2 nathanw #include <dev/bluetooth/bluetooth.h>
63 1.3.2.2 nathanw
64 1.3.2.2 nathanw #ifdef UBT_DEBUG
65 1.3.2.2 nathanw #define DPRINTF(x) if (ubtdebug) logprintf x
66 1.3.2.2 nathanw #define DPRINTFN(n,x) if (ubtdebug>(n)) logprintf x
67 1.3.2.3 thorpej int ubtdebug = 99;
68 1.3.2.2 nathanw #else
69 1.3.2.2 nathanw #define DPRINTF(x)
70 1.3.2.2 nathanw #define DPRINTFN(n,x)
71 1.3.2.2 nathanw #endif
72 1.3.2.2 nathanw
73 1.3.2.2 nathanw /*
74 1.3.2.2 nathanw * Protocol related definitions
75 1.3.2.2 nathanw */
76 1.3.2.2 nathanw
77 1.3.2.2 nathanw struct ubt_softc {
78 1.3.2.2 nathanw USBBASEDEVICE sc_dev;
79 1.3.2.2 nathanw usbd_device_handle sc_udev;
80 1.3.2.2 nathanw usbd_interface_handle sc_ctl_iface;
81 1.3.2.2 nathanw usbd_interface_handle sc_isoc_iface;
82 1.3.2.2 nathanw
83 1.3.2.3 thorpej /* Control */
84 1.3.2.3 thorpej usbd_pipe_handle sc_ctl_pipe;
85 1.3.2.3 thorpej usbd_xfer_handle sc_ctl_xfer;
86 1.3.2.3 thorpej u_int8_t *sc_ctl_buf;
87 1.3.2.3 thorpej
88 1.3.2.3 thorpej /* Events */
89 1.3.2.3 thorpej int sc_evt_addr;
90 1.3.2.3 thorpej usbd_pipe_handle sc_evt_pipe;
91 1.3.2.3 thorpej usbd_xfer_handle sc_evt_xfer;
92 1.3.2.3 thorpej u_int8_t *sc_evt_buf;
93 1.3.2.3 thorpej
94 1.3.2.3 thorpej /* ACL data (in) */
95 1.3.2.3 thorpej int sc_aclrd_addr;
96 1.3.2.3 thorpej usbd_pipe_handle sc_aclrd_pipe;
97 1.3.2.3 thorpej usbd_xfer_handle sc_aclrd_xfer;
98 1.3.2.3 thorpej u_int8_t *sc_aclrd_buf;
99 1.3.2.3 thorpej int sc_aclrd_running;
100 1.3.2.3 thorpej
101 1.3.2.3 thorpej /* ACL data (out) */
102 1.3.2.3 thorpej int sc_aclwr_addr;
103 1.3.2.3 thorpej usbd_pipe_handle sc_aclwr_pipe;
104 1.3.2.3 thorpej usbd_xfer_handle sc_aclwr_xfer;
105 1.3.2.3 thorpej u_int8_t *sc_aclwr_buf;
106 1.3.2.2 nathanw
107 1.3.2.2 nathanw struct device *sc_child;
108 1.3.2.3 thorpej struct btframe_callback_methods const *sc_cb;
109 1.3.2.3 thorpej
110 1.3.2.3 thorpej unsigned int sc_blocked;
111 1.3.2.3 thorpej
112 1.3.2.2 nathanw int sc_refcnt;
113 1.3.2.2 nathanw char sc_dying;
114 1.3.2.2 nathanw };
115 1.3.2.2 nathanw
116 1.3.2.3 thorpej static int ubt_open(void *h, int flag, int mode, usb_proc_ptr p);
117 1.3.2.3 thorpej static int ubt_close(void *h, int flag, int mode, usb_proc_ptr p);
118 1.3.2.3 thorpej
119 1.3.2.4 thorpej static u_int8_t* ubt_alloc_control(void*, size_t, struct btframe_buffer**);
120 1.3.2.4 thorpej static int ubt_send_control(void*, struct btframe_buffer*, size_t);
121 1.3.2.4 thorpej
122 1.3.2.4 thorpej static u_int8_t* ubt_alloc_acldata(void*, size_t, struct btframe_buffer**);
123 1.3.2.4 thorpej static int ubt_send_acldata(void*, struct btframe_buffer*, size_t);
124 1.3.2.4 thorpej
125 1.3.2.4 thorpej static u_int8_t* ubt_alloc_scodata(void*, size_t, struct btframe_buffer**);
126 1.3.2.4 thorpej static int ubt_send_scodata(void*, struct btframe_buffer*, size_t);
127 1.3.2.3 thorpej
128 1.3.2.3 thorpej static int ubt_splraise(void);
129 1.3.2.3 thorpej static unsigned int ubt_blockcb(void *h, unsigned int cbblocks);
130 1.3.2.3 thorpej static unsigned int ubt_unblockcb(void *h, unsigned int cbblocks);
131 1.3.2.3 thorpej
132 1.3.2.3 thorpej static void ubt_event_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
133 1.3.2.3 thorpej static void ubt_aclrd_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
134 1.3.2.3 thorpej static void ubt_aclrd_request(struct ubt_softc *);
135 1.3.2.3 thorpej
136 1.3.2.3 thorpej static struct btframe_methods const ubt_methods = {
137 1.3.2.4 thorpej ubt_open, ubt_close,
138 1.3.2.4 thorpej {ubt_alloc_control, ubt_send_control},
139 1.3.2.4 thorpej {ubt_alloc_acldata, ubt_send_acldata},
140 1.3.2.4 thorpej {ubt_alloc_scodata, ubt_send_scodata},
141 1.3.2.3 thorpej ubt_splraise, ubt_blockcb, ubt_unblockcb
142 1.3.2.3 thorpej };
143 1.3.2.3 thorpej
144 1.3.2.2 nathanw USB_DECLARE_DRIVER(ubt);
145 1.3.2.2 nathanw
146 1.3.2.2 nathanw USB_MATCH(ubt)
147 1.3.2.2 nathanw {
148 1.3.2.2 nathanw USB_MATCH_START(ubt, uaa);
149 1.3.2.2 nathanw usb_interface_descriptor_t *id;
150 1.3.2.2 nathanw
151 1.3.2.2 nathanw DPRINTFN(50,("ubt_match\n"));
152 1.3.2.2 nathanw
153 1.3.2.2 nathanw if (uaa->iface == NULL)
154 1.3.2.2 nathanw return (UMATCH_NONE);
155 1.3.2.2 nathanw
156 1.3.2.2 nathanw id = usbd_get_interface_descriptor(uaa->iface);
157 1.3.2.2 nathanw if (id != NULL &&
158 1.3.2.2 nathanw id->bInterfaceClass == UICLASS_WIRELESS &&
159 1.3.2.2 nathanw id->bInterfaceSubClass == UISUBCLASS_RF &&
160 1.3.2.2 nathanw id->bInterfaceProtocol == UIPROTO_BLUETOOTH)
161 1.3.2.2 nathanw return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
162 1.3.2.2 nathanw return (UMATCH_NONE);
163 1.3.2.2 nathanw }
164 1.3.2.2 nathanw
165 1.3.2.2 nathanw USB_ATTACH(ubt)
166 1.3.2.2 nathanw {
167 1.3.2.2 nathanw USB_ATTACH_START(ubt, sc, uaa);
168 1.3.2.2 nathanw usbd_device_handle dev = uaa->device;
169 1.3.2.2 nathanw usbd_interface_handle iface = uaa->iface;
170 1.3.2.2 nathanw struct bt_attach_args bt;
171 1.3.2.3 thorpej usb_interface_descriptor_t const *id;
172 1.3.2.2 nathanw char devinfo[1024];
173 1.3.2.3 thorpej usb_endpoint_descriptor_t const *ed;
174 1.3.2.2 nathanw u_int8_t epcount;
175 1.3.2.2 nathanw int i;
176 1.3.2.2 nathanw
177 1.3.2.2 nathanw DPRINTFN(10,("ubt_attach: sc=%p\n", sc));
178 1.3.2.2 nathanw
179 1.3.2.2 nathanw usbd_devinfo(dev, 0, devinfo);
180 1.3.2.2 nathanw USB_ATTACH_SETUP;
181 1.3.2.2 nathanw printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
182 1.3.2.2 nathanw
183 1.3.2.2 nathanw sc->sc_udev = dev;
184 1.3.2.2 nathanw sc->sc_ctl_iface = iface;
185 1.3.2.2 nathanw
186 1.3.2.3 thorpej /*
187 1.3.2.2 nathanw * The control interface comes before the isoc interface
188 1.3.2.2 nathanw * according to the spec, so we find it first.
189 1.3.2.2 nathanw */
190 1.3.2.2 nathanw epcount = 0;
191 1.3.2.2 nathanw (void)usbd_endpoint_count(iface, &epcount);
192 1.3.2.3 thorpej
193 1.3.2.3 thorpej sc->sc_evt_addr = -1;
194 1.3.2.3 thorpej sc->sc_aclrd_addr = -1;
195 1.3.2.3 thorpej sc->sc_aclwr_addr = -1;
196 1.3.2.3 thorpej
197 1.3.2.2 nathanw for (i = 0; i < epcount; i++) {
198 1.3.2.2 nathanw ed = usbd_interface2endpoint_descriptor(iface, i);
199 1.3.2.2 nathanw if (ed == NULL) {
200 1.3.2.2 nathanw printf("%s: couldn't get ep %d\n",
201 1.3.2.2 nathanw USBDEVNAME(sc->sc_dev), i);
202 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
203 1.3.2.2 nathanw }
204 1.3.2.3 thorpej
205 1.3.2.3 thorpej DPRINTFN(10, ("%s: addr=%d attr=%d\n", __func__,
206 1.3.2.3 thorpej (int)ed->bEndpointAddress,
207 1.3.2.3 thorpej (int)ed->bmAttributes));
208 1.3.2.3 thorpej
209 1.3.2.2 nathanw if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
210 1.3.2.3 thorpej UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
211 1.3.2.3 thorpej sc->sc_evt_addr = ed->bEndpointAddress;
212 1.3.2.3 thorpej } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
213 1.3.2.3 thorpej UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
214 1.3.2.3 thorpej sc->sc_aclrd_addr = ed->bEndpointAddress;
215 1.3.2.2 nathanw } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
216 1.3.2.2 nathanw UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
217 1.3.2.3 thorpej sc->sc_aclwr_addr = ed->bEndpointAddress;
218 1.3.2.2 nathanw }
219 1.3.2.2 nathanw }
220 1.3.2.3 thorpej
221 1.3.2.3 thorpej if (sc->sc_evt_addr == -1 ||
222 1.3.2.3 thorpej sc->sc_aclrd_addr == -1 || sc->sc_aclwr_addr == -1) {
223 1.3.2.2 nathanw printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
224 1.3.2.2 nathanw USB_ATTACH_ERROR_RETURN;
225 1.3.2.2 nathanw }
226 1.3.2.2 nathanw
227 1.3.2.2 nathanw /* XXX works because isoc comes after ctl */
228 1.3.2.2 nathanw /* Grab isoc interface as well. */
229 1.3.2.2 nathanw for (i = 0; i < uaa->nifaces; i++) {
230 1.3.2.2 nathanw if (uaa->ifaces[i] == NULL)
231 1.3.2.2 nathanw continue;
232 1.3.2.2 nathanw id = usbd_get_interface_descriptor(uaa->ifaces[i]);
233 1.3.2.2 nathanw if (id != NULL &&
234 1.3.2.2 nathanw id->bInterfaceClass == UICLASS_WIRELESS &&
235 1.3.2.2 nathanw id->bInterfaceSubClass == UISUBCLASS_RF &&
236 1.3.2.2 nathanw id->bInterfaceProtocol == UIPROTO_BLUETOOTH) {
237 1.3.2.2 nathanw sc->sc_isoc_iface = uaa->ifaces[i];
238 1.3.2.2 nathanw uaa->ifaces[i] = NULL;
239 1.3.2.2 nathanw }
240 1.3.2.2 nathanw }
241 1.3.2.2 nathanw
242 1.3.2.2 nathanw printf("%s: has%s isoc data\n", USBDEVNAME(sc->sc_dev),
243 1.3.2.2 nathanw sc->sc_isoc_iface != NULL ? "" : " no");
244 1.3.2.2 nathanw
245 1.3.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
246 1.3.2.2 nathanw USBDEV(sc->sc_dev));
247 1.3.2.2 nathanw
248 1.3.2.3 thorpej bt.bt_methods = &ubt_methods;
249 1.3.2.3 thorpej bt.bt_cb = &sc->sc_cb;
250 1.3.2.3 thorpej
251 1.3.2.2 nathanw sc->sc_child = config_found(self, &bt, bt_print);
252 1.3.2.2 nathanw
253 1.3.2.2 nathanw USB_ATTACH_SUCCESS_RETURN;
254 1.3.2.2 nathanw }
255 1.3.2.2 nathanw
256 1.3.2.3 thorpej static void
257 1.3.2.3 thorpej ubt_abortdealloc(struct ubt_softc *sc)
258 1.3.2.3 thorpej {
259 1.3.2.3 thorpej DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
260 1.3.2.3 thorpej
261 1.3.2.3 thorpej if (sc->sc_evt_pipe != NULL) {
262 1.3.2.3 thorpej usbd_abort_pipe(sc->sc_evt_pipe);
263 1.3.2.3 thorpej usbd_close_pipe(sc->sc_evt_pipe);
264 1.3.2.3 thorpej sc->sc_evt_pipe = NULL;
265 1.3.2.3 thorpej }
266 1.3.2.3 thorpej if (sc->sc_evt_buf != NULL) {
267 1.3.2.3 thorpej free(sc->sc_evt_buf, M_USBDEV);
268 1.3.2.3 thorpej sc->sc_evt_buf = NULL;
269 1.3.2.3 thorpej }
270 1.3.2.3 thorpej if (sc->sc_aclrd_pipe != NULL) {
271 1.3.2.3 thorpej usbd_abort_pipe(sc->sc_aclrd_pipe);
272 1.3.2.3 thorpej usbd_close_pipe(sc->sc_aclrd_pipe);
273 1.3.2.3 thorpej sc->sc_aclrd_pipe = NULL;
274 1.3.2.3 thorpej }
275 1.3.2.3 thorpej if (sc->sc_aclwr_pipe != NULL) {
276 1.3.2.3 thorpej usbd_abort_pipe(sc->sc_aclwr_pipe);
277 1.3.2.3 thorpej usbd_close_pipe(sc->sc_aclwr_pipe);
278 1.3.2.3 thorpej sc->sc_aclwr_pipe = NULL;
279 1.3.2.3 thorpej }
280 1.3.2.3 thorpej if (sc->sc_aclrd_xfer != NULL) {
281 1.3.2.3 thorpej usbd_free_xfer(sc->sc_aclrd_xfer);
282 1.3.2.3 thorpej sc->sc_aclrd_xfer = NULL;
283 1.3.2.3 thorpej sc->sc_aclrd_buf = NULL;
284 1.3.2.3 thorpej }
285 1.3.2.3 thorpej if (sc->sc_aclwr_xfer != NULL) {
286 1.3.2.3 thorpej usbd_free_xfer(sc->sc_aclwr_xfer);
287 1.3.2.3 thorpej sc->sc_aclwr_xfer = NULL;
288 1.3.2.3 thorpej sc->sc_aclwr_buf = NULL;
289 1.3.2.3 thorpej }
290 1.3.2.3 thorpej }
291 1.3.2.3 thorpej
292 1.3.2.2 nathanw USB_DETACH(ubt)
293 1.3.2.2 nathanw {
294 1.3.2.2 nathanw USB_DETACH_START(ubt, sc);
295 1.3.2.2 nathanw int s;
296 1.3.2.2 nathanw int rv = 0;
297 1.3.2.2 nathanw
298 1.3.2.3 thorpej DPRINTF(("%s: sc=%p flags=%d\n", __func__, sc, flags));
299 1.3.2.2 nathanw
300 1.3.2.2 nathanw sc->sc_dying = 1;
301 1.3.2.3 thorpej
302 1.3.2.2 nathanw /* Abort all pipes. Causes processes waiting for transfer to wake. */
303 1.3.2.3 thorpej ubt_abortdealloc(sc);
304 1.3.2.2 nathanw
305 1.3.2.3 thorpej DPRINTFN(1, ("%s: waiting for USB detach\n", __func__));
306 1.3.2.2 nathanw s = splusb();
307 1.3.2.2 nathanw if (--sc->sc_refcnt >= 0) {
308 1.3.2.2 nathanw /* Wait for processes to go away. */
309 1.3.2.2 nathanw usb_detach_wait(USBDEV(sc->sc_dev));
310 1.3.2.2 nathanw }
311 1.3.2.2 nathanw splx(s);
312 1.3.2.3 thorpej DPRINTFN(1, ("%s: USB detach complete\n", __func__));
313 1.3.2.2 nathanw
314 1.3.2.2 nathanw if (sc->sc_child != NULL) {
315 1.3.2.3 thorpej DPRINTFN(1, ("%s: waiting for child detach\n", __func__));
316 1.3.2.2 nathanw rv = config_detach(sc->sc_child, flags);
317 1.3.2.2 nathanw sc->sc_child = NULL;
318 1.3.2.3 thorpej DPRINTFN(1, ("%s: child detach complete\n", __func__));
319 1.3.2.2 nathanw }
320 1.3.2.2 nathanw
321 1.3.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
322 1.3.2.2 nathanw USBDEV(sc->sc_dev));
323 1.3.2.2 nathanw
324 1.3.2.3 thorpej DPRINTFN(1, ("%s: driver detached\n", __func__));
325 1.3.2.3 thorpej
326 1.3.2.2 nathanw return (rv);
327 1.3.2.2 nathanw }
328 1.3.2.2 nathanw
329 1.3.2.2 nathanw int
330 1.3.2.2 nathanw ubt_activate(device_ptr_t self, enum devact act)
331 1.3.2.2 nathanw {
332 1.3.2.2 nathanw struct ubt_softc *sc = (struct ubt_softc *)self;
333 1.3.2.2 nathanw int error = 0;
334 1.3.2.2 nathanw
335 1.3.2.2 nathanw switch (act) {
336 1.3.2.2 nathanw case DVACT_ACTIVATE:
337 1.3.2.2 nathanw return (EOPNOTSUPP);
338 1.3.2.2 nathanw break;
339 1.3.2.2 nathanw
340 1.3.2.2 nathanw case DVACT_DEACTIVATE:
341 1.3.2.2 nathanw sc->sc_dying = 1;
342 1.3.2.2 nathanw if (sc->sc_child != NULL)
343 1.3.2.2 nathanw error = config_deactivate(sc->sc_child);
344 1.3.2.2 nathanw break;
345 1.3.2.2 nathanw }
346 1.3.2.2 nathanw return (error);
347 1.3.2.2 nathanw }
348 1.3.2.3 thorpej
349 1.3.2.3 thorpej static int
350 1.3.2.3 thorpej ubt_open(void *h, int flag, int mode, usb_proc_ptr p)
351 1.3.2.3 thorpej {
352 1.3.2.3 thorpej struct ubt_softc *sc = h;
353 1.3.2.3 thorpej int error;
354 1.3.2.3 thorpej usbd_status err;
355 1.3.2.3 thorpej
356 1.3.2.3 thorpej DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
357 1.3.2.3 thorpej
358 1.3.2.3 thorpej sc->sc_evt_buf = malloc(BTHCI_EVENT_MAX_LEN, M_USBDEV, M_NOWAIT);
359 1.3.2.3 thorpej if (sc->sc_evt_buf == NULL) {
360 1.3.2.3 thorpej error = ENOMEM;
361 1.3.2.3 thorpej goto bad0;
362 1.3.2.3 thorpej }
363 1.3.2.3 thorpej
364 1.3.2.3 thorpej err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_evt_addr,
365 1.3.2.3 thorpej USBD_SHORT_XFER_OK, &sc->sc_evt_pipe,
366 1.3.2.3 thorpej sc, sc->sc_evt_buf, BTHCI_EVENT_MAX_LEN,
367 1.3.2.3 thorpej ubt_event_cb, UBT_EVENT_EP_INTERVAL);
368 1.3.2.3 thorpej if (err != USBD_NORMAL_COMPLETION) {
369 1.3.2.3 thorpej error = EIO;
370 1.3.2.3 thorpej goto bad1;
371 1.3.2.3 thorpej }
372 1.3.2.3 thorpej
373 1.3.2.3 thorpej err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_aclrd_addr,
374 1.3.2.3 thorpej 0, &sc->sc_aclrd_pipe);
375 1.3.2.3 thorpej if (err != USBD_NORMAL_COMPLETION) {
376 1.3.2.3 thorpej error = EIO;
377 1.3.2.3 thorpej goto bad2;
378 1.3.2.3 thorpej }
379 1.3.2.3 thorpej
380 1.3.2.3 thorpej err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_aclwr_addr,
381 1.3.2.3 thorpej 0, &sc->sc_aclwr_pipe);
382 1.3.2.3 thorpej if (err != USBD_NORMAL_COMPLETION) {
383 1.3.2.3 thorpej error = EIO;
384 1.3.2.3 thorpej goto bad3;
385 1.3.2.3 thorpej }
386 1.3.2.3 thorpej
387 1.3.2.3 thorpej sc->sc_ctl_xfer = usbd_alloc_xfer(sc->sc_udev);
388 1.3.2.3 thorpej if (sc->sc_ctl_xfer == NULL) {
389 1.3.2.3 thorpej error = ENOMEM;
390 1.3.2.3 thorpej goto bad4;
391 1.3.2.3 thorpej }
392 1.3.2.3 thorpej sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
393 1.3.2.3 thorpej if (sc->sc_aclrd_xfer == NULL) {
394 1.3.2.3 thorpej error = ENOMEM;
395 1.3.2.3 thorpej goto bad5;
396 1.3.2.3 thorpej }
397 1.3.2.3 thorpej sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
398 1.3.2.3 thorpej if (sc->sc_aclwr_xfer == NULL) {
399 1.3.2.3 thorpej error = ENOMEM;
400 1.3.2.3 thorpej goto bad6;
401 1.3.2.3 thorpej }
402 1.3.2.3 thorpej
403 1.3.2.3 thorpej /* Buffers */
404 1.3.2.3 thorpej sc->sc_ctl_buf = usbd_alloc_buffer(sc->sc_ctl_xfer,
405 1.3.2.3 thorpej BTHCI_COMMAND_MAX_LEN);
406 1.3.2.3 thorpej if (sc->sc_ctl_buf == NULL) {
407 1.3.2.3 thorpej error = ENOMEM;
408 1.3.2.3 thorpej goto bad7;
409 1.3.2.3 thorpej }
410 1.3.2.3 thorpej sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer,
411 1.3.2.3 thorpej BTHCI_ACL_DATA_MAX_LEN);
412 1.3.2.3 thorpej if (sc->sc_aclrd_buf == NULL) {
413 1.3.2.3 thorpej error = ENOMEM;
414 1.3.2.3 thorpej goto bad7;
415 1.3.2.3 thorpej }
416 1.3.2.3 thorpej sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer,
417 1.3.2.3 thorpej BTHCI_ACL_DATA_MAX_LEN);
418 1.3.2.3 thorpej if (sc->sc_aclwr_buf == NULL) {
419 1.3.2.3 thorpej error = ENOMEM;
420 1.3.2.3 thorpej goto bad7;
421 1.3.2.3 thorpej }
422 1.3.2.3 thorpej
423 1.3.2.3 thorpej /* Start reading */
424 1.3.2.3 thorpej ubt_aclrd_request(sc);
425 1.3.2.3 thorpej
426 1.3.2.3 thorpej return 0;
427 1.3.2.3 thorpej
428 1.3.2.3 thorpej bad7:
429 1.3.2.3 thorpej usbd_free_xfer(sc->sc_aclwr_xfer);
430 1.3.2.3 thorpej sc->sc_aclwr_xfer = NULL;
431 1.3.2.3 thorpej bad6:
432 1.3.2.3 thorpej usbd_free_xfer(sc->sc_aclrd_xfer);
433 1.3.2.3 thorpej sc->sc_aclrd_xfer = NULL;
434 1.3.2.3 thorpej bad5:
435 1.3.2.3 thorpej usbd_free_xfer(sc->sc_ctl_xfer);
436 1.3.2.3 thorpej sc->sc_ctl_xfer = NULL;
437 1.3.2.3 thorpej bad4:
438 1.3.2.3 thorpej usbd_close_pipe(sc->sc_aclwr_pipe);
439 1.3.2.3 thorpej sc->sc_aclwr_pipe = NULL;
440 1.3.2.3 thorpej bad3:
441 1.3.2.3 thorpej usbd_close_pipe(sc->sc_aclrd_pipe);
442 1.3.2.3 thorpej sc->sc_aclrd_pipe = NULL;
443 1.3.2.3 thorpej bad2:
444 1.3.2.3 thorpej usbd_close_pipe(sc->sc_evt_pipe);
445 1.3.2.3 thorpej sc->sc_evt_pipe = NULL;
446 1.3.2.3 thorpej bad1:
447 1.3.2.3 thorpej free(sc->sc_evt_buf, M_USBDEV);
448 1.3.2.3 thorpej sc->sc_evt_buf = NULL;
449 1.3.2.3 thorpej bad0:
450 1.3.2.3 thorpej return error;
451 1.3.2.3 thorpej }
452 1.3.2.3 thorpej
453 1.3.2.3 thorpej static int
454 1.3.2.3 thorpej ubt_close(void *h, int flag, int mode, usb_proc_ptr p)
455 1.3.2.3 thorpej {
456 1.3.2.3 thorpej struct ubt_softc *sc = h;
457 1.3.2.3 thorpej
458 1.3.2.3 thorpej DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
459 1.3.2.3 thorpej
460 1.3.2.3 thorpej ubt_abortdealloc(sc);
461 1.3.2.3 thorpej
462 1.3.2.3 thorpej return 0;
463 1.3.2.3 thorpej }
464 1.3.2.3 thorpej
465 1.3.2.4 thorpej static u_int8_t*
466 1.3.2.4 thorpej ubt_alloc_control(void *h, size_t len, struct btframe_buffer **buf)
467 1.3.2.4 thorpej {
468 1.3.2.4 thorpej struct ubt_softc *sc = h;
469 1.3.2.4 thorpej
470 1.3.2.4 thorpej /*
471 1.3.2.4 thorpej * We should be catching this earlier, but at the moment a
472 1.3.2.4 thorpej * user request can generate oversized allocations.
473 1.3.2.4 thorpej */
474 1.3.2.4 thorpej if (len > BTHCI_COMMAND_MAX_LEN)
475 1.3.2.4 thorpej return NULL;
476 1.3.2.4 thorpej
477 1.3.2.4 thorpej *buf = (struct btframe_buffer*)sc->sc_ctl_buf;
478 1.3.2.4 thorpej return sc->sc_ctl_buf;
479 1.3.2.4 thorpej }
480 1.3.2.4 thorpej
481 1.3.2.3 thorpej static int
482 1.3.2.4 thorpej ubt_send_control(void *h, struct btframe_buffer *buf, size_t len)
483 1.3.2.3 thorpej {
484 1.3.2.3 thorpej struct ubt_softc *sc = h;
485 1.3.2.3 thorpej usb_device_request_t req;
486 1.3.2.3 thorpej usbd_status status;
487 1.3.2.3 thorpej
488 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
489 1.3.2.3 thorpej
490 1.3.2.4 thorpej #ifdef DIAGNOSTIC
491 1.3.2.4 thorpej if ((u_int8_t*)buf != sc->sc_ctl_buf)
492 1.3.2.4 thorpej panic("ubt_control() called with wrong buffer");
493 1.3.2.4 thorpej #endif
494 1.3.2.4 thorpej
495 1.3.2.3 thorpej if (sc->sc_dying)
496 1.3.2.3 thorpej return EIO;
497 1.3.2.3 thorpej
498 1.3.2.3 thorpej if (len < BTHCI_COMMAND_MIN_LEN || len > BTHCI_COMMAND_MAX_LEN)
499 1.3.2.3 thorpej return EINVAL;
500 1.3.2.3 thorpej
501 1.3.2.3 thorpej sc->sc_refcnt++;
502 1.3.2.3 thorpej
503 1.3.2.3 thorpej memset(&req, 0, sizeof(req));
504 1.3.2.3 thorpej req.bmRequestType = UT_WRITE_CLASS_DEVICE;
505 1.3.2.3 thorpej USETW(req.wLength, len);
506 1.3.2.3 thorpej
507 1.3.2.3 thorpej usbd_setup_default_xfer(sc->sc_ctl_xfer,
508 1.3.2.3 thorpej sc->sc_udev,
509 1.3.2.3 thorpej sc,
510 1.3.2.3 thorpej USBD_DEFAULT_TIMEOUT,
511 1.3.2.3 thorpej &req, sc->sc_ctl_buf, len,
512 1.3.2.3 thorpej USBD_SYNCHRONOUS | USBD_NO_COPY, NULL);
513 1.3.2.3 thorpej
514 1.3.2.3 thorpej status = usbd_transfer(sc->sc_ctl_xfer);
515 1.3.2.3 thorpej
516 1.3.2.3 thorpej if (--sc->sc_refcnt < 0)
517 1.3.2.3 thorpej usb_detach_wakeup(USBDEV(sc->sc_dev));
518 1.3.2.3 thorpej
519 1.3.2.3 thorpej if (status != USBD_NORMAL_COMPLETION)
520 1.3.2.3 thorpej return EIO;
521 1.3.2.3 thorpej
522 1.3.2.3 thorpej return 0;
523 1.3.2.3 thorpej }
524 1.3.2.3 thorpej
525 1.3.2.4 thorpej static u_int8_t*
526 1.3.2.4 thorpej ubt_alloc_acldata(void *h, size_t len, struct btframe_buffer **buf)
527 1.3.2.4 thorpej {
528 1.3.2.4 thorpej struct ubt_softc *sc = h;
529 1.3.2.4 thorpej
530 1.3.2.4 thorpej /*
531 1.3.2.4 thorpej * We should be catching this earlier, but at the moment a
532 1.3.2.4 thorpej * user request can generate oversized allocations.
533 1.3.2.4 thorpej */
534 1.3.2.4 thorpej if (len > BTHCI_ACL_DATA_MAX_LEN)
535 1.3.2.4 thorpej return NULL;
536 1.3.2.4 thorpej
537 1.3.2.4 thorpej *buf = (struct btframe_buffer*)sc->sc_aclwr_buf;
538 1.3.2.4 thorpej return sc->sc_aclwr_buf;
539 1.3.2.4 thorpej }
540 1.3.2.4 thorpej
541 1.3.2.3 thorpej static int
542 1.3.2.4 thorpej ubt_send_acldata(void *h, struct btframe_buffer *buf, size_t len)
543 1.3.2.3 thorpej {
544 1.3.2.3 thorpej struct ubt_softc *sc = h;
545 1.3.2.3 thorpej usbd_status status;
546 1.3.2.3 thorpej
547 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
548 1.3.2.3 thorpej
549 1.3.2.4 thorpej #ifdef DIAGNOSTIC
550 1.3.2.4 thorpej if ((u_int8_t*)buf != sc->sc_aclwr_buf)
551 1.3.2.4 thorpej panic("ubt_sendacldata() called with wrong buffer");
552 1.3.2.4 thorpej #endif
553 1.3.2.4 thorpej
554 1.3.2.3 thorpej if (sc->sc_dying)
555 1.3.2.3 thorpej return EIO;
556 1.3.2.3 thorpej
557 1.3.2.3 thorpej if (len < BTHCI_ACL_DATA_MIN_LEN || len > BTHCI_ACL_DATA_MAX_LEN)
558 1.3.2.3 thorpej return EINVAL;
559 1.3.2.3 thorpej
560 1.3.2.3 thorpej sc->sc_refcnt++;
561 1.3.2.3 thorpej
562 1.3.2.3 thorpej usbd_setup_xfer(sc->sc_aclwr_xfer,
563 1.3.2.3 thorpej sc->sc_aclwr_pipe,
564 1.3.2.3 thorpej (usbd_private_handle)sc,
565 1.3.2.3 thorpej sc->sc_aclwr_buf, len,
566 1.3.2.3 thorpej USBD_SYNCHRONOUS | USBD_NO_COPY,
567 1.3.2.3 thorpej USBD_DEFAULT_TIMEOUT,
568 1.3.2.3 thorpej NULL);
569 1.3.2.3 thorpej
570 1.3.2.3 thorpej status = usbd_transfer(sc->sc_aclwr_xfer);
571 1.3.2.3 thorpej
572 1.3.2.3 thorpej if (--sc->sc_refcnt < 0)
573 1.3.2.3 thorpej usb_detach_wakeup(USBDEV(sc->sc_dev));
574 1.3.2.3 thorpej
575 1.3.2.3 thorpej if (status != USBD_NORMAL_COMPLETION)
576 1.3.2.3 thorpej return EIO;
577 1.3.2.3 thorpej
578 1.3.2.3 thorpej return 0;
579 1.3.2.3 thorpej }
580 1.3.2.3 thorpej
581 1.3.2.4 thorpej static u_int8_t*
582 1.3.2.4 thorpej ubt_alloc_scodata(void *h, size_t len, struct btframe_buffer **buf)
583 1.3.2.4 thorpej {
584 1.3.2.4 thorpej return NULL;
585 1.3.2.4 thorpej }
586 1.3.2.4 thorpej
587 1.3.2.3 thorpej static int
588 1.3.2.4 thorpej ubt_send_scodata(void *h, struct btframe_buffer *buf, size_t len)
589 1.3.2.3 thorpej {
590 1.3.2.3 thorpej struct ubt_softc *sc = h;
591 1.3.2.3 thorpej
592 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
593 1.3.2.3 thorpej
594 1.3.2.3 thorpej if (sc->sc_dying)
595 1.3.2.3 thorpej return EIO;
596 1.3.2.3 thorpej
597 1.3.2.3 thorpej return ENXIO;
598 1.3.2.3 thorpej }
599 1.3.2.3 thorpej
600 1.3.2.3 thorpej static void
601 1.3.2.3 thorpej ubt_event_cb(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
602 1.3.2.3 thorpej {
603 1.3.2.3 thorpej struct ubt_softc *sc = h;
604 1.3.2.3 thorpej void *buf;
605 1.3.2.3 thorpej u_int32_t size;
606 1.3.2.3 thorpej
607 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p status=%s\n", __func__, sc,
608 1.3.2.3 thorpej usbd_errstr(status)));
609 1.3.2.3 thorpej
610 1.3.2.3 thorpej if (status != USBD_NORMAL_COMPLETION || sc->sc_dying ||
611 1.3.2.3 thorpej sc->sc_child == NULL)
612 1.3.2.3 thorpej return;
613 1.3.2.3 thorpej
614 1.3.2.3 thorpej usbd_get_xfer_status(xfer, NULL, &buf, &size, NULL);
615 1.3.2.3 thorpej
616 1.3.2.3 thorpej sc->sc_cb->bt_recveventdata(sc->sc_child, buf, (size_t)size);
617 1.3.2.3 thorpej }
618 1.3.2.3 thorpej
619 1.3.2.3 thorpej static void
620 1.3.2.3 thorpej ubt_aclrd_request(struct ubt_softc *sc)
621 1.3.2.3 thorpej {
622 1.3.2.3 thorpej usbd_status status;
623 1.3.2.3 thorpej int s;
624 1.3.2.3 thorpej
625 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
626 1.3.2.3 thorpej
627 1.3.2.3 thorpej if (sc->sc_dying)
628 1.3.2.3 thorpej return;
629 1.3.2.3 thorpej
630 1.3.2.3 thorpej s = splusb();
631 1.3.2.3 thorpej if (sc->sc_aclrd_running) {
632 1.3.2.3 thorpej splx(s);
633 1.3.2.3 thorpej return;
634 1.3.2.3 thorpej }
635 1.3.2.3 thorpej sc->sc_aclrd_running = 1;
636 1.3.2.3 thorpej splx(s);
637 1.3.2.4 thorpej
638 1.3.2.3 thorpej usbd_setup_xfer(sc->sc_aclrd_xfer, sc->sc_aclrd_pipe,
639 1.3.2.3 thorpej sc, sc->sc_aclrd_buf, BTHCI_ACL_DATA_MAX_LEN,
640 1.3.2.3 thorpej USBD_SHORT_XFER_OK | USBD_NO_COPY,
641 1.3.2.3 thorpej USBD_NO_TIMEOUT, ubt_aclrd_cb);
642 1.3.2.3 thorpej
643 1.3.2.3 thorpej status = usbd_transfer(sc->sc_aclrd_xfer);
644 1.3.2.3 thorpej if (status == USBD_IN_PROGRESS || USBD_CANCELLED)
645 1.3.2.3 thorpej return;
646 1.3.2.3 thorpej
647 1.3.2.3 thorpej DPRINTFN(1,("%s: read request failed: %s\n", __func__,
648 1.3.2.3 thorpej usbd_errstr(status)));
649 1.3.2.3 thorpej
650 1.3.2.3 thorpej sc->sc_aclrd_running = 0;
651 1.3.2.3 thorpej sc->sc_blocked |= BT_CBBLOCK_ACL_DATA;
652 1.3.2.3 thorpej }
653 1.3.2.3 thorpej
654 1.3.2.3 thorpej static void
655 1.3.2.3 thorpej ubt_aclrd_cb(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
656 1.3.2.3 thorpej {
657 1.3.2.3 thorpej struct ubt_softc *sc = h;
658 1.3.2.3 thorpej void *buf;
659 1.3.2.3 thorpej u_int32_t size;
660 1.3.2.3 thorpej
661 1.3.2.3 thorpej DPRINTFN(1,("%s: sc=%p status=%s\n", __func__, sc,
662 1.3.2.3 thorpej usbd_errstr(status)));
663 1.3.2.3 thorpej
664 1.3.2.3 thorpej sc->sc_aclrd_running = 0;
665 1.3.2.3 thorpej
666 1.3.2.3 thorpej if (status != USBD_NORMAL_COMPLETION || sc->sc_dying ||
667 1.3.2.3 thorpej sc->sc_child == NULL)
668 1.3.2.3 thorpej return;
669 1.3.2.3 thorpej
670 1.3.2.3 thorpej usbd_get_xfer_status(xfer, NULL, &buf, &size, NULL);
671 1.3.2.3 thorpej
672 1.3.2.3 thorpej sc->sc_cb->bt_recvacldata(sc->sc_child, buf, (size_t)size);
673 1.3.2.3 thorpej
674 1.3.2.3 thorpej /* Re-issue the request if not blocked */
675 1.3.2.3 thorpej if (!sc->sc_dying && !(sc->sc_blocked & BT_CBBLOCK_ACL_DATA))
676 1.3.2.3 thorpej ubt_aclrd_request(sc);
677 1.3.2.3 thorpej }
678 1.3.2.3 thorpej
679 1.3.2.3 thorpej static unsigned int
680 1.3.2.3 thorpej ubt_blockcb(void *h, unsigned int cbblocks)
681 1.3.2.3 thorpej {
682 1.3.2.3 thorpej struct ubt_softc *sc = h;
683 1.3.2.3 thorpej
684 1.3.2.3 thorpej sc->sc_blocked |= (cbblocks & BT_CBBLOCK_ACL_DATA);
685 1.3.2.3 thorpej
686 1.3.2.3 thorpej return sc->sc_blocked;
687 1.3.2.3 thorpej }
688 1.3.2.3 thorpej
689 1.3.2.3 thorpej static unsigned int
690 1.3.2.3 thorpej ubt_unblockcb(void *h, unsigned int cbblocks)
691 1.3.2.3 thorpej {
692 1.3.2.3 thorpej struct ubt_softc *sc = h;
693 1.3.2.3 thorpej unsigned int oblocks, changes;
694 1.3.2.3 thorpej
695 1.3.2.3 thorpej oblocks = sc->sc_blocked;
696 1.3.2.3 thorpej sc->sc_blocked = oblocks & ~cbblocks;
697 1.3.2.3 thorpej
698 1.3.2.3 thorpej changes = oblocks & cbblocks;
699 1.3.2.3 thorpej
700 1.3.2.3 thorpej if (changes & BT_CBBLOCK_ACL_DATA)
701 1.3.2.3 thorpej /* Re-issue the request if action un-blocked reads */
702 1.3.2.3 thorpej ubt_aclrd_request(sc);
703 1.3.2.3 thorpej
704 1.3.2.3 thorpej return sc->sc_blocked;
705 1.3.2.3 thorpej }
706 1.3.2.3 thorpej
707 1.3.2.3 thorpej static int
708 1.3.2.3 thorpej ubt_splraise(void)
709 1.3.2.3 thorpej {
710 1.3.2.3 thorpej return splusb();
711 1.3.2.3 thorpej }
712