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