1 /* $NetBSD: uhub.c,v 1.68 2004/06/29 06:30:05 mycroft Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/sysctl.h>
57 #include <machine/bus.h>
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>
64 #define UHUB_INTR_INTERVAL 255 /* ms */
67 #define DPRINTF(x) if (uhubdebug) printf x
68 #define DPRINTFN(n,x) if (uhubdebug > (n)) printf x
69 #define DEVPRINTF(x) if (uhubdebug) device_printf x
70 #define DEVPRINTFN(n, x)if (uhubdebug > (n)) device_printf x
72 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
73 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
74 &uhubdebug, 0, "uhub debug level");
79 #define DEVPRINTFN(n,x)
83 device_t sc_dev; /* base device */
84 usbd_device_handle sc_hub; /* USB device */
85 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
86 u_int8_t sc_status[32]; /* max 255 ports */
89 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
90 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
91 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
93 static usbd_status uhub_explore(usbd_device_handle hub);
94 static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
97 * We need two attachment points:
98 * hub to usb and hub to hub
99 * Every other driver only connects to hubs
102 static device_probe_t uhub_match;
103 static device_attach_t uhub_attach;
104 static device_detach_t uhub_detach;
105 static bus_child_location_str_t uhub_child_location_str;
106 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
108 static device_method_t uhub_methods[] = {
109 /* Device interface */
110 DEVMETHOD(device_probe, uhub_match),
111 DEVMETHOD(device_attach, uhub_attach),
112 DEVMETHOD(device_detach, uhub_detach),
113 DEVMETHOD(device_suspend, bus_generic_suspend),
114 DEVMETHOD(device_resume, bus_generic_resume),
115 DEVMETHOD(device_shutdown, bus_generic_shutdown),
117 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
118 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
119 /* XXX driver_added needs special care */
120 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
124 static driver_t uhub_driver = {
127 sizeof(struct uhub_softc)
130 static devclass_t uhub_devclass;
132 /* Create the driver instance for the hub connected to usb case. */
133 devclass_t uhubroot_devclass;
135 static device_method_t uhubroot_methods[] = {
136 DEVMETHOD(device_probe, uhub_match),
137 DEVMETHOD(device_attach, uhub_attach),
138 DEVMETHOD(device_detach, uhub_detach),
139 DEVMETHOD(device_suspend, bus_generic_suspend),
140 DEVMETHOD(device_resume, bus_generic_resume),
141 DEVMETHOD(device_shutdown, bus_generic_shutdown),
143 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
144 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
145 /* XXX driver_added needs special care */
146 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
151 static driver_t uhubroot_driver = {
154 sizeof(struct uhub_softc)
158 uhub_match(device_t self)
160 struct usb_attach_arg *uaa = device_get_ivars(self);
161 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
163 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
165 * The subclass for hubs seems to be 0 for some and 1 for others,
166 * so we just ignore the subclass.
168 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
169 return (UMATCH_DEVCLASS_DEVSUBCLASS);
170 return (UMATCH_NONE);
174 uhub_attach(device_t self)
176 struct uhub_softc *sc = device_get_softc(self);
177 struct usb_attach_arg *uaa = device_get_ivars(self);
178 usbd_device_handle dev = uaa->device;
180 struct usbd_hub *hub = NULL;
181 usb_device_request_t req;
182 usb_hub_descriptor_t hubdesc;
183 int p, port, nports, nremov, pwrdly;
184 usbd_interface_handle iface;
185 usb_endpoint_descriptor_t *ed;
186 struct usbd_tt *tts = NULL;
188 DPRINTFN(1,("uhub_attach\n"));
192 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
193 device_printf(sc->sc_dev, "%s transaction translator%s\n",
194 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
195 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
197 err = usbd_set_config_index(dev, 0, 1);
199 DEVPRINTF((sc->sc_dev, "configuration failed, error=%s\n",
204 if (dev->depth > USB_HUB_MAX_DEPTH) {
205 device_printf(sc->sc_dev, "hub depth (%d) exceeded, hub ignored\n",
210 /* Get hub descriptor. */
211 req.bmRequestType = UT_READ_CLASS_DEVICE;
212 req.bRequest = UR_GET_DESCRIPTOR;
213 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
214 USETW(req.wIndex, 0);
215 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
216 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
217 err = usbd_do_request(dev, &req, &hubdesc);
218 nports = hubdesc.bNbrPorts;
219 if (!err && nports > 7) {
220 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
221 err = usbd_do_request(dev, &req, &hubdesc);
224 DEVPRINTF((sc->sc_dev, "getting hub descriptor failed: %s\n",
229 for (nremov = 0, port = 1; port <= nports; port++)
230 if (!UHD_NOT_REMOV(&hubdesc, port))
232 device_printf(sc->sc_dev, "%d port%s with %d removable, %s powered\n",
233 nports, nports != 1 ? "s" : "", nremov,
234 dev->self_powered ? "self" : "bus");
237 device_printf(sc->sc_dev, "no ports, hub ignored\n");
241 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
247 dev->hub->hubsoftc = sc;
248 hub->explore = uhub_explore;
249 hub->hubdesc = hubdesc;
251 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
252 "parent->selfpowered=%d\n",
253 dev->self_powered, dev->powersrc->parent,
254 dev->powersrc->parent ?
255 dev->powersrc->parent->self_powered : 0));
257 if (!dev->self_powered && dev->powersrc->parent != NULL &&
258 !dev->powersrc->parent->self_powered) {
259 device_printf(sc->sc_dev, "bus powered hub connected to bus "
260 "powered hub, ignored\n");
264 /* Set up interrupt pipe. */
265 err = usbd_device2interface_handle(dev, 0, &iface);
267 device_printf(sc->sc_dev, "no interface handle\n");
270 ed = usbd_interface2endpoint_descriptor(iface, 0);
272 device_printf(sc->sc_dev, "no endpoint descriptor\n");
275 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
276 device_printf(sc->sc_dev, "bad interrupt endpoint\n");
280 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
281 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
282 (nports + 1 + 7) / 8, uhub_intr, UHUB_INTR_INTERVAL);
284 device_printf(sc->sc_dev, "cannot open interrupt pipe\n");
288 /* Wait with power off for a while. */
289 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
291 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
294 * To have the best chance of success we do things in the exact same
295 * order as Windoze98. This should not be necessary, but some
296 * devices do not follow the USB specs to the letter.
298 * These are the events on the bus when a hub is attached:
299 * Get device and config descriptors (see attach code)
300 * Get hub descriptor (see above)
303 * wait for power to become stable
304 * (all below happens in explore code)
306 * clear C_PORT_CONNECTION
309 * if device connected
315 * proceed with device attachment
318 if (UHUB_IS_HIGH_SPEED(sc)) {
319 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
320 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
325 /* Set up data structures */
326 for (p = 0; p < nports; p++) {
327 struct usbd_port *up = &hub->ports[p];
331 if (dev->self_powered)
332 /* Self powered hub, give ports maximum current. */
333 up->power = USB_MAX_POWER;
335 up->power = USB_MIN_POWER;
337 if (UHUB_IS_HIGH_SPEED(sc)) {
338 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
345 /* XXX should check for none, individual, or ganged power? */
347 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
348 + USB_EXTRA_POWER_UP_TIME;
349 for (port = 1; port <= nports; port++) {
350 /* Turn the power on. */
351 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
353 device_printf(sc->sc_dev,
354 "port %d power on failed, %s\n", port,
356 DPRINTF(("usb_init_port: turn on port %d power\n", port));
357 /* Wait for stable power. */
358 usbd_delay_ms(dev, pwrdly);
361 /* The usual exploration will finish the setup. */
373 uhub_explore(usbd_device_handle dev)
375 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
376 struct uhub_softc *sc = dev->hub->hubsoftc;
377 struct usbd_port *up;
383 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
386 return (USBD_NOT_STARTED);
388 /* Ignore hubs that are too deep. */
389 if (dev->depth > USB_HUB_MAX_DEPTH)
390 return (USBD_TOO_DEEP);
392 for(port = 1; port <= hd->bNbrPorts; port++) {
393 up = &dev->hub->ports[port-1];
394 err = usbd_get_port_status(dev, port, &up->status);
396 DPRINTF(("uhub_explore: get port status failed, "
397 "error=%s\n", usbd_errstr(err)));
400 status = UGETW(up->status.wPortStatus);
401 change = UGETW(up->status.wPortChange);
402 DEVPRINTFN(3,(sc->sc_dev,
403 "uhub_explore: port %d status 0x%04x 0x%04x\n", port,
405 if (change & UPS_C_PORT_ENABLED) {
406 DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
407 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
408 if (change & UPS_C_CONNECT_STATUS) {
409 /* Ignore the port error if the device
411 } else if (status & UPS_PORT_ENABLED) {
412 device_printf(sc->sc_dev,
413 "illegal enable change, port %d\n", port);
415 /* Port error condition. */
416 if (up->restartcnt) /* no message first time */
417 device_printf(sc->sc_dev,
418 "port error, restarting port %d\n",
421 if (up->restartcnt++ < USBD_RESTART_MAX)
424 device_printf(sc->sc_dev,
425 "port error, giving up port %d\n",
429 if (!(change & UPS_C_CONNECT_STATUS)) {
430 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
432 /* No status change, just do recursive explore. */
433 if (up->device != NULL && up->device->hub != NULL)
434 up->device->hub->explore(up->device);
435 #if 0 && defined(DIAGNOSTIC)
436 if (up->device == NULL &&
437 (status & UPS_CURRENT_CONNECT_STATUS))
438 deivce_printf(sc->sc_dev,
439 "connected, no device\n");
444 /* We have a connect status change, handle it. */
446 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
447 dev->address, port));
448 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
449 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
451 * If there is already a device on the port the change status
452 * must mean that is has disconnected. Looking at the
453 * current connect status is not enough to figure this out
454 * since a new unit may have been connected before we handle
458 if (up->device != NULL) {
460 DPRINTF(("uhub_explore: device addr=%d disappeared "
461 "on port %d\n", up->device->address, port));
462 usb_disconnect_port(up, sc->sc_dev);
463 usbd_clear_port_feature(dev, port,
464 UHF_C_PORT_CONNECTION);
466 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
467 /* Nothing connected, just ignore it. */
468 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
475 if (!(status & UPS_PORT_POWER))
476 device_printf(sc->sc_dev,
477 "strange, connected port %d has no power\n", port);
479 /* Wait for maximum device power up time. */
480 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
482 /* Reset port, which implies enabling it. */
483 if (usbd_reset_port(dev, port, &up->status)) {
484 device_printf(sc->sc_dev, "port %d reset failed\n",
488 /* Get port status again, it might have changed during reset */
489 err = usbd_get_port_status(dev, port, &up->status);
491 DPRINTF(("uhub_explore: get port status failed, "
492 "error=%s\n", usbd_errstr(err)));
495 status = UGETW(up->status.wPortStatus);
496 change = UGETW(up->status.wPortChange);
497 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
498 /* Nothing connected, just ignore it. */
500 device_printf(sc->sc_dev,
501 "port %d, device disappeared after reset\n", port);
507 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
508 device_printf(sc->sc_dev,
509 "port %d, transaction translation not implemented,"
510 " low/full speed device ignored\n", port);
515 /* Figure out device speed */
516 if (status & UPS_HIGH_SPEED)
517 speed = USB_SPEED_HIGH;
518 else if (status & UPS_LOW_SPEED)
519 speed = USB_SPEED_LOW;
521 speed = USB_SPEED_FULL;
522 /* Get device info and set its address. */
523 err = usbd_new_device(sc->sc_dev, dev->bus,
524 dev->depth + 1, speed, port, up);
525 /* XXX retry a few times? */
527 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
528 "error=%s\n", usbd_errstr(err)));
529 /* Avoid addressing problems by disabling. */
530 /* usbd_reset_port(dev, port, &up->status); */
533 * The unit refused to accept a new address, or had
534 * some other serious problem. Since we cannot leave
535 * at 0 we have to disable the port instead.
537 device_printf(sc->sc_dev,
538 "device problem (%s), disabling port %d\n",
539 usbd_errstr(err), port);
540 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
542 /* The port set up succeeded, reset error count. */
546 up->device->hub->explore(up->device);
549 return (USBD_NORMAL_COMPLETION);
553 * Called from process context when the hub is gone.
554 * Detach all devices on active ports.
557 uhub_detach(device_t self)
559 struct uhub_softc *sc = device_get_softc(self);
560 struct usbd_hub *hub = sc->sc_hub->hub;
561 struct usbd_port *rup;
564 DPRINTF(("uhub_detach: sc=%port\n", sc));
565 if (hub == NULL) /* Must be partially working */
568 usbd_abort_pipe(sc->sc_ipipe);
569 usbd_close_pipe(sc->sc_ipipe);
571 nports = hub->hubdesc.bNbrPorts;
572 for(port = 0; port < nports; port++) {
573 rup = &hub->ports[port];
575 usb_disconnect_port(rup, self);
578 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
580 if (hub->ports[0].tt)
581 free(hub->ports[0].tt, M_USBDEV);
583 sc->sc_hub->hub = NULL;
589 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
592 struct uhub_softc *sc = device_get_softc(cbdev);
593 usbd_device_handle devhub = sc->sc_hub;
594 usbd_device_handle dev;
600 nports = devhub->hub->hubdesc.bNbrPorts;
601 for (port = 0; port < nports; port++) {
602 dev = devhub->hub->ports[port].device;
603 if (dev && dev->subdevs) {
604 for (i = 0; dev->subdevs[i]; i++) {
605 if (dev->subdevs[i] == child) {
606 if (dev->ifacenums == NULL) {
607 snprintf(buf, buflen,
610 snprintf(buf, buflen,
611 "port=%i interface=%i",
612 port, dev->ifacenums[i]);
619 DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
627 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
630 struct uhub_softc *sc = device_get_softc(cbdev);
631 usbd_device_handle devhub = sc->sc_hub;
632 usbd_device_handle dev;
633 struct usbd_interface *iface;
640 nports = devhub->hub->hubdesc.bNbrPorts;
641 for (port = 0; port < nports; port++) {
642 dev = devhub->hub->ports[port].device;
643 if (dev && dev->subdevs) {
644 for (i = 0; dev->subdevs[i]; i++) {
645 if (dev->subdevs[i] == child) {
651 DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
658 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, serial,
660 if (dev->ifacenums == NULL) {
661 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
662 "devclass=0x%02x devsubclass=0x%02x "
663 "release=0x%04x sernum=\"%s\"",
664 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
665 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
666 UGETW(dev->ddesc.bcdDevice), serial);
668 iface = &dev->ifaces[dev->ifacenums[i]];
669 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
670 "devclass=0x%02x devsubclass=0x%02x "
671 "release=0x%04x sernum=\"%s\" "
672 "intclass=0x%02x intsubclass=0x%02x",
673 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
674 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
675 UGETW(dev->ddesc.bcdDevice), serial,
676 iface->idesc->bInterfaceClass,
677 iface->idesc->bInterfaceSubClass);
685 * This an indication that some port has changed status.
686 * Notify the bus event handler thread that we need
687 * to be explored again.
690 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
692 struct uhub_softc *sc = addr;
694 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
695 if (status == USBD_STALLED)
696 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
697 else if (status == USBD_NORMAL_COMPLETION)
698 usb_needs_explore(sc->sc_hub);
701 MODULE_DEPEND(uhub, usb, 1, 1, 1);
702 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
703 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);