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 err = usbd_set_config_index(udev, 0); 477 if (err) 478 device_printf(bus->bdev, "Could not configure root HUB\n"); 479 480 usbd_enum_unlock(udev); 481 482 USB_BUS_LOCK(bus); 483 } 484 485 /*------------------------------------------------------------------------* 486 * usb_bus_shutdown 487 * 488 * This function is used to shutdown the USB contoller. 489 *------------------------------------------------------------------------*/ 490 static void 491 usb_bus_shutdown(struct usb_proc_msg *pm) 492 { 493 struct usb_bus *bus; 494 struct usb_device *udev; 495 usb_error_t err; 496 497 bus = ((struct usb_bus_msg *)pm)->bus; 498 udev = bus->devices[USB_ROOT_HUB_ADDR]; 499 500 if (udev == NULL || bus->bdev == NULL) 501 return; 502 503 USB_BUS_UNLOCK(bus); 504 505 bus_generic_shutdown(bus->bdev); 506 507 usbd_enum_lock(udev); 508 509 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 510 if (err) 511 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 512 513 USB_BUS_LOCK(bus); 514 bus->hw_power_state = 0; 515 bus->no_explore = 1; 516 USB_BUS_UNLOCK(bus); 517 518 if (bus->methods->set_hw_power != NULL) 519 (bus->methods->set_hw_power) (bus); 520 521 if (bus->methods->set_hw_power_sleep != NULL) 522 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 523 524 usbd_enum_unlock(udev); 525 526 USB_BUS_LOCK(bus); 527 } 528 529 static void 530 usb_power_wdog(void *arg) 531 { 532 struct usb_bus *bus = arg; 533 534 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 535 536 usb_callout_reset(&bus->power_wdog, 537 4 * hz, usb_power_wdog, arg); 538 539 #ifdef DDB 540 /* 541 * The following line of code is only here to recover from 542 * DDB: 543 */ 544 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 545 #endif 546 547 #if USB_HAVE_POWERD 548 USB_BUS_UNLOCK(bus); 549 550 usb_bus_power_update(bus); 551 552 USB_BUS_LOCK(bus); 553 #endif 554 } 555 556 /*------------------------------------------------------------------------* 557 * usb_bus_attach 558 * 559 * This function attaches USB in context of the explore thread. 560 *------------------------------------------------------------------------*/ 561 static void 562 usb_bus_attach(struct usb_proc_msg *pm) 563 { 564 struct usb_bus *bus; 565 struct usb_device *child; 566 device_t dev; 567 usb_error_t err; 568 enum usb_dev_speed speed; 569 570 bus = ((struct usb_bus_msg *)pm)->bus; 571 dev = bus->bdev; 572 573 DPRINTF("\n"); 574 575 switch (bus->usbrev) { 576 case USB_REV_1_0: 577 speed = USB_SPEED_FULL; 578 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 579 break; 580 581 case USB_REV_1_1: 582 speed = USB_SPEED_FULL; 583 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 584 break; 585 586 case USB_REV_2_0: 587 speed = USB_SPEED_HIGH; 588 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 589 break; 590 591 case USB_REV_2_5: 592 speed = USB_SPEED_VARIABLE; 593 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 594 break; 595 596 case USB_REV_3_0: 597 speed = USB_SPEED_SUPER; 598 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 599 break; 600 601 default: 602 device_printf(bus->bdev, "Unsupported USB revision\n"); 603 usb_root_mount_rel(bus); 604 return; 605 } 606 607 /* default power_mask value */ 608 bus->hw_power_state = 609 USB_HW_POWER_CONTROL | 610 USB_HW_POWER_BULK | 611 USB_HW_POWER_INTERRUPT | 612 USB_HW_POWER_ISOC | 613 USB_HW_POWER_NON_ROOT_HUB; 614 615 USB_BUS_UNLOCK(bus); 616 617 /* make sure power is set at least once */ 618 619 if (bus->methods->set_hw_power != NULL) { 620 (bus->methods->set_hw_power) (bus); 621 } 622 623 /* allocate the Root USB device */ 624 625 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 626 speed, USB_MODE_HOST); 627 if (child) { 628 err = usb_probe_and_attach(child, 629 USB_IFACE_INDEX_ANY); 630 if (!err) { 631 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 632 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 633 err = USB_ERR_NO_ROOT_HUB; 634 } 635 } 636 } else { 637 err = USB_ERR_NOMEM; 638 } 639 640 USB_BUS_LOCK(bus); 641 642 if (err) { 643 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 644 usbd_errstr(err)); 645 usb_root_mount_rel(bus); 646 } 647 648 /* set softc - we are ready */ 649 device_set_softc(dev, bus); 650 651 /* start watchdog */ 652 usb_power_wdog(bus); 653 } 654 655 /*------------------------------------------------------------------------* 656 * usb_attach_sub 657 * 658 * This function creates a thread which runs the USB attach code. 659 *------------------------------------------------------------------------*/ 660 static void 661 usb_attach_sub(device_t dev, struct usb_bus *bus) 662 { 663 const char *pname = device_get_nameunit(dev); 664 665 mtx_lock(&Giant); 666 if (usb_devclass_ptr == NULL) 667 usb_devclass_ptr = devclass_find("usbus"); 668 mtx_unlock(&Giant); 669 670 #if USB_HAVE_PF 671 usbpf_attach(bus); 672 #endif 673 /* Initialise USB process messages */ 674 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 675 bus->explore_msg[0].bus = bus; 676 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 677 bus->explore_msg[1].bus = bus; 678 679 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 680 bus->detach_msg[0].bus = bus; 681 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 682 bus->detach_msg[1].bus = bus; 683 684 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 685 bus->attach_msg[0].bus = bus; 686 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 687 bus->attach_msg[1].bus = bus; 688 689 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 690 bus->suspend_msg[0].bus = bus; 691 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 692 bus->suspend_msg[1].bus = bus; 693 694 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 695 bus->resume_msg[0].bus = bus; 696 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 697 bus->resume_msg[1].bus = bus; 698 699 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 700 bus->shutdown_msg[0].bus = bus; 701 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 702 bus->shutdown_msg[1].bus = bus; 703 704 /* Create USB explore and callback processes */ 705 706 if (usb_proc_create(&bus->giant_callback_proc, 707 &bus->bus_mtx, pname, USB_PRI_MED)) { 708 device_printf(dev, "WARNING: Creation of USB Giant " 709 "callback process failed.\n"); 710 } else if (usb_proc_create(&bus->non_giant_callback_proc, 711 &bus->bus_mtx, pname, USB_PRI_HIGH)) { 712 device_printf(dev, "WARNING: Creation of USB non-Giant " 713 "callback process failed.\n"); 714 } else if (usb_proc_create(&bus->explore_proc, 715 &bus->bus_mtx, pname, USB_PRI_MED)) { 716 device_printf(dev, "WARNING: Creation of USB explore " 717 "process failed.\n"); 718 } else if (usb_proc_create(&bus->control_xfer_proc, 719 &bus->bus_mtx, pname, USB_PRI_MED)) { 720 device_printf(dev, "WARNING: Creation of USB control transfer " 721 "process failed.\n"); 722 } else { 723 /* Get final attach going */ 724 USB_BUS_LOCK(bus); 725 usb_proc_msignal(&bus->explore_proc, 726 &bus->attach_msg[0], &bus->attach_msg[1]); 727 USB_BUS_UNLOCK(bus); 728 729 /* Do initial explore */ 730 usb_needs_explore(bus, 1); 731 } 732 } 733 734 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 735 736 /*------------------------------------------------------------------------* 737 * usb_bus_mem_flush_all_cb 738 *------------------------------------------------------------------------*/ 739 #if USB_HAVE_BUSDMA 740 static void 741 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 742 struct usb_page *pg, usb_size_t size, usb_size_t align) 743 { 744 usb_pc_cpu_flush(pc); 745 } 746 #endif 747 748 /*------------------------------------------------------------------------* 749 * usb_bus_mem_flush_all - factored out code 750 *------------------------------------------------------------------------*/ 751 #if USB_HAVE_BUSDMA 752 void 753 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 754 { 755 if (cb) { 756 cb(bus, &usb_bus_mem_flush_all_cb); 757 } 758 } 759 #endif 760 761 /*------------------------------------------------------------------------* 762 * usb_bus_mem_alloc_all_cb 763 *------------------------------------------------------------------------*/ 764 #if USB_HAVE_BUSDMA 765 static void 766 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 767 struct usb_page *pg, usb_size_t size, usb_size_t align) 768 { 769 /* need to initialize the page cache */ 770 pc->tag_parent = bus->dma_parent_tag; 771 772 if (usb_pc_alloc_mem(pc, pg, size, align)) { 773 bus->alloc_failed = 1; 774 } 775 } 776 #endif 777 778 /*------------------------------------------------------------------------* 779 * usb_bus_mem_alloc_all - factored out code 780 * 781 * Returns: 782 * 0: Success 783 * Else: Failure 784 *------------------------------------------------------------------------*/ 785 uint8_t 786 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 787 usb_bus_mem_cb_t *cb) 788 { 789 bus->alloc_failed = 0; 790 791 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 792 NULL, MTX_DEF | MTX_RECURSE); 793 794 usb_callout_init_mtx(&bus->power_wdog, 795 &bus->bus_mtx, 0); 796 797 TAILQ_INIT(&bus->intr_q.head); 798 799 #if USB_HAVE_BUSDMA 800 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 801 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 802 #endif 803 if ((bus->devices_max > USB_MAX_DEVICES) || 804 (bus->devices_max < USB_MIN_DEVICES) || 805 (bus->devices == NULL)) { 806 DPRINTFN(0, "Devices field has not been " 807 "initialised properly\n"); 808 bus->alloc_failed = 1; /* failure */ 809 } 810 #if USB_HAVE_BUSDMA 811 if (cb) { 812 cb(bus, &usb_bus_mem_alloc_all_cb); 813 } 814 #endif 815 if (bus->alloc_failed) { 816 usb_bus_mem_free_all(bus, cb); 817 } 818 return (bus->alloc_failed); 819 } 820 821 /*------------------------------------------------------------------------* 822 * usb_bus_mem_free_all_cb 823 *------------------------------------------------------------------------*/ 824 #if USB_HAVE_BUSDMA 825 static void 826 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 827 struct usb_page *pg, usb_size_t size, usb_size_t align) 828 { 829 usb_pc_free_mem(pc); 830 } 831 #endif 832 833 /*------------------------------------------------------------------------* 834 * usb_bus_mem_free_all - factored out code 835 *------------------------------------------------------------------------*/ 836 void 837 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 838 { 839 #if USB_HAVE_BUSDMA 840 if (cb) { 841 cb(bus, &usb_bus_mem_free_all_cb); 842 } 843 usb_dma_tag_unsetup(bus->dma_parent_tag); 844 #endif 845 846 mtx_destroy(&bus->bus_mtx); 847 } 848