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