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