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