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-fh.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-common.h>
31 
32 #include <media/videobuf2-v4l2.h>
33 
34 static int debug;
35 module_param(debug, int, 0644);
36 
37 #define dprintk(level, fmt, arg...)					      \
38 	do {								      \
39 		if (debug >= level)					      \
40 			pr_info("vb2-v4l2: %s: " fmt, __func__, ## arg); \
41 	} while (0)
42 
43 /* Flags that are set by the vb2 core */
44 #define V4L2_BUFFER_MASK_FLAGS	(V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
45 				 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
46 				 V4L2_BUF_FLAG_PREPARED | \
47 				 V4L2_BUF_FLAG_TIMESTAMP_MASK)
48 /* Output buffer flags that should be passed on to the driver */
49 #define V4L2_BUFFER_OUT_FLAGS	(V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
50 				 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
51 
52 /*
53  * __verify_planes_array() - verify that the planes array passed in struct
54  * v4l2_buffer from userspace can be safely used
55  */
__verify_planes_array(struct vb2_buffer * vb,const struct v4l2_buffer * b)56 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
57 {
58 	if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
59 		return 0;
60 
61 	/* Is memory for copying plane information present? */
62 	if (b->m.planes == NULL) {
63 		dprintk(1, "multi-planar buffer passed but planes array not provided\n");
64 		return -EINVAL;
65 	}
66 
67 	if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
68 		dprintk(1, "incorrect planes array length, expected %d, got %d\n",
69 			vb->num_planes, b->length);
70 		return -EINVAL;
71 	}
72 
73 	return 0;
74 }
75 
__verify_planes_array_core(struct vb2_buffer * vb,const void * pb)76 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
77 {
78 	return __verify_planes_array(vb, pb);
79 }
80 
81 /*
82  * __verify_length() - Verify that the bytesused value for each plane fits in
83  * the plane length and that the data offset doesn't exceed the bytesused value.
84  */
__verify_length(struct vb2_buffer * vb,const struct v4l2_buffer * b)85 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
86 {
87 	unsigned int length;
88 	unsigned int bytesused;
89 	unsigned int plane;
90 
91 	if (!V4L2_TYPE_IS_OUTPUT(b->type))
92 		return 0;
93 
94 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
95 		for (plane = 0; plane < vb->num_planes; ++plane) {
96 			length = (b->memory == VB2_MEMORY_USERPTR ||
97 				  b->memory == VB2_MEMORY_DMABUF)
98 			       ? b->m.planes[plane].length
99 				: vb->planes[plane].length;
100 			bytesused = b->m.planes[plane].bytesused
101 				  ? b->m.planes[plane].bytesused : length;
102 
103 			if (b->m.planes[plane].bytesused > length)
104 				return -EINVAL;
105 
106 			if (b->m.planes[plane].data_offset > 0 &&
107 			    b->m.planes[plane].data_offset >= bytesused)
108 				return -EINVAL;
109 		}
110 	} else {
111 		length = (b->memory == VB2_MEMORY_USERPTR)
112 			? b->length : vb->planes[0].length;
113 
114 		if (b->bytesused > length)
115 			return -EINVAL;
116 	}
117 
118 	return 0;
119 }
120 
__copy_timestamp(struct vb2_buffer * vb,const void * pb)121 static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
122 {
123 	const struct v4l2_buffer *b = pb;
124 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
125 	struct vb2_queue *q = vb->vb2_queue;
126 
127 	if (q->is_output) {
128 		/*
129 		 * For output buffers copy the timestamp if needed,
130 		 * and the timecode field and flag if needed.
131 		 */
132 		if (q->copy_timestamp)
133 			vb->timestamp = timeval_to_ns(&b->timestamp);
134 		vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
135 		if (b->flags & V4L2_BUF_FLAG_TIMECODE)
136 			vbuf->timecode = b->timecode;
137 	}
138 };
139 
vb2_warn_zero_bytesused(struct vb2_buffer * vb)140 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
141 {
142 	static bool check_once;
143 
144 	if (check_once)
145 		return;
146 
147 	check_once = true;
148 
149 	pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
150 	if (vb->vb2_queue->allow_zero_bytesused)
151 		pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
152 	else
153 		pr_warn("use the actual size instead.\n");
154 }
155 
vb2_queue_or_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b,const char * opname)156 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
157 				    const char *opname)
158 {
159 	if (b->type != q->type) {
160 		dprintk(1, "%s: invalid buffer type\n", opname);
161 		return -EINVAL;
162 	}
163 
164 	if (b->index >= q->num_buffers) {
165 		dprintk(1, "%s: buffer index out of range\n", opname);
166 		return -EINVAL;
167 	}
168 
169 	if (q->bufs[b->index] == NULL) {
170 		/* Should never happen */
171 		dprintk(1, "%s: buffer is NULL\n", opname);
172 		return -EINVAL;
173 	}
174 
175 	if (b->memory != q->memory) {
176 		dprintk(1, "%s: invalid memory type\n", opname);
177 		return -EINVAL;
178 	}
179 
180 	return __verify_planes_array(q->bufs[b->index], b);
181 }
182 
183 /*
184  * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
185  * returned to userspace
186  */
__fill_v4l2_buffer(struct vb2_buffer * vb,void * pb)187 static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
188 {
189 	struct v4l2_buffer *b = pb;
190 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
191 	struct vb2_queue *q = vb->vb2_queue;
192 	unsigned int plane;
193 
194 	/* Copy back data such as timestamp, flags, etc. */
195 	b->index = vb->index;
196 	b->type = vb->type;
197 	b->memory = vb->memory;
198 	b->bytesused = 0;
199 
200 	b->flags = vbuf->flags;
201 	b->field = vbuf->field;
202 	b->timestamp = ns_to_timeval(vb->timestamp);
203 	b->timecode = vbuf->timecode;
204 	b->sequence = vbuf->sequence;
205 	b->reserved2 = 0;
206 	b->reserved = 0;
207 
208 	if (q->is_multiplanar) {
209 		/*
210 		 * Fill in plane-related data if userspace provided an array
211 		 * for it. The caller has already verified memory and size.
212 		 */
213 		b->length = vb->num_planes;
214 		for (plane = 0; plane < vb->num_planes; ++plane) {
215 			struct v4l2_plane *pdst = &b->m.planes[plane];
216 			struct vb2_plane *psrc = &vb->planes[plane];
217 
218 			pdst->bytesused = psrc->bytesused;
219 			pdst->length = psrc->length;
220 			if (q->memory == VB2_MEMORY_MMAP)
221 				pdst->m.mem_offset = psrc->m.offset;
222 			else if (q->memory == VB2_MEMORY_USERPTR)
223 				pdst->m.userptr = psrc->m.userptr;
224 			else if (q->memory == VB2_MEMORY_DMABUF)
225 				pdst->m.fd = psrc->m.fd;
226 			pdst->data_offset = psrc->data_offset;
227 			memset(pdst->reserved, 0, sizeof(pdst->reserved));
228 		}
229 	} else {
230 		/*
231 		 * We use length and offset in v4l2_planes array even for
232 		 * single-planar buffers, but userspace does not.
233 		 */
234 		b->length = vb->planes[0].length;
235 		b->bytesused = vb->planes[0].bytesused;
236 		if (q->memory == VB2_MEMORY_MMAP)
237 			b->m.offset = vb->planes[0].m.offset;
238 		else if (q->memory == VB2_MEMORY_USERPTR)
239 			b->m.userptr = vb->planes[0].m.userptr;
240 		else if (q->memory == VB2_MEMORY_DMABUF)
241 			b->m.fd = vb->planes[0].m.fd;
242 	}
243 
244 	/*
245 	 * Clear any buffer state related flags.
246 	 */
247 	b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
248 	b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
249 	if (!q->copy_timestamp) {
250 		/*
251 		 * For non-COPY timestamps, drop timestamp source bits
252 		 * and obtain the timestamp source from the queue.
253 		 */
254 		b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
255 		b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
256 	}
257 
258 	switch (vb->state) {
259 	case VB2_BUF_STATE_QUEUED:
260 	case VB2_BUF_STATE_ACTIVE:
261 		b->flags |= V4L2_BUF_FLAG_QUEUED;
262 		break;
263 	case VB2_BUF_STATE_ERROR:
264 		b->flags |= V4L2_BUF_FLAG_ERROR;
265 		/* fall through */
266 	case VB2_BUF_STATE_DONE:
267 		b->flags |= V4L2_BUF_FLAG_DONE;
268 		break;
269 	case VB2_BUF_STATE_PREPARED:
270 		b->flags |= V4L2_BUF_FLAG_PREPARED;
271 		break;
272 	case VB2_BUF_STATE_PREPARING:
273 	case VB2_BUF_STATE_DEQUEUED:
274 	case VB2_BUF_STATE_REQUEUEING:
275 		/* nothing */
276 		break;
277 	}
278 
279 	if (vb2_buffer_in_use(q, vb))
280 		b->flags |= V4L2_BUF_FLAG_MAPPED;
281 
282 	if (!q->is_output &&
283 		b->flags & V4L2_BUF_FLAG_DONE &&
284 		b->flags & V4L2_BUF_FLAG_LAST)
285 		q->last_buffer_dequeued = true;
286 }
287 
288 /*
289  * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
290  * v4l2_buffer by the userspace. It also verifies that struct
291  * v4l2_buffer has a valid number of planes.
292  */
__fill_vb2_buffer(struct vb2_buffer * vb,const void * pb,struct vb2_plane * planes)293 static int __fill_vb2_buffer(struct vb2_buffer *vb,
294 		const void *pb, struct vb2_plane *planes)
295 {
296 	struct vb2_queue *q = vb->vb2_queue;
297 	const struct v4l2_buffer *b = pb;
298 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
299 	unsigned int plane;
300 	int ret;
301 
302 	ret = __verify_length(vb, b);
303 	if (ret < 0) {
304 		dprintk(1, "plane parameters verification failed: %d\n", ret);
305 		return ret;
306 	}
307 	if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
308 		/*
309 		 * If the format's field is ALTERNATE, then the buffer's field
310 		 * should be either TOP or BOTTOM, not ALTERNATE since that
311 		 * makes no sense. The driver has to know whether the
312 		 * buffer represents a top or a bottom field in order to
313 		 * program any DMA correctly. Using ALTERNATE is wrong, since
314 		 * that just says that it is either a top or a bottom field,
315 		 * but not which of the two it is.
316 		 */
317 		dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
318 		return -EINVAL;
319 	}
320 	vb->timestamp = 0;
321 	vbuf->sequence = 0;
322 
323 	if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
324 		if (b->memory == VB2_MEMORY_USERPTR) {
325 			for (plane = 0; plane < vb->num_planes; ++plane) {
326 				planes[plane].m.userptr =
327 					b->m.planes[plane].m.userptr;
328 				planes[plane].length =
329 					b->m.planes[plane].length;
330 			}
331 		}
332 		if (b->memory == VB2_MEMORY_DMABUF) {
333 			for (plane = 0; plane < vb->num_planes; ++plane) {
334 				planes[plane].m.fd =
335 					b->m.planes[plane].m.fd;
336 				planes[plane].length =
337 					b->m.planes[plane].length;
338 			}
339 		}
340 
341 		/* Fill in driver-provided information for OUTPUT types */
342 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
343 			/*
344 			 * Will have to go up to b->length when API starts
345 			 * accepting variable number of planes.
346 			 *
347 			 * If bytesused == 0 for the output buffer, then fall
348 			 * back to the full buffer size. In that case
349 			 * userspace clearly never bothered to set it and
350 			 * it's a safe assumption that they really meant to
351 			 * use the full plane sizes.
352 			 *
353 			 * Some drivers, e.g. old codec drivers, use bytesused == 0
354 			 * as a way to indicate that streaming is finished.
355 			 * In that case, the driver should use the
356 			 * allow_zero_bytesused flag to keep old userspace
357 			 * applications working.
358 			 */
359 			for (plane = 0; plane < vb->num_planes; ++plane) {
360 				struct vb2_plane *pdst = &planes[plane];
361 				struct v4l2_plane *psrc = &b->m.planes[plane];
362 
363 				if (psrc->bytesused == 0)
364 					vb2_warn_zero_bytesused(vb);
365 
366 				if (vb->vb2_queue->allow_zero_bytesused)
367 					pdst->bytesused = psrc->bytesused;
368 				else
369 					pdst->bytesused = psrc->bytesused ?
370 						psrc->bytesused : pdst->length;
371 				pdst->data_offset = psrc->data_offset;
372 			}
373 		}
374 	} else {
375 		/*
376 		 * Single-planar buffers do not use planes array,
377 		 * so fill in relevant v4l2_buffer struct fields instead.
378 		 * In videobuf we use our internal V4l2_planes struct for
379 		 * single-planar buffers as well, for simplicity.
380 		 *
381 		 * If bytesused == 0 for the output buffer, then fall back
382 		 * to the full buffer size as that's a sensible default.
383 		 *
384 		 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
385 		 * a way to indicate that streaming is finished. In that case,
386 		 * the driver should use the allow_zero_bytesused flag to keep
387 		 * old userspace applications working.
388 		 */
389 		if (b->memory == VB2_MEMORY_USERPTR) {
390 			planes[0].m.userptr = b->m.userptr;
391 			planes[0].length = b->length;
392 		}
393 
394 		if (b->memory == VB2_MEMORY_DMABUF) {
395 			planes[0].m.fd = b->m.fd;
396 			planes[0].length = b->length;
397 		}
398 
399 		if (V4L2_TYPE_IS_OUTPUT(b->type)) {
400 			if (b->bytesused == 0)
401 				vb2_warn_zero_bytesused(vb);
402 
403 			if (vb->vb2_queue->allow_zero_bytesused)
404 				planes[0].bytesused = b->bytesused;
405 			else
406 				planes[0].bytesused = b->bytesused ?
407 					b->bytesused : planes[0].length;
408 		} else
409 			planes[0].bytesused = 0;
410 
411 	}
412 
413 	/* Zero flags that the vb2 core handles */
414 	vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
415 	if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
416 		/*
417 		 * Non-COPY timestamps and non-OUTPUT queues will get
418 		 * their timestamp and timestamp source flags from the
419 		 * queue.
420 		 */
421 		vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
422 	}
423 
424 	if (V4L2_TYPE_IS_OUTPUT(b->type)) {
425 		/*
426 		 * For output buffers mask out the timecode flag:
427 		 * this will be handled later in vb2_qbuf().
428 		 * The 'field' is valid metadata for this output buffer
429 		 * and so that needs to be copied here.
430 		 */
431 		vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
432 		vbuf->field = b->field;
433 	} else {
434 		/* Zero any output buffer flags as this is a capture buffer */
435 		vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
436 		/* Zero last flag, this is a signal from driver to userspace */
437 		vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
438 	}
439 
440 	return 0;
441 }
442 
443 static const struct vb2_buf_ops v4l2_buf_ops = {
444 	.verify_planes_array	= __verify_planes_array_core,
445 	.fill_user_buffer	= __fill_v4l2_buffer,
446 	.fill_vb2_buffer	= __fill_vb2_buffer,
447 	.copy_timestamp		= __copy_timestamp,
448 };
449 
450 /*
451  * vb2_querybuf() - query video buffer information
452  * @q:		videobuf queue
453  * @b:		buffer struct passed from userspace to vidioc_querybuf handler
454  *		in driver
455  *
456  * Should be called from vidioc_querybuf ioctl handler in driver.
457  * This function will verify the passed v4l2_buffer structure and fill the
458  * relevant information for the userspace.
459  *
460  * The return values from this function are intended to be directly returned
461  * from vidioc_querybuf handler in driver.
462  */
vb2_querybuf(struct vb2_queue * q,struct v4l2_buffer * b)463 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
464 {
465 	struct vb2_buffer *vb;
466 	int ret;
467 
468 	if (b->type != q->type) {
469 		dprintk(1, "wrong buffer type\n");
470 		return -EINVAL;
471 	}
472 
473 	if (b->index >= q->num_buffers) {
474 		dprintk(1, "buffer index out of range\n");
475 		return -EINVAL;
476 	}
477 	vb = q->bufs[b->index];
478 	ret = __verify_planes_array(vb, b);
479 	if (!ret)
480 		vb2_core_querybuf(q, b->index, b);
481 	return ret;
482 }
483 EXPORT_SYMBOL(vb2_querybuf);
484 
vb2_reqbufs(struct vb2_queue * q,struct v4l2_requestbuffers * req)485 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
486 {
487 	int ret = vb2_verify_memory_type(q, req->memory, req->type);
488 
489 	return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
490 }
491 EXPORT_SYMBOL_GPL(vb2_reqbufs);
492 
vb2_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b)493 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
494 {
495 	int ret;
496 
497 	if (vb2_fileio_is_active(q)) {
498 		dprintk(1, "file io in progress\n");
499 		return -EBUSY;
500 	}
501 
502 	ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
503 
504 	return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
505 }
506 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
507 
vb2_create_bufs(struct vb2_queue * q,struct v4l2_create_buffers * create)508 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
509 {
510 	unsigned requested_planes = 1;
511 	unsigned requested_sizes[VIDEO_MAX_PLANES];
512 	struct v4l2_format *f = &create->format;
513 	int ret = vb2_verify_memory_type(q, create->memory, f->type);
514 	unsigned i;
515 
516 	create->index = q->num_buffers;
517 	if (create->count == 0)
518 		return ret != -EBUSY ? ret : 0;
519 
520 	switch (f->type) {
521 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
522 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
523 		requested_planes = f->fmt.pix_mp.num_planes;
524 		if (requested_planes == 0 ||
525 		    requested_planes > VIDEO_MAX_PLANES)
526 			return -EINVAL;
527 		for (i = 0; i < requested_planes; i++)
528 			requested_sizes[i] =
529 				f->fmt.pix_mp.plane_fmt[i].sizeimage;
530 		break;
531 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
532 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
533 		requested_sizes[0] = f->fmt.pix.sizeimage;
534 		break;
535 	case V4L2_BUF_TYPE_VBI_CAPTURE:
536 	case V4L2_BUF_TYPE_VBI_OUTPUT:
537 		requested_sizes[0] = f->fmt.vbi.samples_per_line *
538 			(f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
539 		break;
540 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
541 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
542 		requested_sizes[0] = f->fmt.sliced.io_size;
543 		break;
544 	case V4L2_BUF_TYPE_SDR_CAPTURE:
545 	case V4L2_BUF_TYPE_SDR_OUTPUT:
546 		requested_sizes[0] = f->fmt.sdr.buffersize;
547 		break;
548 	case V4L2_BUF_TYPE_META_CAPTURE:
549 		requested_sizes[0] = f->fmt.meta.buffersize;
550 		break;
551 	default:
552 		return -EINVAL;
553 	}
554 	for (i = 0; i < requested_planes; i++)
555 		if (requested_sizes[i] == 0)
556 			return -EINVAL;
557 	return ret ? ret : vb2_core_create_bufs(q, create->memory,
558 		&create->count, requested_planes, requested_sizes);
559 }
560 EXPORT_SYMBOL_GPL(vb2_create_bufs);
561 
vb2_qbuf(struct vb2_queue * q,struct v4l2_buffer * b)562 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
563 {
564 	int ret;
565 
566 	if (vb2_fileio_is_active(q)) {
567 		dprintk(1, "file io in progress\n");
568 		return -EBUSY;
569 	}
570 
571 	ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
572 	return ret ? ret : vb2_core_qbuf(q, b->index, b);
573 }
574 EXPORT_SYMBOL_GPL(vb2_qbuf);
575 
vb2_dqbuf(struct vb2_queue * q,struct v4l2_buffer * b,bool nonblocking)576 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
577 {
578 	int ret;
579 
580 	if (vb2_fileio_is_active(q)) {
581 		dprintk(1, "file io in progress\n");
582 		return -EBUSY;
583 	}
584 
585 	if (b->type != q->type) {
586 		dprintk(1, "invalid buffer type\n");
587 		return -EINVAL;
588 	}
589 
590 	ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
591 
592 	/*
593 	 *  After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
594 	 *  cleared.
595 	 */
596 	b->flags &= ~V4L2_BUF_FLAG_DONE;
597 
598 	return ret;
599 }
600 EXPORT_SYMBOL_GPL(vb2_dqbuf);
601 
vb2_streamon(struct vb2_queue * q,enum v4l2_buf_type type)602 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
603 {
604 	if (vb2_fileio_is_active(q)) {
605 		dprintk(1, "file io in progress\n");
606 		return -EBUSY;
607 	}
608 	return vb2_core_streamon(q, type);
609 }
610 EXPORT_SYMBOL_GPL(vb2_streamon);
611 
vb2_streamoff(struct vb2_queue * q,enum v4l2_buf_type type)612 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
613 {
614 	if (vb2_fileio_is_active(q)) {
615 		dprintk(1, "file io in progress\n");
616 		return -EBUSY;
617 	}
618 	return vb2_core_streamoff(q, type);
619 }
620 EXPORT_SYMBOL_GPL(vb2_streamoff);
621 
vb2_expbuf(struct vb2_queue * q,struct v4l2_exportbuffer * eb)622 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
623 {
624 	return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
625 				eb->plane, eb->flags);
626 }
627 EXPORT_SYMBOL_GPL(vb2_expbuf);
628 
vb2_queue_init(struct vb2_queue * q)629 int vb2_queue_init(struct vb2_queue *q)
630 {
631 	/*
632 	 * Sanity check
633 	 */
634 	if (WARN_ON(!q)			  ||
635 	    WARN_ON(q->timestamp_flags &
636 		    ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
637 		      V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
638 		return -EINVAL;
639 
640 	/* Warn that the driver should choose an appropriate timestamp type */
641 	WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
642 		V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
643 
644 	/* Warn that vb2_memory should match with v4l2_memory */
645 	if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
646 		|| WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
647 		|| WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
648 		return -EINVAL;
649 
650 	if (q->buf_struct_size == 0)
651 		q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
652 
653 	q->buf_ops = &v4l2_buf_ops;
654 	q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
655 	q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
656 	q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
657 			== V4L2_BUF_FLAG_TIMESTAMP_COPY;
658 	/*
659 	 * For compatibility with vb1: if QBUF hasn't been called yet, then
660 	 * return EPOLLERR as well. This only affects capture queues, output
661 	 * queues will always initialize waiting_for_buffers to false.
662 	 */
663 	q->quirk_poll_must_check_waiting_for_buffers = true;
664 
665 	return vb2_core_queue_init(q);
666 }
667 EXPORT_SYMBOL_GPL(vb2_queue_init);
668 
vb2_queue_release(struct vb2_queue * q)669 void vb2_queue_release(struct vb2_queue *q)
670 {
671 	vb2_core_queue_release(q);
672 }
673 EXPORT_SYMBOL_GPL(vb2_queue_release);
674 
vb2_poll(struct vb2_queue * q,struct file * file,poll_table * wait)675 __poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
676 {
677 	struct video_device *vfd = video_devdata(file);
678 	__poll_t req_events = poll_requested_events(wait);
679 	__poll_t res = 0;
680 
681 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
682 		struct v4l2_fh *fh = file->private_data;
683 
684 		if (v4l2_event_pending(fh))
685 			res = EPOLLPRI;
686 		else if (req_events & EPOLLPRI)
687 			poll_wait(file, &fh->wait, wait);
688 	}
689 
690 	return res | vb2_core_poll(q, file, wait);
691 }
692 EXPORT_SYMBOL_GPL(vb2_poll);
693 
694 /*
695  * The following functions are not part of the vb2 core API, but are helper
696  * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
697  * and struct vb2_ops.
698  * They contain boilerplate code that most if not all drivers have to do
699  * and so they simplify the driver code.
700  */
701 
702 /* 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)703 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
704 {
705 	return vdev->queue->owner && vdev->queue->owner != file->private_data;
706 }
707 
708 /* vb2 ioctl helpers */
709 
vb2_ioctl_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * p)710 int vb2_ioctl_reqbufs(struct file *file, void *priv,
711 			  struct v4l2_requestbuffers *p)
712 {
713 	struct video_device *vdev = video_devdata(file);
714 	int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
715 
716 	if (res)
717 		return res;
718 	if (vb2_queue_is_busy(vdev, file))
719 		return -EBUSY;
720 	res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
721 	/* If count == 0, then the owner has released all buffers and he
722 	   is no longer owner of the queue. Otherwise we have a new owner. */
723 	if (res == 0)
724 		vdev->queue->owner = p->count ? file->private_data : NULL;
725 	return res;
726 }
727 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
728 
vb2_ioctl_create_bufs(struct file * file,void * priv,struct v4l2_create_buffers * p)729 int vb2_ioctl_create_bufs(struct file *file, void *priv,
730 			  struct v4l2_create_buffers *p)
731 {
732 	struct video_device *vdev = video_devdata(file);
733 	int res = vb2_verify_memory_type(vdev->queue, p->memory,
734 			p->format.type);
735 
736 	p->index = vdev->queue->num_buffers;
737 	/*
738 	 * If count == 0, then just check if memory and type are valid.
739 	 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
740 	 */
741 	if (p->count == 0)
742 		return res != -EBUSY ? res : 0;
743 	if (res)
744 		return res;
745 	if (vb2_queue_is_busy(vdev, file))
746 		return -EBUSY;
747 
748 	res = vb2_create_bufs(vdev->queue, p);
749 	if (res == 0)
750 		vdev->queue->owner = file->private_data;
751 	return res;
752 }
753 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
754 
vb2_ioctl_prepare_buf(struct file * file,void * priv,struct v4l2_buffer * p)755 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
756 			  struct v4l2_buffer *p)
757 {
758 	struct video_device *vdev = video_devdata(file);
759 
760 	if (vb2_queue_is_busy(vdev, file))
761 		return -EBUSY;
762 	return vb2_prepare_buf(vdev->queue, p);
763 }
764 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
765 
vb2_ioctl_querybuf(struct file * file,void * priv,struct v4l2_buffer * p)766 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
767 {
768 	struct video_device *vdev = video_devdata(file);
769 
770 	/* No need to call vb2_queue_is_busy(), anyone can query buffers. */
771 	return vb2_querybuf(vdev->queue, p);
772 }
773 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
774 
vb2_ioctl_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)775 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
776 {
777 	struct video_device *vdev = video_devdata(file);
778 
779 	if (vb2_queue_is_busy(vdev, file))
780 		return -EBUSY;
781 	return vb2_qbuf(vdev->queue, p);
782 }
783 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
784 
vb2_ioctl_dqbuf(struct file * file,void * priv,struct v4l2_buffer * p)785 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
786 {
787 	struct video_device *vdev = video_devdata(file);
788 
789 	if (vb2_queue_is_busy(vdev, file))
790 		return -EBUSY;
791 	return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
792 }
793 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
794 
vb2_ioctl_streamon(struct file * file,void * priv,enum v4l2_buf_type i)795 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
796 {
797 	struct video_device *vdev = video_devdata(file);
798 
799 	if (vb2_queue_is_busy(vdev, file))
800 		return -EBUSY;
801 	return vb2_streamon(vdev->queue, i);
802 }
803 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
804 
vb2_ioctl_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)805 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
806 {
807 	struct video_device *vdev = video_devdata(file);
808 
809 	if (vb2_queue_is_busy(vdev, file))
810 		return -EBUSY;
811 	return vb2_streamoff(vdev->queue, i);
812 }
813 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
814 
vb2_ioctl_expbuf(struct file * file,void * priv,struct v4l2_exportbuffer * p)815 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
816 {
817 	struct video_device *vdev = video_devdata(file);
818 
819 	if (vb2_queue_is_busy(vdev, file))
820 		return -EBUSY;
821 	return vb2_expbuf(vdev->queue, p);
822 }
823 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
824 
825 /* v4l2_file_operations helpers */
826 
vb2_fop_mmap(struct file * file,struct vm_area_struct * vma)827 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
828 {
829 	struct video_device *vdev = video_devdata(file);
830 
831 	return vb2_mmap(vdev->queue, vma);
832 }
833 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
834 
_vb2_fop_release(struct file * file,struct mutex * lock)835 int _vb2_fop_release(struct file *file, struct mutex *lock)
836 {
837 	struct video_device *vdev = video_devdata(file);
838 
839 	if (lock)
840 		mutex_lock(lock);
841 	if (file->private_data == vdev->queue->owner) {
842 		vb2_queue_release(vdev->queue);
843 		vdev->queue->owner = NULL;
844 	}
845 	if (lock)
846 		mutex_unlock(lock);
847 	return v4l2_fh_release(file);
848 }
849 EXPORT_SYMBOL_GPL(_vb2_fop_release);
850 
vb2_fop_release(struct file * file)851 int vb2_fop_release(struct file *file)
852 {
853 	struct video_device *vdev = video_devdata(file);
854 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
855 
856 	return _vb2_fop_release(file, lock);
857 }
858 EXPORT_SYMBOL_GPL(vb2_fop_release);
859 
vb2_fop_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)860 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
861 		size_t count, loff_t *ppos)
862 {
863 	struct video_device *vdev = video_devdata(file);
864 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
865 	int err = -EBUSY;
866 
867 	if (!(vdev->queue->io_modes & VB2_WRITE))
868 		return -EINVAL;
869 	if (lock && mutex_lock_interruptible(lock))
870 		return -ERESTARTSYS;
871 	if (vb2_queue_is_busy(vdev, file))
872 		goto exit;
873 	err = vb2_write(vdev->queue, buf, count, ppos,
874 		       file->f_flags & O_NONBLOCK);
875 	if (vdev->queue->fileio)
876 		vdev->queue->owner = file->private_data;
877 exit:
878 	if (lock)
879 		mutex_unlock(lock);
880 	return err;
881 }
882 EXPORT_SYMBOL_GPL(vb2_fop_write);
883 
vb2_fop_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)884 ssize_t vb2_fop_read(struct file *file, char __user *buf,
885 		size_t count, loff_t *ppos)
886 {
887 	struct video_device *vdev = video_devdata(file);
888 	struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
889 	int err = -EBUSY;
890 
891 	if (!(vdev->queue->io_modes & VB2_READ))
892 		return -EINVAL;
893 	if (lock && mutex_lock_interruptible(lock))
894 		return -ERESTARTSYS;
895 	if (vb2_queue_is_busy(vdev, file))
896 		goto exit;
897 	err = vb2_read(vdev->queue, buf, count, ppos,
898 		       file->f_flags & O_NONBLOCK);
899 	if (vdev->queue->fileio)
900 		vdev->queue->owner = file->private_data;
901 exit:
902 	if (lock)
903 		mutex_unlock(lock);
904 	return err;
905 }
906 EXPORT_SYMBOL_GPL(vb2_fop_read);
907 
vb2_fop_poll(struct file * file,poll_table * wait)908 __poll_t vb2_fop_poll(struct file *file, poll_table *wait)
909 {
910 	struct video_device *vdev = video_devdata(file);
911 	struct vb2_queue *q = vdev->queue;
912 	struct mutex *lock = q->lock ? q->lock : vdev->lock;
913 	__poll_t res;
914 	void *fileio;
915 
916 	/*
917 	 * If this helper doesn't know how to lock, then you shouldn't be using
918 	 * it but you should write your own.
919 	 */
920 	WARN_ON(!lock);
921 
922 	if (lock && mutex_lock_interruptible(lock))
923 		return EPOLLERR;
924 
925 	fileio = q->fileio;
926 
927 	res = vb2_poll(vdev->queue, file, wait);
928 
929 	/* If fileio was started, then we have a new queue owner. */
930 	if (!fileio && q->fileio)
931 		q->owner = file->private_data;
932 	if (lock)
933 		mutex_unlock(lock);
934 	return res;
935 }
936 EXPORT_SYMBOL_GPL(vb2_fop_poll);
937 
938 #ifndef CONFIG_MMU
vb2_fop_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)939 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
940 		unsigned long len, unsigned long pgoff, unsigned long flags)
941 {
942 	struct video_device *vdev = video_devdata(file);
943 
944 	return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
945 }
946 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
947 #endif
948 
949 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
950 
vb2_ops_wait_prepare(struct vb2_queue * vq)951 void vb2_ops_wait_prepare(struct vb2_queue *vq)
952 {
953 	mutex_unlock(vq->lock);
954 }
955 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
956 
vb2_ops_wait_finish(struct vb2_queue * vq)957 void vb2_ops_wait_finish(struct vb2_queue *vq)
958 {
959 	mutex_lock(vq->lock);
960 }
961 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
962 
963 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
964 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
965 MODULE_LICENSE("GPL");
966