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.12 2004/03/12 03:43:06 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 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
321 0660, "usb%d", device_get_unit(self));
322 if (!global_init_done) {
323 /* The device spitting out events */
324 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
326 global_init_done = 1;
330 USB_ATTACH_SUCCESS_RETURN;
334 usb_create_event_thread(void *arg)
336 struct usb_softc *sc = arg;
337 static int created = 0;
339 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
340 "%s", USBDEVNAME(sc->sc_dev))) {
341 printf("%s: unable to create event thread for\n",
342 USBDEVNAME(sc->sc_dev));
343 panic("usb_create_event_thread");
347 TAILQ_INIT(&usb_all_tasks);
348 if (usb_kthread_create2(usb_task_thread, NULL,
349 &usb_task_thread_proc, "usbtask")) {
350 printf("unable to create task thread\n");
351 panic("usb_create_event_thread task");
357 * Add a task to be performed by the task thread. This function can be
358 * called from any context and the task will be executed in a process
362 usb_add_task(usbd_device_handle dev, struct usb_task *task)
367 if (!task->onqueue) {
368 DPRINTFN(2,("usb_add_task: task=%p\n", task));
369 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
372 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
374 wakeup(&usb_all_tasks);
379 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
385 TAILQ_REMOVE(&usb_all_tasks, task, next);
392 usb_event_thread(void *arg)
394 struct usb_softc *sc = arg;
396 DPRINTF(("usb_event_thread: start\n"));
399 * In case this controller is a companion controller to an
400 * EHCI controller we need to wait until the EHCI controller
401 * has grabbed the port.
402 * XXX It would be nicer to do this with a tsleep(), but I don't
403 * know how to synchronize the creation of the threads so it
406 usb_delay_ms(sc->sc_bus, 500);
408 /* Make sure first discover does something. */
409 sc->sc_bus->needs_explore = 1;
411 config_pending_decr();
413 while (!sc->sc_dying) {
415 if (usb_noexplore < 2)
419 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
420 usb_noexplore ? 0 : hz * 60);
422 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
425 DPRINTFN(2,("usb_event_thread: woke up\n"));
427 sc->sc_event_thread = NULL;
429 /* In case parent is waiting for us to exit. */
432 DPRINTF(("usb_event_thread: exit\n"));
437 usb_task_thread(void *arg)
439 struct usb_task *task;
442 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
446 DPRINTF(("usb_task_thread: start\n"));
450 task = TAILQ_FIRST(&usb_all_tasks);
452 tsleep(&usb_all_tasks, 0, "usbtsk", 0);
453 task = TAILQ_FIRST(&usb_all_tasks);
455 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
457 TAILQ_REMOVE(&usb_all_tasks, task, next);
460 task->fun(task->arg);
466 #if defined(__NetBSD__) || defined(__OpenBSD__)
468 usbctlprint(void *aux, const char *pnp)
470 /* only "usb"es can attach to host controllers */
472 printf("usb at %s", pnp);
476 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
479 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
481 int unit = USBUNIT(dev);
482 struct usb_softc *sc;
484 if (unit == USB_DEV_MINOR) {
488 usb_async_proc = NULL;
492 USB_GET_SC_OPEN(usb, unit, sc);
501 usbread(dev_t dev, struct uio *uio, int flag)
504 int unit = USBUNIT(dev);
507 if (unit != USB_DEV_MINOR)
510 if (uio->uio_resid != sizeof(struct usb_event))
516 n = usb_get_next_event(&ue);
519 if (flag & IO_NDELAY) {
523 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
529 error = uiomove((void *)&ue, uio->uio_resid, uio);
535 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
537 int unit = USBUNIT(dev);
539 if (unit == USB_DEV_MINOR) {
540 usb_async_proc = NULL;
548 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
550 struct usb_softc *sc;
551 int unit = USBUNIT(devt);
553 if (unit == USB_DEV_MINOR) {
556 /* All handled in the upper FS layer. */
561 #if defined(__DragonFly__)
562 usb_async_proc = p->td_proc;
563 #elif __FreeBSD_version >= 500000
564 usb_async_proc = p->td_proc;
569 usb_async_proc = NULL;
577 USB_GET_SC(usb, unit, sc);
583 #if defined(__FreeBSD__) || defined(__DragonFly__)
584 /* This part should be deleted */
590 struct usb_ctl_request *ur = (void *)data;
591 int len = UGETW(ur->ucr_request.wLength);
595 int addr = ur->ucr_addr;
599 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
600 if (len < 0 || len > 32768)
602 if (addr < 0 || addr >= USB_MAX_DEVICES ||
603 sc->sc_bus->devices[addr] == 0)
606 iov.iov_base = (caddr_t)ur->ucr_data;
612 uio.uio_segflg = UIO_USERSPACE;
614 ur->ucr_request.bmRequestType & UT_READ ?
615 UIO_READ : UIO_WRITE;
617 ptr = malloc(len, M_TEMP, M_WAITOK);
618 if (uio.uio_rw == UIO_WRITE) {
619 error = uiomove(ptr, len, &uio);
624 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
625 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
626 USBD_DEFAULT_TIMEOUT);
632 if (uio.uio_rw == UIO_READ) {
633 error = uiomove(ptr, len, &uio);
646 struct usb_device_info *di = (void *)data;
647 int addr = di->udi_addr;
648 usbd_device_handle dev;
650 if (addr < 1 || addr >= USB_MAX_DEVICES)
652 dev = sc->sc_bus->devices[addr];
655 usbd_fill_deviceinfo(dev, di, 1);
659 case USB_DEVICESTATS:
660 *(struct usb_device_stats *)data = sc->sc_bus->stats;
670 usbpoll(dev_t dev, int events, usb_proc_ptr p)
672 int revents, mask, s;
673 int unit = USBUNIT(dev);
675 if (unit == USB_DEV_MINOR) {
677 mask = POLLIN | POLLRDNORM;
680 if (events & mask && usb_nevents > 0)
681 revents |= events & mask;
682 if (revents == 0 && events & mask)
683 selrecord(p, &usb_selevent);
688 #if defined(__FreeBSD__) || defined(__DragonFly__)
689 return (0); /* select/poll never wakes up - back compat */
696 /* Explore device tree from the root. */
698 usb_discover(void *v)
700 struct usb_softc *sc = v;
702 #if defined(__FreeBSD__) || defined(__DragonFly__)
703 /* splxxx should be changed to mutexes for preemption safety some day */
707 DPRINTFN(2,("usb_discover\n"));
709 if (usb_noexplore > 1)
714 * We need mutual exclusion while traversing the device tree,
715 * but this is guaranteed since this function is only called
716 * from the event thread for the controller.
718 #if defined(__FreeBSD__) || defined(__DragonFly__)
721 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
722 sc->sc_bus->needs_explore = 0;
723 #if defined(__FreeBSD__) || defined(__DragonFly__)
726 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
727 #if defined(__FreeBSD__) || defined(__DragonFly__)
731 #if defined(__FreeBSD__) || defined(__DragonFly__)
737 usb_needs_explore(usbd_device_handle dev)
739 DPRINTFN(2,("usb_needs_explore\n"));
740 dev->bus->needs_explore = 1;
741 wakeup(&dev->bus->needs_explore);
744 /* Called at splusb() */
746 usb_get_next_event(struct usb_event *ue)
748 struct usb_event_q *ueq;
750 if (usb_nevents <= 0)
752 ueq = TAILQ_FIRST(&usb_events);
755 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
761 TAILQ_REMOVE(&usb_events, ueq, next);
768 usbd_add_dev_event(int type, usbd_device_handle udev)
772 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
773 usb_add_event(type, &ue);
777 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
781 ue.u.ue_driver.ue_cookie = udev->cookie;
782 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
783 sizeof ue.u.ue_driver.ue_devname);
784 usb_add_event(type, &ue);
788 usb_add_event(int type, struct usb_event *uep)
790 struct usb_event_q *ueq;
792 struct timeval thetime;
795 ueq = malloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
797 ueq->ue.ue_type = type;
799 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
802 if (USB_EVENT_IS_DETACH(type)) {
803 struct usb_event_q *ueqi, *ueqi_next;
805 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
806 ueqi_next = TAILQ_NEXT(ueqi, next);
807 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
808 uep->u.ue_device.udi_cookie.cookie) {
809 TAILQ_REMOVE(&usb_events, ueqi, next);
810 free(ueqi, M_USBDEV);
812 ueqi_next = TAILQ_FIRST(&usb_events);
816 if (usb_nevents >= USB_MAX_EVENTS) {
817 /* Too many queued events, drop an old one. */
818 DPRINTF(("usb: event dropped\n"));
819 (void)usb_get_next_event(&ue);
821 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
824 selwakeuppri(&usb_selevent, 0);
825 if (usb_async_proc != NULL) {
826 PROC_LOCK(usb_async_proc);
827 psignal(usb_async_proc, SIGIO);
828 PROC_UNLOCK(usb_async_proc);
834 usb_schedsoftintr(usbd_bus_handle bus)
836 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
837 #ifdef USB_USE_SOFTINTR
838 if (bus->use_polling) {
839 bus->methods->soft_intr(bus);
841 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
842 softintr_schedule(bus->soft);
844 if (!callout_pending(&bus->softi))
845 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
847 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
850 bus->methods->soft_intr(bus);
851 #endif /* USB_USE_SOFTINTR */
855 #if defined(__NetBSD__) || defined(__OpenBSD__)
857 usb_activate(device_ptr_t self, enum devact act)
859 struct usb_softc *sc = (struct usb_softc *)self;
860 usbd_device_handle dev = sc->sc_port.device;
867 case DVACT_DEACTIVATE:
869 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
870 for (i = 0; dev->subdevs[i]; i++)
871 rv |= config_deactivate(dev->subdevs[i]);
879 usb_detach(device_ptr_t self, int flags)
881 struct usb_softc *sc = (struct usb_softc *)self;
884 DPRINTF(("usb_detach: start\n"));
888 /* Make all devices disconnect. */
889 if (sc->sc_port.device != NULL)
890 usb_disconnect_port(&sc->sc_port, self);
892 /* Kill off event thread. */
893 if (sc->sc_event_thread != NULL) {
894 wakeup(&sc->sc_bus->needs_explore);
895 if (tsleep(sc, 0, "usbdet", hz * 60))
896 printf("%s: event thread didn't die\n",
897 USBDEVNAME(sc->sc_dev));
898 DPRINTF(("usb_detach: event thread dead\n"));
903 #ifdef USB_USE_SOFTINTR
904 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
905 if (sc->sc_bus->soft != NULL) {
906 softintr_disestablish(sc->sc_bus->soft);
907 sc->sc_bus->soft = NULL;
910 callout_stop(&sc->sc_bus->softi);
914 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
915 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
919 #elif defined(__FreeBSD__) || defined(__DragonFly__)
921 usb_detach(device_t self)
923 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
930 #if defined(__FreeBSD__) || defined(__DragonFly__)
931 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
932 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
933 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);