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