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