Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.89.8.2
      1 /*	$NetBSD: uhub.c,v 1.89.8.2 2007/08/07 01:00:59 jmcneill 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  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.89.8.2 2007/08/07 01:00:59 jmcneill Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/malloc.h>
     52 #if defined(__NetBSD__) || defined(__OpenBSD__)
     53 #include <sys/device.h>
     54 #include <sys/proc.h>
     55 #elif defined(__FreeBSD__)
     56 #include <sys/module.h>
     57 #include <sys/bus.h>
     58 #include "bus_if.h"
     59 #endif
     60 
     61 #include <machine/bus.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdi_util.h>
     66 #include <dev/usb/usbdivar.h>
     67 
     68 #ifdef UHUB_DEBUG
     69 #define DPRINTF(x)	if (uhubdebug) logprintf x
     70 #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
     71 int	uhubdebug = 0;
     72 #else
     73 #define DPRINTF(x)
     74 #define DPRINTFN(n,x)
     75 #endif
     76 
     77 struct uhub_softc {
     78 	USBBASEDEVICE		sc_dev;		/* base device */
     79 	usbd_device_handle	sc_hub;		/* USB device */
     80 	int			sc_proto;	/* device protocol */
     81 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     82 
     83 	/* XXX second buffer needed because we can't suspend pipes yet */
     84 	u_int8_t		*sc_statusbuf;
     85 	u_int8_t		*sc_status;
     86 	size_t			sc_statuslen;
     87 	int			sc_explorepending;
     88 
     89 	u_char			sc_running;
     90 };
     91 
     92 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
     93 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
     94 
     95 #define PORTSTAT_ISSET(sc, port) \
     96 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
     97 
     98 Static pnp_status_t uhub_power(device_t, pnp_request_t, void *);
     99 Static usbd_status uhub_explore(usbd_device_handle hub);
    100 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
    101 
    102 
    103 /*
    104  * We need two attachment points:
    105  * hub to usb and hub to hub
    106  * Every other driver only connects to hubs
    107  */
    108 
    109 #if defined(__NetBSD__) || defined(__OpenBSD__)
    110 USB_DECLARE_DRIVER(uhub);
    111 #elif defined(__FreeBSD__)
    112 USB_DECLARE_DRIVER_INIT(uhub,
    113 			DEVMETHOD(bus_child_detached, uhub_child_detached));
    114 
    115 /* Create the driver instance for the hub connected to usb case. */
    116 devclass_t uhubroot_devclass;
    117 
    118 Static device_method_t uhubroot_methods[] = {
    119 	DEVMETHOD(device_probe, uhub_match),
    120 	DEVMETHOD(device_attach, uhub_attach),
    121 
    122 	/* detach is not allowed for a root hub */
    123 	{0,0}
    124 };
    125 
    126 Static	driver_t uhubroot_driver = {
    127 	"uhub",
    128 	uhubroot_methods,
    129 	sizeof(struct uhub_softc)
    130 };
    131 #endif
    132 
    133 USB_MATCH(uhub)
    134 {
    135 	USB_MATCH_START(uhub, uaa);
    136 
    137 	DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
    138 	/*
    139 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    140 	 * so we just ignore the subclass.
    141 	 */
    142 	if (uaa->class == UDCLASS_HUB)
    143 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    144 	return (UMATCH_NONE);
    145 }
    146 
    147 USB_ATTACH(uhub)
    148 {
    149 	USB_ATTACH_START(uhub, sc, uaa);
    150 	usbd_device_handle dev = uaa->device;
    151 	char *devinfop;
    152 	usbd_status err;
    153 	struct usbd_hub *hub = NULL;
    154 	usb_device_request_t req;
    155 	usb_hub_descriptor_t hubdesc;
    156 	int p, port, nports, nremov, pwrdly;
    157 	usbd_interface_handle iface;
    158 	usb_endpoint_descriptor_t *ed;
    159 #if 0 /* notyet */
    160 	struct usbd_tt *tts = NULL;
    161 #endif
    162 
    163 	DPRINTFN(1,("uhub_attach\n"));
    164 	sc->sc_hub = dev;
    165 	sc->sc_proto = uaa->proto;
    166 
    167 	devinfop = usbd_devinfo_alloc(dev, 1);
    168 	USB_ATTACH_SETUP;
    169 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    170 	usbd_devinfo_free(devinfop);
    171 
    172 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
    173 		printf("%s: %s transaction translator%s\n",
    174 		       USBDEVNAME(sc->sc_dev),
    175 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
    176 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
    177 	}
    178 
    179 	err = usbd_set_config_index(dev, 0, 1);
    180 	if (err) {
    181 		DPRINTF(("%s: configuration failed, error=%s\n",
    182 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    183 		USB_ATTACH_ERROR_RETURN;
    184 	}
    185 
    186 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    187 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    188 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    189 		USB_ATTACH_ERROR_RETURN;
    190 	}
    191 
    192 	/* Get hub descriptor. */
    193 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    194 	req.bRequest = UR_GET_DESCRIPTOR;
    195 	USETW2(req.wValue, UDESC_HUB, 0);
    196 	USETW(req.wIndex, 0);
    197 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    198 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    199 	err = usbd_do_request(dev, &req, &hubdesc);
    200 	nports = hubdesc.bNbrPorts;
    201 	if (!err && nports > 7) {
    202 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    203 		err = usbd_do_request(dev, &req, &hubdesc);
    204 	}
    205 	if (err) {
    206 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    207 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    208 		USB_ATTACH_ERROR_RETURN;
    209 	}
    210 
    211 	for (nremov = 0, port = 1; port <= nports; port++)
    212 		if (!UHD_NOT_REMOV(&hubdesc, port))
    213 			nremov++;
    214 	printf("%s: %d port%s with %d removable, %s powered\n",
    215 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    216 	       nremov, dev->self_powered ? "self" : "bus");
    217 
    218 	if (nports == 0) {
    219 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
    220 		goto bad;
    221 	}
    222 
    223 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    224 		     M_USBDEV, M_NOWAIT);
    225 	if (hub == NULL)
    226 		USB_ATTACH_ERROR_RETURN;
    227 	dev->hub = hub;
    228 	dev->hub->hubsoftc = sc;
    229 	hub->explore = uhub_explore;
    230 	hub->hubdesc = hubdesc;
    231 
    232 	/* Set up interrupt pipe. */
    233 	err = usbd_device2interface_handle(dev, 0, &iface);
    234 	if (err) {
    235 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    236 		goto bad;
    237 	}
    238 
    239 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
    240 		err = usbd_set_interface(iface, 1);
    241 		if (err)
    242 			printf("%s: can't enable multiple TTs\n",
    243 			       USBDEVNAME(sc->sc_dev));
    244 	}
    245 
    246 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    247 	if (ed == NULL) {
    248 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    249 		goto bad;
    250 	}
    251 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    252 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    253 		goto bad;
    254 	}
    255 
    256 	sc->sc_statuslen = (nports + 1 + 7) / 8;
    257 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    258 	if (!sc->sc_statusbuf)
    259 		goto bad;
    260 	sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
    261 	if (!sc->sc_status)
    262 		goto bad;
    263 
    264 	/* force initial scan */
    265 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
    266 	sc->sc_explorepending = 1;
    267 
    268 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
    269 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
    270 		  sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
    271 	if (err) {
    272 		printf("%s: cannot open interrupt pipe\n",
    273 		       USBDEVNAME(sc->sc_dev));
    274 		goto bad;
    275 	}
    276 
    277 	/* Wait with power off for a while. */
    278 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    279 
    280 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
    281 
    282 	/*
    283 	 * To have the best chance of success we do things in the exact same
    284 	 * order as Windoze98.  This should not be necessary, but some
    285 	 * devices do not follow the USB specs to the letter.
    286 	 *
    287 	 * These are the events on the bus when a hub is attached:
    288 	 *  Get device and config descriptors (see attach code)
    289 	 *  Get hub descriptor (see above)
    290 	 *  For all ports
    291 	 *     turn on power
    292 	 *     wait for power to become stable
    293 	 * (all below happens in explore code)
    294 	 *  For all ports
    295 	 *     clear C_PORT_CONNECTION
    296 	 *  For all ports
    297 	 *     get port status
    298 	 *     if device connected
    299 	 *        wait 100 ms
    300 	 *        turn on reset
    301 	 *        wait
    302 	 *        clear C_PORT_RESET
    303 	 *        get port status
    304 	 *        proceed with device attachment
    305 	 */
    306 
    307 #if 0
    308 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
    309 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
    310 			     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
    311 		if (!tts)
    312 			goto bad;
    313 	}
    314 #endif
    315 	/* Set up data structures */
    316 	for (p = 0; p < nports; p++) {
    317 		struct usbd_port *up = &hub->ports[p];
    318 		up->device = NULL;
    319 		up->parent = dev;
    320 		up->portno = p+1;
    321 		if (dev->self_powered)
    322 			/* Self powered hub, give ports maximum current. */
    323 			up->power = USB_MAX_POWER;
    324 		else
    325 			up->power = USB_MIN_POWER;
    326 		up->restartcnt = 0;
    327 		up->reattach = 0;
    328 #if 0
    329 		if (UHUB_IS_HIGH_SPEED(sc)) {
    330 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
    331 			up->tt->hub = hub;
    332 		} else {
    333 			up->tt = NULL;
    334 		}
    335 #endif
    336 	}
    337 
    338 	/* XXX should check for none, individual, or ganged power? */
    339 
    340 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
    341 	    + USB_EXTRA_POWER_UP_TIME;
    342 	for (port = 1; port <= nports; port++) {
    343 		/* Turn the power on. */
    344 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    345 		if (err)
    346 			printf("%s: port %d power on failed, %s\n",
    347 			       USBDEVNAME(sc->sc_dev), port,
    348 			       usbd_errstr(err));
    349 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
    350 		/* Wait for stable power. */
    351 		usbd_delay_ms(dev, pwrdly);
    352 	}
    353 
    354 	/* The usual exploration will finish the setup. */
    355 
    356 	sc->sc_running = 1;
    357 
    358 	if (pnp_register(&sc->sc_dev, uhub_power) != PNP_STATUS_SUCCESS)
    359 		aprint_error("%s: couldn't establish power handler\n",
    360 		    device_xname(self));
    361 
    362 	USB_ATTACH_SUCCESS_RETURN;
    363 
    364  bad:
    365 	if (sc->sc_status)
    366 		free(sc->sc_status, M_USBDEV);
    367 	if (hub)
    368 		free(hub, M_USBDEV);
    369 	dev->hub = NULL;
    370 	USB_ATTACH_ERROR_RETURN;
    371 }
    372 
    373 usbd_status
    374 uhub_explore(usbd_device_handle dev)
    375 {
    376 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    377 	struct uhub_softc *sc = dev->hub->hubsoftc;
    378 	struct usbd_port *up;
    379 	usbd_status err;
    380 	int speed;
    381 	int port;
    382 	int change, status, reconnect;
    383 
    384 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    385 
    386 	if (!sc->sc_running)
    387 		return (USBD_NOT_STARTED);
    388 
    389 	/* Ignore hubs that are too deep. */
    390 	if (dev->depth > USB_HUB_MAX_DEPTH)
    391 		return (USBD_TOO_DEEP);
    392 
    393 	for (port = 1; port <= hd->bNbrPorts; port++) {
    394 		up = &dev->hub->ports[port-1];
    395 
    396 		/* reattach is needed after firmware upload */
    397 		reconnect = up->reattach;
    398 		up->reattach = 0;
    399 
    400 		status = change = 0;
    401 
    402 		/* don't check if no change summary notification */
    403 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    404 			err = usbd_get_port_status(dev, port, &up->status);
    405 			if (err) {
    406 				DPRINTF(("uhub_explore: get port stat failed, "
    407 					 "error=%s\n", usbd_errstr(err)));
    408 				continue;
    409 			}
    410 			status = UGETW(up->status.wPortStatus);
    411 			change = UGETW(up->status.wPortChange);
    412 		}
    413 		if (!change && !reconnect) {
    414 			/* No status change, just do recursive explore. */
    415 			if (up->device != NULL && up->device->hub != NULL)
    416 				up->device->hub->explore(up->device);
    417 			continue;
    418 		}
    419 
    420 		if (change & UPS_C_PORT_ENABLED) {
    421 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    422 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    423 			if (change & UPS_C_CONNECT_STATUS) {
    424 				/* Ignore the port error if the device
    425 				   vanished. */
    426 			} else if (status & UPS_PORT_ENABLED) {
    427 				printf("%s: illegal enable change, port %d\n",
    428 				       USBDEVNAME(sc->sc_dev), port);
    429 			} else {
    430 				/* Port error condition. */
    431 				if (up->restartcnt) /* no message first time */
    432 					printf("%s: port error, restarting "
    433 					       "port %d\n",
    434 					       USBDEVNAME(sc->sc_dev), port);
    435 
    436 				if (up->restartcnt++ < USBD_RESTART_MAX)
    437 					goto disco;
    438 				else
    439 					printf("%s: port error, giving up "
    440 					       "port %d\n",
    441 					       USBDEVNAME(sc->sc_dev), port);
    442 			}
    443 		}
    444 
    445 		/* XXX handle overcurrent and resume events! */
    446 
    447 		if (!(change & UPS_C_CONNECT_STATUS))
    448 			continue;
    449 
    450 		/* We have a connect status change, handle it. */
    451 
    452 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    453 			 dev->address, port));
    454 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    455 		/*
    456 		 * If there is already a device on the port the change status
    457 		 * must mean that is has disconnected.  Looking at the
    458 		 * current connect status is not enough to figure this out
    459 		 * since a new unit may have been connected before we handle
    460 		 * the disconnect.
    461 		 */
    462 	disco:
    463 		if (up->device != NULL) {
    464 			/* Disconnected */
    465 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    466 				 "on port %d\n", up->device->address, port));
    467 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    468 			usbd_clear_port_feature(dev, port,
    469 						UHF_C_PORT_CONNECTION);
    470 		}
    471 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    472 			/* Nothing connected, just ignore it. */
    473 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    474 				    "_STATUS\n", port));
    475 			continue;
    476 		}
    477 
    478 		/* Connected */
    479 
    480 		if (!(status & UPS_PORT_POWER))
    481 			printf("%s: strange, connected port %d has no power\n",
    482 			       USBDEVNAME(sc->sc_dev), port);
    483 
    484 		/* Wait for maximum device power up time. */
    485 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    486 
    487 		/* Reset port, which implies enabling it. */
    488 		if (usbd_reset_port(dev, port, &up->status)) {
    489 			printf("%s: port %d reset failed\n",
    490 			       USBDEVNAME(sc->sc_dev), port);
    491 			continue;
    492 		}
    493 		/* Get port status again, it might have changed during reset */
    494 		err = usbd_get_port_status(dev, port, &up->status);
    495 		if (err) {
    496 			DPRINTF(("uhub_explore: get port status failed, "
    497 				 "error=%s\n", usbd_errstr(err)));
    498 			continue;
    499 		}
    500 		status = UGETW(up->status.wPortStatus);
    501 		change = UGETW(up->status.wPortChange);
    502 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    503 			/* Nothing connected, just ignore it. */
    504 #ifdef DIAGNOSTIC
    505 			printf("%s: port %d, device disappeared after reset\n",
    506 			       USBDEVNAME(sc->sc_dev), port);
    507 #endif
    508 			continue;
    509 		}
    510 
    511 		/* Figure out device speed */
    512 		if (status & UPS_HIGH_SPEED)
    513 			speed = USB_SPEED_HIGH;
    514 		else if (status & UPS_LOW_SPEED)
    515 			speed = USB_SPEED_LOW;
    516 		else
    517 			speed = USB_SPEED_FULL;
    518 		/* Get device info and set its address. */
    519 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    520 			  dev->depth + 1, speed, port, up);
    521 		/* XXX retry a few times? */
    522 		if (err) {
    523 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    524 				     "error=%s\n", usbd_errstr(err)));
    525 			/* Avoid addressing problems by disabling. */
    526 			/* usbd_reset_port(dev, port, &up->status); */
    527 
    528 			/*
    529 			 * The unit refused to accept a new address, or had
    530 			 * some other serious problem.  Since we cannot leave
    531 			 * at 0 we have to disable the port instead.
    532 			 */
    533 			printf("%s: device problem, disabling port %d\n",
    534 			       USBDEVNAME(sc->sc_dev), port);
    535 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    536 		} else {
    537 			/* The port set up succeeded, reset error count. */
    538 			up->restartcnt = 0;
    539 
    540 			if (up->device->hub)
    541 				up->device->hub->explore(up->device);
    542 		}
    543 	}
    544 	/* enable status change notifications again */
    545 	sc->sc_explorepending = 0;
    546 	return (USBD_NORMAL_COMPLETION);
    547 }
    548 
    549 #if defined(__NetBSD__) || defined(__OpenBSD__)
    550 int
    551 uhub_activate(device_ptr_t self, enum devact act)
    552 {
    553 	struct uhub_softc *sc = (struct uhub_softc *)self;
    554 	struct usbd_hub *hub = sc->sc_hub->hub;
    555 	usbd_device_handle dev;
    556 	int nports, port, i;
    557 
    558 	switch (act) {
    559 	case DVACT_ACTIVATE:
    560 		return (EOPNOTSUPP);
    561 
    562 	case DVACT_DEACTIVATE:
    563 		if (hub == NULL) /* malfunctioning hub */
    564 			break;
    565 		nports = hub->hubdesc.bNbrPorts;
    566 		for(port = 0; port < nports; port++) {
    567 			dev = hub->ports[port].device;
    568 			if (dev != NULL && dev->subdevs != NULL) {
    569 				for (i = 0; dev->subdevs[i] != NULL; i++)
    570 					config_deactivate(dev->subdevs[i]);
    571 			}
    572 		}
    573 		break;
    574 	}
    575 	return (0);
    576 }
    577 #endif
    578 
    579 /*
    580  * Called from process context when the hub is gone.
    581  * Detach all devices on active ports.
    582  */
    583 USB_DETACH(uhub)
    584 {
    585 	USB_DETACH_START(uhub, sc);
    586 	struct usbd_hub *hub = sc->sc_hub->hub;
    587 	struct usbd_port *rup;
    588 	int port, nports;
    589 
    590 #if defined(__NetBSD__) || defined(__OpenBSD__)
    591 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    592 #elif defined(__FreeBSD__)
    593 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    594 #endif
    595 
    596 	if (hub == NULL)		/* Must be partially working */
    597 		return (0);
    598 
    599 	usbd_abort_pipe(sc->sc_ipipe);
    600 	usbd_close_pipe(sc->sc_ipipe);
    601 
    602 	nports = hub->hubdesc.bNbrPorts;
    603 	for(port = 0; port < nports; port++) {
    604 		rup = &hub->ports[port];
    605 		if (rup->device)
    606 			usb_disconnect_port(rup, self);
    607 	}
    608 
    609 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    610 			   USBDEV(sc->sc_dev));
    611 
    612 #if 0
    613 	if (hub->ports[0].tt)
    614 		free(hub->ports[0].tt, M_USBDEV);
    615 #endif
    616 	free(hub, M_USBDEV);
    617 	sc->sc_hub->hub = NULL;
    618 	if (sc->sc_status)
    619 		free(sc->sc_status, M_USBDEV);
    620 
    621 	return (0);
    622 }
    623 
    624 #if defined(__FreeBSD__)
    625 /* Called when a device has been detached from it */
    626 Static void
    627 uhub_child_detached(device_t self, device_t child)
    628 {
    629        struct uhub_softc *sc = device_get_softc(self);
    630        usbd_device_handle devhub = sc->sc_hub;
    631        usbd_device_handle dev;
    632        int nports;
    633        int port;
    634        int i;
    635 
    636        if (!devhub->hub)
    637                /* should never happen; children are only created after init */
    638                panic("hub not fully initialised, but child deleted?");
    639 
    640        nports = devhub->hub->hubdesc.bNbrPorts;
    641        for (port = 0; port < nports; port++) {
    642                dev = devhub->hub->ports[port].device;
    643                if (dev && dev->subdevs) {
    644                        for (i = 0; dev->subdevs[i]; i++) {
    645                                if (dev->subdevs[i] == child) {
    646                                        dev->subdevs[i] = NULL;
    647                                        return;
    648                                }
    649                        }
    650                }
    651        }
    652 }
    653 #endif
    654 
    655 
    656 /*
    657  * Hub interrupt.
    658  * This an indication that some port has changed status.
    659  * Notify the bus event handler thread that we need
    660  * to be explored again.
    661  */
    662 void
    663 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
    664     usbd_status status)
    665 {
    666 	struct uhub_softc *sc = addr;
    667 
    668 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    669 
    670 	if (status == USBD_STALLED)
    671 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    672 	else if (status == USBD_NORMAL_COMPLETION &&
    673 		 !sc->sc_explorepending) {
    674 		/*
    675 		 * Make sure the status is not overwritten in between.
    676 		 * XXX we should suspend the pipe instead
    677 		 */
    678 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
    679 		sc->sc_explorepending = 1;
    680 		usb_needs_explore(sc->sc_hub);
    681 	}
    682 	/*
    683 	 * XXX workaround for broken implementation of the interrupt
    684 	 * pipe in EHCI root hub emulation which doesn't resend
    685 	 * status change notifications until handled: force a rescan
    686 	 * of the ports we touched in the last run
    687 	 */
    688 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
    689       !strcmp(sc->sc_dev.dv_parent->dv_parent->dv_cfdriver->cd_name, "ehci"))
    690 		usb_needs_explore(sc->sc_hub);
    691 }
    692 
    693 Static pnp_status_t
    694 uhub_power(device_t dv, pnp_request_t req, void *opaque)
    695 {
    696 	struct uhub_softc *sc;
    697 	pnp_capabilities_t *pcaps;
    698 	pnp_state_t *pstate;
    699 
    700 	sc = (struct uhub_softc *)dv;
    701 
    702 	switch (req) {
    703 	case PNP_REQUEST_GET_CAPABILITIES:
    704 		pcaps = opaque;
    705 		pcaps->state = PNP_STATE_D0 | PNP_STATE_D3;
    706 		break;
    707 	case PNP_REQUEST_GET_STATE:
    708 		pstate = opaque;
    709 		*pstate = sc->sc_running == 1 ? PNP_STATE_D0 : PNP_STATE_D3;
    710 		break;
    711 	case PNP_REQUEST_SET_STATE:
    712 		pstate = opaque;
    713 		switch (*pstate) {
    714 		case PNP_STATE_D0:
    715 			sc->sc_running = 1;
    716 			break;
    717 		case PNP_STATE_D3:
    718 			sc->sc_running = 0;
    719 			break;
    720 		default:
    721 			return PNP_STATUS_UNSUPPORTED;
    722 		}
    723 		break;
    724 	default:
    725 		return PNP_STATUS_UNSUPPORTED;
    726 	}
    727 
    728 	return PNP_STATUS_SUCCESS;
    729 }
    730 
    731 #if defined(__FreeBSD__)
    732 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    733 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    734 #endif
    735