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_hub.h> 57 58 #include <bus/u4b/usb_controller.h> 59 #include <bus/u4b/usb_bus.h> 60 #include <bus/u4b/usb_pf.h> 61 #include "usb_if.h" 62 63 /* function prototypes */ 64 65 static device_probe_t usb_probe; 66 static device_attach_t usb_attach; 67 static device_detach_t usb_detach; 68 static device_suspend_t usb_suspend; 69 static device_resume_t usb_resume; 70 static device_shutdown_t usb_shutdown; 71 72 static void usb_attach_sub(device_t, struct usb_bus *); 73 74 /* static variables */ 75 76 #ifdef USB_DEBUG 77 static int usb_ctrl_debug = 0; 78 79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller"); 80 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0, 81 "Debug level"); 82 #endif 83 84 static int usb_no_boot_wait = 0; 85 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait); 86 /* XXX freebsd uses CTLFLAG_RDTUN here */ 87 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RW, &usb_no_boot_wait, 0, 88 "No USB device enumerate waiting at boot."); 89 90 static int usb_no_shutdown_wait = 0; 91 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait); 92 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW, &usb_no_shutdown_wait, 0, 93 "No USB device waiting at system shutdown."); 94 95 static devclass_t usb_devclass; 96 97 static device_method_t usb_methods[] = { 98 DEVMETHOD(device_probe, usb_probe), 99 DEVMETHOD(device_attach, usb_attach), 100 DEVMETHOD(device_detach, usb_detach), 101 DEVMETHOD(device_suspend, usb_suspend), 102 DEVMETHOD(device_resume, usb_resume), 103 DEVMETHOD(device_shutdown, usb_shutdown), 104 {0, 0} 105 }; 106 107 static driver_t usb_driver = { 108 .name = "usbus", 109 .methods = usb_methods, 110 .size = 0, 111 }; 112 113 /* Host Only Drivers */ 114 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 115 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 116 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 117 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0); 118 119 /* Device Only Drivers */ 120 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0); 121 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0); 122 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0); 123 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0); 124 125 /*------------------------------------------------------------------------* 126 * usb_probe 127 * 128 * This function is called from "{ehci,ohci,uhci}_pci_attach()". 129 *------------------------------------------------------------------------*/ 130 static int 131 usb_probe(device_t dev) 132 { 133 DPRINTF("\n"); 134 return (0); 135 } 136 137 static void 138 usb_root_mount_rel(struct usb_bus *bus) 139 { 140 if (bus->bus_roothold != NULL) { 141 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold); 142 #if 0 /* XXX Dragonflybsd seems to not have this? */ 143 root_mount_rel(bus->bus_roothold); 144 #endif 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 0 /* XXX: Dragonfly does not seem to have this mechanism? */ 165 if (usb_no_boot_wait == 0) { 166 /* delay vfs_mountroot until the bus is explored */ 167 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 168 } 169 #endif 170 171 usb_attach_sub(dev, bus); 172 173 return (0); /* return success */ 174 } 175 176 /*------------------------------------------------------------------------* 177 * usb_detach 178 *------------------------------------------------------------------------*/ 179 static int 180 usb_detach(device_t dev) 181 { 182 struct usb_bus *bus = device_get_softc(dev); 183 184 DPRINTF("\n"); 185 186 if (bus == NULL) { 187 /* was never setup properly */ 188 return (0); 189 } 190 /* Stop power watchdog */ 191 usb_callout_drain(&bus->power_wdog); 192 193 /* Let the USB explore process detach all devices. */ 194 usb_root_mount_rel(bus); 195 196 USB_BUS_LOCK(bus); 197 198 /* Queue detach job */ 199 usb_proc_msignal(&bus->explore_proc, 200 &bus->detach_msg[0], &bus->detach_msg[1]); 201 202 /* Wait for detach to complete */ 203 usb_proc_mwait(&bus->explore_proc, 204 &bus->detach_msg[0], &bus->detach_msg[1]); 205 206 USB_BUS_UNLOCK(bus); 207 208 /* Get rid of USB callback processes */ 209 210 usb_proc_free(&bus->giant_callback_proc); 211 usb_proc_free(&bus->non_giant_callback_proc); 212 213 /* Get rid of USB explore process */ 214 215 usb_proc_free(&bus->explore_proc); 216 217 /* Get rid of control transfer process */ 218 219 usb_proc_free(&bus->control_xfer_proc); 220 221 #if USB_HAVE_PF 222 usbpf_detach(bus); 223 #endif 224 return (0); 225 } 226 227 /*------------------------------------------------------------------------* 228 * usb_suspend 229 *------------------------------------------------------------------------*/ 230 static int 231 usb_suspend(device_t dev) 232 { 233 struct usb_bus *bus = device_get_softc(dev); 234 235 DPRINTF("\n"); 236 237 if (bus == NULL) { 238 /* was never setup properly */ 239 return (0); 240 } 241 242 USB_BUS_LOCK(bus); 243 usb_proc_msignal(&bus->explore_proc, 244 &bus->suspend_msg[0], &bus->suspend_msg[1]); 245 USB_BUS_UNLOCK(bus); 246 247 return (0); 248 } 249 250 /*------------------------------------------------------------------------* 251 * usb_resume 252 *------------------------------------------------------------------------*/ 253 static int 254 usb_resume(device_t dev) 255 { 256 struct usb_bus *bus = device_get_softc(dev); 257 258 DPRINTF("\n"); 259 260 if (bus == NULL) { 261 /* was never setup properly */ 262 return (0); 263 } 264 265 USB_BUS_LOCK(bus); 266 usb_proc_msignal(&bus->explore_proc, 267 &bus->resume_msg[0], &bus->resume_msg[1]); 268 USB_BUS_UNLOCK(bus); 269 270 return (0); 271 } 272 273 /*------------------------------------------------------------------------* 274 * usb_shutdown 275 *------------------------------------------------------------------------*/ 276 static int 277 usb_shutdown(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 device_printf(bus->bdev, "Controller shutdown\n"); 289 290 USB_BUS_LOCK(bus); 291 usb_proc_msignal(&bus->explore_proc, 292 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 293 if (usb_no_shutdown_wait == 0) { 294 /* wait for shutdown callback to be executed */ 295 usb_proc_mwait(&bus->explore_proc, 296 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 297 } 298 USB_BUS_UNLOCK(bus); 299 300 device_printf(bus->bdev, "Controller shutdown complete\n"); 301 302 return (0); 303 } 304 305 /*------------------------------------------------------------------------* 306 * usb_bus_explore 307 * 308 * This function is used to explore the device tree from the root. 309 *------------------------------------------------------------------------*/ 310 static void 311 usb_bus_explore(struct usb_proc_msg *pm) 312 { 313 struct usb_bus *bus; 314 struct usb_device *udev; 315 316 bus = ((struct usb_bus_msg *)pm)->bus; 317 udev = bus->devices[USB_ROOT_HUB_ADDR]; 318 319 if (bus->no_explore != 0) 320 return; 321 322 if (udev && udev->hub) { 323 324 if (bus->do_probe) { 325 bus->do_probe = 0; 326 bus->driver_added_refcount++; 327 } 328 if (bus->driver_added_refcount == 0) { 329 /* avoid zero, hence that is memory default */ 330 bus->driver_added_refcount = 1; 331 } 332 333 #ifdef DDB 334 /* 335 * The following three lines of code are only here to 336 * recover from DDB: 337 */ 338 usb_proc_rewakeup(&bus->control_xfer_proc); 339 usb_proc_rewakeup(&bus->giant_callback_proc); 340 usb_proc_rewakeup(&bus->non_giant_callback_proc); 341 #endif 342 343 USB_BUS_UNLOCK(bus); 344 345 #if USB_HAVE_POWERD 346 /* 347 * First update the USB power state! 348 */ 349 usb_bus_powerd(bus); 350 #endif 351 /* Explore the Root USB HUB. */ 352 (udev->hub->explore) (udev); 353 USB_BUS_LOCK(bus); 354 } 355 usb_root_mount_rel(bus); 356 } 357 358 /*------------------------------------------------------------------------* 359 * usb_bus_detach 360 * 361 * This function is used to detach the device tree from the root. 362 *------------------------------------------------------------------------*/ 363 static void 364 usb_bus_detach(struct usb_proc_msg *pm) 365 { 366 struct usb_bus *bus; 367 struct usb_device *udev; 368 device_t dev; 369 370 bus = ((struct usb_bus_msg *)pm)->bus; 371 udev = bus->devices[USB_ROOT_HUB_ADDR]; 372 dev = bus->bdev; 373 /* clear the softc */ 374 device_set_softc(dev, NULL); 375 USB_BUS_UNLOCK(bus); 376 377 /* detach children first */ 378 bus_generic_detach(dev); 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 USB_BUS_LOCK_ASSERT(bus); 542 543 usb_callout_reset(&bus->power_wdog, 544 4 * hz, usb_power_wdog, arg); 545 546 #ifdef DDB 547 /* 548 * The following line of code is only here to recover from 549 * DDB: 550 */ 551 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 552 #endif 553 554 #if USB_HAVE_POWERD 555 USB_BUS_UNLOCK(bus); 556 557 usb_bus_power_update(bus); 558 559 USB_BUS_LOCK(bus); 560 #endif 561 } 562 563 /*------------------------------------------------------------------------* 564 * usb_bus_attach 565 * 566 * This function attaches USB in context of the explore thread. 567 *------------------------------------------------------------------------*/ 568 static void 569 usb_bus_attach(struct usb_proc_msg *pm) 570 { 571 struct usb_bus *bus; 572 struct usb_device *child; 573 device_t dev; 574 usb_error_t err; 575 enum usb_dev_speed speed; 576 577 bus = ((struct usb_bus_msg *)pm)->bus; 578 dev = bus->bdev; 579 580 DPRINTF("\n"); 581 582 switch (bus->usbrev) { 583 case USB_REV_1_0: 584 speed = USB_SPEED_FULL; 585 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 586 break; 587 588 case USB_REV_1_1: 589 speed = USB_SPEED_FULL; 590 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 591 break; 592 593 case USB_REV_2_0: 594 speed = USB_SPEED_HIGH; 595 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 596 break; 597 598 case USB_REV_2_5: 599 speed = USB_SPEED_VARIABLE; 600 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 601 break; 602 603 case USB_REV_3_0: 604 speed = USB_SPEED_SUPER; 605 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 606 break; 607 608 default: 609 device_printf(bus->bdev, "Unsupported USB revision\n"); 610 usb_root_mount_rel(bus); 611 return; 612 } 613 614 /* default power_mask value */ 615 bus->hw_power_state = 616 USB_HW_POWER_CONTROL | 617 USB_HW_POWER_BULK | 618 USB_HW_POWER_INTERRUPT | 619 USB_HW_POWER_ISOC | 620 USB_HW_POWER_NON_ROOT_HUB; 621 622 USB_BUS_UNLOCK(bus); 623 624 /* make sure power is set at least once */ 625 626 if (bus->methods->set_hw_power != NULL) { 627 (bus->methods->set_hw_power) (bus); 628 } 629 630 /* allocate the Root USB device */ 631 632 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 633 speed, USB_MODE_HOST); 634 if (child) { 635 err = usb_probe_and_attach(child, 636 USB_IFACE_INDEX_ANY); 637 if (!err) { 638 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 639 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 640 err = USB_ERR_NO_ROOT_HUB; 641 } 642 } 643 } else { 644 err = USB_ERR_NOMEM; 645 } 646 647 USB_BUS_LOCK(bus); 648 649 if (err) { 650 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 651 usbd_errstr(err)); 652 usb_root_mount_rel(bus); 653 } 654 655 /* set softc - we are ready */ 656 device_set_softc(dev, bus); 657 658 /* start watchdog */ 659 usb_power_wdog(bus); 660 } 661 662 /*------------------------------------------------------------------------* 663 * usb_attach_sub 664 * 665 * This function creates a thread which runs the USB attach code. 666 *------------------------------------------------------------------------*/ 667 static void 668 usb_attach_sub(device_t dev, struct usb_bus *bus) 669 { 670 const char *pname = device_get_nameunit(dev); 671 672 if (usb_devclass_ptr == NULL) 673 usb_devclass_ptr = devclass_find("usbus"); 674 675 #if USB_HAVE_PF 676 usbpf_attach(bus); 677 #endif 678 /* Initialise USB process messages */ 679 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 680 bus->explore_msg[0].bus = bus; 681 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 682 bus->explore_msg[1].bus = bus; 683 684 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 685 bus->detach_msg[0].bus = bus; 686 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 687 bus->detach_msg[1].bus = bus; 688 689 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 690 bus->attach_msg[0].bus = bus; 691 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 692 bus->attach_msg[1].bus = bus; 693 694 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 695 bus->suspend_msg[0].bus = bus; 696 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 697 bus->suspend_msg[1].bus = bus; 698 699 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 700 bus->resume_msg[0].bus = bus; 701 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 702 bus->resume_msg[1].bus = bus; 703 704 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 705 bus->shutdown_msg[0].bus = bus; 706 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 707 bus->shutdown_msg[1].bus = bus; 708 709 /* Create USB explore and callback processes */ 710 711 if (usb_proc_create(&bus->giant_callback_proc, 712 &bus->bus_lock, pname, USB_PRI_MED)) { 713 device_printf(dev, "WARNING: Creation of USB Giant " 714 "callback process failed.\n"); 715 } else if (usb_proc_create(&bus->non_giant_callback_proc, 716 &bus->bus_lock, pname, USB_PRI_HIGH)) { 717 device_printf(dev, "WARNING: Creation of USB non-Giant " 718 "callback process failed.\n"); 719 } else if (usb_proc_create(&bus->explore_proc, 720 &bus->bus_lock, pname, USB_PRI_MED)) { 721 device_printf(dev, "WARNING: Creation of USB explore " 722 "process failed.\n"); 723 } else if (usb_proc_create(&bus->control_xfer_proc, 724 &bus->bus_lock, pname, USB_PRI_MED)) { 725 device_printf(dev, "WARNING: Creation of USB control transfer " 726 "process failed.\n"); 727 } else { 728 /* Get final attach going */ 729 USB_BUS_LOCK(bus); 730 usb_proc_msignal(&bus->explore_proc, 731 &bus->attach_msg[0], &bus->attach_msg[1]); 732 USB_BUS_UNLOCK(bus); 733 734 /* Do initial explore */ 735 usb_needs_explore(bus, 1); 736 } 737 } 738 739 SYSUNINIT(usb_bus_unload, SI_SUB_CONFIGURE, SI_ORDER_ANY, usb_bus_unload, NULL); 740 741 /*------------------------------------------------------------------------* 742 * usb_bus_mem_flush_all_cb 743 *------------------------------------------------------------------------*/ 744 #if USB_HAVE_BUSDMA 745 static void 746 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 747 struct usb_page *pg, usb_size_t size, usb_size_t align) 748 { 749 usb_pc_cpu_flush(pc); 750 } 751 #endif 752 753 /*------------------------------------------------------------------------* 754 * usb_bus_mem_flush_all - factored out code 755 *------------------------------------------------------------------------*/ 756 #if USB_HAVE_BUSDMA 757 void 758 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 759 { 760 if (cb) { 761 cb(bus, &usb_bus_mem_flush_all_cb); 762 } 763 } 764 #endif 765 766 /*------------------------------------------------------------------------* 767 * usb_bus_mem_alloc_all_cb 768 *------------------------------------------------------------------------*/ 769 #if USB_HAVE_BUSDMA 770 static void 771 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 772 struct usb_page *pg, usb_size_t size, usb_size_t align) 773 { 774 /* need to initialize the page cache */ 775 pc->tag_parent = bus->dma_parent_tag; 776 777 if (usb_pc_alloc_mem(pc, pg, size, align)) { 778 bus->alloc_failed = 1; 779 } 780 } 781 #endif 782 783 /*------------------------------------------------------------------------* 784 * usb_bus_mem_alloc_all - factored out code 785 * 786 * Returns: 787 * 0: Success 788 * Else: Failure 789 *------------------------------------------------------------------------*/ 790 uint8_t 791 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 792 usb_bus_mem_cb_t *cb) 793 { 794 bus->alloc_failed = 0; 795 796 lockinit(&bus->bus_lock, "USB bus mem", 0, LK_CANRECURSE); 797 798 usb_callout_init_mtx(&bus->power_wdog, 799 &bus->bus_lock, 0); 800 801 TAILQ_INIT(&bus->intr_q.head); 802 803 #if USB_HAVE_BUSDMA 804 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 805 dmat, &bus->bus_lock, NULL, 32, USB_BUS_DMA_TAG_MAX); 806 #endif 807 if ((bus->devices_max > USB_MAX_DEVICES) || 808 (bus->devices_max < USB_MIN_DEVICES) || 809 (bus->devices == NULL)) { 810 DPRINTFN(0, "Devices field has not been " 811 "initialised properly\n"); 812 bus->alloc_failed = 1; /* failure */ 813 } 814 #if USB_HAVE_BUSDMA 815 if (cb) { 816 cb(bus, &usb_bus_mem_alloc_all_cb); 817 } 818 #endif 819 if (bus->alloc_failed) { 820 usb_bus_mem_free_all(bus, cb); 821 } 822 return (bus->alloc_failed); 823 } 824 825 /*------------------------------------------------------------------------* 826 * usb_bus_mem_free_all_cb 827 *------------------------------------------------------------------------*/ 828 #if USB_HAVE_BUSDMA 829 static void 830 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 831 struct usb_page *pg, usb_size_t size, usb_size_t align) 832 { 833 usb_pc_free_mem(pc); 834 } 835 #endif 836 837 /*------------------------------------------------------------------------* 838 * usb_bus_mem_free_all - factored out code 839 *------------------------------------------------------------------------*/ 840 void 841 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 842 { 843 #if USB_HAVE_BUSDMA 844 if (cb) { 845 cb(bus, &usb_bus_mem_free_all_cb); 846 } 847 usb_dma_tag_unsetup(bus->dma_parent_tag); 848 #endif 849 850 lockuninit(&bus->bus_lock); 851 } 852