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