1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifdef USB_GLOBAL_INCLUDE_FILE 28 #include USB_GLOBAL_INCLUDE_FILE 29 #else 30 #include "opt_ddb.h" 31 32 #include <sys/stdint.h> 33 #include <sys/stddef.h> 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <sys/types.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/module.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/condvar.h> 44 #include <sys/sysctl.h> 45 #include <sys/sx.h> 46 #include <sys/unistd.h> 47 #include <sys/callout.h> 48 #include <sys/malloc.h> 49 #include <sys/priv.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 54 #define USB_DEBUG_VAR usb_ctrl_debug 55 56 #include <dev/usb/usb_core.h> 57 #include <dev/usb/usb_debug.h> 58 #include <dev/usb/usb_process.h> 59 #include <dev/usb/usb_busdma.h> 60 #include <dev/usb/usb_dynamic.h> 61 #include <dev/usb/usb_device.h> 62 #include <dev/usb/usb_hub.h> 63 64 #include <dev/usb/usb_controller.h> 65 #include <dev/usb/usb_bus.h> 66 #include <dev/usb/usb_pf.h> 67 #include "usb_if.h" 68 #endif /* USB_GLOBAL_INCLUDE_FILE */ 69 70 /* function prototypes */ 71 72 static device_probe_t usb_probe; 73 static device_attach_t usb_attach; 74 static device_detach_t usb_detach; 75 static device_suspend_t usb_suspend; 76 static device_resume_t usb_resume; 77 static device_shutdown_t usb_shutdown; 78 79 static void usb_attach_sub(device_t, struct usb_bus *); 80 81 /* static variables */ 82 83 #ifdef USB_DEBUG 84 static int usb_ctrl_debug = 0; 85 86 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller"); 87 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0, 88 "Debug level"); 89 #endif 90 91 #if USB_HAVE_ROOT_MOUNT_HOLD 92 static int usb_no_boot_wait = 0; 93 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait); 94 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0, 95 "No USB device enumerate waiting at boot."); 96 #endif 97 98 static int usb_no_suspend_wait = 0; 99 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait); 100 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN, 101 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend."); 102 103 static int usb_no_shutdown_wait = 0; 104 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait); 105 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN, 106 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown."); 107 108 static devclass_t usb_devclass; 109 110 static device_method_t usb_methods[] = { 111 DEVMETHOD(device_probe, usb_probe), 112 DEVMETHOD(device_attach, usb_attach), 113 DEVMETHOD(device_detach, usb_detach), 114 DEVMETHOD(device_suspend, usb_suspend), 115 DEVMETHOD(device_resume, usb_resume), 116 DEVMETHOD(device_shutdown, usb_shutdown), 117 {0, 0} 118 }; 119 120 static driver_t usb_driver = { 121 .name = "usbus", 122 .methods = usb_methods, 123 .size = 0, 124 }; 125 126 /* Host Only Drivers */ 127 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 128 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 129 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 130 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0); 131 132 /* Device Only Drivers */ 133 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0); 134 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0); 135 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0); 136 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0); 137 138 /* Dual Mode Drivers */ 139 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0); 140 141 /*------------------------------------------------------------------------* 142 * usb_probe 143 * 144 * This function is called from "{ehci,ohci,uhci}_pci_attach()". 145 *------------------------------------------------------------------------*/ 146 static int 147 usb_probe(device_t dev) 148 { 149 DPRINTF("\n"); 150 return (0); 151 } 152 153 #if USB_HAVE_ROOT_MOUNT_HOLD 154 static void 155 usb_root_mount_rel(struct usb_bus *bus) 156 { 157 if (bus->bus_roothold != NULL) { 158 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold); 159 root_mount_rel(bus->bus_roothold); 160 bus->bus_roothold = NULL; 161 } 162 } 163 #endif 164 165 /*------------------------------------------------------------------------* 166 * usb_attach 167 *------------------------------------------------------------------------*/ 168 static int 169 usb_attach(device_t dev) 170 { 171 struct usb_bus *bus = device_get_ivars(dev); 172 173 DPRINTF("\n"); 174 175 if (bus == NULL) { 176 device_printf(dev, "USB device has no ivars\n"); 177 return (ENXIO); 178 } 179 180 #if USB_HAVE_ROOT_MOUNT_HOLD 181 if (usb_no_boot_wait == 0) { 182 /* delay vfs_mountroot until the bus is explored */ 183 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 184 } 185 #endif 186 usb_attach_sub(dev, bus); 187 188 return (0); /* return success */ 189 } 190 191 /*------------------------------------------------------------------------* 192 * usb_detach 193 *------------------------------------------------------------------------*/ 194 static int 195 usb_detach(device_t dev) 196 { 197 struct usb_bus *bus = device_get_softc(dev); 198 199 DPRINTF("\n"); 200 201 if (bus == NULL) { 202 /* was never setup properly */ 203 return (0); 204 } 205 /* Stop power watchdog */ 206 usb_callout_drain(&bus->power_wdog); 207 208 #if USB_HAVE_ROOT_MOUNT_HOLD 209 /* Let the USB explore process detach all devices. */ 210 usb_root_mount_rel(bus); 211 #endif 212 213 USB_BUS_LOCK(bus); 214 215 /* Queue detach job */ 216 usb_proc_msignal(&bus->explore_proc, 217 &bus->detach_msg[0], &bus->detach_msg[1]); 218 219 /* Wait for detach to complete */ 220 usb_proc_mwait(&bus->explore_proc, 221 &bus->detach_msg[0], &bus->detach_msg[1]); 222 223 USB_BUS_UNLOCK(bus); 224 225 /* Get rid of USB callback processes */ 226 227 usb_proc_free(&bus->giant_callback_proc); 228 usb_proc_free(&bus->non_giant_callback_proc); 229 230 /* Get rid of USB explore process */ 231 232 usb_proc_free(&bus->explore_proc); 233 234 /* Get rid of control transfer process */ 235 236 usb_proc_free(&bus->control_xfer_proc); 237 238 #if USB_HAVE_PF 239 usbpf_detach(bus); 240 #endif 241 return (0); 242 } 243 244 /*------------------------------------------------------------------------* 245 * usb_suspend 246 *------------------------------------------------------------------------*/ 247 static int 248 usb_suspend(device_t dev) 249 { 250 struct usb_bus *bus = device_get_softc(dev); 251 252 DPRINTF("\n"); 253 254 if (bus == NULL) { 255 /* was never setup properly */ 256 return (0); 257 } 258 259 USB_BUS_LOCK(bus); 260 usb_proc_msignal(&bus->explore_proc, 261 &bus->suspend_msg[0], &bus->suspend_msg[1]); 262 if (usb_no_suspend_wait == 0) { 263 /* wait for suspend callback to be executed */ 264 usb_proc_mwait(&bus->explore_proc, 265 &bus->suspend_msg[0], &bus->suspend_msg[1]); 266 } 267 USB_BUS_UNLOCK(bus); 268 269 return (0); 270 } 271 272 /*------------------------------------------------------------------------* 273 * usb_resume 274 *------------------------------------------------------------------------*/ 275 static int 276 usb_resume(device_t dev) 277 { 278 struct usb_bus *bus = device_get_softc(dev); 279 280 DPRINTF("\n"); 281 282 if (bus == NULL) { 283 /* was never setup properly */ 284 return (0); 285 } 286 287 USB_BUS_LOCK(bus); 288 usb_proc_msignal(&bus->explore_proc, 289 &bus->resume_msg[0], &bus->resume_msg[1]); 290 USB_BUS_UNLOCK(bus); 291 292 return (0); 293 } 294 295 /*------------------------------------------------------------------------* 296 * usb_shutdown 297 *------------------------------------------------------------------------*/ 298 static int 299 usb_shutdown(device_t dev) 300 { 301 struct usb_bus *bus = device_get_softc(dev); 302 303 DPRINTF("\n"); 304 305 if (bus == NULL) { 306 /* was never setup properly */ 307 return (0); 308 } 309 310 device_printf(bus->bdev, "Controller shutdown\n"); 311 312 USB_BUS_LOCK(bus); 313 usb_proc_msignal(&bus->explore_proc, 314 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 315 if (usb_no_shutdown_wait == 0) { 316 /* wait for shutdown callback to be executed */ 317 usb_proc_mwait(&bus->explore_proc, 318 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 319 } 320 USB_BUS_UNLOCK(bus); 321 322 device_printf(bus->bdev, "Controller shutdown complete\n"); 323 324 return (0); 325 } 326 327 /*------------------------------------------------------------------------* 328 * usb_bus_explore 329 * 330 * This function is used to explore the device tree from the root. 331 *------------------------------------------------------------------------*/ 332 static void 333 usb_bus_explore(struct usb_proc_msg *pm) 334 { 335 struct usb_bus *bus; 336 struct usb_device *udev; 337 338 bus = ((struct usb_bus_msg *)pm)->bus; 339 udev = bus->devices[USB_ROOT_HUB_ADDR]; 340 341 if (bus->no_explore != 0) 342 return; 343 344 if (udev && udev->hub) { 345 346 if (bus->do_probe) { 347 bus->do_probe = 0; 348 bus->driver_added_refcount++; 349 } 350 if (bus->driver_added_refcount == 0) { 351 /* avoid zero, hence that is memory default */ 352 bus->driver_added_refcount = 1; 353 } 354 355 #ifdef DDB 356 /* 357 * The following three lines of code are only here to 358 * recover from DDB: 359 */ 360 usb_proc_rewakeup(&bus->control_xfer_proc); 361 usb_proc_rewakeup(&bus->giant_callback_proc); 362 usb_proc_rewakeup(&bus->non_giant_callback_proc); 363 #endif 364 365 USB_BUS_UNLOCK(bus); 366 367 #if USB_HAVE_POWERD 368 /* 369 * First update the USB power state! 370 */ 371 usb_bus_powerd(bus); 372 #endif 373 /* Explore the Root USB HUB. */ 374 (udev->hub->explore) (udev); 375 USB_BUS_LOCK(bus); 376 } 377 #if USB_HAVE_ROOT_MOUNT_HOLD 378 usb_root_mount_rel(bus); 379 #endif 380 } 381 382 /*------------------------------------------------------------------------* 383 * usb_bus_detach 384 * 385 * This function is used to detach the device tree from the root. 386 *------------------------------------------------------------------------*/ 387 static void 388 usb_bus_detach(struct usb_proc_msg *pm) 389 { 390 struct usb_bus *bus; 391 struct usb_device *udev; 392 device_t dev; 393 394 bus = ((struct usb_bus_msg *)pm)->bus; 395 udev = bus->devices[USB_ROOT_HUB_ADDR]; 396 dev = bus->bdev; 397 /* clear the softc */ 398 device_set_softc(dev, NULL); 399 USB_BUS_UNLOCK(bus); 400 401 /* detach children first */ 402 mtx_lock(&Giant); 403 bus_generic_detach(dev); 404 mtx_unlock(&Giant); 405 406 /* 407 * Free USB device and all subdevices, if any. 408 */ 409 usb_free_device(udev, 0); 410 411 USB_BUS_LOCK(bus); 412 /* clear bdev variable last */ 413 bus->bdev = NULL; 414 } 415 416 /*------------------------------------------------------------------------* 417 * usb_bus_suspend 418 * 419 * This function is used to suspend the USB contoller. 420 *------------------------------------------------------------------------*/ 421 static void 422 usb_bus_suspend(struct usb_proc_msg *pm) 423 { 424 struct usb_bus *bus; 425 struct usb_device *udev; 426 usb_error_t err; 427 428 bus = ((struct usb_bus_msg *)pm)->bus; 429 udev = bus->devices[USB_ROOT_HUB_ADDR]; 430 431 if (udev == NULL || bus->bdev == NULL) 432 return; 433 434 USB_BUS_UNLOCK(bus); 435 436 /* 437 * We use the shutdown event here because the suspend and 438 * resume events are reserved for the USB port suspend and 439 * resume. The USB system suspend is implemented like full 440 * shutdown and all connected USB devices will be disconnected 441 * subsequently. At resume all USB devices will be 442 * re-connected again. 443 */ 444 445 bus_generic_shutdown(bus->bdev); 446 447 usbd_enum_lock(udev); 448 449 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 450 if (err) 451 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 452 453 USB_BUS_LOCK(bus); 454 bus->hw_power_state = 0; 455 bus->no_explore = 1; 456 USB_BUS_UNLOCK(bus); 457 458 if (bus->methods->set_hw_power != NULL) 459 (bus->methods->set_hw_power) (bus); 460 461 if (bus->methods->set_hw_power_sleep != NULL) 462 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 463 464 usbd_enum_unlock(udev); 465 466 USB_BUS_LOCK(bus); 467 } 468 469 /*------------------------------------------------------------------------* 470 * usb_bus_resume 471 * 472 * This function is used to resume the USB contoller. 473 *------------------------------------------------------------------------*/ 474 static void 475 usb_bus_resume(struct usb_proc_msg *pm) 476 { 477 struct usb_bus *bus; 478 struct usb_device *udev; 479 usb_error_t err; 480 481 bus = ((struct usb_bus_msg *)pm)->bus; 482 udev = bus->devices[USB_ROOT_HUB_ADDR]; 483 484 if (udev == NULL || bus->bdev == NULL) 485 return; 486 487 USB_BUS_UNLOCK(bus); 488 489 usbd_enum_lock(udev); 490 #if 0 491 DEVMETHOD(usb_take_controller, NULL); /* dummy */ 492 #endif 493 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 494 495 USB_BUS_LOCK(bus); 496 bus->hw_power_state = 497 USB_HW_POWER_CONTROL | 498 USB_HW_POWER_BULK | 499 USB_HW_POWER_INTERRUPT | 500 USB_HW_POWER_ISOC | 501 USB_HW_POWER_NON_ROOT_HUB; 502 bus->no_explore = 0; 503 USB_BUS_UNLOCK(bus); 504 505 if (bus->methods->set_hw_power_sleep != NULL) 506 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 507 508 if (bus->methods->set_hw_power != NULL) 509 (bus->methods->set_hw_power) (bus); 510 511 /* restore USB configuration to index 0 */ 512 err = usbd_set_config_index(udev, 0); 513 if (err) 514 device_printf(bus->bdev, "Could not configure root HUB\n"); 515 516 /* probe and attach */ 517 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 518 if (err) { 519 device_printf(bus->bdev, "Could not probe and " 520 "attach root HUB\n"); 521 } 522 523 usbd_enum_unlock(udev); 524 525 USB_BUS_LOCK(bus); 526 } 527 528 /*------------------------------------------------------------------------* 529 * usb_bus_shutdown 530 * 531 * This function is used to shutdown the USB contoller. 532 *------------------------------------------------------------------------*/ 533 static void 534 usb_bus_shutdown(struct usb_proc_msg *pm) 535 { 536 struct usb_bus *bus; 537 struct usb_device *udev; 538 usb_error_t err; 539 540 bus = ((struct usb_bus_msg *)pm)->bus; 541 udev = bus->devices[USB_ROOT_HUB_ADDR]; 542 543 if (udev == NULL || bus->bdev == NULL) 544 return; 545 546 USB_BUS_UNLOCK(bus); 547 548 bus_generic_shutdown(bus->bdev); 549 550 usbd_enum_lock(udev); 551 552 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 553 if (err) 554 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 555 556 USB_BUS_LOCK(bus); 557 bus->hw_power_state = 0; 558 bus->no_explore = 1; 559 USB_BUS_UNLOCK(bus); 560 561 if (bus->methods->set_hw_power != NULL) 562 (bus->methods->set_hw_power) (bus); 563 564 if (bus->methods->set_hw_power_sleep != NULL) 565 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 566 567 usbd_enum_unlock(udev); 568 569 USB_BUS_LOCK(bus); 570 } 571 572 static void 573 usb_power_wdog(void *arg) 574 { 575 struct usb_bus *bus = arg; 576 577 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 578 579 usb_callout_reset(&bus->power_wdog, 580 4 * hz, usb_power_wdog, arg); 581 582 #ifdef DDB 583 /* 584 * The following line of code is only here to recover from 585 * DDB: 586 */ 587 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 588 #endif 589 590 #if USB_HAVE_POWERD 591 USB_BUS_UNLOCK(bus); 592 593 usb_bus_power_update(bus); 594 595 USB_BUS_LOCK(bus); 596 #endif 597 } 598 599 /*------------------------------------------------------------------------* 600 * usb_bus_attach 601 * 602 * This function attaches USB in context of the explore thread. 603 *------------------------------------------------------------------------*/ 604 static void 605 usb_bus_attach(struct usb_proc_msg *pm) 606 { 607 struct usb_bus *bus; 608 struct usb_device *child; 609 device_t dev; 610 usb_error_t err; 611 enum usb_dev_speed speed; 612 613 bus = ((struct usb_bus_msg *)pm)->bus; 614 dev = bus->bdev; 615 616 DPRINTF("\n"); 617 618 switch (bus->usbrev) { 619 case USB_REV_1_0: 620 speed = USB_SPEED_FULL; 621 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 622 break; 623 624 case USB_REV_1_1: 625 speed = USB_SPEED_FULL; 626 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 627 break; 628 629 case USB_REV_2_0: 630 speed = USB_SPEED_HIGH; 631 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 632 break; 633 634 case USB_REV_2_5: 635 speed = USB_SPEED_VARIABLE; 636 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 637 break; 638 639 case USB_REV_3_0: 640 speed = USB_SPEED_SUPER; 641 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 642 break; 643 644 default: 645 device_printf(bus->bdev, "Unsupported USB revision\n"); 646 #if USB_HAVE_ROOT_MOUNT_HOLD 647 usb_root_mount_rel(bus); 648 #endif 649 return; 650 } 651 652 /* default power_mask value */ 653 bus->hw_power_state = 654 USB_HW_POWER_CONTROL | 655 USB_HW_POWER_BULK | 656 USB_HW_POWER_INTERRUPT | 657 USB_HW_POWER_ISOC | 658 USB_HW_POWER_NON_ROOT_HUB; 659 660 USB_BUS_UNLOCK(bus); 661 662 /* make sure power is set at least once */ 663 664 if (bus->methods->set_hw_power != NULL) { 665 (bus->methods->set_hw_power) (bus); 666 } 667 668 /* allocate the Root USB device */ 669 670 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 671 speed, USB_MODE_HOST); 672 if (child) { 673 err = usb_probe_and_attach(child, 674 USB_IFACE_INDEX_ANY); 675 if (!err) { 676 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 677 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 678 err = USB_ERR_NO_ROOT_HUB; 679 } 680 } 681 } else { 682 err = USB_ERR_NOMEM; 683 } 684 685 USB_BUS_LOCK(bus); 686 687 if (err) { 688 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 689 usbd_errstr(err)); 690 #if USB_HAVE_ROOT_MOUNT_HOLD 691 usb_root_mount_rel(bus); 692 #endif 693 } 694 695 /* set softc - we are ready */ 696 device_set_softc(dev, bus); 697 698 /* start watchdog */ 699 usb_power_wdog(bus); 700 } 701 702 /*------------------------------------------------------------------------* 703 * usb_attach_sub 704 * 705 * This function creates a thread which runs the USB attach code. 706 *------------------------------------------------------------------------*/ 707 static void 708 usb_attach_sub(device_t dev, struct usb_bus *bus) 709 { 710 const char *pname = device_get_nameunit(dev); 711 712 mtx_lock(&Giant); 713 if (usb_devclass_ptr == NULL) 714 usb_devclass_ptr = devclass_find("usbus"); 715 mtx_unlock(&Giant); 716 717 #if USB_HAVE_PF 718 usbpf_attach(bus); 719 #endif 720 /* Initialise USB process messages */ 721 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 722 bus->explore_msg[0].bus = bus; 723 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 724 bus->explore_msg[1].bus = bus; 725 726 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 727 bus->detach_msg[0].bus = bus; 728 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 729 bus->detach_msg[1].bus = bus; 730 731 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 732 bus->attach_msg[0].bus = bus; 733 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 734 bus->attach_msg[1].bus = bus; 735 736 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 737 bus->suspend_msg[0].bus = bus; 738 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 739 bus->suspend_msg[1].bus = bus; 740 741 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 742 bus->resume_msg[0].bus = bus; 743 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 744 bus->resume_msg[1].bus = bus; 745 746 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 747 bus->shutdown_msg[0].bus = bus; 748 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 749 bus->shutdown_msg[1].bus = bus; 750 751 /* Create USB explore and callback processes */ 752 753 if (usb_proc_create(&bus->giant_callback_proc, 754 &bus->bus_mtx, pname, USB_PRI_MED)) { 755 device_printf(dev, "WARNING: Creation of USB Giant " 756 "callback process failed.\n"); 757 } else if (usb_proc_create(&bus->non_giant_callback_proc, 758 &bus->bus_mtx, pname, USB_PRI_HIGH)) { 759 device_printf(dev, "WARNING: Creation of USB non-Giant " 760 "callback process failed.\n"); 761 } else if (usb_proc_create(&bus->explore_proc, 762 &bus->bus_mtx, pname, USB_PRI_MED)) { 763 device_printf(dev, "WARNING: Creation of USB explore " 764 "process failed.\n"); 765 } else if (usb_proc_create(&bus->control_xfer_proc, 766 &bus->bus_mtx, pname, USB_PRI_MED)) { 767 device_printf(dev, "WARNING: Creation of USB control transfer " 768 "process failed.\n"); 769 } else { 770 /* Get final attach going */ 771 USB_BUS_LOCK(bus); 772 usb_proc_msignal(&bus->explore_proc, 773 &bus->attach_msg[0], &bus->attach_msg[1]); 774 USB_BUS_UNLOCK(bus); 775 776 /* Do initial explore */ 777 usb_needs_explore(bus, 1); 778 } 779 } 780 781 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 782 783 /*------------------------------------------------------------------------* 784 * usb_bus_mem_flush_all_cb 785 *------------------------------------------------------------------------*/ 786 #if USB_HAVE_BUSDMA 787 static void 788 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 789 struct usb_page *pg, usb_size_t size, usb_size_t align) 790 { 791 usb_pc_cpu_flush(pc); 792 } 793 #endif 794 795 /*------------------------------------------------------------------------* 796 * usb_bus_mem_flush_all - factored out code 797 *------------------------------------------------------------------------*/ 798 #if USB_HAVE_BUSDMA 799 void 800 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 801 { 802 if (cb) { 803 cb(bus, &usb_bus_mem_flush_all_cb); 804 } 805 } 806 #endif 807 808 /*------------------------------------------------------------------------* 809 * usb_bus_mem_alloc_all_cb 810 *------------------------------------------------------------------------*/ 811 #if USB_HAVE_BUSDMA 812 static void 813 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 814 struct usb_page *pg, usb_size_t size, usb_size_t align) 815 { 816 /* need to initialize the page cache */ 817 pc->tag_parent = bus->dma_parent_tag; 818 819 if (usb_pc_alloc_mem(pc, pg, size, align)) { 820 bus->alloc_failed = 1; 821 } 822 } 823 #endif 824 825 /*------------------------------------------------------------------------* 826 * usb_bus_mem_alloc_all - factored out code 827 * 828 * Returns: 829 * 0: Success 830 * Else: Failure 831 *------------------------------------------------------------------------*/ 832 uint8_t 833 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 834 usb_bus_mem_cb_t *cb) 835 { 836 bus->alloc_failed = 0; 837 838 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 839 NULL, MTX_DEF | MTX_RECURSE); 840 841 usb_callout_init_mtx(&bus->power_wdog, 842 &bus->bus_mtx, 0); 843 844 TAILQ_INIT(&bus->intr_q.head); 845 846 #if USB_HAVE_BUSDMA 847 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 848 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 849 #endif 850 if ((bus->devices_max > USB_MAX_DEVICES) || 851 (bus->devices_max < USB_MIN_DEVICES) || 852 (bus->devices == NULL)) { 853 DPRINTFN(0, "Devices field has not been " 854 "initialised properly\n"); 855 bus->alloc_failed = 1; /* failure */ 856 } 857 #if USB_HAVE_BUSDMA 858 if (cb) { 859 cb(bus, &usb_bus_mem_alloc_all_cb); 860 } 861 #endif 862 if (bus->alloc_failed) { 863 usb_bus_mem_free_all(bus, cb); 864 } 865 return (bus->alloc_failed); 866 } 867 868 /*------------------------------------------------------------------------* 869 * usb_bus_mem_free_all_cb 870 *------------------------------------------------------------------------*/ 871 #if USB_HAVE_BUSDMA 872 static void 873 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 874 struct usb_page *pg, usb_size_t size, usb_size_t align) 875 { 876 usb_pc_free_mem(pc); 877 } 878 #endif 879 880 /*------------------------------------------------------------------------* 881 * usb_bus_mem_free_all - factored out code 882 *------------------------------------------------------------------------*/ 883 void 884 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 885 { 886 #if USB_HAVE_BUSDMA 887 if (cb) { 888 cb(bus, &usb_bus_mem_free_all_cb); 889 } 890 usb_dma_tag_unsetup(bus->dma_parent_tag); 891 #endif 892 893 mtx_destroy(&bus->bus_mtx); 894 } 895