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