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_alloc(struct v4l2_fh
*fh
, unsigned int n
)
39 while (fh
->nallocated
< n
) {
40 struct v4l2_kevent
*kev
;
42 kev
= kzalloc(sizeof(*kev
), GFP_KERNEL
);
46 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
47 list_add_tail(&kev
->list
, &fh
->free
);
49 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
54 EXPORT_SYMBOL_GPL(v4l2_event_alloc
);
56 #define list_kfree(list, type, member) \
57 while (!list_empty(list)) { \
59 hi = list_first_entry(list, type, member); \
60 list_del(&hi->member); \
64 void v4l2_event_free(struct v4l2_fh
*fh
)
66 list_kfree(&fh
->free
, struct v4l2_kevent
, list
);
67 list_kfree(&fh
->available
, struct v4l2_kevent
, list
);
68 v4l2_event_unsubscribe_all(fh
);
70 EXPORT_SYMBOL_GPL(v4l2_event_free
);
72 static int __v4l2_event_dequeue(struct v4l2_fh
*fh
, struct v4l2_event
*event
)
74 struct v4l2_kevent
*kev
;
77 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
79 if (list_empty(&fh
->available
)) {
80 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
84 WARN_ON(fh
->navailable
== 0);
86 kev
= list_first_entry(&fh
->available
, struct v4l2_kevent
, list
);
87 list_move(&kev
->list
, &fh
->free
);
90 kev
->event
.pending
= fh
->navailable
;
93 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
98 int v4l2_event_dequeue(struct v4l2_fh
*fh
, struct v4l2_event
*event
,
104 return __v4l2_event_dequeue(fh
, event
);
106 /* Release the vdev lock while waiting */
108 mutex_unlock(fh
->vdev
->lock
);
111 ret
= wait_event_interruptible(fh
->wait
,
112 fh
->navailable
!= 0);
116 ret
= __v4l2_event_dequeue(fh
, event
);
117 } while (ret
== -ENOENT
);
120 mutex_lock(fh
->vdev
->lock
);
124 EXPORT_SYMBOL_GPL(v4l2_event_dequeue
);
126 /* Caller must hold fh->vdev->fh_lock! */
127 static struct v4l2_subscribed_event
*v4l2_event_subscribed(
128 struct v4l2_fh
*fh
, u32 type
, u32 id
)
130 struct v4l2_subscribed_event
*sev
;
132 assert_spin_locked(&fh
->vdev
->fh_lock
);
134 list_for_each_entry(sev
, &fh
->subscribed
, list
) {
135 if (sev
->type
== type
&& sev
->id
== id
)
142 static void __v4l2_event_queue_fh(struct v4l2_fh
*fh
, const struct v4l2_event
*ev
,
143 const struct timespec
*ts
)
145 struct v4l2_subscribed_event
*sev
;
146 struct v4l2_kevent
*kev
;
148 /* Are we subscribed? */
149 sev
= v4l2_event_subscribed(fh
, ev
->type
, ev
->id
);
153 /* Increase event sequence number on fh. */
156 /* Do we have any free events? */
157 if (list_empty(&fh
->free
))
160 /* Take one and fill it. */
161 kev
= list_first_entry(&fh
->free
, struct v4l2_kevent
, list
);
162 kev
->event
.type
= ev
->type
;
163 kev
->event
.u
= ev
->u
;
164 kev
->event
.id
= ev
->id
;
165 kev
->event
.timestamp
= *ts
;
166 kev
->event
.sequence
= fh
->sequence
;
167 list_move_tail(&kev
->list
, &fh
->available
);
171 wake_up_all(&fh
->wait
);
174 void v4l2_event_queue(struct video_device
*vdev
, const struct v4l2_event
*ev
)
178 struct timespec timestamp
;
180 ktime_get_ts(×tamp
);
182 spin_lock_irqsave(&vdev
->fh_lock
, flags
);
184 list_for_each_entry(fh
, &vdev
->fh_list
, list
) {
185 __v4l2_event_queue_fh(fh
, ev
, ×tamp
);
188 spin_unlock_irqrestore(&vdev
->fh_lock
, flags
);
190 EXPORT_SYMBOL_GPL(v4l2_event_queue
);
192 void v4l2_event_queue_fh(struct v4l2_fh
*fh
, const struct v4l2_event
*ev
)
195 struct timespec timestamp
;
197 ktime_get_ts(×tamp
);
199 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
200 __v4l2_event_queue_fh(fh
, ev
, ×tamp
);
201 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
203 EXPORT_SYMBOL_GPL(v4l2_event_queue_fh
);
205 int v4l2_event_pending(struct v4l2_fh
*fh
)
207 return fh
->navailable
;
209 EXPORT_SYMBOL_GPL(v4l2_event_pending
);
211 int v4l2_event_subscribe(struct v4l2_fh
*fh
,
212 struct v4l2_event_subscription
*sub
)
214 struct v4l2_subscribed_event
*sev
, *found_ev
;
215 struct v4l2_ctrl
*ctrl
= NULL
;
216 struct v4l2_ctrl_fh
*ctrl_fh
= NULL
;
219 if (sub
->type
== V4L2_EVENT_CTRL
) {
220 ctrl
= v4l2_ctrl_find(fh
->ctrl_handler
, sub
->id
);
225 sev
= kmalloc(sizeof(*sev
), GFP_KERNEL
);
229 ctrl_fh
= kzalloc(sizeof(*ctrl_fh
), GFP_KERNEL
);
237 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
239 found_ev
= v4l2_event_subscribed(fh
, sub
->type
, sub
->id
);
241 INIT_LIST_HEAD(&sev
->list
);
242 sev
->type
= sub
->type
;
245 list_add(&sev
->list
, &fh
->subscribed
);
249 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
251 /* v4l2_ctrl_add_fh uses a mutex, so do this outside the spin lock */
256 v4l2_ctrl_add_fh(fh
->ctrl_handler
, ctrl_fh
, sub
);
263 EXPORT_SYMBOL_GPL(v4l2_event_subscribe
);
265 static void v4l2_event_unsubscribe_all(struct v4l2_fh
*fh
)
267 struct v4l2_event_subscription sub
;
268 struct v4l2_subscribed_event
*sev
;
274 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
275 if (!list_empty(&fh
->subscribed
)) {
276 sev
= list_first_entry(&fh
->subscribed
,
277 struct v4l2_subscribed_event
, list
);
278 sub
.type
= sev
->type
;
281 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
283 v4l2_event_unsubscribe(fh
, &sub
);
287 int v4l2_event_unsubscribe(struct v4l2_fh
*fh
,
288 struct v4l2_event_subscription
*sub
)
290 struct v4l2_subscribed_event
*sev
;
293 if (sub
->type
== V4L2_EVENT_ALL
) {
294 v4l2_event_unsubscribe_all(fh
);
298 spin_lock_irqsave(&fh
->vdev
->fh_lock
, flags
);
300 sev
= v4l2_event_subscribed(fh
, sub
->type
, sub
->id
);
302 list_del(&sev
->list
);
304 spin_unlock_irqrestore(&fh
->vdev
->fh_lock
, flags
);
305 if (sev
->type
== V4L2_EVENT_CTRL
) {
306 struct v4l2_ctrl
*ctrl
= v4l2_ctrl_find(fh
->ctrl_handler
, sev
->id
);
309 v4l2_ctrl_del_fh(ctrl
, fh
);
316 EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe
);