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