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