xref: /netbsd-src/sys/dev/usb/uhub.c (revision aa73cae19608873cc4d1f712c4a0f8f8435f1ffa)
1 /*	$NetBSD: uhub.c,v 1.73 2005/02/27 00:27:51 perry 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.73 2005/02/27 00:27:51 perry 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 		up->reattach = 0;
314 		if (UHUB_IS_HIGH_SPEED(sc)) {
315 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
316 			up->tt->hub = hub;
317 		} else {
318 			up->tt = NULL;
319 		}
320 	}
321 
322 	/* XXX should check for none, individual, or ganged power? */
323 
324 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
325 	    + USB_EXTRA_POWER_UP_TIME;
326 	for (port = 1; port <= nports; port++) {
327 		/* Turn the power on. */
328 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
329 		if (err)
330 			printf("%s: port %d power on failed, %s\n",
331 			       USBDEVNAME(sc->sc_dev), port,
332 			       usbd_errstr(err));
333 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
334 		/* Wait for stable power. */
335 		usbd_delay_ms(dev, pwrdly);
336 	}
337 
338 	/* The usual exploration will finish the setup. */
339 
340 	sc->sc_running = 1;
341 
342 	USB_ATTACH_SUCCESS_RETURN;
343 
344  bad:
345 	if (hub)
346 		free(hub, M_USBDEV);
347 	dev->hub = NULL;
348 	USB_ATTACH_ERROR_RETURN;
349 }
350 
351 usbd_status
352 uhub_explore(usbd_device_handle dev)
353 {
354 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
355 	struct uhub_softc *sc = dev->hub->hubsoftc;
356 	struct usbd_port *up;
357 	usbd_status err;
358 	int speed;
359 	int port;
360 	int change, status, reconnect;
361 
362 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
363 
364 	if (!sc->sc_running)
365 		return (USBD_NOT_STARTED);
366 
367 	/* Ignore hubs that are too deep. */
368 	if (dev->depth > USB_HUB_MAX_DEPTH)
369 		return (USBD_TOO_DEEP);
370 
371 	for(port = 1; port <= hd->bNbrPorts; port++) {
372 		up = &dev->hub->ports[port-1];
373 		err = usbd_get_port_status(dev, port, &up->status);
374 		if (err) {
375 			DPRINTF(("uhub_explore: get port status failed, "
376 				 "error=%s\n", usbd_errstr(err)));
377 			continue;
378 		}
379 		status = UGETW(up->status.wPortStatus);
380 		change = UGETW(up->status.wPortChange);
381 		reconnect = up->reattach;
382 		up->reattach = 0;
383 		DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
384 			    USBDEVNAME(sc->sc_dev), port, status, change));
385 		if (change & UPS_C_PORT_ENABLED) {
386 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
387 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
388 			if (change & UPS_C_CONNECT_STATUS) {
389 				/* Ignore the port error if the device
390 				   vanished. */
391 			} else if (status & UPS_PORT_ENABLED) {
392 				printf("%s: illegal enable change, port %d\n",
393 				       USBDEVNAME(sc->sc_dev), port);
394 			} else {
395 				/* Port error condition. */
396 				if (up->restartcnt) /* no message first time */
397 					printf("%s: port error, restarting "
398 					       "port %d\n",
399 					       USBDEVNAME(sc->sc_dev), port);
400 
401 				if (up->restartcnt++ < USBD_RESTART_MAX)
402 					goto disco;
403 				else
404 					printf("%s: port error, giving up "
405 					       "port %d\n",
406 					       USBDEVNAME(sc->sc_dev), port);
407 			}
408 		}
409 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
410 			DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
411 				    "STATUS\n", port));
412 			/* No status change, just do recursive explore. */
413 			if (up->device != NULL && up->device->hub != NULL)
414 				up->device->hub->explore(up->device);
415 #if 0 && defined(DIAGNOSTIC)
416 			if (up->device == NULL &&
417 			    (status & UPS_CURRENT_CONNECT_STATUS))
418 				printf("%s: connected, no device\n",
419 				       USBDEVNAME(sc->sc_dev));
420 #endif
421 			continue;
422 		}
423 
424 		/* We have a connect status change, handle it. */
425 
426 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
427 			 dev->address, port));
428 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
429 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
430 		/*
431 		 * If there is already a device on the port the change status
432 		 * must mean that is has disconnected.  Looking at the
433 		 * current connect status is not enough to figure this out
434 		 * since a new unit may have been connected before we handle
435 		 * the disconnect.
436 		 */
437 	disco:
438 		if (up->device != NULL) {
439 			/* Disconnected */
440 			DPRINTF(("uhub_explore: device addr=%d disappeared "
441 				 "on port %d\n", up->device->address, port));
442 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
443 			usbd_clear_port_feature(dev, port,
444 						UHF_C_PORT_CONNECTION);
445 		}
446 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
447 			/* Nothing connected, just ignore it. */
448 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
449 				    "_STATUS\n", port));
450 			continue;
451 		}
452 
453 		/* Connected */
454 
455 		if (!(status & UPS_PORT_POWER))
456 			printf("%s: strange, connected port %d has no power\n",
457 			       USBDEVNAME(sc->sc_dev), port);
458 
459 		/* Wait for maximum device power up time. */
460 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
461 
462 		/* Reset port, which implies enabling it. */
463 		if (usbd_reset_port(dev, port, &up->status)) {
464 			printf("%s: port %d reset failed\n",
465 			       USBDEVNAME(sc->sc_dev), port);
466 			continue;
467 		}
468 		/* Get port status again, it might have changed during reset */
469 		err = usbd_get_port_status(dev, port, &up->status);
470 		if (err) {
471 			DPRINTF(("uhub_explore: get port status failed, "
472 				 "error=%s\n", usbd_errstr(err)));
473 			continue;
474 		}
475 		status = UGETW(up->status.wPortStatus);
476 		change = UGETW(up->status.wPortChange);
477 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
478 			/* Nothing connected, just ignore it. */
479 #ifdef DIAGNOSTIC
480 			printf("%s: port %d, device disappeared after reset\n",
481 			       USBDEVNAME(sc->sc_dev), port);
482 #endif
483 			continue;
484 		}
485 
486 #if 0
487 		if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
488 			printf("%s: port %d, transaction translation not "
489 			       "implemented, low/full speed device ignored\n",
490 			       USBDEVNAME(sc->sc_dev), port);
491 			continue;
492 		}
493 #endif
494 
495 		/* Figure out device speed */
496 		if (status & UPS_HIGH_SPEED)
497 			speed = USB_SPEED_HIGH;
498 		else if (status & UPS_LOW_SPEED)
499 			speed = USB_SPEED_LOW;
500 		else
501 			speed = USB_SPEED_FULL;
502 		/* Get device info and set its address. */
503 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
504 			  dev->depth + 1, speed, port, up);
505 		/* XXX retry a few times? */
506 		if (err) {
507 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
508 				     "error=%s\n", usbd_errstr(err)));
509 			/* Avoid addressing problems by disabling. */
510 			/* usbd_reset_port(dev, port, &up->status); */
511 
512 			/*
513 			 * The unit refused to accept a new address, or had
514 			 * some other serious problem.  Since we cannot leave
515 			 * at 0 we have to disable the port instead.
516 			 */
517 			printf("%s: device problem, disabling port %d\n",
518 			       USBDEVNAME(sc->sc_dev), port);
519 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
520 		} else {
521 			/* The port set up succeeded, reset error count. */
522 			up->restartcnt = 0;
523 
524 			if (up->device->hub)
525 				up->device->hub->explore(up->device);
526 		}
527 	}
528 	return (USBD_NORMAL_COMPLETION);
529 }
530 
531 #if defined(__NetBSD__) || defined(__OpenBSD__)
532 int
533 uhub_activate(device_ptr_t self, enum devact act)
534 {
535 	struct uhub_softc *sc = (struct uhub_softc *)self;
536 	struct usbd_hub *hub = sc->sc_hub->hub;
537 	usbd_device_handle dev;
538 	int nports, port, i;
539 
540 	switch (act) {
541 	case DVACT_ACTIVATE:
542 		return (EOPNOTSUPP);
543 
544 	case DVACT_DEACTIVATE:
545 		if (hub == NULL) /* malfunctioning hub */
546 			break;
547 		nports = hub->hubdesc.bNbrPorts;
548 		for(port = 0; port < nports; port++) {
549 			dev = hub->ports[port].device;
550 			if (dev != NULL && dev->subdevs != NULL) {
551 				for (i = 0; dev->subdevs[i] != NULL; i++)
552 					config_deactivate(dev->subdevs[i]);
553 			}
554 		}
555 		break;
556 	}
557 	return (0);
558 }
559 #endif
560 
561 /*
562  * Called from process context when the hub is gone.
563  * Detach all devices on active ports.
564  */
565 USB_DETACH(uhub)
566 {
567 	USB_DETACH_START(uhub, sc);
568 	struct usbd_hub *hub = sc->sc_hub->hub;
569 	struct usbd_port *rup;
570 	int port, nports;
571 
572 #if defined(__NetBSD__) || defined(__OpenBSD__)
573 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
574 #elif defined(__FreeBSD__)
575 	DPRINTF(("uhub_detach: sc=%port\n", sc));
576 #endif
577 
578 	if (hub == NULL)		/* Must be partially working */
579 		return (0);
580 
581 	usbd_abort_pipe(sc->sc_ipipe);
582 	usbd_close_pipe(sc->sc_ipipe);
583 
584 	nports = hub->hubdesc.bNbrPorts;
585 	for(port = 0; port < nports; port++) {
586 		rup = &hub->ports[port];
587 		if (rup->device)
588 			usb_disconnect_port(rup, self);
589 	}
590 
591 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
592 			   USBDEV(sc->sc_dev));
593 
594 	if (hub->ports[0].tt)
595 		free(hub->ports[0].tt, M_USBDEV);
596 	free(hub, M_USBDEV);
597 	sc->sc_hub->hub = NULL;
598 
599 	return (0);
600 }
601 
602 #if defined(__FreeBSD__)
603 /* Called when a device has been detached from it */
604 Static void
605 uhub_child_detached(device_t self, device_t child)
606 {
607        struct uhub_softc *sc = device_get_softc(self);
608        usbd_device_handle devhub = sc->sc_hub;
609        usbd_device_handle dev;
610        int nports;
611        int port;
612        int i;
613 
614        if (!devhub->hub)
615                /* should never happen; children are only created after init */
616                panic("hub not fully initialised, but child deleted?");
617 
618        nports = devhub->hub->hubdesc.bNbrPorts;
619        for (port = 0; port < nports; port++) {
620                dev = devhub->hub->ports[port].device;
621                if (dev && dev->subdevs) {
622                        for (i = 0; dev->subdevs[i]; i++) {
623                                if (dev->subdevs[i] == child) {
624                                        dev->subdevs[i] = NULL;
625                                        return;
626                                }
627                        }
628                }
629        }
630 }
631 #endif
632 
633 
634 /*
635  * Hub interrupt.
636  * This an indication that some port has changed status.
637  * Notify the bus event handler thread that we need
638  * to be explored again.
639  */
640 void
641 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
642 {
643 	struct uhub_softc *sc = addr;
644 
645 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
646 	if (status == USBD_STALLED)
647 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
648 	else if (status == USBD_NORMAL_COMPLETION)
649 		usb_needs_explore(sc->sc_hub);
650 }
651 
652 #if defined(__FreeBSD__)
653 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
654 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
655 #endif
656