1 /*
2  * videobuf2-v4l2.c - V4L2 driver helper framework
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.com>
7  *	   Marek Szyprowski <m.szyprowski@samsung.com>
8  *
9  * The vb2_thread implementation was based on code from videobuf-dvb.c:
10  *	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation.
15  */
16 
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mm.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
26 
27 #include <media/v4l2-dev.h>
28 #include <media/v4l2-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-common.h>
31 
32 #include <media/videobuf2-v4l2.h>
33 
34 static int debug;
35 module_param(debug, int, 0644);
36 
37 #define dprintk(level, fmt, arg...)					      \
38 	do {								      \
39 		if (debug >= level)					      \
40 			pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
41 	} while (0)
42 
43 /* Flags that are set by the vb2 core */
44 #define V4L2_BUFFER_MASK_FLAGS	(V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
45 				 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
46 				 V4L2_BUF_FLAG_PREPARED | \
47 				 V4L2_BUF_FLAG_TIMESTAMP_MASK)
48 /* Output buffer flags that should be passed on to the driver */
49 #define V4L2_BUFFER_OUT_FLAGS	(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
50 				 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
51 
52 /*
53  * __verify_planes_array() - verify that the planes array passed in struct
54  * v4l2_buffer from userspace can be safely used
55  */
__verify_planes_array(struct vb2_buffer * vb,const struct v4l2_buffer * b)56 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
57 {
58 	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
59 		return 0;
60 
61 	/* Is memory for copying plane information present? */
62 	if (b->m.planes == NULL) {
63 		dprintk(1, "multi-planar buffer passed but planes array not provided\n");
64 		return -EINVAL;
65 	}
66 
67 	if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
68 		dprintk(1, "incorrect planes array length, expected %d, got %d\n",
69 			vb->num_planes, b->length);
70 		return -EINVAL;
71 	}
72 
73 	return 0;
74 }
75 
__verify_planes_array_core(struct vb2_buffer * vb,const void * pb)76 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
77 {
78 	return __verify_planes_array(vb, pb);
79 }
80 
81 /*
82  * __verify_length() - Verify that the bytesused value for each plane fits in
83  * the plane length and that the data offset doesn't exceed the bytesused value.
84  */
__verify_length(struct vb2_buffer * vb,const struct v4l2_buffer * b)85 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
86 {
87 	unsigned int length;
88 	unsigned int bytesused;
89 	unsigned int plane;
90 
91 	if (!V4L2_TYPE_IS_OUTPUT(b->type))
92 		return 0;
93 
94 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
95 		for (plane = 0; plane < vb->num_planes; ++plane) {
96 			length = (b->memory == VB2_MEMORY_USERPTR ||
97 				  b->memory == VB2_MEMORY_DMABUF)
98 			       ? b->m.planes[plane].length
99 				: vb->planes[plane].length;
100 			bytesused = b->m.planes[plane].bytesused
101 				  ? b->m.planes[plane].bytesused : length;
102 
103 			if (b->m.planes[plane].bytesused > length)
104 				return -EINVAL;
105 
106 			if (b->m.planes[plane].data_offset > 0 &&
107 			    b->m.planes[plane].data_offset >= bytesused)
108 				return -EINVAL;
109 		}
110 	} else {
111 		length = (b->memory == VB2_MEMORY_USERPTR)
112 			? b->length : vb->planes[0].length;
113 
114 		if (b->bytesused > length)
115 			return -EINVAL;
116 	}
117 
118 	return 0;
119 }
120 
__copy_timestamp(struct vb2_buffer * vb,const void * pb)121 static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
122 {
123 	const struct v4l2_buffer *b = pb;
124 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
125 	struct vb2_queue *q = vb->vb2_queue;
126 
127 	if (q->is_output) {
128 		/*
129 		 * For output buffers copy the timestamp if needed,
130 		 * and the timecode field and flag if needed.
131 		 */
132 		if (q->copy_timestamp)
133 			vb->timestamp = timeval_to_ns(&b->timestamp);
134 		vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
135 		if (b->flags & V4L2_BUF_FLAG_TIMECODE)
136 			vbuf->timecode = b->timecode;
137 	}
138 };
139 
vb2_warn_zero_bytesused(struct vb2_buffer * vb)140 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
141 {
142 	static bool check_once;
143 
144 	if (check_once)
145 		return;
146 
147 	check_once = true;
148 	WARN_ON(1);
149 
150 	pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
151 	if (vb->vb2_queue->allow_zero_bytesused)
152 		pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
153 	else
154 		pr_warn("use the actual size instead.\n");
155 }
156 
vb2_queue_or_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b,const char * opname)157 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
158 				    const char *opname)
159 {
160 	if (b->type != q->type) {
161 		dprintk(1, "%s: invalid buffer type\n", opname);
162 		return -EINVAL;
163 	}
164 
165 	if (b->index >= q->num_buffers) {
166 		dprintk(1, "%s: buffer index out of range\n", opname);
167 		return -EINVAL;
168 	}
169 
170 	if (q->bufs[b->index] == NULL) {
171 		/* Should never happen */
172 		dprintk(1, "%s: buffer is NULL\n", opname);
173 		return -EINVAL;
174 	}
175 
176 	if (b->memory != q->memory) {
177 		dprintk(1, "%s: invalid memory type\n", opname);
178 		return -EINVAL;
179 	}
180 
181 	return __verify_planes_array(q->bufs[b->index], b);
182 }
183 
184 /*
185  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
186  * returned to userspace
187  */
__fill_v4l2_buffer(struct vb2_buffer * vb,void * pb)188 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
189 {
190 	struct v4l2_buffer *b = pb;
191 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
192 	struct vb2_queue *q = vb->vb2_queue;
193 	unsigned int plane;
194 
195 	/* Copy back data such as timestamp, flags, etc. */
196 	b->index = vb->index;
197 	b->type = vb->type;
198 	b->memory = vb->memory;
199 	b->bytesused = 0;
200 
201 	b->flags = vbuf->flags;
202 	b->field = vbuf->field;
203 	b->timestamp = ns_to_timeval(vb->timestamp);
204 	b->timecode = vbuf->timecode;
205 	b->sequence = vbuf->sequence;
206 	b->reserved2 = 0;
207 	b->reserved = 0;
208 
209 	if (q->is_multiplanar) {
210 		/*
211 		 * Fill in plane-related data if userspace provided an array
212 		 * for it. The caller has already verified memory and size.
213 		 */
214 		b->length = vb->num_planes;
215 		for (plane = 0; plane < vb->num_planes; ++plane) {
216 			struct v4l2_plane *pdst = &b->m.planes[plane];
217 			struct vb2_plane *psrc = &vb->planes[plane];
218 
219 			pdst->bytesused = psrc->bytesused;
220 			pdst->length = psrc->length;
221 			if (q->memory == VB2_MEMORY_MMAP)
222 				pdst->m.mem_offset = psrc->m.offset;
223 			else if (q->memory == VB2_MEMORY_USERPTR)
224 				pdst->m.userptr = psrc->m.userptr;
225 			else if (q->memory == VB2_MEMORY_DMABUF)
226 				pdst->m.fd = psrc->m.fd;
227 			pdst->data_offset = psrc->data_offset;
228 			memset(pdst->reserved, 0, sizeof(pdst->reserved));
229 		}
230 	} else {
231 		/*
232 		 * We use length and offset in v4l2_planes array even for
233 		 * single-planar buffers, but userspace does not.
234 		 */
235 		b->length = vb->planes[0].length;
236 		b->bytesused = vb->planes[0].bytesused;
237 		if (q->memory == VB2_MEMORY_MMAP)
238 			b->m.offset = vb->planes[0].m.offset;
239 		else if (q->memory == VB2_MEMORY_USERPTR)
240 			b->m.userptr = vb->planes[0].m.userptr;
241 		else if (q->memory == VB2_MEMORY_DMABUF)
242 			b->m.fd = vb->planes[0].m.fd;
243 	}
244 
245 	/*
246 	 * Clear any buffer state related flags.
247 	 */
248 	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
249 	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
250 	if (!q->copy_timestamp) {
251 		/*
252 		 * For non-COPY timestamps, drop timestamp source bits
253 		 * and obtain the timestamp source from the queue.
254 		 */
255 		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
256 		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
257 	}
258 
259 	switch (vb->state) {
260 	case VB2_BUF_STATE_QUEUED:
261 	case VB2_BUF_STATE_ACTIVE:
262 		b->flags |= V4L2_BUF_FLAG_QUEUED;
263 		break;
264 	case VB2_BUF_STATE_ERROR:
265 		b->flags |= V4L2_BUF_FLAG_ERROR;
266 		/* fall through */
267 	case VB2_BUF_STATE_DONE:
268 		b->flags |= V4L2_BUF_FLAG_DONE;
269 		break;
270 	case VB2_BUF_STATE_PREPARED:
271 		b->flags |= V4L2_BUF_FLAG_PREPARED;
272 		break;
273 	case VB2_BUF_STATE_PREPARING:
274 	case VB2_BUF_STATE_DEQUEUED:
275 	case VB2_BUF_STATE_REQUEUEING:
276 		/* nothing */
277 		break;
278 	}
279 
280 	if (vb2_buffer_in_use(q, vb))
281 		b->flags |= V4L2_BUF_FLAG_MAPPED;
282 
283 	if (!q->is_output &&
284 		b->flags & V4L2_BUF_FLAG_DONE &&
285 		b->flags & V4L2_BUF_FLAG_LAST)
286 		q->last_buffer_dequeued = true;
287 }
288 
289 /*
290  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
291  * v4l2_buffer by the userspace. It also verifies that struct
292  * v4l2_buffer has a valid number of planes.
293  */
__fill_vb2_buffer(struct vb2_buffer * vb,const void * pb,struct vb2_plane * planes)294 static int __fill_vb2_buffer(struct vb2_buffer *vb,
295 		const void *pb, struct vb2_plane *planes)
296 {
297 	struct vb2_queue *q = vb->vb2_queue;
298 	const struct v4l2_buffer *b = pb;
299 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
300 	unsigned int plane;
301 	int ret;
302 
303 	ret = __verify_length(vb, b);
304 	if (ret < 0) {
305 		dprintk(1, "plane parameters verification failed: %d\n", ret);
306 		return ret;
307 	}
308 	if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
309 		/*
310 		 * If the format's field is ALTERNATE, then the buffer's field
311 		 * should be either TOP or BOTTOM, not ALTERNATE since that
312 		 * makes no sense. The driver has to know whether the
313 		 * buffer represents a top or a bottom field in order to
314 		 * program any DMA correctly. Using ALTERNATE is wrong, since
315 		 * that just says that it is either a top or a bottom field,
316 		 * but not which of the two it is.
317 		 */
318 		dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
319 		return -EINVAL;
320 	}
321 	vb->timestamp = 0;
322 	vbuf->sequence = 0;
323 
324 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
325 		if (b->memory == VB2_MEMORY_USERPTR) {
326 			for (plane = 0; plane < vb->num_planes; ++plane) {
327 				planes[plane].m.userptr =
328 					b->m.planes[plane].m.userptr;
329 				planes[plane].length =
330 					b->m.planes[plane].length;
331 			}
332 		}
333 		if (b->memory == VB2_MEMORY_DMABUF) {
334 			for (plane = 0; plane < vb->num_planes; ++plane) {
335 				planes[plane].m.fd =
336 					b->m.planes[plane].m.fd;
337 				planes[plane].length =
338 					b->m.planes[plane].length;
339 			}
340 		}
341 
342 		/* Fill in driver-provided information for OUTPUT types */
343 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
344 			/*
345 			 * Will have to go up to b->length when API starts
346 			 * accepting variable number of planes.
347 			 *
348 			 * If bytesused == 0 for the output buffer, then fall
349 			 * back to the full buffer size. In that case
350 			 * userspace clearly never bothered to set it and
351 			 * it's a safe assumption that they really meant to
352 			 * use the full plane sizes.
353 			 *
354 			 * Some drivers, e.g. old codec drivers, use bytesused == 0
355 			 * as a way to indicate that streaming is finished.
356 			 * In that case, the driver should use the
357 			 * allow_zero_bytesused flag to keep old userspace
358 			 * applications working.
359 			 */
360 			for (plane = 0; plane < vb->num_planes; ++plane) {
361 				struct vb2_plane *pdst = &planes[plane];
362 				struct v4l2_plane *psrc = &b->m.planes[plane];
363 
364 				if (psrc->bytesused == 0)
365 					vb2_warn_zero_bytesused(vb);
366 
367 				if (vb->vb2_queue->allow_zero_bytesused)
368 					pdst->bytesused = psrc->bytesused;
369 				else
370 					pdst->bytesused = psrc->bytesused ?
371 						psrc->bytesused : pdst->length;
372 				pdst->data_offset = psrc->data_offset;
373 			}
374 		}
375 	} else {
376 		/*
377 		 * Single-planar buffers do not use planes array,
378 		 * so fill in relevant v4l2_buffer struct fields instead.
379 		 * In videobuf we use our internal V4l2_planes struct for
380 		 * single-planar buffers as well, for simplicity.
381 		 *
382 		 * If bytesused == 0 for the output buffer, then fall back
383 		 * to the full buffer size as that's a sensible default.
384 		 *
385 		 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
386 		 * a way to indicate that streaming is finished. In that case,
387 		 * the driver should use the allow_zero_bytesused flag to keep
388 		 * old userspace applications working.
389 		 */
390 		if (b->memory == VB2_MEMORY_USERPTR) {
391 			planes[0].m.userptr = b->m.userptr;
392 			planes[0].length = b->length;
393 		}
394 
395 		if (b->memory == VB2_MEMORY_DMABUF) {
396 			planes[0].m.fd = b->m.fd;
397 			planes[0].length = b->length;
398 		}
399 
400 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
401 			if (b->bytesused == 0)
402 				vb2_warn_zero_bytesused(vb);
403 
404 			if (vb->vb2_queue->allow_zero_bytesused)
405 				planes[0].bytesused = b->bytesused;
406 			else
407 				planes[0].bytesused = b->bytesused ?
408 					b->bytesused : planes[0].length;
409 		} else
410 			planes[0].bytesused = 0;
411 
412 	}
413 
414 	/* Zero flags that the vb2 core handles */
415 	vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
416 	if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
417 		/*
418 		 * Non-COPY timestamps and non-OUTPUT queues will get
419 		 * their timestamp and timestamp source flags from the
420 		 * queue.
421 		 */
422 		vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
423 	}
424 
425 	if (V4L2_TYPE_IS_OUTPUT(b->type)) {
426 		/*
427 		 * For output buffers mask out the timecode flag:
428 		 * this will be handled later in vb2_qbuf().
429 		 * The 'field' is valid metadata for this output buffer
430 		 * and so that needs to be copied here.
431 		 */
432 		vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
433 		vbuf->field = b->field;
434 	} else {
435 		/* Zero any output buffer flags as this is a capture buffer */
436 		vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
437 		/* Zero last flag, this is a signal from driver to userspace */
438 		vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
439 	}
440 
441 	return 0;
442 }
443 
444 static const struct vb2_buf_ops v4l2_buf_ops = {
445 	.verify_planes_array	= __verify_planes_array_core,
446 	.fill_user_buffer	= __fill_v4l2_buffer,
447 	.fill_vb2_buffer	= __fill_vb2_buffer,
448 	.copy_timestamp		= __copy_timestamp,
449 };
450 
451 /*
452  * vb2_querybuf() - query video buffer information
453  * @q:		videobuf queue
454  * @b:		buffer struct passed from userspace to vidioc_querybuf handler
455  *		in driver
456  *
457  * Should be called from vidioc_querybuf ioctl handler in driver.
458  * This function will verify the passed v4l2_buffer structure and fill the
459  * relevant information for the userspace.
460  *
461  * The return values from this function are intended to be directly returned
462  * from vidioc_querybuf handler in driver.
463  */
vb2_querybuf(struct vb2_queue * q,struct v4l2_buffer * b)464 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
465 {
466 	struct vb2_buffer *vb;
467 	int ret;
468 
469 	if (b->type != q->type) {
470 		dprintk(1, "wrong buffer type\n");
471 		return -EINVAL;
472 	}
473 
474 	if (b->index >= q->num_buffers) {
475 		dprintk(1, "buffer index out of range\n");
476 		return -EINVAL;
477 	}
478 	vb = q->bufs[b->index];
479 	ret = __verify_planes_array(vb, b);
480 	if (!ret)
481 		vb2_core_querybuf(q, b->index, b);
482 	return ret;
483 }
484 EXPORT_SYMBOL(vb2_querybuf);
485 
vb2_reqbufs(struct vb2_queue * q,struct v4l2_requestbuffers * req)486 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
487 {
488 	int ret = vb2_verify_memory_type(q, req->memory, req->type);
489 
490 	return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
491 }
492 EXPORT_SYMBOL_GPL(vb2_reqbufs);
493 
vb2_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b)494 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
495 {
496 	int ret;
497 
498 	if (vb2_fileio_is_active(q)) {
499 		dprintk(1, "file io in progress\n");
500 		return -EBUSY;
501 	}
502 
503 	ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
504 
505 	return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
506 }
507 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
508 
vb2_create_bufs(struct vb2_queue * q,struct v4l2_create_buffers * create)509 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
510 {
511 	unsigned requested_planes = 1;
512 	unsigned requested_sizes[VIDEO_MAX_PLANES];
513 	struct v4l2_format *f = &create->format;
514 	int ret = vb2_verify_memory_type(q, create->memory, f->type);
515 	unsigned i;
516 
517 	create->index = q->num_buffers;
518 	if (create->count == 0)
519 		return ret != -EBUSY ? ret : 0;
520 
521 	switch (f->type) {
522 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
523 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
524 		requested_planes = f->fmt.pix_mp.num_planes;
525 		if (requested_planes == 0 ||
526 		    requested_planes > VIDEO_MAX_PLANES)
527 			return -EINVAL;
528 		for (i = 0; i < requested_planes; i++)
529 			requested_sizes[i] =
530 				f->fmt.pix_mp.plane_fmt[i].sizeimage;
531 		break;
532 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
533 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
534 		requested_sizes[0] = f->fmt.pix.sizeimage;
535 		break;
536 	case V4L2_BUF_TYPE_VBI_CAPTURE:
537 	case V4L2_BUF_TYPE_VBI_OUTPUT:
538 		requested_sizes[0] = f->fmt.vbi.samples_per_line *
539 			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
540 		break;
541 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
542 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
543 		requested_sizes[0] = f->fmt.sliced.io_size;
544 		break;
545 	case V4L2_BUF_TYPE_SDR_CAPTURE:
546 	case V4L2_BUF_TYPE_SDR_OUTPUT:
547 		requested_sizes[0] = f->fmt.sdr.buffersize;
548 		break;
549 	case V4L2_BUF_TYPE_META_CAPTURE:
550 		requested_sizes[0] = f->fmt.meta.buffersize;
551 		break;
552 	default:
553 		return -EINVAL;
554 	}
555 	for (i = 0; i < requested_planes; i++)
556 		if (requested_sizes[i] == 0)
557 			return -EINVAL;
558 	return ret ? ret : vb2_core_create_bufs(q, create->memory,
559 		&create->count, requested_planes, requested_sizes);
560 }
561 EXPORT_SYMBOL_GPL(vb2_create_bufs);
562 
vb2_qbuf(struct vb2_queue * q,struct v4l2_buffer * b)563 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
564 {
565 	int ret;
566 
567 	if (vb2_fileio_is_active(q)) {
568 		dprintk(1, "file io in progress\n");
569 		return -EBUSY;
570 	}
571 
572 	ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
573 	return ret ? ret : vb2_core_qbuf(q, b->index, b);
574 }
575 EXPORT_SYMBOL_GPL(vb2_qbuf);
576 
vb2_dqbuf(struct vb2_queue * q,struct v4l2_buffer * b,bool nonblocking)577 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
578 {
579 	int ret;
580 
581 	if (vb2_fileio_is_active(q)) {
582 		dprintk(1, "file io in progress\n");
583 		return -EBUSY;
584 	}
585 
586 	if (b->type != q->type) {
587 		dprintk(1, "invalid buffer type\n");
588 		return -EINVAL;
589 	}
590 
591 	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
592 
593 	/*
594 	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
595 	 *  cleared.
596 	 */
597 	b->flags &= ~V4L2_BUF_FLAG_DONE;
598 
599 	return ret;
600 }
601 EXPORT_SYMBOL_GPL(vb2_dqbuf);
602 
vb2_streamon(struct vb2_queue * q,enum v4l2_buf_type type)603 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
604 {
605 	if (vb2_fileio_is_active(q)) {
606 		dprintk(1, "file io in progress\n");
607 		return -EBUSY;
608 	}
609 	return vb2_core_streamon(q, type);
610 }
611 EXPORT_SYMBOL_GPL(vb2_streamon);
612 
vb2_streamoff(struct vb2_queue * q,enum v4l2_buf_type type)613 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
614 {
615 	if (vb2_fileio_is_active(q)) {
616 		dprintk(1, "file io in progress\n");
617 		return -EBUSY;
618 	}
619 	return vb2_core_streamoff(q, type);
620 }
621 EXPORT_SYMBOL_GPL(vb2_streamoff);
622 
vb2_expbuf(struct vb2_queue * q,struct v4l2_exportbuffer * eb)623 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
624 {
625 	return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
626 				eb->plane, eb->flags);
627 }
628 EXPORT_SYMBOL_GPL(vb2_expbuf);
629 
vb2_queue_init(struct vb2_queue * q)630 int vb2_queue_init(struct vb2_queue *q)
631 {
632 	/*
633 	 * Sanity check
634 	 */
635 	if (WARN_ON(!q)			  ||
636 	    WARN_ON(q->timestamp_flags &
637 		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
638 		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
639 		return -EINVAL;
640 
641 	/* Warn that the driver should choose an appropriate timestamp type */
642 	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
643 		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
644 
645 	/* Warn that vb2_memory should match with v4l2_memory */
646 	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
647 		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
648 		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
649 		return -EINVAL;
650 
651 	if (q->buf_struct_size == 0)
652 		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
653 
654 	q->buf_ops = &v4l2_buf_ops;
655 	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
656 	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
657 	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
658 			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
659 	/*
660 	 * For compatibility with vb1: if QBUF hasn't been called yet, then
661 	 * return EPOLLERR as well. This only affects capture queues, output
662 	 * queues will always initialize waiting_for_buffers to false.
663 	 */
664 	q->quirk_poll_must_check_waiting_for_buffers = true;
665 
666 	return vb2_core_queue_init(q);
667 }
668 EXPORT_SYMBOL_GPL(vb2_queue_init);
669 
vb2_queue_release(struct vb2_queue * q)670 void vb2_queue_release(struct vb2_queue *q)
671 {
672 	vb2_core_queue_release(q);
673 }
674 EXPORT_SYMBOL_GPL(vb2_queue_release);
675 
vb2_poll(struct vb2_queue * q,struct file * file,poll_table * wait)676 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
677 {
678 	struct video_device *vfd = video_devdata(file);
679 	__poll_t req_events = poll_requested_events(wait);
680 	__poll_t res = 0;
681 
682 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
683 		struct v4l2_fh *fh = file->private_data;
684 
685 		if (v4l2_event_pending(fh))
686 			res = EPOLLPRI;
687 		else if (req_events & EPOLLPRI)
688 			poll_wait(file, &fh->wait, wait);
689 	}
690 
691 	return res | vb2_core_poll(q, file, wait);
692 }
693 EXPORT_SYMBOL_GPL(vb2_poll);
694 
695 /*
696  * The following functions are not part of the vb2 core API, but are helper
697  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
698  * and struct vb2_ops.
699  * They contain boilerplate code that most if not all drivers have to do
700  * and so they simplify the driver code.
701  */
702 
703 /* The queue is busy if there is a owner and you are not that owner. */
vb2_queue_is_busy(struct video_device * vdev,struct file * file)704 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
705 {
706 	return vdev->queue->owner && vdev->queue->owner != file->private_data;
707 }
708 
709 /* vb2 ioctl helpers */
710 
vb2_ioctl_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * p)711 int vb2_ioctl_reqbufs(struct file *file, void *priv,
712 			  struct v4l2_requestbuffers *p)
713 {
714 	struct video_device *vdev = video_devdata(file);
715 	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
716 
717 	if (res)
718 		return res;
719 	if (vb2_queue_is_busy(vdev, file))
720 		return -EBUSY;
721 	res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
722 	/* If count == 0, then the owner has released all buffers and he
723 	   is no longer owner of the queue. Otherwise we have a new owner. */
724 	if (res == 0)
725 		vdev->queue->owner = p->count ? file->private_data : NULL;
726 	return res;
727 }
728 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
729 
vb2_ioctl_create_bufs(struct file * file,void * priv,struct v4l2_create_buffers * p)730 int vb2_ioctl_create_bufs(struct file *file, void *priv,
731 			  struct v4l2_create_buffers *p)
732 {
733 	struct video_device *vdev = video_devdata(file);
734 	int res = vb2_verify_memory_type(vdev->queue, p->memory,
735 			p->format.type);
736 
737 	p->index = vdev->queue->num_buffers;
738 	/*
739 	 * If count == 0, then just check if memory and type are valid.
740 	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
741 	 */
742 	if (p->count == 0)
743 		return res != -EBUSY ? res : 0;
744 	if (res)
745 		return res;
746 	if (vb2_queue_is_busy(vdev, file))
747 		return -EBUSY;
748 
749 	res = vb2_create_bufs(vdev->queue, p);
750 	if (res == 0)
751 		vdev->queue->owner = file->private_data;
752 	return res;
753 }
754 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
755 
vb2_ioctl_prepare_buf(struct file * file,void * priv,struct v4l2_buffer * p)756 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
757 			  struct v4l2_buffer *p)
758 {
759 	struct video_device *vdev = video_devdata(file);
760 
761 	if (vb2_queue_is_busy(vdev, file))
762 		return -EBUSY;
763 	return vb2_prepare_buf(vdev->queue, p);
764 }
765 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
766 
vb2_ioctl_querybuf(struct file * file,void * priv,struct v4l2_buffer * p)767 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
768 {
769 	struct video_device *vdev = video_devdata(file);
770 
771 	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
772 	return vb2_querybuf(vdev->queue, p);
773 }
774 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
775 
vb2_ioctl_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)776 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
777 {
778 	struct video_device *vdev = video_devdata(file);
779 
780 	if (vb2_queue_is_busy(vdev, file))
781 		return -EBUSY;
782 	return vb2_qbuf(vdev->queue, p);
783 }
784 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
785 
vb2_ioctl_dqbuf(struct file * file,void * priv,struct v4l2_buffer * p)786 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
787 {
788 	struct video_device *vdev = video_devdata(file);
789 
790 	if (vb2_queue_is_busy(vdev, file))
791 		return -EBUSY;
792 	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
793 }
794 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
795 
vb2_ioctl_streamon(struct file * file,void * priv,enum v4l2_buf_type i)796 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
797 {
798 	struct video_device *vdev = video_devdata(file);
799 
800 	if (vb2_queue_is_busy(vdev, file))
801 		return -EBUSY;
802 	return vb2_streamon(vdev->queue, i);
803 }
804 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
805 
vb2_ioctl_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)806 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
807 {
808 	struct video_device *vdev = video_devdata(file);
809 
810 	if (vb2_queue_is_busy(vdev, file))
811 		return -EBUSY;
812 	return vb2_streamoff(vdev->queue, i);
813 }
814 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
815 
vb2_ioctl_expbuf(struct file * file,void * priv,struct v4l2_exportbuffer * p)816 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
817 {
818 	struct video_device *vdev = video_devdata(file);
819 
820 	if (vb2_queue_is_busy(vdev, file))
821 		return -EBUSY;
822 	return vb2_expbuf(vdev->queue, p);
823 }
824 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
825 
826 /* v4l2_file_operations helpers */
827 
vb2_fop_mmap(struct file * file,struct vm_area_struct * vma)828 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
829 {
830 	struct video_device *vdev = video_devdata(file);
831 
832 	return vb2_mmap(vdev->queue, vma);
833 }
834 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
835 
_vb2_fop_release(struct file * file,struct mutex * lock)836 int _vb2_fop_release(struct file *file, struct mutex *lock)
837 {
838 	struct video_device *vdev = video_devdata(file);
839 
840 	if (lock)
841 		mutex_lock(lock);
842 	if (file->private_data == vdev->queue->owner) {
843 		vb2_queue_release(vdev->queue);
844 		vdev->queue->owner = NULL;
845 	}
846 	if (lock)
847 		mutex_unlock(lock);
848 	return v4l2_fh_release(file);
849 }
850 EXPORT_SYMBOL_GPL(_vb2_fop_release);
851 
vb2_fop_release(struct file * file)852 int vb2_fop_release(struct file *file)
853 {
854 	struct video_device *vdev = video_devdata(file);
855 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
856 
857 	return _vb2_fop_release(file, lock);
858 }
859 EXPORT_SYMBOL_GPL(vb2_fop_release);
860 
vb2_fop_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)861 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
862 		size_t count, loff_t *ppos)
863 {
864 	struct video_device *vdev = video_devdata(file);
865 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
866 	int err = -EBUSY;
867 
868 	if (!(vdev->queue->io_modes & VB2_WRITE))
869 		return -EINVAL;
870 	if (lock && mutex_lock_interruptible(lock))
871 		return -ERESTARTSYS;
872 	if (vb2_queue_is_busy(vdev, file))
873 		goto exit;
874 	err = vb2_write(vdev->queue, buf, count, ppos,
875 		       file->f_flags & O_NONBLOCK);
876 	if (vdev->queue->fileio)
877 		vdev->queue->owner = file->private_data;
878 exit:
879 	if (lock)
880 		mutex_unlock(lock);
881 	return err;
882 }
883 EXPORT_SYMBOL_GPL(vb2_fop_write);
884 
vb2_fop_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)885 ssize_t vb2_fop_read(struct file *file, char __user *buf,
886 		size_t count, loff_t *ppos)
887 {
888 	struct video_device *vdev = video_devdata(file);
889 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
890 	int err = -EBUSY;
891 
892 	if (!(vdev->queue->io_modes & VB2_READ))
893 		return -EINVAL;
894 	if (lock && mutex_lock_interruptible(lock))
895 		return -ERESTARTSYS;
896 	if (vb2_queue_is_busy(vdev, file))
897 		goto exit;
898 	err = vb2_read(vdev->queue, buf, count, ppos,
899 		       file->f_flags & O_NONBLOCK);
900 	if (vdev->queue->fileio)
901 		vdev->queue->owner = file->private_data;
902 exit:
903 	if (lock)
904 		mutex_unlock(lock);
905 	return err;
906 }
907 EXPORT_SYMBOL_GPL(vb2_fop_read);
908 
vb2_fop_poll(struct file * file,poll_table * wait)909 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
910 {
911 	struct video_device *vdev = video_devdata(file);
912 	struct vb2_queue *q = vdev->queue;
913 	struct mutex *lock = q->lock ? q->lock : vdev->lock;
914 	__poll_t res;
915 	void *fileio;
916 
917 	/*
918 	 * If this helper doesn't know how to lock, then you shouldn't be using
919 	 * it but you should write your own.
920 	 */
921 	WARN_ON(!lock);
922 
923 	if (lock && mutex_lock_interruptible(lock))
924 		return EPOLLERR;
925 
926 	fileio = q->fileio;
927 
928 	res = vb2_poll(vdev->queue, file, wait);
929 
930 	/* If fileio was started, then we have a new queue owner. */
931 	if (!fileio && q->fileio)
932 		q->owner = file->private_data;
933 	if (lock)
934 		mutex_unlock(lock);
935 	return res;
936 }
937 EXPORT_SYMBOL_GPL(vb2_fop_poll);
938 
939 #ifndef CONFIG_MMU
vb2_fop_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)940 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
941 		unsigned long len, unsigned long pgoff, unsigned long flags)
942 {
943 	struct video_device *vdev = video_devdata(file);
944 
945 	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
946 }
947 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
948 #endif
949 
950 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
951 
vb2_ops_wait_prepare(struct vb2_queue * vq)952 void vb2_ops_wait_prepare(struct vb2_queue *vq)
953 {
954 	mutex_unlock(vq->lock);
955 }
956 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
957 
vb2_ops_wait_finish(struct vb2_queue * vq)958 void vb2_ops_wait_finish(struct vb2_queue *vq)
959 {
960 	mutex_lock(vq->lock);
961 }
962 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
963 
964 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
965 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
966 MODULE_LICENSE("GPL");
967