Merge remote branch 'crater/master' into kq_devices
[dragonfly.git] / sys / bus / usb / usb.c
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  */
6
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 $
12  */
13
14 /*
15  * Copyright (c) 1998 The NetBSD Foundation, Inc.
16  * All rights reserved.
17  *
18  * This code is derived from software contributed to The NetBSD Foundation
19  * by Lennart Augustsson (lennart@augustsson.net) at
20  * Carlstedt Research & Technology.
21  *
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.
37  *
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.
49  */
50
51 /*
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/
55  */
56
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/event.h>
74 #include <sys/select.h>
75 #include <sys/vnode.h>
76 #include <sys/signalvar.h>
77 #include <sys/sysctl.h>
78 #include <sys/thread2.h>
79
80 #include <bus/usb/usb.h>
81 #include <bus/usb/usbdi.h>
82 #include <bus/usb/usbdi_util.h>
83
84 #define USBUNIT(d)      (minor(d))      /* usb_discover device nodes, kthread */
85 #define USB_DEV_MINOR   255             /* event queue device */
86
87 MALLOC_DEFINE(M_USB, "USB", "USB");
88 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
89 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
90
91 #include "usb_if.h"
92
93 #include <bus/usb/usbdivar.h>
94 #include <bus/usb/usb_quirks.h>
95
96 /* Define this unconditionally in case a kernel module is loaded that
97  * has been compiled with debugging options.
98  */
99 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
100
101 /*
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:     
105  *
106  *      set hw.usb.hack_defer_exploration=0
107  */ 
108 static int      hack_defer_exploration = 1;
109
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");
116
117 /*
118  * 0  - do usual exploration
119  * 1  - do not use timeout exploration
120  * >1 - do no exploration
121  */
122 int     usb_noexplore = 0;
123 #else
124 #define DPRINTF(x)
125 #define DPRINTFN(n,x)
126 #endif
127
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 */
133
134         struct thread   *sc_event_thread;
135
136         char            sc_dying;
137 };
138
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. */
144 };
145 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
146
147 d_open_t  usbopen;
148 d_close_t usbclose;
149 d_read_t usbread;
150 d_ioctl_t usbioctl;
151 d_poll_t usbpoll;
152 d_kqfilter_t usbkqfilter;
153
154 static void usbfilt_detach(struct knote *);
155 static int usbfilt(struct knote *, long);
156
157 struct dev_ops usb_ops = {
158         { "usb", USB_CDEV_MAJOR, D_KQFILTER },
159         .d_open =       usbopen,
160         .d_close =      usbclose,
161         .d_read =       usbread,
162         .d_ioctl =      usbioctl,
163         .d_poll =       usbpoll,
164         .d_kqfilter =   usbkqfilter
165 };
166
167 static void     usb_discover(device_t);
168 static bus_child_detached_t usb_child_detached;
169 static void     usb_create_event_thread(device_t);
170 static void     usb_event_thread(void *);
171 static void     usb_task_thread(void *);
172
173 static cdev_t usb_dev;          /* The /dev/usb device. */
174 static int usb_ndevs;           /* Number of /dev/usbN devices. */
175 /* Busses to explore at the end of boot-time device configuration */
176 static TAILQ_HEAD(, usb_softc) usb_coldexplist =
177     TAILQ_HEAD_INITIALIZER(usb_coldexplist);
178  
179 #define USB_MAX_EVENTS 100
180 struct usb_event_q {
181         struct usb_event ue;
182         TAILQ_ENTRY(usb_event_q) next;
183 };
184 static TAILQ_HEAD(, usb_event_q) usb_events =
185         TAILQ_HEAD_INITIALIZER(usb_events);
186 static int usb_nevents = 0;
187 static struct selinfo usb_selevent;
188 static struct proc *usb_async_proc;  /* process that wants USB SIGIO */
189 static int usb_dev_open = 0;
190 static void usb_add_event(int, struct usb_event *);
191
192 static int usb_get_next_event(struct usb_event *);
193
194 static const char *usbrev_str[] = USBREV_STR;
195
196 static device_probe_t usb_match;
197 static device_attach_t usb_attach;
198 static device_detach_t usb_detach;
199
200 static devclass_t usb_devclass;
201
202 static kobj_method_t usb_methods[] = {
203         DEVMETHOD(device_probe, usb_match),
204         DEVMETHOD(device_attach, usb_attach),
205         DEVMETHOD(device_detach, usb_detach),
206         DEVMETHOD(bus_child_detached, usb_child_detached),
207         DEVMETHOD(device_suspend, bus_generic_suspend),
208         DEVMETHOD(device_resume, bus_generic_resume),
209         DEVMETHOD(device_shutdown, bus_generic_shutdown),
210         {0,0}
211 };
212
213 static driver_t usb_driver = {
214         "usb",
215         usb_methods,
216         sizeof(struct usb_softc)
217 };
218
219 MODULE_DEPEND(usb, usb, 1, 1, 1);
220 MODULE_VERSION(usb, 1);
221
222 static int
223 usb_match(device_t self)
224 {
225         DPRINTF(("usb_match\n"));
226         return (UMATCH_GENERIC);
227 }
228
229 static int
230 usb_attach(device_t self)
231 {
232         struct usb_softc *sc = device_get_softc(self);
233         void *aux = device_get_ivars(self);
234         cdev_t tmp_dev;
235         usbd_device_handle dev;
236         usbd_status err;
237         int usbrev;
238         int speed;
239         struct usb_event ue;
240
241         TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
242             &hack_defer_exploration);
243
244         DPRINTF(("usb_attach\n"));
245
246         usbd_init();
247         sc->sc_bus = aux;
248         sc->sc_bus->usbctl = sc;
249         sc->sc_port.power = USB_MAX_POWER;
250
251         usbrev = sc->sc_bus->usbrev;
252         device_printf(self, "USB revision %s", usbrev_str[usbrev]);
253         switch (usbrev) {
254         case USBREV_1_0:
255         case USBREV_1_1:
256                 speed = USB_SPEED_FULL;
257                 break;
258         case USBREV_2_0:
259                 speed = USB_SPEED_HIGH;
260                 break;
261         default:
262                 kprintf(", not supported\n");
263                 sc->sc_dying = 1;
264                 return ENXIO;
265         }
266         kprintf("\n");
267
268         /* Make sure not to use tsleep() if we are cold booting. */
269         if (hack_defer_exploration && cold)
270                 sc->sc_bus->use_polling++;
271
272         ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
273         usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
274
275 #ifdef USB_USE_SOFTINTR
276         callout_init(&sc->sc_bus->softi);
277 #endif
278
279         err = usbd_new_device(self, sc->sc_bus, 0, speed, 0, &sc->sc_port);
280         if (!err) {
281                 dev = sc->sc_port.device;
282                 if (dev->hub == NULL) {
283                         sc->sc_dying = 1;
284                         device_printf(self,
285                             "root device is not a hub\n");
286                         return ENXIO;
287                 }
288                 sc->sc_bus->root_hub = dev;
289 #if 1
290                 /*
291                  * Turning this code off will delay attachment of USB devices
292                  * until the USB event thread is running, which means that
293                  * the keyboard will not work until after cold boot.
294                  */
295                 if (cold) {
296                         if (hack_defer_exploration) {
297                                 /* Explore high-speed busses before others. */
298                                 if (speed == USB_SPEED_HIGH)
299                                         dev->hub->explore(sc->sc_bus->root_hub);
300                                 else
301                                         TAILQ_INSERT_TAIL(&usb_coldexplist, sc,
302                                             sc_coldexplist);
303                         } else {
304                                 /*
305                                  * XXX Exploring high speed devices here will
306                                  * hang the system.
307                                  */
308                                 if (speed != USB_SPEED_HIGH)
309                                         dev->hub->explore(sc->sc_bus->root_hub);
310                         }
311                 }
312 #endif
313         } else {
314                 device_printf(self,
315                     "root hub problem, error=%d\n", err);
316                 sc->sc_dying = 1;
317         }
318         if (hack_defer_exploration && cold)
319                 sc->sc_bus->use_polling--;
320
321         usb_create_event_thread(self);
322
323         /*
324          * The per controller devices (used for usb_discover)
325          * XXX This is redundant now, but old usbd's will want it
326          */
327         tmp_dev = make_dev(&usb_ops, device_get_unit(self),
328                            UID_ROOT, GID_OPERATOR, 0660,
329                            "usb%d", device_get_unit(self));
330         sc->sc_usbdev = reference_dev(tmp_dev);
331         if (usb_ndevs++ == 0) {
332                 /* The device spitting out events */
333                 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
334                                    UID_ROOT, GID_OPERATOR, 0660, "usb");
335                 usb_dev = reference_dev(tmp_dev);
336         }
337
338         return 0;
339 }
340
341 static const char *taskq_names[] = USB_TASKQ_NAMES;
342
343 void
344 usb_create_event_thread(device_t self)
345 {
346         struct usb_softc *sc = device_get_softc(self);
347         int i;
348
349         if (kthread_create(usb_event_thread, self, &sc->sc_event_thread,
350                            "%s", device_get_nameunit(self))) {
351                 device_printf(self,
352                     "unable to create event thread for\n");
353                 panic("usb_create_event_thread");
354         }
355
356         for (i = 0; i < USB_NUM_TASKQS; i++) {
357                 struct usb_taskq *taskq = &usb_taskq[i];
358
359                 if (taskq->taskcreated == 0) {
360                         taskq->taskcreated = 1;
361                         taskq->name = taskq_names[i];
362                         TAILQ_INIT(&taskq->tasks);
363                         if (kthread_create(usb_task_thread, taskq,
364                             &taskq->task_thread_proc, taskq->name)) {
365                                 kprintf("unable to create task thread\n");
366                                 panic("usb_create_event_thread task");
367                         }
368                 }
369         }
370 }
371
372 /*
373  * Add a task to be performed by the task thread.  This function can be
374  * called from any context and the task will be executed in a process
375  * context ASAP.
376  */
377 void
378 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
379 {
380         struct usb_taskq *taskq;
381
382         crit_enter();
383
384         taskq = &usb_taskq[queue];
385         if (task->queue == -1) {
386                 DPRINTFN(2,("usb_add_task: task=%p\n", task));
387                 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
388                 task->queue = queue;
389         } else {
390                 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
391         }
392         wakeup(&taskq->tasks);
393
394         crit_exit();
395 }
396
397 void
398 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
399             int time_out)
400 {
401         struct usb_taskq *taskq;
402
403         crit_enter();
404
405         taskq = &usb_taskq[queue];
406         if (task->queue == -1) {
407                 DPRINTFN(2,("usb_add_task: task=%p\n", task));
408                 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
409                 task->queue = queue;
410         } else {
411                 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
412         }
413         wakeup(&taskq->tasks);
414
415         /* Wait until task is finished */
416         tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
417
418         crit_exit();
419 }
420
421 void
422 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
423 {
424         crit_enter();
425         if (task->queue != -1) {
426                 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
427                 task->queue = -1;
428         }
429         crit_exit();
430 }
431
432 void
433 usb_event_thread(void *arg)
434 {
435         device_t self = arg;
436         struct usb_softc *sc = device_get_softc(self);
437
438         DPRINTF(("usb_event_thread: start\n"));
439
440         /*
441          * In case this controller is a companion controller to an
442          * EHCI controller we need to wait until the EHCI controller
443          * has grabbed the port.
444          * XXX It would be nicer to do this with a tsleep(), but I don't
445          * know how to synchronize the creation of the threads so it
446          * will work.
447          */
448         usb_delay_ms(sc->sc_bus, 500);
449
450         crit_enter();
451
452         /* Make sure first discover does something. */
453         sc->sc_bus->needs_explore = 1;
454         usb_discover(self);
455
456         while (!sc->sc_dying) {
457 #ifdef USB_DEBUG
458                 if (usb_noexplore < 2)
459 #endif
460                 usb_discover(self);
461 #ifdef USB_DEBUG
462                 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
463                        usb_noexplore ? 0 : hz * 60);
464 #else
465                 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
466 #endif
467                 DPRINTFN(2,("usb_event_thread: woke up\n"));
468         }
469         sc->sc_event_thread = NULL;
470
471         crit_exit();
472
473         /* In case parent is waiting for us to exit. */
474         wakeup(sc);
475
476         DPRINTF(("usb_event_thread: exit\n"));
477         kthread_exit();
478 }
479
480 void
481 usb_task_thread(void *arg)
482 {
483         struct usb_task *task;
484         struct usb_taskq *taskq;
485
486         crit_enter();
487
488         taskq = arg;
489         DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
490
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);
496                 }
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));
505                 }
506         }
507
508         crit_exit();
509
510         taskq->taskcreated = 0;
511         wakeup(&taskq->taskcreated);
512
513         DPRINTF(("usb_event_thread: exit\n"));
514 }
515
516 int
517 usbopen(struct dev_open_args *ap)
518 {
519         cdev_t dev = ap->a_head.a_dev;
520         int unit = USBUNIT(dev);
521         struct usb_softc *sc;
522
523         if (unit == USB_DEV_MINOR) {
524                 if (usb_dev_open)
525                         return (EBUSY);
526                 usb_dev_open = 1;
527                 usb_async_proc = NULL;
528                 return (0);
529         }
530
531         sc = devclass_get_softc(usb_devclass, unit);
532         if (sc == NULL)
533                 return (ENXIO);
534
535         if (sc->sc_dying)
536                 return (EIO);
537
538         return (0);
539 }
540
541 int
542 usbread(struct dev_read_args *ap)
543 {
544         cdev_t dev = ap->a_head.a_dev;
545         struct uio *uio = ap->a_uio;
546         struct usb_event ue;
547         int unit = USBUNIT(dev);
548         int error, n;
549
550         if (unit != USB_DEV_MINOR)
551                 return (ENODEV);
552
553         if (uio->uio_resid != sizeof(struct usb_event))
554                 return (EINVAL);
555
556         error = 0;
557         crit_enter();
558         for (;;) {
559                 n = usb_get_next_event(&ue);
560                 if (n != 0)
561                         break;
562                 if (ap->a_ioflag & IO_NDELAY) {
563                         error = EWOULDBLOCK;
564                         break;
565                 }
566                 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
567                 if (error)
568                         break;
569         }
570         crit_exit();
571         if (!error)
572                 error = uiomove((void *)&ue, uio->uio_resid, uio);
573
574         return (error);
575 }
576
577 int
578 usbclose(struct dev_close_args *ap)
579 {
580         cdev_t dev = ap->a_head.a_dev;
581         int unit = USBUNIT(dev);
582
583         if (unit == USB_DEV_MINOR) {
584                 usb_async_proc = NULL;
585                 usb_dev_open = 0;
586         }
587
588         return (0);
589 }
590
591 int
592 usbioctl(struct dev_ioctl_args *ap)
593 {
594         cdev_t devt = ap->a_head.a_dev;
595         struct usb_softc *sc;
596         int unit = USBUNIT(devt);
597
598         if (unit == USB_DEV_MINOR) {
599                 switch (ap->a_cmd) {
600                 case FIOASYNC:
601                         if (*(int *)ap->a_data)
602                                 usb_async_proc = curproc;
603                         else
604                                 usb_async_proc = NULL;
605                         return (0);
606
607                 default:
608                         return (EINVAL);
609                 }
610         }
611
612         sc = devclass_get_softc(usb_devclass, unit);
613
614         if (sc->sc_dying)
615                 return (EIO);
616
617         switch (ap->a_cmd) {
618         /* This part should be deleted */
619         case USB_DISCOVER:
620                 break;
621         case USB_REQUEST:
622         {
623                 struct usb_ctl_request *ur = (void *)ap->a_data;
624                 size_t len = UGETW(ur->ucr_request.wLength);
625                 struct iovec iov;
626                 struct uio uio;
627                 void *ptr = 0;
628                 int addr = ur->ucr_addr;
629                 usbd_status err;
630                 int error = 0;
631
632                 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%zu\n", addr, len));
633                 if (len > 32768)
634                         return (EINVAL);
635                 if (addr < 0 || addr >= USB_MAX_DEVICES ||
636                     sc->sc_bus->devices[addr] == 0)
637                         return (EINVAL);
638                 if (len != 0) {
639                         iov.iov_base = (caddr_t)ur->ucr_data;
640                         iov.iov_len = len;
641                         uio.uio_iov = &iov;
642                         uio.uio_iovcnt = 1;
643                         uio.uio_resid = len;
644                         uio.uio_offset = 0;
645                         uio.uio_segflg = UIO_USERSPACE;
646                         uio.uio_rw =
647                                 ur->ucr_request.bmRequestType & UT_READ ?
648                                 UIO_READ : UIO_WRITE;
649                         uio.uio_td = curthread;
650                         ptr = kmalloc(len, M_TEMP, M_WAITOK);
651                         if (uio.uio_rw == UIO_WRITE) {
652                                 error = uiomove(ptr, len, &uio);
653                                 if (error)
654                                         goto ret;
655                         }
656                 }
657                 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
658                           &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
659                           USBD_DEFAULT_TIMEOUT);
660                 if (err) {
661                         error = EIO;
662                         goto ret;
663                 }
664                 if (len != 0) {
665                         if (uio.uio_rw == UIO_READ) {
666                                 error = uiomove(ptr, len, &uio);
667                                 if (error)
668                                         goto ret;
669                         }
670                 }
671         ret:
672                 if (ptr)
673                         kfree(ptr, M_TEMP);
674                 return (error);
675         }
676
677         case USB_DEVICEINFO:
678         {
679                 struct usb_device_info *di = (void *)ap->a_data;
680                 int addr = di->udi_addr;
681                 usbd_device_handle dev;
682
683                 if (addr < 1 || addr >= USB_MAX_DEVICES)
684                         return (EINVAL);
685                 dev = sc->sc_bus->devices[addr];
686                 if (dev == NULL)
687                         return (ENXIO);
688                 usbd_fill_deviceinfo(dev, di, 1);
689                 break;
690         }
691
692         case USB_DEVICESTATS:
693                 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
694                 break;
695
696         default:
697                 return (EINVAL);
698         }
699         return (0);
700 }
701
702 int
703 usbpoll(struct dev_poll_args *ap)
704 {
705         cdev_t dev = ap->a_head.a_dev;
706         int revents, mask;
707         int unit = USBUNIT(dev);
708
709         if (unit == USB_DEV_MINOR) {
710                 revents = 0;
711                 mask = POLLIN | POLLRDNORM;
712
713                 crit_enter();
714                 if (ap->a_events & mask && usb_nevents > 0)
715                         revents |= ap->a_events & mask;
716                 if (revents == 0 && ap->a_events & mask)
717                         selrecord(curthread, &usb_selevent);
718                 crit_exit();
719                 ap->a_events = revents;
720                 return (0);
721         } else {
722                 ap->a_events = 0;
723                 return (0);     /* select/poll never wakes up - back compat */
724         }
725 }
726
727 static struct filterops usbfiltops =
728         { 1, NULL, usbfilt_detach, usbfilt };
729
730 int
731 usbkqfilter(struct dev_kqfilter_args *ap)
732 {
733         cdev_t dev = ap->a_head.a_dev;
734         struct knote *kn = ap->a_kn;
735         struct klist *klist;
736
737         ap->a_result = 0;
738
739         switch (kn->kn_filter) {
740         case EVFILT_READ:
741                 kn->kn_fop = &usbfiltops;
742                 kn->kn_hook = (caddr_t)dev;
743                 break;
744         default:
745                 ap->a_result = 1;
746                 return (0);
747         }
748
749         crit_enter();
750         klist = &usb_selevent.si_note;
751         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
752         crit_exit();
753
754         return (0);
755 }
756
757 static void
758 usbfilt_detach(struct knote *kn)
759 {
760         struct klist *klist;
761
762         crit_enter();
763         klist = &usb_selevent.si_note;
764         SLIST_REMOVE(klist, kn, knote, kn_selnext);
765         crit_exit();
766 }
767
768 static int
769 usbfilt(struct knote *kn, long hint)
770 {
771         cdev_t dev = (cdev_t)kn->kn_hook;
772         int unit = USBUNIT(dev);
773         int ready = 0;
774
775         if (unit == USB_DEV_MINOR) {
776                 crit_enter();
777                 if (usb_nevents > 0)
778                         ready = 1;
779                 crit_exit();
780         }
781
782         return (ready);
783 }
784
785 /* Explore device tree from the root. */
786 static void
787 usb_discover(device_t self)
788 {
789         struct usb_softc *sc = device_get_softc(self);
790
791         DPRINTFN(2,("usb_discover\n"));
792 #ifdef USB_DEBUG
793         if (usb_noexplore > 1)
794                 return;
795 #endif
796
797         /*
798          * We need mutual exclusion while traversing the device tree,
799          * but this is guaranteed since this function is only called
800          * from the event thread for the controller.
801          */
802         crit_enter();
803         while (sc->sc_bus->needs_explore && !sc->sc_dying) {
804                 sc->sc_bus->needs_explore = 0;
805
806                 crit_exit();
807                 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
808                 crit_enter();
809
810         }
811         crit_exit();
812 }
813
814 void
815 usb_needs_explore(usbd_device_handle dev)
816 {
817         DPRINTFN(2,("usb_needs_explore\n"));
818         dev->bus->needs_explore = 1;
819         wakeup(&dev->bus->needs_explore);
820 }
821
822 /* Called from a critical section */
823 int
824 usb_get_next_event(struct usb_event *ue)
825 {
826         struct usb_event_q *ueq;
827
828         if (usb_nevents <= 0)
829                 return (0);
830         ueq = TAILQ_FIRST(&usb_events);
831 #ifdef DIAGNOSTIC
832         if (ueq == NULL) {
833                 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
834                 usb_nevents = 0;
835                 return (0);
836         }
837 #endif
838         if (ue)
839                 *ue = ueq->ue;
840         TAILQ_REMOVE(&usb_events, ueq, next);
841         kfree(ueq, M_USBDEV);
842         usb_nevents--;
843         return (1);
844 }
845
846 void
847 usbd_add_dev_event(int type, usbd_device_handle udev)
848 {
849         struct usb_event ue;
850
851         usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
852         usb_add_event(type, &ue);
853 }
854
855 void
856 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
857 {
858         struct usb_event ue;
859
860         ue.u.ue_driver.ue_cookie = udev->cookie;
861         strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
862                 sizeof ue.u.ue_driver.ue_devname);
863         usb_add_event(type, &ue);
864 }
865
866 void
867 usb_add_event(int type, struct usb_event *uep)
868 {
869         struct usb_event_q *ueq;
870         struct timeval thetime;
871
872         ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
873         ueq->ue = *uep;
874         ueq->ue.ue_type = type;
875         microtime(&thetime);
876         TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
877
878         crit_enter();
879         if (USB_EVENT_IS_DETACH(type)) {
880                 struct usb_event_q *ueqi, *ueqi_next;
881
882                 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
883                         ueqi_next = TAILQ_NEXT(ueqi, next);
884                         if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
885                             uep->u.ue_device.udi_cookie.cookie) {
886                                 TAILQ_REMOVE(&usb_events, ueqi, next);
887                                 kfree(ueqi, M_USBDEV);
888                                 usb_nevents--;
889                                 ueqi_next = TAILQ_FIRST(&usb_events);
890                         }
891                 }
892         }
893         if (usb_nevents >= USB_MAX_EVENTS) {
894                 /* Too many queued events, drop an old one. */
895                 DPRINTF(("usb: event dropped\n"));
896                 usb_get_next_event(NULL);
897         }
898         TAILQ_INSERT_TAIL(&usb_events, ueq, next);
899         usb_nevents++;
900         wakeup(&usb_events);
901         selwakeup(&usb_selevent);
902         if (usb_async_proc != NULL) {
903                 ksignal(usb_async_proc, SIGIO);
904         }
905         crit_exit();
906 }
907
908 void
909 usb_schedsoftintr(usbd_bus_handle bus)
910 {
911         DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
912 #ifdef USB_USE_SOFTINTR
913         if (bus->use_polling) {
914                 bus->methods->soft_intr(bus);
915         } else {
916                 if (!callout_pending(&bus->softi))
917                         callout_reset(&bus->softi, 0, bus->methods->soft_intr,
918                             bus);
919         }
920 #else
921        bus->methods->soft_intr(bus);
922 #endif /* USB_USE_SOFTINTR */
923 }
924
925 static int
926 usb_detach(device_t self)
927 {
928         struct usb_softc *sc = device_get_softc(self);
929         struct usb_event ue;
930
931         DPRINTF(("usb_detach: start\n"));
932
933         sc->sc_dying = 1;
934
935         /* Make all devices disconnect. */
936         if (sc->sc_port.device != NULL)
937                 usb_disconnect_port(&sc->sc_port, self);
938
939         /* Kill off event thread. */
940         if (sc->sc_event_thread != NULL) {
941                 wakeup(&sc->sc_bus->needs_explore);
942                 if (tsleep(sc, 0, "usbdet", hz * 60))
943                         device_printf(self,
944                             "event thread didn't die\n");
945                 DPRINTF(("usb_detach: event thread dead\n"));
946         }
947
948         release_dev(sc->sc_usbdev);
949
950         if (--usb_ndevs == 0) {
951                 int i;
952
953                 release_dev(usb_dev);
954                 dev_ops_remove_minor(&usb_ops, USB_DEV_MINOR);
955                 usb_dev = NULL;
956
957                 for (i = 0; i < USB_NUM_TASKQS; i++) {
958                         struct usb_taskq *taskq = &usb_taskq[i];
959                         wakeup(&taskq->tasks);
960                         if (tsleep(&taskq->taskcreated, 0, "usbtdt",
961                             hz * 60)) {
962                                 kprintf("usb task thread %s didn't die\n",
963                                     taskq->name);
964                         }
965                 }
966         }
967
968         usbd_finish();
969
970 #ifdef USB_USE_SOFTINTR
971         callout_stop(&sc->sc_bus->softi);
972 #endif
973
974         ue.u.ue_ctrlr.ue_bus = device_get_unit(self);
975         usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
976
977         return (0);
978 }
979
980 static void
981 usb_child_detached(device_t self, device_t child)
982 {
983         struct usb_softc *sc = device_get_softc(self);
984
985         /* XXX, should check it is the right device. */
986         sc->sc_port.device = NULL;
987 }
988
989 /* Explore USB busses at the end of device configuration */
990 static void
991 usb_cold_explore(void *arg)
992 {
993         struct usb_softc *sc;
994
995         TUNABLE_INT_FETCH("hw.usb.hack_defer_exploration",
996             &hack_defer_exploration);
997
998         if (!hack_defer_exploration)
999                 return;
1000
1001         KASSERT(cold || TAILQ_EMPTY(&usb_coldexplist),
1002             ("usb_cold_explore: busses to explore when !cold"));
1003         while (!TAILQ_EMPTY(&usb_coldexplist)) {
1004                 sc = TAILQ_FIRST(&usb_coldexplist);
1005                 TAILQ_REMOVE(&usb_coldexplist, sc, sc_coldexplist);
1006
1007                 sc->sc_bus->use_polling++;
1008                 sc->sc_port.device->hub->explore(sc->sc_bus->root_hub);
1009                 sc->sc_bus->use_polling--;
1010         }
1011 }
1012
1013 struct usbd_bus *
1014 usb_getbushandle(struct usb_softc *sc)
1015 {
1016         return (sc->sc_bus);
1017 }
1018
1019
1020 SYSINIT(usb_cold_explore, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
1021     usb_cold_explore, NULL);
1022
1023 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1024 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1025 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);