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