Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.85.8.1
      1 /*	$NetBSD: uhub.c,v 1.85.8.1 2007/05/22 14:57:42 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.1 2007/05/22 14:57:42 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 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     88 	u_int8_t		*sc_status;
     89 	u_char			sc_running;
     90 };
     91 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
     92 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
     93 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
     94 
     95 Static usbd_status uhub_explore(usbd_device_handle hub);
     96 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
     97 
     98 #if defined(__FreeBSD__)
     99 Static bus_child_location_str_t uhub_child_location_str;
    100 Static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
    101 #endif
    102 
    103 /*
    104  * We need two attachment points:
    105  * hub to usb and hub to hub
    106  * Every other driver only connects to hubs
    107  */
    108 
    109 #if defined(__NetBSD__) || defined(__OpenBSD__)
    110 USB_DECLARE_DRIVER(uhub);
    111 #elif defined(__FreeBSD__)
    112 USB_DECLARE_DRIVER_INIT(uhub,
    113 	DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
    114 	DEVMETHOD(bus_child_location_str, uhub_child_location_str),
    115 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
    116 	DEVMETHOD(device_suspend, bus_generic_suspend),
    117 	DEVMETHOD(device_resume, bus_generic_resume),
    118 	DEVMETHOD(device_shutdown, bus_generic_shutdown)
    119 	);
    120 
    121 /* Create the driver instance for the hub connected to usb case. */
    122 devclass_t uhubroot_devclass;
    123 
    124 Static device_method_t uhubroot_methods[] = {
    125 	DEVMETHOD(bus_child_location_str, uhub_child_location_str),
    126 	DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
    127 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
    128 
    129 	DEVMETHOD(device_probe, uhub_match),
    130 	DEVMETHOD(device_attach, uhub_attach),
    131 	DEVMETHOD(device_detach, uhub_detach),
    132 	DEVMETHOD(device_suspend, bus_generic_suspend),
    133 	DEVMETHOD(device_resume, bus_generic_resume),
    134 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
    135 	{0,0}
    136 };
    137 
    138 Static	driver_t uhubroot_driver = {
    139 	"uhub",
    140 	uhubroot_methods,
    141 	sizeof(struct uhub_softc)
    142 };
    143 #endif
    144 
    145 USB_MATCH(uhub)
    146 {
    147 	USB_MATCH_START(uhub, uaa);
    148 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
    149 
    150 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
    151 	/*
    152 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    153 	 * so we just ignore the subclass.
    154 	 */
    155 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
    156 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    157 	return (UMATCH_NONE);
    158 }
    159 
    160 USB_ATTACH(uhub)
    161 {
    162 	USB_ATTACH_START(uhub, sc, uaa);
    163 	usbd_device_handle dev = uaa->device;
    164 	char *devinfo;
    165 	usbd_status err;
    166 	struct usbd_hub *hub = NULL;
    167 	usb_device_request_t req;
    168 	usb_hub_descriptor_t hubdesc;
    169 	int p, port, nports, nremov, pwrdly;
    170 	usbd_interface_handle iface;
    171 	usb_endpoint_descriptor_t *ed;
    172 	struct usbd_tt *tts = NULL;
    173 	size_t statuslen;
    174 
    175 	DPRINTFN(1,("uhub_attach\n"));
    176 	sc->sc_hub = dev;
    177 
    178 	devinfo = usbd_devinfo_alloc(dev, 1);
    179 	USB_ATTACH_SETUP;
    180 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    181 	usbd_devinfo_free(devinfo);
    182 
    183 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    184 		printf("%s: %s transaction translator%s\n",
    185 		       USBDEVNAME(sc->sc_dev),
    186 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    187 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    188 	}
    189 
    190 	err = usbd_set_config_index(dev, 0, 1);
    191 	if (err) {
    192 		DPRINTF(("%s: configuration failed, error=%s\n",
    193 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    194 		USB_ATTACH_ERROR_RETURN;
    195 	}
    196 
    197 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    198 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    199 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    200 		USB_ATTACH_ERROR_RETURN;
    201 	}
    202 
    203 	/* Get hub descriptor. */
    204 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    205 	req.bRequest = UR_GET_DESCRIPTOR;
    206 	USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
    207 	USETW(req.wIndex, 0);
    208 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    209 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    210 	err = usbd_do_request(dev, &req, &hubdesc);
    211 	nports = hubdesc.bNbrPorts;
    212 	if (!err && nports > 7) {
    213 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    214 		err = usbd_do_request(dev, &req, &hubdesc);
    215 	}
    216 	if (err) {
    217 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    218 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    219 		USB_ATTACH_ERROR_RETURN;
    220 	}
    221 
    222 	for (nremov = 0, port = 1; port <= nports; port++)
    223 		if (!UHD_NOT_REMOV(&hubdesc, port))
    224 			nremov++;
    225 	printf("%s: %d port%s with %d removable, %s powered\n",
    226 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    227 	       nremov, dev->self_powered ? "self" : "bus");
    228 
    229 	if (nports == 0) {
    230 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
    231 		goto bad;
    232 	}
    233 
    234 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    235 		     M_USBDEV, M_NOWAIT);
    236 	if (hub == NULL)
    237 		USB_ATTACH_ERROR_RETURN;
    238 	dev->hub = hub;
    239 	dev->hub->hubsoftc = sc;
    240 	hub->explore = uhub_explore;
    241 	hub->hubdesc = hubdesc;
    242 
    243 #if 0	/* This chack is disabled since the spec is unclear. */
    244 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
    245 		    "parent->selfpowered=%d\n",
    246 		 dev->self_powered, dev->powersrc->parent,
    247 		 dev->powersrc->parent ?
    248 		 dev->powersrc->parent->self_powered : 0));
    249 
    250 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
    251 	    !dev->powersrc->parent->self_powered) {
    252 		printf("%s: bus powered hub connected to bus powered hub, "
    253 		       "ignored\n", USBDEVNAME(sc->sc_dev));
    254 		goto bad;
    255 	}
    256 #endif
    257 
    258 	/* Set up interrupt pipe. */
    259 	err = usbd_device2interface_handle(dev, 0, &iface);
    260 	if (err) {
    261 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    262 		goto bad;
    263 	}
    264 
    265 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    266 		err = usbd_set_interface(iface, 1);
    267 		if (err)
    268 			printf("%s: can't enable multiple TTs\n",
    269 			       USBDEVNAME(sc->sc_dev));
    270 	}
    271 
    272 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    273 	if (ed == NULL) {
    274 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    275 		goto bad;
    276 	}
    277 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    278 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    279 		goto bad;
    280 	}
    281 
    282 	statuslen = (nports + 1 + 7) / 8;
    283 	sc->sc_status = malloc(statuslen, M_USBDEV, M_NOWAIT);
    284 	if (!sc->sc_status)
    285 		goto bad;
    286 
    287 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    288 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
    289 		  statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
    290 	if (err) {
    291 		printf("%s: cannot open interrupt pipe\n",
    292 		       USBDEVNAME(sc->sc_dev));
    293 		goto bad;
    294 	}
    295 
    296 	/* Wait with power off for a while. */
    297 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    298 
    299 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    300 
    301 	/*
    302 	 * To have the best chance of success we do things in the exact same
    303 	 * order as Windoze98.  This should not be necessary, but some
    304 	 * devices do not follow the USB specs to the letter.
    305 	 *
    306 	 * These are the events on the bus when a hub is attached:
    307 	 *  Get device and config descriptors (see attach code)
    308 	 *  Get hub descriptor (see above)
    309 	 *  For all ports
    310 	 *     turn on power
    311 	 *     wait for power to become stable
    312 	 * (all below happens in explore code)
    313 	 *  For all ports
    314 	 *     clear C_PORT_CONNECTION
    315 	 *  For all ports
    316 	 *     get port status
    317 	 *     if device connected
    318 	 *        wait 100 ms
    319 	 *        turn on reset
    320 	 *        wait
    321 	 *        clear C_PORT_RESET
    322 	 *        get port status
    323 	 *        proceed with device attachment
    324 	 */
    325 
    326 	if (UHUB_IS_HIGH_SPEED(sc)) {
    327 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    328 		    sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
    329 		if (!tts)
    330 			goto bad;
    331 	}
    332 
    333 	/* Set up data structures */
    334 	for (p = 0; p < nports; p++) {
    335 		struct usbd_port *up = &hub->ports[p];
    336 		up->device = NULL;
    337 		up->parent = dev;
    338 		up->portno = p+1;
    339 		if (dev->self_powered)
    340 			/* Self powered hub, give ports maximum current. */
    341 			up->power = USB_MAX_POWER;
    342 		else
    343 			up->power = USB_MIN_POWER;
    344 		up->restartcnt = 0;
    345 		up->reattach = 0;
    346 		if (UHUB_IS_HIGH_SPEED(sc)) {
    347 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    348 			up->tt->hub = hub;
    349 		} else {
    350 			up->tt = NULL;
    351 		}
    352 	}
    353 
    354 	/* XXX should check for none, individual, or ganged power? */
    355 
    356 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    357 	    + USB_EXTRA_POWER_UP_TIME;
    358 	for (port = 1; port <= nports; port++) {
    359 		/* Turn the power on. */
    360 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    361 		if (err)
    362 			printf("%s: port %d power on failed, %s\n",
    363 			       USBDEVNAME(sc->sc_dev), port,
    364 			       usbd_errstr(err));
    365 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    366 		/* Wait for stable power. */
    367 		usbd_delay_ms(dev, pwrdly);
    368 	}
    369 
    370 	/* The usual exploration will finish the setup. */
    371 
    372 	sc->sc_running = 1;
    373 
    374 	USB_ATTACH_SUCCESS_RETURN;
    375 
    376  bad:
    377 	if (sc->sc_status)
    378 		free(sc->sc_status, M_USBDEV);
    379 	if (hub)
    380 		free(hub, M_USBDEV);
    381 	dev->hub = NULL;
    382 	USB_ATTACH_ERROR_RETURN;
    383 }
    384 
    385 usbd_status
    386 uhub_explore(usbd_device_handle dev)
    387 {
    388 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    389 	struct uhub_softc *sc = dev->hub->hubsoftc;
    390 	struct usbd_port *up;
    391 	usbd_status err;
    392 	int speed;
    393 	int port;
    394 	int change, status, reconnect;
    395 
    396 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    397 
    398 	if (!sc->sc_running)
    399 		return (USBD_NOT_STARTED);
    400 
    401 	/* Ignore hubs that are too deep. */
    402 	if (dev->depth > USB_HUB_MAX_DEPTH)
    403 		return (USBD_TOO_DEEP);
    404 
    405 	for (port = 1; port <= hd->bNbrPorts; port++) {
    406 		up = &dev->hub->ports[port-1];
    407 		err = usbd_get_port_status(dev, port, &up->status);
    408 		if (err) {
    409 			DPRINTF(("uhub_explore: get port status failed, "
    410 				 "error=%s\n", usbd_errstr(err)));
    411 			continue;
    412 		}
    413 		status = UGETW(up->status.wPortStatus);
    414 		change = UGETW(up->status.wPortChange);
    415 		reconnect = up->reattach;
    416 		up->reattach = 0;
    417 		DPRINTFN(3,("%s: uhub_explore: port %d status 0x%04x 0x%04x\n",
    418 			    USBDEVNAME(sc->sc_dev), port, status, change));
    419 		if (change & UPS_C_PORT_ENABLED) {
    420 			DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
    421 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    422 			if (change & UPS_C_CONNECT_STATUS) {
    423 				/* Ignore the port error if the device
    424 				   vanished. */
    425 			} else if (status & UPS_PORT_ENABLED) {
    426 				printf("%s: illegal enable change, port %d\n",
    427 				       USBDEVNAME(sc->sc_dev), port);
    428 			} else {
    429 				/* Port error condition. */
    430 				if (up->restartcnt) /* no message first time */
    431 					printf("%s: port error, restarting "
    432 					       "port %d\n",
    433 					       USBDEVNAME(sc->sc_dev), port);
    434 
    435 				if (up->restartcnt++ < USBD_RESTART_MAX)
    436 					goto disco;
    437 				else
    438 					printf("%s: port error, giving up "
    439 					       "port %d\n",
    440 					       USBDEVNAME(sc->sc_dev), port);
    441 			}
    442 		}
    443 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    444 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
    445 				    "STATUS\n", port));
    446 			/* No status change, just do recursive explore. */
    447 			if (up->device != NULL && up->device->hub != NULL)
    448 				up->device->hub->explore(up->device);
    449 #if 0 && defined(DIAGNOSTIC)
    450 			if (up->device == NULL &&
    451 			    (status & UPS_CURRENT_CONNECT_STATUS))
    452 				printf("%s: connected, no device\n",
    453 				       USBDEVNAME(sc->sc_dev));
    454 #endif
    455 			continue;
    456 		}
    457 
    458 		/* We have a connect status change, handle it. */
    459 
    460 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    461 			 dev->address, port));
    462 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    463 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
    464 		/*
    465 		 * If there is already a device on the port the change status
    466 		 * must mean that is has disconnected.  Looking at the
    467 		 * current connect status is not enough to figure this out
    468 		 * since a new unit may have been connected before we handle
    469 		 * the disconnect.
    470 		 */
    471 	disco:
    472 		if (up->device != NULL) {
    473 			/* Disconnected */
    474 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    475 				 "on port %d\n", up->device->address, port));
    476 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    477 			usbd_clear_port_feature(dev, port,
    478 						UHF_C_PORT_CONNECTION);
    479 		}
    480 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    481 			/* Nothing connected, just ignore it. */
    482 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    483 				    "_STATUS\n", port));
    484 			continue;
    485 		}
    486 
    487 		/* Connected */
    488 
    489 		if (!(status & UPS_PORT_POWER))
    490 			printf("%s: strange, connected port %d has no power\n",
    491 			       USBDEVNAME(sc->sc_dev), port);
    492 
    493 		/* Wait for maximum device power up time. */
    494 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    495 
    496 		/* Reset port, which implies enabling it. */
    497 		if (usbd_reset_port(dev, port, &up->status)) {
    498 			printf("%s: port %d reset failed\n",
    499 			       USBDEVNAME(sc->sc_dev), port);
    500 			continue;
    501 		}
    502 		/* Get port status again, it might have changed during reset */
    503 		err = usbd_get_port_status(dev, port, &up->status);
    504 		if (err) {
    505 			DPRINTF(("uhub_explore: get port status failed, "
    506 				 "error=%s\n", usbd_errstr(err)));
    507 			continue;
    508 		}
    509 		status = UGETW(up->status.wPortStatus);
    510 		change = UGETW(up->status.wPortChange);
    511 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    512 			/* Nothing connected, just ignore it. */
    513 #ifdef DIAGNOSTIC
    514 			printf("%s: port %d, device disappeared after reset\n",
    515 			       USBDEVNAME(sc->sc_dev), port);
    516 #endif
    517 			continue;
    518 		}
    519 
    520 #if 0
    521 		if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
    522 			printf("%s: port %d, transaction translation not "
    523 			       "implemented, low/full speed device ignored\n",
    524 			       USBDEVNAME(sc->sc_dev), port);
    525 			continue;
    526 		}
    527 #endif
    528 
    529 		/* Figure out device speed */
    530 		if (status & UPS_HIGH_SPEED)
    531 			speed = USB_SPEED_HIGH;
    532 		else if (status & UPS_LOW_SPEED)
    533 			speed = USB_SPEED_LOW;
    534 		else
    535 			speed = USB_SPEED_FULL;
    536 		/* Get device info and set its address. */
    537 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    538 		    dev->depth + 1, speed, port, up);
    539 		/* XXX retry a few times? */
    540 		if (err) {
    541 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    542 				     "error=%s\n", usbd_errstr(err)));
    543 			/* Avoid addressing problems by disabling. */
    544 			/* usbd_reset_port(dev, port, &up->status); */
    545 
    546 			/*
    547 			 * The unit refused to accept a new address, or had
    548 			 * some other serious problem.  Since we cannot leave
    549 			 * at 0 we have to disable the port instead.
    550 			 */
    551 			printf("%s: device problem (%s), disabling port %d\n",
    552 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err), port);
    553 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    554 		} else {
    555 			/* The port set up succeeded, reset error count. */
    556 			up->restartcnt = 0;
    557 
    558 			if (up->device->hub)
    559 				up->device->hub->explore(up->device);
    560 		}
    561 	}
    562 	return (USBD_NORMAL_COMPLETION);
    563 }
    564 
    565 #if defined(__NetBSD__) || defined(__OpenBSD__)
    566 int
    567 uhub_activate(device_ptr_t self, enum devact act)
    568 {
    569 	struct uhub_softc *sc = (struct uhub_softc *)self;
    570 	struct usbd_hub *hub = sc->sc_hub->hub;
    571 	usbd_device_handle dev;
    572 	int nports, port, i;
    573 
    574 	switch (act) {
    575 	case DVACT_ACTIVATE:
    576 		return (EOPNOTSUPP);
    577 
    578 	case DVACT_DEACTIVATE:
    579 		if (hub == NULL) /* malfunctioning hub */
    580 			break;
    581 		nports = hub->hubdesc.bNbrPorts;
    582 		for(port = 0; port < nports; port++) {
    583 			dev = hub->ports[port].device;
    584 			if (dev != NULL && dev->subdevs != NULL) {
    585 				for (i = 0; dev->subdevs[i] != NULL; i++)
    586 					config_deactivate(dev->subdevs[i]);
    587 			}
    588 		}
    589 		break;
    590 	}
    591 	return (0);
    592 }
    593 #endif
    594 
    595 /*
    596  * Called from process context when the hub is gone.
    597  * Detach all devices on active ports.
    598  */
    599 USB_DETACH(uhub)
    600 {
    601 	USB_DETACH_START(uhub, sc);
    602 	struct usbd_hub *hub = sc->sc_hub->hub;
    603 	struct usbd_port *rup;
    604 	int port, nports;
    605 
    606 #if defined(__NetBSD__) || defined(__OpenBSD__)
    607 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    608 #elif defined(__FreeBSD__)
    609 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    610 #endif
    611 
    612 	if (hub == NULL)		/* Must be partially working */
    613 		return (0);
    614 
    615 	usbd_abort_pipe(sc->sc_ipipe);
    616 	usbd_close_pipe(sc->sc_ipipe);
    617 
    618 	nports = hub->hubdesc.bNbrPorts;
    619 	for(port = 0; port < nports; port++) {
    620 		rup = &hub->ports[port];
    621 		if (rup->device)
    622 			usb_disconnect_port(rup, self);
    623 	}
    624 
    625 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    626 			   USBDEV(sc->sc_dev));
    627 
    628 	if (hub->ports[0].tt)
    629 		free(hub->ports[0].tt, M_USBDEV);
    630 	free(hub, M_USBDEV);
    631 	sc->sc_hub->hub = NULL;
    632 	if (sc->sc_status)
    633 		free(sc->sc_status, M_USBDEV);
    634 
    635 	return (0);
    636 }
    637 
    638 #if defined(__FreeBSD__)
    639 int
    640 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
    641     size_t buflen)
    642 {
    643 	struct uhub_softc *sc = device_get_softc(cbdev);
    644 	usbd_device_handle devhub = sc->sc_hub;
    645 	usbd_device_handle dev;
    646 	int nports;
    647 	int port;
    648 	int i;
    649 
    650 	mtx_lock(&Giant);
    651 	nports = devhub->hub->hubdesc.bNbrPorts;
    652 	for (port = 0; port < nports; port++) {
    653 		dev = devhub->hub->ports[port].device;
    654 		if (dev && dev->subdevs) {
    655 			for (i = 0; dev->subdevs[i]; i++) {
    656 				if (dev->subdevs[i] == child) {
    657 					if (dev->ifacenums == NULL) {
    658 						snprintf(buf, buflen,
    659 						    "port=%i", port);
    660 					} else {
    661 						snprintf(buf, buflen,
    662 						    "port=%i interface=%i",
    663 						    port, dev->ifacenums[i]);
    664 					}
    665 					goto found_dev;
    666 				}
    667 			}
    668 		}
    669 	}
    670 	DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
    671 	buf[0] = '\0';
    672 found_dev:
    673 	mtx_unlock(&Giant);
    674 	return (0);
    675 }
    676 
    677 int
    678 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
    679     size_t buflen)
    680 {
    681 	struct uhub_softc *sc = device_get_softc(cbdev);
    682 	usbd_device_handle devhub = sc->sc_hub;
    683 	usbd_device_handle dev;
    684 	struct usbd_interface *iface;
    685 	char serial[128];
    686 	int nports;
    687 	int port;
    688 	int i;
    689 
    690 	mtx_lock(&Giant);
    691 	nports = devhub->hub->hubdesc.bNbrPorts;
    692 	for (port = 0; port < nports; port++) {
    693 		dev = devhub->hub->ports[port].device;
    694 		if (dev && dev->subdevs) {
    695 			for (i = 0; dev->subdevs[i]; i++) {
    696 				if (dev->subdevs[i] == child) {
    697 					goto found_dev;
    698 				}
    699 			}
    700 		}
    701 	}
    702 	DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
    703 	buf[0] = '\0';
    704 	mtx_unlock(&Giant);
    705 	return (0);
    706 
    707 found_dev:
    708 	/* XXX can sleep */
    709 	(void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
    710 	if (dev->ifacenums == NULL) {
    711 		snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
    712 		    "devclass=0x%02x devsubclass=0x%02x "
    713 		    "release=0x%04x sernum=\"%s\"",
    714 		    UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
    715 		    dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
    716 		    UGETW(dev->ddesc.bcdDevice), serial);
    717 	} else {
    718 		iface = &dev->ifaces[dev->ifacenums[i]];
    719 		snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
    720 		    "devclass=0x%02x devsubclass=0x%02x "
    721 		    "release=0x%04x sernum=\"%s\" "
    722 		    "intclass=0x%02x intsubclass=0x%02x",
    723 		    UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
    724 		    dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
    725 		    UGETW(dev->ddesc.bcdDevice), serial,
    726 		    iface->idesc->bInterfaceClass,
    727 		    iface->idesc->bInterfaceSubClass);
    728 	}
    729 	mtx_unlock(&Giant);
    730 	return (0);
    731 }
    732 #endif /* defined(__FreeBSD__) */
    733 
    734 /*
    735  * Hub interrupt.
    736  * This an indication that some port has changed status.
    737  * Notify the bus event handler thread that we need
    738  * to be explored again.
    739  */
    740 void
    741 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
    742     usbd_status status)
    743 {
    744 	struct uhub_softc *sc = addr;
    745 
    746 #if 0
    747 	void *buf; int cnt;
    748 	usbd_get_xfer_status(xfer, NULL, &buf, &cnt, NULL);
    749 #endif
    750 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    751 	if (status == USBD_STALLED)
    752 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    753 	else if (status == USBD_NORMAL_COMPLETION)
    754 		usb_needs_explore(sc->sc_hub);
    755 }
    756 
    757 #if defined(__FreeBSD__)
    758 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    759 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    760 #endif
    761