xref: /netbsd-src/sys/dev/usb/uhub.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: uhub.c,v 1.97 2008/04/28 20:23:59 martin 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.97 2008/04/28 20:23:59 martin Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/device.h>
47 #include <sys/proc.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/module.h>
50 #include <sys/bus.h>
51 #include "bus_if.h"
52 #endif
53 
54 #include <sys/bus.h>
55 
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdivar.h>
60 
61 #ifdef UHUB_DEBUG
62 #define DPRINTF(x)	if (uhubdebug) logprintf x
63 #define DPRINTFN(n,x)	if (uhubdebug>(n)) logprintf x
64 int	uhubdebug = 0;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69 
70 struct uhub_softc {
71 	USBBASEDEVICE		sc_dev;		/* base device */
72 	usbd_device_handle	sc_hub;		/* USB device */
73 	int			sc_proto;	/* device protocol */
74 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
75 
76 	/* XXX second buffer needed because we can't suspend pipes yet */
77 	u_int8_t		*sc_statusbuf;
78 	u_int8_t		*sc_status;
79 	size_t			sc_statuslen;
80 	int			sc_explorepending;
81 
82 	u_char			sc_running;
83 };
84 
85 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
86 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
87 
88 #define PORTSTAT_ISSET(sc, port) \
89 	((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
90 
91 Static usbd_status uhub_explore(usbd_device_handle hub);
92 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
93 
94 
95 /*
96  * We need two attachment points:
97  * hub to usb and hub to hub
98  * Every other driver only connects to hubs
99  */
100 
101 int uhub_match(device_t, struct cfdata *, void *);
102 void uhub_attach(device_t, device_t, void *);
103 void uhub_childdet(device_t, device_t);
104 int uhub_detach(device_t, int);
105 int uhub_activate(device_t, enum devact);
106 extern struct cfdriver uhub_cd;
107 CFATTACH_DECL2(uhub, sizeof(struct uhub_softc), uhub_match,
108     uhub_attach, uhub_detach, uhub_activate, NULL, uhub_childdet);
109 
110 USB_MATCH(uhub)
111 {
112 	USB_MATCH_START(uhub, uaa);
113 
114 	DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
115 	/*
116 	 * The subclass for hubs seems to be 0 for some and 1 for others,
117 	 * so we just ignore the subclass.
118 	 */
119 	if (uaa->class == UDCLASS_HUB)
120 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
121 	return (UMATCH_NONE);
122 }
123 
124 USB_ATTACH(uhub)
125 {
126 	USB_ATTACH_START(uhub, sc, uaa);
127 	usbd_device_handle dev = uaa->device;
128 	char *devinfop;
129 	usbd_status err;
130 	struct usbd_hub *hub = NULL;
131 	usb_device_request_t req;
132 	usb_hub_descriptor_t hubdesc;
133 	int p, port, nports, nremov, pwrdly;
134 	usbd_interface_handle iface;
135 	usb_endpoint_descriptor_t *ed;
136 #if 0 /* notyet */
137 	struct usbd_tt *tts = NULL;
138 #endif
139 
140 	DPRINTFN(1,("uhub_attach\n"));
141 	sc->sc_hub = dev;
142 	sc->sc_proto = uaa->proto;
143 
144 	devinfop = usbd_devinfo_alloc(dev, 1);
145 	aprint_naive("\n");
146 	aprint_normal(": %s\n", devinfop);
147 	usbd_devinfo_free(devinfop);
148 
149 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
150 		aprint_normal("%s: %s transaction translator%s\n",
151 		       USBDEVNAME(sc->sc_dev),
152 		       UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
153 		       UHUB_IS_SINGLE_TT(sc) ? "" : "s");
154 	}
155 
156 	err = usbd_set_config_index(dev, 0, 1);
157 	if (err) {
158 		DPRINTF(("%s: configuration failed, error=%s\n",
159 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
160 		USB_ATTACH_ERROR_RETURN;
161 	}
162 
163 	if (dev->depth > USB_HUB_MAX_DEPTH) {
164 		aprint_error("%s: hub depth (%d) exceeded, hub ignored\n",
165 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
166 		USB_ATTACH_ERROR_RETURN;
167 	}
168 
169 	/* Get hub descriptor. */
170 	req.bmRequestType = UT_READ_CLASS_DEVICE;
171 	req.bRequest = UR_GET_DESCRIPTOR;
172 	USETW2(req.wValue, UDESC_HUB, 0);
173 	USETW(req.wIndex, 0);
174 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
175 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
176 	err = usbd_do_request(dev, &req, &hubdesc);
177 	nports = hubdesc.bNbrPorts;
178 	if (!err && nports > 7) {
179 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
180 		err = usbd_do_request(dev, &req, &hubdesc);
181 	}
182 	if (err) {
183 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
184 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
185 		USB_ATTACH_ERROR_RETURN;
186 	}
187 
188 	for (nremov = 0, port = 1; port <= nports; port++)
189 		if (!UHD_NOT_REMOV(&hubdesc, port))
190 			nremov++;
191 	aprint_verbose("%s: %d port%s with %d removable, %s powered\n",
192 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
193 	       nremov, dev->self_powered ? "self" : "bus");
194 
195 	if (nports == 0) {
196 		aprint_debug("%s: no ports, hub ignored\n",
197 		    USBDEVNAME(sc->sc_dev));
198 		goto bad;
199 	}
200 
201 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
202 		     M_USBDEV, M_NOWAIT);
203 	if (hub == NULL)
204 		USB_ATTACH_ERROR_RETURN;
205 	dev->hub = hub;
206 	dev->hub->hubsoftc = sc;
207 	hub->explore = uhub_explore;
208 	hub->hubdesc = hubdesc;
209 
210 	/* Set up interrupt pipe. */
211 	err = usbd_device2interface_handle(dev, 0, &iface);
212 	if (err) {
213 		aprint_error("%s: no interface handle\n",
214 		    USBDEVNAME(sc->sc_dev));
215 		goto bad;
216 	}
217 
218 	if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
219 		err = usbd_set_interface(iface, 1);
220 		if (err)
221 			aprint_error("%s: can't enable multiple TTs\n",
222 			       USBDEVNAME(sc->sc_dev));
223 	}
224 
225 	ed = usbd_interface2endpoint_descriptor(iface, 0);
226 	if (ed == NULL) {
227 		aprint_error("%s: no endpoint descriptor\n",
228 		    USBDEVNAME(sc->sc_dev));
229 		goto bad;
230 	}
231 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
232 		aprint_error("%s: bad interrupt endpoint\n",
233 		    USBDEVNAME(sc->sc_dev));
234 		goto bad;
235 	}
236 
237 	sc->sc_statuslen = (nports + 1 + 7) / 8;
238 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
239 	if (!sc->sc_statusbuf)
240 		goto bad;
241 	sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
242 	if (!sc->sc_status)
243 		goto bad;
244 
245 	/* force initial scan */
246 	memset(sc->sc_status, 0xff, sc->sc_statuslen);
247 	sc->sc_explorepending = 1;
248 
249 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
250 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
251 		  sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
252 	if (err) {
253 		aprint_error("%s: cannot open interrupt pipe\n",
254 		       USBDEVNAME(sc->sc_dev));
255 		goto bad;
256 	}
257 
258 	/* Wait with power off for a while. */
259 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
260 
261 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
262 
263 	/*
264 	 * To have the best chance of success we do things in the exact same
265 	 * order as Windoze98.  This should not be necessary, but some
266 	 * devices do not follow the USB specs to the letter.
267 	 *
268 	 * These are the events on the bus when a hub is attached:
269 	 *  Get device and config descriptors (see attach code)
270 	 *  Get hub descriptor (see above)
271 	 *  For all ports
272 	 *     turn on power
273 	 *     wait for power to become stable
274 	 * (all below happens in explore code)
275 	 *  For all ports
276 	 *     clear C_PORT_CONNECTION
277 	 *  For all ports
278 	 *     get port status
279 	 *     if device connected
280 	 *        wait 100 ms
281 	 *        turn on reset
282 	 *        wait
283 	 *        clear C_PORT_RESET
284 	 *        get port status
285 	 *        proceed with device attachment
286 	 */
287 
288 #if 0
289 	if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
290 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
291 			     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
292 		if (!tts)
293 			goto bad;
294 	}
295 #endif
296 	/* Set up data structures */
297 	for (p = 0; p < nports; p++) {
298 		struct usbd_port *up = &hub->ports[p];
299 		up->device = NULL;
300 		up->parent = dev;
301 		up->portno = p+1;
302 		if (dev->self_powered)
303 			/* Self powered hub, give ports maximum current. */
304 			up->power = USB_MAX_POWER;
305 		else
306 			up->power = USB_MIN_POWER;
307 		up->restartcnt = 0;
308 		up->reattach = 0;
309 #if 0
310 		if (UHUB_IS_HIGH_SPEED(sc)) {
311 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
312 			up->tt->hub = hub;
313 		} else {
314 			up->tt = NULL;
315 		}
316 #endif
317 	}
318 
319 	/* XXX should check for none, individual, or ganged power? */
320 
321 	pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
322 	    + USB_EXTRA_POWER_UP_TIME;
323 	for (port = 1; port <= nports; port++) {
324 		/* Turn the power on. */
325 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
326 		if (err)
327 			aprint_error("%s: port %d power on failed, %s\n",
328 			       USBDEVNAME(sc->sc_dev), port,
329 			       usbd_errstr(err));
330 		DPRINTF(("usb_init_port: turn on port %d power\n", port));
331 	}
332 
333 	/* Wait for stable power if we are not a root hub */
334 	if (dev->powersrc->parent != NULL)
335 		usbd_delay_ms(dev, pwrdly);
336 
337 	/* The usual exploration will finish the setup. */
338 
339 	sc->sc_running = 1;
340 
341 	if (!pmf_device_register(self, NULL, NULL))
342 		aprint_error_dev(self, "couldn't establish power handler\n");
343 
344 	USB_ATTACH_SUCCESS_RETURN;
345 
346  bad:
347 	if (sc->sc_status)
348 		free(sc->sc_status, M_USBDEV);
349 	if (hub)
350 		free(hub, M_USBDEV);
351 	dev->hub = NULL;
352 	USB_ATTACH_ERROR_RETURN;
353 }
354 
355 usbd_status
356 uhub_explore(usbd_device_handle dev)
357 {
358 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
359 	struct uhub_softc *sc = dev->hub->hubsoftc;
360 	struct usbd_port *up;
361 	usbd_status err;
362 	int speed;
363 	int port;
364 	int change, status, reconnect;
365 
366 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
367 
368 	if (!sc->sc_running)
369 		return (USBD_NOT_STARTED);
370 
371 	/* Ignore hubs that are too deep. */
372 	if (dev->depth > USB_HUB_MAX_DEPTH)
373 		return (USBD_TOO_DEEP);
374 
375 	for (port = 1; port <= hd->bNbrPorts; port++) {
376 		up = &dev->hub->ports[port-1];
377 
378 		/* reattach is needed after firmware upload */
379 		reconnect = up->reattach;
380 		up->reattach = 0;
381 
382 		status = change = 0;
383 
384 		/* don't check if no change summary notification */
385 		if (PORTSTAT_ISSET(sc, port) || reconnect) {
386 			err = usbd_get_port_status(dev, port, &up->status);
387 			if (err) {
388 				DPRINTF(("uhub_explore: get port stat failed, "
389 					 "error=%s\n", usbd_errstr(err)));
390 				continue;
391 			}
392 			status = UGETW(up->status.wPortStatus);
393 			change = UGETW(up->status.wPortChange);
394 		}
395 		if (!change && !reconnect) {
396 			/* No status change, just do recursive explore. */
397 			if (up->device != NULL && up->device->hub != NULL)
398 				up->device->hub->explore(up->device);
399 			continue;
400 		}
401 
402 		if (change & UPS_C_PORT_ENABLED) {
403 			DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
404 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
405 			if (change & UPS_C_CONNECT_STATUS) {
406 				/* Ignore the port error if the device
407 				   vanished. */
408 			} else if (status & UPS_PORT_ENABLED) {
409 				printf("%s: illegal enable change, port %d\n",
410 				       USBDEVNAME(sc->sc_dev), port);
411 			} else {
412 				/* Port error condition. */
413 				if (up->restartcnt) /* no message first time */
414 					printf("%s: port error, restarting "
415 					       "port %d\n",
416 					       USBDEVNAME(sc->sc_dev), port);
417 
418 				if (up->restartcnt++ < USBD_RESTART_MAX)
419 					goto disco;
420 				else
421 					printf("%s: port error, giving up "
422 					       "port %d\n",
423 					       USBDEVNAME(sc->sc_dev), port);
424 			}
425 		}
426 
427 		/* XXX handle overcurrent and resume events! */
428 
429 		if (!(change & UPS_C_CONNECT_STATUS))
430 			continue;
431 
432 		/* We have a connect status change, handle it. */
433 
434 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
435 			 dev->address, port));
436 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
437 		/*
438 		 * If there is already a device on the port the change status
439 		 * must mean that is has disconnected.  Looking at the
440 		 * current connect status is not enough to figure this out
441 		 * since a new unit may have been connected before we handle
442 		 * the disconnect.
443 		 */
444 	disco:
445 		if (up->device != NULL) {
446 			/* Disconnected */
447 			DPRINTF(("uhub_explore: device addr=%d disappeared "
448 				 "on port %d\n", up->device->address, port));
449 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
450 			usbd_clear_port_feature(dev, port,
451 						UHF_C_PORT_CONNECTION);
452 		}
453 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
454 			/* Nothing connected, just ignore it. */
455 			DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
456 				    "_STATUS\n", port));
457 			continue;
458 		}
459 
460 		/* Connected */
461 
462 		if (!(status & UPS_PORT_POWER))
463 			printf("%s: strange, connected port %d has no power\n",
464 			       USBDEVNAME(sc->sc_dev), port);
465 
466 		/* Wait for maximum device power up time. */
467 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
468 
469 		/* Reset port, which implies enabling it. */
470 		if (usbd_reset_port(dev, port, &up->status)) {
471 			printf("%s: port %d reset failed\n",
472 			       USBDEVNAME(sc->sc_dev), port);
473 			continue;
474 		}
475 		/* Get port status again, it might have changed during reset */
476 		err = usbd_get_port_status(dev, port, &up->status);
477 		if (err) {
478 			DPRINTF(("uhub_explore: get port status failed, "
479 				 "error=%s\n", usbd_errstr(err)));
480 			continue;
481 		}
482 		status = UGETW(up->status.wPortStatus);
483 		change = UGETW(up->status.wPortChange);
484 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
485 			/* Nothing connected, just ignore it. */
486 #ifdef DIAGNOSTIC
487 			printf("%s: port %d, device disappeared after reset\n",
488 			       USBDEVNAME(sc->sc_dev), port);
489 #endif
490 			continue;
491 		}
492 
493 		/* Figure out device speed */
494 		if (status & UPS_HIGH_SPEED)
495 			speed = USB_SPEED_HIGH;
496 		else if (status & UPS_LOW_SPEED)
497 			speed = USB_SPEED_LOW;
498 		else
499 			speed = USB_SPEED_FULL;
500 		/* Get device info and set its address. */
501 		err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
502 			  dev->depth + 1, speed, port, up);
503 		/* XXX retry a few times? */
504 		if (err) {
505 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
506 				     "error=%s\n", usbd_errstr(err)));
507 			/* Avoid addressing problems by disabling. */
508 			/* usbd_reset_port(dev, port, &up->status); */
509 
510 			/*
511 			 * The unit refused to accept a new address, or had
512 			 * some other serious problem.  Since we cannot leave
513 			 * at 0 we have to disable the port instead.
514 			 */
515 			printf("%s: device problem, disabling port %d\n",
516 			       USBDEVNAME(sc->sc_dev), port);
517 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
518 		} else {
519 			/* The port set up succeeded, reset error count. */
520 			up->restartcnt = 0;
521 
522 			if (up->device->hub)
523 				up->device->hub->explore(up->device);
524 		}
525 	}
526 	/* enable status change notifications again */
527 	sc->sc_explorepending = 0;
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 	pmf_device_deregister(self);
582 	usbd_abort_pipe(sc->sc_ipipe);
583 	usbd_close_pipe(sc->sc_ipipe);
584 
585 	nports = hub->hubdesc.bNbrPorts;
586 	for(port = 0; port < nports; port++) {
587 		rup = &hub->ports[port];
588 		if (rup->device)
589 			usb_disconnect_port(rup, self);
590 	}
591 
592 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
593 			   USBDEV(sc->sc_dev));
594 
595 #if 0
596 	if (hub->ports[0].tt)
597 		free(hub->ports[0].tt, M_USBDEV);
598 #endif
599 	free(hub, M_USBDEV);
600 	sc->sc_hub->hub = NULL;
601 	if (sc->sc_status)
602 		free(sc->sc_status, M_USBDEV);
603 
604 	return (0);
605 }
606 
607 /* Called when a device has been detached from it */
608 void
609 uhub_childdet(device_t self, device_t child)
610 {
611 	struct uhub_softc *sc = device_private(self);
612 	usbd_device_handle devhub = sc->sc_hub;
613 	usbd_device_handle dev;
614 	int nports;
615 	int port;
616 	int i;
617 
618 	if (!devhub->hub)
619 		/* should never happen; children are only created after init */
620 		panic("hub not fully initialised, but child deleted?");
621 
622 	nports = devhub->hub->hubdesc.bNbrPorts;
623 	for (port = 0; port < nports; port++) {
624 		dev = devhub->hub->ports[port].device;
625 		if (dev == NULL || dev->subdevs == NULL)
626 			continue;
627 		for (i = 0; dev->subdevs[i]; i++) {
628 			if (dev->subdevs[i] == child) {
629 				dev->subdevs[i] = NULL;
630 				return;
631 			}
632 		}
633 	}
634 	KASSERT(false);
635 }
636 
637 
638 /*
639  * Hub interrupt.
640  * This an indication that some port has changed status.
641  * Notify the bus event handler thread that we need
642  * to be explored again.
643  */
644 void
645 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
646     usbd_status status)
647 {
648 	struct uhub_softc *sc = addr;
649 
650 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
651 
652 	if (status == USBD_STALLED)
653 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
654 	else if (status == USBD_NORMAL_COMPLETION &&
655 		 !sc->sc_explorepending) {
656 		/*
657 		 * Make sure the status is not overwritten in between.
658 		 * XXX we should suspend the pipe instead
659 		 */
660 		memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
661 		sc->sc_explorepending = 1;
662 		usb_needs_explore(sc->sc_hub);
663 	}
664 	/*
665 	 * XXX workaround for broken implementation of the interrupt
666 	 * pipe in EHCI root hub emulation which doesn't resend
667 	 * status change notifications until handled: force a rescan
668 	 * of the ports we touched in the last run
669 	 */
670 	if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
671       !strcmp(sc->sc_dev.dv_parent->dv_parent->dv_cfdriver->cd_name, "ehci"))
672 		usb_needs_explore(sc->sc_hub);
673 }
674 
675 #if defined(__FreeBSD__)
676 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
677 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
678 #endif
679