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