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