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-device.h>
29 #include <media/v4l2-fh.h>
30 #include <media/v4l2-event.h>
31 #include <media/v4l2-common.h>
32 
33 #include <media/videobuf2-v4l2.h>
34 
35 static int debug;
36 module_param(debug, int, 0644);
37 
38 #define dprintk(q, level, fmt, arg...)					      \
39 	do {								      \
40 		if (debug >= level)					      \
41 			pr_info("vb2-v4l2: [%p] %s: " fmt,		      \
42 				(q)->name, __func__, ## arg);		      \
43 	} while (0)
44 
45 /* Flags that are set by us */
46 #define V4L2_BUFFER_MASK_FLAGS	(V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
47 				 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
48 				 V4L2_BUF_FLAG_PREPARED | \
49 				 V4L2_BUF_FLAG_IN_REQUEST | \
50 				 V4L2_BUF_FLAG_REQUEST_FD | \
51 				 V4L2_BUF_FLAG_TIMESTAMP_MASK)
52 /* Output buffer flags that should be passed on to the driver */
53 #define V4L2_BUFFER_OUT_FLAGS	(V4L2_BUF_FLAG_PFRAME | \
54 				 V4L2_BUF_FLAG_BFRAME | \
55 				 V4L2_BUF_FLAG_KEYFRAME | \
56 				 V4L2_BUF_FLAG_TIMECODE | \
57 				 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
58 
59 /*
60  * __verify_planes_array() - verify that the planes array passed in struct
61  * v4l2_buffer from userspace can be safely used
62  */
63 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
64 {
65 	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
66 		return 0;
67 
68 	/* Is memory for copying plane information present? */
69 	if (b->m.planes == NULL) {
70 		dprintk(vb->vb2_queue, 1,
71 			"multi-planar buffer passed but planes array not provided\n");
72 		return -EINVAL;
73 	}
74 
75 	if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
76 		dprintk(vb->vb2_queue, 1,
77 			"incorrect planes array length, expected %d, got %d\n",
78 			vb->num_planes, b->length);
79 		return -EINVAL;
80 	}
81 
82 	return 0;
83 }
84 
85 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
86 {
87 	return __verify_planes_array(vb, pb);
88 }
89 
90 /*
91  * __verify_length() - Verify that the bytesused value for each plane fits in
92  * the plane length and that the data offset doesn't exceed the bytesused value.
93  */
94 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
95 {
96 	unsigned int length;
97 	unsigned int bytesused;
98 	unsigned int plane;
99 
100 	if (V4L2_TYPE_IS_CAPTURE(b->type))
101 		return 0;
102 
103 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
104 		for (plane = 0; plane < vb->num_planes; ++plane) {
105 			length = (b->memory == VB2_MEMORY_USERPTR ||
106 				  b->memory == VB2_MEMORY_DMABUF)
107 			       ? b->m.planes[plane].length
108 				: vb->planes[plane].length;
109 			bytesused = b->m.planes[plane].bytesused
110 				  ? b->m.planes[plane].bytesused : length;
111 
112 			if (b->m.planes[plane].bytesused > length)
113 				return -EINVAL;
114 
115 			if (b->m.planes[plane].data_offset > 0 &&
116 			    b->m.planes[plane].data_offset >= bytesused)
117 				return -EINVAL;
118 		}
119 	} else {
120 		length = (b->memory == VB2_MEMORY_USERPTR ||
121 			  b->memory == VB2_MEMORY_DMABUF)
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 videobuf 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 	/*
349 	 * DMA exporter should take care of cache syncs, so we can avoid
350 	 * explicit ->prepare()/->finish() syncs. For other ->memory types
351 	 * we always need ->prepare() or/and ->finish() cache sync.
352 	 */
353 	if (q->memory == VB2_MEMORY_DMABUF) {
354 		vb->need_cache_sync_on_finish = 0;
355 		vb->need_cache_sync_on_prepare = 0;
356 		return;
357 	}
358 
359 	/*
360 	 * Cache sync/invalidation flags are set by default in order to
361 	 * preserve existing behaviour for old apps/drivers.
362 	 */
363 	vb->need_cache_sync_on_prepare = 1;
364 	vb->need_cache_sync_on_finish = 1;
365 
366 	if (!vb2_queue_allows_cache_hints(q)) {
367 		/*
368 		 * Clear buffer cache flags if queue does not support user
369 		 * space hints. That's to indicate to userspace that these
370 		 * flags won't work.
371 		 */
372 		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
373 		b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_CLEAN;
374 		return;
375 	}
376 
377 	/*
378 	 * ->finish() cache sync can be avoided when queue direction is
379 	 * TO_DEVICE.
380 	 */
381 	if (q->dma_dir == DMA_TO_DEVICE)
382 		vb->need_cache_sync_on_finish = 0;
383 
384 	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE)
385 		vb->need_cache_sync_on_finish = 0;
386 
387 	if (b->flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN)
388 		vb->need_cache_sync_on_prepare = 0;
389 }
390 
391 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
392 				    struct v4l2_buffer *b, bool is_prepare,
393 				    struct media_request **p_req)
394 {
395 	const char *opname = is_prepare ? "prepare_buf" : "qbuf";
396 	struct media_request *req;
397 	struct vb2_v4l2_buffer *vbuf;
398 	struct vb2_buffer *vb;
399 	int ret;
400 
401 	if (b->type != q->type) {
402 		dprintk(q, 1, "%s: invalid buffer type\n", opname);
403 		return -EINVAL;
404 	}
405 
406 	if (b->index >= q->num_buffers) {
407 		dprintk(q, 1, "%s: buffer index out of range\n", opname);
408 		return -EINVAL;
409 	}
410 
411 	if (q->bufs[b->index] == NULL) {
412 		/* Should never happen */
413 		dprintk(q, 1, "%s: buffer is NULL\n", opname);
414 		return -EINVAL;
415 	}
416 
417 	if (b->memory != q->memory) {
418 		dprintk(q, 1, "%s: invalid memory type\n", opname);
419 		return -EINVAL;
420 	}
421 
422 	vb = q->bufs[b->index];
423 	vbuf = to_vb2_v4l2_buffer(vb);
424 	ret = __verify_planes_array(vb, b);
425 	if (ret)
426 		return ret;
427 
428 	if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
429 	    vb->state != VB2_BUF_STATE_DEQUEUED) {
430 		dprintk(q, 1, "%s: buffer is not in dequeued state\n", opname);
431 		return -EINVAL;
432 	}
433 
434 	if (!vb->prepared) {
435 		set_buffer_cache_hints(q, vb, b);
436 		/* Copy relevant information provided by the userspace */
437 		memset(vbuf->planes, 0,
438 		       sizeof(vbuf->planes[0]) * vb->num_planes);
439 		ret = vb2_fill_vb2_v4l2_buffer(vb, b);
440 		if (ret)
441 			return ret;
442 	}
443 
444 	if (is_prepare)
445 		return 0;
446 
447 	if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
448 		if (q->requires_requests) {
449 			dprintk(q, 1, "%s: queue requires requests\n", opname);
450 			return -EBADR;
451 		}
452 		if (q->uses_requests) {
453 			dprintk(q, 1, "%s: queue uses requests\n", opname);
454 			return -EBUSY;
455 		}
456 		return 0;
457 	} else if (!q->supports_requests) {
458 		dprintk(q, 1, "%s: queue does not support requests\n", opname);
459 		return -EBADR;
460 	} else if (q->uses_qbuf) {
461 		dprintk(q, 1, "%s: queue does not use requests\n", opname);
462 		return -EBUSY;
463 	}
464 
465 	/*
466 	 * For proper locking when queueing a request you need to be able
467 	 * to lock access to the vb2 queue, so check that there is a lock
468 	 * that we can use. In addition p_req must be non-NULL.
469 	 */
470 	if (WARN_ON(!q->lock || !p_req))
471 		return -EINVAL;
472 
473 	/*
474 	 * Make sure this op is implemented by the driver. It's easy to forget
475 	 * this callback, but is it important when canceling a buffer in a
476 	 * queued request.
477 	 */
478 	if (WARN_ON(!q->ops->buf_request_complete))
479 		return -EINVAL;
480 	/*
481 	 * Make sure this op is implemented by the driver for the output queue.
482 	 * It's easy to forget this callback, but is it important to correctly
483 	 * validate the 'field' value at QBUF time.
484 	 */
485 	if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
486 		     q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
487 		    !q->ops->buf_out_validate))
488 		return -EINVAL;
489 
490 	if (b->request_fd < 0) {
491 		dprintk(q, 1, "%s: request_fd < 0\n", opname);
492 		return -EINVAL;
493 	}
494 
495 	req = media_request_get_by_fd(mdev, b->request_fd);
496 	if (IS_ERR(req)) {
497 		dprintk(q, 1, "%s: invalid request_fd\n", opname);
498 		return PTR_ERR(req);
499 	}
500 
501 	/*
502 	 * Early sanity check. This is checked again when the buffer
503 	 * is bound to the request in vb2_core_qbuf().
504 	 */
505 	if (req->state != MEDIA_REQUEST_STATE_IDLE &&
506 	    req->state != MEDIA_REQUEST_STATE_UPDATING) {
507 		dprintk(q, 1, "%s: request is not idle\n", opname);
508 		media_request_put(req);
509 		return -EBUSY;
510 	}
511 
512 	*p_req = req;
513 	vbuf->request_fd = b->request_fd;
514 
515 	return 0;
516 }
517 
518 /*
519  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
520  * returned to userspace
521  */
522 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
523 {
524 	struct v4l2_buffer *b = pb;
525 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
526 	struct vb2_queue *q = vb->vb2_queue;
527 	unsigned int plane;
528 
529 	/* Copy back data such as timestamp, flags, etc. */
530 	b->index = vb->index;
531 	b->type = vb->type;
532 	b->memory = vb->memory;
533 	b->bytesused = 0;
534 
535 	b->flags = vbuf->flags;
536 	b->field = vbuf->field;
537 	v4l2_buffer_set_timestamp(b, vb->timestamp);
538 	b->timecode = vbuf->timecode;
539 	b->sequence = vbuf->sequence;
540 	b->reserved2 = 0;
541 	b->request_fd = 0;
542 
543 	if (q->is_multiplanar) {
544 		/*
545 		 * Fill in plane-related data if userspace provided an array
546 		 * for it. The caller has already verified memory and size.
547 		 */
548 		b->length = vb->num_planes;
549 		for (plane = 0; plane < vb->num_planes; ++plane) {
550 			struct v4l2_plane *pdst = &b->m.planes[plane];
551 			struct vb2_plane *psrc = &vb->planes[plane];
552 
553 			pdst->bytesused = psrc->bytesused;
554 			pdst->length = psrc->length;
555 			if (q->memory == VB2_MEMORY_MMAP)
556 				pdst->m.mem_offset = psrc->m.offset;
557 			else if (q->memory == VB2_MEMORY_USERPTR)
558 				pdst->m.userptr = psrc->m.userptr;
559 			else if (q->memory == VB2_MEMORY_DMABUF)
560 				pdst->m.fd = psrc->m.fd;
561 			pdst->data_offset = psrc->data_offset;
562 			memset(pdst->reserved, 0, sizeof(pdst->reserved));
563 		}
564 	} else {
565 		/*
566 		 * We use length and offset in v4l2_planes array even for
567 		 * single-planar buffers, but userspace does not.
568 		 */
569 		b->length = vb->planes[0].length;
570 		b->bytesused = vb->planes[0].bytesused;
571 		if (q->memory == VB2_MEMORY_MMAP)
572 			b->m.offset = vb->planes[0].m.offset;
573 		else if (q->memory == VB2_MEMORY_USERPTR)
574 			b->m.userptr = vb->planes[0].m.userptr;
575 		else if (q->memory == VB2_MEMORY_DMABUF)
576 			b->m.fd = vb->planes[0].m.fd;
577 	}
578 
579 	/*
580 	 * Clear any buffer state related flags.
581 	 */
582 	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
583 	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
584 	if (!q->copy_timestamp) {
585 		/*
586 		 * For non-COPY timestamps, drop timestamp source bits
587 		 * and obtain the timestamp source from the queue.
588 		 */
589 		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
590 		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
591 	}
592 
593 	switch (vb->state) {
594 	case VB2_BUF_STATE_QUEUED:
595 	case VB2_BUF_STATE_ACTIVE:
596 		b->flags |= V4L2_BUF_FLAG_QUEUED;
597 		break;
598 	case VB2_BUF_STATE_IN_REQUEST:
599 		b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
600 		break;
601 	case VB2_BUF_STATE_ERROR:
602 		b->flags |= V4L2_BUF_FLAG_ERROR;
603 		/* fall through */
604 	case VB2_BUF_STATE_DONE:
605 		b->flags |= V4L2_BUF_FLAG_DONE;
606 		break;
607 	case VB2_BUF_STATE_PREPARING:
608 	case VB2_BUF_STATE_DEQUEUED:
609 		/* nothing */
610 		break;
611 	}
612 
613 	if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
614 	     vb->state == VB2_BUF_STATE_IN_REQUEST) &&
615 	    vb->synced && vb->prepared)
616 		b->flags |= V4L2_BUF_FLAG_PREPARED;
617 
618 	if (vb2_buffer_in_use(q, vb))
619 		b->flags |= V4L2_BUF_FLAG_MAPPED;
620 	if (vbuf->request_fd >= 0) {
621 		b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
622 		b->request_fd = vbuf->request_fd;
623 	}
624 }
625 
626 /*
627  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
628  * v4l2_buffer by the userspace. It also verifies that struct
629  * v4l2_buffer has a valid number of planes.
630  */
631 static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
632 {
633 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
634 	unsigned int plane;
635 
636 	if (!vb->vb2_queue->copy_timestamp)
637 		vb->timestamp = 0;
638 
639 	for (plane = 0; plane < vb->num_planes; ++plane) {
640 		if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
641 			planes[plane].m = vbuf->planes[plane].m;
642 			planes[plane].length = vbuf->planes[plane].length;
643 		}
644 		planes[plane].bytesused = vbuf->planes[plane].bytesused;
645 		planes[plane].data_offset = vbuf->planes[plane].data_offset;
646 	}
647 	return 0;
648 }
649 
650 static const struct vb2_buf_ops v4l2_buf_ops = {
651 	.verify_planes_array	= __verify_planes_array_core,
652 	.init_buffer		= __init_vb2_v4l2_buffer,
653 	.fill_user_buffer	= __fill_v4l2_buffer,
654 	.fill_vb2_buffer	= __fill_vb2_buffer,
655 	.copy_timestamp		= __copy_timestamp,
656 };
657 
658 int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
659 		       unsigned int start_idx)
660 {
661 	unsigned int i;
662 
663 	for (i = start_idx; i < q->num_buffers; i++)
664 		if (q->bufs[i]->copied_timestamp &&
665 		    q->bufs[i]->timestamp == timestamp)
666 			return i;
667 	return -1;
668 }
669 EXPORT_SYMBOL_GPL(vb2_find_timestamp);
670 
671 /*
672  * vb2_querybuf() - query video buffer information
673  * @q:		videobuf queue
674  * @b:		buffer struct passed from userspace to vidioc_querybuf handler
675  *		in driver
676  *
677  * Should be called from vidioc_querybuf ioctl handler in driver.
678  * This function will verify the passed v4l2_buffer structure and fill the
679  * relevant information for the userspace.
680  *
681  * The return values from this function are intended to be directly returned
682  * from vidioc_querybuf handler in driver.
683  */
684 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
685 {
686 	struct vb2_buffer *vb;
687 	int ret;
688 
689 	if (b->type != q->type) {
690 		dprintk(q, 1, "wrong buffer type\n");
691 		return -EINVAL;
692 	}
693 
694 	if (b->index >= q->num_buffers) {
695 		dprintk(q, 1, "buffer index out of range\n");
696 		return -EINVAL;
697 	}
698 	vb = q->bufs[b->index];
699 	ret = __verify_planes_array(vb, b);
700 	if (!ret)
701 		vb2_core_querybuf(q, b->index, b);
702 	return ret;
703 }
704 EXPORT_SYMBOL(vb2_querybuf);
705 
706 static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
707 {
708 	*caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
709 	if (q->io_modes & VB2_MMAP)
710 		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
711 	if (q->io_modes & VB2_USERPTR)
712 		*caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
713 	if (q->io_modes & VB2_DMABUF)
714 		*caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
715 	if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
716 		*caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
717 	if (q->allow_cache_hints && q->io_modes & VB2_MMAP)
718 		*caps |= V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS;
719 #ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
720 	if (q->supports_requests)
721 		*caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
722 #endif
723 }
724 
725 static void clear_consistency_attr(struct vb2_queue *q,
726 				   int memory,
727 				   unsigned int *flags)
728 {
729 	if (!q->allow_cache_hints || memory != V4L2_MEMORY_MMAP)
730 		*flags &= ~V4L2_FLAG_MEMORY_NON_CONSISTENT;
731 }
732 
733 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
734 {
735 	int ret = vb2_verify_memory_type(q, req->memory, req->type);
736 
737 	fill_buf_caps(q, &req->capabilities);
738 	clear_consistency_attr(q, req->memory, &req->flags);
739 	return ret ? ret : vb2_core_reqbufs(q, req->memory,
740 					    req->flags, &req->count);
741 }
742 EXPORT_SYMBOL_GPL(vb2_reqbufs);
743 
744 int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
745 		    struct v4l2_buffer *b)
746 {
747 	int ret;
748 
749 	if (vb2_fileio_is_active(q)) {
750 		dprintk(q, 1, "file io in progress\n");
751 		return -EBUSY;
752 	}
753 
754 	if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
755 		return -EINVAL;
756 
757 	ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
758 
759 	return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
760 }
761 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
762 
763 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
764 {
765 	unsigned requested_planes = 1;
766 	unsigned requested_sizes[VIDEO_MAX_PLANES];
767 	struct v4l2_format *f = &create->format;
768 	int ret = vb2_verify_memory_type(q, create->memory, f->type);
769 	unsigned i;
770 
771 	fill_buf_caps(q, &create->capabilities);
772 	clear_consistency_attr(q, create->memory, &create->flags);
773 	create->index = q->num_buffers;
774 	if (create->count == 0)
775 		return ret != -EBUSY ? ret : 0;
776 
777 	switch (f->type) {
778 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
779 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
780 		requested_planes = f->fmt.pix_mp.num_planes;
781 		if (requested_planes == 0 ||
782 		    requested_planes > VIDEO_MAX_PLANES)
783 			return -EINVAL;
784 		for (i = 0; i < requested_planes; i++)
785 			requested_sizes[i] =
786 				f->fmt.pix_mp.plane_fmt[i].sizeimage;
787 		break;
788 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
789 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
790 		requested_sizes[0] = f->fmt.pix.sizeimage;
791 		break;
792 	case V4L2_BUF_TYPE_VBI_CAPTURE:
793 	case V4L2_BUF_TYPE_VBI_OUTPUT:
794 		requested_sizes[0] = f->fmt.vbi.samples_per_line *
795 			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
796 		break;
797 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
798 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
799 		requested_sizes[0] = f->fmt.sliced.io_size;
800 		break;
801 	case V4L2_BUF_TYPE_SDR_CAPTURE:
802 	case V4L2_BUF_TYPE_SDR_OUTPUT:
803 		requested_sizes[0] = f->fmt.sdr.buffersize;
804 		break;
805 	case V4L2_BUF_TYPE_META_CAPTURE:
806 	case V4L2_BUF_TYPE_META_OUTPUT:
807 		requested_sizes[0] = f->fmt.meta.buffersize;
808 		break;
809 	default:
810 		return -EINVAL;
811 	}
812 	for (i = 0; i < requested_planes; i++)
813 		if (requested_sizes[i] == 0)
814 			return -EINVAL;
815 	return ret ? ret : vb2_core_create_bufs(q, create->memory,
816 						create->flags,
817 						&create->count,
818 						requested_planes,
819 						requested_sizes);
820 }
821 EXPORT_SYMBOL_GPL(vb2_create_bufs);
822 
823 int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
824 	     struct v4l2_buffer *b)
825 {
826 	struct media_request *req = NULL;
827 	int ret;
828 
829 	if (vb2_fileio_is_active(q)) {
830 		dprintk(q, 1, "file io in progress\n");
831 		return -EBUSY;
832 	}
833 
834 	ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
835 	if (ret)
836 		return ret;
837 	ret = vb2_core_qbuf(q, b->index, b, req);
838 	if (req)
839 		media_request_put(req);
840 	return ret;
841 }
842 EXPORT_SYMBOL_GPL(vb2_qbuf);
843 
844 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
845 {
846 	int ret;
847 
848 	if (vb2_fileio_is_active(q)) {
849 		dprintk(q, 1, "file io in progress\n");
850 		return -EBUSY;
851 	}
852 
853 	if (b->type != q->type) {
854 		dprintk(q, 1, "invalid buffer type\n");
855 		return -EINVAL;
856 	}
857 
858 	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
859 
860 	if (!q->is_output &&
861 	    b->flags & V4L2_BUF_FLAG_DONE &&
862 	    b->flags & V4L2_BUF_FLAG_LAST)
863 		q->last_buffer_dequeued = true;
864 
865 	/*
866 	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
867 	 *  cleared.
868 	 */
869 	b->flags &= ~V4L2_BUF_FLAG_DONE;
870 
871 	return ret;
872 }
873 EXPORT_SYMBOL_GPL(vb2_dqbuf);
874 
875 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
876 {
877 	if (vb2_fileio_is_active(q)) {
878 		dprintk(q, 1, "file io in progress\n");
879 		return -EBUSY;
880 	}
881 	return vb2_core_streamon(q, type);
882 }
883 EXPORT_SYMBOL_GPL(vb2_streamon);
884 
885 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
886 {
887 	if (vb2_fileio_is_active(q)) {
888 		dprintk(q, 1, "file io in progress\n");
889 		return -EBUSY;
890 	}
891 	return vb2_core_streamoff(q, type);
892 }
893 EXPORT_SYMBOL_GPL(vb2_streamoff);
894 
895 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
896 {
897 	return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
898 				eb->plane, eb->flags);
899 }
900 EXPORT_SYMBOL_GPL(vb2_expbuf);
901 
902 int vb2_queue_init_name(struct vb2_queue *q, const char *name)
903 {
904 	/*
905 	 * Sanity check
906 	 */
907 	if (WARN_ON(!q)			  ||
908 	    WARN_ON(q->timestamp_flags &
909 		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
910 		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
911 		return -EINVAL;
912 
913 	/* Warn that the driver should choose an appropriate timestamp type */
914 	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
915 		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
916 
917 	/* Warn that vb2_memory should match with v4l2_memory */
918 	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
919 		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
920 		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
921 		return -EINVAL;
922 
923 	if (q->buf_struct_size == 0)
924 		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
925 
926 	q->buf_ops = &v4l2_buf_ops;
927 	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
928 	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
929 	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
930 			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
931 	/*
932 	 * For compatibility with vb1: if QBUF hasn't been called yet, then
933 	 * return EPOLLERR as well. This only affects capture queues, output
934 	 * queues will always initialize waiting_for_buffers to false.
935 	 */
936 	q->quirk_poll_must_check_waiting_for_buffers = true;
937 
938 	if (name)
939 		strscpy(q->name, name, sizeof(q->name));
940 	else
941 		q->name[0] = '\0';
942 
943 	return vb2_core_queue_init(q);
944 }
945 EXPORT_SYMBOL_GPL(vb2_queue_init_name);
946 
947 int vb2_queue_init(struct vb2_queue *q)
948 {
949 	return vb2_queue_init_name(q, NULL);
950 }
951 EXPORT_SYMBOL_GPL(vb2_queue_init);
952 
953 void vb2_queue_release(struct vb2_queue *q)
954 {
955 	vb2_core_queue_release(q);
956 }
957 EXPORT_SYMBOL_GPL(vb2_queue_release);
958 
959 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
960 {
961 	struct video_device *vfd = video_devdata(file);
962 	__poll_t res;
963 
964 	res = vb2_core_poll(q, file, wait);
965 
966 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
967 		struct v4l2_fh *fh = file->private_data;
968 
969 		poll_wait(file, &fh->wait, wait);
970 		if (v4l2_event_pending(fh))
971 			res |= EPOLLPRI;
972 	}
973 
974 	return res;
975 }
976 EXPORT_SYMBOL_GPL(vb2_poll);
977 
978 /*
979  * The following functions are not part of the vb2 core API, but are helper
980  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
981  * and struct vb2_ops.
982  * They contain boilerplate code that most if not all drivers have to do
983  * and so they simplify the driver code.
984  */
985 
986 /* The queue is busy if there is a owner and you are not that owner. */
987 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
988 {
989 	return vdev->queue->owner && vdev->queue->owner != file->private_data;
990 }
991 
992 /* vb2 ioctl helpers */
993 
994 int vb2_ioctl_reqbufs(struct file *file, void *priv,
995 			  struct v4l2_requestbuffers *p)
996 {
997 	struct video_device *vdev = video_devdata(file);
998 	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
999 
1000 	fill_buf_caps(vdev->queue, &p->capabilities);
1001 	clear_consistency_attr(vdev->queue, p->memory, &p->flags);
1002 	if (res)
1003 		return res;
1004 	if (vb2_queue_is_busy(vdev, file))
1005 		return -EBUSY;
1006 	res = vb2_core_reqbufs(vdev->queue, p->memory, p->flags, &p->count);
1007 	/* If count == 0, then the owner has released all buffers and he
1008 	   is no longer owner of the queue. Otherwise we have a new owner. */
1009 	if (res == 0)
1010 		vdev->queue->owner = p->count ? file->private_data : NULL;
1011 	return res;
1012 }
1013 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1014 
1015 int vb2_ioctl_create_bufs(struct file *file, void *priv,
1016 			  struct v4l2_create_buffers *p)
1017 {
1018 	struct video_device *vdev = video_devdata(file);
1019 	int res = vb2_verify_memory_type(vdev->queue, p->memory,
1020 			p->format.type);
1021 
1022 	p->index = vdev->queue->num_buffers;
1023 	fill_buf_caps(vdev->queue, &p->capabilities);
1024 	clear_consistency_attr(vdev->queue, p->memory, &p->flags);
1025 	/*
1026 	 * If count == 0, then just check if memory and type are valid.
1027 	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1028 	 */
1029 	if (p->count == 0)
1030 		return res != -EBUSY ? res : 0;
1031 	if (res)
1032 		return res;
1033 	if (vb2_queue_is_busy(vdev, file))
1034 		return -EBUSY;
1035 
1036 	res = vb2_create_bufs(vdev->queue, p);
1037 	if (res == 0)
1038 		vdev->queue->owner = file->private_data;
1039 	return res;
1040 }
1041 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1042 
1043 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1044 			  struct v4l2_buffer *p)
1045 {
1046 	struct video_device *vdev = video_devdata(file);
1047 
1048 	if (vb2_queue_is_busy(vdev, file))
1049 		return -EBUSY;
1050 	return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
1051 }
1052 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1053 
1054 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1055 {
1056 	struct video_device *vdev = video_devdata(file);
1057 
1058 	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1059 	return vb2_querybuf(vdev->queue, p);
1060 }
1061 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1062 
1063 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1064 {
1065 	struct video_device *vdev = video_devdata(file);
1066 
1067 	if (vb2_queue_is_busy(vdev, file))
1068 		return -EBUSY;
1069 	return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
1070 }
1071 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1072 
1073 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1074 {
1075 	struct video_device *vdev = video_devdata(file);
1076 
1077 	if (vb2_queue_is_busy(vdev, file))
1078 		return -EBUSY;
1079 	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1080 }
1081 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1082 
1083 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1084 {
1085 	struct video_device *vdev = video_devdata(file);
1086 
1087 	if (vb2_queue_is_busy(vdev, file))
1088 		return -EBUSY;
1089 	return vb2_streamon(vdev->queue, i);
1090 }
1091 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1092 
1093 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1094 {
1095 	struct video_device *vdev = video_devdata(file);
1096 
1097 	if (vb2_queue_is_busy(vdev, file))
1098 		return -EBUSY;
1099 	return vb2_streamoff(vdev->queue, i);
1100 }
1101 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1102 
1103 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1104 {
1105 	struct video_device *vdev = video_devdata(file);
1106 
1107 	if (vb2_queue_is_busy(vdev, file))
1108 		return -EBUSY;
1109 	return vb2_expbuf(vdev->queue, p);
1110 }
1111 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1112 
1113 /* v4l2_file_operations helpers */
1114 
1115 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1116 {
1117 	struct video_device *vdev = video_devdata(file);
1118 
1119 	return vb2_mmap(vdev->queue, vma);
1120 }
1121 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1122 
1123 int _vb2_fop_release(struct file *file, struct mutex *lock)
1124 {
1125 	struct video_device *vdev = video_devdata(file);
1126 
1127 	if (lock)
1128 		mutex_lock(lock);
1129 	if (file->private_data == vdev->queue->owner) {
1130 		vb2_queue_release(vdev->queue);
1131 		vdev->queue->owner = NULL;
1132 	}
1133 	if (lock)
1134 		mutex_unlock(lock);
1135 	return v4l2_fh_release(file);
1136 }
1137 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1138 
1139 int vb2_fop_release(struct file *file)
1140 {
1141 	struct video_device *vdev = video_devdata(file);
1142 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1143 
1144 	return _vb2_fop_release(file, lock);
1145 }
1146 EXPORT_SYMBOL_GPL(vb2_fop_release);
1147 
1148 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1149 		size_t count, loff_t *ppos)
1150 {
1151 	struct video_device *vdev = video_devdata(file);
1152 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1153 	int err = -EBUSY;
1154 
1155 	if (!(vdev->queue->io_modes & VB2_WRITE))
1156 		return -EINVAL;
1157 	if (lock && mutex_lock_interruptible(lock))
1158 		return -ERESTARTSYS;
1159 	if (vb2_queue_is_busy(vdev, file))
1160 		goto exit;
1161 	err = vb2_write(vdev->queue, buf, count, ppos,
1162 		       file->f_flags & O_NONBLOCK);
1163 	if (vdev->queue->fileio)
1164 		vdev->queue->owner = file->private_data;
1165 exit:
1166 	if (lock)
1167 		mutex_unlock(lock);
1168 	return err;
1169 }
1170 EXPORT_SYMBOL_GPL(vb2_fop_write);
1171 
1172 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1173 		size_t count, loff_t *ppos)
1174 {
1175 	struct video_device *vdev = video_devdata(file);
1176 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1177 	int err = -EBUSY;
1178 
1179 	if (!(vdev->queue->io_modes & VB2_READ))
1180 		return -EINVAL;
1181 	if (lock && mutex_lock_interruptible(lock))
1182 		return -ERESTARTSYS;
1183 	if (vb2_queue_is_busy(vdev, file))
1184 		goto exit;
1185 	err = vb2_read(vdev->queue, buf, count, ppos,
1186 		       file->f_flags & O_NONBLOCK);
1187 	if (vdev->queue->fileio)
1188 		vdev->queue->owner = file->private_data;
1189 exit:
1190 	if (lock)
1191 		mutex_unlock(lock);
1192 	return err;
1193 }
1194 EXPORT_SYMBOL_GPL(vb2_fop_read);
1195 
1196 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1197 {
1198 	struct video_device *vdev = video_devdata(file);
1199 	struct vb2_queue *q = vdev->queue;
1200 	struct mutex *lock = q->lock ? q->lock : vdev->lock;
1201 	__poll_t res;
1202 	void *fileio;
1203 
1204 	/*
1205 	 * If this helper doesn't know how to lock, then you shouldn't be using
1206 	 * it but you should write your own.
1207 	 */
1208 	WARN_ON(!lock);
1209 
1210 	if (lock && mutex_lock_interruptible(lock))
1211 		return EPOLLERR;
1212 
1213 	fileio = q->fileio;
1214 
1215 	res = vb2_poll(vdev->queue, file, wait);
1216 
1217 	/* If fileio was started, then we have a new queue owner. */
1218 	if (!fileio && q->fileio)
1219 		q->owner = file->private_data;
1220 	if (lock)
1221 		mutex_unlock(lock);
1222 	return res;
1223 }
1224 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1225 
1226 #ifndef CONFIG_MMU
1227 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1228 		unsigned long len, unsigned long pgoff, unsigned long flags)
1229 {
1230 	struct video_device *vdev = video_devdata(file);
1231 
1232 	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1233 }
1234 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1235 #endif
1236 
1237 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1238 
1239 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1240 {
1241 	mutex_unlock(vq->lock);
1242 }
1243 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1244 
1245 void vb2_ops_wait_finish(struct vb2_queue *vq)
1246 {
1247 	mutex_lock(vq->lock);
1248 }
1249 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1250 
1251 /*
1252  * Note that this function is called during validation time and
1253  * thus the req_queue_mutex is held to ensure no request objects
1254  * can be added or deleted while validating. So there is no need
1255  * to protect the objects list.
1256  */
1257 int vb2_request_validate(struct media_request *req)
1258 {
1259 	struct media_request_object *obj;
1260 	int ret = 0;
1261 
1262 	if (!vb2_request_buffer_cnt(req))
1263 		return -ENOENT;
1264 
1265 	list_for_each_entry(obj, &req->objects, list) {
1266 		if (!obj->ops->prepare)
1267 			continue;
1268 
1269 		ret = obj->ops->prepare(obj);
1270 		if (ret)
1271 			break;
1272 	}
1273 
1274 	if (ret) {
1275 		list_for_each_entry_continue_reverse(obj, &req->objects, list)
1276 			if (obj->ops->unprepare)
1277 				obj->ops->unprepare(obj);
1278 		return ret;
1279 	}
1280 	return 0;
1281 }
1282 EXPORT_SYMBOL_GPL(vb2_request_validate);
1283 
1284 void vb2_request_queue(struct media_request *req)
1285 {
1286 	struct media_request_object *obj, *obj_safe;
1287 
1288 	/*
1289 	 * Queue all objects. Note that buffer objects are at the end of the
1290 	 * objects list, after all other object types. Once buffer objects
1291 	 * are queued, the driver might delete them immediately (if the driver
1292 	 * processes the buffer at once), so we have to use
1293 	 * list_for_each_entry_safe() to handle the case where the object we
1294 	 * queue is deleted.
1295 	 */
1296 	list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1297 		if (obj->ops->queue)
1298 			obj->ops->queue(obj);
1299 }
1300 EXPORT_SYMBOL_GPL(vb2_request_queue);
1301 
1302 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1303 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1304 MODULE_LICENSE("GPL");
1305