summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb/usb.c
diff options
context:
space:
mode:
authornate <nate@openbsd.org>2002-05-07 18:08:04 +0000
committernate <nate@openbsd.org>2002-05-07 18:08:04 +0000
commit5cd2922027cbe695029751aaaedcb9d3378bc016 (patch)
tree08f49c8f84e3554a0597aa466d12449dd6a4fb74 /sys/dev/usb/usb.c
parentgrep is right start (diff)
downloadwireguard-openbsd-5cd2922027cbe695029751aaaedcb9d3378bc016.tar.xz
wireguard-openbsd-5cd2922027cbe695029751aaaedcb9d3378bc016.zip
Sync ulpt driver with NetBSD
Diffstat (limited to 'sys/dev/usb/usb.c')
-rw-r--r--sys/dev/usb/usb.c236
1 files changed, 170 insertions, 66 deletions
diff --git a/sys/dev/usb/usb.c b/sys/dev/usb/usb.c
index 03e769920d3..3cc61c6bb74 100644
--- a/sys/dev/usb/usb.c
+++ b/sys/dev/usb/usb.c
@@ -1,9 +1,8 @@
-/* $OpenBSD: usb.c,v 1.17 2002/05/02 20:08:04 nate Exp $ */
-/* $NetBSD: usb.c,v 1.53 2001/01/23 17:04:30 augustss Exp $ */
-/* $FreeBSD: src/sys/dev/usb/usb.c,v 1.20 1999/11/17 22:33:46 n_hibma Exp $ */
+/* $OpenBSD: usb.c,v 1.18 2002/05/07 18:08:04 nate Exp $ */
+/* $NetBSD: usb.c,v 1.69 2002/04/23 06:34:11 augustss Exp $ */
/*
- * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
@@ -53,6 +52,7 @@
#include <sys/kthread.h>
#include <sys/proc.h>
#include <sys/conf.h>
+#include <sys/fcntl.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/vnode.h>
@@ -79,7 +79,7 @@ int uhcidebug;
#ifdef OHCI_DEBUG
int ohcidebug;
#endif
-/*
+/*
* 0 - do usual exploration
* 1 - do not use timeout exploration
* >1 - do no exploration
@@ -95,30 +95,31 @@ struct usb_softc {
usbd_bus_handle sc_bus; /* USB controller */
struct usbd_port sc_port; /* dummy port for root hub */
- TAILQ_HEAD(, usb_task) sc_tasks;
- struct proc *sc_event_thread;
-
- struct usb_task sc_exp_task;
+ usb_proc_ptr sc_event_thread;
char sc_dying;
};
+TAILQ_HEAD(, usb_task) usb_all_tasks;
+
cdev_decl(usb);
Static void usb_discover(void *);
Static void usb_create_event_thread(void *);
Static void usb_event_thread(void *);
+Static void usb_task_thread(void *);
+Static usb_proc_ptr usb_task_thread_proc = NULL;
#define USB_MAX_EVENTS 100
struct usb_event_q {
struct usb_event ue;
SIMPLEQ_ENTRY(usb_event_q) next;
};
-Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
+Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
SIMPLEQ_HEAD_INITIALIZER(usb_events);
Static int usb_nevents = 0;
Static struct selinfo usb_selevent;
-Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
+Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */
Static int usb_dev_open = 0;
Static void usb_add_event(int, struct usb_event *);
@@ -140,22 +141,29 @@ USB_ATTACH(usb)
usbd_device_handle dev;
usbd_status err;
int usbrev;
+ int speed;
struct usb_event ue;
-
+
DPRINTF(("usbd_attach\n"));
usbd_init();
sc->sc_bus = aux;
sc->sc_bus->usbctl = sc;
sc->sc_port.power = USB_MAX_POWER;
- TAILQ_INIT(&sc->sc_tasks);
-
- usb_init_task(&sc->sc_exp_task, usb_discover, sc);
usbrev = sc->sc_bus->usbrev;
printf(": USB revision %s", usbrev_str[usbrev]);
- if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
+ switch (usbrev) {
+ case USBREV_1_0:
+ case USBREV_1_1:
+ speed = USB_SPEED_FULL;
+ break;
+ case USBREV_2_0:
+ speed = USB_SPEED_HIGH;
+ break;
+ default:
printf(", not supported\n");
+ sc->sc_dying = 1;
USB_ATTACH_ERROR_RETURN;
}
printf("\n");
@@ -167,19 +175,34 @@ USB_ATTACH(usb)
ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
- err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
+#ifdef USB_USE_SOFTINTR
+#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
+ /* XXX we should have our own level */
+ sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
+ sc->sc_bus->methods->soft_intr, sc->sc_bus);
+ if (sc->sc_bus->soft == NULL) {
+ printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
+ sc->sc_dying = 1;
+ USB_ATTACH_ERROR_RETURN;
+ }
+#else
+ usb_callout_init(&sc->sc_bus->softi);
+#endif
+#endif
+
+ err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
&sc->sc_port);
if (!err) {
dev = sc->sc_port.device;
if (dev->hub == NULL) {
sc->sc_dying = 1;
- printf("%s: root device is not a hub\n",
+ printf("%s: root device is not a hub\n",
USBDEVNAME(sc->sc_dev));
USB_ATTACH_ERROR_RETURN;
}
sc->sc_bus->root_hub = dev;
#if 1
- /*
+ /*
* Turning this code off will delay attachment of USB devices
* until the USB event thread is running, which means that
* the keyboard will not work until after cold boot.
@@ -188,8 +211,8 @@ USB_ATTACH(usb)
dev->hub->explore(sc->sc_bus->root_hub);
#endif
} else {
- printf("%s: root hub problem, error=%d\n",
- USBDEVNAME(sc->sc_dev), err);
+ printf("%s: root hub problem, error=%d\n",
+ USBDEVNAME(sc->sc_dev), err);
sc->sc_dying = 1;
}
if (cold)
@@ -206,6 +229,7 @@ void
usb_create_event_thread(void *arg)
{
struct usb_softc *sc = arg;
+ static int created = 0;
if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
"%s", sc->sc_dev.dv_xname)) {
@@ -213,39 +237,47 @@ usb_create_event_thread(void *arg)
sc->sc_dev.dv_xname);
panic("usb_create_event_thread");
}
+ if (!created) {
+ created = 1;
+ TAILQ_INIT(&usb_all_tasks);
+ if (usb_kthread_create1(usb_task_thread, NULL,
+ &usb_task_thread_proc, "usbtask")) {
+ printf("unable to create task thread\n");
+ panic("usb_create_event_thread task");
+ }
+ }
}
/*
- * Add a task to be performed by the event thread. This function can be
+ * Add a task to be performed by the task thread. This function can be
* called from any context and the task will be executed in a process
* context ASAP.
*/
void
usb_add_task(usbd_device_handle dev, struct usb_task *task)
{
- struct usb_softc *sc = dev->bus->usbctl;
int s;
s = splusb();
if (!task->onqueue) {
- DPRINTFN(2,("usb_add_task: sc=%p task=%p\n", sc, task));
- TAILQ_INSERT_TAIL(&sc->sc_tasks, task, next);
+ DPRINTFN(2,("usb_add_task: task=%p\n", task));
+ TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
task->onqueue = 1;
- } else
- DPRINTFN(3,("usb_add_task: sc=%p task=%p on q\n", sc, task));
- wakeup(&sc->sc_tasks);
+ } else {
+ DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
+ }
+ wakeup(&usb_all_tasks);
splx(s);
}
void
usb_rem_task(usbd_device_handle dev, struct usb_task *task)
{
- struct usb_softc *sc = dev->bus->usbctl;
int s;
s = splusb();
if (task->onqueue) {
- TAILQ_REMOVE(&sc->sc_tasks, task, next);
+ TAILQ_REMOVE(&usb_all_tasks, task, next);
task->onqueue = 0;
}
splx(s);
@@ -255,31 +287,37 @@ void
usb_event_thread(void *arg)
{
struct usb_softc *sc = arg;
- struct usb_task *task;
- int s;
DPRINTF(("usb_event_thread: start\n"));
+ /*
+ * In case this controller is a companion controller to an
+ * EHCI controller we need to wait until the EHCI controller
+ * has grabbed the port.
+ * XXX It would be nicer to do this with a tsleep(), but I don't
+ * know how to synchronize the creation of the threads so it
+ * will work.
+ */
+ usb_delay_ms(sc->sc_bus, 500);
+
/* Make sure first discover does something. */
sc->sc_bus->needs_explore = 1;
usb_discover(sc);
config_pending_decr();
while (!sc->sc_dying) {
- s = splusb();
- task = TAILQ_FIRST(&sc->sc_tasks);
- if (task == NULL) {
- tsleep(&sc->sc_tasks, PWAIT, "usbevt", 0);
- task = TAILQ_FIRST(&sc->sc_tasks);
- }
- DPRINTFN(2,("usb_event_thread: woke up task=%p\n", task));
- if (task != NULL && !sc->sc_dying) {
- TAILQ_REMOVE(&sc->sc_tasks, task, next);
- task->onqueue = 0;
- splx(s);
- task->fun(task->arg);
- } else
- splx(s);
+#ifdef USB_DEBUG
+ if (usb_noexplore < 2)
+#endif
+ usb_discover(sc);
+#ifdef USB_DEBUG
+ (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
+ usb_noexplore ? 0 : hz * 60);
+#else
+ (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
+ hz * 60);
+#endif
+ DPRINTFN(2,("usb_event_thread: woke up\n"));
}
sc->sc_event_thread = NULL;
@@ -290,6 +328,32 @@ usb_event_thread(void *arg)
kthread_exit(0);
}
+void
+usb_task_thread(void *arg)
+{
+ struct usb_task *task;
+ int s;
+
+ DPRINTF(("usb_task_thread: start\n"));
+
+ s = splusb();
+ for (;;) {
+ task = TAILQ_FIRST(&usb_all_tasks);
+ if (task == NULL) {
+ tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
+ task = TAILQ_FIRST(&usb_all_tasks);
+ }
+ DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
+ if (task != NULL) {
+ TAILQ_REMOVE(&usb_all_tasks, task, next);
+ task->onqueue = 0;
+ splx(s);
+ task->fun(task->arg);
+ s = splusb();
+ }
+ }
+}
+
int
usbctlprint(void *aux, const char *pnp)
{
@@ -302,7 +366,7 @@ usbctlprint(void *aux, const char *pnp)
#endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
int
-usbopen(dev_t dev, int flag, int mode, struct proc *p)
+usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
{
int unit = minor(dev);
struct usb_softc *sc;
@@ -357,7 +421,7 @@ usbread(dev_t dev, struct uio *uio, int flag)
}
int
-usbclose(dev_t dev, int flag, int mode, struct proc *p)
+usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
{
int unit = minor(dev);
@@ -370,7 +434,7 @@ usbclose(dev_t dev, int flag, int mode, struct proc *p)
}
int
-usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
+usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
{
struct usb_softc *sc;
int unit = minor(devt);
@@ -380,7 +444,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
case FIONBIO:
/* All handled in the upper FS layer. */
return (0);
-
+
case FIOASYNC:
if (*(int *)data)
usb_async_proc = p;
@@ -401,6 +465,8 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
switch (cmd) {
#ifdef USB_DEBUG
case USB_SETDEBUG:
+ if (!(flag & FWRITE))
+ return (EBADF);
usbdebug = ((*(int *)data) & 0x000000ff);
#ifdef UHCI_DEBUG
uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
@@ -409,7 +475,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
#endif
break;
-#endif
+#endif /* USB_DEBUG */
case USB_REQUEST:
{
struct usb_ctl_request *ur = (void *)data;
@@ -421,10 +487,13 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
usbd_status err;
int error = 0;
+ if (!(flag & FWRITE))
+ return (EBADF);
+
DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
if (len < 0 || len > 32768)
return (EINVAL);
- if (addr < 0 || addr >= USB_MAX_DEVICES ||
+ if (addr < 0 || addr >= USB_MAX_DEVICES ||
sc->sc_bus->devices[addr] == 0)
return (EINVAL);
if (len != 0) {
@@ -436,7 +505,7 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
uio.uio_offset = 0;
uio.uio_segflg = UIO_USERSPACE;
uio.uio_rw =
- ur->ucr_request.bmRequestType & UT_READ ?
+ ur->ucr_request.bmRequestType & UT_READ ?
UIO_READ : UIO_WRITE;
uio.uio_procp = p;
ptr = malloc(len, M_TEMP, M_WAITOK);
@@ -447,8 +516,8 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
}
}
err = usbd_do_request_flags(sc->sc_bus->devices[addr],
- &ur->ucr_request, ptr, ur->ucr_flags,
- &ur->ucr_actlen);
+ &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
+ USBD_DEFAULT_TIMEOUT);
if (err) {
error = EIO;
goto ret;
@@ -492,21 +561,21 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
}
int
-usbpoll(dev_t dev, int events, struct proc *p)
+usbpoll(dev_t dev, int events, usb_proc_ptr p)
{
int revents, mask, s;
if (minor(dev) == USB_DEV_MINOR) {
revents = 0;
mask = POLLIN | POLLRDNORM;
-
+
s = splusb();
if (events & mask && usb_nevents > 0)
revents |= events & mask;
if (revents == 0 && events & mask)
selrecord(p, &usb_selevent);
splx(s);
-
+
return (revents);
} else {
return (ENXIO);
@@ -524,7 +593,7 @@ usb_discover(void *v)
if (usb_noexplore > 1)
return;
#endif
- /*
+ /*
* We need mutual exclusion while traversing the device tree,
* but this is guaranteed since this function is only called
* from the event thread for the controller.
@@ -540,7 +609,7 @@ usb_needs_explore(usbd_device_handle dev)
{
DPRINTFN(2,("usb_needs_explore\n"));
dev->bus->needs_explore = 1;
- usb_add_task(dev, &dev->bus->usbctl->sc_exp_task);
+ wakeup(&dev->bus->needs_explore);
}
/* Called at splusb() */
@@ -552,6 +621,13 @@ usb_get_next_event(struct usb_event *ue)
if (usb_nevents <= 0)
return (0);
ueq = SIMPLEQ_FIRST(&usb_events);
+#ifdef DIAGNOSTIC
+ if (ueq == NULL) {
+ printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
+ usb_nevents = 0;
+ return (0);
+ }
+#endif
*ue = ueq->ue;
SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
free(ueq, M_USBDEV);
@@ -574,7 +650,7 @@ usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
struct usb_event ue;
ue.u.ue_driver.ue_cookie = udev->cookie;
- strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
+ strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
sizeof ue.u.ue_driver.ue_devname);
usb_add_event(type, &ue);
}
@@ -607,10 +683,26 @@ usb_add_event(int type, struct usb_event *uep)
psignal(usb_async_proc, SIGIO);
splx(s);
}
+
void
-usb_schedsoftintr(struct usbd_bus *bus)
+usb_schedsoftintr(usbd_bus_handle bus)
{
+ DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
+#ifdef USB_USE_SOFTINTR
+ if (bus->use_polling) {
+ bus->methods->soft_intr(bus);
+ } else {
+#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
+ softintr_schedule(bus->soft);
+#else
+ if (!usb_callout_pending(bus->softi))
+ usb_callout(bus->softi, 0, bus->methods->soft_intr,
+ bus);
+#endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
+ }
+#else
bus->methods->soft_intr(bus);
+#endif /* USB_USE_SOFTINTR */
}
int
@@ -627,7 +719,8 @@ usb_activate(device_ptr_t self, enum devact act)
case DVACT_DEACTIVATE:
sc->sc_dying = 1;
- if (dev && dev->cdesc && dev->subdevs) {
+ if (dev != NULL && dev->cdesc != NULL &&
+ dev->subdevs != NULL) {
for (i = 0; dev->subdevs[i]; i++)
rv |= config_deactivate(dev->subdevs[i]);
}
@@ -647,12 +740,12 @@ usb_detach(device_ptr_t self, int flags)
sc->sc_dying = 1;
/* Make all devices disconnect. */
- if (sc->sc_port.device)
+ if (sc->sc_port.device != NULL)
usb_disconnect_port(&sc->sc_port, self);
/* Kill off event thread. */
- if (sc->sc_event_thread) {
- wakeup(&sc->sc_tasks);
+ if (sc->sc_event_thread != NULL) {
+ wakeup(&sc->sc_bus->needs_explore);
if (tsleep(sc, PWAIT, "usbdet", hz * 60))
printf("%s: event thread didn't die\n",
USBDEVNAME(sc->sc_dev));
@@ -661,6 +754,17 @@ usb_detach(device_ptr_t self, int flags)
usbd_finish();
+#ifdef USB_USE_SOFTINTR
+#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
+ if (sc->sc_bus->soft != NULL) {
+ softintr_disestablish(sc->sc_bus->soft);
+ sc->sc_bus->soft = NULL;
+ }
+#else
+ usb_uncallout(sc->sc_bus->softi, bus->methods->soft_intr, bus);
+#endif
+#endif
+
ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);