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.5 2003/06/27 01:53:24 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>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/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>
86 #include <dev/usb/usbdivar.h>
87 #include <dev/usb/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 /* close */ usbclose,
146 /* ioctl */ usbioctl,
149 /* strategy */ nostrategy,
151 /* maj */ USB_CDEV_MAJOR,
159 Static usbd_status usb_discover(struct usb_softc *);
160 #if defined(__NetBSD__) || defined(__OpenBSD__)
161 Static void usb_create_event_thread(void *);
162 Static void usb_event_thread(void *);
165 #define USB_MAX_EVENTS 50
168 SIMPLEQ_ENTRY(usb_event_q) next;
170 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
171 SIMPLEQ_HEAD_INITIALIZER(usb_events);
172 Static int usb_nevents = 0;
173 Static struct selinfo usb_selevent;
174 Static struct thread *usb_async_proc; /* process who wants USB SIGIO */
175 Static int usb_dev_open = 0;
177 Static int usb_get_next_event(struct usb_event *);
179 #if defined(__NetBSD__) || defined(__OpenBSD__)
180 /* Flag to see if we are in the cold boot process. */
184 Static const char *usbrev_str[] = USBREV_STR;
186 USB_DECLARE_DRIVER_INIT(usb,
187 DEVMETHOD(device_suspend, bus_generic_suspend),
188 DEVMETHOD(device_resume, bus_generic_resume),
189 DEVMETHOD(device_shutdown, bus_generic_shutdown)
194 DPRINTF(("usbd_match\n"));
195 return (UMATCH_GENERIC);
200 #if defined(__NetBSD__) || defined(__OpenBSD__)
201 struct usb_softc *sc = (struct usb_softc *)self;
202 #elif defined(__FreeBSD__)
203 struct usb_softc *sc = device_get_softc(self);
204 void *aux = device_get_ivars(self);
205 static int global_init_done = 0;
207 usbd_device_handle dev;
213 DPRINTF(("usbd_attach\n"));
217 sc->sc_bus->usbctl = sc;
218 sc->sc_port.power = USB_MAX_POWER;
220 #if defined(__FreeBSD__)
221 printf("%s", USBDEVNAME(sc->sc_dev));
223 usbrev = sc->sc_bus->usbrev;
224 printf(": USB revision %s", usbrev_str[usbrev]);
225 if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
226 printf(", not supported\n");
227 USB_ATTACH_ERROR_RETURN;
231 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
234 dev = sc->sc_port.device;
235 if (dev->hub == NULL) {
237 printf("%s: root device is not a hub\n",
238 USBDEVNAME(sc->sc_dev));
239 USB_ATTACH_ERROR_RETURN;
241 sc->sc_bus->root_hub = dev;
244 * Turning this code off will delay attachment of USB devices
245 * until the USB event thread is running, which means that
246 * the keyboard will not work until after cold boot.
249 sc->sc_bus->use_polling++;
250 dev->hub->explore(sc->sc_bus->root_hub);
251 sc->sc_bus->use_polling--;
255 printf("%s: root hub problem, error=%d\n",
256 USBDEVNAME(sc->sc_dev), err);
260 kthread_create(usb_create_event_thread, sc);
262 #if defined(__FreeBSD__)
263 /* The per controller devices (used for usb_discover) */
264 make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
265 0644, "usb%d", device_get_unit(self));
266 if (!global_init_done) {
267 /* The device spitting out events */
268 make_dev(&usb_cdevsw, USB_DEV_MINOR, UID_ROOT, GID_OPERATOR,
270 global_init_done = 1;
274 USB_ATTACH_SUCCESS_RETURN;
277 #if defined(__NetBSD__) || defined(__OpenBSD__)
279 usb_create_event_thread(void *arg)
281 struct usb_softc *sc = arg;
283 if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
284 "%s", sc->sc_dev.dv_xname)) {
285 printf("%s: unable to create event thread for\n",
286 sc->sc_dev.dv_xname);
287 panic("usb_create_event_thread");
292 usb_event_thread(void *arg)
294 struct usb_softc *sc = arg;
296 DPRINTF(("usb_event_thread: start\n"));
298 while (!sc->sc_dying) {
300 if (usb_noexplore < 2)
303 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
309 DPRINTFN(2,("usb_event_thread: woke up\n"));
311 sc->sc_event_thread = 0;
313 /* In case parent is waiting for us to exit. */
316 DPRINTF(("usb_event_thread: exit\n"));
321 usbctlprint(void *aux, const char *pnp)
323 /* only "usb"es can attach to host controllers */
325 printf("usb at %s", pnp);
329 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
332 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
334 int unit = USBUNIT(dev);
335 struct usb_softc *sc;
337 if (unit == USB_DEV_MINOR) {
341 usb_async_proc = NULL;
344 USB_GET_SC_OPEN(usb, unit, sc);
354 usbread(dev_t dev, struct uio *uio, int flag)
357 int unit = USBUNIT(dev);
360 if (unit != USB_DEV_MINOR)
363 if (uio->uio_resid != sizeof(struct usb_event))
369 n = usb_get_next_event(&ue);
372 if (flag & IO_NDELAY) {
376 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
382 error = uiomove((void *)&ue, uio->uio_resid, uio);
388 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
390 int unit = USBUNIT(dev);
392 if (unit == USB_DEV_MINOR) {
393 usb_async_proc = NULL;
401 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
403 struct usb_softc *sc;
404 int unit = USBUNIT(devt);
406 if (unit == USB_DEV_MINOR) {
409 /* All handled in the upper FS layer. */
416 usb_async_proc = NULL;
424 USB_GET_SC(usb, unit, sc);
430 #if defined(__FreeBSD__)
431 /* This part should be deleted when kthreads is available */
438 usbdebug = ((*(int *)data) & 0x000000ff);
439 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
440 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
445 struct usb_ctl_request *ur = (void *)data;
446 int len = UGETW(ur->ucr_request.wLength);
450 int addr = ur->ucr_addr;
454 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
455 if (len < 0 || len > 32768)
457 if (addr < 0 || addr >= USB_MAX_DEVICES ||
458 sc->sc_bus->devices[addr] == 0)
461 iov.iov_base = (caddr_t)ur->ucr_data;
467 uio.uio_segflg = UIO_USERSPACE;
469 ur->ucr_request.bmRequestType & UT_READ ?
470 UIO_READ : UIO_WRITE;
472 ptr = malloc(len, M_TEMP, M_WAITOK);
473 if (uio.uio_rw == UIO_WRITE) {
474 error = uiomove(ptr, len, &uio);
479 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
480 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen);
486 if (uio.uio_rw == UIO_READ) {
487 error = uiomove(ptr, len, &uio);
500 struct usb_device_info *di = (void *)data;
501 int addr = di->udi_addr;
502 usbd_device_handle dev;
504 if (addr < 1 || addr >= USB_MAX_DEVICES)
506 dev = sc->sc_bus->devices[addr];
509 usbd_fill_deviceinfo(dev, di);
513 case USB_DEVICESTATS:
514 *(struct usb_device_stats *)data = sc->sc_bus->stats;
524 usbpoll(dev_t dev, int events, usb_proc_ptr p)
526 int revents, mask, s;
527 int unit = USBUNIT(dev);
529 if (unit == USB_DEV_MINOR) {
531 mask = POLLIN | POLLRDNORM;
534 if ((events & mask) && usb_nevents > 0)
535 revents |= events & mask;
536 if (revents == 0 && (events & mask)) {
537 DPRINTFN(2,("usb: sleeping on %p\n", &usb_selevent));
538 selrecord(p, &usb_selevent);
544 #if defined(__FreeBSD__)
545 /* This part should be deleted when kthreads is available */
546 struct usb_softc *sc;
548 USB_GET_SC(usb, unit, sc);
551 mask = POLLOUT | POLLRDNORM;
554 if ((events & mask) && sc->sc_bus->needs_explore)
555 revents |= events & mask;
556 if (revents == 0 && (events & mask))
557 selrecord(p, &sc->sc_consel);
567 /* Explore device tree from the root. */
569 usb_discover(struct usb_softc *sc)
571 #if defined(__FreeBSD__)
572 /* The splxxx parts should be deleted when kthreads is available */
577 * We need mutual exclusion while traversing the device tree,
578 * but this is guaranteed since this function is only called
579 * from the event thread for the controller.
581 #if defined(__FreeBSD__)
584 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
585 sc->sc_bus->needs_explore = 0;
586 #if defined(__FreeBSD__)
589 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
590 #if defined(__FreeBSD__)
594 #if defined(__FreeBSD__)
598 return (USBD_NORMAL_COMPLETION);
602 usb_needs_explore(usbd_bus_handle bus)
604 bus->needs_explore = 1;
605 #if defined(__FreeBSD__)
606 /* This part should be deleted when kthreads is available */
607 selwakeup(&bus->usbctl->sc_consel);
609 wakeup(&bus->needs_explore);
612 /* Called at splusb() */
614 usb_get_next_event(struct usb_event *ue)
616 struct usb_event_q *ueq;
618 if (usb_nevents <= 0)
620 ueq = SIMPLEQ_FIRST(&usb_events);
622 SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
629 usbd_add_event(int type, usbd_device_handle dev)
631 struct usb_event_q *ueq;
633 struct timeval thetime;
637 if (++usb_nevents >= USB_MAX_EVENTS) {
638 /* Too many queued events, drop an old one. */
639 DPRINTF(("usb: event dropped\n"));
640 (void)usb_get_next_event(&ue);
642 /* Don't want to wait here inside splusb() */
643 ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
645 printf("usb: no memory, event dropped\n");
649 ueq->ue.ue_type = type;
650 ueq->ue.u.ue_driver.ue_cookie = dev->cookie;
651 usbd_fill_deviceinfo(dev, &ueq->ue.u.ue_device);
653 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
654 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
656 selwakeup(&usb_selevent);
657 if (usb_async_proc != NULL && usb_async_proc->td_proc)
658 psignal(usb_async_proc->td_proc, SIGIO);
662 #if defined(__NetBSD__) || defined(__OpenBSD__)
664 usb_activate(device_ptr_t self, devact act)
666 struct usb_softc *sc = (struct usb_softc *)self;
667 usbd_device_handle dev = sc->sc_port.device;
675 case DVACT_DEACTIVATE:
677 if (dev && dev->cdesc && dev->subdevs) {
678 for (i = 0; dev->subdevs[i]; i++)
679 rv |= config_deactivate(dev->subdevs[i]);
687 usb_detach(device_ptr_t self, int flags)
689 struct usb_softc *sc = (struct usb_softc *)self;
691 DPRINTF(("usb_detach: start\n"));
695 /* Make all devices disconnect. */
696 if (sc->sc_port.device)
697 usb_disconnect_port(&sc->sc_port, self);
699 /* Kill off event thread. */
700 if (sc->sc_event_thread) {
701 wakeup(&sc->sc_bus->needs_explore);
702 if (tsleep(sc, PWAIT, "usbdet", hz * 60))
703 printf("%s: event thread didn't die\n",
704 USBDEVNAME(sc->sc_dev));
705 DPRINTF(("usb_detach: event thread dead\n"));
711 #elif defined(__FreeBSD__)
713 usb_detach(device_t self)
715 DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
722 #if defined(__FreeBSD__)
723 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
724 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);