Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.99
      1 /*	$NetBSD: uhub.c,v 1.99 2008/05/25 21:41:35 drochner 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.99 2008/05/25 21:41:35 drochner Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #if defined(__NetBSD__) || defined(__OpenBSD__)
     46 #include <sys/device.h>
     47 #include <sys/proc.h>
     48 #elif defined(__FreeBSD__)
     49 #include <sys/module.h>
     50 #include <sys/bus.h>
     51 #include "bus_if.h"
     52 #endif
     53 
     54 #include <sys/bus.h>
     55 
     56 #include <dev/usb/usb.h>
     57 #include <dev/usb/usbdi.h>
     58 #include <dev/usb/usbdi_util.h>
     59 #include <dev/usb/usbdivar.h>
     60 
     61 #ifdef UHUB_DEBUG
     62 #define DPRINTF(x)	if (uhubdebug) logprintf x
     63 #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
     64 int	uhubdebug = 0;
     65 #else
     66 #define DPRINTF(x)
     67 #define DPRINTFN(n,x)
     68 #endif
     69 
     70 struct uhub_softc {
     71 	USBBASEDEVICE		sc_dev;		/* base device */
     72 	usbd_device_handle	sc_hub;		/* USB device */
     73 	int			sc_proto;	/* device protocol */
     74 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     75 
     76 	/* XXX second buffer needed because we can't suspend pipes yet */
     77 	u_int8_t		*sc_statusbuf;
     78 	u_int8_t		*sc_status;
     79 	size_t			sc_statuslen;
     80 	int			sc_explorepending;
     81 
     82 	u_char			sc_running;
     83 };
     84 
     85 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
     86 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
     87 
     88 #define PORTSTAT_ISSET(sc, port) \
     89 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
     90 
     91 Static usbd_status uhub_explore(usbd_device_handle hub);
     92 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
     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 int uhub_match(device_t, cfdata_t, void *);
    102 void uhub_attach(device_t, device_t, void *);
    103 void uhub_childdet(device_t, device_t);
    104 int uhub_detach(device_t, int);
    105 int uhub_activate(device_t, enum devact);
    106 extern struct cfdriver uhub_cd;
    107 CFATTACH_DECL2_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    108     uhub_attach, uhub_detach, uhub_activate, NULL, uhub_childdet);
    109 CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    110     uhub_attach, uhub_detach, uhub_activate, NULL, uhub_childdet);
    111 
    112 USB_MATCH(uhub)
    113 {
    114 	USB_MATCH_START(uhub, uaa);
    115 
    116 	DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
    117 	/*
    118 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    119 	 * so we just ignore the subclass.
    120 	 */
    121 	if (uaa->class == UDCLASS_HUB)
    122 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    123 	return (UMATCH_NONE);
    124 }
    125 
    126 USB_ATTACH(uhub)
    127 {
    128 	USB_ATTACH_START(uhub, sc, uaa);
    129 	usbd_device_handle dev = uaa->device;
    130 	char *devinfop;
    131 	usbd_status err;
    132 	struct usbd_hub *hub = NULL;
    133 	usb_device_request_t req;
    134 	usb_hub_descriptor_t hubdesc;
    135 	int p, port, nports, nremov, pwrdly;
    136 	usbd_interface_handle iface;
    137 	usb_endpoint_descriptor_t *ed;
    138 #if 0 /* notyet */
    139 	struct usbd_tt *tts = NULL;
    140 #endif
    141 
    142 	DPRINTFN(1,("uhub_attach\n"));
    143 	sc->sc_dev = self;
    144 	sc->sc_hub = dev;
    145 	sc->sc_proto = uaa->proto;
    146 
    147 	devinfop = usbd_devinfo_alloc(dev, 1);
    148 	aprint_naive("\n");
    149 	aprint_normal(": %s\n", devinfop);
    150 	usbd_devinfo_free(devinfop);
    151 
    152 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    153 		aprint_normal_dev(self, "%s transaction translator%s\n",
    154 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    155 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    156 	}
    157 
    158 	err = usbd_set_config_index(dev, 0, 1);
    159 	if (err) {
    160 		DPRINTF(("%s: configuration failed, error=%s\n",
    161 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    162 		USB_ATTACH_ERROR_RETURN;
    163 	}
    164 
    165 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    166 		aprint_error_dev(self,
    167 		    "hub depth (%d) exceeded, hub ignored\n",
    168 		    USB_HUB_MAX_DEPTH);
    169 		USB_ATTACH_ERROR_RETURN;
    170 	}
    171 
    172 	/* Get hub descriptor. */
    173 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    174 	req.bRequest = UR_GET_DESCRIPTOR;
    175 	USETW2(req.wValue, UDESC_HUB, 0);
    176 	USETW(req.wIndex, 0);
    177 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    178 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    179 	err = usbd_do_request(dev, &req, &hubdesc);
    180 	nports = hubdesc.bNbrPorts;
    181 	if (!err && nports > 7) {
    182 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    183 		err = usbd_do_request(dev, &req, &hubdesc);
    184 	}
    185 	if (err) {
    186 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    187 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    188 		USB_ATTACH_ERROR_RETURN;
    189 	}
    190 
    191 	for (nremov = 0, port = 1; port <= nports; port++)
    192 		if (!UHD_NOT_REMOV(&hubdesc, port))
    193 			nremov++;
    194 	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
    195 	    nports, nports != 1 ? "s" : "", nremov,
    196 	    dev->self_powered ? "self" : "bus");
    197 
    198 	if (nports == 0) {
    199 		aprint_debug_dev(self, "no ports, hub ignored\n");
    200 		goto bad;
    201 	}
    202 
    203 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    204 		     M_USBDEV, M_NOWAIT);
    205 	if (hub == NULL)
    206 		USB_ATTACH_ERROR_RETURN;
    207 	dev->hub = hub;
    208 	dev->hub->hubsoftc = sc;
    209 	hub->explore = uhub_explore;
    210 	hub->hubdesc = hubdesc;
    211 
    212 	/* Set up interrupt pipe. */
    213 	err = usbd_device2interface_handle(dev, 0, &iface);
    214 	if (err) {
    215 		aprint_error_dev(self, "no interface handle\n");
    216 		goto bad;
    217 	}
    218 
    219 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    220 		err = usbd_set_interface(iface, 1);
    221 		if (err)
    222 			aprint_error_dev(self, "can't enable multiple TTs\n");
    223 	}
    224 
    225 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    226 	if (ed == NULL) {
    227 		aprint_error_dev(self, "no endpoint descriptor\n");
    228 		goto bad;
    229 	}
    230 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    231 		aprint_error_dev(self, "bad interrupt endpoint\n");
    232 		goto bad;
    233 	}
    234 
    235 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    236 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    237 	if (!sc->sc_statusbuf)
    238 		goto bad;
    239 	sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    240 	if (!sc->sc_status)
    241 		goto bad;
    242 
    243 	/* force initial scan */
    244 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    245 	sc->sc_explorepending = 1;
    246 
    247 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    248 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
    249 		  sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
    250 	if (err) {
    251 		aprint_error_dev(self, "cannot open interrupt pipe\n");
    252 		goto bad;
    253 	}
    254 
    255 	/* Wait with power off for a while. */
    256 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    257 
    258 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    259 
    260 	/*
    261 	 * To have the best chance of success we do things in the exact same
    262 	 * order as Windoze98.  This should not be necessary, but some
    263 	 * devices do not follow the USB specs to the letter.
    264 	 *
    265 	 * These are the events on the bus when a hub is attached:
    266 	 *  Get device and config descriptors (see attach code)
    267 	 *  Get hub descriptor (see above)
    268 	 *  For all ports
    269 	 *     turn on power
    270 	 *     wait for power to become stable
    271 	 * (all below happens in explore code)
    272 	 *  For all ports
    273 	 *     clear C_PORT_CONNECTION
    274 	 *  For all ports
    275 	 *     get port status
    276 	 *     if device connected
    277 	 *        wait 100 ms
    278 	 *        turn on reset
    279 	 *        wait
    280 	 *        clear C_PORT_RESET
    281 	 *        get port status
    282 	 *        proceed with device attachment
    283 	 */
    284 
    285 #if 0
    286 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    287 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    288 			     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
    289 		if (!tts)
    290 			goto bad;
    291 	}
    292 #endif
    293 	/* Set up data structures */
    294 	for (p = 0; p < nports; p++) {
    295 		struct usbd_port *up = &hub->ports[p];
    296 		up->device = NULL;
    297 		up->parent = dev;
    298 		up->portno = p+1;
    299 		if (dev->self_powered)
    300 			/* Self powered hub, give ports maximum current. */
    301 			up->power = USB_MAX_POWER;
    302 		else
    303 			up->power = USB_MIN_POWER;
    304 		up->restartcnt = 0;
    305 		up->reattach = 0;
    306 #if 0
    307 		if (UHUB_IS_HIGH_SPEED(sc)) {
    308 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    309 			up->tt->hub = hub;
    310 		} else {
    311 			up->tt = NULL;
    312 		}
    313 #endif
    314 	}
    315 
    316 	/* XXX should check for none, individual, or ganged power? */
    317 
    318 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    319 	    + USB_EXTRA_POWER_UP_TIME;
    320 	for (port = 1; port <= nports; port++) {
    321 		/* Turn the power on. */
    322 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    323 		if (err)
    324 			aprint_error_dev(self, "port %d power on failed, %s\n",
    325 			    port, usbd_errstr(err));
    326 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    327 	}
    328 
    329 	/* Wait for stable power if we are not a root hub */
    330 	if (dev->powersrc->parent != NULL)
    331 		usbd_delay_ms(dev, pwrdly);
    332 
    333 	/* The usual exploration will finish the setup. */
    334 
    335 	sc->sc_running = 1;
    336 
    337 	if (!pmf_device_register(self, NULL, NULL))
    338 		aprint_error_dev(self, "couldn't establish power handler\n");
    339 
    340 	USB_ATTACH_SUCCESS_RETURN;
    341 
    342  bad:
    343 	if (sc->sc_status)
    344 		free(sc->sc_status, M_USBDEV);
    345 	if (hub)
    346 		free(hub, M_USBDEV);
    347 	dev->hub = NULL;
    348 	USB_ATTACH_ERROR_RETURN;
    349 }
    350 
    351 usbd_status
    352 uhub_explore(usbd_device_handle dev)
    353 {
    354 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    355 	struct uhub_softc *sc = dev->hub->hubsoftc;
    356 	struct usbd_port *up;
    357 	usbd_status err;
    358 	int speed;
    359 	int port;
    360 	int change, status, reconnect;
    361 
    362 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    363 
    364 	if (!sc->sc_running)
    365 		return (USBD_NOT_STARTED);
    366 
    367 	/* Ignore hubs that are too deep. */
    368 	if (dev->depth > USB_HUB_MAX_DEPTH)
    369 		return (USBD_TOO_DEEP);
    370 
    371 	for (port = 1; port <= hd->bNbrPorts; port++) {
    372 		up = &dev->hub->ports[port-1];
    373 
    374 		/* reattach is needed after firmware upload */
    375 		reconnect = up->reattach;
    376 		up->reattach = 0;
    377 
    378 		status = change = 0;
    379 
    380 		/* don't check if no change summary notification */
    381 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    382 			err = usbd_get_port_status(dev, port, &up->status);
    383 			if (err) {
    384 				DPRINTF(("uhub_explore: get port stat failed, "
    385 					 "error=%s\n", usbd_errstr(err)));
    386 				continue;
    387 			}
    388 			status = UGETW(up->status.wPortStatus);
    389 			change = UGETW(up->status.wPortChange);
    390 		}
    391 		if (!change && !reconnect) {
    392 			/* No status change, just do recursive explore. */
    393 			if (up->device != NULL && up->device->hub != NULL)
    394 				up->device->hub->explore(up->device);
    395 			continue;
    396 		}
    397 
    398 		if (change & UPS_C_PORT_ENABLED) {
    399 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    400 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    401 			if (change & UPS_C_CONNECT_STATUS) {
    402 				/* Ignore the port error if the device
    403 				   vanished. */
    404 			} else if (status & UPS_PORT_ENABLED) {
    405 				aprint_error_dev(sc->sc_dev,
    406 				    "illegal enable change, port %d\n", port);
    407 			} else {
    408 				/* Port error condition. */
    409 				if (up->restartcnt) /* no message first time */
    410 					aprint_error_dev(sc->sc_dev,
    411 					    "port error, restarting port %d\n",
    412 					    port);
    413 
    414 				if (up->restartcnt++ < USBD_RESTART_MAX)
    415 					goto disco;
    416 				else
    417 					aprint_error_dev(sc->sc_dev,
    418 					    "port error, giving up port %d\n",
    419 					    port);
    420 			}
    421 		}
    422 
    423 		/* XXX handle overcurrent and resume events! */
    424 
    425 		if (!(change & UPS_C_CONNECT_STATUS))
    426 			continue;
    427 
    428 		/* We have a connect status change, handle it. */
    429 
    430 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    431 			 dev->address, port));
    432 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    433 		/*
    434 		 * If there is already a device on the port the change status
    435 		 * must mean that is has disconnected.  Looking at the
    436 		 * current connect status is not enough to figure this out
    437 		 * since a new unit may have been connected before we handle
    438 		 * the disconnect.
    439 		 */
    440 	disco:
    441 		if (up->device != NULL) {
    442 			/* Disconnected */
    443 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    444 				 "on port %d\n", up->device->address, port));
    445 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    446 			usbd_clear_port_feature(dev, port,
    447 						UHF_C_PORT_CONNECTION);
    448 		}
    449 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    450 			/* Nothing connected, just ignore it. */
    451 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    452 				    "_STATUS\n", port));
    453 			continue;
    454 		}
    455 
    456 		/* Connected */
    457 
    458 		if (!(status & UPS_PORT_POWER))
    459 			aprint_normal_dev(sc->sc_dev,
    460 			    "strange, connected port %d has no power\n", port);
    461 
    462 		/* Wait for maximum device power up time. */
    463 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    464 
    465 		/* Reset port, which implies enabling it. */
    466 		if (usbd_reset_port(dev, port, &up->status)) {
    467 			aprint_error_dev(sc->sc_dev,
    468 			    "port %d reset failed\n", port);
    469 			continue;
    470 		}
    471 		/* Get port status again, it might have changed during reset */
    472 		err = usbd_get_port_status(dev, port, &up->status);
    473 		if (err) {
    474 			DPRINTF(("uhub_explore: get port status failed, "
    475 				 "error=%s\n", usbd_errstr(err)));
    476 			continue;
    477 		}
    478 		status = UGETW(up->status.wPortStatus);
    479 		change = UGETW(up->status.wPortChange);
    480 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    481 			/* Nothing connected, just ignore it. */
    482 #ifdef DIAGNOSTIC
    483 			aprint_debug_dev(sc->sc_dev,
    484 			    "port %d, device disappeared after reset\n", port);
    485 #endif
    486 			continue;
    487 		}
    488 
    489 		/* Figure out device speed */
    490 		if (status & UPS_HIGH_SPEED)
    491 			speed = USB_SPEED_HIGH;
    492 		else if (status & UPS_LOW_SPEED)
    493 			speed = USB_SPEED_LOW;
    494 		else
    495 			speed = USB_SPEED_FULL;
    496 		/* Get device info and set its address. */
    497 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    498 			  dev->depth + 1, speed, port, up);
    499 		/* XXX retry a few times? */
    500 		if (err) {
    501 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    502 				     "error=%s\n", usbd_errstr(err)));
    503 			/* Avoid addressing problems by disabling. */
    504 			/* usbd_reset_port(dev, port, &up->status); */
    505 
    506 			/*
    507 			 * The unit refused to accept a new address, or had
    508 			 * some other serious problem.  Since we cannot leave
    509 			 * at 0 we have to disable the port instead.
    510 			 */
    511 			aprint_error_dev(sc->sc_dev,
    512 			    "device problem, disabling port %d\n", port);
    513 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    514 		} else {
    515 			/* The port set up succeeded, reset error count. */
    516 			up->restartcnt = 0;
    517 
    518 			if (up->device->hub)
    519 				up->device->hub->explore(up->device);
    520 		}
    521 	}
    522 	/* enable status change notifications again */
    523 	sc->sc_explorepending = 0;
    524 	return (USBD_NORMAL_COMPLETION);
    525 }
    526 
    527 #if defined(__NetBSD__) || defined(__OpenBSD__)
    528 int
    529 uhub_activate(device_ptr_t self, enum devact act)
    530 {
    531 	struct uhub_softc *sc = device_private(self);
    532 	struct usbd_hub *hub = sc->sc_hub->hub;
    533 	usbd_device_handle dev;
    534 	int nports, port, i;
    535 
    536 	switch (act) {
    537 	case DVACT_ACTIVATE:
    538 		return (EOPNOTSUPP);
    539 
    540 	case DVACT_DEACTIVATE:
    541 		if (hub == NULL) /* malfunctioning hub */
    542 			break;
    543 		nports = hub->hubdesc.bNbrPorts;
    544 		for(port = 0; port < nports; port++) {
    545 			dev = hub->ports[port].device;
    546 			if (!dev)
    547 				continue;
    548 			for (i = 0; i < dev->subdevlen; i++)
    549 				if (dev->subdevs[i])
    550 					config_deactivate(dev->subdevs[i]);
    551 		}
    552 		break;
    553 	}
    554 	return (0);
    555 }
    556 #endif
    557 
    558 /*
    559  * Called from process context when the hub is gone.
    560  * Detach all devices on active ports.
    561  */
    562 USB_DETACH(uhub)
    563 {
    564 	USB_DETACH_START(uhub, sc);
    565 	struct usbd_hub *hub = sc->sc_hub->hub;
    566 	struct usbd_port *rup;
    567 	int port, nports;
    568 
    569 #if defined(__NetBSD__) || defined(__OpenBSD__)
    570 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    571 #elif defined(__FreeBSD__)
    572 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    573 #endif
    574 
    575 	if (hub == NULL)		/* Must be partially working */
    576 		return (0);
    577 
    578 	pmf_device_deregister(self);
    579 	usbd_abort_pipe(sc->sc_ipipe);
    580 	usbd_close_pipe(sc->sc_ipipe);
    581 
    582 	nports = hub->hubdesc.bNbrPorts;
    583 	for(port = 0; port < nports; port++) {
    584 		rup = &hub->ports[port];
    585 		if (rup->device)
    586 			usb_disconnect_port(rup, self);
    587 	}
    588 
    589 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    590 			   USBDEV(sc->sc_dev));
    591 
    592 #if 0
    593 	if (hub->ports[0].tt)
    594 		free(hub->ports[0].tt, M_USBDEV);
    595 #endif
    596 	free(hub, M_USBDEV);
    597 	sc->sc_hub->hub = NULL;
    598 	if (sc->sc_status)
    599 		free(sc->sc_status, M_USBDEV);
    600 
    601 	return (0);
    602 }
    603 
    604 /* Called when a device has been detached from it */
    605 void
    606 uhub_childdet(device_t self, device_t child)
    607 {
    608 	struct uhub_softc *sc = device_private(self);
    609 	usbd_device_handle devhub = sc->sc_hub;
    610 	usbd_device_handle dev;
    611 	int nports;
    612 	int port;
    613 	int i;
    614 
    615 	if (!devhub->hub)
    616 		/* should never happen; children are only created after init */
    617 		panic("hub not fully initialised, but child deleted?");
    618 
    619 	nports = devhub->hub->hubdesc.bNbrPorts;
    620 	for (port = 0; port < nports; port++) {
    621 		dev = devhub->hub->ports[port].device;
    622 		if (!dev)
    623 			continue;
    624 		for (i = 0; i < dev->subdevlen; i++) {
    625 			if (dev->subdevs[i] == child) {
    626 				dev->subdevs[i] = NULL;
    627 				return;
    628 			}
    629 		}
    630 	}
    631 	KASSERT(false);
    632 }
    633 
    634 
    635 /*
    636  * Hub interrupt.
    637  * This an indication that some port has changed status.
    638  * Notify the bus event handler thread that we need
    639  * to be explored again.
    640  */
    641 void
    642 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
    643     usbd_status status)
    644 {
    645 	struct uhub_softc *sc = addr;
    646 
    647 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    648 
    649 	if (status == USBD_STALLED)
    650 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    651 	else if (status == USBD_NORMAL_COMPLETION &&
    652 		 !sc->sc_explorepending) {
    653 		/*
    654 		 * Make sure the status is not overwritten in between.
    655 		 * XXX we should suspend the pipe instead
    656 		 */
    657 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
    658 		sc->sc_explorepending = 1;
    659 		usb_needs_explore(sc->sc_hub);
    660 	}
    661 	/*
    662 	 * XXX workaround for broken implementation of the interrupt
    663 	 * pipe in EHCI root hub emulation which doesn't resend
    664 	 * status change notifications until handled: force a rescan
    665 	 * of the ports we touched in the last run
    666 	 */
    667 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
    668 	    device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
    669 		usb_needs_explore(sc->sc_hub);
    670 }
    671 
    672 #if defined(__FreeBSD__)
    673 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    674 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    675 #endif
    676