1 /* $NetBSD: uhub.c,v 1.34 1999/11/18 23:32:29 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 (augustss@carlstedt.se) 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/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl
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>
58 #include <machine/bus.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
66 #define DPRINTF(x) if (uhubdebug) logprintf x
67 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
75 USBBASEDEVICE sc_dev; /* base device */
76 usbd_device_handle sc_hub; /* USB device */
77 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
78 u_int8_t sc_status[1]; /* XXX more ports */
82 static usbd_status uhub_init_port __P((struct usbd_port *));
83 static usbd_status uhub_explore __P((usbd_device_handle hub));
84 static void uhub_intr __P((usbd_xfer_handle, usbd_private_handle,usbd_status));
86 #if defined(__FreeBSD__)
87 static bus_child_detached_t uhub_child_detached;
92 * We need two attachment points:
93 * hub to usb and hub to hub
94 * Every other driver only connects to hubs
97 #if defined(__NetBSD__) || defined(__OpenBSD__)
98 USB_DECLARE_DRIVER(uhub);
100 /* Create the driver instance for the hub connected to hub case */
101 struct cfattach uhub_uhub_ca = {
102 sizeof(struct uhub_softc), uhub_match, uhub_attach,
103 uhub_detach, uhub_activate
105 #elif defined(__FreeBSD__)
106 USB_DECLARE_DRIVER_INIT(uhub,
107 DEVMETHOD(bus_child_detached, uhub_child_detached),
108 DEVMETHOD(device_suspend, bus_generic_suspend),
109 DEVMETHOD(device_resume, bus_generic_resume),
110 DEVMETHOD(device_shutdown, bus_generic_shutdown)
113 /* Create the driver instance for the hub connected to usb case. */
114 devclass_t uhubroot_devclass;
116 static device_method_t uhubroot_methods[] = {
117 DEVMETHOD(device_probe, uhub_match),
118 DEVMETHOD(device_attach, uhub_attach),
119 /* detach is not allowed for a root hub */
120 DEVMETHOD(device_suspend, bus_generic_suspend),
121 DEVMETHOD(device_resume, bus_generic_suspend),
122 DEVMETHOD(device_shutdown, bus_generic_shutdown),
126 static driver_t uhubroot_driver = {
129 sizeof(struct uhub_softc)
135 USB_MATCH_START(uhub, uaa);
136 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
138 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
140 * The subclass for hubs seems to be 0 for some and 1 for others,
141 * so we just ignore the subclass.
143 if (uaa->iface == NULL && dd->bDeviceClass == UCLASS_HUB)
144 return (UMATCH_DEVCLASS_DEVSUBCLASS);
145 return (UMATCH_NONE);
150 USB_ATTACH_START(uhub, sc, uaa);
151 usbd_device_handle dev = uaa->device;
154 struct usbd_hub *hub;
155 usb_device_request_t req;
156 usb_hub_descriptor_t hubdesc;
157 int p, port, nports, nremov;
158 usbd_interface_handle iface;
159 usb_endpoint_descriptor_t *ed;
161 DPRINTFN(1,("uhub_attach\n"));
163 usbd_devinfo(dev, 1, devinfo);
165 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
167 err = usbd_set_config_index(dev, 0, 1);
169 DPRINTF(("%s: configuration failed, error=%s\n",
170 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
171 USB_ATTACH_ERROR_RETURN;
174 if (dev->depth > USB_HUB_MAX_DEPTH) {
175 printf("%s: hub depth (%d) exceeded, hub ignored\n",
176 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
177 USB_ATTACH_ERROR_RETURN;
180 /* Get hub descriptor. */
181 req.bmRequestType = UT_READ_CLASS_DEVICE;
182 req.bRequest = UR_GET_DESCRIPTOR;
183 USETW(req.wValue, 0);
184 USETW(req.wIndex, 0);
185 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
186 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
187 err = usbd_do_request(dev, &req, &hubdesc);
188 nports = hubdesc.bNbrPorts;
189 if (!err && nports > 7) {
190 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
191 err = usbd_do_request(dev, &req, &hubdesc);
194 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
195 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
196 USB_ATTACH_ERROR_RETURN;
199 for (nremov = 0, port = 1; port <= nports; port++)
200 if (!UHD_NOT_REMOV(&hubdesc, port))
202 printf("%s: %d port%s with %d removable, %s powered\n",
203 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
204 nremov, dev->self_powered ? "self" : "bus");
206 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
209 USB_ATTACH_ERROR_RETURN;
211 dev->hub->hubsoftc = sc;
212 hub->explore = uhub_explore;
213 hub->hubdesc = hubdesc;
215 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
216 "parent->selfpowered=%d\n",
217 dev->self_powered, dev->powersrc->parent,
218 dev->powersrc->parent ?
219 dev->powersrc->parent->self_powered : 0));
221 if (!dev->self_powered && dev->powersrc->parent != NULL &&
222 !dev->powersrc->parent->self_powered) {
223 printf("%s: bus powered hub connected to bus powered hub, "
224 "ignored\n", USBDEVNAME(sc->sc_dev));
228 /* Set up interrupt pipe. */
229 err = usbd_device2interface_handle(dev, 0, &iface);
231 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
234 ed = usbd_interface2endpoint_descriptor(iface, 0);
236 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
239 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
240 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
244 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
245 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
246 sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL);
248 printf("%s: cannot open interrupt pipe\n",
249 USBDEVNAME(sc->sc_dev));
253 /* Wait with power off for a while. */
254 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
256 for (p = 0; p < nports; p++) {
257 struct usbd_port *up = &hub->ports[p];
261 err = uhub_init_port(up);
263 printf("%s: init of port %d failed\n",
264 USBDEVNAME(sc->sc_dev), up->portno);
268 USB_ATTACH_SUCCESS_RETURN;
273 USB_ATTACH_ERROR_RETURN;
278 struct usbd_port *up;
280 int port = up->portno;
281 usbd_device_handle dev = up->parent;
285 err = usbd_get_port_status(dev, port, &up->status);
288 pstatus = UGETW(up->status.wPortStatus);
289 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
291 port, pstatus, UGETW(up->status.wPortChange)));
292 if ((pstatus & UPS_PORT_POWER) == 0) {
293 /* Port lacks power, turn it on */
295 /* First let the device go through a good power cycle, */
296 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
299 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
300 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
303 /* then turn the power on. */
304 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
307 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
309 port, UGETW(up->status.wPortStatus),
310 UGETW(up->status.wPortChange)));
311 /* Wait for stable power. */
312 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
314 /* Get the port status again. */
315 err = usbd_get_port_status(dev, port, &up->status);
318 DPRINTF(("usb_init_port: after power on status=0x%04x "
320 UGETW(up->status.wPortStatus),
321 UGETW(up->status.wPortChange)));
324 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
325 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
326 usbd_get_port_status(dev, port, &up->status);
329 pstatus = UGETW(up->status.wPortStatus);
330 if ((pstatus & UPS_PORT_POWER) == 0)
331 printf("%s: port %d did not power up\n",
332 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
335 if (dev->self_powered)
336 /* Self powered hub, give ports maximum current. */
337 up->power = USB_MAX_POWER;
339 up->power = USB_MIN_POWER;
340 return (USBD_NORMAL_COMPLETION);
345 usbd_device_handle dev;
347 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
348 struct uhub_softc *sc = dev->hub->hubsoftc;
349 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: port %d status 0x%04x 0x%04x\n",
374 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++ < USBD_RESTART_MAX) {
384 printf("%s: port error, restarting "
386 USBDEVNAME(sc->sc_dev), port);
389 printf("%s: port error, giving up "
391 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 && up->device->hub)
400 up->device->hub->explore(up->device);
403 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
404 dev->address, port));
405 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
406 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
408 * If there is already a device on the port the change status
409 * must mean that is has disconnected. Looking at the
410 * current connect status is not enough to figure this out
411 * since a new unit may have been connected before we handle
415 if (up->device != NULL) {
417 DPRINTF(("uhub_explore: device addr=%d disappeared "
418 "on port %d\n", up->device->address, port));
419 usb_disconnect_port(up, USBDEV(sc->sc_dev));
420 usbd_clear_port_feature(dev, port,
421 UHF_C_PORT_CONNECTION);
423 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
424 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
432 /* Wait for maximum device power up time. */
433 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
435 /* Reset port, which implies enabling it. */
436 if (usbd_reset_port(dev, port, &up->status)) {
437 DPRINTF(("uhub_explore: port=%d reset failed\n",
442 /* Get device info and set its address. */
443 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
444 dev->depth + 1, status & UPS_LOW_SPEED,
446 /* XXX retry a few times? */
448 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
449 "error=%s\n", usbd_errstr(err)));
450 /* Avoid addressing problems by disabling. */
451 /* usbd_reset_port(dev, port, &up->status); */
454 * The unit refused to accept a new address, or had
455 * some other serious problem. Since we cannot leave
456 * at 0 we have to disable the port instead.
458 printf("%s: device problem, disabling port %d\n",
459 USBDEVNAME(sc->sc_dev), port);
460 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
461 /* Make sure we don't try to restart it infinitely. */
462 up->restartcnt = USBD_RESTART_MAX;
465 up->device->hub->explore(up->device);
468 return (USBD_NORMAL_COMPLETION);
471 #if defined(__NetBSD__) || defined(__OpenBSD__)
473 uhub_activate(self, act)
477 struct uhub_softc *sc = (struct uhub_softc *)self;
478 usbd_device_handle devhub = sc->sc_hub;
479 usbd_device_handle dev;
487 case DVACT_DEACTIVATE:
488 nports = devhub->hub->hubdesc.bNbrPorts;
489 for(port = 0; port < nports; port++) {
490 dev = devhub->hub->ports[port].device;
492 for (i = 0; dev->subdevs[i]; i++)
493 config_deactivate(dev->subdevs[i]);
503 * Called from process context when the hub is gone.
504 * Detach all devices on active ports.
508 USB_DETACH_START(uhub, sc);
509 usbd_device_handle dev = sc->sc_hub;
510 struct usbd_port *rup;
513 #if defined(__NetBSD__) || defined(__OpenBSD__)
514 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
515 #elif defined(__FreeBSD__)
516 DPRINTF(("uhub_detach: sc=%port\n", sc));
519 if (dev->hub == NULL) /* Must be partially working */
522 usbd_abort_pipe(sc->sc_ipipe);
523 usbd_close_pipe(sc->sc_ipipe);
525 nports = dev->hub->hubdesc.bNbrPorts;
526 for(port = 0; port < nports; port++) {
527 rup = &dev->hub->ports[port];
529 usb_disconnect_port(rup, self);
532 free(dev->hub, M_USBDEV);
538 #if defined(__FreeBSD__)
539 /* Called when a device has been detached from it */
541 uhub_child_detached(self, child)
545 struct uhub_softc *sc = device_get_softc(self);
546 usbd_device_handle devhub = sc->sc_hub;
547 usbd_device_handle dev;
553 /* should never happen; children are only created after init */
554 panic("hub not fully initialised, but child deleted?");
556 nports = devhub->hub->hubdesc.bNbrPorts;
557 for (port = 0; port < nports; port++) {
558 dev = devhub->hub->ports[port].device;
559 if (dev && dev->subdevs) {
560 for (i = 0; dev->subdevs[i]; i++) {
561 if (dev->subdevs[i] == child) {
562 dev->subdevs[i] = NULL;
574 * This an indication that some port has changed status.
575 * Notify the bus event handler thread that we need
576 * to be explored again.
579 uhub_intr(xfer, addr, status)
580 usbd_xfer_handle xfer;
581 usbd_private_handle addr;
584 struct uhub_softc *sc = addr;
586 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
587 if (status != USBD_NORMAL_COMPLETION)
588 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
590 usb_needs_explore(sc->sc_hub->bus);
593 #if defined(__FreeBSD__)
594 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
595 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);