v4l2-event.c 7.0 KB
Newer Older
S
Sakari Ailus 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * v4l2-event.c
 *
 * V4L2 events.
 *
 * Copyright (C) 2009--2010 Nokia Corporation.
 *
 * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <media/v4l2-dev.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-event.h>
28
#include <media/v4l2-ctrls.h>
S
Sakari Ailus 已提交
29 30 31 32

#include <linux/sched.h>
#include <linux/slab.h>

33 34
static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh);

S
Sakari Ailus 已提交
35 36 37 38
int v4l2_event_alloc(struct v4l2_fh *fh, unsigned int n)
{
	unsigned long flags;

39
	while (fh->nallocated < n) {
S
Sakari Ailus 已提交
40 41 42 43 44 45 46
		struct v4l2_kevent *kev;

		kev = kzalloc(sizeof(*kev), GFP_KERNEL);
		if (kev == NULL)
			return -ENOMEM;

		spin_lock_irqsave(&fh->vdev->fh_lock, flags);
47 48
		list_add_tail(&kev->list, &fh->free);
		fh->nallocated++;
S
Sakari Ailus 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
	}

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_event_alloc);

#define list_kfree(list, type, member)				\
	while (!list_empty(list)) {				\
		type *hi;					\
		hi = list_first_entry(list, type, member);	\
		list_del(&hi->member);				\
		kfree(hi);					\
	}

void v4l2_event_free(struct v4l2_fh *fh)
{
66 67
	list_kfree(&fh->free, struct v4l2_kevent, list);
	list_kfree(&fh->available, struct v4l2_kevent, list);
68
	v4l2_event_unsubscribe_all(fh);
S
Sakari Ailus 已提交
69 70 71 72 73 74 75 76 77 78
}
EXPORT_SYMBOL_GPL(v4l2_event_free);

static int __v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event)
{
	struct v4l2_kevent *kev;
	unsigned long flags;

	spin_lock_irqsave(&fh->vdev->fh_lock, flags);

79
	if (list_empty(&fh->available)) {
S
Sakari Ailus 已提交
80 81 82 83
		spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
		return -ENOENT;
	}

84
	WARN_ON(fh->navailable == 0);
S
Sakari Ailus 已提交
85

86 87 88
	kev = list_first_entry(&fh->available, struct v4l2_kevent, list);
	list_move(&kev->list, &fh->free);
	fh->navailable--;
S
Sakari Ailus 已提交
89

90
	kev->event.pending = fh->navailable;
S
Sakari Ailus 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	*event = kev->event;

	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);

	return 0;
}

int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
		       int nonblocking)
{
	int ret;

	if (nonblocking)
		return __v4l2_event_dequeue(fh, event);

106 107 108 109
	/* Release the vdev lock while waiting */
	if (fh->vdev->lock)
		mutex_unlock(fh->vdev->lock);

S
Sakari Ailus 已提交
110
	do {
111 112
		ret = wait_event_interruptible(fh->wait,
					       fh->navailable != 0);
S
Sakari Ailus 已提交
113
		if (ret < 0)
114
			break;
S
Sakari Ailus 已提交
115 116 117 118

		ret = __v4l2_event_dequeue(fh, event);
	} while (ret == -ENOENT);

119 120 121
	if (fh->vdev->lock)
		mutex_lock(fh->vdev->lock);

S
Sakari Ailus 已提交
122 123
	return ret;
}
124
EXPORT_SYMBOL_GPL(v4l2_event_dequeue);
S
Sakari Ailus 已提交
125

126
/* Caller must hold fh->vdev->fh_lock! */
S
Sakari Ailus 已提交
127
static struct v4l2_subscribed_event *v4l2_event_subscribed(
128
		struct v4l2_fh *fh, u32 type, u32 id)
S
Sakari Ailus 已提交
129 130 131
{
	struct v4l2_subscribed_event *sev;

132
	assert_spin_locked(&fh->vdev->fh_lock);
S
Sakari Ailus 已提交
133

134
	list_for_each_entry(sev, &fh->subscribed, list) {
135
		if (sev->type == type && sev->id == id)
S
Sakari Ailus 已提交
136 137 138 139 140 141
			return sev;
	}

	return NULL;
}

142 143 144 145 146 147 148 149 150 151 152 153
static void __v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev,
		const struct timespec *ts)
{
	struct v4l2_subscribed_event *sev;
	struct v4l2_kevent *kev;

	/* Are we subscribed? */
	sev = v4l2_event_subscribed(fh, ev->type, ev->id);
	if (sev == NULL)
		return;

	/* Increase event sequence number on fh. */
154
	fh->sequence++;
155 156

	/* Do we have any free events? */
157
	if (list_empty(&fh->free))
158 159 160
		return;

	/* Take one and fill it. */
161
	kev = list_first_entry(&fh->free, struct v4l2_kevent, list);
162 163 164 165
	kev->event.type = ev->type;
	kev->event.u = ev->u;
	kev->event.id = ev->id;
	kev->event.timestamp = *ts;
166 167
	kev->event.sequence = fh->sequence;
	list_move_tail(&kev->list, &fh->available);
168

169
	fh->navailable++;
170

171
	wake_up_all(&fh->wait);
172 173
}

