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