xref: /openbsd-src/sys/dev/usb/uhub.c (revision a75de095e6f09c4e7fb9476b06a43a86bb3ea317)
1 /*	$OpenBSD: uhub.c,v 1.34 2006/06/26 19:12:38 mjc Exp $ */
2 /*	$NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /*
43  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #if defined(__NetBSD__) || defined(__OpenBSD__)
51 #include <sys/device.h>
52 #include <sys/proc.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include "bus_if.h"
57 #endif
58 
59 #include <machine/bus.h>
60 
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64 #include <dev/usb/usbdivar.h>
65 
66 #define UHUB_INTR_INTERVAL 255	/* ms */
67 
68 #ifdef UHUB_DEBUG
69 #define DPRINTF(x)	do { if (uhubdebug) logprintf x; } while (0)
70 #define DPRINTFN(n,x)	do { if (uhubdebug>(n)) logprintf x; } while (0)
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 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
81 	u_int8_t		sc_status[1];	/* XXX more ports */
82 	u_char			sc_running;
83 };
84 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
85 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
86 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == 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 
105 #if defined(__NetBSD__)
106 /* Create the driver instance for the hub connected to hub case */
107 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
108     uhub_match, uhub_attach, uhub_detach, uhub_activate);
109 #else
110 struct cfattach uhub_uhub_ca = {
111 	sizeof(struct uhub_softc), uhub_match, uhub_attach,
112 	uhub_detach, uhub_activate
113 };
114 #endif
115 #elif defined(__FreeBSD__)
116 USB_DECLARE_DRIVER_INIT(uhub,
117 			DEVMETHOD(bus_child_detached, uhub_child_detached));
118 
119 /* Create the driver instance for the hub connected to usb case. */
120 devclass_t uhubroot_devclass;
121 
122 Static device_method_t uhubroot_methods[] = {
123 	DEVMETHOD(device_probe, uhub_match),
124 	DEVMETHOD(device_attach, uhub_attach),
125 
126 	/* detach is not allowed for a root hub */
127 	{0,0}
128 };
129 
130 Static	driver_t uhubroot_driver = {
131 	"uhub",
132 	uhubroot_methods,
133 	sizeof(struct uhub_softc)
134 };
135 #endif
136 
137 USB_MATCH(uhub)
138 {
139 	USB_MATCH_START(uhub, uaa);
140 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
141 
142 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
143 	/*
144 	 * The subclass for hubs seems to be 0 for some and 1 for others,
145 	 * so we just ignore the subclass.
146 	 */
147 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
148 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
149 	return (UMATCH_NONE);
150 }
151 
152 USB_ATTACH(uhub)
153 {
154 	USB_ATTACH_START(uhub, sc, uaa);
155 	usbd_device_handle dev = uaa->device;
156 	char *devinfop;
157 	usbd_status err;
158 	struct usbd_hub *hub = NULL;
159 	usb_device_request_t req;
160 	usb_hub_descriptor_t hubdesc;
161 	int p, port, nports, nremov, pwrdly;
162 	usbd_interface_handle iface;
163 	usb_endpoint_descriptor_t *ed;
164 	struct usbd_tt *tts = NULL;
165 
166 	DPRINTFN(1,("uhub_attach\n"));
167 	sc->sc_hub = dev;
168 
169 	devinfop = usbd_devinfo_alloc(dev, 0);
170 	USB_ATTACH_SETUP;
171 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
172 	usbd_devinfo_free(devinfop);
173 
174 	err = usbd_set_config_index(dev, 0, 1);
175 	if (err) {
176 		DPRINTF(("%s: configuration failed, error=%s\n",
177 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
178 		USB_ATTACH_ERROR_RETURN;
179 	}
180 
181 	if (dev->depth > USB_HUB_MAX_DEPTH) {
182 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
183 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
184 		USB_ATTACH_ERROR_RETURN;
185 	}
186 
187 	/* Get hub descriptor. */
188 	req.bmRequestType = UT_READ_CLASS_DEVICE;
189 	req.bRequest = UR_GET_DESCRIPTOR;
190 	USETW2(req.wValue, UDESC_HUB, 0);
191 	USETW(req.wIndex, 0);
192 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
193 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
194 	err = usbd_do_request(dev, &req, &hubdesc);
195 	nports = hubdesc.bNbrPorts;
196 	if (!err && nports > 7) {
197 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
198 		err = usbd_do_request(dev, &req, &hubdesc);
199 	}
200 	if (err) {
201 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
202 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
203 		USB_ATTACH_ERROR_RETURN;
204 	}
205 
206 	for (nremov = 0, port = 1; port <= nports; port++)
207 		if (!UHD_NOT_REMOV(&hubdesc, port))
208 			nremov++;
209 	printf("%s: %d port%s with %d removable, %s powered",
210 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
211 	       nremov, dev->self_powered ? "self" : "bus");
212 
213 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
214 		printf(", %s transaction translator%s",
215 		    UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
216 		    UHUB_IS_SINGLE_TT(sc) ? "" : "s");
217 	}
218 	printf("\n");
219 
220 	if (nports == 0) {
221 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
222 		goto bad;
223 	}
224 
225 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
226 		     M_USBDEV, M_NOWAIT);
227 	if (hub == NULL)
228 		USB_ATTACH_ERROR_RETURN;
229 	dev->hub = hub;
230 	dev->hub->hubsoftc = sc;
231 	hub->explore = uhub_explore;
232 	hub->hubdesc = hubdesc;
233 
234 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
235 		    "parent->selfpowered=%d\n",
236 		 dev->self_powered, dev->powersrc->parent,
237 		 dev->powersrc->parent ?
238 		 dev->powersrc->parent->self_powered : 0));
239 
240 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
241 	    !dev->powersrc->parent->self_powered) {
242 		printf("%s: bus powered hub connected to bus powered hub, "
243 		       "ignored\n", USBDEVNAME(sc->sc_dev));
244 		goto bad;
245 	}
246 
247 	/* Set up interrupt pipe. */
248 	err = usbd_device2interface_handle(dev, 0, &iface);
249 	if (err) {
250 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
251 		goto bad;
252 	}
253 	ed = usbd_interface2endpoint_descriptor(iface, 0);
254 	if (ed == NULL) {
255 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
256 		goto bad;
257 	}
258 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
259 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
260 		goto bad;
261 	}
262 
263 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
264 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
265 		  sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
266 	if (err) {
267 		printf("%s: cannot open interrupt pipe\n",
268 		       USBDEVNAME(sc->sc_dev));
269 		goto bad;
270 	}
271 
272 	/* Wait with power off for a while. */
273 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
274 
275 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
276 
277 	/*
278 	 * To have the best chance of success we do things in the exact same
279 	 * order as Windoze98.  This should not be necessary, but some
280 	 * devices do not follow the USB specs to the letter.
281 	 *
282 	 * These are the events on the bus when a hub is attached:
283 	 *  Get device and config descriptors (see attach code)
284 	 *  Get hub descriptor (see above)
285 	 *  For all ports
286 	 *     turn on power
287 	 *     wait for power to become stable
288 	 * (all below happens in explore code)
289 	 *  For all ports
290 	 *     clear C_PORT_CONNECTION
291 	 *  For all ports
292 	 *     get port status
293 	 *     if device connected
294 	 *        wait 100 ms
295 	 *        turn on reset
296 	 *        wait
297 	 *        clear C_PORT_RESET
298 	 *        get port status
299 	 *        proceed with device attachment
300 	 */
301 
302 	if (UHUB_IS_HIGH_SPEED(sc)) {
303 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
304 		    sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
305 		if (!tts)
306 			goto bad;
307 	}
308 	/* Set up data structures */
309 	for (p = 0; p < nports; p++) {
310 		struct usbd_port *up = &hub->ports[p];
311 		up->device = NULL;
312 		up->parent = dev;
313 		up->portno = p+1;
314 		if (dev->self_powered)
315 			/* Self powered hub, give ports maximum current. */
316 			up->power = USB_MAX_POWER;
317 		else
318 			up->power = USB_MIN_POWER;
319 		up->restartcnt = 0;
320 		up->reattach = 0;
321 		if (UHUB_IS_HIGH_SPEED(sc)) {
322 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
323 			up->tt->hub = hub;
324 		} else {
325 			up->tt = NULL;
326 		}
327 	}
328 
329 	/* XXX should check for none, individual, or ganged power? */
330 
331 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
332 	    + USB_EXTRA_POWER_UP_TIME;
333 	for (port = 1; port <= nports; port++) {
334 		/* Turn the power on. */
335 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
336 		if (err)
337 			printf("%s: port %d power on failed, %s\n",
338 			       USBDEVNAME(sc->sc_dev), port,
339 			       usbd_errstr(err));
340 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
341 		/* Wait for stable power. */
342 		usbd_delay_ms(dev, pwrdly);
343 	}
344 
345 	/* The usual exploration will finish the setup. */
346 
347 	sc->sc_running = 1;
348 
349 	USB_ATTACH_SUCCESS_RETURN;
350 
351  bad:
352 	if (hub)
353 		free(hub, M_USBDEV);
354 	dev->hub = NULL;
355 	USB_ATTACH_ERROR_RETURN;
356 }
357 
358 usbd_status
359 uhub_explore(usbd_device_handle dev)
360 {
361 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
362 	struct uhub_softc *sc = dev->hub->hubsoftc;
363 	struct usbd_port *up;
364 	usbd_status err;
365 	int speed;
366 	int port;
367 	int change, status, reconnect;
368 
369 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
370 
371 	if (!sc->sc_running)
372 		return (USBD_NOT_STARTED);
373 
374 	/* Ignore hubs that are too deep. */
375 	if (dev->depth > USB_HUB_MAX_DEPTH)
376 		return (USBD_TOO_DEEP);
377 
378 	for(port = 1; port <= hd->bNbrPorts; port++) {
379 		up = &dev->hub->ports[port-1];
380 		err = usbd_get_port_status(dev, port, &up->status);
381 		if (err) {
382 			DPRINTF(("uhub_explore: get port status failed, "
383 				 "error=%s\n", usbd_errstr(err)));
384 			continue;
385 		}
386 		status = UGETW(up->status.wPortStatus);
387 		change = UGETW(up->status.wPortChange);
388 		reconnect = up->reattach;
389 		up->reattach = 0;
390 		DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
391 			    USBDEVNAME(sc->sc_dev), port, status, change));
392 		if (change & UPS_C_PORT_ENABLED) {
393 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
394 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
395 			if (change & UPS_C_CONNECT_STATUS) {
396 				/* Ignore the port error if the device
397 				   vanished. */
398 			} else if (status & UPS_PORT_ENABLED) {
399 				printf("%s: illegal enable change, port %d\n",
400 				       USBDEVNAME(sc->sc_dev), port);
401 			} else {
402 				/* Port error condition. */
403 				if (up->restartcnt) /* no message first time */
404 					printf("%s: port error, restarting "
405 					       "port %d\n",
406 					       USBDEVNAME(sc->sc_dev), port);
407 
408 				if (up->restartcnt++ < USBD_RESTART_MAX)
409 					goto disco;
410 				else
411 					printf("%s: port error, giving up "
412 					       "port %d\n",
413 					       USBDEVNAME(sc->sc_dev), port);
414 			}
415 		}
416 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
417 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
418 				    "STATUS\n", port));
419 			/* No status change, just do recursive explore. */
420 			if (up->device != NULL && up->device->hub != NULL)
421 				up->device->hub->explore(up->device);
422 #if 0 && defined(DIAGNOSTIC)
423 			if (up->device == NULL &&
424 			    (status & UPS_CURRENT_CONNECT_STATUS))
425 				printf("%s: connected, no device\n",
426 				       USBDEVNAME(sc->sc_dev));
427 #endif
428 			continue;
429 		}
430 
431 		/* We have a connect status change, handle it. */
432 
433 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
434 			 dev->address, port));
435 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
436 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
437 		/*
438 		 * If there is already a device on the port the change status
439 		 * must mean that is has disconnected.  Looking at the
440 		 * current connect status is not enough to figure this out
441 		 * since a new unit may have been connected before we handle
442 		 * the disconnect.
443 		 */
444 	disco:
445 		if (up->device != NULL) {
446 			/* Disconnected */
447 			DPRINTF(("uhub_explore: device addr=%d disappeared "
448 				 "on port %d\n", up->device->address, port));
449 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
450 			usbd_clear_port_feature(dev, port,
451 						UHF_C_PORT_CONNECTION);
452 		}
453 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
454 			/* Nothing connected, just ignore it. */
455 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
456 				    "_STATUS\n", port));
457 			continue;
458 		}
459 
460 		/* Connected */
461 
462 		if (!(status & UPS_PORT_POWER))
463 			printf("%s: strange, connected port %d has no power\n",
464 			       USBDEVNAME(sc->sc_dev), port);
465 
466 		/* Wait for maximum device power up time. */
467 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
468 
469 		/* Reset port, which implies enabling it. */
470 		if (usbd_reset_port(dev, port, &up->status)) {
471 			printf("%s: port %d reset failed\n",
472 			       USBDEVNAME(sc->sc_dev), port);
473 			continue;
474 		}
475 		/* Get port status again, it might have changed during reset */
476 		err = usbd_get_port_status(dev, port, &up->status);
477 		if (err) {
478 			DPRINTF(("uhub_explore: get port status failed, "
479 				 "error=%s\n", usbd_errstr(err)));
480 			continue;
481 		}
482 		status = UGETW(up->status.wPortStatus);
483 		change = UGETW(up->status.wPortChange);
484 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
485 			/* Nothing connected, just ignore it. */
486 #ifdef UHUB_DEBUG
487 			printf("%s: port %d, device disappeared after reset\n",
488 			       USBDEVNAME(sc->sc_dev), port);
489 #endif
490 			continue;
491 		}
492 
493 		/* Figure out device speed */
494 		if (status & UPS_HIGH_SPEED)
495 			speed = USB_SPEED_HIGH;
496 		else if (status & UPS_LOW_SPEED)
497 			speed = USB_SPEED_LOW;
498 		else
499 			speed = USB_SPEED_FULL;
500 		/* Get device info and set its address. */
501 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
502 			  dev->depth + 1, speed, port, up);
503 		/* XXX retry a few times? */
504 		if (err) {
505 			DPRINTFN(-1,("uhub_explore: usbd_new_device failed, "
506 				     "error=%s\n", usbd_errstr(err)));
507 			/* Avoid addressing problems by disabling. */
508 			/* usbd_reset_port(dev, port, &up->status); */
509 
510 			/*
511 			 * The unit refused to accept a new address, or had
512 			 * some other serious problem.  Since we cannot leave
513 			 * at 0 we have to disable the port instead.
514 			 */
515 			printf("%s: device problem, disabling port %d\n",
516 			       USBDEVNAME(sc->sc_dev), port);
517 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
518 		} else {
519 			/* The port set up succeeded, reset error count. */
520 			up->restartcnt = 0;
521 
522 			if (up->device->hub)
523 				up->device->hub->explore(up->device);
524 		}
525 	}
526 	return (USBD_NORMAL_COMPLETION);
527 }
528 
529 #if defined(__NetBSD__) || defined(__OpenBSD__)
530 int
531 uhub_activate(device_ptr_t self, enum devact act)
532 {
533 	struct uhub_softc *sc = (struct uhub_softc *)self;
534 	struct usbd_hub *hub = sc->sc_hub->hub;
535 	usbd_device_handle dev;
536 	int nports, port, i;
537 
538 	switch (act) {
539 	case DVACT_ACTIVATE:
540 		break;
541 
542 	case DVACT_DEACTIVATE:
543 		if (hub == NULL) /* malfunctioning hub */
544 			break;
545 		nports = hub->hubdesc.bNbrPorts;
546 		for(port = 0; port < nports; port++) {
547 			dev = hub->ports[port].device;
548 			if (dev != NULL && dev->subdevs != NULL) {
549 				for (i = 0; dev->subdevs[i] != NULL; i++)
550 					config_deactivate(dev->subdevs[i]);
551 			}
552 		}
553 		break;
554 	}
555 	return (0);
556 }
557 #endif
558 
559 /*
560  * Called from process context when the hub is gone.
561  * Detach all devices on active ports.
562  */
563 USB_DETACH(uhub)
564 {
565 	USB_DETACH_START(uhub, sc);
566 	struct usbd_hub *hub = sc->sc_hub->hub;
567 	struct usbd_port *rup;
568 	int port, nports;
569 
570 #if defined(__NetBSD__) || defined(__OpenBSD__)
571 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
572 #elif defined(__FreeBSD__)
573 	DPRINTF(("uhub_detach: sc=%port\n", sc));
574 #endif
575 
576 	if (hub == NULL)		/* Must be partially working */
577 		return (0);
578 
579 	usbd_abort_pipe(sc->sc_ipipe);
580 	usbd_close_pipe(sc->sc_ipipe);
581 
582 	nports = hub->hubdesc.bNbrPorts;
583 	for(port = 0; port < nports; port++) {
584 		rup = &hub->ports[port];
585 		if (rup->device)
586 			usb_disconnect_port(rup, self);
587 	}
588 
589 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
590 			   USBDEV(sc->sc_dev));
591 
592 	if (hub->ports[0].tt)
593 		free(hub->ports[0].tt, M_USBDEV);
594 	free(hub, M_USBDEV);
595 	sc->sc_hub->hub = NULL;
596 
597 	return (0);
598 }
599 
600 #if defined(__FreeBSD__)
601 /* Called when a device has been detached from it */
602 Static void
603 uhub_child_detached(device_t self, device_t child)
604 {
605        struct uhub_softc *sc = device_get_softc(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 && dev->subdevs) {
620                        for (i = 0; dev->subdevs[i]; i++) {
621                                if (dev->subdevs[i] == child) {
622                                        dev->subdevs[i] = NULL;
623                                        return;
624                                }
625                        }
626                }
627        }
628 }
629 #endif
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, usbd_status status)
640 {
641 	struct uhub_softc *sc = addr;
642 
643 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
644 	if (status == USBD_STALLED)
645 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
646 	else if (status == USBD_NORMAL_COMPLETION)
647 		usb_needs_explore(sc->sc_hub);
648 }
649 
650 #if defined(__FreeBSD__)
651 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
652 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
653 #endif
654