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