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