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