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