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