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 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, uss820, 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, saf1761, 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 USB_BUS_UNLOCK(bus); 226 227 #if USB_HAVE_PER_BUS_PROCESS 228 /* Get rid of USB callback processes */ 229 230 usb_proc_free(USB_BUS_GIANT_PROC(bus)); 231 usb_proc_free(USB_BUS_NON_GIANT_PROC(bus)); 232 233 /* Get rid of USB explore process */ 234 235 usb_proc_free(USB_BUS_EXPLORE_PROC(bus)); 236 237 /* Get rid of control transfer process */ 238 239 usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus)); 240 #endif 241 242 #if USB_HAVE_PF 243 usbpf_detach(bus); 244 #endif 245 return (0); 246 } 247 248 /*------------------------------------------------------------------------* 249 * usb_suspend 250 *------------------------------------------------------------------------*/ 251 static int 252 usb_suspend(device_t dev) 253 { 254 struct usb_bus *bus = device_get_softc(dev); 255 256 DPRINTF("\n"); 257 258 if (bus == NULL) { 259 /* was never setup properly */ 260 return (0); 261 } 262 263 USB_BUS_LOCK(bus); 264 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 265 &bus->suspend_msg[0], &bus->suspend_msg[1]); 266 if (usb_no_suspend_wait == 0) { 267 /* wait for suspend callback to be executed */ 268 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 269 &bus->suspend_msg[0], &bus->suspend_msg[1]); 270 } 271 USB_BUS_UNLOCK(bus); 272 273 return (0); 274 } 275 276 /*------------------------------------------------------------------------* 277 * usb_resume 278 *------------------------------------------------------------------------*/ 279 static int 280 usb_resume(device_t dev) 281 { 282 struct usb_bus *bus = device_get_softc(dev); 283 284 DPRINTF("\n"); 285 286 if (bus == NULL) { 287 /* was never setup properly */ 288 return (0); 289 } 290 291 USB_BUS_LOCK(bus); 292 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 293 &bus->resume_msg[0], &bus->resume_msg[1]); 294 USB_BUS_UNLOCK(bus); 295 296 return (0); 297 } 298 299 /*------------------------------------------------------------------------* 300 * usb_bus_reset_async_locked 301 *------------------------------------------------------------------------*/ 302 void 303 usb_bus_reset_async_locked(struct usb_bus *bus) 304 { 305 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 306 307 DPRINTF("\n"); 308 309 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL || 310 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) { 311 DPRINTF("Reset already pending\n"); 312 return; 313 } 314 315 device_printf(bus->parent, "Resetting controller\n"); 316 317 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 318 &bus->reset_msg[0], &bus->reset_msg[1]); 319 } 320 321 /*------------------------------------------------------------------------* 322 * usb_shutdown 323 *------------------------------------------------------------------------*/ 324 static int 325 usb_shutdown(device_t dev) 326 { 327 struct usb_bus *bus = device_get_softc(dev); 328 329 DPRINTF("\n"); 330 331 if (bus == NULL) { 332 /* was never setup properly */ 333 return (0); 334 } 335 336 device_printf(bus->bdev, "Controller shutdown\n"); 337 338 USB_BUS_LOCK(bus); 339 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 340 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 341 if (usb_no_shutdown_wait == 0) { 342 /* wait for shutdown callback to be executed */ 343 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 344 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 345 } 346 USB_BUS_UNLOCK(bus); 347 348 device_printf(bus->bdev, "Controller shutdown complete\n"); 349 350 return (0); 351 } 352 353 /*------------------------------------------------------------------------* 354 * usb_bus_explore 355 * 356 * This function is used to explore the device tree from the root. 357 *------------------------------------------------------------------------*/ 358 static void 359 usb_bus_explore(struct usb_proc_msg *pm) 360 { 361 struct usb_bus *bus; 362 struct usb_device *udev; 363 364 bus = ((struct usb_bus_msg *)pm)->bus; 365 udev = bus->devices[USB_ROOT_HUB_ADDR]; 366 367 if (bus->no_explore != 0) 368 return; 369 370 if (udev && udev->hub) { 371 372 if (bus->do_probe) { 373 bus->do_probe = 0; 374 bus->driver_added_refcount++; 375 } 376 if (bus->driver_added_refcount == 0) { 377 /* avoid zero, hence that is memory default */ 378 bus->driver_added_refcount = 1; 379 } 380 381 #ifdef DDB 382 /* 383 * The following three lines of code are only here to 384 * recover from DDB: 385 */ 386 usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus)); 387 usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus)); 388 usb_proc_rewakeup(USB_BUS_NON_GIANT_PROC(bus)); 389 #endif 390 391 USB_BUS_UNLOCK(bus); 392 393 #if USB_HAVE_POWERD 394 /* 395 * First update the USB power state! 396 */ 397 usb_bus_powerd(bus); 398 #endif 399 /* Explore the Root USB HUB. */ 400 (udev->hub->explore) (udev); 401 USB_BUS_LOCK(bus); 402 } 403 #if USB_HAVE_ROOT_MOUNT_HOLD 404 usb_root_mount_rel(bus); 405 #endif 406 } 407 408 /*------------------------------------------------------------------------* 409 * usb_bus_detach 410 * 411 * This function is used to detach the device tree from the root. 412 *------------------------------------------------------------------------*/ 413 static void 414 usb_bus_detach(struct usb_proc_msg *pm) 415 { 416 struct usb_bus *bus; 417 struct usb_device *udev; 418 device_t dev; 419 420 bus = ((struct usb_bus_msg *)pm)->bus; 421 udev = bus->devices[USB_ROOT_HUB_ADDR]; 422 dev = bus->bdev; 423 /* clear the softc */ 424 device_set_softc(dev, NULL); 425 USB_BUS_UNLOCK(bus); 426 427 /* detach children first */ 428 mtx_lock(&Giant); 429 bus_generic_detach(dev); 430 mtx_unlock(&Giant); 431 432 /* 433 * Free USB device and all subdevices, if any. 434 */ 435 usb_free_device(udev, 0); 436 437 USB_BUS_LOCK(bus); 438 /* clear bdev variable last */ 439 bus->bdev = NULL; 440 } 441 442 /*------------------------------------------------------------------------* 443 * usb_bus_suspend 444 * 445 * This function is used to suspend the USB controller. 446 *------------------------------------------------------------------------*/ 447 static void 448 usb_bus_suspend(struct usb_proc_msg *pm) 449 { 450 struct usb_bus *bus; 451 struct usb_device *udev; 452 usb_error_t err; 453 uint8_t do_unlock; 454 455 DPRINTF("\n"); 456 457 bus = ((struct usb_bus_msg *)pm)->bus; 458 udev = bus->devices[USB_ROOT_HUB_ADDR]; 459 460 if (udev == NULL || bus->bdev == NULL) 461 return; 462 463 USB_BUS_UNLOCK(bus); 464 465 /* 466 * We use the shutdown event here because the suspend and 467 * resume events are reserved for the USB port suspend and 468 * resume. The USB system suspend is implemented like full 469 * shutdown and all connected USB devices will be disconnected 470 * subsequently. At resume all USB devices will be 471 * re-connected again. 472 */ 473 474 bus_generic_shutdown(bus->bdev); 475 476 do_unlock = usbd_enum_lock(udev); 477 478 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 479 if (err) 480 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 481 482 USB_BUS_LOCK(bus); 483 bus->hw_power_state = 0; 484 bus->no_explore = 1; 485 USB_BUS_UNLOCK(bus); 486 487 if (bus->methods->set_hw_power != NULL) 488 (bus->methods->set_hw_power) (bus); 489 490 if (bus->methods->set_hw_power_sleep != NULL) 491 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 492 493 if (do_unlock) 494 usbd_enum_unlock(udev); 495 496 USB_BUS_LOCK(bus); 497 } 498 499 /*------------------------------------------------------------------------* 500 * usb_bus_resume 501 * 502 * This function is used to resume the USB controller. 503 *------------------------------------------------------------------------*/ 504 static void 505 usb_bus_resume(struct usb_proc_msg *pm) 506 { 507 struct usb_bus *bus; 508 struct usb_device *udev; 509 usb_error_t err; 510 uint8_t do_unlock; 511 512 DPRINTF("\n"); 513 514 bus = ((struct usb_bus_msg *)pm)->bus; 515 udev = bus->devices[USB_ROOT_HUB_ADDR]; 516 517 if (udev == NULL || bus->bdev == NULL) 518 return; 519 520 USB_BUS_UNLOCK(bus); 521 522 do_unlock = usbd_enum_lock(udev); 523 #if 0 524 DEVMETHOD(usb_take_controller, NULL); /* dummy */ 525 #endif 526 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 527 528 USB_BUS_LOCK(bus); 529 bus->hw_power_state = 530 USB_HW_POWER_CONTROL | 531 USB_HW_POWER_BULK | 532 USB_HW_POWER_INTERRUPT | 533 USB_HW_POWER_ISOC | 534 USB_HW_POWER_NON_ROOT_HUB; 535 bus->no_explore = 0; 536 USB_BUS_UNLOCK(bus); 537 538 if (bus->methods->set_hw_power_sleep != NULL) 539 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 540 541 if (bus->methods->set_hw_power != NULL) 542 (bus->methods->set_hw_power) (bus); 543 544 /* restore USB configuration to index 0 */ 545 err = usbd_set_config_index(udev, 0); 546 if (err) 547 device_printf(bus->bdev, "Could not configure root HUB\n"); 548 549 /* probe and attach */ 550 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 551 if (err) { 552 device_printf(bus->bdev, "Could not probe and " 553 "attach root HUB\n"); 554 } 555 556 if (do_unlock) 557 usbd_enum_unlock(udev); 558 559 USB_BUS_LOCK(bus); 560 } 561 562 /*------------------------------------------------------------------------* 563 * usb_bus_reset 564 * 565 * This function is used to reset the USB controller. 566 *------------------------------------------------------------------------*/ 567 static void 568 usb_bus_reset(struct usb_proc_msg *pm) 569 { 570 struct usb_bus *bus; 571 572 DPRINTF("\n"); 573 574 bus = ((struct usb_bus_msg *)pm)->bus; 575 576 if (bus->bdev == NULL || bus->no_explore != 0) 577 return; 578 579 /* a suspend and resume will reset the USB controller */ 580 usb_bus_suspend(pm); 581 usb_bus_resume(pm); 582 } 583 584 /*------------------------------------------------------------------------* 585 * usb_bus_shutdown 586 * 587 * This function is used to shutdown the USB controller. 588 *------------------------------------------------------------------------*/ 589 static void 590 usb_bus_shutdown(struct usb_proc_msg *pm) 591 { 592 struct usb_bus *bus; 593 struct usb_device *udev; 594 usb_error_t err; 595 uint8_t do_unlock; 596 597 bus = ((struct usb_bus_msg *)pm)->bus; 598 udev = bus->devices[USB_ROOT_HUB_ADDR]; 599 600 if (udev == NULL || bus->bdev == NULL) 601 return; 602 603 USB_BUS_UNLOCK(bus); 604 605 bus_generic_shutdown(bus->bdev); 606 607 do_unlock = usbd_enum_lock(udev); 608 609 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 610 if (err) 611 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 612 613 USB_BUS_LOCK(bus); 614 bus->hw_power_state = 0; 615 bus->no_explore = 1; 616 USB_BUS_UNLOCK(bus); 617 618 if (bus->methods->set_hw_power != NULL) 619 (bus->methods->set_hw_power) (bus); 620 621 if (bus->methods->set_hw_power_sleep != NULL) 622 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 623 624 if (do_unlock) 625 usbd_enum_unlock(udev); 626 627 USB_BUS_LOCK(bus); 628 } 629 630 static void 631 usb_power_wdog(void *arg) 632 { 633 struct usb_bus *bus = arg; 634 635 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 636 637 usb_callout_reset(&bus->power_wdog, 638 4 * hz, usb_power_wdog, arg); 639 640 #ifdef DDB 641 /* 642 * The following line of code is only here to recover from 643 * DDB: 644 */ 645 usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */ 646 #endif 647 648 #if USB_HAVE_POWERD 649 USB_BUS_UNLOCK(bus); 650 651 usb_bus_power_update(bus); 652 653 USB_BUS_LOCK(bus); 654 #endif 655 } 656 657 /*------------------------------------------------------------------------* 658 * usb_bus_attach 659 * 660 * This function attaches USB in context of the explore thread. 661 *------------------------------------------------------------------------*/ 662 static void 663 usb_bus_attach(struct usb_proc_msg *pm) 664 { 665 struct usb_bus *bus; 666 struct usb_device *child; 667 device_t dev; 668 usb_error_t err; 669 enum usb_dev_speed speed; 670 671 bus = ((struct usb_bus_msg *)pm)->bus; 672 dev = bus->bdev; 673 674 DPRINTF("\n"); 675 676 switch (bus->usbrev) { 677 case USB_REV_1_0: 678 speed = USB_SPEED_FULL; 679 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 680 break; 681 682 case USB_REV_1_1: 683 speed = USB_SPEED_FULL; 684 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 685 break; 686 687 case USB_REV_2_0: 688 speed = USB_SPEED_HIGH; 689 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 690 break; 691 692 case USB_REV_2_5: 693 speed = USB_SPEED_VARIABLE; 694 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 695 break; 696 697 case USB_REV_3_0: 698 speed = USB_SPEED_SUPER; 699 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 700 break; 701 702 default: 703 device_printf(bus->bdev, "Unsupported USB revision\n"); 704 #if USB_HAVE_ROOT_MOUNT_HOLD 705 usb_root_mount_rel(bus); 706 #endif 707 return; 708 } 709 710 /* default power_mask value */ 711 bus->hw_power_state = 712 USB_HW_POWER_CONTROL | 713 USB_HW_POWER_BULK | 714 USB_HW_POWER_INTERRUPT | 715 USB_HW_POWER_ISOC | 716 USB_HW_POWER_NON_ROOT_HUB; 717 718 USB_BUS_UNLOCK(bus); 719 720 /* make sure power is set at least once */ 721 722 if (bus->methods->set_hw_power != NULL) { 723 (bus->methods->set_hw_power) (bus); 724 } 725 726 /* allocate the Root USB device */ 727 728 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 729 speed, USB_MODE_HOST); 730 if (child) { 731 err = usb_probe_and_attach(child, 732 USB_IFACE_INDEX_ANY); 733 if (!err) { 734 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 735 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 736 err = USB_ERR_NO_ROOT_HUB; 737 } 738 } 739 } else { 740 err = USB_ERR_NOMEM; 741 } 742 743 USB_BUS_LOCK(bus); 744 745 if (err) { 746 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 747 usbd_errstr(err)); 748 #if USB_HAVE_ROOT_MOUNT_HOLD 749 usb_root_mount_rel(bus); 750 #endif 751 } 752 753 /* set softc - we are ready */ 754 device_set_softc(dev, bus); 755 756 /* start watchdog */ 757 usb_power_wdog(bus); 758 } 759 760 /*------------------------------------------------------------------------* 761 * usb_attach_sub 762 * 763 * This function creates a thread which runs the USB attach code. 764 *------------------------------------------------------------------------*/ 765 static void 766 usb_attach_sub(device_t dev, struct usb_bus *bus) 767 { 768 mtx_lock(&Giant); 769 if (usb_devclass_ptr == NULL) 770 usb_devclass_ptr = devclass_find("usbus"); 771 mtx_unlock(&Giant); 772 773 #if USB_HAVE_PF 774 usbpf_attach(bus); 775 #endif 776 /* Initialise USB process messages */ 777 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 778 bus->explore_msg[0].bus = bus; 779 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 780 bus->explore_msg[1].bus = bus; 781 782 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 783 bus->detach_msg[0].bus = bus; 784 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 785 bus->detach_msg[1].bus = bus; 786 787 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 788 bus->attach_msg[0].bus = bus; 789 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 790 bus->attach_msg[1].bus = bus; 791 792 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 793 bus->suspend_msg[0].bus = bus; 794 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 795 bus->suspend_msg[1].bus = bus; 796 797 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 798 bus->resume_msg[0].bus = bus; 799 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 800 bus->resume_msg[1].bus = bus; 801 802 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset; 803 bus->reset_msg[0].bus = bus; 804 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset; 805 bus->reset_msg[1].bus = bus; 806 807 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 808 bus->shutdown_msg[0].bus = bus; 809 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 810 bus->shutdown_msg[1].bus = bus; 811 812 #if USB_HAVE_PER_BUS_PROCESS 813 /* Create USB explore and callback processes */ 814 815 if (usb_proc_create(USB_BUS_GIANT_PROC(bus), 816 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 817 device_printf(dev, "WARNING: Creation of USB Giant " 818 "callback process failed.\n"); 819 } else if (usb_proc_create(USB_BUS_NON_GIANT_PROC(bus), 820 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) { 821 device_printf(dev, "WARNING: Creation of USB non-Giant " 822 "callback process failed.\n"); 823 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus), 824 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 825 device_printf(dev, "WARNING: Creation of USB explore " 826 "process failed.\n"); 827 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus), 828 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 829 device_printf(dev, "WARNING: Creation of USB control transfer " 830 "process failed.\n"); 831 } else 832 #endif 833 { 834 /* Get final attach going */ 835 USB_BUS_LOCK(bus); 836 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 837 &bus->attach_msg[0], &bus->attach_msg[1]); 838 USB_BUS_UNLOCK(bus); 839 840 /* Do initial explore */ 841 usb_needs_explore(bus, 1); 842 } 843 } 844 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 845 846 /*------------------------------------------------------------------------* 847 * usb_bus_mem_flush_all_cb 848 *------------------------------------------------------------------------*/ 849 #if USB_HAVE_BUSDMA 850 static void 851 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 852 struct usb_page *pg, usb_size_t size, usb_size_t align) 853 { 854 usb_pc_cpu_flush(pc); 855 } 856 #endif 857 858 /*------------------------------------------------------------------------* 859 * usb_bus_mem_flush_all - factored out code 860 *------------------------------------------------------------------------*/ 861 #if USB_HAVE_BUSDMA 862 void 863 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 864 { 865 if (cb) { 866 cb(bus, &usb_bus_mem_flush_all_cb); 867 } 868 } 869 #endif 870 871 /*------------------------------------------------------------------------* 872 * usb_bus_mem_alloc_all_cb 873 *------------------------------------------------------------------------*/ 874 #if USB_HAVE_BUSDMA 875 static void 876 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 877 struct usb_page *pg, usb_size_t size, usb_size_t align) 878 { 879 /* need to initialize the page cache */ 880 pc->tag_parent = bus->dma_parent_tag; 881 882 if (usb_pc_alloc_mem(pc, pg, size, align)) { 883 bus->alloc_failed = 1; 884 } 885 } 886 #endif 887 888 /*------------------------------------------------------------------------* 889 * usb_bus_mem_alloc_all - factored out code 890 * 891 * Returns: 892 * 0: Success 893 * Else: Failure 894 *------------------------------------------------------------------------*/ 895 uint8_t 896 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 897 usb_bus_mem_cb_t *cb) 898 { 899 bus->alloc_failed = 0; 900 901 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 902 NULL, MTX_DEF | MTX_RECURSE); 903 904 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent), 905 NULL, MTX_SPIN | MTX_RECURSE); 906 907 usb_callout_init_mtx(&bus->power_wdog, 908 &bus->bus_mtx, 0); 909 910 TAILQ_INIT(&bus->intr_q.head); 911 912 #if USB_HAVE_BUSDMA 913 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 914 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 915 #endif 916 if ((bus->devices_max > USB_MAX_DEVICES) || 917 (bus->devices_max < USB_MIN_DEVICES) || 918 (bus->devices == NULL)) { 919 DPRINTFN(0, "Devices field has not been " 920 "initialised properly\n"); 921 bus->alloc_failed = 1; /* failure */ 922 } 923 #if USB_HAVE_BUSDMA 924 if (cb) { 925 cb(bus, &usb_bus_mem_alloc_all_cb); 926 } 927 #endif 928 if (bus->alloc_failed) { 929 usb_bus_mem_free_all(bus, cb); 930 } 931 return (bus->alloc_failed); 932 } 933 934 /*------------------------------------------------------------------------* 935 * usb_bus_mem_free_all_cb 936 *------------------------------------------------------------------------*/ 937 #if USB_HAVE_BUSDMA 938 static void 939 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 940 struct usb_page *pg, usb_size_t size, usb_size_t align) 941 { 942 usb_pc_free_mem(pc); 943 } 944 #endif 945 946 /*------------------------------------------------------------------------* 947 * usb_bus_mem_free_all - factored out code 948 *------------------------------------------------------------------------*/ 949 void 950 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 951 { 952 #if USB_HAVE_BUSDMA 953 if (cb) { 954 cb(bus, &usb_bus_mem_free_all_cb); 955 } 956 usb_dma_tag_unsetup(bus->dma_parent_tag); 957 #endif 958 959 mtx_destroy(&bus->bus_mtx); 960 mtx_destroy(&bus->bus_spin_lock); 961 } 962 963 /* convenience wrappers */ 964 void 965 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2) 966 { 967 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2); 968 } 969 970 void * 971 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2) 972 { 973 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2)); 974 } 975 976 void 977 usb_proc_explore_lock(struct usb_device *udev) 978 { 979 USB_BUS_LOCK(udev->bus); 980 } 981 982 void 983 usb_proc_explore_unlock(struct usb_device *udev) 984 { 985 USB_BUS_UNLOCK(udev->bus); 986 } 987