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