Allow a NULL pointer as argument to usb_get_next_event(), and don't
[dragonfly.git] / sys / bus / usb / usb.c
blob6165a3de7f5adb60da3e6084b81f160001e41131
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.48 2008/05/26 14:00:46 mneumann 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");
100 #ifdef USB_DEBUG
101 #define DPRINTF(x) if (usbdebug) kprintf x
102 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
103 int usbdebug = 0;
104 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
105 &usbdebug, 0, "usb debug level");
107 * 0 - do usual exploration
108 * 1 - do not use timeout exploration
109 * >1 - do no exploration
111 int usb_noexplore = 0;
112 #else
113 #define DPRINTF(x)
114 #define DPRINTFN(n,x)
115 #endif
117 struct usb_softc {
118 cdev_t sc_usbdev;
119 TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
120 usbd_bus_handle sc_bus; /* USB controller */
121 struct usbd_port sc_port; /* dummy port for root hub */
123 struct thread *sc_event_thread;
125 char sc_dying;
128 struct usb_taskq {
129 TAILQ_HEAD(, usb_task) tasks;
130 struct thread *task_thread_proc;
131 const char *name;
132 int taskcreated; /* task thread exists. */
134 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
136 d_open_t usbopen;
137 d_close_t usbclose;
138 d_read_t usbread;
139 d_ioctl_t usbioctl;
140 d_poll_t usbpoll;
142 struct dev_ops usb_ops = {
143 { "usb", USB_CDEV_MAJOR, 0 },
144 .d_open = usbopen,
145 .d_close = usbclose,
146 .d_read = usbread,
147 .d_ioctl = usbioctl,
148 .d_poll = usbpoll,
151 static void usb_discover(device_t);
152 static bus_child_detached_t usb_child_detached;
153 static void usb_create_event_thread(device_t);
154 static void usb_event_thread(void *);
155 static void usb_task_thread(void *);
157 static cdev_t usb_dev; /* The /dev/usb device. */
158 static int usb_ndevs; /* Number of /dev/usbN devices. */
159 /* Busses to explore at the end of boot-time device configuration */
160 static TAILQ_HEAD(, usb_softc) usb_coldexplist =
161 TAILQ_HEAD_INITIALIZER(usb_coldexplist);
163 #define USB_MAX_EVENTS 100
164 struct usb_event_q {
165 struct usb_event ue;
166 TAILQ_ENTRY(usb_event_q) next;
168 static TAILQ_HEAD(, usb_event_q) usb_events =
169 TAILQ_HEAD_INITIALIZER(usb_events);
170 static int usb_nevents = 0;
171 static struct selinfo usb_selevent;
172 static struct proc *usb_async_proc; /* process that wants USB SIGIO */
173 static int usb_dev_open = 0;
174 static void usb_add_event(int, struct usb_event *);
176 static int usb_get_next_event(struct usb_event *);
178 static const char *usbrev_str[] = USBREV_STR;
180 static device_probe_t usb_match;
181 static device_attach_t usb_attach;
182 static device_detach_t usb_detach;
184 static devclass_t usb_devclass;
186 static kobj_method_t usb_methods[] = {
187 DEVMETHOD(device_probe, usb_match),
188 DEVMETHOD(device_attach, usb_attach),
189 DEVMETHOD(device_detach, usb_detach),
190 DEVMETHOD(bus_child_detached, usb_child_detached),
191 DEVMETHOD(device_suspend, bus_generic_suspend),
192 DEVMETHOD(device_resume, bus_generic_resume),
193 DEVMETHOD(device_shutdown, bus_generic_shutdown),
194 {0,0}
197 static driver_t usb_driver = {
198 "usb",
199 usb_methods,
200 sizeof(struct usb_softc)
203 MODULE_DEPEND(usb, usb, 1, 1, 1);
204 MODULE_VERSION(usb, 1);
206 static int
207 usb_match(device_t self)
209 DPRINTF(("usb_match\n"));
210 return (UMATCH_GENERIC);
213 static int
214 usb_attach(device_t self)
216 struct usb_softc *sc = device_get_softc(self);
217 void *aux = device_get_ivars(self);
218 cdev_t tmp_dev;
219 usbd_device_handle dev;
220 usbd_status err;
221 int usbrev;
222 int speed;
223 struct usb_event ue;
225 DPRINTF(("usb_attach\n"));
227 usbd_init();
228 sc->sc_bus = aux;
229 sc->sc_bus->usbctl = sc;
230 sc->sc_port.power = USB_MAX_POWER;
232 usbrev = sc->sc_bus->usbrev;
233 device_printf(self, "USB revision %s", usbrev_str[usbrev]);
234 switch (usbrev) {
235 case USBREV_1_0:
236 case USBREV_1_1:
237 speed = USB_SPEED_FULL;
238 break;
239 case USBREV_2_0:
240 speed = USB_SPEED_HIGH;
241 break;
242 default:
243 kprintf(", not supported\n");
244 sc->sc_dying = 1;
245 return ENXIO;
247 kprintf("\n");
249 /* Make sure not to use tsleep() if we are cold booting. */
250 if (cold)
251 sc->sc_bus->use_polling++;
253 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
254 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
256 #ifdef USB_USE_SOFTINTR
257 callout_init(&sc->sc_bus->softi);
258 #endif
260 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
261 &sc->sc_port);
262 if (!err) {
263 dev = sc->sc_port.device;
264 if (dev->hub == NULL) {
265 sc->sc_dying = 1;
266 device_printf(self,
267 "root device is not a hub\n");
268 return ENXIO;
270 sc->sc_bus->root_hub = dev;
271 #if 1
273 * Turning this code off will delay attachment of USB devices
274 * until the USB event thread is running, which means that
275 * the keyboard will not work until after cold boot.
277 if (cold) {
278 /* Explore high-speed busses before others. */
279 if (speed == USB_SPEED_HIGH)
280 dev->hub->explore(sc->sc_bus->root_hub);
281 else
282 TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
283 sc_coldexplist);
285 #endif
286 } else {
287 device_printf(self,
288 "root hub problem, error=%d\n", err);
289 sc->sc_dying = 1;
291 if (cold)
292 sc->sc_bus->use_polling--;
294 usb_create_event_thread(self);
295 /* The per controller devices (used for usb_discover) */
296 /* XXX This is redundant now, but old usbd's will want it */
297 dev_ops_add(&usb_ops, -1, device_get_unit(self));
298 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
299 UID_ROOT, GID_OPERATOR, 0660,
300 "usb%d", device_get_unit(self));
301 sc->sc_usbdev = reference_dev(tmp_dev);
302 if (usb_ndevs++ == 0) {
303 /* The device spitting out events */
304 dev_ops_add(&usb_ops, -1, USB_DEV_MINOR);
305 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
306 UID_ROOT, GID_OPERATOR, 0660, "usb");
307 usb_dev = reference_dev(tmp_dev);
310 return 0;
313 static const char *taskq_names[] = USB_TASKQ_NAMES;
315 void
316 usb_create_event_thread(device_t self)
318 struct usb_softc *sc = device_get_softc(self);
319 int i;
321 if (kthread_create(usb_event_thread, self, &sc->sc_event_thread,
322 "%s", device_get_nameunit(self))) {
323 device_printf(self,
324 "unable to create event thread for\n");
325 panic("usb_create_event_thread");
328 for (i = 0; i < USB_NUM_TASKQS; i++) {
329 struct usb_taskq *taskq = &usb_taskq[i];
331 if (taskq->taskcreated == 0) {
332 taskq->taskcreated = 1;
333 taskq->name = taskq_names[i];
334 TAILQ_INIT(&taskq->tasks);
335 if (kthread_create(usb_task_thread, taskq,
336 &taskq->task_thread_proc, taskq->name)) {
337 kprintf("unable to create task thread\n");
338 panic("usb_create_event_thread task");
345 * Add a task to be performed by the task thread. This function can be
346 * called from any context and the task will be executed in a process
347 * context ASAP.
349 void
350 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
352 struct usb_taskq *taskq;
354 crit_enter();
356 taskq = &usb_taskq[queue];
357 if (task->queue == -1) {
358 DPRINTFN(2,("usb_add_task: task=%p\n", task));
359 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
360 task->queue = queue;
361 } else {
362 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
364 wakeup(&taskq->tasks);
366 crit_exit();
369 void
370 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
371 int time_out)
373 struct usb_taskq *taskq;
375 crit_enter();
377 taskq = &usb_taskq[queue];
378 if (task->queue == -1) {
379 DPRINTFN(2,("usb_add_task: task=%p\n", task));
380 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
381 task->queue = queue;
382 } else {
383 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
385 wakeup(&taskq->tasks);
387 /* Wait until task is finished */
388 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
390 crit_exit();
393 void
394 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
396 crit_enter();
397 if (task->queue != -1) {
398 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
399 task->queue = -1;
401 crit_exit();
404 void
405 usb_event_thread(void *arg)
407 device_t self = arg;
408 struct usb_softc *sc = device_get_softc(self);
410 DPRINTF(("usb_event_thread: start\n"));
413 * In case this controller is a companion controller to an
414 * EHCI controller we need to wait until the EHCI controller
415 * has grabbed the port.
416 * XXX It would be nicer to do this with a tsleep(), but I don't
417 * know how to synchronize the creation of the threads so it
418 * will work.
420 usb_delay_ms(sc->sc_bus, 500);
422 crit_enter();
424 /* Make sure first discover does something. */
425 sc->sc_bus->needs_explore = 1;
426 usb_discover(self);
428 while (!sc->sc_dying) {
429 #ifdef USB_DEBUG
430 if (usb_noexplore < 2)
431 #endif
432 usb_discover(self);
433 #ifdef USB_DEBUG
434 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
435 usb_noexplore ? 0 : hz * 60);
436 #else
437 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
438 #endif
439 DPRINTFN(2,("usb_event_thread: woke up\n"));
441 sc->sc_event_thread = NULL;
443 crit_exit();
445 /* In case parent is waiting for us to exit. */
446 wakeup(sc);
448 DPRINTF(("usb_event_thread: exit\n"));
449 kthread_exit();
452 void
453 usb_task_thread(void *arg)
455 struct usb_task *task;
456 struct usb_taskq *taskq;
458 crit_enter();
460 taskq = arg;
461 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
463 while (usb_ndevs > 0) {
464 task = TAILQ_FIRST(&taskq->tasks);
465 if (task == NULL) {
466 tsleep(&taskq->tasks, 0, "usbtsk", 0);
467 task = TAILQ_FIRST(&taskq->tasks);
469 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
470 if (task != NULL) {
471 TAILQ_REMOVE(&taskq->tasks, task, next);
472 task->queue = -1;
473 crit_exit();
474 task->fun(task->arg);
475 crit_enter();
476 wakeup((&taskq->tasks + 1));
480 crit_exit();
482 taskq->taskcreated = 0;
483 wakeup(&taskq->taskcreated);
485 DPRINTF(("usb_event_thread: exit\n"));
489 usbopen(struct dev_open_args *ap)
491 cdev_t dev = ap->a_head.a_dev;
492 int unit = USBUNIT(dev);
493 struct usb_softc *sc;
495 if (unit == USB_DEV_MINOR) {
496 if (usb_dev_open)
497 return (EBUSY);
498 usb_dev_open = 1;
499 usb_async_proc = NULL;
500 return (0);
503 sc = devclass_get_softc(usb_devclass, unit);
504 if (sc == NULL)
505 return (ENXIO);
507 if (sc->sc_dying)
508 return (EIO);
510 return (0);
514 usbread(struct dev_read_args *ap)
516 cdev_t dev = ap->a_head.a_dev;
517 struct uio *uio = ap->a_uio;
518 struct usb_event ue;
519 int unit = USBUNIT(dev);
520 int error, n;
522 if (unit != USB_DEV_MINOR)
523 return (ENODEV);
525 if (uio->uio_resid != sizeof(struct usb_event))
526 return (EINVAL);
528 error = 0;
529 crit_enter();
530 for (;;) {
531 n = usb_get_next_event(&ue);
532 if (n != 0)
533 break;
534 if (ap->a_ioflag & IO_NDELAY) {
535 error = EWOULDBLOCK;
536 break;
538 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
539 if (error)
540 break;
542 crit_exit();
543 if (!error)
544 error = uiomove((void *)&ue, uio->uio_resid, uio);
546 return (error);
550 usbclose(struct dev_close_args *ap)
552 cdev_t dev = ap->a_head.a_dev;
553 int unit = USBUNIT(dev);
555 if (unit == USB_DEV_MINOR) {
556 usb_async_proc = NULL;
557 usb_dev_open = 0;
560 return (0);
564 usbioctl(struct dev_ioctl_args *ap)
566 cdev_t devt = ap->a_head.a_dev;
567 struct usb_softc *sc;
568 int unit = USBUNIT(devt);
570 if (unit == USB_DEV_MINOR) {
571 switch (ap->a_cmd) {
572 case FIOASYNC:
573 if (*(int *)ap->a_data)
574 usb_async_proc = curproc;
575 else
576 usb_async_proc = NULL;
577 return (0);
579 default:
580 return (EINVAL);
584 sc = devclass_get_softc(usb_devclass, unit);
586 if (sc->sc_dying)
587 return (EIO);
589 switch (ap->a_cmd) {
590 /* This part should be deleted */
591 case USB_DISCOVER:
592 break;
593 case USB_REQUEST:
595 struct usb_ctl_request *ur = (void *)ap->a_data;
596 int len = UGETW(ur->ucr_request.wLength);
597 struct iovec iov;
598 struct uio uio;
599 void *ptr = 0;
600 int addr = ur->ucr_addr;
601 usbd_status err;
602 int error = 0;
604 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
605 if (len < 0 || len > 32768)
606 return (EINVAL);
607 if (addr < 0 || addr >= USB_MAX_DEVICES ||
608 sc->sc_bus->devices[addr] == 0)
609 return (EINVAL);
610 if (len != 0) {
611 iov.iov_base = (caddr_t)ur->ucr_data;
612 iov.iov_len = len;
613 uio.uio_iov = &iov;
614 uio.uio_iovcnt = 1;
615 uio.uio_resid = len;
616 uio.uio_offset = 0;
617 uio.uio_segflg = UIO_USERSPACE;
618 uio.uio_rw =
619 ur->ucr_request.bmRequestType & UT_READ ?
620 UIO_READ : UIO_WRITE;
621 uio.uio_td = curthread;
622 ptr = kmalloc(len, M_TEMP, M_WAITOK);
623 if (uio.uio_rw == UIO_WRITE) {
624 error = uiomove(ptr, len, &uio);
625 if (error)
626 goto ret;
629 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
630 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
631 USBD_DEFAULT_TIMEOUT);
632 if (err) {
633 error = EIO;
634 goto ret;
636 if (len != 0) {
637 if (uio.uio_rw == UIO_READ) {
638 error = uiomove(ptr, len, &uio);
639 if (error)
640 goto ret;
643 ret:
644 if (ptr)
645 kfree(ptr, M_TEMP);
646 return (error);
649 case USB_DEVICEINFO:
651 struct usb_device_info *di = (void *)ap->a_data;
652 int addr = di->udi_addr;
653 usbd_device_handle dev;
655 if (addr < 1 || addr >= USB_MAX_DEVICES)
656 return (EINVAL);
657 dev = sc->sc_bus->devices[addr];
658 if (dev == NULL)
659 return (ENXIO);
660 usbd_fill_deviceinfo(dev, di, 1);
661 break;
664 case USB_DEVICESTATS:
665 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
666 break;
668 default:
669 return (EINVAL);
671 return (0);
675 usbpoll(struct dev_poll_args *ap)
677 cdev_t dev = ap->a_head.a_dev;
678 int revents, mask;
679 int unit = USBUNIT(dev);
681 if (unit == USB_DEV_MINOR) {
682 revents = 0;
683 mask = POLLIN | POLLRDNORM;
685 crit_enter();
686 if (ap->a_events & mask && usb_nevents > 0)
687 revents |= ap->a_events & mask;
688 if (revents == 0 && ap->a_events & mask)
689 selrecord(curthread, &usb_selevent);
690 crit_exit();
691 ap->a_events = revents;
692 return (0);
693 } else {
694 ap->a_events = 0;
695 return (0); /* select/poll never wakes up - back compat */
699 /* Explore device tree from the root. */
700 static void
701 usb_discover(device_t self)
703 struct usb_softc *sc = device_get_softc(self);
705 DPRINTFN(2,("usb_discover\n"));
706 #ifdef USB_DEBUG
707 if (usb_noexplore > 1)
708 return;
709 #endif
712 * We need mutual exclusion while traversing the device tree,
713 * but this is guaranteed since this function is only called
714 * from the event thread for the controller.
716 crit_enter();
717 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
718 sc->sc_bus->needs_explore = 0;
720 crit_exit();
721 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
722 crit_enter();
725 crit_exit();
728 void
729 usb_needs_explore(usbd_device_handle dev)
731 DPRINTFN(2,("usb_needs_explore\n"));
732 dev->bus->needs_explore = 1;
733 wakeup(&dev->bus->needs_explore);
736 /* Called from a critical section */
738 usb_get_next_event(struct usb_event *ue)
740 struct usb_event_q *ueq;
742 if (usb_nevents <= 0)
743 return (0);
744 ueq = TAILQ_FIRST(&usb_events);
745 #ifdef DIAGNOSTIC
746 if (ueq == NULL) {
747 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
748 usb_nevents = 0;
749 return (0);
751 #endif
752 if (ue)
753 *ue = ueq->ue;
754 TAILQ_REMOVE(&usb_events, ueq, next);
755 kfree(ueq, M_USBDEV);
756 usb_nevents--;
757 return (1);
760 void
761 usbd_add_dev_event(int type, usbd_device_handle udev)
763 struct usb_event ue;
765 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
766 usb_add_event(type, &ue);
769 void
770 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
772 struct usb_event ue;
774 ue.u.ue_driver.ue_cookie = udev->cookie;
775 strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
776 sizeof ue.u.ue_driver.ue_devname);
777 usb_add_event(type, &ue);
780 void
781 usb_add_event(int type, struct usb_event *uep)
783 struct usb_event_q *ueq;
784 struct timeval thetime;
786 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
787 ueq->ue = *uep;
788 ueq->ue.ue_type = type;
789 microtime(&thetime);
790 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
792 crit_enter();
793 if (USB_EVENT_IS_DETACH(type)) {
794 struct usb_event_q *ueqi, *ueqi_next;
796 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
797 ueqi_next = TAILQ_NEXT(ueqi, next);
798 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
799 uep->u.ue_device.udi_cookie.cookie) {
800 TAILQ_REMOVE(&usb_events, ueqi, next);
801 kfree(ueqi, M_USBDEV);
802 usb_nevents--;
803 ueqi_next = TAILQ_FIRST(&usb_events);
807 if (usb_nevents >= USB_MAX_EVENTS) {
808 /* Too many queued events, drop an old one. */
809 DPRINTF(("usb: event dropped\n"));
810 usb_get_next_event(NULL);
812 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
813 usb_nevents++;
814 wakeup(&usb_events);
815 selwakeup(&usb_selevent);
816 if (usb_async_proc != NULL) {
817 ksignal(usb_async_proc, SIGIO);
819 crit_exit();
822 void
823 usb_schedsoftintr(usbd_bus_handle bus)
825 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
826 #ifdef USB_USE_SOFTINTR
827 if (bus->use_polling) {
828 bus->methods->soft_intr(bus);
829 } else {
830 if (!callout_pending(&bus->softi))
831 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
832 bus);
834 #else
835 bus->methods->soft_intr(bus);
836 #endif /* USB_USE_SOFTINTR */
839 static int
840 usb_detach(device_t self)
842 struct usb_softc *sc = device_get_softc(self);
843 struct usb_event ue;
845 DPRINTF(("usb_detach: start\n"));
847 sc->sc_dying = 1;
849 /* Make all devices disconnect. */
850 if (sc->sc_port.device != NULL)
851 usb_disconnect_port(&sc->sc_port, self);
853 /* Kill off event thread. */
854 if (sc->sc_event_thread != NULL) {
855 wakeup(&sc->sc_bus->needs_explore);
856 if (tsleep(sc, 0, "usbdet", hz * 60))
857 device_printf(self,
858 "event thread didn't die\n");
859 DPRINTF(("usb_detach: event thread dead\n"));
862 destroy_dev(sc->sc_usbdev);
863 if (--usb_ndevs == 0) {
864 int i;
866 destroy_dev(usb_dev);
867 usb_dev = NULL;
869 for (i = 0; i < USB_NUM_TASKQS; i++) {
870 struct usb_taskq *taskq = &usb_taskq[i];
871 wakeup(&taskq->tasks);
872 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
873 hz * 60)) {
874 kprintf("usb task thread %s didn't die\n",
875 taskq->name);
880 usbd_finish();
882 #ifdef USB_USE_SOFTINTR
883 callout_stop(&sc->sc_bus->softi);
884 #endif
886 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
887 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
889 return (0);
892 static void
893 usb_child_detached(device_t self, device_t child)
895 struct usb_softc *sc = device_get_softc(self);
897 /* XXX, should check it is the right device. */
898 sc->sc_port.device = NULL;
901 /* Explore USB busses at the end of device configuration */
902 static void
903 usb_cold_explore(void *arg)
905 struct usb_softc *sc;
907 KASSERT(cold || TAILQ_EMPTY(&usb_coldexplist),
908 ("usb_cold_explore: busses to explore when !cold"));
909 while (!TAILQ_EMPTY(&usb_coldexplist)) {
910 sc = TAILQ_FIRST(&usb_coldexplist);
911 TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
913 sc->sc_bus->use_polling++;
914 sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
915 sc->sc_bus->use_polling--;
919 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
920 usb_cold_explore, NULL);
922 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
923 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
924 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);