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