1 /* $OpenBSD: uhub.c,v 1.48 2007/10/11 18:33:15 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 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/device.h> 51 #include <sys/proc.h> 52 53 #include <machine/bus.h> 54 55 #include <dev/usb/usb.h> 56 #include <dev/usb/usbdi.h> 57 #include <dev/usb/usbdi_util.h> 58 #include <dev/usb/usbdivar.h> 59 60 #define UHUB_INTR_INTERVAL 255 /* ms */ 61 62 #ifdef UHUB_DEBUG 63 #define DPRINTF(x) do { if (uhubdebug) printf x; } while (0) 64 #define DPRINTFN(n,x) do { if (uhubdebug>(n)) printf x; } while (0) 65 int uhubdebug = 0; 66 #else 67 #define DPRINTF(x) 68 #define DPRINTFN(n,x) 69 #endif 70 71 struct uhub_softc { 72 struct device sc_dev; /* base device */ 73 usbd_device_handle sc_hub; /* USB device */ 74 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 75 u_int8_t sc_status[1]; /* XXX more ports */ 76 u_char sc_running; 77 }; 78 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol) 79 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 80 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 81 82 usbd_status uhub_explore(usbd_device_handle hub); 83 void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status); 84 85 /* 86 * We need two attachment points: 87 * hub to usb and hub to hub 88 * Every other driver only connects to hubs 89 */ 90 91 int uhub_match(struct device *, void *, void *); 92 void uhub_attach(struct device *, struct device *, void *); 93 int uhub_detach(struct device *, int); 94 int uhub_activate(struct device *, enum devact); 95 96 struct cfdriver uhub_cd = { 97 NULL, "uhub", DV_DULL 98 }; 99 100 const struct cfattach uhub_ca = { 101 sizeof(struct uhub_softc), 102 uhub_match, 103 uhub_attach, 104 uhub_detach, 105 uhub_activate, 106 }; 107 108 struct cfattach uhub_uhub_ca = { 109 sizeof(struct uhub_softc), uhub_match, uhub_attach, 110 uhub_detach, uhub_activate 111 }; 112 113 int 114 uhub_match(struct device *parent, void *match, void *aux) 115 { 116 struct usb_attach_arg *uaa = aux; 117 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 118 119 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 120 /* 121 * The subclass for hubs seems to be 0 for some and 1 for others, 122 * so we just ignore the subclass. 123 */ 124 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 125 return (UMATCH_DEVCLASS_DEVSUBCLASS); 126 return (UMATCH_NONE); 127 } 128 129 void 130 uhub_attach(struct device *parent, struct device *self, void *aux) 131 { 132 struct uhub_softc *sc = (struct uhub_softc *)self; 133 struct usb_attach_arg *uaa = aux; 134 usbd_device_handle dev = uaa->device; 135 usbd_status err; 136 struct usbd_hub *hub = NULL; 137 usb_device_request_t req; 138 usb_hub_descriptor_t hubdesc; 139 int p, port, nports, nremov, pwrdly; 140 usbd_interface_handle iface; 141 usb_endpoint_descriptor_t *ed; 142 struct usbd_tt *tts = NULL; 143 144 DPRINTFN(1,("uhub_attach\n")); 145 sc->sc_hub = dev; 146 147 err = usbd_set_config_index(dev, 0, 1); 148 if (err) { 149 DPRINTF(("%s: configuration failed, error=%s\n", 150 sc->sc_dev.dv_xname, usbd_errstr(err))); 151 return; 152 } 153 154 if (dev->depth > USB_HUB_MAX_DEPTH) { 155 printf("%s: hub depth (%d) exceeded, hub ignored\n", 156 sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH); 157 return; 158 } 159 160 /* Get hub descriptor. */ 161 req.bmRequestType = UT_READ_CLASS_DEVICE; 162 req.bRequest = UR_GET_DESCRIPTOR; 163 USETW2(req.wValue, UDESC_HUB, 0); 164 USETW(req.wIndex, 0); 165 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 166 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 167 err = usbd_do_request(dev, &req, &hubdesc); 168 nports = hubdesc.bNbrPorts; 169 if (!err && nports > 7) { 170 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 171 err = usbd_do_request(dev, &req, &hubdesc); 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 for (nremov = 0, port = 1; port <= nports; port++) 180 if (!UHD_NOT_REMOV(&hubdesc, port)) 181 nremov++; 182 183 #ifdef UHUB_DEBUG 184 printf("%s: %d port%s with %d removable, %s powered", 185 sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "", 186 nremov, dev->self_powered ? "self" : "bus"); 187 188 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) { 189 printf(", %s transaction translator%s", 190 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple", 191 UHUB_IS_SINGLE_TT(sc) ? "" : "s"); 192 } 193 printf("\n"); 194 #endif 195 196 if (nports == 0) { 197 printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname); 198 goto bad; 199 } 200 201 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 202 M_USBDEV, M_NOWAIT); 203 if (hub == NULL) 204 return; 205 dev->hub = hub; 206 dev->hub->hubsoftc = sc; 207 hub->explore = uhub_explore; 208 hub->hubdesc = hubdesc; 209 210 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 211 "parent->selfpowered=%d\n", 212 dev->self_powered, dev->powersrc->parent, 213 dev->powersrc->parent ? 214 dev->powersrc->parent->self_powered : 0)); 215 216 if (!dev->self_powered && dev->powersrc->parent != NULL && 217 !dev->powersrc->parent->self_powered) { 218 printf("%s: bus powered hub connected to bus powered hub, " 219 "ignored\n", sc->sc_dev.dv_xname); 220 goto bad; 221 } 222 223 /* Set up interrupt pipe. */ 224 err = usbd_device2interface_handle(dev, 0, &iface); 225 if (err) { 226 printf("%s: no interface handle\n", sc->sc_dev.dv_xname); 227 goto bad; 228 } 229 ed = usbd_interface2endpoint_descriptor(iface, 0); 230 if (ed == NULL) { 231 printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname); 232 goto bad; 233 } 234 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 235 printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname); 236 goto bad; 237 } 238 239 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 240 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 241 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL); 242 if (err) { 243 printf("%s: cannot open interrupt pipe\n", 244 sc->sc_dev.dv_xname); 245 goto bad; 246 } 247 248 /* Wait with power off for a while. */ 249 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 250 251 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, &sc->sc_dev); 252 253 /* 254 * To have the best chance of success we do things in the exact same 255 * order as Windoze98. This should not be necessary, but some 256 * devices do not follow the USB specs to the letter. 257 * 258 * These are the events on the bus when a hub is attached: 259 * Get device and config descriptors (see attach code) 260 * Get hub descriptor (see above) 261 * For all ports 262 * turn on power 263 * wait for power to become stable 264 * (all below happens in explore code) 265 * For all ports 266 * clear C_PORT_CONNECTION 267 * For all ports 268 * get port status 269 * if device connected 270 * wait 100 ms 271 * turn on reset 272 * wait 273 * clear C_PORT_RESET 274 * get port status 275 * proceed with device attachment 276 */ 277 278 if (UHUB_IS_HIGH_SPEED(sc)) { 279 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) * 280 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT); 281 if (!tts) 282 goto bad; 283 } 284 /* Set up data structures */ 285 for (p = 0; p < nports; p++) { 286 struct usbd_port *up = &hub->ports[p]; 287 up->device = NULL; 288 up->parent = dev; 289 up->portno = p+1; 290 if (dev->self_powered) 291 /* Self powered hub, give ports maximum current. */ 292 up->power = USB_MAX_POWER; 293 else 294 up->power = USB_MIN_POWER; 295 up->restartcnt = 0; 296 up->reattach = 0; 297 if (UHUB_IS_HIGH_SPEED(sc)) { 298 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p]; 299 up->tt->hub = hub; 300 } else { 301 up->tt = NULL; 302 } 303 } 304 305 /* XXX should check for none, individual, or ganged power? */ 306 307 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR 308 + USB_EXTRA_POWER_UP_TIME; 309 for (port = 1; port <= nports; port++) { 310 /* Turn the power on. */ 311 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 312 if (err) 313 printf("%s: port %d power on failed, %s\n", 314 sc->sc_dev.dv_xname, port, 315 usbd_errstr(err)); 316 DPRINTF(("usb_init_port: turn on port %d power\n", port)); 317 } 318 319 /* Wait for stable power. Root hubs delay in their event thread. */ 320 if (dev->powersrc->parent != NULL) 321 usbd_delay_ms(dev, pwrdly); 322 323 /* The usual exploration will finish the setup. */ 324 325 sc->sc_running = 1; 326 327 return; 328 329 bad: 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, enum devact 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 free(hub, M_USBDEV); 567 sc->sc_hub->hub = NULL; 568 569 return (0); 570 } 571 572 /* 573 * Hub interrupt. 574 * This an indication that some port has changed status. 575 * Notify the bus event handler thread that we need 576 * to be explored again. 577 */ 578 void 579 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status) 580 { 581 struct uhub_softc *sc = addr; 582 583 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 584 if (status == USBD_STALLED) 585 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 586 else if (status == USBD_NORMAL_COMPLETION) 587 usb_needs_explore(sc->sc_hub); 588 } 589