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