1 /* $NetBSD: usb.c,v 1.67 2002/02/11 15:11:49 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * USB specifications and other documentation can be found at
43 * http://www.usb.org/developers/data/ and
44 * http://www.usb.org/developers/index.html .
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #if defined(__NetBSD__) || defined(__OpenBSD__)
54 #include <sys/device.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/unistd.h>
57 #include <sys/module.h>
59 #include <sys/filio.h>
62 #include <sys/kthread.h>
66 #if __FreeBSD_version >= 500014
67 #include <sys/selinfo.h>
69 #include <sys/select.h>
71 #include <sys/vnode.h>
72 #include <sys/signalvar.h>
73 #include <sys/sysctl.h>
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
77 #include <dev/usb/usbdi_util.h>
79 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
80 #define USB_DEV_MINOR 255 /* event queue device */
82 #if defined(__FreeBSD__)
83 MALLOC_DEFINE(M_USB, "USB", "USB");
84 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
85 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
88 #endif /* defined(__FreeBSD__) */
90 #include <machine/bus.h>
92 #include <dev/usb/usbdivar.h>
93 #include <dev/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) logprintf x
102 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
104 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
105 &usbdebug, 0, "usb debug level");
107 extern int uhcidebug;
110 extern int ohcidebug;
113 * 0 - do usual exploration
114 * 1 - do not use timeout exploration
115 * >1 - do no exploration
117 int usb_noexplore = 0;
120 #define DPRINTFN(n,x)
124 USBBASEDEVICE sc_dev; /* base device */
125 usbd_bus_handle sc_bus; /* USB controller */
126 struct usbd_port sc_port; /* dummy port for root hub */
128 struct proc *sc_event_thread;
133 TAILQ_HEAD(, usb_task) usb_all_tasks;
135 #if defined(__NetBSD__) || defined(__OpenBSD__)
137 #elif defined(__FreeBSD__)
144 struct cdevsw usb_cdevsw = {
146 /* close */ usbclose,
149 /* ioctl */ usbioctl,
152 /* strategy */ nostrategy,
154 /* maj */ USB_CDEV_MAJOR,
158 #if __FreeBSD_version < 500014
164 Static void usb_discover(void *);
165 Static void usb_create_event_thread(void *);
166 Static void usb_event_thread(void *);
167 Static void usb_task_thread(void *);
168 Static struct proc *usb_task_thread_proc = NULL;
170 #define USB_MAX_EVENTS 100
173 TAILQ_ENTRY(usb_event_q) next;
175 Static TAILQ_HEAD(, usb_event_q) usb_events =
176 TAILQ_HEAD_INITIALIZER(usb_events);
177 Static int usb_nevents = 0;
178 Static struct selinfo usb_selevent;
179 Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
180 Static int usb_dev_open = 0;
181 Static void usb_add_event(int, struct usb_event *);
183 Static int usb_get_next_event(struct usb_event *);
185 Static const char *usbrev_str[] = USBREV_STR;
187 USB_DECLARE_DRIVER_INIT(usb,
188 DEVMETHOD(device_suspend, bus_generic_suspend),
189 DEVMETHOD(device_resume, bus_generic_resume),
190 DEVMETHOD(device_shutdown, bus_generic_shutdown)
193 #if defined(__FreeBSD__)
194 MODULE_VERSION(usb, 1);
199 DPRINTF(("usbd_match\n"));
200 return (UMATCH_GENERIC);
205 #if defined(__NetBSD__) || defined(__OpenBSD__)
206 struct usb_softc *sc = (struct usb_softc *)self;
207 #elif defined(__FreeBSD__)
208 struct usb_softc *sc = device_get_softc(self);
209 void *aux = device_get_ivars(self);
210 static int global_init_done = 0;
212 usbd_device_handle dev;
220 DPRINTF(("usbd_attach\n"));
224 sc->sc_bus->usbctl = sc;
225 sc->sc_port.power = USB_MAX_POWER;
227 #if defined(__FreeBSD__)
228 printf("%s", USBDEVNAME(sc->sc_dev));
230 usbrev = sc->sc_bus->usbrev;
231 printf(": USB revision %s", usbrev_str[usbrev]);
235 speed = USB_SPEED_FULL;
238 speed = USB_SPEED_HIGH;
241 printf(", not supported\n");
243 USB_ATTACH_ERROR_RETURN;
247 /* Make sure not to use tsleep() if we are cold booting. */
249 sc->sc_bus->use_polling++;
251 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
252 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
254 #ifdef USB_USE_SOFTINTR
255 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
256 /* XXX we should have our own level */
257 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
258 sc->sc_bus->methods->soft_intr, sc->sc_bus);
259 if (sc->sc_bus->soft == NULL) {
260 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
262 USB_ATTACH_ERROR_RETURN;
265 usb_callout_init(sc->sc_bus->softi);
269 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
272 dev = sc->sc_port.device;
273 if (dev->hub == NULL) {
275 printf("%s: root device is not a hub\n",
276 USBDEVNAME(sc->sc_dev));
277 USB_ATTACH_ERROR_RETURN;
279 sc->sc_bus->root_hub = dev;
282 * Turning this code off will delay attachment of USB devices
283 * until the USB event thread is running, which means that
284 * the keyboard will not work until after cold boot.
286 #if defined(__FreeBSD__)
289 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
291 dev->hub->explore(sc->sc_bus->root_hub);
294 printf("%s: root hub problem, error=%d\n",
295 USBDEVNAME(sc->sc_dev), err);
299 sc->sc_bus->use_polling--;
301 config_pending_incr();
302 #if defined(__NetBSD__) || defined(__OpenBSD__)
303 usb_kthread_create(usb_create_event_thread, sc);
306 #if defined(__FreeBSD__)
307 usb_create_event_thread(sc);
308 /* The per controller devices (used for usb_discover) */
309 /* XXX This is redundant now, but old usbd's will want it */
310 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
311 0660, "usb%d", device_get_unit(self));
312 if (!global_init_done) {
313 /* The device spitting out events */
314 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
316 global_init_done = 1;
320 USB_ATTACH_SUCCESS_RETURN;
324 usb_create_event_thread(void *arg)
326 struct usb_softc *sc = arg;
327 static int created = 0;
329 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
330 "%s", USBDEVNAME(sc->sc_dev))) {
331 printf("%s: unable to create event thread for\n",
332 USBDEVNAME(sc->sc_dev));
333 panic("usb_create_event_thread");
337 TAILQ_INIT(&usb_all_tasks);
338 if (usb_kthread_create2(usb_task_thread, NULL,
339 &usb_task_thread_proc, "usbtask")) {
340 printf("unable to create task thread\n");
341 panic("usb_create_event_thread task");
347 * Add a task to be performed by the task thread. This function can be
348 * called from any context and the task will be executed in a process
352 usb_add_task(usbd_device_handle dev, struct usb_task *task)
357 if (!task->onqueue) {
358 DPRINTFN(2,("usb_add_task: task=%p\n", task));
359 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
362 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
364 wakeup(&usb_all_tasks);
369 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
375 TAILQ_REMOVE(&usb_all_tasks, task, next);
382 usb_event_thread(void *arg)
384 struct usb_softc *sc = arg;
390 DPRINTF(("usb_event_thread: start\n"));
393 * In case this controller is a companion controller to an
394 * EHCI controller we need to wait until the EHCI controller
395 * has grabbed the port.
396 * XXX It would be nicer to do this with a tsleep(), but I don't
397 * know how to synchronize the creation of the threads so it
400 usb_delay_ms(sc->sc_bus, 500);
402 /* Make sure first discover does something. */
403 sc->sc_bus->needs_explore = 1;
405 config_pending_decr();
407 while (!sc->sc_dying) {
409 if (usb_noexplore < 2)
413 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
414 usb_noexplore ? 0 : hz * 60);
416 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
419 DPRINTFN(2,("usb_event_thread: woke up\n"));
421 sc->sc_event_thread = NULL;
423 /* In case parent is waiting for us to exit. */
426 DPRINTF(("usb_event_thread: exit\n"));
431 usb_task_thread(void *arg)
433 struct usb_task *task;
440 DPRINTF(("usb_task_thread: start\n"));
444 task = TAILQ_FIRST(&usb_all_tasks);
446 tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
447 task = TAILQ_FIRST(&usb_all_tasks);
449 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
451 TAILQ_REMOVE(&usb_all_tasks, task, next);
454 task->fun(task->arg);
460 #if defined(__NetBSD__) || defined(__OpenBSD__)
462 usbctlprint(void *aux, const char *pnp)
464 /* only "usb"es can attach to host controllers */
466 printf("usb at %s", pnp);
470 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
473 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
475 int unit = USBUNIT(dev);
476 struct usb_softc *sc;
478 if (unit == USB_DEV_MINOR) {
486 USB_GET_SC_OPEN(usb, unit, sc);
495 usbread(dev_t dev, struct uio *uio, int flag)
498 int unit = USBUNIT(dev);
501 if (unit != USB_DEV_MINOR)
504 if (uio->uio_resid != sizeof(struct usb_event))
510 n = usb_get_next_event(&ue);
513 if (flag & IO_NDELAY) {
517 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
523 error = uiomove((void *)&ue, uio->uio_resid, uio);
529 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
531 int unit = USBUNIT(dev);
533 if (unit == USB_DEV_MINOR) {
542 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
544 struct usb_softc *sc;
545 int unit = USBUNIT(devt);
547 if (unit == USB_DEV_MINOR) {
550 /* All handled in the upper FS layer. */
555 usb_async_proc = p->td_proc;
565 USB_GET_SC(usb, unit, sc);
571 #if defined(__FreeBSD__)
572 /* This part should be deleted */
578 usbdebug = ((*(int *)data) & 0x000000ff);
579 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
580 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
582 #endif /* USB_DEBUG */
585 struct usb_ctl_request *ur = (void *)data;
586 int len = UGETW(ur->ucr_request.wLength);
590 int addr = ur->ucr_addr;
594 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
595 if (len < 0 || len > 32768)
597 if (addr < 0 || addr >= USB_MAX_DEVICES ||
598 sc->sc_bus->devices[addr] == 0)
601 iov.iov_base = (caddr_t)ur->ucr_data;
607 uio.uio_segflg = UIO_USERSPACE;
609 ur->ucr_request.bmRequestType & UT_READ ?
610 UIO_READ : UIO_WRITE;
612 ptr = malloc(len, M_TEMP, M_WAITOK);
613 if (uio.uio_rw == UIO_WRITE) {
614 error = uiomove(ptr, len, &uio);
619 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
620 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
621 USBD_DEFAULT_TIMEOUT);
627 if (uio.uio_rw == UIO_READ) {
628 error = uiomove(ptr, len, &uio);
641 struct usb_device_info *di = (void *)data;
642 int addr = di->udi_addr;
643 usbd_device_handle dev;
645 if (addr < 1 || addr >= USB_MAX_DEVICES)
647 dev = sc->sc_bus->devices[addr];
650 usbd_fill_deviceinfo(dev, di, 1);
654 case USB_DEVICESTATS:
655 *(struct usb_device_stats *)data = sc->sc_bus->stats;
665 usbpoll(dev_t dev, int events, usb_proc_ptr p)
667 int revents, mask, s;
668 int unit = USBUNIT(dev);
670 if (unit == USB_DEV_MINOR) {
672 mask = POLLIN | POLLRDNORM;
675 if (events & mask && usb_nevents > 0)
676 revents |= events & mask;
677 if (revents == 0 && events & mask)
678 selrecord(p, &usb_selevent);
683 #if defined(__FreeBSD__)
684 return (0); /* select/poll never wakes up - back compat */
691 /* Explore device tree from the root. */
693 usb_discover(void *v)
695 struct usb_softc *sc = v;
697 #if defined(__FreeBSD__)
698 /* splxxx should be changed to mutexes for preemption safety some day */
702 DPRINTFN(2,("usb_discover\n"));
704 if (usb_noexplore > 1)
709 * We need mutual exclusion while traversing the device tree,
710 * but this is guaranteed since this function is only called
711 * from the event thread for the controller.
713 #if defined(__FreeBSD__)
716 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
717 sc->sc_bus->needs_explore = 0;
718 #if defined(__FreeBSD__)
721 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
722 #if defined(__FreeBSD__)
726 #if defined(__FreeBSD__)
732 usb_needs_explore(usbd_device_handle dev)
734 DPRINTFN(2,("usb_needs_explore\n"));
735 dev->bus->needs_explore = 1;
736 wakeup(&dev->bus->needs_explore);
739 /* Called at splusb() */
741 usb_get_next_event(struct usb_event *ue)
743 struct usb_event_q *ueq;
745 if (usb_nevents <= 0)
747 ueq = TAILQ_FIRST(&usb_events);
750 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
756 TAILQ_REMOVE(&usb_events, ueq, next);
763 usbd_add_dev_event(int type, usbd_device_handle udev)
767 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
768 usb_add_event(type, &ue);
772 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
776 ue.u.ue_driver.ue_cookie = udev->cookie;
777 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
778 sizeof ue.u.ue_driver.ue_devname);
779 usb_add_event(type, &ue);
783 usb_add_event(int type, struct usb_event *uep)
785 struct usb_event_q *ueq;
787 struct timeval thetime;
790 ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
792 ueq->ue.ue_type = type;
794 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
797 if (USB_EVENT_IS_DETACH(type)) {
798 struct usb_event_q *ueqi, *ueqi_next;
800 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
801 ueqi_next = TAILQ_NEXT(ueqi, next);
802 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
803 uep->u.ue_device.udi_cookie.cookie) {
804 TAILQ_REMOVE(&usb_events, ueqi, next);
805 free(ueqi, M_USBDEV);
807 ueqi_next = TAILQ_FIRST(&usb_events);
811 if (usb_nevents >= USB_MAX_EVENTS) {
812 /* Too many queued events, drop an old one. */
813 DPRINTF(("usb: event dropped\n"));
814 (void)usb_get_next_event(&ue);
816 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
819 selwakeup(&usb_selevent);
820 if (usb_async_proc != NULL) {
821 PROC_LOCK(usb_async_proc);
822 psignal(usb_async_proc, SIGIO);
823 PROC_UNLOCK(usb_async_proc);
829 usb_schedsoftintr(usbd_bus_handle bus)
831 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
832 #ifdef USB_USE_SOFTINTR
833 if (bus->use_polling) {
834 bus->methods->soft_intr(bus);
836 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
837 softintr_schedule(bus->soft);
839 if (!callout_pending(&bus->softi))
840 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
842 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
845 bus->methods->soft_intr(bus);
846 #endif /* USB_USE_SOFTINTR */
849 #if defined(__NetBSD__) || defined(__OpenBSD__)
851 usb_activate(device_ptr_t self, enum devact act)
853 struct usb_softc *sc = (struct usb_softc *)self;
854 usbd_device_handle dev = sc->sc_port.device;
862 case DVACT_DEACTIVATE:
864 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
865 for (i = 0; dev->subdevs[i]; i++)
866 rv |= config_deactivate(dev->subdevs[i]);
874 usb_detach(device_ptr_t self, int flags)
876 struct usb_softc *sc = (struct usb_softc *)self;
879 DPRINTF(("usb_detach: start\n"));
883 /* Make all devices disconnect. */
884 if (sc->sc_port.device != NULL)
885 usb_disconnect_port(&sc->sc_port, self);
887 /* Kill off event thread. */
888 if (sc->sc_event_thread != NULL) {
889 wakeup(&sc->sc_bus->needs_explore);
890 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
891 printf("%s: event thread didn't die\n",
892 USBDEVNAME(sc->sc_dev));
893 DPRINTF(("usb_detach: event thread dead\n"));
898 #ifdef USB_USE_SOFTINTR
899 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
900 if (sc->sc_bus->soft != NULL) {
901 softintr_disestablish(sc->sc_bus->soft);
902 sc->sc_bus->soft = NULL;
905 callout_stop(&sc->sc_bus->softi);
909 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
910 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
914 #elif defined(__FreeBSD__)
916 usb_detach(device_t self)
918 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
925 #if defined(__FreeBSD__)
926 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
927 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);