1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35static int debug;
36module_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
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
52#define V4L2_BUFFER_OUT_FLAGS (V4L2_BUF_FLAG_PFRAME | \
53 V4L2_BUF_FLAG_BFRAME | \
54 V4L2_BUF_FLAG_KEYFRAME | \
55 V4L2_BUF_FLAG_TIMECODE | \
56 V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)
57
58
59
60
61
62static int __verify_planes_array(struct vb2_buffer *vb, const struct v4l2_buffer *b)
63{
64 if (!V4L2_TYPE_IS_MULTIPLANAR(b->type))
65 return 0;
66
67
68 if (b->m.planes == NULL) {
69 dprintk(1, "multi-planar buffer passed but planes array not provided\n");
70 return -EINVAL;
71 }
72
73 if (b->length < vb->num_planes || b->length > VB2_MAX_PLANES) {
74 dprintk(1, "incorrect planes array length, expected %d, got %d\n",
75 vb->num_planes, b->length);
76 return -EINVAL;
77 }
78
79 return 0;
80}
81
82static int __verify_planes_array_core(struct vb2_buffer *vb, const void *pb)
83{
84 return __verify_planes_array(vb, pb);
85}
86
87
88
89
90
91static int __verify_length(struct vb2_buffer *vb, const struct v4l2_buffer *b)
92{
93 unsigned int length;
94 unsigned int bytesused;
95 unsigned int plane;
96
97 if (!V4L2_TYPE_IS_OUTPUT(b->type))
98 return 0;
99
100 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
101 for (plane = 0; plane < vb->num_planes; ++plane) {
102 length = (b->memory == VB2_MEMORY_USERPTR ||
103 b->memory == VB2_MEMORY_DMABUF)
104 ? b->m.planes[plane].length
105 : vb->planes[plane].length;
106 bytesused = b->m.planes[plane].bytesused
107 ? b->m.planes[plane].bytesused : length;
108
109 if (b->m.planes[plane].bytesused > length)
110 return -EINVAL;
111
112 if (b->m.planes[plane].data_offset > 0 &&
113 b->m.planes[plane].data_offset >= bytesused)
114 return -EINVAL;
115 }
116 } else {
117 length = (b->memory == VB2_MEMORY_USERPTR)
118 ? b->length : vb->planes[0].length;
119
120 if (b->bytesused > length)
121 return -EINVAL;
122 }
123
124 return 0;
125}
126
127
128
129
130static void __init_vb2_v4l2_buffer(struct vb2_buffer *vb)
131{
132 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
133
134 vbuf->request_fd = -1;
135}
136
137static void __copy_timestamp(struct vb2_buffer *vb, const void *pb)
138{
139 const struct v4l2_buffer *b = pb;
140 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
141 struct vb2_queue *q = vb->vb2_queue;
142
143 if (q->is_output) {
144
145
146
147
148 if (q->copy_timestamp)
149 vb->timestamp = v4l2_buffer_get_timestamp(b);
150 vbuf->flags |= b->flags & V4L2_BUF_FLAG_TIMECODE;
151 if (b->flags & V4L2_BUF_FLAG_TIMECODE)
152 vbuf->timecode = b->timecode;
153 }
154};
155
156static void vb2_warn_zero_bytesused(struct vb2_buffer *vb)
157{
158 static bool check_once;
159
160 if (check_once)
161 return;
162
163 check_once = true;
164
165 pr_warn("use of bytesused == 0 is deprecated and will be removed in the future,\n");
166 if (vb->vb2_queue->allow_zero_bytesused)
167 pr_warn("use VIDIOC_DECODER_CMD(V4L2_DEC_CMD_STOP) instead.\n");
168 else
169 pr_warn("use the actual size instead.\n");
170}
171
172static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b)
173{
174 struct vb2_queue *q = vb->vb2_queue;
175 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
176 struct vb2_plane *planes = vbuf->planes;
177 unsigned int plane;
178 int ret;
179
180 ret = __verify_length(vb, b);
181 if (ret < 0) {
182 dprintk(1, "plane parameters verification failed: %d\n", ret);
183 return ret;
184 }
185 if (b->field == V4L2_FIELD_ALTERNATE && q->is_output) {
186
187
188
189
190
191
192
193
194
195 dprintk(1, "the field is incorrectly set to ALTERNATE for an output buffer\n");
196 return -EINVAL;
197 }
198 vbuf->sequence = 0;
199 vbuf->request_fd = -1;
200 vbuf->is_held = false;
201
202 if (V4L2_TYPE_IS_MULTIPLANAR(b->type)) {
203 switch (b->memory) {
204 case VB2_MEMORY_USERPTR:
205 for (plane = 0; plane < vb->num_planes; ++plane) {
206 planes[plane].m.userptr =
207 b->m.planes[plane].m.userptr;
208 planes[plane].length =
209 b->m.planes[plane].length;
210 }
211 break;
212 case VB2_MEMORY_DMABUF:
213 for (plane = 0; plane < vb->num_planes; ++plane) {
214 planes[plane].m.fd =
215 b->m.planes[plane].m.fd;
216 planes[plane].length =
217 b->m.planes[plane].length;
218 }
219 break;
220 default:
221 for (plane = 0; plane < vb->num_planes; ++plane) {
222 planes[plane].m.offset =
223 vb->planes[plane].m.offset;
224 planes[plane].length =
225 vb->planes[plane].length;
226 }
227 break;
228 }
229
230
231 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 for (plane = 0; plane < vb->num_planes; ++plane) {
249 struct vb2_plane *pdst = &planes[plane];
250 struct v4l2_plane *psrc = &b->m.planes[plane];
251
252 if (psrc->bytesused == 0)
253 vb2_warn_zero_bytesused(vb);
254
255 if (vb->vb2_queue->allow_zero_bytesused)
256 pdst->bytesused = psrc->bytesused;
257 else
258 pdst->bytesused = psrc->bytesused ?
259 psrc->bytesused : pdst->length;
260 pdst->data_offset = psrc->data_offset;
261 }
262 }
263 } else {
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 switch (b->memory) {
279 case VB2_MEMORY_USERPTR:
280 planes[0].m.userptr = b->m.userptr;
281 planes[0].length = b->length;
282 break;
283 case VB2_MEMORY_DMABUF:
284 planes[0].m.fd = b->m.fd;
285 planes[0].length = b->length;
286 break;
287 default:
288 planes[0].m.offset = vb->planes[0].m.offset;
289 planes[0].length = vb->planes[0].length;
290 break;
291 }
292
293 planes[0].data_offset = 0;
294 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
295 if (b->bytesused == 0)
296 vb2_warn_zero_bytesused(vb);
297
298 if (vb->vb2_queue->allow_zero_bytesused)
299 planes[0].bytesused = b->bytesused;
300 else
301 planes[0].bytesused = b->bytesused ?
302 b->bytesused : planes[0].length;
303 } else
304 planes[0].bytesused = 0;
305
306 }
307
308
309 vbuf->flags = b->flags & ~V4L2_BUFFER_MASK_FLAGS;
310 if (!vb->vb2_queue->copy_timestamp || !V4L2_TYPE_IS_OUTPUT(b->type)) {
311
312
313
314
315
316 vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
317 }
318
319 if (V4L2_TYPE_IS_OUTPUT(b->type)) {
320
321
322
323
324
325
326 vbuf->flags &= ~V4L2_BUF_FLAG_TIMECODE;
327 vbuf->field = b->field;
328 if (!(q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF))
329 vbuf->flags &= ~V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
330 } else {
331
332 vbuf->flags &= ~V4L2_BUFFER_OUT_FLAGS;
333
334 vbuf->flags &= ~V4L2_BUF_FLAG_LAST;
335 }
336
337 return 0;
338}
339
340static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
341 struct v4l2_buffer *b, bool is_prepare,
342 struct media_request **p_req)
343{
344 const char *opname = is_prepare ? "prepare_buf" : "qbuf";
345 struct media_request *req;
346 struct vb2_v4l2_buffer *vbuf;
347 struct vb2_buffer *vb;
348 int ret;
349
350 if (b->type != q->type) {
351 dprintk(1, "%s: invalid buffer type\n", opname);
352 return -EINVAL;
353 }
354
355 if (b->index >= q->num_buffers) {
356 dprintk(1, "%s: buffer index out of range\n", opname);
357 return -EINVAL;
358 }
359
360 if (q->bufs[b->index] == NULL) {
361
362 dprintk(1, "%s: buffer is NULL\n", opname);
363 return -EINVAL;
364 }
365
366 if (b->memory != q->memory) {
367 dprintk(1, "%s: invalid memory type\n", opname);
368 return -EINVAL;
369 }
370
371 vb = q->bufs[b->index];
372 vbuf = to_vb2_v4l2_buffer(vb);
373 ret = __verify_planes_array(vb, b);
374 if (ret)
375 return ret;
376
377 if (!is_prepare && (b->flags & V4L2_BUF_FLAG_REQUEST_FD) &&
378 vb->state != VB2_BUF_STATE_DEQUEUED) {
379 dprintk(1, "%s: buffer is not in dequeued state\n", opname);
380 return -EINVAL;
381 }
382
383 if (!vb->prepared) {
384
385 memset(vbuf->planes, 0,
386 sizeof(vbuf->planes[0]) * vb->num_planes);
387 ret = vb2_fill_vb2_v4l2_buffer(vb, b);
388 if (ret)
389 return ret;
390 }
391
392 if (is_prepare)
393 return 0;
394
395 if (!(b->flags & V4L2_BUF_FLAG_REQUEST_FD)) {
396 if (q->requires_requests) {
397 dprintk(1, "%s: queue requires requests\n", opname);
398 return -EBADR;
399 }
400 if (q->uses_requests) {
401 dprintk(1, "%s: queue uses requests\n", opname);
402 return -EBUSY;
403 }
404 return 0;
405 } else if (!q->supports_requests) {
406 dprintk(1, "%s: queue does not support requests\n", opname);
407 return -EBADR;
408 } else if (q->uses_qbuf) {
409 dprintk(1, "%s: queue does not use requests\n", opname);
410 return -EBUSY;
411 }
412
413
414
415
416
417
418 if (WARN_ON(!q->lock || !p_req))
419 return -EINVAL;
420
421
422
423
424
425
426 if (WARN_ON(!q->ops->buf_request_complete))
427 return -EINVAL;
428
429
430
431
432
433 if (WARN_ON((q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
434 q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
435 !q->ops->buf_out_validate))
436 return -EINVAL;
437
438 if (b->request_fd < 0) {
439 dprintk(1, "%s: request_fd < 0\n", opname);
440 return -EINVAL;
441 }
442
443 req = media_request_get_by_fd(mdev, b->request_fd);
444 if (IS_ERR(req)) {
445 dprintk(1, "%s: invalid request_fd\n", opname);
446 return PTR_ERR(req);
447 }
448
449
450
451
452
453 if (req->state != MEDIA_REQUEST_STATE_IDLE &&
454 req->state != MEDIA_REQUEST_STATE_UPDATING) {
455 dprintk(1, "%s: request is not idle\n", opname);
456 media_request_put(req);
457 return -EBUSY;
458 }
459
460 *p_req = req;
461 vbuf->request_fd = b->request_fd;
462
463 return 0;
464}
465
466
467
468
469
470static void __fill_v4l2_buffer(struct vb2_buffer *vb, void *pb)
471{
472 struct v4l2_buffer *b = pb;
473 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
474 struct vb2_queue *q = vb->vb2_queue;
475 unsigned int plane;
476
477
478 b->index = vb->index;
479 b->type = vb->type;
480 b->memory = vb->memory;
481 b->bytesused = 0;
482
483 b->flags = vbuf->flags;
484 b->field = vbuf->field;
485 v4l2_buffer_set_timestamp(b, vb->timestamp);
486 b->timecode = vbuf->timecode;
487 b->sequence = vbuf->sequence;
488 b->reserved2 = 0;
489 b->request_fd = 0;
490
491 if (q->is_multiplanar) {
492
493
494
495
496 b->length = vb->num_planes;
497 for (plane = 0; plane < vb->num_planes; ++plane) {
498 struct v4l2_plane *pdst = &b->m.planes[plane];
499 struct vb2_plane *psrc = &vb->planes[plane];
500
501 pdst->bytesused = psrc->bytesused;
502 pdst->length = psrc->length;
503 if (q->memory == VB2_MEMORY_MMAP)
504 pdst->m.mem_offset = psrc->m.offset;
505 else if (q->memory == VB2_MEMORY_USERPTR)
506 pdst->m.userptr = psrc->m.userptr;
507 else if (q->memory == VB2_MEMORY_DMABUF)
508 pdst->m.fd = psrc->m.fd;
509 pdst->data_offset = psrc->data_offset;
510 memset(pdst->reserved, 0, sizeof(pdst->reserved));
511 }
512 } else {
513
514
515
516
517 b->length = vb->planes[0].length;
518 b->bytesused = vb->planes[0].bytesused;
519 if (q->memory == VB2_MEMORY_MMAP)
520 b->m.offset = vb->planes[0].m.offset;
521 else if (q->memory == VB2_MEMORY_USERPTR)
522 b->m.userptr = vb->planes[0].m.userptr;
523 else if (q->memory == VB2_MEMORY_DMABUF)
524 b->m.fd = vb->planes[0].m.fd;
525 }
526
527
528
529
530 b->flags &= ~V4L2_BUFFER_MASK_FLAGS;
531 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
532 if (!q->copy_timestamp) {
533
534
535
536
537 b->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
538 b->flags |= q->timestamp_flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
539 }
540
541 switch (vb->state) {
542 case VB2_BUF_STATE_QUEUED:
543 case VB2_BUF_STATE_ACTIVE:
544 b->flags |= V4L2_BUF_FLAG_QUEUED;
545 break;
546 case VB2_BUF_STATE_IN_REQUEST:
547 b->flags |= V4L2_BUF_FLAG_IN_REQUEST;
548 break;
549 case VB2_BUF_STATE_ERROR:
550 b->flags |= V4L2_BUF_FLAG_ERROR;
551
552 case VB2_BUF_STATE_DONE:
553 b->flags |= V4L2_BUF_FLAG_DONE;
554 break;
555 case VB2_BUF_STATE_PREPARING:
556 case VB2_BUF_STATE_DEQUEUED:
557
558 break;
559 }
560
561 if ((vb->state == VB2_BUF_STATE_DEQUEUED ||
562 vb->state == VB2_BUF_STATE_IN_REQUEST) &&
563 vb->synced && vb->prepared)
564 b->flags |= V4L2_BUF_FLAG_PREPARED;
565
566 if (vb2_buffer_in_use(q, vb))
567 b->flags |= V4L2_BUF_FLAG_MAPPED;
568 if (vbuf->request_fd >= 0) {
569 b->flags |= V4L2_BUF_FLAG_REQUEST_FD;
570 b->request_fd = vbuf->request_fd;
571 }
572}
573
574
575
576
577
578
579static int __fill_vb2_buffer(struct vb2_buffer *vb, struct vb2_plane *planes)
580{
581 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
582 unsigned int plane;
583
584 if (!vb->vb2_queue->copy_timestamp)
585 vb->timestamp = 0;
586
587 for (plane = 0; plane < vb->num_planes; ++plane) {
588 if (vb->vb2_queue->memory != VB2_MEMORY_MMAP) {
589 planes[plane].m = vbuf->planes[plane].m;
590 planes[plane].length = vbuf->planes[plane].length;
591 }
592 planes[plane].bytesused = vbuf->planes[plane].bytesused;
593 planes[plane].data_offset = vbuf->planes[plane].data_offset;
594 }
595 return 0;
596}
597
598static const struct vb2_buf_ops v4l2_buf_ops = {
599 .verify_planes_array = __verify_planes_array_core,
600 .init_buffer = __init_vb2_v4l2_buffer,
601 .fill_user_buffer = __fill_v4l2_buffer,
602 .fill_vb2_buffer = __fill_vb2_buffer,
603 .copy_timestamp = __copy_timestamp,
604};
605
606int vb2_find_timestamp(const struct vb2_queue *q, u64 timestamp,
607 unsigned int start_idx)
608{
609 unsigned int i;
610
611 for (i = start_idx; i < q->num_buffers; i++)
612 if (q->bufs[i]->copied_timestamp &&
613 q->bufs[i]->timestamp == timestamp)
614 return i;
615 return -1;
616}
617EXPORT_SYMBOL_GPL(vb2_find_timestamp);
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632int vb2_querybuf(struct vb2_queue *q, struct v4l2_buffer *b)
633{
634 struct vb2_buffer *vb;
635 int ret;
636
637 if (b->type != q->type) {
638 dprintk(1, "wrong buffer type\n");
639 return -EINVAL;
640 }
641
642 if (b->index >= q->num_buffers) {
643 dprintk(1, "buffer index out of range\n");
644 return -EINVAL;
645 }
646 vb = q->bufs[b->index];
647 ret = __verify_planes_array(vb, b);
648 if (!ret)
649 vb2_core_querybuf(q, b->index, b);
650 return ret;
651}
652EXPORT_SYMBOL(vb2_querybuf);
653
654static void fill_buf_caps(struct vb2_queue *q, u32 *caps)
655{
656 *caps = V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS;
657 if (q->io_modes & VB2_MMAP)
658 *caps |= V4L2_BUF_CAP_SUPPORTS_MMAP;
659 if (q->io_modes & VB2_USERPTR)
660 *caps |= V4L2_BUF_CAP_SUPPORTS_USERPTR;
661 if (q->io_modes & VB2_DMABUF)
662 *caps |= V4L2_BUF_CAP_SUPPORTS_DMABUF;
663 if (q->subsystem_flags & VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
664 *caps |= V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
665#ifdef CONFIG_MEDIA_CONTROLLER_REQUEST_API
666 if (q->supports_requests)
667 *caps |= V4L2_BUF_CAP_SUPPORTS_REQUESTS;
668#endif
669}
670
671int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
672{
673 int ret = vb2_verify_memory_type(q, req->memory, req->type);
674
675 fill_buf_caps(q, &req->capabilities);
676 return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
677}
678EXPORT_SYMBOL_GPL(vb2_reqbufs);
679
680int vb2_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
681 struct v4l2_buffer *b)
682{
683 int ret;
684
685 if (vb2_fileio_is_active(q)) {
686 dprintk(1, "file io in progress\n");
687 return -EBUSY;
688 }
689
690 if (b->flags & V4L2_BUF_FLAG_REQUEST_FD)
691 return -EINVAL;
692
693 ret = vb2_queue_or_prepare_buf(q, mdev, b, true, NULL);
694
695 return ret ? ret : vb2_core_prepare_buf(q, b->index, b);
696}
697EXPORT_SYMBOL_GPL(vb2_prepare_buf);
698
699int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
700{
701 unsigned requested_planes = 1;
702 unsigned requested_sizes[VIDEO_MAX_PLANES];
703 struct v4l2_format *f = &create->format;
704 int ret = vb2_verify_memory_type(q, create->memory, f->type);
705 unsigned i;
706
707 fill_buf_caps(q, &create->capabilities);
708 create->index = q->num_buffers;
709 if (create->count == 0)
710 return ret != -EBUSY ? ret : 0;
711
712 switch (f->type) {
713 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
714 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
715 requested_planes = f->fmt.pix_mp.num_planes;
716 if (requested_planes == 0 ||
717 requested_planes > VIDEO_MAX_PLANES)
718 return -EINVAL;
719 for (i = 0; i < requested_planes; i++)
720 requested_sizes[i] =
721 f->fmt.pix_mp.plane_fmt[i].sizeimage;
722 break;
723 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
724 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
725 requested_sizes[0] = f->fmt.pix.sizeimage;
726 break;
727 case V4L2_BUF_TYPE_VBI_CAPTURE:
728 case V4L2_BUF_TYPE_VBI_OUTPUT:
729 requested_sizes[0] = f->fmt.vbi.samples_per_line *
730 (f->fmt.vbi.count[0] + f->fmt.vbi.count[1]);
731 break;
732 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
733 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
734 requested_sizes[0] = f->fmt.sliced.io_size;
735 break;
736 case V4L2_BUF_TYPE_SDR_CAPTURE:
737 case V4L2_BUF_TYPE_SDR_OUTPUT:
738 requested_sizes[0] = f->fmt.sdr.buffersize;
739 break;
740 case V4L2_BUF_TYPE_META_CAPTURE:
741 case V4L2_BUF_TYPE_META_OUTPUT:
742 requested_sizes[0] = f->fmt.meta.buffersize;
743 break;
744 default:
745 return -EINVAL;
746 }
747 for (i = 0; i < requested_planes; i++)
748 if (requested_sizes[i] == 0)
749 return -EINVAL;
750 return ret ? ret : vb2_core_create_bufs(q, create->memory,
751 &create->count, requested_planes, requested_sizes);
752}
753EXPORT_SYMBOL_GPL(vb2_create_bufs);
754
755int vb2_qbuf(struct vb2_queue *q, struct media_device *mdev,
756 struct v4l2_buffer *b)
757{
758 struct media_request *req = NULL;
759 int ret;
760
761 if (vb2_fileio_is_active(q)) {
762 dprintk(1, "file io in progress\n");
763 return -EBUSY;
764 }
765
766 ret = vb2_queue_or_prepare_buf(q, mdev, b, false, &req);
767 if (ret)
768 return ret;
769 ret = vb2_core_qbuf(q, b->index, b, req);
770 if (req)
771 media_request_put(req);
772 return ret;
773}
774EXPORT_SYMBOL_GPL(vb2_qbuf);
775
776int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)
777{
778 int ret;
779
780 if (vb2_fileio_is_active(q)) {
781 dprintk(1, "file io in progress\n");
782 return -EBUSY;
783 }
784
785 if (b->type != q->type) {
786 dprintk(1, "invalid buffer type\n");
787 return -EINVAL;
788 }
789
790 ret = vb2_core_dqbuf(q, NULL, b, nonblocking);
791
792 if (!q->is_output &&
793 b->flags & V4L2_BUF_FLAG_DONE &&
794 b->flags & V4L2_BUF_FLAG_LAST)
795 q->last_buffer_dequeued = true;
796
797
798
799
800
801 b->flags &= ~V4L2_BUF_FLAG_DONE;
802
803 return ret;
804}
805EXPORT_SYMBOL_GPL(vb2_dqbuf);
806
807int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
808{
809 if (vb2_fileio_is_active(q)) {
810 dprintk(1, "file io in progress\n");
811 return -EBUSY;
812 }
813 return vb2_core_streamon(q, type);
814}
815EXPORT_SYMBOL_GPL(vb2_streamon);
816
817int vb2_streamoff(struct vb2_queue *q, enum v4l2_buf_type type)
818{
819 if (vb2_fileio_is_active(q)) {
820 dprintk(1, "file io in progress\n");
821 return -EBUSY;
822 }
823 return vb2_core_streamoff(q, type);
824}
825EXPORT_SYMBOL_GPL(vb2_streamoff);
826
827int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb)
828{
829 return vb2_core_expbuf(q, &eb->fd, eb->type, eb->index,
830 eb->plane, eb->flags);
831}
832EXPORT_SYMBOL_GPL(vb2_expbuf);
833
834int vb2_queue_init(struct vb2_queue *q)
835{
836
837
838
839 if (WARN_ON(!q) ||
840 WARN_ON(q->timestamp_flags &
841 ~(V4L2_BUF_FLAG_TIMESTAMP_MASK |
842 V4L2_BUF_FLAG_TSTAMP_SRC_MASK)))
843 return -EINVAL;
844
845
846 WARN_ON((q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) ==
847 V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN);
848
849
850 if (WARN_ON(VB2_MEMORY_MMAP != (int)V4L2_MEMORY_MMAP)
851 || WARN_ON(VB2_MEMORY_USERPTR != (int)V4L2_MEMORY_USERPTR)
852 || WARN_ON(VB2_MEMORY_DMABUF != (int)V4L2_MEMORY_DMABUF))
853 return -EINVAL;
854
855 if (q->buf_struct_size == 0)
856 q->buf_struct_size = sizeof(struct vb2_v4l2_buffer);
857
858 q->buf_ops = &v4l2_buf_ops;
859 q->is_multiplanar = V4L2_TYPE_IS_MULTIPLANAR(q->type);
860 q->is_output = V4L2_TYPE_IS_OUTPUT(q->type);
861 q->copy_timestamp = (q->timestamp_flags & V4L2_BUF_FLAG_TIMESTAMP_MASK)
862 == V4L2_BUF_FLAG_TIMESTAMP_COPY;
863
864
865
866
867
868 q->quirk_poll_must_check_waiting_for_buffers = true;
869
870 return vb2_core_queue_init(q);
871}
872EXPORT_SYMBOL_GPL(vb2_queue_init);
873
874void vb2_queue_release(struct vb2_queue *q)
875{
876 vb2_core_queue_release(q);
877}
878EXPORT_SYMBOL_GPL(vb2_queue_release);
879
880__poll_t vb2_poll(struct vb2_queue *q, struct file *file, poll_table *wait)
881{
882 struct video_device *vfd = video_devdata(file);
883 __poll_t res;
884
885 res = vb2_core_poll(q, file, wait);
886
887 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
888 struct v4l2_fh *fh = file->private_data;
889
890 poll_wait(file, &fh->wait, wait);
891 if (v4l2_event_pending(fh))
892 res |= EPOLLPRI;
893 }
894
895 return res;
896}
897EXPORT_SYMBOL_GPL(vb2_poll);
898
899
900
901
902
903
904
905
906
907
908static inline bool vb2_queue_is_busy(struct video_device *vdev, struct file *file)
909{
910 return vdev->queue->owner && vdev->queue->owner != file->private_data;
911}
912
913
914
915int vb2_ioctl_reqbufs(struct file *file, void *priv,
916 struct v4l2_requestbuffers *p)
917{
918 struct video_device *vdev = video_devdata(file);
919 int res = vb2_verify_memory_type(vdev->queue, p->memory, p->type);
920
921 fill_buf_caps(vdev->queue, &p->capabilities);
922 if (res)
923 return res;
924 if (vb2_queue_is_busy(vdev, file))
925 return -EBUSY;
926 res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
927
928
929 if (res == 0)
930 vdev->queue->owner = p->count ? file->private_data : NULL;
931 return res;
932}
933EXPORT_SYMBOL_GPL(vb2_ioctl_reqbufs);
934
935int vb2_ioctl_create_bufs(struct file *file, void *priv,
936 struct v4l2_create_buffers *p)
937{
938 struct video_device *vdev = video_devdata(file);
939 int res = vb2_verify_memory_type(vdev->queue, p->memory,
940 p->format.type);
941
942 p->index = vdev->queue->num_buffers;
943 fill_buf_caps(vdev->queue, &p->capabilities);
944
945
946
947
948 if (p->count == 0)
949 return res != -EBUSY ? res : 0;
950 if (res)
951 return res;
952 if (vb2_queue_is_busy(vdev, file))
953 return -EBUSY;
954
955 res = vb2_create_bufs(vdev->queue, p);
956 if (res == 0)
957 vdev->queue->owner = file->private_data;
958 return res;
959}
960EXPORT_SYMBOL_GPL(vb2_ioctl_create_bufs);
961
962int vb2_ioctl_prepare_buf(struct file *file, void *priv,
963 struct v4l2_buffer *p)
964{
965 struct video_device *vdev = video_devdata(file);
966
967 if (vb2_queue_is_busy(vdev, file))
968 return -EBUSY;
969 return vb2_prepare_buf(vdev->queue, vdev->v4l2_dev->mdev, p);
970}
971EXPORT_SYMBOL_GPL(vb2_ioctl_prepare_buf);
972
973int vb2_ioctl_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
974{
975 struct video_device *vdev = video_devdata(file);
976
977
978 return vb2_querybuf(vdev->queue, p);
979}
980EXPORT_SYMBOL_GPL(vb2_ioctl_querybuf);
981
982int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
983{
984 struct video_device *vdev = video_devdata(file);
985
986 if (vb2_queue_is_busy(vdev, file))
987 return -EBUSY;
988 return vb2_qbuf(vdev->queue, vdev->v4l2_dev->mdev, p);
989}
990EXPORT_SYMBOL_GPL(vb2_ioctl_qbuf);
991
992int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
993{
994 struct video_device *vdev = video_devdata(file);
995
996 if (vb2_queue_is_busy(vdev, file))
997 return -EBUSY;
998 return vb2_dqbuf(vdev->queue, p, file->f_flags & O_NONBLOCK);
999}
1000EXPORT_SYMBOL_GPL(vb2_ioctl_dqbuf);
1001
1002int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1003{
1004 struct video_device *vdev = video_devdata(file);
1005
1006 if (vb2_queue_is_busy(vdev, file))
1007 return -EBUSY;
1008 return vb2_streamon(vdev->queue, i);
1009}
1010EXPORT_SYMBOL_GPL(vb2_ioctl_streamon);
1011
1012int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1013{
1014 struct video_device *vdev = video_devdata(file);
1015
1016 if (vb2_queue_is_busy(vdev, file))
1017 return -EBUSY;
1018 return vb2_streamoff(vdev->queue, i);
1019}
1020EXPORT_SYMBOL_GPL(vb2_ioctl_streamoff);
1021
1022int vb2_ioctl_expbuf(struct file *file, void *priv, struct v4l2_exportbuffer *p)
1023{
1024 struct video_device *vdev = video_devdata(file);
1025
1026 if (vb2_queue_is_busy(vdev, file))
1027 return -EBUSY;
1028 return vb2_expbuf(vdev->queue, p);
1029}
1030EXPORT_SYMBOL_GPL(vb2_ioctl_expbuf);
1031
1032
1033
1034int vb2_fop_mmap(struct file *file, struct vm_area_struct *vma)
1035{
1036 struct video_device *vdev = video_devdata(file);
1037
1038 return vb2_mmap(vdev->queue, vma);
1039}
1040EXPORT_SYMBOL_GPL(vb2_fop_mmap);
1041
1042int _vb2_fop_release(struct file *file, struct mutex *lock)
1043{
1044 struct video_device *vdev = video_devdata(file);
1045
1046 if (lock)
1047 mutex_lock(lock);
1048 if (file->private_data == vdev->queue->owner) {
1049 vb2_queue_release(vdev->queue);
1050 vdev->queue->owner = NULL;
1051 }
1052 if (lock)
1053 mutex_unlock(lock);
1054 return v4l2_fh_release(file);
1055}
1056EXPORT_SYMBOL_GPL(_vb2_fop_release);
1057
1058int vb2_fop_release(struct file *file)
1059{
1060 struct video_device *vdev = video_devdata(file);
1061 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1062
1063 return _vb2_fop_release(file, lock);
1064}
1065EXPORT_SYMBOL_GPL(vb2_fop_release);
1066
1067ssize_t vb2_fop_write(struct file *file, const char __user *buf,
1068 size_t count, loff_t *ppos)
1069{
1070 struct video_device *vdev = video_devdata(file);
1071 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1072 int err = -EBUSY;
1073
1074 if (!(vdev->queue->io_modes & VB2_WRITE))
1075 return -EINVAL;
1076 if (lock && mutex_lock_interruptible(lock))
1077 return -ERESTARTSYS;
1078 if (vb2_queue_is_busy(vdev, file))
1079 goto exit;
1080 err = vb2_write(vdev->queue, buf, count, ppos,
1081 file->f_flags & O_NONBLOCK);
1082 if (vdev->queue->fileio)
1083 vdev->queue->owner = file->private_data;
1084exit:
1085 if (lock)
1086 mutex_unlock(lock);
1087 return err;
1088}
1089EXPORT_SYMBOL_GPL(vb2_fop_write);
1090
1091ssize_t vb2_fop_read(struct file *file, char __user *buf,
1092 size_t count, loff_t *ppos)
1093{
1094 struct video_device *vdev = video_devdata(file);
1095 struct mutex *lock = vdev->queue->lock ? vdev->queue->lock : vdev->lock;
1096 int err = -EBUSY;
1097
1098 if (!(vdev->queue->io_modes & VB2_READ))
1099 return -EINVAL;
1100 if (lock && mutex_lock_interruptible(lock))
1101 return -ERESTARTSYS;
1102 if (vb2_queue_is_busy(vdev, file))
1103 goto exit;
1104 err = vb2_read(vdev->queue, buf, count, ppos,
1105 file->f_flags & O_NONBLOCK);
1106 if (vdev->queue->fileio)
1107 vdev->queue->owner = file->private_data;
1108exit:
1109 if (lock)
1110 mutex_unlock(lock);
1111 return err;
1112}
1113EXPORT_SYMBOL_GPL(vb2_fop_read);
1114
1115__poll_t vb2_fop_poll(struct file *file, poll_table *wait)
1116{
1117 struct video_device *vdev = video_devdata(file);
1118 struct vb2_queue *q = vdev->queue;
1119 struct mutex *lock = q->lock ? q->lock : vdev->lock;
1120 __poll_t res;
1121 void *fileio;
1122
1123
1124
1125
1126
1127 WARN_ON(!lock);
1128
1129 if (lock && mutex_lock_interruptible(lock))
1130 return EPOLLERR;
1131
1132 fileio = q->fileio;
1133
1134 res = vb2_poll(vdev->queue, file, wait);
1135
1136
1137 if (!fileio && q->fileio)
1138 q->owner = file->private_data;
1139 if (lock)
1140 mutex_unlock(lock);
1141 return res;
1142}
1143EXPORT_SYMBOL_GPL(vb2_fop_poll);
1144
1145#ifndef CONFIG_MMU
1146unsigned long vb2_fop_get_unmapped_area(struct file *file, unsigned long addr,
1147 unsigned long len, unsigned long pgoff, unsigned long flags)
1148{
1149 struct video_device *vdev = video_devdata(file);
1150
1151 return vb2_get_unmapped_area(vdev->queue, addr, len, pgoff, flags);
1152}
1153EXPORT_SYMBOL_GPL(vb2_fop_get_unmapped_area);
1154#endif
1155
1156
1157
1158void vb2_ops_wait_prepare(struct vb2_queue *vq)
1159{
1160 mutex_unlock(vq->lock);
1161}
1162EXPORT_SYMBOL_GPL(vb2_ops_wait_prepare);
1163
1164void vb2_ops_wait_finish(struct vb2_queue *vq)
1165{
1166 mutex_lock(vq->lock);
1167}
1168EXPORT_SYMBOL_GPL(vb2_ops_wait_finish);
1169
1170
1171
1172
1173
1174
1175
1176int vb2_request_validate(struct media_request *req)
1177{
1178 struct media_request_object *obj;
1179 int ret = 0;
1180
1181 if (!vb2_request_buffer_cnt(req))
1182 return -ENOENT;
1183
1184 list_for_each_entry(obj, &req->objects, list) {
1185 if (!obj->ops->prepare)
1186 continue;
1187
1188 ret = obj->ops->prepare(obj);
1189 if (ret)
1190 break;
1191 }
1192
1193 if (ret) {
1194 list_for_each_entry_continue_reverse(obj, &req->objects, list)
1195 if (obj->ops->unprepare)
1196 obj->ops->unprepare(obj);
1197 return ret;
1198 }
1199 return 0;
1200}
1201EXPORT_SYMBOL_GPL(vb2_request_validate);
1202
1203void vb2_request_queue(struct media_request *req)
1204{
1205 struct media_request_object *obj, *obj_safe;
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 list_for_each_entry_safe(obj, obj_safe, &req->objects, list)
1216 if (obj->ops->queue)
1217 obj->ops->queue(obj);
1218}
1219EXPORT_SYMBOL_GPL(vb2_request_queue);
1220
1221MODULE_DESCRIPTION("Driver helper framework for Video for Linux 2");
1222MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>, Marek Szyprowski");
1223MODULE_LICENSE("GPL");
1224