blob: 0ba2dfa86d07535a6159bc905c578a3d59dac0e0 [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>
Paul Gortmaker35a24632011-08-01 15:26:38 -040032#include <linux/export.h>
Sakari Ailusc3b5b022010-03-01 05:14:18 -030033
Hans Verkuilf1e393d2011-06-13 19:24:17 -030034static unsigned sev_pos(const struct v4l2_subscribed_event *sev, unsigned idx)
Sakari Ailusc3b5b022010-03-01 05:14:18 -030035{
Hans Verkuilf1e393d2011-06-13 19:24:17 -030036 idx += sev->first;
37 return idx >= sev->elems ? idx - sev->elems : idx;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030038}
Sakari Ailusc3b5b022010-03-01 05:14:18 -030039
40static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
41{
Sakari Ailusc3b5b022010-03-01 05:14:18 -030042 struct v4l2_kevent *kev;
43 unsigned long flags;
44
45 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
46
Hans Verkuil523f46d2011-06-13 17:44:42 -030047 if (list_empty(&fh->available)) {
Sakari Ailusc3b5b022010-03-01 05:14:18 -030048 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
49 return -ENOENT;
50 }
51
Hans Verkuil523f46d2011-06-13 17:44:42 -030052 WARN_ON(fh->navailable == 0);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030053
Hans Verkuil523f46d2011-06-13 17:44:42 -030054 kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
Hans Verkuilf1e393d2011-06-13 19:24:17 -030055 list_del(&kev->list);
Hans Verkuil523f46d2011-06-13 17:44:42 -030056 fh->navailable--;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030057
Hans Verkuil523f46d2011-06-13 17:44:42 -030058 kev->event.pending = fh->navailable;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030059 *event = kev->event;
Hans Verkuilf1e393d2011-06-13 19:24:17 -030060 kev->sev->first = sev_pos(kev->sev, 1);
61 kev->sev->in_use--;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030062
63 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
64
65 return 0;
66}
67
68int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
69 int nonblocking)
70{
Sakari Ailusc3b5b022010-03-01 05:14:18 -030071 int ret;
72
73 if (nonblocking)
74 return __v4l2_event_dequeue(fh, event);
75
Hans Verkuilee6869a2010-09-26 08:47:38 -030076 /* Release the vdev lock while waiting */
77 if (fh->vdev->lock)
78 mutex_unlock(fh->vdev->lock);
79
Sakari Ailusc3b5b022010-03-01 05:14:18 -030080 do {
Hans Verkuil523f46d2011-06-13 17:44:42 -030081 ret = wait_event_interruptible(fh->wait,
82 fh->navailable != 0);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030083 if (ret < 0)
Hans Verkuilee6869a2010-09-26 08:47:38 -030084 break;
Sakari Ailusc3b5b022010-03-01 05:14:18 -030085
86 ret = __v4l2_event_dequeue(fh, event);
87 } while (ret == -ENOENT);
88
Hans Verkuilee6869a2010-09-26 08:47:38 -030089 if (fh->vdev->lock)
90 mutex_lock(fh->vdev->lock);
91
Sakari Ailusc3b5b022010-03-01 05:14:18 -030092 return ret;
93}
Laurent Pinchart0a4f8d02010-05-02 14:32:43 -030094EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
Sakari Ailusc3b5b022010-03-01 05:14:18 -030095
Hans Verkuil6e239392011-06-07 11:13:44 -030096/* Caller must hold fh->vdev->fh_lock! */
Sakari Ailusc3b5b022010-03-01 05:14:18 -030097static struct v4l2_subscribed_event *v4l2_event_subscribed(
Hans Verkuil6e239392011-06-07 11:13:44 -030098 struct v4l2_fh *fh, u32 type, u32 id)
Sakari Ailusc3b5b022010-03-01 05:14:18 -030099{
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300100 struct v4l2_subscribed_event *sev;
101
Sakari Ailusf3cd3852010-05-03 12:42:46 -0300102 assert_spin_locked(&fh->vdev->fh_lock);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300103
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300104 list_for_each_entry(sev, &fh->subscribed, list)
Hans Verkuil6e239392011-06-07 11:13:44 -0300105 if (sev->type == type && sev->id == id)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300106 return sev;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300107
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;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300116 bool copy_payload = true;
Hans Verkuil6e239392011-06-07 11:13:44 -0300117
118 /* Are we subscribed? */
119 sev = v4l2_event_subscribed(fh, ev->type, ev->id);
120 if (sev == NULL)
121 return;
122
Hans de Goedec53c2542012-04-08 12:59:46 -0300123 /*
124 * If the event has been added to the fh->subscribed list, but its
125 * add op has not completed yet elems will be 0, treat this as
126 * not being subscribed.
127 */
128 if (!sev->elems)
129 return;
130
Hans Verkuil6e239392011-06-07 11:13:44 -0300131 /* Increase event sequence number on fh. */
Hans Verkuil523f46d2011-06-13 17:44:42 -0300132 fh->sequence++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300133
134 /* Do we have any free events? */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300135 if (sev->in_use == sev->elems) {
136 /* no, remove the oldest one */
137 kev = sev->events + sev_pos(sev, 0);
138 list_del(&kev->list);
139 sev->in_use--;
140 sev->first = sev_pos(sev, 1);
141 fh->navailable--;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300142 if (sev->elems == 1) {
Hans de Goedec53c2542012-04-08 12:59:46 -0300143 if (sev->ops && sev->ops->replace) {
144 sev->ops->replace(&kev->event, ev);
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300145 copy_payload = false;
146 }
Hans de Goedec53c2542012-04-08 12:59:46 -0300147 } else if (sev->ops && sev->ops->merge) {
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300148 struct v4l2_kevent *second_oldest =
149 sev->events + sev_pos(sev, 0);
Hans de Goedec53c2542012-04-08 12:59:46 -0300150 sev->ops->merge(&kev->event, &second_oldest->event);
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300151 }
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300152 }
Hans Verkuil6e239392011-06-07 11:13:44 -0300153
154 /* Take one and fill it. */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300155 kev = sev->events + sev_pos(sev, sev->in_use);
Hans Verkuil6e239392011-06-07 11:13:44 -0300156 kev->event.type = ev->type;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300157 if (copy_payload)
158 kev->event.u = ev->u;
Hans Verkuil6e239392011-06-07 11:13:44 -0300159 kev->event.id = ev->id;
160 kev->event.timestamp = *ts;
Hans Verkuil523f46d2011-06-13 17:44:42 -0300161 kev->event.sequence = fh->sequence;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300162 sev->in_use++;
163 list_add_tail(&kev->list, &fh->available);
Hans Verkuil6e239392011-06-07 11:13:44 -0300164
Hans Verkuil523f46d2011-06-13 17:44:42 -0300165 fh->navailable++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300166
Hans Verkuil523f46d2011-06-13 17:44:42 -0300167 wake_up_all(&fh->wait);
Hans Verkuil6e239392011-06-07 11:13:44 -0300168}
169
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300170void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
171{
172 struct v4l2_fh *fh;
173 unsigned long flags;
174 struct timespec timestamp;
175
176 ktime_get_ts(&timestamp);
177
178 spin_lock_irqsave(&vdev->fh_lock, flags);
179
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300180 list_for_each_entry(fh, &vdev->fh_list, list)
Hans Verkuil6e239392011-06-07 11:13:44 -0300181 __v4l2_event_queue_fh(fh, ev, &timestamp);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300182
183 spin_unlock_irqrestore(&vdev->fh_lock, flags);
184}
185EXPORT_SYMBOL_GPL(v4l2_event_queue);
186
Hans Verkuil6e239392011-06-07 11:13:44 -0300187void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
188{
189 unsigned long flags;
190 struct timespec timestamp;
191
192 ktime_get_ts(&timestamp);
193
194 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
195 __v4l2_event_queue_fh(fh, ev, &timestamp);
196 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
197}
198EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
199
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300200int v4l2_event_pending(struct v4l2_fh *fh)
201{
Hans Verkuil523f46d2011-06-13 17:44:42 -0300202 return fh->navailable;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300203}
204EXPORT_SYMBOL_GPL(v4l2_event_pending);
205
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300206static void ctrls_replace(struct v4l2_event *old, const struct v4l2_event *new)
207{
208 u32 old_changes = old->u.ctrl.changes;
209
210 old->u.ctrl = new->u.ctrl;
211 old->u.ctrl.changes |= old_changes;
212}
213
214static void ctrls_merge(const struct v4l2_event *old, struct v4l2_event *new)
215{
216 new->u.ctrl.changes |= old->u.ctrl.changes;
217}
218
Hans de Goedec53c2542012-04-08 12:59:46 -0300219static const struct v4l2_subscribed_event_ops ctrl_ops = {
220 .replace = ctrls_replace,
221 .merge = ctrls_merge,
222};
223
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300224int v4l2_event_subscribe(struct v4l2_fh *fh,
Hans de Goedec53c2542012-04-08 12:59:46 -0300225 struct v4l2_event_subscription *sub, unsigned elems,
226 const struct v4l2_subscribed_event_ops *ops)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300227{
Hans Verkuil6e239392011-06-07 11:13:44 -0300228 struct v4l2_subscribed_event *sev, *found_ev;
229 struct v4l2_ctrl *ctrl = NULL;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300230 unsigned long flags;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300231 unsigned i;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300232
Hans de Goedeb36b5052011-10-24 05:03:27 -0300233 if (sub->type == V4L2_EVENT_ALL)
234 return -EINVAL;
235
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300236 if (elems < 1)
237 elems = 1;
Hans Verkuil6e239392011-06-07 11:13:44 -0300238 if (sub->type == V4L2_EVENT_CTRL) {
239 ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
240 if (ctrl == NULL)
241 return -EINVAL;
242 }
243
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300244 sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300245 if (!sev)
246 return -ENOMEM;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300247 for (i = 0; i < elems; i++)
248 sev->events[i].sev = sev;
249 sev->type = sub->type;
250 sev->id = sub->id;
251 sev->flags = sub->flags;
252 sev->fh = fh;
Hans de Goedec53c2542012-04-08 12:59:46 -0300253 sev->ops = ops;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300254 if (ctrl) {
Hans de Goedec53c2542012-04-08 12:59:46 -0300255 sev->ops = &ctrl_ops;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300256 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300257
258 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300259 found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300260 if (!found_ev)
Hans Verkuil523f46d2011-06-13 17:44:42 -0300261 list_add(&sev->list, &fh->subscribed);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300262 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
263
Hans de Goedec53c2542012-04-08 12:59:46 -0300264 if (found_ev) {
Hans Verkuil77068d32011-06-13 18:55:58 -0300265 kfree(sev);
Hans de Goedec53c2542012-04-08 12:59:46 -0300266 return 0; /* Already listening */
267 }
268
269 if (sev->ops && sev->ops->add) {
270 int ret = sev->ops->add(sev);
271 if (ret) {
272 sev->ops = NULL;
273 v4l2_event_unsubscribe(fh, sub);
274 return ret;
275 }
276 }
277
278 /* v4l2_ctrl_add_event uses a mutex, so do this outside the spin lock */
279 if (ctrl)
Hans Verkuil77068d32011-06-13 18:55:58 -0300280 v4l2_ctrl_add_event(ctrl, sev);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300281
Hans de Goedec53c2542012-04-08 12:59:46 -0300282 /* Mark as ready for use */
283 sev->elems = elems;
284
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300285 return 0;
286}
287EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
288
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300289void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300290{
Hans Verkuil6e239392011-06-07 11:13:44 -0300291 struct v4l2_event_subscription sub;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300292 struct v4l2_subscribed_event *sev;
293 unsigned long flags;
294
295 do {
296 sev = NULL;
297
298 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil523f46d2011-06-13 17:44:42 -0300299 if (!list_empty(&fh->subscribed)) {
300 sev = list_first_entry(&fh->subscribed,
Hans Verkuil6e239392011-06-07 11:13:44 -0300301 struct v4l2_subscribed_event, list);
302 sub.type = sev->type;
303 sub.id = sev->id;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300304 }
305 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300306 if (sev)
307 v4l2_event_unsubscribe(fh, &sub);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300308 } while (sev);
309}
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300310EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300311
312int v4l2_event_unsubscribe(struct v4l2_fh *fh,
313 struct v4l2_event_subscription *sub)
314{
315 struct v4l2_subscribed_event *sev;
316 unsigned long flags;
Hans de Goede78c87e82011-10-26 05:40:27 -0300317 int i;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300318
319 if (sub->type == V4L2_EVENT_ALL) {
320 v4l2_event_unsubscribe_all(fh);
321 return 0;
322 }
323
324 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
325
Hans Verkuil6e239392011-06-07 11:13:44 -0300326 sev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuil77068d32011-06-13 18:55:58 -0300327 if (sev != NULL) {
Hans de Goede78c87e82011-10-26 05:40:27 -0300328 /* Remove any pending events for this subscription */
329 for (i = 0; i < sev->in_use; i++) {
330 list_del(&sev->events[sev_pos(sev, i)].list);
331 fh->navailable--;
332 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300333 list_del(&sev->list);
Hans Verkuil77068d32011-06-13 18:55:58 -0300334 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300335
336 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans de Goedec53c2542012-04-08 12:59:46 -0300337
338 if (sev && sev->ops && sev->ops->del)
339 sev->ops->del(sev);
340
Hans Verkuil77068d32011-06-13 18:55:58 -0300341 if (sev && sev->type == V4L2_EVENT_CTRL) {
Hans Verkuil6e239392011-06-07 11:13:44 -0300342 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
343
344 if (ctrl)
Hans Verkuil77068d32011-06-13 18:55:58 -0300345 v4l2_ctrl_del_event(ctrl, sev);
Hans Verkuil6e239392011-06-07 11:13:44 -0300346 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300347
348 kfree(sev);
349
350 return 0;
351}
352EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);