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