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 <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/linker_set.h> 36 #include <sys/module.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/condvar.h> 40 #include <sys/sysctl.h> 41 #include <sys/sx.h> 42 #include <sys/unistd.h> 43 #include <sys/callout.h> 44 #include <sys/malloc.h> 45 #include <sys/priv.h> 46 47 #include <dev/usb/usb.h> 48 #include <dev/usb/usbdi.h> 49 50 #define USB_DEBUG_VAR usb_ctrl_debug 51 52 #include <dev/usb/usb_core.h> 53 #include <dev/usb/usb_debug.h> 54 #include <dev/usb/usb_process.h> 55 #include <dev/usb/usb_busdma.h> 56 #include <dev/usb/usb_dynamic.h> 57 #include <dev/usb/usb_device.h> 58 #include <dev/usb/usb_hub.h> 59 60 #include <dev/usb/usb_controller.h> 61 #include <dev/usb/usb_bus.h> 62 63 /* function prototypes */ 64 65 static device_probe_t usb_probe; 66 static device_attach_t usb_attach; 67 static device_detach_t usb_detach; 68 69 static void usb_attach_sub(device_t, struct usb_bus *); 70 static void usb_post_init(void *); 71 72 /* static variables */ 73 74 #ifdef USB_DEBUG 75 static int usb_ctrl_debug = 0; 76 77 SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller"); 78 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0, 79 "Debug level"); 80 #endif 81 82 static int usb_no_boot_wait = 0; 83 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait); 84 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0, 85 "No device enumerate waiting at boot."); 86 87 static uint8_t usb_post_init_called = 0; 88 89 static devclass_t usb_devclass; 90 91 static device_method_t usb_methods[] = { 92 DEVMETHOD(device_probe, usb_probe), 93 DEVMETHOD(device_attach, usb_attach), 94 DEVMETHOD(device_detach, usb_detach), 95 DEVMETHOD(device_suspend, bus_generic_suspend), 96 DEVMETHOD(device_resume, bus_generic_resume), 97 DEVMETHOD(device_shutdown, bus_generic_shutdown), 98 {0, 0} 99 }; 100 101 static driver_t usb_driver = { 102 .name = "usbus", 103 .methods = usb_methods, 104 .size = 0, 105 }; 106 107 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 108 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 109 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 110 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0); 111 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0); 112 113 /*------------------------------------------------------------------------* 114 * usb_probe 115 * 116 * This function is called from "{ehci,ohci,uhci}_pci_attach()". 117 *------------------------------------------------------------------------*/ 118 static int 119 usb_probe(device_t dev) 120 { 121 DPRINTF("\n"); 122 return (0); 123 } 124 125 /*------------------------------------------------------------------------* 126 * usb_attach 127 *------------------------------------------------------------------------*/ 128 static int 129 usb_attach(device_t dev) 130 { 131 struct usb_bus *bus = device_get_ivars(dev); 132 133 DPRINTF("\n"); 134 135 if (bus == NULL) { 136 DPRINTFN(0, "USB device has no ivars\n"); 137 return (ENXIO); 138 } 139 140 if (usb_no_boot_wait == 0) { 141 /* delay vfs_mountroot until the bus is explored */ 142 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 143 } 144 145 if (usb_post_init_called) { 146 mtx_lock(&Giant); 147 usb_attach_sub(dev, bus); 148 mtx_unlock(&Giant); 149 usb_needs_explore(bus, 1); 150 } 151 return (0); /* return success */ 152 } 153 154 /*------------------------------------------------------------------------* 155 * usb_detach 156 *------------------------------------------------------------------------*/ 157 static int 158 usb_detach(device_t dev) 159 { 160 struct usb_bus *bus = device_get_softc(dev); 161 162 DPRINTF("\n"); 163 164 if (bus == NULL) { 165 /* was never setup properly */ 166 return (0); 167 } 168 /* Stop power watchdog */ 169 usb_callout_drain(&bus->power_wdog); 170 171 /* Let the USB explore process detach all devices. */ 172 if (bus->bus_roothold != NULL) { 173 root_mount_rel(bus->bus_roothold); 174 bus->bus_roothold = NULL; 175 } 176 177 USB_BUS_LOCK(bus); 178 if (usb_proc_msignal(&bus->explore_proc, 179 &bus->detach_msg[0], &bus->detach_msg[1])) { 180 /* ignore */ 181 } 182 /* Wait for detach to complete */ 183 184 usb_proc_mwait(&bus->explore_proc, 185 &bus->detach_msg[0], &bus->detach_msg[1]); 186 187 USB_BUS_UNLOCK(bus); 188 189 /* Get rid of USB callback processes */ 190 191 usb_proc_free(&bus->giant_callback_proc); 192 usb_proc_free(&bus->non_giant_callback_proc); 193 194 /* Get rid of USB explore process */ 195 196 usb_proc_free(&bus->explore_proc); 197 198 /* Get rid of control transfer process */ 199 200 usb_proc_free(&bus->control_xfer_proc); 201 202 return (0); 203 } 204 205 /*------------------------------------------------------------------------* 206 * usb_bus_explore 207 * 208 * This function is used to explore the device tree from the root. 209 *------------------------------------------------------------------------*/ 210 static void 211 usb_bus_explore(struct usb_proc_msg *pm) 212 { 213 struct usb_bus *bus; 214 struct usb_device *udev; 215 216 bus = ((struct usb_bus_msg *)pm)->bus; 217 udev = bus->devices[USB_ROOT_HUB_ADDR]; 218 219 if (udev && udev->hub) { 220 221 if (bus->do_probe) { 222 bus->do_probe = 0; 223 bus->driver_added_refcount++; 224 } 225 if (bus->driver_added_refcount == 0) { 226 /* avoid zero, hence that is memory default */ 227 bus->driver_added_refcount = 1; 228 } 229 USB_BUS_UNLOCK(bus); 230 231 mtx_lock(&Giant); 232 233 /* 234 * First update the USB power state! 235 */ 236 usb_bus_powerd(bus); 237 /* 238 * Explore the Root USB HUB. This call can sleep, 239 * exiting Giant, which is actually Giant. 240 */ 241 (udev->hub->explore) (udev); 242 243 mtx_unlock(&Giant); 244 245 USB_BUS_LOCK(bus); 246 } 247 if (bus->bus_roothold != NULL) { 248 root_mount_rel(bus->bus_roothold); 249 bus->bus_roothold = NULL; 250 } 251 } 252 253 /*------------------------------------------------------------------------* 254 * usb_bus_detach 255 * 256 * This function is used to detach the device tree from the root. 257 *------------------------------------------------------------------------*/ 258 static void 259 usb_bus_detach(struct usb_proc_msg *pm) 260 { 261 struct usb_bus *bus; 262 struct usb_device *udev; 263 device_t dev; 264 265 bus = ((struct usb_bus_msg *)pm)->bus; 266 udev = bus->devices[USB_ROOT_HUB_ADDR]; 267 dev = bus->bdev; 268 /* clear the softc */ 269 device_set_softc(dev, NULL); 270 USB_BUS_UNLOCK(bus); 271 272 mtx_lock(&Giant); 273 274 /* detach children first */ 275 bus_generic_detach(dev); 276 277 /* 278 * Free USB Root device, but not any sub-devices, hence they 279 * are freed by the caller of this function: 280 */ 281 usb_free_device(udev, 282 USB_UNCFG_FLAG_FREE_EP0); 283 284 mtx_unlock(&Giant); 285 USB_BUS_LOCK(bus); 286 /* clear bdev variable last */ 287 bus->bdev = NULL; 288 } 289 290 static void 291 usb_power_wdog(void *arg) 292 { 293 struct usb_bus *bus = arg; 294 295 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 296 297 usb_callout_reset(&bus->power_wdog, 298 4 * hz, usb_power_wdog, arg); 299 300 USB_BUS_UNLOCK(bus); 301 302 usb_bus_power_update(bus); 303 304 USB_BUS_LOCK(bus); 305 } 306 307 /*------------------------------------------------------------------------* 308 * usb_bus_attach 309 * 310 * This function attaches USB in context of the explore thread. 311 *------------------------------------------------------------------------*/ 312 static void 313 usb_bus_attach(struct usb_proc_msg *pm) 314 { 315 struct usb_bus *bus; 316 struct usb_device *child; 317 device_t dev; 318 usb_error_t err; 319 enum usb_dev_speed speed; 320 321 bus = ((struct usb_bus_msg *)pm)->bus; 322 dev = bus->bdev; 323 324 DPRINTF("\n"); 325 326 switch (bus->usbrev) { 327 case USB_REV_1_0: 328 speed = USB_SPEED_FULL; 329 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 330 break; 331 332 case USB_REV_1_1: 333 speed = USB_SPEED_FULL; 334 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 335 break; 336 337 case USB_REV_2_0: 338 speed = USB_SPEED_HIGH; 339 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 340 break; 341 342 case USB_REV_2_5: 343 speed = USB_SPEED_VARIABLE; 344 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 345 break; 346 347 default: 348 device_printf(bus->bdev, "Unsupported USB revision!\n"); 349 return; 350 } 351 352 USB_BUS_UNLOCK(bus); 353 mtx_lock(&Giant); /* XXX not required by USB */ 354 355 /* default power_mask value */ 356 bus->hw_power_state = 357 USB_HW_POWER_CONTROL | 358 USB_HW_POWER_BULK | 359 USB_HW_POWER_INTERRUPT | 360 USB_HW_POWER_ISOC | 361 USB_HW_POWER_NON_ROOT_HUB; 362 363 /* make sure power is set at least once */ 364 365 if (bus->methods->set_hw_power != NULL) { 366 (bus->methods->set_hw_power) (bus); 367 } 368 369 /* Allocate the Root USB device */ 370 371 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 372 speed, USB_MODE_HOST); 373 if (child) { 374 err = usb_probe_and_attach(child, 375 USB_IFACE_INDEX_ANY); 376 if (!err) { 377 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 378 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 379 err = USB_ERR_NO_ROOT_HUB; 380 } 381 } 382 } else { 383 err = USB_ERR_NOMEM; 384 } 385 386 mtx_unlock(&Giant); 387 USB_BUS_LOCK(bus); 388 389 if (err) { 390 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 391 usbd_errstr(err)); 392 } 393 394 /* set softc - we are ready */ 395 device_set_softc(dev, bus); 396 397 /* start watchdog */ 398 usb_power_wdog(bus); 399 } 400 401 /*------------------------------------------------------------------------* 402 * usb_attach_sub 403 * 404 * This function creates a thread which runs the USB attach code. It 405 * is factored out, hence it can be called at two different places in 406 * time. During bootup this function is called from 407 * "usb_post_init". During hot-plug it is called directly from the 408 * "usb_attach()" method. 409 *------------------------------------------------------------------------*/ 410 static void 411 usb_attach_sub(device_t dev, struct usb_bus *bus) 412 { 413 const char *pname = device_get_nameunit(dev); 414 415 /* Initialise USB process messages */ 416 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 417 bus->explore_msg[0].bus = bus; 418 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 419 bus->explore_msg[1].bus = bus; 420 421 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 422 bus->detach_msg[0].bus = bus; 423 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 424 bus->detach_msg[1].bus = bus; 425 426 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 427 bus->attach_msg[0].bus = bus; 428 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 429 bus->attach_msg[1].bus = bus; 430 431 /* Create USB explore and callback processes */ 432 433 if (usb_proc_create(&bus->giant_callback_proc, 434 &bus->bus_mtx, pname, USB_PRI_MED)) { 435 printf("WARNING: Creation of USB Giant " 436 "callback process failed.\n"); 437 } else if (usb_proc_create(&bus->non_giant_callback_proc, 438 &bus->bus_mtx, pname, USB_PRI_HIGH)) { 439 printf("WARNING: Creation of USB non-Giant " 440 "callback process failed.\n"); 441 } else if (usb_proc_create(&bus->explore_proc, 442 &bus->bus_mtx, pname, USB_PRI_MED)) { 443 printf("WARNING: Creation of USB explore " 444 "process failed.\n"); 445 } else if (usb_proc_create(&bus->control_xfer_proc, 446 &bus->bus_mtx, pname, USB_PRI_MED)) { 447 printf("WARNING: Creation of USB control transfer " 448 "process failed.\n"); 449 } else { 450 /* Get final attach going */ 451 USB_BUS_LOCK(bus); 452 if (usb_proc_msignal(&bus->explore_proc, 453 &bus->attach_msg[0], &bus->attach_msg[1])) { 454 /* ignore */ 455 } 456 USB_BUS_UNLOCK(bus); 457 } 458 } 459 460 /*------------------------------------------------------------------------* 461 * usb_post_init 462 * 463 * This function is called to attach all USB busses that were found 464 * during bootup. 465 *------------------------------------------------------------------------*/ 466 static void 467 usb_post_init(void *arg) 468 { 469 struct usb_bus *bus; 470 devclass_t dc; 471 device_t dev; 472 int max; 473 int n; 474 475 mtx_lock(&Giant); 476 477 usb_devclass_ptr = devclass_find("usbus"); 478 479 dc = usb_devclass_ptr; 480 if (dc) { 481 max = devclass_get_maxunit(dc) + 1; 482 for (n = 0; n != max; n++) { 483 dev = devclass_get_device(dc, n); 484 if (dev && device_is_attached(dev)) { 485 bus = device_get_ivars(dev); 486 if (bus) { 487 mtx_lock(&Giant); 488 usb_attach_sub(dev, bus); 489 mtx_unlock(&Giant); 490 } 491 } 492 } 493 } else { 494 DPRINTFN(0, "no devclass\n"); 495 } 496 usb_post_init_called = 1; 497 498 /* explore all USB busses in parallell */ 499 500 usb_needs_explore_all(); 501 502 mtx_unlock(&Giant); 503 } 504 505 SYSINIT(usb_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_post_init, NULL); 506 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 507 508 /*------------------------------------------------------------------------* 509 * usb_bus_mem_flush_all_cb 510 *------------------------------------------------------------------------*/ 511 #if USB_HAVE_BUSDMA 512 static void 513 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 514 struct usb_page *pg, usb_size_t size, usb_size_t align) 515 { 516 usb_pc_cpu_flush(pc); 517 } 518 #endif 519 520 /*------------------------------------------------------------------------* 521 * usb_bus_mem_flush_all - factored out code 522 *------------------------------------------------------------------------*/ 523 #if USB_HAVE_BUSDMA 524 void 525 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 526 { 527 if (cb) { 528 cb(bus, &usb_bus_mem_flush_all_cb); 529 } 530 } 531 #endif 532 533 /*------------------------------------------------------------------------* 534 * usb_bus_mem_alloc_all_cb 535 *------------------------------------------------------------------------*/ 536 #if USB_HAVE_BUSDMA 537 static void 538 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 539 struct usb_page *pg, usb_size_t size, usb_size_t align) 540 { 541 /* need to initialize the page cache */ 542 pc->tag_parent = bus->dma_parent_tag; 543 544 if (usb_pc_alloc_mem(pc, pg, size, align)) { 545 bus->alloc_failed = 1; 546 } 547 } 548 #endif 549 550 /*------------------------------------------------------------------------* 551 * usb_bus_mem_alloc_all - factored out code 552 * 553 * Returns: 554 * 0: Success 555 * Else: Failure 556 *------------------------------------------------------------------------*/ 557 uint8_t 558 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 559 usb_bus_mem_cb_t *cb) 560 { 561 bus->alloc_failed = 0; 562 563 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 564 NULL, MTX_DEF | MTX_RECURSE); 565 566 usb_callout_init_mtx(&bus->power_wdog, 567 &bus->bus_mtx, 0); 568 569 TAILQ_INIT(&bus->intr_q.head); 570 571 #if USB_HAVE_BUSDMA 572 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 573 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 574 #endif 575 if ((bus->devices_max > USB_MAX_DEVICES) || 576 (bus->devices_max < USB_MIN_DEVICES) || 577 (bus->devices == NULL)) { 578 DPRINTFN(0, "Devices field has not been " 579 "initialised properly!\n"); 580 bus->alloc_failed = 1; /* failure */ 581 } 582 #if USB_HAVE_BUSDMA 583 if (cb) { 584 cb(bus, &usb_bus_mem_alloc_all_cb); 585 } 586 #endif 587 if (bus->alloc_failed) { 588 usb_bus_mem_free_all(bus, cb); 589 } 590 return (bus->alloc_failed); 591 } 592 593 /*------------------------------------------------------------------------* 594 * usb_bus_mem_free_all_cb 595 *------------------------------------------------------------------------*/ 596 #if USB_HAVE_BUSDMA 597 static void 598 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 599 struct usb_page *pg, usb_size_t size, usb_size_t align) 600 { 601 usb_pc_free_mem(pc); 602 } 603 #endif 604 605 /*------------------------------------------------------------------------* 606 * usb_bus_mem_free_all - factored out code 607 *------------------------------------------------------------------------*/ 608 void 609 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 610 { 611 #if USB_HAVE_BUSDMA 612 if (cb) { 613 cb(bus, &usb_bus_mem_free_all_cb); 614 } 615 usb_dma_tag_unsetup(bus->dma_parent_tag); 616 #endif 617 618 mtx_destroy(&bus->bus_mtx); 619 } 620