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