network - Fix issue with recent unix domain socket gc fixes
[dragonfly.git] / sys / bus / usb / usb.c
blob2f6db14c6a0402dc0e931c5f391e1d4eab6623da
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/event.h>
73 #include <sys/vnode.h>
74 #include <sys/signalvar.h>
75 #include <sys/sysctl.h>
77 #include <sys/thread2.h>
78 #include <sys/mplock2.h>
80 #include <bus/usb/usb.h>
81 #include <bus/usb/usbdi.h>
82 #include <bus/usb/usbdi_util.h>
84 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
85 #define USB_DEV_MINOR 255 /* event queue device */
87 MALLOC_DEFINE(M_USB, "USB", "USB");
88 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
89 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
91 #include "usb_if.h"
93 #include <bus/usb/usbdivar.h>
94 #include <bus/usb/usb_quirks.h>
96 /* Define this unconditionally in case a kernel module is loaded that
97 * has been compiled with debugging options.
99 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
102 * XXX: This is a hack! If your USB keyboard doesn't work
103 * early at boot, try setting this tunable to 0 from
104 * bootleader:
106 * set hw.usb.hack_defer_exploration=0
108 static int hack_defer_exploration = 1;
110 #ifdef USB_DEBUG
111 #define DPRINTF(x) if (usbdebug) kprintf x
112 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
113 int usbdebug = 0;
114 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
115 &usbdebug, 0, "usb debug level");
118 * 0 - do usual exploration
119 * 1 - do not use timeout exploration
120 * >1 - do no exploration
122 int usb_noexplore = 0;
123 #else
124 #define DPRINTF(x)
125 #define DPRINTFN(n,x)
126 #endif
128 struct usb_softc {
129 cdev_t sc_usbdev;
130 TAILQ_ENTRY(usb_softc) sc_coldexplist; /* cold needs-explore list */
131 usbd_bus_handle sc_bus; /* USB controller */
132 struct usbd_port sc_port; /* dummy port for root hub */
134 struct thread *sc_event_thread;
136 char sc_dying;
139 struct usb_taskq {
140 TAILQ_HEAD(, usb_task) tasks;
141 struct thread *task_thread_proc;
142 const char *name;
143 int taskcreated; /* task thread exists. */
145 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
147 d_open_t usbopen;
148 d_close_t usbclose;
149 d_read_t usbread;
150 d_ioctl_t usbioctl;
151 d_kqfilter_t usbkqfilter;
153 static void usbfilt_detach(struct knote *);
154 static int usbfilt(struct knote *, long);
156 static struct dev_ops usb_ops = {
157 { "usb", USB_CDEV_MAJOR, 0 },
158 .d_open = usbopen,
159 .d_close = usbclose,
160 .d_read = usbread,
161 .d_ioctl = usbioctl,
162 .d_kqfilter = usbkqfilter
165 static void usb_discover(device_t);
166 static bus_child_detached_t usb_child_detached;
167 static void usb_create_event_thread(device_t);
168 static void usb_event_thread(void *);
169 static void usb_task_thread(void *);
171 static cdev_t usb_dev; /* The /dev/usb device. */
172 static int usb_ndevs; /* Number of /dev/usbN devices. */
173 /* Busses to explore at the end of boot-time device configuration */
174 static TAILQ_HEAD(, usb_softc) usb_coldexplist =
175 TAILQ_HEAD_INITIALIZER(usb_coldexplist);
177 #define USB_MAX_EVENTS 100
178 struct usb_event_q {
179 struct usb_event ue;
180 TAILQ_ENTRY(usb_event_q) next;
182 static TAILQ_HEAD(, usb_event_q) usb_events =
183 TAILQ_HEAD_INITIALIZER(usb_events);
184 static int usb_nevents = 0;
185 static struct kqinfo usb_kqevent;
186 static struct proc *usb_async_proc; /* process that wants USB SIGIO */
187 static int usb_dev_open = 0;
188 static void usb_add_event(int, struct usb_event *);
190 static int usb_get_next_event(struct usb_event *);
192 static const char *usbrev_str[] = USBREV_STR;
194 static device_probe_t usb_match;
195 static device_attach_t usb_attach;
196 static device_detach_t usb_detach;
198 static devclass_t usb_devclass;
200 static kobj_method_t usb_methods[] = {
201 DEVMETHOD(device_probe, usb_match),
202 DEVMETHOD(device_attach, usb_attach),
203 DEVMETHOD(device_detach, usb_detach),
204 DEVMETHOD(bus_child_detached, usb_child_detached),
205 DEVMETHOD(device_suspend, bus_generic_suspend),
206 DEVMETHOD(device_resume, bus_generic_resume),
207 DEVMETHOD(device_shutdown, bus_generic_shutdown),
208 {0,0}
211 static driver_t usb_driver = {
212 "usb",
213 usb_methods,
214 sizeof(struct usb_softc)
217 MODULE_DEPEND(usb, usb, 1, 1, 1);
218 MODULE_VERSION(usb, 1);
220 static int
221 usb_match(device_t self)
223 DPRINTF(("usb_match\n"));
224 return (UMATCH_GENERIC);
227 static int
228 usb_attach(device_t self)
230 struct usb_softc *sc = device_get_softc(self);
231 void *aux = device_get_ivars(self);
232 cdev_t tmp_dev;
233 usbd_device_handle dev;
234 usbd_status err;
235 int usbrev;
236 int speed;
237 struct usb_event ue;
239 TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
240 &hack_defer_exploration);
242 DPRINTF(("usb_attach\n"));
244 usbd_init();
245 sc->sc_bus = aux;
246 sc->sc_bus->usbctl = sc;
247 sc->sc_port.power = USB_MAX_POWER;
249 usbrev = sc->sc_bus->usbrev;
250 device_printf(self, "USB revision %s", usbrev_str[usbrev]);
251 switch (usbrev) {
252 case USBREV_1_0:
253 case USBREV_1_1:
254 speed = USB_SPEED_FULL;
255 break;
256 case USBREV_2_0:
257 speed = USB_SPEED_HIGH;
258 break;
259 default:
260 kprintf(", not supported\n");
261 sc->sc_dying = 1;
262 return ENXIO;
264 kprintf("\n");
266 /* Make sure not to use tsleep() if we are cold booting. */
267 if (hack_defer_exploration && cold)
268 sc->sc_bus->use_polling++;
270 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
271 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
273 #ifdef USB_USE_SOFTINTR
274 callout_init(&sc->sc_bus->softi);
275 #endif
277 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0, &sc->sc_port);
278 if (!err) {
279 dev = sc->sc_port.device;
280 if (dev->hub == NULL) {
281 sc->sc_dying = 1;
282 device_printf(self,
283 "root device is not a hub\n");
284 return ENXIO;
286 sc->sc_bus->root_hub = dev;
287 #if 1
289 * Turning this code off will delay attachment of USB devices
290 * until the USB event thread is running, which means that
291 * the keyboard will not work until after cold boot.
293 if (cold) {
294 if (hack_defer_exploration) {
295 /* Explore high-speed busses before others. */
296 if (speed == USB_SPEED_HIGH)
297 dev->hub->explore(sc->sc_bus->root_hub);
298 else
299 TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
300 sc_coldexplist);
301 } else {
303 * XXX Exploring high speed devices here will
304 * hang the system.
306 if (speed != USB_SPEED_HIGH)
307 dev->hub->explore(sc->sc_bus->root_hub);
310 #endif
311 } else {
312 device_printf(self,
313 "root hub problem, error=%d\n", err);
314 sc->sc_dying = 1;
316 if (hack_defer_exploration && cold)
317 sc->sc_bus->use_polling--;
319 usb_create_event_thread(self);
322 * The per controller devices (used for usb_discover)
323 * XXX This is redundant now, but old usbd's will want it
325 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
326 UID_ROOT, GID_OPERATOR, 0660,
327 "usb%d", device_get_unit(self));
328 sc->sc_usbdev = reference_dev(tmp_dev);
329 if (usb_ndevs++ == 0) {
330 /* The device spitting out events */
331 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
332 UID_ROOT, GID_OPERATOR, 0660, "usb");
333 usb_dev = reference_dev(tmp_dev);
336 return 0;
339 static const char *taskq_names[] = USB_TASKQ_NAMES;
341 void
342 usb_create_event_thread(device_t self)
344 struct usb_softc *sc = device_get_softc(self);
345 int i;
347 if (kthread_create(usb_event_thread, self, &sc->sc_event_thread,
348 "%s", device_get_nameunit(self))) {
349 device_printf(self,
350 "unable to create event thread for\n");
351 panic("usb_create_event_thread");
354 for (i = 0; i < USB_NUM_TASKQS; i++) {
355 struct usb_taskq *taskq = &usb_taskq[i];
357 if (taskq->taskcreated == 0) {
358 taskq->taskcreated = 1;
359 taskq->name = taskq_names[i];
360 TAILQ_INIT(&taskq->tasks);
361 if (kthread_create(usb_task_thread, taskq,
362 &taskq->task_thread_proc, taskq->name)) {
363 kprintf("unable to create task thread\n");
364 panic("usb_create_event_thread task");
371 * Add a task to be performed by the task thread. This function can be
372 * called from any context and the task will be executed in a process
373 * context ASAP.
375 void
376 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
378 struct usb_taskq *taskq;
380 crit_enter();
382 taskq = &usb_taskq[queue];
383 if (task->queue == -1) {
384 DPRINTFN(2,("usb_add_task: task=%p\n", task));
385 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
386 task->queue = queue;
387 } else {
388 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
390 wakeup(&taskq->tasks);
392 crit_exit();
395 void
396 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
397 int time_out)
399 struct usb_taskq *taskq;
401 crit_enter();
403 taskq = &usb_taskq[queue];
404 if (task->queue == -1) {
405 DPRINTFN(2,("usb_add_task: task=%p\n", task));
406 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
407 task->queue = queue;
408 } else {
409 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
411 wakeup(&taskq->tasks);
413 /* Wait until task is finished */
414 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
416 crit_exit();
419 void
420 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
422 crit_enter();
423 if (task->queue != -1) {
424 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
425 task->queue = -1;
427 crit_exit();
430 void
431 usb_event_thread(void *arg)
433 device_t self = arg;
434 struct usb_softc *sc = device_get_softc(self);
436 DPRINTF(("usb_event_thread: start\n"));
439 * In case this controller is a companion controller to an
440 * EHCI controller we need to wait until the EHCI controller
441 * has grabbed the port.
442 * XXX It would be nicer to do this with a tsleep(), but I don't
443 * know how to synchronize the creation of the threads so it
444 * will work.
446 usb_delay_ms(sc->sc_bus, 500);
448 get_mplock();
449 crit_enter();
451 /* Make sure first discover does something. */
452 sc->sc_bus->needs_explore = 1;
453 usb_discover(self);
455 while (!sc->sc_dying) {
456 #ifdef USB_DEBUG
457 if (usb_noexplore < 2)
458 #endif
459 usb_discover(self);
460 #ifdef USB_DEBUG
461 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
462 usb_noexplore ? 0 : hz * 60);
463 #else
464 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
465 #endif
466 DPRINTFN(2,("usb_event_thread: woke up\n"));
468 sc->sc_event_thread = NULL;
470 crit_exit();
471 rel_mplock();
473 /* In case parent is waiting for us to exit. */
474 wakeup(sc);
476 DPRINTF(("usb_event_thread: exit\n"));
479 void
480 usb_task_thread(void *arg)
482 struct usb_task *task;
483 struct usb_taskq *taskq;
485 get_mplock();
486 crit_enter();
488 taskq = arg;
489 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
491 while (usb_ndevs > 0) {
492 task = TAILQ_FIRST(&taskq->tasks);
493 if (task == NULL) {
494 tsleep(&taskq->tasks, 0, "usbtsk", 0);
495 task = TAILQ_FIRST(&taskq->tasks);
497 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
498 if (task != NULL) {
499 TAILQ_REMOVE(&taskq->tasks, task, next);
500 task->queue = -1;
501 crit_exit();
502 task->fun(task->arg);
503 crit_enter();
504 wakeup((&taskq->tasks + 1));
508 crit_exit();
509 rel_mplock();
511 taskq->taskcreated = 0;
512 wakeup(&taskq->taskcreated);
514 DPRINTF(("usb_event_thread: exit\n"));
518 usbopen(struct dev_open_args *ap)
520 cdev_t dev = ap->a_head.a_dev;
521 int unit = USBUNIT(dev);
522 struct usb_softc *sc;
524 if (unit == USB_DEV_MINOR) {
525 if (usb_dev_open)
526 return (EBUSY);
527 usb_dev_open = 1;
528 usb_async_proc = NULL;
529 return (0);
532 sc = devclass_get_softc(usb_devclass, unit);
533 if (sc == NULL)
534 return (ENXIO);
536 if (sc->sc_dying)
537 return (EIO);
539 return (0);
543 usbread(struct dev_read_args *ap)
545 cdev_t dev = ap->a_head.a_dev;
546 struct uio *uio = ap->a_uio;
547 struct usb_event ue;
548 int unit = USBUNIT(dev);
549 int error, n;
551 if (unit != USB_DEV_MINOR)
552 return (ENODEV);
554 if (uio->uio_resid != sizeof(struct usb_event))
555 return (EINVAL);
557 error = 0;
558 crit_enter();
559 for (;;) {
560 n = usb_get_next_event(&ue);
561 if (n != 0)
562 break;
563 if (ap->a_ioflag & IO_NDELAY) {
564 error = EWOULDBLOCK;
565 break;
567 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
568 if (error)
569 break;
571 crit_exit();
572 if (!error)
573 error = uiomove((void *)&ue, uio->uio_resid, uio);
575 return (error);
579 usbclose(struct dev_close_args *ap)
581 cdev_t dev = ap->a_head.a_dev;
582 int unit = USBUNIT(dev);
584 if (unit == USB_DEV_MINOR) {
585 usb_async_proc = NULL;
586 usb_dev_open = 0;
589 return (0);
593 usbioctl(struct dev_ioctl_args *ap)
595 cdev_t devt = ap->a_head.a_dev;
596 struct usb_softc *sc;
597 int unit = USBUNIT(devt);
599 if (unit == USB_DEV_MINOR) {
600 switch (ap->a_cmd) {
601 case FIOASYNC:
602 if (*(int *)ap->a_data)
603 usb_async_proc = curproc;
604 else
605 usb_async_proc = NULL;
606 return (0);
608 default:
609 return (EINVAL);
613 sc = devclass_get_softc(usb_devclass, unit);
615 if (sc->sc_dying)
616 return (EIO);
618 switch (ap->a_cmd) {
619 /* This part should be deleted */
620 case USB_DISCOVER:
621 break;
622 case USB_REQUEST:
624 struct usb_ctl_request *ur = (void *)ap->a_data;
625 size_t len = UGETW(ur->ucr_request.wLength);
626 struct iovec iov;
627 struct uio uio;
628 void *ptr = 0;
629 int addr = ur->ucr_addr;
630 usbd_status err;
631 int error = 0;
633 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%zu\n", addr, len));
634 if (len > 32768)
635 return (EINVAL);
636 if (addr < 0 || addr >= USB_MAX_DEVICES ||
637 sc->sc_bus->devices[addr] == 0)
638 return (EINVAL);
639 if (len != 0) {
640 iov.iov_base = (caddr_t)ur->ucr_data;
641 iov.iov_len = len;
642 uio.uio_iov = &iov;
643 uio.uio_iovcnt = 1;
644 uio.uio_resid = len;
645 uio.uio_offset = 0;
646 uio.uio_segflg = UIO_USERSPACE;
647 uio.uio_rw =
648 ur->ucr_request.bmRequestType & UT_READ ?
649 UIO_READ : UIO_WRITE;
650 uio.uio_td = curthread;
651 ptr = kmalloc(len, M_TEMP, M_WAITOK);
652 if (uio.uio_rw == UIO_WRITE) {
653 error = uiomove(ptr, len, &uio);
654 if (error)
655 goto ret;
658 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
659 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
660 USBD_DEFAULT_TIMEOUT);
661 if (err) {
662 error = EIO;
663 goto ret;
665 if (len != 0) {
666 if (uio.uio_rw == UIO_READ) {
667 error = uiomove(ptr, len, &uio);
668 if (error)
669 goto ret;
672 ret:
673 if (ptr)
674 kfree(ptr, M_TEMP);
675 return (error);
678 case USB_DEVICEINFO:
680 struct usb_device_info *di = (void *)ap->a_data;
681 int addr = di->udi_addr;
682 usbd_device_handle dev;
684 if (addr < 1 || addr >= USB_MAX_DEVICES)
685 return (EINVAL);
686 dev = sc->sc_bus->devices[addr];
687 if (dev == NULL)
688 return (ENXIO);
689 usbd_fill_deviceinfo(dev, di, 1);
690 break;
693 case USB_DEVICESTATS:
694 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
695 break;
697 default:
698 return (EINVAL);
700 return (0);
703 static struct filterops usbfiltops =
704 { FILTEROP_ISFD, NULL, usbfilt_detach, usbfilt };
707 usbkqfilter(struct dev_kqfilter_args *ap)
709 cdev_t dev = ap->a_head.a_dev;
710 struct knote *kn = ap->a_kn;
711 struct klist *klist;
713 ap->a_result = 0;
715 switch (kn->kn_filter) {
716 case EVFILT_READ:
717 kn->kn_fop = &usbfiltops;
718 kn->kn_hook = (caddr_t)dev;
719 break;
720 default:
721 ap->a_result = EOPNOTSUPP;
722 return (0);
725 klist = &usb_kqevent.ki_note;
726 knote_insert(klist, kn);
728 return (0);
731 static void
732 usbfilt_detach(struct knote *kn)
734 struct klist *klist;
736 klist = &usb_kqevent.ki_note;
737 knote_remove(klist, kn);
740 static int
741 usbfilt(struct knote *kn, long hint)
743 cdev_t dev = (cdev_t)kn->kn_hook;
744 int unit = USBUNIT(dev);
745 int ready = 0;
747 if (unit == USB_DEV_MINOR) {
748 crit_enter();
749 if (usb_nevents > 0)
750 ready = 1;
751 crit_exit();
754 return (ready);
757 /* Explore device tree from the root. */
758 static void
759 usb_discover(device_t self)
761 struct usb_softc *sc = device_get_softc(self);
763 DPRINTFN(2,("usb_discover\n"));
764 #ifdef USB_DEBUG
765 if (usb_noexplore > 1)
766 return;
767 #endif
770 * We need mutual exclusion while traversing the device tree,
771 * but this is guaranteed since this function is only called
772 * from the event thread for the controller.
774 crit_enter();
775 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
776 sc->sc_bus->needs_explore = 0;
778 crit_exit();
779 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
780 crit_enter();
783 crit_exit();
786 void
787 usb_needs_explore(usbd_device_handle dev)
789 DPRINTFN(2,("usb_needs_explore\n"));
790 dev->bus->needs_explore = 1;
791 wakeup(&dev->bus->needs_explore);
794 /* Called from a critical section */
796 usb_get_next_event(struct usb_event *ue)
798 struct usb_event_q *ueq;
800 if (usb_nevents <= 0)
801 return (0);
802 ueq = TAILQ_FIRST(&usb_events);
803 #ifdef DIAGNOSTIC
804 if (ueq == NULL) {
805 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
806 usb_nevents = 0;
807 return (0);
809 #endif
810 if (ue)
811 *ue = ueq->ue;
812 TAILQ_REMOVE(&usb_events, ueq, next);
813 kfree(ueq, M_USBDEV);
814 usb_nevents--;
815 return (1);
818 void
819 usbd_add_dev_event(int type, usbd_device_handle udev)
821 struct usb_event ue;
823 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
824 usb_add_event(type, &ue);
827 void
828 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
830 struct usb_event ue;
832 ue.u.ue_driver.ue_cookie = udev->cookie;
833 strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
834 sizeof ue.u.ue_driver.ue_devname);
835 usb_add_event(type, &ue);
838 void
839 usb_add_event(int type, struct usb_event *uep)
841 struct usb_event_q *ueq;
842 struct timeval thetime;
844 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
845 ueq->ue = *uep;
846 ueq->ue.ue_type = type;
847 microtime(&thetime);
848 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
850 crit_enter();
851 if (USB_EVENT_IS_DETACH(type)) {
852 struct usb_event_q *ueqi, *ueqi_next;
854 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
855 ueqi_next = TAILQ_NEXT(ueqi, next);
856 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
857 uep->u.ue_device.udi_cookie.cookie) {
858 TAILQ_REMOVE(&usb_events, ueqi, next);
859 kfree(ueqi, M_USBDEV);
860 usb_nevents--;
861 ueqi_next = TAILQ_FIRST(&usb_events);
865 if (usb_nevents >= USB_MAX_EVENTS) {
866 /* Too many queued events, drop an old one. */
867 DPRINTF(("usb: event dropped\n"));
868 usb_get_next_event(NULL);
870 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
871 usb_nevents++;
872 wakeup(&usb_events);
873 KNOTE(&usb_kqevent.ki_note, 0);
874 if (usb_async_proc != NULL) {
875 ksignal(usb_async_proc, SIGIO);
877 crit_exit();
880 void
881 usb_schedsoftintr(usbd_bus_handle bus)
883 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
884 #ifdef USB_USE_SOFTINTR
885 if (bus->use_polling) {
886 bus->methods->soft_intr(bus);
887 } else {
888 if (!callout_pending(&bus->softi))
889 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
890 bus);
892 #else
893 bus->methods->soft_intr(bus);
894 #endif /* USB_USE_SOFTINTR */
897 static int
898 usb_detach(device_t self)
900 struct usb_softc *sc = device_get_softc(self);
901 struct usb_event ue;
903 DPRINTF(("usb_detach: start\n"));
905 sc->sc_dying = 1;
907 /* Make all devices disconnect. */
908 if (sc->sc_port.device != NULL)
909 usb_disconnect_port(&sc->sc_port, self);
911 /* Kill off event thread. */
912 if (sc->sc_event_thread != NULL) {
913 wakeup(&sc->sc_bus->needs_explore);
914 if (tsleep(sc, 0, "usbdet", hz * 60))
915 device_printf(self,
916 "event thread didn't die\n");
917 DPRINTF(("usb_detach: event thread dead\n"));
920 release_dev(sc->sc_usbdev);
922 if (--usb_ndevs == 0) {
923 int i;
925 release_dev(usb_dev);
926 dev_ops_remove_minor(&usb_ops, USB_DEV_MINOR);
927 usb_dev = NULL;
929 for (i = 0; i < USB_NUM_TASKQS; i++) {
930 struct usb_taskq *taskq = &usb_taskq[i];
931 wakeup(&taskq->tasks);
932 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
933 hz * 60)) {
934 kprintf("usb task thread %s didn't die\n",
935 taskq->name);
940 usbd_finish();
942 #ifdef USB_USE_SOFTINTR
943 callout_stop(&sc->sc_bus->softi);
944 #endif
946 ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
947 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
949 return (0);
952 static void
953 usb_child_detached(device_t self, device_t child)
955 struct usb_softc *sc = device_get_softc(self);
957 /* XXX, should check it is the right device. */
958 sc->sc_port.device = NULL;
961 /* Explore USB busses at the end of device configuration */
962 static void
963 usb_cold_explore(void *arg)
965 struct usb_softc *sc;
967 TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
968 &hack_defer_exploration);
970 if (!hack_defer_exploration)
971 return;
973 KASSERT(cold || TAILQ_EMPTY(&usb_coldexplist),
974 ("usb_cold_explore: busses to explore when !cold"));
975 while (!TAILQ_EMPTY(&usb_coldexplist)) {
976 sc = TAILQ_FIRST(&usb_coldexplist);
977 TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
979 sc->sc_bus->use_polling++;
980 sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
981 sc->sc_bus->use_polling--;
985 struct usbd_bus *
986 usb_getbushandle(struct usb_softc *sc)
988 return (sc->sc_bus);
992 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
993 usb_cold_explore, NULL);
995 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
996 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
997 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);