6 * Copyright (C) 2009--2010 Nokia Corporation.
8 * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
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.
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.
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
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-ctrls.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
33 static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh);
35 int v4l2_event_init(struct v4l2_fh *fh)
37 fh->events = kzalloc(sizeof(*fh->events), GFP_KERNEL);
38 if (fh->events == NULL)
41 init_waitqueue_head(&fh->events->wait);
43 INIT_LIST_HEAD(&fh->events->free);
44 INIT_LIST_HEAD(&fh->events->available);
45 INIT_LIST_HEAD(&fh->events->subscribed);
47 fh->events->sequence = -1;
51 EXPORT_SYMBOL_GPL(v4l2_event_init);
53 int v4l2_event_alloc(struct v4l2_fh *fh, unsigned int n)
55 struct v4l2_events *events = fh->events;
63 while (events->nallocated < n) {
64 struct v4l2_kevent *kev;
66 kev = kzalloc(sizeof(*kev), GFP_KERNEL);
70 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
71 list_add_tail(&kev->list, &events->free);
73 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
78 EXPORT_SYMBOL_GPL(v4l2_event_alloc);
80 #define list_kfree(list, type, member) \
81 while (!list_empty(list)) { \
83 hi = list_first_entry(list, type, member); \
84 list_del(&hi->member); \
88 void v4l2_event_free(struct v4l2_fh *fh)
90 struct v4l2_events *events = fh->events;
95 list_kfree(&events->free, struct v4l2_kevent, list);
96 list_kfree(&events->available, struct v4l2_kevent, list);
97 v4l2_event_unsubscribe_all(fh);
102 EXPORT_SYMBOL_GPL(v4l2_event_free);
104 static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
106 struct v4l2_events *events = fh->events;
107 struct v4l2_kevent *kev;
110 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
112 if (list_empty(&events->available)) {
113 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
117 WARN_ON(events->navailable == 0);
119 kev = list_first_entry(&events->available, struct v4l2_kevent, list);
120 list_move(&kev->list, &events->free);
121 events->navailable--;
123 kev->event.pending = events->navailable;
126 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
131 int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
134 struct v4l2_events *events = fh->events;
138 return __v4l2_event_dequeue(fh, event);
140 /* Release the vdev lock while waiting */
142 mutex_unlock(fh->vdev->lock);
145 ret = wait_event_interruptible(events->wait,
146 events->navailable != 0);
150 ret = __v4l2_event_dequeue(fh, event);
151 } while (ret == -ENOENT);
154 mutex_lock(fh->vdev->lock);
158 EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
160 /* Caller must hold fh->vdev->fh_lock! */
161 static struct v4l2_subscribed_event *v4l2_event_subscribed(
162 struct v4l2_fh *fh, u32 type, u32 id)
164 struct v4l2_events *events = fh->events;
165 struct v4l2_subscribed_event *sev;
167 assert_spin_locked(&fh->vdev->fh_lock);
169 list_for_each_entry(sev, &events->subscribed, list) {
170 if (sev->type == type && sev->id == id)
177 static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
178 const struct timespec *ts)
180 struct v4l2_events *events = fh->events;
181 struct v4l2_subscribed_event *sev;
182 struct v4l2_kevent *kev;
184 /* Are we subscribed? */
185 sev = v4l2_event_subscribed(fh, ev->type, ev->id);
189 /* Increase event sequence number on fh. */
192 /* Do we have any free events? */
193 if (list_empty(&events->free))
196 /* Take one and fill it. */
197 kev = list_first_entry(&events->free, struct v4l2_kevent, list);
198 kev->event.type = ev->type;
199 kev->event.u = ev->u;
200 kev->event.id = ev->id;
201 kev->event.timestamp = *ts;
202 kev->event.sequence = events->sequence;
203 list_move_tail(&kev->list, &events->available);
205 events->navailable++;
207 wake_up_all(&events->wait);
210 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
214 struct timespec timestamp;
216 ktime_get_ts(×tamp);
218 spin_lock_irqsave(&vdev->fh_lock, flags);
220 list_for_each_entry(fh, &vdev->fh_list, list) {
221 __v4l2_event_queue_fh(fh, ev, ×tamp);
224 spin_unlock_irqrestore(&vdev->fh_lock, flags);
226 EXPORT_SYMBOL_GPL(v4l2_event_queue);
228 void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
231 struct timespec timestamp;
233 ktime_get_ts(×tamp);
235 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
236 __v4l2_event_queue_fh(fh, ev, ×tamp);
237 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
239 EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);
241 int v4l2_event_pending(struct v4l2_fh *fh)
243 return fh->events->navailable;
245 EXPORT_SYMBOL_GPL(v4l2_event_pending);
247 int v4l2_event_subscribe(struct v4l2_fh *fh,
248 struct v4l2_event_subscription *sub)
250 struct v4l2_events *events = fh->events;
251 struct v4l2_subscribed_event *sev, *found_ev;
252 struct v4l2_ctrl *ctrl = NULL;
253 struct v4l2_ctrl_fh *ctrl_fh = NULL;
256 if (fh->events == NULL) {
261 if (sub->type == V4L2_EVENT_CTRL) {
262 ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
267 sev = kmalloc(sizeof(*sev), GFP_KERNEL);
271 ctrl_fh = kzalloc(sizeof(*ctrl_fh), GFP_KERNEL);
279 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
281 found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
283 INIT_LIST_HEAD(&sev->list);
284 sev->type = sub->type;
287 list_add(&sev->list, &events->subscribed);
291 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
293 /* v4l2_ctrl_add_fh uses a mutex, so do this outside the spin lock */
298 v4l2_ctrl_add_fh(fh->ctrl_handler, ctrl_fh, sub);
305 EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
307 static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
309 struct v4l2_events *events = fh->events;
310 struct v4l2_event_subscription sub;
311 struct v4l2_subscribed_event *sev;
317 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
318 if (!list_empty(&events->subscribed)) {
319 sev = list_first_entry(&events->subscribed,
320 struct v4l2_subscribed_event, list);
321 sub.type = sev->type;
324 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
326 v4l2_event_unsubscribe(fh, &sub);
330 int v4l2_event_unsubscribe(struct v4l2_fh *fh,
331 struct v4l2_event_subscription *sub)
333 struct v4l2_subscribed_event *sev;
336 if (sub->type == V4L2_EVENT_ALL) {
337 v4l2_event_unsubscribe_all(fh);
341 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
343 sev = v4l2_event_subscribed(fh, sub->type, sub->id);
345 list_del(&sev->list);
347 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
348 if (sev->type == V4L2_EVENT_CTRL) {
349 struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);
352 v4l2_ctrl_del_fh(ctrl, fh);
359 EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);