1 /* $NetBSD: usb.c,v 1.68 2002/02/20 20:30:12 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb.c,v 1.106 2005/03/27 15:31:23 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usb.c,v 1.27 2007/06/16 20:57:59 dillon Exp $ */
5 /* Also already merged from NetBSD:
6 * $NetBSD: usb.c,v 1.70 2002/05/09 21:54:32 augustss Exp $
7 * $NetBSD: usb.c,v 1.71 2002/06/01 23:51:04 lukem Exp $
8 * $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
9 * $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $
13 * Copyright (c) 1998 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Lennart Augustsson (lennart@augustsson.net) at
18 * Carlstedt Research & Technology.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the NetBSD
31 * Foundation, Inc. and its contributors.
32 * 4. Neither the name of The NetBSD Foundation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
50 * USB specifications and other documentation can be found at
51 * http://www.usb.org/developers/docs/ and
52 * http://www.usb.org/developers/devclass_docs/
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #if __FreeBSD_version >= 500000
61 #include <sys/mutex.h>
63 #if defined(__NetBSD__) || defined(__OpenBSD__)
64 #include <sys/device.h>
65 #elif defined(__FreeBSD__) || defined(__DragonFly__)
66 #include <sys/unistd.h>
67 #include <sys/module.h>
69 #include <sys/fcntl.h>
70 #include <sys/filio.h>
73 #include <sys/kthread.h>
76 #include <sys/device.h>
78 #if __FreeBSD_version >= 500014
79 #include <sys/selinfo.h>
81 #include <sys/select.h>
83 #include <sys/vnode.h>
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/thread2.h>
88 #include <bus/usb/usb.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
92 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
93 #define USB_DEV_MINOR 255 /* event queue device */
95 #if defined(__FreeBSD__) || defined(__DragonFly__)
96 MALLOC_DEFINE(M_USB
, "USB", "USB");
97 MALLOC_DEFINE(M_USBDEV
, "USBdev", "USB device");
98 MALLOC_DEFINE(M_USBHC
, "USBHC", "USB host controller");
101 #endif /* defined(__FreeBSD__) || defined(__DragonFly__) */
103 #include <bus/usb/usbdivar.h>
104 #include <bus/usb/usb_quirks.h>
106 /* Define this unconditionally in case a kernel module is loaded that
107 * has been compiled with debugging options.
109 SYSCTL_NODE(_hw
, OID_AUTO
, usb
, CTLFLAG_RW
, 0, "USB debugging");
112 #define DPRINTF(x) if (usbdebug) logprintf x
113 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
115 SYSCTL_INT(_hw_usb
, OID_AUTO
, debug
, CTLFLAG_RW
,
116 &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;
125 #define DPRINTFN(n,x)
129 USBBASEDEVICE sc_dev
; /* base device */
130 #if defined(__FreeBSD__) || defined(__DragonFly__)
133 usbd_bus_handle sc_bus
; /* USB controller */
134 struct usbd_port sc_port
; /* dummy port for root hub */
136 struct thread
*sc_event_thread
;
142 TAILQ_HEAD(, usb_task
) tasks
;
143 usb_proc_ptr task_thread_proc
;
145 int taskcreated
; /* task thread exists. */
147 static struct usb_taskq usb_taskq
[USB_NUM_TASKQS
];
149 #if defined(__NetBSD__) || defined(__OpenBSD__)
151 #elif defined(__FreeBSD__) || defined(__DragonFly__)
158 struct dev_ops usb_ops
= {
159 { "usb", USB_CDEV_MAJOR
, 0 },
168 Static
void usb_discover(void *);
169 #if defined(__FreeBSD__) || defined(__DragonFly__)
170 Static bus_child_detached_t usb_child_detached
;
172 Static
void usb_create_event_thread(void *);
173 Static
void usb_event_thread(void *);
174 Static
void usb_task_thread(void *);
176 #if defined(__FreeBSD__) || defined(__DragonFly__)
177 Static cdev_t usb_dev
; /* The /dev/usb device. */
178 Static
int usb_ndevs
; /* Number of /dev/usbN devices. */
179 Static
int usb_exitevthreads
; /* Ask the event threads to exit */
182 #define USB_MAX_EVENTS 100
185 TAILQ_ENTRY(usb_event_q
) next
;
187 Static
TAILQ_HEAD(, usb_event_q
) usb_events
=
188 TAILQ_HEAD_INITIALIZER(usb_events
);
189 Static
int usb_nevents
= 0;
190 Static
struct selinfo usb_selevent
;
191 Static
struct proc
*usb_async_proc
; /* process that wants USB SIGIO */
192 Static
int usb_dev_open
= 0;
193 Static
void usb_add_event(int, struct usb_event
*);
195 Static
int usb_get_next_event(struct usb_event
*);
197 Static
const char *usbrev_str
[] = USBREV_STR
;
199 USB_DECLARE_DRIVER_INIT(usb
,
200 DEVMETHOD(bus_child_detached
, usb_child_detached
),
201 DEVMETHOD(device_suspend
, bus_generic_suspend
),
202 DEVMETHOD(device_resume
, bus_generic_resume
),
203 DEVMETHOD(device_shutdown
, bus_generic_shutdown
)
206 #if defined(__FreeBSD__) || defined(__DragonFly__)
207 MODULE_VERSION(usb
, 1);
212 DPRINTF(("usbd_match\n"));
213 return (UMATCH_GENERIC
);
218 #if defined(__NetBSD__) || defined(__OpenBSD__)
219 struct usb_softc
*sc
= (struct usb_softc
*)self
;
220 #elif defined(__FreeBSD__) || defined(__DragonFly__)
221 struct usb_softc
*sc
= device_get_softc(self
);
222 void *aux
= device_get_ivars(self
);
225 usbd_device_handle dev
;
233 DPRINTF(("usbd_attach\n"));
237 sc
->sc_bus
->usbctl
= sc
;
238 sc
->sc_port
.power
= USB_MAX_POWER
;
240 #if defined(__FreeBSD__) || defined(__DragonFly__)
241 kprintf("%s", USBDEVNAME(sc
->sc_dev
));
243 usbrev
= sc
->sc_bus
->usbrev
;
244 kprintf(": USB revision %s", usbrev_str
[usbrev
]);
248 speed
= USB_SPEED_FULL
;
251 speed
= USB_SPEED_HIGH
;
254 kprintf(", not supported\n");
256 USB_ATTACH_ERROR_RETURN
;
261 /* Make sure not to use tsleep() if we are cold booting. */
263 sc
->sc_bus
->use_polling
++;
266 ue
.u
.ue_ctrlr
.ue_bus
= USBDEVUNIT(sc
->sc_dev
);
267 usb_add_event(USB_EVENT_CTRLR_ATTACH
, &ue
);
269 #ifdef USB_USE_SOFTINTR
270 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
271 /* XXX we should have our own level */
272 sc
->sc_bus
->soft
= softintr_establish(IPL_SOFTNET
,
273 sc
->sc_bus
->methods
->soft_intr
, sc
->sc_bus
);
274 if (sc
->sc_bus
->soft
== NULL
) {
275 kprintf("%s: can't register softintr\n", USBDEVNAME(sc
->sc_dev
));
277 USB_ATTACH_ERROR_RETURN
;
280 usb_callout_init(sc
->sc_bus
->softi
);
283 #if defined(__FreeBSD__) || defined(__DragonFly__)
284 usb_exitevthreads
= 0;
285 usb_create_event_thread(sc
);
288 err
= usbd_new_device(USBDEV(sc
->sc_dev
), sc
->sc_bus
, 0, speed
, 0,
291 dev
= sc
->sc_port
.device
;
292 if (dev
->hub
== NULL
) {
294 kprintf("%s: root device is not a hub\n",
295 USBDEVNAME(sc
->sc_dev
));
296 USB_ATTACH_ERROR_RETURN
;
298 sc
->sc_bus
->root_hub
= dev
;
301 * Turning this code off will delay attachment of USB devices
302 * until the USB event thread is running, which means that
303 * the keyboard will not work until after cold boot.
305 #if defined(__FreeBSD__) || defined(__DragonFly__)
308 * XXX Exploring high speed device here will
311 if (speed
!= USB_SPEED_HIGH
)
312 dev
->hub
->explore(sc
->sc_bus
->root_hub
);
315 if (cold
&& (sc
->sc_dev
.dv_cfdata
->cf_flags
& 1))
316 dev
->hub
->explore(sc
->sc_bus
->root_hub
);
320 kprintf("%s: root hub problem, error=%d\n",
321 USBDEVNAME(sc
->sc_dev
), err
);
326 sc
->sc_bus
->use_polling
--;
329 config_pending_incr();
330 #if defined(__NetBSD__) || defined(__OpenBSD__)
331 usb_kthread_create(usb_create_event_thread
, sc
);
334 #if defined(__FreeBSD__) || defined(__DragonFly__)
335 /* The per controller devices (used for usb_discover) */
336 /* XXX This is redundant now, but old usbd's will want it */
337 dev_ops_add(&usb_ops
, -1, device_get_unit(self
));
338 tmp_dev
= make_dev(&usb_ops
, device_get_unit(self
),
339 UID_ROOT
, GID_OPERATOR
, 0660,
340 "usb%d", device_get_unit(self
));
341 sc
->sc_usbdev
= reference_dev(tmp_dev
);
342 if (usb_ndevs
++ == 0) {
343 /* The device spitting out events */
344 dev_ops_add(&usb_ops
, -1, USB_DEV_MINOR
);
345 tmp_dev
= make_dev(&usb_ops
, USB_DEV_MINOR
,
346 UID_ROOT
, GID_OPERATOR
, 0660, "usb");
347 usb_dev
= reference_dev(tmp_dev
);
351 USB_ATTACH_SUCCESS_RETURN
;
354 Static
const char *taskq_names
[] = USB_TASKQ_NAMES
;
357 usb_create_event_thread(void *arg
)
359 struct usb_softc
*sc
= arg
;
362 for (i
= 0; i
< USB_NUM_TASKQS
; i
++) {
363 struct usb_taskq
*taskq
= &usb_taskq
[i
];
365 if (taskq
->taskcreated
== 0) {
366 taskq
->taskcreated
= 1;
367 taskq
->name
= taskq_names
[i
];
368 TAILQ_INIT(&taskq
->tasks
);
369 if (usb_kthread_create2(usb_task_thread
, taskq
,
370 &taskq
->task_thread_proc
, taskq
->name
)) {
371 kprintf("unable to create task thread\n");
372 panic("usb_create_event_thread task");
376 if (usb_kthread_create1(usb_event_thread
, sc
, &sc
->sc_event_thread
,
377 "%s", USBDEVNAME(sc
->sc_dev
))) {
378 kprintf("%s: unable to create event thread for\n",
379 USBDEVNAME(sc
->sc_dev
));
380 panic("usb_create_event_thread");
385 * Add a task to be performed by the task thread. This function can be
386 * called from any context and the task will be executed in a process
390 usb_add_task(usbd_device_handle dev
, struct usb_task
*task
, int queue
)
392 struct usb_taskq
*taskq
;
396 taskq
= &usb_taskq
[queue
];
397 if (task
->queue
== -1) {
398 DPRINTFN(2,("usb_add_task: task=%p\n", task
));
399 TAILQ_INSERT_TAIL(&taskq
->tasks
, task
, next
);
402 DPRINTFN(3,("usb_add_task: task=%p on q\n", task
));
404 wakeup(&taskq
->tasks
);
410 usb_do_task(usbd_device_handle dev
, struct usb_task
*task
, int queue
,
413 struct usb_taskq
*taskq
;
417 taskq
= &usb_taskq
[queue
];
418 if (task
->queue
== -1) {
419 DPRINTFN(2,("usb_add_task: task=%p\n", task
));
420 TAILQ_INSERT_TAIL(&taskq
->tasks
, task
, next
);
423 DPRINTFN(3,("usb_add_task: task=%p on q\n", task
));
425 wakeup(&taskq
->tasks
);
427 /* Wait until task is finished */
428 tsleep((&taskq
->tasks
+ 1), 0, "usbdotsk", time_out
);
434 usb_rem_task(usbd_device_handle dev
, struct usb_task
*task
)
437 if (task
->queue
!= -1) {
438 TAILQ_REMOVE(&usb_taskq
[task
->queue
].tasks
, task
, next
);
445 usb_event_thread(void *arg
)
447 struct usb_softc
*sc
= arg
;
449 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
453 DPRINTF(("usb_event_thread: start\n"));
456 * In case this controller is a companion controller to an
457 * EHCI controller we need to wait until the EHCI controller
458 * has grabbed the port.
459 * XXX It would be nicer to do this with a tsleep(), but I don't
460 * know how to synchronize the creation of the threads so it
463 usb_delay_ms(sc
->sc_bus
, 500);
467 /* Make sure first discover does something. */
468 sc
->sc_bus
->needs_explore
= 1;
470 config_pending_decr();
472 while (!sc
->sc_dying
) {
474 if (usb_noexplore
< 2)
478 tsleep(&sc
->sc_bus
->needs_explore
, 0, "usbevt",
479 usb_noexplore
? 0 : hz
* 60);
481 tsleep(&sc
->sc_bus
->needs_explore
, 0, "usbevt", hz
* 60);
483 DPRINTFN(2,("usb_event_thread: woke up\n"));
485 sc
->sc_event_thread
= NULL
;
489 /* In case parent is waiting for us to exit. */
492 DPRINTF(("usb_event_thread: exit\n"));
497 usb_task_thread(void *arg
)
499 struct usb_task
*task
;
500 struct usb_taskq
*taskq
;
502 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
509 DPRINTF(("usb_task_thread: start taskq %s\n", taskq
->name
));
511 while (usb_exitevthreads
== 0) {
512 task
= TAILQ_FIRST(&taskq
->tasks
);
514 tsleep(&taskq
->tasks
, 0, "usbtsk", 0);
515 task
= TAILQ_FIRST(&taskq
->tasks
);
517 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task
));
519 TAILQ_REMOVE(&taskq
->tasks
, task
, next
);
522 task
->fun(task
->arg
);
524 wakeup((&taskq
->tasks
+ 1));
530 taskq
->taskcreated
= 0;
531 wakeup(&taskq
->taskcreated
);
533 DPRINTF(("usb_event_thread: exit\n"));
536 #if defined(__NetBSD__) || defined(__OpenBSD__)
538 usbctlprint(void *aux
, const char *pnp
)
540 /* only "usb"es can attach to host controllers */
542 kprintf("usb at %s", pnp
);
546 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
549 usbopen(struct dev_open_args
*ap
)
551 cdev_t dev
= ap
->a_head
.a_dev
;
552 int unit
= USBUNIT(dev
);
553 struct usb_softc
*sc
;
555 if (unit
== USB_DEV_MINOR
) {
559 usb_async_proc
= NULL
;
563 USB_GET_SC_OPEN(usb
, unit
, sc
);
572 usbread(struct dev_read_args
*ap
)
574 cdev_t dev
= ap
->a_head
.a_dev
;
575 struct uio
*uio
= ap
->a_uio
;
577 int unit
= USBUNIT(dev
);
580 if (unit
!= USB_DEV_MINOR
)
583 if (uio
->uio_resid
!= sizeof(struct usb_event
))
589 n
= usb_get_next_event(&ue
);
592 if (ap
->a_ioflag
& IO_NDELAY
) {
596 error
= tsleep(&usb_events
, PCATCH
, "usbrea", 0);
602 error
= uiomove((void *)&ue
, uio
->uio_resid
, uio
);
608 usbclose(struct dev_close_args
*ap
)
610 cdev_t dev
= ap
->a_head
.a_dev
;
611 int unit
= USBUNIT(dev
);
613 if (unit
== USB_DEV_MINOR
) {
614 usb_async_proc
= NULL
;
622 usbioctl(struct dev_ioctl_args
*ap
)
624 cdev_t devt
= ap
->a_head
.a_dev
;
625 struct usb_softc
*sc
;
626 int unit
= USBUNIT(devt
);
628 if (unit
== USB_DEV_MINOR
) {
631 if (*(int *)ap
->a_data
)
632 #if defined(__DragonFly__)
633 usb_async_proc
= curproc
;
634 #elif __FreeBSD_version >= 500000
635 usb_async_proc
= p
->td_proc
;
640 usb_async_proc
= NULL
;
648 USB_GET_SC(usb
, unit
, sc
);
654 #if defined(__FreeBSD__) || defined(__DragonFly__)
655 /* This part should be deleted */
661 struct usb_ctl_request
*ur
= (void *)ap
->a_data
;
662 int len
= UGETW(ur
->ucr_request
.wLength
);
666 int addr
= ur
->ucr_addr
;
670 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr
, len
));
671 if (len
< 0 || len
> 32768)
673 if (addr
< 0 || addr
>= USB_MAX_DEVICES
||
674 sc
->sc_bus
->devices
[addr
] == 0)
677 iov
.iov_base
= (caddr_t
)ur
->ucr_data
;
683 uio
.uio_segflg
= UIO_USERSPACE
;
685 ur
->ucr_request
.bmRequestType
& UT_READ
?
686 UIO_READ
: UIO_WRITE
;
687 uio
.uio_td
= curthread
;
688 ptr
= kmalloc(len
, M_TEMP
, M_WAITOK
);
689 if (uio
.uio_rw
== UIO_WRITE
) {
690 error
= uiomove(ptr
, len
, &uio
);
695 err
= usbd_do_request_flags(sc
->sc_bus
->devices
[addr
],
696 &ur
->ucr_request
, ptr
, ur
->ucr_flags
, &ur
->ucr_actlen
,
697 USBD_DEFAULT_TIMEOUT
);
703 if (uio
.uio_rw
== UIO_READ
) {
704 error
= uiomove(ptr
, len
, &uio
);
717 struct usb_device_info
*di
= (void *)ap
->a_data
;
718 int addr
= di
->udi_addr
;
719 usbd_device_handle dev
;
721 if (addr
< 1 || addr
>= USB_MAX_DEVICES
)
723 dev
= sc
->sc_bus
->devices
[addr
];
726 usbd_fill_deviceinfo(dev
, di
, 1);
730 case USB_DEVICESTATS
:
731 *(struct usb_device_stats
*)ap
->a_data
= sc
->sc_bus
->stats
;
741 usbpoll(struct dev_poll_args
*ap
)
743 cdev_t dev
= ap
->a_head
.a_dev
;
745 int unit
= USBUNIT(dev
);
747 if (unit
== USB_DEV_MINOR
) {
749 mask
= POLLIN
| POLLRDNORM
;
752 if (ap
->a_events
& mask
&& usb_nevents
> 0)
753 revents
|= ap
->a_events
& mask
;
754 if (revents
== 0 && ap
->a_events
& mask
)
755 selrecord(curthread
, &usb_selevent
);
757 ap
->a_events
= revents
;
761 return (0); /* select/poll never wakes up - back compat */
765 /* Explore device tree from the root. */
767 usb_discover(void *v
)
769 struct usb_softc
*sc
= v
;
771 DPRINTFN(2,("usb_discover\n"));
773 if (usb_noexplore
> 1)
778 * We need mutual exclusion while traversing the device tree,
779 * but this is guaranteed since this function is only called
780 * from the event thread for the controller.
783 while (sc
->sc_bus
->needs_explore
&& !sc
->sc_dying
) {
784 sc
->sc_bus
->needs_explore
= 0;
787 sc
->sc_bus
->root_hub
->hub
->explore(sc
->sc_bus
->root_hub
);
795 usb_needs_explore(usbd_device_handle dev
)
797 DPRINTFN(2,("usb_needs_explore\n"));
798 dev
->bus
->needs_explore
= 1;
799 wakeup(&dev
->bus
->needs_explore
);
802 /* Called from a critical section */
804 usb_get_next_event(struct usb_event
*ue
)
806 struct usb_event_q
*ueq
;
808 if (usb_nevents
<= 0)
810 ueq
= TAILQ_FIRST(&usb_events
);
813 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents
);
819 TAILQ_REMOVE(&usb_events
, ueq
, next
);
820 kfree(ueq
, M_USBDEV
);
826 usbd_add_dev_event(int type
, usbd_device_handle udev
)
830 usbd_fill_deviceinfo(udev
, &ue
.u
.ue_device
, USB_EVENT_IS_ATTACH(type
));
831 usb_add_event(type
, &ue
);
835 usbd_add_drv_event(int type
, usbd_device_handle udev
, device_ptr_t dev
)
839 ue
.u
.ue_driver
.ue_cookie
= udev
->cookie
;
840 strncpy(ue
.u
.ue_driver
.ue_devname
, USBDEVPTRNAME(dev
),
841 sizeof ue
.u
.ue_driver
.ue_devname
);
842 usb_add_event(type
, &ue
);
846 usb_add_event(int type
, struct usb_event
*uep
)
848 struct usb_event_q
*ueq
;
850 struct timeval thetime
;
852 ueq
= kmalloc(sizeof *ueq
, M_USBDEV
, M_INTWAIT
);
854 ueq
->ue
.ue_type
= type
;
856 TIMEVAL_TO_TIMESPEC(&thetime
, &ueq
->ue
.ue_time
);
859 if (USB_EVENT_IS_DETACH(type
)) {
860 struct usb_event_q
*ueqi
, *ueqi_next
;
862 for (ueqi
= TAILQ_FIRST(&usb_events
); ueqi
; ueqi
= ueqi_next
) {
863 ueqi_next
= TAILQ_NEXT(ueqi
, next
);
864 if (ueqi
->ue
.u
.ue_driver
.ue_cookie
.cookie
==
865 uep
->u
.ue_device
.udi_cookie
.cookie
) {
866 TAILQ_REMOVE(&usb_events
, ueqi
, next
);
867 kfree(ueqi
, M_USBDEV
);
869 ueqi_next
= TAILQ_FIRST(&usb_events
);
873 if (usb_nevents
>= USB_MAX_EVENTS
) {
874 /* Too many queued events, drop an old one. */
875 DPRINTF(("usb: event dropped\n"));
876 usb_get_next_event(&ue
);
878 TAILQ_INSERT_TAIL(&usb_events
, ueq
, next
);
881 selwakeuppri(&usb_selevent
, 0);
882 if (usb_async_proc
!= NULL
) {
883 PROC_LOCK(usb_async_proc
);
884 ksignal(usb_async_proc
, SIGIO
);
885 PROC_UNLOCK(usb_async_proc
);
891 usb_schedsoftintr(usbd_bus_handle bus
)
893 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus
->use_polling
));
894 #ifdef USB_USE_SOFTINTR
895 if (bus
->use_polling
) {
896 bus
->methods
->soft_intr(bus
);
898 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
899 softintr_schedule(bus
->soft
);
901 if (!callout_pending(&bus
->softi
))
902 callout_reset(&bus
->softi
, 0, bus
->methods
->soft_intr
,
904 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
907 bus
->methods
->soft_intr(bus
);
908 #endif /* USB_USE_SOFTINTR */
911 #if defined(__NetBSD__) || defined(__OpenBSD__)
913 usb_activate(device_ptr_t self
, enum devact act
)
915 struct usb_softc
*sc
= (struct usb_softc
*)self
;
916 usbd_device_handle dev
= sc
->sc_port
.device
;
923 case DVACT_DEACTIVATE
:
925 if (dev
!= NULL
&& dev
->cdesc
!= NULL
&& dev
->subdevs
!= NULL
) {
926 for (i
= 0; dev
->subdevs
[i
]; i
++)
927 rv
|= config_deactivate(dev
->subdevs
[i
]);
937 USB_DETACH_START(usb
, sc
);
940 DPRINTF(("usb_detach: start\n"));
944 /* Make all devices disconnect. */
945 if (sc
->sc_port
.device
!= NULL
)
946 usb_disconnect_port(&sc
->sc_port
, self
);
948 /* Kill off event thread. */
949 if (sc
->sc_event_thread
!= NULL
) {
950 wakeup(&sc
->sc_bus
->needs_explore
);
951 if (tsleep(sc
, 0, "usbdet", hz
* 60))
952 kprintf("%s: event thread didn't die\n",
953 USBDEVNAME(sc
->sc_dev
));
954 DPRINTF(("usb_detach: event thread dead\n"));
957 #if defined(__FreeBSD__) || defined(__DragonFly__)
958 destroy_dev(sc
->sc_usbdev
);
959 if (--usb_ndevs
== 0) {
962 destroy_dev(usb_dev
);
965 usb_exitevthreads
= 1;
966 for (i
= 0; i
< USB_NUM_TASKQS
; i
++) {
967 struct usb_taskq
*taskq
= &usb_taskq
[i
];
968 wakeup(&taskq
->tasks
);
969 if (tsleep(&taskq
->taskcreated
, 0, "usbtdt",
971 kprintf("usb task thread %s didn't die\n",
980 #ifdef USB_USE_SOFTINTR
981 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
982 if (sc
->sc_bus
->soft
!= NULL
) {
983 softintr_disestablish(sc
->sc_bus
->soft
);
984 sc
->sc_bus
->soft
= NULL
;
987 callout_stop(&sc
->sc_bus
->softi
);
991 ue
.u
.ue_ctrlr
.ue_bus
= USBDEVUNIT(sc
->sc_dev
);
992 usb_add_event(USB_EVENT_CTRLR_DETACH
, &ue
);
997 #if defined(__FreeBSD__) || defined(__DragonFly__)
999 usb_child_detached(device_t self
, device_t child
)
1001 struct usb_softc
*sc
= device_get_softc(self
);
1003 /* XXX, should check it is the right device. */
1004 sc
->sc_port
.device
= NULL
;
1007 DRIVER_MODULE(usb
, ohci
, usb_driver
, usb_devclass
, 0, 0);
1008 DRIVER_MODULE(usb
, uhci
, usb_driver
, usb_devclass
, 0, 0);
1009 DRIVER_MODULE(usb
, ehci
, usb_driver
, usb_devclass
, 0, 0);