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