1/* $FreeBSD: stable/9/sys/dev/usb/controller/usb_controller.c 242775 2012-11-08 16:13:51Z hselasky $ */
|
1/* $FreeBSD: stable/9/sys/dev/usb/controller/usb_controller.c 247090 2013-02-21 07:48:07Z hselasky $ */ |
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 82SYSCTL_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_shutdown 282 *------------------------------------------------------------------------*/ 283static int 284usb_shutdown(device_t dev) 285{ 286 struct usb_bus *bus = device_get_softc(dev); 287 288 DPRINTF("\n"); 289 290 if (bus == NULL) { 291 /* was never setup properly */ 292 return (0); 293 } 294 295 device_printf(bus->bdev, "Controller shutdown\n"); 296 297 USB_BUS_LOCK(bus); 298 usb_proc_msignal(&bus->explore_proc, 299 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 300 if (usb_no_shutdown_wait == 0) { 301 /* wait for shutdown callback to be executed */ 302 usb_proc_mwait(&bus->explore_proc, 303 &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 304 } 305 USB_BUS_UNLOCK(bus); 306 307 device_printf(bus->bdev, "Controller shutdown complete\n"); 308 309 return (0); 310} 311 312/*------------------------------------------------------------------------* 313 * usb_bus_explore 314 * 315 * This function is used to explore the device tree from the root. 316 *------------------------------------------------------------------------*/ 317static void 318usb_bus_explore(struct usb_proc_msg *pm) 319{ 320 struct usb_bus *bus; 321 struct usb_device *udev; 322 323 bus = ((struct usb_bus_msg *)pm)->bus; 324 udev = bus->devices[USB_ROOT_HUB_ADDR]; 325 326 if (bus->no_explore != 0) 327 return; 328 329 if (udev && udev->hub) { 330 331 if (bus->do_probe) { 332 bus->do_probe = 0; 333 bus->driver_added_refcount++; 334 } 335 if (bus->driver_added_refcount == 0) { 336 /* avoid zero, hence that is memory default */ 337 bus->driver_added_refcount = 1; 338 } 339 340#ifdef DDB 341 /* 342 * The following three lines of code are only here to 343 * recover from DDB: 344 */ 345 usb_proc_rewakeup(&bus->control_xfer_proc); 346 usb_proc_rewakeup(&bus->giant_callback_proc); 347 usb_proc_rewakeup(&bus->non_giant_callback_proc); 348#endif 349 350 USB_BUS_UNLOCK(bus); 351 352#if USB_HAVE_POWERD 353 /* 354 * First update the USB power state! 355 */ 356 usb_bus_powerd(bus); 357#endif 358 /* Explore the Root USB HUB. */ 359 (udev->hub->explore) (udev); 360 USB_BUS_LOCK(bus); 361 } 362 usb_root_mount_rel(bus); 363} 364 365/*------------------------------------------------------------------------* 366 * usb_bus_detach 367 * 368 * This function is used to detach the device tree from the root. 369 *------------------------------------------------------------------------*/ 370static void 371usb_bus_detach(struct usb_proc_msg *pm) 372{ 373 struct usb_bus *bus; 374 struct usb_device *udev; 375 device_t dev; 376 377 bus = ((struct usb_bus_msg *)pm)->bus; 378 udev = bus->devices[USB_ROOT_HUB_ADDR]; 379 dev = bus->bdev; 380 /* clear the softc */ 381 device_set_softc(dev, NULL); 382 USB_BUS_UNLOCK(bus); 383 384 /* detach children first */ 385 mtx_lock(&Giant); 386 bus_generic_detach(dev); 387 mtx_unlock(&Giant); 388 389 /* 390 * Free USB device and all subdevices, if any. 391 */ 392 usb_free_device(udev, 0); 393 394 USB_BUS_LOCK(bus); 395 /* clear bdev variable last */ 396 bus->bdev = NULL; 397} 398 399/*------------------------------------------------------------------------* 400 * usb_bus_suspend 401 * 402 * This function is used to suspend the USB contoller. 403 *------------------------------------------------------------------------*/ 404static void 405usb_bus_suspend(struct usb_proc_msg *pm) 406{ 407 struct usb_bus *bus; 408 struct usb_device *udev; 409 usb_error_t err;
|
410 uint8_t do_unlock; |
411 412 bus = ((struct usb_bus_msg *)pm)->bus; 413 udev = bus->devices[USB_ROOT_HUB_ADDR]; 414 415 if (udev == NULL || bus->bdev == NULL) 416 return; 417 418 USB_BUS_UNLOCK(bus); 419 420 /* 421 * We use the shutdown event here because the suspend and 422 * resume events are reserved for the USB port suspend and 423 * resume. The USB system suspend is implemented like full 424 * shutdown and all connected USB devices will be disconnected 425 * subsequently. At resume all USB devices will be 426 * re-connected again. 427 */ 428 429 bus_generic_shutdown(bus->bdev); 430
|
430 usbd_enum_lock(udev);
|
431 do_unlock = usbd_enum_lock(udev); |
432 433 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 434 if (err) 435 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 436 437 USB_BUS_LOCK(bus); 438 bus->hw_power_state = 0; 439 bus->no_explore = 1; 440 USB_BUS_UNLOCK(bus); 441 442 if (bus->methods->set_hw_power != NULL) 443 (bus->methods->set_hw_power) (bus); 444 445 if (bus->methods->set_hw_power_sleep != NULL) 446 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 447
|
447 usbd_enum_unlock(udev);
|
448 if (do_unlock) 449 usbd_enum_unlock(udev); |
450 451 USB_BUS_LOCK(bus); 452} 453 454/*------------------------------------------------------------------------* 455 * usb_bus_resume 456 * 457 * This function is used to resume the USB contoller. 458 *------------------------------------------------------------------------*/ 459static void 460usb_bus_resume(struct usb_proc_msg *pm) 461{ 462 struct usb_bus *bus; 463 struct usb_device *udev; 464 usb_error_t err;
|
465 uint8_t do_unlock; |
466 467 bus = ((struct usb_bus_msg *)pm)->bus; 468 udev = bus->devices[USB_ROOT_HUB_ADDR]; 469 470 if (udev == NULL || bus->bdev == NULL) 471 return; 472 473 USB_BUS_UNLOCK(bus); 474
|
472 usbd_enum_lock(udev);
|
475 do_unlock = usbd_enum_lock(udev); |
476#if 0 477 DEVMETHOD(usb_take_controller, NULL); /* dummy */ 478#endif 479 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 480 481 USB_BUS_LOCK(bus); 482 bus->hw_power_state = 483 USB_HW_POWER_CONTROL | 484 USB_HW_POWER_BULK | 485 USB_HW_POWER_INTERRUPT | 486 USB_HW_POWER_ISOC | 487 USB_HW_POWER_NON_ROOT_HUB; 488 bus->no_explore = 0; 489 USB_BUS_UNLOCK(bus); 490 491 if (bus->methods->set_hw_power_sleep != NULL) 492 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 493 494 if (bus->methods->set_hw_power != NULL) 495 (bus->methods->set_hw_power) (bus); 496 497 /* restore USB configuration to index 0 */ 498 err = usbd_set_config_index(udev, 0); 499 if (err) 500 device_printf(bus->bdev, "Could not configure root HUB\n"); 501 502 /* probe and attach */ 503 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 504 if (err) { 505 device_printf(bus->bdev, "Could not probe and " 506 "attach root HUB\n"); 507 } 508
|
506 usbd_enum_unlock(udev);
|
509 if (do_unlock) 510 usbd_enum_unlock(udev); |
511 512 USB_BUS_LOCK(bus); 513} 514 515/*------------------------------------------------------------------------* 516 * usb_bus_shutdown 517 * 518 * This function is used to shutdown the USB contoller. 519 *------------------------------------------------------------------------*/ 520static void 521usb_bus_shutdown(struct usb_proc_msg *pm) 522{ 523 struct usb_bus *bus; 524 struct usb_device *udev; 525 usb_error_t err;
|
526 uint8_t do_unlock; |
527 528 bus = ((struct usb_bus_msg *)pm)->bus; 529 udev = bus->devices[USB_ROOT_HUB_ADDR]; 530 531 if (udev == NULL || bus->bdev == NULL) 532 return; 533 534 USB_BUS_UNLOCK(bus); 535 536 bus_generic_shutdown(bus->bdev); 537
|
533 usbd_enum_lock(udev);
|
538 do_unlock = usbd_enum_lock(udev); |
539 540 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 541 if (err) 542 device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 543 544 USB_BUS_LOCK(bus); 545 bus->hw_power_state = 0; 546 bus->no_explore = 1; 547 USB_BUS_UNLOCK(bus); 548 549 if (bus->methods->set_hw_power != NULL) 550 (bus->methods->set_hw_power) (bus); 551 552 if (bus->methods->set_hw_power_sleep != NULL) 553 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 554
|
550 usbd_enum_unlock(udev);
|
555 if (do_unlock) 556 usbd_enum_unlock(udev); |
557 558 USB_BUS_LOCK(bus); 559} 560 561static void 562usb_power_wdog(void *arg) 563{ 564 struct usb_bus *bus = arg; 565 566 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 567 568 usb_callout_reset(&bus->power_wdog, 569 4 * hz, usb_power_wdog, arg); 570 571#ifdef DDB 572 /* 573 * The following line of code is only here to recover from 574 * DDB: 575 */ 576 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 577#endif 578 579#if USB_HAVE_POWERD 580 USB_BUS_UNLOCK(bus); 581 582 usb_bus_power_update(bus); 583 584 USB_BUS_LOCK(bus); 585#endif 586} 587 588/*------------------------------------------------------------------------* 589 * usb_bus_attach 590 * 591 * This function attaches USB in context of the explore thread. 592 *------------------------------------------------------------------------*/ 593static void 594usb_bus_attach(struct usb_proc_msg *pm) 595{ 596 struct usb_bus *bus; 597 struct usb_device *child; 598 device_t dev; 599 usb_error_t err; 600 enum usb_dev_speed speed; 601 602 bus = ((struct usb_bus_msg *)pm)->bus; 603 dev = bus->bdev; 604 605 DPRINTF("\n"); 606 607 switch (bus->usbrev) { 608 case USB_REV_1_0: 609 speed = USB_SPEED_FULL; 610 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 611 break; 612 613 case USB_REV_1_1: 614 speed = USB_SPEED_FULL; 615 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 616 break; 617 618 case USB_REV_2_0: 619 speed = USB_SPEED_HIGH; 620 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 621 break; 622 623 case USB_REV_2_5: 624 speed = USB_SPEED_VARIABLE; 625 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 626 break; 627 628 case USB_REV_3_0: 629 speed = USB_SPEED_SUPER; 630 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 631 break; 632 633 default: 634 device_printf(bus->bdev, "Unsupported USB revision\n"); 635 usb_root_mount_rel(bus); 636 return; 637 } 638 639 /* default power_mask value */ 640 bus->hw_power_state = 641 USB_HW_POWER_CONTROL | 642 USB_HW_POWER_BULK | 643 USB_HW_POWER_INTERRUPT | 644 USB_HW_POWER_ISOC | 645 USB_HW_POWER_NON_ROOT_HUB; 646 647 USB_BUS_UNLOCK(bus); 648 649 /* make sure power is set at least once */ 650 651 if (bus->methods->set_hw_power != NULL) { 652 (bus->methods->set_hw_power) (bus); 653 } 654 655 /* allocate the Root USB device */ 656 657 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 658 speed, USB_MODE_HOST); 659 if (child) { 660 err = usb_probe_and_attach(child, 661 USB_IFACE_INDEX_ANY); 662 if (!err) { 663 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 664 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 665 err = USB_ERR_NO_ROOT_HUB; 666 } 667 } 668 } else { 669 err = USB_ERR_NOMEM; 670 } 671 672 USB_BUS_LOCK(bus); 673 674 if (err) { 675 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 676 usbd_errstr(err)); 677 usb_root_mount_rel(bus); 678 } 679 680 /* set softc - we are ready */ 681 device_set_softc(dev, bus); 682 683 /* start watchdog */ 684 usb_power_wdog(bus); 685} 686 687/*------------------------------------------------------------------------* 688 * usb_attach_sub 689 * 690 * This function creates a thread which runs the USB attach code. 691 *------------------------------------------------------------------------*/ 692static void 693usb_attach_sub(device_t dev, struct usb_bus *bus) 694{ 695 const char *pname = device_get_nameunit(dev); 696 697 mtx_lock(&Giant); 698 if (usb_devclass_ptr == NULL) 699 usb_devclass_ptr = devclass_find("usbus"); 700 mtx_unlock(&Giant); 701 702#if USB_HAVE_PF 703 usbpf_attach(bus); 704#endif 705 /* Initialise USB process messages */ 706 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 707 bus->explore_msg[0].bus = bus; 708 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 709 bus->explore_msg[1].bus = bus; 710 711 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 712 bus->detach_msg[0].bus = bus; 713 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 714 bus->detach_msg[1].bus = bus; 715 716 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 717 bus->attach_msg[0].bus = bus; 718 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 719 bus->attach_msg[1].bus = bus; 720 721 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 722 bus->suspend_msg[0].bus = bus; 723 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 724 bus->suspend_msg[1].bus = bus; 725 726 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 727 bus->resume_msg[0].bus = bus; 728 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 729 bus->resume_msg[1].bus = bus; 730 731 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 732 bus->shutdown_msg[0].bus = bus; 733 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 734 bus->shutdown_msg[1].bus = bus; 735 736 /* Create USB explore and callback processes */ 737 738 if (usb_proc_create(&bus->giant_callback_proc, 739 &bus->bus_mtx, pname, USB_PRI_MED)) { 740 device_printf(dev, "WARNING: Creation of USB Giant " 741 "callback process failed.\n"); 742 } else if (usb_proc_create(&bus->non_giant_callback_proc, 743 &bus->bus_mtx, pname, USB_PRI_HIGH)) { 744 device_printf(dev, "WARNING: Creation of USB non-Giant " 745 "callback process failed.\n"); 746 } else if (usb_proc_create(&bus->explore_proc, 747 &bus->bus_mtx, pname, USB_PRI_MED)) { 748 device_printf(dev, "WARNING: Creation of USB explore " 749 "process failed.\n"); 750 } else if (usb_proc_create(&bus->control_xfer_proc, 751 &bus->bus_mtx, pname, USB_PRI_MED)) { 752 device_printf(dev, "WARNING: Creation of USB control transfer " 753 "process failed.\n"); 754 } else { 755 /* Get final attach going */ 756 USB_BUS_LOCK(bus); 757 usb_proc_msignal(&bus->explore_proc, 758 &bus->attach_msg[0], &bus->attach_msg[1]); 759 USB_BUS_UNLOCK(bus); 760 761 /* Do initial explore */ 762 usb_needs_explore(bus, 1); 763 } 764} 765 766SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 767 768/*------------------------------------------------------------------------* 769 * usb_bus_mem_flush_all_cb 770 *------------------------------------------------------------------------*/ 771#if USB_HAVE_BUSDMA 772static void 773usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 774 struct usb_page *pg, usb_size_t size, usb_size_t align) 775{ 776 usb_pc_cpu_flush(pc); 777} 778#endif 779 780/*------------------------------------------------------------------------* 781 * usb_bus_mem_flush_all - factored out code 782 *------------------------------------------------------------------------*/ 783#if USB_HAVE_BUSDMA 784void 785usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 786{ 787 if (cb) { 788 cb(bus, &usb_bus_mem_flush_all_cb); 789 } 790} 791#endif 792 793/*------------------------------------------------------------------------* 794 * usb_bus_mem_alloc_all_cb 795 *------------------------------------------------------------------------*/ 796#if USB_HAVE_BUSDMA 797static void 798usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 799 struct usb_page *pg, usb_size_t size, usb_size_t align) 800{ 801 /* need to initialize the page cache */ 802 pc->tag_parent = bus->dma_parent_tag; 803 804 if (usb_pc_alloc_mem(pc, pg, size, align)) { 805 bus->alloc_failed = 1; 806 } 807} 808#endif 809 810/*------------------------------------------------------------------------* 811 * usb_bus_mem_alloc_all - factored out code 812 * 813 * Returns: 814 * 0: Success 815 * Else: Failure 816 *------------------------------------------------------------------------*/ 817uint8_t 818usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 819 usb_bus_mem_cb_t *cb) 820{ 821 bus->alloc_failed = 0; 822 823 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 824 NULL, MTX_DEF | MTX_RECURSE); 825 826 usb_callout_init_mtx(&bus->power_wdog, 827 &bus->bus_mtx, 0); 828 829 TAILQ_INIT(&bus->intr_q.head); 830 831#if USB_HAVE_BUSDMA 832 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 833 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 834#endif 835 if ((bus->devices_max > USB_MAX_DEVICES) || 836 (bus->devices_max < USB_MIN_DEVICES) || 837 (bus->devices == NULL)) { 838 DPRINTFN(0, "Devices field has not been " 839 "initialised properly\n"); 840 bus->alloc_failed = 1; /* failure */ 841 } 842#if USB_HAVE_BUSDMA 843 if (cb) { 844 cb(bus, &usb_bus_mem_alloc_all_cb); 845 } 846#endif 847 if (bus->alloc_failed) { 848 usb_bus_mem_free_all(bus, cb); 849 } 850 return (bus->alloc_failed); 851} 852 853/*------------------------------------------------------------------------* 854 * usb_bus_mem_free_all_cb 855 *------------------------------------------------------------------------*/ 856#if USB_HAVE_BUSDMA 857static void 858usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 859 struct usb_page *pg, usb_size_t size, usb_size_t align) 860{ 861 usb_pc_free_mem(pc); 862} 863#endif 864 865/*------------------------------------------------------------------------* 866 * usb_bus_mem_free_all - factored out code 867 *------------------------------------------------------------------------*/ 868void 869usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 870{ 871#if USB_HAVE_BUSDMA 872 if (cb) { 873 cb(bus, &usb_bus_mem_free_all_cb); 874 } 875 usb_dma_tag_unsetup(bus->dma_parent_tag); 876#endif 877 878 mtx_destroy(&bus->bus_mtx); 879}
|