1 /* $OpenBSD: uhub.c,v 1.79 2014/11/20 10:24:09 mpi 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 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/device.h> 40 41 #include <machine/bus.h> 42 43 #include <dev/usb/usb.h> 44 #include <dev/usb/usbdi.h> 45 #include <dev/usb/usbdi_util.h> 46 #include <dev/usb/usbdivar.h> 47 48 #define UHUB_INTR_INTERVAL 255 /* ms */ 49 50 #ifdef UHUB_DEBUG 51 #define DPRINTF(x...) do { printf(x); } while (0) 52 #else 53 #define DPRINTF(x...) 54 #endif 55 56 struct uhub_softc { 57 struct device sc_dev; /* base device */ 58 struct usbd_device *sc_hub; /* USB device */ 59 struct usbd_pipe *sc_ipipe; /* interrupt pipe */ 60 61 uint32_t sc_status; /* status from last interrupt */ 62 uint8_t *sc_statusbuf; /* per port status buffer */ 63 size_t sc_statuslen; /* status bufferlen */ 64 65 u_char sc_running; 66 }; 67 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol) 68 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 69 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 70 71 int uhub_explore(struct usbd_device *hub); 72 void uhub_intr(struct usbd_xfer *, void *, usbd_status); 73 74 /* 75 * We need two attachment points: 76 * hub to usb and hub to hub 77 * Every other driver only connects to hubs 78 */ 79 80 int uhub_match(struct device *, void *, void *); 81 void uhub_attach(struct device *, struct device *, void *); 82 int uhub_detach(struct device *, int); 83 84 struct cfdriver uhub_cd = { 85 NULL, "uhub", DV_DULL 86 }; 87 88 const struct cfattach uhub_ca = { 89 sizeof(struct uhub_softc), uhub_match, uhub_attach, uhub_detach 90 }; 91 92 const struct cfattach uhub_uhub_ca = { 93 sizeof(struct uhub_softc), uhub_match, uhub_attach, uhub_detach 94 }; 95 96 int 97 uhub_match(struct device *parent, void *match, void *aux) 98 { 99 struct usb_attach_arg *uaa = aux; 100 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 101 102 /* 103 * The subclass for hubs seems to be 0 for some and 1 for others, 104 * so we just ignore the subclass. 105 */ 106 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 107 return (UMATCH_DEVCLASS_DEVSUBCLASS); 108 return (UMATCH_NONE); 109 } 110 111 void 112 uhub_attach(struct device *parent, struct device *self, void *aux) 113 { 114 struct uhub_softc *sc = (struct uhub_softc *)self; 115 struct usb_attach_arg *uaa = aux; 116 struct usbd_device *dev = uaa->device; 117 struct usbd_hub *hub = NULL; 118 union { 119 usb_hub_descriptor_t hs; 120 usb_hub_ss_descriptor_t ss; 121 } hd; 122 int p, port, nports, powerdelay; 123 struct usbd_interface *iface; 124 usb_endpoint_descriptor_t *ed; 125 struct usbd_tt *tts = NULL; 126 uint8_t ttthink = 0; 127 usbd_status err; 128 #ifdef UHUB_DEBUG 129 int nremov; 130 #endif 131 132 sc->sc_hub = dev; 133 134 err = usbd_set_config_index(dev, 0, 1); 135 if (err) { 136 DPRINTF("%s: configuration failed, error=%s\n", 137 sc->sc_dev.dv_xname, usbd_errstr(err)); 138 return; 139 } 140 141 if (dev->depth > USB_HUB_MAX_DEPTH) { 142 printf("%s: hub depth (%d) exceeded, hub ignored\n", 143 sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH); 144 return; 145 } 146 147 /* 148 * Super-Speed hubs need to know their depth to be able to 149 * parse the bits of the route-string that correspond to 150 * their downstream port number. 151 * 152 * This does no apply to root hubs. 153 */ 154 if (dev->depth != 0 && dev->speed == USB_SPEED_SUPER) { 155 if (usbd_set_hub_depth(dev, dev->depth - 1)) { 156 printf("%s: unable to set HUB depth\n", 157 sc->sc_dev.dv_xname); 158 return; 159 } 160 } 161 162 /* Get hub descriptor. */ 163 if (dev->speed == USB_SPEED_SUPER) { 164 err = usbd_get_hub_ss_descriptor(dev, &hd.ss, 1); 165 nports = hd.ss.bNbrPorts; 166 powerdelay = (hd.ss.bPwrOn2PwrGood * UHD_PWRON_FACTOR); 167 if (!err && nports > 7) 168 usbd_get_hub_ss_descriptor(dev, &hd.ss, nports); 169 } else { 170 err = usbd_get_hub_descriptor(dev, &hd.hs, 1); 171 nports = hd.hs.bNbrPorts; 172 powerdelay = (hd.hs.bPwrOn2PwrGood * UHD_PWRON_FACTOR); 173 if (!err && nports > 7) 174 usbd_get_hub_descriptor(dev, &hd.hs, nports); 175 } 176 177 if (err) { 178 DPRINTF("%s: getting hub descriptor failed, error=%s\n", 179 sc->sc_dev.dv_xname, usbd_errstr(err)); 180 return; 181 } 182 183 #ifdef UHUB_DEBUG 184 for (nremov = 0, port = 1; port <= nports; port++) { 185 if (dev->speed == USB_SPEED_SUPER) { 186 if (!UHD_NOT_REMOV(&hd.ss, port)) 187 nremov++; 188 } else { 189 if (!UHD_NOT_REMOV(&hd.hs, port)) 190 nremov++; 191 } 192 } 193 194 printf("%s: %d port%s with %d removable, %s powered", 195 sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "", 196 nremov, dev->self_powered ? "self" : "bus"); 197 198 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) { 199 printf(", %s transaction translator%s", 200 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple", 201 UHUB_IS_SINGLE_TT(sc) ? "" : "s"); 202 } 203 204 printf("\n"); 205 #endif 206 207 if (nports == 0) { 208 printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname); 209 goto bad; 210 } 211 212 hub = malloc(sizeof(*hub), M_USBDEV, M_NOWAIT); 213 if (hub == NULL) 214 return; 215 hub->ports = malloc(sizeof(struct usbd_port) * nports, 216 M_USBDEV, M_NOWAIT); 217 if (hub->ports == NULL) { 218 free(hub, M_USBDEV, 0); 219 return; 220 } 221 dev->hub = hub; 222 dev->hub->hubsoftc = sc; 223 hub->explore = uhub_explore; 224 hub->nports = nports; 225 hub->powerdelay = powerdelay; 226 hub->ttthink = ttthink; 227 228 if (!dev->self_powered && dev->powersrc->parent != NULL && 229 !dev->powersrc->parent->self_powered) { 230 printf("%s: bus powered hub connected to bus powered hub, " 231 "ignored\n", sc->sc_dev.dv_xname); 232 goto bad; 233 } 234 235 /* Set up interrupt pipe. */ 236 err = usbd_device2interface_handle(dev, 0, &iface); 237 if (err) { 238 printf("%s: no interface handle\n", sc->sc_dev.dv_xname); 239 goto bad; 240 } 241 ed = usbd_interface2endpoint_descriptor(iface, 0); 242 if (ed == NULL) { 243 printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname); 244 goto bad; 245 } 246 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 247 printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname); 248 goto bad; 249 } 250 251 sc->sc_statuslen = (nports + 1 + 7) / 8; 252 sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT); 253 if (!sc->sc_statusbuf) 254 goto bad; 255 256 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 257 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf, 258 sc->sc_statuslen, uhub_intr, UHUB_INTR_INTERVAL); 259 if (err) { 260 printf("%s: cannot open interrupt pipe\n", 261 sc->sc_dev.dv_xname); 262 goto bad; 263 } 264 265 /* Wait with power off for a while. */ 266 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 267 268 /* 269 * To have the best chance of success we do things in the exact same 270 * order as Windoze98. This should not be necessary, but some 271 * devices do not follow the USB specs to the letter. 272 * 273 * These are the events on the bus when a hub is attached: 274 * Get device and config descriptors (see attach code) 275 * Get hub descriptor (see above) 276 * For all ports 277 * turn on power 278 * wait for power to become stable 279 * (all below happens in explore code) 280 * For all ports 281 * clear C_PORT_CONNECTION 282 * For all ports 283 * get port status 284 * if device connected 285 * wait 100 ms 286 * turn on reset 287 * wait 288 * clear C_PORT_RESET 289 * get port status 290 * proceed with device attachment 291 */ 292 293 if (UHUB_IS_HIGH_SPEED(sc)) { 294 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 295 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT); 296 if (!tts) 297 goto bad; 298 } 299 /* Set up data structures */ 300 for (p = 0; p < nports; p++) { 301 struct usbd_port *up = &hub->ports[p]; 302 up->device = NULL; 303 up->parent = dev; 304 up->portno = p+1; 305 if (dev->self_powered) 306 /* Self powered hub, give ports maximum current. */ 307 up->power = USB_MAX_POWER; 308 else 309 up->power = USB_MIN_POWER; 310 up->restartcnt = 0; 311 up->reattach = 0; 312 if (UHUB_IS_HIGH_SPEED(sc)) { 313 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 314 up->tt->hub = hub; 315 } else { 316 up->tt = NULL; 317 } 318 } 319 320 for (port = 1; port <= nports; port++) { 321 /* Turn the power on. */ 322 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 323 if (err) 324 printf("%s: port %d power on failed, %s\n", 325 sc->sc_dev.dv_xname, port, 326 usbd_errstr(err)); 327 /* Make sure we check the port status at least once. */ 328 sc->sc_status |= (1 << port); 329 } 330 331 /* Wait for stable power. */ 332 if (dev->powersrc->parent != NULL) 333 usbd_delay_ms(dev, powerdelay + USB_EXTRA_POWER_UP_TIME); 334 335 /* The usual exploration will finish the setup. */ 336 337 sc->sc_running = 1; 338 339 return; 340 341 bad: 342 if (sc->sc_statusbuf) 343 free(sc->sc_statusbuf, M_USBDEV, 0); 344 if (hub) { 345 if (hub->ports) 346 free(hub->ports, M_USBDEV, 0); 347 free(hub, M_USBDEV, 0); 348 } 349 dev->hub = NULL; 350 } 351 352 int 353 uhub_explore(struct usbd_device *dev) 354 { 355 struct uhub_softc *sc = dev->hub->hubsoftc; 356 struct usbd_port *up; 357 usbd_status err; 358 int speed; 359 int port; 360 int change, status, reconnect; 361 362 if (usbd_is_dying(dev)) 363 return (EIO); 364 365 if (!sc->sc_running) 366 return (ENXIO); 367 368 /* Ignore hubs that are too deep. */ 369 if (dev->depth > USB_HUB_MAX_DEPTH) 370 return (EOPNOTSUPP); 371 372 for (port = 1; port <= dev->hub->nports; port++) { 373 up = &dev->hub->ports[port-1]; 374 375 reconnect = up->reattach; 376 up->reattach = 0; 377 change = 0; 378 status = 0; 379 380 if ((sc->sc_status & (1 << port)) || reconnect) { 381 sc->sc_status &= ~(1 << port); 382 383 if (usbd_get_port_status(dev, port, &up->status)) 384 continue; 385 386 status = UGETW(up->status.wPortStatus); 387 change = UGETW(up->status.wPortChange); 388 DPRINTF("%s: port %d status=0x%04x change=0x%04x\n", 389 sc->sc_dev.dv_xname, port, status, change); 390 } 391 392 if (change & UPS_C_PORT_ENABLED) { 393 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 394 if (change & UPS_C_CONNECT_STATUS) { 395 /* Ignore the port error if the device 396 vanished. */ 397 } else if (status & UPS_PORT_ENABLED) { 398 printf("%s: illegal enable change, port %d\n", 399 sc->sc_dev.dv_xname, port); 400 } else { 401 /* Port error condition. */ 402 if (up->restartcnt) /* no message first time */ 403 printf("%s: port error, restarting " 404 "port %d\n", 405 sc->sc_dev.dv_xname, port); 406 407 if (up->restartcnt++ < USBD_RESTART_MAX) 408 goto disco; 409 else 410 printf("%s: port error, giving up " 411 "port %d\n", 412 sc->sc_dev.dv_xname, port); 413 } 414 } 415 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) { 416 /* No status change, just do recursive explore. */ 417 if (up->device != NULL && up->device->hub != NULL) 418 up->device->hub->explore(up->device); 419 continue; 420 } 421 422 /* We have a connect status change, handle it. */ 423 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 424 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 usbd_detach(up->device, &sc->sc_dev); 436 up->device = NULL; 437 usbd_clear_port_feature(dev, port, 438 UHF_C_PORT_CONNECTION); 439 } 440 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 441 /* Nothing connected, just ignore it. */ 442 continue; 443 } 444 445 /* Connected */ 446 if (!(status & (UPS_PORT_POWER|UPS_PORT_POWER_SS))) { 447 printf("%s: connected port %d has no power\n", 448 sc->sc_dev.dv_xname, port); 449 continue; 450 } 451 452 /* Wait for maximum device power up time. */ 453 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 454 455 /* Reset port, which implies enabling it. */ 456 if (usbd_reset_port(dev, port)) { 457 printf("%s: port %d reset failed\n", 458 sc->sc_dev.dv_xname, port); 459 continue; 460 } 461 /* Get port status again, it might have changed during reset */ 462 err = usbd_get_port_status(dev, port, &up->status); 463 if (err) { 464 DPRINTF("%s: get port %d status failed, error=%s\n", 465 sc->sc_dev.dv_xname, port, usbd_errstr(err)); 466 continue; 467 } 468 status = UGETW(up->status.wPortStatus); 469 change = UGETW(up->status.wPortChange); 470 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 471 /* Nothing connected, just ignore it. */ 472 DPRINTF("%s: port %d, device disappeared after reset\n", 473 sc->sc_dev.dv_xname, port); 474 continue; 475 } 476 477 /* 478 * Figure out device speed. This is a bit tricky because 479 * UPS_PORT_POWER_SS and UPS_LOW_SPEED share the same bit. 480 */ 481 if ((status & UPS_PORT_POWER) == 0) 482 status &= ~UPS_PORT_POWER_SS; 483 484 if (status & UPS_SUPER_SPEED) 485 speed = USB_SPEED_SUPER; 486 else if (status & UPS_HIGH_SPEED) 487 speed = USB_SPEED_HIGH; 488 else if (status & UPS_LOW_SPEED) 489 speed = USB_SPEED_LOW; 490 else { 491 /* 492 * If there is no power bit set, it is certainly 493 * a Super Speed device, so use the speed of its 494 * parent hub. 495 */ 496 if (status & UPS_PORT_POWER) 497 speed = USB_SPEED_FULL; 498 else 499 speed = sc->sc_hub->speed; 500 } 501 502 /* 503 * Reduce the speed, otherwise we won't setup the proper 504 * transfer methods. 505 */ 506 if (speed > sc->sc_hub->speed) 507 speed = sc->sc_hub->speed; 508 509 /* Get device info and set its address. */ 510 err = usbd_new_device(&sc->sc_dev, dev->bus, 511 dev->depth + 1, speed, port, up); 512 /* XXX retry a few times? */ 513 if (err) { 514 DPRINTF("%s: usbd_new_device failed, error=%s\n", 515 sc->sc_dev.dv_xname, usbd_errstr(err)); 516 /* Avoid addressing problems by disabling. */ 517 /* usbd_reset_port(dev, port, &up->status); */ 518 519 /* 520 * The unit refused to accept a new address, or had 521 * some other serious problem. Since we cannot leave 522 * at 0 we have to disable the port instead. 523 */ 524 printf("%s: device problem, disabling port %d\n", 525 sc->sc_dev.dv_xname, port); 526 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 527 } else { 528 /* The port set up succeeded, reset error count. */ 529 up->restartcnt = 0; 530 531 if (up->device->hub) 532 up->device->hub->explore(up->device); 533 } 534 } 535 536 return (0); 537 } 538 539 /* 540 * Called from process context when the hub is gone. 541 * Detach all devices on active ports. 542 */ 543 int 544 uhub_detach(struct device *self, int flags) 545 { 546 struct uhub_softc *sc = (struct uhub_softc *)self; 547 struct usbd_hub *hub = sc->sc_hub->hub; 548 struct usbd_port *rup; 549 int port; 550 551 if (hub == NULL) /* Must be partially working */ 552 return (0); 553 554 usbd_abort_pipe(sc->sc_ipipe); 555 usbd_close_pipe(sc->sc_ipipe); 556 557 for (port = 0; port < hub->nports; port++) { 558 rup = &hub->ports[port]; 559 if (rup->device != NULL) { 560 usbd_detach(rup->device, self); 561 rup->device = NULL; 562 } 563 } 564 565 if (hub->ports[0].tt) 566 free(hub->ports[0].tt, M_USBDEV, 0); 567 if (sc->sc_statusbuf) 568 free(sc->sc_statusbuf, M_USBDEV, 0); 569 if (hub->ports) 570 free(hub->ports, M_USBDEV, 0); 571 free(hub, M_USBDEV, 0); 572 sc->sc_hub->hub = NULL; 573 574 return (0); 575 } 576 577 /* 578 * This is an indication that some port has changed status. Remember 579 * the ports that need attention and notify the USB task thread that 580 * we need to be explored again. 581 */ 582 void 583 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) 584 { 585 struct uhub_softc *sc = addr; 586 uint32_t stats = 0; 587 int i; 588 589 if (usbd_is_dying(sc->sc_hub)) 590 return; 591 592 DPRINTF("%s: intr status=%d\n", sc->sc_dev.dv_xname, status); 593 594 if (status == USBD_STALLED) 595 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 596 else if (status == USBD_NORMAL_COMPLETION) { 597 for (i = 0; i < xfer->actlen; i++) 598 stats |= (uint32_t)(xfer->buffer[i]) << (i * 8); 599 sc->sc_status |= stats; 600 601 usb_needs_explore(sc->sc_hub, 0); 602 } 603 } 604