S
Sakari Ailus 已提交
174 175 176 177 178 179 180 181 182 183 184
void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
{
	struct v4l2_fh *fh;
	unsigned long flags;
	struct timespec timestamp;

	ktime_get_ts(&timestamp);

	spin_lock_irqsave(&vdev->fh_lock, flags);

	list_for_each_entry(fh, &vdev->fh_list, list) {
185
		__v4l2_event_queue_fh(fh, ev, &timestamp);
S
Sakari Ailus 已提交
186 187 188 189 190 191
	}

	spin_unlock_irqrestore(&vdev->fh_lock, flags);
}
EXPORT_SYMBOL_GPL(v4l2_event_queue);

192 193 194 195 196 197 198 199 200 201 202 203 204
void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
{
	unsigned long flags;
	struct timespec timestamp;

	ktime_get_ts(&timestamp);

	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
	__v4l2_event_queue_fh(fh, ev, &timestamp);
	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
}
EXPORT_SYMBOL_GPL(v4l2_event_queue_fh);

S
Sakari Ailus 已提交
205 206
int v4l2_event_pending(struct v4l2_fh *fh)
{
207
	return fh->navailable;
S
Sakari Ailus 已提交
208 209 210 211 212 213
}
EXPORT_SYMBOL_GPL(v4l2_event_pending);

int v4l2_event_subscribe(struct v4l2_fh *fh,
			 struct v4l2_event_subscription *sub)
{
214 215
	struct v4l2_subscribed_event *sev, *found_ev;
	struct v4l2_ctrl *ctrl = NULL;
S
Sakari Ailus 已提交
216 217
	unsigned long flags;

218 219 220 221 222 223
	if (sub->type == V4L2_EVENT_CTRL) {
		ctrl = v4l2_ctrl_find(fh->ctrl_handler, sub->id);
		if (ctrl == NULL)
			return -EINVAL;
	}

224
	sev = kzalloc(sizeof(*sev), GFP_KERNEL);
S
Sakari Ailus 已提交
225 226 227 228 229
	if (!sev)
		return -ENOMEM;

	spin_lock_irqsave(&fh->vdev->fh_lock, flags);

230 231
	found_ev = v4l2_event_subscribed(fh, sub->type, sub->id);
	if (!found_ev) {
S
Sakari Ailus 已提交
232 233
		INIT_LIST_HEAD(&sev->list);
		sev->type = sub->type;
234
		sev->id = sub->id;
235 236
		sev->fh = fh;
		sev->flags = sub->flags;
S
Sakari Ailus 已提交
237

238
		list_add(&sev->list, &fh->subscribed);
S
Sakari Ailus 已提交
239 240 241 242
	}

	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);

243
	/* v4l2_ctrl_add_fh uses a mutex, so do this outside the spin lock */
244 245 246 247
	if (found_ev)
		kfree(sev);
	else if (ctrl)
		v4l2_ctrl_add_event(ctrl, sev);
S
Sakari Ailus 已提交
248 249 250 251 252 253 254

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_event_subscribe);

static void v4l2_event_unsubscribe_all(struct v4l2_fh *fh)
{
255
	struct v4l2_event_subscription sub;
S
Sakari Ailus 已提交
256 257 258 259 260 261 262
	struct v4l2_subscribed_event *sev;
	unsigned long flags;

	do {
		sev = NULL;

		spin_lock_irqsave(&fh->vdev->fh_lock, flags);
263 264
		if (!list_empty(&fh->subscribed)) {
			sev = list_first_entry(&fh->subscribed,
265 266 267
					struct v4l2_subscribed_event, list);
			sub.type = sev->type;
			sub.id = sev->id;
S
Sakari Ailus 已提交
268 269
		}
		spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
270 271
		if (sev)
			v4l2_event_unsubscribe(fh, &sub);
S
Sakari Ailus 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	} while (sev);
}

int v4l2_event_unsubscribe(struct v4l2_fh *fh,
			   struct v4l2_event_subscription *sub)
{
	struct v4l2_subscribed_event *sev;
	unsigned long flags;

	if (sub->type == V4L2_EVENT_ALL) {
		v4l2_event_unsubscribe_all(fh);
		return 0;
	}

	spin_lock_irqsave(&fh->vdev->fh_lock, flags);

288
	sev = v4l2_event_subscribed(fh, sub->type, sub->id);
289
	if (sev != NULL) {
S
Sakari Ailus 已提交
290
		list_del(&sev->list);
291 292
		sev->fh = NULL;
	}
S
Sakari Ailus 已提交
293 294

	spin_unlock_irqrestore(&fh->vdev->fh_lock, flags);
295
	if (sev && sev->type == V4L2_EVENT_CTRL) {
296 297 298
		struct v4l2_ctrl *ctrl = v4l2_ctrl_find(fh->ctrl_handler, sev->id);

		if (ctrl)
299
			v4l2_ctrl_del_event(ctrl, sev);
300
	}
S
Sakari Ailus 已提交
301 302 303 304 305 306

	kfree(sev);

	return 0;
}
EXPORT_SYMBOL_GPL(v4l2_event_unsubscribe);