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