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