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