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