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.43 2008/05/24 09:11:09 swildner Exp $
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
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>
61 #include <sys/malloc.h>
62 #include <sys/unistd.h>
63 #include <sys/module.h>
65 #include <sys/fcntl.h>
66 #include <sys/filio.h>
68 #include <sys/kthread.h>
71 #include <sys/device.h>
73 #include <sys/select.h>
74 #include <sys/vnode.h>
75 #include <sys/signalvar.h>
76 #include <sys/sysctl.h>
77 #include <sys/thread2.h>
79 #include <bus/usb/usb.h>
80 #include <bus/usb/usbdi.h>
81 #include <bus/usb/usbdi_util.h>
83 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
84 #define USB_DEV_MINOR 255 /* event queue device */
86 MALLOC_DEFINE(M_USB, "USB", "USB");
87 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
88 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
92 #include <bus/usb/usbdivar.h>
93 #include <bus/usb/usb_quirks.h>
95 /* Define this unconditionally in case a kernel module is loaded that
96 * has been compiled with debugging options.
98 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
101 #define DPRINTF(x) if (usbdebug) kprintf x
102 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
104 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
105 &usbdebug, 0, "usb debug level");
107 * 0 - do usual exploration
108 * 1 - do not use timeout exploration
109 * >1 - do no exploration
111 int usb_noexplore = 0;
114 #define DPRINTFN(n,x)
118 device_t sc_dev; /* base device */
120 usbd_bus_handle sc_bus; /* USB controller */
121 struct usbd_port sc_port; /* dummy port for root hub */
123 struct thread *sc_event_thread;
129 TAILQ_HEAD(, usb_task) tasks;
130 struct thread *task_thread_proc;
132 int taskcreated; /* task thread exists. */
134 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
142 struct dev_ops usb_ops = {
143 { "usb", USB_CDEV_MAJOR, 0 },
151 static void usb_discover(void *);
152 static bus_child_detached_t usb_child_detached;
153 static void usb_create_event_thread(void *);
154 static void usb_event_thread(void *);
155 static void usb_task_thread(void *);
157 static cdev_t usb_dev; /* The /dev/usb device. */
158 static int usb_ndevs; /* Number of /dev/usbN devices. */
160 #define USB_MAX_EVENTS 100
163 TAILQ_ENTRY(usb_event_q) next;
165 static TAILQ_HEAD(, usb_event_q) usb_events =
166 TAILQ_HEAD_INITIALIZER(usb_events);
167 static int usb_nevents = 0;
168 static struct selinfo usb_selevent;
169 static struct proc *usb_async_proc; /* process that wants USB SIGIO */
170 static int usb_dev_open = 0;
171 static void usb_add_event(int, struct usb_event *);
173 static int usb_get_next_event(struct usb_event *);
175 static const char *usbrev_str[] = USBREV_STR;
177 static device_probe_t usb_match;
178 static device_attach_t usb_attach;
179 static device_detach_t usb_detach;
181 static devclass_t usb_devclass;
183 static kobj_method_t usb_methods[] = {
184 DEVMETHOD(device_probe, usb_match),
185 DEVMETHOD(device_attach, usb_attach),
186 DEVMETHOD(device_detach, usb_detach),
187 DEVMETHOD(bus_child_detached, usb_child_detached),
188 DEVMETHOD(device_suspend, bus_generic_suspend),
189 DEVMETHOD(device_resume, bus_generic_resume),
190 DEVMETHOD(device_shutdown, bus_generic_shutdown),
194 static driver_t usb_driver = {
197 sizeof(struct usb_softc)
200 MODULE_DEPEND(usb, usb, 1, 1, 1);
201 MODULE_VERSION(usb, 1);
204 usb_match(device_t self)
206 DPRINTF(("usbd_match\n"));
207 return (UMATCH_GENERIC);
211 usb_attach(device_t self)
213 struct usb_softc *sc = device_get_softc(self);
214 void *aux = device_get_ivars(self);
216 usbd_device_handle dev;
224 DPRINTF(("usbd_attach\n"));
228 sc->sc_bus->usbctl = sc;
229 sc->sc_port.power = USB_MAX_POWER;
231 usbrev = sc->sc_bus->usbrev;
232 device_printf(sc->sc_dev, "USB revision %s", usbrev_str[usbrev]);
236 speed = USB_SPEED_FULL;
239 speed = USB_SPEED_HIGH;
242 kprintf(", not supported\n");
249 /* Make sure not to use tsleep() if we are cold booting. */
251 sc->sc_bus->use_polling++;
254 ue.u.ue_ctrlr.ue_bus = device_get_unit(sc->sc_dev);
255 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
257 #ifdef USB_USE_SOFTINTR
258 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
259 /* XXX we should have our own level */
260 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
261 sc->sc_bus->methods->soft_intr, sc->sc_bus);
262 if (sc->sc_bus->soft == NULL) {
263 device_printf(sc->sc_dev, "can't register softintr\n");
268 callout_init(&sc->sc_bus->softi);
272 err = usbd_new_device(sc->sc_dev, sc->sc_bus, 0, speed, 0,
275 dev = sc->sc_port.device;
276 if (dev->hub == NULL) {
278 device_printf(sc->sc_dev,
279 "root device is not a hub\n");
282 sc->sc_bus->root_hub = dev;
285 * Turning this code off will delay attachment of USB devices
286 * until the USB event thread is running, which means that
287 * the keyboard will not work until after cold boot.
291 * XXX Exploring high speed device here will
294 if (speed != USB_SPEED_HIGH)
295 dev->hub->explore(sc->sc_bus->root_hub);
299 device_printf(sc->sc_dev,
300 "root hub problem, error=%d\n", err);
305 sc->sc_bus->use_polling--;
308 usb_create_event_thread(sc);
309 /* The per controller devices (used for usb_discover) */
310 /* XXX This is redundant now, but old usbd's will want it */
311 dev_ops_add(&usb_ops, -1, device_get_unit(self));
312 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
313 UID_ROOT, GID_OPERATOR, 0660,
314 "usb%d", device_get_unit(self));
315 sc->sc_usbdev = reference_dev(tmp_dev);
316 if (usb_ndevs++ == 0) {
317 /* The device spitting out events */
318 dev_ops_add(&usb_ops, -1, USB_DEV_MINOR);
319 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
320 UID_ROOT, GID_OPERATOR, 0660, "usb");
321 usb_dev = reference_dev(tmp_dev);
327 static const char *taskq_names[] = USB_TASKQ_NAMES;
330 usb_create_event_thread(void *arg)
332 struct usb_softc *sc = arg;
335 if (kthread_create(usb_event_thread, sc, &sc->sc_event_thread,
336 "%s", device_get_nameunit(sc->sc_dev))) {
337 device_printf(sc->sc_dev,
338 "unable to create event thread for\n");
339 panic("usb_create_event_thread");
342 for (i = 0; i < USB_NUM_TASKQS; i++) {
343 struct usb_taskq *taskq = &usb_taskq[i];
345 if (taskq->taskcreated == 0) {
346 taskq->taskcreated = 1;
347 taskq->name = taskq_names[i];
348 TAILQ_INIT(&taskq->tasks);
349 if (kthread_create(usb_task_thread, taskq,
350 &taskq->task_thread_proc, taskq->name)) {
351 kprintf("unable to create task thread\n");
352 panic("usb_create_event_thread task");
359 * Add a task to be performed by the task thread. This function can be
360 * called from any context and the task will be executed in a process
364 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
366 struct usb_taskq *taskq;
370 taskq = &usb_taskq[queue];
371 if (task->queue == -1) {
372 DPRINTFN(2,("usb_add_task: task=%p\n", task));
373 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
376 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
378 wakeup(&taskq->tasks);
384 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
387 struct usb_taskq *taskq;
391 taskq = &usb_taskq[queue];
392 if (task->queue == -1) {
393 DPRINTFN(2,("usb_add_task: task=%p\n", task));
394 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
397 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
399 wakeup(&taskq->tasks);
401 /* Wait until task is finished */
402 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
408 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
411 if (task->queue != -1) {
412 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
419 usb_event_thread(void *arg)
421 struct usb_softc *sc = arg;
423 DPRINTF(("usb_event_thread: start\n"));
426 * In case this controller is a companion controller to an
427 * EHCI controller we need to wait until the EHCI controller
428 * has grabbed the port.
429 * XXX It would be nicer to do this with a tsleep(), but I don't
430 * know how to synchronize the creation of the threads so it
433 usb_delay_ms(sc->sc_bus, 500);
437 /* Make sure first discover does something. */
438 sc->sc_bus->needs_explore = 1;
441 while (!sc->sc_dying) {
443 if (usb_noexplore < 2)
447 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
448 usb_noexplore ? 0 : hz * 60);
450 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
452 DPRINTFN(2,("usb_event_thread: woke up\n"));
454 sc->sc_event_thread = NULL;
458 /* In case parent is waiting for us to exit. */
461 DPRINTF(("usb_event_thread: exit\n"));
466 usb_task_thread(void *arg)
468 struct usb_task *task;
469 struct usb_taskq *taskq;
474 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
476 while (usb_ndevs > 0) {
477 task = TAILQ_FIRST(&taskq->tasks);
479 tsleep(&taskq->tasks, 0, "usbtsk", 0);
480 task = TAILQ_FIRST(&taskq->tasks);
482 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
484 TAILQ_REMOVE(&taskq->tasks, task, next);
487 task->fun(task->arg);
489 wakeup((&taskq->tasks + 1));
495 taskq->taskcreated = 0;
496 wakeup(&taskq->taskcreated);
498 DPRINTF(("usb_event_thread: exit\n"));
502 usbopen(struct dev_open_args *ap)
504 cdev_t dev = ap->a_head.a_dev;
505 int unit = USBUNIT(dev);
506 struct usb_softc *sc;
508 if (unit == USB_DEV_MINOR) {
512 usb_async_proc = NULL;
516 sc = devclass_get_softc(usb_devclass, unit);
527 usbread(struct dev_read_args *ap)
529 cdev_t dev = ap->a_head.a_dev;
530 struct uio *uio = ap->a_uio;
532 int unit = USBUNIT(dev);
535 if (unit != USB_DEV_MINOR)
538 if (uio->uio_resid != sizeof(struct usb_event))
544 n = usb_get_next_event(&ue);
547 if (ap->a_ioflag & IO_NDELAY) {
551 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
557 error = uiomove((void *)&ue, uio->uio_resid, uio);
563 usbclose(struct dev_close_args *ap)
565 cdev_t dev = ap->a_head.a_dev;
566 int unit = USBUNIT(dev);
568 if (unit == USB_DEV_MINOR) {
569 usb_async_proc = NULL;
577 usbioctl(struct dev_ioctl_args *ap)
579 cdev_t devt = ap->a_head.a_dev;
580 struct usb_softc *sc;
581 int unit = USBUNIT(devt);
583 if (unit == USB_DEV_MINOR) {
586 if (*(int *)ap->a_data)
587 usb_async_proc = curproc;
589 usb_async_proc = NULL;
597 sc = devclass_get_softc(usb_devclass, unit);
603 /* This part should be deleted */
608 struct usb_ctl_request *ur = (void *)ap->a_data;
609 int len = UGETW(ur->ucr_request.wLength);
613 int addr = ur->ucr_addr;
617 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
618 if (len < 0 || len > 32768)
620 if (addr < 0 || addr >= USB_MAX_DEVICES ||
621 sc->sc_bus->devices[addr] == 0)
624 iov.iov_base = (caddr_t)ur->ucr_data;
630 uio.uio_segflg = UIO_USERSPACE;
632 ur->ucr_request.bmRequestType & UT_READ ?
633 UIO_READ : UIO_WRITE;
634 uio.uio_td = curthread;
635 ptr = kmalloc(len, M_TEMP, M_WAITOK);
636 if (uio.uio_rw == UIO_WRITE) {
637 error = uiomove(ptr, len, &uio);
642 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
643 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
644 USBD_DEFAULT_TIMEOUT);
650 if (uio.uio_rw == UIO_READ) {
651 error = uiomove(ptr, len, &uio);
664 struct usb_device_info *di = (void *)ap->a_data;
665 int addr = di->udi_addr;
666 usbd_device_handle dev;
668 if (addr < 1 || addr >= USB_MAX_DEVICES)
670 dev = sc->sc_bus->devices[addr];
673 usbd_fill_deviceinfo(dev, di, 1);
677 case USB_DEVICESTATS:
678 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
688 usbpoll(struct dev_poll_args *ap)
690 cdev_t dev = ap->a_head.a_dev;
692 int unit = USBUNIT(dev);
694 if (unit == USB_DEV_MINOR) {
696 mask = POLLIN | POLLRDNORM;
699 if (ap->a_events & mask && usb_nevents > 0)
700 revents |= ap->a_events & mask;
701 if (revents == 0 && ap->a_events & mask)
702 selrecord(curthread, &usb_selevent);
704 ap->a_events = revents;
708 return (0); /* select/poll never wakes up - back compat */
712 /* Explore device tree from the root. */
714 usb_discover(void *v)
716 struct usb_softc *sc = v;
718 DPRINTFN(2,("usb_discover\n"));
720 if (usb_noexplore > 1)
725 * We need mutual exclusion while traversing the device tree,
726 * but this is guaranteed since this function is only called
727 * from the event thread for the controller.
730 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
731 sc->sc_bus->needs_explore = 0;
734 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
742 usb_needs_explore(usbd_device_handle dev)
744 DPRINTFN(2,("usb_needs_explore\n"));
745 dev->bus->needs_explore = 1;
746 wakeup(&dev->bus->needs_explore);
749 /* Called from a critical section */
751 usb_get_next_event(struct usb_event *ue)
753 struct usb_event_q *ueq;
755 if (usb_nevents <= 0)
757 ueq = TAILQ_FIRST(&usb_events);
760 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
766 TAILQ_REMOVE(&usb_events, ueq, next);
767 kfree(ueq, M_USBDEV);
773 usbd_add_dev_event(int type, usbd_device_handle udev)
777 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
778 usb_add_event(type, &ue);
782 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev)
786 ue.u.ue_driver.ue_cookie = udev->cookie;
787 strncpy(ue.u.ue_driver.ue_devname, device_get_nameunit(dev),
788 sizeof ue.u.ue_driver.ue_devname);
789 usb_add_event(type, &ue);
793 usb_add_event(int type, struct usb_event *uep)
795 struct usb_event_q *ueq;
797 struct timeval thetime;
799 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
801 ueq->ue.ue_type = type;
803 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
806 if (USB_EVENT_IS_DETACH(type)) {
807 struct usb_event_q *ueqi, *ueqi_next;
809 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
810 ueqi_next = TAILQ_NEXT(ueqi, next);
811 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
812 uep->u.ue_device.udi_cookie.cookie) {
813 TAILQ_REMOVE(&usb_events, ueqi, next);
814 kfree(ueqi, M_USBDEV);
816 ueqi_next = TAILQ_FIRST(&usb_events);
820 if (usb_nevents >= USB_MAX_EVENTS) {
821 /* Too many queued events, drop an old one. */
822 DPRINTF(("usb: event dropped\n"));
823 usb_get_next_event(&ue);
825 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
828 selwakeup(&usb_selevent);
829 if (usb_async_proc != NULL) {
830 ksignal(usb_async_proc, SIGIO);
836 usb_schedsoftintr(usbd_bus_handle bus)
838 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
839 #ifdef USB_USE_SOFTINTR
840 if (bus->use_polling) {
841 bus->methods->soft_intr(bus);
843 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
844 softintr_schedule(bus->soft);
846 if (!callout_pending(&bus->softi))
847 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
849 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
852 bus->methods->soft_intr(bus);
853 #endif /* USB_USE_SOFTINTR */
857 usb_detach(device_t self)
859 struct usb_softc *sc = device_get_softc(self);
862 DPRINTF(("usb_detach: start\n"));
866 /* Make all devices disconnect. */
867 if (sc->sc_port.device != NULL)
868 usb_disconnect_port(&sc->sc_port, self);
870 /* Kill off event thread. */
871 if (sc->sc_event_thread != NULL) {
872 wakeup(&sc->sc_bus->needs_explore);
873 if (tsleep(sc, 0, "usbdet", hz * 60))
874 device_printf(sc->sc_dev,
875 "event thread didn't die\n");
876 DPRINTF(("usb_detach: event thread dead\n"));
879 destroy_dev(sc->sc_usbdev);
880 if (--usb_ndevs == 0) {
883 destroy_dev(usb_dev);
886 for (i = 0; i < USB_NUM_TASKQS; i++) {
887 struct usb_taskq *taskq = &usb_taskq[i];
888 wakeup(&taskq->tasks);
889 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
891 kprintf("usb task thread %s didn't die\n",
899 #ifdef USB_USE_SOFTINTR
900 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
901 if (sc->sc_bus->soft != NULL) {
902 softintr_disestablish(sc->sc_bus->soft);
903 sc->sc_bus->soft = NULL;
906 callout_stop(&sc->sc_bus->softi);
910 ue.u.ue_ctrlr.ue_bus = device_get_unit(sc->sc_dev);
911 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
917 usb_child_detached(device_t self, device_t child)
919 struct usb_softc *sc = device_get_softc(self);
921 /* XXX, should check it is the right device. */
922 sc->sc_port.device = NULL;
925 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
926 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
927 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);