2 * $NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $
3 * $FreeBSD: src/sys/dev/usb/uhub.c,v 1.54 2003/08/24 17:55:55 obrien Exp $
4 * $DragonFly: src/sys/bus/usb/uhub.c,v 1.6 2004/03/12 03:43:06 dillon Exp $
8 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Lennart Augustsson (lennart@augustsson.net) at
13 * Carlstedt Research & Technology.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
45 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
55 #elif defined(__FreeBSD__) || defined(__DragonFly__)
56 #include <sys/module.h>
60 #include <sys/sysctl.h>
62 #include <machine/bus.h>
66 #include "usbdi_util.h"
69 #define UHUB_INTR_INTERVAL 255 /* ms */
72 #define DPRINTF(x) if (uhubdebug) logprintf x
73 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
75 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
76 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
77 &uhubdebug, 0, "uhub debug level");
84 USBBASEDEVICE sc_dev; /* base device */
85 usbd_device_handle sc_hub; /* USB device */
86 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
87 u_int8_t sc_status[1]; /* XXX more ports */
91 Static usbd_status uhub_explore(usbd_device_handle hub);
92 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
94 #if defined(__FreeBSD__) || defined(__DragonFly__)
95 Static bus_driver_added_t uhub_driver_added;
96 Static bus_child_detached_t uhub_child_detached;
101 * We need two attachment points:
102 * hub to usb and hub to hub
103 * Every other driver only connects to hubs
106 #if defined(__NetBSD__) || defined(__OpenBSD__)
107 USB_DECLARE_DRIVER(uhub);
109 /* Create the driver instance for the hub connected to hub case */
110 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
111 uhub_match, uhub_attach, uhub_detach, uhub_activate);
112 #elif defined(__FreeBSD__) || defined(__DragonFly__)
113 USB_DECLARE_DRIVER_INIT(uhub,
114 DEVMETHOD(bus_driver_added, uhub_driver_added),
115 DEVMETHOD(bus_child_detached, uhub_child_detached),
116 DEVMETHOD(device_suspend, bus_generic_suspend),
117 DEVMETHOD(device_resume, bus_generic_resume),
118 DEVMETHOD(device_shutdown, bus_generic_shutdown)
121 /* Create the driver instance for the hub connected to usb case. */
122 devclass_t uhubroot_devclass;
124 Static device_method_t uhubroot_methods[] = {
125 DEVMETHOD(device_probe, uhub_match),
126 DEVMETHOD(device_attach, uhub_attach),
128 /* detach is not allowed for a root hub */
129 DEVMETHOD(device_suspend, bus_generic_suspend),
130 DEVMETHOD(device_resume, bus_generic_resume),
131 DEVMETHOD(device_shutdown, bus_generic_shutdown),
135 Static driver_t uhubroot_driver = {
138 sizeof(struct uhub_softc)
144 USB_MATCH_START(uhub, uaa);
145 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
147 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
149 * The subclass for hubs seems to be 0 for some and 1 for others,
150 * so we just ignore the subclass.
152 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
153 return (UMATCH_DEVCLASS_DEVSUBCLASS);
154 return (UMATCH_NONE);
159 USB_ATTACH_START(uhub, sc, uaa);
160 usbd_device_handle dev = uaa->device;
163 struct usbd_hub *hub;
164 usb_device_request_t req;
165 usb_hub_descriptor_t hubdesc;
166 int p, port, nports, nremov, pwrdly;
167 usbd_interface_handle iface;
168 usb_endpoint_descriptor_t *ed;
170 devinfo = malloc(1024, M_TEMP, M_WAITOK);
171 DPRINTFN(1,("uhub_attach\n"));
173 usbd_devinfo(dev, 1, devinfo);
175 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
177 err = usbd_set_config_index(dev, 0, 1);
179 DPRINTF(("%s: configuration failed, error=%s\n",
180 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
181 free(devinfo, M_TEMP);
182 USB_ATTACH_ERROR_RETURN;
185 if (dev->depth > USB_HUB_MAX_DEPTH) {
186 printf("%s: hub depth (%d) exceeded, hub ignored\n",
187 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
188 free(devinfo, M_TEMP);
189 USB_ATTACH_ERROR_RETURN;
192 /* Get hub descriptor. */
193 req.bmRequestType = UT_READ_CLASS_DEVICE;
194 req.bRequest = UR_GET_DESCRIPTOR;
195 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
196 USETW(req.wIndex, 0);
197 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
198 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
199 err = usbd_do_request(dev, &req, &hubdesc);
200 nports = hubdesc.bNbrPorts;
201 if (!err && nports > 7) {
202 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
203 err = usbd_do_request(dev, &req, &hubdesc);
206 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
207 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
208 free(devinfo, M_TEMP);
209 USB_ATTACH_ERROR_RETURN;
212 for (nremov = 0, port = 1; port <= nports; port++)
213 if (!UHD_NOT_REMOV(&hubdesc, port))
215 printf("%s: %d port%s with %d removable, %s powered\n",
216 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
217 nremov, dev->self_powered ? "self" : "bus");
219 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
222 dev->hub->hubsoftc = sc;
223 hub->explore = uhub_explore;
224 hub->hubdesc = hubdesc;
226 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
227 "parent->selfpowered=%d\n",
228 dev->self_powered, dev->powersrc->parent,
229 dev->powersrc->parent ?
230 dev->powersrc->parent->self_powered : 0));
232 if (!dev->self_powered && dev->powersrc->parent != NULL &&
233 !dev->powersrc->parent->self_powered) {
234 printf("%s: bus powered hub connected to bus powered hub, "
235 "ignored\n", USBDEVNAME(sc->sc_dev));
239 /* Set up interrupt pipe. */
240 err = usbd_device2interface_handle(dev, 0, &iface);
242 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
245 ed = usbd_interface2endpoint_descriptor(iface, 0);
247 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
250 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
251 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
255 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
256 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
257 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
259 printf("%s: cannot open interrupt pipe\n",
260 USBDEVNAME(sc->sc_dev));
264 /* Wait with power off for a while. */
265 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
267 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
270 * To have the best chance of success we do things in the exact same
271 * order as Windoze98. This should not be necessary, but some
272 * devices do not follow the USB specs to the letter.
274 * These are the events on the bus when a hub is attached:
275 * Get device and config descriptors (see attach code)
276 * Get hub descriptor (see above)
279 * wait for power to become stable
280 * (all below happens in explore code)
282 * clear C_PORT_CONNECTION
285 * if device connected
291 * proceed with device attachment
294 /* Set up data structures */
295 for (p = 0; p < nports; p++) {
296 struct usbd_port *up = &hub->ports[p];
300 if (dev->self_powered)
301 /* Self powered hub, give ports maximum current. */
302 up->power = USB_MAX_POWER;
304 up->power = USB_MIN_POWER;
307 /* XXX should check for none, individual, or ganged power? */
309 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
310 + USB_EXTRA_POWER_UP_TIME;
311 for (port = 1; port <= nports; port++) {
312 /* Turn the power on. */
313 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
315 printf("%s: port %d power on failed, %s\n",
316 USBDEVNAME(sc->sc_dev), port,
318 DPRINTF(("usb_init_port: turn on port %d power\n", port));
319 /* Wait for stable power. */
320 usbd_delay_ms(dev, pwrdly);
323 /* The usual exploration will finish the setup. */
327 USB_ATTACH_SUCCESS_RETURN;
331 free(devinfo, M_TEMP);
333 USB_ATTACH_ERROR_RETURN;
337 uhub_explore(usbd_device_handle dev)
339 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
340 struct uhub_softc *sc = dev->hub->hubsoftc;
341 struct usbd_port *up;
347 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
350 return (USBD_NOT_STARTED);
352 /* Ignore hubs that are too deep. */
353 if (dev->depth > USB_HUB_MAX_DEPTH)
354 return (USBD_TOO_DEEP);
356 for(port = 1; port <= hd->bNbrPorts; port++) {
357 up = &dev->hub->ports[port-1];
358 err = usbd_get_port_status(dev, port, &up->status);
360 DPRINTF(("uhub_explore: get port status failed, "
361 "error=%s\n", usbd_errstr(err)));
364 status = UGETW(up->status.wPortStatus);
365 change = UGETW(up->status.wPortChange);
366 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
367 USBDEVNAME(sc->sc_dev), port, status, change));
368 if (change & UPS_C_PORT_ENABLED) {
369 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
370 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
371 if (status & UPS_PORT_ENABLED) {
372 printf("%s: illegal enable change, port %d\n",
373 USBDEVNAME(sc->sc_dev), port);
375 /* Port error condition. */
376 if (up->restartcnt) /* no message first time */
377 printf("%s: port error, restarting "
379 USBDEVNAME(sc->sc_dev), port);
381 if (up->restartcnt++ < USBD_RESTART_MAX)
384 printf("%s: port error, giving up "
386 USBDEVNAME(sc->sc_dev), port);
389 if (!(change & UPS_C_CONNECT_STATUS)) {
390 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
392 /* No status change, just do recursive explore. */
393 if (up->device != NULL && up->device->hub != NULL)
394 up->device->hub->explore(up->device);
395 #if 0 && defined(DIAGNOSTIC)
396 if (up->device == NULL &&
397 (status & UPS_CURRENT_CONNECT_STATUS))
398 printf("%s: connected, no device\n",
399 USBDEVNAME(sc->sc_dev));
404 /* We have a connect status change, handle it. */
406 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
407 dev->address, port));
408 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
409 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
411 * If there is already a device on the port the change status
412 * must mean that is has disconnected. Looking at the
413 * current connect status is not enough to figure this out
414 * since a new unit may have been connected before we handle
418 if (up->device != NULL) {
420 DPRINTF(("uhub_explore: device addr=%d disappeared "
421 "on port %d\n", up->device->address, port));
422 usb_disconnect_port(up, USBDEV(sc->sc_dev));
423 usbd_clear_port_feature(dev, port,
424 UHF_C_PORT_CONNECTION);
426 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
427 /* Nothing connected, just ignore it. */
428 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
435 if (!(status & UPS_PORT_POWER))
436 printf("%s: strange, connected port %d has no power\n",
437 USBDEVNAME(sc->sc_dev), port);
439 /* Wait for maximum device power up time. */
440 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
442 /* Reset port, which implies enabling it. */
443 if (usbd_reset_port(dev, port, &up->status)) {
444 printf("%s: port %d reset failed\n",
445 USBDEVNAME(sc->sc_dev), port);
448 /* Get port status again, it might have changed during reset */
449 err = usbd_get_port_status(dev, port, &up->status);
451 DPRINTF(("uhub_explore: get port status failed, "
452 "error=%s\n", usbd_errstr(err)));
455 status = UGETW(up->status.wPortStatus);
456 change = UGETW(up->status.wPortChange);
457 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
458 /* Nothing connected, just ignore it. */
460 printf("%s: port %d, device disappeared after reset\n",
461 USBDEVNAME(sc->sc_dev), port);
466 /* Figure out device speed */
467 if (status & UPS_HIGH_SPEED)
468 speed = USB_SPEED_HIGH;
469 else if (status & UPS_LOW_SPEED)
470 speed = USB_SPEED_LOW;
472 speed = USB_SPEED_FULL;
473 /* Get device info and set its address. */
474 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
475 dev->depth + 1, speed, port, up);
476 /* XXX retry a few times? */
478 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
479 "error=%s\n", usbd_errstr(err)));
480 /* Avoid addressing problems by disabling. */
481 /* usbd_reset_port(dev, port, &up->status); */
484 * The unit refused to accept a new address, or had
485 * some other serious problem. Since we cannot leave
486 * at 0 we have to disable the port instead.
488 printf("%s: device problem, disabling port %d\n",
489 USBDEVNAME(sc->sc_dev), port);
490 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
492 /* The port set up succeeded, reset error count. */
496 up->device->hub->explore(up->device);
499 return (USBD_NORMAL_COMPLETION);
502 #if defined(__NetBSD__) || defined(__OpenBSD__)
504 uhub_activate(device_ptr_t self, enum devact act)
506 struct uhub_softc *sc = (struct uhub_softc *)self;
507 struct usbd_hub *hub = sc->sc_hub->hub;
508 usbd_device_handle dev;
515 case DVACT_DEACTIVATE:
516 if (hub == NULL) /* malfunctioning hub */
518 nports = hub->hubdesc.bNbrPorts;
519 for(port = 0; port < nports; port++) {
520 dev = hub->ports[port].device;
521 if (dev != NULL && dev->subdevs != NULL) {
522 for (i = 0; dev->subdevs[i] != NULL; i++)
523 config_deactivate(dev->subdevs[i]);
533 * Called from process context when the hub is gone.
534 * Detach all devices on active ports.
538 USB_DETACH_START(uhub, sc);
539 struct usbd_hub *hub = sc->sc_hub->hub;
540 struct usbd_port *rup;
543 #if defined(__NetBSD__) || defined(__OpenBSD__)
544 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
545 #elif defined(__FreeBSD__) || defined(__DragonFly__)
546 DPRINTF(("uhub_detach: sc=%port\n", sc));
549 if (hub == NULL) /* Must be partially working */
552 usbd_abort_pipe(sc->sc_ipipe);
553 usbd_close_pipe(sc->sc_ipipe);
555 nports = hub->hubdesc.bNbrPorts;
556 for(port = 0; port < nports; port++) {
557 rup = &hub->ports[port];
559 usb_disconnect_port(rup, self);
562 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
566 sc->sc_hub->hub = NULL;
571 #if defined(__FreeBSD__) || defined(__DragonFly__)
572 /* Called when a device has been detached from it */
574 uhub_child_detached(device_t self, device_t child)
576 struct uhub_softc *sc = device_get_softc(self);
577 usbd_device_handle devhub = sc->sc_hub;
578 usbd_device_handle dev;
584 /* should never happen; children are only created after init */
585 panic("hub not fully initialised, but child deleted?");
587 nports = devhub->hub->hubdesc.bNbrPorts;
588 for (port = 0; port < nports; port++) {
589 dev = devhub->hub->ports[port].device;
590 if (dev && dev->subdevs) {
591 for (i = 0; dev->subdevs[i]; i++) {
592 if (dev->subdevs[i] == child) {
593 dev->subdevs[i] = NULL;
602 uhub_driver_added(device_t _dev, driver_t *_driver)
604 /* Don't do anything, as reprobing does not work currently. We should
605 * really call through to usbd_new_device or a function along those
606 * lines that reinitialises the device if it is not owned by any
607 * driver. But this is complicated. Manual replugging by the user is
618 * This an indication that some port has changed status.
619 * Notify the bus event handler thread that we need
620 * to be explored again.
623 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
625 struct uhub_softc *sc = addr;
627 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
628 if (status == USBD_STALLED)
629 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
630 else if (status == USBD_NORMAL_COMPLETION)
631 usb_needs_explore(sc->sc_hub);
634 #if defined(__FreeBSD__) || defined(__DragonFly__)
635 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
636 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);