1 /* $NetBSD: uhub.c,v 1.57 2001/11/20 16:08:37 augustss 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/data/usbspec.zip
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 #include <sys/device.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
57 #include <sys/sysctl.h>
59 #include <machine/bus.h>
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdivar.h>
66 #define UHUB_INTR_INTERVAL 255 /* ms */
69 #define DPRINTF(x) if (uhubdebug) logprintf x
70 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf 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");
81 USBBASEDEVICE sc_dev; /* base device */
82 usbd_device_handle sc_hub; /* USB device */
83 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
84 u_int8_t sc_status[1]; /* XXX more ports */
88 Static usbd_status uhub_explore(usbd_device_handle hub);
89 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
91 #if defined(__FreeBSD__)
92 Static bus_driver_added_t uhub_driver_added;
93 Static bus_child_detached_t uhub_child_detached;
98 * We need two attachment points:
99 * hub to usb and hub to hub
100 * Every other driver only connects to hubs
103 #if defined(__NetBSD__) || defined(__OpenBSD__)
104 USB_DECLARE_DRIVER(uhub);
106 /* Create the driver instance for the hub connected to hub case */
107 struct cfattach uhub_uhub_ca = {
108 sizeof(struct uhub_softc), uhub_match, uhub_attach,
109 uhub_detach, uhub_activate
111 #elif defined(__FreeBSD__)
112 USB_DECLARE_DRIVER_INIT(uhub,
113 DEVMETHOD(bus_driver_added, uhub_driver_added),
114 DEVMETHOD(bus_child_detached, uhub_child_detached),
115 DEVMETHOD(device_suspend, bus_generic_suspend),
116 DEVMETHOD(device_resume, bus_generic_resume),
117 DEVMETHOD(device_shutdown, bus_generic_shutdown)
120 /* Create the driver instance for the hub connected to usb case. */
121 devclass_t uhubroot_devclass;
123 Static device_method_t uhubroot_methods[] = {
124 DEVMETHOD(device_probe, uhub_match),
125 DEVMETHOD(device_attach, uhub_attach),
127 /* detach is not allowed for a root hub */
128 DEVMETHOD(device_suspend, bus_generic_suspend),
129 DEVMETHOD(device_resume, bus_generic_resume),
130 DEVMETHOD(device_shutdown, bus_generic_shutdown),
134 Static driver_t uhubroot_driver = {
137 sizeof(struct uhub_softc)
143 USB_MATCH_START(uhub, uaa);
144 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
146 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
148 * The subclass for hubs seems to be 0 for some and 1 for others,
149 * so we just ignore the subclass.
151 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
152 return (UMATCH_DEVCLASS_DEVSUBCLASS);
153 return (UMATCH_NONE);
158 USB_ATTACH_START(uhub, sc, uaa);
159 usbd_device_handle dev = uaa->device;
162 struct usbd_hub *hub;
163 usb_device_request_t req;
164 usb_hub_descriptor_t hubdesc;
165 int p, port, nports, nremov, pwrdly;
166 usbd_interface_handle iface;
167 usb_endpoint_descriptor_t *ed;
169 devinfo = malloc(1024, M_TEMP, M_NOWAIT);
170 if (devinfo == NULL) {
171 USB_ATTACH_ERROR_RETURN;
173 DPRINTFN(1,("uhub_attach\n"));
175 usbd_devinfo(dev, 1, devinfo);
177 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
179 err = usbd_set_config_index(dev, 0, 1);
181 DPRINTF(("%s: configuration failed, error=%s\n",
182 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
183 free(devinfo, M_TEMP);
184 USB_ATTACH_ERROR_RETURN;
187 if (dev->depth > USB_HUB_MAX_DEPTH) {
188 printf("%s: hub depth (%d) exceeded, hub ignored\n",
189 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
190 free(devinfo, M_TEMP);
191 USB_ATTACH_ERROR_RETURN;
194 /* Get hub descriptor. */
195 req.bmRequestType = UT_READ_CLASS_DEVICE;
196 req.bRequest = UR_GET_DESCRIPTOR;
197 USETW(req.wValue, 0);
198 USETW(req.wIndex, 0);
199 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
200 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
201 err = usbd_do_request(dev, &req, &hubdesc);
202 nports = hubdesc.bNbrPorts;
203 if (!err && nports > 7) {
204 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
205 err = usbd_do_request(dev, &req, &hubdesc);
208 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
209 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
210 free(devinfo, M_TEMP);
211 USB_ATTACH_ERROR_RETURN;
214 for (nremov = 0, port = 1; port <= nports; port++)
215 if (!UHD_NOT_REMOV(&hubdesc, port))
217 printf("%s: %d port%s with %d removable, %s powered\n",
218 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
219 nremov, dev->self_powered ? "self" : "bus");
221 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
224 free(devinfo, M_TEMP);
225 USB_ATTACH_ERROR_RETURN;
228 dev->hub->hubsoftc = sc;
229 hub->explore = uhub_explore;
230 hub->hubdesc = hubdesc;
232 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
233 "parent->selfpowered=%d\n",
234 dev->self_powered, dev->powersrc->parent,
235 dev->powersrc->parent ?
236 dev->powersrc->parent->self_powered : 0));
238 if (!dev->self_powered && dev->powersrc->parent != NULL &&
239 !dev->powersrc->parent->self_powered) {
240 printf("%s: bus powered hub connected to bus powered hub, "
241 "ignored\n", USBDEVNAME(sc->sc_dev));
245 /* Set up interrupt pipe. */
246 err = usbd_device2interface_handle(dev, 0, &iface);
248 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
251 ed = usbd_interface2endpoint_descriptor(iface, 0);
253 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
256 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
257 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
261 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
262 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
263 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
265 printf("%s: cannot open interrupt pipe\n",
266 USBDEVNAME(sc->sc_dev));
270 /* Wait with power off for a while. */
271 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
273 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
276 * To have the best chance of success we do things in the exact same
277 * order as Windoze98. This should not be necessary, but some
278 * devices do not follow the USB specs to the letter.
280 * These are the events on the bus when a hub is attached:
281 * Get device and config descriptors (see attach code)
282 * Get hub descriptor (see above)
285 * wait for power to become stable
286 * (all below happens in explore code)
288 * clear C_PORT_CONNECTION
291 * if device connected
297 * proceed with device attachment
300 /* Set up data structures */
301 for (p = 0; p < nports; p++) {
302 struct usbd_port *up = &hub->ports[p];
306 if (dev->self_powered)
307 /* Self powered hub, give ports maximum current. */
308 up->power = USB_MAX_POWER;
310 up->power = USB_MIN_POWER;
313 /* XXX should check for none, individual, or ganged power? */
315 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
316 + USB_EXTRA_POWER_UP_TIME;
317 for (port = 1; port <= nports; port++) {
318 /* Turn the power on. */
319 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
321 printf("%s: port %d power on failed, %s\n",
322 USBDEVNAME(sc->sc_dev), port,
324 DPRINTF(("usb_init_port: turn on port %d power\n", port));
325 /* Wait for stable power. */
326 usbd_delay_ms(dev, pwrdly);
329 /* The usual exploration will finish the setup. */
333 USB_ATTACH_SUCCESS_RETURN;
337 free(devinfo, M_TEMP);
339 USB_ATTACH_ERROR_RETURN;
343 uhub_explore(usbd_device_handle dev)
345 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
346 struct uhub_softc *sc = dev->hub->hubsoftc;
347 struct usbd_port *up;
353 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
356 return (USBD_NOT_STARTED);
358 /* Ignore hubs that are too deep. */
359 if (dev->depth > USB_HUB_MAX_DEPTH)
360 return (USBD_TOO_DEEP);
362 for(port = 1; port <= hd->bNbrPorts; port++) {
363 up = &dev->hub->ports[port-1];
364 err = usbd_get_port_status(dev, port, &up->status);
366 DPRINTF(("uhub_explore: get port status failed, "
367 "error=%s\n", usbd_errstr(err)));
370 status = UGETW(up->status.wPortStatus);
371 change = UGETW(up->status.wPortChange);
372 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
373 USBDEVNAME(sc->sc_dev), port, status, change));
374 if (change & UPS_C_PORT_ENABLED) {
375 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
376 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
377 if (status & UPS_PORT_ENABLED) {
378 printf("%s: illegal enable change, port %d\n",
379 USBDEVNAME(sc->sc_dev), port);
381 /* Port error condition. */
382 if (up->restartcnt) /* no message first time */
383 printf("%s: port error, restarting "
385 USBDEVNAME(sc->sc_dev), port);
387 if (up->restartcnt++ < USBD_RESTART_MAX)
390 printf("%s: port error, giving up "
392 USBDEVNAME(sc->sc_dev), port);
395 if (!(change & UPS_C_CONNECT_STATUS)) {
396 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
398 /* No status change, just do recursive explore. */
399 if (up->device != NULL && up->device->hub != NULL)
400 up->device->hub->explore(up->device);
401 #if 0 && defined(DIAGNOSTIC)
402 if (up->device == NULL &&
403 (status & UPS_CURRENT_CONNECT_STATUS))
404 printf("%s: connected, no device\n",
405 USBDEVNAME(sc->sc_dev));
410 /* We have a connect status change, handle it. */
412 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
413 dev->address, port));
414 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
415 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
417 * If there is already a device on the port the change status
418 * must mean that is has disconnected. Looking at the
419 * current connect status is not enough to figure this out
420 * since a new unit may have been connected before we handle
424 if (up->device != NULL) {
426 DPRINTF(("uhub_explore: device addr=%d disappeared "
427 "on port %d\n", up->device->address, port));
428 usb_disconnect_port(up, USBDEV(sc->sc_dev));
429 usbd_clear_port_feature(dev, port,
430 UHF_C_PORT_CONNECTION);
432 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
433 /* Nothing connected, just ignore it. */
434 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
441 if (!(status & UPS_PORT_POWER))
442 printf("%s: strange, connected port %d has no power\n",
443 USBDEVNAME(sc->sc_dev), port);
445 /* Wait for maximum device power up time. */
446 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
448 /* Reset port, which implies enabling it. */
449 if (usbd_reset_port(dev, port, &up->status)) {
450 printf("%s: port %d reset failed\n",
451 USBDEVNAME(sc->sc_dev), port);
454 /* Get port status again, it might have changed during reset */
455 err = usbd_get_port_status(dev, port, &up->status);
457 DPRINTF(("uhub_explore: get port status failed, "
458 "error=%s\n", usbd_errstr(err)));
461 status = UGETW(up->status.wPortStatus);
462 change = UGETW(up->status.wPortChange);
463 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
464 /* Nothing connected, just ignore it. */
466 printf("%s: port %d, device disappeared after reset\n",
467 USBDEVNAME(sc->sc_dev), port);
472 /* Figure out device speed */
473 if (status & UPS_HIGH_SPEED)
474 speed = USB_SPEED_HIGH;
475 else if (status & UPS_LOW_SPEED)
476 speed = USB_SPEED_LOW;
478 speed = USB_SPEED_FULL;
479 /* Get device info and set its address. */
480 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
481 dev->depth + 1, speed, port, up);
482 /* XXX retry a few times? */
484 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
485 "error=%s\n", usbd_errstr(err)));
486 /* Avoid addressing problems by disabling. */
487 /* usbd_reset_port(dev, port, &up->status); */
490 * The unit refused to accept a new address, or had
491 * some other serious problem. Since we cannot leave
492 * at 0 we have to disable the port instead.
494 printf("%s: device problem, disabling port %d\n",
495 USBDEVNAME(sc->sc_dev), port);
496 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
498 /* The port set up succeeded, reset error count. */
502 up->device->hub->explore(up->device);
505 return (USBD_NORMAL_COMPLETION);
508 #if defined(__NetBSD__) || defined(__OpenBSD__)
510 uhub_activate(device_ptr_t self, enum devact act)
512 struct uhub_softc *sc = (struct uhub_softc *)self;
513 struct usbd_hub *hub = sc->sc_hub->hub;
514 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__)
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__)
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__)
642 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
643 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);