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