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