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.95 2003/11/09 23:54:21 joe Exp $
4 * $DragonFly: src/sys/bus/usb/usb.c,v 1.14 2004/05/19 22:52:39 dillon 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 $
14 * Copyright (c) 1998 The NetBSD Foundation, Inc.
15 * All rights reserved.
17 * This code is derived from software contributed to The NetBSD Foundation
18 * by Lennart Augustsson (lennart@augustsson.net) at
19 * Carlstedt Research & Technology.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by the NetBSD
32 * Foundation, Inc. and its contributors.
33 * 4. Neither the name of The NetBSD Foundation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
38 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
39 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
41 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
51 * USB specifications and other documentation can be found at
52 * http://www.usb.org/developers/data/ and
53 * http://www.usb.org/developers/index.html .
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
60 #include <sys/malloc.h>
61 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
62 #include <sys/mutex.h>
64 #if defined(__NetBSD__) || defined(__OpenBSD__)
65 #include <sys/device.h>
66 #elif defined(__FreeBSD__) || defined(__DragonFly__)
67 #include <sys/unistd.h>
68 #include <sys/module.h>
70 #include <sys/filio.h>
73 #include <sys/kthread.h>
77 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
78 #include <sys/selinfo.h>
80 #include <sys/select.h>
82 #include <sys/vnode.h>
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
88 #include "usbdi_util.h"
90 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
91 #define USB_DEV_MINOR 255 /* event queue device */
93 #if defined(__FreeBSD__) || defined(__DragonFly__)
94 MALLOC_DEFINE(M_USB, "USB", "USB");
95 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
96 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
99 #endif /* defined(__FreeBSD__) */
101 #include <machine/bus.h>
103 #include "usbdivar.h"
104 #include "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 usbd_bus_handle sc_bus; /* USB controller */
131 struct usbd_port sc_port; /* dummy port for root hub */
133 struct thread *sc_event_thread;
138 TAILQ_HEAD(, usb_task) usb_all_tasks;
140 #if defined(__NetBSD__) || defined(__OpenBSD__)
142 #elif defined(__FreeBSD__) || defined(__DragonFly__)
147 int usbpoll(dev_t, int, usb_proc_ptr);
149 struct cdevsw usb_cdevsw = {
151 /* maj */ USB_CDEV_MAJOR,
157 /* close */ usbclose,
160 /* ioctl */ usbioctl,
163 /* strategy */ nostrategy,
169 Static void usb_discover(void *);
170 Static void usb_create_event_thread(void *);
171 Static void usb_event_thread(void *);
172 Static void usb_task_thread(void *);
173 Static usb_proc_ptr usb_task_thread_proc = NULL;
175 #define USB_MAX_EVENTS 100
178 TAILQ_ENTRY(usb_event_q) next;
180 Static TAILQ_HEAD(, usb_event_q) usb_events =
181 TAILQ_HEAD_INITIALIZER(usb_events);
182 Static int usb_nevents = 0;
183 Static struct selinfo usb_selevent;
184 Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
185 Static int usb_dev_open = 0;
186 Static void usb_add_event(int, struct usb_event *);
188 Static int usb_get_next_event(struct usb_event *);
190 #if defined(__NetBSD__) || defined(__OpenBSD__)
191 /* Flag to see if we are in the cold boot process. */
195 Static const char *usbrev_str[] = USBREV_STR;
197 USB_DECLARE_DRIVER_INIT(usb,
198 DEVMETHOD(device_suspend, bus_generic_suspend),
199 DEVMETHOD(device_resume, bus_generic_resume),
200 DEVMETHOD(device_shutdown, bus_generic_shutdown)
203 #if defined(__FreeBSD__) || defined(__DragonFly__)
204 MODULE_VERSION(usb, 1);
209 DPRINTF(("usbd_match\n"));
210 return (UMATCH_GENERIC);
215 #if defined(__NetBSD__) || defined(__OpenBSD__)
216 struct usb_softc *sc = (struct usb_softc *)self;
217 #elif defined(__FreeBSD__) || defined(__DragonFly__)
218 struct usb_softc *sc = device_get_softc(self);
219 void *aux = device_get_ivars(self);
220 static int global_init_done = 0;
222 usbd_device_handle dev;
230 DPRINTF(("usbd_attach\n"));
234 sc->sc_bus->usbctl = sc;
235 sc->sc_port.power = USB_MAX_POWER;
237 #if defined(__FreeBSD__) || defined(__DragonFly__)
238 printf("%s", USBDEVNAME(sc->sc_dev));
240 usbrev = sc->sc_bus->usbrev;
241 printf(": USB revision %s", usbrev_str[usbrev]);
245 speed = USB_SPEED_FULL;
248 speed = USB_SPEED_HIGH;
251 printf(", not supported\n");
253 USB_ATTACH_ERROR_RETURN;
257 /* Make sure not to use tsleep() if we are cold booting. */
259 sc->sc_bus->use_polling++;
261 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
262 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
264 #ifdef USB_USE_SOFTINTR
265 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
266 /* XXX we should have our own level */
267 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
268 sc->sc_bus->methods->soft_intr, sc->sc_bus);
269 if (sc->sc_bus->soft == NULL) {
270 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
272 USB_ATTACH_ERROR_RETURN;
275 usb_callout_init(sc->sc_bus->softi);
279 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
282 dev = sc->sc_port.device;
283 if (dev->hub == NULL) {
285 printf("%s: root device is not a hub\n",
286 USBDEVNAME(sc->sc_dev));
287 USB_ATTACH_ERROR_RETURN;
289 sc->sc_bus->root_hub = dev;
292 * Turning this code off will delay attachment of USB devices
293 * until the USB event thread is running, which means that
294 * the keyboard will not work until after cold boot.
296 #if defined(__FreeBSD__) || defined(__DragonFly__)
299 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
301 dev->hub->explore(sc->sc_bus->root_hub);
304 printf("%s: root hub problem, error=%d\n",
305 USBDEVNAME(sc->sc_dev), err);
309 sc->sc_bus->use_polling--;
311 config_pending_incr();
312 #if defined(__NetBSD__) || defined(__OpenBSD__)
313 usb_kthread_create(usb_create_event_thread, sc);
316 #if defined(__FreeBSD__) || defined(__DragonFly__)
317 usb_create_event_thread(sc);
318 /* The per controller devices (used for usb_discover) */
319 /* XXX This is redundant now, but old usbd's will want it */
320 if (!global_init_done) {
321 /* The device spitting out events */
322 cdevsw_add(&usb_cdevsw, -1, USB_DEV_MINOR);
323 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
325 global_init_done = 1;
327 cdevsw_add(&usb_cdevsw, -1, device_get_unit(self));
328 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
329 0660, "usb%d", device_get_unit(self));
332 USB_ATTACH_SUCCESS_RETURN;
336 usb_create_event_thread(void *arg)
338 struct usb_softc *sc = arg;
339 static int created = 0;
341 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
342 "%s", USBDEVNAME(sc->sc_dev))) {
343 printf("%s: unable to create event thread for\n",
344 USBDEVNAME(sc->sc_dev));
345 panic("usb_create_event_thread");
349 TAILQ_INIT(&usb_all_tasks);
350 if (usb_kthread_create2(usb_task_thread, NULL,
351 &usb_task_thread_proc, "usbtask")) {
352 printf("unable to create task thread\n");
353 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)
369 if (!task->onqueue) {
370 DPRINTFN(2,("usb_add_task: task=%p\n", task));
371 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
374 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
376 wakeup(&usb_all_tasks);
381 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
387 TAILQ_REMOVE(&usb_all_tasks, task, next);
394 usb_event_thread(void *arg)
396 struct usb_softc *sc = arg;
398 DPRINTF(("usb_event_thread: start\n"));
401 * In case this controller is a companion controller to an
402 * EHCI controller we need to wait until the EHCI controller
403 * has grabbed the port.
404 * XXX It would be nicer to do this with a tsleep(), but I don't
405 * know how to synchronize the creation of the threads so it
408 usb_delay_ms(sc->sc_bus, 500);
410 /* Make sure first discover does something. */
411 sc->sc_bus->needs_explore = 1;
413 config_pending_decr();
415 while (!sc->sc_dying) {
417 if (usb_noexplore < 2)
421 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
422 usb_noexplore ? 0 : hz * 60);
424 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
427 DPRINTFN(2,("usb_event_thread: woke up\n"));
429 sc->sc_event_thread = NULL;
431 /* In case parent is waiting for us to exit. */
434 DPRINTF(("usb_event_thread: exit\n"));
439 usb_task_thread(void *arg)
441 struct usb_task *task;
444 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
448 DPRINTF(("usb_task_thread: start\n"));
452 task = TAILQ_FIRST(&usb_all_tasks);
454 tsleep(&usb_all_tasks, 0, "usbtsk", 0);
455 task = TAILQ_FIRST(&usb_all_tasks);
457 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
459 TAILQ_REMOVE(&usb_all_tasks, task, next);
462 task->fun(task->arg);
468 #if defined(__NetBSD__) || defined(__OpenBSD__)
470 usbctlprint(void *aux, const char *pnp)
472 /* only "usb"es can attach to host controllers */
474 printf("usb at %s", pnp);
478 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
481 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
483 int unit = USBUNIT(dev);
484 struct usb_softc *sc;
486 if (unit == USB_DEV_MINOR) {
490 usb_async_proc = NULL;
494 USB_GET_SC_OPEN(usb, unit, sc);
503 usbread(dev_t dev, struct uio *uio, int flag)
506 int unit = USBUNIT(dev);
509 if (unit != USB_DEV_MINOR)
512 if (uio->uio_resid != sizeof(struct usb_event))
518 n = usb_get_next_event(&ue);
521 if (flag & IO_NDELAY) {
525 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
531 error = uiomove((void *)&ue, uio->uio_resid, uio);
537 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
539 int unit = USBUNIT(dev);
541 if (unit == USB_DEV_MINOR) {
542 usb_async_proc = NULL;
550 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
552 struct usb_softc *sc;
553 int unit = USBUNIT(devt);
555 if (unit == USB_DEV_MINOR) {
558 /* All handled in the upper FS layer. */
563 #if defined(__DragonFly__)
564 usb_async_proc = p->td_proc;
565 #elif __FreeBSD_version >= 500000
566 usb_async_proc = p->td_proc;
571 usb_async_proc = NULL;
579 USB_GET_SC(usb, unit, sc);
585 #if defined(__FreeBSD__) || defined(__DragonFly__)
586 /* This part should be deleted */
592 struct usb_ctl_request *ur = (void *)data;
593 int len = UGETW(ur->ucr_request.wLength);
597 int addr = ur->ucr_addr;
601 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
602 if (len < 0 || len > 32768)
604 if (addr < 0 || addr >= USB_MAX_DEVICES ||
605 sc->sc_bus->devices[addr] == 0)
608 iov.iov_base = (caddr_t)ur->ucr_data;
614 uio.uio_segflg = UIO_USERSPACE;
616 ur->ucr_request.bmRequestType & UT_READ ?
617 UIO_READ : UIO_WRITE;
619 ptr = malloc(len, M_TEMP, M_WAITOK);
620 if (uio.uio_rw == UIO_WRITE) {
621 error = uiomove(ptr, len, &uio);
626 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
627 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
628 USBD_DEFAULT_TIMEOUT);
634 if (uio.uio_rw == UIO_READ) {
635 error = uiomove(ptr, len, &uio);
648 struct usb_device_info *di = (void *)data;
649 int addr = di->udi_addr;
650 usbd_device_handle dev;
652 if (addr < 1 || addr >= USB_MAX_DEVICES)
654 dev = sc->sc_bus->devices[addr];
657 usbd_fill_deviceinfo(dev, di, 1);
661 case USB_DEVICESTATS:
662 *(struct usb_device_stats *)data = sc->sc_bus->stats;
672 usbpoll(dev_t dev, int events, usb_proc_ptr p)
674 int revents, mask, s;
675 int unit = USBUNIT(dev);
677 if (unit == USB_DEV_MINOR) {
679 mask = POLLIN | POLLRDNORM;
682 if (events & mask && usb_nevents > 0)
683 revents |= events & mask;
684 if (revents == 0 && events & mask)
685 selrecord(p, &usb_selevent);
690 #if defined(__FreeBSD__) || defined(__DragonFly__)
691 return (0); /* select/poll never wakes up - back compat */
698 /* Explore device tree from the root. */
700 usb_discover(void *v)
702 struct usb_softc *sc = v;
704 #if defined(__FreeBSD__) || defined(__DragonFly__)
705 /* splxxx should be changed to mutexes for preemption safety some day */
709 DPRINTFN(2,("usb_discover\n"));
711 if (usb_noexplore > 1)
716 * We need mutual exclusion while traversing the device tree,
717 * but this is guaranteed since this function is only called
718 * from the event thread for the controller.
720 #if defined(__FreeBSD__) || defined(__DragonFly__)
723 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
724 sc->sc_bus->needs_explore = 0;
725 #if defined(__FreeBSD__) || defined(__DragonFly__)
728 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
729 #if defined(__FreeBSD__) || defined(__DragonFly__)
733 #if defined(__FreeBSD__) || defined(__DragonFly__)
739 usb_needs_explore(usbd_device_handle dev)
741 DPRINTFN(2,("usb_needs_explore\n"));
742 dev->bus->needs_explore = 1;
743 wakeup(&dev->bus->needs_explore);
746 /* Called at splusb() */
748 usb_get_next_event(struct usb_event *ue)
750 struct usb_event_q *ueq;
752 if (usb_nevents <= 0)
754 ueq = TAILQ_FIRST(&usb_events);
757 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
763 TAILQ_REMOVE(&usb_events, ueq, next);
770 usbd_add_dev_event(int type, usbd_device_handle udev)
774 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
775 usb_add_event(type, &ue);
779 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
783 ue.u.ue_driver.ue_cookie = udev->cookie;
784 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
785 sizeof ue.u.ue_driver.ue_devname);
786 usb_add_event(type, &ue);
790 usb_add_event(int type, struct usb_event *uep)
792 struct usb_event_q *ueq;
794 struct timeval thetime;
797 ueq = malloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
799 ueq->ue.ue_type = type;
801 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
804 if (USB_EVENT_IS_DETACH(type)) {
805 struct usb_event_q *ueqi, *ueqi_next;
807 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
808 ueqi_next = TAILQ_NEXT(ueqi, next);
809 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
810 uep->u.ue_device.udi_cookie.cookie) {
811 TAILQ_REMOVE(&usb_events, ueqi, next);
812 free(ueqi, M_USBDEV);
814 ueqi_next = TAILQ_FIRST(&usb_events);
818 if (usb_nevents >= USB_MAX_EVENTS) {
819 /* Too many queued events, drop an old one. */
820 DPRINTF(("usb: event dropped\n"));
821 (void)usb_get_next_event(&ue);
823 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
826 selwakeuppri(&usb_selevent, 0);
827 if (usb_async_proc != NULL) {
828 PROC_LOCK(usb_async_proc);
829 psignal(usb_async_proc, SIGIO);
830 PROC_UNLOCK(usb_async_proc);
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 #if defined(__NetBSD__) || defined(__OpenBSD__)
859 usb_activate(device_ptr_t self, enum devact act)
861 struct usb_softc *sc = (struct usb_softc *)self;
862 usbd_device_handle dev = sc->sc_port.device;
869 case DVACT_DEACTIVATE:
871 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
872 for (i = 0; dev->subdevs[i]; i++)
873 rv |= config_deactivate(dev->subdevs[i]);
881 usb_detach(device_ptr_t self, int flags)
883 struct usb_softc *sc = (struct usb_softc *)self;
886 DPRINTF(("usb_detach: start\n"));
890 /* Make all devices disconnect. */
891 if (sc->sc_port.device != NULL)
892 usb_disconnect_port(&sc->sc_port, self);
894 /* Kill off event thread. */
895 if (sc->sc_event_thread != NULL) {
896 wakeup(&sc->sc_bus->needs_explore);
897 if (tsleep(sc, 0, "usbdet", hz * 60))
898 printf("%s: event thread didn't die\n",
899 USBDEVNAME(sc->sc_dev));
900 DPRINTF(("usb_detach: event thread dead\n"));
905 #ifdef USB_USE_SOFTINTR
906 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
907 if (sc->sc_bus->soft != NULL) {
908 softintr_disestablish(sc->sc_bus->soft);
909 sc->sc_bus->soft = NULL;
912 callout_stop(&sc->sc_bus->softi);
916 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
917 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
921 #elif defined(__FreeBSD__) || defined(__DragonFly__)
923 usb_detach(device_t self)
925 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
932 #if defined(__FreeBSD__) || defined(__DragonFly__)
933 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
934 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
935 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);