ubt.c revision 1.22.10.4 1 1.22.10.4 itohy /* $NetBSD: ubt.c,v 1.22.10.4 2007/06/18 13:46:27 itohy Exp $ */
2 1.1 augustss
3 1.14 gdamore /*-
4 1.14 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.14 gdamore * All rights reserved.
6 1.14 gdamore *
7 1.14 gdamore * Written by Iain Hibbert for Itronix Inc.
8 1.14 gdamore *
9 1.14 gdamore * Redistribution and use in source and binary forms, with or without
10 1.14 gdamore * modification, are permitted provided that the following conditions
11 1.14 gdamore * are met:
12 1.14 gdamore * 1. Redistributions of source code must retain the above copyright
13 1.14 gdamore * notice, this list of conditions and the following disclaimer.
14 1.14 gdamore * 2. Redistributions in binary form must reproduce the above copyright
15 1.14 gdamore * notice, this list of conditions and the following disclaimer in the
16 1.14 gdamore * documentation and/or other materials provided with the distribution.
17 1.14 gdamore * 3. The name of Itronix Inc. may not be used to endorse
18 1.14 gdamore * or promote products derived from this software without specific
19 1.14 gdamore * prior written permission.
20 1.14 gdamore *
21 1.14 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.14 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.14 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.14 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.14 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.14 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.14 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.14 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.14 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.14 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.14 gdamore * POSSIBILITY OF SUCH DAMAGE.
32 1.14 gdamore */
33 1.1 augustss /*
34 1.5 dsainty * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
35 1.1 augustss * All rights reserved.
36 1.1 augustss *
37 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
38 1.5 dsainty * by Lennart Augustsson (lennart (at) augustsson.net) and
39 1.5 dsainty * David Sainty (David.Sainty (at) dtsp.co.nz).
40 1.1 augustss *
41 1.1 augustss * Redistribution and use in source and binary forms, with or without
42 1.1 augustss * modification, are permitted provided that the following conditions
43 1.1 augustss * are met:
44 1.1 augustss * 1. Redistributions of source code must retain the above copyright
45 1.1 augustss * notice, this list of conditions and the following disclaimer.
46 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
47 1.1 augustss * notice, this list of conditions and the following disclaimer in the
48 1.1 augustss * documentation and/or other materials provided with the distribution.
49 1.1 augustss * 3. All advertising materials mentioning features or use of this software
50 1.1 augustss * must display the following acknowledgement:
51 1.1 augustss * This product includes software developed by the NetBSD
52 1.1 augustss * Foundation, Inc. and its contributors.
53 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
54 1.1 augustss * contributors may be used to endorse or promote products derived
55 1.1 augustss * from this software without specific prior written permission.
56 1.1 augustss *
57 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
58 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
61 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
68 1.1 augustss */
69 1.14 gdamore /*
70 1.14 gdamore * This driver originally written by Lennart Augustsson and David Sainty,
71 1.14 gdamore * but was mostly rewritten for the NetBSD Bluetooth protocol stack by
72 1.14 gdamore * Iain Hibbert for Itronix, Inc using the FreeBSD ng_ubt.c driver as a
73 1.14 gdamore * reference.
74 1.14 gdamore */
75 1.1 augustss
76 1.1 augustss #include <sys/cdefs.h>
77 1.22.10.4 itohy __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.22.10.4 2007/06/18 13:46:27 itohy Exp $");
78 1.1 augustss
79 1.1 augustss #include <sys/param.h>
80 1.14 gdamore #include <sys/device.h>
81 1.14 gdamore #include <sys/ioctl.h>
82 1.1 augustss #include <sys/kernel.h>
83 1.5 dsainty #include <sys/malloc.h>
84 1.14 gdamore #include <sys/mbuf.h>
85 1.1 augustss #include <sys/proc.h>
86 1.14 gdamore #include <sys/sysctl.h>
87 1.14 gdamore #include <sys/systm.h>
88 1.1 augustss
89 1.1 augustss #include <dev/usb/usb.h>
90 1.1 augustss #include <dev/usb/usbdi.h>
91 1.1 augustss #include <dev/usb/usbdi_util.h>
92 1.1 augustss #include <dev/usb/usbdevs.h>
93 1.1 augustss
94 1.14 gdamore #include <netbt/bluetooth.h>
95 1.14 gdamore #include <netbt/hci.h>
96 1.14 gdamore
97 1.14 gdamore /*******************************************************************************
98 1.14 gdamore *
99 1.14 gdamore * debugging stuff
100 1.14 gdamore */
101 1.14 gdamore #undef DPRINTF
102 1.14 gdamore #undef DPRINTFN
103 1.3 augustss
104 1.1 augustss #ifdef UBT_DEBUG
105 1.14 gdamore int ubt_debug = UBT_DEBUG;
106 1.14 gdamore
107 1.14 gdamore #define DPRINTF(fmt, args...) do { \
108 1.14 gdamore if (ubt_debug) \
109 1.14 gdamore printf("%s: "fmt, __func__ , ##args); \
110 1.14 gdamore } while (/* CONSTCOND */0)
111 1.14 gdamore
112 1.14 gdamore #define DPRINTFN(n, fmt, args...) do { \
113 1.14 gdamore if (ubt_debug > (n)) \
114 1.14 gdamore printf("%s: "fmt, __func__ , ##args); \
115 1.14 gdamore } while (/* CONSTCOND */0)
116 1.14 gdamore
117 1.14 gdamore SYSCTL_SETUP(sysctl_hw_ubt_debug_setup, "sysctl hw.ubt_debug setup")
118 1.14 gdamore {
119 1.14 gdamore
120 1.14 gdamore sysctl_createv(NULL, 0, NULL, NULL,
121 1.14 gdamore CTLFLAG_PERMANENT,
122 1.14 gdamore CTLTYPE_NODE, "hw",
123 1.14 gdamore NULL,
124 1.14 gdamore NULL, 0,
125 1.14 gdamore NULL, 0,
126 1.14 gdamore CTL_HW, CTL_EOL);
127 1.14 gdamore
128 1.14 gdamore sysctl_createv(NULL, 0, NULL, NULL,
129 1.14 gdamore CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
130 1.14 gdamore CTLTYPE_INT, "ubt_debug",
131 1.14 gdamore SYSCTL_DESCR("ubt debug level"),
132 1.14 gdamore NULL, 0,
133 1.14 gdamore &ubt_debug, sizeof(ubt_debug),
134 1.14 gdamore CTL_HW, CTL_CREATE, CTL_EOL);
135 1.14 gdamore }
136 1.1 augustss #else
137 1.14 gdamore #define DPRINTF(...)
138 1.14 gdamore #define DPRINTFN(...)
139 1.1 augustss #endif
140 1.1 augustss
141 1.14 gdamore /*******************************************************************************
142 1.14 gdamore *
143 1.14 gdamore * ubt softc structure
144 1.14 gdamore *
145 1.14 gdamore */
146 1.14 gdamore
147 1.14 gdamore /* buffer sizes */
148 1.1 augustss /*
149 1.14 gdamore * NB: although ACL packets can extend to 65535 bytes, most devices
150 1.14 gdamore * have max_acl_size at much less (largest I have seen is 384)
151 1.1 augustss */
152 1.14 gdamore #define UBT_BUFSIZ_CMD (HCI_CMD_PKT_SIZE - 1)
153 1.14 gdamore #define UBT_BUFSIZ_ACL (2048 - 1)
154 1.14 gdamore #define UBT_BUFSIZ_EVENT (HCI_EVENT_PKT_SIZE - 1)
155 1.14 gdamore
156 1.14 gdamore /* Transmit timeouts */
157 1.14 gdamore #define UBT_CMD_TIMEOUT USBD_DEFAULT_TIMEOUT
158 1.14 gdamore #define UBT_ACL_TIMEOUT USBD_DEFAULT_TIMEOUT
159 1.14 gdamore
160 1.14 gdamore /*
161 1.14 gdamore * ISOC transfers
162 1.14 gdamore *
163 1.14 gdamore * xfer buffer size depends on the frame size, and the number
164 1.14 gdamore * of frames per transfer is fixed, as each frame should be
165 1.14 gdamore * 1ms worth of data. This keeps the rate that xfers complete
166 1.14 gdamore * fairly constant. We use multiple xfers to keep the hardware
167 1.14 gdamore * busy
168 1.14 gdamore */
169 1.14 gdamore #define UBT_NXFERS 3 /* max xfers to queue */
170 1.14 gdamore #define UBT_NFRAMES 10 /* frames per xfer */
171 1.14 gdamore
172 1.14 gdamore struct ubt_isoc_xfer {
173 1.14 gdamore struct ubt_softc *softc;
174 1.14 gdamore usbd_xfer_handle xfer;
175 1.14 gdamore uint8_t *buf;
176 1.14 gdamore uint16_t size[UBT_NFRAMES];
177 1.14 gdamore int busy;
178 1.14 gdamore };
179 1.1 augustss
180 1.1 augustss struct ubt_softc {
181 1.14 gdamore USBBASEDEVICE sc_dev;
182 1.14 gdamore usbd_device_handle sc_udev;
183 1.14 gdamore int sc_refcnt;
184 1.14 gdamore int sc_dying;
185 1.14 gdamore
186 1.14 gdamore /* Control Interface */
187 1.14 gdamore usbd_interface_handle sc_iface0;
188 1.14 gdamore
189 1.14 gdamore /* Commands (control) */
190 1.14 gdamore usbd_xfer_handle sc_cmd_xfer;
191 1.14 gdamore uint8_t *sc_cmd_buf;
192 1.14 gdamore
193 1.14 gdamore /* Events (interrupt) */
194 1.14 gdamore int sc_evt_addr; /* endpoint address */
195 1.14 gdamore usbd_pipe_handle sc_evt_pipe;
196 1.14 gdamore uint8_t *sc_evt_buf;
197 1.5 dsainty
198 1.5 dsainty /* ACL data (in) */
199 1.14 gdamore int sc_aclrd_addr; /* endpoint address */
200 1.14 gdamore usbd_pipe_handle sc_aclrd_pipe; /* read pipe */
201 1.14 gdamore usbd_xfer_handle sc_aclrd_xfer; /* read xfer */
202 1.14 gdamore uint8_t *sc_aclrd_buf; /* read buffer */
203 1.14 gdamore int sc_aclrd_busy; /* reading */
204 1.5 dsainty
205 1.5 dsainty /* ACL data (out) */
206 1.14 gdamore int sc_aclwr_addr; /* endpoint address */
207 1.14 gdamore usbd_pipe_handle sc_aclwr_pipe; /* write pipe */
208 1.14 gdamore usbd_xfer_handle sc_aclwr_xfer; /* write xfer */
209 1.14 gdamore uint8_t *sc_aclwr_buf; /* write buffer */
210 1.14 gdamore
211 1.14 gdamore /* ISOC interface */
212 1.14 gdamore usbd_interface_handle sc_iface1; /* ISOC interface */
213 1.14 gdamore struct sysctllog *sc_log; /* sysctl log */
214 1.14 gdamore int sc_config; /* current config no */
215 1.14 gdamore int sc_alt_config; /* no of alternates */
216 1.14 gdamore
217 1.14 gdamore /* SCO data (in) */
218 1.14 gdamore int sc_scord_addr; /* endpoint address */
219 1.14 gdamore usbd_pipe_handle sc_scord_pipe; /* read pipe */
220 1.14 gdamore int sc_scord_size; /* frame length */
221 1.14 gdamore struct ubt_isoc_xfer sc_scord[UBT_NXFERS];
222 1.14 gdamore struct mbuf *sc_scord_mbuf; /* current packet */
223 1.14 gdamore
224 1.14 gdamore /* SCO data (out) */
225 1.14 gdamore int sc_scowr_addr; /* endpoint address */
226 1.14 gdamore usbd_pipe_handle sc_scowr_pipe; /* write pipe */
227 1.14 gdamore int sc_scowr_size; /* frame length */
228 1.14 gdamore struct ubt_isoc_xfer sc_scowr[UBT_NXFERS];
229 1.14 gdamore struct mbuf *sc_scowr_mbuf; /* current packet */
230 1.1 augustss
231 1.14 gdamore /* Protocol structure */
232 1.14 gdamore struct hci_unit sc_unit;
233 1.22.10.3 itohy
234 1.22.10.3 itohy /* Successfully attached */
235 1.22.10.3 itohy int sc_ok;
236 1.14 gdamore };
237 1.14 gdamore
238 1.14 gdamore /*******************************************************************************
239 1.14 gdamore *
240 1.14 gdamore * Bluetooth unit/USB callback routines
241 1.14 gdamore *
242 1.14 gdamore */
243 1.14 gdamore static int ubt_enable(struct hci_unit *);
244 1.14 gdamore static void ubt_disable(struct hci_unit *);
245 1.14 gdamore
246 1.14 gdamore static void ubt_xmit_cmd_start(struct hci_unit *);
247 1.14 gdamore static void ubt_xmit_cmd_complete(usbd_xfer_handle,
248 1.14 gdamore usbd_private_handle, usbd_status);
249 1.14 gdamore
250 1.14 gdamore static void ubt_xmit_acl_start(struct hci_unit *);
251 1.14 gdamore static void ubt_xmit_acl_complete(usbd_xfer_handle,
252 1.14 gdamore usbd_private_handle, usbd_status);
253 1.14 gdamore
254 1.14 gdamore static void ubt_xmit_sco_start(struct hci_unit *);
255 1.14 gdamore static void ubt_xmit_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
256 1.14 gdamore static void ubt_xmit_sco_complete(usbd_xfer_handle,
257 1.14 gdamore usbd_private_handle, usbd_status);
258 1.5 dsainty
259 1.14 gdamore static void ubt_recv_event(usbd_xfer_handle,
260 1.14 gdamore usbd_private_handle, usbd_status);
261 1.1 augustss
262 1.14 gdamore static void ubt_recv_acl_start(struct ubt_softc *);
263 1.14 gdamore static void ubt_recv_acl_complete(usbd_xfer_handle,
264 1.14 gdamore usbd_private_handle, usbd_status);
265 1.5 dsainty
266 1.14 gdamore static void ubt_recv_sco_start1(struct ubt_softc *, struct ubt_isoc_xfer *);
267 1.14 gdamore static void ubt_recv_sco_complete(usbd_xfer_handle,
268 1.14 gdamore usbd_private_handle, usbd_status);
269 1.6 dsainty
270 1.6 dsainty
271 1.14 gdamore /*******************************************************************************
272 1.14 gdamore *
273 1.14 gdamore * USB Autoconfig stuff
274 1.14 gdamore *
275 1.14 gdamore */
276 1.5 dsainty
277 1.1 augustss USB_DECLARE_DRIVER(ubt);
278 1.1 augustss
279 1.14 gdamore static int ubt_set_isoc_config(struct ubt_softc *);
280 1.14 gdamore static int ubt_sysctl_config(SYSCTLFN_PROTO);
281 1.14 gdamore static void ubt_abortdealloc(struct ubt_softc *);
282 1.14 gdamore
283 1.15 plunky /*
284 1.22.10.3 itohy * Match against the whole device, since we want to take
285 1.22.10.3 itohy * both interfaces. If a device should be ignored then add
286 1.15 plunky *
287 1.15 plunky * { VendorID, ProductID }
288 1.15 plunky *
289 1.22.10.3 itohy * to the ubt_ignore list.
290 1.15 plunky */
291 1.15 plunky static const struct usb_devno ubt_ignore[] = {
292 1.15 plunky { USB_VENDOR_BROADCOM, USB_PRODUCT_BROADCOM_BCM2033NF },
293 1.15 plunky { 0, 0 } /* end of list */
294 1.15 plunky };
295 1.15 plunky
296 1.1 augustss USB_MATCH(ubt)
297 1.1 augustss {
298 1.1 augustss USB_MATCH_START(ubt, uaa);
299 1.22.10.4 itohy #ifndef USB_USE_IFATTACH
300 1.22.10.3 itohy usb_device_descriptor_t *dd;
301 1.22.10.4 itohy #endif /* USB_USE_IFATTACH */
302 1.1 augustss
303 1.14 gdamore DPRINTFN(50, "ubt_match\n");
304 1.1 augustss
305 1.22.10.4 itohy #ifndef USB_USE_IFATTACH
306 1.22.10.3 itohy if (uaa->iface != NULL)
307 1.14 gdamore return UMATCH_NONE;
308 1.22.10.4 itohy #endif /* USB_USE_IFATTACH */
309 1.1 augustss
310 1.15 plunky if (usb_lookup(ubt_ignore, uaa->vendor, uaa->product))
311 1.15 plunky return UMATCH_NONE;
312 1.15 plunky
313 1.22.10.4 itohy #ifndef USB_USE_IFATTACH
314 1.22.10.3 itohy dd = usbd_get_device_descriptor(uaa->device);
315 1.22.10.3 itohy if (dd != NULL
316 1.22.10.3 itohy && dd->bDeviceClass == UDCLASS_WIRELESS
317 1.22.10.3 itohy && dd->bDeviceSubClass == UDSUBCLASS_RF
318 1.22.10.3 itohy && dd->bDeviceProtocol == UDPROTO_BLUETOOTH)
319 1.22.10.4 itohy #else
320 1.22.10.4 itohy if (uaa->class == UDCLASS_WIRELESS
321 1.22.10.4 itohy && uaa->subclass == UDSUBCLASS_RF
322 1.22.10.4 itohy && uaa->proto == UDPROTO_BLUETOOTH)
323 1.22.10.4 itohy #endif /* USB_USE_IFATTACH */
324 1.22.10.3 itohy return UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO;
325 1.14 gdamore
326 1.14 gdamore return UMATCH_NONE;
327 1.1 augustss }
328 1.1 augustss
329 1.1 augustss USB_ATTACH(ubt)
330 1.1 augustss {
331 1.1 augustss USB_ATTACH_START(ubt, sc, uaa);
332 1.14 gdamore usb_config_descriptor_t *cd;
333 1.14 gdamore usb_endpoint_descriptor_t *ed;
334 1.14 gdamore const struct sysctlnode *node;
335 1.14 gdamore char *devinfop;
336 1.14 gdamore int err;
337 1.14 gdamore uint8_t count, i;
338 1.1 augustss
339 1.14 gdamore DPRINTFN(50, "ubt_attach: sc=%p\n", sc);
340 1.1 augustss
341 1.14 gdamore sc->sc_udev = uaa->device;
342 1.14 gdamore
343 1.14 gdamore devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
344 1.1 augustss USB_ATTACH_SETUP;
345 1.14 gdamore aprint_normal("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
346 1.12 augustss usbd_devinfo_free(devinfop);
347 1.1 augustss
348 1.14 gdamore /*
349 1.22.10.3 itohy * Move the device into the configured state
350 1.14 gdamore */
351 1.22.10.3 itohy err = usbd_set_config_index(sc->sc_udev, 0, 1);
352 1.22.10.3 itohy if (err) {
353 1.22.10.3 itohy aprint_error("%s: failed to set configuration idx 0: %s\n",
354 1.22.10.3 itohy USBDEVNAME(sc->sc_dev), usbd_errstr(err));
355 1.14 gdamore
356 1.14 gdamore USB_ATTACH_ERROR_RETURN;
357 1.14 gdamore }
358 1.1 augustss
359 1.5 dsainty /*
360 1.14 gdamore * Interface 0 must have 3 endpoints
361 1.14 gdamore * 1) Interrupt endpoint to receive HCI events
362 1.14 gdamore * 2) Bulk IN endpoint to receive ACL data
363 1.14 gdamore * 3) Bulk OUT endpoint to send ACL data
364 1.3 augustss */
365 1.22.10.3 itohy err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
366 1.14 gdamore if (err) {
367 1.14 gdamore aprint_error("%s: Could not get interface 0 handle %s (%d)\n",
368 1.14 gdamore USBDEVNAME(sc->sc_dev), usbd_errstr(err), err);
369 1.14 gdamore
370 1.14 gdamore USB_ATTACH_ERROR_RETURN;
371 1.14 gdamore }
372 1.5 dsainty
373 1.5 dsainty sc->sc_evt_addr = -1;
374 1.5 dsainty sc->sc_aclrd_addr = -1;
375 1.5 dsainty sc->sc_aclwr_addr = -1;
376 1.5 dsainty
377 1.14 gdamore count = 0;
378 1.22.10.3 itohy (void)usbd_endpoint_count(sc->sc_iface0, &count);
379 1.14 gdamore
380 1.14 gdamore for (i = 0 ; i < count ; i++) {
381 1.14 gdamore int dir, type;
382 1.14 gdamore
383 1.22.10.3 itohy ed = usbd_interface2endpoint_descriptor(sc->sc_iface0, i);
384 1.1 augustss if (ed == NULL) {
385 1.14 gdamore aprint_error("%s: could not read endpoint descriptor %d\n",
386 1.1 augustss USBDEVNAME(sc->sc_dev), i);
387 1.14 gdamore
388 1.1 augustss USB_ATTACH_ERROR_RETURN;
389 1.1 augustss }
390 1.5 dsainty
391 1.14 gdamore dir = UE_GET_DIR(ed->bEndpointAddress);
392 1.14 gdamore type = UE_GET_XFERTYPE(ed->bmAttributes);
393 1.5 dsainty
394 1.14 gdamore if (dir == UE_DIR_IN && type == UE_INTERRUPT)
395 1.5 dsainty sc->sc_evt_addr = ed->bEndpointAddress;
396 1.14 gdamore else if (dir == UE_DIR_IN && type == UE_BULK)
397 1.5 dsainty sc->sc_aclrd_addr = ed->bEndpointAddress;
398 1.14 gdamore else if (dir == UE_DIR_OUT && type == UE_BULK)
399 1.5 dsainty sc->sc_aclwr_addr = ed->bEndpointAddress;
400 1.1 augustss }
401 1.5 dsainty
402 1.14 gdamore if (sc->sc_evt_addr == -1) {
403 1.14 gdamore aprint_error("%s: missing INTERRUPT endpoint on interface 0\n",
404 1.14 gdamore USBDEVNAME(sc->sc_dev));
405 1.14 gdamore
406 1.14 gdamore USB_ATTACH_ERROR_RETURN;
407 1.14 gdamore }
408 1.14 gdamore if (sc->sc_aclrd_addr == -1) {
409 1.14 gdamore aprint_error("%s: missing BULK IN endpoint on interface 0\n",
410 1.14 gdamore USBDEVNAME(sc->sc_dev));
411 1.14 gdamore
412 1.14 gdamore USB_ATTACH_ERROR_RETURN;
413 1.14 gdamore }
414 1.14 gdamore if (sc->sc_aclwr_addr == -1) {
415 1.14 gdamore aprint_error("%s: missing BULK OUT endpoint on interface 0\n",
416 1.14 gdamore USBDEVNAME(sc->sc_dev));
417 1.14 gdamore
418 1.14 gdamore USB_ATTACH_ERROR_RETURN;
419 1.14 gdamore }
420 1.14 gdamore
421 1.14 gdamore /*
422 1.14 gdamore * Interface 1 must have 2 endpoints
423 1.14 gdamore * 1) Isochronous IN endpoint to receive SCO data
424 1.14 gdamore * 2) Isochronous OUT endpoint to send SCO data
425 1.14 gdamore *
426 1.14 gdamore * and will have several configurations, which can be selected
427 1.14 gdamore * via a sysctl variable. We select config 0 to start, which
428 1.14 gdamore * means that no SCO data will be available.
429 1.14 gdamore */
430 1.22.10.3 itohy err = usbd_device2interface_handle(sc->sc_udev, 1, &sc->sc_iface1);
431 1.14 gdamore if (err) {
432 1.14 gdamore aprint_error("%s: Could not get interface 1 handle %s (%d)\n",
433 1.14 gdamore USBDEVNAME(sc->sc_dev), usbd_errstr(err), err);
434 1.14 gdamore
435 1.14 gdamore USB_ATTACH_ERROR_RETURN;
436 1.14 gdamore }
437 1.14 gdamore
438 1.14 gdamore cd = usbd_get_config_descriptor(sc->sc_udev);
439 1.14 gdamore if (cd == NULL) {
440 1.14 gdamore aprint_error("%s: could not get config descriptor\n",
441 1.14 gdamore USBDEVNAME(sc->sc_dev));
442 1.14 gdamore
443 1.14 gdamore USB_ATTACH_ERROR_RETURN;
444 1.14 gdamore }
445 1.14 gdamore
446 1.14 gdamore sc->sc_alt_config = usbd_get_no_alts(cd, 1);
447 1.14 gdamore
448 1.14 gdamore /* set initial config */
449 1.14 gdamore err = ubt_set_isoc_config(sc);
450 1.14 gdamore if (err) {
451 1.14 gdamore aprint_error("%s: ISOC config failed\n",
452 1.14 gdamore USBDEVNAME(sc->sc_dev));
453 1.14 gdamore
454 1.1 augustss USB_ATTACH_ERROR_RETURN;
455 1.1 augustss }
456 1.1 augustss
457 1.14 gdamore /* Attach HCI */
458 1.14 gdamore sc->sc_unit.hci_softc = sc;
459 1.14 gdamore sc->sc_unit.hci_devname = USBDEVNAME(sc->sc_dev);
460 1.14 gdamore sc->sc_unit.hci_enable = ubt_enable;
461 1.14 gdamore sc->sc_unit.hci_disable = ubt_disable;
462 1.14 gdamore sc->sc_unit.hci_start_cmd = ubt_xmit_cmd_start;
463 1.14 gdamore sc->sc_unit.hci_start_acl = ubt_xmit_acl_start;
464 1.14 gdamore sc->sc_unit.hci_start_sco = ubt_xmit_sco_start;
465 1.22 yamt sc->sc_unit.hci_ipl = makeiplcookie(IPL_USB); /* XXX: IPL_SOFTUSB ?? */
466 1.14 gdamore hci_attach(&sc->sc_unit);
467 1.14 gdamore
468 1.14 gdamore usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
469 1.14 gdamore USBDEV(sc->sc_dev));
470 1.14 gdamore
471 1.14 gdamore /* sysctl set-up for alternate configs */
472 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, NULL,
473 1.14 gdamore CTLFLAG_PERMANENT,
474 1.14 gdamore CTLTYPE_NODE, "hw",
475 1.14 gdamore NULL,
476 1.14 gdamore NULL, 0,
477 1.14 gdamore NULL, 0,
478 1.14 gdamore CTL_HW, CTL_EOL);
479 1.14 gdamore
480 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, &node,
481 1.14 gdamore 0,
482 1.14 gdamore CTLTYPE_NODE, USBDEVNAME(sc->sc_dev),
483 1.14 gdamore SYSCTL_DESCR("ubt driver information"),
484 1.14 gdamore NULL, 0,
485 1.14 gdamore NULL, 0,
486 1.14 gdamore CTL_HW,
487 1.14 gdamore CTL_CREATE, CTL_EOL);
488 1.14 gdamore
489 1.14 gdamore if (node != NULL) {
490 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, NULL,
491 1.14 gdamore CTLFLAG_READWRITE,
492 1.14 gdamore CTLTYPE_INT, "config",
493 1.14 gdamore SYSCTL_DESCR("configuration number"),
494 1.14 gdamore ubt_sysctl_config, 0,
495 1.14 gdamore sc, 0,
496 1.14 gdamore CTL_HW, node->sysctl_num,
497 1.14 gdamore CTL_CREATE, CTL_EOL);
498 1.14 gdamore
499 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, NULL,
500 1.14 gdamore CTLFLAG_READONLY,
501 1.14 gdamore CTLTYPE_INT, "alt_config",
502 1.14 gdamore SYSCTL_DESCR("number of alternate configurations"),
503 1.14 gdamore NULL, 0,
504 1.14 gdamore &sc->sc_alt_config, sizeof(sc->sc_alt_config),
505 1.14 gdamore CTL_HW, node->sysctl_num,
506 1.14 gdamore CTL_CREATE, CTL_EOL);
507 1.14 gdamore
508 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, NULL,
509 1.14 gdamore CTLFLAG_READONLY,
510 1.14 gdamore CTLTYPE_INT, "sco_rxsize",
511 1.14 gdamore SYSCTL_DESCR("max SCO receive size"),
512 1.14 gdamore NULL, 0,
513 1.14 gdamore &sc->sc_scord_size, sizeof(sc->sc_scord_size),
514 1.14 gdamore CTL_HW, node->sysctl_num,
515 1.14 gdamore CTL_CREATE, CTL_EOL);
516 1.14 gdamore
517 1.14 gdamore sysctl_createv(&sc->sc_log, 0, NULL, NULL,
518 1.14 gdamore CTLFLAG_READONLY,
519 1.14 gdamore CTLTYPE_INT, "sco_txsize",
520 1.14 gdamore SYSCTL_DESCR("max SCO transmit size"),
521 1.14 gdamore NULL, 0,
522 1.14 gdamore &sc->sc_scowr_size, sizeof(sc->sc_scowr_size),
523 1.14 gdamore CTL_HW, node->sysctl_num,
524 1.14 gdamore CTL_CREATE, CTL_EOL);
525 1.14 gdamore }
526 1.14 gdamore
527 1.22.10.3 itohy sc->sc_ok = 1;
528 1.14 gdamore USB_ATTACH_SUCCESS_RETURN;
529 1.14 gdamore }
530 1.14 gdamore
531 1.14 gdamore USB_DETACH(ubt)
532 1.14 gdamore {
533 1.14 gdamore USB_DETACH_START(ubt, sc);
534 1.14 gdamore int s;
535 1.14 gdamore
536 1.14 gdamore DPRINTF("sc=%p flags=%d\n", sc, flags);
537 1.14 gdamore
538 1.14 gdamore sc->sc_dying = 1;
539 1.14 gdamore
540 1.22.10.3 itohy if (!sc->sc_ok)
541 1.22.10.3 itohy return 0;
542 1.22.10.3 itohy
543 1.14 gdamore /* delete sysctl nodes */
544 1.14 gdamore sysctl_teardown(&sc->sc_log);
545 1.14 gdamore
546 1.14 gdamore /* Detach HCI interface */
547 1.14 gdamore hci_detach(&sc->sc_unit);
548 1.14 gdamore
549 1.14 gdamore /*
550 1.14 gdamore * Abort all pipes. Causes processes waiting for transfer to wake.
551 1.14 gdamore *
552 1.14 gdamore * Actually, hci_detach() above will call ubt_disable() which may
553 1.14 gdamore * call ubt_abortdealloc(), but lets be sure since doing it twice
554 1.14 gdamore * wont cause an error.
555 1.14 gdamore */
556 1.14 gdamore ubt_abortdealloc(sc);
557 1.14 gdamore
558 1.14 gdamore /* wait for all processes to finish */
559 1.14 gdamore s = splusb();
560 1.14 gdamore if (sc->sc_refcnt-- > 0)
561 1.14 gdamore usb_detach_wait(USBDEV(sc->sc_dev));
562 1.14 gdamore
563 1.14 gdamore splx(s);
564 1.14 gdamore
565 1.14 gdamore usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
566 1.14 gdamore USBDEV(sc->sc_dev));
567 1.14 gdamore
568 1.14 gdamore DPRINTFN(1, "driver detached\n");
569 1.14 gdamore
570 1.14 gdamore return 0;
571 1.14 gdamore }
572 1.14 gdamore
573 1.14 gdamore int
574 1.14 gdamore ubt_activate(device_ptr_t self, enum devact act)
575 1.14 gdamore {
576 1.14 gdamore struct ubt_softc *sc = (struct ubt_softc *)self;
577 1.14 gdamore int error = 0;
578 1.14 gdamore
579 1.14 gdamore DPRINTFN(1, "ubt_activate: sc=%p, act=%d\n", sc, act);
580 1.14 gdamore
581 1.14 gdamore switch (act) {
582 1.14 gdamore case DVACT_ACTIVATE:
583 1.14 gdamore return EOPNOTSUPP;
584 1.14 gdamore break;
585 1.14 gdamore
586 1.14 gdamore case DVACT_DEACTIVATE:
587 1.14 gdamore sc->sc_dying = 1;
588 1.14 gdamore break;
589 1.14 gdamore }
590 1.14 gdamore return error;
591 1.14 gdamore }
592 1.14 gdamore
593 1.14 gdamore /* set ISOC configuration */
594 1.14 gdamore static int
595 1.14 gdamore ubt_set_isoc_config(struct ubt_softc *sc)
596 1.14 gdamore {
597 1.14 gdamore usb_endpoint_descriptor_t *ed;
598 1.14 gdamore int rd_addr, wr_addr, rd_size, wr_size;
599 1.14 gdamore uint8_t count, i;
600 1.14 gdamore int err;
601 1.14 gdamore
602 1.14 gdamore err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
603 1.17 plunky if (err != USBD_NORMAL_COMPLETION) {
604 1.14 gdamore aprint_error(
605 1.14 gdamore "%s: Could not set config %d on ISOC interface. %s (%d)\n",
606 1.14 gdamore USBDEVNAME(sc->sc_dev), sc->sc_config, usbd_errstr(err), err);
607 1.14 gdamore
608 1.17 plunky return err == USBD_IN_USE ? EBUSY : EIO;
609 1.14 gdamore }
610 1.14 gdamore
611 1.14 gdamore /*
612 1.14 gdamore * We wont get past the above if there are any pipes open, so no
613 1.14 gdamore * need to worry about buf/xfer/pipe deallocation. If we get an
614 1.14 gdamore * error after this, the frame quantities will be 0 and no SCO
615 1.14 gdamore * data will be possible.
616 1.14 gdamore */
617 1.14 gdamore
618 1.14 gdamore sc->sc_scord_size = rd_size = 0;
619 1.14 gdamore sc->sc_scord_addr = rd_addr = -1;
620 1.14 gdamore
621 1.14 gdamore sc->sc_scowr_size = wr_size = 0;
622 1.14 gdamore sc->sc_scowr_addr = wr_addr = -1;
623 1.14 gdamore
624 1.14 gdamore count = 0;
625 1.14 gdamore (void)usbd_endpoint_count(sc->sc_iface1, &count);
626 1.14 gdamore
627 1.14 gdamore for (i = 0 ; i < count ; i++) {
628 1.14 gdamore ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
629 1.14 gdamore if (ed == NULL) {
630 1.14 gdamore printf("%s: could not read endpoint descriptor %d\n",
631 1.14 gdamore USBDEVNAME(sc->sc_dev), i);
632 1.14 gdamore
633 1.14 gdamore return EIO;
634 1.14 gdamore }
635 1.14 gdamore
636 1.14 gdamore DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
637 1.14 gdamore USBDEVNAME(sc->sc_dev),
638 1.14 gdamore UE_GET_XFERTYPE(ed->bmAttributes),
639 1.14 gdamore UE_GET_ISO_TYPE(ed->bmAttributes),
640 1.14 gdamore ed->bEndpointAddress,
641 1.14 gdamore UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");
642 1.14 gdamore
643 1.14 gdamore if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
644 1.3 augustss continue;
645 1.14 gdamore
646 1.14 gdamore if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
647 1.14 gdamore rd_addr = ed->bEndpointAddress;
648 1.14 gdamore rd_size = UGETW(ed->wMaxPacketSize);
649 1.14 gdamore } else {
650 1.14 gdamore wr_addr = ed->bEndpointAddress;
651 1.14 gdamore wr_size = UGETW(ed->wMaxPacketSize);
652 1.3 augustss }
653 1.3 augustss }
654 1.3 augustss
655 1.14 gdamore if (rd_addr == -1) {
656 1.14 gdamore aprint_error(
657 1.14 gdamore "%s: missing ISOC IN endpoint on interface config %d\n",
658 1.14 gdamore USBDEVNAME(sc->sc_dev), sc->sc_config);
659 1.14 gdamore
660 1.14 gdamore return ENOENT;
661 1.14 gdamore }
662 1.14 gdamore if (wr_addr == -1) {
663 1.14 gdamore aprint_error(
664 1.14 gdamore "%s: missing ISOC OUT endpoint on interface config %d\n",
665 1.14 gdamore USBDEVNAME(sc->sc_dev), sc->sc_config);
666 1.14 gdamore
667 1.14 gdamore return ENOENT;
668 1.14 gdamore }
669 1.14 gdamore
670 1.14 gdamore #ifdef DIAGNOSTIC
671 1.14 gdamore if (rd_size > MLEN) {
672 1.14 gdamore printf("%s: rd_size=%d exceeds MLEN\n",
673 1.14 gdamore USBDEVNAME(sc->sc_dev), rd_size);
674 1.14 gdamore
675 1.14 gdamore return EOVERFLOW;
676 1.14 gdamore }
677 1.3 augustss
678 1.14 gdamore if (wr_size > MLEN) {
679 1.14 gdamore printf("%s: wr_size=%d exceeds MLEN\n",
680 1.14 gdamore USBDEVNAME(sc->sc_dev), wr_size);
681 1.14 gdamore
682 1.14 gdamore return EOVERFLOW;
683 1.14 gdamore }
684 1.14 gdamore #endif
685 1.14 gdamore
686 1.14 gdamore sc->sc_scord_size = rd_size;
687 1.14 gdamore sc->sc_scord_addr = rd_addr;
688 1.14 gdamore
689 1.14 gdamore sc->sc_scowr_size = wr_size;
690 1.14 gdamore sc->sc_scowr_addr = wr_addr;
691 1.14 gdamore
692 1.14 gdamore return 0;
693 1.14 gdamore }
694 1.1 augustss
695 1.14 gdamore /* sysctl helper to set alternate configurations */
696 1.14 gdamore static int
697 1.14 gdamore ubt_sysctl_config(SYSCTLFN_ARGS)
698 1.14 gdamore {
699 1.14 gdamore struct sysctlnode node;
700 1.14 gdamore struct ubt_softc *sc;
701 1.14 gdamore int t, error;
702 1.14 gdamore
703 1.14 gdamore node = *rnode;
704 1.14 gdamore sc = node.sysctl_data;
705 1.14 gdamore
706 1.14 gdamore t = sc->sc_config;
707 1.14 gdamore node.sysctl_data = &t;
708 1.14 gdamore error = sysctl_lookup(SYSCTLFN_CALL(&node));
709 1.14 gdamore if (error || newp == NULL)
710 1.14 gdamore return error;
711 1.5 dsainty
712 1.14 gdamore if (t < 0 || t >= sc->sc_alt_config)
713 1.14 gdamore return EINVAL;
714 1.1 augustss
715 1.18 plunky /* This may not change when the unit is enabled */
716 1.18 plunky if (sc->sc_unit.hci_flags & BTF_RUNNING)
717 1.18 plunky return EBUSY;
718 1.18 plunky
719 1.14 gdamore sc->sc_config = t;
720 1.14 gdamore return ubt_set_isoc_config(sc);
721 1.1 augustss }
722 1.1 augustss
723 1.5 dsainty static void
724 1.5 dsainty ubt_abortdealloc(struct ubt_softc *sc)
725 1.5 dsainty {
726 1.14 gdamore int i;
727 1.14 gdamore
728 1.14 gdamore DPRINTFN(1, "sc=%p\n", sc);
729 1.5 dsainty
730 1.14 gdamore /* Abort all pipes */
731 1.22.10.2 itohy if (sc->sc_evt_pipe != NULL)
732 1.5 dsainty usbd_abort_pipe(sc->sc_evt_pipe);
733 1.14 gdamore
734 1.22.10.2 itohy if (sc->sc_aclrd_pipe != NULL)
735 1.5 dsainty usbd_abort_pipe(sc->sc_aclrd_pipe);
736 1.14 gdamore
737 1.22.10.2 itohy if (sc->sc_aclwr_pipe != NULL)
738 1.5 dsainty usbd_abort_pipe(sc->sc_aclwr_pipe);
739 1.14 gdamore
740 1.22.10.2 itohy if (sc->sc_scord_pipe != NULL)
741 1.14 gdamore usbd_abort_pipe(sc->sc_scord_pipe);
742 1.14 gdamore
743 1.22.10.2 itohy if (sc->sc_scowr_pipe != NULL)
744 1.14 gdamore usbd_abort_pipe(sc->sc_scowr_pipe);
745 1.14 gdamore
746 1.14 gdamore /* Free event buffer */
747 1.14 gdamore if (sc->sc_evt_buf != NULL) {
748 1.14 gdamore free(sc->sc_evt_buf, M_USBDEV);
749 1.14 gdamore sc->sc_evt_buf = NULL;
750 1.14 gdamore }
751 1.14 gdamore
752 1.14 gdamore /* Free all xfers and xfer buffers (implicit) */
753 1.14 gdamore if (sc->sc_cmd_xfer != NULL) {
754 1.14 gdamore usbd_free_xfer(sc->sc_cmd_xfer);
755 1.14 gdamore sc->sc_cmd_xfer = NULL;
756 1.14 gdamore sc->sc_cmd_buf = NULL;
757 1.14 gdamore }
758 1.14 gdamore
759 1.5 dsainty if (sc->sc_aclrd_xfer != NULL) {
760 1.5 dsainty usbd_free_xfer(sc->sc_aclrd_xfer);
761 1.5 dsainty sc->sc_aclrd_xfer = NULL;
762 1.5 dsainty sc->sc_aclrd_buf = NULL;
763 1.5 dsainty }
764 1.14 gdamore
765 1.5 dsainty if (sc->sc_aclwr_xfer != NULL) {
766 1.5 dsainty usbd_free_xfer(sc->sc_aclwr_xfer);
767 1.5 dsainty sc->sc_aclwr_xfer = NULL;
768 1.5 dsainty sc->sc_aclwr_buf = NULL;
769 1.5 dsainty }
770 1.5 dsainty
771 1.14 gdamore for (i = 0 ; i < UBT_NXFERS ; i++) {
772 1.14 gdamore if (sc->sc_scord[i].xfer != NULL) {
773 1.14 gdamore usbd_free_xfer(sc->sc_scord[i].xfer);
774 1.14 gdamore sc->sc_scord[i].xfer = NULL;
775 1.14 gdamore sc->sc_scord[i].buf = NULL;
776 1.14 gdamore }
777 1.1 augustss
778 1.14 gdamore if (sc->sc_scowr[i].xfer != NULL) {
779 1.14 gdamore usbd_free_xfer(sc->sc_scowr[i].xfer);
780 1.14 gdamore sc->sc_scowr[i].xfer = NULL;
781 1.14 gdamore sc->sc_scowr[i].buf = NULL;
782 1.14 gdamore }
783 1.1 augustss }
784 1.1 augustss
785 1.22.10.2 itohy /* Close all pipes */
786 1.22.10.2 itohy if (sc->sc_evt_pipe != NULL) {
787 1.22.10.2 itohy usbd_close_pipe(sc->sc_evt_pipe);
788 1.22.10.2 itohy sc->sc_evt_pipe = NULL;
789 1.22.10.2 itohy }
790 1.22.10.2 itohy
791 1.22.10.2 itohy if (sc->sc_aclrd_pipe != NULL) {
792 1.22.10.2 itohy usbd_close_pipe(sc->sc_aclrd_pipe);
793 1.22.10.2 itohy sc->sc_aclrd_pipe = NULL;
794 1.22.10.2 itohy }
795 1.22.10.2 itohy
796 1.22.10.2 itohy if (sc->sc_aclwr_pipe != NULL) {
797 1.22.10.2 itohy usbd_close_pipe(sc->sc_aclwr_pipe);
798 1.22.10.2 itohy sc->sc_aclwr_pipe = NULL;
799 1.22.10.2 itohy }
800 1.22.10.2 itohy
801 1.22.10.2 itohy if (sc->sc_scord_pipe != NULL) {
802 1.22.10.2 itohy usbd_close_pipe(sc->sc_scord_pipe);
803 1.22.10.2 itohy sc->sc_scord_pipe = NULL;
804 1.22.10.2 itohy }
805 1.22.10.2 itohy
806 1.22.10.2 itohy if (sc->sc_scowr_pipe != NULL) {
807 1.22.10.2 itohy usbd_close_pipe(sc->sc_scowr_pipe);
808 1.22.10.2 itohy sc->sc_scowr_pipe = NULL;
809 1.22.10.2 itohy }
810 1.22.10.2 itohy
811 1.14 gdamore /* Free partial SCO packets */
812 1.14 gdamore if (sc->sc_scord_mbuf != NULL) {
813 1.14 gdamore m_freem(sc->sc_scord_mbuf);
814 1.14 gdamore sc->sc_scord_mbuf = NULL;
815 1.1 augustss }
816 1.1 augustss
817 1.14 gdamore if (sc->sc_scowr_mbuf != NULL) {
818 1.14 gdamore m_freem(sc->sc_scowr_mbuf);
819 1.14 gdamore sc->sc_scowr_mbuf = NULL;
820 1.1 augustss }
821 1.5 dsainty }
822 1.5 dsainty
823 1.14 gdamore /*******************************************************************************
824 1.14 gdamore *
825 1.14 gdamore * Bluetooth Unit/USB callbacks
826 1.14 gdamore *
827 1.14 gdamore * All of this will be called at the IPL_ we specified above
828 1.14 gdamore */
829 1.5 dsainty static int
830 1.14 gdamore ubt_enable(struct hci_unit *unit)
831 1.5 dsainty {
832 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
833 1.5 dsainty usbd_status err;
834 1.14 gdamore int i, error;
835 1.5 dsainty
836 1.14 gdamore DPRINTFN(1, "sc=%p\n", sc);
837 1.5 dsainty
838 1.14 gdamore if (unit->hci_flags & BTF_RUNNING)
839 1.14 gdamore return 0;
840 1.14 gdamore
841 1.14 gdamore /* Events */
842 1.14 gdamore sc->sc_evt_buf = malloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
843 1.5 dsainty if (sc->sc_evt_buf == NULL) {
844 1.5 dsainty error = ENOMEM;
845 1.14 gdamore goto bad;
846 1.5 dsainty }
847 1.14 gdamore err = usbd_open_pipe_intr(sc->sc_iface0,
848 1.14 gdamore sc->sc_evt_addr,
849 1.14 gdamore USBD_SHORT_XFER_OK,
850 1.14 gdamore &sc->sc_evt_pipe,
851 1.14 gdamore sc,
852 1.14 gdamore sc->sc_evt_buf,
853 1.14 gdamore UBT_BUFSIZ_EVENT,
854 1.14 gdamore ubt_recv_event,
855 1.21 drochner USBD_DEFAULT_INTERVAL);
856 1.5 dsainty if (err != USBD_NORMAL_COMPLETION) {
857 1.5 dsainty error = EIO;
858 1.14 gdamore goto bad;
859 1.5 dsainty }
860 1.5 dsainty
861 1.14 gdamore /* Commands */
862 1.22.10.1 itohy sc->sc_cmd_xfer = usbd_alloc_default_xfer(sc->sc_udev);
863 1.14 gdamore if (sc->sc_cmd_xfer == NULL) {
864 1.14 gdamore error = ENOMEM;
865 1.14 gdamore goto bad;
866 1.14 gdamore }
867 1.14 gdamore sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
868 1.14 gdamore if (sc->sc_cmd_buf == NULL) {
869 1.14 gdamore error = ENOMEM;
870 1.14 gdamore goto bad;
871 1.5 dsainty }
872 1.5 dsainty
873 1.14 gdamore /* ACL read */
874 1.14 gdamore err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
875 1.14 gdamore USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
876 1.5 dsainty if (err != USBD_NORMAL_COMPLETION) {
877 1.5 dsainty error = EIO;
878 1.14 gdamore goto bad;
879 1.5 dsainty }
880 1.22.10.1 itohy sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev, sc->sc_aclrd_pipe);
881 1.5 dsainty if (sc->sc_aclrd_xfer == NULL) {
882 1.5 dsainty error = ENOMEM;
883 1.14 gdamore goto bad;
884 1.5 dsainty }
885 1.14 gdamore sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
886 1.14 gdamore if (sc->sc_aclrd_buf == NULL) {
887 1.5 dsainty error = ENOMEM;
888 1.14 gdamore goto bad;
889 1.5 dsainty }
890 1.14 gdamore sc->sc_aclrd_busy = 0;
891 1.14 gdamore ubt_recv_acl_start(sc);
892 1.5 dsainty
893 1.14 gdamore /* ACL write */
894 1.14 gdamore err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
895 1.14 gdamore USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
896 1.14 gdamore if (err != USBD_NORMAL_COMPLETION) {
897 1.14 gdamore error = EIO;
898 1.14 gdamore goto bad;
899 1.5 dsainty }
900 1.22.10.1 itohy sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev, sc->sc_aclwr_pipe);
901 1.14 gdamore if (sc->sc_aclwr_xfer == NULL) {
902 1.5 dsainty error = ENOMEM;
903 1.14 gdamore goto bad;
904 1.5 dsainty }
905 1.14 gdamore sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
906 1.5 dsainty if (sc->sc_aclwr_buf == NULL) {
907 1.5 dsainty error = ENOMEM;
908 1.14 gdamore goto bad;
909 1.14 gdamore }
910 1.14 gdamore
911 1.14 gdamore /* SCO read */
912 1.14 gdamore if (sc->sc_scord_size > 0) {
913 1.14 gdamore err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
914 1.14 gdamore USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
915 1.14 gdamore if (err != USBD_NORMAL_COMPLETION) {
916 1.14 gdamore error = EIO;
917 1.14 gdamore goto bad;
918 1.14 gdamore }
919 1.14 gdamore
920 1.14 gdamore for (i = 0 ; i < UBT_NXFERS ; i++) {
921 1.22.10.1 itohy sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev,
922 1.22.10.1 itohy sc->sc_scord_pipe);
923 1.14 gdamore if (sc->sc_scord[i].xfer == NULL) {
924 1.14 gdamore error = ENOMEM;
925 1.14 gdamore goto bad;
926 1.14 gdamore }
927 1.14 gdamore sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
928 1.14 gdamore sc->sc_scord_size * UBT_NFRAMES);
929 1.14 gdamore if (sc->sc_scord[i].buf == NULL) {
930 1.14 gdamore error = ENOMEM;
931 1.14 gdamore goto bad;
932 1.14 gdamore }
933 1.14 gdamore sc->sc_scord[i].softc = sc;
934 1.14 gdamore sc->sc_scord[i].busy = 0;
935 1.14 gdamore ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
936 1.14 gdamore }
937 1.5 dsainty }
938 1.5 dsainty
939 1.14 gdamore /* SCO write */
940 1.14 gdamore if (sc->sc_scowr_size > 0) {
941 1.14 gdamore err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
942 1.14 gdamore USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
943 1.14 gdamore if (err != USBD_NORMAL_COMPLETION) {
944 1.14 gdamore error = EIO;
945 1.14 gdamore goto bad;
946 1.14 gdamore }
947 1.14 gdamore
948 1.14 gdamore for (i = 0 ; i < UBT_NXFERS ; i++) {
949 1.22.10.1 itohy sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev,
950 1.22.10.1 itohy sc->sc_scowr_pipe);
951 1.14 gdamore if (sc->sc_scowr[i].xfer == NULL) {
952 1.14 gdamore error = ENOMEM;
953 1.14 gdamore goto bad;
954 1.14 gdamore }
955 1.14 gdamore sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
956 1.14 gdamore sc->sc_scowr_size * UBT_NFRAMES);
957 1.14 gdamore if (sc->sc_scowr[i].buf == NULL) {
958 1.14 gdamore error = ENOMEM;
959 1.14 gdamore goto bad;
960 1.14 gdamore }
961 1.14 gdamore sc->sc_scowr[i].softc = sc;
962 1.14 gdamore sc->sc_scowr[i].busy = 0;
963 1.14 gdamore }
964 1.14 gdamore }
965 1.5 dsainty
966 1.14 gdamore unit->hci_flags &= ~BTF_XMIT;
967 1.14 gdamore unit->hci_flags |= BTF_RUNNING;
968 1.5 dsainty return 0;
969 1.5 dsainty
970 1.14 gdamore bad:
971 1.14 gdamore ubt_abortdealloc(sc);
972 1.5 dsainty return error;
973 1.5 dsainty }
974 1.5 dsainty
975 1.14 gdamore static void
976 1.14 gdamore ubt_disable(struct hci_unit *unit)
977 1.5 dsainty {
978 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
979 1.14 gdamore
980 1.14 gdamore DPRINTFN(1, "sc=%p\n", sc);
981 1.5 dsainty
982 1.14 gdamore if ((unit->hci_flags & BTF_RUNNING) == 0)
983 1.14 gdamore return;
984 1.5 dsainty
985 1.5 dsainty ubt_abortdealloc(sc);
986 1.5 dsainty
987 1.14 gdamore unit->hci_flags &= ~BTF_RUNNING;
988 1.5 dsainty }
989 1.5 dsainty
990 1.14 gdamore static void
991 1.14 gdamore ubt_xmit_cmd_start(struct hci_unit *unit)
992 1.5 dsainty {
993 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
994 1.5 dsainty usb_device_request_t req;
995 1.5 dsainty usbd_status status;
996 1.14 gdamore struct mbuf *m;
997 1.14 gdamore int len;
998 1.5 dsainty
999 1.14 gdamore if (sc->sc_dying)
1000 1.14 gdamore return;
1001 1.5 dsainty
1002 1.14 gdamore if (MBUFQ_FIRST(&unit->hci_cmdq) == NULL)
1003 1.14 gdamore return;
1004 1.6 dsainty
1005 1.14 gdamore MBUFQ_DEQUEUE(&unit->hci_cmdq, m);
1006 1.14 gdamore KASSERT(m != NULL);
1007 1.5 dsainty
1008 1.14 gdamore DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
1009 1.14 gdamore unit->hci_devname, m->m_pkthdr.len);
1010 1.5 dsainty
1011 1.5 dsainty sc->sc_refcnt++;
1012 1.14 gdamore unit->hci_flags |= BTF_XMIT_CMD;
1013 1.14 gdamore
1014 1.14 gdamore len = m->m_pkthdr.len - 1;
1015 1.14 gdamore m_copydata(m, 1, len, sc->sc_cmd_buf);
1016 1.14 gdamore m_freem(m);
1017 1.5 dsainty
1018 1.5 dsainty memset(&req, 0, sizeof(req));
1019 1.5 dsainty req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1020 1.5 dsainty USETW(req.wLength, len);
1021 1.5 dsainty
1022 1.14 gdamore usbd_setup_default_xfer(sc->sc_cmd_xfer,
1023 1.5 dsainty sc->sc_udev,
1024 1.14 gdamore unit,
1025 1.14 gdamore UBT_CMD_TIMEOUT,
1026 1.14 gdamore &req,
1027 1.14 gdamore sc->sc_cmd_buf,
1028 1.14 gdamore len,
1029 1.14 gdamore USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1030 1.14 gdamore ubt_xmit_cmd_complete);
1031 1.14 gdamore
1032 1.14 gdamore status = usbd_transfer(sc->sc_cmd_xfer);
1033 1.14 gdamore
1034 1.14 gdamore KASSERT(status != USBD_NORMAL_COMPLETION);
1035 1.14 gdamore
1036 1.14 gdamore if (status != USBD_IN_PROGRESS) {
1037 1.14 gdamore DPRINTF("usbd_transfer status=%s (%d)\n",
1038 1.14 gdamore usbd_errstr(status), status);
1039 1.14 gdamore
1040 1.14 gdamore sc->sc_refcnt--;
1041 1.14 gdamore unit->hci_flags &= ~BTF_XMIT_CMD;
1042 1.14 gdamore }
1043 1.14 gdamore }
1044 1.14 gdamore
1045 1.14 gdamore static void
1046 1.14 gdamore ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
1047 1.14 gdamore usbd_private_handle h, usbd_status status)
1048 1.14 gdamore {
1049 1.14 gdamore struct hci_unit *unit = h;
1050 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
1051 1.14 gdamore uint32_t count;
1052 1.5 dsainty
1053 1.14 gdamore DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
1054 1.14 gdamore unit->hci_devname, usbd_errstr(status), status);
1055 1.5 dsainty
1056 1.14 gdamore unit->hci_flags &= ~BTF_XMIT_CMD;
1057 1.14 gdamore
1058 1.14 gdamore if (--sc->sc_refcnt < 0) {
1059 1.14 gdamore DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
1060 1.5 dsainty usb_detach_wakeup(USBDEV(sc->sc_dev));
1061 1.14 gdamore return;
1062 1.14 gdamore }
1063 1.5 dsainty
1064 1.14 gdamore if (sc->sc_dying) {
1065 1.14 gdamore DPRINTF("sc_dying\n");
1066 1.14 gdamore return;
1067 1.14 gdamore }
1068 1.5 dsainty
1069 1.14 gdamore if (status != USBD_NORMAL_COMPLETION) {
1070 1.14 gdamore DPRINTF("status=%s (%d)\n",
1071 1.14 gdamore usbd_errstr(status), status);
1072 1.5 dsainty
1073 1.14 gdamore unit->hci_stats.err_tx++;
1074 1.14 gdamore return;
1075 1.14 gdamore }
1076 1.6 dsainty
1077 1.14 gdamore usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1078 1.14 gdamore unit->hci_stats.cmd_tx++;
1079 1.14 gdamore unit->hci_stats.byte_tx += count;
1080 1.6 dsainty
1081 1.14 gdamore ubt_xmit_cmd_start(unit);
1082 1.6 dsainty }
1083 1.6 dsainty
1084 1.14 gdamore static void
1085 1.14 gdamore ubt_xmit_acl_start(struct hci_unit *unit)
1086 1.5 dsainty {
1087 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
1088 1.14 gdamore struct mbuf *m;
1089 1.5 dsainty usbd_status status;
1090 1.14 gdamore int len;
1091 1.14 gdamore
1092 1.14 gdamore if (sc->sc_dying)
1093 1.14 gdamore return;
1094 1.14 gdamore
1095 1.14 gdamore if (MBUFQ_FIRST(&unit->hci_acltxq) == NULL)
1096 1.14 gdamore return;
1097 1.5 dsainty
1098 1.14 gdamore sc->sc_refcnt++;
1099 1.14 gdamore unit->hci_flags |= BTF_XMIT_ACL;
1100 1.14 gdamore
1101 1.14 gdamore MBUFQ_DEQUEUE(&unit->hci_acltxq, m);
1102 1.14 gdamore KASSERT(m != NULL);
1103 1.5 dsainty
1104 1.14 gdamore DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
1105 1.14 gdamore unit->hci_devname, m->m_pkthdr.len);
1106 1.14 gdamore
1107 1.14 gdamore len = m->m_pkthdr.len - 1;
1108 1.14 gdamore if (len > UBT_BUFSIZ_ACL) {
1109 1.14 gdamore DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
1110 1.14 gdamore unit->hci_devname, len, UBT_BUFSIZ_ACL);
1111 1.6 dsainty
1112 1.14 gdamore len = UBT_BUFSIZ_ACL;
1113 1.14 gdamore }
1114 1.5 dsainty
1115 1.14 gdamore m_copydata(m, 1, len, sc->sc_aclwr_buf);
1116 1.14 gdamore m_freem(m);
1117 1.5 dsainty
1118 1.14 gdamore unit->hci_stats.acl_tx++;
1119 1.14 gdamore unit->hci_stats.byte_tx += len;
1120 1.5 dsainty
1121 1.5 dsainty usbd_setup_xfer(sc->sc_aclwr_xfer,
1122 1.5 dsainty sc->sc_aclwr_pipe,
1123 1.14 gdamore unit,
1124 1.14 gdamore sc->sc_aclwr_buf,
1125 1.14 gdamore len,
1126 1.14 gdamore USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1127 1.14 gdamore UBT_ACL_TIMEOUT,
1128 1.14 gdamore ubt_xmit_acl_complete);
1129 1.5 dsainty
1130 1.5 dsainty status = usbd_transfer(sc->sc_aclwr_xfer);
1131 1.5 dsainty
1132 1.14 gdamore KASSERT(status != USBD_NORMAL_COMPLETION);
1133 1.14 gdamore
1134 1.14 gdamore if (status != USBD_IN_PROGRESS) {
1135 1.14 gdamore DPRINTF("usbd_transfer status=%s (%d)\n",
1136 1.14 gdamore usbd_errstr(status), status);
1137 1.14 gdamore
1138 1.14 gdamore sc->sc_refcnt--;
1139 1.14 gdamore unit->hci_flags &= ~BTF_XMIT_ACL;
1140 1.14 gdamore }
1141 1.14 gdamore }
1142 1.14 gdamore
1143 1.14 gdamore static void
1144 1.20 christos ubt_xmit_acl_complete(usbd_xfer_handle xfer,
1145 1.14 gdamore usbd_private_handle h, usbd_status status)
1146 1.14 gdamore {
1147 1.14 gdamore struct hci_unit *unit = h;
1148 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
1149 1.14 gdamore
1150 1.14 gdamore DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
1151 1.14 gdamore unit->hci_devname, usbd_errstr(status), status);
1152 1.14 gdamore
1153 1.14 gdamore unit->hci_flags &= ~BTF_XMIT_ACL;
1154 1.14 gdamore
1155 1.14 gdamore if (--sc->sc_refcnt < 0) {
1156 1.5 dsainty usb_detach_wakeup(USBDEV(sc->sc_dev));
1157 1.14 gdamore return;
1158 1.14 gdamore }
1159 1.5 dsainty
1160 1.14 gdamore if (sc->sc_dying)
1161 1.14 gdamore return;
1162 1.5 dsainty
1163 1.14 gdamore if (status != USBD_NORMAL_COMPLETION) {
1164 1.14 gdamore DPRINTF("status=%s (%d)\n",
1165 1.14 gdamore usbd_errstr(status), status);
1166 1.14 gdamore
1167 1.14 gdamore unit->hci_stats.err_tx++;
1168 1.14 gdamore
1169 1.14 gdamore if (status == USBD_STALLED)
1170 1.14 gdamore usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
1171 1.14 gdamore else
1172 1.14 gdamore return;
1173 1.14 gdamore }
1174 1.14 gdamore
1175 1.14 gdamore ubt_xmit_acl_start(unit);
1176 1.14 gdamore }
1177 1.14 gdamore
1178 1.14 gdamore static void
1179 1.14 gdamore ubt_xmit_sco_start(struct hci_unit *unit)
1180 1.14 gdamore {
1181 1.14 gdamore struct ubt_softc *sc = unit->hci_softc;
1182 1.14 gdamore int i;
1183 1.14 gdamore
1184 1.14 gdamore if (sc->sc_dying || sc->sc_scowr_size == 0)
1185 1.14 gdamore return;
1186 1.14 gdamore
1187 1.14 gdamore for (i = 0 ; i < UBT_NXFERS ; i++) {
1188 1.14 gdamore if (sc->sc_scowr[i].busy)
1189 1.14 gdamore continue;
1190 1.14 gdamore
1191 1.14 gdamore ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
1192 1.14 gdamore }
1193 1.5 dsainty }
1194 1.5 dsainty
1195 1.14 gdamore static void
1196 1.14 gdamore ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1197 1.6 dsainty {
1198 1.14 gdamore struct mbuf *m;
1199 1.14 gdamore uint8_t *buf;
1200 1.14 gdamore int num, len, size, space;
1201 1.14 gdamore
1202 1.14 gdamore space = sc->sc_scowr_size * UBT_NFRAMES;
1203 1.14 gdamore buf = isoc->buf;
1204 1.14 gdamore len = 0;
1205 1.14 gdamore
1206 1.14 gdamore /*
1207 1.14 gdamore * Fill the request buffer with data from the queue,
1208 1.14 gdamore * keeping any leftover packet on our private hook.
1209 1.14 gdamore *
1210 1.14 gdamore * Complete packets are passed back up to the stack
1211 1.14 gdamore * for disposal, since we can't rely on the controller
1212 1.14 gdamore * to tell us when it has finished with them.
1213 1.14 gdamore */
1214 1.14 gdamore
1215 1.14 gdamore m = sc->sc_scowr_mbuf;
1216 1.14 gdamore while (space > 0) {
1217 1.14 gdamore if (m == NULL) {
1218 1.14 gdamore MBUFQ_DEQUEUE(&sc->sc_unit.hci_scotxq, m);
1219 1.14 gdamore if (m == NULL)
1220 1.14 gdamore break;
1221 1.14 gdamore
1222 1.14 gdamore m_adj(m, 1); /* packet type */
1223 1.14 gdamore }
1224 1.14 gdamore
1225 1.14 gdamore if (m->m_pkthdr.len > 0) {
1226 1.14 gdamore size = MIN(m->m_pkthdr.len, space);
1227 1.14 gdamore
1228 1.14 gdamore m_copydata(m, 0, size, buf);
1229 1.14 gdamore m_adj(m, size);
1230 1.14 gdamore
1231 1.14 gdamore buf += size;
1232 1.14 gdamore len += size;
1233 1.14 gdamore space -= size;
1234 1.14 gdamore }
1235 1.14 gdamore
1236 1.14 gdamore if (m->m_pkthdr.len == 0) {
1237 1.14 gdamore sc->sc_unit.hci_stats.sco_tx++;
1238 1.14 gdamore hci_complete_sco(&sc->sc_unit, m);
1239 1.14 gdamore m = NULL;
1240 1.14 gdamore }
1241 1.14 gdamore }
1242 1.14 gdamore sc->sc_scowr_mbuf = m;
1243 1.14 gdamore
1244 1.14 gdamore DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
1245 1.14 gdamore
1246 1.14 gdamore if (len == 0) /* nothing to send */
1247 1.14 gdamore return;
1248 1.14 gdamore
1249 1.14 gdamore sc->sc_refcnt++;
1250 1.14 gdamore sc->sc_unit.hci_flags |= BTF_XMIT_SCO;
1251 1.14 gdamore sc->sc_unit.hci_stats.byte_tx += len;
1252 1.14 gdamore isoc->busy = 1;
1253 1.14 gdamore
1254 1.14 gdamore /*
1255 1.14 gdamore * calculate number of isoc frames and sizes
1256 1.14 gdamore */
1257 1.14 gdamore
1258 1.14 gdamore for (num = 0 ; len > 0 ; num++) {
1259 1.14 gdamore size = MIN(sc->sc_scowr_size, len);
1260 1.14 gdamore
1261 1.14 gdamore isoc->size[num] = size;
1262 1.14 gdamore len -= size;
1263 1.14 gdamore }
1264 1.14 gdamore
1265 1.14 gdamore usbd_setup_isoc_xfer(isoc->xfer,
1266 1.14 gdamore sc->sc_scowr_pipe,
1267 1.14 gdamore isoc,
1268 1.14 gdamore isoc->size,
1269 1.14 gdamore num,
1270 1.14 gdamore USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1271 1.14 gdamore ubt_xmit_sco_complete);
1272 1.14 gdamore
1273 1.14 gdamore usbd_transfer(isoc->xfer);
1274 1.6 dsainty }
1275 1.6 dsainty
1276 1.14 gdamore static void
1277 1.20 christos ubt_xmit_sco_complete(usbd_xfer_handle xfer,
1278 1.14 gdamore usbd_private_handle h, usbd_status status)
1279 1.5 dsainty {
1280 1.14 gdamore struct ubt_isoc_xfer *isoc = h;
1281 1.14 gdamore struct ubt_softc *sc;
1282 1.14 gdamore int i;
1283 1.14 gdamore
1284 1.14 gdamore KASSERT(xfer == isoc->xfer);
1285 1.14 gdamore sc = isoc->softc;
1286 1.14 gdamore
1287 1.14 gdamore DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
1288 1.14 gdamore isoc, usbd_errstr(status), status);
1289 1.14 gdamore
1290 1.14 gdamore isoc->busy = 0;
1291 1.14 gdamore
1292 1.14 gdamore for (i = 0 ; ; i++) {
1293 1.14 gdamore if (i == UBT_NXFERS) {
1294 1.14 gdamore sc->sc_unit.hci_flags &= ~BTF_XMIT_SCO;
1295 1.14 gdamore break;
1296 1.14 gdamore }
1297 1.5 dsainty
1298 1.14 gdamore if (sc->sc_scowr[i].busy)
1299 1.14 gdamore break;
1300 1.14 gdamore }
1301 1.14 gdamore
1302 1.14 gdamore if (--sc->sc_refcnt < 0) {
1303 1.14 gdamore usb_detach_wakeup(USBDEV(sc->sc_dev));
1304 1.14 gdamore return;
1305 1.14 gdamore }
1306 1.5 dsainty
1307 1.5 dsainty if (sc->sc_dying)
1308 1.14 gdamore return;
1309 1.5 dsainty
1310 1.14 gdamore if (status != USBD_NORMAL_COMPLETION) {
1311 1.14 gdamore DPRINTF("status=%s (%d)\n",
1312 1.14 gdamore usbd_errstr(status), status);
1313 1.14 gdamore
1314 1.14 gdamore sc->sc_unit.hci_stats.err_tx++;
1315 1.14 gdamore
1316 1.14 gdamore if (status == USBD_STALLED)
1317 1.14 gdamore usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
1318 1.14 gdamore else
1319 1.14 gdamore return;
1320 1.14 gdamore }
1321 1.14 gdamore
1322 1.14 gdamore ubt_xmit_sco_start(&sc->sc_unit);
1323 1.14 gdamore }
1324 1.14 gdamore
1325 1.14 gdamore /*
1326 1.14 gdamore * load incoming data into an mbuf with
1327 1.14 gdamore * leading type byte
1328 1.14 gdamore */
1329 1.14 gdamore static struct mbuf *
1330 1.14 gdamore ubt_mbufload(uint8_t *buf, int count, uint8_t type)
1331 1.14 gdamore {
1332 1.14 gdamore struct mbuf *m;
1333 1.14 gdamore
1334 1.14 gdamore MGETHDR(m, M_DONTWAIT, MT_DATA);
1335 1.14 gdamore if (m == NULL)
1336 1.14 gdamore return NULL;
1337 1.14 gdamore
1338 1.14 gdamore *mtod(m, uint8_t *) = type;
1339 1.14 gdamore m->m_pkthdr.len = m->m_len = MHLEN;
1340 1.14 gdamore m_copyback(m, 1, count, buf); // (extends if necessary)
1341 1.14 gdamore if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
1342 1.14 gdamore m_free(m);
1343 1.14 gdamore return NULL;
1344 1.14 gdamore }
1345 1.14 gdamore
1346 1.14 gdamore m->m_pkthdr.len = count + 1;
1347 1.14 gdamore m->m_len = MIN(MHLEN, m->m_pkthdr.len);
1348 1.14 gdamore
1349 1.14 gdamore return m;
1350 1.5 dsainty }
1351 1.5 dsainty
1352 1.5 dsainty static void
1353 1.14 gdamore ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
1354 1.5 dsainty {
1355 1.5 dsainty struct ubt_softc *sc = h;
1356 1.14 gdamore struct mbuf *m;
1357 1.14 gdamore uint32_t count;
1358 1.5 dsainty void *buf;
1359 1.5 dsainty
1360 1.14 gdamore DPRINTFN(15, "sc=%p status=%s (%d)\n",
1361 1.14 gdamore sc, usbd_errstr(status), status);
1362 1.5 dsainty
1363 1.14 gdamore if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
1364 1.5 dsainty return;
1365 1.5 dsainty
1366 1.14 gdamore usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1367 1.14 gdamore
1368 1.16 plunky if (count < sizeof(hci_event_hdr_t) - 1) {
1369 1.16 plunky DPRINTF("dumped undersized event (count = %d)\n", count);
1370 1.16 plunky sc->sc_unit.hci_stats.err_rx++;
1371 1.16 plunky return;
1372 1.16 plunky }
1373 1.16 plunky
1374 1.14 gdamore sc->sc_unit.hci_stats.evt_rx++;
1375 1.14 gdamore sc->sc_unit.hci_stats.byte_rx += count;
1376 1.5 dsainty
1377 1.14 gdamore m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
1378 1.14 gdamore if (m != NULL)
1379 1.14 gdamore hci_input_event(&sc->sc_unit, m);
1380 1.14 gdamore else
1381 1.14 gdamore sc->sc_unit.hci_stats.err_rx++;
1382 1.5 dsainty }
1383 1.5 dsainty
1384 1.5 dsainty static void
1385 1.14 gdamore ubt_recv_acl_start(struct ubt_softc *sc)
1386 1.5 dsainty {
1387 1.5 dsainty usbd_status status;
1388 1.5 dsainty
1389 1.14 gdamore DPRINTFN(15, "sc=%p\n", sc);
1390 1.5 dsainty
1391 1.14 gdamore if (sc->sc_aclrd_busy || sc->sc_dying) {
1392 1.14 gdamore DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
1393 1.14 gdamore sc->sc_aclrd_busy,
1394 1.14 gdamore sc->sc_dying);
1395 1.5 dsainty
1396 1.5 dsainty return;
1397 1.5 dsainty }
1398 1.6 dsainty
1399 1.14 gdamore sc->sc_refcnt++;
1400 1.14 gdamore sc->sc_aclrd_busy = 1;
1401 1.14 gdamore
1402 1.14 gdamore usbd_setup_xfer(sc->sc_aclrd_xfer,
1403 1.14 gdamore sc->sc_aclrd_pipe,
1404 1.14 gdamore sc,
1405 1.14 gdamore sc->sc_aclrd_buf,
1406 1.14 gdamore UBT_BUFSIZ_ACL,
1407 1.14 gdamore USBD_NO_COPY | USBD_SHORT_XFER_OK,
1408 1.14 gdamore USBD_NO_TIMEOUT,
1409 1.14 gdamore ubt_recv_acl_complete);
1410 1.5 dsainty
1411 1.5 dsainty status = usbd_transfer(sc->sc_aclrd_xfer);
1412 1.9 dsainty
1413 1.14 gdamore KASSERT(status != USBD_NORMAL_COMPLETION);
1414 1.5 dsainty
1415 1.14 gdamore if (status != USBD_IN_PROGRESS) {
1416 1.14 gdamore DPRINTF("usbd_transfer status=%s (%d)\n",
1417 1.14 gdamore usbd_errstr(status), status);
1418 1.5 dsainty
1419 1.14 gdamore sc->sc_refcnt--;
1420 1.14 gdamore sc->sc_aclrd_busy = 0;
1421 1.14 gdamore }
1422 1.5 dsainty }
1423 1.5 dsainty
1424 1.5 dsainty static void
1425 1.14 gdamore ubt_recv_acl_complete(usbd_xfer_handle xfer,
1426 1.14 gdamore usbd_private_handle h, usbd_status status)
1427 1.5 dsainty {
1428 1.5 dsainty struct ubt_softc *sc = h;
1429 1.14 gdamore struct mbuf *m;
1430 1.14 gdamore uint32_t count;
1431 1.5 dsainty void *buf;
1432 1.5 dsainty
1433 1.14 gdamore DPRINTFN(15, "sc=%p status=%s (%d)\n",
1434 1.14 gdamore sc, usbd_errstr(status), status);
1435 1.14 gdamore
1436 1.14 gdamore sc->sc_aclrd_busy = 0;
1437 1.14 gdamore
1438 1.14 gdamore if (--sc->sc_refcnt < 0) {
1439 1.14 gdamore DPRINTF("refcnt = %d\n", sc->sc_refcnt);
1440 1.14 gdamore usb_detach_wakeup(USBDEV(sc->sc_dev));
1441 1.14 gdamore return;
1442 1.14 gdamore }
1443 1.14 gdamore
1444 1.14 gdamore if (sc->sc_dying) {
1445 1.14 gdamore DPRINTF("sc_dying\n");
1446 1.14 gdamore return;
1447 1.14 gdamore }
1448 1.14 gdamore
1449 1.14 gdamore if (status != USBD_NORMAL_COMPLETION) {
1450 1.14 gdamore DPRINTF("status=%s (%d)\n",
1451 1.14 gdamore usbd_errstr(status), status);
1452 1.14 gdamore
1453 1.14 gdamore sc->sc_unit.hci_stats.err_rx++;
1454 1.14 gdamore
1455 1.14 gdamore if (status == USBD_STALLED)
1456 1.14 gdamore usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
1457 1.14 gdamore else
1458 1.14 gdamore return;
1459 1.14 gdamore } else {
1460 1.14 gdamore usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1461 1.14 gdamore
1462 1.16 plunky if (count < sizeof(hci_acldata_hdr_t) - 1) {
1463 1.16 plunky DPRINTF("dumped undersized packet (%d)\n", count);
1464 1.16 plunky sc->sc_unit.hci_stats.err_rx++;
1465 1.16 plunky } else {
1466 1.16 plunky sc->sc_unit.hci_stats.acl_rx++;
1467 1.16 plunky sc->sc_unit.hci_stats.byte_rx += count;
1468 1.14 gdamore
1469 1.16 plunky m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
1470 1.16 plunky if (m != NULL)
1471 1.16 plunky hci_input_acl(&sc->sc_unit, m);
1472 1.16 plunky else
1473 1.16 plunky sc->sc_unit.hci_stats.err_rx++;
1474 1.16 plunky }
1475 1.14 gdamore }
1476 1.5 dsainty
1477 1.14 gdamore /* and restart */
1478 1.14 gdamore ubt_recv_acl_start(sc);
1479 1.14 gdamore }
1480 1.14 gdamore
1481 1.14 gdamore static void
1482 1.14 gdamore ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1483 1.14 gdamore {
1484 1.14 gdamore int i;
1485 1.14 gdamore
1486 1.14 gdamore DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
1487 1.14 gdamore
1488 1.14 gdamore if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
1489 1.14 gdamore DPRINTF("%s%s%s\n",
1490 1.14 gdamore isoc->busy ? " busy" : "",
1491 1.14 gdamore sc->sc_dying ? " dying" : "",
1492 1.14 gdamore sc->sc_scord_size == 0 ? " size=0" : "");
1493 1.5 dsainty
1494 1.5 dsainty return;
1495 1.14 gdamore }
1496 1.14 gdamore
1497 1.14 gdamore sc->sc_refcnt++;
1498 1.14 gdamore isoc->busy = 1;
1499 1.5 dsainty
1500 1.14 gdamore for (i = 0 ; i < UBT_NFRAMES ; i++)
1501 1.14 gdamore isoc->size[i] = sc->sc_scord_size;
1502 1.5 dsainty
1503 1.14 gdamore usbd_setup_isoc_xfer(isoc->xfer,
1504 1.14 gdamore sc->sc_scord_pipe,
1505 1.14 gdamore isoc,
1506 1.14 gdamore isoc->size,
1507 1.14 gdamore UBT_NFRAMES,
1508 1.14 gdamore USBD_NO_COPY | USBD_SHORT_XFER_OK,
1509 1.14 gdamore ubt_recv_sco_complete);
1510 1.5 dsainty
1511 1.14 gdamore usbd_transfer(isoc->xfer);
1512 1.5 dsainty }
1513 1.5 dsainty
1514 1.14 gdamore static void
1515 1.14 gdamore ubt_recv_sco_complete(usbd_xfer_handle xfer,
1516 1.14 gdamore usbd_private_handle h, usbd_status status)
1517 1.5 dsainty {
1518 1.14 gdamore struct ubt_isoc_xfer *isoc = h;
1519 1.14 gdamore struct ubt_softc *sc;
1520 1.14 gdamore struct mbuf *m;
1521 1.14 gdamore uint32_t count;
1522 1.14 gdamore uint8_t *ptr, *frame;
1523 1.14 gdamore int i, size, got, want;
1524 1.14 gdamore
1525 1.14 gdamore KASSERT(isoc != NULL);
1526 1.14 gdamore KASSERT(isoc->xfer == xfer);
1527 1.14 gdamore
1528 1.14 gdamore sc = isoc->softc;
1529 1.14 gdamore isoc->busy = 0;
1530 1.14 gdamore
1531 1.14 gdamore if (--sc->sc_refcnt < 0) {
1532 1.14 gdamore DPRINTF("refcnt=%d\n", sc->sc_refcnt);
1533 1.14 gdamore usb_detach_wakeup(USBDEV(sc->sc_dev));
1534 1.14 gdamore return;
1535 1.14 gdamore }
1536 1.14 gdamore
1537 1.14 gdamore if (sc->sc_dying) {
1538 1.14 gdamore DPRINTF("sc_dying\n");
1539 1.14 gdamore return;
1540 1.14 gdamore }
1541 1.14 gdamore
1542 1.14 gdamore if (status != USBD_NORMAL_COMPLETION) {
1543 1.14 gdamore DPRINTF("status=%s (%d)\n",
1544 1.14 gdamore usbd_errstr(status), status);
1545 1.14 gdamore
1546 1.14 gdamore sc->sc_unit.hci_stats.err_rx++;
1547 1.14 gdamore
1548 1.14 gdamore if (status == USBD_STALLED) {
1549 1.14 gdamore usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
1550 1.14 gdamore goto restart;
1551 1.14 gdamore }
1552 1.14 gdamore
1553 1.14 gdamore return;
1554 1.14 gdamore }
1555 1.14 gdamore
1556 1.14 gdamore usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1557 1.14 gdamore if (count == 0)
1558 1.14 gdamore goto restart;
1559 1.14 gdamore
1560 1.14 gdamore DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
1561 1.14 gdamore sc, isoc, count);
1562 1.14 gdamore
1563 1.14 gdamore sc->sc_unit.hci_stats.byte_rx += count;
1564 1.14 gdamore
1565 1.14 gdamore /*
1566 1.14 gdamore * Extract SCO packets from ISOC frames. The way we have it,
1567 1.14 gdamore * no SCO packet can be bigger than MHLEN. This is unlikely
1568 1.14 gdamore * to actually happen, but if we ran out of mbufs and lost
1569 1.14 gdamore * sync then we may get spurious data that makes it seem that
1570 1.14 gdamore * way, so we discard data that wont fit. This doesnt really
1571 1.14 gdamore * help with the lost sync situation alas.
1572 1.14 gdamore */
1573 1.14 gdamore
1574 1.14 gdamore m = sc->sc_scord_mbuf;
1575 1.14 gdamore if (m != NULL) {
1576 1.14 gdamore sc->sc_scord_mbuf = NULL;
1577 1.14 gdamore ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
1578 1.14 gdamore got = m->m_pkthdr.len;
1579 1.14 gdamore want = sizeof(hci_scodata_hdr_t);
1580 1.14 gdamore if (got >= want)
1581 1.14 gdamore want += mtod(m, hci_scodata_hdr_t *)->length ;
1582 1.14 gdamore } else {
1583 1.14 gdamore ptr = NULL;
1584 1.14 gdamore got = 0;
1585 1.14 gdamore want = 0;
1586 1.14 gdamore }
1587 1.14 gdamore
1588 1.14 gdamore for (i = 0 ; i < UBT_NFRAMES ; i++) {
1589 1.14 gdamore frame = isoc->buf + (i * sc->sc_scord_size);
1590 1.14 gdamore
1591 1.14 gdamore while (isoc->size[i] > 0) {
1592 1.14 gdamore size = isoc->size[i];
1593 1.14 gdamore
1594 1.14 gdamore if (m == NULL) {
1595 1.14 gdamore MGETHDR(m, M_DONTWAIT, MT_DATA);
1596 1.14 gdamore if (m == NULL) {
1597 1.14 gdamore printf("%s: out of memory (xfer halted)\n",
1598 1.14 gdamore USBDEVNAME(sc->sc_dev));
1599 1.14 gdamore
1600 1.14 gdamore sc->sc_unit.hci_stats.err_rx++;
1601 1.14 gdamore return; /* lost sync */
1602 1.14 gdamore }
1603 1.14 gdamore
1604 1.14 gdamore ptr = mtod(m, uint8_t *);
1605 1.14 gdamore *ptr++ = HCI_SCO_DATA_PKT;
1606 1.14 gdamore got = 1;
1607 1.14 gdamore want = sizeof(hci_scodata_hdr_t);
1608 1.14 gdamore }
1609 1.14 gdamore
1610 1.14 gdamore if (got + size > want)
1611 1.14 gdamore size = want - got;
1612 1.14 gdamore
1613 1.14 gdamore if (got + size > MHLEN)
1614 1.14 gdamore memcpy(ptr, frame, MHLEN - got);
1615 1.14 gdamore else
1616 1.14 gdamore memcpy(ptr, frame, size);
1617 1.14 gdamore
1618 1.14 gdamore ptr += size;
1619 1.14 gdamore got += size;
1620 1.14 gdamore frame += size;
1621 1.14 gdamore
1622 1.14 gdamore if (got == want) {
1623 1.14 gdamore /*
1624 1.14 gdamore * If we only got a header, add the packet
1625 1.14 gdamore * length to our want count. Send complete
1626 1.14 gdamore * packets up to protocol stack.
1627 1.14 gdamore */
1628 1.14 gdamore if (want == sizeof(hci_scodata_hdr_t))
1629 1.14 gdamore want += mtod(m, hci_scodata_hdr_t *)->length;
1630 1.14 gdamore
1631 1.14 gdamore if (got == want) {
1632 1.14 gdamore m->m_pkthdr.len = m->m_len = got;
1633 1.14 gdamore sc->sc_unit.hci_stats.sco_rx++;
1634 1.14 gdamore hci_input_sco(&sc->sc_unit, m);
1635 1.14 gdamore m = NULL;
1636 1.14 gdamore }
1637 1.14 gdamore }
1638 1.14 gdamore
1639 1.14 gdamore isoc->size[i] -= size;
1640 1.14 gdamore }
1641 1.14 gdamore }
1642 1.14 gdamore
1643 1.14 gdamore if (m != NULL) {
1644 1.14 gdamore m->m_pkthdr.len = m->m_len = got;
1645 1.14 gdamore sc->sc_scord_mbuf = m;
1646 1.14 gdamore }
1647 1.14 gdamore
1648 1.14 gdamore restart: /* and restart */
1649 1.14 gdamore ubt_recv_sco_start1(sc, isoc);
1650 1.1 augustss }
1651