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