1 /* $NetBSD: usb.c,v 1.33 1999/11/22 21:57:09 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb.c,v 1.26.2.9 2002/11/13 15:15:22 joe 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>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/kthread.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
58 #include <sys/filio.h>
63 #include <sys/select.h>
64 #include <sys/vnode.h>
65 #include <sys/signalvar.h>
66 #include <sys/sysctl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
72 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
73 #define USB_DEV_MINOR 255 /* event queue device */
75 #if defined(__FreeBSD__)
76 MALLOC_DEFINE(M_USB, "USB", "USB");
77 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
78 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
81 #endif /* defined(__FreeBSD__) */
83 #include <machine/bus.h>
85 #include <dev/usb/usbdivar.h>
86 #include <dev/usb/usb_quirks.h>
88 /* Define this unconditionally in case a kernel module is loaded that
89 * has been compiled with debugging options.
91 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
94 #define DPRINTF(x) if (usbdebug) logprintf x
95 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
97 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
98 &usbdebug, 0, "usb debug level");
100 extern int uhcidebug;
103 extern int ohcidebug;
106 * 0 - do usual exploration
107 * 1 - do not use timeout exploration
108 * >1 - do no exploration
110 int usb_noexplore = 0;
113 #define DPRINTFN(n,x)
117 USBBASEDEVICE sc_dev; /* base device */
118 usbd_bus_handle sc_bus; /* USB controller */
119 struct usbd_port sc_port; /* dummy port for root hub */
121 #if defined(__FreeBSD__)
122 /* This part should be deleted when kthreads is available */
123 struct selinfo sc_consel; /* waiting for connect change */
125 struct proc *sc_event_thread;
131 #if defined(__NetBSD__) || defined(__OpenBSD__)
133 #elif defined(__FreeBSD__)
138 int usbpoll(dev_t, int, usb_proc_ptr);
140 struct cdevsw usb_cdevsw = {
142 /* close */ usbclose,
145 /* ioctl */ usbioctl,
148 /* strategy */ nostrategy,
150 /* maj */ USB_CDEV_MAJOR,
158 Static usbd_status usb_discover(struct usb_softc *);
159 #if defined(__NetBSD__) || defined(__OpenBSD__)
160 Static void usb_create_event_thread(void *);
161 Static void usb_event_thread(void *);
164 #define USB_MAX_EVENTS 50
167 SIMPLEQ_ENTRY(usb_event_q) next;
169 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
170 SIMPLEQ_HEAD_INITIALIZER(usb_events);
171 Static int usb_nevents = 0;
172 Static struct selinfo usb_selevent;
173 Static struct proc *usb_async_proc; /* process who wants USB SIGIO */
174 Static int usb_dev_open = 0;
176 Static int usb_get_next_event(struct usb_event *);
178 #if defined(__NetBSD__) || defined(__OpenBSD__)
179 /* Flag to see if we are in the cold boot process. */
183 Static const char *usbrev_str[] = USBREV_STR;
185 USB_DECLARE_DRIVER_INIT(usb,
186 DEVMETHOD(device_suspend, bus_generic_suspend),
187 DEVMETHOD(device_resume, bus_generic_resume),
188 DEVMETHOD(device_shutdown, bus_generic_shutdown)
193 DPRINTF(("usbd_match\n"));
194 return (UMATCH_GENERIC);
199 #if defined(__NetBSD__) || defined(__OpenBSD__)
200 struct usb_softc *sc = (struct usb_softc *)self;
201 #elif defined(__FreeBSD__)
202 struct usb_softc *sc = device_get_softc(self);
203 void *aux = device_get_ivars(self);
204 static int global_init_done = 0;
206 usbd_device_handle dev;
212 DPRINTF(("usbd_attach\n"));
216 sc->sc_bus->usbctl = sc;
217 sc->sc_port.power = USB_MAX_POWER;
219 #if defined(__FreeBSD__)
220 printf("%s", USBDEVNAME(sc->sc_dev));
222 usbrev = sc->sc_bus->usbrev;
223 printf(": USB revision %s", usbrev_str[usbrev]);
224 if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
225 printf(", not supported\n");
226 USB_ATTACH_ERROR_RETURN;
230 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
233 dev = sc->sc_port.device;
234 if (dev->hub == NULL) {
236 printf("%s: root device is not a hub\n",
237 USBDEVNAME(sc->sc_dev));
238 USB_ATTACH_ERROR_RETURN;
240 sc->sc_bus->root_hub = dev;
243 * Turning this code off will delay attachment of USB devices
244 * until the USB event thread is running, which means that
245 * the keyboard will not work until after cold boot.
248 sc->sc_bus->use_polling++;
249 dev->hub->explore(sc->sc_bus->root_hub);
250 sc->sc_bus->use_polling--;
254 printf("%s: root hub problem, error=%d\n",
255 USBDEVNAME(sc->sc_dev), err);
259 kthread_create(usb_create_event_thread, sc);
261 #if defined(__FreeBSD__)
262 /* The per controller devices (used for usb_discover) */
263 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
264 0644, "usb%d", device_get_unit(self));
265 if (!global_init_done) {
266 /* The device spitting out events */
267 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
269 global_init_done = 1;
273 USB_ATTACH_SUCCESS_RETURN;
276 #if defined(__NetBSD__) || defined(__OpenBSD__)
278 usb_create_event_thread(void *arg)
280 struct usb_softc *sc = arg;
282 if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
283 "%s", sc->sc_dev.dv_xname)) {
284 printf("%s: unable to create event thread for\n",
285 sc->sc_dev.dv_xname);
286 panic("usb_create_event_thread");
291 usb_event_thread(void *arg)
293 struct usb_softc *sc = arg;
295 DPRINTF(("usb_event_thread: start\n"));
297 while (!sc->sc_dying) {
299 if (usb_noexplore < 2)
302 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
308 DPRINTFN(2,("usb_event_thread: woke up\n"));
310 sc->sc_event_thread = 0;
312 /* In case parent is waiting for us to exit. */
315 DPRINTF(("usb_event_thread: exit\n"));
320 usbctlprint(void *aux, const char *pnp)
322 /* only "usb"es can attach to host controllers */
324 printf("usb at %s", pnp);
328 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
331 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
333 int unit = USBUNIT(dev);
334 struct usb_softc *sc;
336 if (unit == USB_DEV_MINOR) {
343 USB_GET_SC_OPEN(usb, unit, sc);
353 usbread(dev_t dev, struct uio *uio, int flag)
356 int unit = USBUNIT(dev);
359 if (unit != USB_DEV_MINOR)
362 if (uio->uio_resid != sizeof(struct usb_event))
368 n = usb_get_next_event(&ue);
371 if (flag & IO_NDELAY) {
375 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
381 error = uiomove((void *)&ue, uio->uio_resid, uio);
387 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
389 int unit = USBUNIT(dev);
391 if (unit == USB_DEV_MINOR) {
400 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
402 struct usb_softc *sc;
403 int unit = USBUNIT(devt);
405 if (unit == USB_DEV_MINOR) {
408 /* All handled in the upper FS layer. */
423 USB_GET_SC(usb, unit, sc);
429 #if defined(__FreeBSD__)
430 /* This part should be deleted when kthreads is available */
437 usbdebug = ((*(int *)data) & 0x000000ff);
438 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
439 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
444 struct usb_ctl_request *ur = (void *)data;
445 int len = UGETW(ur->ucr_request.wLength);
449 int addr = ur->ucr_addr;
453 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
454 if (len < 0 || len > 32768)
456 if (addr < 0 || addr >= USB_MAX_DEVICES ||
457 sc->sc_bus->devices[addr] == 0)
460 iov.iov_base = (caddr_t)ur->ucr_data;
466 uio.uio_segflg = UIO_USERSPACE;
468 ur->ucr_request.bmRequestType & UT_READ ?
469 UIO_READ : UIO_WRITE;
471 ptr = malloc(len, M_TEMP, M_WAITOK);
472 if (uio.uio_rw == UIO_WRITE) {
473 error = uiomove(ptr, len, &uio);
478 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
479 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen);
485 if (uio.uio_rw == UIO_READ) {
486 error = uiomove(ptr, len, &uio);
499 struct usb_device_info *di = (void *)data;
500 int addr = di->udi_addr;
501 usbd_device_handle dev;
503 if (addr < 1 || addr >= USB_MAX_DEVICES)
505 dev = sc->sc_bus->devices[addr];
508 usbd_fill_deviceinfo(dev, di);
512 case USB_DEVICESTATS:
513 *(struct usb_device_stats *)data = sc->sc_bus->stats;
523 usbpoll(dev_t dev, int events, usb_proc_ptr p)
525 int revents, mask, s;
526 int unit = USBUNIT(dev);
528 if (unit == USB_DEV_MINOR) {
530 mask = POLLIN | POLLRDNORM;
533 if ((events & mask) && usb_nevents > 0)
534 revents |= events & mask;
535 if (revents == 0 && (events & mask)) {
536 DPRINTFN(2,("usb: sleeping on %p\n", &usb_selevent));
537 selrecord(p, &usb_selevent);
543 #if defined(__FreeBSD__)
544 /* This part should be deleted when kthreads is available */
545 struct usb_softc *sc;
547 USB_GET_SC(usb, unit, sc);
550 mask = POLLOUT | POLLRDNORM;
553 if ((events & mask) && sc->sc_bus->needs_explore)
554 revents |= events & mask;
555 if (revents == 0 && (events & mask))
556 selrecord(p, &sc->sc_consel);
566 /* Explore device tree from the root. */
568 usb_discover(struct usb_softc *sc)
570 #if defined(__FreeBSD__)
571 /* The splxxx parts should be deleted when kthreads is available */
576 * We need mutual exclusion while traversing the device tree,
577 * but this is guaranteed since this function is only called
578 * from the event thread for the controller.
580 #if defined(__FreeBSD__)
583 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
584 sc->sc_bus->needs_explore = 0;
585 #if defined(__FreeBSD__)
588 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
589 #if defined(__FreeBSD__)
593 #if defined(__FreeBSD__)
597 return (USBD_NORMAL_COMPLETION);
601 usb_needs_explore(usbd_bus_handle bus)
603 bus->needs_explore = 1;
604 #if defined(__FreeBSD__)
605 /* This part should be deleted when kthreads is available */
606 selwakeup(&bus->usbctl->sc_consel);
608 wakeup(&bus->needs_explore);
611 /* Called at splusb() */
613 usb_get_next_event(struct usb_event *ue)
615 struct usb_event_q *ueq;
617 if (usb_nevents <= 0)
619 ueq = SIMPLEQ_FIRST(&usb_events);
621 SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
628 usbd_add_event(int type, usbd_device_handle dev)
630 struct usb_event_q *ueq;
632 struct timeval thetime;
636 if (++usb_nevents >= USB_MAX_EVENTS) {
637 /* Too many queued events, drop an old one. */
638 DPRINTF(("usb: event dropped\n"));
639 (void)usb_get_next_event(&ue);
641 /* Don't want to wait here inside splusb() */
642 ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
644 printf("usb: no memory, event dropped\n");
648 ueq->ue.ue_type = type;
649 ueq->ue.u.ue_driver.ue_cookie = dev->cookie;
650 usbd_fill_deviceinfo(dev, &ueq->ue.u.ue_device);
652 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
653 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
655 selwakeup(&usb_selevent);
656 if (usb_async_proc != NULL)
657 psignal(usb_async_proc, SIGIO);
661 #if defined(__NetBSD__) || defined(__OpenBSD__)
663 usb_activate(device_ptr_t self, devact act)
665 struct usb_softc *sc = (struct usb_softc *)self;
666 usbd_device_handle dev = sc->sc_port.device;
674 case DVACT_DEACTIVATE:
676 if (dev && dev->cdesc && dev->subdevs) {
677 for (i = 0; dev->subdevs[i]; i++)
678 rv |= config_deactivate(dev->subdevs[i]);
686 usb_detach(device_ptr_t self, int flags)
688 struct usb_softc *sc = (struct usb_softc *)self;
690 DPRINTF(("usb_detach: start\n"));
694 /* Make all devices disconnect. */
695 if (sc->sc_port.device)
696 usb_disconnect_port(&sc->sc_port, self);
698 /* Kill off event thread. */
699 if (sc->sc_event_thread) {
700 wakeup(&sc->sc_bus->needs_explore);
701 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
702 printf("%s: event thread didn't die\n",
703 USBDEVNAME(sc->sc_dev));
704 DPRINTF(("usb_detach: event thread dead\n"));
710 #elif defined(__FreeBSD__)
712 usb_detach(device_t self)
714 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
721 #if defined(__FreeBSD__)
722 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
723 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);