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