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