1 /* $OpenBSD: uhub.c,v 1.24 2004/07/08 22:18:44 deraadt Exp $ */ 2 /* $NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #if defined(__NetBSD__) || defined(__OpenBSD__) 51 #include <sys/device.h> 52 #include <sys/proc.h> 53 #elif defined(__FreeBSD__) 54 #include <sys/module.h> 55 #include <sys/bus.h> 56 #include "bus_if.h" 57 #endif 58 59 #include <machine/bus.h> 60 61 #include <dev/usb/usb.h> 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usbdi_util.h> 64 #include <dev/usb/usbdivar.h> 65 66 #define UHUB_INTR_INTERVAL 255 /* ms */ 67 68 #ifdef UHUB_DEBUG 69 #define DPRINTF(x) do { if (uhubdebug) logprintf x; } while (0) 70 #define DPRINTFN(n,x) do { if (uhubdebug>(n)) logprintf x; } while (0) 71 int uhubdebug = 0; 72 #else 73 #define DPRINTF(x) 74 #define DPRINTFN(n,x) 75 #endif 76 77 struct uhub_softc { 78 USBBASEDEVICE sc_dev; /* base device */ 79 usbd_device_handle sc_hub; /* USB device */ 80 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 81 u_int8_t sc_status[1]; /* XXX more ports */ 82 u_char sc_running; 83 }; 84 85 Static usbd_status uhub_explore(usbd_device_handle hub); 86 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status); 87 88 #if defined(__FreeBSD__) 89 Static bus_child_detached_t uhub_child_detached; 90 #endif 91 92 93 /* 94 * We need two attachment points: 95 * hub to usb and hub to hub 96 * Every other driver only connects to hubs 97 */ 98 99 #if defined(__NetBSD__) || defined(__OpenBSD__) 100 USB_DECLARE_DRIVER(uhub); 101 102 #if defined(__NetBSD__) 103 /* Create the driver instance for the hub connected to hub case */ 104 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc), 105 uhub_match, uhub_attach, uhub_detach, uhub_activate); 106 #else 107 struct cfattach uhub_uhub_ca = { 108 sizeof(struct uhub_softc), uhub_match, uhub_attach, 109 uhub_detach, uhub_activate 110 }; 111 #endif 112 #elif defined(__FreeBSD__) 113 USB_DECLARE_DRIVER_INIT(uhub, 114 DEVMETHOD(bus_child_detached, uhub_child_detached)); 115 116 /* Create the driver instance for the hub connected to usb case. */ 117 devclass_t uhubroot_devclass; 118 119 Static device_method_t uhubroot_methods[] = { 120 DEVMETHOD(device_probe, uhub_match), 121 DEVMETHOD(device_attach, uhub_attach), 122 123 /* detach is not allowed for a root hub */ 124 {0,0} 125 }; 126 127 Static driver_t uhubroot_driver = { 128 "uhub", 129 uhubroot_methods, 130 sizeof(struct uhub_softc) 131 }; 132 #endif 133 134 USB_MATCH(uhub) 135 { 136 USB_MATCH_START(uhub, uaa); 137 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 138 139 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 140 /* 141 * The subclass for hubs seems to be 0 for some and 1 for others, 142 * so we just ignore the subclass. 143 */ 144 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 145 return (UMATCH_DEVCLASS_DEVSUBCLASS); 146 return (UMATCH_NONE); 147 } 148 149 USB_ATTACH(uhub) 150 { 151 USB_ATTACH_START(uhub, sc, uaa); 152 usbd_device_handle dev = uaa->device; 153 char devinfo[1024]; 154 usbd_status err; 155 struct usbd_hub *hub; 156 usb_device_request_t req; 157 usb_hub_descriptor_t hubdesc; 158 int p, port, nports, nremov, pwrdly; 159 usbd_interface_handle iface; 160 usb_endpoint_descriptor_t *ed; 161 162 DPRINTFN(1,("uhub_attach\n")); 163 sc->sc_hub = dev; 164 usbd_devinfo(dev, 1, devinfo, sizeof devinfo); 165 USB_ATTACH_SETUP; 166 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 167 168 err = usbd_set_config_index(dev, 0, 1); 169 if (err) { 170 DPRINTF(("%s: configuration failed, error=%s\n", 171 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 172 USB_ATTACH_ERROR_RETURN; 173 } 174 175 if (dev->depth > USB_HUB_MAX_DEPTH) { 176 printf("%s: hub depth (%d) exceeded, hub ignored\n", 177 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 178 USB_ATTACH_ERROR_RETURN; 179 } 180 181 /* Get hub descriptor. */ 182 req.bmRequestType = UT_READ_CLASS_DEVICE; 183 req.bRequest = UR_GET_DESCRIPTOR; 184 USETW(req.wValue, 0); 185 USETW(req.wIndex, 0); 186 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 187 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 188 err = usbd_do_request(dev, &req, &hubdesc); 189 nports = hubdesc.bNbrPorts; 190 if (!err && nports > 7) { 191 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 192 err = usbd_do_request(dev, &req, &hubdesc); 193 } 194 if (err) { 195 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 196 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 197 USB_ATTACH_ERROR_RETURN; 198 } 199 200 for (nremov = 0, port = 1; port <= nports; port++) 201 if (!UHD_NOT_REMOV(&hubdesc, port)) 202 nremov++; 203 printf("%s: %d port%s with %d removable, %s powered\n", 204 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 205 nremov, dev->self_powered ? "self" : "bus"); 206 207 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 208 M_USBDEV, M_NOWAIT); 209 if (hub == NULL) 210 USB_ATTACH_ERROR_RETURN; 211 dev->hub = hub; 212 dev->hub->hubsoftc = sc; 213 hub->explore = uhub_explore; 214 hub->hubdesc = hubdesc; 215 216 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 217 "parent->selfpowered=%d\n", 218 dev->self_powered, dev->powersrc->parent, 219 dev->powersrc->parent ? 220 dev->powersrc->parent->self_powered : 0)); 221 222 if (!dev->self_powered && dev->powersrc->parent != NULL && 223 !dev->powersrc->parent->self_powered) { 224 printf("%s: bus powered hub connected to bus powered hub, " 225 "ignored\n", USBDEVNAME(sc->sc_dev)); 226 goto bad; 227 } 228 229 /* Set up interrupt pipe. */ 230 err = usbd_device2interface_handle(dev, 0, &iface); 231 if (err) { 232 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev)); 233 goto bad; 234 } 235 ed = usbd_interface2endpoint_descriptor(iface, 0); 236 if (ed == NULL) { 237 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 238 goto bad; 239 } 240 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 241 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 242 goto bad; 243 } 244 245 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 246 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 247 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL); 248 if (err) { 249 printf("%s: cannot open interrupt pipe\n", 250 USBDEVNAME(sc->sc_dev)); 251 goto bad; 252 } 253 254 /* Wait with power off for a while. */ 255 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 256 257 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev)); 258 259 /* 260 * To have the best chance of success we do things in the exact same 261 * order as Windoze98. This should not be necessary, but some 262 * devices do not follow the USB specs to the letter. 263 * 264 * These are the events on the bus when a hub is attached: 265 * Get device and config descriptors (see attach code) 266 * Get hub descriptor (see above) 267 * For all ports 268 * turn on power 269 * wait for power to become stable 270 * (all below happens in explore code) 271 * For all ports 272 * clear C_PORT_CONNECTION 273 * For all ports 274 * get port status 275 * if device connected 276 * wait 100 ms 277 * turn on reset 278 * wait 279 * clear C_PORT_RESET 280 * get port status 281 * proceed with device attachment 282 */ 283 284 /* Set up data structures */ 285 for (p = 0; p < nports; p++) { 286 struct usbd_port *up = &hub->ports[p]; 287 up->device = 0; 288 up->parent = dev; 289 up->portno = p+1; 290 if (dev->self_powered) 291 /* Self powered hub, give ports maximum current. */ 292 up->power = USB_MAX_POWER; 293 else 294 up->power = USB_MIN_POWER; 295 up->restartcnt = 0; 296 } 297 298 /* XXX should check for none, individual, or ganged power? */ 299 300 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 301 + USB_EXTRA_POWER_UP_TIME; 302 for (port = 1; port <= nports; port++) { 303 /* Turn the power on. */ 304 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 305 if (err) 306 printf("%s: port %d power on failed, %s\n", 307 USBDEVNAME(sc->sc_dev), port, 308 usbd_errstr(err)); 309 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 310 /* Wait for stable power. */ 311 usbd_delay_ms(dev, pwrdly); 312 } 313 314 /* The usual exploration will finish the setup. */ 315 316 sc->sc_running = 1; 317 318 USB_ATTACH_SUCCESS_RETURN; 319 320 bad: 321 free(hub, M_USBDEV); 322 dev->hub = 0; 323 USB_ATTACH_ERROR_RETURN; 324 } 325 326 usbd_status 327 uhub_explore(usbd_device_handle dev) 328 { 329 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 330 struct uhub_softc *sc = dev->hub->hubsoftc; 331 struct usbd_port *up; 332 usbd_status err; 333 int speed; 334 int port; 335 int change, status; 336 337 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 338 339 if (!sc->sc_running) 340 return (USBD_NOT_STARTED); 341 342 /* Ignore hubs that are too deep. */ 343 if (dev->depth > USB_HUB_MAX_DEPTH) 344 return (USBD_TOO_DEEP); 345 346 for(port = 1; port <= hd->bNbrPorts; port++) { 347 up = &dev->hub->ports[port-1]; 348 err = usbd_get_port_status(dev, port, &up->status); 349 if (err) { 350 DPRINTF(("uhub_explore: get port status failed, " 351 "error=%s\n", usbd_errstr(err))); 352 continue; 353 } 354 status = UGETW(up->status.wPortStatus); 355 change = UGETW(up->status.wPortChange); 356 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n", 357 USBDEVNAME(sc->sc_dev), port, status, change)); 358 if (change & UPS_C_PORT_ENABLED) { 359 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 360 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 361 if (status & UPS_PORT_ENABLED) { 362 printf("%s: illegal enable change, port %d\n", 363 USBDEVNAME(sc->sc_dev), port); 364 } else { 365 /* Port error condition. */ 366 if (up->restartcnt) /* no message first time */ 367 printf("%s: port error, restarting " 368 "port %d\n", 369 USBDEVNAME(sc->sc_dev), port); 370 371 if (up->restartcnt++ < USBD_RESTART_MAX) 372 goto disco; 373 else 374 printf("%s: port error, giving up " 375 "port %d\n", 376 USBDEVNAME(sc->sc_dev), port); 377 } 378 } 379 if (!(change & UPS_C_CONNECT_STATUS)) { 380 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 381 "STATUS\n", port)); 382 /* No status change, just do recursive explore. */ 383 if (up->device != NULL && up->device->hub != NULL) 384 up->device->hub->explore(up->device); 385 #if 0 && defined(DIAGNOSTIC) 386 if (up->device == NULL && 387 (status & UPS_CURRENT_CONNECT_STATUS)) 388 printf("%s: connected, no device\n", 389 USBDEVNAME(sc->sc_dev)); 390 #endif 391 continue; 392 } 393 394 /* We have a connect status change, handle it. */ 395 396 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 397 dev->address, port)); 398 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 399 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/ 400 /* 401 * If there is already a device on the port the change status 402 * must mean that is has disconnected. Looking at the 403 * current connect status is not enough to figure this out 404 * since a new unit may have been connected before we handle 405 * the disconnect. 406 */ 407 disco: 408 if (up->device != NULL) { 409 /* Disconnected */ 410 DPRINTF(("uhub_explore: device addr=%d disappeared " 411 "on port %d\n", up->device->address, port)); 412 usb_disconnect_port(up, USBDEV(sc->sc_dev)); 413 usbd_clear_port_feature(dev, port, 414 UHF_C_PORT_CONNECTION); 415 } 416 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 417 /* Nothing connected, just ignore it. */ 418 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 419 "_STATUS\n", port)); 420 continue; 421 } 422 423 /* Connected */ 424 425 if (!(status & UPS_PORT_POWER)) 426 printf("%s: strange, connected port %d has no power\n", 427 USBDEVNAME(sc->sc_dev), port); 428 429 /* Wait for maximum device power up time. */ 430 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 431 432 /* Reset port, which implies enabling it. */ 433 if (usbd_reset_port(dev, port, &up->status)) { 434 printf("%s: port %d reset failed\n", 435 USBDEVNAME(sc->sc_dev), port); 436 continue; 437 } 438 /* Get port status again, it might have changed during reset */ 439 err = usbd_get_port_status(dev, port, &up->status); 440 if (err) { 441 DPRINTF(("uhub_explore: get port status failed, " 442 "error=%s\n", usbd_errstr(err))); 443 continue; 444 } 445 status = UGETW(up->status.wPortStatus); 446 change = UGETW(up->status.wPortChange); 447 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 448 /* Nothing connected, just ignore it. */ 449 #ifdef UHUB_DEBUG 450 printf("%s: port %d, device disappeared after reset\n", 451 USBDEVNAME(sc->sc_dev), port); 452 #endif 453 continue; 454 } 455 456 /* Figure out device speed */ 457 if (status & UPS_HIGH_SPEED) 458 speed = USB_SPEED_HIGH; 459 else if (status & UPS_LOW_SPEED) 460 speed = USB_SPEED_LOW; 461 else 462 speed = USB_SPEED_FULL; 463 /* Get device info and set its address. */ 464 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 465 dev->depth + 1, speed, port, up); 466 /* XXX retry a few times? */ 467 if (err) { 468 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 469 "error=%s\n", usbd_errstr(err))); 470 /* Avoid addressing problems by disabling. */ 471 /* usbd_reset_port(dev, port, &up->status); */ 472 473 /* 474 * The unit refused to accept a new address, or had 475 * some other serious problem. Since we cannot leave 476 * at 0 we have to disable the port instead. 477 */ 478 printf("%s: device problem, disabling port %d\n", 479 USBDEVNAME(sc->sc_dev), port); 480 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 481 } else { 482 /* The port set up succeeded, reset error count. */ 483 up->restartcnt = 0; 484 485 if (up->device->hub) 486 up->device->hub->explore(up->device); 487 } 488 } 489 return (USBD_NORMAL_COMPLETION); 490 } 491 492 #if defined(__NetBSD__) || defined(__OpenBSD__) 493 int 494 uhub_activate(device_ptr_t self, enum devact act) 495 { 496 struct uhub_softc *sc = (struct uhub_softc *)self; 497 struct usbd_hub *hub = sc->sc_hub->hub; 498 usbd_device_handle dev; 499 int nports, port, i; 500 501 switch (act) { 502 case DVACT_ACTIVATE: 503 return (EOPNOTSUPP); 504 505 case DVACT_DEACTIVATE: 506 if (hub == NULL) /* malfunctioning hub */ 507 break; 508 nports = hub->hubdesc.bNbrPorts; 509 for(port = 0; port < nports; port++) { 510 dev = hub->ports[port].device; 511 if (dev != NULL && dev->subdevs != NULL) { 512 for (i = 0; dev->subdevs[i] != NULL; i++) 513 config_deactivate(dev->subdevs[i]); 514 } 515 } 516 break; 517 } 518 return (0); 519 } 520 #endif 521 522 /* 523 * Called from process context when the hub is gone. 524 * Detach all devices on active ports. 525 */ 526 USB_DETACH(uhub) 527 { 528 USB_DETACH_START(uhub, sc); 529 struct usbd_hub *hub = sc->sc_hub->hub; 530 struct usbd_port *rup; 531 int port, nports; 532 533 #if defined(__NetBSD__) || defined(__OpenBSD__) 534 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 535 #elif defined(__FreeBSD__) 536 DPRINTF(("uhub_detach: sc=%port\n", sc)); 537 #endif 538 539 if (hub == NULL) /* Must be partially working */ 540 return (0); 541 542 usbd_abort_pipe(sc->sc_ipipe); 543 usbd_close_pipe(sc->sc_ipipe); 544 545 nports = hub->hubdesc.bNbrPorts; 546 for(port = 0; port < nports; port++) { 547 rup = &hub->ports[port]; 548 if (rup->device) 549 usb_disconnect_port(rup, self); 550 } 551 552 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 553 USBDEV(sc->sc_dev)); 554 555 free(hub, M_USBDEV); 556 sc->sc_hub->hub = NULL; 557 558 return (0); 559 } 560 561 #if defined(__FreeBSD__) 562 /* Called when a device has been detached from it */ 563 Static void 564 uhub_child_detached(device_t self, device_t child) 565 { 566 struct uhub_softc *sc = device_get_softc(self); 567 usbd_device_handle devhub = sc->sc_hub; 568 usbd_device_handle dev; 569 int nports; 570 int port; 571 int i; 572 573 if (!devhub->hub) 574 /* should never happen; children are only created after init */ 575 panic("hub not fully initialised, but child deleted?"); 576 577 nports = devhub->hub->hubdesc.bNbrPorts; 578 for (port = 0; port < nports; port++) { 579 dev = devhub->hub->ports[port].device; 580 if (dev && dev->subdevs) { 581 for (i = 0; dev->subdevs[i]; i++) { 582 if (dev->subdevs[i] == child) { 583 dev->subdevs[i] = NULL; 584 return; 585 } 586 } 587 } 588 } 589 } 590 #endif 591 592 593 /* 594 * Hub interrupt. 595 * This an indication that some port has changed status. 596 * Notify the bus event handler thread that we need 597 * to be explored again. 598 */ 599 void 600 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 601 { 602 struct uhub_softc *sc = addr; 603 604 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 605 if (status == USBD_STALLED) 606 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 607 else if (status == USBD_NORMAL_COMPLETION) 608 usb_needs_explore(sc->sc_hub); 609 } 610 611 #if defined(__FreeBSD__) 612 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 613 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 614 #endif 615