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