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 $ */
3 /* $DragonFly: src/sys/bus/usb/usb.c,v 1.8 2003/08/07 21:16:47 dillon Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * USB specifications and other documentation can be found at
44 * http://www.usb.org/developers/data/ and
45 * http://www.usb.org/developers/index.html .
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
54 #include <sys/kthread.h>
56 #elif defined(__FreeBSD__)
57 #include <sys/module.h>
59 #include <sys/filio.h>
64 #include <sys/select.h>
65 #include <sys/vnode.h>
66 #include <sys/signalvar.h>
67 #include <sys/sysctl.h>
71 #include "usbdi_util.h"
73 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
74 #define USB_DEV_MINOR 255 /* event queue device */
76 #if defined(__FreeBSD__)
77 MALLOC_DEFINE(M_USB, "USB", "USB");
78 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
79 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
82 #endif /* defined(__FreeBSD__) */
84 #include <machine/bus.h>
87 #include "usb_quirks.h"
89 /* Define this unconditionally in case a kernel module is loaded that
90 * has been compiled with debugging options.
92 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
95 #define DPRINTF(x) if (usbdebug) logprintf x
96 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
98 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
99 &usbdebug, 0, "usb debug level");
101 extern int uhcidebug;
104 extern int ohcidebug;
107 * 0 - do usual exploration
108 * 1 - do not use timeout exploration
109 * >1 - do no exploration
111 int usb_noexplore = 0;
114 #define DPRINTFN(n,x)
118 USBBASEDEVICE sc_dev; /* base device */
119 usbd_bus_handle sc_bus; /* USB controller */
120 struct usbd_port sc_port; /* dummy port for root hub */
122 #if defined(__FreeBSD__)
123 /* This part should be deleted when kthreads is available */
124 struct selinfo sc_consel; /* waiting for connect change */
126 struct proc *sc_event_thread;
132 #if defined(__NetBSD__) || defined(__OpenBSD__)
134 #elif defined(__FreeBSD__)
139 int usbpoll(dev_t, int, usb_proc_ptr);
141 struct cdevsw usb_cdevsw = {
143 /* maj */ USB_CDEV_MAJOR,
149 /* close */ usbclose,
152 /* ioctl */ usbioctl,
155 /* strategy */ nostrategy,
161 Static usbd_status usb_discover(struct usb_softc *);
162 #if defined(__NetBSD__) || defined(__OpenBSD__)
163 Static void usb_create_event_thread(void *);
164 Static void usb_event_thread(void *);
167 #define USB_MAX_EVENTS 50
170 SIMPLEQ_ENTRY(usb_event_q) next;
172 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
173 SIMPLEQ_HEAD_INITIALIZER(usb_events);
174 Static int usb_nevents = 0;
175 Static struct selinfo usb_selevent;
176 Static struct thread *usb_async_proc; /* process who wants USB SIGIO */
177 Static int usb_dev_open = 0;
179 Static int usb_get_next_event(struct usb_event *);
181 #if defined(__NetBSD__) || defined(__OpenBSD__)
182 /* Flag to see if we are in the cold boot process. */
186 Static const char *usbrev_str[] = USBREV_STR;
188 USB_DECLARE_DRIVER_INIT(usb,
189 DEVMETHOD(device_suspend, bus_generic_suspend),
190 DEVMETHOD(device_resume, bus_generic_resume),
191 DEVMETHOD(device_shutdown, bus_generic_shutdown)
196 DPRINTF(("usbd_match\n"));
197 return (UMATCH_GENERIC);
202 #if defined(__NetBSD__) || defined(__OpenBSD__)
203 struct usb_softc *sc = (struct usb_softc *)self;
204 #elif defined(__FreeBSD__)
205 struct usb_softc *sc = device_get_softc(self);
206 void *aux = device_get_ivars(self);
207 static int global_init_done = 0;
209 usbd_device_handle dev;
215 DPRINTF(("usbd_attach\n"));
219 sc->sc_bus->usbctl = sc;
220 sc->sc_port.power = USB_MAX_POWER;
222 #if defined(__FreeBSD__)
223 printf("%s", USBDEVNAME(sc->sc_dev));
225 usbrev = sc->sc_bus->usbrev;
226 printf(": USB revision %s", usbrev_str[usbrev]);
227 if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
228 printf(", not supported\n");
229 USB_ATTACH_ERROR_RETURN;
233 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
236 dev = sc->sc_port.device;
237 if (dev->hub == NULL) {
239 printf("%s: root device is not a hub\n",
240 USBDEVNAME(sc->sc_dev));
241 USB_ATTACH_ERROR_RETURN;
243 sc->sc_bus->root_hub = dev;
246 * Turning this code off will delay attachment of USB devices
247 * until the USB event thread is running, which means that
248 * the keyboard will not work until after cold boot.
251 sc->sc_bus->use_polling++;
252 dev->hub->explore(sc->sc_bus->root_hub);
253 sc->sc_bus->use_polling--;
257 printf("%s: root hub problem, error=%d\n",
258 USBDEVNAME(sc->sc_dev), err);
262 kthread_create(usb_create_event_thread, sc);
264 #if defined(__FreeBSD__)
265 /* The per controller devices (used for usb_discover) */
266 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
267 0644, "usb%d", device_get_unit(self));
268 if (!global_init_done) {
269 /* The device spitting out events */
270 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
272 global_init_done = 1;
276 USB_ATTACH_SUCCESS_RETURN;
279 #if defined(__NetBSD__) || defined(__OpenBSD__)
281 usb_create_event_thread(void *arg)
283 struct usb_softc *sc = arg;
285 if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
286 "%s", sc->sc_dev.dv_xname)) {
287 printf("%s: unable to create event thread for\n",
288 sc->sc_dev.dv_xname);
289 panic("usb_create_event_thread");
294 usb_event_thread(void *arg)
296 struct usb_softc *sc = arg;
298 DPRINTF(("usb_event_thread: start\n"));
300 while (!sc->sc_dying) {
302 if (usb_noexplore < 2)
305 (void)tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
311 DPRINTFN(2,("usb_event_thread: woke up\n"));
313 sc->sc_event_thread = 0;
315 /* In case parent is waiting for us to exit. */
318 DPRINTF(("usb_event_thread: exit\n"));
323 usbctlprint(void *aux, const char *pnp)
325 /* only "usb"es can attach to host controllers */
327 printf("usb at %s", pnp);
331 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
334 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
336 int unit = USBUNIT(dev);
337 struct usb_softc *sc;
339 if (unit == USB_DEV_MINOR) {
343 usb_async_proc = NULL;
346 USB_GET_SC_OPEN(usb, unit, sc);
356 usbread(dev_t dev, struct uio *uio, int flag)
359 int unit = USBUNIT(dev);
362 if (unit != USB_DEV_MINOR)
365 if (uio->uio_resid != sizeof(struct usb_event))
371 n = usb_get_next_event(&ue);
374 if (flag & IO_NDELAY) {
378 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
384 error = uiomove((void *)&ue, uio->uio_resid, uio);
390 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
392 int unit = USBUNIT(dev);
394 if (unit == USB_DEV_MINOR) {
395 usb_async_proc = NULL;
403 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
405 struct usb_softc *sc;
406 int unit = USBUNIT(devt);
408 if (unit == USB_DEV_MINOR) {
411 /* All handled in the upper FS layer. */
418 usb_async_proc = NULL;
426 USB_GET_SC(usb, unit, sc);
432 #if defined(__FreeBSD__)
433 /* This part should be deleted when kthreads is available */
440 usbdebug = ((*(int *)data) & 0x000000ff);
441 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
442 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
447 struct usb_ctl_request *ur = (void *)data;
448 int len = UGETW(ur->ucr_request.wLength);
452 int addr = ur->ucr_addr;
456 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
457 if (len < 0 || len > 32768)
459 if (addr < 0 || addr >= USB_MAX_DEVICES ||
460 sc->sc_bus->devices[addr] == 0)
463 iov.iov_base = (caddr_t)ur->ucr_data;
469 uio.uio_segflg = UIO_USERSPACE;
471 ur->ucr_request.bmRequestType & UT_READ ?
472 UIO_READ : UIO_WRITE;
474 ptr = malloc(len, M_TEMP, M_WAITOK);
475 if (uio.uio_rw == UIO_WRITE) {
476 error = uiomove(ptr, len, &uio);
481 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
482 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen);
488 if (uio.uio_rw == UIO_READ) {
489 error = uiomove(ptr, len, &uio);
502 struct usb_device_info *di = (void *)data;
503 int addr = di->udi_addr;
504 usbd_device_handle dev;
506 if (addr < 1 || addr >= USB_MAX_DEVICES)
508 dev = sc->sc_bus->devices[addr];
511 usbd_fill_deviceinfo(dev, di);
515 case USB_DEVICESTATS:
516 *(struct usb_device_stats *)data = sc->sc_bus->stats;
526 usbpoll(dev_t dev, int events, usb_proc_ptr p)
528 int revents, mask, s;
529 int unit = USBUNIT(dev);
531 if (unit == USB_DEV_MINOR) {
533 mask = POLLIN | POLLRDNORM;
536 if ((events & mask) && usb_nevents > 0)
537 revents |= events & mask;
538 if (revents == 0 && (events & mask)) {
539 DPRINTFN(2,("usb: sleeping on %p\n", &usb_selevent));
540 selrecord(p, &usb_selevent);
546 #if defined(__FreeBSD__)
547 /* This part should be deleted when kthreads is available */
548 struct usb_softc *sc;
550 USB_GET_SC(usb, unit, sc);
553 mask = POLLOUT | POLLRDNORM;
556 if ((events & mask) && sc->sc_bus->needs_explore)
557 revents |= events & mask;
558 if (revents == 0 && (events & mask))
559 selrecord(p, &sc->sc_consel);
569 /* Explore device tree from the root. */
571 usb_discover(struct usb_softc *sc)
573 #if defined(__FreeBSD__)
574 /* The splxxx parts should be deleted when kthreads is available */
579 * We need mutual exclusion while traversing the device tree,
580 * but this is guaranteed since this function is only called
581 * from the event thread for the controller.
583 #if defined(__FreeBSD__)
586 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
587 sc->sc_bus->needs_explore = 0;
588 #if defined(__FreeBSD__)
591 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
592 #if defined(__FreeBSD__)
596 #if defined(__FreeBSD__)
600 return (USBD_NORMAL_COMPLETION);
604 usb_needs_explore(usbd_bus_handle bus)
606 bus->needs_explore = 1;
607 #if defined(__FreeBSD__)
608 /* This part should be deleted when kthreads is available */
609 selwakeup(&bus->usbctl->sc_consel);
611 wakeup(&bus->needs_explore);
614 /* Called at splusb() */
616 usb_get_next_event(struct usb_event *ue)
618 struct usb_event_q *ueq;
620 if (usb_nevents <= 0)
622 ueq = SIMPLEQ_FIRST(&usb_events);
624 SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
631 usbd_add_event(int type, usbd_device_handle dev)
633 struct usb_event_q *ueq;
635 struct timeval thetime;
639 if (++usb_nevents >= USB_MAX_EVENTS) {
640 /* Too many queued events, drop an old one. */
641 DPRINTF(("usb: event dropped\n"));
642 (void)usb_get_next_event(&ue);
644 /* Don't want to wait here inside splusb() */
645 ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
647 printf("usb: no memory, event dropped\n");
651 ueq->ue.ue_type = type;
652 ueq->ue.u.ue_driver.ue_cookie = dev->cookie;
653 usbd_fill_deviceinfo(dev, &ueq->ue.u.ue_device);
655 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
656 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
658 selwakeup(&usb_selevent);
659 if (usb_async_proc != NULL && usb_async_proc->td_proc)
660 psignal(usb_async_proc->td_proc, SIGIO);
664 #if defined(__NetBSD__) || defined(__OpenBSD__)
666 usb_activate(device_ptr_t self, devact act)
668 struct usb_softc *sc = (struct usb_softc *)self;
669 usbd_device_handle dev = sc->sc_port.device;
677 case DVACT_DEACTIVATE:
679 if (dev && dev->cdesc && dev->subdevs) {
680 for (i = 0; dev->subdevs[i]; i++)
681 rv |= config_deactivate(dev->subdevs[i]);
689 usb_detach(device_ptr_t self, int flags)
691 struct usb_softc *sc = (struct usb_softc *)self;
693 DPRINTF(("usb_detach: start\n"));
697 /* Make all devices disconnect. */
698 if (sc->sc_port.device)
699 usb_disconnect_port(&sc->sc_port, self);
701 /* Kill off event thread. */
702 if (sc->sc_event_thread) {
703 wakeup(&sc->sc_bus->needs_explore);
704 if (tsleep(sc, 0, "usbdet", hz * 60))
705 printf("%s: event thread didn't die\n",
706 USBDEVNAME(sc->sc_dev));
707 DPRINTF(("usb_detach: event thread dead\n"));
713 #elif defined(__FreeBSD__)
715 usb_detach(device_t self)
717 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
724 #if defined(__FreeBSD__)
725 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
726 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);