Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.76.2.8
      1 /*	$NetBSD: uhub.c,v 1.76.2.8 2008/02/04 09:23:39 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.8 2008/02/04 09:23:39 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 	}
    354 
    355 	/* Wait for stable power if we are not a root hub */
    356 	if (dev->powersrc->parent != NULL)
    357 		usbd_delay_ms(dev, pwrdly);
    358 
    359 	/* The usual exploration will finish the setup. */
    360 
    361 	sc->sc_running = 1;
    362 
    363 	if (!pmf_device_register(self, NULL, NULL))
    364 		aprint_error_dev(self, "couldn't establish power handler\n");
    365 
    366 	USB_ATTACH_SUCCESS_RETURN;
    367 
    368  bad:
    369 	if (sc->sc_status)
    370 		free(sc->sc_status, M_USBDEV);
    371 	if (hub)
    372 		free(hub, M_USBDEV);
    373 	dev->hub = NULL;
    374 	USB_ATTACH_ERROR_RETURN;
    375 }
    376 
    377 usbd_status
    378 uhub_explore(usbd_device_handle dev)
    379 {
    380 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    381 	struct uhub_softc *sc = dev->hub->hubsoftc;
    382 	struct usbd_port *up;
    383 	usbd_status err;
    384 	int speed;
    385 	int port;
    386 	int change, status, reconnect;
    387 
    388 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    389 
    390 	if (!sc->sc_running)
    391 		return (USBD_NOT_STARTED);
    392 
    393 	/* Ignore hubs that are too deep. */
    394 	if (dev->depth > USB_HUB_MAX_DEPTH)
    395 		return (USBD_TOO_DEEP);
    396 
    397 	for (port = 1; port <= hd->bNbrPorts; port++) {
    398 		up = &dev->hub->ports[port-1];
    399 
    400 		/* reattach is needed after firmware upload */
    401 		reconnect = up->reattach;
    402 		up->reattach = 0;
    403 
    404 		status = change = 0;
    405 
    406 		/* don't check if no change summary notification */
    407 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
    408 			err = usbd_get_port_status(dev, port, &up->status);
    409 			if (err) {
    410 				DPRINTF(("uhub_explore: get port stat failed, "
    411 					 "error=%s\n", usbd_errstr(err)));
    412 				continue;
    413 			}
    414 			status = UGETW(up->status.wPortStatus);
    415 			change = UGETW(up->status.wPortChange);
    416 		}
    417 		if (!change && !reconnect) {
    418 			/* No status change, just do recursive explore. */
    419 			if (up->device != NULL && up->device->hub != NULL)
    420 				up->device->hub->explore(up->device);
    421 			continue;
    422 		}
    423 
    424 		if (change & UPS_C_PORT_ENABLED) {
    425 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
    426 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    427 			if (change & UPS_C_CONNECT_STATUS) {
    428 				/* Ignore the port error if the device
    429 				   vanished. */
    430 			} else if (status & UPS_PORT_ENABLED) {
    431 				printf("%s: illegal enable change, port %d\n",
    432 				       USBDEVNAME(sc->sc_dev), port);
    433 			} else {
    434 				/* Port error condition. */
    435 				if (up->restartcnt) /* no message first time */
    436 					printf("%s: port error, restarting "
    437 					       "port %d\n",
    438 					       USBDEVNAME(sc->sc_dev), port);
    439 
    440 				if (up->restartcnt++ < USBD_RESTART_MAX)
    441 					goto disco;
    442 				else
    443 					printf("%s: port error, giving up "
    444 					       "port %d\n",
    445 					       USBDEVNAME(sc->sc_dev), port);
    446 			}
    447 		}
    448 
    449 		/* XXX handle overcurrent and resume events! */
    450 
    451 		if (!(change & UPS_C_CONNECT_STATUS))
    452 			continue;
    453 
    454 		/* We have a connect status change, handle it. */
    455 
    456 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    457 			 dev->address, port));
    458 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    459 		/*
    460 		 * If there is already a device on the port the change status
    461 		 * must mean that is has disconnected.  Looking at the
    462 		 * current connect status is not enough to figure this out
    463 		 * since a new unit may have been connected before we handle
    464 		 * the disconnect.
    465 		 */
    466 	disco:
    467 		if (up->device != NULL) {
    468 			/* Disconnected */
    469 			DPRINTF(("uhub_explore: device addr=%d disappeared "
    470 				 "on port %d\n", up->device->address, port));
    471 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    472 			usbd_clear_port_feature(dev, port,
    473 						UHF_C_PORT_CONNECTION);
    474 		}
    475 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    476 			/* Nothing connected, just ignore it. */
    477 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
    478 				    "_STATUS\n", port));
    479 			continue;
    480 		}
    481 
    482 		/* Connected */
    483 
    484 		if (!(status & UPS_PORT_POWER))
    485 			printf("%s: strange, connected port %d has no power\n",
    486 			       USBDEVNAME(sc->sc_dev), port);
    487 
    488 		/* Wait for maximum device power up time. */
    489 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    490 
    491 		/* Reset port, which implies enabling it. */
    492 		if (usbd_reset_port(dev, port, &up->status)) {
    493 			printf("%s: port %d reset failed\n",
    494 			       USBDEVNAME(sc->sc_dev), port);
    495 			continue;
    496 		}
    497 		/* Get port status again, it might have changed during reset */
    498 		err = usbd_get_port_status(dev, port, &up->status);
    499 		if (err) {
    500 			DPRINTF(("uhub_explore: get port status failed, "
    501 				 "error=%s\n", usbd_errstr(err)));
    502 			continue;
    503 		}
    504 		status = UGETW(up->status.wPortStatus);
    505 		change = UGETW(up->status.wPortChange);
    506 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
    507 			/* Nothing connected, just ignore it. */
    508 #ifdef DIAGNOSTIC
    509 			printf("%s: port %d, device disappeared after reset\n",
    510 			       USBDEVNAME(sc->sc_dev), port);
    511 #endif
    512 			continue;
    513 		}
    514 
    515 		/* Figure out device speed */
    516 		if (status & UPS_HIGH_SPEED)
    517 			speed = USB_SPEED_HIGH;
    518 		else if (status & UPS_LOW_SPEED)
    519 			speed = USB_SPEED_LOW;
    520 		else
    521 			speed = USB_SPEED_FULL;
    522 		/* Get device info and set its address. */
    523 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    524 			  dev->depth + 1, speed, port, up);
    525 		/* XXX retry a few times? */
    526 		if (err) {
    527 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    528 				     "error=%s\n", usbd_errstr(err)));
    529 			/* Avoid addressing problems by disabling. */
    530 			/* usbd_reset_port(dev, port, &up->status); */
    531 
    532 			/*
    533 			 * The unit refused to accept a new address, or had
    534 			 * some other serious problem.  Since we cannot leave
    535 			 * at 0 we have to disable the port instead.
    536 			 */
    537 			printf("%s: device problem, disabling port %d\n",
    538 			       USBDEVNAME(sc->sc_dev), port);
    539 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    540 		} else {
    541 			/* The port set up succeeded, reset error count. */
    542 			up->restartcnt = 0;
    543 
    544 			if (up->device->hub)
    545 				up->device->hub->explore(up->device);
    546 		}
    547 	}
    548 	/* enable status change notifications again */
    549 	sc->sc_explorepending = 0;
    550 	return (USBD_NORMAL_COMPLETION);
    551 }
    552 
    553 #if defined(__NetBSD__) || defined(__OpenBSD__)
    554 int
    555 uhub_activate(device_ptr_t self, enum devact act)
    556 {
    557 	struct uhub_softc *sc = (struct uhub_softc *)self;
    558 	struct usbd_hub *hub = sc->sc_hub->hub;
    559 	usbd_device_handle dev;
    560 	int nports, port, i;
    561 
    562 	switch (act) {
    563 	case DVACT_ACTIVATE:
    564 		return (EOPNOTSUPP);
    565 
    566 	case DVACT_DEACTIVATE:
    567 		if (hub == NULL) /* malfunctioning hub */
    568 			break;
    569 		nports = hub->hubdesc.bNbrPorts;
    570 		for(port = 0; port < nports; port++) {
    571 			dev = hub->ports[port].device;
    572 			if (dev != NULL && dev->subdevs != NULL) {
    573 				for (i = 0; dev->subdevs[i] != NULL; i++)
    574 					config_deactivate(dev->subdevs[i]);
    575 			}
    576 		}
    577 		break;
    578 	}
    579 	return (0);
    580 }
    581 #endif
    582 
    583 /*
    584  * Called from process context when the hub is gone.
    585  * Detach all devices on active ports.
    586  */
    587 USB_DETACH(uhub)
    588 {
    589 	USB_DETACH_START(uhub, sc);
    590 	struct usbd_hub *hub = sc->sc_hub->hub;
    591 	struct usbd_port *rup;
    592 	int port, nports;
    593 
    594 #if defined(__NetBSD__) || defined(__OpenBSD__)
    595 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    596 #elif defined(__FreeBSD__)
    597 	DPRINTF(("uhub_detach: sc=%port\n", sc));
    598 #endif
    599 
    600 	if (hub == NULL)		/* Must be partially working */
    601 		return (0);
    602 
    603 	pmf_device_deregister(self);
    604 	usbd_abort_pipe(sc->sc_ipipe);
    605 	usbd_close_pipe(sc->sc_ipipe);
    606 
    607 	nports = hub->hubdesc.bNbrPorts;
    608 	for(port = 0; port < nports; port++) {
    609 		rup = &hub->ports[port];
    610 		if (rup->device)
    611 			usb_disconnect_port(rup, self);
    612 	}
    613 
    614 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
    615 			   USBDEV(sc->sc_dev));
    616 
    617 #if 0
    618 	if (hub->ports[0].tt)
    619 		free(hub->ports[0].tt, M_USBDEV);
    620 #endif
    621 	free(hub, M_USBDEV);
    622 	sc->sc_hub->hub = NULL;
    623 	if (sc->sc_status)
    624 		free(sc->sc_status, M_USBDEV);
    625 
    626 	return (0);
    627 }
    628 
    629 #if defined(__FreeBSD__)
    630 /* Called when a device has been detached from it */
    631 Static void
    632 uhub_child_detached(device_t self, device_t child)
    633 {
    634        struct uhub_softc *sc = device_get_softc(self);
    635        usbd_device_handle devhub = sc->sc_hub;
    636        usbd_device_handle dev;
    637        int nports;
    638        int port;
    639        int i;
    640 
    641        if (!devhub->hub)
    642                /* should never happen; children are only created after init */
    643                panic("hub not fully initialised, but child deleted?");
    644 
    645        nports = devhub->hub->hubdesc.bNbrPorts;
    646        for (port = 0; port < nports; port++) {
    647                dev = devhub->hub->ports[port].device;
    648                if (dev && dev->subdevs) {
    649                        for (i = 0; dev->subdevs[i]; i++) {
    650                                if (dev->subdevs[i] == child) {
    651                                        dev->subdevs[i] = NULL;
    652                                        return;
    653                                }
    654                        }
    655                }
    656        }
    657 }
    658 #endif
    659 
    660 
    661 /*
    662  * Hub interrupt.
    663  * This an indication that some port has changed status.
    664  * Notify the bus event handler thread that we need
    665  * to be explored again.
    666  */
    667 void
    668 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
    669     usbd_status status)
    670 {
    671 	struct uhub_softc *sc = addr;
    672 
    673 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    674 
    675 	if (status == USBD_STALLED)
    676 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    677 	else if (status == USBD_NORMAL_COMPLETION &&
    678 		 !sc->sc_explorepending) {
    679 		/*
    680 		 * Make sure the status is not overwritten in between.
    681 		 * XXX we should suspend the pipe instead
    682 		 */
    683 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
    684 		sc->sc_explorepending = 1;
    685 		usb_needs_explore(sc->sc_hub);
    686 	}
    687 	/*
    688 	 * XXX workaround for broken implementation of the interrupt
    689 	 * pipe in EHCI root hub emulation which doesn't resend
    690 	 * status change notifications until handled: force a rescan
    691 	 * of the ports we touched in the last run
    692 	 */
    693 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
    694       !strcmp(sc->sc_dev.dv_parent->dv_parent->dv_cfdriver->cd_name, "ehci"))
    695 		usb_needs_explore(sc->sc_hub);
    696 }
    697 
    698 #if defined(__FreeBSD__)
    699 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    700 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    701 #endif
    702