1 /* $NetBSD: uhub.c,v 1.40 2000/02/29 21:37:01 augustss Exp $ */ 2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma 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 static usbd_status uhub_init_port __P((struct usbd_port *)); 83 static usbd_status uhub_explore __P((usbd_device_handle hub)); 84 static 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 /* 92 * We need two attachment points: 93 * hub to usb and hub to hub 94 * Every other driver only connects to hubs 95 */ 96 97 #if defined(__NetBSD__) || defined(__OpenBSD__) 98 USB_DECLARE_DRIVER(uhub); 99 100 /* Create the driver instance for the hub connected to hub case */ 101 struct cfattach uhub_uhub_ca = { 102 sizeof(struct uhub_softc), uhub_match, uhub_attach, 103 uhub_detach, uhub_activate 104 }; 105 #elif defined(__FreeBSD__) 106 USB_DECLARE_DRIVER_INIT(uhub, 107 DEVMETHOD(bus_child_detached, uhub_child_detached)); 108 109 /* Create the driver instance for the hub connected to usb case. */ 110 devclass_t uhubroot_devclass; 111 112 static device_method_t uhubroot_methods[] = { 113 DEVMETHOD(device_probe, uhub_match), 114 DEVMETHOD(device_attach, uhub_attach), 115 116 /* detach is not allowed for a root hub */ 117 {0,0} 118 }; 119 120 static driver_t uhubroot_driver = { 121 "uhub", 122 uhubroot_methods, 123 sizeof(struct uhub_softc) 124 }; 125 #endif 126 127 USB_MATCH(uhub) 128 { 129 USB_MATCH_START(uhub, uaa); 130 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 131 132 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 133 /* 134 * The subclass for hubs seems to be 0 for some and 1 for others, 135 * so we just ignore the subclass. 136 */ 137 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB) 138 return (UMATCH_DEVCLASS_DEVSUBCLASS); 139 return (UMATCH_NONE); 140 } 141 142 USB_ATTACH(uhub) 143 { 144 USB_ATTACH_START(uhub, sc, uaa); 145 usbd_device_handle dev = uaa->device; 146 char devinfo[1024]; 147 usbd_status err; 148 struct usbd_hub *hub; 149 usb_device_request_t req; 150 usb_hub_descriptor_t hubdesc; 151 int p, port, nports, nremov; 152 usbd_interface_handle iface; 153 usb_endpoint_descriptor_t *ed; 154 155 DPRINTFN(1,("uhub_attach\n")); 156 sc->sc_hub = dev; 157 usbd_devinfo(dev, 1, devinfo); 158 USB_ATTACH_SETUP; 159 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 160 161 err = usbd_set_config_index(dev, 0, 1); 162 if (err) { 163 DPRINTF(("%s: configuration failed, error=%s\n", 164 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 165 USB_ATTACH_ERROR_RETURN; 166 } 167 168 if (dev->depth > USB_HUB_MAX_DEPTH) { 169 printf("%s: hub depth (%d) exceeded, hub ignored\n", 170 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 171 USB_ATTACH_ERROR_RETURN; 172 } 173 174 /* Get hub descriptor. */ 175 req.bmRequestType = UT_READ_CLASS_DEVICE; 176 req.bRequest = UR_GET_DESCRIPTOR; 177 USETW(req.wValue, 0); 178 USETW(req.wIndex, 0); 179 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 180 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 181 err = usbd_do_request(dev, &req, &hubdesc); 182 nports = hubdesc.bNbrPorts; 183 if (!err && nports > 7) { 184 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 185 err = usbd_do_request(dev, &req, &hubdesc); 186 } 187 if (err) { 188 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 189 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 190 USB_ATTACH_ERROR_RETURN; 191 } 192 193 for (nremov = 0, port = 1; port <= nports; port++) 194 if (!UHD_NOT_REMOV(&hubdesc, port)) 195 nremov++; 196 printf("%s: %d port%s with %d removable, %s powered\n", 197 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 198 nremov, dev->self_powered ? "self" : "bus"); 199 200 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 201 M_USBDEV, M_NOWAIT); 202 if (hub == NULL) 203 USB_ATTACH_ERROR_RETURN; 204 dev->hub = hub; 205 dev->hub->hubsoftc = sc; 206 hub->explore = uhub_explore; 207 hub->hubdesc = hubdesc; 208 209 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 210 "parent->selfpowered=%d\n", 211 dev->self_powered, dev->powersrc->parent, 212 dev->powersrc->parent ? 213 dev->powersrc->parent->self_powered : 0)); 214 215 if (!dev->self_powered && dev->powersrc->parent != NULL && 216 !dev->powersrc->parent->self_powered) { 217 printf("%s: bus powered hub connected to bus powered hub, " 218 "ignored\n", USBDEVNAME(sc->sc_dev)); 219 goto bad; 220 } 221 222 /* Set up interrupt pipe. */ 223 err = usbd_device2interface_handle(dev, 0, &iface); 224 if (err) { 225 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev)); 226 goto bad; 227 } 228 ed = usbd_interface2endpoint_descriptor(iface, 0); 229 if (ed == NULL) { 230 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 231 goto bad; 232 } 233 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 234 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 235 goto bad; 236 } 237 238 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, 239 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 240 sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL); 241 if (err) { 242 printf("%s: cannot open interrupt pipe\n", 243 USBDEVNAME(sc->sc_dev)); 244 goto bad; 245 } 246 247 /* Wait with power off for a while. */ 248 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 249 250 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev)); 251 252 for (p = 0; p < nports; p++) { 253 struct usbd_port *up = &hub->ports[p]; 254 up->device = 0; 255 up->parent = dev; 256 up->portno = p+1; 257 err = uhub_init_port(up); 258 if (err) 259 printf("%s: init of port %d failed\n", 260 USBDEVNAME(sc->sc_dev), up->portno); 261 } 262 sc->sc_running = 1; 263 264 USB_ATTACH_SUCCESS_RETURN; 265 266 bad: 267 free(hub, M_USBDEV); 268 dev->hub = 0; 269 USB_ATTACH_ERROR_RETURN; 270 } 271 272 usbd_status 273 uhub_init_port(up) 274 struct usbd_port *up; 275 { 276 int port = up->portno; 277 usbd_device_handle dev = up->parent; 278 usbd_status err; 279 u_int16_t pstatus; 280 281 err = usbd_get_port_status(dev, port, &up->status); 282 if (err) 283 return (err); 284 pstatus = UGETW(up->status.wPortStatus); 285 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x " 286 "change=0x%04x\n", 287 port, pstatus, UGETW(up->status.wPortChange))); 288 if ((pstatus & UPS_PORT_POWER) == 0) { 289 /* Port lacks power, turn it on */ 290 291 /* First let the device go through a good power cycle, */ 292 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME); 293 294 /* then turn the power on. */ 295 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 296 if (err) 297 return (err); 298 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x " 299 "change=0x%04x\n", 300 port, UGETW(up->status.wPortStatus), 301 UGETW(up->status.wPortChange))); 302 /* Wait for stable power. */ 303 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood * 304 UHD_PWRON_FACTOR); 305 /* Get the port status again. */ 306 err = usbd_get_port_status(dev, port, &up->status); 307 if (err) 308 return (err); 309 DPRINTF(("usb_init_port: after power on status=0x%04x " 310 "change=0x%04x\n", 311 UGETW(up->status.wPortStatus), 312 UGETW(up->status.wPortChange))); 313 314 #if 0 315 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT); 316 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT); 317 usbd_get_port_status(dev, port, &up->status); 318 #endif 319 320 pstatus = UGETW(up->status.wPortStatus); 321 if ((pstatus & UPS_PORT_POWER) == 0) 322 printf("%s: port %d did not power up\n", 323 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port); 324 325 } 326 if (dev->self_powered) 327 /* Self powered hub, give ports maximum current. */ 328 up->power = USB_MAX_POWER; 329 else 330 up->power = USB_MIN_POWER; 331 return (USBD_NORMAL_COMPLETION); 332 } 333 334 usbd_status 335 uhub_explore(dev) 336 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 port; 343 int change, status; 344 345 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address)); 346 347 if (!sc->sc_running) 348 return (USBD_NOT_STARTED); 349 350 /* Ignore hubs that are too deep. */ 351 if (dev->depth > USB_HUB_MAX_DEPTH) 352 return (USBD_TOO_DEEP); 353 354 for(port = 1; port <= hd->bNbrPorts; port++) { 355 up = &dev->hub->ports[port-1]; 356 err = usbd_get_port_status(dev, port, &up->status); 357 if (err) { 358 DPRINTF(("uhub_explore: get port status failed, " 359 "error=%s\n", usbd_errstr(err))); 360 continue; 361 } 362 status = UGETW(up->status.wPortStatus); 363 change = UGETW(up->status.wPortChange); 364 DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n", 365 port, status, change)); 366 if (change & UPS_C_PORT_ENABLED) { 367 DPRINTF(("uhub_explore: C_PORT_ENABLED\n")); 368 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 369 if (status & UPS_PORT_ENABLED) { 370 printf("%s: illegal enable change, port %d\n", 371 USBDEVNAME(sc->sc_dev), port); 372 } else { 373 /* Port error condition. */ 374 if (up->restartcnt++ < USBD_RESTART_MAX) { 375 printf("%s: port error, restarting " 376 "port %d\n", 377 USBDEVNAME(sc->sc_dev), port); 378 goto disco; 379 } else { 380 printf("%s: port error, giving up " 381 "port %d\n", 382 USBDEVNAME(sc->sc_dev), port); 383 } 384 } 385 } 386 if (!(change & UPS_C_CONNECT_STATUS)) { 387 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_" 388 "STATUS\n", port)); 389 /* No status change, just do recursive explore. */ 390 if (up->device && up->device->hub) 391 up->device->hub->explore(up->device); 392 continue; 393 } 394 DPRINTF(("uhub_explore: status change hub=%d port=%d\n", 395 dev->address, port)); 396 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION); 397 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE); 398 /* 399 * If there is already a device on the port the change status 400 * must mean that is has disconnected. Looking at the 401 * current connect status is not enough to figure this out 402 * since a new unit may have been connected before we handle 403 * the disconnect. 404 */ 405 disco: 406 if (up->device != NULL) { 407 /* Disconnected */ 408 DPRINTF(("uhub_explore: device addr=%d disappeared " 409 "on port %d\n", up->device->address, port)); 410 usb_disconnect_port(up, USBDEV(sc->sc_dev)); 411 usbd_clear_port_feature(dev, port, 412 UHF_C_PORT_CONNECTION); 413 } 414 if (!(status & UPS_CURRENT_CONNECT_STATUS)) { 415 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT" 416 "_STATUS\n", port)); 417 continue; 418 } 419 420 /* Connected */ 421 up->restartcnt = 0; 422 423 /* Wait for maximum device power up time. */ 424 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 425 426 /* Reset port, which implies enabling it. */ 427 if (usbd_reset_port(dev, port, &up->status)) { 428 DPRINTF(("uhub_explore: port=%d reset failed\n", 429 port)); 430 continue; 431 } 432 433 /* Get device info and set its address. */ 434 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 435 dev->depth + 1, status & UPS_LOW_SPEED, 436 port, up); 437 /* XXX retry a few times? */ 438 if (err) { 439 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 440 "error=%s\n", usbd_errstr(err))); 441 /* Avoid addressing problems by disabling. */ 442 /* usbd_reset_port(dev, port, &up->status); */ 443 444 /* 445 * The unit refused to accept a new address, or had 446 * some other serious problem. Since we cannot leave 447 * at 0 we have to disable the port instead. 448 */ 449 printf("%s: device problem, disabling port %d\n", 450 USBDEVNAME(sc->sc_dev), port); 451 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 452 /* Make sure we don't try to restart it infinitely. */ 453 up->restartcnt = USBD_RESTART_MAX; 454 } else { 455 if (up->device->hub) 456 up->device->hub->explore(up->device); 457 } 458 } 459 return (USBD_NORMAL_COMPLETION); 460 } 461 462 #if defined(__NetBSD__) || defined(__OpenBSD__) 463 int 464 uhub_activate(self, act) 465 device_ptr_t self; 466 enum devact act; 467 { 468 struct uhub_softc *sc = (struct uhub_softc *)self; 469 struct usbd_hub *hub = sc->sc_hub->hub; 470 usbd_device_handle dev; 471 int nports, port, i; 472 473 switch (act) { 474 case DVACT_ACTIVATE: 475 return (EOPNOTSUPP); 476 break; 477 478 case DVACT_DEACTIVATE: 479 if (hub == NULL) /* malfunctioning hub */ 480 break; 481 nports = hub->hubdesc.bNbrPorts; 482 for(port = 0; port < nports; port++) { 483 dev = hub->ports[port].device; 484 if (dev != NULL) { 485 for (i = 0; dev->subdevs[i]; i++) 486 config_deactivate(dev->subdevs[i]); 487 } 488 } 489 break; 490 } 491 return (0); 492 } 493 #endif 494 495 /* 496 * Called from process context when the hub is gone. 497 * Detach all devices on active ports. 498 */ 499 USB_DETACH(uhub) 500 { 501 USB_DETACH_START(uhub, sc); 502 struct usbd_hub *hub = sc->sc_hub->hub; 503 struct usbd_port *rup; 504 int port, nports; 505 506 #if defined(__NetBSD__) || defined(__OpenBSD__) 507 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 508 #elif defined(__FreeBSD__) 509 DPRINTF(("uhub_detach: sc=%port\n", sc)); 510 #endif 511 512 if (hub == NULL) /* Must be partially working */ 513 return (0); 514 515 usbd_abort_pipe(sc->sc_ipipe); 516 usbd_close_pipe(sc->sc_ipipe); 517 518 nports = hub->hubdesc.bNbrPorts; 519 for(port = 0; port < nports; port++) { 520 rup = &hub->ports[port]; 521 if (rup->device) 522 usb_disconnect_port(rup, self); 523 } 524 525 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, 526 USBDEV(sc->sc_dev)); 527 528 free(hub, M_USBDEV); 529 sc->sc_hub->hub = NULL; 530 531 return (0); 532 } 533 534 #if defined(__FreeBSD__) 535 /* Called when a device has been detached from it */ 536 static void 537 uhub_child_detached(self, child) 538 device_t self; 539 device_t child; 540 { 541 struct uhub_softc *sc = device_get_softc(self); 542 usbd_device_handle devhub = sc->sc_hub; 543 usbd_device_handle dev; 544 int nports; 545 int port; 546 int i; 547 548 if (!devhub->hub) 549 /* should never happen; children are only created after init */ 550 panic("hub not fully initialised, but child deleted?"); 551 552 nports = devhub->hub->hubdesc.bNbrPorts; 553 for (port = 0; port < nports; port++) { 554 dev = devhub->hub->ports[port].device; 555 if (dev && dev->subdevs) { 556 for (i = 0; dev->subdevs[i]; i++) { 557 if (dev->subdevs[i] == child) { 558 dev->subdevs[i] = NULL; 559 return; 560 } 561 } 562 } 563 } 564 } 565 #endif 566 567 568 /* 569 * Hub interrupt. 570 * This an indication that some port has changed status. 571 * Notify the bus event handler thread that we need 572 * to be explored again. 573 */ 574 void 575 uhub_intr(xfer, addr, status) 576 usbd_xfer_handle xfer; 577 usbd_private_handle addr; 578 usbd_status status; 579 { 580 struct uhub_softc *sc = addr; 581 582 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 583 if (status != USBD_NORMAL_COMPLETION) 584 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 585 586 usb_needs_explore(sc->sc_hub->bus); 587 } 588 589 #if defined(__FreeBSD__) 590 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 591 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 592 #endif 593