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