summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorJacob Meuser <jakemsr@cvs.openbsd.org>2011-02-04 08:21:40 +0000
committerJacob Meuser <jakemsr@cvs.openbsd.org>2011-02-04 08:21:40 +0000
commit4515d91db5080342bab395c673d6d8aabc0bb2ef (patch)
tree2cc47e8ff8d6da42f9f828b07e00fe3bf2cbd13a /sys
parentf0a0961e47c4711ff31e11aa2dceabd58ece1b9e (diff)
* merge 'onqueue' and 'running' members of struct usb_task into
a new member, 'state'. * add new function 'usb_wait_task()', which waits for queued or running usb_tasks to complete. * in the USB_DEVICEINFO ioctl, fill struct usb_device_info in a usb_task, thereby avoiding races against driver attach/detach. ok miod@
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/usb/usb.c119
-rw-r--r--sys/dev/usb/usbdi.h18
2 files changed, 97 insertions, 40 deletions
diff --git a/sys/dev/usb/usb.c b/sys/dev/usb/usb.c
index e0698f6d89c..1f3788739dc 100644
--- a/sys/dev/usb/usb.c
+++ b/sys/dev/usb/usb.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: usb.c,v 1.72 2011/01/25 20:03:36 jakemsr Exp $ */
+/* $OpenBSD: usb.c,v 1.73 2011/02/04 08:21:39 jakemsr Exp $ */
/* $NetBSD: usb.c,v 1.77 2003/01/01 00:10:26 thorpej Exp $ */
/*
@@ -275,15 +275,15 @@ usb_add_task(usbd_device_handle dev, struct usb_task *task)
{
int s;
- DPRINTFN(2,("%s: task=%p onqueue=%d type=%d\n", __func__, task,
- task->onqueue, task->type));
+ DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
+ task->state, task->type));
/* Don't add task if the device's root hub is dying. */
if (usbd_is_dying(dev))
return;
s = splusb();
- if (!task->onqueue) {
+ if (task->state != USB_TASK_STATE_ONQ) {
switch (task->type) {
case USB_TASK_TYPE_ABORT:
TAILQ_INSERT_TAIL(&usb_abort_tasks, task, next);
@@ -295,7 +295,7 @@ usb_add_task(usbd_device_handle dev, struct usb_task *task)
TAILQ_INSERT_TAIL(&usb_generic_tasks, task, next);
break;
}
- task->onqueue = 1;
+ task->state = USB_TASK_STATE_ONQ;
task->dev = dev;
}
if (task->type == USB_TASK_TYPE_ABORT)
@@ -310,38 +310,43 @@ usb_rem_task(usbd_device_handle dev, struct usb_task *task)
{
int s;
- DPRINTFN(2,("%s: task=%p onqueue=%d type=%d\n", __func__, task,
- task->onqueue, task->type));
+ DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
+ task->state, task->type));
+
+ if (task->state != USB_TASK_STATE_ONQ)
+ return;
s = splusb();
- if (task->onqueue) {
- switch (task->type) {
- case USB_TASK_TYPE_ABORT:
- TAILQ_REMOVE(&usb_abort_tasks, task, next);
- break;
- case USB_TASK_TYPE_EXPLORE:
- TAILQ_REMOVE(&usb_explore_tasks, task, next);
- break;
- case USB_TASK_TYPE_GENERIC:
- TAILQ_REMOVE(&usb_generic_tasks, task, next);
- break;
- }
- task->onqueue = 0;
+
+ switch (task->type) {
+ case USB_TASK_TYPE_ABORT:
+ TAILQ_REMOVE(&usb_abort_tasks, task, next);
+ break;
+ case USB_TASK_TYPE_EXPLORE:
+ TAILQ_REMOVE(&usb_explore_tasks, task, next);
+ break;
+ case USB_TASK_TYPE_GENERIC:
+ TAILQ_REMOVE(&usb_generic_tasks, task, next);
+ break;
}
+ task->state = USB_TASK_STATE_NONE;
+
splx(s);
}
void
-usb_rem_wait_task(usbd_device_handle dev, struct usb_task *task)
+usb_wait_task(usbd_device_handle dev, struct usb_task *task)
{
int s;
- DPRINTFN(2,("%s: task=%p onqueue=%d type=%d\n", __func__, task,
- task->onqueue, task->type));
+ DPRINTFN(2,("%s: task=%p state=%d type=%d\n", __func__, task,
+ task->state, task->type));
+
+ if (task->state == USB_TASK_STATE_NONE)
+ return;
s = splusb();
- usb_rem_task(dev, task);
- while (task->running) {
+ while (task->state != USB_TASK_STATE_NONE) {
DPRINTF(("%s: waiting for task to complete\n", __func__));
tsleep(task, PWAIT, "endtask", 0);
}
@@ -349,6 +354,13 @@ usb_rem_wait_task(usbd_device_handle dev, struct usb_task *task)
}
void
+usb_rem_wait_task(usbd_device_handle dev, struct usb_task *task)
+{
+ usb_rem_task(dev, task);
+ usb_wait_task(dev, task);
+}
+
+void
usb_first_explore(void *arg)
{
struct usb_softc *sc = arg;
@@ -406,15 +418,16 @@ usb_task_thread(void *arg)
tsleep(&usb_run_tasks, PWAIT, "usbtsk", 0);
continue;
}
- task->onqueue = 0;
/* Don't execute the task if the root hub is gone. */
- if (usbd_is_dying(task->dev))
+ if (usbd_is_dying(task->dev)) {
+ task->state = USB_TASK_STATE_NONE;
continue;
- task->running = 1;
+ }
+ task->state = USB_TASK_STATE_RUN;
splx(s);
task->fun(task->arg);
s = splusb();
- task->running = 0;
+ task->state = USB_TASK_STATE_NONE;
wakeup(task);
}
splx(s);
@@ -443,15 +456,16 @@ usb_abort_task_thread(void *arg)
tsleep(&usb_run_abort_tasks, PWAIT, "usbatsk", 0);
continue;
}
- task->onqueue = 0;
/* Don't execute the task if the root hub is gone. */
- if (usbd_is_dying(task->dev))
+ if (usbd_is_dying(task->dev)) {
+ task->state = USB_TASK_STATE_NONE;
continue;
- task->running = 1;
+ }
+ task->state = USB_TASK_STATE_RUN;
splx(s);
task->fun(task->arg);
s = splusb();
- task->running = 0;
+ task->state = USB_TASK_STATE_NONE;
wakeup(task);
}
splx(s);
@@ -493,6 +507,26 @@ usbclose(dev_t dev, int flag, int mode, struct proc *p)
return (0);
}
+void
+usbd_fill_di_task(void *arg)
+{
+ struct usb_device_info *di = (struct usb_device_info *)arg;
+ struct usb_softc *sc;
+ usbd_device_handle dev;
+
+ /* check that the bus and device are still present */
+ if (di->udi_bus >= usb_cd.cd_ndevs)
+ return;
+ sc = usb_cd.cd_devs[di->udi_bus];
+ if (sc == NULL)
+ return;
+ dev = sc->sc_bus->devices[di->udi_addr];
+ if (dev == NULL)
+ return;
+
+ usbd_fill_deviceinfo(dev, di, 1);
+}
+
int
usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
{
@@ -589,14 +623,31 @@ usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct proc *p)
{
struct usb_device_info *di = (void *)data;
int addr = di->udi_addr;
+ struct usb_task di_task;
usbd_device_handle dev;
if (addr < 1 || addr >= USB_MAX_DEVICES)
return (EINVAL);
+
dev = sc->sc_bus->devices[addr];
if (dev == NULL)
return (ENXIO);
- usbd_fill_deviceinfo(dev, di, 1);
+
+ di->udi_bus = unit;
+
+ /* All devices get a driver, thanks to ugen(4). If the
+ * task ends without adding a driver name, there was an error.
+ */
+ di->udi_devnames[0][0] = '\0';
+
+ usb_init_task(&di_task, usbd_fill_di_task, di,
+ USB_TASK_TYPE_GENERIC);
+ usb_add_task(sc->sc_bus->root_hub, &di_task);
+ usb_wait_task(sc->sc_bus->root_hub, &di_task);
+
+ if (di->udi_devnames[0][0] == '\0')
+ return (ENXIO);
+
break;
}
diff --git a/sys/dev/usb/usbdi.h b/sys/dev/usb/usbdi.h
index 5a990d38fa3..a38a953031d 100644
--- a/sys/dev/usb/usbdi.h
+++ b/sys/dev/usb/usbdi.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: usbdi.h,v 1.39 2011/01/25 20:03:36 jakemsr Exp $ */
+/* $OpenBSD: usbdi.h,v 1.40 2011/02/04 08:21:39 jakemsr Exp $ */
/* $NetBSD: usbdi.h,v 1.62 2002/07/11 21:14:35 augustss Exp $ */
/* $FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $ */
@@ -139,6 +139,7 @@ usbd_status usbd_set_interface(usbd_interface_handle, int);
int usbd_get_no_alts(usb_config_descriptor_t *, int);
usbd_status usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface);
void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
+void usbd_fill_di_task(void *);
int usbd_get_interface_altindex(usbd_interface_handle iface);
usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
@@ -187,28 +188,33 @@ const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
* has been detected. But it may also be used by drivers that need to
* perform (short) tasks that must have a process context.
*/
+enum usb_task_state {
+ USB_TASK_STATE_NONE,
+ USB_TASK_STATE_ONQ,
+ USB_TASK_STATE_RUN
+};
+
struct usb_task {
TAILQ_ENTRY(usb_task) next;
usbd_device_handle dev;
void (*fun)(void *);
void *arg;
char type;
-#define USB_TASK_TYPE_GENERIC 0
+#define USB_TASK_TYPE_GENERIC 0
#define USB_TASK_TYPE_EXPLORE 1
#define USB_TASK_TYPE_ABORT 2
- char onqueue;
- char running;
+ enum usb_task_state state;
};
void usb_add_task(usbd_device_handle, struct usb_task *);
void usb_rem_task(usbd_device_handle, struct usb_task *);
+void usb_wait_task(usbd_device_handle, struct usb_task *);
void usb_rem_wait_task(usbd_device_handle, struct usb_task *);
#define usb_init_task(t, f, a, y) \
((t)->fun = (f), \
(t)->arg = (a), \
(t)->type = (y), \
- (t)->onqueue = 0, \
- (t)->running = 0)
+ (t)->state = USB_TASK_STATE_NONE)
struct usb_devno {
u_int16_t ud_vendor;