1 /* $NetBSD: uhub.c,v 1.47 2000/09/24 02:08:38 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.21.2.7 2002/11/06 20:23:50 joe 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/docs.htm
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>
56 #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
71 static int uhubdebug = 0;
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_child_detached_t uhub_child_detached;
97 * We need two attachment points:
98 * hub to usb and hub to hub
99 * Every other driver only connects to hubs
102 #if defined(__NetBSD__) || defined(__OpenBSD__)
103 USB_DECLARE_DRIVER(uhub);
105 /* Create the driver instance for the hub connected to hub case */
106 struct cfattach uhub_uhub_ca = {
107 sizeof(struct uhub_softc), uhub_match, uhub_attach,
108 uhub_detach, uhub_activate
110 #elif defined(__FreeBSD__)
111 USB_DECLARE_DRIVER_INIT(uhub,
112 DEVMETHOD(bus_child_detached, uhub_child_detached),
113 DEVMETHOD(device_suspend, bus_generic_suspend),
114 DEVMETHOD(device_resume, bus_generic_resume),
115 DEVMETHOD(device_shutdown, bus_generic_shutdown)
118 /* Create the driver instance for the hub connected to usb case. */
119 devclass_t uhubroot_devclass;
121 Static device_method_t uhubroot_methods[] = {
122 DEVMETHOD(device_probe, uhub_match),
123 DEVMETHOD(device_attach, uhub_attach),
124 /* detach is not allowed for a root hub */
125 DEVMETHOD(device_suspend, bus_generic_suspend),
126 DEVMETHOD(device_resume, bus_generic_resume),
127 DEVMETHOD(device_shutdown, bus_generic_shutdown),
131 Static driver_t uhubroot_driver = {
134 sizeof(struct uhub_softc)
140 USB_MATCH_START(uhub, uaa);
141 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
143 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
145 * The subclass for hubs seems to be 0 for some and 1 for others,
146 * so we just ignore the subclass.
148 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
149 return (UMATCH_DEVCLASS_DEVSUBCLASS);
150 return (UMATCH_NONE);
155 USB_ATTACH_START(uhub, sc, uaa);
156 usbd_device_handle dev = uaa->device;
159 struct usbd_hub *hub;
160 usb_device_request_t req;
161 usb_hub_descriptor_t hubdesc;
162 int p, port, nports, nremov, pwrdly;
163 usbd_interface_handle iface;
164 usb_endpoint_descriptor_t *ed;
166 DPRINTFN(1,("uhub_attach\n"));
168 usbd_devinfo(dev, 1, devinfo);
170 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
172 err = usbd_set_config_index(dev, 0, 1);
174 DPRINTF(("%s: configuration failed, error=%s\n",
175 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
176 USB_ATTACH_ERROR_RETURN;
179 if (dev->depth > USB_HUB_MAX_DEPTH) {
180 printf("%s: hub depth (%d) exceeded, hub ignored\n",
181 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
182 USB_ATTACH_ERROR_RETURN;
185 /* Get hub descriptor. */
186 req.bmRequestType = UT_READ_CLASS_DEVICE;
187 req.bRequest = UR_GET_DESCRIPTOR;
188 USETW(req.wValue, 0);
189 USETW(req.wIndex, 0);
190 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
191 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
192 err = usbd_do_request(dev, &req, &hubdesc);
193 nports = hubdesc.bNbrPorts;
194 if (!err && nports > 7) {
195 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
196 err = usbd_do_request(dev, &req, &hubdesc);
199 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
200 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
201 USB_ATTACH_ERROR_RETURN;
204 for (nremov = 0, port = 1; port <= nports; port++)
205 if (!UHD_NOT_REMOV(&hubdesc, port))
207 printf("%s: %d port%s with %d removable, %s powered\n",
208 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
209 nremov, dev->self_powered ? "self" : "bus");
211 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
214 USB_ATTACH_ERROR_RETURN;
216 dev->hub->hubsoftc = sc;
217 hub->explore = uhub_explore;
218 hub->hubdesc = hubdesc;
220 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
221 "parent->selfpowered=%d\n",
222 dev->self_powered, dev->powersrc->parent,
223 dev->powersrc->parent ?
224 dev->powersrc->parent->self_powered : 0));
226 if (!dev->self_powered && dev->powersrc->parent != NULL &&
227 !dev->powersrc->parent->self_powered) {
228 printf("%s: bus powered hub connected to bus powered hub, "
229 "ignored\n", USBDEVNAME(sc->sc_dev));
233 /* Set up interrupt pipe. */
234 err = usbd_device2interface_handle(dev, 0, &iface);
236 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
239 ed = usbd_interface2endpoint_descriptor(iface, 0);
241 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
244 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
245 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
249 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
250 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
251 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
253 printf("%s: cannot open interrupt pipe\n",
254 USBDEVNAME(sc->sc_dev));
258 /* Wait with power off for a while. */
259 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
262 * To have the best chance of success we do things in the exact same
263 * order as Windoze98. This should not be necessary, but some
264 * devices do not follow the USB specs to the letter.
266 * These are the events on the bus when a hub is attached:
267 * Get device and config descriptors (see attach code)
268 * Get hub descriptor (see above)
271 * wait for power to become stable
272 * (all below happens in explore code)
274 * clear C_PORT_CONNECTION
277 * if device connected
282 * proceed with device attachment
285 /* Set up data structures */
286 for (p = 0; p < nports; p++) {
287 struct usbd_port *up = &hub->ports[p];
291 if (dev->self_powered)
292 /* Self powered hub, give ports maximum current. */
293 up->power = USB_MAX_POWER;
295 up->power = USB_MIN_POWER;
298 /* XXX should check for none, individual, or ganged power? */
300 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
301 + USB_EXTRA_POWER_UP_TIME;
302 for (port = 1; port <= nports; port++) {
303 /* Turn the power on. */
304 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
306 printf("%s: port %d power on failed, %s\n",
307 USBDEVNAME(sc->sc_dev), port,
309 DPRINTF(("usb_init_port: turn on port %d power\n", port));
310 /* Wait for stable power. */
311 usbd_delay_ms(dev, pwrdly);
314 /* The usual exploration will finish the setup. */
318 USB_ATTACH_SUCCESS_RETURN;
323 USB_ATTACH_ERROR_RETURN;
327 uhub_explore(usbd_device_handle dev)
329 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
330 struct uhub_softc *sc = dev->hub->hubsoftc;
331 struct usbd_port *up;
336 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
339 return (USBD_NOT_STARTED);
341 /* Ignore hubs that are too deep. */
342 if (dev->depth > USB_HUB_MAX_DEPTH)
343 return (USBD_TOO_DEEP);
345 for(port = 1; port <= hd->bNbrPorts; port++) {
346 up = &dev->hub->ports[port-1];
347 err = usbd_get_port_status(dev, port, &up->status);
349 DPRINTF(("uhub_explore: get port status failed, "
350 "error=%s\n", usbd_errstr(err)));
353 status = UGETW(up->status.wPortStatus);
354 change = UGETW(up->status.wPortChange);
355 DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n",
356 port, status, change));
357 if (change & UPS_C_PORT_ENABLED) {
358 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
359 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
360 if (status & UPS_PORT_ENABLED) {
361 printf("%s: illegal enable change, port %d\n",
362 USBDEVNAME(sc->sc_dev), port);
364 /* Port error condition. */
365 if (up->restartcnt) /* no message first time */
366 printf("%s: port error, restarting "
368 USBDEVNAME(sc->sc_dev), port);
370 if (up->restartcnt++ < USBD_RESTART_MAX)
373 printf("%s: port error, giving up "
375 USBDEVNAME(sc->sc_dev), port);
378 if (!(change & UPS_C_CONNECT_STATUS)) {
379 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
381 /* No status change, just do recursive explore. */
382 if (up->device && up->device->hub)
383 up->device->hub->explore(up->device);
387 /* We have a connect status change, handle it. */
389 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
390 dev->address, port));
391 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
392 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
394 * If there is already a device on the port the change status
395 * must mean that is has disconnected. Looking at the
396 * current connect status is not enough to figure this out
397 * since a new unit may have been connected before we handle
401 if (up->device != NULL) {
403 DPRINTF(("uhub_explore: device addr=%d disappeared "
404 "on port %d\n", up->device->address, port));
405 usb_disconnect_port(up, USBDEV(sc->sc_dev));
406 usbd_clear_port_feature(dev, port,
407 UHF_C_PORT_CONNECTION);
409 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
410 /* Nothing connected, just ignore it. */
411 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
418 if (!(status & UPS_PORT_POWER))
419 printf("%s: strange, connected port %d has no power\n",
420 USBDEVNAME(sc->sc_dev), port);
422 /* Wait for maximum device power up time. */
423 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
425 /* Reset port, which implies enabling it. */
426 if (usbd_reset_port(dev, port, &up->status)) {
427 printf("uhub_explore: port=%d reset failed\n",
432 /* Get device info and set its address. */
433 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
434 dev->depth + 1, status & UPS_LOW_SPEED,
436 /* XXX retry a few times? */
438 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
439 "error=%s\n", usbd_errstr(err)));
440 /* Avoid addressing problems by disabling. */
441 /* usbd_reset_port(dev, port, &up->status); */
444 * The unit refused to accept a new address, or had
445 * some other serious problem. Since we cannot leave
446 * at 0 we have to disable the port instead.
448 printf("%s: device problem, disabling port %d\n",
449 USBDEVNAME(sc->sc_dev), port);
450 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
452 /* The port set up succeeded, reset error count. */
456 up->device->hub->explore(up->device);
459 return (USBD_NORMAL_COMPLETION);
462 #if defined(__NetBSD__) || defined(__OpenBSD__)
464 uhub_activate(device_ptr_t self, enum devact act)
466 struct uhub_softc *sc = (struct uhub_softc *)self;
467 struct usbd_hub *hub = sc->sc_hub->hub;
468 usbd_device_handle dev;
476 case DVACT_DEACTIVATE:
477 if (hub == NULL) /* malfunctioning hub */
479 nports = hub->hubdesc.bNbrPorts;
480 for(port = 0; port < nports; port++) {
481 dev = hub->ports[port].device;
482 if (dev != NULL && dev->subdevs != NULL) {
483 for (i = 0; dev->subdevs[i] != NULL; i++)
484 config_deactivate(dev->subdevs[i]);
494 * Called from process context when the hub is gone.
495 * Detach all devices on active ports.
499 USB_DETACH_START(uhub, sc);
500 struct usbd_hub *hub = sc->sc_hub->hub;
501 struct usbd_port *rup;
504 #if defined(__NetBSD__) || defined(__OpenBSD__)
505 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
506 #elif defined(__FreeBSD__)
507 DPRINTF(("uhub_detach: sc=%port\n", sc));
510 if (hub == NULL) /* Must be partially working */
513 usbd_abort_pipe(sc->sc_ipipe);
514 usbd_close_pipe(sc->sc_ipipe);
516 nports = hub->hubdesc.bNbrPorts;
517 for(port = 0; port < nports; port++) {
518 rup = &hub->ports[port];
520 usb_disconnect_port(rup, self);
525 sc->sc_hub->hub = NULL;
530 #if defined(__FreeBSD__)
531 /* Called when a device has been detached from it */
533 uhub_child_detached(device_t self, device_t child)
535 struct uhub_softc *sc = device_get_softc(self);
536 usbd_device_handle devhub = sc->sc_hub;
537 usbd_device_handle dev;
543 /* should never happen; children are only created after init */
544 panic("hub not fully initialised, but child deleted?");
546 nports = devhub->hub->hubdesc.bNbrPorts;
547 for (port = 0; port < nports; port++) {
548 dev = devhub->hub->ports[port].device;
549 if (dev && dev->subdevs) {
550 for (i = 0; dev->subdevs[i]; i++) {
551 if (dev->subdevs[i] == child) {
552 dev->subdevs[i] = NULL;
564 * This an indication that some port has changed status.
565 * Notify the bus event handler thread that we need
566 * to be explored again.
569 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
571 struct uhub_softc *sc = addr;
573 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
574 if (status != USBD_NORMAL_COMPLETION)
575 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
577 usb_needs_explore(sc->sc_hub->bus);
580 #if defined(__FreeBSD__)
581 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
582 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);