Kernel mtx - Add mtxsleep(), interlocked tsleep w/ mutexes
[dragonfly.git] / sys / bus / usb / usb.c
blobd2b736db386f86b44aea6febaba8a0ac1f9e5a95
1 /*
2 * $NetBSD: usb.c,v 1.68 2002/02/20 20:30:12 christos Exp $
3 * $FreeBSD: src/sys/dev/usb/usb.c,v 1.106 2005/03/27 15:31:23 iedowse Exp $
4 * $DragonFly: src/sys/bus/usb/usb.c,v 1.50 2008/09/26 08:21:22 hasso Exp $
5 */
7 /* Also already merged from NetBSD:
8 * $NetBSD: usb.c,v 1.70 2002/05/09 21:54:32 augustss Exp $
9 * $NetBSD: usb.c,v 1.71 2002/06/01 23:51:04 lukem Exp $
10 * $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
11 * $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $
15 * Copyright (c) 1998 The NetBSD Foundation, Inc.
16 * All rights reserved.
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Lennart Augustsson (lennart@augustsson.net) at
20 * Carlstedt Research & Technology.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by the NetBSD
33 * Foundation, Inc. and its contributors.
34 * 4. Neither the name of The NetBSD Foundation nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
40 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
42 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
52 * USB specifications and other documentation can be found at
53 * http://www.usb.org/developers/docs/ and
54 * http://www.usb.org/developers/devclass_docs/
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/unistd.h>
63 #include <sys/module.h>
64 #include <sys/bus.h>
65 #include <sys/fcntl.h>
66 #include <sys/filio.h>
67 #include <sys/uio.h>
68 #include <sys/kthread.h>
69 #include <sys/proc.h>
70 #include <sys/conf.h>
71 #include <sys/device.h>
72 #include <sys/poll.h>
73 #include <sys/select.h>
74 #include <sys/vnode.h>
75 #include <sys/signalvar.h>
76 #include <sys/sysctl.h>
77 #include <sys/thread2.h>
79 #include <bus/usb/usb.h>
80 #include <bus/usb/usbdi.h>
81 #include <bus/usb/usbdi_util.h>
83 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
84 #define USB_DEV_MINOR 255 /* event queue device */
86 MALLOC_DEFINE(M_USB, "USB", "USB");
87 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
88 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
90 #include "usb_if.h"
92 #include <bus/usb/usbdivar.h>
93 #include <bus/usb/usb_quirks.h>
95 /* Define this unconditionally in case a kernel module is loaded that
96 * has been compiled with debugging options.
98 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
101 * XXX: This is a hack! If your USB keyboard doesn't work
102 * early at boot, try setting this tunable to 0 from
103 * bootleader:
105 * set hw.usb.hack_defer_exploration=0
107 static int hack_defer_exploration = 1;
109 #ifdef USB_DEBUG
110 #define DPRINTF(x) if (usbdebug) kprintf x
111 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
112 int usbdebug = 0;
113 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
114 &usbdebug, 0, "usb debug level");
117 * 0 - do usual exploration
118 * 1 - do not use timeout exploration
119 * >1 - do no exploration
121 int usb_noexplore = 0;
122 #else
123 #define DPRINTF(x)
124 #define DPRINTFN(n,x)
125 #endif
127 struct usb_softc {
128 cdev_t sc_usbdev;
129 TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
130 usbd_bus_handle sc_bus; /* USB controller */
131 struct usbd_port sc_port; /* dummy port for root hub */
133 struct thread *sc_event_thread;
135 char sc_dying;
138 struct usb_taskq {
139 TAILQ_HEAD(, usb_task) tasks;
140 struct thread *task_thread_proc;
141 const char *name;
142 int taskcreated; /* task thread exists. */
144 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
146 d_open_t usbopen;
147 d_close_t usbclose;
148 d_read_t usbread;
149 d_ioctl_t usbioctl;
150 d_poll_t usbpoll;
152 struct dev_ops usb_ops = {
153 { "usb", USB_CDEV_MAJOR, 0 },
154 .d_open = usbopen,
155 .d_close = usbclose,
156 .d_read = usbread,
157 .d_ioctl = usbioctl,
158 .d_poll = usbpoll,
161 static void usb_discover(device_t);
162 static bus_child_detached_t usb_child_detached;
163 static void usb_create_event_thread(device_t);
164 static void usb_event_thread(void *);
165 static void usb_task_thread(void *);
167 static cdev_t usb_dev; /* The /dev/usb device. */
168 static int usb_ndevs; /* Number of /dev/usbN devices. */
169 /* Busses to explore at the end of boot-time device configuration */
170 static TAILQ_HEAD(, usb_softc) usb_coldexplist =
171 TAILQ_HEAD_INITIALIZER(usb_coldexplist);
173 #define USB_MAX_EVENTS 100
174 struct usb_event_q {
175 struct usb_event ue;
176 TAILQ_ENTRY(usb_event_q) next;
178 static TAILQ_HEAD(, usb_event_q) usb_events =
179 TAILQ_HEAD_INITIALIZER(usb_events);
180 static int usb_nevents = 0;
181 static struct selinfo usb_selevent;
182 static struct proc *usb_async_proc; /* process that wants USB SIGIO */
183 static int usb_dev_open = 0;
184 static void usb_add_event(int, struct usb_event *);
186 static int usb_get_next_event(struct usb_event *);
188 static const char *usbrev_str[] = USBREV_STR;
190 static device_probe_t usb_match;
191 static device_attach_t usb_attach;
192 static device_detach_t usb_detach;
194 static devclass_t usb_devclass;
196 static kobj_method_t usb_methods[] = {
197 DEVMETHOD(device_probe, usb_match),
198 DEVMETHOD(device_attach, usb_attach),
199 DEVMETHOD(device_detach, usb_detach),
200 DEVMETHOD(bus_child_detached, usb_child_detached),
201 DEVMETHOD(device_suspend, bus_generic_suspend),
202 DEVMETHOD(device_resume, bus_generic_resume),
203 DEVMETHOD(device_shutdown, bus_generic_shutdown),
204 {0,0}
207 static driver_t usb_driver = {
208 "usb",
209 usb_methods,
210 sizeof(struct usb_softc)
213 MODULE_DEPEND(usb, usb, 1, 1, 1);
214 MODULE_VERSION(usb, 1);
216 static int
217 usb_match(device_t self)
219 DPRINTF(("usb_match\n"));
220 return (UMATCH_GENERIC);
223 static int
224 usb_attach(device_t self)
226 struct usb_softc *sc = device_get_softc(self);
227 void *aux = device_get_ivars(self);
228 cdev_t tmp_dev;
229 usbd_device_handle dev;
230 usbd_status err;
231 int usbrev;
232 int speed;
233 struct usb_event ue;
235 TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
236 &hack_defer_exploration);
238 DPRINTF(("usb_attach\n"));
240 usbd_init();
241 sc->sc_bus = aux;
242 sc->sc_bus->usbctl = sc;
243 sc->sc_port.power = USB_MAX_POWER;
245 usbrev = sc->sc_bus->usbrev;
246 device_printf(self, "USB revision %s", usbrev_str[usbrev]);
247 switch (usbrev) {
248 case USBREV_1_0:
249 case USBREV_1_1:
250 speed = USB_SPEED_FULL;
251 break;
252 case USBREV_2_0:
253 speed = USB_SPEED_HIGH;
254 break;
255 default:
256 kprintf(", not supported\n");
257 sc->sc_dying = 1;
258 return ENXIO;
260 kprintf("\n");
262 /* Make sure not to use tsleep() if we are cold booting. */
263 if (hack_defer_exploration && cold)
264 sc->sc_bus->use_polling++;
266 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
267 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
269 #ifdef USB_USE_SOFTINTR
270 callout_init(&sc->sc_bus->softi);
271 #endif
273 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
274 &sc->sc_port);
275 if (!err) {
276 dev = sc->sc_port.device;
277 if (dev->hub == NULL) {
278 sc->sc_dying = 1;
279 device_printf(self,
280 "root device is not a hub\n");
281 return ENXIO;
283 sc->sc_bus->root_hub = dev;
284 #if 1
286 * Turning this code off will delay attachment of USB devices
287 * until the USB event thread is running, which means that
288 * the keyboard will not work until after cold boot.
290 if (cold) {
291 if (hack_defer_exploration) {
292 /* Explore high-speed busses before others. */
293 if (speed == USB_SPEED_HIGH)
294 dev->hub->explore(sc->sc_bus->root_hub);
295 else
296 TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
297 sc_coldexplist);
298 } else {
300 * XXX Exploring high speed devices here will
301 * hang the system.
303 if (speed != USB_SPEED_HIGH)
304 dev->hub->explore(sc->sc_bus->root_hub);
307 #endif
308 } else {
309 device_printf(self,
310 "root hub problem, error=%d\n", err);
311 sc->sc_dying = 1;
313 if (hack_defer_exploration && cold)
314 sc->sc_bus->use_polling--;
316 usb_create_event_thread(self);
319 * The per controller devices (used for usb_discover)
320 * XXX This is redundant now, but old usbd's will want it
322 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
323 UID_ROOT, GID_OPERATOR, 0660,
324 "usb%d", device_get_unit(self));
325 sc->sc_usbdev = reference_dev(tmp_dev);
326 if (usb_ndevs++ == 0) {
327 /* The device spitting out events */
328 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
329 UID_ROOT, GID_OPERATOR, 0660, "usb");
330 usb_dev = reference_dev(tmp_dev);
333 return 0;
336 static const char *taskq_names[] = USB_TASKQ_NAMES;
338 void
339 usb_create_event_thread(device_t self)
341 struct usb_softc *sc = device_get_softc(self);
342 int i;
344 if (kthread_create(usb_event_thread, self, &sc->sc_event_thread,
345 "%s", device_get_nameunit(self))) {
346 device_printf(self,
347 "unable to create event thread for\n");
348 panic("usb_create_event_thread");
351 for (i = 0; i < USB_NUM_TASKQS; i++) {
352 struct usb_taskq *taskq = &usb_taskq[i];
354 if (taskq->taskcreated == 0) {
355 taskq->taskcreated = 1;
356 taskq->name = taskq_names[i];
357 TAILQ_INIT(&taskq->tasks);
358 if (kthread_create(usb_task_thread, taskq,
359 &taskq->task_thread_proc, taskq->name)) {
360 kprintf("unable to create task thread\n");
361 panic("usb_create_event_thread task");
368 * Add a task to be performed by the task thread. This function can be
369 * called from any context and the task will be executed in a process
370 * context ASAP.
372 void
373 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
375 struct usb_taskq *taskq;
377 crit_enter();
379 taskq = &usb_taskq[queue];
380 if (task->queue == -1) {
381 DPRINTFN(2,("usb_add_task: task=%p\n", task));
382 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
383 task->queue = queue;
384 } else {
385 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
387 wakeup(&taskq->tasks);
389 crit_exit();
392 void
393 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
394 int time_out)
396 struct usb_taskq *taskq;
398 crit_enter();
400 taskq = &usb_taskq[queue];
401 if (task->queue == -1) {
402 DPRINTFN(2,("usb_add_task: task=%p\n", task));
403 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
404 task->queue = queue;
405 } else {
406 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
408 wakeup(&taskq->tasks);
410 /* Wait until task is finished */
411 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
413 crit_exit();
416 void
417 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
419 crit_enter();
420 if (task->queue != -1) {
421 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
422 task->queue = -1;
424 crit_exit();
427 void
428 usb_event_thread(void *arg)
430 device_t self = arg;
431 struct usb_softc *sc = device_get_softc(self);
433 DPRINTF(("usb_event_thread: start\n"));
436 * In case this controller is a companion controller to an
437 * EHCI controller we need to wait until the EHCI controller
438 * has grabbed the port.
439 * XXX It would be nicer to do this with a tsleep(), but I don't
440 * know how to synchronize the creation of the threads so it
441 * will work.
443 usb_delay_ms(sc->sc_bus, 500);
445 crit_enter();
447 /* Make sure first discover does something. */
448 sc->sc_bus->needs_explore = 1;
449 usb_discover(self);
451 while (!sc->sc_dying) {
452 #ifdef USB_DEBUG
453 if (usb_noexplore < 2)
454 #endif
455 usb_discover(self);
456 #ifdef USB_DEBUG
457 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
458 usb_noexplore ? 0 : hz * 60);
459 #else
460 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
461 #endif
462 DPRINTFN(2,("usb_event_thread: woke up\n"));
464 sc->sc_event_thread = NULL;
466 crit_exit();
468 /* In case parent is waiting for us to exit. */
469 wakeup(sc);
471 DPRINTF(("usb_event_thread: exit\n"));
472 kthread_exit();
475 void
476 usb_task_thread(void *arg)
478 struct usb_task *task;
479 struct usb_taskq *taskq;
481 crit_enter();
483 taskq = arg;
484 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
486 while (usb_ndevs > 0) {
487 task = TAILQ_FIRST(&taskq->tasks);
488 if (task == NULL) {
489 tsleep(&taskq->tasks, 0, "usbtsk", 0);
490 task = TAILQ_FIRST(&taskq->tasks);
492 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
493 if (task != NULL) {
494 TAILQ_REMOVE(&taskq->tasks, task, next);
495 task->queue = -1;
496 crit_exit();
497 task->fun(task->arg);
498 crit_enter();
499 wakeup((&taskq->tasks + 1));
503 crit_exit();
505 taskq->taskcreated = 0;
506 wakeup(&taskq->taskcreated);
508 DPRINTF(("usb_event_thread: exit\n"));
512 usbopen(struct dev_open_args *ap)
514 cdev_t dev = ap->a_head.a_dev;
515 int unit = USBUNIT(dev);
516 struct usb_softc *sc;
518 if (unit == USB_DEV_MINOR) {
519 if (usb_dev_open)
520 return (EBUSY);
521 usb_dev_open = 1;
522 usb_async_proc = NULL;
523 return (0);
526 sc = devclass_get_softc(usb_devclass, unit);
527 if (sc == NULL)
528 return (ENXIO);
530 if (sc->sc_dying)
531 return (EIO);
533 return (0);
537 usbread(struct dev_read_args *ap)
539 cdev_t dev = ap->a_head.a_dev;
540 struct uio *uio = ap->a_uio;
541 struct usb_event ue;
542 int unit = USBUNIT(dev);
543 int error, n;
545 if (unit != USB_DEV_MINOR)
546 return (ENODEV);
548 if (uio->uio_resid != sizeof(struct usb_event))
549 return (EINVAL);
551 error = 0;
552 crit_enter();
553 for (;;) {
554 n = usb_get_next_event(&ue);
555 if (n != 0)
556 break;
557 if (ap->a_ioflag & IO_NDELAY) {
558 error = EWOULDBLOCK;
559 break;
561 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
562 if (error)
563 break;
565 crit_exit();
566 if (!error)
567 error = uiomove((void *)&ue, uio->uio_resid, uio);
569 return (error);
573 usbclose(struct dev_close_args *ap)
575 cdev_t dev = ap->a_head.a_dev;
576 int unit = USBUNIT(dev);
578 if (unit == USB_DEV_MINOR) {
579 usb_async_proc = NULL;
580 usb_dev_open = 0;
583 return (0);
587 usbioctl(struct dev_ioctl_args *ap)
589 cdev_t devt = ap->a_head.a_dev;
590 struct usb_softc *sc;
591 int unit = USBUNIT(devt);
593 if (unit == USB_DEV_MINOR) {
594 switch (ap->a_cmd) {
595 case FIOASYNC:
596 if (*(int *)ap->a_data)
597 usb_async_proc = curproc;
598 else
599 usb_async_proc = NULL;
600 return (0);
602 default:
603 return (EINVAL);
607 sc = devclass_get_softc(usb_devclass, unit);
609 if (sc->sc_dying)
610 return (EIO);
612 switch (ap->a_cmd) {
613 /* This part should be deleted */
614 case USB_DISCOVER:
615 break;
616 case USB_REQUEST:
618 struct usb_ctl_request *ur = (void *)ap->a_data;
619 int len = UGETW(ur->ucr_request.wLength);
620 struct iovec iov;
621 struct uio uio;
622 void *ptr = 0;
623 int addr = ur->ucr_addr;
624 usbd_status err;
625 int error = 0;
627 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
628 if (len < 0 || len > 32768)
629 return (EINVAL);
630 if (addr < 0 || addr >= USB_MAX_DEVICES ||
631 sc->sc_bus->devices[addr] == 0)
632 return (EINVAL);
633 if (len != 0) {
634 iov.iov_base = (caddr_t)ur->ucr_data;
635 iov.iov_len = len;
636 uio.uio_iov = &iov;
637 uio.uio_iovcnt = 1;
638 uio.uio_resid = len;
639 uio.uio_offset = 0;
640 uio.uio_segflg = UIO_USERSPACE;
641 uio.uio_rw =
642 ur->ucr_request.bmRequestType & UT_READ ?
643 UIO_READ : UIO_WRITE;
644 uio.uio_td = curthread;
645 ptr = kmalloc(len, M_TEMP, M_WAITOK);
646 if (uio.uio_rw == UIO_WRITE) {
647 error = uiomove(ptr, len, &uio);
648 if (error)
649 goto ret;
652 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
653 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
654 USBD_DEFAULT_TIMEOUT);
655 if (err) {
656 error = EIO;
657 goto ret;
659 if (len != 0) {
660 if (uio.uio_rw == UIO_READ) {
661 error = uiomove(ptr, len, &uio);
662 if (error)
663 goto ret;
666 ret:
667 if (ptr)
668 kfree(ptr, M_TEMP);
669 return (error);
672 case USB_DEVICEINFO:
674 struct usb_device_info *di = (void *)ap->a_data;
675 int addr = di->udi_addr;
676 usbd_device_handle dev;
678 if (addr < 1 || addr >= USB_MAX_DEVICES)
679 return (EINVAL);
680 dev = sc->sc_bus->devices[addr];
681 if (dev == NULL)
682 return (ENXIO);
683 usbd_fill_deviceinfo(dev, di, 1);
684 break;
687 case USB_DEVICESTATS:
688 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
689 break;
691 default:
692 return (EINVAL);
694 return (0);
698 usbpoll(struct dev_poll_args *ap)
700 cdev_t dev = ap->a_head.a_dev;
701 int revents, mask;
702 int unit = USBUNIT(dev);
704 if (unit == USB_DEV_MINOR) {
705 revents = 0;
706 mask = POLLIN | POLLRDNORM;
708 crit_enter();
709 if (ap->a_events & mask && usb_nevents > 0)
710 revents |= ap->a_events & mask;
711 if (revents == 0 && ap->a_events & mask)
712 selrecord(curthread, &usb_selevent);
713 crit_exit();
714 ap->a_events = revents;
715 return (0);
716 } else {
717 ap->a_events = 0;
718 return (0); /* select/poll never wakes up - back compat */
722 /* Explore device tree from the root. */
723 static void
724 usb_discover(device_t self)
726 struct usb_softc *sc = device_get_softc(self);
728 DPRINTFN(2,("usb_discover\n"));
729 #ifdef USB_DEBUG
730 if (usb_noexplore > 1)
731 return;
732 #endif
735 * We need mutual exclusion while traversing the device tree,
736 * but this is guaranteed since this function is only called
737 * from the event thread for the controller.
739 crit_enter();
740 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
741 sc->sc_bus->needs_explore = 0;
743 crit_exit();
744 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
745 crit_enter();
748 crit_exit();
751 void
752 usb_needs_explore(usbd_device_handle dev)
754 DPRINTFN(2,("usb_needs_explore\n"));
755 dev->bus->needs_explore = 1;
756 wakeup(&dev->bus->needs_explore);
759 /* Called from a critical section */
761 usb_get_next_event(struct usb_event *ue)
763 struct usb_event_q *ueq;
765 if (usb_nevents <= 0)
766 return (0);
767 ueq = TAILQ_FIRST(&usb_events);
768 #ifdef DIAGNOSTIC
769 if (ueq == NULL) {
770 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
771 usb_nevents = 0;
772 return (0);
774 #endif
775 if (ue)
776 *ue = ueq->ue;
777 TAILQ_REMOVE(&usb_events, ueq, next);
778 kfree(ueq, M_USBDEV);
779 usb_nevents--;
780 return (1);
783 void
784 usbd_add_dev_event(int type, usbd_device_handle udev)
786 struct usb_event ue;
788 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
789 usb_add_event(type, &ue);
792 void
793 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
795 struct usb_event ue;
797 ue.u.ue_driver.ue_cookie = udev->cookie;
798 strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
799 sizeof ue.u.ue_driver.ue_devname);
800 usb_add_event(type, &ue);
803 void
804 usb_add_event(int type, struct usb_event *uep)
806 struct usb_event_q *ueq;
807 struct timeval thetime;
809 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
810 ueq->ue = *uep;
811 ueq->ue.ue_type = type;
812 microtime(&thetime);
813 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
815 crit_enter();
816 if (USB_EVENT_IS_DETACH(type)) {
817 struct usb_event_q *ueqi, *ueqi_next;
819 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
820 ueqi_next = TAILQ_NEXT(ueqi, next);
821 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
822 uep->u.ue_device.udi_cookie.cookie) {
823 TAILQ_REMOVE(&usb_events, ueqi, next);
824 kfree(ueqi, M_USBDEV);
825 usb_nevents--;
826 ueqi_next = TAILQ_FIRST(&usb_events);
830 if (usb_nevents >= USB_MAX_EVENTS) {
831 /* Too many queued events, drop an old one. */
832 DPRINTF(("usb: event dropped\n"));
833 usb_get_next_event(NULL);
835 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
836 usb_nevents++;
837 wakeup(&usb_events);
838 selwakeup(&usb_selevent);
839 if (usb_async_proc != NULL) {
840 ksignal(usb_async_proc, SIGIO);
842 crit_exit();
845 void
846 usb_schedsoftintr(usbd_bus_handle bus)
848 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
849 #ifdef USB_USE_SOFTINTR
850 if (bus->use_polling) {
851 bus->methods->soft_intr(bus);
852 } else {
853 if (!callout_pending(&bus->softi))
854 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
855 bus);
857 #else
858 bus->methods->soft_intr(bus);
859 #endif /* USB_USE_SOFTINTR */
862 static int
863 usb_detach(device_t self)
865 struct usb_softc *sc = device_get_softc(self);
866 struct usb_event ue;
868 DPRINTF(("usb_detach: start\n"));
870 sc->sc_dying = 1;
872 /* Make all devices disconnect. */
873 if (sc->sc_port.device != NULL)
874 usb_disconnect_port(&sc->sc_port, self);
876 /* Kill off event thread. */
877 if (sc->sc_event_thread != NULL) {
878 wakeup(&sc->sc_bus->needs_explore);
879 if (tsleep(sc, 0, "usbdet", hz * 60))
880 device_printf(self,
881 "event thread didn't die\n");
882 DPRINTF(("usb_detach: event thread dead\n"));
885 release_dev(sc->sc_usbdev);
887 if (--usb_ndevs == 0) {
888 int i;
890 release_dev(usb_dev);
891 dev_ops_remove_minor(&usb_ops, USB_DEV_MINOR);
892 usb_dev = NULL;
894 for (i = 0; i < USB_NUM_TASKQS; i++) {
895 struct usb_taskq *taskq = &usb_taskq[i];
896 wakeup(&taskq->tasks);
897 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
898 hz * 60)) {
899 kprintf("usb task thread %s didn't die\n",
900 taskq->name);
905 usbd_finish();
907 #ifdef USB_USE_SOFTINTR
908 callout_stop(&sc->sc_bus->softi);
909 #endif
911 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
912 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
914 return (0);
917 static void
918 usb_child_detached(device_t self, device_t child)
920 struct usb_softc *sc = device_get_softc(self);
922 /* XXX, should check it is the right device. */
923 sc->sc_port.device = NULL;
926 /* Explore USB busses at the end of device configuration */
927 static void
928 usb_cold_explore(void *arg)
930 struct usb_softc *sc;
932 TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
933 &hack_defer_exploration);
935 if (!hack_defer_exploration)
936 return;
938 KASSERT(cold || TAILQ_EMPTY(&usb_coldexplist),
939 ("usb_cold_explore: busses to explore when !cold"));
940 while (!TAILQ_EMPTY(&usb_coldexplist)) {
941 sc = TAILQ_FIRST(&usb_coldexplist);
942 TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
944 sc->sc_bus->use_polling++;
945 sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
946 sc->sc_bus->use_polling--;
950 struct usbd_bus *
951 usb_getbushandle(struct usb_softc *sc)
953 return (sc->sc_bus);
957 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
958 usb_cold_explore, NULL);
960 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
961 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
962 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);