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