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