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.9 2006/10/25 20:55:52 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>
64 #include "usbdi_util.h"
67 #define UHUB_INTR_INTERVAL 255 /* ms */
70 #define DPRINTF(x) if (uhubdebug) logprintf x
71 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
73 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
74 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
75 &uhubdebug, 0, "uhub debug level");
82 USBBASEDEVICE sc_dev; /* base device */
83 usbd_device_handle sc_hub; /* USB device */
84 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
85 u_int8_t sc_status[1]; /* XXX more ports */
89 Static usbd_status uhub_explore(usbd_device_handle hub);
90 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
92 #if defined(__FreeBSD__) || defined(__DragonFly__)
93 Static bus_driver_added_t uhub_driver_added;
94 Static bus_child_detached_t uhub_child_detached;
99 * We need two attachment points:
100 * hub to usb and hub to hub
101 * Every other driver only connects to hubs
104 #if defined(__NetBSD__) || defined(__OpenBSD__)
105 USB_DECLARE_DRIVER(uhub);
107 /* Create the driver instance for the hub connected to hub case */
108 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
109 uhub_match, uhub_attach, uhub_detach, uhub_activate);
110 #elif defined(__FreeBSD__) || defined(__DragonFly__)
111 USB_DECLARE_DRIVER_INIT(uhub,
112 DEVMETHOD(bus_driver_added, uhub_driver_added),
113 DEVMETHOD(bus_child_detached, uhub_child_detached),
114 DEVMETHOD(device_suspend, bus_generic_suspend),
115 DEVMETHOD(device_resume, bus_generic_resume),
116 DEVMETHOD(device_shutdown, bus_generic_shutdown)
119 /* Create the driver instance for the hub connected to usb case. */
120 devclass_t uhubroot_devclass;
122 Static device_method_t uhubroot_methods[] = {
123 DEVMETHOD(device_probe, uhub_match),
124 DEVMETHOD(device_attach, uhub_attach),
126 /* detach is not allowed for a root hub */
127 DEVMETHOD(device_suspend, bus_generic_suspend),
128 DEVMETHOD(device_resume, bus_generic_resume),
129 DEVMETHOD(device_shutdown, bus_generic_shutdown),
133 Static driver_t uhubroot_driver = {
136 sizeof(struct uhub_softc)
142 USB_MATCH_START(uhub, uaa);
143 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
145 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
147 * The subclass for hubs seems to be 0 for some and 1 for others,
148 * so we just ignore the subclass.
150 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
151 return (UMATCH_DEVCLASS_DEVSUBCLASS);
152 return (UMATCH_NONE);
157 USB_ATTACH_START(uhub, sc, uaa);
158 usbd_device_handle dev = uaa->device;
161 struct usbd_hub *hub;
162 usb_device_request_t req;
163 usb_hub_descriptor_t hubdesc;
164 int p, port, nports, nremov, pwrdly;
165 usbd_interface_handle iface;
166 usb_endpoint_descriptor_t *ed;
168 devinfo = kmalloc(1024, M_TEMP, M_WAITOK);
169 DPRINTFN(1,("uhub_attach\n"));
171 usbd_devinfo(dev, 1, devinfo);
173 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
175 err = usbd_set_config_index(dev, 0, 1);
177 DPRINTF(("%s: configuration failed, error=%s\n",
178 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
179 kfree(devinfo, M_TEMP);
180 USB_ATTACH_ERROR_RETURN;
183 if (dev->depth > USB_HUB_MAX_DEPTH) {
184 printf("%s: hub depth (%d) exceeded, hub ignored\n",
185 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
186 kfree(devinfo, M_TEMP);
187 USB_ATTACH_ERROR_RETURN;
190 /* Get hub descriptor. */
191 req.bmRequestType = UT_READ_CLASS_DEVICE;
192 req.bRequest = UR_GET_DESCRIPTOR;
193 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
194 USETW(req.wIndex, 0);
195 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
196 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
197 err = usbd_do_request(dev, &req, &hubdesc);
198 nports = hubdesc.bNbrPorts;
199 if (!err && nports > 7) {
200 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
201 err = usbd_do_request(dev, &req, &hubdesc);
204 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
205 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
206 kfree(devinfo, M_TEMP);
207 USB_ATTACH_ERROR_RETURN;
210 for (nremov = 0, port = 1; port <= nports; port++)
211 if (!UHD_NOT_REMOV(&hubdesc, port))
213 printf("%s: %d port%s with %d removable, %s powered\n",
214 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
215 nremov, dev->self_powered ? "self" : "bus");
217 hub = kmalloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
220 dev->hub->hubsoftc = sc;
221 hub->explore = uhub_explore;
222 hub->hubdesc = hubdesc;
224 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
225 "parent->selfpowered=%d\n",
226 dev->self_powered, dev->powersrc->parent,
227 dev->powersrc->parent ?
228 dev->powersrc->parent->self_powered : 0));
230 if (!dev->self_powered && dev->powersrc->parent != NULL &&
231 !dev->powersrc->parent->self_powered) {
232 printf("%s: bus powered hub connected to bus powered hub, "
233 "ignored\n", USBDEVNAME(sc->sc_dev));
237 /* Set up interrupt pipe. */
238 err = usbd_device2interface_handle(dev, 0, &iface);
240 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
243 ed = usbd_interface2endpoint_descriptor(iface, 0);
245 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
248 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
249 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
253 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
254 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
255 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
257 printf("%s: cannot open interrupt pipe\n",
258 USBDEVNAME(sc->sc_dev));
262 /* Wait with power off for a while. */
263 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
265 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
268 * To have the best chance of success we do things in the exact same
269 * order as Windoze98. This should not be necessary, but some
270 * devices do not follow the USB specs to the letter.
272 * These are the events on the bus when a hub is attached:
273 * Get device and config descriptors (see attach code)
274 * Get hub descriptor (see above)
277 * wait for power to become stable
278 * (all below happens in explore code)
280 * clear C_PORT_CONNECTION
283 * if device connected
289 * proceed with device attachment
292 /* Set up data structures */
293 for (p = 0; p < nports; p++) {
294 struct usbd_port *up = &hub->ports[p];
298 if (dev->self_powered)
299 /* Self powered hub, give ports maximum current. */
300 up->power = USB_MAX_POWER;
302 up->power = USB_MIN_POWER;
305 /* XXX should check for none, individual, or ganged power? */
307 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
308 + USB_EXTRA_POWER_UP_TIME;
309 for (port = 1; port <= nports; port++) {
310 /* Turn the power on. */
311 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
313 printf("%s: port %d power on failed, %s\n",
314 USBDEVNAME(sc->sc_dev), port,
316 DPRINTF(("usb_init_port: turn on port %d power\n", port));
317 /* Wait for stable power. */
318 usbd_delay_ms(dev, pwrdly);
321 /* The usual exploration will finish the setup. */
325 USB_ATTACH_SUCCESS_RETURN;
328 kfree(hub, M_USBDEV);
329 kfree(devinfo, M_TEMP);
331 USB_ATTACH_ERROR_RETURN;
335 uhub_explore(usbd_device_handle dev)
337 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
338 struct uhub_softc *sc = dev->hub->hubsoftc;
339 struct usbd_port *up;
345 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
348 return (USBD_NOT_STARTED);
350 /* Ignore hubs that are too deep. */
351 if (dev->depth > USB_HUB_MAX_DEPTH)
352 return (USBD_TOO_DEEP);
354 for(port = 1; port <= hd->bNbrPorts; port++) {
355 up = &dev->hub->ports[port-1];
356 err = usbd_get_port_status(dev, port, &up->status);
358 DPRINTF(("uhub_explore: get port status failed, "
359 "error=%s\n", usbd_errstr(err)));
362 status = UGETW(up->status.wPortStatus);
363 change = UGETW(up->status.wPortChange);
364 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
365 USBDEVNAME(sc->sc_dev), port, status, change));
366 if (change & UPS_C_PORT_ENABLED) {
367 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
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);
373 /* Port error condition. */
374 if (up->restartcnt) /* no message first time */
375 printf("%s: port error, restarting "
377 USBDEVNAME(sc->sc_dev), port);
379 if (up->restartcnt++ < USBD_RESTART_MAX)
382 printf("%s: port error, giving up "
384 USBDEVNAME(sc->sc_dev), port);
387 if (!(change & UPS_C_CONNECT_STATUS)) {
388 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
390 /* No status change, just do recursive explore. */
391 if (up->device != NULL && up->device->hub != NULL)
392 up->device->hub->explore(up->device);
393 #if 0 && defined(DIAGNOSTIC)
394 if (up->device == NULL &&
395 (status & UPS_CURRENT_CONNECT_STATUS))
396 printf("%s: connected, no device\n",
397 USBDEVNAME(sc->sc_dev));
402 /* We have a connect status change, handle it. */
404 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
405 dev->address, port));
406 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
407 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
409 * If there is already a device on the port the change status
410 * must mean that is has disconnected. Looking at the
411 * current connect status is not enough to figure this out
412 * since a new unit may have been connected before we handle
416 if (up->device != NULL) {
418 DPRINTF(("uhub_explore: device addr=%d disappeared "
419 "on port %d\n", up->device->address, port));
420 usb_disconnect_port(up, USBDEV(sc->sc_dev));
421 usbd_clear_port_feature(dev, port,
422 UHF_C_PORT_CONNECTION);
424 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
425 /* Nothing connected, just ignore it. */
426 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
433 if (!(status & UPS_PORT_POWER))
434 printf("%s: strange, connected port %d has no power\n",
435 USBDEVNAME(sc->sc_dev), port);
437 /* Wait for maximum device power up time. */
438 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
440 /* Reset port, which implies enabling it. */
441 if (usbd_reset_port(dev, port, &up->status)) {
442 printf("%s: port %d reset failed\n",
443 USBDEVNAME(sc->sc_dev), port);
446 /* Get port status again, it might have changed during reset */
447 err = usbd_get_port_status(dev, port, &up->status);
449 DPRINTF(("uhub_explore: get port status failed, "
450 "error=%s\n", usbd_errstr(err)));
453 status = UGETW(up->status.wPortStatus);
454 change = UGETW(up->status.wPortChange);
455 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
456 /* Nothing connected, just ignore it. */
458 printf("%s: port %d, device disappeared after reset\n",
459 USBDEVNAME(sc->sc_dev), port);
464 /* Figure out device speed */
465 if (status & UPS_HIGH_SPEED)
466 speed = USB_SPEED_HIGH;
467 else if (status & UPS_LOW_SPEED)
468 speed = USB_SPEED_LOW;
470 speed = USB_SPEED_FULL;
471 /* Get device info and set its address. */
472 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
473 dev->depth + 1, speed, port, up);
474 /* XXX retry a few times? */
476 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
477 "error=%s\n", usbd_errstr(err)));
478 /* Avoid addressing problems by disabling. */
479 /* usbd_reset_port(dev, port, &up->status); */
482 * The unit refused to accept a new address, or had
483 * some other serious problem. Since we cannot leave
484 * at 0 we have to disable the port instead.
486 printf("%s: device problem, disabling port %d\n",
487 USBDEVNAME(sc->sc_dev), port);
488 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
490 /* The port set up succeeded, reset error count. */
494 up->device->hub->explore(up->device);
497 return (USBD_NORMAL_COMPLETION);
500 #if defined(__NetBSD__) || defined(__OpenBSD__)
502 uhub_activate(device_ptr_t self, enum devact act)
504 struct uhub_softc *sc = (struct uhub_softc *)self;
505 struct usbd_hub *hub = sc->sc_hub->hub;
506 usbd_device_handle dev;
513 case DVACT_DEACTIVATE:
514 if (hub == NULL) /* malfunctioning hub */
516 nports = hub->hubdesc.bNbrPorts;
517 for(port = 0; port < nports; port++) {
518 dev = hub->ports[port].device;
519 if (dev != NULL && dev->subdevs != NULL) {
520 for (i = 0; dev->subdevs[i] != NULL; i++)
521 config_deactivate(dev->subdevs[i]);
531 * Called from process context when the hub is gone.
532 * Detach all devices on active ports.
536 USB_DETACH_START(uhub, sc);
537 struct usbd_hub *hub = sc->sc_hub->hub;
538 struct usbd_port *rup;
541 #if defined(__NetBSD__) || defined(__OpenBSD__)
542 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
543 #elif defined(__FreeBSD__) || defined(__DragonFly__)
544 DPRINTF(("uhub_detach: sc=%port\n", sc));
547 if (hub == NULL) /* Must be partially working */
550 usbd_abort_pipe(sc->sc_ipipe);
551 usbd_close_pipe(sc->sc_ipipe);
553 nports = hub->hubdesc.bNbrPorts;
554 for(port = 0; port < nports; port++) {
555 rup = &hub->ports[port];
557 usb_disconnect_port(rup, self);
560 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
563 kfree(hub, M_USBDEV);
564 sc->sc_hub->hub = NULL;
569 #if defined(__FreeBSD__) || defined(__DragonFly__)
570 /* Called when a device has been detached from it */
572 uhub_child_detached(device_t self, device_t child)
574 struct uhub_softc *sc = device_get_softc(self);
575 usbd_device_handle devhub = sc->sc_hub;
576 usbd_device_handle dev;
582 /* should never happen; children are only created after init */
583 panic("hub not fully initialised, but child deleted?");
585 nports = devhub->hub->hubdesc.bNbrPorts;
586 for (port = 0; port < nports; port++) {
587 dev = devhub->hub->ports[port].device;
588 if (dev && dev->subdevs) {
589 for (i = 0; dev->subdevs[i]; i++) {
590 if (dev->subdevs[i] == child) {
591 dev->subdevs[i] = NULL;
600 uhub_driver_added(device_t _dev, driver_t *_driver)
602 /* Don't do anything, as reprobing does not work currently. We should
603 * really call through to usbd_new_device or a function along those
604 * lines that reinitialises the device if it is not owned by any
605 * driver. But this is complicated. Manual replugging by the user is
616 * This an indication that some port has changed status.
617 * Notify the bus event handler thread that we need
618 * to be explored again.
621 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
623 struct uhub_softc *sc = addr;
625 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
626 if (status == USBD_STALLED)
627 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
628 else if (status == USBD_NORMAL_COMPLETION)
629 usb_needs_explore(sc->sc_hub);
632 #if defined(__FreeBSD__) || defined(__DragonFly__)
633 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
634 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);