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