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 #include "videobuf2-internal.h"
35
36 /* Flags that are set by the vb2 core */
37 #define V4L2_BUFFER_MASK_FLAGS (V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | \
38 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR | \
39 V4L2_BUF_FLAG_PREPARED | \
40 V4L2_BUF_FLAG_TIMESTAMP_MASK)
41 /* Output buffer flags that should be passed on to the driver */
42 #define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME | \
43 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_TIMECODE)
44
45 /**
46 * __verify_planes_array() - verify that the planes array passed in struct
47 * v4l2_buffer from userspace can be safely used
48 */
__verify_planes_array(struct vb2_buffer * vb,const struct v4l2_buffer * b)49 static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
50 {
51 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
52 return 0;
53
54 /* Is memory for copying plane information present? */
55 if (NULL == b->m.planes) {
56 dprintk(1, "multi-planar buffer passed but "
57 "planes array not provided\n");
58 return -EINVAL;
59 }
60
61 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
62 dprintk(1, "incorrect planes array length, "
63 "expected %d, got %d\n", vb->num_planes, b->length);
64 return -EINVAL;
65 }
66
67 return 0;
68 }
69
__verify_planes_array_core(struct vb2_buffer * vb,const void * pb)70 static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
71 {
72 return __verify_planes_array(vb, pb);
73 }
74
75 /**
76 * __verify_length() - Verify that the bytesused value for each plane fits in
77 * the plane length and that the data offset doesn't exceed the bytesused value.
78 */
__verify_length(struct vb2_buffer * vb,const struct v4l2_buffer * b)79 static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
80 {
81 unsigned int length;
82 unsigned int bytesused;
83 unsigned int plane;
84
85 if (!V4L2_TYPE_IS_OUTPUT(b->type))
86 return 0;
87
88 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
89 for (plane = 0; plane < vb->num_planes; ++plane) {
90 length = (b->memory == VB2_MEMORY_USERPTR ||
91 b->memory == VB2_MEMORY_DMABUF)
92 ? b->m.planes[plane].length
93 : vb->planes[plane].length;
94 bytesused = b->m.planes[plane].bytesused
95 ? b->m.planes[plane].bytesused : length;
96
97 if (b->m.planes[plane].bytesused > length)
98 return -EINVAL;
99
100 if (b->m.planes[plane].data_offset > 0 &&
101 b->m.planes[plane].data_offset >= bytesused)
102 return -EINVAL;
103 }
104 } else {
105 length = (b->memory == VB2_MEMORY_USERPTR)
106 ? b->length : vb->planes[0].length;
107
108 if (b->bytesused > length)
109 return -EINVAL;
110 }
111
112 return 0;
113 }
114
__set_timestamp(struct vb2_buffer * vb,const void * pb)115 static int __set_timestamp(struct vb2_buffer *vb, const void *pb)
116 {
117 const struct v4l2_buffer *b = pb;
118 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
119 struct vb2_queue *q = vb->vb2_queue;
120
121 if (q->is_output) {
122 /*
123 * For output buffers copy the timestamp if needed,
124 * and the timecode field and flag if needed.
125 */
126 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
127 V4L2_BUF_FLAG_TIMESTAMP_COPY)
128 vbuf->timestamp = b->timestamp;
129 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
130 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
131 vbuf->timecode = b->timecode;
132 }
133 return 0;
134 };
135
vb2_warn_zero_bytesused(struct vb2_buffer * vb)136 static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
137 {
138 static bool check_once;
139
140 if (check_once)
141 return;
142
143 check_once = true;
144
145 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
146 if (vb->vb2_queue->allow_zero_bytesused)
147 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
148 else
149 pr_warn("use the actual size instead.\n");
150 }
151
vb2_queue_or_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b,const char * opname)152 static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b,
153 const char *opname)
154 {
155 if (b->type != q->type) {
156 dprintk(1, "%s: invalid buffer type\n", opname);
157 return -EINVAL;
158 }
159
160 if (b->index >= q->num_buffers) {
161 dprintk(1, "%s: buffer index out of range\n", opname);
162 return -EINVAL;
163 }
164
165 if (q->bufs[b->index] == NULL) {
166 /* Should never happen */
167 dprintk(1, "%s: buffer is NULL\n", opname);
168 return -EINVAL;
169 }
170
171 if (b->memory != q->memory) {
172 dprintk(1, "%s: invalid memory type\n", opname);
173 return -EINVAL;
174 }
175
176 return __verify_planes_array(q->bufs[b->index], b);
177 }
178
179 /**
180 * __fill_v4l2_buffer() - fill in a struct v4l2_buffer with information to be
181 * returned to userspace
182 */
__fill_v4l2_buffer(struct vb2_buffer * vb,void * pb)183 static int __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
184 {
185 struct v4l2_buffer *b = pb;
186 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
187 struct vb2_queue *q = vb->vb2_queue;
188 unsigned int plane;
189
190 /* Copy back data such as timestamp, flags, etc. */
191 b->index = vb->index;
192 b->type = vb->type;
193 b->memory = vb->memory;
194 b->bytesused = 0;
195
196 b->flags = vbuf->flags;
197 b->field = vbuf->field;
198 b->timestamp = vbuf->timestamp;
199 b->timecode = vbuf->timecode;
200 b->sequence = vbuf->sequence;
201 b->reserved2 = 0;
202 b->reserved = 0;
203
204 if (q->is_multiplanar) {
205 /*
206 * Fill in plane-related data if userspace provided an array
207 * for it. The caller has already verified memory and size.
208 */
209 b->length = vb->num_planes;
210 for (plane = 0; plane < vb->num_planes; ++plane) {
211 struct v4l2_plane *pdst = &b->m.planes[plane];
212 struct vb2_plane *psrc = &vb->planes[plane];
213
214 pdst->bytesused = psrc->bytesused;
215 pdst->length = psrc->length;
216 if (q->memory == VB2_MEMORY_MMAP)
217 pdst->m.mem_offset = psrc->m.offset;
218 else if (q->memory == VB2_MEMORY_USERPTR)
219 pdst->m.userptr = psrc->m.userptr;
220 else if (q->memory == VB2_MEMORY_DMABUF)
221 pdst->m.fd = psrc->m.fd;
222 pdst->data_offset = psrc->data_offset;
223 memset(pdst->reserved, 0, sizeof(pdst->reserved));
224 }
225 } else {
226 /*
227 * We use length and offset in v4l2_planes array even for
228 * single-planar buffers, but userspace does not.
229 */
230 b->length = vb->planes[0].length;
231 b->bytesused = vb->planes[0].bytesused;
232 if (q->memory == VB2_MEMORY_MMAP)
233 b->m.offset = vb->planes[0].m.offset;
234 else if (q->memory == VB2_MEMORY_USERPTR)
235 b->m.userptr = vb->planes[0].m.userptr;
236 else if (q->memory == VB2_MEMORY_DMABUF)
237 b->m.fd = vb->planes[0].m.fd;
238 }
239
240 /*
241 * Clear any buffer state related flags.
242 */
243 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
244 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
245 if ((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
246 V4L2_BUF_FLAG_TIMESTAMP_COPY) {
247 /*
248 * For non-COPY timestamps, drop timestamp source bits
249 * and obtain the timestamp source from the queue.
250 */
251 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
252 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
253 }
254
255 switch (vb->state) {
256 case VB2_BUF_STATE_QUEUED:
257 case VB2_BUF_STATE_ACTIVE:
258 b->flags |= V4L2_BUF_FLAG_QUEUED;
259 break;
260 case VB2_BUF_STATE_ERROR:
261 b->flags |= V4L2_BUF_FLAG_ERROR;
262 /* fall through */
263 case VB2_BUF_STATE_DONE:
264 b->flags |= V4L2_BUF_FLAG_DONE;
265 break;
266 case VB2_BUF_STATE_PREPARED:
267 b->flags |= V4L2_BUF_FLAG_PREPARED;
268 break;
269 case VB2_BUF_STATE_PREPARING:
270 case VB2_BUF_STATE_DEQUEUED:
271 case VB2_BUF_STATE_REQUEUEING:
272 /* nothing */
273 break;
274 }
275
276 if (vb2_buffer_in_use(q, vb))
277 b->flags |= V4L2_BUF_FLAG_MAPPED;
278
279 return 0;
280 }
281
282 /**
283 * __fill_vb2_buffer() - fill a vb2_buffer with information provided in a
284 * v4l2_buffer by the userspace. It also verifies that struct
285 * v4l2_buffer has a valid number of planes.
286 */
__fill_vb2_buffer(struct vb2_buffer * vb,const void * pb,struct vb2_plane * planes)287 static int __fill_vb2_buffer(struct vb2_buffer *vb,
288 const void *pb, struct vb2_plane *planes)
289 {
290 struct vb2_queue *q = vb->vb2_queue;
291 const struct v4l2_buffer *b = pb;
292 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
293 unsigned int plane;
294 int ret;
295
296 ret = __verify_length(vb, b);
297 if (ret < 0) {
298 dprintk(1, "plane parameters verification failed: %d\n", ret);
299 return ret;
300 }
301 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
302 /*
303 * If the format's field is ALTERNATE, then the buffer's field
304 * should be either TOP or BOTTOM, not ALTERNATE since that
305 * makes no sense. The driver has to know whether the
306 * buffer represents a top or a bottom field in order to
307 * program any DMA correctly. Using ALTERNATE is wrong, since
308 * that just says that it is either a top or a bottom field,
309 * but not which of the two it is.
310 */
311 dprintk(1, "the field is incorrectly set to ALTERNATE "
312 "for an output buffer\n");
313 return -EINVAL;
314 }
315 vbuf->timestamp.tv_sec = 0;
316 vbuf->timestamp.tv_usec = 0;
317 vbuf->sequence = 0;
318
319 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
320 if (b->memory == VB2_MEMORY_USERPTR) {
321 for (plane = 0; plane < vb->num_planes; ++plane) {
322 planes[plane].m.userptr =
323 b->m.planes[plane].m.userptr;
324 planes[plane].length =
325 b->m.planes[plane].length;
326 }
327 }
328 if (b->memory == VB2_MEMORY_DMABUF) {
329 for (plane = 0; plane < vb->num_planes; ++plane) {
330 planes[plane].m.fd =
331 b->m.planes[plane].m.fd;
332 planes[plane].length =
333 b->m.planes[plane].length;
334 }
335 }
336
337 /* Fill in driver-provided information for OUTPUT types */
338 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
339 /*
340 * Will have to go up to b->length when API starts
341 * accepting variable number of planes.
342 *
343 * If bytesused == 0 for the output buffer, then fall
344 * back to the full buffer size. In that case
345 * userspace clearly never bothered to set it and
346 * it's a safe assumption that they really meant to
347 * use the full plane sizes.
348 *
349 * Some drivers, e.g. old codec drivers, use bytesused == 0
350 * as a way to indicate that streaming is finished.
351 * In that case, the driver should use the
352 * allow_zero_bytesused flag to keep old userspace
353 * applications working.
354 */
355 for (plane = 0; plane < vb->num_planes; ++plane) {
356 struct vb2_plane *pdst = &planes[plane];
357 struct v4l2_plane *psrc = &b->m.planes[plane];
358
359 if (psrc->bytesused == 0)
360 vb2_warn_zero_bytesused(vb);
361
362 if (vb->vb2_queue->allow_zero_bytesused)
363 pdst->bytesused = psrc->bytesused;
364 else
365 pdst->bytesused = psrc->bytesused ?
366 psrc->bytesused : pdst->length;
367 pdst->data_offset = psrc->data_offset;
368 }
369 }
370 } else {
371 /*
372 * Single-planar buffers do not use planes array,
373 * so fill in relevant v4l2_buffer struct fields instead.
374 * In videobuf we use our internal V4l2_planes struct for
375 * single-planar buffers as well, for simplicity.
376 *
377 * If bytesused == 0 for the output buffer, then fall back
378 * to the full buffer size as that's a sensible default.
379 *
380 * Some drivers, e.g. old codec drivers, use bytesused == 0 as
381 * a way to indicate that streaming is finished. In that case,
382 * the driver should use the allow_zero_bytesused flag to keep
383 * old userspace applications working.
384 */
385 if (b->memory == VB2_MEMORY_USERPTR) {
386 planes[0].m.userptr = b->m.userptr;
387 planes[0].length = b->length;
388 }
389
390 if (b->memory == VB2_MEMORY_DMABUF) {
391 planes[0].m.fd = b->m.fd;
392 planes[0].length = b->length;
393 }
394
395 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
396 if (b->bytesused == 0)
397 vb2_warn_zero_bytesused(vb);
398
399 if (vb->vb2_queue->allow_zero_bytesused)
400 planes[0].bytesused = b->bytesused;
401 else
402 planes[0].bytesused = b->bytesused ?
403 b->bytesused : planes[0].length;
404 } else
405 planes[0].bytesused = 0;
406
407 }
408
409 /* Zero flags that the vb2 core handles */
410 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
411 if ((vb->vb2_queue->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) !=
412 V4L2_BUF_FLAG_TIMESTAMP_COPY || !V4L2_TYPE_IS_OUTPUT(b->type)) {
413 /*
414 * Non-COPY timestamps and non-OUTPUT queues will get
415 * their timestamp and timestamp source flags from the
416 * queue.
417 */
418 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
419 }
420
421 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
422 /*
423 * For output buffers mask out the timecode flag:
424 * this will be handled later in vb2_internal_qbuf().
425 * The 'field' is valid metadata for this output buffer
426 * and so that needs to be copied here.
427 */
428 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
429 vbuf->field = b->field;
430 } else {
431 /* Zero any output buffer flags as this is a capture buffer */
432 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
433 }
434
435 return 0;
436 }
437
438 static const struct vb2_buf_ops v4l2_buf_ops = {
439 .verify_planes_array = __verify_planes_array_core,
440 .fill_user_buffer = __fill_v4l2_buffer,
441 .fill_vb2_buffer = __fill_vb2_buffer,
442 .set_timestamp = __set_timestamp,
443 };
444
445 /**
446 * vb2_querybuf() - query video buffer information
447 * @q: videobuf queue
448 * @b: buffer struct passed from userspace to vidioc_querybuf handler
449 * in driver
450 *
451 * Should be called from vidioc_querybuf ioctl handler in driver.
452 * This function will verify the passed v4l2_buffer structure and fill the
453 * relevant information for the userspace.
454 *
455 * The return values from this function are intended to be directly returned
456 * from vidioc_querybuf handler in driver.
457 */
vb2_querybuf(struct vb2_queue * q,struct v4l2_buffer * b)458 int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
459 {
460 struct vb2_buffer *vb;
461 int ret;
462
463 if (b->type != q->type) {
464 dprintk(1, "wrong buffer type\n");
465 return -EINVAL;
466 }
467
468 if (b->index >= q->num_buffers) {
469 dprintk(1, "buffer index out of range\n");
470 return -EINVAL;
471 }
472 vb = q->bufs[b->index];
473 ret = __verify_planes_array(vb, b);
474
475 return ret ? ret : vb2_core_querybuf(q, b->index, b);
476 }
477 EXPORT_SYMBOL(vb2_querybuf);
478
479 /**
480 * vb2_reqbufs() - Wrapper for vb2_core_reqbufs() that also verifies
481 * the memory and type values.
482 * @q: videobuf2 queue
483 * @req: struct passed from userspace to vidioc_reqbufs handler
484 * in driver
485 */
vb2_reqbufs(struct vb2_queue * q,struct v4l2_requestbuffers * req)486 int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
487 {
488 int ret = vb2_verify_memory_type(q, req->memory, req->type);
489
490 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
491 }
492 EXPORT_SYMBOL_GPL(vb2_reqbufs);
493
494 /**
495 * vb2_prepare_buf() - Pass ownership of a buffer from userspace to the kernel
496 * @q: videobuf2 queue
497 * @b: buffer structure passed from userspace to vidioc_prepare_buf
498 * handler in driver
499 *
500 * Should be called from vidioc_prepare_buf ioctl handler of a driver.
501 * This function:
502 * 1) verifies the passed buffer,
503 * 2) calls buf_prepare callback in the driver (if provided), in which
504 * driver-specific buffer initialization can be performed,
505 *
506 * The return values from this function are intended to be directly returned
507 * from vidioc_prepare_buf handler in driver.
508 */
vb2_prepare_buf(struct vb2_queue * q,struct v4l2_buffer * b)509 int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b)
510 {
511 int ret;
512
513 if (vb2_fileio_is_active(q)) {
514 dprintk(1, "file io in progress\n");
515 return -EBUSY;
516 }
517
518 ret = vb2_queue_or_prepare_buf(q, b, "prepare_buf");
519
520 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
521 }
522 EXPORT_SYMBOL_GPL(vb2_prepare_buf);
523
524 /**
525 * vb2_create_bufs() - Wrapper for vb2_core_create_bufs() that also verifies
526 * the memory and type values.
527 * @q: videobuf2 queue
528 * @create: creation parameters, passed from userspace to vidioc_create_bufs
529 * handler in driver
530 */
vb2_create_bufs(struct vb2_queue * q,struct v4l2_create_buffers * create)531 int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
532 {
533 int ret = vb2_verify_memory_type(q, create->memory,
534 create->format.type);
535
536 create->index = q->num_buffers;
537 if (create->count == 0)
538 return ret != -EBUSY ? ret : 0;
539 return ret ? ret : vb2_core_create_bufs(q, create->memory,
540 &create->count, &create->format);
541 }
542 EXPORT_SYMBOL_GPL(vb2_create_bufs);
543
vb2_internal_qbuf(struct vb2_queue * q,struct v4l2_buffer * b)544 static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
545 {
546 int ret = vb2_queue_or_prepare_buf(q, b, "qbuf");
547
548 return ret ? ret : vb2_core_qbuf(q, b->index, b);
549 }
550
551 /**
552 * vb2_qbuf() - Queue a buffer from userspace
553 * @q: videobuf2 queue
554 * @b: buffer structure passed from userspace to vidioc_qbuf handler
555 * in driver
556 *
557 * Should be called from vidioc_qbuf ioctl handler of a driver.
558 * This function:
559 * 1) verifies the passed buffer,
560 * 2) if necessary, calls buf_prepare callback in the driver (if provided), in
561 * which driver-specific buffer initialization can be performed,
562 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue
563 * callback for processing.
564 *
565 * The return values from this function are intended to be directly returned
566 * from vidioc_qbuf handler in driver.
567 */
vb2_qbuf(struct vb2_queue * q,struct v4l2_buffer * b)568 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
569 {
570 if (vb2_fileio_is_active(q)) {
571 dprintk(1, "file io in progress\n");
572 return -EBUSY;
573 }
574
575 return vb2_internal_qbuf(q, b);
576 }
577 EXPORT_SYMBOL_GPL(vb2_qbuf);
578
vb2_internal_dqbuf(struct vb2_queue * q,struct v4l2_buffer * b,bool nonblocking)579 static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b,
580 bool nonblocking)
581 {
582 int ret;
583
584 if (b->type != q->type) {
585 dprintk(1, "invalid buffer type\n");
586 return -EINVAL;
587 }
588
589 ret = vb2_core_dqbuf(q, b, nonblocking);
590
591 if (!ret && !q->is_output &&
592 b->flags & V4L2_BUF_FLAG_LAST)
593 q->last_buffer_dequeued = true;
594
595 /*
596 * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
597 * cleared.
598 */
599 b->flags &= ~V4L2_BUF_FLAG_DONE;
600
601 return ret;
602 }
603
604 /**
605 * vb2_dqbuf() - Dequeue a buffer to the userspace
606 * @q: videobuf2 queue
607 * @b: buffer structure passed from userspace to vidioc_dqbuf handler
608 * in driver
609 * @nonblocking: if true, this call will not sleep waiting for a buffer if no
610 * buffers ready for dequeuing are present. Normally the driver
611 * would be passing (file->f_flags & O_NONBLOCK) here
612 *
613 * Should be called from vidioc_dqbuf ioctl handler of a driver.
614 * This function:
615 * 1) verifies the passed buffer,
616 * 2) calls buf_finish callback in the driver (if provided), in which
617 * driver can perform any additional operations that may be required before
618 * returning the buffer to userspace, such as cache sync,
619 * 3) the buffer struct members are filled with relevant information for
620 * the userspace.
621 *
622 * The return values from this function are intended to be directly returned
623 * from vidioc_dqbuf handler in driver.
624 */
vb2_dqbuf(struct vb2_queue * q,struct v4l2_buffer * b,bool nonblocking)625 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
626 {
627 if (vb2_fileio_is_active(q)) {
628 dprintk(1, "file io in progress\n");
629 return -EBUSY;
630 }
631 return vb2_internal_dqbuf(q, b, nonblocking);
632 }
633 EXPORT_SYMBOL_GPL(vb2_dqbuf);
634
635 /**
636 * vb2_streamon - start streaming
637 * @q: videobuf2 queue
638 * @type: type argument passed from userspace to vidioc_streamon handler
639 *
640 * Should be called from vidioc_streamon handler of a driver.
641 * This function:
642 * 1) verifies current state
643 * 2) passes any previously queued buffers to the driver and starts streaming
644 *
645 * The return values from this function are intended to be directly returned
646 * from vidioc_streamon handler in the driver.
647 */
vb2_streamon(struct vb2_queue * q,enum v4l2_buf_type type)648 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
649 {
650 if (vb2_fileio_is_active(q)) {
651 dprintk(1, "file io in progress\n");
652 return -EBUSY;
653 }
654 return vb2_core_streamon(q, type);
655 }
656 EXPORT_SYMBOL_GPL(vb2_streamon);
657
658 /**
659 * vb2_streamoff - stop streaming
660 * @q: videobuf2 queue
661 * @type: type argument passed from userspace to vidioc_streamoff handler
662 *
663 * Should be called from vidioc_streamoff handler of a driver.
664 * This function:
665 * 1) verifies current state,
666 * 2) stop streaming and dequeues any queued buffers, including those previously
667 * passed to the driver (after waiting for the driver to finish).
668 *
669 * This call can be used for pausing playback.
670 * The return values from this function are intended to be directly returned
671 * from vidioc_streamoff handler in the driver
672 */
vb2_streamoff(struct vb2_queue * q,enum v4l2_buf_type type)673 int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
674 {
675 if (vb2_fileio_is_active(q)) {
676 dprintk(1, "file io in progress\n");
677 return -EBUSY;
678 }
679 return vb2_core_streamoff(q, type);
680 }
681 EXPORT_SYMBOL_GPL(vb2_streamoff);
682
683 /**
684 * vb2_expbuf() - Export a buffer as a file descriptor
685 * @q: videobuf2 queue
686 * @eb: export buffer structure passed from userspace to vidioc_expbuf
687 * handler in driver
688 *
689 * The return values from this function are intended to be directly returned
690 * from vidioc_expbuf handler in driver.
691 */
vb2_expbuf(struct vb2_queue * q,struct v4l2_exportbuffer * eb)692 int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
693 {
694 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
695 eb->plane, eb->flags);
696 }
697 EXPORT_SYMBOL_GPL(vb2_expbuf);
698
699 /**
700 * vb2_queue_init() - initialize a videobuf2 queue
701 * @q: videobuf2 queue; this structure should be allocated in driver
702 *
703 * The vb2_queue structure should be allocated by the driver. The driver is
704 * responsible of clearing it's content and setting initial values for some
705 * required entries before calling this function.
706 * q->ops, q->mem_ops, q->type and q->io_modes are mandatory. Please refer
707 * to the struct vb2_queue description in include/media/videobuf2-core.h
708 * for more information.
709 */
vb2_queue_init(struct vb2_queue * q)710 int vb2_queue_init(struct vb2_queue *q)
711 {
712 /*
713 * Sanity check
714 */
715 if (WARN_ON(!q) ||
716 WARN_ON(q->timestamp_flags &
717 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
718 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
719 return -EINVAL;
720
721 /* Warn that the driver should choose an appropriate timestamp type */
722 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
723 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
724
725 /* Warn that vb2_memory should match with v4l2_memory */
726 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
727 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
728 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
729 return -EINVAL;
730
731 if (q->buf_struct_size == 0)
732 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
733
734 q->buf_ops = &v4l2_buf_ops;
735 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
736 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
737
738 return vb2_core_queue_init(q);
739 }
740 EXPORT_SYMBOL_GPL(vb2_queue_init);
741
742 static int __vb2_init_fileio(struct vb2_queue *q, int read);
743 static int __vb2_cleanup_fileio(struct vb2_queue *q);
744
745 /**
746 * vb2_queue_release() - stop streaming, release the queue and free memory
747 * @q: videobuf2 queue
748 *
749 * This function stops streaming and performs necessary clean ups, including
750 * freeing video buffer memory. The driver is responsible for freeing
751 * the vb2_queue structure itself.
752 */
vb2_queue_release(struct vb2_queue * q)753 void vb2_queue_release(struct vb2_queue *q)
754 {
755 __vb2_cleanup_fileio(q);
756 vb2_core_queue_release(q);
757 }
758 EXPORT_SYMBOL_GPL(vb2_queue_release);
759
760 /**
761 * vb2_poll() - implements poll userspace operation
762 * @q: videobuf2 queue
763 * @file: file argument passed to the poll file operation handler
764 * @wait: wait argument passed to the poll file operation handler
765 *
766 * This function implements poll file operation handler for a driver.
767 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will
768 * be informed that the file descriptor of a video device is available for
769 * reading.
770 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor
771 * will be reported as available for writing.
772 *
773 * If the driver uses struct v4l2_fh, then vb2_poll() will also check for any
774 * pending events.
775 *
776 * The return values from this function are intended to be directly returned
777 * from poll handler in driver.
778 */
vb2_poll(struct vb2_queue * q,struct file * file,poll_table * wait)779 unsigned int vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
780 {
781 struct video_device *vfd = video_devdata(file);
782 unsigned long req_events = poll_requested_events(wait);
783 struct vb2_buffer *vb = NULL;
784 unsigned int res = 0;
785 unsigned long flags;
786
787 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
788 struct v4l2_fh *fh = file->private_data;
789
790 if (v4l2_event_pending(fh))
791 res = POLLPRI;
792 else if (req_events & POLLPRI)
793 poll_wait(file, &fh->wait, wait);
794 }
795
796 if (!q->is_output && !(req_events & (POLLIN | POLLRDNORM)))
797 return res;
798 if (q->is_output && !(req_events & (POLLOUT | POLLWRNORM)))
799 return res;
800
801 /*
802 * Start file I/O emulator only if streaming API has not been used yet.
803 */
804 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) {
805 if (!q->is_output && (q->io_modes & VB2_READ) &&
806 (req_events & (POLLIN | POLLRDNORM))) {
807 if (__vb2_init_fileio(q, 1))
808 return res | POLLERR;
809 }
810 if (q->is_output && (q->io_modes & VB2_WRITE) &&
811 (req_events & (POLLOUT | POLLWRNORM))) {
812 if (__vb2_init_fileio(q, 0))
813 return res | POLLERR;
814 /*
815 * Write to OUTPUT queue can be done immediately.
816 */
817 return res | POLLOUT | POLLWRNORM;
818 }
819 }
820
821 /*
822 * There is nothing to wait for if the queue isn't streaming, or if the
823 * error flag is set.
824 */
825 if (!vb2_is_streaming(q) || q->error)
826 return res | POLLERR;
827 /*
828 * For compatibility with vb1: if QBUF hasn't been called yet, then
829 * return POLLERR as well. This only affects capture queues, output
830 * queues will always initialize waiting_for_buffers to false.
831 */
832 if (q->waiting_for_buffers)
833 return res | POLLERR;
834
835 /*
836 * For output streams you can call write() as long as there are fewer
837 * buffers queued than there are buffers available.
838 */
839 if (q->is_output && q->fileio && q->queued_count < q->num_buffers)
840 return res | POLLOUT | POLLWRNORM;
841
842 if (list_empty(&q->done_list)) {
843 /*
844 * If the last buffer was dequeued from a capture queue,
845 * return immediately. DQBUF will return -EPIPE.
846 */
847 if (q->last_buffer_dequeued)
848 return res | POLLIN | POLLRDNORM;
849
850 poll_wait(file, &q->done_wq, wait);
851 }
852
853 /*
854 * Take first buffer available for dequeuing.
855 */
856 spin_lock_irqsave(&q->done_lock, flags);
857 if (!list_empty(&q->done_list))
858 vb = list_first_entry(&q->done_list, struct vb2_buffer,
859 done_entry);
860 spin_unlock_irqrestore(&q->done_lock, flags);
861
862 if (vb && (vb->state == VB2_BUF_STATE_DONE
863 || vb->state == VB2_BUF_STATE_ERROR)) {
864 return (q->is_output) ?
865 res | POLLOUT | POLLWRNORM :
866 res | POLLIN | POLLRDNORM;
867 }
868 return res;
869 }
870 EXPORT_SYMBOL_GPL(vb2_poll);
871
872 /**
873 * struct vb2_fileio_buf - buffer context used by file io emulator
874 *
875 * vb2 provides a compatibility layer and emulator of file io (read and
876 * write) calls on top of streaming API. This structure is used for
877 * tracking context related to the buffers.
878 */
879 struct vb2_fileio_buf {
880 void *vaddr;
881 unsigned int size;
882 unsigned int pos;
883 unsigned int queued:1;
884 };
885
886 /**
887 * struct vb2_fileio_data - queue context used by file io emulator
888 *
889 * @cur_index: the index of the buffer currently being read from or
890 * written to. If equal to q->num_buffers then a new buffer
891 * must be dequeued.
892 * @initial_index: in the read() case all buffers are queued up immediately
893 * in __vb2_init_fileio() and __vb2_perform_fileio() just cycles
894 * buffers. However, in the write() case no buffers are initially
895 * queued, instead whenever a buffer is full it is queued up by
896 * __vb2_perform_fileio(). Only once all available buffers have
897 * been queued up will __vb2_perform_fileio() start to dequeue
898 * buffers. This means that initially __vb2_perform_fileio()
899 * needs to know what buffer index to use when it is queuing up
900 * the buffers for the first time. That initial index is stored
901 * in this field. Once it is equal to q->num_buffers all
902 * available buffers have been queued and __vb2_perform_fileio()
903 * should start the normal dequeue/queue cycle.
904 *
905 * vb2 provides a compatibility layer and emulator of file io (read and
906 * write) calls on top of streaming API. For proper operation it required
907 * this structure to save the driver state between each call of the read
908 * or write function.
909 */
910 struct vb2_fileio_data {
911 struct v4l2_requestbuffers req;
912 struct v4l2_plane p;
913 struct v4l2_buffer b;
914 struct vb2_fileio_buf bufs[VB2_MAX_FRAME];
915 unsigned int cur_index;
916 unsigned int initial_index;
917 unsigned int q_count;
918 unsigned int dq_count;
919 unsigned read_once:1;
920 unsigned write_immediately:1;
921 };
922
923 /**
924 * __vb2_init_fileio() - initialize file io emulator
925 * @q: videobuf2 queue
926 * @read: mode selector (1 means read, 0 means write)
927 */
__vb2_init_fileio(struct vb2_queue * q,int read)928 static int __vb2_init_fileio(struct vb2_queue *q, int read)
929 {
930 struct vb2_fileio_data *fileio;
931 int i, ret;
932 unsigned int count = 0;
933
934 /*
935 * Sanity check
936 */
937 if (WARN_ON((read && !(q->io_modes & VB2_READ)) ||
938 (!read && !(q->io_modes & VB2_WRITE))))
939 return -EINVAL;
940
941 /*
942 * Check if device supports mapping buffers to kernel virtual space.
943 */
944 if (!q->mem_ops->vaddr)
945 return -EBUSY;
946
947 /*
948 * Check if streaming api has not been already activated.
949 */
950 if (q->streaming || q->num_buffers > 0)
951 return -EBUSY;
952
953 /*
954 * Start with count 1, driver can increase it in queue_setup()
955 */
956 count = 1;
957
958 dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
959 (read) ? "read" : "write", count, q->fileio_read_once,
960 q->fileio_write_immediately);
961
962 fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
963 if (fileio == NULL)
964 return -ENOMEM;
965
966 fileio->read_once = q->fileio_read_once;
967 fileio->write_immediately = q->fileio_write_immediately;
968
969 /*
970 * Request buffers and use MMAP type to force driver
971 * to allocate buffers by itself.
972 */
973 fileio->req.count = count;
974 fileio->req.memory = VB2_MEMORY_MMAP;
975 fileio->req.type = q->type;
976 q->fileio = fileio;
977 ret = vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
978 if (ret)
979 goto err_kfree;
980
981 /*
982 * Check if plane_count is correct
983 * (multiplane buffers are not supported).
984 */
985 if (q->bufs[0]->num_planes != 1) {
986 ret = -EBUSY;
987 goto err_reqbufs;
988 }
989
990 /*
991 * Get kernel address of each buffer.
992 */
993 for (i = 0; i < q->num_buffers; i++) {
994 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0);
995 if (fileio->bufs[i].vaddr == NULL) {
996 ret = -EINVAL;
997 goto err_reqbufs;
998 }
999 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0);
1000 }
1001
1002 /*
1003 * Read mode requires pre queuing of all buffers.
1004 */
1005 if (read) {
1006 bool is_multiplanar = q->is_multiplanar;
1007
1008 /*
1009 * Queue all buffers.
1010 */
1011 for (i = 0; i < q->num_buffers; i++) {
1012 struct v4l2_buffer *b = &fileio->b;
1013
1014 memset(b, 0, sizeof(*b));
1015 b->type = q->type;
1016 if (is_multiplanar) {
1017 memset(&fileio->p, 0, sizeof(fileio->p));
1018 b->m.planes = &fileio->p;
1019 b->length = 1;
1020 }
1021 b->memory = q->memory;
1022 b->index = i;
1023 ret = vb2_internal_qbuf(q, b);
1024 if (ret)
1025 goto err_reqbufs;
1026 fileio->bufs[i].queued = 1;
1027 }
1028 /*
1029 * All buffers have been queued, so mark that by setting
1030 * initial_index to q->num_buffers
1031 */
1032 fileio->initial_index = q->num_buffers;
1033 fileio->cur_index = q->num_buffers;
1034 }
1035
1036 /*
1037 * Start streaming.
1038 */
1039 ret = vb2_core_streamon(q, q->type);
1040 if (ret)
1041 goto err_reqbufs;
1042
1043 return ret;
1044
1045 err_reqbufs:
1046 fileio->req.count = 0;
1047 vb2_core_reqbufs(q, fileio->req.memory, &fileio->req.count);
1048
1049 err_kfree:
1050 q->fileio = NULL;
1051 kfree(fileio);
1052 return ret;
1053 }
1054
1055 /**
1056 * __vb2_cleanup_fileio() - free resourced used by file io emulator
1057 * @q: videobuf2 queue
1058 */
__vb2_cleanup_fileio(struct vb2_queue * q)1059 static int __vb2_cleanup_fileio(struct vb2_queue *q)
1060 {
1061 struct vb2_fileio_data *fileio = q->fileio;
1062
1063 if (fileio) {
1064 vb2_core_streamoff(q, q->type);
1065 q->fileio = NULL;
1066 fileio->req.count = 0;
1067 vb2_reqbufs(q, &fileio->req);
1068 kfree(fileio);
1069 dprintk(3, "file io emulator closed\n");
1070 }
1071 return 0;
1072 }
1073
1074 /**
1075 * __vb2_perform_fileio() - perform a single file io (read or write) operation
1076 * @q: videobuf2 queue
1077 * @data: pointed to target userspace buffer
1078 * @count: number of bytes to read or write
1079 * @ppos: file handle position tracking pointer
1080 * @nonblock: mode selector (1 means blocking calls, 0 means nonblocking)
1081 * @read: access mode selector (1 means read, 0 means write)
1082 */
__vb2_perform_fileio(struct vb2_queue * q,char __user * data,size_t count,loff_t * ppos,int nonblock,int read)1083 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count,
1084 loff_t *ppos, int nonblock, int read)
1085 {
1086 struct vb2_fileio_data *fileio;
1087 struct vb2_fileio_buf *buf;
1088 bool is_multiplanar = q->is_multiplanar;
1089 /*
1090 * When using write() to write data to an output video node the vb2 core
1091 * should set timestamps if V4L2_BUF_FLAG_TIMESTAMP_COPY is set. Nobody
1092 * else is able to provide this information with the write() operation.
1093 */
1094 bool set_timestamp = !read &&
1095 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1096 V4L2_BUF_FLAG_TIMESTAMP_COPY;
1097 int ret, index;
1098
1099 dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
1100 read ? "read" : "write", (long)*ppos, count,
1101 nonblock ? "non" : "");
1102
1103 if (!data)
1104 return -EINVAL;
1105
1106 /*
1107 * Initialize emulator on first call.
1108 */
1109 if (!vb2_fileio_is_active(q)) {
1110 ret = __vb2_init_fileio(q, read);
1111 dprintk(3, "vb2_init_fileio result: %d\n", ret);
1112 if (ret)
1113 return ret;
1114 }
1115 fileio = q->fileio;
1116
1117 /*
1118 * Check if we need to dequeue the buffer.
1119 */
1120 index = fileio->cur_index;
1121 if (index >= q->num_buffers) {
1122 /*
1123 * Call vb2_dqbuf to get buffer back.
1124 */
1125 memset(&fileio->b, 0, sizeof(fileio->b));
1126 fileio->b.type = q->type;
1127 fileio->b.memory = q->memory;
1128 if (is_multiplanar) {
1129 memset(&fileio->p, 0, sizeof(fileio->p));
1130 fileio->b.m.planes = &fileio->p;
1131 fileio->b.length = 1;
1132 }
1133 ret = vb2_internal_dqbuf(q, &fileio->b, nonblock);
1134 dprintk(5, "vb2_dqbuf result: %d\n", ret);
1135 if (ret)
1136 return ret;
1137 fileio->dq_count += 1;
1138
1139 fileio->cur_index = index = fileio->b.index;
1140 buf = &fileio->bufs[index];
1141
1142 /*
1143 * Get number of bytes filled by the driver
1144 */
1145 buf->pos = 0;
1146 buf->queued = 0;
1147 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0)
1148 : vb2_plane_size(q->bufs[index], 0);
1149 /* Compensate for data_offset on read in the multiplanar case. */
1150 if (is_multiplanar && read &&
1151 fileio->b.m.planes[0].data_offset < buf->size) {
1152 buf->pos = fileio->b.m.planes[0].data_offset;
1153 buf->size -= buf->pos;
1154 }
1155 } else {
1156 buf = &fileio->bufs[index];
1157 }
1158
1159 /*
1160 * Limit count on last few bytes of the buffer.
1161 */
1162 if (buf->pos + count > buf->size) {
1163 count = buf->size - buf->pos;
1164 dprintk(5, "reducing read count: %zd\n", count);
1165 }
1166
1167 /*
1168 * Transfer data to userspace.
1169 */
1170 dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
1171 count, index, buf->pos);
1172 if (read)
1173 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
1174 else
1175 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
1176 if (ret) {
1177 dprintk(3, "error copying data\n");
1178 return -EFAULT;
1179 }
1180
1181 /*
1182 * Update counters.
1183 */
1184 buf->pos += count;
1185 *ppos += count;
1186
1187 /*
1188 * Queue next buffer if required.
1189 */
1190 if (buf->pos == buf->size || (!read && fileio->write_immediately)) {
1191 /*
1192 * Check if this is the last buffer to read.
1193 */
1194 if (read && fileio->read_once && fileio->dq_count == 1) {
1195 dprintk(3, "read limit reached\n");
1196 return __vb2_cleanup_fileio(q);
1197 }
1198
1199 /*
1200 * Call vb2_qbuf and give buffer to the driver.
1201 */
1202 memset(&fileio->b, 0, sizeof(fileio->b));
1203 fileio->b.type = q->type;
1204 fileio->b.memory = q->memory;
1205 fileio->b.index = index;
1206 fileio->b.bytesused = buf->pos;
1207 if (is_multiplanar) {
1208 memset(&fileio->p, 0, sizeof(fileio->p));
1209 fileio->p.bytesused = buf->pos;
1210 fileio->b.m.planes = &fileio->p;
1211 fileio->b.length = 1;
1212 }
1213 if (set_timestamp)
1214 v4l2_get_timestamp(&fileio->b.timestamp);
1215 ret = vb2_internal_qbuf(q, &fileio->b);
1216 dprintk(5, "vb2_dbuf result: %d\n", ret);
1217 if (ret)
1218 return ret;
1219
1220 /*
1221 * Buffer has been queued, update the status
1222 */
1223 buf->pos = 0;
1224 buf->queued = 1;
1225 buf->size = vb2_plane_size(q->bufs[index], 0);
1226 fileio->q_count += 1;
1227 /*
1228 * If we are queuing up buffers for the first time, then
1229 * increase initial_index by one.
1230 */
1231 if (fileio->initial_index < q->num_buffers)
1232 fileio->initial_index++;
1233 /*
1234 * The next buffer to use is either a buffer that's going to be
1235 * queued for the first time (initial_index < q->num_buffers)
1236 * or it is equal to q->num_buffers, meaning that the next
1237 * time we need to dequeue a buffer since we've now queued up
1238 * all the 'first time' buffers.
1239 */
1240 fileio->cur_index = fileio->initial_index;
1241 }
1242
1243 /*
1244 * Return proper number of bytes processed.
1245 */
1246 if (ret == 0)
1247 ret = count;
1248 return ret;
1249 }
1250
vb2_read(struct vb2_queue * q,char __user * data,size_t count,loff_t * ppos,int nonblocking)1251 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count,
1252 loff_t *ppos, int nonblocking)
1253 {
1254 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1);
1255 }
1256 EXPORT_SYMBOL_GPL(vb2_read);
1257
vb2_write(struct vb2_queue * q,const char __user * data,size_t count,loff_t * ppos,int nonblocking)1258 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count,
1259 loff_t *ppos, int nonblocking)
1260 {
1261 return __vb2_perform_fileio(q, (char __user *) data, count,
1262 ppos, nonblocking, 0);
1263 }
1264 EXPORT_SYMBOL_GPL(vb2_write);
1265
1266 struct vb2_threadio_data {
1267 struct task_struct *thread;
1268 vb2_thread_fnc fnc;
1269 void *priv;
1270 bool stop;
1271 };
1272
vb2_thread(void * data)1273 static int vb2_thread(void *data)
1274 {
1275 struct vb2_queue *q = data;
1276 struct vb2_threadio_data *threadio = q->threadio;
1277 struct vb2_fileio_data *fileio = q->fileio;
1278 bool set_timestamp = false;
1279 int prequeue = 0;
1280 int index = 0;
1281 int ret = 0;
1282
1283 if (q->is_output) {
1284 prequeue = q->num_buffers;
1285 set_timestamp =
1286 (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
1287 V4L2_BUF_FLAG_TIMESTAMP_COPY;
1288 }
1289
1290 set_freezable();
1291
1292 for (;;) {
1293 struct vb2_buffer *vb;
1294
1295 /*
1296 * Call vb2_dqbuf to get buffer back.
1297 */
1298 memset(&fileio->b, 0, sizeof(fileio->b));
1299 fileio->b.type = q->type;
1300 fileio->b.memory = q->memory;
1301 if (prequeue) {
1302 fileio->b.index = index++;
1303 prequeue--;
1304 } else {
1305 call_void_qop(q, wait_finish, q);
1306 if (!threadio->stop)
1307 ret = vb2_internal_dqbuf(q, &fileio->b, 0);
1308 call_void_qop(q, wait_prepare, q);
1309 dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
1310 }
1311 if (ret || threadio->stop)
1312 break;
1313 try_to_freeze();
1314
1315 vb = q->bufs[fileio->b.index];
1316 if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR))
1317 if (threadio->fnc(vb, threadio->priv))
1318 break;
1319 call_void_qop(q, wait_finish, q);
1320 if (set_timestamp)
1321 v4l2_get_timestamp(&fileio->b.timestamp);
1322 if (!threadio->stop)
1323 ret = vb2_internal_qbuf(q, &fileio->b);
1324 call_void_qop(q, wait_prepare, q);
1325 if (ret || threadio->stop)
1326 break;
1327 }
1328
1329 /* Hmm, linux becomes *very* unhappy without this ... */
1330 while (!kthread_should_stop()) {
1331 set_current_state(TASK_INTERRUPTIBLE);
1332 schedule();
1333 }
1334 return 0;
1335 }
1336
1337 /*
1338 * This function should not be used for anything else but the videobuf2-dvb
1339 * support. If you think you have another good use-case for this, then please
1340 * contact the linux-media mailinglist first.
1341 */
vb2_thread_start(struct vb2_queue * q,vb2_thread_fnc fnc,void * priv,const char * thread_name)1342 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv,
1343 const char *thread_name)
1344 {
1345 struct vb2_threadio_data *threadio;
1346 int ret = 0;
1347
1348 if (q->threadio)
1349 return -EBUSY;
1350 if (vb2_is_busy(q))
1351 return -EBUSY;
1352 if (WARN_ON(q->fileio))
1353 return -EBUSY;
1354
1355 threadio = kzalloc(sizeof(*threadio), GFP_KERNEL);
1356 if (threadio == NULL)
1357 return -ENOMEM;
1358 threadio->fnc = fnc;
1359 threadio->priv = priv;
1360
1361 ret = __vb2_init_fileio(q, !q->is_output);
1362 dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
1363 if (ret)
1364 goto nomem;
1365 q->threadio = threadio;
1366 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name);
1367 if (IS_ERR(threadio->thread)) {
1368 ret = PTR_ERR(threadio->thread);
1369 threadio->thread = NULL;
1370 goto nothread;
1371 }
1372 return 0;
1373
1374 nothread:
1375 __vb2_cleanup_fileio(q);
1376 nomem:
1377 kfree(threadio);
1378 return ret;
1379 }
1380 EXPORT_SYMBOL_GPL(vb2_thread_start);
1381
vb2_thread_stop(struct vb2_queue * q)1382 int vb2_thread_stop(struct vb2_queue *q)
1383 {
1384 struct vb2_threadio_data *threadio = q->threadio;
1385 int err;
1386
1387 if (threadio == NULL)
1388 return 0;
1389 threadio->stop = true;
1390 /* Wake up all pending sleeps in the thread */
1391 vb2_queue_error(q);
1392 err = kthread_stop(threadio->thread);
1393 __vb2_cleanup_fileio(q);
1394 threadio->thread = NULL;
1395 kfree(threadio);
1396 q->threadio = NULL;
1397 return err;
1398 }
1399 EXPORT_SYMBOL_GPL(vb2_thread_stop);
1400
1401 /*
1402 * The following functions are not part of the vb2 core API, but are helper
1403 * functions that plug into struct v4l2_ioctl_ops, struct v4l2_file_operations
1404 * and struct vb2_ops.
1405 * They contain boilerplate code that most if not all drivers have to do
1406 * and so they simplify the driver code.
1407 */
1408
1409 /* 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)1410 static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
1411 {
1412 return vdev->queue->owner && vdev->queue->owner != file->private_data;
1413 }
1414
1415 /* vb2 ioctl helpers */
1416
vb2_ioctl_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * p)1417 int vb2_ioctl_reqbufs(struct file *file, void *priv,
1418 struct v4l2_requestbuffers *p)
1419 {
1420 struct video_device *vdev = video_devdata(file);
1421 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
1422
1423 if (res)
1424 return res;
1425 if (vb2_queue_is_busy(vdev, file))
1426 return -EBUSY;
1427 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
1428 /* If count == 0, then the owner has released all buffers and he
1429 is no longer owner of the queue. Otherwise we have a new owner. */
1430 if (res == 0)
1431 vdev->queue->owner = p->count ? file->private_data : NULL;
1432 return res;
1433 }
1434 EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
1435
vb2_ioctl_create_bufs(struct file * file,void * priv,struct v4l2_create_buffers * p)1436 int vb2_ioctl_create_bufs(struct file *file, void *priv,
1437 struct v4l2_create_buffers *p)
1438 {
1439 struct video_device *vdev = video_devdata(file);
1440 int res = vb2_verify_memory_type(vdev->queue, p->memory,
1441 p->format.type);
1442
1443 p->index = vdev->queue->num_buffers;
1444 /*
1445 * If count == 0, then just check if memory and type are valid.
1446 * Any -EBUSY result from vb2_verify_memory_type can be mapped to 0.
1447 */
1448 if (p->count == 0)
1449 return res != -EBUSY ? res : 0;
1450 if (res)
1451 return res;
1452 if (vb2_queue_is_busy(vdev, file))
1453 return -EBUSY;
1454 res = vb2_core_create_bufs(vdev->queue, p->memory, &p->count,
1455 &p->format);
1456 if (res == 0)
1457 vdev->queue->owner = file->private_data;
1458 return res;
1459 }
1460 EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
1461
vb2_ioctl_prepare_buf(struct file * file,void * priv,struct v4l2_buffer * p)1462 int vb2_ioctl_prepare_buf(struct file *file, void *priv,
1463 struct v4l2_buffer *p)
1464 {
1465 struct video_device *vdev = video_devdata(file);
1466
1467 if (vb2_queue_is_busy(vdev, file))
1468 return -EBUSY;
1469 return vb2_prepare_buf(vdev->queue, p);
1470 }
1471 EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
1472
vb2_ioctl_querybuf(struct file * file,void * priv,struct v4l2_buffer * p)1473 int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1474 {
1475 struct video_device *vdev = video_devdata(file);
1476
1477 /* No need to call vb2_queue_is_busy(), anyone can query buffers. */
1478 return vb2_querybuf(vdev->queue, p);
1479 }
1480 EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
1481
vb2_ioctl_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)1482 int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1483 {
1484 struct video_device *vdev = video_devdata(file);
1485
1486 if (vb2_queue_is_busy(vdev, file))
1487 return -EBUSY;
1488 return vb2_qbuf(vdev->queue, p);
1489 }
1490 EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
1491
vb2_ioctl_dqbuf(struct file * file,void * priv,struct v4l2_buffer * p)1492 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1493 {
1494 struct video_device *vdev = video_devdata(file);
1495
1496 if (vb2_queue_is_busy(vdev, file))
1497 return -EBUSY;
1498 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
1499 }
1500 EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1501
vb2_ioctl_streamon(struct file * file,void * priv,enum v4l2_buf_type i)1502 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1503 {
1504 struct video_device *vdev = video_devdata(file);
1505
1506 if (vb2_queue_is_busy(vdev, file))
1507 return -EBUSY;
1508 return vb2_streamon(vdev->queue, i);
1509 }
1510 EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1511
vb2_ioctl_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)1512 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1513 {
1514 struct video_device *vdev = video_devdata(file);
1515
1516 if (vb2_queue_is_busy(vdev, file))
1517 return -EBUSY;
1518 return vb2_streamoff(vdev->queue, i);
1519 }
1520 EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1521
vb2_ioctl_expbuf(struct file * file,void * priv,struct v4l2_exportbuffer * p)1522 int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1523 {
1524 struct video_device *vdev = video_devdata(file);
1525
1526 if (vb2_queue_is_busy(vdev, file))
1527 return -EBUSY;
1528 return vb2_expbuf(vdev->queue, p);
1529 }
1530 EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1531
1532 /* v4l2_file_operations helpers */
1533
vb2_fop_mmap(struct file * file,struct vm_area_struct * vma)1534 int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1535 {
1536 struct video_device *vdev = video_devdata(file);
1537
1538 return vb2_mmap(vdev->queue, vma);
1539 }
1540 EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1541
_vb2_fop_release(struct file * file,struct mutex * lock)1542 int _vb2_fop_release(struct file *file, struct mutex *lock)
1543 {
1544 struct video_device *vdev = video_devdata(file);
1545
1546 if (lock)
1547 mutex_lock(lock);
1548 if (file->private_data == vdev->queue->owner) {
1549 vb2_queue_release(vdev->queue);
1550 vdev->queue->owner = NULL;
1551 }
1552 if (lock)
1553 mutex_unlock(lock);
1554 return v4l2_fh_release(file);
1555 }
1556 EXPORT_SYMBOL_GPL(_vb2_fop_release);
1557
vb2_fop_release(struct file * file)1558 int vb2_fop_release(struct file *file)
1559 {
1560 struct video_device *vdev = video_devdata(file);
1561 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1562
1563 return _vb2_fop_release(file, lock);
1564 }
1565 EXPORT_SYMBOL_GPL(vb2_fop_release);
1566
vb2_fop_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1567 ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1568 size_t count, loff_t *ppos)
1569 {
1570 struct video_device *vdev = video_devdata(file);
1571 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1572 int err = -EBUSY;
1573
1574 if (!(vdev->queue->io_modes & VB2_WRITE))
1575 return -EINVAL;
1576 if (lock && mutex_lock_interruptible(lock))
1577 return -ERESTARTSYS;
1578 if (vb2_queue_is_busy(vdev, file))
1579 goto exit;
1580 err = vb2_write(vdev->queue, buf, count, ppos,
1581 file->f_flags & O_NONBLOCK);
1582 if (vdev->queue->fileio)
1583 vdev->queue->owner = file->private_data;
1584 exit:
1585 if (lock)
1586 mutex_unlock(lock);
1587 return err;
1588 }
1589 EXPORT_SYMBOL_GPL(vb2_fop_write);
1590
vb2_fop_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1591 ssize_t vb2_fop_read(struct file *file, char __user *buf,
1592 size_t count, loff_t *ppos)
1593 {
1594 struct video_device *vdev = video_devdata(file);
1595 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1596 int err = -EBUSY;
1597
1598 if (!(vdev->queue->io_modes & VB2_READ))
1599 return -EINVAL;
1600 if (lock && mutex_lock_interruptible(lock))
1601 return -ERESTARTSYS;
1602 if (vb2_queue_is_busy(vdev, file))
1603 goto exit;
1604 err = vb2_read(vdev->queue, buf, count, ppos,
1605 file->f_flags & O_NONBLOCK);
1606 if (vdev->queue->fileio)
1607 vdev->queue->owner = file->private_data;
1608 exit:
1609 if (lock)
1610 mutex_unlock(lock);
1611 return err;
1612 }
1613 EXPORT_SYMBOL_GPL(vb2_fop_read);
1614
vb2_fop_poll(struct file * file,poll_table * wait)1615 unsigned int vb2_fop_poll(struct file *file, poll_table *wait)
1616 {
1617 struct video_device *vdev = video_devdata(file);
1618 struct vb2_queue *q = vdev->queue;
1619 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1620 unsigned res;
1621 void *fileio;
1622
1623 /*
1624 * If this helper doesn't know how to lock, then you shouldn't be using
1625 * it but you should write your own.
1626 */
1627 WARN_ON(!lock);
1628
1629 if (lock && mutex_lock_interruptible(lock))
1630 return POLLERR;
1631
1632 fileio = q->fileio;
1633
1634 res = vb2_poll(vdev->queue, file, wait);
1635
1636 /* If fileio was started, then we have a new queue owner. */
1637 if (!fileio && q->fileio)
1638 q->owner = file->private_data;
1639 if (lock)
1640 mutex_unlock(lock);
1641 return res;
1642 }
1643 EXPORT_SYMBOL_GPL(vb2_fop_poll);
1644
1645 #ifndef CONFIG_MMU
vb2_fop_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)1646 unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1647 unsigned long len, unsigned long pgoff, unsigned long flags)
1648 {
1649 struct video_device *vdev = video_devdata(file);
1650
1651 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1652 }
1653 EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1654 #endif
1655
1656 /* vb2_ops helpers. Only use if vq->lock is non-NULL. */
1657
vb2_ops_wait_prepare(struct vb2_queue * vq)1658 void vb2_ops_wait_prepare(struct vb2_queue *vq)
1659 {
1660 mutex_unlock(vq->lock);
1661 }
1662 EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1663
vb2_ops_wait_finish(struct vb2_queue * vq)1664 void vb2_ops_wait_finish(struct vb2_queue *vq)
1665 {
1666 mutex_lock(vq->lock);
1667 }
1668 EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1669
1670 MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1671 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1672 MODULE_LICENSE("GPL");
1673