blob: 86dcb5483c4219da6d86d2c3423a8c857449256b [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 *
Sakari Ailus8c5dff92012-10-28 06:44:17 -03008 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
Sakari Ailusc3b5b022010-03-01 05:14:18 -03009 *
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>
28
29#include <linux/sched.h>
30#include <linux/slab.h>
Paul Gortmaker35a24632011-08-01 15:26:38 -040031#include <linux/export.h>
Sakari Ailusc3b5b022010-03-01 05:14:18 -030032
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 Verkuil3f66f0e2011-06-20 11:56:24 -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;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300106
107 return NULL;
108}
109
Hans Verkuil6e239392011-06-07 11:13:44 -0300110static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
111 const struct timespec *ts)
112{
Hans Verkuil6e239392011-06-07 11:13:44 -0300113 struct v4l2_subscribed_event *sev;
114 struct v4l2_kevent *kev;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300115 bool copy_payload = true;
Hans Verkuil6e239392011-06-07 11:13:44 -0300116
117 /* Are we subscribed? */
118 sev = v4l2_event_subscribed(fh, ev->type, ev->id);
119 if (sev == NULL)
120 return;
121
Hans de Goedec53c2542012-04-08 12:59:46 -0300122 /*
123 * If the event has been added to the fh->subscribed list, but its
124 * add op has not completed yet elems will be 0, treat this as
125 * not being subscribed.
126 */
127 if (!sev->elems)
128 return;
129
Hans Verkuil6e239392011-06-07 11:13:44 -0300130 /* Increase event sequence number on fh. */
Hans Verkuil523f46d2011-06-13 17:44:42 -0300131 fh->sequence++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300132
133 /* Do we have any free events? */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300134 if (sev->in_use == sev->elems) {
135 /* no, remove the oldest one */
136 kev = sev->events + sev_pos(sev, 0);
137 list_del(&kev->list);
138 sev->in_use--;
139 sev->first = sev_pos(sev, 1);
140 fh->navailable--;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300141 if (sev->elems == 1) {
Hans de Goedec53c2542012-04-08 12:59:46 -0300142 if (sev->ops && sev->ops->replace) {
143 sev->ops->replace(&kev->event, ev);
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300144 copy_payload = false;
145 }
Hans de Goedec53c2542012-04-08 12:59:46 -0300146 } else if (sev->ops && sev->ops->merge) {
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300147 struct v4l2_kevent *second_oldest =
148 sev->events + sev_pos(sev, 0);
Hans de Goedec53c2542012-04-08 12:59:46 -0300149 sev->ops->merge(&kev->event, &second_oldest->event);
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300150 }
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300151 }
Hans Verkuil6e239392011-06-07 11:13:44 -0300152
153 /* Take one and fill it. */
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300154 kev = sev->events + sev_pos(sev, sev->in_use);
Hans Verkuil6e239392011-06-07 11:13:44 -0300155 kev->event.type = ev->type;
Hans Verkuil2151bdc2011-06-18 07:02:20 -0300156 if (copy_payload)
157 kev->event.u = ev->u;
Hans Verkuil6e239392011-06-07 11:13:44 -0300158 kev->event.id = ev->id;
159 kev->event.timestamp = *ts;
Hans Verkuil523f46d2011-06-13 17:44:42 -0300160 kev->event.sequence = fh->sequence;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300161 sev->in_use++;
162 list_add_tail(&kev->list, &fh->available);
Hans Verkuil6e239392011-06-07 11:13:44 -0300163
Hans Verkuil523f46d2011-06-13 17:44:42 -0300164 fh->navailable++;
Hans Verkuil6e239392011-06-07 11:13:44 -0300165
Hans Verkuil523f46d2011-06-13 17:44:42 -0300166 wake_up_all(&fh->wait);
Hans Verkuil6e239392011-06-07 11:13:44 -0300167}
168
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300169void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
170{
171 struct v4l2_fh *fh;
172 unsigned long flags;
173 struct timespec timestamp;
174
175 ktime_get_ts(&timestamp);
176
177 spin_lock_irqsave(&vdev->fh_lock, flags);
178
Hans Verkuil3f66f0e2011-06-20 11:56:24 -0300179 list_for_each_entry(fh, &vdev->fh_list, list)
Hans Verkuil6e239392011-06-07 11:13:44 -0300180 __v4l2_event_queue_fh(fh, ev, &timestamp);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300181
182 spin_unlock_irqrestore(&vdev->fh_lock, flags);
183}
184EXPORT_SYMBOL_GPL(v4l2_event_queue);
185
Hans Verkuil6e239392011-06-07 11:13:44 -0300186void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
187{
188 unsigned long flags;
189 struct timespec timestamp;
190
191 ktime_get_ts(&timestamp);
192
193 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
194 __v4l2_event_queue_fh(fh, ev, &timestamp);
195 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
196}
197EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
198
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300199int v4l2_event_pending(struct v4l2_fh *fh)
200{
Hans Verkuil523f46d2011-06-13 17:44:42 -0300201 return fh->navailable;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300202}
203EXPORT_SYMBOL_GPL(v4l2_event_pending);
204
205int v4l2_event_subscribe(struct v4l2_fh *fh,
Hans Verkuil85f5fe32012-09-04 11:46:09 -0300206 const struct v4l2_event_subscription *sub, unsigned elems,
Hans de Goedec53c2542012-04-08 12:59:46 -0300207 const struct v4l2_subscribed_event_ops *ops)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300208{
Hans Verkuil6e239392011-06-07 11:13:44 -0300209 struct v4l2_subscribed_event *sev, *found_ev;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300210 unsigned long flags;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300211 unsigned i;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300212
Hans de Goedeb36b5052011-10-24 05:03:27 -0300213 if (sub->type == V4L2_EVENT_ALL)
214 return -EINVAL;
215
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300216 if (elems < 1)
217 elems = 1;
Hans Verkuil6e239392011-06-07 11:13:44 -0300218
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300219 sev = kzalloc(sizeof(*sev) + sizeof(struct v4l2_kevent) * elems, GFP_KERNEL);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300220 if (!sev)
221 return -ENOMEM;
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300222 for (i = 0; i < elems; i++)
223 sev->events[i].sev = sev;
224 sev->type = sub->type;
225 sev->id = sub->id;
226 sev->flags = sub->flags;
227 sev->fh = fh;
Hans de Goedec53c2542012-04-08 12:59:46 -0300228 sev->ops = ops;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300229
230 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300231 found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300232 if (!found_ev)
Hans Verkuil523f46d2011-06-13 17:44:42 -0300233 list_add(&sev->list, &fh->subscribed);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300234 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
235
Hans de Goedec53c2542012-04-08 12:59:46 -0300236 if (found_ev) {
Hans Verkuil77068d32011-06-13 18:55:58 -0300237 kfree(sev);
Hans de Goedec53c2542012-04-08 12:59:46 -0300238 return 0; /* Already listening */
239 }
240
241 if (sev->ops && sev->ops->add) {
Hans Verkuil6e6d76c2012-05-07 16:53:20 -0300242 int ret = sev->ops->add(sev, elems);
Hans de Goedec53c2542012-04-08 12:59:46 -0300243 if (ret) {
244 sev->ops = NULL;
245 v4l2_event_unsubscribe(fh, sub);
246 return ret;
247 }
248 }
249
Hans de Goedec53c2542012-04-08 12:59:46 -0300250 /* Mark as ready for use */
251 sev->elems = elems;
252
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300253 return 0;
254}
255EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
256
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300257void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300258{
Hans Verkuil6e239392011-06-07 11:13:44 -0300259 struct v4l2_event_subscription sub;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300260 struct v4l2_subscribed_event *sev;
261 unsigned long flags;
262
263 do {
264 sev = NULL;
265
266 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
Hans Verkuil523f46d2011-06-13 17:44:42 -0300267 if (!list_empty(&fh->subscribed)) {
268 sev = list_first_entry(&fh->subscribed,
Hans Verkuil6e239392011-06-07 11:13:44 -0300269 struct v4l2_subscribed_event, list);
270 sub.type = sev->type;
271 sub.id = sev->id;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300272 }
273 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans Verkuil6e239392011-06-07 11:13:44 -0300274 if (sev)
275 v4l2_event_unsubscribe(fh, &sub);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300276 } while (sev);
277}
Hans Verkuilf1e393d2011-06-13 19:24:17 -0300278EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe_all);
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300279
280int v4l2_event_unsubscribe(struct v4l2_fh *fh,
Hans Verkuil85f5fe32012-09-04 11:46:09 -0300281 const struct v4l2_event_subscription *sub)
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300282{
283 struct v4l2_subscribed_event *sev;
284 unsigned long flags;
Hans de Goede78c87e82011-10-26 05:40:27 -0300285 int i;
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300286
287 if (sub->type == V4L2_EVENT_ALL) {
288 v4l2_event_unsubscribe_all(fh);
289 return 0;
290 }
291
292 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
293
Hans Verkuil6e239392011-06-07 11:13:44 -0300294 sev = v4l2_event_subscribed(fh, sub->type, sub->id);
Hans Verkuil77068d32011-06-13 18:55:58 -0300295 if (sev != NULL) {
Hans de Goede78c87e82011-10-26 05:40:27 -0300296 /* Remove any pending events for this subscription */
297 for (i = 0; i < sev->in_use; i++) {
298 list_del(&sev->events[sev_pos(sev, i)].list);
299 fh->navailable--;
300 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300301 list_del(&sev->list);
Hans Verkuil77068d32011-06-13 18:55:58 -0300302 }
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300303
304 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
Hans de Goedec53c2542012-04-08 12:59:46 -0300305
306 if (sev && sev->ops && sev->ops->del)
307 sev->ops->del(sev);
308
Sakari Ailusc3b5b022010-03-01 05:14:18 -0300309 kfree(sev);
310
311 return 0;
312}
313EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);
Sylwester Nawrocki4f4d14b2013-01-22 18:58:57 -0300314
315int v4l2_event_subdev_unsubscribe(struct v4l2_subdev *sd, struct v4l2_fh *fh,
316 struct v4l2_event_subscription *sub)
317{
318 return v4l2_event_unsubscribe(fh, sub);
319}
320EXPORT_SYMBOL_GPL(v4l2_event_subdev_unsubscribe);