xref: /openbsd/sys/dev/usb/usb.c (revision 898184e3)
1 /*	$OpenBSD: usb.c,v 1.82 2013/03/16 09:58:40 mpi Exp $	*/
2 /*	$NetBSD: usb.c,v 1.77 2003/01/01 00:10:26 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 2002 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 specifications and other documentation can be found at
36  * http://www.usb.org/developers/docs/ and
37  * http://www.usb.org/developers/devclass_docs/
38  */
39 
40 #include "ohci.h"
41 #include "uhci.h"
42 #include "ehci.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/timeout.h>
50 #include <sys/kthread.h>
51 #include <sys/proc.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/poll.h>
55 #include <sys/selinfo.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/time.h>
59 
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 
64 #include <machine/bus.h>
65 
66 #include <dev/usb/usbdivar.h>
67 #include <dev/usb/usb_quirks.h>
68 
69 #ifdef USB_DEBUG
70 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
71 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
72 int	usbdebug = 0;
73 #if defined(UHCI_DEBUG) && NUHCI > 0
74 extern int	uhcidebug;
75 #endif
76 #if defined(OHCI_DEBUG) && NOHCI > 0
77 extern int	ohcidebug;
78 #endif
79 #if defined(EHCI_DEBUG) && NEHCI > 0
80 extern int	ehcidebug;
81 #endif
82 /*
83  * 0  - do usual exploration
84  * !0 - do no exploration
85  */
86 int	usb_noexplore = 0;
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n,x)
90 #endif
91 
92 struct usb_softc {
93 	struct device	 sc_dev;	/* base device */
94 	usbd_bus_handle  sc_bus;	/* USB controller */
95 	struct usbd_port sc_port;	/* dummy port for root hub */
96 
97 	struct usb_task	 sc_explore_task;
98 
99 	struct timeval	 sc_ptime;
100 };
101 
102 TAILQ_HEAD(, usb_task) usb_abort_tasks;
103 TAILQ_HEAD(, usb_task) usb_explore_tasks;
104 TAILQ_HEAD(, usb_task) usb_generic_tasks;
105 
106 int usb_run_tasks, usb_run_abort_tasks;
107 int explore_pending;
108 
109 void	usb_explore(void *);
110 void	usb_create_task_threads(void *);
111 void	usb_task_thread(void *);
112 struct proc *usb_task_thread_proc = NULL;
113 void	usb_abort_task_thread(void *);
114 struct proc *usb_abort_task_thread_proc = NULL;
115 
116 const char *usbrev_str[] = USBREV_STR;
117 
118 int usb_match(struct device *, void *, void *);
119 void usb_attach(struct device *, struct device *, void *);
120 int usb_detach(struct device *, int);
121 int usb_activate(struct device *, int);
122 
123 struct cfdriver usb_cd = {
124 	NULL, "usb", DV_DULL
125 };
126 
127 const struct cfattach usb_ca = {
128 	sizeof(struct usb_softc),
129 	usb_match,
130 	usb_attach,
131 	usb_detach,
132 	usb_activate,
133 };
134 
135 int
136 usb_match(struct device *parent, void *match, void *aux)
137 {
138 	DPRINTF(("usbd_match\n"));
139 	return (UMATCH_GENERIC);
140 }
141 
142 void
143 usb_attach(struct device *parent, struct device *self, void *aux)
144 {
145 	struct usb_softc *sc = (struct usb_softc *)self;
146 	usbd_device_handle dev;
147 	usbd_status err;
148 	int usbrev;
149 	int speed;
150 
151 	DPRINTF(("usbd_attach\n"));
152 
153 	usbd_init();
154 	sc->sc_bus = aux;
155 	sc->sc_bus->usbctl = self;
156 	sc->sc_port.power = USB_MAX_POWER;
157 
158 	usbrev = sc->sc_bus->usbrev;
159 	printf(": USB revision %s", usbrev_str[usbrev]);
160 	switch (usbrev) {
161 	case USBREV_1_0:
162 	case USBREV_1_1:
163 		speed = USB_SPEED_FULL;
164 		break;
165 	case USBREV_2_0:
166 		speed = USB_SPEED_HIGH;
167 		break;
168 	default:
169 		printf(", not supported\n");
170 		sc->sc_bus->dying = 1;
171 		return;
172 	}
173 	printf("\n");
174 
175 	/* Make sure not to use tsleep() if we are cold booting. */
176 	if (cold)
177 		sc->sc_bus->use_polling++;
178 
179 	/* Don't let hub interrupts cause explore until ready. */
180 	sc->sc_bus->flags |= USB_BUS_CONFIG_PENDING;
181 
182 	/* explore task */
183 	usb_init_task(&sc->sc_explore_task, usb_explore, sc,
184 	    USB_TASK_TYPE_EXPLORE);
185 
186 	/* XXX we should have our own level */
187 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
188 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
189 	if (sc->sc_bus->soft == NULL) {
190 		printf("%s: can't register softintr\n", sc->sc_dev.dv_xname);
191 		sc->sc_bus->dying = 1;
192 		return;
193 	}
194 
195 	err = usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, speed, 0,
196 		  &sc->sc_port);
197 	if (!err) {
198 		dev = sc->sc_port.device;
199 		if (dev->hub == NULL) {
200 			sc->sc_bus->dying = 1;
201 			printf("%s: root device is not a hub\n",
202 			       sc->sc_dev.dv_xname);
203 			return;
204 		}
205 		sc->sc_bus->root_hub = dev;
206 #if 1
207 		/*
208 		 * Turning this code off will delay attachment of USB devices
209 		 * until the USB task thread is running, which means that
210 		 * the keyboard will not work until after cold boot.
211 		 */
212 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
213 			dev->hub->explore(sc->sc_bus->root_hub);
214 #endif
215 	} else {
216 		printf("%s: root hub problem, error=%d\n",
217 		       sc->sc_dev.dv_xname, err);
218 		sc->sc_bus->dying = 1;
219 	}
220 	if (cold)
221 		sc->sc_bus->use_polling--;
222 
223 	if (!sc->sc_bus->dying) {
224 		getmicrouptime(&sc->sc_ptime);
225 		if (sc->sc_bus->usbrev == USBREV_2_0)
226 			explore_pending++;
227 		config_pending_incr();
228 		usb_needs_explore(sc->sc_bus->root_hub, 1);
229 	}
230 }
231 
232 /*
233  * Called by usbd_init when first usb is attached.
234  */
235 void
236 usb_begin_tasks(void)
237 {
238 	TAILQ_INIT(&usb_abort_tasks);
239 	TAILQ_INIT(&usb_explore_tasks);
240 	TAILQ_INIT(&usb_generic_tasks);
241 	usb_run_tasks = usb_run_abort_tasks = 1;
242 	kthread_create_deferred(usb_create_task_threads, NULL);
243 }
244 
245 /*
246  * Called by usbd_finish when last usb is detached.
247  */
248 void
249 usb_end_tasks(void)
250 {
251 	usb_run_tasks = usb_run_abort_tasks = 0;
252 	wakeup(&usb_run_abort_tasks);
253 	wakeup(&usb_run_tasks);
254 }
255 
256 void
257 usb_create_task_threads(void *arg)
258 {
259 	if (kthread_create(usb_abort_task_thread, NULL,
260 	    &usb_abort_task_thread_proc, "usbatsk"))
261 		panic("unable to create usb abort task thread");
262 
263 	if (kthread_create(usb_task_thread, NULL,
264 	    &usb_task_thread_proc, "usbtask"))
265 		panic("unable to create usb task thread");
266 }
267 
268 /*
269  * Add a task to be performed by the task thread.  This function can be
270  * called from any context and the task will be executed in a process
271  * context ASAP.
272  */
273 void
274 usb_add_task(usbd_device_handle dev, struct usb_task *task)
275 {
276 	int s;
277 
278 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
279 	    task->state, task->type));
280 
281 	/* Don't add task if the device's root hub is dying. */
282 	if (usbd_is_dying(dev))
283 		return;
284 
285 	s = splusb();
286 	if (!(task->state & USB_TASK_STATE_ONQ)) {
287 		switch (task->type) {
288 		case USB_TASK_TYPE_ABORT:
289 			TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next);
290 			break;
291 		case USB_TASK_TYPE_EXPLORE:
292 			TAILQ_INSERT_TAIL(&usb_explore_tasks, task, next);
293 			break;
294 		case USB_TASK_TYPE_GENERIC:
295 			TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next);
296 			break;
297 		}
298 		task->state |= USB_TASK_STATE_ONQ;
299 		task->dev = dev;
300 	}
301 	if (task->type == USB_TASK_TYPE_ABORT)
302 		wakeup(&usb_run_abort_tasks);
303 	else
304 		wakeup(&usb_run_tasks);
305 	splx(s);
306 }
307 
308 void
309 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
310 {
311 	int s;
312 
313 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
314 	    task->state, task->type));
315 
316 	if (!(task->state & USB_TASK_STATE_ONQ))
317 		return;
318 
319 	s = splusb();
320 
321 	switch (task->type) {
322 	case USB_TASK_TYPE_ABORT:
323 		TAILQ_REMOVE(&usb_abort_tasks, task, next);
324 		break;
325 	case USB_TASK_TYPE_EXPLORE:
326 		TAILQ_REMOVE(&usb_explore_tasks, task, next);
327 		break;
328 	case USB_TASK_TYPE_GENERIC:
329 		TAILQ_REMOVE(&usb_generic_tasks, task, next);
330 		break;
331 	}
332 	task->state &= ~USB_TASK_STATE_ONQ;
333 	if (task->state == USB_TASK_STATE_NONE)
334 		wakeup(task);
335 
336 	splx(s);
337 }
338 
339 void
340 usb_wait_task(usbd_device_handle dev, struct usb_task *task)
341 {
342 	int s;
343 
344 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
345 	    task->state, task->type));
346 
347 	if (task->state == USB_TASK_STATE_NONE)
348 		return;
349 
350 	s = splusb();
351 	while (task->state != USB_TASK_STATE_NONE) {
352 		DPRINTF(("%s: waiting for task to complete\n", __func__));
353 		tsleep(task, PWAIT, "endtask", 0);
354 	}
355 	splx(s);
356 }
357 
358 void
359 usb_rem_wait_task(usbd_device_handle dev, struct usb_task *task)
360 {
361 	usb_rem_task(dev, task);
362 	usb_wait_task(dev, task);
363 }
364 
365 void
366 usb_task_thread(void *arg)
367 {
368 	struct usb_task *task;
369 	int s;
370 
371 	DPRINTF(("usb_task_thread: start\n"));
372 
373 	s = splusb();
374 	while (usb_run_tasks) {
375 		if ((task = TAILQ_FIRST(&usb_explore_tasks)) != NULL)
376 			TAILQ_REMOVE(&usb_explore_tasks, task, next);
377 		else if ((task = TAILQ_FIRST(&usb_generic_tasks)) != NULL)
378 			TAILQ_REMOVE(&usb_generic_tasks, task, next);
379 		else {
380 			tsleep(&usb_run_tasks, PWAIT, "usbtsk", 0);
381 			continue;
382 		}
383 		/*
384 		 * Set the state run bit before clearing the onq bit.
385 		 * This avoids state == none between dequeue and
386 		 * execution, which could cause usb_wait_task() to do
387 		 * the wrong thing.
388 		 */
389 		task->state |= USB_TASK_STATE_RUN;
390 		task->state &= ~USB_TASK_STATE_ONQ;
391 		/* Don't actually execute the task if dying. */
392 		if (!usbd_is_dying(task->dev)) {
393 			splx(s);
394 			task->fun(task->arg);
395 			s = splusb();
396 		}
397 		task->state &= ~USB_TASK_STATE_RUN;
398 		if (task->state == USB_TASK_STATE_NONE)
399 			wakeup(task);
400 	}
401 	splx(s);
402 
403 	kthread_exit(0);
404 }
405 
406 /*
407  * This thread is ONLY for the HCI drivers to be able to abort xfers.
408  * Synchronous xfers sleep the task thread, so the aborts need to happen
409  * in a different thread.
410  */
411 void
412 usb_abort_task_thread(void *arg)
413 {
414 	struct usb_task *task;
415 	int s;
416 
417 	DPRINTF(("usb_xfer_abort_thread: start\n"));
418 
419 	s = splusb();
420 	while (usb_run_abort_tasks) {
421 		if ((task = TAILQ_FIRST(&usb_abort_tasks)) != NULL)
422 			TAILQ_REMOVE(&usb_abort_tasks, task, next);
423 		else {
424 			tsleep(&usb_run_abort_tasks, PWAIT, "usbatsk", 0);
425 			continue;
426 		}
427 		/*
428 		 * Set the state run bit before clearing the onq bit.
429 		 * This avoids state == none between dequeue and
430 		 * execution, which could cause usb_wait_task() to do
431 		 * the wrong thing.
432 		 */
433 		task->state |= USB_TASK_STATE_RUN;
434 		task->state &= ~USB_TASK_STATE_ONQ;
435 		/* Don't actually execute the task if dying. */
436 		if (!usbd_is_dying(task->dev)) {
437 			splx(s);
438 			task->fun(task->arg);
439 			s = splusb();
440 		}
441 		task->state &= ~USB_TASK_STATE_RUN;
442 		if (task->state == USB_TASK_STATE_NONE)
443 			wakeup(task);
444 	}
445 	splx(s);
446 
447 	kthread_exit(0);
448 }
449 
450 int
451 usbctlprint(void *aux, const char *pnp)
452 {
453 	/* only "usb"es can attach to host controllers */
454 	if (pnp)
455 		printf("usb at %s", pnp);
456 
457 	return (UNCONF);
458 }
459 
460 int
461 usbopen(dev_t dev, int flag, int mode, struct proc *p)
462 {
463 	int unit = minor(dev);
464 	struct usb_softc *sc;
465 
466 	if (unit >= usb_cd.cd_ndevs)
467 		return (ENXIO);
468 	sc = usb_cd.cd_devs[unit];
469 	if (sc == NULL)
470 		return (ENXIO);
471 
472 	if (sc->sc_bus->dying)
473 		return (EIO);
474 
475 	return (0);
476 }
477 
478 int
479 usbclose(dev_t dev, int flag, int mode, struct proc *p)
480 {
481 	return (0);
482 }
483 
484 void
485 usbd_fill_di_task(void *arg)
486 {
487 	struct usb_device_info *di = (struct usb_device_info *)arg;
488 	struct usb_softc *sc;
489 	usbd_device_handle dev;
490 
491 	/* check that the bus and device are still present */
492 	if (di->udi_bus >= usb_cd.cd_ndevs)
493 		return;
494 	sc = usb_cd.cd_devs[di->udi_bus];
495 	if (sc == NULL)
496 		return;
497 	dev = sc->sc_bus->devices[di->udi_addr];
498 	if (dev == NULL)
499 		return;
500 
501 	usbd_fill_deviceinfo(dev, di, 1);
502 }
503 
504 int
505 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
506 {
507 	struct usb_softc *sc;
508 	int unit = minor(devt);
509 	int error;
510 
511 	sc = usb_cd.cd_devs[unit];
512 
513 	if (sc->sc_bus->dying)
514 		return (EIO);
515 
516 	error = 0;
517 	switch (cmd) {
518 #ifdef USB_DEBUG
519 	case USB_SETDEBUG:
520 		/* only root can access to these debug flags */
521 		if ((error = suser(curproc, 0)) != 0)
522 			return (error);
523 		if (!(flag & FWRITE))
524 			return (EBADF);
525 		usbdebug  = ((*(unsigned int *)data) & 0x000000ff);
526 #if defined(UHCI_DEBUG) && NUHCI > 0
527 		uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8;
528 #endif
529 #if defined(OHCI_DEBUG) && NOHCI > 0
530 		ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16;
531 #endif
532 #if defined(EHCI_DEBUG) && NEHCI > 0
533 		ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24;
534 #endif
535 		break;
536 #endif /* USB_DEBUG */
537 	case USB_REQUEST:
538 	{
539 		struct usb_ctl_request *ur = (void *)data;
540 		int len = UGETW(ur->ucr_request.wLength);
541 		struct iovec iov;
542 		struct uio uio;
543 		void *ptr = 0;
544 		int addr = ur->ucr_addr;
545 		usbd_status err;
546 		int error = 0;
547 
548 		if (!(flag & FWRITE))
549 			return (EBADF);
550 
551 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
552 		if (len < 0 || len > 32768)
553 			return (EINVAL);
554 		if (addr < 0 || addr >= USB_MAX_DEVICES)
555 			return (EINVAL);
556 		if (sc->sc_bus->devices[addr] == NULL)
557 			return (ENXIO);
558 		if (len != 0) {
559 			iov.iov_base = (caddr_t)ur->ucr_data;
560 			iov.iov_len = len;
561 			uio.uio_iov = &iov;
562 			uio.uio_iovcnt = 1;
563 			uio.uio_resid = len;
564 			uio.uio_offset = 0;
565 			uio.uio_segflg = UIO_USERSPACE;
566 			uio.uio_rw =
567 				ur->ucr_request.bmRequestType & UT_READ ?
568 				UIO_READ : UIO_WRITE;
569 			uio.uio_procp = p;
570 			ptr = malloc(len, M_TEMP, M_WAITOK);
571 			if (uio.uio_rw == UIO_WRITE) {
572 				error = uiomove(ptr, len, &uio);
573 				if (error)
574 					goto ret;
575 			}
576 		}
577 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
578 			  &ur->ucr_request, ptr, ur->ucr_flags,
579 			  &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT);
580 		if (err) {
581 			error = EIO;
582 			goto ret;
583 		}
584 		if (len != 0) {
585 			if (uio.uio_rw == UIO_READ) {
586 				error = uiomove(ptr, len, &uio);
587 				if (error)
588 					goto ret;
589 			}
590 		}
591 	ret:
592 		if (ptr)
593 			free(ptr, M_TEMP);
594 		return (error);
595 	}
596 
597 	case USB_DEVICEINFO:
598 	{
599 		struct usb_device_info *di = (void *)data;
600 		int addr = di->udi_addr;
601 		struct usb_task di_task;
602 		usbd_device_handle dev;
603 
604 		if (addr < 1 || addr >= USB_MAX_DEVICES)
605 			return (EINVAL);
606 
607 		dev = sc->sc_bus->devices[addr];
608 		if (dev == NULL)
609 			return (ENXIO);
610 
611 		di->udi_bus = unit;
612 
613 		/* All devices get a driver, thanks to ugen(4).  If the
614 		 * task ends without adding a driver name, there was an error.
615 		 */
616 		di->udi_devnames[0][0] = '\0';
617 
618 		usb_init_task(&di_task, usbd_fill_di_task, di,
619 		    USB_TASK_TYPE_GENERIC);
620 		usb_add_task(sc->sc_bus->root_hub, &di_task);
621 		usb_wait_task(sc->sc_bus->root_hub, &di_task);
622 
623 		if (di->udi_devnames[0][0] == '\0')
624 			return (ENXIO);
625 
626 		break;
627 	}
628 
629 	case USB_DEVICESTATS:
630 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
631 		break;
632 
633 	default:
634 		return (EINVAL);
635 	}
636 	return (0);
637 }
638 
639 /*
640  * Explore device tree from the root.  We need mutual exclusion to this
641  * hub while traversing the device tree, but this is guaranteed since this
642  * function is only called from the task thread, with one exception:
643  * usb_attach() calls this function, but there shouldn't be anything else
644  * trying to explore this hub at that time.
645  */
646 void
647 usb_explore(void *v)
648 {
649 	struct usb_softc *sc = v;
650 	struct timeval now, waited;
651 	int pwrdly, waited_ms;
652 
653 	DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname));
654 #ifdef USB_DEBUG
655 	if (usb_noexplore)
656 		return;
657 #endif
658 
659 	if (sc->sc_bus->dying)
660 		return;
661 
662 	if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) {
663 		/*
664 		 * If this is a low/full speed hub and there is a high
665 		 * speed hub that hasn't explored yet, reshedule this
666 		 * task, allowing the high speed explore task to run.
667 		 */
668 		if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) {
669 			usb_add_task(sc->sc_bus->root_hub,
670 			    &sc->sc_explore_task);
671 			return;
672 		}
673 
674 		/*
675 		 * Wait for power to stabilize.
676 		 */
677 		getmicrouptime(&now);
678 		timersub(&now, &sc->sc_ptime, &waited);
679 		waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000;
680 
681 		pwrdly = sc->sc_bus->root_hub->hub->hubdesc.bPwrOn2PwrGood *
682 		    UHD_PWRON_FACTOR + USB_EXTRA_POWER_UP_TIME;
683 		if (pwrdly > waited_ms)
684 			usb_delay_ms(sc->sc_bus, pwrdly - waited_ms);
685 	}
686 
687 	sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
688 
689 	if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) {
690 		DPRINTF(("%s: %s: first explore done\n", __func__,
691 		    sc->sc_dev.dv_xname));
692 		if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending)
693 			explore_pending--;
694 		config_pending_decr();
695 		sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING);
696 	}
697 }
698 
699 void
700 usb_needs_explore(usbd_device_handle dev, int first_explore)
701 {
702 	struct usb_softc *usbctl = (struct usb_softc *)dev->bus->usbctl;
703 
704 	DPRINTFN(3,("%s: %s\n", usbctl->sc_dev.dv_xname, __func__));
705 
706 	if (!first_explore && (dev->bus->flags & USB_BUS_CONFIG_PENDING)) {
707 		DPRINTF(("%s: %s: not exploring before first explore\n",
708 		    __func__, usbctl->sc_dev.dv_xname));
709 		return;
710 	}
711 
712 	usb_add_task(dev, &usbctl->sc_explore_task);
713 }
714 
715 void
716 usb_needs_reattach(usbd_device_handle dev)
717 {
718 	DPRINTFN(2,("usb_needs_reattach\n"));
719 	dev->powersrc->reattach = 1;
720 	usb_needs_explore(dev, 0);
721 }
722 
723 void
724 usb_schedsoftintr(usbd_bus_handle bus)
725 {
726 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
727 
728 	if (bus->use_polling) {
729 		bus->methods->soft_intr(bus);
730 	} else {
731 		softintr_schedule(bus->soft);
732 	}
733 }
734 
735 int
736 usb_activate(struct device *self, int act)
737 {
738 	struct usb_softc *sc = (struct usb_softc *)self;
739 	usbd_device_handle dev = sc->sc_port.device;
740 	int i, rv = 0, r;
741 
742 	switch (act) {
743 	case DVACT_DEACTIVATE:
744 		sc->sc_bus->dying = 1;
745 		if (dev != NULL && dev->cdesc != NULL &&
746 		    dev->subdevs != NULL) {
747 			for (i = 0; dev->subdevs[i]; i++) {
748 				r = config_deactivate(dev->subdevs[i]);
749 				if (r)
750 					rv = r;
751 			}
752 		}
753 		break;
754 	case DVACT_RESUME:
755 		usb_needs_explore(sc->sc_bus->root_hub, 0);
756 		break;
757 	}
758 	return (rv);
759 }
760 
761 int
762 usb_detach(struct device *self, int flags)
763 {
764 	struct usb_softc *sc = (struct usb_softc *)self;
765 
766 	DPRINTF(("usb_detach: start\n"));
767 
768 	sc->sc_bus->dying = 1;
769 
770 	if (sc->sc_bus->root_hub != NULL) {
771 		/* Make all devices disconnect. */
772 		if (sc->sc_port.device != NULL)
773 			usb_disconnect_port(&sc->sc_port, self);
774 
775 		usb_rem_wait_task(sc->sc_bus->root_hub, &sc->sc_explore_task);
776 
777 		usbd_finish();
778 	}
779 
780 	if (sc->sc_bus->soft != NULL) {
781 		softintr_disestablish(sc->sc_bus->soft);
782 		sc->sc_bus->soft = NULL;
783 	}
784 
785 	return (0);
786 }
787