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