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