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