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