ubt.c revision 1.30.6.2 1 /* $NetBSD: ubt.c,v 1.30.6.2 2008/09/28 10:40:33 mjf 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.30.6.2 2008/09/28 10:40:33 mjf 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 (!pmf_device_register(self, NULL, NULL))
523 aprint_error_dev(self, "couldn't establish power handler\n");
524 USB_ATTACH_SUCCESS_RETURN;
525 }
526
527 USB_DETACH(ubt)
528 {
529 USB_DETACH_START(ubt, sc);
530 int s;
531
532 DPRINTF("sc=%p flags=%d\n", sc, flags);
533
534 if (device_pmf_is_registered(self))
535 pmf_device_deregister(self);
536
537 sc->sc_dying = 1;
538
539 if (!sc->sc_ok)
540 return 0;
541
542 /* delete sysctl nodes */
543 sysctl_teardown(&sc->sc_log);
544
545 /* Detach HCI interface */
546 if (sc->sc_unit) {
547 hci_detach(sc->sc_unit);
548 sc->sc_unit = NULL;
549 }
550
551 /*
552 * Abort all pipes. Causes processes waiting for transfer to wake.
553 *
554 * Actually, hci_detach() above will call ubt_disable() which may
555 * call ubt_abortdealloc(), but lets be sure since doing it twice
556 * wont cause an error.
557 */
558 ubt_abortdealloc(sc);
559
560 /* wait for all processes to finish */
561 s = splusb();
562 if (sc->sc_refcnt-- > 0)
563 usb_detach_wait(USBDEV(sc->sc_dev));
564
565 splx(s);
566
567 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
568 USBDEV(sc->sc_dev));
569
570 DPRINTFN(1, "driver detached\n");
571
572 return 0;
573 }
574
575 int
576 ubt_activate(device_ptr_t self, enum devact act)
577 {
578 struct ubt_softc *sc = USBGETSOFTC(self);
579 int error = 0;
580
581 DPRINTFN(1, "sc=%p, act=%d\n", sc, act);
582
583 switch (act) {
584 case DVACT_ACTIVATE:
585 break;
586
587 case DVACT_DEACTIVATE:
588 sc->sc_dying = 1;
589 break;
590
591 default:
592 error = EOPNOTSUPP;
593 break;
594 }
595
596 return error;
597 }
598
599 /* set ISOC configuration */
600 static int
601 ubt_set_isoc_config(struct ubt_softc *sc)
602 {
603 usb_endpoint_descriptor_t *ed;
604 int rd_addr, wr_addr, rd_size, wr_size;
605 uint8_t count, i;
606 int err;
607
608 err = usbd_set_interface(sc->sc_iface1, sc->sc_config);
609 if (err != USBD_NORMAL_COMPLETION) {
610 aprint_error_dev(sc->sc_dev,
611 "Could not set config %d on ISOC interface. %s (%d)\n",
612 sc->sc_config, usbd_errstr(err), err);
613
614 return err == USBD_IN_USE ? EBUSY : EIO;
615 }
616
617 /*
618 * We wont get past the above if there are any pipes open, so no
619 * need to worry about buf/xfer/pipe deallocation. If we get an
620 * error after this, the frame quantities will be 0 and no SCO
621 * data will be possible.
622 */
623
624 sc->sc_scord_size = rd_size = 0;
625 sc->sc_scord_addr = rd_addr = -1;
626
627 sc->sc_scowr_size = wr_size = 0;
628 sc->sc_scowr_addr = wr_addr = -1;
629
630 count = 0;
631 (void)usbd_endpoint_count(sc->sc_iface1, &count);
632
633 for (i = 0 ; i < count ; i++) {
634 ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i);
635 if (ed == NULL) {
636 aprint_error_dev(sc->sc_dev,
637 "could not read endpoint descriptor %d\n", i);
638
639 return EIO;
640 }
641
642 DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n",
643 USBDEVNAME(sc->sc_dev),
644 UE_GET_XFERTYPE(ed->bmAttributes),
645 UE_GET_ISO_TYPE(ed->bmAttributes),
646 ed->bEndpointAddress,
647 UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out");
648
649 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
650 continue;
651
652 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
653 rd_addr = ed->bEndpointAddress;
654 rd_size = UGETW(ed->wMaxPacketSize);
655 } else {
656 wr_addr = ed->bEndpointAddress;
657 wr_size = UGETW(ed->wMaxPacketSize);
658 }
659 }
660
661 if (rd_addr == -1) {
662 aprint_error_dev(sc->sc_dev,
663 "missing ISOC IN endpoint on interface config %d\n",
664 sc->sc_config);
665
666 return ENOENT;
667 }
668 if (wr_addr == -1) {
669 aprint_error_dev(sc->sc_dev,
670 "missing ISOC OUT endpoint on interface config %d\n",
671 sc->sc_config);
672
673 return ENOENT;
674 }
675
676 #ifdef DIAGNOSTIC
677 if (rd_size > MLEN) {
678 aprint_error_dev(sc->sc_dev, "rd_size=%d exceeds MLEN\n",
679 rd_size);
680
681 return EOVERFLOW;
682 }
683
684 if (wr_size > MLEN) {
685 aprint_error_dev(sc->sc_dev, "wr_size=%d exceeds MLEN\n",
686 wr_size);
687
688 return EOVERFLOW;
689 }
690 #endif
691
692 sc->sc_scord_size = rd_size;
693 sc->sc_scord_addr = rd_addr;
694
695 sc->sc_scowr_size = wr_size;
696 sc->sc_scowr_addr = wr_addr;
697
698 return 0;
699 }
700
701 /* sysctl helper to set alternate configurations */
702 static int
703 ubt_sysctl_config(SYSCTLFN_ARGS)
704 {
705 struct sysctlnode node;
706 struct ubt_softc *sc;
707 int t, error;
708
709 node = *rnode;
710 sc = node.sysctl_data;
711
712 t = sc->sc_config;
713 node.sysctl_data = &t;
714 error = sysctl_lookup(SYSCTLFN_CALL(&node));
715 if (error || newp == NULL)
716 return error;
717
718 if (t < 0 || t >= sc->sc_alt_config)
719 return EINVAL;
720
721 /* This may not change when the unit is enabled */
722 if (sc->sc_enabled)
723 return EBUSY;
724
725 sc->sc_config = t;
726 return ubt_set_isoc_config(sc);
727 }
728
729 static void
730 ubt_abortdealloc(struct ubt_softc *sc)
731 {
732 int i;
733
734 DPRINTFN(1, "sc=%p\n", sc);
735
736 /* Abort all pipes */
737 if (sc->sc_evt_pipe != NULL) {
738 usbd_abort_pipe(sc->sc_evt_pipe);
739 usbd_close_pipe(sc->sc_evt_pipe);
740 sc->sc_evt_pipe = NULL;
741 }
742
743 if (sc->sc_aclrd_pipe != NULL) {
744 usbd_abort_pipe(sc->sc_aclrd_pipe);
745 usbd_close_pipe(sc->sc_aclrd_pipe);
746 sc->sc_aclrd_pipe = NULL;
747 }
748
749 if (sc->sc_aclwr_pipe != NULL) {
750 usbd_abort_pipe(sc->sc_aclwr_pipe);
751 usbd_close_pipe(sc->sc_aclwr_pipe);
752 sc->sc_aclwr_pipe = NULL;
753 }
754
755 if (sc->sc_scord_pipe != NULL) {
756 usbd_abort_pipe(sc->sc_scord_pipe);
757 usbd_close_pipe(sc->sc_scord_pipe);
758 sc->sc_scord_pipe = NULL;
759 }
760
761 if (sc->sc_scowr_pipe != NULL) {
762 usbd_abort_pipe(sc->sc_scowr_pipe);
763 usbd_close_pipe(sc->sc_scowr_pipe);
764 sc->sc_scowr_pipe = NULL;
765 }
766
767 /* Free event buffer */
768 if (sc->sc_evt_buf != NULL) {
769 free(sc->sc_evt_buf, M_USBDEV);
770 sc->sc_evt_buf = NULL;
771 }
772
773 /* Free all xfers and xfer buffers (implicit) */
774 if (sc->sc_cmd_xfer != NULL) {
775 usbd_free_xfer(sc->sc_cmd_xfer);
776 sc->sc_cmd_xfer = NULL;
777 sc->sc_cmd_buf = NULL;
778 }
779
780 if (sc->sc_aclrd_xfer != NULL) {
781 usbd_free_xfer(sc->sc_aclrd_xfer);
782 sc->sc_aclrd_xfer = NULL;
783 sc->sc_aclrd_buf = NULL;
784 }
785
786 if (sc->sc_aclwr_xfer != NULL) {
787 usbd_free_xfer(sc->sc_aclwr_xfer);
788 sc->sc_aclwr_xfer = NULL;
789 sc->sc_aclwr_buf = NULL;
790 }
791
792 for (i = 0 ; i < UBT_NXFERS ; i++) {
793 if (sc->sc_scord[i].xfer != NULL) {
794 usbd_free_xfer(sc->sc_scord[i].xfer);
795 sc->sc_scord[i].xfer = NULL;
796 sc->sc_scord[i].buf = NULL;
797 }
798
799 if (sc->sc_scowr[i].xfer != NULL) {
800 usbd_free_xfer(sc->sc_scowr[i].xfer);
801 sc->sc_scowr[i].xfer = NULL;
802 sc->sc_scowr[i].buf = NULL;
803 }
804 }
805
806 /* Free partial SCO packets */
807 if (sc->sc_scord_mbuf != NULL) {
808 m_freem(sc->sc_scord_mbuf);
809 sc->sc_scord_mbuf = NULL;
810 }
811
812 if (sc->sc_scowr_mbuf != NULL) {
813 m_freem(sc->sc_scowr_mbuf);
814 sc->sc_scowr_mbuf = NULL;
815 }
816
817 /* Empty mbuf queues */
818 MBUFQ_DRAIN(&sc->sc_cmd_queue);
819 MBUFQ_DRAIN(&sc->sc_aclwr_queue);
820 MBUFQ_DRAIN(&sc->sc_scowr_queue);
821 }
822
823 /*******************************************************************************
824 *
825 * Bluetooth Unit/USB callbacks
826 *
827 */
828 static int
829 ubt_enable(device_ptr_t self)
830 {
831 struct ubt_softc *sc = USBGETSOFTC(self);
832 usbd_status err;
833 int s, i, error;
834
835 DPRINTFN(1, "sc=%p\n", sc);
836
837 if (sc->sc_enabled)
838 return 0;
839
840 s = splusb();
841
842 /* Events */
843 sc->sc_evt_buf = malloc(UBT_BUFSIZ_EVENT, M_USBDEV, M_NOWAIT);
844 if (sc->sc_evt_buf == NULL) {
845 error = ENOMEM;
846 goto bad;
847 }
848 err = usbd_open_pipe_intr(sc->sc_iface0,
849 sc->sc_evt_addr,
850 USBD_SHORT_XFER_OK,
851 &sc->sc_evt_pipe,
852 sc,
853 sc->sc_evt_buf,
854 UBT_BUFSIZ_EVENT,
855 ubt_recv_event,
856 USBD_DEFAULT_INTERVAL);
857 if (err != USBD_NORMAL_COMPLETION) {
858 error = EIO;
859 goto bad;
860 }
861
862 /* Commands */
863 sc->sc_cmd_xfer = usbd_alloc_xfer(sc->sc_udev);
864 if (sc->sc_cmd_xfer == NULL) {
865 error = ENOMEM;
866 goto bad;
867 }
868 sc->sc_cmd_buf = usbd_alloc_buffer(sc->sc_cmd_xfer, UBT_BUFSIZ_CMD);
869 if (sc->sc_cmd_buf == NULL) {
870 error = ENOMEM;
871 goto bad;
872 }
873 sc->sc_cmd_busy = 0;
874
875 /* ACL read */
876 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclrd_addr,
877 USBD_EXCLUSIVE_USE, &sc->sc_aclrd_pipe);
878 if (err != USBD_NORMAL_COMPLETION) {
879 error = EIO;
880 goto bad;
881 }
882 sc->sc_aclrd_xfer = usbd_alloc_xfer(sc->sc_udev);
883 if (sc->sc_aclrd_xfer == NULL) {
884 error = ENOMEM;
885 goto bad;
886 }
887 sc->sc_aclrd_buf = usbd_alloc_buffer(sc->sc_aclrd_xfer, UBT_BUFSIZ_ACL);
888 if (sc->sc_aclrd_buf == NULL) {
889 error = ENOMEM;
890 goto bad;
891 }
892 sc->sc_aclrd_busy = 0;
893 ubt_recv_acl_start(sc);
894
895 /* ACL write */
896 err = usbd_open_pipe(sc->sc_iface0, sc->sc_aclwr_addr,
897 USBD_EXCLUSIVE_USE, &sc->sc_aclwr_pipe);
898 if (err != USBD_NORMAL_COMPLETION) {
899 error = EIO;
900 goto bad;
901 }
902 sc->sc_aclwr_xfer = usbd_alloc_xfer(sc->sc_udev);
903 if (sc->sc_aclwr_xfer == NULL) {
904 error = ENOMEM;
905 goto bad;
906 }
907 sc->sc_aclwr_buf = usbd_alloc_buffer(sc->sc_aclwr_xfer, UBT_BUFSIZ_ACL);
908 if (sc->sc_aclwr_buf == NULL) {
909 error = ENOMEM;
910 goto bad;
911 }
912 sc->sc_aclwr_busy = 0;
913
914 /* SCO read */
915 if (sc->sc_scord_size > 0) {
916 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scord_addr,
917 USBD_EXCLUSIVE_USE, &sc->sc_scord_pipe);
918 if (err != USBD_NORMAL_COMPLETION) {
919 error = EIO;
920 goto bad;
921 }
922
923 for (i = 0 ; i < UBT_NXFERS ; i++) {
924 sc->sc_scord[i].xfer = usbd_alloc_xfer(sc->sc_udev);
925 if (sc->sc_scord[i].xfer == NULL) {
926 error = ENOMEM;
927 goto bad;
928 }
929 sc->sc_scord[i].buf = usbd_alloc_buffer(sc->sc_scord[i].xfer,
930 sc->sc_scord_size * UBT_NFRAMES);
931 if (sc->sc_scord[i].buf == NULL) {
932 error = ENOMEM;
933 goto bad;
934 }
935 sc->sc_scord[i].softc = sc;
936 sc->sc_scord[i].busy = 0;
937 ubt_recv_sco_start1(sc, &sc->sc_scord[i]);
938 }
939 }
940
941 /* SCO write */
942 if (sc->sc_scowr_size > 0) {
943 err = usbd_open_pipe(sc->sc_iface1, sc->sc_scowr_addr,
944 USBD_EXCLUSIVE_USE, &sc->sc_scowr_pipe);
945 if (err != USBD_NORMAL_COMPLETION) {
946 error = EIO;
947 goto bad;
948 }
949
950 for (i = 0 ; i < UBT_NXFERS ; i++) {
951 sc->sc_scowr[i].xfer = usbd_alloc_xfer(sc->sc_udev);
952 if (sc->sc_scowr[i].xfer == NULL) {
953 error = ENOMEM;
954 goto bad;
955 }
956 sc->sc_scowr[i].buf = usbd_alloc_buffer(sc->sc_scowr[i].xfer,
957 sc->sc_scowr_size * UBT_NFRAMES);
958 if (sc->sc_scowr[i].buf == NULL) {
959 error = ENOMEM;
960 goto bad;
961 }
962 sc->sc_scowr[i].softc = sc;
963 sc->sc_scowr[i].busy = 0;
964 }
965
966 sc->sc_scowr_busy = 0;
967 }
968
969 sc->sc_enabled = 1;
970 splx(s);
971 return 0;
972
973 bad:
974 ubt_abortdealloc(sc);
975 splx(s);
976 return error;
977 }
978
979 static void
980 ubt_disable(device_ptr_t self)
981 {
982 struct ubt_softc *sc = USBGETSOFTC(self);
983 int s;
984
985 DPRINTFN(1, "sc=%p\n", sc);
986
987 if (sc->sc_enabled == 0)
988 return;
989
990 s = splusb();
991 ubt_abortdealloc(sc);
992
993 sc->sc_enabled = 0;
994 splx(s);
995 }
996
997 static void
998 ubt_xmit_cmd(device_ptr_t self, struct mbuf *m)
999 {
1000 struct ubt_softc *sc = USBGETSOFTC(self);
1001 int s;
1002
1003 KASSERT(sc->sc_enabled);
1004
1005 s = splusb();
1006 MBUFQ_ENQUEUE(&sc->sc_cmd_queue, m);
1007
1008 if (sc->sc_cmd_busy == 0)
1009 ubt_xmit_cmd_start(sc);
1010
1011 splx(s);
1012 }
1013
1014 static void
1015 ubt_xmit_cmd_start(struct ubt_softc *sc)
1016 {
1017 usb_device_request_t req;
1018 usbd_status status;
1019 struct mbuf *m;
1020 int len;
1021
1022 if (sc->sc_dying)
1023 return;
1024
1025 if (MBUFQ_FIRST(&sc->sc_cmd_queue) == NULL)
1026 return;
1027
1028 MBUFQ_DEQUEUE(&sc->sc_cmd_queue, m);
1029 KASSERT(m != NULL);
1030
1031 DPRINTFN(15, "%s: xmit CMD packet (%d bytes)\n",
1032 USBDEVNAME(sc->sc_dev), m->m_pkthdr.len);
1033
1034 sc->sc_refcnt++;
1035 sc->sc_cmd_busy = 1;
1036
1037 len = m->m_pkthdr.len - 1;
1038 m_copydata(m, 1, len, sc->sc_cmd_buf);
1039 m_freem(m);
1040
1041 memset(&req, 0, sizeof(req));
1042 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
1043 USETW(req.wLength, len);
1044
1045 usbd_setup_default_xfer(sc->sc_cmd_xfer,
1046 sc->sc_udev,
1047 sc,
1048 UBT_CMD_TIMEOUT,
1049 &req,
1050 sc->sc_cmd_buf,
1051 len,
1052 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1053 ubt_xmit_cmd_complete);
1054
1055 status = usbd_transfer(sc->sc_cmd_xfer);
1056
1057 KASSERT(status != USBD_NORMAL_COMPLETION);
1058
1059 if (status != USBD_IN_PROGRESS) {
1060 DPRINTF("usbd_transfer status=%s (%d)\n",
1061 usbd_errstr(status), status);
1062
1063 sc->sc_refcnt--;
1064 sc->sc_cmd_busy = 0;
1065 }
1066 }
1067
1068 static void
1069 ubt_xmit_cmd_complete(usbd_xfer_handle xfer,
1070 usbd_private_handle h, usbd_status status)
1071 {
1072 struct ubt_softc *sc = h;
1073 uint32_t count;
1074
1075 DPRINTFN(15, "%s: CMD complete status=%s (%d)\n",
1076 USBDEVNAME(sc->sc_dev), usbd_errstr(status), status);
1077
1078 sc->sc_cmd_busy = 0;
1079
1080 if (--sc->sc_refcnt < 0) {
1081 DPRINTF("sc_refcnt=%d\n", sc->sc_refcnt);
1082 usb_detach_wakeup(USBDEV(sc->sc_dev));
1083 return;
1084 }
1085
1086 if (sc->sc_dying) {
1087 DPRINTF("sc_dying\n");
1088 return;
1089 }
1090
1091 if (status != USBD_NORMAL_COMPLETION) {
1092 DPRINTF("status=%s (%d)\n",
1093 usbd_errstr(status), status);
1094
1095 sc->sc_stats.err_tx++;
1096 return;
1097 }
1098
1099 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1100 sc->sc_stats.cmd_tx++;
1101 sc->sc_stats.byte_tx += count;
1102
1103 ubt_xmit_cmd_start(sc);
1104 }
1105
1106 static void
1107 ubt_xmit_acl(device_ptr_t self, struct mbuf *m)
1108 {
1109 struct ubt_softc *sc = USBGETSOFTC(self);
1110 int s;
1111
1112 KASSERT(sc->sc_enabled);
1113
1114 s = splusb();
1115 MBUFQ_ENQUEUE(&sc->sc_aclwr_queue, m);
1116
1117 if (sc->sc_aclwr_busy == 0)
1118 ubt_xmit_acl_start(sc);
1119
1120 splx(s);
1121 }
1122
1123 static void
1124 ubt_xmit_acl_start(struct ubt_softc *sc)
1125 {
1126 struct mbuf *m;
1127 usbd_status status;
1128 int len;
1129
1130 if (sc->sc_dying)
1131 return;
1132
1133 if (MBUFQ_FIRST(&sc->sc_aclwr_queue) == NULL)
1134 return;
1135
1136 sc->sc_refcnt++;
1137 sc->sc_aclwr_busy = 1;
1138
1139 MBUFQ_DEQUEUE(&sc->sc_aclwr_queue, m);
1140 KASSERT(m != NULL);
1141
1142 DPRINTFN(15, "%s: xmit ACL packet (%d bytes)\n",
1143 USBDEVNAME(sc->sc_dev), m->m_pkthdr.len);
1144
1145 len = m->m_pkthdr.len - 1;
1146 if (len > UBT_BUFSIZ_ACL) {
1147 DPRINTF("%s: truncating ACL packet (%d => %d)!\n",
1148 USBDEVNAME(sc->sc_dev), len, UBT_BUFSIZ_ACL);
1149
1150 len = UBT_BUFSIZ_ACL;
1151 }
1152
1153 m_copydata(m, 1, len, sc->sc_aclwr_buf);
1154 m_freem(m);
1155
1156 sc->sc_stats.acl_tx++;
1157 sc->sc_stats.byte_tx += len;
1158
1159 usbd_setup_xfer(sc->sc_aclwr_xfer,
1160 sc->sc_aclwr_pipe,
1161 sc,
1162 sc->sc_aclwr_buf,
1163 len,
1164 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1165 UBT_ACL_TIMEOUT,
1166 ubt_xmit_acl_complete);
1167
1168 status = usbd_transfer(sc->sc_aclwr_xfer);
1169
1170 KASSERT(status != USBD_NORMAL_COMPLETION);
1171
1172 if (status != USBD_IN_PROGRESS) {
1173 DPRINTF("usbd_transfer status=%s (%d)\n",
1174 usbd_errstr(status), status);
1175
1176 sc->sc_refcnt--;
1177 sc->sc_aclwr_busy = 0;
1178 }
1179 }
1180
1181 static void
1182 ubt_xmit_acl_complete(usbd_xfer_handle xfer,
1183 usbd_private_handle h, usbd_status status)
1184 {
1185 struct ubt_softc *sc = h;
1186
1187 DPRINTFN(15, "%s: ACL complete status=%s (%d)\n",
1188 USBDEVNAME(sc->sc_dev), usbd_errstr(status), status);
1189
1190 sc->sc_aclwr_busy = 0;
1191
1192 if (--sc->sc_refcnt < 0) {
1193 usb_detach_wakeup(USBDEV(sc->sc_dev));
1194 return;
1195 }
1196
1197 if (sc->sc_dying)
1198 return;
1199
1200 if (status != USBD_NORMAL_COMPLETION) {
1201 DPRINTF("status=%s (%d)\n",
1202 usbd_errstr(status), status);
1203
1204 sc->sc_stats.err_tx++;
1205
1206 if (status == USBD_STALLED)
1207 usbd_clear_endpoint_stall_async(sc->sc_aclwr_pipe);
1208 else
1209 return;
1210 }
1211
1212 ubt_xmit_acl_start(sc);
1213 }
1214
1215 static void
1216 ubt_xmit_sco(device_ptr_t self, struct mbuf *m)
1217 {
1218 struct ubt_softc *sc = USBGETSOFTC(self);
1219 int s;
1220
1221 KASSERT(sc->sc_enabled);
1222
1223 s = splusb();
1224 MBUFQ_ENQUEUE(&sc->sc_scowr_queue, m);
1225
1226 if (sc->sc_scowr_busy == 0)
1227 ubt_xmit_sco_start(sc);
1228
1229 splx(s);
1230 }
1231
1232 static void
1233 ubt_xmit_sco_start(struct ubt_softc *sc)
1234 {
1235 int i;
1236
1237 if (sc->sc_dying || sc->sc_scowr_size == 0)
1238 return;
1239
1240 for (i = 0 ; i < UBT_NXFERS ; i++) {
1241 if (sc->sc_scowr[i].busy)
1242 continue;
1243
1244 ubt_xmit_sco_start1(sc, &sc->sc_scowr[i]);
1245 }
1246 }
1247
1248 static void
1249 ubt_xmit_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1250 {
1251 struct mbuf *m;
1252 uint8_t *buf;
1253 int num, len, size, space;
1254
1255 space = sc->sc_scowr_size * UBT_NFRAMES;
1256 buf = isoc->buf;
1257 len = 0;
1258
1259 /*
1260 * Fill the request buffer with data from the queue,
1261 * keeping any leftover packet on our private hook.
1262 *
1263 * Complete packets are passed back up to the stack
1264 * for disposal, since we can't rely on the controller
1265 * to tell us when it has finished with them.
1266 */
1267
1268 m = sc->sc_scowr_mbuf;
1269 while (space > 0) {
1270 if (m == NULL) {
1271 MBUFQ_DEQUEUE(&sc->sc_scowr_queue, m);
1272 if (m == NULL)
1273 break;
1274
1275 m_adj(m, 1); /* packet type */
1276 }
1277
1278 if (m->m_pkthdr.len > 0) {
1279 size = MIN(m->m_pkthdr.len, space);
1280
1281 m_copydata(m, 0, size, buf);
1282 m_adj(m, size);
1283
1284 buf += size;
1285 len += size;
1286 space -= size;
1287 }
1288
1289 if (m->m_pkthdr.len == 0) {
1290 sc->sc_stats.sco_tx++;
1291 if (!hci_complete_sco(sc->sc_unit, m))
1292 sc->sc_stats.err_tx++;
1293
1294 m = NULL;
1295 }
1296 }
1297 sc->sc_scowr_mbuf = m;
1298
1299 DPRINTFN(15, "isoc=%p, len=%d, space=%d\n", isoc, len, space);
1300
1301 if (len == 0) /* nothing to send */
1302 return;
1303
1304 sc->sc_refcnt++;
1305 sc->sc_scowr_busy = 1;
1306 sc->sc_stats.byte_tx += len;
1307 isoc->busy = 1;
1308
1309 /*
1310 * calculate number of isoc frames and sizes
1311 */
1312
1313 for (num = 0 ; len > 0 ; num++) {
1314 size = MIN(sc->sc_scowr_size, len);
1315
1316 isoc->size[num] = size;
1317 len -= size;
1318 }
1319
1320 usbd_setup_isoc_xfer(isoc->xfer,
1321 sc->sc_scowr_pipe,
1322 isoc,
1323 isoc->size,
1324 num,
1325 USBD_NO_COPY | USBD_FORCE_SHORT_XFER,
1326 ubt_xmit_sco_complete);
1327
1328 usbd_transfer(isoc->xfer);
1329 }
1330
1331 static void
1332 ubt_xmit_sco_complete(usbd_xfer_handle xfer,
1333 usbd_private_handle h, usbd_status status)
1334 {
1335 struct ubt_isoc_xfer *isoc = h;
1336 struct ubt_softc *sc;
1337 int i;
1338
1339 KASSERT(xfer == isoc->xfer);
1340 sc = isoc->softc;
1341
1342 DPRINTFN(15, "isoc=%p, status=%s (%d)\n",
1343 isoc, usbd_errstr(status), status);
1344
1345 isoc->busy = 0;
1346
1347 for (i = 0 ; ; i++) {
1348 if (i == UBT_NXFERS) {
1349 sc->sc_scowr_busy = 0;
1350 break;
1351 }
1352
1353 if (sc->sc_scowr[i].busy)
1354 break;
1355 }
1356
1357 if (--sc->sc_refcnt < 0) {
1358 usb_detach_wakeup(USBDEV(sc->sc_dev));
1359 return;
1360 }
1361
1362 if (sc->sc_dying)
1363 return;
1364
1365 if (status != USBD_NORMAL_COMPLETION) {
1366 DPRINTF("status=%s (%d)\n",
1367 usbd_errstr(status), status);
1368
1369 sc->sc_stats.err_tx++;
1370
1371 if (status == USBD_STALLED)
1372 usbd_clear_endpoint_stall_async(sc->sc_scowr_pipe);
1373 else
1374 return;
1375 }
1376
1377 ubt_xmit_sco_start(sc);
1378 }
1379
1380 /*
1381 * load incoming data into an mbuf with
1382 * leading type byte
1383 */
1384 static struct mbuf *
1385 ubt_mbufload(uint8_t *buf, int count, uint8_t type)
1386 {
1387 struct mbuf *m;
1388
1389 MGETHDR(m, M_DONTWAIT, MT_DATA);
1390 if (m == NULL)
1391 return NULL;
1392
1393 *mtod(m, uint8_t *) = type;
1394 m->m_pkthdr.len = m->m_len = MHLEN;
1395 m_copyback(m, 1, count, buf); // (extends if necessary)
1396 if (m->m_pkthdr.len != MAX(MHLEN, count + 1)) {
1397 m_free(m);
1398 return NULL;
1399 }
1400
1401 m->m_pkthdr.len = count + 1;
1402 m->m_len = MIN(MHLEN, m->m_pkthdr.len);
1403
1404 return m;
1405 }
1406
1407 static void
1408 ubt_recv_event(usbd_xfer_handle xfer, usbd_private_handle h, usbd_status status)
1409 {
1410 struct ubt_softc *sc = h;
1411 struct mbuf *m;
1412 uint32_t count;
1413 void *buf;
1414
1415 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1416 sc, usbd_errstr(status), status);
1417
1418 if (status != USBD_NORMAL_COMPLETION || sc->sc_dying)
1419 return;
1420
1421 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1422
1423 if (count < sizeof(hci_event_hdr_t) - 1) {
1424 DPRINTF("dumped undersized event (count = %d)\n", count);
1425 sc->sc_stats.err_rx++;
1426 return;
1427 }
1428
1429 sc->sc_stats.evt_rx++;
1430 sc->sc_stats.byte_rx += count;
1431
1432 m = ubt_mbufload(buf, count, HCI_EVENT_PKT);
1433 if (m == NULL || !hci_input_event(sc->sc_unit, m))
1434 sc->sc_stats.err_rx++;
1435 }
1436
1437 static void
1438 ubt_recv_acl_start(struct ubt_softc *sc)
1439 {
1440 usbd_status status;
1441
1442 DPRINTFN(15, "sc=%p\n", sc);
1443
1444 if (sc->sc_aclrd_busy || sc->sc_dying) {
1445 DPRINTF("sc_aclrd_busy=%d, sc_dying=%d\n",
1446 sc->sc_aclrd_busy,
1447 sc->sc_dying);
1448
1449 return;
1450 }
1451
1452 sc->sc_refcnt++;
1453 sc->sc_aclrd_busy = 1;
1454
1455 usbd_setup_xfer(sc->sc_aclrd_xfer,
1456 sc->sc_aclrd_pipe,
1457 sc,
1458 sc->sc_aclrd_buf,
1459 UBT_BUFSIZ_ACL,
1460 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1461 USBD_NO_TIMEOUT,
1462 ubt_recv_acl_complete);
1463
1464 status = usbd_transfer(sc->sc_aclrd_xfer);
1465
1466 KASSERT(status != USBD_NORMAL_COMPLETION);
1467
1468 if (status != USBD_IN_PROGRESS) {
1469 DPRINTF("usbd_transfer status=%s (%d)\n",
1470 usbd_errstr(status), status);
1471
1472 sc->sc_refcnt--;
1473 sc->sc_aclrd_busy = 0;
1474 }
1475 }
1476
1477 static void
1478 ubt_recv_acl_complete(usbd_xfer_handle xfer,
1479 usbd_private_handle h, usbd_status status)
1480 {
1481 struct ubt_softc *sc = h;
1482 struct mbuf *m;
1483 uint32_t count;
1484 void *buf;
1485
1486 DPRINTFN(15, "sc=%p status=%s (%d)\n",
1487 sc, usbd_errstr(status), status);
1488
1489 sc->sc_aclrd_busy = 0;
1490
1491 if (--sc->sc_refcnt < 0) {
1492 DPRINTF("refcnt = %d\n", sc->sc_refcnt);
1493 usb_detach_wakeup(USBDEV(sc->sc_dev));
1494 return;
1495 }
1496
1497 if (sc->sc_dying) {
1498 DPRINTF("sc_dying\n");
1499 return;
1500 }
1501
1502 if (status != USBD_NORMAL_COMPLETION) {
1503 DPRINTF("status=%s (%d)\n",
1504 usbd_errstr(status), status);
1505
1506 sc->sc_stats.err_rx++;
1507
1508 if (status == USBD_STALLED)
1509 usbd_clear_endpoint_stall_async(sc->sc_aclrd_pipe);
1510 else
1511 return;
1512 } else {
1513 usbd_get_xfer_status(xfer, NULL, &buf, &count, NULL);
1514
1515 if (count < sizeof(hci_acldata_hdr_t) - 1) {
1516 DPRINTF("dumped undersized packet (%d)\n", count);
1517 sc->sc_stats.err_rx++;
1518 } else {
1519 sc->sc_stats.acl_rx++;
1520 sc->sc_stats.byte_rx += count;
1521
1522 m = ubt_mbufload(buf, count, HCI_ACL_DATA_PKT);
1523 if (m == NULL || !hci_input_acl(sc->sc_unit, m))
1524 sc->sc_stats.err_rx++;
1525 }
1526 }
1527
1528 /* and restart */
1529 ubt_recv_acl_start(sc);
1530 }
1531
1532 static void
1533 ubt_recv_sco_start1(struct ubt_softc *sc, struct ubt_isoc_xfer *isoc)
1534 {
1535 int i;
1536
1537 DPRINTFN(15, "sc=%p, isoc=%p\n", sc, isoc);
1538
1539 if (isoc->busy || sc->sc_dying || sc->sc_scord_size == 0) {
1540 DPRINTF("%s%s%s\n",
1541 isoc->busy ? " busy" : "",
1542 sc->sc_dying ? " dying" : "",
1543 sc->sc_scord_size == 0 ? " size=0" : "");
1544
1545 return;
1546 }
1547
1548 sc->sc_refcnt++;
1549 isoc->busy = 1;
1550
1551 for (i = 0 ; i < UBT_NFRAMES ; i++)
1552 isoc->size[i] = sc->sc_scord_size;
1553
1554 usbd_setup_isoc_xfer(isoc->xfer,
1555 sc->sc_scord_pipe,
1556 isoc,
1557 isoc->size,
1558 UBT_NFRAMES,
1559 USBD_NO_COPY | USBD_SHORT_XFER_OK,
1560 ubt_recv_sco_complete);
1561
1562 usbd_transfer(isoc->xfer);
1563 }
1564
1565 static void
1566 ubt_recv_sco_complete(usbd_xfer_handle xfer,
1567 usbd_private_handle h, usbd_status status)
1568 {
1569 struct ubt_isoc_xfer *isoc = h;
1570 struct ubt_softc *sc;
1571 struct mbuf *m;
1572 uint32_t count;
1573 uint8_t *ptr, *frame;
1574 int i, size, got, want;
1575
1576 KASSERT(isoc != NULL);
1577 KASSERT(isoc->xfer == xfer);
1578
1579 sc = isoc->softc;
1580 isoc->busy = 0;
1581
1582 if (--sc->sc_refcnt < 0) {
1583 DPRINTF("refcnt=%d\n", sc->sc_refcnt);
1584 usb_detach_wakeup(USBDEV(sc->sc_dev));
1585 return;
1586 }
1587
1588 if (sc->sc_dying) {
1589 DPRINTF("sc_dying\n");
1590 return;
1591 }
1592
1593 if (status != USBD_NORMAL_COMPLETION) {
1594 DPRINTF("status=%s (%d)\n",
1595 usbd_errstr(status), status);
1596
1597 sc->sc_stats.err_rx++;
1598
1599 if (status == USBD_STALLED) {
1600 usbd_clear_endpoint_stall_async(sc->sc_scord_pipe);
1601 goto restart;
1602 }
1603
1604 return;
1605 }
1606
1607 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
1608 if (count == 0)
1609 goto restart;
1610
1611 DPRINTFN(15, "sc=%p, isoc=%p, count=%u\n",
1612 sc, isoc, count);
1613
1614 sc->sc_stats.byte_rx += count;
1615
1616 /*
1617 * Extract SCO packets from ISOC frames. The way we have it,
1618 * no SCO packet can be bigger than MHLEN. This is unlikely
1619 * to actually happen, but if we ran out of mbufs and lost
1620 * sync then we may get spurious data that makes it seem that
1621 * way, so we discard data that wont fit. This doesnt really
1622 * help with the lost sync situation alas.
1623 */
1624
1625 m = sc->sc_scord_mbuf;
1626 if (m != NULL) {
1627 sc->sc_scord_mbuf = NULL;
1628 ptr = mtod(m, uint8_t *) + m->m_pkthdr.len;
1629 got = m->m_pkthdr.len;
1630 want = sizeof(hci_scodata_hdr_t);
1631 if (got >= want)
1632 want += mtod(m, hci_scodata_hdr_t *)->length ;
1633 } else {
1634 ptr = NULL;
1635 got = 0;
1636 want = 0;
1637 }
1638
1639 for (i = 0 ; i < UBT_NFRAMES ; i++) {
1640 frame = isoc->buf + (i * sc->sc_scord_size);
1641
1642 while (isoc->size[i] > 0) {
1643 size = isoc->size[i];
1644
1645 if (m == NULL) {
1646 MGETHDR(m, M_DONTWAIT, MT_DATA);
1647 if (m == NULL) {
1648 aprint_error_dev(sc->sc_dev,
1649 "out of memory (xfer halted)\n");
1650
1651 sc->sc_stats.err_rx++;
1652 return; /* lost sync */
1653 }
1654
1655 ptr = mtod(m, uint8_t *);
1656 *ptr++ = HCI_SCO_DATA_PKT;
1657 got = 1;
1658 want = sizeof(hci_scodata_hdr_t);
1659 }
1660
1661 if (got + size > want)
1662 size = want - got;
1663
1664 if (got + size > MHLEN)
1665 memcpy(ptr, frame, MHLEN - got);
1666 else
1667 memcpy(ptr, frame, size);
1668
1669 ptr += size;
1670 got += size;
1671 frame += size;
1672
1673 if (got == want) {
1674 /*
1675 * If we only got a header, add the packet
1676 * length to our want count. Send complete
1677 * packets up to protocol stack.
1678 */
1679 if (want == sizeof(hci_scodata_hdr_t))
1680 want += mtod(m, hci_scodata_hdr_t *)->length;
1681
1682 if (got == want) {
1683 m->m_pkthdr.len = m->m_len = got;
1684 sc->sc_stats.sco_rx++;
1685 if (!hci_input_sco(sc->sc_unit, m))
1686 sc->sc_stats.err_rx++;
1687
1688 m = NULL;
1689 }
1690 }
1691
1692 isoc->size[i] -= size;
1693 }
1694 }
1695
1696 if (m != NULL) {
1697 m->m_pkthdr.len = m->m_len = got;
1698 sc->sc_scord_mbuf = m;
1699 }
1700
1701 restart: /* and restart */
1702 ubt_recv_sco_start1(sc, isoc);
1703 }
1704
1705 void
1706 ubt_stats(device_ptr_t self, struct bt_stats *dest, int flush)
1707 {
1708 struct ubt_softc *sc = USBGETSOFTC(self);
1709 int s;
1710
1711 s = splusb();
1712 memcpy(dest, &sc->sc_stats, sizeof(struct bt_stats));
1713
1714 if (flush)
1715 memset(&sc->sc_stats, 0, sizeof(struct bt_stats));
1716
1717 splx(s);
1718 }
1719