xref: /linux/drivers/media/common/videobuf2/videobuf2-v4l2.c (revision 3c147c29310b838fb79a84f01448e34fbcbf0334)
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/device.h>
18 #include <linux/err.h>
19 #include <linux/freezer.h>
20 #include <linux/kernel.h>
21 #include <linux/kthread.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/poll.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-dev.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-event.h>
32 #include <media/v4l2-fh.h>
33 
34 #include <media/videobuf2-v4l2.h>
35 
36 static int debug;
37 module_param(debug, int, 0644);
38 
39 #define dprintk(q, level, fmt, arg...)					      \
40 	do {								      \
41 		if (debug >= level)					      \
42 			pr_info("vb2-v4l2: [%p] %s: " fmt,		      \
43 				(q)->name, __func__, ## arg);		      \
44 	} while (0)
45 
46 /* Flags that are set by us */
47 #define V4L2_BUFFER_MASK_FLAGS	(V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
48 				 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
49 				 V4L2_BUF_FLAG_PREPARED | \
50 				 V4L2_BUF_FLAG_IN_REQUEST | \
51 				 V4L2_BUF_FLAG_REQUEST_FD | \
52 				 V4L2_BUF_FLAG_TIMESTAMP_MASK)
53 /* Output buffer flags that should be passed on to the driver */
54 #define V4L2_BUFFER_OUT_FLAGS	(V4L2_BUF_FLAG_PFRAME | \
55 				 V4L2_BUF_FLAG_BFRAME | \
56 				 V4L2_BUF_FLAG_KEYFRAME | \
57 				 V4L2_BUF_FLAG_TIMECODE | \
58 				 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
59 
60 /*
61  * __verify_planes_array() - verify that the planes array passed in struct
62  * v4l2_buffer from userspace can be safely used
63  */
64 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
65 {
66 	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
67 		return 0;
68 
69 	/* Is memory for copying plane information present? */
70 	if (b->m.planes == NULL) {
71 		dprintk(vb->vb2_queue, 1,
72 			"multi-planar buffer passed but planes array not provided\n");
73 		return -EINVAL;
74 	}
75 
76 	if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
77 		dprintk(vb->vb2_queue, 1,
78 			"incorrect planes array length, expected %d, got %d\n",
79 			vb->num_planes, b->length);
80 		return -EINVAL;
81 	}
82 
83 	return 0;
84 }
85 
86 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
87 {
88 	return __verify_planes_array(vb, pb);
89 }
90 
91 /*
92  * __verify_length() - Verify that the bytesused value for each plane fits in
93  * the plane length and that the data offset doesn't exceed the bytesused value.
94  */
95 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
96 {
97 	unsigned int length;
98 	unsigned int bytesused;
99 	unsigned int plane;
100 
101 	if (V4L2_TYPE_IS_CAPTURE(b->type))
102 		return 0;
103 
104 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
105 		for (plane = 0; plane < vb->num_planes; ++plane) {
106 			length = (b->memory == VB2_MEMORY_USERPTR ||
107 				  b->memory == VB2_MEMORY_DMABUF)
108 			       ? b->m.planes[plane].length
109 				: vb->planes[plane].length;
110 			bytesused = b->m.planes[plane].bytesused
111 				  ? b->m.planes[plane].bytesused : length;
112 
113 			if (b->m.planes[plane].bytesused > length)
114 				return -EINVAL;
115 
116 			if (b->m.planes[plane].data_offset > 0 &&
117 			    b->m.planes[plane].data_offset >= bytesused)
118 				return -EINVAL;
119 		}
120 	} else {
121 		length = (b->memory == VB2_MEMORY_USERPTR)
122 			? b->length : vb->planes[0].length;
123 
124 		if (b->bytesused > length)
125 			return -EINVAL;
126 	}
127 
128 	return 0;
129 }
130 
131 /*
132  * __init_vb2_v4l2_buffer() - initialize the vb2_v4l2_buffer struct
133  */
134 static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb)
135 {
136 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
137 
138 	vbuf->request_fd = -1;
139 }
140 
141 static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
142 {
143 	const struct v4l2_buffer *b = pb;
144 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
145 	struct vb2_queue *q = vb->vb2_queue;
146 
147 	if (q->is_output) {
148 		/*
149 		 * For output buffers copy the timestamp if needed,
150 		 * and the timecode field and flag if needed.
151 		 */
152 		if (q->copy_timestamp)
153 			vb->timestamp = v4l2_buffer_get_timestamp(b);
154 		vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
155 		if (b->flags & V4L2_BUF_FLAG_TIMECODE)
156 			vbuf->timecode = b->timecode;
157 	}
158 };
159 
160 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
161 {
162 	static bool check_once;
163 
164 	if (check_once)
165 		return;
166 
167 	check_once = true;
168 
169 	pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
170 	if (vb->vb2_queue->allow_zero_bytesused)
171 		pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
172 	else
173 		pr_warn("use the actual size instead.\n");
174 }
175 
176 static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
177 {
178 	struct vb2_queue *q = vb->vb2_queue;
179 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
180 	struct vb2_plane *planes = vbuf->planes;
181 	unsigned int plane;
182 	int ret;
183 
184 	ret = __verify_length(vb, b);
185 	if (ret < 0) {
186 		dprintk(q, 1, "plane parameters verification failed: %d\n", ret);
187 		return ret;
188 	}
189 	if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
190 		/*
191 		 * If the format's field is ALTERNATE, then the buffer's field
192 		 * should be either TOP or BOTTOM, not ALTERNATE since that
193 		 * makes no sense. The driver has to know whether the
194 		 * buffer represents a top or a bottom field in order to
195 		 * program any DMA correctly. Using ALTERNATE is wrong, since
196 		 * that just says that it is either a top or a bottom field,
197 		 * but not which of the two it is.
198 		 */
199 		dprintk(q, 1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
200 		return -EINVAL;
201 	}
202 	vbuf->sequence = 0;
203 	vbuf->request_fd = -1;
204 	vbuf->is_held = false;
205 
206 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
207 		switch (b->memory) {
208 		case VB2_MEMORY_USERPTR:
209 			for (plane = 0; plane < vb->num_planes; ++plane) {
210 				planes[plane].m.userptr =
211 					b->m.planes[plane].m.userptr;
212 				planes[plane].length =
213 					b->m.planes[plane].length;
214 			}
215 			break;
216 		case VB2_MEMORY_DMABUF:
217 			for (plane = 0; plane < vb->num_planes; ++plane) {
218 				planes[plane].m.fd =
219 					b->m.planes[plane].m.fd;
220 				planes[plane].length =
221 					b->m.planes[plane].length;
222 			}
223 			break;
224 		default:
225 			for (plane = 0; plane < vb->num_planes; ++plane) {
226 				planes[plane].m.offset =
227 					vb->planes[plane].m.offset;
228 				planes[plane].length =
229 					vb->planes[plane].length;
230 			}
231 			break;
232 		}
233 
234 		/* Fill in driver-provided information for OUTPUT types */
235 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
236 			/*
237 			 * Will have to go up to b->length when API starts
238 			 * accepting variable number of planes.
239 			 *
240 			 * If bytesused == 0 for the output buffer, then fall
241 			 * back to the full buffer size. In that case
242 			 * userspace clearly never bothered to set it and
243 			 * it's a safe assumption that they really meant to
244 			 * use the full plane sizes.
245 			 *
246 			 * Some drivers, e.g. old codec drivers, use bytesused == 0
247 			 * as a way to indicate that streaming is finished.
248 			 * In that case, the driver should use the
249 			 * allow_zero_bytesused flag to keep old userspace
250 			 * applications working.
251 			 */
252 			for (plane = 0; plane < vb->num_planes; ++plane) {
253 				struct vb2_plane *pdst = &planes[plane];
254 				struct v4l2_plane *psrc = &b->m.planes[plane];
255 
256 				if (psrc->bytesused == 0)
257 					vb2_warn_zero_bytesused(vb);
258 
259 				if (vb->vb2_queue->allow_zero_bytesused)
260 					pdst->bytesused = psrc->bytesused;
261 				else
262 					pdst->bytesused = psrc->bytesused ?
263 						psrc->bytesused : pdst->length;
264 				pdst->data_offset = psrc->data_offset;
265 			}
266 		}
267 	} else {
268 		/*
269 		 * Single-planar buffers do not use planes array,
270 		 * so fill in relevant v4l2_buffer struct fields instead.
271 		 * In vb2 we use our internal V4l2_planes struct for
272 		 * single-planar buffers as well, for simplicity.
273 		 *
274 		 * If bytesused == 0 for the output buffer, then fall back
275 		 * to the full buffer size as that's a sensible default.
276 		 *
277 		 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
278 		 * a way to indicate that streaming is finished. In that case,
279 		 * the driver should use the allow_zero_bytesused flag to keep
280 		 * old userspace applications working.
281 		 */
282 		switch (b->memory) {
283 		case VB2_MEMORY_USERPTR:
284 			planes[0].m.userptr = b->m.userptr;
285 			planes[0].length = b->length;
286 			break;
287 		case VB2_MEMORY_DMABUF:
288 			planes[0].m.fd = b->m.fd;
289 			planes[0].length = b->length;
290 			break;
291 		default:
292 			planes[0].m.offset = vb->planes[0].m.offset;
293 			planes[0].length = vb->planes[0].length;
294 			break;
295 		}
296 
297 		planes[0].data_offset = 0;
298 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
299 			if (b->bytesused == 0)
300 				vb2_warn_zero_bytesused(vb);
301 
302 			if (vb->vb2_queue->allow_zero_bytesused)
303 				planes[0].bytesused = b->bytesused;
304 			else
305 				planes[0].bytesused = b->bytesused ?
306 					b->bytesused : planes[0].length;
307 		} else
308 			planes[0].bytesused = 0;
309 
310 	}
311 
312 	/* Zero flags that we handle */
313 	vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
314 	if (!vb->vb2_queue->copy_timestamp || V4L2_TYPE_IS_CAPTURE(b->type)) {
315 		/*
316 		 * Non-COPY timestamps and non-OUTPUT queues will get
317 		 * their timestamp and timestamp source flags from the
318 		 * queue.
319 		 */
320 		vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
321 	}
322 
323 	if (V4L2_TYPE_IS_OUTPUT(b->type)) {
324 		/*
325 		 * For output buffers mask out the timecode flag:
326 		 * this will be handled later in vb2_qbuf().
327 		 * The 'field' is valid metadata for this output buffer
328 		 * and so that needs to be copied here.
329 		 */
330 		vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
331 		vbuf->field = b->field;
332 		if (!(q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
333 			vbuf->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
334 	} else {
335 		/* Zero any output buffer flags as this is a capture buffer */
336 		vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
337 		/* Zero last flag, this is a signal from driver to userspace */
338 		vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
339 	}
340 
341 	return 0;
342 }
343 
344 static void set_buffer_cache_hints(struct vb2_queue *q,
345 				   struct vb2_buffer *vb,
346 				   struct v4l2_buffer *b)
347 {
348 	if (!vb2_queue_allows_cache_hints(q)) {
349 		/*
350 		 * Clear buffer cache flags if queue does not support user
351 		 * space hints. That's to indicate to userspace that these
352 		 * flags won't work.
353 		 */
354 		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
355 		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_CLEAN;
356 		return;
357 	}
358 
359 	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE)
360 		vb->skip_cache_sync_on_finish = 1;
361 
362 	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN)
363 		vb->skip_cache_sync_on_prepare = 1;
364 }
365 
366 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
367 				    struct vb2_buffer *vb, struct v4l2_buffer *b,
368 				    bool is_prepare, struct media_request **p_req)
369 {
370 	const char *opname = is_prepare ? "prepare_buf" : "qbuf";
371 	struct media_request *req;
372 	struct vb2_v4l2_buffer *vbuf;
373 	int ret;
374 
375 	if (b->type != q->type) {
376 		dprintk(q, 1, "%s: invalid buffer type\n", opname);
377 		return -EINVAL;
378 	}
379 
380 	if (b->memory != q->memory) {
381 		dprintk(q, 1, "%s: invalid memory type\n", opname);
382 		return -EINVAL;
383 	}
384 
385 	vbuf = to_vb2_v4l2_buffer(vb);
386 	ret = __verify_planes_array(vb, b);
387 	if (ret)
388 		return ret;
389 
390 	if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
391 	    vb->state != VB2_BUF_STATE_DEQUEUED) {
392 		dprintk(q, 1, "%s: buffer is not in dequeued state\n", opname);
393 		return -EINVAL;
394 	}
395 
396 	if (!vb->prepared) {
397 		set_buffer_cache_hints(q, vb, b);
398 		/* Copy relevant information provided by the userspace */
399 		memset(vbuf->planes, 0,
400 		       sizeof(vbuf->planes[0]) * vb->num_planes);
401 		ret = vb2_fill_vb2_v4l2_buffer(vb, b);
402 		if (ret)
403 			return ret;
404 	}
405 
406 	if (is_prepare)
407 		return 0;
408 
409 	if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
410 		if (q->requires_requests) {
411 			dprintk(q, 1, "%s: queue requires requests\n", opname);
412 			return -EBADR;
413 		}
414 		if (q->uses_requests) {
415 			dprintk(q, 1, "%s: queue uses requests\n", opname);
416 			return -EBUSY;
417 		}
418 		return 0;
419 	} else if (!q->supports_requests) {
420 		dprintk(q, 1, "%s: queue does not support requests\n", opname);
421 		return -EBADR;
422 	} else if (q->uses_qbuf) {
423 		dprintk(q, 1, "%s: queue does not use requests\n", opname);
424 		return -EBUSY;
425 	}
426 
427 	/*
428 	 * For proper locking when queueing a request you need to be able
429 	 * to lock access to the vb2 queue, so check that there is a lock
430 	 * that we can use. In addition p_req must be non-NULL.
431 	 */
432 	if (WARN_ON(!q->lock || !p_req))
433 		return -EINVAL;
434 
435 	/*
436 	 * Make sure this op is implemented by the driver. It's easy to forget
437 	 * this callback, but is it important when canceling a buffer in a
438 	 * queued request.
439 	 */
440 	if (WARN_ON(!q->ops->buf_request_complete))
441 		return -EINVAL;
442 	/*
443 	 * Make sure this op is implemented by the driver for the output queue.
444 	 * It's easy to forget this callback, but is it important to correctly
445 	 * validate the 'field' value at QBUF time.
446 	 */
447 	if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
448 		     q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
449 		    !q->ops->buf_out_validate))
450 		return -EINVAL;
451 
452 	req = media_request_get_by_fd(mdev, b->request_fd);
453 	if (IS_ERR(req)) {
454 		dprintk(q, 1, "%s: invalid request_fd\n", opname);
455 		return PTR_ERR(req);
456 	}
457 
458 	/*
459 	 * Early sanity check. This is checked again when the buffer
460 	 * is bound to the request in vb2_core_qbuf().
461 	 */
462 	if (req->state != MEDIA_REQUEST_STATE_IDLE &&
463 	    req->state != MEDIA_REQUEST_STATE_UPDATING) {
464 		dprintk(q, 1, "%s: request is not idle\n", opname);
465 		media_request_put(req);
466 		return -EBUSY;
467 	}
468 
469 	*p_req = req;
470 	vbuf->request_fd = b->request_fd;
471 
472 	return 0;
473 }
474 
475 /*
476  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
477  * returned to userspace
478  */
479 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
480 {
481 	struct v4l2_buffer *b = pb;
482 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
483 	struct vb2_queue *q = vb->vb2_queue;
484 	unsigned int plane;
485 
486 	/* Copy back data such as timestamp, flags, etc. */
487 	b->index = vb->index;
488 	b->type = vb->type;
489 	b->memory = vb->memory;
490 	b->bytesused = 0;
491 
492 	b->flags = vbuf->flags;
493 	b->field = vbuf->field;
494 	v4l2_buffer_set_timestamp(b, vb->timestamp);
495 	b->timecode = vbuf->timecode;
496 	b->sequence = vbuf->sequence;
497 	b->reserved2 = 0;
498 	b->request_fd = 0;
499 
500 	if (q->is_multiplanar) {
501 		/*
502 		 * Fill in plane-related data if userspace provided an array
503 		 * for it. The caller has already verified memory and size.
504 		 */
505 		b->length = vb->num_planes;
506 		for (plane = 0; plane < vb->num_planes; ++plane) {
507 			struct v4l2_plane *pdst = &b->m.planes[plane];
508 			struct vb2_plane *psrc = &vb->planes[plane];
509 
510 			pdst->bytesused = psrc->bytesused;
511 			pdst->length = psrc->length;
512 			if (q->memory == VB2_MEMORY_MMAP)
513 				pdst->m.mem_offset = psrc->m.offset;
514 			else if (q->memory == VB2_MEMORY_USERPTR)
515 				pdst->m.userptr = psrc->m.userptr;
516 			else if (q->memory == VB2_MEMORY_DMABUF)
517 				pdst->m.fd = psrc->m.fd;
518 			pdst->data_offset = psrc->data_offset;
519 			memset(pdst->reserved, 0, sizeof(pdst->reserved));
520 		}
521 	} else {
522 		/*
523 		 * We use length and offset in v4l2_planes array even for
524 		 * single-planar buffers, but userspace does not.
525 		 */
526 		b->length = vb->planes[0].length;
527 		b->bytesused = vb->planes[0].bytesused;
528 		if (q->memory == VB2_MEMORY_MMAP)
529 			b->m.offset = vb->planes[0].m.offset;
530 		else if (q->memory == VB2_MEMORY_USERPTR)
531 			b->m.userptr = vb->planes[0].m.userptr;
532 		else if (q->memory == VB2_MEMORY_DMABUF)
533 			b->m.fd = vb->planes[0].m.fd;
534 	}
535 
536 	/*
537 	 * Clear any buffer state related flags.
538 	 */
539 	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
540 	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
541 	if (!q->copy_timestamp) {
542 		/*
543 		 * For non-COPY timestamps, drop timestamp source bits
544 		 * and obtain the timestamp source from the queue.
545 		 */
546 		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
547 		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
548 	}
549 
550 	switch (vb->state) {
551 	case VB2_BUF_STATE_QUEUED:
552 	case VB2_BUF_STATE_ACTIVE:
553 		b->flags |= V4L2_BUF_FLAG_QUEUED;
554 		break;
555 	case VB2_BUF_STATE_IN_REQUEST:
556 		b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
557 		break;
558 	case VB2_BUF_STATE_ERROR:
559 		b->flags |= V4L2_BUF_FLAG_ERROR;
560 		fallthrough;
561 	case VB2_BUF_STATE_DONE:
562 		b->flags |= V4L2_BUF_FLAG_DONE;
563 		break;
564 	case VB2_BUF_STATE_PREPARING:
565 	case VB2_BUF_STATE_DEQUEUED:
566 		/* nothing */
567 		break;
568 	}
569 
570 	if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
571 	     vb->state == VB2_BUF_STATE_IN_REQUEST) &&
572 	    vb->synced && vb->prepared)
573 		b->flags |= V4L2_BUF_FLAG_PREPARED;
574 
575 	if (vb2_buffer_in_use(q, vb))
576 		b->flags |= V4L2_BUF_FLAG_MAPPED;
577 	if (vbuf->request_fd >= 0) {
578 		b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
579 		b->request_fd = vbuf->request_fd;
580 	}
581 }
582 
583 /*
584  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
585  * v4l2_buffer by the userspace. It also verifies that struct
586  * v4l2_buffer has a valid number of planes.
587  */
588 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
589 {
590 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
591 	unsigned int plane;
592 
593 	if (!vb->vb2_queue->copy_timestamp)
594 		vb->timestamp = 0;
595 
596 	for (plane = 0; plane < vb->num_planes; ++plane) {
597 		if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
598 			planes[plane].m = vbuf->planes[plane].m;
599 			planes[plane].length = vbuf->planes[plane].length;
600 		}
601 		planes[plane].bytesused = vbuf->planes[plane].bytesused;
602 		planes[plane].data_offset = vbuf->planes[plane].data_offset;
603 	}
604 	return 0;
605 }
606 
607 static const struct vb2_buf_ops v4l2_buf_ops = {
608 	.verify_planes_array	= __verify_planes_array_core,
609 	.init_buffer		= __init_vb2_v4l2_buffer,
610 	.fill_user_buffer	= __fill_v4l2_buffer,
611 	.fill_vb2_buffer	= __fill_vb2_buffer,
612 	.copy_timestamp		= __copy_timestamp,
613 };
614 
615 struct vb2_buffer *vb2_find_buffer(struct vb2_queue *q, u64 timestamp)
616 {
617 	unsigned int i;
618 
619 	for (i = 0; i < q->num_buffers; i++)
620 		if (q->bufs[i]->copied_timestamp &&
621 		    q->bufs[i]->timestamp == timestamp)
622 			return vb2_get_buffer(q, i);
623 	return NULL;
624 }
625 EXPORT_SYMBOL_GPL(vb2_find_buffer);
626 
627 /*
628  * vb2_querybuf() - query video buffer information
629  * @q:		vb2 queue
630  * @b:		buffer struct passed from userspace to vidioc_querybuf handler
631  *		in driver
632  *
633  * Should be called from vidioc_querybuf ioctl handler in driver.
634  * This function will verify the passed v4l2_buffer structure and fill the
635  * relevant information for the userspace.
636  *
637  * The return values from this function are intended to be directly returned
638  * from vidioc_querybuf handler in driver.
639  */
640 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
641 {
642 	struct vb2_buffer *vb;
643 	int ret;
644 
645 	if (b->type != q->type) {
646 		dprintk(q, 1, "wrong buffer type\n");
647 		return -EINVAL;
648 	}
649 
650 	if (b->index >= q->num_buffers) {
651 		dprintk(q, 1, "buffer index out of range\n");
652 		return -EINVAL;
653 	}
654 	vb = q->bufs[b->index];
655 	ret = __verify_planes_array(vb, b);
656 	if (!ret)
657 		vb2_core_querybuf(q, vb, b);
658 	return ret;
659 }
660 EXPORT_SYMBOL(vb2_querybuf);
661 
662 static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
663 {
664 	*caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
665 	if (q->io_modes & VB2_MMAP)
666 		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
667 	if (q->io_modes & VB2_USERPTR)
668 		*caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
669 	if (q->io_modes & VB2_DMABUF)
670 		*caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
671 	if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
672 		*caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
673 	if (q->allow_cache_hints && q->io_modes & VB2_MMAP)
674 		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS;
675 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
676 	if (q->supports_requests)
677 		*caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
678 #endif
679 }
680 
681 static void validate_memory_flags(struct vb2_queue *q,
682 				  int memory,
683 				  u32 *flags)
684 {
685 	if (!q->allow_cache_hints || memory != V4L2_MEMORY_MMAP) {
686 		/*
687 		 * This needs to clear V4L2_MEMORY_FLAG_NON_COHERENT only,
688 		 * but in order to avoid bugs we zero out all bits.
689 		 */
690 		*flags = 0;
691 	} else {
692 		/* Clear all unknown flags. */
693 		*flags &= V4L2_MEMORY_FLAG_NON_COHERENT;
694 	}
695 }
696 
697 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
698 {
699 	int ret = vb2_verify_memory_type(q, req->memory, req->type);
700 	u32 flags = req->flags;
701 
702 	fill_buf_caps(q, &req->capabilities);
703 	validate_memory_flags(q, req->memory, &flags);
704 	req->flags = flags;
705 	return ret ? ret : vb2_core_reqbufs(q, req->memory,
706 					    req->flags, &req->count);
707 }
708 EXPORT_SYMBOL_GPL(vb2_reqbufs);
709 
710 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
711 		    struct v4l2_buffer *b)
712 {
713 	struct vb2_buffer *vb;
714 	int ret;
715 
716 	if (vb2_fileio_is_active(q)) {
717 		dprintk(q, 1, "file io in progress\n");
718 		return -EBUSY;
719 	}
720 
721 	if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
722 		return -EINVAL;
723 
724 	if (b->index >= q->num_buffers) {
725 		dprintk(q, 1, "buffer index out of range\n");
726 		return -EINVAL;
727 	}
728 	vb = q->bufs[b->index];
729 
730 	ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, true, NULL);
731 
732 	return ret ? ret : vb2_core_prepare_buf(q, vb, b);
733 }
734 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
735 
736 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
737 {
738 	unsigned requested_planes = 1;
739 	unsigned requested_sizes[VIDEO_MAX_PLANES];
740 	struct v4l2_format *f = &create->format;
741 	int ret = vb2_verify_memory_type(q, create->memory, f->type);
742 	unsigned i;
743 
744 	fill_buf_caps(q, &create->capabilities);
745 	validate_memory_flags(q, create->memory, &create->flags);
746 	create->index = q->num_buffers;
747 	if (create->count == 0)
748 		return ret != -EBUSY ? ret : 0;
749 
750 	switch (f->type) {
751 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
752 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
753 		requested_planes = f->fmt.pix_mp.num_planes;
754 		if (requested_planes == 0 ||
755 		    requested_planes > VIDEO_MAX_PLANES)
756 			return -EINVAL;
757 		for (i = 0; i < requested_planes; i++)
758 			requested_sizes[i] =
759 				f->fmt.pix_mp.plane_fmt[i].sizeimage;
760 		break;
761 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
762 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
763 		requested_sizes[0] = f->fmt.pix.sizeimage;
764 		break;
765 	case V4L2_BUF_TYPE_VBI_CAPTURE:
766 	case V4L2_BUF_TYPE_VBI_OUTPUT:
767 		requested_sizes[0] = f->fmt.vbi.samples_per_line *
768 			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
769 		break;
770 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
771 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
772 		requested_sizes[0] = f->fmt.sliced.io_size;
773 		break;
774 	case V4L2_BUF_TYPE_SDR_CAPTURE:
775 	case V4L2_BUF_TYPE_SDR_OUTPUT:
776 		requested_sizes[0] = f->fmt.sdr.buffersize;
777 		break;
778 	case V4L2_BUF_TYPE_META_CAPTURE:
779 	case V4L2_BUF_TYPE_META_OUTPUT:
780 		requested_sizes[0] = f->fmt.meta.buffersize;
781 		break;
782 	default:
783 		return -EINVAL;
784 	}
785 	for (i = 0; i < requested_planes; i++)
786 		if (requested_sizes[i] == 0)
787 			return -EINVAL;
788 	return ret ? ret : vb2_core_create_bufs(q, create->memory,
789 						create->flags,
790 						&create->count,
791 						requested_planes,
792 						requested_sizes);
793 }
794 EXPORT_SYMBOL_GPL(vb2_create_bufs);
795 
796 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
797 	     struct v4l2_buffer *b)
798 {
799 	struct media_request *req = NULL;
800 	struct vb2_buffer *vb;
801 	int ret;
802 
803 	if (vb2_fileio_is_active(q)) {
804 		dprintk(q, 1, "file io in progress\n");
805 		return -EBUSY;
806 	}
807 
808 	if (b->index >= q->num_buffers) {
809 		dprintk(q, 1, "buffer index out of range\n");
810 		return -EINVAL;
811 	}
812 	vb = q->bufs[b->index];
813 
814 	ret = vb2_queue_or_prepare_buf(q, mdev, vb, b, false, &req);
815 	if (ret)
816 		return ret;
817 	ret = vb2_core_qbuf(q, vb, b, req);
818 	if (req)
819 		media_request_put(req);
820 	return ret;
821 }
822 EXPORT_SYMBOL_GPL(vb2_qbuf);
823 
824 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
825 {
826 	int ret;
827 
828 	if (vb2_fileio_is_active(q)) {
829 		dprintk(q, 1, "file io in progress\n");
830 		return -EBUSY;
831 	}
832 
833 	if (b->type != q->type) {
834 		dprintk(q, 1, "invalid buffer type\n");
835 		return -EINVAL;
836 	}
837 
838 	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
839 
840 	if (!q->is_output &&
841 	    b->flags & V4L2_BUF_FLAG_DONE &&
842 	    b->flags & V4L2_BUF_FLAG_LAST)
843 		q->last_buffer_dequeued = true;
844 
845 	/*
846 	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
847 	 *  cleared.
848 	 */
849 	b->flags &= ~V4L2_BUF_FLAG_DONE;
850 
851 	return ret;
852 }
853 EXPORT_SYMBOL_GPL(vb2_dqbuf);
854 
855 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
856 {
857 	if (vb2_fileio_is_active(q)) {
858 		dprintk(q, 1, "file io in progress\n");
859 		return -EBUSY;
860 	}
861 	return vb2_core_streamon(q, type);
862 }
863 EXPORT_SYMBOL_GPL(vb2_streamon);
864 
865 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
866 {
867 	if (vb2_fileio_is_active(q)) {
868 		dprintk(q, 1, "file io in progress\n");
869 		return -EBUSY;
870 	}
871 	return vb2_core_streamoff(q, type);
872 }
873 EXPORT_SYMBOL_GPL(vb2_streamoff);
874 
875 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
876 {
877 	struct vb2_buffer *vb;
878 
879 	if (eb->index >= q->num_buffers) {
880 		dprintk(q, 1, "buffer index out of range\n");
881 		return -EINVAL;
882 	}
883 	vb = q->bufs[eb->index];
884 
885 	return vb2_core_expbuf(q, &eb->fd, eb->type, vb,
886 				eb->plane, eb->flags);
887 }
888 EXPORT_SYMBOL_GPL(vb2_expbuf);
889 
890 int vb2_queue_init_name(struct vb2_queue *q, const char *name)
891 {
892 	/*
893 	 * Sanity check
894 	 */
895 	if (WARN_ON(!q)			  ||
896 	    WARN_ON(q->timestamp_flags &
897 		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
898 		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
899 		return -EINVAL;
900 
901 	/* Warn that the driver should choose an appropriate timestamp type */
902 	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
903 		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
904 
905 	/* Warn that vb2_memory should match with v4l2_memory */
906 	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
907 		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
908 		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
909 		return -EINVAL;
910 
911 	if (q->buf_struct_size == 0)
912 		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
913 
914 	q->buf_ops = &v4l2_buf_ops;
915 	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
916 	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
917 	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
918 			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
919 	/*
920 	 * For compatibility with vb1: if QBUF hasn't been called yet, then
921 	 * return EPOLLERR as well. This only affects capture queues, output
922 	 * queues will always initialize waiting_for_buffers to false.
923 	 */
924 	q->quirk_poll_must_check_waiting_for_buffers = true;
925 
926 	if (name)
927 		strscpy(q->name, name, sizeof(q->name));
928 	else
929 		q->name[0] = '\0';
930 
931 	return vb2_core_queue_init(q);
932 }
933 EXPORT_SYMBOL_GPL(vb2_queue_init_name);
934 
935 int vb2_queue_init(struct vb2_queue *q)
936 {
937 	return vb2_queue_init_name(q, NULL);
938 }
939 EXPORT_SYMBOL_GPL(vb2_queue_init);
940 
941 void vb2_queue_release(struct vb2_queue *q)
942 {
943 	vb2_core_queue_release(q);
944 }
945 EXPORT_SYMBOL_GPL(vb2_queue_release);
946 
947 int vb2_queue_change_type(struct vb2_queue *q, unsigned int type)
948 {
949 	if (type == q->type)
950 		return 0;
951 
952 	if (vb2_is_busy(q))
953 		return -EBUSY;
954 
955 	q->type = type;
956 
957 	return 0;
958 }
959 EXPORT_SYMBOL_GPL(vb2_queue_change_type);
960 
961 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
962 {
963 	struct video_device *vfd = video_devdata(file);
964 	__poll_t res;
965 
966 	res = vb2_core_poll(q, file, wait);
967 
968 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
969 		struct v4l2_fh *fh = file->private_data;
970 
971 		poll_wait(file, &fh->wait, wait);
972 		if (v4l2_event_pending(fh))
973 			res |= EPOLLPRI;
974 	}
975 
976 	return res;
977 }
978 EXPORT_SYMBOL_GPL(vb2_poll);
979 
980 /*
981  * The following functions are not part of the vb2 core API, but are helper
982  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
983  * and struct vb2_ops.
984  * They contain boilerplate code that most if not all drivers have to do
985  * and so they simplify the driver code.
986  */
987 
988 /* vb2 ioctl helpers */
989 
990 int vb2_ioctl_reqbufs(struct file *file, void *priv,
991 			  struct v4l2_requestbuffers *p)
992 {
993 	struct video_device *vdev = video_devdata(file);
994 	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
995 	u32 flags = p->flags;
996 
997 	fill_buf_caps(vdev->queue, &p->capabilities);
998 	validate_memory_flags(vdev->queue, p->memory, &flags);
999 	p->flags = flags;
1000 	if (res)
1001 		return res;
1002 	if (vb2_queue_is_busy(vdev->queue, file))
1003 		return -EBUSY;
1004 	res = vb2_core_reqbufs(vdev->queue, p->memory, p->flags, &p->count);
1005 	/* If count == 0, then the owner has released all buffers and he
1006 	   is no longer owner of the queue. Otherwise we have a new owner. */
1007 	if (res == 0)
1008 		vdev->queue->owner = p->count ? file->private_data : NULL;
1009 	return res;
1010 }
1011 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1012 
1013 int vb2_ioctl_create_bufs(struct file *file, void *priv,
1014 			  struct v4l2_create_buffers *p)
1015 {
1016 	struct video_device *vdev = video_devdata(file);
1017 	int res = vb2_verify_memory_type(vdev->queue, p->memory,
1018 			p->format.type);
1019 
1020 	p->index = vdev->queue->num_buffers;
1021 	fill_buf_caps(vdev->queue, &p->capabilities);
1022 	validate_memory_flags(vdev->queue, p->memory, &p->flags);
1023 	/*
1024 	 * If count == 0, then just check if memory and type are valid.
1025 	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1026 	 */
1027 	if (p->count == 0)
1028 		return res != -EBUSY ? res : 0;
1029 	if (res)
1030 		return res;
1031 	if (vb2_queue_is_busy(vdev->queue, file))
1032 		return -EBUSY;
1033 
1034 	res = vb2_create_bufs(vdev->queue, p);
1035 	if (res == 0)
1036 		vdev->queue->owner = file->private_data;
1037 	return res;
1038 }
1039 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1040 
1041 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1042 			  struct v4l2_buffer *p)
1043 {
1044 	struct video_device *vdev = video_devdata(file);
1045 
1046 	if (vb2_queue_is_busy(vdev->queue, file))
1047 		return -EBUSY;
1048 	return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
1049 }
1050 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1051 
1052 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1053 {
1054 	struct video_device *vdev = video_devdata(file);
1055 
1056 	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1057 	return vb2_querybuf(vdev->queue, p);
1058 }
1059 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1060 
1061 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1062 {
1063 	struct video_device *vdev = video_devdata(file);
1064 
1065 	if (vb2_queue_is_busy(vdev->queue, file))
1066 		return -EBUSY;
1067 	return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
1068 }
1069 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1070 
1071 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1072 {
1073 	struct video_device *vdev = video_devdata(file);
1074 
1075 	if (vb2_queue_is_busy(vdev->queue, file))
1076 		return -EBUSY;
1077 	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1078 }
1079 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1080 
1081 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1082 {
1083 	struct video_device *vdev = video_devdata(file);
1084 
1085 	if (vb2_queue_is_busy(vdev->queue, file))
1086 		return -EBUSY;
1087 	return vb2_streamon(vdev->queue, i);
1088 }
1089 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1090 
1091 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1092 {
1093 	struct video_device *vdev = video_devdata(file);
1094 
1095 	if (vb2_queue_is_busy(vdev->queue, file))
1096 		return -EBUSY;
1097 	return vb2_streamoff(vdev->queue, i);
1098 }
1099 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1100 
1101 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1102 {
1103 	struct video_device *vdev = video_devdata(file);
1104 
1105 	if (vb2_queue_is_busy(vdev->queue, file))
1106 		return -EBUSY;
1107 	return vb2_expbuf(vdev->queue, p);
1108 }
1109 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1110 
1111 /* v4l2_file_operations helpers */
1112 
1113 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1114 {
1115 	struct video_device *vdev = video_devdata(file);
1116 
1117 	return vb2_mmap(vdev->queue, vma);
1118 }
1119 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1120 
1121 int _vb2_fop_release(struct file *file, struct mutex *lock)
1122 {
1123 	struct video_device *vdev = video_devdata(file);
1124 
1125 	if (lock)
1126 		mutex_lock(lock);
1127 	if (file->private_data == vdev->queue->owner) {
1128 		vb2_queue_release(vdev->queue);
1129 		vdev->queue->owner = NULL;
1130 	}
1131 	if (lock)
1132 		mutex_unlock(lock);
1133 	return v4l2_fh_release(file);
1134 }
1135 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1136 
1137 int vb2_fop_release(struct file *file)
1138 {
1139 	struct video_device *vdev = video_devdata(file);
1140 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1141 
1142 	return _vb2_fop_release(file, lock);
1143 }
1144 EXPORT_SYMBOL_GPL(vb2_fop_release);
1145 
1146 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1147 		size_t count, loff_t *ppos)
1148 {
1149 	struct video_device *vdev = video_devdata(file);
1150 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1151 	int err = -EBUSY;
1152 
1153 	if (!(vdev->queue->io_modes & VB2_WRITE))
1154 		return -EINVAL;
1155 	if (lock && mutex_lock_interruptible(lock))
1156 		return -ERESTARTSYS;
1157 	if (vb2_queue_is_busy(vdev->queue, file))
1158 		goto exit;
1159 	err = vb2_write(vdev->queue, buf, count, ppos,
1160 		       file->f_flags & O_NONBLOCK);
1161 	if (vdev->queue->fileio)
1162 		vdev->queue->owner = file->private_data;
1163 exit:
1164 	if (lock)
1165 		mutex_unlock(lock);
1166 	return err;
1167 }
1168 EXPORT_SYMBOL_GPL(vb2_fop_write);
1169 
1170 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1171 		size_t count, loff_t *ppos)
1172 {
1173 	struct video_device *vdev = video_devdata(file);
1174 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1175 	int err = -EBUSY;
1176 
1177 	if (!(vdev->queue->io_modes & VB2_READ))
1178 		return -EINVAL;
1179 	if (lock && mutex_lock_interruptible(lock))
1180 		return -ERESTARTSYS;
1181 	if (vb2_queue_is_busy(vdev->queue, file))
1182 		goto exit;
1183 	vdev->queue->owner = file->private_data;
1184 	err = vb2_read(vdev->queue, buf, count, ppos,
1185 		       file->f_flags & O_NONBLOCK);
1186 	if (!vdev->queue->fileio)
1187 		vdev->queue->owner = NULL;
1188 exit:
1189 	if (lock)
1190 		mutex_unlock(lock);
1191 	return err;
1192 }
1193 EXPORT_SYMBOL_GPL(vb2_fop_read);
1194 
1195 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1196 {
1197 	struct video_device *vdev = video_devdata(file);
1198 	struct vb2_queue *q = vdev->queue;
1199 	struct mutex *lock = q->lock ? q->lock : vdev->lock;
1200 	__poll_t res;
1201 	void *fileio;
1202 
1203 	/*
1204 	 * If this helper doesn't know how to lock, then you shouldn't be using
1205 	 * it but you should write your own.
1206 	 */
1207 	WARN_ON(!lock);
1208 
1209 	if (lock && mutex_lock_interruptible(lock))
1210 		return EPOLLERR;
1211 
1212 	fileio = q->fileio;
1213 
1214 	res = vb2_poll(vdev->queue, file, wait);
1215 
1216 	/* If fileio was started, then we have a new queue owner. */
1217 	if (!fileio && q->fileio)
1218 		q->owner = file->private_data;
1219 	if (lock)
1220 		mutex_unlock(lock);
1221 	return res;
1222 }
1223 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1224 
1225 #ifndef CONFIG_MMU
1226 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1227 		unsigned long len, unsigned long pgoff, unsigned long flags)
1228 {
1229 	struct video_device *vdev = video_devdata(file);
1230 
1231 	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1232 }
1233 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1234 #endif
1235 
1236 void vb2_video_unregister_device(struct video_device *vdev)
1237 {
1238 	/* Check if vdev was ever registered at all */
1239 	if (!vdev || !video_is_registered(vdev))
1240 		return;
1241 
1242 	/*
1243 	 * Calling this function only makes sense if vdev->queue is set.
1244 	 * If it is NULL, then just call video_unregister_device() instead.
1245 	 */
1246 	WARN_ON(!vdev->queue);
1247 
1248 	/*
1249 	 * Take a reference to the device since video_unregister_device()
1250 	 * calls device_unregister(), but we don't want that to release
1251 	 * the device since we want to clean up the queue first.
1252 	 */
1253 	get_device(&vdev->dev);
1254 	video_unregister_device(vdev);
1255 	if (vdev->queue && vdev->queue->owner) {
1256 		struct mutex *lock = vdev->queue->lock ?
1257 			vdev->queue->lock : vdev->lock;
1258 
1259 		if (lock)
1260 			mutex_lock(lock);
1261 		vb2_queue_release(vdev->queue);
1262 		vdev->queue->owner = NULL;
1263 		if (lock)
1264 			mutex_unlock(lock);
1265 	}
1266 	/*
1267 	 * Now we put the device, and in most cases this will release
1268 	 * everything.
1269 	 */
1270 	put_device(&vdev->dev);
1271 }
1272 EXPORT_SYMBOL_GPL(vb2_video_unregister_device);
1273 
1274 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1275 
1276 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1277 {
1278 	mutex_unlock(vq->lock);
1279 }
1280 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1281 
1282 void vb2_ops_wait_finish(struct vb2_queue *vq)
1283 {
1284 	mutex_lock(vq->lock);
1285 }
1286 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1287 
1288 /*
1289  * Note that this function is called during validation time and
1290  * thus the req_queue_mutex is held to ensure no request objects
1291  * can be added or deleted while validating. So there is no need
1292  * to protect the objects list.
1293  */
1294 int vb2_request_validate(struct media_request *req)
1295 {
1296 	struct media_request_object *obj;
1297 	int ret = 0;
1298 
1299 	if (!vb2_request_buffer_cnt(req))
1300 		return -ENOENT;
1301 
1302 	list_for_each_entry(obj, &req->objects, list) {
1303 		if (!obj->ops->prepare)
1304 			continue;
1305 
1306 		ret = obj->ops->prepare(obj);
1307 		if (ret)
1308 			break;
1309 	}
1310 
1311 	if (ret) {
1312 		list_for_each_entry_continue_reverse(obj, &req->objects, list)
1313 			if (obj->ops->unprepare)
1314 				obj->ops->unprepare(obj);
1315 		return ret;
1316 	}
1317 	return 0;
1318 }
1319 EXPORT_SYMBOL_GPL(vb2_request_validate);
1320 
1321 void vb2_request_queue(struct media_request *req)
1322 {
1323 	struct media_request_object *obj, *obj_safe;
1324 
1325 	/*
1326 	 * Queue all objects. Note that buffer objects are at the end of the
1327 	 * objects list, after all other object types. Once buffer objects
1328 	 * are queued, the driver might delete them immediately (if the driver
1329 	 * processes the buffer at once), so we have to use
1330 	 * list_for_each_entry_safe() to handle the case where the object we
1331 	 * queue is deleted.
1332 	 */
1333 	list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1334 		if (obj->ops->queue)
1335 			obj->ops->queue(obj);
1336 }
1337 EXPORT_SYMBOL_GPL(vb2_request_queue);
1338 
1339 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1340 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1341 MODULE_LICENSE("GPL");
1342