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