2010-03-01 15:14:18 +07:00
|
|
|
/*
|
|
|
|
* v4l2-event.c
|
|
|
|
*
|
|
|
|
* V4L2 events.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009--2010 Nokia Corporation.
|
|
|
|
*
|
|
|
|
* Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <media/v4l2-dev.h>
|
|
|
|
#include <media/v4l2-fh.h>
|
|
|
|
#include <media/v4l2-event.h>
|
2011-06-07 21:13:44 +07:00
|
|
|
#include <media/v4l2-ctrls.h>
|
2010-03-01 15:14:18 +07:00
|
|
|
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh);
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
int v4l2_event_alloc(struct v4l2_fh *fh, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
while (fh->nallocated < n) {
|
2010-03-01 15:14:18 +07:00
|
|
|
struct v4l2_kevent *kev;
|
|
|
|
|
|
|
|
kev = kzalloc(sizeof(*kev), GFP_KERNEL);
|
|
|
|
if (kev == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
2011-06-14 03:44:42 +07:00
|
|
|
list_add_tail(&kev->list, &fh->free);
|
|
|
|
fh->nallocated++;
|
2010-03-01 15:14:18 +07:00
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_alloc);
|
|
|
|
|
|
|
|
#define list_kfree(list, type, member) \
|
|
|
|
while (!list_empty(list)) { \
|
|
|
|
type *hi; \
|
|
|
|
hi = list_first_entry(list, type, member); \
|
|
|
|
list_del(&hi->member); \
|
|
|
|
kfree(hi); \
|
|
|
|
}
|
|
|
|
|
|
|
|
void v4l2_event_free(struct v4l2_fh *fh)
|
|
|
|
{
|
2011-06-14 03:44:42 +07:00
|
|
|
list_kfree(&fh->free, struct v4l2_kevent, list);
|
|
|
|
list_kfree(&fh->available, struct v4l2_kevent, list);
|
2011-06-07 21:13:44 +07:00
|
|
|
v4l2_event_unsubscribe_all(fh);
|
2010-03-01 15:14:18 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_free);
|
|
|
|
|
|
|
|
static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
|
|
|
|
{
|
|
|
|
struct v4l2_kevent *kev;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
if (list_empty(&fh->available)) {
|
2010-03-01 15:14:18 +07:00
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
WARN_ON(fh->navailable == 0);
|
2010-03-01 15:14:18 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
|
|
|
|
list_move(&kev->list, &fh->free);
|
|
|
|
fh->navailable--;
|
2010-03-01 15:14:18 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
kev->event.pending = fh->navailable;
|
2010-03-01 15:14:18 +07:00
|
|
|
*event = kev->event;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
|
|
|
|
int nonblocking)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (nonblocking)
|
|
|
|
return __v4l2_event_dequeue(fh, event);
|
|
|
|
|
2010-09-26 18:47:38 +07:00
|
|
|
/* Release the vdev lock while waiting */
|
|
|
|
if (fh->vdev->lock)
|
|
|
|
mutex_unlock(fh->vdev->lock);
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
do {
|
2011-06-14 03:44:42 +07:00
|
|
|
ret = wait_event_interruptible(fh->wait,
|
|
|
|
fh->navailable != 0);
|
2010-03-01 15:14:18 +07:00
|
|
|
if (ret < 0)
|
2010-09-26 18:47:38 +07:00
|
|
|
break;
|
2010-03-01 15:14:18 +07:00
|
|
|
|
|
|
|
ret = __v4l2_event_dequeue(fh, event);
|
|
|
|
} while (ret == -ENOENT);
|
|
|
|
|
2010-09-26 18:47:38 +07:00
|
|
|
if (fh->vdev->lock)
|
|
|
|
mutex_lock(fh->vdev->lock);
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
return ret;
|
|
|
|
}
|
2010-05-03 00:32:43 +07:00
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
|
2010-03-01 15:14:18 +07:00
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
/* Caller must hold fh->vdev->fh_lock! */
|
2010-03-01 15:14:18 +07:00
|
|
|
static struct v4l2_subscribed_event *v4l2_event_subscribed(
|
2011-06-07 21:13:44 +07:00
|
|
|
struct v4l2_fh *fh, u32 type, u32 id)
|
2010-03-01 15:14:18 +07:00
|
|
|
{
|
|
|
|
struct v4l2_subscribed_event *sev;
|
|
|
|
|
2010-05-03 22:42:46 +07:00
|
|
|
assert_spin_locked(&fh->vdev->fh_lock);
|
2010-03-01 15:14:18 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
list_for_each_entry(sev, &fh->subscribed, list) {
|
2011-06-07 21:13:44 +07:00
|
|
|
if (sev->type == type && sev->id == id)
|
2010-03-01 15:14:18 +07:00
|
|
|
return sev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
|
|
|
|
const struct timespec *ts)
|
|
|
|
{
|
|
|
|
struct v4l2_subscribed_event *sev;
|
|
|
|
struct v4l2_kevent *kev;
|
|
|
|
|
|
|
|
/* Are we subscribed? */
|
|
|
|
sev = v4l2_event_subscribed(fh, ev->type, ev->id);
|
|
|
|
if (sev == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Increase event sequence number on fh. */
|
2011-06-14 03:44:42 +07:00
|
|
|
fh->sequence++;
|
2011-06-07 21:13:44 +07:00
|
|
|
|
|
|
|
/* Do we have any free events? */
|
2011-06-14 03:44:42 +07:00
|
|
|
if (list_empty(&fh->free))
|
2011-06-07 21:13:44 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Take one and fill it. */
|
2011-06-14 03:44:42 +07:00
|
|
|
kev = list_first_entry(&fh->free, struct v4l2_kevent, list);
|
2011-06-07 21:13:44 +07:00
|
|
|
kev->event.type = ev->type;
|
|
|
|
kev->event.u = ev->u;
|
|
|
|
kev->event.id = ev->id;
|
|
|
|
kev->event.timestamp = *ts;
|
2011-06-14 03:44:42 +07:00
|
|
|
kev->event.sequence = fh->sequence;
|
|
|
|
list_move_tail(&kev->list, &fh->available);
|
2011-06-07 21:13:44 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
fh->navailable++;
|
2011-06-07 21:13:44 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
wake_up_all(&fh->wait);
|
2011-06-07 21:13:44 +07:00
|
|
|
}
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
|
|
|
|
{
|
|
|
|
struct v4l2_fh *fh;
|
|
|
|
unsigned long flags;
|
|
|
|
struct timespec timestamp;
|
|
|
|
|
|
|
|
ktime_get_ts(×tamp);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vdev->fh_lock, flags);
|
|
|
|
|
|
|
|
list_for_each_entry(fh, &vdev->fh_list, list) {
|
2011-06-07 21:13:44 +07:00
|
|
|
__v4l2_event_queue_fh(fh, ev, ×tamp);
|
2010-03-01 15:14:18 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&vdev->fh_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_queue);
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct timespec timestamp;
|
|
|
|
|
|
|
|
ktime_get_ts(×tamp);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
|
|
|
__v4l2_event_queue_fh(fh, ev, ×tamp);
|
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
int v4l2_event_pending(struct v4l2_fh *fh)
|
|
|
|
{
|
2011-06-14 03:44:42 +07:00
|
|
|
return fh->navailable;
|
2010-03-01 15:14:18 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_pending);
|
|
|
|
|
|
|
|
int v4l2_event_subscribe(struct v4l2_fh *fh,
|
|
|
|
struct v4l2_event_subscription *sub)
|
|
|
|
{
|
2011-06-07 21:13:44 +07:00
|
|
|
struct v4l2_subscribed_event *sev, *found_ev;
|
|
|
|
struct v4l2_ctrl *ctrl = NULL;
|
|
|
|
struct v4l2_ctrl_fh *ctrl_fh = NULL;
|
2010-03-01 15:14:18 +07:00
|
|
|
unsigned long flags;
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
if (sub->type == V4L2_EVENT_CTRL) {
|
|
|
|
ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
|
|
|
|
if (ctrl == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
sev = kmalloc(sizeof(*sev), GFP_KERNEL);
|
|
|
|
if (!sev)
|
|
|
|
return -ENOMEM;
|
2011-06-07 21:13:44 +07:00
|
|
|
if (ctrl) {
|
|
|
|
ctrl_fh = kzalloc(sizeof(*ctrl_fh), GFP_KERNEL);
|
|
|
|
if (!ctrl_fh) {
|
|
|
|
kfree(sev);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
ctrl_fh->fh = fh;
|
|
|
|
}
|
2010-03-01 15:14:18 +07:00
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
|
|
|
|
if (!found_ev) {
|
2010-03-01 15:14:18 +07:00
|
|
|
INIT_LIST_HEAD(&sev->list);
|
|
|
|
sev->type = sub->type;
|
2011-06-07 21:13:44 +07:00
|
|
|
sev->id = sub->id;
|
2010-03-01 15:14:18 +07:00
|
|
|
|
2011-06-14 03:44:42 +07:00
|
|
|
list_add(&sev->list, &fh->subscribed);
|
2010-03-01 15:14:18 +07:00
|
|
|
sev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
/* v4l2_ctrl_add_fh uses a mutex, so do this outside the spin lock */
|
|
|
|
if (ctrl) {
|
|
|
|
if (found_ev)
|
|
|
|
kfree(ctrl_fh);
|
|
|
|
else
|
|
|
|
v4l2_ctrl_add_fh(fh->ctrl_handler, ctrl_fh, sub);
|
|
|
|
}
|
|
|
|
|
2010-03-01 15:14:18 +07:00
|
|
|
kfree(sev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
|
|
|
|
|
|
|
|
static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
|
|
|
|
{
|
2011-06-07 21:13:44 +07:00
|
|
|
struct v4l2_event_subscription sub;
|
2010-03-01 15:14:18 +07:00
|
|
|
struct v4l2_subscribed_event *sev;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
do {
|
|
|
|
sev = NULL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
2011-06-14 03:44:42 +07:00
|
|
|
if (!list_empty(&fh->subscribed)) {
|
|
|
|
sev = list_first_entry(&fh->subscribed,
|
2011-06-07 21:13:44 +07:00
|
|
|
struct v4l2_subscribed_event, list);
|
|
|
|
sub.type = sev->type;
|
|
|
|
sub.id = sev->id;
|
2010-03-01 15:14:18 +07:00
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
2011-06-07 21:13:44 +07:00
|
|
|
if (sev)
|
|
|
|
v4l2_event_unsubscribe(fh, &sub);
|
2010-03-01 15:14:18 +07:00
|
|
|
} while (sev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int v4l2_event_unsubscribe(struct v4l2_fh *fh,
|
|
|
|
struct v4l2_event_subscription *sub)
|
|
|
|
{
|
|
|
|
struct v4l2_subscribed_event *sev;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (sub->type == V4L2_EVENT_ALL) {
|
|
|
|
v4l2_event_unsubscribe_all(fh);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&fh->vdev->fh_lock, flags);
|
|
|
|
|
2011-06-07 21:13:44 +07:00
|
|
|
sev = v4l2_event_subscribed(fh, sub->type, sub->id);
|
2010-03-01 15:14:18 +07:00
|
|
|
if (sev != NULL)
|
|
|
|
list_del(&sev->list);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
|
2011-06-07 21:13:44 +07:00
|
|
|
if (sev->type == V4L2_EVENT_CTRL) {
|
|
|
|
struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
|
|
|
|
|
|
|
|
if (ctrl)
|
|
|
|
v4l2_ctrl_del_fh(ctrl, fh);
|
|
|
|
}
|
2010-03-01 15:14:18 +07:00
|
|
|
|
|
|
|
kfree(sev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
|