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