Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.85.8.3
      1 /*	$NetBSD: uhub.c,v 1.85.8.3 2007/06/21 15:21:40 itohy Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.85.8.3 2007/06/21 15:21:40 itohy Exp $");
     46 /* __FBSDID("$FreeBSD: src/sys/dev/usb/uhub.c,v 1.74 2007/02/03 21:11:11 flz Exp $"); */
     47 
     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>
     54 #include <sys/proc.h>
     55 #elif defined(__FreeBSD__)
     56 #include <sys/module.h>
     57 #include <sys/bus.h>
     58 #include <sys/lock.h>
     59 #include <sys/mutex.h>
     60 #include <sys/sysctl.h>
     61 #endif
     62 
     63 #include <machine/bus.h>
     64 
     65 #include <dev/usb/usb.h>
     66 #include <dev/usb/usbdi.h>
     67 #include <dev/usb/usbdi_util.h>
     68 #include <dev/usb/usbdivar.h>
     69 
     70 #ifdef UHUB_DEBUG
     71 #define DPRINTF(x)	if (uhubdebug) printf x
     72 #define DPRINTFN(n,x)	if (uhubdebug > (n)) printf x
     73 int	uhubdebug = 0;
     74 #if defined(__FreeBSD__)
     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");
     78 #endif
     79 #else
     80 #define DPRINTF(x)
     81 #define DPRINTFN(n,x)
     82 #endif
     83 
     84 struct uhub_softc {
     85 	USBBASEDEVICE		sc_dev;		/* base device */
     86 	usbd_device_handle	sc_hub;		/* USB device */
     87 	int			sc_proto;	/* device protocol */
     88 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     89 
     90 	/* XXX second buffer needed because we can't suspend pipes yet */
     91 	u_int8_t		*sc_statusbuf;
     92 	u_int8_t		*sc_status;
     93 	size_t			sc_statuslen;
     94 	int			sc_explorepending;
     95 
     96 	u_char			sc_running;
     97 };
     98 
     99 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
    100 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
    101 
    102 #define PORTSTAT_ISSET(sc, port) \
    103 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
    104 
    105 Static usbd_status uhub_explore(usbd_device_handle hub);
    106 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
    107 
    108 #if defined(__FreeBSD__)
    109 Static bus_child_location_str_t uhub_child_location_str;
    110 Static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
    111 #endif
    112 
    113 /*
    114  * We need two attachment points:
    115  * hub to usb and hub to hub
    116  * Every other driver only connects to hubs
    117  */
    118 
    119 #if defined(__NetBSD__) || defined(__OpenBSD__)
    120 USB_DECLARE_DRIVER(uhub);
    121 #elif defined(__FreeBSD__)
    122 USB_DECLARE_DRIVER_INIT(uhub,
    123 	DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
    124 	DEVMETHOD(bus_child_location_str, uhub_child_location_str),
    125 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
    126 	DEVMETHOD(device_suspend, bus_generic_suspend),
    127 	DEVMETHOD(device_resume, bus_generic_resume),
    128 	DEVMETHOD(device_shutdown, bus_generic_shutdown)
    129 	);
    130 
    131 /* Create the driver instance for the hub connected to usb case. */
    132 devclass_t uhubroot_devclass;
    133 
    134 Static device_method_t uhubroot_methods[] = {
    135 	DEVMETHOD(bus_child_location_str, uhub_child_location_str),
    136 	DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
    137 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
    138 
    139 	DEVMETHOD(device_probe, uhub_match),
    140 	DEVMETHOD(device_attach, uhub_attach),
    141 	DEVMETHOD(device_detach, uhub_detach),
    142 	DEVMETHOD(device_suspend, bus_generic_suspend),
    143 	DEVMETHOD(device_resume, bus_generic_resume),
    144 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
    145 	{0,0}
    146 };
    147 
    148 Static	driver_t uhubroot_driver = {
    149 	"uhub",
    150 	uhubroot_methods,
    151 	sizeof(struct uhub_softc)
    152 };
    153 #endif
    154 
    155 USB_MATCH(uhub)
    156 {
    157 	USB_MATCH_START(uhub, uaa);
    158 #ifndef USB_USE_IFATTACH
    159 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
    160 #endif /* USB_USE_IFATTACH */
    161 
    162 	DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
    163 	/*
    164 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    165 	 * so we just ignore the subclass.
    166 	 */
    167 #ifndef USB_USE_IFATTACH
    168 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
    169 #else
    170 	if (uaa->class == UDCLASS_HUB)
    171 #endif /* USB_USE_IFATTACH */
    172 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    173 	return (UMATCH_NONE);
    174 }
    175 
    176 USB_ATTACH(uhub)
    177 {
    178 	USB_ATTACH_START(uhub, sc, uaa);
    179 	usbd_device_handle dev = uaa->device;
    180 	char *devinfo;
    181 	usbd_status err;
    182 	struct usbd_hub *hub = NULL;
    183 	usb_device_request_t req;
    184 	usb_hub_descriptor_t hubdesc;
    185 	int p, port, nports, nremov, pwrdly;
    186 	usbd_interface_handle iface;
    187 	usb_endpoint_descriptor_t *ed;
    188 	struct usbd_tt *tts = NULL;
    189 
    190 	DPRINTFN(1,("uhub_attach\n"));
    191 	sc->sc_hub = dev;
    192 #ifndef USB_USE_IFATTACH
    193 	sc->sc_proto = sc->sc_hub->ddesc.bDeviceProtocol;
    194 #else
    195 	sc->sc_proto = uaa->proto;
    196 #endif /* USB_USE_IFATTACH */
    197 
    198 	devinfo = usbd_devinfo_alloc(dev, 1);
    199 	USB_ATTACH_SETUP;
    200 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    201 	usbd_devinfo_free(devinfo);
    202 
    203 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    204 		printf("%s: %s transaction translator%s\n",
    205 		       USBDEVNAME(sc->sc_dev),
    206 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    207 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    208 	}
    209 
    210 	err = usbd_set_config_index(dev, 0, 1);
    211 	if (err) {
    212 		DPRINTF(("%s: configuration failed, error=%s\n",
    213 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    214 		USB_ATTACH_ERROR_RETURN;
    215 	}
    216 
    217 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    218 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    219 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    220 		USB_ATTACH_ERROR_RETURN;
    221 	}
    222 
    223 	/* Get hub descriptor. */
    224 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    225 	req.bRequest = UR_GET_DESCRIPTOR;
    226 	USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
    227 	USETW(req.wIndex, 0);
    228 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    229 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    230 	err = usbd_do_request(dev, &req, &hubdesc);
    231 	nports = hubdesc.bNbrPorts;
    232 	if (!err && nports > 7) {
    233 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    234 		err = usbd_do_request(dev, &req, &hubdesc);
    235 	}
    236 	if (err) {
    237 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    238 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    239 		USB_ATTACH_ERROR_RETURN;
    240 	}
    241 
    242 	for (nremov = 0, port = 1; port <= nports; port++)
    243 		if (!UHD_NOT_REMOV(&hubdesc, port))
    244 			nremov++;
    245 	printf("%s: %d port%s with %d removable, %s powered\n",
    246 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    247 	       nremov, dev->self_powered ? "self" : "bus");
    248 
    249 	if (nports == 0) {
    250 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
    251 		goto bad;
    252 	}
    253 
    254 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    255 		     M_USBDEV, M_NOWAIT);
    256 	if (hub == NULL)
    257 		USB_ATTACH_ERROR_RETURN;
    258 	dev->hub = hub;
    259 	dev->hub->hubsoftc = sc;
    260 	hub->explore = uhub_explore;
    261 	hub->hubdesc = hubdesc;
    262 
    263 #if 0	/* This chack is disabled since the spec is unclear. */
    264 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
    265 		    "parent->selfpowered=%d\n",
    266 		 dev->self_powered, dev->powersrc->parent,
    267 		 dev->powersrc->parent ?
    268 		 dev->powersrc->parent->self_powered : 0));
    269 
    270 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
    271 	    !dev->powersrc->parent->self_powered) {
    272 		printf("%s: bus powered hub connected to bus powered hub, "
    273 		       "ignored\n", USBDEVNAME(sc->sc_dev));
    274 		goto bad;
    275 	}
    276 #endif
    277 
    278 	/* Set up interrupt pipe. */
    279 	err = usbd_device2interface_handle(dev, 0, &iface);
    280 	if (err) {
    281 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    282 		goto bad;
    283 	}
    284 
    285 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    286 		err = usbd_set_interface(iface, 1);
    287 		if (err)
    288 			printf("%s: can't enable multiple TTs\n",
    289 			       USBDEVNAME(sc->sc_dev));
    290 	}
    291 
    292 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    293 	if (ed == NULL) {
    294 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    295 		goto bad;
    296 	}
    297 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    298 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    299 		goto bad;
    300 	}
    301 
    302 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    303 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    304 	if (!sc->sc_statusbuf)
    305 		goto bad;
    306 	sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    307 	if (!sc->sc_status)
    308 		goto bad;
    309 
    310 	/* force initial scan */
    311 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    312 	sc->sc_explorepending = 1;
    313 
    314 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    315 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
    316 		  sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
    317 	if (err) {
    318 		printf("%s: cannot open interrupt pipe\n",
    319 		       USBDEVNAME(sc->sc_dev));
    320 		goto bad;
    321 	}
    322 
    323 	/* Wait with power off for a while. */
    324 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    325 
    326 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    327 
    328 	/*
    329 	 * To have the best chance of success we do things in the exact same
    330 	 * order as Windoze98.  This should not be necessary, but some
    331 	 * devices do not follow the USB specs to the letter.
    332 	 *
    333 	 * These are the events on the bus when a hub is attached:
    334 	 *  Get device and config descriptors (see attach code)
    335 	 *  Get hub descriptor (see above)
    336 	 *  For all ports
    337 	 *     turn on power
    338 	 *     wait for power to become stable
    339 	 * (all below happens in explore code)
    340 	 *  For all ports
    341 	 *     clear C_PORT_CONNECTION
    342 	 *  For all ports
    343 	 *     get port status
    344 	 *     if device connected
    345 	 *        wait 100 ms
    346 	 *        turn on reset
    347 	 *        wait
    348 	 *        clear C_PORT_RESET
    349 	 *        get port status
    350 	 *        proceed with device attachment
    351 	 */
    352 
    353 	if (UHUB_IS_HIGH_SPEED(sc)) {
    354 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    355 		    sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
    356 		if (!tts)
    357 			goto bad;
    358 	}
    359 
    360 	/* Set up data structures */
    361 	for (p = 0; p < nports; p++) {
    362 		struct usbd_port *up = &hub->ports[p];
    363 		up->device = NULL;
    364 		up->parent = dev;
    365 		up->portno = p+1;
    366 		if (dev->self_powered)
    367 			/* Self powered hub, give ports maximum current. */
    368 			up->power = USB_MAX_POWER;
    369 		else
    370 			up->power = USB_MIN_POWER;
    371 		up->restartcnt = 0;
    372 		up->reattach = 0;
    373 		if (UHUB_IS_HIGH_SPEED(sc)) {
    374 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    375 			up->tt->hub = hub;
    376 		} else {
    377 			up->tt = NULL;
    378 		}
    379 	}
    380 
    381 	/* XXX should check for none, individual, or ganged power? */
    382 
    383 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    384 	    + USB_EXTRA_POWER_UP_TIME;
    385 	for (port = 1; port <= nports; port++) {
    386 		/* Turn the power on. */
    387 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    388 		if (err)
    389 			printf("%s: port %d power on failed, %s\n",
    390 			       USBDEVNAME(sc->sc_dev), port,
    391 			       usbd_errstr(err));
    392 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    393 		/* Wait for stable power. */
    394 		usbd_delay_ms(dev, pwrdly);
    395 	}
    396 
    397 	/* The usual exploration will finish the setup. */
    398 
    399 	sc->sc_running = 1;
    400 
    401 	USB_ATTACH_SUCCESS_RETURN;
    402 
    403  bad:
    404 	if (sc->sc_status)
    405 		free(sc->sc_status, M_USBDEV);
    406 	if (hub)
    407 		free(hub, M_USBDEV);
    408 	dev->hub = NULL;
    409 	USB_ATTACH_ERROR_RETURN;
    410 }
    411 
    412 usbd_status
    413 uhub_explore(usbd_device_handle dev)
    414 {
    415 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    416 	struct uhub_softc *sc = dev->hub->hubsoftc;
    417 	struct usbd_port *up;
    418 	usbd_status err;
    419 	int speed;
    420 	int port;
    421 	int change, status, reconnect;
    422 
    423 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    424 
    425 	if (!sc->sc_running)
    426 		return (USBD_NOT_STARTED);
    427 
    428 	/* Ignore hubs that are too deep. */
    429 	if (dev->depth > USB_HUB_MAX_DEPTH)
    430 		return (USBD_TOO_DEEP);
    431 
    432 	for (port = 1; port <= hd->bNbrPorts; port++) {
    433 		up = &dev->hub->ports[port-1];
    434 
    435 		/* reattach is needed after firmware upload */
    436 		reconnect = up->reattach;
    437 		up->reattach = 0;
    438 
    439 		status = change = 0;
    440 
    441 		/* don't check if no change summary notification */
    442 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    443 			err = usbd_get_port_status(dev, port, &up->status);
    444 			if (err) {
    445 				DPRINTF(("%s: uhub_explore: get port stat failed, "
    446 					 "error=%s\n", USBDEVNAME(sc->sc_dev),
    447 					 usbd_errstr(err)));
    448 				continue;
    449 			}
    450 			status = UGETW(up->status.wPortStatus);
    451 			change = UGETW(up->status.wPortChange);
    452 		}
    453 		if (!change && !reconnect) {
    454 			/* No status change, just do recursive explore. */
    455 			if (up->device != NULL && up->device->hub != NULL)
    456 				up->device->hub->explore(up->device);
    457 			continue;
    458 		}
    459 
    460 		if (change & UPS_C_PORT_ENABLED) {
    461 			DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
    462 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    463 			if (change & UPS_C_CONNECT_STATUS) {
    464 				/* Ignore the port error if the device
    465 				   vanished. */
    466 			} else if (status & UPS_PORT_ENABLED) {
    467 				printf("%s: illegal enable change, port %d\n",
    468 				       USBDEVNAME(sc->sc_dev), port);
    469 			} else {
    470 				/* Port error condition. */
    471 				if (up->restartcnt) /* no message first time */
    472 					printf("%s: port error, restarting "
    473 					       "port %d\n",
    474 					       USBDEVNAME(sc->sc_dev), port);
    475 
    476 				if (up->restartcnt++ < USBD_RESTART_MAX)
    477 					goto disco;
    478 				else
    479 					printf("%s: port error, giving up "
    480 					       "port %d\n",
    481 					       USBDEVNAME(sc->sc_dev), port);
    482 			}
    483 		}
    484 
    485 		/* XXX handle overcurrent and resume events! */
    486 
    487 		if (!(change & UPS_C_CONNECT_STATUS))
    488 			continue;
    489 
    490 		/* We have a connect status change, handle it. */
    491 
    492 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    493 			 dev->address, port));
    494 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    495 		/*
    496 		 * If there is already a device on the port the change status
    497 		 * must mean that is has disconnected.  Looking at the
    498 		 * current connect status is not enough to figure this out
    499 		 * since a new unit may have been connected before we handle
    500 		 * the disconnect.
    501 		 */
    502 	disco:
    503 		if (up->device != NULL) {
    504 			/* Disconnected */
    505 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    506 				 "on port %d\n", up->device->address, port));
    507 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    508 			usbd_clear_port_feature(dev, port,
    509 						UHF_C_PORT_CONNECTION);
    510 		}
    511 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    512 			/* Nothing connected, just ignore it. */
    513 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    514 				    "_STATUS\n", port));
    515 			continue;
    516 		}
    517 
    518 		/* Connected */
    519 
    520 		if (!(status & UPS_PORT_POWER))
    521 			printf("%s: strange, connected port %d has no power\n",
    522 			       USBDEVNAME(sc->sc_dev), port);
    523 
    524 		/* Wait for maximum device power up time. */
    525 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    526 
    527 		/* Reset port, which implies enabling it. */
    528 		if (usbd_reset_port(dev, port, &up->status)) {
    529 			printf("%s: port %d reset failed\n",
    530 			       USBDEVNAME(sc->sc_dev), port);
    531 			continue;
    532 		}
    533 		/* Get port status again, it might have changed during reset */
    534 		err = usbd_get_port_status(dev, port, &up->status);
    535 		if (err) {
    536 			DPRINTF(("uhub_explore: get port status failed, "
    537 				 "error=%s\n", usbd_errstr(err)));
    538 			continue;
    539 		}
    540 		status = UGETW(up->status.wPortStatus);
    541 		change = UGETW(up->status.wPortChange);
    542 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    543 			/* Nothing connected, just ignore it. */
    544 #ifdef DIAGNOSTIC
    545 			printf("%s: port %d, device disappeared after reset\n",
    546 			       USBDEVNAME(sc->sc_dev), port);
    547 #endif
    548 			continue;
    549 		}
    550 
    551 		/* Figure out device speed */
    552 		if (status & UPS_HIGH_SPEED)
    553 			speed = USB_SPEED_HIGH;
    554 		else if (status & UPS_LOW_SPEED)
    555 			speed = USB_SPEED_LOW;
    556 		else
    557 			speed = USB_SPEED_FULL;
    558 		/* Get device info and set its address. */
    559 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    560 		    dev->depth + 1, speed, port, up);
    561 		/* XXX retry a few times? */
    562 		if (err) {
    563 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    564 				     "error=%s\n", usbd_errstr(err)));
    565 			/* Avoid addressing problems by disabling. */
    566 			/* usbd_reset_port(dev, port, &up->status); */
    567 
    568 			/*
    569 			 * The unit refused to accept a new address, or had
    570 			 * some other serious problem.  Since we cannot leave
    571 			 * at 0 we have to disable the port instead.
    572 			 */
    573 			printf("%s: device problem (%s), disabling port %d\n",
    574 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err), port);
    575 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    576 		} else {
    577 			/* The port set up succeeded, reset error count. */
    578 			up->restartcnt = 0;
    579 
    580 			if (up->device->hub)
    581 				up->device->hub->explore(up->device);
    582 		}
    583 	}
    584 	/* enable status change notifications again */
    585 	sc->sc_explorepending = 0;
    586 	return (USBD_NORMAL_COMPLETION);
    587 }
    588 
    589 #if defined(__NetBSD__) || defined(__OpenBSD__)
    590 int
    591 uhub_activate(device_ptr_t self, enum devact act)
    592 {
    593 	struct uhub_softc *sc = (struct uhub_softc *)self;
    594 	struct usbd_hub *hub = sc->sc_hub->hub;
    595 	usbd_device_handle dev;
    596 	int nports, port, i;
    597 
    598 	switch (act) {
    599 	case DVACT_ACTIVATE:
    600 		return (EOPNOTSUPP);
    601 
    602 	case DVACT_DEACTIVATE:
    603 		if (hub == NULL) /* malfunctioning hub */
    604 			break;
    605 		nports = hub->hubdesc.bNbrPorts;
    606 		for(port = 0; port < nports; port++) {
    607 			dev = hub->ports[port].device;
    608 			if (dev != NULL && dev->subdevs != NULL) {
    609 				for (i = 0; dev->subdevs[i] != NULL; i++)
    610 					config_deactivate(dev->subdevs[i]);
    611 			}
    612 		}
    613 		break;
    614 	}
    615 	return (0);
    616 }
    617 #endif
    618 
    619 /*
    620  * Called from process context when the hub is gone.
    621  * Detach all devices on active ports.
    622  */
    623 USB_DETACH(uhub)
    624 {
    625 	USB_DETACH_START(uhub, sc);
    626 	struct usbd_hub *hub = sc->sc_hub->hub;
    627 	struct usbd_port *rup;
    628 	int port, nports;
    629 
    630 #if defined(__NetBSD__) || defined(__OpenBSD__)
    631 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    632 #elif defined(__FreeBSD__)
    633 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    634 #endif
    635 
    636 	if (hub == NULL)		/* Must be partially working */
    637 		return (0);
    638 
    639 	usbd_abort_pipe(sc->sc_ipipe);
    640 	usbd_close_pipe(sc->sc_ipipe);
    641 
    642 	nports = hub->hubdesc.bNbrPorts;
    643 	for(port = 0; port < nports; port++) {
    644 		rup = &hub->ports[port];
    645 		if (rup->device)
    646 			usb_disconnect_port(rup, self);
    647 	}
    648 
    649 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    650 			   USBDEV(sc->sc_dev));
    651 
    652 	if (hub->ports[0].tt)
    653 		free(hub->ports[0].tt, M_USBDEV);
    654 	free(hub, M_USBDEV);
    655 	sc->sc_hub->hub = NULL;
    656 	if (sc->sc_status)
    657 		free(sc->sc_status, M_USBDEV);
    658 
    659 	return (0);
    660 }
    661 
    662 #if defined(__FreeBSD__)
    663 int
    664 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
    665     size_t buflen)
    666 {
    667 	struct uhub_softc *sc = device_get_softc(cbdev);
    668 	usbd_device_handle devhub = sc->sc_hub;
    669 	usbd_device_handle dev;
    670 	int nports;
    671 	int port;
    672 	int i;
    673 
    674 	mtx_lock(&Giant);
    675 	nports = devhub->hub->hubdesc.bNbrPorts;
    676 	for (port = 0; port < nports; port++) {
    677 		dev = devhub->hub->ports[port].device;
    678 		if (dev && dev->subdevs) {
    679 			for (i = 0; dev->subdevs[i]; i++) {
    680 				if (dev->subdevs[i] == child) {
    681 					if (dev->ifacenums == NULL) {
    682 						snprintf(buf, buflen,
    683 						    "port=%i", port);
    684 					} else {
    685 						snprintf(buf, buflen,
    686 						    "port=%i interface=%i",
    687 						    port, dev->ifacenums[i]);
    688 					}
    689 					goto found_dev;
    690 				}
    691 			}
    692 		}
    693 	}
    694 	DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
    695 	buf[0] = '\0';
    696 found_dev:
    697 	mtx_unlock(&Giant);
    698 	return (0);
    699 }
    700 
    701 int
    702 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
    703     size_t buflen)
    704 {
    705 	struct uhub_softc *sc = device_get_softc(cbdev);
    706 	usbd_device_handle devhub = sc->sc_hub;
    707 	usbd_device_handle dev;
    708 	struct usbd_interface *iface;
    709 	char serial[128];
    710 	int nports;
    711 	int port;
    712 	int i;
    713 
    714 	mtx_lock(&Giant);
    715 	nports = devhub->hub->hubdesc.bNbrPorts;
    716 	for (port = 0; port < nports; port++) {
    717 		dev = devhub->hub->ports[port].device;
    718 		if (dev && dev->subdevs) {
    719 			for (i = 0; dev->subdevs[i]; i++) {
    720 				if (dev->subdevs[i] == child) {
    721 					goto found_dev;
    722 				}
    723 			}
    724 		}
    725 	}
    726 	DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
    727 	buf[0] = '\0';
    728 	mtx_unlock(&Giant);
    729 	return (0);
    730 
    731 found_dev:
    732 	/* XXX can sleep */
    733 	(void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
    734 	if (dev->ifacenums == NULL) {
    735 		snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
    736 		    "devclass=0x%02x devsubclass=0x%02x "
    737 		    "release=0x%04x sernum=\"%s\"",
    738 		    UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
    739 		    dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
    740 		    UGETW(dev->ddesc.bcdDevice), serial);
    741 	} else {
    742 		iface = &dev->ifaces[dev->ifacenums[i]];
    743 		snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
    744 		    "devclass=0x%02x devsubclass=0x%02x "
    745 		    "release=0x%04x sernum=\"%s\" "
    746 		    "intclass=0x%02x intsubclass=0x%02x",
    747 		    UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
    748 		    dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
    749 		    UGETW(dev->ddesc.bcdDevice), serial,
    750 		    iface->idesc->bInterfaceClass,
    751 		    iface->idesc->bInterfaceSubClass);
    752 	}
    753 	mtx_unlock(&Giant);
    754 	return (0);
    755 }
    756 #endif /* defined(__FreeBSD__) */
    757 
    758 /*
    759  * Hub interrupt.
    760  * This an indication that some port has changed status.
    761  * Notify the bus event handler thread that we need
    762  * to be explored again.
    763  */
    764 void
    765 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
    766     usbd_status status)
    767 {
    768 	struct uhub_softc *sc = addr;
    769 
    770 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    771 
    772 	if (status == USBD_STALLED)
    773 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    774 	else if (status == USBD_NORMAL_COMPLETION &&
    775 		 !sc->sc_explorepending) {
    776 		/*
    777 		 * Make sure the status is not overwritten in between.
    778 		 * XXX we should suspend the pipe instead
    779 		 */
    780 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
    781 		sc->sc_explorepending = 1;
    782 		usb_needs_explore(sc->sc_hub);
    783 	}
    784 }
    785 
    786 #if defined(__FreeBSD__)
    787 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    788 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    789 #endif
    790