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