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