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