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