1 /* $OpenBSD: uhub.c,v 1.7 2000/03/26 08:39:46 aaron Exp $ */ 2 /* $NetBSD: uhub.c,v 1.40 2000/02/29 21:37:01 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (augustss@carlstedt.se) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #if defined(__NetBSD__) || defined(__OpenBSD__) 50 #include <sys/device.h> 51 #include <sys/proc.h> 52 #elif defined(__FreeBSD__) 53 #include <sys/module.h> 54 #include <sys/bus.h> 55 #include "bus_if.h" 56 #endif 57 58 #include <machine/bus.h> 59 60 #include <dev/usb/usb.h> 61 #include <dev/usb/usbdi.h> 62 #include <dev/usb/usbdi_util.h> 63 #include <dev/usb/usbdivar.h> 64 65 #ifdef UHUB_DEBUG 66 #define DPRINTF(x) if (uhubdebug) logprintf x 67 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x 68 int uhubdebug; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 struct uhub_softc { 75 USBBASEDEVICE sc_dev; /* base device */ 76 usbd_device_handle sc_hub; /* USB device */ 77 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 78 u_int8_t sc_status[1]; /* XXX more ports */ 79 u_char sc_running; 80 }; 81 82 usbd_status uhub_init_port __P((struct usbd_port *)); 83 usbd_status uhub_explore __P((usbd_device_handle hub)); 84 void uhub_intr __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); 85 86 #if defined(__FreeBSD__) 87 static bus_child_detached_t uhub_child_detached; 88 #endif 89 90 /* 91 * We need two attachment points: 92 * hub to usb and hub to hub 93 * Every other driver only connects to hubs 94 */ 95 96 #if defined(__NetBSD__) || defined(__OpenBSD__) 97 USB_DECLARE_DRIVER(uhub); 98 99 /* Create the driver instance for the hub connected to hub case */ 100 struct cfattach uhub_uhub_ca = { 101 sizeof(struct uhub_softc), uhub_match, uhub_attach, 102 uhub_detach, uhub_activate 103 }; 104 #elif defined(__FreeBSD__) 105 USB_DECLARE_DRIVER_INIT(uhub. 106 DEVMETHOD(bus_child_detached, uhub_child_detached)); 107 108 /* Create the driver instance for the hub connected to usb case. */ 109 devclass_t uhubroot_devclass; 110 111 static device_method_t uhubroot_methods[] = { 112 DEVMETHOD(device_probe, uhub_match), 113 DEVMETHOD(device_attach, uhub_attach), 114 115 /* detach is not allowed for a root hub */ 116 {0,0} 117 }; 118 119 static driver_t uhubroot_driver = { 120 "uhub", 121 uhubroot_methods, 122 sizeof(struct uhub_softc) 123 }; 124 #endif 125 126 USB_MATCH(uhub) 127 { 128 USB_MATCH_START(uhub, uaa); 129 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 130 131 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 132 /* 133 * The subclass for hubs seems to be 0 for some and 1 for others, 134 * so we just ignore the subclass. 135 */ 136 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 137 return (UMATCH_DEVCLASS_DEVSUBCLASS); 138 return (UMATCH_NONE); 139 } 140 141 USB_ATTACH(uhub) 142 { 143 USB_ATTACH_START(uhub, sc, uaa); 144 usbd_device_handle dev = uaa->device; 145 char devinfo[1024]; 146 usbd_status err; 147 struct usbd_hub *hub; 148 usb_device_request_t req; 149 usb_hub_descriptor_t hubdesc; 150 int p, port, nports, nremov; 151 usbd_interface_handle iface; 152 usb_endpoint_descriptor_t *ed; 153 154 DPRINTFN(1,("uhub_attach\n")); 155 sc->sc_hub = dev; 156 usbd_devinfo(dev, 1, devinfo); 157 USB_ATTACH_SETUP; 158 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 159 160 err = usbd_set_config_index(dev, 0, 1); 161 if (err) { 162 DPRINTF(("%s: configuration failed, error=%s\n", 163 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 164 USB_ATTACH_ERROR_RETURN; 165 } 166 167 if (dev->depth > USB_HUB_MAX_DEPTH) { 168 printf("%s: hub depth (%d) exceeded, hub ignored\n", 169 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 170 USB_ATTACH_ERROR_RETURN; 171 } 172 173 /* Get hub descriptor. */ 174 req.bmRequestType = UT_READ_CLASS_DEVICE; 175 req.bRequest = UR_GET_DESCRIPTOR; 176 USETW(req.wValue, 0); 177 USETW(req.wIndex, 0); 178 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 179 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 180 err = usbd_do_request(dev, &req, &hubdesc); 181 nports = hubdesc.bNbrPorts; 182 if (!err && nports > 7) { 183 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 184 err = usbd_do_request(dev, &req, &hubdesc); 185 } 186 if (err) { 187 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 188 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 189 USB_ATTACH_ERROR_RETURN; 190 } 191 192 for (nremov = 0, port = 1; port <= nports; port++) 193 if (!UHD_NOT_REMOV(&hubdesc, port)) 194 nremov++; 195 printf("%s: %d port%s with %d removable, %s powered\n", 196 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 197 nremov, dev->self_powered ? "self" : "bus"); 198 199 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 200 M_USBDEV, M_NOWAIT); 201 if (hub == NULL) 202 USB_ATTACH_ERROR_RETURN; 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", USBDEVNAME(sc->sc_dev)); 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", USBDEVNAME(sc->sc_dev)); 225 goto bad; 226 } 227 ed = usbd_interface2endpoint_descriptor(iface, 0); 228 if (ed == NULL) { 229 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 230 goto bad; 231 } 232 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 233 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 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, USBD_DEFAULT_INTERVAL); 240 if (err) { 241 printf("%s: cannot open interrupt pipe\n", 242 USBDEVNAME(sc->sc_dev)); 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, USBDEV(sc->sc_dev)); 250 251 for (p = 0; p < nports; p++) { 252 struct usbd_port *up = &hub->ports[p]; 253 up->device = 0; 254 up->parent = dev; 255 up->portno = p+1; 256 err = uhub_init_port(up); 257 if (err) 258 printf("%s: init of port %d failed\n", 259 USBDEVNAME(sc->sc_dev), up->portno); 260 } 261 sc->sc_running = 1; 262 263 USB_ATTACH_SUCCESS_RETURN; 264 265 bad: 266 free(hub, M_USBDEV); 267 dev->hub = 0; 268 USB_ATTACH_ERROR_RETURN; 269 } 270 271 usbd_status 272 uhub_init_port(up) 273 struct usbd_port *up; 274 { 275 int port = up->portno; 276 usbd_device_handle dev = up->parent; 277 usbd_status err; 278 u_int16_t pstatus; 279 280 err = usbd_get_port_status(dev, port, &up->status); 281 if (err) 282 return (err); 283 pstatus = UGETW(up->status.wPortStatus); 284 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x " 285 "change=0x%04x\n", 286 port, pstatus, UGETW(up->status.wPortChange))); 287 if ((pstatus & UPS_PORT_POWER) == 0) { 288 /* Port lacks power, turn it on */ 289 290 /* First let the device go through a good power cycle, */ 291 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME); 292 293 /* then turn the power on. */ 294 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 295 if (err) 296 return (err); 297 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x " 298 "change=0x%04x\n", 299 port, UGETW(up->status.wPortStatus), 300 UGETW(up->status.wPortChange))); 301 /* Wait for stable power. */ 302 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood * 303 UHD_PWRON_FACTOR); 304 /* Get the port status again. */ 305 err = usbd_get_port_status(dev, port, &up->status); 306 if (err) 307 return (err); 308 DPRINTF(("usb_init_port: after power on status=0x%04x " 309 "change=0x%04x\n", 310 UGETW(up->status.wPortStatus), 311 UGETW(up->status.wPortChange))); 312 313 #if 0 314 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT); 315 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT); 316 usbd_get_port_status(dev, port, &up->status); 317 #endif 318 319 pstatus = UGETW(up->status.wPortStatus); 320 if ((pstatus & UPS_PORT_POWER) == 0) 321 printf("%s: port %d did not power up\n", 322 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port); 323 324 } 325 if (dev->self_powered) 326 /* Self powered hub, give ports maximum current. */ 327 up->power = USB_MAX_POWER; 328 else 329 up->power = USB_MIN_POWER; 330 return (USBD_NORMAL_COMPLETION); 331 } 332 333 usbd_status 334 uhub_explore(dev) 335 usbd_device_handle dev; 336 { 337 usb_hub_descriptor_t *hd = &dev->hub->hubdesc; 338 struct uhub_softc *sc = dev->hub->hubsoftc; 339 struct usbd_port *up; 340 usbd_status err; 341 int port; 342 int change, status; 343 344 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 345 346 if (!sc->sc_running) 347 return (USBD_NOT_STARTED); 348 349 /* Ignore hubs that are too deep. */ 350 if (dev->depth > USB_HUB_MAX_DEPTH) 351 return (USBD_TOO_DEEP); 352 353 for(port = 1; port <= hd->bNbrPorts; port++) { 354 up = &dev->hub->ports[port-1]; 355 err = usbd_get_port_status(dev, port, &up->status); 356 if (err) { 357 DPRINTF(("uhub_explore: get port status failed, " 358 "error=%s\n", usbd_errstr(err))); 359 continue; 360 } 361 status = UGETW(up->status.wPortStatus); 362 change = UGETW(up->status.wPortChange); 363 DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n", 364 port, status, change)); 365 if (change & UPS_C_PORT_ENABLED) { 366 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 367 if (status & UPS_PORT_ENABLED) { 368 printf("%s: illegal enable change, port %d\n", 369 USBDEVNAME(sc->sc_dev), port); 370 } else { 371 /* Port error condition. */ 372 if (up->restartcnt++ < USBD_RESTART_MAX) { 373 printf("%s: port error, restarting " 374 "port %d\n", 375 USBDEVNAME(sc->sc_dev), port); 376 goto disco; 377 } else { 378 printf("%s: port error, giving up " 379 "port %d\n", 380 USBDEVNAME(sc->sc_dev), port); 381 } 382 } 383 } 384 if (!(change & UPS_C_CONNECT_STATUS)) { 385 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 386 "STATUS\n", port)); 387 /* No status change, just do recursive explore. */ 388 if (up->device && up->device->hub) 389 up->device->hub->explore(up->device); 390 continue; 391 } 392 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 393 dev->address, port)); 394 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 395 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 396 /* 397 * If there is already a device on the port the change status 398 * must mean that is has disconnected. Looking at the 399 * current connect status is not enough to figure this out 400 * since a new unit may have been connected before we handle 401 * the disconnect. 402 */ 403 disco: 404 if (up->device != NULL) { 405 /* Disconnected */ 406 DPRINTF(("uhub_explore: device addr=%d disappeared " 407 "on port %d\n", up->device->address, port)); 408 usb_disconnect_port(up, USBDEV(sc->sc_dev)); 409 usbd_clear_port_feature(dev, port, 410 UHF_C_PORT_CONNECTION); 411 } 412 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 413 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 414 "_STATUS\n", port)); 415 continue; 416 } 417 418 /* Connected */ 419 up->restartcnt = 0; 420 421 /* Wait for maximum device power up time. */ 422 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 423 424 /* Reset port, which implies enabling it. */ 425 if (usbd_reset_port(dev, port, &up->status)) { 426 DPRINTF(("uhub_explore: port=%d reset failed\n", 427 port)); 428 continue; 429 } 430 431 /* Get device info and set its address. */ 432 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 433 dev->depth + 1, status & UPS_LOW_SPEED, 434 port, up); 435 /* XXX retry a few times? */ 436 if (err) { 437 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 438 "error=%s\n", usbd_errstr(err))); 439 /* Avoid addressing problems by disabling. */ 440 /* usbd_reset_port(dev, port, &up->status); */ 441 442 /* 443 * The unit refused to accept a new address, or had 444 * some other serious problem. Since we cannot leave 445 * at 0 we have to disable the port instead. 446 */ 447 printf("%s: device problem, disabling port %d\n", 448 USBDEVNAME(sc->sc_dev), port); 449 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 450 /* Make sure we don't try to restart it infinitely. */ 451 up->restartcnt = USBD_RESTART_MAX; 452 } else { 453 if (up->device->hub) 454 up->device->hub->explore(up->device); 455 } 456 } 457 return (USBD_NORMAL_COMPLETION); 458 } 459 460 #if defined(__NetBSD__) || defined(__OpenBSD__) 461 int 462 uhub_activate(self, act) 463 device_ptr_t self; 464 enum devact act; 465 { 466 struct uhub_softc *sc = (struct uhub_softc *)self; 467 struct usbd_hub *hub = sc->sc_hub->hub; 468 usbd_device_handle dev; 469 int nports, port, i; 470 471 switch (act) { 472 case DVACT_ACTIVATE: 473 return (EOPNOTSUPP); 474 break; 475 476 case DVACT_DEACTIVATE: 477 if (hub == NULL) /* malfunctioning hub */ 478 break; 479 nports = hub->hubdesc.bNbrPorts; 480 for(port = 0; port < nports; port++) { 481 dev = hub->ports[port].device; 482 if (dev != NULL) { 483 for (i = 0; dev->subdevs[i]; i++) 484 config_deactivate(dev->subdevs[i]); 485 } 486 } 487 break; 488 } 489 return (0); 490 } 491 #endif 492 493 /* 494 * Called from process context when the hub is gone. 495 * Detach all devices on active ports. 496 */ 497 USB_DETACH(uhub) 498 { 499 USB_DETACH_START(uhub, sc); 500 struct usbd_hub *hub = sc->sc_hub->hub; 501 struct usbd_port *rup; 502 int port, nports; 503 504 #if defined(__NetBSD__) || defined(__OpenBSD__) 505 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 506 #elif defined(__FreeBSD__) 507 DPRINTF(("uhub_detach: sc=%port\n", sc)); 508 #endif 509 510 if (hub == NULL) /* Must be partially working */ 511 return (0); 512 513 usbd_abort_pipe(sc->sc_ipipe); 514 usbd_close_pipe(sc->sc_ipipe); 515 516 nports = hub->hubdesc.bNbrPorts; 517 for(port = 0; port < nports; port++) { 518 rup = &hub->ports[port]; 519 if (rup->device) 520 usb_disconnect_port(rup, self); 521 } 522 523 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 524 USBDEV(sc->sc_dev)); 525 526 free(hub, M_USBDEV); 527 sc->sc_hub->hub = NULL; 528 529 return (0); 530 } 531 532 #if defined(__FreeBSD__) 533 /* Called when a device has been detached from it */ 534 static void 535 uhub_child_detached(self, child) 536 device_t self; 537 device_t child; 538 { 539 struct uhub_softc *sc = device_get_softc(self); 540 usbd_device_handle devhub = sc->sc_hub; 541 usbd_device_handle dev; 542 int nports; 543 int port; 544 int i; 545 546 if (!devhub->hub) 547 /* should never happen; children are only created after init */ 548 panic("hub not fully initialised, but child deleted?"); 549 550 nports = devhub->hub->hubdesc.bNbrPorts; 551 for (port = 0; port < nports; port++) { 552 dev = devhub->hub->ports[port].device; 553 if (dev && dev->subdevs) { 554 for (i = 0; dev->subdevs[i]; i++) { 555 if (dev->subdevs[i] == child) { 556 dev->subdevs[i] = NULL; 557 return; 558 } 559 } 560 } 561 } 562 } 563 #endif 564 565 /* 566 * Hub interrupt. 567 * This an indication that some port has changed status. 568 * Notify the bus event handler thread that we need 569 * to be explored again. 570 */ 571 void 572 uhub_intr(xfer, addr, status) 573 usbd_xfer_handle xfer; 574 usbd_private_handle addr; 575 usbd_status status; 576 { 577 struct uhub_softc *sc = addr; 578 579 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 580 if (status != USBD_NORMAL_COMPLETION) 581 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 582 583 usb_needs_explore(sc->sc_hub->bus); 584 } 585 586 #if defined(__FreeBSD__) 587 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 588 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 589 #endif 590