1 /* $NetBSD: ubt.c,v 1.30 2007/12/16 19:01:37 christos 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.30 2007/12/16 19:01:37 christos 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 if (!device_pmf_is_registered(self)) 531 if (!pmf_device_register(self, NULL, NULL)) 532 aprint_error_dev(self, 533 "couldn't establish power handler\n"); 534 USB_ATTACH_SUCCESS_RETURN; 535 } 536 537 USB_DETACH(ubt) 538 { 539 USB_DETACH_START(ubt, sc); 540 int s; 541 542 DPRINTF("sc=%p flags=%d\n", sc, flags); 543 544 pmf_device_deregister(self); 545 546 sc->sc_dying = 1; 547 548 if (!sc->sc_ok) 549 return 0; 550 551 /* delete sysctl nodes */ 552 sysctl_teardown(&sc->sc_log); 553 554 /* Detach HCI interface */ 555 if (sc->sc_unit) { 556 hci_detach(sc->sc_unit); 557 sc->sc_unit = NULL; 558 } 559 560 /* 561 * Abort all pipes. Causes processes waiting for transfer to wake. 562 * 563 * Actually, hci_detach() above will call ubt_disable() which may 564 * call ubt_abortdealloc(), but lets be sure since doing it twice 565 * wont cause an error. 566 */ 567 ubt_abortdealloc(sc); 568 569 /* wait for all processes to finish */ 570 s = splusb(); 571 if (sc->sc_refcnt-- > 0) 572 usb_detach_wait(USBDEV(sc->sc_dev)); 573 574 splx(s); 575 576 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 577 USBDEV(sc->sc_dev)); 578 579 DPRINTFN(1, "driver detached\n"); 580 581 return 0; 582 } 583 584 int 585 ubt_activate(device_ptr_t self, enum devact act) 586 { 587 struct ubt_softc *sc = USBGETSOFTC(self); 588 int error = 0; 589 590 DPRINTFN(1, "sc=%p, act=%d\n", sc, act); 591 592 switch (act) { 593 case DVACT_ACTIVATE: 594 break; 595 596 case DVACT_DEACTIVATE: 597 sc->sc_dying = 1; 598 break; 599 600 default: 601 error = EOPNOTSUPP; 602 break; 603 } 604 605 return error; 606 } 607 608 /* set ISOC configuration */ 609 static int 610 ubt_set_isoc_config(struct ubt_softc *sc) 611 { 612 usb_endpoint_descriptor_t *ed; 613 int rd_addr, wr_addr, rd_size, wr_size; 614 uint8_t count, i; 615 int err; 616 617 err = usbd_set_interface(sc->sc_iface1, sc->sc_config); 618 if (err != USBD_NORMAL_COMPLETION) { 619 aprint_error( 620 "%s: Could not set config %d on ISOC interface. %s (%d)\n", 621 USBDEVNAME(sc->sc_dev), sc->sc_config, usbd_errstr(err), err); 622 623 return err == USBD_IN_USE ? EBUSY : EIO; 624 } 625 626 /* 627 * We wont get past the above if there are any pipes open, so no 628 * need to worry about buf/xfer/pipe deallocation. If we get an 629 * error after this, the frame quantities will be 0 and no SCO 630 * data will be possible. 631 */ 632 633 sc->sc_scord_size = rd_size = 0; 634 sc->sc_scord_addr = rd_addr = -1; 635 636 sc->sc_scowr_size = wr_size = 0; 637 sc->sc_scowr_addr = wr_addr = -1; 638 639 count = 0; 640 (void)usbd_endpoint_count(sc->sc_iface1, &count); 641 642 for (i = 0 ; i < count ; i++) { 643 ed = usbd_interface2endpoint_descriptor(sc->sc_iface1, i); 644 if (ed == NULL) { 645 aprint_error("%s: could not read endpoint descriptor %d\n", 646 USBDEVNAME(sc->sc_dev), i); 647 648 return EIO; 649 } 650 651 DPRINTFN(5, "%s: endpoint type %02x (%02x) addr %02x (%s)\n", 652 USBDEVNAME(sc->sc_dev), 653 UE_GET_XFERTYPE(ed->bmAttributes), 654 UE_GET_ISO_TYPE(ed->bmAttributes), 655 ed->bEndpointAddress, 656 UE_GET_DIR(ed->bEndpointAddress) ? "in" : "out"); 657 658 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS) 659 continue; 660 661 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) { 662 rd_addr = ed->bEndpointAddress; 663 rd_size = UGETW(ed->wMaxPacketSize); 664 } else { 665 wr_addr = ed->bEndpointAddress; 666 wr_size = UGETW(ed->wMaxPacketSize); 667 } 668 } 669 670 if (rd_addr == -1) { 671 aprint_error( 672 "%s: missing ISOC IN endpoint on interface config %d\n", 673 USBDEVNAME(sc->sc_dev), sc->sc_config); 674 675 return ENOENT; 676 } 677 if (wr_addr == -1) { 678 aprint_error( 679 "%s: missing ISOC OUT endpoint on interface config %d\n", 680 USBDEVNAME(sc->sc_dev), sc->sc_config); 681 682 return ENOENT; 683 } 684 685 #ifdef DIAGNOSTIC 686 if (rd_size > MLEN) { 687 aprint_error("%s: rd_size=%d exceeds MLEN\n", 688 USBDEVNAME(sc->sc_dev), rd_size); 689 690 return EOVERFLOW; 691 } 692 693 if (wr_size > MLEN) { 694 aprint_error("%s: wr_size=%d exceeds MLEN\n", 695 USBDEVNAME(sc->sc_dev), wr_size); 696 697 return EOVERFLOW; 698 } 699 #endif 700 701 sc->sc_scord_size = rd_size; 702 sc->sc_scord_addr = rd_addr; 703 704 sc->sc_scowr_size = wr_size; 705 sc->sc_scowr_addr = wr_addr; 706 707 return 0; 708 } 709 710 /* sysctl helper to set alternate configurations */ 711 static int 712 ubt_sysctl_config(SYSCTLFN_ARGS) 713 { 714 struct sysctlnode node; 715 struct ubt_softc *sc; 716 int t, error; 717 718 node = *rnode; 719 sc = node.sysctl_data; 720 721 t = sc->sc_config; 722 node.sysctl_data = &t; 723 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 724 if (error || newp == NULL) 725 return error; 726 727 if (t < 0 || t >= sc->sc_alt_config) 728 return EINVAL; 729 730 /* This may not change when the unit is enabled */ 731 if (sc->sc_enabled) 732 return EBUSY; 733 734 sc->sc_config = t; 735 return ubt_set_isoc_config(sc); 736 } 737 738 static void 739 ubt_abortdealloc(struct ubt_softc *sc) 740 { 741 int i; 742 743 DPRINTFN(1, "sc=%p\n", sc); 744 745 /* Abort all pipes */ 746 if (sc->sc_evt_pipe != NULL) { 747 usbd_abort_pipe(sc->sc_evt_pipe); 748 usbd_close_pipe(sc->sc_evt_pipe); 749 sc->sc_evt_pipe = NULL; 750 } 751 752 if (sc->sc_aclrd_pipe != NULL) { 753 usbd_abort_pipe(sc->sc_aclrd_pipe); 754 usbd_close_pipe(sc->sc_aclrd_pipe); 755 sc->sc_aclrd_pipe = NULL; 756 } 757 758 if (sc->sc_aclwr_pipe != NULL) { 759 usbd_abort_pipe(sc->sc_aclwr_pipe); 760 usbd_close_pipe(sc->sc_aclwr_pipe); 761 sc->sc_aclwr_pipe = NULL; 762 } 763 764 if (sc->sc_scord_pipe != NULL) { 765 usbd_abort_pipe(sc->sc_scord_pipe); 766 usbd_close_pipe(sc->sc_scord_pipe); 767 sc->sc_scord_pipe = NULL; 768 } 769 770 if (sc->sc_scowr_pipe != NULL) { 771 usbd_abort_pipe(sc->sc_scowr_pipe); 772 usbd_close_pipe(sc->sc_scowr_pipe); 773 sc->sc_scowr_pipe = NULL; 774 } 775 776 /* Free event buffer */ 777 if (sc->sc_evt_buf != NULL) { 778 free(sc->sc_evt_buf, M_USBDEV); 779 sc->sc_evt_buf = NULL; 780 } 781 782 /* Free all xfers and xfer buffers (implicit) */ 783 if (sc->sc_cmd_xfer != NULL) { 784 usbd_free_xfer(sc->sc_cmd_xfer); 785 sc->sc_cmd_xfer = NULL; 786 sc->sc_cmd_buf = NULL; 787 } 788 789 if (sc->sc_aclrd_xfer != NULL) { 790 usbd_free_xfer(sc->sc_aclrd_xfer); 791 sc->sc_aclrd_xfer = NULL; 792 sc->sc_aclrd_buf = NULL; 793 } 794 795 if (sc->sc_aclwr_xfer != NULL) { 796 usbd_free_xfer(sc->sc_aclwr_xfer); 797 sc->sc_aclwr_xfer = NULL; 798 sc->sc_aclwr_buf = NULL; 799 } 800 801 for (i = 0 ; i < UBT_NXFERS ; i++) { 802 if (sc->sc_scord[i].xfer != NULL) { 803 usbd_free_xfer(sc->sc_scord[i].xfer); 804 sc->sc_scord[i].xfer = NULL; 805 sc->sc_scord[i].buf = NULL; 806 } 807 808 if (sc->sc_scowr[i].xfer != NULL) { 809 usbd_free_xfer(sc->sc_scowr[i].xfer); 810 sc->sc_scowr[i].xfer = NULL; 811 sc->sc_scowr[i].buf = NULL; 812 } 813 } 814 815 /* Free partial SCO packets */ 816 if (sc->sc_scord_mbuf != NULL) { 817 m_freem(sc->sc_scord_mbuf); 818 sc->sc_scord_mbuf = NULL; 819 } 820 821 if (sc->sc_scowr_mbuf != NULL) { 822 m_freem(sc->sc_scowr_mbuf); 823 sc->sc_scowr_mbuf = NULL; 824 } 825 826 /* Empty mbuf queues */ 827 MBUFQ_DRAIN(&sc->sc_cmd_queue); 828 MBUFQ_DRAIN(&sc->sc_aclwr_queue); 829 MBUFQ_DRAIN(&sc->sc_scowr_queue); 830 } 831 832 /******************************************************************************* 833 * 834 * Bluetooth Unit/USB callbacks 835 * 836 * All of this will be called at the IPL_ we specified above 837 */ 838 static int 839 ubt_enable(device_ptr_t self) 840 { 841 struct ubt_softc *sc = USBGETSOFTC(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_ptr_t self) 991 { 992 struct ubt_softc *sc = USBGETSOFTC(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_ptr_t self, struct mbuf *m) 1009 { 1010 struct ubt_softc *sc = USBGETSOFTC(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 USBDEVNAME(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 USBDEVNAME(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_wakeup(USBDEV(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_ptr_t self, struct mbuf *m) 1118 { 1119 struct ubt_softc *sc = USBGETSOFTC(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 USBDEVNAME(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 USBDEVNAME(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 USBDEVNAME(sc->sc_dev), usbd_errstr(status), status); 1199 1200 sc->sc_aclwr_busy = 0; 1201 1202 if (--sc->sc_refcnt < 0) { 1203 usb_detach_wakeup(USBDEV(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_ptr_t self, struct mbuf *m) 1227 { 1228 struct ubt_softc *sc = USBGETSOFTC(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_wakeup(USBDEV(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_wakeup(USBDEV(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_wakeup(USBDEV(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("%s: out of memory (xfer halted)\n", 1659 USBDEVNAME(sc->sc_dev)); 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_ptr_t self, struct bt_stats *dest, int flush) 1717 { 1718 struct ubt_softc *sc = USBGETSOFTC(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