Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.76.2.1
      1 /*	$NetBSD: uhub.c,v 1.76.2.1 2006/06/21 15:07:44 yamt Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     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.
     27  *
     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.
     39  */
     40 
     41 /*
     42  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.76.2.1 2006/06/21 15:07:44 yamt 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 "bus_if.h"
     59 #endif
     60 
     61 #include <machine/bus.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdi_util.h>
     66 #include <dev/usb/usbdivar.h>
     67 
     68 #define UHUB_INTR_INTERVAL 255	/* ms */
     69 
     70 #ifdef UHUB_DEBUG
     71 #define DPRINTF(x)	if (uhubdebug) logprintf x
     72 #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
     73 int	uhubdebug = 0;
     74 #else
     75 #define DPRINTF(x)
     76 #define DPRINTFN(n,x)
     77 #endif
     78 
     79 struct uhub_softc {
     80 	USBBASEDEVICE		sc_dev;		/* base device */
     81 	usbd_device_handle	sc_hub;		/* USB device */
     82 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     83 	u_int8_t		sc_status[1];	/* XXX more ports */
     84 	u_char			sc_running;
     85 };
     86 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
     87 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
     88 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
     89 
     90 Static usbd_status uhub_explore(usbd_device_handle hub);
     91 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
     92 
     93 #if defined(__FreeBSD__)
     94 Static bus_child_detached_t uhub_child_detached;
     95 #endif
     96 
     97 
     98 /*
     99  * We need two attachment points:
    100  * hub to usb and hub to hub
    101  * Every other driver only connects to hubs
    102  */
    103 
    104 #if defined(__NetBSD__) || defined(__OpenBSD__)
    105 USB_DECLARE_DRIVER(uhub);
    106 
    107 /* Create the driver instance for the hub connected to hub case */
    108 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
    109     uhub_match, uhub_attach, uhub_detach, uhub_activate);
    110 #elif defined(__FreeBSD__)
    111 USB_DECLARE_DRIVER_INIT(uhub,
    112 			DEVMETHOD(bus_child_detached, uhub_child_detached));
    113 
    114 /* Create the driver instance for the hub connected to usb case. */
    115 devclass_t uhubroot_devclass;
    116 
    117 Static device_method_t uhubroot_methods[] = {
    118 	DEVMETHOD(device_probe, uhub_match),
    119 	DEVMETHOD(device_attach, uhub_attach),
    120 
    121 	/* detach is not allowed for a root hub */
    122 	{0,0}
    123 };
    124 
    125 Static	driver_t uhubroot_driver = {
    126 	"uhub",
    127 	uhubroot_methods,
    128 	sizeof(struct uhub_softc)
    129 };
    130 #endif
    131 
    132 USB_MATCH(uhub)
    133 {
    134 	USB_MATCH_START(uhub, uaa);
    135 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
    136 
    137 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
    138 	/*
    139 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    140 	 * so we just ignore the subclass.
    141 	 */
    142 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
    143 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    144 	return (UMATCH_NONE);
    145 }
    146 
    147 USB_ATTACH(uhub)
    148 {
    149 	USB_ATTACH_START(uhub, sc, uaa);
    150 	usbd_device_handle dev = uaa->device;
    151 	char *devinfop;
    152 	usbd_status err;
    153 	struct usbd_hub *hub = NULL;
    154 	usb_device_request_t req;
    155 	usb_hub_descriptor_t hubdesc;
    156 	int p, port, nports, nremov, pwrdly;
    157 	usbd_interface_handle iface;
    158 	usb_endpoint_descriptor_t *ed;
    159 	struct usbd_tt *tts = NULL;
    160 
    161 	DPRINTFN(1,("uhub_attach\n"));
    162 	sc->sc_hub = dev;
    163 
    164 	devinfop = usbd_devinfo_alloc(dev, 1);
    165 	USB_ATTACH_SETUP;
    166 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    167 	usbd_devinfo_free(devinfop);
    168 
    169 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    170 		printf("%s: %s transaction translator%s\n",
    171 		       USBDEVNAME(sc->sc_dev),
    172 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    173 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    174 	}
    175 
    176 	err = usbd_set_config_index(dev, 0, 1);
    177 	if (err) {
    178 		DPRINTF(("%s: configuration failed, error=%s\n",
    179 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    180 		USB_ATTACH_ERROR_RETURN;
    181 	}
    182 
    183 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    184 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    185 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    186 		USB_ATTACH_ERROR_RETURN;
    187 	}
    188 
    189 	/* Get hub descriptor. */
    190 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    191 	req.bRequest = UR_GET_DESCRIPTOR;
    192 	USETW2(req.wValue, UDESC_HUB, 0);
    193 	USETW(req.wIndex, 0);
    194 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    195 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    196 	err = usbd_do_request(dev, &req, &hubdesc);
    197 	nports = hubdesc.bNbrPorts;
    198 	if (!err && nports > 7) {
    199 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    200 		err = usbd_do_request(dev, &req, &hubdesc);
    201 	}
    202 	if (err) {
    203 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    204 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    205 		USB_ATTACH_ERROR_RETURN;
    206 	}
    207 
    208 	for (nremov = 0, port = 1; port <= nports; port++)
    209 		if (!UHD_NOT_REMOV(&hubdesc, port))
    210 			nremov++;
    211 	printf("%s: %d port%s with %d removable, %s powered\n",
    212 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    213 	       nremov, dev->self_powered ? "self" : "bus");
    214 
    215 	if (nports == 0) {
    216 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
    217 		goto bad;
    218 	}
    219 
    220 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    221 		     M_USBDEV, M_NOWAIT);
    222 	if (hub == NULL)
    223 		USB_ATTACH_ERROR_RETURN;
    224 	dev->hub = hub;
    225 	dev->hub->hubsoftc = sc;
    226 	hub->explore = uhub_explore;
    227 	hub->hubdesc = hubdesc;
    228 
    229 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
    230 		    "parent->selfpowered=%d\n",
    231 		 dev->self_powered, dev->powersrc->parent,
    232 		 dev->powersrc->parent ?
    233 		 dev->powersrc->parent->self_powered : 0));
    234 
    235 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
    236 	    !dev->powersrc->parent->self_powered) {
    237 		printf("%s: bus powered hub connected to bus powered hub, "
    238 		       "ignored\n", USBDEVNAME(sc->sc_dev));
    239 		goto bad;
    240 	}
    241 
    242 	/* Set up interrupt pipe. */
    243 	err = usbd_device2interface_handle(dev, 0, &iface);
    244 	if (err) {
    245 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    246 		goto bad;
    247 	}
    248 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    249 	if (ed == NULL) {
    250 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    251 		goto bad;
    252 	}
    253 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    254 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    255 		goto bad;
    256 	}
    257 
    258 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    259 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
    260 		  sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
    261 	if (err) {
    262 		printf("%s: cannot open interrupt pipe\n",
    263 		       USBDEVNAME(sc->sc_dev));
    264 		goto bad;
    265 	}
    266 
    267 	/* Wait with power off for a while. */
    268 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    269 
    270 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    271 
    272 	/*
    273 	 * To have the best chance of success we do things in the exact same
    274 	 * order as Windoze98.  This should not be necessary, but some
    275 	 * devices do not follow the USB specs to the letter.
    276 	 *
    277 	 * These are the events on the bus when a hub is attached:
    278 	 *  Get device and config descriptors (see attach code)
    279 	 *  Get hub descriptor (see above)
    280 	 *  For all ports
    281 	 *     turn on power
    282 	 *     wait for power to become stable
    283 	 * (all below happens in explore code)
    284 	 *  For all ports
    285 	 *     clear C_PORT_CONNECTION
    286 	 *  For all ports
    287 	 *     get port status
    288 	 *     if device connected
    289 	 *        wait 100 ms
    290 	 *        turn on reset
    291 	 *        wait
    292 	 *        clear C_PORT_RESET
    293 	 *        get port status
    294 	 *        proceed with device attachment
    295 	 */
    296 
    297 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    298 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    299 			     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
    300 		if (!tts)
    301 			goto bad;
    302 	}
    303 	/* Set up data structures */
    304 	for (p = 0; p < nports; p++) {
    305 		struct usbd_port *up = &hub->ports[p];
    306 		up->device = NULL;
    307 		up->parent = dev;
    308 		up->portno = p+1;
    309 		if (dev->self_powered)
    310 			/* Self powered hub, give ports maximum current. */
    311 			up->power = USB_MAX_POWER;
    312 		else
    313 			up->power = USB_MIN_POWER;
    314 		up->restartcnt = 0;
    315 		up->reattach = 0;
    316 		if (UHUB_IS_HIGH_SPEED(sc)) {
    317 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    318 			up->tt->hub = hub;
    319 		} else {
    320 			up->tt = NULL;
    321 		}
    322 	}
    323 
    324 	/* XXX should check for none, individual, or ganged power? */
    325 
    326 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    327 	    + USB_EXTRA_POWER_UP_TIME;
    328 	for (port = 1; port <= nports; port++) {
    329 		/* Turn the power on. */
    330 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    331 		if (err)
    332 			printf("%s: port %d power on failed, %s\n",
    333 			       USBDEVNAME(sc->sc_dev), port,
    334 			       usbd_errstr(err));
    335 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    336 		/* Wait for stable power. */
    337 		usbd_delay_ms(dev, pwrdly);
    338 	}
    339 
    340 	/* The usual exploration will finish the setup. */
    341 
    342 	sc->sc_running = 1;
    343 
    344 	USB_ATTACH_SUCCESS_RETURN;
    345 
    346  bad:
    347 	if (hub)
    348 		free(hub, M_USBDEV);
    349 	dev->hub = NULL;
    350 	USB_ATTACH_ERROR_RETURN;
    351 }
    352 
    353 usbd_status
    354 uhub_explore(usbd_device_handle dev)
    355 {
    356 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    357 	struct uhub_softc *sc = dev->hub->hubsoftc;
    358 	struct usbd_port *up;
    359 	usbd_status err;
    360 	int speed;
    361 	int port;
    362 	int change, status, reconnect;
    363 
    364 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    365 
    366 	if (!sc->sc_running)
    367 		return (USBD_NOT_STARTED);
    368 
    369 	/* Ignore hubs that are too deep. */
    370 	if (dev->depth > USB_HUB_MAX_DEPTH)
    371 		return (USBD_TOO_DEEP);
    372 
    373 	for(port = 1; port <= hd->bNbrPorts; port++) {
    374 		up = &dev->hub->ports[port-1];
    375 		err = usbd_get_port_status(dev, port, &up->status);
    376 		if (err) {
    377 			DPRINTF(("uhub_explore: get port status failed, "
    378 				 "error=%s\n", usbd_errstr(err)));
    379 			continue;
    380 		}
    381 		status = UGETW(up->status.wPortStatus);
    382 		change = UGETW(up->status.wPortChange);
    383 		reconnect = up->reattach;
    384 		up->reattach = 0;
    385 		DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
    386 			    USBDEVNAME(sc->sc_dev), port, status, change));
    387 		if (change & UPS_C_PORT_ENABLED) {
    388 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    389 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    390 			if (change & UPS_C_CONNECT_STATUS) {
    391 				/* Ignore the port error if the device
    392 				   vanished. */
    393 			} else if (status & UPS_PORT_ENABLED) {
    394 				printf("%s: illegal enable change, port %d\n",
    395 				       USBDEVNAME(sc->sc_dev), port);
    396 			} else {
    397 				/* Port error condition. */
    398 				if (up->restartcnt) /* no message first time */
    399 					printf("%s: port error, restarting "
    400 					       "port %d\n",
    401 					       USBDEVNAME(sc->sc_dev), port);
    402 
    403 				if (up->restartcnt++ < USBD_RESTART_MAX)
    404 					goto disco;
    405 				else
    406 					printf("%s: port error, giving up "
    407 					       "port %d\n",
    408 					       USBDEVNAME(sc->sc_dev), port);
    409 			}
    410 		}
    411 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    412 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
    413 				    "STATUS\n", port));
    414 			/* No status change, just do recursive explore. */
    415 			if (up->device != NULL && up->device->hub != NULL)
    416 				up->device->hub->explore(up->device);
    417 #if 0 && defined(DIAGNOSTIC)
    418 			if (up->device == NULL &&
    419 			    (status & UPS_CURRENT_CONNECT_STATUS))
    420 				printf("%s: connected, no device\n",
    421 				       USBDEVNAME(sc->sc_dev));
    422 #endif
    423 			continue;
    424 		}
    425 
    426 		/* We have a connect status change, handle it. */
    427 
    428 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    429 			 dev->address, port));
    430 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    431 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
    432 		/*
    433 		 * If there is already a device on the port the change status
    434 		 * must mean that is has disconnected.  Looking at the
    435 		 * current connect status is not enough to figure this out
    436 		 * since a new unit may have been connected before we handle
    437 		 * the disconnect.
    438 		 */
    439 	disco:
    440 		if (up->device != NULL) {
    441 			/* Disconnected */
    442 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    443 				 "on port %d\n", up->device->address, port));
    444 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    445 			usbd_clear_port_feature(dev, port,
    446 						UHF_C_PORT_CONNECTION);
    447 		}
    448 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    449 			/* Nothing connected, just ignore it. */
    450 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    451 				    "_STATUS\n", port));
    452 			continue;
    453 		}
    454 
    455 		/* Connected */
    456 
    457 		if (!(status & UPS_PORT_POWER))
    458 			printf("%s: strange, connected port %d has no power\n",
    459 			       USBDEVNAME(sc->sc_dev), port);
    460 
    461 		/* Wait for maximum device power up time. */
    462 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    463 
    464 		/* Reset port, which implies enabling it. */
    465 		if (usbd_reset_port(dev, port, &up->status)) {
    466 			printf("%s: port %d reset failed\n",
    467 			       USBDEVNAME(sc->sc_dev), port);
    468 			continue;
    469 		}
    470 		/* Get port status again, it might have changed during reset */
    471 		err = usbd_get_port_status(dev, port, &up->status);
    472 		if (err) {
    473 			DPRINTF(("uhub_explore: get port status failed, "
    474 				 "error=%s\n", usbd_errstr(err)));
    475 			continue;
    476 		}
    477 		status = UGETW(up->status.wPortStatus);
    478 		change = UGETW(up->status.wPortChange);
    479 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    480 			/* Nothing connected, just ignore it. */
    481 #ifdef DIAGNOSTIC
    482 			printf("%s: port %d, device disappeared after reset\n",
    483 			       USBDEVNAME(sc->sc_dev), port);
    484 #endif
    485 			continue;
    486 		}
    487 
    488 #if 0
    489 		if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
    490 			printf("%s: port %d, transaction translation not "
    491 			       "implemented, low/full speed device ignored\n",
    492 			       USBDEVNAME(sc->sc_dev), port);
    493 			continue;
    494 		}
    495 #endif
    496 
    497 		/* Figure out device speed */
    498 		if (status & UPS_HIGH_SPEED)
    499 			speed = USB_SPEED_HIGH;
    500 		else if (status & UPS_LOW_SPEED)
    501 			speed = USB_SPEED_LOW;
    502 		else
    503 			speed = USB_SPEED_FULL;
    504 		/* Get device info and set its address. */
    505 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    506 			  dev->depth + 1, speed, port, up);
    507 		/* XXX retry a few times? */
    508 		if (err) {
    509 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    510 				     "error=%s\n", usbd_errstr(err)));
    511 			/* Avoid addressing problems by disabling. */
    512 			/* usbd_reset_port(dev, port, &up->status); */
    513 
    514 			/*
    515 			 * The unit refused to accept a new address, or had
    516 			 * some other serious problem.  Since we cannot leave
    517 			 * at 0 we have to disable the port instead.
    518 			 */
    519 			printf("%s: device problem, disabling port %d\n",
    520 			       USBDEVNAME(sc->sc_dev), port);
    521 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    522 		} else {
    523 			/* The port set up succeeded, reset error count. */
    524 			up->restartcnt = 0;
    525 
    526 			if (up->device->hub)
    527 				up->device->hub->explore(up->device);
    528 		}
    529 	}
    530 	return (USBD_NORMAL_COMPLETION);
    531 }
    532 
    533 #if defined(__NetBSD__) || defined(__OpenBSD__)
    534 int
    535 uhub_activate(device_ptr_t self, enum devact act)
    536 {
    537 	struct uhub_softc *sc = (struct uhub_softc *)self;
    538 	struct usbd_hub *hub = sc->sc_hub->hub;
    539 	usbd_device_handle dev;
    540 	int nports, port, i;
    541 
    542 	switch (act) {
    543 	case DVACT_ACTIVATE:
    544 		return (EOPNOTSUPP);
    545 
    546 	case DVACT_DEACTIVATE:
    547 		if (hub == NULL) /* malfunctioning hub */
    548 			break;
    549 		nports = hub->hubdesc.bNbrPorts;
    550 		for(port = 0; port < nports; port++) {
    551 			dev = hub->ports[port].device;
    552 			if (dev != NULL && dev->subdevs != NULL) {
    553 				for (i = 0; dev->subdevs[i] != NULL; i++)
    554 					config_deactivate(dev->subdevs[i]);
    555 			}
    556 		}
    557 		break;
    558 	}
    559 	return (0);
    560 }
    561 #endif
    562 
    563 /*
    564  * Called from process context when the hub is gone.
    565  * Detach all devices on active ports.
    566  */
    567 USB_DETACH(uhub)
    568 {
    569 	USB_DETACH_START(uhub, sc);
    570 	struct usbd_hub *hub = sc->sc_hub->hub;
    571 	struct usbd_port *rup;
    572 	int port, nports;
    573 
    574 #if defined(__NetBSD__) || defined(__OpenBSD__)
    575 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    576 #elif defined(__FreeBSD__)
    577 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    578 #endif
    579 
    580 	if (hub == NULL)		/* Must be partially working */
    581 		return (0);
    582 
    583 	usbd_abort_pipe(sc->sc_ipipe);
    584 	usbd_close_pipe(sc->sc_ipipe);
    585 
    586 	nports = hub->hubdesc.bNbrPorts;
    587 	for(port = 0; port < nports; port++) {
    588 		rup = &hub->ports[port];
    589 		if (rup->device)
    590 			usb_disconnect_port(rup, self);
    591 	}
    592 
    593 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    594 			   USBDEV(sc->sc_dev));
    595 
    596 	if (hub->ports[0].tt)
    597 		free(hub->ports[0].tt, M_USBDEV);
    598 	free(hub, M_USBDEV);
    599 	sc->sc_hub->hub = NULL;
    600 
    601 	return (0);
    602 }
    603 
    604 #if defined(__FreeBSD__)
    605 /* Called when a device has been detached from it */
    606 Static void
    607 uhub_child_detached(device_t self, device_t child)
    608 {
    609        struct uhub_softc *sc = device_get_softc(self);
    610        usbd_device_handle devhub = sc->sc_hub;
    611        usbd_device_handle dev;
    612        int nports;
    613        int port;
    614        int i;
    615 
    616        if (!devhub->hub)
    617                /* should never happen; children are only created after init */
    618                panic("hub not fully initialised, but child deleted?");
    619 
    620        nports = devhub->hub->hubdesc.bNbrPorts;
    621        for (port = 0; port < nports; port++) {
    622                dev = devhub->hub->ports[port].device;
    623                if (dev && dev->subdevs) {
    624                        for (i = 0; dev->subdevs[i]; i++) {
    625                                if (dev->subdevs[i] == child) {
    626                                        dev->subdevs[i] = NULL;
    627                                        return;
    628                                }
    629                        }
    630                }
    631        }
    632 }
    633 #endif
    634 
    635 
    636 /*
    637  * Hub interrupt.
    638  * This an indication that some port has changed status.
    639  * Notify the bus event handler thread that we need
    640  * to be explored again.
    641  */
    642 void
    643 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    644 {
    645 	struct uhub_softc *sc = addr;
    646 
    647 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    648 	if (status == USBD_STALLED)
    649 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    650 	else if (status == USBD_NORMAL_COMPLETION)
    651 		usb_needs_explore(sc->sc_hub);
    652 }
    653 
    654 #if defined(__FreeBSD__)
    655 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    656 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    657 #endif
    658