xref: /openbsd/sys/dev/usb/usb.c (revision a6445c1d)
1 /*	$OpenBSD: usb.c,v 1.102 2014/08/10 11:18:57 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/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/poll.h>
54 #include <sys/selinfo.h>
55 #include <sys/signalvar.h>
56 #include <sys/time.h>
57 #include <sys/rwlock.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 
63 #include <machine/bus.h>
64 
65 #include <dev/usb/usbdivar.h>
66 #include <dev/usb/usb_quirks.h>
67 
68 #ifdef USB_DEBUG
69 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
70 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
71 int	usbdebug = 0;
72 #if defined(UHCI_DEBUG) && NUHCI > 0
73 extern int	uhcidebug;
74 #endif
75 #if defined(OHCI_DEBUG) && NOHCI > 0
76 extern int	ohcidebug;
77 #endif
78 #if defined(EHCI_DEBUG) && NEHCI > 0
79 extern int	ehcidebug;
80 #endif
81 /*
82  * 0  - do usual exploration
83  * !0 - do no exploration
84  */
85 int	usb_noexplore = 0;
86 #else
87 #define DPRINTF(x)
88 #define DPRINTFN(n,x)
89 #endif
90 
91 struct usb_softc {
92 	struct device	 sc_dev;	/* base device */
93 	struct usbd_bus  *sc_bus;	/* USB controller */
94 	struct usbd_port sc_port;	/* dummy port for root hub */
95 	int		 sc_speed;
96 
97 	struct usb_task	 sc_explore_task;
98 
99 	struct timeval	 sc_ptime;
100 };
101 
102 struct rwlock usbpalock;
103 
104 TAILQ_HEAD(, usb_task) usb_abort_tasks;
105 TAILQ_HEAD(, usb_task) usb_explore_tasks;
106 TAILQ_HEAD(, usb_task) usb_generic_tasks;
107 
108 static int usb_nbuses = 0;
109 static int usb_run_tasks, usb_run_abort_tasks;
110 int explore_pending;
111 const char *usbrev_str[] = USBREV_STR;
112 
113 void		 usb_explore(void *);
114 void		 usb_create_task_threads(void *);
115 void		 usb_task_thread(void *);
116 struct proc	*usb_task_thread_proc = NULL;
117 void		 usb_abort_task_thread(void *);
118 struct proc	*usb_abort_task_thread_proc = NULL;
119 
120 void		 usb_fill_di_task(void *);
121 void		 usb_fill_udc_task(void *);
122 void		 usb_fill_udf_task(void *);
123 
124 int		 usb_match(struct device *, void *, void *);
125 void		 usb_attach(struct device *, struct device *, void *);
126 int		 usb_detach(struct device *, int);
127 int		 usb_activate(struct device *, int);
128 
129 int		 usb_attach_roothub(struct usb_softc *);
130 void		 usb_detach_roothub(struct usb_softc *);
131 
132 struct cfdriver usb_cd = {
133 	NULL, "usb", DV_DULL
134 };
135 
136 const struct cfattach usb_ca = {
137 	sizeof(struct usb_softc), usb_match, usb_attach, usb_detach,
138 	usb_activate,
139 };
140 
141 int
142 usb_match(struct device *parent, void *match, void *aux)
143 {
144 	return (1);
145 }
146 
147 void
148 usb_attach(struct device *parent, struct device *self, void *aux)
149 {
150 	struct usb_softc *sc = (struct usb_softc *)self;
151 	int usbrev;
152 
153 	if (usb_nbuses == 0) {
154 		rw_init(&usbpalock, "usbpalock");
155 		TAILQ_INIT(&usb_abort_tasks);
156 		TAILQ_INIT(&usb_explore_tasks);
157 		TAILQ_INIT(&usb_generic_tasks);
158 		usb_run_tasks = usb_run_abort_tasks = 1;
159 		kthread_create_deferred(usb_create_task_threads, NULL);
160 	}
161 	usb_nbuses++;
162 
163 	sc->sc_bus = aux;
164 	sc->sc_bus->usbctl = self;
165 	sc->sc_port.power = USB_MAX_POWER;
166 
167 	usbrev = sc->sc_bus->usbrev;
168 	printf(": USB revision %s", usbrev_str[usbrev]);
169 	switch (usbrev) {
170 	case USBREV_1_0:
171 	case USBREV_1_1:
172 		sc->sc_speed = USB_SPEED_FULL;
173 		break;
174 	case USBREV_2_0:
175 		sc->sc_speed = USB_SPEED_HIGH;
176 		break;
177 	case USBREV_3_0:
178 		sc->sc_speed = USB_SPEED_SUPER;
179 		break;
180 	default:
181 		printf(", not supported\n");
182 		sc->sc_bus->dying = 1;
183 		return;
184 	}
185 	printf("\n");
186 
187 	/* Make sure not to use tsleep() if we are cold booting. */
188 	if (cold)
189 		sc->sc_bus->use_polling++;
190 
191 	/* Don't let hub interrupts cause explore until ready. */
192 	sc->sc_bus->flags |= USB_BUS_CONFIG_PENDING;
193 
194 	/* explore task */
195 	usb_init_task(&sc->sc_explore_task, usb_explore, sc,
196 	    USB_TASK_TYPE_EXPLORE);
197 
198 	sc->sc_bus->soft = softintr_establish(IPL_SOFTUSB,
199 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
200 	if (sc->sc_bus->soft == NULL) {
201 		printf("%s: can't register softintr\n", sc->sc_dev.dv_xname);
202 		sc->sc_bus->dying = 1;
203 		return;
204 	}
205 
206 
207 
208 	if (!usb_attach_roothub(sc)) {
209 		struct usbd_device *dev = sc->sc_bus->root_hub;
210 #if 1
211 		/*
212 		 * Turning this code off will delay attachment of USB devices
213 		 * until the USB task thread is running, which means that
214 		 * the keyboard will not work until after cold boot.
215 		 */
216 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
217 			dev->hub->explore(sc->sc_bus->root_hub);
218 #endif
219 	}
220 
221 	if (cold)
222 		sc->sc_bus->use_polling--;
223 
224 	if (!sc->sc_bus->dying) {
225 		getmicrouptime(&sc->sc_ptime);
226 		if (sc->sc_bus->usbrev == USBREV_2_0)
227 			explore_pending++;
228 		config_pending_incr();
229 		usb_needs_explore(sc->sc_bus->root_hub, 1);
230 	}
231 }
232 
233 int
234 usb_attach_roothub(struct usb_softc *sc)
235 {
236 	struct usbd_device *dev;
237 
238 	if (usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, sc->sc_speed, 0,
239 	    &sc->sc_port)) {
240 		printf("%s: root hub problem\n", sc->sc_dev.dv_xname);
241 		sc->sc_bus->dying = 1;
242 		return (1);
243 	}
244 
245 	dev = sc->sc_port.device;
246 	if (dev->hub == NULL) {
247 		printf("%s: root device is not a hub\n", sc->sc_dev.dv_xname);
248 		sc->sc_bus->dying = 1;
249 		return (1);
250 	}
251 	sc->sc_bus->root_hub = dev;
252 
253 	return (0);
254 }
255 
256 void
257 usb_detach_roothub(struct usb_softc *sc)
258 {
259 	/*
260 	 * To avoid races with the usb task thread, mark the root hub
261 	 * as disconnecting and schedule an exploration task to detach
262 	 * it.
263 	 */
264 	sc->sc_bus->flags |= USB_BUS_DISCONNECTING;
265 	usb_needs_explore(sc->sc_bus->root_hub, 0);
266 
267 	usb_wait_task(sc->sc_bus->root_hub, &sc->sc_explore_task);
268 
269 	sc->sc_bus->root_hub = NULL;
270 }
271 
272 void
273 usb_create_task_threads(void *arg)
274 {
275 	if (kthread_create(usb_abort_task_thread, NULL,
276 	    &usb_abort_task_thread_proc, "usbatsk"))
277 		panic("unable to create usb abort task thread");
278 
279 	if (kthread_create(usb_task_thread, NULL,
280 	    &usb_task_thread_proc, "usbtask"))
281 		panic("unable to create usb task thread");
282 }
283 
284 /*
285  * Add a task to be performed by the task thread.  This function can be
286  * called from any context and the task will be executed in a process
287  * context ASAP.
288  */
289 void
290 usb_add_task(struct usbd_device *dev, struct usb_task *task)
291 {
292 	int s;
293 
294 	/* Don't add task if the device's root hub is dying. */
295 	if (usbd_is_dying(dev))
296 		return;
297 
298 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
299 	    task->state, task->type));
300 
301 	s = splusb();
302 	if (!(task->state & USB_TASK_STATE_ONQ)) {
303 		switch (task->type) {
304 		case USB_TASK_TYPE_ABORT:
305 			TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next);
306 			break;
307 		case USB_TASK_TYPE_EXPLORE:
308 			TAILQ_INSERT_TAIL(&usb_explore_tasks, task, next);
309 			break;
310 		case USB_TASK_TYPE_GENERIC:
311 			TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next);
312 			break;
313 		}
314 		task->state |= USB_TASK_STATE_ONQ;
315 		task->dev = dev;
316 	}
317 	if (task->type == USB_TASK_TYPE_ABORT)
318 		wakeup(&usb_run_abort_tasks);
319 	else
320 		wakeup(&usb_run_tasks);
321 	splx(s);
322 }
323 
324 void
325 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
326 {
327 	int s;
328 
329 	if (!(task->state & USB_TASK_STATE_ONQ))
330 		return;
331 
332 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
333 	    task->state, task->type));
334 
335 	s = splusb();
336 
337 	switch (task->type) {
338 	case USB_TASK_TYPE_ABORT:
339 		TAILQ_REMOVE(&usb_abort_tasks, task, next);
340 		break;
341 	case USB_TASK_TYPE_EXPLORE:
342 		TAILQ_REMOVE(&usb_explore_tasks, task, next);
343 		break;
344 	case USB_TASK_TYPE_GENERIC:
345 		TAILQ_REMOVE(&usb_generic_tasks, task, next);
346 		break;
347 	}
348 	task->state &= ~USB_TASK_STATE_ONQ;
349 	if (task->state == USB_TASK_STATE_NONE)
350 		wakeup(task);
351 
352 	splx(s);
353 }
354 
355 void
356 usb_wait_task(struct usbd_device *dev, struct usb_task *task)
357 {
358 	int s;
359 
360 	DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
361 	    task->state, task->type));
362 
363 	if (task->state == USB_TASK_STATE_NONE)
364 		return;
365 
366 	s = splusb();
367 	while (task->state != USB_TASK_STATE_NONE) {
368 		DPRINTF(("%s: waiting for task to complete\n", __func__));
369 		tsleep(task, PWAIT, "endtask", 0);
370 	}
371 	splx(s);
372 }
373 
374 void
375 usb_rem_wait_task(struct usbd_device *dev, struct usb_task *task)
376 {
377 	usb_rem_task(dev, task);
378 	usb_wait_task(dev, task);
379 }
380 
381 void
382 usb_task_thread(void *arg)
383 {
384 	struct usb_task *task;
385 	int s;
386 
387 	DPRINTF(("usb_task_thread: start\n"));
388 
389 	s = splusb();
390 	while (usb_run_tasks) {
391 		if ((task = TAILQ_FIRST(&usb_explore_tasks)) != NULL)
392 			TAILQ_REMOVE(&usb_explore_tasks, task, next);
393 		else if ((task = TAILQ_FIRST(&usb_generic_tasks)) != NULL)
394 			TAILQ_REMOVE(&usb_generic_tasks, task, next);
395 		else {
396 			tsleep(&usb_run_tasks, PWAIT, "usbtsk", 0);
397 			continue;
398 		}
399 		/*
400 		 * Set the state run bit before clearing the onq bit.
401 		 * This avoids state == none between dequeue and
402 		 * execution, which could cause usb_wait_task() to do
403 		 * the wrong thing.
404 		 */
405 		task->state |= USB_TASK_STATE_RUN;
406 		task->state &= ~USB_TASK_STATE_ONQ;
407 		/* Don't actually execute the task if dying. */
408 		if (!usbd_is_dying(task->dev)) {
409 			splx(s);
410 			task->fun(task->arg);
411 			s = splusb();
412 		}
413 		task->state &= ~USB_TASK_STATE_RUN;
414 		if (task->state == USB_TASK_STATE_NONE)
415 			wakeup(task);
416 	}
417 	splx(s);
418 
419 	kthread_exit(0);
420 }
421 
422 /*
423  * This thread is ONLY for the HCI drivers to be able to abort xfers.
424  * Synchronous xfers sleep the task thread, so the aborts need to happen
425  * in a different thread.
426  */
427 void
428 usb_abort_task_thread(void *arg)
429 {
430 	struct usb_task *task;
431 	int s;
432 
433 	DPRINTF(("usb_xfer_abort_thread: start\n"));
434 
435 	s = splusb();
436 	while (usb_run_abort_tasks) {
437 		if ((task = TAILQ_FIRST(&usb_abort_tasks)) != NULL)
438 			TAILQ_REMOVE(&usb_abort_tasks, task, next);
439 		else {
440 			tsleep(&usb_run_abort_tasks, PWAIT, "usbatsk", 0);
441 			continue;
442 		}
443 		/*
444 		 * Set the state run bit before clearing the onq bit.
445 		 * This avoids state == none between dequeue and
446 		 * execution, which could cause usb_wait_task() to do
447 		 * the wrong thing.
448 		 */
449 		task->state |= USB_TASK_STATE_RUN;
450 		task->state &= ~USB_TASK_STATE_ONQ;
451 		/* Don't actually execute the task if dying. */
452 		if (!usbd_is_dying(task->dev)) {
453 			splx(s);
454 			task->fun(task->arg);
455 			s = splusb();
456 		}
457 		task->state &= ~USB_TASK_STATE_RUN;
458 		if (task->state == USB_TASK_STATE_NONE)
459 			wakeup(task);
460 	}
461 	splx(s);
462 
463 	kthread_exit(0);
464 }
465 
466 int
467 usbctlprint(void *aux, const char *pnp)
468 {
469 	/* only "usb"es can attach to host controllers */
470 	if (pnp)
471 		printf("usb at %s", pnp);
472 
473 	return (UNCONF);
474 }
475 
476 int
477 usbopen(dev_t dev, int flag, int mode, struct proc *p)
478 {
479 	int unit = minor(dev);
480 	struct usb_softc *sc;
481 
482 	if (unit >= usb_cd.cd_ndevs)
483 		return (ENXIO);
484 	sc = usb_cd.cd_devs[unit];
485 	if (sc == NULL)
486 		return (ENXIO);
487 
488 	if (sc->sc_bus->dying)
489 		return (EIO);
490 
491 	return (0);
492 }
493 
494 int
495 usbclose(dev_t dev, int flag, int mode, struct proc *p)
496 {
497 	return (0);
498 }
499 
500 void
501 usb_fill_di_task(void *arg)
502 {
503 	struct usb_device_info *di = (struct usb_device_info *)arg;
504 	struct usb_softc *sc;
505 	struct usbd_device *dev;
506 
507 	/* check that the bus and device are still present */
508 	if (di->udi_bus >= usb_cd.cd_ndevs)
509 		return;
510 	sc = usb_cd.cd_devs[di->udi_bus];
511 	if (sc == NULL)
512 		return;
513 	dev = sc->sc_bus->devices[di->udi_addr];
514 	if (dev == NULL)
515 		return;
516 
517 	usbd_fill_deviceinfo(dev, di, 1);
518 }
519 
520 void
521 usb_fill_udc_task(void *arg)
522 {
523 	struct usb_device_cdesc *udc = (struct usb_device_cdesc *)arg;
524 	struct usb_softc *sc;
525 	struct usbd_device *dev;
526 	int addr = udc->udc_addr;
527 	usb_config_descriptor_t *cdesc;
528 
529 	/* check that the bus and device are still present */
530 	if (udc->udc_bus >= usb_cd.cd_ndevs)
531 		return;
532 	sc = usb_cd.cd_devs[udc->udc_bus];
533 	if (sc == NULL)
534 		return;
535 	dev = sc->sc_bus->devices[udc->udc_addr];
536 	if (dev == NULL)
537 		return;
538 
539 	cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr],
540 	    udc->udc_config_index, 0);
541 	if (cdesc == NULL)
542 		return;
543 	udc->udc_desc = *cdesc;
544 	free(cdesc, M_TEMP, 0);
545 }
546 
547 void
548 usb_fill_udf_task(void *arg)
549 {
550 	struct usb_device_fdesc *udf = (struct usb_device_fdesc *)arg;
551 	struct usb_softc *sc;
552 	struct usbd_device *dev;
553 	int addr = udf->udf_addr;
554 	usb_config_descriptor_t *cdesc;
555 
556 	/* check that the bus and device are still present */
557 	if (udf->udf_bus >= usb_cd.cd_ndevs)
558 		return;
559 	sc = usb_cd.cd_devs[udf->udf_bus];
560 	if (sc == NULL)
561 		return;
562 	dev = sc->sc_bus->devices[udf->udf_addr];
563 	if (dev == NULL)
564 		return;
565 
566 	cdesc = usbd_get_cdesc(sc->sc_bus->devices[addr],
567 	    udf->udf_config_index, &udf->udf_size);
568 	udf->udf_data = (char *)cdesc;
569 }
570 
571 int
572 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
573 {
574 	struct usb_softc *sc;
575 	int unit = minor(devt);
576 	int error;
577 
578 	sc = usb_cd.cd_devs[unit];
579 
580 	if (sc->sc_bus->dying)
581 		return (EIO);
582 
583 	error = 0;
584 	switch (cmd) {
585 #ifdef USB_DEBUG
586 	case USB_SETDEBUG:
587 		/* only root can access to these debug flags */
588 		if ((error = suser(curproc, 0)) != 0)
589 			return (error);
590 		if (!(flag & FWRITE))
591 			return (EBADF);
592 		usbdebug  = ((*(unsigned int *)data) & 0x000000ff);
593 #if defined(UHCI_DEBUG) && NUHCI > 0
594 		uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8;
595 #endif
596 #if defined(OHCI_DEBUG) && NOHCI > 0
597 		ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16;
598 #endif
599 #if defined(EHCI_DEBUG) && NEHCI > 0
600 		ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24;
601 #endif
602 		break;
603 #endif /* USB_DEBUG */
604 	case USB_REQUEST:
605 	{
606 		struct usb_ctl_request *ur = (void *)data;
607 		int len = UGETW(ur->ucr_request.wLength);
608 		struct iovec iov;
609 		struct uio uio;
610 		void *ptr = 0;
611 		int addr = ur->ucr_addr;
612 		usbd_status err;
613 		int error = 0;
614 
615 		if (!(flag & FWRITE))
616 			return (EBADF);
617 
618 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
619 		if (len < 0 || len > 32768)
620 			return (EINVAL);
621 		if (addr < 0 || addr >= USB_MAX_DEVICES)
622 			return (EINVAL);
623 		if (sc->sc_bus->devices[addr] == NULL)
624 			return (ENXIO);
625 		if (len != 0) {
626 			iov.iov_base = (caddr_t)ur->ucr_data;
627 			iov.iov_len = len;
628 			uio.uio_iov = &iov;
629 			uio.uio_iovcnt = 1;
630 			uio.uio_resid = len;
631 			uio.uio_offset = 0;
632 			uio.uio_segflg = UIO_USERSPACE;
633 			uio.uio_rw =
634 				ur->ucr_request.bmRequestType & UT_READ ?
635 				UIO_READ : UIO_WRITE;
636 			uio.uio_procp = p;
637 			ptr = malloc(len, M_TEMP, M_WAITOK);
638 			if (uio.uio_rw == UIO_WRITE) {
639 				error = uiomove(ptr, len, &uio);
640 				if (error)
641 					goto ret;
642 			}
643 		}
644 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
645 			  &ur->ucr_request, ptr, ur->ucr_flags,
646 			  &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT);
647 		if (err) {
648 			error = EIO;
649 			goto ret;
650 		}
651 		/* Only if USBD_SHORT_XFER_OK is set. */
652 		if (len > ur->ucr_actlen)
653 			len = ur->ucr_actlen;
654 		if (len != 0) {
655 			if (uio.uio_rw == UIO_READ) {
656 				error = uiomove(ptr, len, &uio);
657 				if (error)
658 					goto ret;
659 			}
660 		}
661 	ret:
662 		if (ptr)
663 			free(ptr, M_TEMP, 0);
664 		return (error);
665 	}
666 
667 	case USB_DEVICEINFO:
668 	{
669 		struct usb_device_info *di = (void *)data;
670 		int addr = di->udi_addr;
671 		struct usb_task di_task;
672 		struct usbd_device *dev;
673 
674 		if (addr < 1 || addr >= USB_MAX_DEVICES)
675 			return (EINVAL);
676 
677 		dev = sc->sc_bus->devices[addr];
678 		if (dev == NULL)
679 			return (ENXIO);
680 
681 		di->udi_bus = unit;
682 
683 		/* All devices get a driver, thanks to ugen(4).  If the
684 		 * task ends without adding a driver name, there was an error.
685 		 */
686 		di->udi_devnames[0][0] = '\0';
687 
688 		usb_init_task(&di_task, usb_fill_di_task, di,
689 		    USB_TASK_TYPE_GENERIC);
690 		usb_add_task(sc->sc_bus->root_hub, &di_task);
691 		usb_wait_task(sc->sc_bus->root_hub, &di_task);
692 
693 		if (di->udi_devnames[0][0] == '\0')
694 			return (ENXIO);
695 
696 		break;
697 	}
698 
699 	case USB_DEVICESTATS:
700 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
701 		break;
702 
703 	case USB_DEVICE_GET_DDESC:
704 	{
705 		struct usb_device_ddesc *udd = (struct usb_device_ddesc *)data;
706 		int addr = udd->udd_addr;
707 		struct usbd_device *dev;
708 
709 		if (addr < 1 || addr >= USB_MAX_DEVICES)
710 			return (EINVAL);
711 
712 		dev = sc->sc_bus->devices[addr];
713 		if (dev == NULL)
714 			return (ENXIO);
715 
716 		udd->udd_bus = unit;
717 
718 		udd->udd_desc = *usbd_get_device_descriptor(dev);
719 		break;
720 	}
721 
722 	case USB_DEVICE_GET_CDESC:
723 	{
724 		struct usb_device_cdesc *udc = (struct usb_device_cdesc *)data;
725 		int addr = udc->udc_addr;
726 		struct usb_task udc_task;
727 
728 		if (addr < 1 || addr >= USB_MAX_DEVICES)
729 			return (EINVAL);
730 		if (sc->sc_bus->devices[addr] == NULL)
731 			return (ENXIO);
732 
733 		udc->udc_bus = unit;
734 
735 		udc->udc_desc.bLength = 0;
736 		usb_init_task(&udc_task, usb_fill_udc_task, udc,
737 		    USB_TASK_TYPE_GENERIC);
738 		usb_add_task(sc->sc_bus->root_hub, &udc_task);
739 		usb_wait_task(sc->sc_bus->root_hub, &udc_task);
740 		if (udc->udc_desc.bLength == 0)
741 			return (EINVAL);
742 		break;
743 	}
744 
745 	case USB_DEVICE_GET_FDESC:
746 	{
747 		struct usb_device_fdesc *udf = (struct usb_device_fdesc *)data;
748 		int addr = udf->udf_addr;
749 		struct usb_task udf_task;
750 		struct usb_device_fdesc save_udf;
751 		usb_config_descriptor_t *cdesc;
752 		struct iovec iov;
753 		struct uio uio;
754 		int len, error;
755 
756 		if (addr < 1 || addr >= USB_MAX_DEVICES)
757 			return (EINVAL);
758 		if (sc->sc_bus->devices[addr] == NULL)
759 			return (ENXIO);
760 
761 		udf->udf_bus = unit;
762 
763 		save_udf = *udf;
764 		usb_init_task(&udf_task, usb_fill_udf_task, udf,
765 		    USB_TASK_TYPE_GENERIC);
766 		usb_add_task(sc->sc_bus->root_hub, &udf_task);
767 		usb_wait_task(sc->sc_bus->root_hub, &udf_task);
768 		len = udf->udf_size;
769 		cdesc = (usb_config_descriptor_t *)udf->udf_data;
770 		*udf = save_udf;
771 		if (cdesc == NULL)
772 			return (EINVAL);
773 		if (len > udf->udf_size)
774 			len = udf->udf_size;
775 		iov.iov_base = (caddr_t)udf->udf_data;
776 		iov.iov_len = len;
777 		uio.uio_iov = &iov;
778 		uio.uio_iovcnt = 1;
779 		uio.uio_resid = len;
780 		uio.uio_offset = 0;
781 		uio.uio_segflg = UIO_USERSPACE;
782 		uio.uio_rw = UIO_READ;
783 		uio.uio_procp = p;
784 		error = uiomove((void *)cdesc, len, &uio);
785 		free(cdesc, M_TEMP, 0);
786 		return (error);
787 	}
788 
789 	default:
790 		return (EINVAL);
791 	}
792 	return (0);
793 }
794 
795 /*
796  * Explore device tree from the root.  We need mutual exclusion to this
797  * hub while traversing the device tree, but this is guaranteed since this
798  * function is only called from the task thread, with one exception:
799  * usb_attach() calls this function, but there shouldn't be anything else
800  * trying to explore this hub at that time.
801  */
802 void
803 usb_explore(void *v)
804 {
805 	struct usb_softc *sc = v;
806 	struct timeval now, waited;
807 	int pwrdly, waited_ms;
808 
809 	DPRINTFN(2,("%s: %s\n", __func__, sc->sc_dev.dv_xname));
810 #ifdef USB_DEBUG
811 	if (usb_noexplore)
812 		return;
813 #endif
814 
815 	if (sc->sc_bus->dying)
816 		return;
817 
818 	if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) {
819 		/*
820 		 * If this is a low/full speed hub and there is a high
821 		 * speed hub that hasn't explored yet, reshedule this
822 		 * task, allowing the high speed explore task to run.
823 		 */
824 		if (sc->sc_bus->usbrev < USBREV_2_0 && explore_pending > 0) {
825 			usb_add_task(sc->sc_bus->root_hub,
826 			    &sc->sc_explore_task);
827 			return;
828 		}
829 
830 		/*
831 		 * Wait for power to stabilize.
832 		 */
833 		getmicrouptime(&now);
834 		timersub(&now, &sc->sc_ptime, &waited);
835 		waited_ms = waited.tv_sec * 1000 + waited.tv_usec / 1000;
836 
837 		pwrdly = sc->sc_bus->root_hub->hub->powerdelay +
838 		    USB_EXTRA_POWER_UP_TIME;
839 		if (pwrdly > waited_ms)
840 			usb_delay_ms(sc->sc_bus, pwrdly - waited_ms);
841 	}
842 
843 	if (sc->sc_bus->flags & USB_BUS_DISCONNECTING) {
844 		/* Prevent new tasks from being scheduled. */
845 		sc->sc_bus->dying = 1;
846 
847 		/* Make all devices disconnect. */
848 		if (sc->sc_port.device != NULL) {
849 			usbd_detach(sc->sc_port.device, (struct device *)sc);
850 			sc->sc_port.device = NULL;
851 		}
852 
853 		sc->sc_bus->flags &= ~USB_BUS_DISCONNECTING;
854 	} else {
855 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
856 	}
857 
858 	if (sc->sc_bus->flags & USB_BUS_CONFIG_PENDING) {
859 		DPRINTF(("%s: %s: first explore done\n", __func__,
860 		    sc->sc_dev.dv_xname));
861 		if (sc->sc_bus->usbrev == USBREV_2_0 && explore_pending)
862 			explore_pending--;
863 		config_pending_decr();
864 		sc->sc_bus->flags &= ~(USB_BUS_CONFIG_PENDING);
865 	}
866 }
867 
868 void
869 usb_needs_explore(struct usbd_device *dev, int first_explore)
870 {
871 	struct usb_softc *usbctl = (struct usb_softc *)dev->bus->usbctl;
872 
873 	DPRINTFN(3,("%s: %s\n", usbctl->sc_dev.dv_xname, __func__));
874 
875 	if (!first_explore && (dev->bus->flags & USB_BUS_CONFIG_PENDING)) {
876 		DPRINTF(("%s: %s: not exploring before first explore\n",
877 		    __func__, usbctl->sc_dev.dv_xname));
878 		return;
879 	}
880 
881 	usb_add_task(dev, &usbctl->sc_explore_task);
882 }
883 
884 void
885 usb_needs_reattach(struct usbd_device *dev)
886 {
887 	DPRINTFN(2,("usb_needs_reattach\n"));
888 	dev->powersrc->reattach = 1;
889 	usb_needs_explore(dev, 0);
890 }
891 
892 void
893 usb_schedsoftintr(struct usbd_bus *bus)
894 {
895 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
896 
897 	if (bus->use_polling) {
898 		bus->methods->soft_intr(bus);
899 	} else {
900 		softintr_schedule(bus->soft);
901 	}
902 }
903 
904 int
905 usb_activate(struct device *self, int act)
906 {
907 	struct usb_softc *sc = (struct usb_softc *)self;
908 	int rv = 0;
909 
910 	switch (act) {
911 	case DVACT_QUIESCE:
912 		if (sc->sc_bus->root_hub != NULL)
913 			usb_detach_roothub(sc);
914 		break;
915 	case DVACT_RESUME:
916 		sc->sc_bus->dying = 0;
917 
918 		/*
919 		 * Make sure the root hub is present before interrupts
920 		 * get enabled.   As long as the bus is in polling mode
921 		 * it is safe to call usbd_new_device() now since root
922 		 * hub transfers do not need to sleep.
923 		 */
924 		sc->sc_bus->use_polling++;
925 		if (!usb_attach_roothub(sc))
926 			usb_needs_explore(sc->sc_bus->root_hub, 0);
927 		sc->sc_bus->use_polling--;
928 		break;
929 	default:
930 		rv = config_activate_children(self, act);
931 		break;
932 	}
933 	return (rv);
934 }
935 
936 int
937 usb_detach(struct device *self, int flags)
938 {
939 	struct usb_softc *sc = (struct usb_softc *)self;
940 
941 	if (sc->sc_bus->root_hub != NULL) {
942 		usb_detach_roothub(sc);
943 
944 		if (--usb_nbuses == 0) {
945 			usb_run_tasks = usb_run_abort_tasks = 0;
946 			wakeup(&usb_run_abort_tasks);
947 			wakeup(&usb_run_tasks);
948 		}
949 	}
950 
951 	if (sc->sc_bus->soft != NULL) {
952 		softintr_disestablish(sc->sc_bus->soft);
953 		sc->sc_bus->soft = NULL;
954 	}
955 
956 	return (0);
957 }
958