1 /* $NetBSD: uhub.c,v 1.68 2004/06/29 06:30:05 mycroft Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.69.2.1 2005/12/18 15:51:31 iedowse Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
42 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
52 #include <sys/thread2.h>
53 #include <sys/sysctl.h>
55 #include <bus/usb/usb.h>
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
58 #include <bus/usb/usbdivar.h>
60 #define UHUB_INTR_INTERVAL 255 /* ms */
63 #define DPRINTF(x) if (uhubdebug) kprintf x
64 #define DPRINTFN(n,x) if (uhubdebug>(n)) kprintf x
66 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
67 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
68 &uhubdebug, 0, "uhub debug level");
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 */
80 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
81 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
82 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
84 static usbd_status uhub_explore(usbd_device_handle hub);
85 static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
87 static bus_child_detached_t uhub_child_detached;
88 static bus_child_location_str_t uhub_child_location_str;
89 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
92 * We need two attachment points:
93 * hub to usb and hub to hub
94 * Every other driver only connects to hubs
97 static device_probe_t uhub_match;
98 static device_attach_t uhub_attach;
99 static device_detach_t uhub_detach;
101 static devclass_t uhub_devclass;
103 static kobj_method_t uhub_methods[] = {
104 DEVMETHOD(device_probe, uhub_match),
105 DEVMETHOD(device_attach, uhub_attach),
106 DEVMETHOD(device_detach, uhub_detach),
107 DEVMETHOD(bus_child_detached, uhub_child_detached),
108 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
109 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
110 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
111 DEVMETHOD(device_suspend, bus_generic_suspend),
112 DEVMETHOD(device_resume, bus_generic_resume),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
117 static driver_t uhub_driver = {
120 sizeof(struct uhub_softc)
123 MODULE_DEPEND(uhub, usb, 1, 1, 1);
125 /* Create the driver instance for the hub connected to usb case. */
126 devclass_t uhubroot_devclass;
128 static device_method_t uhubroot_methods[] = {
129 DEVMETHOD(bus_child_detached, uhub_child_detached),
130 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
131 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
132 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
134 DEVMETHOD(device_probe, uhub_match),
135 DEVMETHOD(device_attach, uhub_attach),
136 DEVMETHOD(device_detach, uhub_detach),
137 DEVMETHOD(device_suspend, bus_generic_suspend),
138 DEVMETHOD(device_resume, bus_generic_resume),
139 DEVMETHOD(device_shutdown, bus_generic_shutdown),
143 static driver_t uhubroot_driver = {
146 sizeof(struct uhub_softc)
150 uhub_match(device_t self)
152 struct usb_attach_arg *uaa = device_get_ivars(self);
153 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
155 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
157 * The subclass for hubs seems to be 0 for some and 1 for others,
158 * so we just ignore the subclass.
160 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
161 return (UMATCH_DEVCLASS_DEVSUBCLASS);
162 return (UMATCH_NONE);
166 uhub_attach(device_t self)
168 struct uhub_softc *sc = device_get_softc(self);
169 struct usb_attach_arg *uaa = device_get_ivars(self);
170 usbd_device_handle dev = uaa->device;
172 struct usbd_hub *hub = NULL;
173 usb_device_request_t req;
174 usb_hub_descriptor_t hubdesc;
175 int p, port, nports, nremov, pwrdly;
176 usbd_interface_handle iface;
177 usb_endpoint_descriptor_t *ed;
178 struct usbd_tt *tts = NULL;
180 DPRINTFN(1,("uhub_attach\n"));
183 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
185 "%s transaction translator%s\n",
186 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
187 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
189 err = usbd_set_config_index(dev, 0, 1);
191 DPRINTF(("%s: configuration failed, error=%s\n",
192 device_get_nameunit(self), usbd_errstr(err)));
196 if (dev->depth > USB_HUB_MAX_DEPTH) {
198 "hub depth (%d) exceeded, hub ignored\n",
203 /* Get hub descriptor. */
204 req.bmRequestType = UT_READ_CLASS_DEVICE;
205 req.bRequest = UR_GET_DESCRIPTOR;
206 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
207 USETW(req.wIndex, 0);
208 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
209 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
210 err = usbd_do_request(dev, &req, &hubdesc);
211 nports = hubdesc.bNbrPorts;
212 if (!err && nports > 7) {
213 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
214 err = usbd_do_request(dev, &req, &hubdesc);
217 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
218 device_get_nameunit(self), usbd_errstr(err)));
222 for (nremov = 0, port = 1; port <= nports; port++)
223 if (!UHD_NOT_REMOV(&hubdesc, port))
226 "%d port%s with %d removable, %s powered\n",
227 nports, nports != 1 ? "s" : "",
228 nremov, dev->self_powered ? "self" : "bus");
231 device_printf(self, "no ports, hub ignored\n");
235 hub = kmalloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
238 dev->hub->hubdev = self;
239 hub->explore = uhub_explore;
240 hub->hubdesc = hubdesc;
242 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
243 "parent->selfpowered=%d\n",
244 dev->self_powered, dev->powersrc->parent,
245 dev->powersrc->parent ?
246 dev->powersrc->parent->self_powered : 0));
248 if (!dev->self_powered && dev->powersrc->parent != NULL &&
249 !dev->powersrc->parent->self_powered) {
251 "bus powered hub connected to bus powered hub, "
256 /* Set up interrupt pipe. */
257 err = usbd_device2interface_handle(dev, 0, &iface);
259 device_printf(self, "no interface handle\n");
262 ed = usbd_interface2endpoint_descriptor(iface, 0);
264 device_printf(self, "no endpoint descriptor\n");
267 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
268 device_printf(self, "bad interrupt endpoint\n");
272 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
273 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
274 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
276 device_printf(self, "cannot open interrupt pipe\n");
280 /* Wait with power off for a while. */
281 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
283 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, self);
286 * To have the best chance of success we do things in the exact same
287 * order as Windoze98. This should not be necessary, but some
288 * devices do not follow the USB specs to the letter.
290 * These are the events on the bus when a hub is attached:
291 * Get device and config descriptors (see attach code)
292 * Get hub descriptor (see above)
295 * wait for power to become stable
296 * (all below happens in explore code)
298 * clear C_PORT_CONNECTION
301 * if device connected
307 * proceed with device attachment
310 if (UHUB_IS_HIGH_SPEED(sc)) {
311 tts = kmalloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
312 sizeof(struct usbd_tt), M_USBDEV, M_WAITOK);
315 /* Set up data structures */
316 for (p = 0; p < nports; p++) {
317 struct usbd_port *up = &hub->ports[p];
321 if (dev->self_powered)
322 /* Self powered hub, give ports maximum current. */
323 up->power = USB_MAX_POWER;
325 up->power = USB_MIN_POWER;
327 if (UHUB_IS_HIGH_SPEED(sc)) {
328 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
335 /* XXX should check for none, individual, or ganged power? */
337 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
338 + USB_EXTRA_POWER_UP_TIME;
339 for (port = 1; port <= nports; port++) {
340 /* Turn the power on. */
341 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
344 "port %d power on failed, %s\n",
345 port, usbd_errstr(err));
346 DPRINTF(("usb_init_port: turn on port %d power\n", port));
349 /* Wait for stable power if we are not a root hub */
350 if (dev->powersrc->parent != NULL)
351 usbd_delay_ms(dev, pwrdly);
353 /* The usual exploration will finish the setup. */
361 kfree(hub, M_USBDEV);
367 uhub_explore(usbd_device_handle dev)
369 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
370 device_t self = dev->hub->hubdev;
371 struct uhub_softc *sc = device_get_softc(self);
372 struct usbd_port *up;
378 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
381 return (USBD_NOT_STARTED);
383 /* Ignore hubs that are too deep. */
384 if (dev->depth > USB_HUB_MAX_DEPTH)
385 return (USBD_TOO_DEEP);
387 for(port = 1; port <= hd->bNbrPorts; port++) {
388 up = &dev->hub->ports[port-1];
389 err = usbd_get_port_status(dev, port, &up->status);
391 DPRINTF(("uhub_explore: get port status failed, "
392 "error=%s\n", usbd_errstr(err)));
395 status = UGETW(up->status.wPortStatus);
396 change = UGETW(up->status.wPortChange);
397 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
398 device_get_nameunit(self), port, status, change));
399 if (change & UPS_C_PORT_ENABLED) {
400 DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
401 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
402 if (change & UPS_C_CONNECT_STATUS) {
403 /* Ignore the port error if the device
405 } else if (status & UPS_PORT_ENABLED) {
407 "illegal enable change, port %d\n", port);
409 /* Port error condition. */
410 if (up->restartcnt) /* no message first time */
412 "port error, restarting "
415 if (up->restartcnt++ < USBD_RESTART_MAX)
419 "port error, giving up "
423 if (!(change & UPS_C_CONNECT_STATUS)) {
424 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
426 /* No status change, just do recursive explore. */
427 if (up->device != NULL && up->device->hub != NULL)
428 up->device->hub->explore(up->device);
429 #if 0 && defined(DIAGNOSTIC)
430 if (up->device == NULL &&
431 (status & UPS_CURRENT_CONNECT_STATUS))
433 "connected, no device\n");
438 /* We have a connect status change, handle it. */
440 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
441 dev->address, port));
442 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
443 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
445 * If there is already a device on the port the change status
446 * must mean that is has disconnected. Looking at the
447 * current connect status is not enough to figure this out
448 * since a new unit may have been connected before we handle
452 if (up->device != NULL) {
454 DPRINTF(("uhub_explore: device addr=%d disappeared "
455 "on port %d\n", up->device->address, port));
456 usb_disconnect_port(up, self);
457 usbd_clear_port_feature(dev, port,
458 UHF_C_PORT_CONNECTION);
460 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
461 /* Nothing connected, just ignore it. */
462 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
469 if (!(status & UPS_PORT_POWER))
471 "strange, connected port %d has no power\n",
474 /* Wait for maximum device power up time. */
475 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
477 /* Reset port, which implies enabling it. */
478 if (usbd_reset_port(dev, port, &up->status)) {
480 "port %d reset failed\n", port);
483 /* Get port status again, it might have changed during reset */
484 err = usbd_get_port_status(dev, port, &up->status);
486 DPRINTF(("uhub_explore: get port status failed, "
487 "error=%s\n", usbd_errstr(err)));
490 status = UGETW(up->status.wPortStatus);
491 change = UGETW(up->status.wPortChange);
492 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
493 /* Nothing connected, just ignore it. */
496 "port %d, device disappeared after reset\n",
503 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
505 "port %d, transaction translation not "
506 "implemented, low/full speed device ignored\n",
512 /* Figure out device speed */
513 if (status & UPS_HIGH_SPEED)
514 speed = USB_SPEED_HIGH;
515 else if (status & UPS_LOW_SPEED)
516 speed = USB_SPEED_LOW;
518 speed = USB_SPEED_FULL;
519 /* Get device info and set its address. */
520 err = usbd_new_device(self, dev->bus,
521 dev->depth + 1, speed, port, up);
522 /* XXX retry a few times? */
524 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
525 "error=%s\n", usbd_errstr(err)));
526 /* Avoid addressing problems by disabling. */
527 /* usbd_reset_port(dev, port, &up->status); */
530 * The unit refused to accept a new address, or had
531 * some other serious problem. Since we cannot leave
532 * at 0 we have to disable the port instead.
535 "device problem (%s), disabling port %d\n",
536 usbd_errstr(err), port);
537 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
539 /* The port set up succeeded, reset error count. */
543 up->device->hub->explore(up->device);
546 return (USBD_NORMAL_COMPLETION);
550 * Called from process context when the hub is gone.
551 * Detach all devices on active ports.
554 uhub_detach(device_t self)
556 struct uhub_softc *sc = device_get_softc(self);
557 struct usbd_hub *hub = sc->sc_hub->hub;
558 struct usbd_port *rup;
561 DPRINTF(("uhub_detach: sc=%port\n", sc));
565 if (hub == NULL) { /* Must be partially working */
570 usbd_abort_pipe(sc->sc_ipipe);
571 usbd_close_pipe(sc->sc_ipipe);
573 nports = hub->hubdesc.bNbrPorts;
574 for(port = 0; port < nports; port++) {
575 rup = &hub->ports[port];
577 usb_disconnect_port(rup, self);
580 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, self);
582 if (hub->ports[0].tt)
583 kfree(hub->ports[0].tt, M_USBDEV);
584 kfree(hub, M_USBDEV);
585 sc->sc_hub->hub = NULL;
593 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
596 struct uhub_softc *sc = device_get_softc(cbdev);
597 usbd_device_handle devhub = sc->sc_hub;
598 usbd_device_handle dev;
604 nports = devhub->hub->hubdesc.bNbrPorts;
605 for (port = 0; port < nports; port++) {
606 dev = devhub->hub->ports[port].device;
607 if (dev && dev->subdevs) {
608 for (i = 0; dev->subdevs[i]; i++) {
609 if (dev->subdevs[i] == child) {
610 if (dev->ifacenums == NULL) {
611 ksnprintf(buf, buflen,
614 ksnprintf(buf, buflen,
615 "port=%i interface=%i",
616 port, dev->ifacenums[i]);
623 DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
631 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
634 struct uhub_softc *sc = device_get_softc(cbdev);
635 usbd_device_handle devhub = sc->sc_hub;
636 usbd_device_handle dev;
637 struct usbd_interface *iface;
644 nports = devhub->hub->hubdesc.bNbrPorts;
645 for (port = 0; port < nports; port++) {
646 dev = devhub->hub->ports[port].device;
647 if (dev && dev->subdevs) {
648 for (i = 0; dev->subdevs[i]; i++) {
649 if (dev->subdevs[i] == child) {
655 DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
662 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
663 if (dev->ifacenums == NULL) {
664 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
665 "devclass=0x%02x devsubclass=0x%02x "
666 "release=0x%04x sernum=\"%s\"",
667 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
668 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
669 UGETW(dev->ddesc.bcdDevice), serial);
671 iface = &dev->ifaces[dev->ifacenums[i]];
672 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
673 "devclass=0x%02x devsubclass=0x%02x "
674 "release=0x%04x sernum=\"%s\" "
675 "intclass=0x%02x intsubclass=0x%02x",
676 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
677 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
678 UGETW(dev->ddesc.bcdDevice), serial,
679 iface->idesc->bInterfaceClass,
680 iface->idesc->bInterfaceSubClass);
687 uhub_child_detached(device_t self, device_t child)
689 struct uhub_softc *sc = device_get_softc(self);
690 usbd_device_handle devhub = sc->sc_hub;
691 usbd_device_handle dev = NULL;
692 int nports, port, i = 0;
695 nports = devhub->hub->hubdesc.bNbrPorts;
696 for (port = 0; port < nports; port++) {
697 dev = devhub->hub->ports[port].device;
698 if (dev && dev->subdevs) {
699 for (i = 0; dev->subdevs[i]; ++i) {
700 if (dev->subdevs[i] == child)
710 device_printf(dev->subdevs[i], "at %s", device_get_nameunit(self));
712 kprintf(" port %d", port);
713 kprintf(" (addr %d) disconnected\n", dev->address);
715 kfree(device_get_ivars(dev->subdevs[i]), M_USB);
716 dev->subdevs[i] = NULL;
722 * This an indication that some port has changed status.
723 * Notify the bus event handler thread that we need
724 * to be explored again.
727 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
729 struct uhub_softc *sc = addr;
731 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
732 if (status == USBD_STALLED)
733 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
734 else if (status == USBD_NORMAL_COMPLETION)
735 usb_needs_explore(sc->sc_hub);
738 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, NULL, NULL);
739 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, NULL);