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.5 2004/02/11 15:17:26 joerg 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_NOWAIT);
171 if (devinfo == NULL) {
172 USB_ATTACH_ERROR_RETURN;
174 DPRINTFN(1,("uhub_attach\n"));
176 usbd_devinfo(dev, 1, devinfo);
178 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
180 err = usbd_set_config_index(dev, 0, 1);
182 DPRINTF(("%s: configuration failed, error=%s\n",
183 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
184 free(devinfo, M_TEMP);
185 USB_ATTACH_ERROR_RETURN;
188 if (dev->depth > USB_HUB_MAX_DEPTH) {
189 printf("%s: hub depth (%d) exceeded, hub ignored\n",
190 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
191 free(devinfo, M_TEMP);
192 USB_ATTACH_ERROR_RETURN;
195 /* Get hub descriptor. */
196 req.bmRequestType = UT_READ_CLASS_DEVICE;
197 req.bRequest = UR_GET_DESCRIPTOR;
198 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
199 USETW(req.wIndex, 0);
200 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
201 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
202 err = usbd_do_request(dev, &req, &hubdesc);
203 nports = hubdesc.bNbrPorts;
204 if (!err && nports > 7) {
205 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
206 err = usbd_do_request(dev, &req, &hubdesc);
209 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
210 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
211 free(devinfo, M_TEMP);
212 USB_ATTACH_ERROR_RETURN;
215 for (nremov = 0, port = 1; port <= nports; port++)
216 if (!UHD_NOT_REMOV(&hubdesc, port))
218 printf("%s: %d port%s with %d removable, %s powered\n",
219 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
220 nremov, dev->self_powered ? "self" : "bus");
222 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
225 free(devinfo, M_TEMP);
226 USB_ATTACH_ERROR_RETURN;
229 dev->hub->hubsoftc = sc;
230 hub->explore = uhub_explore;
231 hub->hubdesc = hubdesc;
233 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
234 "parent->selfpowered=%d\n",
235 dev->self_powered, dev->powersrc->parent,
236 dev->powersrc->parent ?
237 dev->powersrc->parent->self_powered : 0));
239 if (!dev->self_powered && dev->powersrc->parent != NULL &&
240 !dev->powersrc->parent->self_powered) {
241 printf("%s: bus powered hub connected to bus powered hub, "
242 "ignored\n", USBDEVNAME(sc->sc_dev));
246 /* Set up interrupt pipe. */
247 err = usbd_device2interface_handle(dev, 0, &iface);
249 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
252 ed = usbd_interface2endpoint_descriptor(iface, 0);
254 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
257 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
258 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
262 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
263 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
264 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
266 printf("%s: cannot open interrupt pipe\n",
267 USBDEVNAME(sc->sc_dev));
271 /* Wait with power off for a while. */
272 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
274 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
277 * To have the best chance of success we do things in the exact same
278 * order as Windoze98. This should not be necessary, but some
279 * devices do not follow the USB specs to the letter.
281 * These are the events on the bus when a hub is attached:
282 * Get device and config descriptors (see attach code)
283 * Get hub descriptor (see above)
286 * wait for power to become stable
287 * (all below happens in explore code)
289 * clear C_PORT_CONNECTION
292 * if device connected
298 * proceed with device attachment
301 /* Set up data structures */
302 for (p = 0; p < nports; p++) {
303 struct usbd_port *up = &hub->ports[p];
307 if (dev->self_powered)
308 /* Self powered hub, give ports maximum current. */
309 up->power = USB_MAX_POWER;
311 up->power = USB_MIN_POWER;
314 /* XXX should check for none, individual, or ganged power? */
316 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
317 + USB_EXTRA_POWER_UP_TIME;
318 for (port = 1; port <= nports; port++) {
319 /* Turn the power on. */
320 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
322 printf("%s: port %d power on failed, %s\n",
323 USBDEVNAME(sc->sc_dev), port,
325 DPRINTF(("usb_init_port: turn on port %d power\n", port));
326 /* Wait for stable power. */
327 usbd_delay_ms(dev, pwrdly);
330 /* The usual exploration will finish the setup. */
334 USB_ATTACH_SUCCESS_RETURN;
338 free(devinfo, M_TEMP);
340 USB_ATTACH_ERROR_RETURN;
344 uhub_explore(usbd_device_handle dev)
346 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
347 struct uhub_softc *sc = dev->hub->hubsoftc;
348 struct usbd_port *up;
354 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
357 return (USBD_NOT_STARTED);
359 /* Ignore hubs that are too deep. */
360 if (dev->depth > USB_HUB_MAX_DEPTH)
361 return (USBD_TOO_DEEP);
363 for(port = 1; port <= hd->bNbrPorts; port++) {
364 up = &dev->hub->ports[port-1];
365 err = usbd_get_port_status(dev, port, &up->status);
367 DPRINTF(("uhub_explore: get port status failed, "
368 "error=%s\n", usbd_errstr(err)));
371 status = UGETW(up->status.wPortStatus);
372 change = UGETW(up->status.wPortChange);
373 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
374 USBDEVNAME(sc->sc_dev), port, status, change));
375 if (change & UPS_C_PORT_ENABLED) {
376 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
377 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
378 if (status & UPS_PORT_ENABLED) {
379 printf("%s: illegal enable change, port %d\n",
380 USBDEVNAME(sc->sc_dev), port);
382 /* Port error condition. */
383 if (up->restartcnt) /* no message first time */
384 printf("%s: port error, restarting "
386 USBDEVNAME(sc->sc_dev), port);
388 if (up->restartcnt++ < USBD_RESTART_MAX)
391 printf("%s: port error, giving up "
393 USBDEVNAME(sc->sc_dev), port);
396 if (!(change & UPS_C_CONNECT_STATUS)) {
397 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
399 /* No status change, just do recursive explore. */
400 if (up->device != NULL && up->device->hub != NULL)
401 up->device->hub->explore(up->device);
402 #if 0 && defined(DIAGNOSTIC)
403 if (up->device == NULL &&
404 (status & UPS_CURRENT_CONNECT_STATUS))
405 printf("%s: connected, no device\n",
406 USBDEVNAME(sc->sc_dev));
411 /* We have a connect status change, handle it. */
413 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
414 dev->address, port));
415 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
416 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
418 * If there is already a device on the port the change status
419 * must mean that is has disconnected. Looking at the
420 * current connect status is not enough to figure this out
421 * since a new unit may have been connected before we handle
425 if (up->device != NULL) {
427 DPRINTF(("uhub_explore: device addr=%d disappeared "
428 "on port %d\n", up->device->address, port));
429 usb_disconnect_port(up, USBDEV(sc->sc_dev));
430 usbd_clear_port_feature(dev, port,
431 UHF_C_PORT_CONNECTION);
433 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
434 /* Nothing connected, just ignore it. */
435 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
442 if (!(status & UPS_PORT_POWER))
443 printf("%s: strange, connected port %d has no power\n",
444 USBDEVNAME(sc->sc_dev), port);
446 /* Wait for maximum device power up time. */
447 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
449 /* Reset port, which implies enabling it. */
450 if (usbd_reset_port(dev, port, &up->status)) {
451 printf("%s: port %d reset failed\n",
452 USBDEVNAME(sc->sc_dev), port);
455 /* Get port status again, it might have changed during reset */
456 err = usbd_get_port_status(dev, port, &up->status);
458 DPRINTF(("uhub_explore: get port status failed, "
459 "error=%s\n", usbd_errstr(err)));
462 status = UGETW(up->status.wPortStatus);
463 change = UGETW(up->status.wPortChange);
464 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
465 /* Nothing connected, just ignore it. */
467 printf("%s: port %d, device disappeared after reset\n",
468 USBDEVNAME(sc->sc_dev), port);
473 /* Figure out device speed */
474 if (status & UPS_HIGH_SPEED)
475 speed = USB_SPEED_HIGH;
476 else if (status & UPS_LOW_SPEED)
477 speed = USB_SPEED_LOW;
479 speed = USB_SPEED_FULL;
480 /* Get device info and set its address. */
481 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
482 dev->depth + 1, speed, port, up);
483 /* XXX retry a few times? */
485 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
486 "error=%s\n", usbd_errstr(err)));
487 /* Avoid addressing problems by disabling. */
488 /* usbd_reset_port(dev, port, &up->status); */
491 * The unit refused to accept a new address, or had
492 * some other serious problem. Since we cannot leave
493 * at 0 we have to disable the port instead.
495 printf("%s: device problem, disabling port %d\n",
496 USBDEVNAME(sc->sc_dev), port);
497 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
499 /* The port set up succeeded, reset error count. */
503 up->device->hub->explore(up->device);
506 return (USBD_NORMAL_COMPLETION);
509 #if defined(__NetBSD__) || defined(__OpenBSD__)
511 uhub_activate(device_ptr_t self, enum devact act)
513 struct uhub_softc *sc = (struct uhub_softc *)self;
514 struct usbd_hub *hub = sc->sc_hub->hub;
515 usbd_device_handle dev;
522 case DVACT_DEACTIVATE:
523 if (hub == NULL) /* malfunctioning hub */
525 nports = hub->hubdesc.bNbrPorts;
526 for(port = 0; port < nports; port++) {
527 dev = hub->ports[port].device;
528 if (dev != NULL && dev->subdevs != NULL) {
529 for (i = 0; dev->subdevs[i] != NULL; i++)
530 config_deactivate(dev->subdevs[i]);
540 * Called from process context when the hub is gone.
541 * Detach all devices on active ports.
545 USB_DETACH_START(uhub, sc);
546 struct usbd_hub *hub = sc->sc_hub->hub;
547 struct usbd_port *rup;
550 #if defined(__NetBSD__) || defined(__OpenBSD__)
551 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
552 #elif defined(__FreeBSD__) || defined(__DragonFly__)
553 DPRINTF(("uhub_detach: sc=%port\n", sc));
556 if (hub == NULL) /* Must be partially working */
559 usbd_abort_pipe(sc->sc_ipipe);
560 usbd_close_pipe(sc->sc_ipipe);
562 nports = hub->hubdesc.bNbrPorts;
563 for(port = 0; port < nports; port++) {
564 rup = &hub->ports[port];
566 usb_disconnect_port(rup, self);
569 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
573 sc->sc_hub->hub = NULL;
578 #if defined(__FreeBSD__) || defined(__DragonFly__)
579 /* Called when a device has been detached from it */
581 uhub_child_detached(device_t self, device_t child)
583 struct uhub_softc *sc = device_get_softc(self);
584 usbd_device_handle devhub = sc->sc_hub;
585 usbd_device_handle dev;
591 /* should never happen; children are only created after init */
592 panic("hub not fully initialised, but child deleted?");
594 nports = devhub->hub->hubdesc.bNbrPorts;
595 for (port = 0; port < nports; port++) {
596 dev = devhub->hub->ports[port].device;
597 if (dev && dev->subdevs) {
598 for (i = 0; dev->subdevs[i]; i++) {
599 if (dev->subdevs[i] == child) {
600 dev->subdevs[i] = NULL;
609 uhub_driver_added(device_t _dev, driver_t *_driver)
611 /* Don't do anything, as reprobing does not work currently. We should
612 * really call through to usbd_new_device or a function along those
613 * lines that reinitialises the device if it is not owned by any
614 * driver. But this is complicated. Manual replugging by the user is
625 * This an indication that some port has changed status.
626 * Notify the bus event handler thread that we need
627 * to be explored again.
630 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
632 struct uhub_softc *sc = addr;
634 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
635 if (status == USBD_STALLED)
636 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
637 else if (status == USBD_NORMAL_COMPLETION)
638 usb_needs_explore(sc->sc_hub);
641 #if defined(__FreeBSD__) || defined(__DragonFly__)
642 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
643 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);