blob: 9e325dd3ce27e256d7158dea748ea615d795eb7d [file] [log] [blame]
Sakari Ailusc3b5b022010-03-01 05:14:18 -03001/*
2 * v4l2-event.c
3 *
4 * V4L2 events.
5 *
6 * Copyright (C) 2009--2010 Nokia Corporation.
7 *
8 * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25#include <media/v4l2-dev.h>
26#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
Hans Verkuil6e239392011-06-07 11:13:44 -030028#include <media/v4l2-ctrls.h>
Sakari Ailusc3b5b022010-03-01 05:14:18 -030029
30#include <linux/sched.h>
31#include <linux/slab.h>
32
Hans Verkuilf1e393d2011-06-13 19:24:17 -030033static unsigned sev_pos(const struct v4l2_subscribed_event *sev, unsigned idx)
Sakari Ailusc3b5b022010-03-01 05:14:18 -030034{
Hans Verkuilf1e393d2011-06-13 19:24:17 -030035 idx += sev->first;
36 return idx >= sev->elems ? idx - sev->elems : idx;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030037}
Sakari Ailusc3b5b022010-03-01 05:14:18 -030038
39static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
40{
Sakari Ailusc3b5b022010-03-01 05:14:18 -030041 struct v4l2_kevent *kev;
42 unsigned long flags;
43
44 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
45
Hans Verkuil523f46d2011-06-13 17:44:42 -030046 if (list_empty(&fh->available)) {
Sakari Ailusc3b5b022010-03-01 05:14:18 -030047 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
48 return -ENOENT;
49 }
50
Hans Verkuil523f46d2011-06-13 17:44:42 -030051 WARN_ON(fh->navailable == 0);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030052
Hans Verkuil523f46d2011-06-13 17:44:42 -030053 kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
Hans Verkuilf1e393d2011-06-13 19:24:17 -030054 list_del(&kev->list);
Hans Verkuil523f46d2011-06-13 17:44:42 -030055 fh->navailable--;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030056
Hans Verkuil523f46d2011-06-13 17:44:42 -030057 kev->event.pending = fh->navailable;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030058 *event = kev->event;
Hans Verkuilf1e393d2011-06-13 19:24:17 -030059 kev->sev->first = sev_pos(kev->sev, 1);
60 kev->sev->in_use--;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030061
62 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
63
64 return 0;
65}
66
67int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
68 int nonblocking)
69{
Sakari Ailusc3b5b022010-03-01 05:14:18 -030070 int ret;
71
72 if (nonblocking)
73 return __v4l2_event_dequeue(fh, event);
74
Hans Verkuilee6869a2010-09-26 08:47:38 -030075 /* Release the vdev lock while waiting */
76 if (fh->vdev->lock)
77 mutex_unlock(fh->vdev->lock);
78
Sakari Ailusc3b5b022010-03-01 05:14:18 -030079 do {
Hans Verkuil523f46d2011-06-13 17:44:42 -030080 ret = wait_event_interruptible(fh->wait,
81 fh->navailable != 0);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030082 if (ret < 0)
Hans Verkuilee6869a2010-09-26 08:47:38 -030083 break;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030084
85 ret = __v4l2_event_dequeue(fh, event);
86 } while (ret == -ENOENT);
87
Hans Verkuilee6869a2010-09-26 08:47:38 -030088 if (fh->vdev->lock)
89 mutex_lock(fh->vdev->lock);
90
Sakari Ailusc3b5b022010-03-01 05:14:18 -030091 return ret;
92}
Laurent Pinchart0a4f8d02010-05-02 14:32:43 -030093EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030094
Hans Verkuil6e239392011-06-07 11:13:44 -030095/* Caller must hold fh->vdev->fh_lock! */
Sakari Ailusc3b5b022010-03-01 05:14:18 -030096static struct v4l2_subscribed_event *v4l2_event_subscribed(
Hans Verkuil6e239392011-06-07 11:13:44 -030097 struct v4l2_fh *fh, u32 type, u32 id)
Sakari Ailusc3b5b022010-03-01 05:14:18 -030098{
Sakari Ailusc3b5b022010-03-01 05:14:18 -030099 struct v4l2_subscribed_event *sev;
100
Sakari Ailusf3cd3852010-05-03 12:42:46 -0300101 assert_spin_locked(&fh->vdev->fh_lock);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300102
Hans Verkuil523f46d2011-06-13 17:44:42 -0300103 list_for_each_entry(sev, &fh->subscribed, list) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300104 if (sev->type == type && sev->id == id)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300105 return sev;
106 }
107
108 return NULL;
109}
110
Hans Verkuil6e239392011-06-07 11:13:44 -0300111static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
112 const struct timespec *ts)
113{
Hans Verkuil6e239392011-06-07 11:13:44 -0300114 struct v4l2_subscribed_event *sev;
115 struct v4l2_kevent *kev;
116
117 /* Are we subscribed? */
118 sev = v4l2_event_subscribed(fh, ev->type, ev->id);
119 if (sev == NULL)
120 return;
121
122 /* Increase event sequence number on fh. */
Hans Verkuil523f46d2011-06-13 17:44:42 -0300123 fh->sequence++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300124
125 /* Do we have any free events? */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300126 if (sev->in_use == sev->elems) {
127 /* no, remove the oldest one */
128 kev = sev->events + sev_pos(sev, 0);
129 list_del(&kev->list);
130 sev->in_use--;
131 sev->first = sev_pos(sev, 1);
132 fh->navailable--;
133 }
Hans Verkuil6e239392011-06-07 11:13:44 -0300134
135 /* Take one and fill it. */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300136 kev = sev->events + sev_pos(sev, sev->in_use);
Hans Verkuil6e239392011-06-07 11:13:44 -0300137 kev->event.type = ev->type;
138 kev->event.u = ev->u;
139 kev->event.id = ev->id;
140 kev->event.timestamp = *ts;
Hans Verkuil523f46d2011-06-13 17:44:42 -0300141 kev->event.sequence = fh->sequence;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300142 sev->in_use++;
143 list_add_tail(&kev->list, &fh->available);
Hans Verkuil6e239392011-06-07 11:13:44 -0300144
Hans Verkuil523f46d2011-06-13 17:44:42 -0300145 fh->navailable++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300146
Hans Verkuil523f46d2011-06-13 17:44:42 -0300147 wake_up_all(&fh->wait);
Hans Verkuil6e239392011-06-07 11:13:44 -0300148}
149
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300150void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
151{
152 struct v4l2_fh *fh;
153 unsigned long flags;
154 struct timespec timestamp;
155
156 ktime_get_ts(&timestamp);
157
158 spin_lock_irqsave(&vdev->fh_lock, flags);
159
160 list_for_each_entry(fh, &vdev->fh_list, list) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300161 __v4l2_event_queue_fh(fh, ev, &timestamp);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300162 }
163
164 spin_unlock_irqrestore(&vdev->fh_lock, flags);
165}
166EXPORT_SYMBOL_GPL(v4l2_event_queue);
167
Hans Verkuil6e239392011-06-07 11:13:44 -0300168void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
169{
170 unsigned long flags;
171 struct timespec timestamp;
172
173 ktime_get_ts(&timestamp);
174
175 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
176 __v4l2_event_queue_fh(fh, ev, &timestamp);
177 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
178}
179EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
180
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300181int v4l2_event_pending(struct v4l2_fh *fh)
182{
Hans Verkuil523f46d2011-06-13 17:44:42 -0300183 return fh->navailable;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300184}
185EXPORT_SYMBOL_GPL(v4l2_event_pending);
186
187int v4l2_event_subscribe(struct v4l2_fh *fh,
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300188 struct v4l2_event_subscription *sub, unsigned elems)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300189{
Hans Verkuil6e239392011-06-07 11:13:44 -0300190 struct v4l2_subscribed_event *sev, *found_ev;
191 struct v4l2_ctrl *ctrl = NULL;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300192 unsigned long flags;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300193 unsigned i;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300194
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300195 if (elems < 1)
196 elems = 1;
Hans Verkuil6e239392011-06-07 11:13:44 -0300197 if (sub->type == V4L2_EVENT_CTRL) {
198 ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
199 if (ctrl == NULL)
200 return -EINVAL;
201 }
202
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300203 sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300204 if (!sev)
205 return -ENOMEM;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300206 for (i = 0; i < elems; i++)
207 sev->events[i].sev = sev;
208 sev->type = sub->type;
209 sev->id = sub->id;
210 sev->flags = sub->flags;
211 sev->fh = fh;
212 sev->elems = elems;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300213
214 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300215 found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300216 if (!found_ev)
Hans Verkuil523f46d2011-06-13 17:44:42 -0300217 list_add(&sev->list, &fh->subscribed);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300218 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
219
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300220 /* v4l2_ctrl_add_event uses a mutex, so do this outside the spin lock */
Hans Verkuil77068d32011-06-13 18:55:58 -0300221 if (found_ev)
222 kfree(sev);
223 else if (ctrl)
224 v4l2_ctrl_add_event(ctrl, sev);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300225
226 return 0;
227}
228EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
229
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300230void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300231{
Hans Verkuil6e239392011-06-07 11:13:44 -0300232 struct v4l2_event_subscription sub;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300233 struct v4l2_subscribed_event *sev;
234 unsigned long flags;
235
236 do {
237 sev = NULL;
238
239 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil523f46d2011-06-13 17:44:42 -0300240 if (!list_empty(&fh->subscribed)) {
241 sev = list_first_entry(&fh->subscribed,
Hans Verkuil6e239392011-06-07 11:13:44 -0300242 struct v4l2_subscribed_event, list);
243 sub.type = sev->type;
244 sub.id = sev->id;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300245 }
246 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300247 if (sev)
248 v4l2_event_unsubscribe(fh, &sub);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300249 } while (sev);
250}
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300251EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300252
253int v4l2_event_unsubscribe(struct v4l2_fh *fh,
254 struct v4l2_event_subscription *sub)
255{
256 struct v4l2_subscribed_event *sev;
257 unsigned long flags;
258
259 if (sub->type == V4L2_EVENT_ALL) {
260 v4l2_event_unsubscribe_all(fh);
261 return 0;
262 }
263
264 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
265
Hans Verkuil6e239392011-06-07 11:13:44 -0300266 sev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuil77068d32011-06-13 18:55:58 -0300267 if (sev != NULL) {
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300268 list_del(&sev->list);
Hans Verkuil77068d32011-06-13 18:55:58 -0300269 sev->fh = NULL;
270 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300271
272 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans Verkuil77068d32011-06-13 18:55:58 -0300273 if (sev && sev->type == V4L2_EVENT_CTRL) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300274 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
275
276 if (ctrl)
Hans Verkuil77068d32011-06-13 18:55:58 -0300277 v4l2_ctrl_del_event(ctrl, sev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300278 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300279
280 kfree(sev);
281
282 return 0;
283}
284EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);