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