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