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