1 /* $NetBSD: uhub.c,v 1.32 1999/10/13 08:10:56 augustss Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (augustss@carlstedt.se) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #if defined(__NetBSD__) || defined(__OpenBSD__) 49 #include <sys/device.h> 50 #elif defined(__FreeBSD__) 51 #include <sys/module.h> 52 #include <sys/bus.h> 53 #include "bus_if.h" 54 #endif 55 #include <sys/proc.h> 56 57 #include <machine/bus.h> 58 59 #include <dev/usb/usb.h> 60 #include <dev/usb/usbdi.h> 61 #include <dev/usb/usbdi_util.h> 62 #include <dev/usb/usbdivar.h> 63 64 #ifdef UHUB_DEBUG 65 #define DPRINTF(x) if (usbdebug) logprintf x 66 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x 67 extern int usbdebug; 68 #else 69 #define DPRINTF(x) 70 #define DPRINTFN(n,x) 71 #endif 72 73 struct uhub_softc { 74 USBBASEDEVICE sc_dev; /* base device */ 75 usbd_device_handle sc_hub; /* USB device */ 76 usbd_pipe_handle sc_ipipe; /* interrupt pipe */ 77 u_int8_t sc_status[1]; /* XXX more ports */ 78 u_char sc_running; 79 }; 80 81 usbd_status uhub_init_port __P((struct usbd_port *)); 82 usbd_status uhub_explore __P((usbd_device_handle hub)); 83 void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status)); 84 85 #if defined(__FreeBSD__) 86 static bus_child_detached_t uhub_child_detached; 87 #endif 88 89 USB_DECLARE_DRIVER_INIT(uhub, 90 DEVMETHOD(bus_child_detached, uhub_child_detached)); 91 92 /* 93 * We need two attachment points: 94 * hub to usb and hub to hub 95 * Every other driver only connects to hubs 96 */ 97 98 #if defined(__NetBSD__) || defined(__OpenBSD__) 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 /* Create the driver instance for the hub connected to usb case. */ 106 devclass_t uhubroot_devclass; 107 108 static device_method_t uhubroot_methods[] = { 109 DEVMETHOD(device_probe, uhub_match), 110 DEVMETHOD(device_attach, uhub_attach), 111 112 /* detach is not allowed for a root hub */ 113 {0,0} 114 }; 115 116 static driver_t uhubroot_driver = { 117 "uhub", 118 uhubroot_methods, 119 sizeof(struct uhub_softc) 120 }; 121 #endif 122 123 USB_MATCH(uhub) 124 { 125 USB_MATCH_START(uhub, uaa); 126 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); 127 128 DPRINTFN(5,("uhub_match, dd=%p\n", dd)); 129 /* 130 * The subclass for hubs seems to be 0 for some and 1 for others, 131 * so we just ignore the subclass. 132 */ 133 if (uaa->iface == 0 && dd->bDeviceClass == UCLASS_HUB) 134 return (UMATCH_DEVCLASS_DEVSUBCLASS); 135 return (UMATCH_NONE); 136 } 137 138 USB_ATTACH(uhub) 139 { 140 USB_ATTACH_START(uhub, sc, uaa); 141 usbd_device_handle dev = uaa->device; 142 char devinfo[1024]; 143 usbd_status r; 144 struct usbd_hub *hub; 145 usb_device_request_t req; 146 usb_hub_descriptor_t hubdesc; 147 int p, port, nports, nremov; 148 usbd_interface_handle iface; 149 usb_endpoint_descriptor_t *ed; 150 151 DPRINTFN(1,("uhub_attach\n")); 152 sc->sc_hub = dev; 153 usbd_devinfo(dev, 1, devinfo); 154 USB_ATTACH_SETUP; 155 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 156 157 r = usbd_set_config_index(dev, 0, 1); 158 if (r != USBD_NORMAL_COMPLETION) { 159 DPRINTF(("%s: configuration failed, error=%s\n", 160 USBDEVNAME(sc->sc_dev), usbd_errstr(r))); 161 USB_ATTACH_ERROR_RETURN; 162 } 163 164 if (dev->depth > USB_HUB_MAX_DEPTH) { 165 printf("%s: hub depth (%d) exceeded, hub ignored\n", 166 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH); 167 USB_ATTACH_ERROR_RETURN; 168 } 169 170 /* Get hub descriptor. */ 171 req.bmRequestType = UT_READ_CLASS_DEVICE; 172 req.bRequest = UR_GET_DESCRIPTOR; 173 USETW(req.wValue, 0); 174 USETW(req.wIndex, 0); 175 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 176 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n")); 177 r = usbd_do_request(dev, &req, &hubdesc); 178 nports = hubdesc.bNbrPorts; 179 if (r == USBD_NORMAL_COMPLETION && nports > 7) { 180 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8); 181 r = usbd_do_request(dev, &req, &hubdesc); 182 } 183 if (r != USBD_NORMAL_COMPLETION) { 184 DPRINTF(("%s: getting hub descriptor failed, error=%s\n", 185 USBDEVNAME(sc->sc_dev), usbd_errstr(r))); 186 USB_ATTACH_ERROR_RETURN; 187 } 188 189 for (nremov = 0, port = 1; port <= nports; port++) 190 if (!UHD_NOT_REMOV(&hubdesc, port)) 191 nremov++; 192 printf("%s: %d port%s with %d removable, %s powered\n", 193 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "", 194 nremov, dev->self_powered ? "self" : "bus"); 195 196 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port), 197 M_USBDEV, M_NOWAIT); 198 if (hub == 0) 199 USB_ATTACH_ERROR_RETURN; 200 dev->hub = hub; 201 dev->hub->hubsoftc = sc; 202 hub->explore = uhub_explore; 203 hub->hubdesc = hubdesc; 204 205 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, " 206 "parent->selfpowered=%d\n", 207 dev->self_powered, dev->powersrc->parent, 208 dev->powersrc->parent ? 209 dev->powersrc->parent->self_powered : 0)); 210 211 if (!dev->self_powered && dev->powersrc->parent && 212 !dev->powersrc->parent->self_powered) { 213 printf("%s: bus powered hub connected to bus powered hub, " 214 "ignored\n", USBDEVNAME(sc->sc_dev)); 215 goto bad; 216 } 217 218 /* Set up interrupt pipe. */ 219 r = usbd_device2interface_handle(dev, 0, &iface); 220 if (r != USBD_NORMAL_COMPLETION) { 221 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev)); 222 goto bad; 223 } 224 ed = usbd_interface2endpoint_descriptor(iface, 0); 225 if (ed == 0) { 226 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); 227 goto bad; 228 } 229 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) { 230 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev)); 231 goto bad; 232 } 233 234 r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK, 235 &sc->sc_ipipe, sc, sc->sc_status, 236 sizeof(sc->sc_status), 237 uhub_intr); 238 if (r != USBD_NORMAL_COMPLETION) { 239 printf("%s: cannot open interrupt pipe\n", 240 USBDEVNAME(sc->sc_dev)); 241 goto bad; 242 } 243 244 /* Wait with power off for a while. */ 245 usbd_delay_ms(dev, USB_POWER_DOWN_TIME); 246 247 for (p = 0; p < nports; p++) { 248 struct usbd_port *up = &hub->ports[p]; 249 up->device = 0; 250 up->parent = dev; 251 up->portno = p+1; 252 r = uhub_init_port(up); 253 if (r != USBD_NORMAL_COMPLETION) 254 printf("%s: init of port %d failed\n", 255 USBDEVNAME(sc->sc_dev), up->portno); 256 } 257 sc->sc_running = 1; 258 259 USB_ATTACH_SUCCESS_RETURN; 260 261 bad: 262 free(hub, M_USBDEV); 263 dev->hub = 0; 264 USB_ATTACH_ERROR_RETURN; 265 } 266 267 usbd_status 268 uhub_init_port(up) 269 struct usbd_port *up; 270 { 271 int port = up->portno; 272 usbd_device_handle dev = up->parent; 273 usbd_status r; 274 u_int16_t pstatus; 275 276 r = usbd_get_port_status(dev, port, &up->status); 277 if (r != USBD_NORMAL_COMPLETION) 278 return (r); 279 pstatus = UGETW(up->status.wPortStatus); 280 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x " 281 "change=0x%04x\n", 282 port, pstatus, UGETW(up->status.wPortChange))); 283 if ((pstatus & UPS_PORT_POWER) == 0) { 284 /* Port lacks power, turn it on */ 285 286 /* First let the device go through a good power cycle, */ 287 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME); 288 289 #if 0 290 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT); 291 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT); 292 #endif 293 294 /* then turn the power on. */ 295 r = usbd_set_port_feature(dev, port, UHF_PORT_POWER); 296 if (r != USBD_NORMAL_COMPLETION) 297 return (r); 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 r = usbd_get_port_status(dev, port, &up->status); 307 if (r != USBD_NORMAL_COMPLETION) 308 return (r); 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 r; 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 r = usbd_get_port_status(dev, port, &up->status); 357 if (r != USBD_NORMAL_COMPLETION) { 358 DPRINTF(("uhub_explore: get port status failed, " 359 "error=%s\n", 360 usbd_errstr(r))); 361 continue; 362 } 363 status = UGETW(up->status.wPortStatus); 364 change = UGETW(up->status.wPortChange); 365 DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n", 366 port, status, change)); 367 if (change & UPS_C_PORT_ENABLED) { 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 /* 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) { 405 /* Disconnected */ 406 DPRINTF(("uhub_explore: device %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 continue; 414 415 /* Connected */ 416 up->restartcnt = 0; 417 418 /* Wait for maximum device power up time. */ 419 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY); 420 421 /* Reset port, which implies enabling it. */ 422 if (usbd_reset_port(dev, port, &up->status) != 423 USBD_NORMAL_COMPLETION) 424 continue; 425 426 /* Get device info and set its address. */ 427 r = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 428 dev->depth + 1, status & UPS_LOW_SPEED, 429 port, up); 430 /* XXX retry a few times? */ 431 if (r != USBD_NORMAL_COMPLETION) { 432 DPRINTFN(-1,("uhub_explore: usb_new_device failed, " 433 "error=%s\n", usbd_errstr(r))); 434 /* Avoid addressing problems by disabling. */ 435 /* usbd_reset_port(dev, port, &up->status); */ 436 437 /* 438 * The unit refused to accept a new address, or had 439 * some other serious problem. Since we cannot leave 440 * at 0 we have to disable the port instead. 441 */ 442 printf("%s: device problem, disabling port %d\n", 443 USBDEVNAME(sc->sc_dev), port); 444 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE); 445 /* Make sure we don't try to restart it infinitely. */ 446 up->restartcnt = USBD_RESTART_MAX; 447 } else { 448 if (up->device->hub) 449 up->device->hub->explore(up->device); 450 } 451 } 452 return (USBD_NORMAL_COMPLETION); 453 } 454 455 #if defined(__NetBSD__) || defined(__OpenBSD__) 456 int 457 uhub_activate(self, act) 458 device_ptr_t self; 459 enum devact act; 460 { 461 struct uhub_softc *sc = (struct uhub_softc *)self; 462 usbd_device_handle devhub = sc->sc_hub; 463 int nports, p, i; 464 465 switch (act) { 466 case DVACT_ACTIVATE: 467 return (EOPNOTSUPP); 468 break; 469 470 case DVACT_DEACTIVATE: 471 nports = devhub->hub->hubdesc.bNbrPorts; 472 for(p = 0; p < nports; p++) { 473 usbd_device_handle dev = devhub->hub->ports[p].device; 474 if (dev) { 475 for (i = 0; dev->subdevs[i]; i++) 476 config_deactivate(dev->subdevs[i]); 477 } 478 } 479 break; 480 } 481 return (0); 482 } 483 #endif 484 485 /* 486 * Called from process context when the hub is gone. 487 * Detach all devices on active ports. 488 */ 489 USB_DETACH(uhub) 490 { 491 USB_DETACH_START(uhub, sc); 492 usbd_device_handle dev = sc->sc_hub; 493 struct usbd_port *rup; 494 int port, nports; 495 496 #if defined(__NetBSD__) || defined(__OpenBSD__) 497 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags)); 498 #elif defined(__FreeBSD__) 499 DPRINTF(("uhub_detach: sc=%port\n", sc)); 500 #endif 501 502 if (!dev->hub) /* Must be partially working */ 503 return (0); 504 505 usbd_abort_pipe(sc->sc_ipipe); 506 usbd_close_pipe(sc->sc_ipipe); 507 508 nports = dev->hub->hubdesc.bNbrPorts; 509 for(port = 0; port < nports; port++) { 510 rup = &dev->hub->ports[port]; 511 if (rup->device) 512 usb_disconnect_port(rup, self); 513 } 514 515 free(dev->hub, M_USBDEV); 516 dev->hub = 0; 517 518 return (0); 519 } 520 521 /* 522 * Hub interrupt. 523 * This an indication that some port has changed status. 524 * Notify the bus event handler thread that we need 525 * to be explored again. 526 */ 527 void 528 uhub_intr(reqh, addr, status) 529 usbd_request_handle reqh; 530 usbd_private_handle addr; 531 usbd_status status; 532 { 533 struct uhub_softc *sc = addr; 534 535 DPRINTFN(5,("uhub_intr: sc=%p\n", sc)); 536 if (status != USBD_NORMAL_COMPLETION) 537 usbd_clear_endpoint_stall_async(sc->sc_ipipe); 538 539 usb_needs_explore(sc->sc_hub->bus); 540 } 541 542 #if defined(__FreeBSD__) 543 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0); 544 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0); 545 #endif 546