Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.126.2.17
      1 /*	$NetBSD: uhub.c,v 1.126.2.17 2016/01/02 13:54:38 skrll Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
      3 /*	$OpenBSD: uhub.c,v 1.86 2015/06/29 18:27:40 mpi Exp $ */
      4 
      5 /*
      6  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Lennart Augustsson (lennart (at) augustsson.net) at
     11  * Carlstedt Research & Technology.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.126.2.17 2016/01/02 13:54:38 skrll Exp $");
     41 
     42 #include <sys/param.h>
     43 
     44 #include <sys/bus.h>
     45 #include <sys/device.h>
     46 #include <sys/kernel.h>
     47 #include <sys/kmem.h>
     48 #include <sys/proc.h>
     49 #include <sys/sysctl.h>
     50 #include <sys/systm.h>
     51 
     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_statuspend;
    109 	uint8_t			*sc_status;
    110 	size_t			sc_statuslen;
    111 	int			sc_explorepending;
    112 	int		sc_isehciroothub; /* see comment in uhub_intr() */
    113 
    114 	u_char			sc_running;
    115 };
    116 
    117 #define UHUB_IS_HIGH_SPEED(sc) \
    118     ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT)
    119 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
    120 
    121 #define PORTSTAT_ISSET(sc, port) \
    122 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
    123 
    124 Static usbd_status uhub_explore(struct usbd_device *);
    125 Static void uhub_intr(struct usbd_xfer *, void *, usbd_status);
    126 
    127 
    128 /*
    129  * We need two attachment points:
    130  * hub to usb and hub to hub
    131  * Every other driver only connects to hubs
    132  */
    133 
    134 int uhub_match(device_t, cfdata_t, void *);
    135 void uhub_attach(device_t, device_t, void *);
    136 int uhub_rescan(device_t, const char *, const int *);
    137 void uhub_childdet(device_t, device_t);
    138 int uhub_detach(device_t, int);
    139 extern struct cfdriver uhub_cd;
    140 CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
    141     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
    142     DVF_DETACH_SHUTDOWN);
    143 CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
    144     uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
    145 
    146 /*
    147  * Setting this to 1 makes sure than an uhub attaches even at higher
    148  * priority than ugen when ugen_override is set to 1.  This allows to
    149  * probe the whole USB bus and attach functions with ugen.
    150  */
    151 int uhub_ubermatch = 0;
    152 
    153 static usbd_status
    154 usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed)
    155 {
    156 	usb_device_request_t req;
    157 	usbd_status err;
    158 	int nports;
    159 
    160 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    161 
    162 	/* don't issue UDESC_HUB to SS hub, or it would stall */
    163 	if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) {
    164 		usb_hub_ss_descriptor_t hssd;
    165 		int rmvlen;
    166 
    167 		memset(&hssd, 0, sizeof(hssd));
    168 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    169 		req.bRequest = UR_GET_DESCRIPTOR;
    170 		USETW2(req.wValue, UDESC_SS_HUB, 0);
    171 		USETW(req.wIndex, 0);
    172 		USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE);
    173 		DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0);
    174 		err = usbd_do_request(dev, &req, &hssd);
    175 		nports = hssd.bNbrPorts;
    176 		if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) {
    177 			DPRINTF("num of ports %d exceeds maxports %d",
    178 			    nports, UHD_SS_NPORTS_MAX, 0, 0);
    179 			nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX;
    180 		}
    181 		rmvlen = (nports + 7) / 8;
    182 		hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE +
    183 		    (rmvlen > 1 ? rmvlen : 1) - 1;
    184 		memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen);
    185 		hd->bDescriptorType		= hssd.bDescriptorType;
    186 		hd->bNbrPorts			= hssd.bNbrPorts;
    187 		hd->wHubCharacteristics[0]	= hssd.wHubCharacteristics[0];
    188 		hd->wHubCharacteristics[1]	= hssd.wHubCharacteristics[1];
    189 		hd->bPwrOn2PwrGood		= hssd.bPwrOn2PwrGood;
    190 		hd->bHubContrCurrent		= hssd.bHubContrCurrent;
    191 	} else {
    192 		req.bmRequestType = UT_READ_CLASS_DEVICE;
    193 		req.bRequest = UR_GET_DESCRIPTOR;
    194 		USETW2(req.wValue, UDESC_HUB, 0);
    195 		USETW(req.wIndex, 0);
    196 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    197 		DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0);
    198 		err = usbd_do_request(dev, &req, hd);
    199 		nports = hd->bNbrPorts;
    200 		if (!err && nports > 7) {
    201 			USETW(req.wLength,
    202 			    USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    203 			err = usbd_do_request(dev, &req, hd);
    204 		}
    205 	}
    206 
    207 	return err;
    208 }
    209 
    210 static usbd_status
    211 usbd_set_hub_depth(struct usbd_device *dev, int depth)
    212 {
    213 	usb_device_request_t req;
    214 
    215 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    216 	req.bRequest = UR_SET_HUB_DEPTH;
    217 	USETW(req.wValue, depth);
    218 	USETW(req.wIndex, 0);
    219 	USETW(req.wLength, 0);
    220 	return usbd_do_request(dev, &req, 0);
    221 }
    222 
    223 int
    224 uhub_match(device_t parent, cfdata_t match, void *aux)
    225 {
    226 	struct usb_attach_arg *uaa = aux;
    227 	int matchvalue;
    228 
    229 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    230 
    231 	if (uhub_ubermatch)
    232 		matchvalue = UMATCH_HIGHEST+1;
    233 	else
    234 		matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
    235 
    236 	DPRINTFN(5, "uaa=%p", uaa, 0, 0, 0);
    237 	/*
    238 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    239 	 * so we just ignore the subclass.
    240 	 */
    241 	if (uaa->uaa_class == UDCLASS_HUB)
    242 		return matchvalue;
    243 	return UMATCH_NONE;
    244 }
    245 
    246 void
    247 uhub_attach(device_t parent, device_t self, void *aux)
    248 {
    249 	struct uhub_softc *sc = device_private(self);
    250 	struct usb_attach_arg *uaa = aux;
    251 	struct usbd_device *dev = uaa->uaa_device;
    252 	char *devinfop;
    253 	usbd_status err;
    254 	struct usbd_hub *hub = NULL;
    255 	usb_hub_descriptor_t hubdesc;
    256 	int p, port, nports, nremov, pwrdly;
    257 	struct usbd_interface *iface;
    258 	usb_endpoint_descriptor_t *ed;
    259 #if 0 /* notyet */
    260 	struct usbd_tt *tts = NULL;
    261 #endif
    262 
    263 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    264 
    265 	sc->sc_dev = self;
    266 	sc->sc_hub = dev;
    267 	sc->sc_proto = uaa->uaa_proto;
    268 
    269 	devinfop = usbd_devinfo_alloc(dev, 1);
    270 	aprint_naive("\n");
    271 	aprint_normal(": %s\n", devinfop);
    272 	usbd_devinfo_free(devinfop);
    273 
    274 	if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    275 		aprint_normal_dev(self, "%s transaction translator%s\n",
    276 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    277 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    278 	}
    279 
    280 	err = usbd_set_config_index(dev, 0, 1);
    281 	if (err) {
    282 		DPRINTF("configuration failed, sc %p error %d", sc, err, 0, 0);
    283 		return;
    284 	}
    285 
    286 	if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
    287 		aprint_error_dev(self,
    288 		    "hub depth (%d) exceeded, hub ignored\n",
    289 		    USB_HUB_MAX_DEPTH);
    290 		return;
    291 	}
    292 
    293 	/* Get hub descriptor. */
    294 	memset(&hubdesc, 0, sizeof(hubdesc));
    295 	err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed);
    296 	nports = hubdesc.bNbrPorts;
    297 	if (err) {
    298 		DPRINTF("getting hub descriptor failed, uhub%d error %d",
    299 		    device_unit(self), err, 0, 0);
    300 		return;
    301 	}
    302 
    303 	for (nremov = 0, port = 1; port <= nports; port++)
    304 		if (!UHD_NOT_REMOV(&hubdesc, port))
    305 			nremov++;
    306 	aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
    307 	    nports, nports != 1 ? "s" : "", nremov,
    308 	    dev->ud_selfpowered ? "self" : "bus");
    309 
    310 	if (nports == 0) {
    311 		aprint_debug_dev(self, "no ports, hub ignored\n");
    312 		goto bad;
    313 	}
    314 
    315 	hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    316 	    KM_SLEEP);
    317 	if (hub == NULL)
    318 		return;
    319 	dev->ud_hub = hub;
    320 	dev->ud_hub->uh_hubsoftc = sc;
    321 	hub->uh_explore = uhub_explore;
    322 	hub->uh_hubdesc = hubdesc;
    323 
    324 	if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) {
    325 		aprint_debug_dev(self, "setting hub depth %u\n",
    326 		    dev->ud_depth-1);
    327 		err = usbd_set_hub_depth(dev, dev->ud_depth - 1);
    328 		if (err) {
    329 			aprint_error_dev(self, "can't set depth\n");
    330 			goto bad;
    331 		}
    332 	}
    333 
    334 	/* Set up interrupt pipe. */
    335 	err = usbd_device2interface_handle(dev, 0, &iface);
    336 	if (err) {
    337 		aprint_error_dev(self, "no interface handle\n");
    338 		goto bad;
    339 	}
    340 
    341 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    342 		err = usbd_set_interface(iface, 1);
    343 		if (err)
    344 			aprint_error_dev(self, "can't enable multiple TTs\n");
    345 	}
    346 
    347 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    348 	if (ed == NULL) {
    349 		aprint_error_dev(self, "no endpoint descriptor\n");
    350 		goto bad;
    351 	}
    352 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    353 		aprint_error_dev(self, "bad interrupt endpoint\n");
    354 		goto bad;
    355 	}
    356 
    357 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    358 	sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    359 	if (!sc->sc_statusbuf)
    360 		goto bad;
    361 	sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP);
    362 	if (!sc->sc_statuspend)
    363 		goto bad;
    364 	sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
    365 	if (!sc->sc_status)
    366 		goto bad;
    367 	if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
    368 		sc->sc_isehciroothub = 1;
    369 
    370 	/* force initial scan */
    371 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    372 	sc->sc_explorepending = 1;
    373 
    374 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    375 		  USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
    376 		  sc->sc_statusbuf, sc->sc_statuslen,
    377 		  uhub_intr, USBD_DEFAULT_INTERVAL);
    378 	if (err) {
    379 		aprint_error_dev(self, "cannot open interrupt pipe\n");
    380 		goto bad;
    381 	}
    382 
    383 	/* Wait with power off for a while. */
    384 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    385 
    386 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
    387 
    388 	/*
    389 	 * To have the best chance of success we do things in the exact same
    390 	 * order as Windows 98.  This should not be necessary, but some
    391 	 * devices do not follow the USB specs to the letter.
    392 	 *
    393 	 * These are the events on the bus when a hub is attached:
    394 	 *  Get device and config descriptors (see attach code)
    395 	 *  Get hub descriptor (see above)
    396 	 *  For all ports
    397 	 *     turn on power
    398 	 *     wait for power to become stable
    399 	 * (all below happens in explore code)
    400 	 *  For all ports
    401 	 *     clear C_PORT_CONNECTION
    402 	 *  For all ports
    403 	 *     get port status
    404 	 *     if device connected
    405 	 *        wait 100 ms
    406 	 *        turn on reset
    407 	 *        wait
    408 	 *        clear C_PORT_RESET
    409 	 *        get port status
    410 	 *        proceed with device attachment
    411 	 */
    412 
    413 #if 0
    414 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    415 		tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    416 			     sizeof(struct usbd_tt), KM_SLEEP);
    417 		if (!tts)
    418 			goto bad;
    419 	}
    420 #endif
    421 	/* Set up data structures */
    422 	for (p = 0; p < nports; p++) {
    423 		struct usbd_port *up = &hub->uh_ports[p];
    424 		up->up_dev = NULL;
    425 		up->up_parent = dev;
    426 		up->up_portno = p+1;
    427 		if (dev->ud_selfpowered)
    428 			/* Self powered hub, give ports maximum current. */
    429 			up->up_power = USB_MAX_POWER;
    430 		else
    431 			up->up_power = USB_MIN_POWER;
    432 		up->up_restartcnt = 0;
    433 		up->up_reattach = 0;
    434 #if 0
    435 		if (UHUB_IS_HIGH_SPEED(sc)) {
    436 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    437 			up->tt->hub = hub;
    438 		} else {
    439 			up->tt = NULL;
    440 		}
    441 #endif
    442 	}
    443 
    444 	/* XXX should check for none, individual, or ganged power? */
    445 
    446 	pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    447 	    + USB_EXTRA_POWER_UP_TIME;
    448 	for (port = 1; port <= nports; port++) {
    449 		/* Turn the power on. */
    450 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    451 		if (err)
    452 			aprint_error_dev(self, "port %d power on failed, %s\n",
    453 			    port, usbd_errstr(err));
    454 		DPRINTF("uhub%d turn on port %d power", device_unit(self),
    455 		    port, 0, 0);
    456 	}
    457 
    458 	/* Wait for stable power if we are not a root hub */
    459 	if (dev->ud_powersrc->up_parent != NULL)
    460 		usbd_delay_ms(dev, pwrdly);
    461 
    462 	/* The usual exploration will finish the setup. */
    463 
    464 	sc->sc_running = 1;
    465 
    466 	if (!pmf_device_register(self, NULL, NULL))
    467 		aprint_error_dev(self, "couldn't establish power handler\n");
    468 
    469 	return;
    470 
    471  bad:
    472 	if (sc->sc_status)
    473 		kmem_free(sc->sc_status, sc->sc_statuslen);
    474 	if (sc->sc_statuspend)
    475 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    476 	if (sc->sc_statusbuf)
    477 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    478 	if (hub)
    479 		kmem_free(hub,
    480 		    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    481 	dev->ud_hub = NULL;
    482 	return;
    483 }
    484 
    485 usbd_status
    486 uhub_explore(struct usbd_device *dev)
    487 {
    488 	usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
    489 	struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
    490 	struct usbd_port *up;
    491 	usbd_status err;
    492 	int speed;
    493 	int port;
    494 	int change, status, reconnect;
    495 
    496 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    497 
    498 	DPRINTFN(10, "uhub%d dev=%p addr=%d speed=%u",
    499 	    device_unit(sc->sc_dev), dev, dev->ud_addr, dev->ud_speed);
    500 
    501 	if (!sc->sc_running)
    502 		return USBD_NOT_STARTED;
    503 
    504 	/* Ignore hubs that are too deep. */
    505 	if (dev->ud_depth > USB_HUB_MAX_DEPTH)
    506 		return USBD_TOO_DEEP;
    507 
    508 	if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
    509 		usb_hub_status_t hs;
    510 
    511 		err = usbd_get_hub_status(dev, &hs);
    512 		if (err) {
    513 			DPRINTF("uhub%d get hub status failed, err %d",
    514 			    device_unit(sc->sc_dev), err, 0, 0);
    515 		} else {
    516 			/* just acknowledge */
    517 			status = UGETW(hs.wHubStatus);
    518 			change = UGETW(hs.wHubChange);
    519 			DPRINTF("uhub%d s/c=%x/%x", device_unit(sc->sc_dev),
    520 			    status, change, 0);
    521 
    522 			if (change & UHS_LOCAL_POWER)
    523 				usbd_clear_hub_feature(dev,
    524 						       UHF_C_HUB_LOCAL_POWER);
    525 			if (change & UHS_OVER_CURRENT)
    526 				usbd_clear_hub_feature(dev,
    527 						       UHF_C_HUB_OVER_CURRENT);
    528 		}
    529 	}
    530 
    531 	for (port = 1; port <= hd->bNbrPorts; port++) {
    532 		up = &dev->ud_hub->uh_ports[port-1];
    533 
    534 		/* reattach is needed after firmware upload */
    535 		reconnect = up->up_reattach;
    536 		up->up_reattach = 0;
    537 
    538 		status = change = 0;
    539 
    540 		/* don't check if no change summary notification */
    541 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    542 			err = usbd_get_port_status(dev, port, &up->up_status);
    543 			if (err) {
    544 				DPRINTF("uhub%d get port stat failed, err %d",
    545 				    device_unit(sc->sc_dev), err, 0, 0);
    546 				continue;
    547 			}
    548 			status = UGETW(up->up_status.wPortStatus);
    549 			change = UGETW(up->up_status.wPortChange);
    550 
    551 			DPRINTF("uhub%d port %d: s/c=%x/%x",
    552 			    device_unit(sc->sc_dev), port, status, change);
    553 		}
    554 		if (!change && !reconnect) {
    555 			/* No status change, just do recursive explore. */
    556 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    557 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    558 			continue;
    559 		}
    560 
    561 		if (change & UPS_C_PORT_ENABLED) {
    562 			DPRINTF("uhub%d port %d C_PORT_ENABLED",
    563 			    device_unit(sc->sc_dev), port, 0, 0);
    564 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    565 			if (change & UPS_C_CONNECT_STATUS) {
    566 				/* Ignore the port error if the device
    567 				   vanished. */
    568 			} else if (status & UPS_PORT_ENABLED) {
    569 				aprint_error_dev(sc->sc_dev,
    570 				    "illegal enable change, port %d\n", port);
    571 			} else {
    572 				/* Port error condition. */
    573 				if (up->up_restartcnt) /* no message first time */
    574 					aprint_error_dev(sc->sc_dev,
    575 					    "port error, restarting port %d\n",
    576 					    port);
    577 
    578 				if (up->up_restartcnt++ < USBD_RESTART_MAX)
    579 					goto disco;
    580 				else
    581 					aprint_error_dev(sc->sc_dev,
    582 					    "port error, giving up port %d\n",
    583 					    port);
    584 			}
    585 		}
    586 		if (change & UPS_C_PORT_RESET) {
    587 			/*
    588 			 * some xHCs set PortResetChange instead of CSC
    589 			 * when port is reset.
    590 			 */
    591 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    592 				change |= UPS_C_CONNECT_STATUS;
    593 			}
    594 			usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
    595 		}
    596 		if (change & UPS_C_BH_PORT_RESET) {
    597 			/*
    598 			 * some xHCs set WarmResetChange instead of CSC
    599 			 * when port is reset.
    600 			 */
    601 			if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
    602 				change |= UPS_C_CONNECT_STATUS;
    603 			}
    604 			usbd_clear_port_feature(dev, port,
    605 			    UHF_C_BH_PORT_RESET);
    606 		}
    607 		if (change & UPS_C_PORT_LINK_STATE)
    608 			usbd_clear_port_feature(dev, port,
    609 			    UHF_C_PORT_LINK_STATE);
    610 		if (change & UPS_C_PORT_CONFIG_ERROR)
    611 			usbd_clear_port_feature(dev, port,
    612 			    UHF_C_PORT_CONFIG_ERROR);
    613 
    614 		/* XXX handle overcurrent and resume events! */
    615 
    616 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
    617 			/* No status change, just do recursive explore. */
    618 			if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
    619 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    620 			continue;
    621 		}
    622 
    623 		/* We have a connect status change, handle it. */
    624 
    625 		DPRINTF("uhub%d status change port %d", device_unit(sc->sc_dev),
    626 		    port, 0, 0);
    627 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    628 		/*
    629 		 * If there is already a device on the port the change status
    630 		 * must mean that is has disconnected.  Looking at the
    631 		 * current connect status is not enough to figure this out
    632 		 * since a new unit may have been connected before we handle
    633 		 * the disconnect.
    634 		 */
    635 	disco:
    636 		if (up->up_dev != NULL) {
    637 			/* Disconnected */
    638 			DPRINTF("uhub%d device addr=%d disappeared on port %d",
    639 			    device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
    640 			    0);
    641 
    642 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    643 			usbd_clear_port_feature(dev, port,
    644 						UHF_C_PORT_CONNECTION);
    645 			continue;
    646 		}
    647 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    648 			/* Nothing connected, just ignore it. */
    649 			DPRINTFN(3, "uhub%d port %d !CURRENT_CONNECT_STATUS",
    650 			    device_unit(sc->sc_dev), port, 0, 0);
    651 			usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
    652 			usbd_clear_port_feature(dev, port,
    653 						UHF_C_PORT_CONNECTION);
    654 			continue;
    655 		}
    656 
    657 		/* Connected */
    658 		DPRINTF("unit %d dev->speed=%u dev->depth=%u",
    659 		    device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);
    660 
    661 		/* Wait for maximum device power up time. */
    662 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    663 
    664 		/* Reset port, which implies enabling it. */
    665 		if (usbd_reset_port(dev, port, &up->up_status)) {
    666 			aprint_error_dev(sc->sc_dev,
    667 			    "port %d reset failed\n", port);
    668 			continue;
    669 		}
    670 		/* Get port status again, it might have changed during reset */
    671 		err = usbd_get_port_status(dev, port, &up->up_status);
    672 		if (err) {
    673 			DPRINTF("uhub%d port %d get port status failed, "
    674 			    "err %d", device_unit(sc->sc_dev), port, err, 0);
    675 			continue;
    676 		}
    677 		status = UGETW(up->up_status.wPortStatus);
    678 		change = UGETW(up->up_status.wPortChange);
    679 		DPRINTF("hub %d port %d after reset: s/c=%x/%x",
    680 		    device_unit(sc->sc_dev), port, status, change);
    681 
    682 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    683 			/* Nothing connected, just ignore it. */
    684 #ifdef DIAGNOSTIC
    685 			aprint_debug_dev(sc->sc_dev,
    686 			    "port %d, device disappeared after reset\n", port);
    687 #endif
    688 			continue;
    689 		}
    690 		if (!(status & UPS_PORT_ENABLED)) {
    691 			/* Not allowed send/receive packet. */
    692 #ifdef DIAGNOSTIC
    693 			printf("%s: port %d, device not enabled\n",
    694 			       device_xname(sc->sc_dev), port);
    695 #endif
    696 			continue;
    697 		}
    698 		/* port reset may cause Warm Reset Change, drop it. */
    699 		if (change & UPS_C_BH_PORT_RESET)
    700 			usbd_clear_port_feature(dev, port,
    701 			    UHF_C_BH_PORT_RESET);
    702 
    703 		/*
    704 		 * Figure out device speed from power bit of port status.
    705 		 *  USB 2.0 ch 11.24.2.7.1
    706 		 *  USB 3.1 ch 10.16.2.6.1
    707 		 */
    708 		int sts = status;
    709 		if ((sts & UPS_PORT_POWER) == 0)
    710 			sts &= ~UPS_PORT_POWER_SS;
    711 
    712 		if (sts & UPS_HIGH_SPEED)
    713 			speed = USB_SPEED_HIGH;
    714 		else if (sts & UPS_LOW_SPEED)
    715 			speed = USB_SPEED_LOW;
    716 		else {
    717 			/*
    718 			 * If there is no power bit set, it is certainly
    719 			 * a Super Speed device, so use the speed of its
    720 			 * parent hub.
    721 			 */
    722 			if (sts & UPS_PORT_POWER)
    723 				speed = USB_SPEED_FULL;
    724 			else
    725 				speed = dev->ud_speed;
    726 		}
    727 
    728 		/*
    729 		 * Reduce the speed, otherwise we won't setup the proper
    730 		 * transfer methods.
    731 		 */
    732 		if (speed > dev->ud_speed)
    733 			speed = dev->ud_speed;
    734 
    735 		DPRINTF("uhub%d speed %u", device_unit(sc->sc_dev), speed, 0,
    736 		    0);
    737 
    738 		/*
    739 		 * To check whether port has power,
    740 		 *  check UPS_PORT_POWER_SS bit if port speed is SS, and
    741 		 *  check UPS_PORT_POWER bit if port speed is HS/FS/LS.
    742 		 */
    743 		if (USB_IS_SS(speed)) {
    744 			/* SS hub port */
    745 			if (!(status & UPS_PORT_POWER_SS))
    746 				aprint_normal_dev(sc->sc_dev,
    747 				    "strange, connected port %d has no power\n",
    748 				    port);
    749 		} else {
    750 			/* HS/FS/LS hub port */
    751 			if (!(status & UPS_PORT_POWER))
    752 				aprint_normal_dev(sc->sc_dev,
    753 				    "strange, connected port %d has no power\n",
    754 				    port);
    755 		}
    756 
    757 		/* Get device info and set its address. */
    758 		err = usbd_new_device(sc->sc_dev, dev->ud_bus,
    759 			  dev->ud_depth + 1, speed, port, up);
    760 		/* XXX retry a few times? */
    761 		if (err) {
    762 			DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
    763 			    0);
    764 			/* Avoid addressing problems by disabling. */
    765 			/* usbd_reset_port(dev, port, &up->status); */
    766 
    767 			/*
    768 			 * The unit refused to accept a new address, or had
    769 			 * some other serious problem.  Since we cannot leave
    770 			 * at 0 we have to disable the port instead.
    771 			 */
    772 			aprint_error_dev(sc->sc_dev,
    773 			    "device problem, disabling port %d\n", port);
    774 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    775 		} else {
    776 			/* The port set up succeeded, reset error count. */
    777 			up->up_restartcnt = 0;
    778 
    779 			if (up->up_dev->ud_hub)
    780 				up->up_dev->ud_hub->uh_explore(up->up_dev);
    781 		}
    782 	}
    783 	/* enable status change notifications again */
    784 	if (!sc->sc_isehciroothub)
    785 		memset(sc->sc_status, 0, sc->sc_statuslen);
    786 	sc->sc_explorepending = 0;
    787 	for (int i = 0; i < sc->sc_statuslen; i++) {
    788 		if (sc->sc_statuspend[i] != 0) {
    789 			memcpy(sc->sc_status, sc->sc_statuspend,
    790 			    sc->sc_statuslen);
    791 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    792 			usb_needs_explore(sc->sc_hub);
    793 			break;
    794 		}
    795 	}
    796 	return USBD_NORMAL_COMPLETION;
    797 }
    798 
    799 /*
    800  * Called from process context when the hub is gone.
    801  * Detach all devices on active ports.
    802  */
    803 int
    804 uhub_detach(device_t self, int flags)
    805 {
    806 	struct uhub_softc *sc = device_private(self);
    807 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    808 	struct usbd_port *rup;
    809 	int nports, port, rc;
    810 
    811 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    812 
    813 	DPRINTF("uhub%d flags=%d", device_unit(self), flags, 0, 0);
    814 
    815 	if (hub == NULL)		/* Must be partially working */
    816 		return 0;
    817 
    818 	/* XXXSMP usb */
    819 	KERNEL_LOCK(1, curlwp);
    820 
    821 	nports = hub->uh_hubdesc.bNbrPorts;
    822 	for(port = 0; port < nports; port++) {
    823 		rup = &hub->uh_ports[port];
    824 		if (rup->up_dev == NULL)
    825 			continue;
    826 		if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
    827 			/* XXXSMP usb */
    828 			KERNEL_UNLOCK_ONE(curlwp);
    829 
    830 			return rc;
    831 		}
    832 	}
    833 
    834 	pmf_device_deregister(self);
    835 	usbd_abort_pipe(sc->sc_ipipe);
    836 	usbd_close_pipe(sc->sc_ipipe);
    837 
    838 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
    839 
    840 #if 0
    841 	if (hub->ports[0].tt)
    842 		kmem_free(hub->ports[0].tt,
    843 		    (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    844 		    sizeof(struct usbd_tt));
    845 #endif
    846 	kmem_free(hub,
    847 	    sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
    848 	sc->sc_hub->ud_hub = NULL;
    849 	if (sc->sc_status)
    850 		kmem_free(sc->sc_status, sc->sc_statuslen);
    851 	if (sc->sc_statuspend)
    852 		kmem_free(sc->sc_statuspend, sc->sc_statuslen);
    853 	if (sc->sc_statusbuf)
    854 		kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
    855 
    856 	/* XXXSMP usb */
    857 	KERNEL_UNLOCK_ONE(curlwp);
    858 
    859 	return 0;
    860 }
    861 
    862 int
    863 uhub_rescan(device_t self, const char *ifattr, const int *locators)
    864 {
    865 	struct uhub_softc *sc = device_private(self);
    866 	struct usbd_hub *hub = sc->sc_hub->ud_hub;
    867 	struct usbd_device *dev;
    868 	int port;
    869 
    870 	for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) {
    871 		dev = hub->uh_ports[port].up_dev;
    872 		if (dev == NULL)
    873 			continue;
    874 		usbd_reattach_device(sc->sc_dev, dev, port, locators);
    875 	}
    876 	return 0;
    877 }
    878 
    879 /* Called when a device has been detached from it */
    880 void
    881 uhub_childdet(device_t self, device_t child)
    882 {
    883 	struct uhub_softc *sc = device_private(self);
    884 	struct usbd_device *devhub = sc->sc_hub;
    885 	struct usbd_device *dev;
    886 	int nports;
    887 	int port;
    888 	int i;
    889 
    890 	if (!devhub->ud_hub)
    891 		/* should never happen; children are only created after init */
    892 		panic("hub not fully initialised, but child deleted?");
    893 
    894 	nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
    895 	for (port = 0; port < nports; port++) {
    896 		dev = devhub->ud_hub->uh_ports[port].up_dev;
    897 		if (!dev || dev->ud_subdevlen == 0)
    898 			continue;
    899 		for (i = 0; i < dev->ud_subdevlen; i++) {
    900 			if (dev->ud_subdevs[i] == child) {
    901 				dev->ud_subdevs[i] = NULL;
    902 				dev->ud_nifaces_claimed--;
    903 			}
    904 		}
    905 		if (dev->ud_nifaces_claimed == 0) {
    906 			kmem_free(dev->ud_subdevs,
    907 			    dev->ud_subdevlen * sizeof(device_t));
    908 			dev->ud_subdevs = NULL;
    909 			dev->ud_subdevlen = 0;
    910 		}
    911 	}
    912 }
    913 
    914 
    915 /*
    916  * Hub interrupt.
    917  * This an indication that some port has changed status.
    918  * Notify the bus event handler thread that we need
    919  * to be explored again.
    920  */
    921 void
    922 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    923 {
    924 	struct uhub_softc *sc = addr;
    925 
    926 	UHUBHIST_FUNC(); UHUBHIST_CALLED();
    927 
    928 	DPRINTFN(5, "uhub%d", device_unit(sc->sc_dev), 0, 0, 0);
    929 
    930 	if (status == USBD_STALLED)
    931 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    932 	else if (status == USBD_NORMAL_COMPLETION) {
    933 		int i;
    934 
    935 		/* merge port bitmap into pending interrupts list */
    936 		for (i = 0; i < sc->sc_statuslen; i++)
    937 			sc->sc_statuspend[i] |= sc->sc_statusbuf[i];
    938 
    939 		if (!sc->sc_explorepending) {
    940 			/*
    941 			 * Make sure the status is not overwritten in between.
    942 			 * XXX we should suspend the pipe instead
    943 			 */
    944 			sc->sc_explorepending = 1;
    945 			memcpy(sc->sc_status, sc->sc_statuspend,
    946 			    sc->sc_statuslen);
    947 			memset(sc->sc_statuspend, 0, sc->sc_statuslen);
    948 			DPRINTFN(5, "uhub%d: exploring ports %02x",
    949 			    device_unit(sc->sc_dev), *sc->sc_status, 0, 0);
    950 			usb_needs_explore(sc->sc_hub);
    951 		}
    952 	}
    953 	/*
    954 	 * XXX workaround for broken implementation of the interrupt
    955 	 * pipe in EHCI root hub emulation which doesn't resend
    956 	 * status change notifications until handled: force a rescan
    957 	 * of the ports we touched in the last run
    958 	 */
    959 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
    960 	    sc->sc_isehciroothub)
    961 		usb_needs_explore(sc->sc_hub);
    962 }
    963