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