xref: /openbsd-src/sys/dev/usb/usb.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: usb.c,v 1.62 2009/11/09 17:53:39 nicm 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/kthread.h>
50 #include <sys/proc.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/poll.h>
54 #include <sys/selinfo.h>
55 #include <sys/vnode.h>
56 #include <sys/signalvar.h>
57 
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 
62 #define USB_DEV_MINOR 255
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  * 1  - do not use timeout exploration
85  * >1 - do no exploration
86  */
87 int	usb_noexplore = 0;
88 #else
89 #define DPRINTF(x)
90 #define DPRINTFN(n,x)
91 #endif
92 
93 struct usb_softc {
94 	struct device	 sc_dev;	/* base device */
95 	usbd_bus_handle  sc_bus;	/* USB controller */
96 	struct usbd_port sc_port;	/* dummy port for root hub */
97 
98 	struct proc	*sc_event_thread;
99 
100 	char		 sc_dying;
101 };
102 
103 TAILQ_HEAD(, usb_task) usb_all_tasks;
104 
105 volatile int threads_pending = 0;
106 
107 void	usb_discover(void *);
108 void	usb_create_event_thread(void *);
109 void	usb_event_thread(void *);
110 void	usb_task_thread(void *);
111 struct proc *usb_task_thread_proc = NULL;
112 
113 #define USB_MAX_EVENTS 100
114 struct usb_event_q {
115 	struct usb_event ue;
116 	SIMPLEQ_ENTRY(usb_event_q) next;
117 };
118 SIMPLEQ_HEAD(, usb_event_q) usb_events =
119 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
120 int usb_nevents = 0;
121 struct selinfo usb_selevent;
122 struct proc *usb_async_proc;  /* process that wants USB SIGIO */
123 int usb_dev_open = 0;
124 void usb_add_event(int, struct usb_event *);
125 
126 int usb_get_next_event(struct usb_event *);
127 
128 const char *usbrev_str[] = USBREV_STR;
129 
130 int usb_match(struct device *, void *, void *);
131 void usb_attach(struct device *, struct device *, void *);
132 int usb_detach(struct device *, int);
133 int usb_activate(struct device *, int);
134 
135 struct cfdriver usb_cd = {
136 	NULL, "usb", DV_DULL
137 };
138 
139 const struct cfattach usb_ca = {
140 	sizeof(struct usb_softc),
141 	usb_match,
142 	usb_attach,
143 	usb_detach,
144 	usb_activate,
145 };
146 
147 int
148 usb_match(struct device *parent, void *match, void *aux)
149 {
150 	DPRINTF(("usbd_match\n"));
151 	return (UMATCH_GENERIC);
152 }
153 
154 void
155 usb_attach(struct device *parent, struct device *self, void *aux)
156 {
157 	struct usb_softc *sc = (struct usb_softc *)self;
158 	usbd_device_handle dev;
159 	usbd_status err;
160 	int usbrev;
161 	int speed;
162 	struct usb_event ue;
163 
164 	DPRINTF(("usbd_attach\n"));
165 
166 	usbd_init();
167 	sc->sc_bus = aux;
168 	sc->sc_bus->usbctl = sc;
169 	sc->sc_port.power = USB_MAX_POWER;
170 
171 	usbrev = sc->sc_bus->usbrev;
172 	printf(": USB revision %s", usbrev_str[usbrev]);
173 	switch (usbrev) {
174 	case USBREV_1_0:
175 	case USBREV_1_1:
176 		speed = USB_SPEED_FULL;
177 		break;
178 	case USBREV_2_0:
179 		speed = USB_SPEED_HIGH;
180 		break;
181 	default:
182 		printf(", not supported\n");
183 		sc->sc_dying = 1;
184 		return;
185 	}
186 	printf("\n");
187 
188 	/* Make sure not to use tsleep() if we are cold booting. */
189 	if (cold)
190 		sc->sc_bus->use_polling++;
191 
192 	ue.u.ue_ctrlr.ue_bus = sc->sc_dev.dv_unit;
193 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
194 
195 	/* XXX we should have our own level */
196 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
197 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
198 	if (sc->sc_bus->soft == NULL) {
199 		printf("%s: can't register softintr\n", sc->sc_dev.dv_xname);
200 		sc->sc_dying = 1;
201 		return;
202 	}
203 
204 	err = usbd_new_device(&sc->sc_dev, sc->sc_bus, 0, speed, 0,
205 		  &sc->sc_port);
206 	if (!err) {
207 		dev = sc->sc_port.device;
208 		if (dev->hub == NULL) {
209 			sc->sc_dying = 1;
210 			printf("%s: root device is not a hub\n",
211 			       sc->sc_dev.dv_xname);
212 			return;
213 		}
214 		sc->sc_bus->root_hub = dev;
215 #if 1
216 		/*
217 		 * Turning this code off will delay attachment of USB devices
218 		 * until the USB event thread is running, which means that
219 		 * the keyboard will not work until after cold boot.
220 		 */
221 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
222 			dev->hub->explore(sc->sc_bus->root_hub);
223 #endif
224 	} else {
225 		printf("%s: root hub problem, error=%d\n",
226 		       sc->sc_dev.dv_xname, err);
227 		sc->sc_dying = 1;
228 	}
229 	if (cold)
230 		sc->sc_bus->use_polling--;
231 
232 	config_pending_incr();
233 	kthread_create_deferred(usb_create_event_thread, sc);
234 }
235 
236 void
237 usb_create_event_thread(void *arg)
238 {
239 	struct usb_softc *sc = arg;
240 	static int created = 0;
241 
242 	if (sc->sc_bus->usbrev == USBREV_2_0)
243 		threads_pending++;
244 
245 	if (kthread_create(usb_event_thread, sc, &sc->sc_event_thread,
246 	    "%s", sc->sc_dev.dv_xname))
247 		panic("unable to create event thread for %s",
248 		    sc->sc_dev.dv_xname);
249 
250 	if (!created) {
251 		created = 1;
252 		TAILQ_INIT(&usb_all_tasks);
253 		if (kthread_create(usb_task_thread, NULL,
254 		    &usb_task_thread_proc, "usbtask"))
255 			panic("unable to create usb task thread");
256 	}
257 }
258 
259 /*
260  * Add a task to be performed by the task thread.  This function can be
261  * called from any context and the task will be executed in a process
262  * context ASAP.
263  */
264 void
265 usb_add_task(usbd_device_handle dev, struct usb_task *task)
266 {
267 	int s;
268 
269 	s = splusb();
270 	if (!task->onqueue) {
271 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
272 		TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
273 		task->onqueue = 1;
274 	} else {
275 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
276 	}
277 	wakeup(&usb_all_tasks);
278 	splx(s);
279 }
280 
281 void
282 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
283 {
284 	int s;
285 
286 	s = splusb();
287 	if (task->onqueue) {
288 		TAILQ_REMOVE(&usb_all_tasks, task, next);
289 		task->onqueue = 0;
290 	}
291 	splx(s);
292 }
293 
294 void
295 usb_event_thread(void *arg)
296 {
297 	struct usb_softc *sc = arg;
298 	int pwrdly;
299 
300 	DPRINTF(("usb_event_thread: start\n"));
301 
302 	/* Wait for power to come good. */
303 	pwrdly = sc->sc_bus->root_hub->hub->hubdesc.bPwrOn2PwrGood *
304 	    UHD_PWRON_FACTOR + USB_EXTRA_POWER_UP_TIME;
305 	usb_delay_ms(sc->sc_bus, pwrdly);
306 
307 	/* USB1 threads wait for USB2 threads to finish their first probe. */
308 	while (sc->sc_bus->usbrev != USBREV_2_0 && threads_pending)
309 		(void)tsleep((void *)&threads_pending, PWAIT, "config", 0);
310 
311 	/* Make sure first discover does something. */
312 	sc->sc_bus->needs_explore = 1;
313 	usb_discover(sc);
314 	config_pending_decr();
315 
316 	/* Wake up any companions waiting for handover before their probes. */
317 	if (sc->sc_bus->usbrev == USBREV_2_0) {
318 		threads_pending--;
319 		wakeup((void *)&threads_pending);
320 	}
321 
322 	while (!sc->sc_dying) {
323 #ifdef USB_DEBUG
324 		if (usb_noexplore < 2)
325 #endif
326 		usb_discover(sc);
327 #ifdef USB_DEBUG
328 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
329 		    usb_noexplore ? 0 : hz * 60);
330 #else
331 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
332 		    hz * 60);
333 #endif
334 		DPRINTFN(2,("usb_event_thread: woke up\n"));
335 	}
336 	sc->sc_event_thread = NULL;
337 
338 	/* In case parent is waiting for us to exit. */
339 	wakeup(sc);
340 
341 	DPRINTF(("usb_event_thread: exit\n"));
342 	kthread_exit(0);
343 }
344 
345 void
346 usb_task_thread(void *arg)
347 {
348 	struct usb_task *task;
349 	int s;
350 
351 	DPRINTF(("usb_task_thread: start\n"));
352 
353 	s = splusb();
354 	for (;;) {
355 		task = TAILQ_FIRST(&usb_all_tasks);
356 		if (task == NULL) {
357 			tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
358 			task = TAILQ_FIRST(&usb_all_tasks);
359 		}
360 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
361 		if (task != NULL) {
362 			TAILQ_REMOVE(&usb_all_tasks, task, next);
363 			task->onqueue = 0;
364 			splx(s);
365 			task->fun(task->arg);
366 			s = splusb();
367 		}
368 	}
369 }
370 
371 int
372 usbctlprint(void *aux, const char *pnp)
373 {
374 	/* only "usb"es can attach to host controllers */
375 	if (pnp)
376 		printf("usb at %s", pnp);
377 
378 	return (UNCONF);
379 }
380 
381 int
382 usbopen(dev_t dev, int flag, int mode, struct proc *p)
383 {
384 	int unit = minor(dev);
385 	struct usb_softc *sc;
386 
387 	if (unit == USB_DEV_MINOR) {
388 		if (usb_dev_open)
389 			return (EBUSY);
390 		usb_dev_open = 1;
391 		usb_async_proc = 0;
392 		return (0);
393 	}
394 
395 	if (unit >= usb_cd.cd_ndevs)
396 		return (ENXIO);
397 	sc = usb_cd.cd_devs[unit];
398 	if (sc == NULL)
399 		return (ENXIO);
400 
401 	if (sc->sc_dying)
402 		return (EIO);
403 
404 	return (0);
405 }
406 
407 int
408 usbread(dev_t dev, struct uio *uio, int flag)
409 {
410 	struct usb_event ue;
411 	int s, error, n;
412 
413 	if (minor(dev) != USB_DEV_MINOR)
414 		return (ENXIO);
415 
416 	if (uio->uio_resid != sizeof(struct usb_event))
417 		return (EINVAL);
418 
419 	error = 0;
420 	s = splusb();
421 	for (;;) {
422 		n = usb_get_next_event(&ue);
423 		if (n != 0)
424 			break;
425 		if (flag & IO_NDELAY) {
426 			error = EWOULDBLOCK;
427 			break;
428 		}
429 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
430 		if (error)
431 			break;
432 	}
433 	splx(s);
434 	if (!error)
435 		error = uiomove((void *)&ue, uio->uio_resid, uio);
436 
437 	return (error);
438 }
439 
440 int
441 usbclose(dev_t dev, int flag, int mode, struct proc *p)
442 {
443 	int unit = minor(dev);
444 
445 	if (unit == USB_DEV_MINOR) {
446 		usb_async_proc = 0;
447 		usb_dev_open = 0;
448 	}
449 
450 	return (0);
451 }
452 
453 int
454 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
455 {
456 	struct usb_softc *sc;
457 	int unit = minor(devt);
458 	int error;
459 
460 	if (unit == USB_DEV_MINOR) {
461 		switch (cmd) {
462 		case FIONBIO:
463 			/* All handled in the upper FS layer. */
464 			return (0);
465 
466 		case FIOASYNC:
467 			if (*(int *)data)
468 				usb_async_proc = p;
469 			else
470 				usb_async_proc = 0;
471 			return (0);
472 
473 		default:
474 			return (EINVAL);
475 		}
476 	}
477 
478 	sc = usb_cd.cd_devs[unit];
479 
480 	if (sc->sc_dying)
481 		return (EIO);
482 
483 	error = 0;
484 	switch (cmd) {
485 #ifdef USB_DEBUG
486 	case USB_SETDEBUG:
487 		/* only root can access to these debug flags */
488 		if ((error = suser(curproc, 0)) != 0)
489 			return (error);
490 		if (!(flag & FWRITE))
491 			return (EBADF);
492 		usbdebug  = ((*(unsigned int *)data) & 0x000000ff);
493 #if defined(UHCI_DEBUG) && NUHCI > 0
494 		uhcidebug = ((*(unsigned int *)data) & 0x0000ff00) >> 8;
495 #endif
496 #if defined(OHCI_DEBUG) && NOHCI > 0
497 		ohcidebug = ((*(unsigned int *)data) & 0x00ff0000) >> 16;
498 #endif
499 #if defined(EHCI_DEBUG) && NEHCI > 0
500 		ehcidebug = ((*(unsigned int *)data) & 0xff000000) >> 24;
501 #endif
502 		break;
503 #endif /* USB_DEBUG */
504 	case USB_REQUEST:
505 	{
506 		struct usb_ctl_request *ur = (void *)data;
507 		int len = UGETW(ur->ucr_request.wLength);
508 		struct iovec iov;
509 		struct uio uio;
510 		void *ptr = 0;
511 		int addr = ur->ucr_addr;
512 		usbd_status err;
513 		int error = 0;
514 
515 		if (!(flag & FWRITE))
516 			return (EBADF);
517 
518 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
519 		if (len < 0 || len > 32768)
520 			return (EINVAL);
521 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
522 		    sc->sc_bus->devices[addr] == 0)
523 			return (EINVAL);
524 		if (len != 0) {
525 			iov.iov_base = (caddr_t)ur->ucr_data;
526 			iov.iov_len = len;
527 			uio.uio_iov = &iov;
528 			uio.uio_iovcnt = 1;
529 			uio.uio_resid = len;
530 			uio.uio_offset = 0;
531 			uio.uio_segflg = UIO_USERSPACE;
532 			uio.uio_rw =
533 				ur->ucr_request.bmRequestType & UT_READ ?
534 				UIO_READ : UIO_WRITE;
535 			uio.uio_procp = p;
536 			ptr = malloc(len, M_TEMP, M_WAITOK);
537 			if (uio.uio_rw == UIO_WRITE) {
538 				error = uiomove(ptr, len, &uio);
539 				if (error)
540 					goto ret;
541 			}
542 		}
543 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
544 			  &ur->ucr_request, ptr, ur->ucr_flags,
545 			  &ur->ucr_actlen, USBD_DEFAULT_TIMEOUT);
546 		if (err) {
547 			error = EIO;
548 			goto ret;
549 		}
550 		if (len != 0) {
551 			if (uio.uio_rw == UIO_READ) {
552 				error = uiomove(ptr, len, &uio);
553 				if (error)
554 					goto ret;
555 			}
556 		}
557 	ret:
558 		if (ptr)
559 			free(ptr, M_TEMP);
560 		return (error);
561 	}
562 
563 	case USB_DEVICEINFO:
564 	{
565 		struct usb_device_info *di = (void *)data;
566 		int addr = di->udi_addr;
567 		usbd_device_handle dev;
568 
569 		if (addr < 1 || addr >= USB_MAX_DEVICES)
570 			return (EINVAL);
571 		dev = sc->sc_bus->devices[addr];
572 		if (dev == NULL)
573 			return (ENXIO);
574 		usbd_fill_deviceinfo(dev, di, 1);
575 		break;
576 	}
577 
578 	case USB_DEVICESTATS:
579 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
580 		break;
581 
582 	default:
583 		return (EINVAL);
584 	}
585 	return (0);
586 }
587 
588 int
589 usbpoll(dev_t dev, int events, struct proc *p)
590 {
591 	int revents, mask, s;
592 
593 	if (minor(dev) == USB_DEV_MINOR) {
594 		revents = 0;
595 		mask = POLLIN | POLLRDNORM;
596 
597 		s = splusb();
598 		if (events & mask && usb_nevents > 0)
599 			revents |= events & mask;
600 		if (revents == 0 && events & mask)
601 			selrecord(p, &usb_selevent);
602 		splx(s);
603 
604 		return (revents);
605 	} else {
606 		return (POLLERR);
607 	}
608 }
609 
610 void filt_usbrdetach(struct knote *);
611 int filt_usbread(struct knote *, long);
612 int usbkqfilter(dev_t, struct knote *);
613 
614 void
615 filt_usbrdetach(struct knote *kn)
616 {
617 	int s;
618 
619 	s = splusb();
620 	SLIST_REMOVE(&usb_selevent.si_note, kn, knote, kn_selnext);
621 	splx(s);
622 }
623 
624 int
625 filt_usbread(struct knote *kn, long hint)
626 {
627 
628 	if (usb_nevents == 0)
629 		return (0);
630 
631 	kn->kn_data = sizeof(struct usb_event);
632 	return (1);
633 }
634 
635 struct filterops usbread_filtops =
636 	{ 1, NULL, filt_usbrdetach, filt_usbread };
637 
638 int
639 usbkqfilter(dev_t dev, struct knote *kn)
640 {
641 	struct klist *klist;
642 	int s;
643 
644 	switch (kn->kn_filter) {
645 	case EVFILT_READ:
646 		if (minor(dev) != USB_DEV_MINOR)
647 			return (1);
648 		klist = &usb_selevent.si_note;
649 		kn->kn_fop = &usbread_filtops;
650 		break;
651 
652 	default:
653 		return (1);
654 	}
655 
656 	kn->kn_hook = NULL;
657 
658 	s = splusb();
659 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
660 	splx(s);
661 
662 	return (0);
663 }
664 
665 /* Explore device tree from the root. */
666 void
667 usb_discover(void *v)
668 {
669 	struct usb_softc *sc = v;
670 
671 	DPRINTFN(2,("usb_discover\n"));
672 #ifdef USB_DEBUG
673 	if (usb_noexplore > 1)
674 		return;
675 #endif
676 	/*
677 	 * We need mutual exclusion while traversing the device tree,
678 	 * but this is guaranteed since this function is only called
679 	 * from the event thread for the controller.
680 	 */
681 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
682 		sc->sc_bus->needs_explore = 0;
683 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
684 	}
685 }
686 
687 void
688 usb_needs_explore(usbd_device_handle dev)
689 {
690 	DPRINTFN(2,("usb_needs_explore\n"));
691 	dev->bus->needs_explore = 1;
692 	wakeup(&dev->bus->needs_explore);
693 }
694 
695 void
696 usb_needs_reattach(usbd_device_handle dev)
697 {
698 	DPRINTFN(2,("usb_needs_reattach\n"));
699 	dev->powersrc->reattach = 1;
700 	dev->bus->needs_explore = 1;
701 	wakeup(&dev->bus->needs_explore);
702 }
703 
704 /* Called at splusb() */
705 int
706 usb_get_next_event(struct usb_event *ue)
707 {
708 	struct usb_event_q *ueq;
709 
710 	if (usb_nevents <= 0)
711 		return (0);
712 	ueq = SIMPLEQ_FIRST(&usb_events);
713 #ifdef DIAGNOSTIC
714 	if (ueq == NULL) {
715 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
716 		usb_nevents = 0;
717 		return (0);
718 	}
719 #endif
720 	*ue = ueq->ue;
721 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
722 	free(ueq, M_USBDEV);
723 	usb_nevents--;
724 	return (1);
725 }
726 
727 void
728 usbd_add_dev_event(int type, usbd_device_handle udev)
729 {
730 	struct usb_event ue;
731 
732 	usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
733 	usb_add_event(type, &ue);
734 }
735 
736 void
737 usbd_add_drv_event(int type, usbd_device_handle udev, struct device *dev)
738 {
739 	struct usb_event ue;
740 
741 	ue.u.ue_driver.ue_cookie = udev->cookie;
742 	strncpy(ue.u.ue_driver.ue_devname, dev->dv_xname,
743 	    sizeof ue.u.ue_driver.ue_devname);
744 	usb_add_event(type, &ue);
745 }
746 
747 void
748 usb_add_event(int type, struct usb_event *uep)
749 {
750 	struct usb_event_q *ueq;
751 	struct usb_event ue;
752 	struct timespec thetime;
753 	int s;
754 
755 	nanotime(&thetime);
756 	/* Don't want to wait here inside splusb() */
757 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
758 	ueq->ue = *uep;
759 	ueq->ue.ue_type = type;
760 	ueq->ue.ue_time = thetime;
761 
762 	s = splusb();
763 	if (++usb_nevents >= USB_MAX_EVENTS) {
764 		/* Too many queued events, drop an old one. */
765 		DPRINTFN(-1,("usb: event dropped\n"));
766 		(void)usb_get_next_event(&ue);
767 	}
768 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
769 	wakeup(&usb_events);
770 	selwakeup(&usb_selevent);
771 	if (usb_async_proc != NULL)
772 		psignal(usb_async_proc, SIGIO);
773 	splx(s);
774 }
775 
776 void
777 usb_schedsoftintr(usbd_bus_handle bus)
778 {
779 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
780 
781 	if (bus->use_polling) {
782 		bus->methods->soft_intr(bus);
783 	} else {
784 		softintr_schedule(bus->soft);
785 	}
786 }
787 
788 int
789 usb_activate(struct device *self, int act)
790 {
791 	struct usb_softc *sc = (struct usb_softc *)self;
792 	usbd_device_handle dev = sc->sc_port.device;
793 	int i, rv = 0;
794 
795 	switch (act) {
796 	case DVACT_ACTIVATE:
797 		break;
798 
799 	case DVACT_DEACTIVATE:
800 		sc->sc_dying = 1;
801 		if (dev != NULL && dev->cdesc != NULL &&
802 		    dev->subdevs != NULL) {
803 			for (i = 0; dev->subdevs[i]; i++)
804 				rv |= config_deactivate(dev->subdevs[i]);
805 		}
806 		break;
807 	}
808 	return (rv);
809 }
810 
811 int
812 usb_detach(struct device *self, int flags)
813 {
814 	struct usb_softc *sc = (struct usb_softc *)self;
815 	struct usb_event ue;
816 
817 	DPRINTF(("usb_detach: start\n"));
818 
819 	sc->sc_dying = 1;
820 
821 	/* Make all devices disconnect. */
822 	if (sc->sc_port.device != NULL)
823 		usb_disconnect_port(&sc->sc_port, self);
824 
825 	/* Kill off event thread. */
826 	if (sc->sc_event_thread != NULL) {
827 		wakeup(&sc->sc_bus->needs_explore);
828 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
829 			printf("%s: event thread didn't die\n",
830 			       sc->sc_dev.dv_xname);
831 		DPRINTF(("usb_detach: event thread dead\n"));
832 	}
833 
834 	usbd_finish();
835 
836 	if (sc->sc_bus->soft != NULL) {
837 		softintr_disestablish(sc->sc_bus->soft);
838 		sc->sc_bus->soft = NULL;
839 	}
840 
841 	ue.u.ue_ctrlr.ue_bus = sc->sc_dev.dv_unit;
842 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
843 
844 	return (0);
845 }
846