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