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 device_printf(dev, "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 device and all subdevices, if any. 274 */ 275 usb_free_device(udev, 0); 276 277 USB_BUS_LOCK(bus); 278 /* clear bdev variable last */ 279 bus->bdev = NULL; 280 } 281 282 static void 283 usb_power_wdog(void *arg) 284 { 285 struct usb_bus *bus = arg; 286 287 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 288 289 usb_callout_reset(&bus->power_wdog, 290 4 * hz, usb_power_wdog, arg); 291 292 /* 293 * The following line of code is only here to recover from 294 * DDB: 295 */ 296 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 297 298 USB_BUS_UNLOCK(bus); 299 300 usb_bus_power_update(bus); 301 302 USB_BUS_LOCK(bus); 303 } 304 305 /*------------------------------------------------------------------------* 306 * usb_bus_attach 307 * 308 * This function attaches USB in context of the explore thread. 309 *------------------------------------------------------------------------*/ 310 static void 311 usb_bus_attach(struct usb_proc_msg *pm) 312 { 313 struct usb_bus *bus; 314 struct usb_device *child; 315 device_t dev; 316 usb_error_t err; 317 enum usb_dev_speed speed; 318 319 bus = ((struct usb_bus_msg *)pm)->bus; 320 dev = bus->bdev; 321 322 DPRINTF("\n"); 323 324 switch (bus->usbrev) { 325 case USB_REV_1_0: 326 speed = USB_SPEED_FULL; 327 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 328 break; 329 330 case USB_REV_1_1: 331 speed = USB_SPEED_FULL; 332 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 333 break; 334 335 case USB_REV_2_0: 336 speed = USB_SPEED_HIGH; 337 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 338 break; 339 340 case USB_REV_2_5: 341 speed = USB_SPEED_VARIABLE; 342 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 343 break; 344 345 default: 346 device_printf(bus->bdev, "Unsupported USB revision\n"); 347 return; 348 } 349 350 USB_BUS_UNLOCK(bus); 351 352 /* default power_mask value */ 353 bus->hw_power_state = 354 USB_HW_POWER_CONTROL | 355 USB_HW_POWER_BULK | 356 USB_HW_POWER_INTERRUPT | 357 USB_HW_POWER_ISOC | 358 USB_HW_POWER_NON_ROOT_HUB; 359 360 /* make sure power is set at least once */ 361 362 if (bus->methods->set_hw_power != NULL) { 363 (bus->methods->set_hw_power) (bus); 364 } 365 366 /* Allocate the Root USB device */ 367 368 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 369 speed, USB_MODE_HOST); 370 if (child) { 371 err = usb_probe_and_attach(child, 372 USB_IFACE_INDEX_ANY); 373 if (!err) { 374 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 375 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 376 err = USB_ERR_NO_ROOT_HUB; 377 } 378 } 379 } else { 380 err = USB_ERR_NOMEM; 381 } 382 383 USB_BUS_LOCK(bus); 384 385 if (err) { 386 device_printf(bus->bdev, "Root HUB problem, error=%s\n", 387 usbd_errstr(err)); 388 } 389 390 /* set softc - we are ready */ 391 device_set_softc(dev, bus); 392 393 /* start watchdog */ 394 usb_power_wdog(bus); 395 } 396 397 /*------------------------------------------------------------------------* 398 * usb_attach_sub 399 * 400 * This function creates a thread which runs the USB attach code. 401 *------------------------------------------------------------------------*/ 402 static void 403 usb_attach_sub(device_t dev, struct usb_bus *bus) 404 { 405 const char *pname = device_get_nameunit(dev); 406 407 mtx_lock(&Giant); 408 if (usb_devclass_ptr == NULL) 409 usb_devclass_ptr = devclass_find("usbus"); 410 mtx_unlock(&Giant); 411 412 /* Initialise USB process messages */ 413 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 414 bus->explore_msg[0].bus = bus; 415 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 416 bus->explore_msg[1].bus = bus; 417 418 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 419 bus->detach_msg[0].bus = bus; 420 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 421 bus->detach_msg[1].bus = bus; 422 423 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 424 bus->attach_msg[0].bus = bus; 425 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 426 bus->attach_msg[1].bus = bus; 427 428 /* Create USB explore and callback processes */ 429 430 if (usb_proc_create(&bus->giant_callback_proc, 431 &bus->bus_mtx, pname, USB_PRI_MED)) { 432 printf("WARNING: Creation of USB Giant " 433 "callback process failed.\n"); 434 } else if (usb_proc_create(&bus->non_giant_callback_proc, 435 &bus->bus_mtx, pname, USB_PRI_HIGH)) { 436 printf("WARNING: Creation of USB non-Giant " 437 "callback process failed.\n"); 438 } else if (usb_proc_create(&bus->explore_proc, 439 &bus->bus_mtx, pname, USB_PRI_MED)) { 440 printf("WARNING: Creation of USB explore " 441 "process failed.\n"); 442 } else if (usb_proc_create(&bus->control_xfer_proc, 443 &bus->bus_mtx, pname, USB_PRI_MED)) { 444 printf("WARNING: Creation of USB control transfer " 445 "process failed.\n"); 446 } else { 447 /* Get final attach going */ 448 USB_BUS_LOCK(bus); 449 if (usb_proc_msignal(&bus->explore_proc, 450 &bus->attach_msg[0], &bus->attach_msg[1])) { 451 /* ignore */ 452 } 453 USB_BUS_UNLOCK(bus); 454 455 /* Do initial explore */ 456 usb_needs_explore(bus, 1); 457 } 458 } 459 460 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 461 462 /*------------------------------------------------------------------------* 463 * usb_bus_mem_flush_all_cb 464 *------------------------------------------------------------------------*/ 465 #if USB_HAVE_BUSDMA 466 static void 467 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 468 struct usb_page *pg, usb_size_t size, usb_size_t align) 469 { 470 usb_pc_cpu_flush(pc); 471 } 472 #endif 473 474 /*------------------------------------------------------------------------* 475 * usb_bus_mem_flush_all - factored out code 476 *------------------------------------------------------------------------*/ 477 #if USB_HAVE_BUSDMA 478 void 479 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 480 { 481 if (cb) { 482 cb(bus, &usb_bus_mem_flush_all_cb); 483 } 484 } 485 #endif 486 487 /*------------------------------------------------------------------------* 488 * usb_bus_mem_alloc_all_cb 489 *------------------------------------------------------------------------*/ 490 #if USB_HAVE_BUSDMA 491 static void 492 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 493 struct usb_page *pg, usb_size_t size, usb_size_t align) 494 { 495 /* need to initialize the page cache */ 496 pc->tag_parent = bus->dma_parent_tag; 497 498 if (usb_pc_alloc_mem(pc, pg, size, align)) { 499 bus->alloc_failed = 1; 500 } 501 } 502 #endif 503 504 /*------------------------------------------------------------------------* 505 * usb_bus_mem_alloc_all - factored out code 506 * 507 * Returns: 508 * 0: Success 509 * Else: Failure 510 *------------------------------------------------------------------------*/ 511 uint8_t 512 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 513 usb_bus_mem_cb_t *cb) 514 { 515 bus->alloc_failed = 0; 516 517 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 518 NULL, MTX_DEF | MTX_RECURSE); 519 520 usb_callout_init_mtx(&bus->power_wdog, 521 &bus->bus_mtx, 0); 522 523 TAILQ_INIT(&bus->intr_q.head); 524 525 #if USB_HAVE_BUSDMA 526 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 527 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 528 #endif 529 if ((bus->devices_max > USB_MAX_DEVICES) || 530 (bus->devices_max < USB_MIN_DEVICES) || 531 (bus->devices == NULL)) { 532 DPRINTFN(0, "Devices field has not been " 533 "initialised properly\n"); 534 bus->alloc_failed = 1; /* failure */ 535 } 536 #if USB_HAVE_BUSDMA 537 if (cb) { 538 cb(bus, &usb_bus_mem_alloc_all_cb); 539 } 540 #endif 541 if (bus->alloc_failed) { 542 usb_bus_mem_free_all(bus, cb); 543 } 544 return (bus->alloc_failed); 545 } 546 547 /*------------------------------------------------------------------------* 548 * usb_bus_mem_free_all_cb 549 *------------------------------------------------------------------------*/ 550 #if USB_HAVE_BUSDMA 551 static void 552 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 553 struct usb_page *pg, usb_size_t size, usb_size_t align) 554 { 555 usb_pc_free_mem(pc); 556 } 557 #endif 558 559 /*------------------------------------------------------------------------* 560 * usb_bus_mem_free_all - factored out code 561 *------------------------------------------------------------------------*/ 562 void 563 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 564 { 565 #if USB_HAVE_BUSDMA 566 if (cb) { 567 cb(bus, &usb_bus_mem_free_all_cb); 568 } 569 usb_dma_tag_unsetup(bus->dma_parent_tag); 570 #endif 571 572 mtx_destroy(&bus->bus_mtx); 573 } 574