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