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