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