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.15 2005/06/02 20:40:40 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>
85 #include <sys/thread2.h>
89 #include "usbdi_util.h"
91 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
92 #define USB_DEV_MINOR 255 /* event queue device */
94 #if defined(__FreeBSD__) || defined(__DragonFly__)
95 MALLOC_DEFINE(M_USB, "USB", "USB");
96 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
97 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
100 #endif /* defined(__FreeBSD__) */
102 #include <machine/bus.h>
104 #include "usbdivar.h"
105 #include "usb_quirks.h"
107 /* Define this unconditionally in case a kernel module is loaded that
108 * has been compiled with debugging options.
110 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
113 #define DPRINTF(x) if (usbdebug) logprintf x
114 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
116 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
117 &usbdebug, 0, "usb debug level");
119 * 0 - do usual exploration
120 * 1 - do not use timeout exploration
121 * >1 - do no exploration
123 int usb_noexplore = 0;
126 #define DPRINTFN(n,x)
130 USBBASEDEVICE sc_dev; /* base device */
131 usbd_bus_handle sc_bus; /* USB controller */
132 struct usbd_port sc_port; /* dummy port for root hub */
134 struct thread *sc_event_thread;
139 TAILQ_HEAD(, usb_task) usb_all_tasks;
141 #if defined(__NetBSD__) || defined(__OpenBSD__)
143 #elif defined(__FreeBSD__) || defined(__DragonFly__)
148 int usbpoll(dev_t, int, usb_proc_ptr);
150 struct cdevsw usb_cdevsw = {
152 /* maj */ USB_CDEV_MAJOR,
158 /* close */ usbclose,
161 /* ioctl */ usbioctl,
164 /* strategy */ nostrategy,
170 Static void usb_discover(void *);
171 Static void usb_create_event_thread(void *);
172 Static void usb_event_thread(void *);
173 Static void usb_task_thread(void *);
174 Static usb_proc_ptr usb_task_thread_proc = NULL;
176 #define USB_MAX_EVENTS 100
179 TAILQ_ENTRY(usb_event_q) next;
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 *);
189 Static int usb_get_next_event(struct usb_event *);
191 #if defined(__NetBSD__) || defined(__OpenBSD__)
192 /* Flag to see if we are in the cold boot process. */
196 Static const char *usbrev_str[] = USBREV_STR;
198 USB_DECLARE_DRIVER_INIT(usb,
199 DEVMETHOD(device_suspend, bus_generic_suspend),
200 DEVMETHOD(device_resume, bus_generic_resume),
201 DEVMETHOD(device_shutdown, bus_generic_shutdown)
204 #if defined(__FreeBSD__) || defined(__DragonFly__)
205 MODULE_VERSION(usb, 1);
210 DPRINTF(("usbd_match\n"));
211 return (UMATCH_GENERIC);
216 #if defined(__NetBSD__) || defined(__OpenBSD__)
217 struct usb_softc *sc = (struct usb_softc *)self;
218 #elif defined(__FreeBSD__) || defined(__DragonFly__)
219 struct usb_softc *sc = device_get_softc(self);
220 void *aux = device_get_ivars(self);
221 static int global_init_done = 0;
223 usbd_device_handle dev;
231 DPRINTF(("usbd_attach\n"));
235 sc->sc_bus->usbctl = sc;
236 sc->sc_port.power = USB_MAX_POWER;
238 #if defined(__FreeBSD__) || defined(__DragonFly__)
239 printf("%s", USBDEVNAME(sc->sc_dev));
241 usbrev = sc->sc_bus->usbrev;
242 printf(": USB revision %s", usbrev_str[usbrev]);
246 speed = USB_SPEED_FULL;
249 speed = USB_SPEED_HIGH;
252 printf(", not supported\n");
254 USB_ATTACH_ERROR_RETURN;
258 /* Make sure not to use tsleep() if we are cold booting. */
260 sc->sc_bus->use_polling++;
262 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
263 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
265 #ifdef USB_USE_SOFTINTR
266 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
267 /* XXX we should have our own level */
268 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
269 sc->sc_bus->methods->soft_intr, sc->sc_bus);
270 if (sc->sc_bus->soft == NULL) {
271 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
273 USB_ATTACH_ERROR_RETURN;
276 usb_callout_init(sc->sc_bus->softi);
280 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
283 dev = sc->sc_port.device;
284 if (dev->hub == NULL) {
286 printf("%s: root device is not a hub\n",
287 USBDEVNAME(sc->sc_dev));
288 USB_ATTACH_ERROR_RETURN;
290 sc->sc_bus->root_hub = dev;
293 * Turning this code off will delay attachment of USB devices
294 * until the USB event thread is running, which means that
295 * the keyboard will not work until after cold boot.
297 #if defined(__FreeBSD__) || defined(__DragonFly__)
300 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
302 dev->hub->explore(sc->sc_bus->root_hub);
305 printf("%s: root hub problem, error=%d\n",
306 USBDEVNAME(sc->sc_dev), err);
310 sc->sc_bus->use_polling--;
312 config_pending_incr();
313 #if defined(__NetBSD__) || defined(__OpenBSD__)
314 usb_kthread_create(usb_create_event_thread, sc);
317 #if defined(__FreeBSD__) || defined(__DragonFly__)
318 usb_create_event_thread(sc);
319 /* The per controller devices (used for usb_discover) */
320 /* XXX This is redundant now, but old usbd's will want it */
321 if (!global_init_done) {
322 /* The device spitting out events */
323 cdevsw_add(&usb_cdevsw, -1, USB_DEV_MINOR);
324 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
326 global_init_done = 1;
328 cdevsw_add(&usb_cdevsw, -1, device_get_unit(self));
329 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
330 0660, "usb%d", device_get_unit(self));
333 USB_ATTACH_SUCCESS_RETURN;
337 usb_create_event_thread(void *arg)
339 struct usb_softc *sc = arg;
340 static int created = 0;
342 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
343 "%s", USBDEVNAME(sc->sc_dev))) {
344 printf("%s: unable to create event thread for\n",
345 USBDEVNAME(sc->sc_dev));
346 panic("usb_create_event_thread");
350 TAILQ_INIT(&usb_all_tasks);
351 if (usb_kthread_create2(usb_task_thread, NULL,
352 &usb_task_thread_proc, "usbtask")) {
353 printf("unable to create task thread\n");
354 panic("usb_create_event_thread task");
360 * Add a task to be performed by the task thread. This function can be
361 * called from any context and the task will be executed in a process
365 usb_add_task(usbd_device_handle dev, struct usb_task *task)
368 if (!task->onqueue) {
369 DPRINTFN(2,("usb_add_task: task=%p\n", task));
370 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
373 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
375 wakeup(&usb_all_tasks);
380 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
384 TAILQ_REMOVE(&usb_all_tasks, task, next);
391 usb_event_thread(void *arg)
393 struct usb_softc *sc = arg;
395 DPRINTF(("usb_event_thread: start\n"));
398 * In case this controller is a companion controller to an
399 * EHCI controller we need to wait until the EHCI controller
400 * has grabbed the port.
401 * XXX It would be nicer to do this with a tsleep(), but I don't
402 * know how to synchronize the creation of the threads so it
405 usb_delay_ms(sc->sc_bus, 500);
407 /* Make sure first discover does something. */
408 sc->sc_bus->needs_explore = 1;
410 config_pending_decr();
412 while (!sc->sc_dying) {
414 if (usb_noexplore < 2)
418 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
419 usb_noexplore ? 0 : hz * 60);
421 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
424 DPRINTFN(2,("usb_event_thread: woke up\n"));
426 sc->sc_event_thread = NULL;
428 /* In case parent is waiting for us to exit. */
431 DPRINTF(("usb_event_thread: exit\n"));
436 usb_task_thread(void *arg)
438 struct usb_task *task;
440 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
444 DPRINTF(("usb_task_thread: start\n"));
448 task = TAILQ_FIRST(&usb_all_tasks);
450 tsleep(&usb_all_tasks, 0, "usbtsk", 0);
451 task = TAILQ_FIRST(&usb_all_tasks);
453 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
455 TAILQ_REMOVE(&usb_all_tasks, task, next);
458 task->fun(task->arg);
464 #if defined(__NetBSD__) || defined(__OpenBSD__)
466 usbctlprint(void *aux, const char *pnp)
468 /* only "usb"es can attach to host controllers */
470 printf("usb at %s", pnp);
474 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
477 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
479 int unit = USBUNIT(dev);
480 struct usb_softc *sc;
482 if (unit == USB_DEV_MINOR) {
486 usb_async_proc = NULL;
490 USB_GET_SC_OPEN(usb, unit, sc);
499 usbread(dev_t dev, struct uio *uio, int flag)
502 int unit = USBUNIT(dev);
505 if (unit != USB_DEV_MINOR)
508 if (uio->uio_resid != sizeof(struct usb_event))
514 n = usb_get_next_event(&ue);
517 if (flag & IO_NDELAY) {
521 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
527 error = uiomove((void *)&ue, uio->uio_resid, uio);
533 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
535 int unit = USBUNIT(dev);
537 if (unit == USB_DEV_MINOR) {
538 usb_async_proc = NULL;
546 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
548 struct usb_softc *sc;
549 int unit = USBUNIT(devt);
551 if (unit == USB_DEV_MINOR) {
554 /* All handled in the upper FS layer. */
559 #if defined(__DragonFly__)
560 usb_async_proc = p->td_proc;
561 #elif __FreeBSD_version >= 500000
562 usb_async_proc = p->td_proc;
567 usb_async_proc = NULL;
575 USB_GET_SC(usb, unit, sc);
581 #if defined(__FreeBSD__) || defined(__DragonFly__)
582 /* This part should be deleted */
588 struct usb_ctl_request *ur = (void *)data;
589 int len = UGETW(ur->ucr_request.wLength);
593 int addr = ur->ucr_addr;
597 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
598 if (len < 0 || len > 32768)
600 if (addr < 0 || addr >= USB_MAX_DEVICES ||
601 sc->sc_bus->devices[addr] == 0)
604 iov.iov_base = (caddr_t)ur->ucr_data;
610 uio.uio_segflg = UIO_USERSPACE;
612 ur->ucr_request.bmRequestType & UT_READ ?
613 UIO_READ : UIO_WRITE;
615 ptr = malloc(len, M_TEMP, M_WAITOK);
616 if (uio.uio_rw == UIO_WRITE) {
617 error = uiomove(ptr, len, &uio);
622 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
623 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
624 USBD_DEFAULT_TIMEOUT);
630 if (uio.uio_rw == UIO_READ) {
631 error = uiomove(ptr, len, &uio);
644 struct usb_device_info *di = (void *)data;
645 int addr = di->udi_addr;
646 usbd_device_handle dev;
648 if (addr < 1 || addr >= USB_MAX_DEVICES)
650 dev = sc->sc_bus->devices[addr];
653 usbd_fill_deviceinfo(dev, di, 1);
657 case USB_DEVICESTATS:
658 *(struct usb_device_stats *)data = sc->sc_bus->stats;
668 usbpoll(dev_t dev, int events, usb_proc_ptr p)
671 int unit = USBUNIT(dev);
673 if (unit == USB_DEV_MINOR) {
675 mask = POLLIN | POLLRDNORM;
678 if (events & mask && usb_nevents > 0)
679 revents |= events & mask;
680 if (revents == 0 && events & mask)
681 selrecord(p, &usb_selevent);
686 #if defined(__FreeBSD__) || defined(__DragonFly__)
687 return (0); /* select/poll never wakes up - back compat */
694 /* Explore device tree from the root. */
696 usb_discover(void *v)
698 struct usb_softc *sc = v;
700 DPRINTFN(2,("usb_discover\n"));
702 if (usb_noexplore > 1)
707 * We need mutual exclusion while traversing the device tree,
708 * but this is guaranteed since this function is only called
709 * from the event thread for the controller.
711 #if defined(__FreeBSD__) || defined(__DragonFly__)
714 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
715 sc->sc_bus->needs_explore = 0;
716 #if defined(__FreeBSD__) || defined(__DragonFly__)
719 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
720 #if defined(__FreeBSD__) || defined(__DragonFly__)
724 #if defined(__FreeBSD__) || defined(__DragonFly__)
730 usb_needs_explore(usbd_device_handle dev)
732 DPRINTFN(2,("usb_needs_explore\n"));
733 dev->bus->needs_explore = 1;
734 wakeup(&dev->bus->needs_explore);
737 /* Called from a critical section */
739 usb_get_next_event(struct usb_event *ue)
741 struct usb_event_q *ueq;
743 if (usb_nevents <= 0)
745 ueq = TAILQ_FIRST(&usb_events);
748 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
754 TAILQ_REMOVE(&usb_events, ueq, next);
761 usbd_add_dev_event(int type, usbd_device_handle udev)
765 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
766 usb_add_event(type, &ue);
770 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
774 ue.u.ue_driver.ue_cookie = udev->cookie;
775 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
776 sizeof ue.u.ue_driver.ue_devname);
777 usb_add_event(type, &ue);
781 usb_add_event(int type, struct usb_event *uep)
783 struct usb_event_q *ueq;
785 struct timeval thetime;
787 ueq = malloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
789 ueq->ue.ue_type = type;
791 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
794 if (USB_EVENT_IS_DETACH(type)) {
795 struct usb_event_q *ueqi, *ueqi_next;
797 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
798 ueqi_next = TAILQ_NEXT(ueqi, next);
799 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
800 uep->u.ue_device.udi_cookie.cookie) {
801 TAILQ_REMOVE(&usb_events, ueqi, next);
802 free(ueqi, M_USBDEV);
804 ueqi_next = TAILQ_FIRST(&usb_events);
808 if (usb_nevents >= USB_MAX_EVENTS) {
809 /* Too many queued events, drop an old one. */
810 DPRINTF(("usb: event dropped\n"));
811 (void)usb_get_next_event(&ue);
813 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
816 selwakeuppri(&usb_selevent, 0);
817 if (usb_async_proc != NULL) {
818 PROC_LOCK(usb_async_proc);
819 psignal(usb_async_proc, SIGIO);
820 PROC_UNLOCK(usb_async_proc);
826 usb_schedsoftintr(usbd_bus_handle bus)
828 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
829 #ifdef USB_USE_SOFTINTR
830 if (bus->use_polling) {
831 bus->methods->soft_intr(bus);
833 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
834 softintr_schedule(bus->soft);
836 if (!callout_pending(&bus->softi))
837 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
839 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
842 bus->methods->soft_intr(bus);
843 #endif /* USB_USE_SOFTINTR */
847 #if defined(__NetBSD__) || defined(__OpenBSD__)
849 usb_activate(device_ptr_t self, enum devact act)
851 struct usb_softc *sc = (struct usb_softc *)self;
852 usbd_device_handle dev = sc->sc_port.device;
859 case DVACT_DEACTIVATE:
861 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
862 for (i = 0; dev->subdevs[i]; i++)
863 rv |= config_deactivate(dev->subdevs[i]);
871 usb_detach(device_ptr_t self, int flags)
873 struct usb_softc *sc = (struct usb_softc *)self;
876 DPRINTF(("usb_detach: start\n"));
880 /* Make all devices disconnect. */
881 if (sc->sc_port.device != NULL)
882 usb_disconnect_port(&sc->sc_port, self);
884 /* Kill off event thread. */
885 if (sc->sc_event_thread != NULL) {
886 wakeup(&sc->sc_bus->needs_explore);
887 if (tsleep(sc, 0, "usbdet", hz * 60))
888 printf("%s: event thread didn't die\n",
889 USBDEVNAME(sc->sc_dev));
890 DPRINTF(("usb_detach: event thread dead\n"));
895 #ifdef USB_USE_SOFTINTR
896 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
897 if (sc->sc_bus->soft != NULL) {
898 softintr_disestablish(sc->sc_bus->soft);
899 sc->sc_bus->soft = NULL;
902 callout_stop(&sc->sc_bus->softi);
906 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
907 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
911 #elif defined(__FreeBSD__) || defined(__DragonFly__)
913 usb_detach(device_t self)
915 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
922 #if defined(__FreeBSD__) || defined(__DragonFly__)
923 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
924 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
925 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);