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