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