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