xref: /netbsd-src/sys/dev/usb/uhub.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: uhub.c,v 1.81 2006/11/16 01:33:26 christos 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@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.81 2006/11/16 01:33:26 christos Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
54 #include <sys/proc.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include "bus_if.h"
59 #endif
60 
61 #include <machine/bus.h>
62 
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdivar.h>
67 
68 #define UHUB_INTR_INTERVAL 255	/* ms */
69 
70 #ifdef UHUB_DEBUG
71 #define DPRINTF(x)	if (uhubdebug) logprintf x
72 #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
73 int	uhubdebug = 0;
74 #else
75 #define DPRINTF(x)
76 #define DPRINTFN(n,x)
77 #endif
78 
79 struct uhub_softc {
80 	USBBASEDEVICE		sc_dev;		/* base device */
81 	usbd_device_handle	sc_hub;		/* USB device */
82 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
83 	u_int8_t		sc_status[1];	/* XXX more ports */
84 	u_char			sc_running;
85 };
86 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
87 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
88 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
89 
90 Static usbd_status uhub_explore(usbd_device_handle hub);
91 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
92 
93 #if defined(__FreeBSD__)
94 Static bus_child_detached_t uhub_child_detached;
95 #endif
96 
97 
98 /*
99  * We need two attachment points:
100  * hub to usb and hub to hub
101  * Every other driver only connects to hubs
102  */
103 
104 #if defined(__NetBSD__) || defined(__OpenBSD__)
105 USB_DECLARE_DRIVER(uhub);
106 #elif defined(__FreeBSD__)
107 USB_DECLARE_DRIVER_INIT(uhub,
108 			DEVMETHOD(bus_child_detached, uhub_child_detached));
109 
110 /* Create the driver instance for the hub connected to usb case. */
111 devclass_t uhubroot_devclass;
112 
113 Static device_method_t uhubroot_methods[] = {
114 	DEVMETHOD(device_probe, uhub_match),
115 	DEVMETHOD(device_attach, uhub_attach),
116 
117 	/* detach is not allowed for a root hub */
118 	{0,0}
119 };
120 
121 Static	driver_t uhubroot_driver = {
122 	"uhub",
123 	uhubroot_methods,
124 	sizeof(struct uhub_softc)
125 };
126 #endif
127 
128 USB_MATCH(uhub)
129 {
130 	USB_MATCH_START(uhub, uaa);
131 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
132 
133 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
134 	/*
135 	 * The subclass for hubs seems to be 0 for some and 1 for others,
136 	 * so we just ignore the subclass.
137 	 */
138 	if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
139 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
140 	return (UMATCH_NONE);
141 }
142 
143 USB_ATTACH(uhub)
144 {
145 	USB_ATTACH_START(uhub, sc, uaa);
146 	usbd_device_handle dev = uaa->device;
147 	char *devinfop;
148 	usbd_status err;
149 	struct usbd_hub *hub = NULL;
150 	usb_device_request_t req;
151 	usb_hub_descriptor_t hubdesc;
152 	int p, port, nports, nremov, pwrdly;
153 	usbd_interface_handle iface;
154 	usb_endpoint_descriptor_t *ed;
155 	struct usbd_tt *tts = NULL;
156 
157 	DPRINTFN(1,("uhub_attach\n"));
158 	sc->sc_hub = dev;
159 
160 	devinfop = usbd_devinfo_alloc(dev, 1);
161 	USB_ATTACH_SETUP;
162 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
163 	usbd_devinfo_free(devinfop);
164 
165 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
166 		printf("%s: %s transaction translator%s\n",
167 		       USBDEVNAME(sc->sc_dev),
168 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
169 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
170 	}
171 
172 	err = usbd_set_config_index(dev, 0, 1);
173 	if (err) {
174 		DPRINTF(("%s: configuration failed, error=%s\n",
175 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
176 		USB_ATTACH_ERROR_RETURN;
177 	}
178 
179 	if (dev->depth > USB_HUB_MAX_DEPTH) {
180 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
181 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
182 		USB_ATTACH_ERROR_RETURN;
183 	}
184 
185 	/* Get hub descriptor. */
186 	req.bmRequestType = UT_READ_CLASS_DEVICE;
187 	req.bRequest = UR_GET_DESCRIPTOR;
188 	USETW2(req.wValue, UDESC_HUB, 0);
189 	USETW(req.wIndex, 0);
190 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
191 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
192 	err = usbd_do_request(dev, &req, &hubdesc);
193 	nports = hubdesc.bNbrPorts;
194 	if (!err && nports > 7) {
195 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
196 		err = usbd_do_request(dev, &req, &hubdesc);
197 	}
198 	if (err) {
199 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
200 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
201 		USB_ATTACH_ERROR_RETURN;
202 	}
203 
204 	for (nremov = 0, port = 1; port <= nports; port++)
205 		if (!UHD_NOT_REMOV(&hubdesc, port))
206 			nremov++;
207 	printf("%s: %d port%s with %d removable, %s powered\n",
208 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
209 	       nremov, dev->self_powered ? "self" : "bus");
210 
211 	if (nports == 0) {
212 		printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
213 		goto bad;
214 	}
215 
216 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
217 		     M_USBDEV, M_NOWAIT);
218 	if (hub == NULL)
219 		USB_ATTACH_ERROR_RETURN;
220 	dev->hub = hub;
221 	dev->hub->hubsoftc = sc;
222 	hub->explore = uhub_explore;
223 	hub->hubdesc = hubdesc;
224 
225 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
226 		    "parent->selfpowered=%d\n",
227 		 dev->self_powered, dev->powersrc->parent,
228 		 dev->powersrc->parent ?
229 		 dev->powersrc->parent->self_powered : 0));
230 
231 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
232 	    !dev->powersrc->parent->self_powered) {
233 		printf("%s: bus powered hub connected to bus powered hub, "
234 		       "ignored\n", USBDEVNAME(sc->sc_dev));
235 		goto bad;
236 	}
237 
238 	/* Set up interrupt pipe. */
239 	err = usbd_device2interface_handle(dev, 0, &iface);
240 	if (err) {
241 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
242 		goto bad;
243 	}
244 	ed = usbd_interface2endpoint_descriptor(iface, 0);
245 	if (ed == NULL) {
246 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
247 		goto bad;
248 	}
249 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
250 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
251 		goto bad;
252 	}
253 
254 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
255 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
256 		  sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
257 	if (err) {
258 		printf("%s: cannot open interrupt pipe\n",
259 		       USBDEVNAME(sc->sc_dev));
260 		goto bad;
261 	}
262 
263 	/* Wait with power off for a while. */
264 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
265 
266 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
267 
268 	/*
269 	 * To have the best chance of success we do things in the exact same
270 	 * order as Windoze98.  This should not be necessary, but some
271 	 * devices do not follow the USB specs to the letter.
272 	 *
273 	 * These are the events on the bus when a hub is attached:
274 	 *  Get device and config descriptors (see attach code)
275 	 *  Get hub descriptor (see above)
276 	 *  For all ports
277 	 *     turn on power
278 	 *     wait for power to become stable
279 	 * (all below happens in explore code)
280 	 *  For all ports
281 	 *     clear C_PORT_CONNECTION
282 	 *  For all ports
283 	 *     get port status
284 	 *     if device connected
285 	 *        wait 100 ms
286 	 *        turn on reset
287 	 *        wait
288 	 *        clear C_PORT_RESET
289 	 *        get port status
290 	 *        proceed with device attachment
291 	 */
292 
293 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
294 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
295 			     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
296 		if (!tts)
297 			goto bad;
298 	}
299 	/* Set up data structures */
300 	for (p = 0; p < nports; p++) {
301 		struct usbd_port *up = &hub->ports[p];
302 		up->device = NULL;
303 		up->parent = dev;
304 		up->portno = p+1;
305 		if (dev->self_powered)
306 			/* Self powered hub, give ports maximum current. */
307 			up->power = USB_MAX_POWER;
308 		else
309 			up->power = USB_MIN_POWER;
310 		up->restartcnt = 0;
311 		up->reattach = 0;
312 		if (UHUB_IS_HIGH_SPEED(sc)) {
313 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
314 			up->tt->hub = hub;
315 		} else {
316 			up->tt = NULL;
317 		}
318 	}
319 
320 	/* XXX should check for none, individual, or ganged power? */
321 
322 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
323 	    + USB_EXTRA_POWER_UP_TIME;
324 	for (port = 1; port <= nports; port++) {
325 		/* Turn the power on. */
326 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
327 		if (err)
328 			printf("%s: port %d power on failed, %s\n",
329 			       USBDEVNAME(sc->sc_dev), port,
330 			       usbd_errstr(err));
331 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
332 		/* Wait for stable power. */
333 		usbd_delay_ms(dev, pwrdly);
334 	}
335 
336 	/* The usual exploration will finish the setup. */
337 
338 	sc->sc_running = 1;
339 
340 	USB_ATTACH_SUCCESS_RETURN;
341 
342  bad:
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 		err = usbd_get_port_status(dev, port, &up->status);
372 		if (err) {
373 			DPRINTF(("uhub_explore: get port status failed, "
374 				 "error=%s\n", usbd_errstr(err)));
375 			continue;
376 		}
377 		status = UGETW(up->status.wPortStatus);
378 		change = UGETW(up->status.wPortChange);
379 		reconnect = up->reattach;
380 		up->reattach = 0;
381 		DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
382 			    USBDEVNAME(sc->sc_dev), port, status, change));
383 		if (change & UPS_C_PORT_ENABLED) {
384 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
385 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
386 			if (change & UPS_C_CONNECT_STATUS) {
387 				/* Ignore the port error if the device
388 				   vanished. */
389 			} else if (status & UPS_PORT_ENABLED) {
390 				printf("%s: illegal enable change, port %d\n",
391 				       USBDEVNAME(sc->sc_dev), port);
392 			} else {
393 				/* Port error condition. */
394 				if (up->restartcnt) /* no message first time */
395 					printf("%s: port error, restarting "
396 					       "port %d\n",
397 					       USBDEVNAME(sc->sc_dev), port);
398 
399 				if (up->restartcnt++ < USBD_RESTART_MAX)
400 					goto disco;
401 				else
402 					printf("%s: port error, giving up "
403 					       "port %d\n",
404 					       USBDEVNAME(sc->sc_dev), port);
405 			}
406 		}
407 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
408 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
409 				    "STATUS\n", port));
410 			/* No status change, just do recursive explore. */
411 			if (up->device != NULL && up->device->hub != NULL)
412 				up->device->hub->explore(up->device);
413 #if 0 && defined(DIAGNOSTIC)
414 			if (up->device == NULL &&
415 			    (status & UPS_CURRENT_CONNECT_STATUS))
416 				printf("%s: connected, no device\n",
417 				       USBDEVNAME(sc->sc_dev));
418 #endif
419 			continue;
420 		}
421 
422 		/* We have a connect status change, handle it. */
423 
424 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
425 			 dev->address, port));
426 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
427 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
428 		/*
429 		 * If there is already a device on the port the change status
430 		 * must mean that is has disconnected.  Looking at the
431 		 * current connect status is not enough to figure this out
432 		 * since a new unit may have been connected before we handle
433 		 * the disconnect.
434 		 */
435 	disco:
436 		if (up->device != NULL) {
437 			/* Disconnected */
438 			DPRINTF(("uhub_explore: device addr=%d disappeared "
439 				 "on port %d\n", up->device->address, port));
440 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
441 			usbd_clear_port_feature(dev, port,
442 						UHF_C_PORT_CONNECTION);
443 		}
444 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
445 			/* Nothing connected, just ignore it. */
446 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
447 				    "_STATUS\n", port));
448 			continue;
449 		}
450 
451 		/* Connected */
452 
453 		if (!(status & UPS_PORT_POWER))
454 			printf("%s: strange, connected port %d has no power\n",
455 			       USBDEVNAME(sc->sc_dev), port);
456 
457 		/* Wait for maximum device power up time. */
458 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
459 
460 		/* Reset port, which implies enabling it. */
461 		if (usbd_reset_port(dev, port, &up->status)) {
462 			printf("%s: port %d reset failed\n",
463 			       USBDEVNAME(sc->sc_dev), port);
464 			continue;
465 		}
466 		/* Get port status again, it might have changed during reset */
467 		err = usbd_get_port_status(dev, port, &up->status);
468 		if (err) {
469 			DPRINTF(("uhub_explore: get port status failed, "
470 				 "error=%s\n", usbd_errstr(err)));
471 			continue;
472 		}
473 		status = UGETW(up->status.wPortStatus);
474 		change = UGETW(up->status.wPortChange);
475 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
476 			/* Nothing connected, just ignore it. */
477 #ifdef DIAGNOSTIC
478 			printf("%s: port %d, device disappeared after reset\n",
479 			       USBDEVNAME(sc->sc_dev), port);
480 #endif
481 			continue;
482 		}
483 
484 #if 0
485 		if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
486 			printf("%s: port %d, transaction translation not "
487 			       "implemented, low/full speed device ignored\n",
488 			       USBDEVNAME(sc->sc_dev), port);
489 			continue;
490 		}
491 #endif
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: usb_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 		return (EOPNOTSUPP);
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,
640     usbd_status status)
641 {
642 	struct uhub_softc *sc = addr;
643 
644 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
645 	if (status == USBD_STALLED)
646 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
647 	else if (status == USBD_NORMAL_COMPLETION)
648 		usb_needs_explore(sc->sc_hub);
649 }
650 
651 #if defined(__FreeBSD__)
652 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
653 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
654 #endif
655