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