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