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