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