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