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>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
32 int v4l2_event_init(struct v4l2_fh *fh)
34 fh->events = kzalloc(sizeof(*fh->events), GFP_KERNEL);
35 if (fh->events == NULL)
38 init_waitqueue_head(&fh->events->wait);
40 INIT_LIST_HEAD(&fh->events->free);
41 INIT_LIST_HEAD(&fh->events->available);
42 INIT_LIST_HEAD(&fh->events->subscribed);
44 fh->events->sequence = -1;
49 int v4l2_event_alloc(struct v4l2_fh *fh, unsigned int n)
51 struct v4l2_events *events = fh->events;
59 while (events->nallocated < n) {
60 struct v4l2_kevent *kev;
62 kev = kzalloc(sizeof(*kev), GFP_KERNEL);
66 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
67 list_add_tail(&kev->list, &events->free);
69 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
74 EXPORT_SYMBOL_GPL(v4l2_event_alloc);
76 #define list_kfree(list, type, member) \
77 while (!list_empty(list)) { \
79 hi = list_first_entry(list, type, member); \
80 list_del(&hi->member); \
84 void v4l2_event_free(struct v4l2_fh *fh)
86 struct v4l2_events *events = fh->events;
91 list_kfree(&events->free, struct v4l2_kevent, list);
92 list_kfree(&events->available, struct v4l2_kevent, list);
93 list_kfree(&events->subscribed, struct v4l2_subscribed_event, list);
98 EXPORT_SYMBOL_GPL(v4l2_event_free);
100 static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
102 struct v4l2_events *events = fh->events;
103 struct v4l2_kevent *kev;
106 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
108 if (list_empty(&events->available)) {
109 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
113 WARN_ON(events->navailable == 0);
115 kev = list_first_entry(&events->available, struct v4l2_kevent, list);
116 list_move(&kev->list, &events->free);
117 events->navailable--;
119 kev->event.pending = events->navailable;
122 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
127 int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
130 struct v4l2_events *events = fh->events;
134 return __v4l2_event_dequeue(fh, event);
137 ret = wait_event_interruptible(events->wait,
138 events->navailable != 0);
142 ret = __v4l2_event_dequeue(fh, event);
143 } while (ret == -ENOENT);
148 /* Caller must hold fh->event->lock! */
149 static struct v4l2_subscribed_event *v4l2_event_subscribed(
150 struct v4l2_fh *fh, u32 type)
152 struct v4l2_events *events = fh->events;
153 struct v4l2_subscribed_event *sev;
155 assert_spin_locked(&fh->vdev->fh_lock);
157 list_for_each_entry(sev, &events->subscribed, list) {
158 if (sev->type == type)
165 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
169 struct timespec timestamp;
171 ktime_get_ts(×tamp);
173 spin_lock_irqsave(&vdev->fh_lock, flags);
175 list_for_each_entry(fh, &vdev->fh_list, list) {
176 struct v4l2_events *events = fh->events;
177 struct v4l2_kevent *kev;
179 /* Are we subscribed? */
180 if (!v4l2_event_subscribed(fh, ev->type))
183 /* Increase event sequence number on fh. */
186 /* Do we have any free events? */
187 if (list_empty(&events->free))
190 /* Take one and fill it. */
191 kev = list_first_entry(&events->free, struct v4l2_kevent, list);
192 kev->event.type = ev->type;
193 kev->event.u = ev->u;
194 kev->event.timestamp = timestamp;
195 kev->event.sequence = events->sequence;
196 list_move_tail(&kev->list, &events->available);
198 events->navailable++;
200 wake_up_all(&events->wait);
203 spin_unlock_irqrestore(&vdev->fh_lock, flags);
205 EXPORT_SYMBOL_GPL(v4l2_event_queue);
207 int v4l2_event_pending(struct v4l2_fh *fh)
209 return fh->events->navailable;
211 EXPORT_SYMBOL_GPL(v4l2_event_pending);
213 int v4l2_event_subscribe(struct v4l2_fh *fh,
214 struct v4l2_event_subscription *sub)
216 struct v4l2_events *events = fh->events;
217 struct v4l2_subscribed_event *sev;
220 if (fh->events == NULL) {
225 sev = kmalloc(sizeof(*sev), GFP_KERNEL);
229 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
231 if (v4l2_event_subscribed(fh, sub->type) == NULL) {
232 INIT_LIST_HEAD(&sev->list);
233 sev->type = sub->type;
235 list_add(&sev->list, &events->subscribed);
239 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
245 EXPORT_SYMBOL_GPL(v4l2_event_subscribe);
247 static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
249 struct v4l2_events *events = fh->events;
250 struct v4l2_subscribed_event *sev;
256 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
257 if (!list_empty(&events->subscribed)) {
258 sev = list_first_entry(&events->subscribed,
259 struct v4l2_subscribed_event, list);
260 list_del(&sev->list);
262 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
267 int v4l2_event_unsubscribe(struct v4l2_fh *fh,
268 struct v4l2_event_subscription *sub)
270 struct v4l2_subscribed_event *sev;
273 if (sub->type == V4L2_EVENT_ALL) {
274 v4l2_event_unsubscribe_all(fh);
278 spin_lock_irqsave(&fh->vdev->fh_lock, flags);
280 sev = v4l2_event_subscribed(fh, sub->type);
282 list_del(&sev->list);
284 spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
290 EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);