hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/drivers/media/common/videobuf2/videobuf2-core.c
....@@ -34,10 +34,11 @@
3434 static int debug;
3535 module_param(debug, int, 0644);
3636
37
-#define dprintk(level, fmt, arg...) \
38
- do { \
39
- if (debug >= level) \
40
- pr_info("%s: " fmt, __func__, ## arg); \
37
+#define dprintk(q, level, fmt, arg...) \
38
+ do { \
39
+ if (debug >= level) \
40
+ pr_info("[%s] %s: " fmt, (q)->name, __func__, \
41
+ ## arg); \
4142 } while (0)
4243
4344 #ifdef CONFIG_VIDEO_ADV_DEBUG
....@@ -51,8 +52,8 @@
5152 */
5253
5354 #define log_memop(vb, op) \
54
- dprintk(2, "call_memop(%p, %d, %s)%s\n", \
55
- (vb)->vb2_queue, (vb)->index, #op, \
55
+ dprintk((vb)->vb2_queue, 2, "call_memop(%d, %s)%s\n", \
56
+ (vb)->index, #op, \
5657 (vb)->vb2_queue->mem_ops->op ? "" : " (nop)")
5758
5859 #define call_memop(vb, op, args...) \
....@@ -90,7 +91,7 @@
9091 })
9192
9293 #define log_qop(q, op) \
93
- dprintk(2, "call_qop(%p, %s)%s\n", q, #op, \
94
+ dprintk(q, 2, "call_qop(%s)%s\n", #op, \
9495 (q)->ops->op ? "" : " (nop)")
9596
9697 #define call_qop(q, op, args...) \
....@@ -113,8 +114,8 @@
113114 })
114115
115116 #define log_vb_qop(vb, op, args...) \
116
- dprintk(2, "call_vb_qop(%p, %d, %s)%s\n", \
117
- (vb)->vb2_queue, (vb)->index, #op, \
117
+ dprintk((vb)->vb2_queue, 2, "call_vb_qop(%d, %s)%s\n", \
118
+ (vb)->index, #op, \
118119 (vb)->vb2_queue->ops->op ? "" : " (nop)")
119120
120121 #define call_vb_qop(vb, op, args...) \
....@@ -190,6 +191,23 @@
190191 static void __vb2_queue_cancel(struct vb2_queue *q);
191192 static void __enqueue_in_driver(struct vb2_buffer *vb);
192193
194
+static const char *vb2_state_name(enum vb2_buffer_state s)
195
+{
196
+ static const char * const state_names[] = {
197
+ [VB2_BUF_STATE_DEQUEUED] = "dequeued",
198
+ [VB2_BUF_STATE_IN_REQUEST] = "in request",
199
+ [VB2_BUF_STATE_PREPARING] = "preparing",
200
+ [VB2_BUF_STATE_QUEUED] = "queued",
201
+ [VB2_BUF_STATE_ACTIVE] = "active",
202
+ [VB2_BUF_STATE_DONE] = "done",
203
+ [VB2_BUF_STATE_ERROR] = "error",
204
+ };
205
+
206
+ if ((unsigned int)(s) < ARRAY_SIZE(state_names))
207
+ return state_names[s];
208
+ return "unknown";
209
+}
210
+
193211 /*
194212 * __vb2_buf_mem_alloc() - allocate video memory for the given buffer
195213 */
....@@ -205,6 +223,7 @@
205223 * NOTE: mmapped areas should be page aligned
206224 */
207225 for (plane = 0; plane < vb->num_planes; ++plane) {
226
+ /* Memops alloc requires size to be page aligned. */
208227 unsigned long size = PAGE_ALIGN(vb->planes[plane].length);
209228
210229 /* Did it wrap around? */
....@@ -245,7 +264,8 @@
245264 for (plane = 0; plane < vb->num_planes; ++plane) {
246265 call_void_memop(vb, put, vb->planes[plane].mem_priv);
247266 vb->planes[plane].mem_priv = NULL;
248
- dprintk(3, "freed plane %d of buffer %d\n", plane, vb->index);
267
+ dprintk(vb->vb2_queue, 3, "freed plane %d of buffer %d\n",
268
+ plane, vb->index);
249269 }
250270 }
251271
....@@ -296,6 +316,44 @@
296316 }
297317
298318 /*
319
+ * __vb2_buf_mem_prepare() - call ->prepare() on buffer's private memory
320
+ * to sync caches
321
+ */
322
+static void __vb2_buf_mem_prepare(struct vb2_buffer *vb)
323
+{
324
+ unsigned int plane;
325
+
326
+ if (vb->synced)
327
+ return;
328
+
329
+ if (vb->need_cache_sync_on_prepare) {
330
+ for (plane = 0; plane < vb->num_planes; ++plane)
331
+ call_void_memop(vb, prepare,
332
+ vb->planes[plane].mem_priv);
333
+ }
334
+ vb->synced = 1;
335
+}
336
+
337
+/*
338
+ * __vb2_buf_mem_finish() - call ->finish on buffer's private memory
339
+ * to sync caches
340
+ */
341
+static void __vb2_buf_mem_finish(struct vb2_buffer *vb)
342
+{
343
+ unsigned int plane;
344
+
345
+ if (!vb->synced)
346
+ return;
347
+
348
+ if (vb->need_cache_sync_on_finish) {
349
+ for (plane = 0; plane < vb->num_planes; ++plane)
350
+ call_void_memop(vb, finish,
351
+ vb->planes[plane].mem_priv);
352
+ }
353
+ vb->synced = 0;
354
+}
355
+
356
+/*
299357 * __setup_offsets() - setup unique offsets ("cookies") for every plane in
300358 * the buffer.
301359 */
....@@ -315,7 +373,7 @@
315373 for (plane = 0; plane < vb->num_planes; ++plane) {
316374 vb->planes[plane].m.offset = off;
317375
318
- dprintk(3, "buffer %d, plane %d offset 0x%08lx\n",
376
+ dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n",
319377 vb->index, plane, off);
320378
321379 off += vb->planes[plane].length;
....@@ -346,7 +404,7 @@
346404 /* Allocate videobuf buffer structures */
347405 vb = kzalloc(q->buf_struct_size, GFP_KERNEL);
348406 if (!vb) {
349
- dprintk(1, "memory alloc for buffer struct failed\n");
407
+ dprintk(q, 1, "memory alloc for buffer struct failed\n");
350408 break;
351409 }
352410
....@@ -356,17 +414,30 @@
356414 vb->index = q->num_buffers + buffer;
357415 vb->type = q->type;
358416 vb->memory = memory;
417
+ /*
418
+ * We need to set these flags here so that the videobuf2 core
419
+ * will call ->prepare()/->finish() cache sync/flush on vb2
420
+ * buffers when appropriate. However, we can avoid explicit
421
+ * ->prepare() and ->finish() cache sync for DMABUF buffers,
422
+ * because DMA exporter takes care of it.
423
+ */
424
+ if (q->memory != VB2_MEMORY_DMABUF) {
425
+ vb->need_cache_sync_on_prepare = 1;
426
+ vb->need_cache_sync_on_finish = 1;
427
+ }
359428 for (plane = 0; plane < num_planes; ++plane) {
360429 vb->planes[plane].length = plane_sizes[plane];
361430 vb->planes[plane].min_length = plane_sizes[plane];
362431 }
432
+ call_void_bufop(q, init_buffer, vb);
433
+
363434 q->bufs[vb->index] = vb;
364435
365436 /* Allocate video buffer memory for the MMAP type */
366437 if (memory == VB2_MEMORY_MMAP) {
367438 ret = __vb2_buf_mem_alloc(vb);
368439 if (ret) {
369
- dprintk(1, "failed allocating memory for buffer %d\n",
440
+ dprintk(q, 1, "failed allocating memory for buffer %d\n",
370441 buffer);
371442 q->bufs[vb->index] = NULL;
372443 kfree(vb);
....@@ -380,7 +451,7 @@
380451 */
381452 ret = call_vb_qop(vb, buf_init, vb);
382453 if (ret) {
383
- dprintk(1, "buffer %d %p initialization failed\n",
454
+ dprintk(q, 1, "buffer %d %p initialization failed\n",
384455 buffer, vb);
385456 __vb2_buf_mem_free(vb);
386457 q->bufs[vb->index] = NULL;
....@@ -390,8 +461,8 @@
390461 }
391462 }
392463
393
- dprintk(1, "allocated %d buffers, %d plane(s) each\n",
394
- buffer, num_planes);
464
+ dprintk(q, 3, "allocated %d buffers, %d plane(s) each\n",
465
+ buffer, num_planes);
395466
396467 return buffer;
397468 }
....@@ -442,7 +513,7 @@
442513 if (q->bufs[buffer] == NULL)
443514 continue;
444515 if (q->bufs[buffer]->state == VB2_BUF_STATE_PREPARING) {
445
- dprintk(1, "preparing buffers, cannot free\n");
516
+ dprintk(q, 1, "preparing buffers, cannot free\n");
446517 return -EAGAIN;
447518 }
448519 }
....@@ -501,8 +572,9 @@
501572 pr_info(" buf_init: %u buf_cleanup: %u buf_prepare: %u buf_finish: %u\n",
502573 vb->cnt_buf_init, vb->cnt_buf_cleanup,
503574 vb->cnt_buf_prepare, vb->cnt_buf_finish);
504
- pr_info(" buf_queue: %u buf_done: %u\n",
505
- vb->cnt_buf_queue, vb->cnt_buf_done);
575
+ pr_info(" buf_out_validate: %u buf_queue: %u buf_done: %u buf_request_complete: %u\n",
576
+ vb->cnt_buf_out_validate, vb->cnt_buf_queue,
577
+ vb->cnt_buf_done, vb->cnt_buf_request_complete);
506578 pr_info(" alloc: %u put: %u prepare: %u finish: %u mmap: %u\n",
507579 vb->cnt_mem_alloc, vb->cnt_mem_put,
508580 vb->cnt_mem_prepare, vb->cnt_mem_finish,
....@@ -619,12 +691,12 @@
619691 {
620692 if (memory != VB2_MEMORY_MMAP && memory != VB2_MEMORY_USERPTR &&
621693 memory != VB2_MEMORY_DMABUF) {
622
- dprintk(1, "unsupported memory type\n");
694
+ dprintk(q, 1, "unsupported memory type\n");
623695 return -EINVAL;
624696 }
625697
626698 if (type != q->type) {
627
- dprintk(1, "requested type is incorrect\n");
699
+ dprintk(q, 1, "requested type is incorrect\n");
628700 return -EINVAL;
629701 }
630702
....@@ -633,17 +705,17 @@
633705 * are available.
634706 */
635707 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) {
636
- dprintk(1, "MMAP for current setup unsupported\n");
708
+ dprintk(q, 1, "MMAP for current setup unsupported\n");
637709 return -EINVAL;
638710 }
639711
640712 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) {
641
- dprintk(1, "USERPTR for current setup unsupported\n");
713
+ dprintk(q, 1, "USERPTR for current setup unsupported\n");
642714 return -EINVAL;
643715 }
644716
645717 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) {
646
- dprintk(1, "DMABUF for current setup unsupported\n");
718
+ dprintk(q, 1, "DMABUF for current setup unsupported\n");
647719 return -EINVAL;
648720 }
649721
....@@ -653,7 +725,7 @@
653725 * do the memory and type validation.
654726 */
655727 if (vb2_fileio_is_active(q)) {
656
- dprintk(1, "file io in progress\n");
728
+ dprintk(q, 1, "file io in progress\n");
657729 return -EBUSY;
658730 }
659731 return 0;
....@@ -661,19 +733,20 @@
661733 EXPORT_SYMBOL(vb2_verify_memory_type);
662734
663735 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
664
- unsigned int *count)
736
+ unsigned int *count)
665737 {
666738 unsigned int num_buffers, allocated_buffers, num_planes = 0;
667739 unsigned plane_sizes[VB2_MAX_PLANES] = { };
740
+ unsigned int i;
668741 int ret;
669742
670743 if (q->streaming) {
671
- dprintk(1, "streaming active\n");
744
+ dprintk(q, 1, "streaming active\n");
672745 return -EBUSY;
673746 }
674747
675748 if (q->waiting_in_dqbuf && *count) {
676
- dprintk(1, "another dup()ped fd is waiting for a buffer\n");
749
+ dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
677750 return -EBUSY;
678751 }
679752
....@@ -684,14 +757,12 @@
684757 * are not in use and can be freed.
685758 */
686759 mutex_lock(&q->mmap_lock);
687
- if (q->memory == VB2_MEMORY_MMAP && __buffers_in_use(q)) {
688
- mutex_unlock(&q->mmap_lock);
689
- dprintk(1, "memory in use, cannot free\n");
690
- return -EBUSY;
691
- }
760
+ if (debug && q->memory == VB2_MEMORY_MMAP &&
761
+ __buffers_in_use(q))
762
+ dprintk(q, 1, "memory in use, orphaning buffers\n");
692763
693764 /*
694
- * Call queue_cancel to clean up any buffers in the PREPARED or
765
+ * Call queue_cancel to clean up any buffers in the
695766 * QUEUED state which is possible if buffers were prepared or
696767 * queued without ever calling STREAMON.
697768 */
....@@ -716,7 +787,13 @@
716787 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed);
717788 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
718789 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
790
+ /*
791
+ * Set this now to ensure that drivers see the correct q->memory value
792
+ * in the queue_setup op.
793
+ */
794
+ mutex_lock(&q->mmap_lock);
719795 q->memory = memory;
796
+ mutex_unlock(&q->mmap_lock);
720797
721798 /*
722799 * Ask the driver how many buffers and planes per buffer it requires.
....@@ -725,14 +802,27 @@
725802 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes,
726803 plane_sizes, q->alloc_devs);
727804 if (ret)
728
- return ret;
805
+ goto error;
806
+
807
+ /* Check that driver has set sane values */
808
+ if (WARN_ON(!num_planes)) {
809
+ ret = -EINVAL;
810
+ goto error;
811
+ }
812
+
813
+ for (i = 0; i < num_planes; i++)
814
+ if (WARN_ON(!plane_sizes[i])) {
815
+ ret = -EINVAL;
816
+ goto error;
817
+ }
729818
730819 /* Finally, allocate buffers and video memory */
731820 allocated_buffers =
732821 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes);
733822 if (allocated_buffers == 0) {
734
- dprintk(1, "memory allocation failed\n");
735
- return -ENOMEM;
823
+ dprintk(q, 1, "memory allocation failed\n");
824
+ ret = -ENOMEM;
825
+ goto error;
736826 }
737827
738828 /*
....@@ -773,7 +863,8 @@
773863 if (ret < 0) {
774864 /*
775865 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
776
- * from q->num_buffers.
866
+ * from q->num_buffers and it will reset q->memory to
867
+ * VB2_MEMORY_UNKNOWN.
777868 */
778869 __vb2_queue_free(q, allocated_buffers);
779870 mutex_unlock(&q->mmap_lock);
....@@ -789,33 +880,49 @@
789880 q->waiting_for_buffers = !q->is_output;
790881
791882 return 0;
883
+
884
+error:
885
+ mutex_lock(&q->mmap_lock);
886
+ q->memory = VB2_MEMORY_UNKNOWN;
887
+ mutex_unlock(&q->mmap_lock);
888
+ return ret;
792889 }
793890 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
794891
795892 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
796
- unsigned int *count, unsigned requested_planes,
797
- const unsigned requested_sizes[])
893
+ unsigned int *count,
894
+ unsigned int requested_planes,
895
+ const unsigned int requested_sizes[])
798896 {
799897 unsigned int num_planes = 0, num_buffers, allocated_buffers;
800898 unsigned plane_sizes[VB2_MAX_PLANES] = { };
899
+ bool no_previous_buffers = !q->num_buffers;
801900 int ret;
802901
803902 if (q->num_buffers == VB2_MAX_FRAME) {
804
- dprintk(1, "maximum number of buffers already allocated\n");
903
+ dprintk(q, 1, "maximum number of buffers already allocated\n");
805904 return -ENOBUFS;
806905 }
807906
808
- if (!q->num_buffers) {
907
+ if (no_previous_buffers) {
809908 if (q->waiting_in_dqbuf && *count) {
810
- dprintk(1, "another dup()ped fd is waiting for a buffer\n");
909
+ dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
811910 return -EBUSY;
812911 }
813912 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
913
+ /*
914
+ * Set this now to ensure that drivers see the correct q->memory
915
+ * value in the queue_setup op.
916
+ */
917
+ mutex_lock(&q->mmap_lock);
814918 q->memory = memory;
919
+ mutex_unlock(&q->mmap_lock);
815920 q->waiting_for_buffers = !q->is_output;
816
- } else if (q->memory != memory) {
817
- dprintk(1, "memory model mismatch\n");
818
- return -EINVAL;
921
+ } else {
922
+ if (q->memory != memory) {
923
+ dprintk(q, 1, "memory model mismatch\n");
924
+ return -EINVAL;
925
+ }
819926 }
820927
821928 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
....@@ -832,14 +939,15 @@
832939 ret = call_qop(q, queue_setup, q, &num_buffers,
833940 &num_planes, plane_sizes, q->alloc_devs);
834941 if (ret)
835
- return ret;
942
+ goto error;
836943
837944 /* Finally, allocate buffers and video memory */
838945 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers,
839946 num_planes, plane_sizes);
840947 if (allocated_buffers == 0) {
841
- dprintk(1, "memory allocation failed\n");
842
- return -ENOMEM;
948
+ dprintk(q, 1, "memory allocation failed\n");
949
+ ret = -ENOMEM;
950
+ goto error;
843951 }
844952
845953 /*
....@@ -870,7 +978,8 @@
870978 if (ret < 0) {
871979 /*
872980 * Note: __vb2_queue_free() will subtract 'allocated_buffers'
873
- * from q->num_buffers.
981
+ * from q->num_buffers and it will reset q->memory to
982
+ * VB2_MEMORY_UNKNOWN.
874983 */
875984 __vb2_queue_free(q, allocated_buffers);
876985 mutex_unlock(&q->mmap_lock);
....@@ -885,6 +994,14 @@
885994 *count = allocated_buffers;
886995
887996 return 0;
997
+
998
+error:
999
+ if (no_previous_buffers) {
1000
+ mutex_lock(&q->mmap_lock);
1001
+ q->memory = VB2_MEMORY_UNKNOWN;
1002
+ mutex_unlock(&q->mmap_lock);
1003
+ }
1004
+ return ret;
8881005 }
8891006 EXPORT_SYMBOL_GPL(vb2_core_create_bufs);
8901007
....@@ -911,15 +1028,13 @@
9111028 {
9121029 struct vb2_queue *q = vb->vb2_queue;
9131030 unsigned long flags;
914
- unsigned int plane;
9151031
9161032 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
9171033 return;
9181034
9191035 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
9201036 state != VB2_BUF_STATE_ERROR &&
921
- state != VB2_BUF_STATE_QUEUED &&
922
- state != VB2_BUF_STATE_REQUEUEING))
1037
+ state != VB2_BUF_STATE_QUEUED))
9231038 state = VB2_BUF_STATE_ERROR;
9241039
9251040 #ifdef CONFIG_VIDEO_ADV_DEBUG
....@@ -929,19 +1044,14 @@
9291044 */
9301045 vb->cnt_buf_done++;
9311046 #endif
932
- dprintk(4, "done processing on buffer %d, state: %d\n",
933
- vb->index, state);
1047
+ dprintk(q, 4, "done processing on buffer %d, state: %s\n",
1048
+ vb->index, vb2_state_name(state));
9341049
935
- if ((state != VB2_BUF_STATE_QUEUED &&
936
- state != VB2_BUF_STATE_REQUEUEING) &&
937
- vb->need_cache_sync_on_finish) {
938
- for (plane = 0; plane < vb->num_planes; ++plane)
939
- call_void_memop(vb, finish, vb->planes[plane].mem_priv);
940
- }
1050
+ if (state != VB2_BUF_STATE_QUEUED)
1051
+ __vb2_buf_mem_finish(vb);
9411052
9421053 spin_lock_irqsave(&q->done_lock, flags);
943
- if (state == VB2_BUF_STATE_QUEUED ||
944
- state == VB2_BUF_STATE_REQUEUEING) {
1054
+ if (state == VB2_BUF_STATE_QUEUED) {
9451055 vb->state = VB2_BUF_STATE_QUEUED;
9461056 } else {
9471057 /* Add the buffer to the done buffers list */
....@@ -949,16 +1059,18 @@
9491059 vb->state = state;
9501060 }
9511061 atomic_dec(&q->owned_by_drv_count);
1062
+
1063
+ if (state != VB2_BUF_STATE_QUEUED && vb->req_obj.req) {
1064
+ media_request_object_unbind(&vb->req_obj);
1065
+ media_request_object_put(&vb->req_obj);
1066
+ }
1067
+
9521068 spin_unlock_irqrestore(&q->done_lock, flags);
9531069
9541070 trace_vb2_buf_done(q, vb);
9551071
9561072 switch (state) {
9571073 case VB2_BUF_STATE_QUEUED:
958
- return;
959
- case VB2_BUF_STATE_REQUEUEING:
960
- if (q->start_streaming_called)
961
- __enqueue_in_driver(vb);
9621074 return;
9631075 default:
9641076 /* Inform any processes that may be waiting for buffers */
....@@ -983,20 +1095,19 @@
9831095 /*
9841096 * __prepare_mmap() - prepare an MMAP buffer
9851097 */
986
-static int __prepare_mmap(struct vb2_buffer *vb, const void *pb)
1098
+static int __prepare_mmap(struct vb2_buffer *vb)
9871099 {
9881100 int ret = 0;
9891101
990
- if (pb)
991
- ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
992
- vb, pb, vb->planes);
1102
+ ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1103
+ vb, vb->planes);
9931104 return ret ? ret : call_vb_qop(vb, buf_prepare, vb);
9941105 }
9951106
9961107 /*
9971108 * __prepare_userptr() - prepare a USERPTR buffer
9981109 */
999
-static int __prepare_userptr(struct vb2_buffer *vb, const void *pb)
1110
+static int __prepare_userptr(struct vb2_buffer *vb)
10001111 {
10011112 struct vb2_plane planes[VB2_MAX_PLANES];
10021113 struct vb2_queue *q = vb->vb2_queue;
....@@ -1007,12 +1118,10 @@
10071118
10081119 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
10091120 /* Copy relevant information provided by the userspace */
1010
- if (pb) {
1011
- ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1012
- vb, pb, planes);
1013
- if (ret)
1014
- return ret;
1015
- }
1121
+ ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1122
+ vb, planes);
1123
+ if (ret)
1124
+ return ret;
10161125
10171126 for (plane = 0; plane < vb->num_planes; ++plane) {
10181127 /* Skip the plane if already verified */
....@@ -1021,12 +1130,12 @@
10211130 && vb->planes[plane].length == planes[plane].length)
10221131 continue;
10231132
1024
- dprintk(3, "userspace address for plane %d changed, reacquiring memory\n",
1133
+ dprintk(q, 3, "userspace address for plane %d changed, reacquiring memory\n",
10251134 plane);
10261135
10271136 /* Check if the provided plane buffer is large enough */
10281137 if (planes[plane].length < vb->planes[plane].min_length) {
1029
- dprintk(1, "provided buffer size %u is less than setup size %u for plane %d\n",
1138
+ dprintk(q, 1, "provided buffer size %u is less than setup size %u for plane %d\n",
10301139 planes[plane].length,
10311140 vb->planes[plane].min_length,
10321141 plane);
....@@ -1038,6 +1147,7 @@
10381147 if (vb->planes[plane].mem_priv) {
10391148 if (!reacquired) {
10401149 reacquired = true;
1150
+ vb->copied_timestamp = 0;
10411151 call_void_vb_qop(vb, buf_cleanup, vb);
10421152 }
10431153 call_void_memop(vb, put_userptr, vb->planes[plane].mem_priv);
....@@ -1055,7 +1165,7 @@
10551165 planes[plane].m.userptr,
10561166 planes[plane].length, q->dma_dir);
10571167 if (IS_ERR(mem_priv)) {
1058
- dprintk(1, "failed acquiring userspace memory for plane %d\n",
1168
+ dprintk(q, 1, "failed acquiring userspace memory for plane %d\n",
10591169 plane);
10601170 ret = PTR_ERR(mem_priv);
10611171 goto err;
....@@ -1082,14 +1192,14 @@
10821192 */
10831193 ret = call_vb_qop(vb, buf_init, vb);
10841194 if (ret) {
1085
- dprintk(1, "buffer initialization failed\n");
1195
+ dprintk(q, 1, "buffer initialization failed\n");
10861196 goto err;
10871197 }
10881198 }
10891199
10901200 ret = call_vb_qop(vb, buf_prepare, vb);
10911201 if (ret) {
1092
- dprintk(1, "buffer preparation failed\n");
1202
+ dprintk(q, 1, "buffer preparation failed\n");
10931203 call_void_vb_qop(vb, buf_cleanup, vb);
10941204 goto err;
10951205 }
....@@ -1112,7 +1222,7 @@
11121222 /*
11131223 * __prepare_dmabuf() - prepare a DMABUF buffer
11141224 */
1115
-static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb)
1225
+static int __prepare_dmabuf(struct vb2_buffer *vb)
11161226 {
11171227 struct vb2_plane planes[VB2_MAX_PLANES];
11181228 struct vb2_queue *q = vb->vb2_queue;
....@@ -1123,18 +1233,16 @@
11231233
11241234 memset(planes, 0, sizeof(planes[0]) * vb->num_planes);
11251235 /* Copy relevant information provided by the userspace */
1126
- if (pb) {
1127
- ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1128
- vb, pb, planes);
1129
- if (ret)
1130
- return ret;
1131
- }
1236
+ ret = call_bufop(vb->vb2_queue, fill_vb2_buffer,
1237
+ vb, planes);
1238
+ if (ret)
1239
+ return ret;
11321240
11331241 for (plane = 0; plane < vb->num_planes; ++plane) {
11341242 struct dma_buf *dbuf = dma_buf_get(planes[plane].m.fd);
11351243
11361244 if (IS_ERR_OR_NULL(dbuf)) {
1137
- dprintk(1, "invalid dmabuf fd for plane %d\n",
1245
+ dprintk(q, 1, "invalid dmabuf fd for plane %d\n",
11381246 plane);
11391247 ret = -EINVAL;
11401248 goto err;
....@@ -1145,7 +1253,7 @@
11451253 planes[plane].length = dbuf->size;
11461254
11471255 if (planes[plane].length < vb->planes[plane].min_length) {
1148
- dprintk(1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
1256
+ dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n",
11491257 planes[plane].length, plane,
11501258 vb->planes[plane].min_length);
11511259 dma_buf_put(dbuf);
....@@ -1160,10 +1268,11 @@
11601268 continue;
11611269 }
11621270
1163
- dprintk(3, "buffer for plane %d changed\n", plane);
1271
+ dprintk(q, 3, "buffer for plane %d changed\n", plane);
11641272
11651273 if (!reacquired) {
11661274 reacquired = true;
1275
+ vb->copied_timestamp = 0;
11671276 call_void_vb_qop(vb, buf_cleanup, vb);
11681277 }
11691278
....@@ -1179,7 +1288,7 @@
11791288 q->alloc_devs[plane] ? : q->dev,
11801289 dbuf, planes[plane].length, q->dma_dir);
11811290 if (IS_ERR(mem_priv)) {
1182
- dprintk(1, "failed to attach dmabuf\n");
1291
+ dprintk(q, 1, "failed to attach dmabuf\n");
11831292 ret = PTR_ERR(mem_priv);
11841293 dma_buf_put(dbuf);
11851294 goto err;
....@@ -1195,9 +1304,12 @@
11951304 * userspace knows sooner rather than later if the dma-buf map fails.
11961305 */
11971306 for (plane = 0; plane < vb->num_planes; ++plane) {
1307
+ if (vb->planes[plane].dbuf_mapped)
1308
+ continue;
1309
+
11981310 ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
11991311 if (ret) {
1200
- dprintk(1, "failed to map dmabuf for plane %d\n",
1312
+ dprintk(q, 1, "failed to map dmabuf for plane %d\n",
12011313 plane);
12021314 goto err;
12031315 }
....@@ -1222,14 +1334,14 @@
12221334 */
12231335 ret = call_vb_qop(vb, buf_init, vb);
12241336 if (ret) {
1225
- dprintk(1, "buffer initialization failed\n");
1337
+ dprintk(q, 1, "buffer initialization failed\n");
12261338 goto err;
12271339 }
12281340 }
12291341
12301342 ret = call_vb_qop(vb, buf_prepare, vb);
12311343 if (ret) {
1232
- dprintk(1, "buffer preparation failed\n");
1344
+ dprintk(q, 1, "buffer preparation failed\n");
12331345 call_void_vb_qop(vb, buf_cleanup, vb);
12341346 goto err;
12351347 }
....@@ -1257,50 +1369,148 @@
12571369 call_void_vb_qop(vb, buf_queue, vb);
12581370 }
12591371
1260
-static int __buf_prepare(struct vb2_buffer *vb, const void *pb)
1372
+static int __buf_prepare(struct vb2_buffer *vb)
12611373 {
12621374 struct vb2_queue *q = vb->vb2_queue;
1263
- unsigned int plane;
1375
+ enum vb2_buffer_state orig_state = vb->state;
12641376 int ret;
12651377
12661378 if (q->error) {
1267
- dprintk(1, "fatal error occurred on queue\n");
1379
+ dprintk(q, 1, "fatal error occurred on queue\n");
12681380 return -EIO;
1381
+ }
1382
+
1383
+ if (vb->prepared)
1384
+ return 0;
1385
+ WARN_ON(vb->synced);
1386
+
1387
+ if (q->is_output) {
1388
+ ret = call_vb_qop(vb, buf_out_validate, vb);
1389
+ if (ret) {
1390
+ dprintk(q, 1, "buffer validation failed\n");
1391
+ return ret;
1392
+ }
12691393 }
12701394
12711395 vb->state = VB2_BUF_STATE_PREPARING;
12721396
12731397 switch (q->memory) {
12741398 case VB2_MEMORY_MMAP:
1275
- ret = __prepare_mmap(vb, pb);
1399
+ ret = __prepare_mmap(vb);
12761400 break;
12771401 case VB2_MEMORY_USERPTR:
1278
- ret = __prepare_userptr(vb, pb);
1402
+ ret = __prepare_userptr(vb);
12791403 break;
12801404 case VB2_MEMORY_DMABUF:
1281
- ret = __prepare_dmabuf(vb, pb);
1405
+ ret = __prepare_dmabuf(vb);
12821406 break;
12831407 default:
12841408 WARN(1, "Invalid queue type\n");
12851409 ret = -EINVAL;
1410
+ break;
12861411 }
12871412
12881413 if (ret) {
1289
- dprintk(1, "buffer preparation failed: %d\n", ret);
1290
- vb->state = VB2_BUF_STATE_DEQUEUED;
1414
+ dprintk(q, 1, "buffer preparation failed: %d\n", ret);
1415
+ vb->state = orig_state;
12911416 return ret;
12921417 }
12931418
1294
- if (vb->need_cache_sync_on_prepare) {
1295
- for (plane = 0; plane < vb->num_planes; ++plane)
1296
- call_void_memop(vb, prepare,
1297
- vb->planes[plane].mem_priv);
1298
- }
1299
-
1300
- vb->state = VB2_BUF_STATE_PREPARED;
1419
+ __vb2_buf_mem_prepare(vb);
1420
+ vb->prepared = 1;
1421
+ vb->state = orig_state;
13011422
13021423 return 0;
13031424 }
1425
+
1426
+static int vb2_req_prepare(struct media_request_object *obj)
1427
+{
1428
+ struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1429
+ int ret;
1430
+
1431
+ if (WARN_ON(vb->state != VB2_BUF_STATE_IN_REQUEST))
1432
+ return -EINVAL;
1433
+
1434
+ mutex_lock(vb->vb2_queue->lock);
1435
+ ret = __buf_prepare(vb);
1436
+ mutex_unlock(vb->vb2_queue->lock);
1437
+ return ret;
1438
+}
1439
+
1440
+static void __vb2_dqbuf(struct vb2_buffer *vb);
1441
+
1442
+static void vb2_req_unprepare(struct media_request_object *obj)
1443
+{
1444
+ struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1445
+
1446
+ mutex_lock(vb->vb2_queue->lock);
1447
+ __vb2_dqbuf(vb);
1448
+ vb->state = VB2_BUF_STATE_IN_REQUEST;
1449
+ mutex_unlock(vb->vb2_queue->lock);
1450
+ WARN_ON(!vb->req_obj.req);
1451
+}
1452
+
1453
+int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1454
+ struct media_request *req);
1455
+
1456
+static void vb2_req_queue(struct media_request_object *obj)
1457
+{
1458
+ struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1459
+
1460
+ mutex_lock(vb->vb2_queue->lock);
1461
+ vb2_core_qbuf(vb->vb2_queue, vb->index, NULL, NULL);
1462
+ mutex_unlock(vb->vb2_queue->lock);
1463
+}
1464
+
1465
+static void vb2_req_unbind(struct media_request_object *obj)
1466
+{
1467
+ struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1468
+
1469
+ if (vb->state == VB2_BUF_STATE_IN_REQUEST)
1470
+ call_void_bufop(vb->vb2_queue, init_buffer, vb);
1471
+}
1472
+
1473
+static void vb2_req_release(struct media_request_object *obj)
1474
+{
1475
+ struct vb2_buffer *vb = container_of(obj, struct vb2_buffer, req_obj);
1476
+
1477
+ if (vb->state == VB2_BUF_STATE_IN_REQUEST) {
1478
+ vb->state = VB2_BUF_STATE_DEQUEUED;
1479
+ if (vb->request)
1480
+ media_request_put(vb->request);
1481
+ vb->request = NULL;
1482
+ }
1483
+}
1484
+
1485
+static const struct media_request_object_ops vb2_core_req_ops = {
1486
+ .prepare = vb2_req_prepare,
1487
+ .unprepare = vb2_req_unprepare,
1488
+ .queue = vb2_req_queue,
1489
+ .unbind = vb2_req_unbind,
1490
+ .release = vb2_req_release,
1491
+};
1492
+
1493
+bool vb2_request_object_is_buffer(struct media_request_object *obj)
1494
+{
1495
+ return obj->ops == &vb2_core_req_ops;
1496
+}
1497
+EXPORT_SYMBOL_GPL(vb2_request_object_is_buffer);
1498
+
1499
+unsigned int vb2_request_buffer_cnt(struct media_request *req)
1500
+{
1501
+ struct media_request_object *obj;
1502
+ unsigned long flags;
1503
+ unsigned int buffer_cnt = 0;
1504
+
1505
+ spin_lock_irqsave(&req->lock, flags);
1506
+ list_for_each_entry(obj, &req->objects, list)
1507
+ if (vb2_request_object_is_buffer(obj))
1508
+ buffer_cnt++;
1509
+ spin_unlock_irqrestore(&req->lock, flags);
1510
+
1511
+ return buffer_cnt;
1512
+}
1513
+EXPORT_SYMBOL_GPL(vb2_request_buffer_cnt);
13041514
13051515 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)
13061516 {
....@@ -1309,21 +1519,25 @@
13091519
13101520 vb = q->bufs[index];
13111521 if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1312
- dprintk(1, "invalid buffer state %d\n",
1313
- vb->state);
1522
+ dprintk(q, 1, "invalid buffer state %s\n",
1523
+ vb2_state_name(vb->state));
1524
+ return -EINVAL;
1525
+ }
1526
+ if (vb->prepared) {
1527
+ dprintk(q, 1, "buffer already prepared\n");
13141528 return -EINVAL;
13151529 }
13161530
1317
- ret = __buf_prepare(vb, pb);
1531
+ ret = __buf_prepare(vb);
13181532 if (ret)
13191533 return ret;
13201534
13211535 /* Fill buffer information for the userspace */
13221536 call_void_bufop(q, fill_user_buffer, vb, pb);
13231537
1324
- dprintk(2, "prepare of buffer %d succeeded\n", vb->index);
1538
+ dprintk(q, 2, "prepare of buffer %d succeeded\n", vb->index);
13251539
1326
- return ret;
1540
+ return 0;
13271541 }
13281542 EXPORT_SYMBOL_GPL(vb2_core_prepare_buf);
13291543
....@@ -1359,7 +1573,7 @@
13591573
13601574 q->start_streaming_called = 0;
13611575
1362
- dprintk(1, "driver refused to start streaming\n");
1576
+ dprintk(q, 1, "driver refused to start streaming\n");
13631577 /*
13641578 * If you see this warning, then the driver isn't cleaning up properly
13651579 * after a failed start_streaming(). See the start_streaming()
....@@ -1390,32 +1604,104 @@
13901604 return ret;
13911605 }
13921606
1393
-int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)
1607
+int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1608
+ struct media_request *req)
13941609 {
13951610 struct vb2_buffer *vb;
13961611 enum vb2_buffer_state orig_state;
13971612 int ret;
13981613
13991614 if (q->error) {
1400
- dprintk(1, "fatal error occurred on queue\n");
1615
+ dprintk(q, 1, "fatal error occurred on queue\n");
14011616 return -EIO;
14021617 }
14031618
14041619 vb = q->bufs[index];
14051620
1406
- switch (vb->state) {
1407
- case VB2_BUF_STATE_DEQUEUED:
1408
- ret = __buf_prepare(vb, pb);
1621
+ if (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1622
+ q->requires_requests) {
1623
+ dprintk(q, 1, "qbuf requires a request\n");
1624
+ return -EBADR;
1625
+ }
1626
+
1627
+ if ((req && q->uses_qbuf) ||
1628
+ (!req && vb->state != VB2_BUF_STATE_IN_REQUEST &&
1629
+ q->uses_requests)) {
1630
+ dprintk(q, 1, "queue in wrong mode (qbuf vs requests)\n");
1631
+ return -EBUSY;
1632
+ }
1633
+
1634
+ if (req) {
1635
+ int ret;
1636
+
1637
+ q->uses_requests = 1;
1638
+ if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1639
+ dprintk(q, 1, "buffer %d not in dequeued state\n",
1640
+ vb->index);
1641
+ return -EINVAL;
1642
+ }
1643
+
1644
+ if (q->is_output && !vb->prepared) {
1645
+ ret = call_vb_qop(vb, buf_out_validate, vb);
1646
+ if (ret) {
1647
+ dprintk(q, 1, "buffer validation failed\n");
1648
+ return ret;
1649
+ }
1650
+ }
1651
+
1652
+ media_request_object_init(&vb->req_obj);
1653
+
1654
+ /* Make sure the request is in a safe state for updating. */
1655
+ ret = media_request_lock_for_update(req);
14091656 if (ret)
14101657 return ret;
1411
- break;
1412
- case VB2_BUF_STATE_PREPARED:
1658
+ ret = media_request_object_bind(req, &vb2_core_req_ops,
1659
+ q, true, &vb->req_obj);
1660
+ media_request_unlock_for_update(req);
1661
+ if (ret)
1662
+ return ret;
1663
+
1664
+ vb->state = VB2_BUF_STATE_IN_REQUEST;
1665
+
1666
+ /*
1667
+ * Increment the refcount and store the request.
1668
+ * The request refcount is decremented again when the
1669
+ * buffer is dequeued. This is to prevent vb2_buffer_done()
1670
+ * from freeing the request from interrupt context, which can
1671
+ * happen if the application closed the request fd after
1672
+ * queueing the request.
1673
+ */
1674
+ media_request_get(req);
1675
+ vb->request = req;
1676
+
1677
+ /* Fill buffer information for the userspace */
1678
+ if (pb) {
1679
+ call_void_bufop(q, copy_timestamp, vb, pb);
1680
+ call_void_bufop(q, fill_user_buffer, vb, pb);
1681
+ }
1682
+
1683
+ dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
1684
+ return 0;
1685
+ }
1686
+
1687
+ if (vb->state != VB2_BUF_STATE_IN_REQUEST)
1688
+ q->uses_qbuf = 1;
1689
+
1690
+ switch (vb->state) {
1691
+ case VB2_BUF_STATE_DEQUEUED:
1692
+ case VB2_BUF_STATE_IN_REQUEST:
1693
+ if (!vb->prepared) {
1694
+ ret = __buf_prepare(vb);
1695
+ if (ret)
1696
+ return ret;
1697
+ }
14131698 break;
14141699 case VB2_BUF_STATE_PREPARING:
1415
- dprintk(1, "buffer still being prepared\n");
1700
+ dprintk(q, 1, "buffer still being prepared\n");
14161701 return -EINVAL;
14171702 default:
1418
- dprintk(1, "invalid buffer state %d\n", vb->state);
1703
+ dprintk(q, 1, "invalid buffer state %s\n",
1704
+ vb2_state_name(vb->state));
14191705 return -EINVAL;
14201706 }
14211707
....@@ -1467,7 +1753,7 @@
14671753 }
14681754 }
14691755
1470
- dprintk(2, "qbuf of buffer %d succeeded\n", vb->index);
1756
+ dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index);
14711757 return 0;
14721758 }
14731759 EXPORT_SYMBOL_GPL(vb2_core_qbuf);
....@@ -1493,22 +1779,22 @@
14931779 int ret;
14941780
14951781 if (q->waiting_in_dqbuf) {
1496
- dprintk(1, "another dup()ped fd is waiting for a buffer\n");
1782
+ dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n");
14971783 return -EBUSY;
14981784 }
14991785
15001786 if (!q->streaming) {
1501
- dprintk(1, "streaming off, will not wait for buffers\n");
1787
+ dprintk(q, 1, "streaming off, will not wait for buffers\n");
15021788 return -EINVAL;
15031789 }
15041790
15051791 if (q->error) {
1506
- dprintk(1, "Queue in error state, will not wait for buffers\n");
1792
+ dprintk(q, 1, "Queue in error state, will not wait for buffers\n");
15071793 return -EIO;
15081794 }
15091795
15101796 if (q->last_buffer_dequeued) {
1511
- dprintk(3, "last buffer dequeued already, will not wait for buffers\n");
1797
+ dprintk(q, 3, "last buffer dequeued already, will not wait for buffers\n");
15121798 return -EPIPE;
15131799 }
15141800
....@@ -1520,7 +1806,7 @@
15201806 }
15211807
15221808 if (nonblocking) {
1523
- dprintk(3, "nonblocking and no buffers to dequeue, will not wait\n");
1809
+ dprintk(q, 3, "nonblocking and no buffers to dequeue, will not wait\n");
15241810 return -EAGAIN;
15251811 }
15261812
....@@ -1535,7 +1821,7 @@
15351821 /*
15361822 * All locks have been released, it is safe to sleep now.
15371823 */
1538
- dprintk(3, "will sleep waiting for buffers\n");
1824
+ dprintk(q, 3, "will sleep waiting for buffers\n");
15391825 ret = wait_event_interruptible(q->done_wq,
15401826 !list_empty(&q->done_list) || !q->streaming ||
15411827 q->error);
....@@ -1547,7 +1833,7 @@
15471833 call_void_qop(q, wait_finish, q);
15481834 q->waiting_in_dqbuf = 0;
15491835 if (ret) {
1550
- dprintk(1, "sleep was interrupted\n");
1836
+ dprintk(q, 1, "sleep was interrupted\n");
15511837 return ret;
15521838 }
15531839 }
....@@ -1595,7 +1881,7 @@
15951881 int vb2_wait_for_all_buffers(struct vb2_queue *q)
15961882 {
15971883 if (!q->streaming) {
1598
- dprintk(1, "streaming off, will not wait for buffers\n");
1884
+ dprintk(q, 1, "streaming off, will not wait for buffers\n");
15991885 return -EINVAL;
16001886 }
16011887
....@@ -1611,7 +1897,6 @@
16111897 static void __vb2_dqbuf(struct vb2_buffer *vb)
16121898 {
16131899 struct vb2_queue *q = vb->vb2_queue;
1614
- unsigned int i;
16151900
16161901 /* nothing to do if the buffer is already dequeued */
16171902 if (vb->state == VB2_BUF_STATE_DEQUEUED)
....@@ -1619,14 +1904,7 @@
16191904
16201905 vb->state = VB2_BUF_STATE_DEQUEUED;
16211906
1622
- /* unmap DMABUF buffer */
1623
- if (q->memory == VB2_MEMORY_DMABUF)
1624
- for (i = 0; i < vb->num_planes; ++i) {
1625
- if (!vb->planes[i].dbuf_mapped)
1626
- continue;
1627
- call_void_memop(vb, unmap_dmabuf, vb->planes[i].mem_priv);
1628
- vb->planes[i].dbuf_mapped = 0;
1629
- }
1907
+ call_void_bufop(q, init_buffer, vb);
16301908 }
16311909
16321910 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,
....@@ -1641,17 +1919,19 @@
16411919
16421920 switch (vb->state) {
16431921 case VB2_BUF_STATE_DONE:
1644
- dprintk(3, "returning done buffer\n");
1922
+ dprintk(q, 3, "returning done buffer\n");
16451923 break;
16461924 case VB2_BUF_STATE_ERROR:
1647
- dprintk(3, "returning done buffer with errors\n");
1925
+ dprintk(q, 3, "returning done buffer with errors\n");
16481926 break;
16491927 default:
1650
- dprintk(1, "invalid buffer state\n");
1928
+ dprintk(q, 1, "invalid buffer state %s\n",
1929
+ vb2_state_name(vb->state));
16511930 return -EINVAL;
16521931 }
16531932
16541933 call_void_vb_qop(vb, buf_finish, vb);
1934
+ vb->prepared = 0;
16551935
16561936 if (pindex)
16571937 *pindex = vb->index;
....@@ -1669,8 +1949,16 @@
16691949 /* go back to dequeued state */
16701950 __vb2_dqbuf(vb);
16711951
1672
- dprintk(2, "dqbuf of buffer %d, with state %d\n",
1673
- vb->index, vb->state);
1952
+ if (WARN_ON(vb->req_obj.req)) {
1953
+ media_request_object_unbind(&vb->req_obj);
1954
+ media_request_object_put(&vb->req_obj);
1955
+ }
1956
+ if (vb->request)
1957
+ media_request_put(vb->request);
1958
+ vb->request = NULL;
1959
+
1960
+ dprintk(q, 2, "dqbuf of buffer %d, state: %s\n",
1961
+ vb->index, vb2_state_name(vb->state));
16741962
16751963 return 0;
16761964
....@@ -1715,6 +2003,8 @@
17152003 q->start_streaming_called = 0;
17162004 q->queued_count = 0;
17172005 q->error = 0;
2006
+ q->uses_requests = 0;
2007
+ q->uses_qbuf = 0;
17182008
17192009 /*
17202010 * Remove all buffers from videobuf's list...
....@@ -1739,22 +2029,42 @@
17392029 */
17402030 for (i = 0; i < q->num_buffers; ++i) {
17412031 struct vb2_buffer *vb = q->bufs[i];
2032
+ struct media_request *req = vb->req_obj.req;
17422033
1743
- if ((vb->state == VB2_BUF_STATE_PREPARED ||
1744
- vb->state == VB2_BUF_STATE_QUEUED) &&
1745
- vb->need_cache_sync_on_finish) {
1746
- unsigned int plane;
2034
+ /*
2035
+ * If a request is associated with this buffer, then
2036
+ * call buf_request_cancel() to give the driver to complete()
2037
+ * related request objects. Otherwise those objects would
2038
+ * never complete.
2039
+ */
2040
+ if (req) {
2041
+ enum media_request_state state;
2042
+ unsigned long flags;
17472043
1748
- for (plane = 0; plane < vb->num_planes; ++plane)
1749
- call_void_memop(vb, finish,
1750
- vb->planes[plane].mem_priv);
2044
+ spin_lock_irqsave(&req->lock, flags);
2045
+ state = req->state;
2046
+ spin_unlock_irqrestore(&req->lock, flags);
2047
+
2048
+ if (state == MEDIA_REQUEST_STATE_QUEUED)
2049
+ call_void_vb_qop(vb, buf_request_complete, vb);
17512050 }
17522051
1753
- if (vb->state != VB2_BUF_STATE_DEQUEUED) {
1754
- vb->state = VB2_BUF_STATE_PREPARED;
2052
+ __vb2_buf_mem_finish(vb);
2053
+
2054
+ if (vb->prepared) {
17552055 call_void_vb_qop(vb, buf_finish, vb);
2056
+ vb->prepared = 0;
17562057 }
17572058 __vb2_dqbuf(vb);
2059
+
2060
+ if (vb->req_obj.req) {
2061
+ media_request_object_unbind(&vb->req_obj);
2062
+ media_request_object_put(&vb->req_obj);
2063
+ }
2064
+ if (vb->request)
2065
+ media_request_put(vb->request);
2066
+ vb->request = NULL;
2067
+ vb->copied_timestamp = 0;
17582068 }
17592069 }
17602070
....@@ -1763,22 +2073,22 @@
17632073 int ret;
17642074
17652075 if (type != q->type) {
1766
- dprintk(1, "invalid stream type\n");
2076
+ dprintk(q, 1, "invalid stream type\n");
17672077 return -EINVAL;
17682078 }
17692079
17702080 if (q->streaming) {
1771
- dprintk(3, "already streaming\n");
2081
+ dprintk(q, 3, "already streaming\n");
17722082 return 0;
17732083 }
17742084
17752085 if (!q->num_buffers) {
1776
- dprintk(1, "no buffers have been allocated\n");
2086
+ dprintk(q, 1, "no buffers have been allocated\n");
17772087 return -EINVAL;
17782088 }
17792089
17802090 if (q->num_buffers < q->min_buffers_needed) {
1781
- dprintk(1, "need at least %u allocated buffers\n",
2091
+ dprintk(q, 1, "need at least %u allocated buffers\n",
17822092 q->min_buffers_needed);
17832093 return -EINVAL;
17842094 }
....@@ -1798,7 +2108,7 @@
17982108
17992109 q->streaming = 1;
18002110
1801
- dprintk(3, "successful\n");
2111
+ dprintk(q, 3, "successful\n");
18022112 return 0;
18032113 }
18042114 EXPORT_SYMBOL_GPL(vb2_core_streamon);
....@@ -1814,7 +2124,7 @@
18142124 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type)
18152125 {
18162126 if (type != q->type) {
1817
- dprintk(1, "invalid stream type\n");
2127
+ dprintk(q, 1, "invalid stream type\n");
18182128 return -EINVAL;
18192129 }
18202130
....@@ -1831,7 +2141,7 @@
18312141 q->waiting_for_buffers = !q->is_output;
18322142 q->last_buffer_dequeued = false;
18332143
1834
- dprintk(3, "successful\n");
2144
+ dprintk(q, 3, "successful\n");
18352145 return 0;
18362146 }
18372147 EXPORT_SYMBOL_GPL(vb2_core_streamoff);
....@@ -1844,6 +2154,22 @@
18442154 {
18452155 struct vb2_buffer *vb;
18462156 unsigned int buffer, plane;
2157
+
2158
+ /*
2159
+ * Sanity checks to ensure the lock is held, MEMORY_MMAP is
2160
+ * used and fileio isn't active.
2161
+ */
2162
+ lockdep_assert_held(&q->mmap_lock);
2163
+
2164
+ if (q->memory != VB2_MEMORY_MMAP) {
2165
+ dprintk(q, 1, "queue is not currently set up for mmap\n");
2166
+ return -EINVAL;
2167
+ }
2168
+
2169
+ if (vb2_fileio_is_active(q)) {
2170
+ dprintk(q, 1, "file io in progress\n");
2171
+ return -EBUSY;
2172
+ }
18472173
18482174 /*
18492175 * Go over all buffers and their planes, comparing the given offset
....@@ -1874,39 +2200,39 @@
18742200 struct dma_buf *dbuf;
18752201
18762202 if (q->memory != VB2_MEMORY_MMAP) {
1877
- dprintk(1, "queue is not currently set up for mmap\n");
2203
+ dprintk(q, 1, "queue is not currently set up for mmap\n");
18782204 return -EINVAL;
18792205 }
18802206
18812207 if (!q->mem_ops->get_dmabuf) {
1882
- dprintk(1, "queue does not support DMA buffer exporting\n");
2208
+ dprintk(q, 1, "queue does not support DMA buffer exporting\n");
18832209 return -EINVAL;
18842210 }
18852211
18862212 if (flags & ~(O_CLOEXEC | O_ACCMODE)) {
1887
- dprintk(1, "queue does support only O_CLOEXEC and access mode flags\n");
2213
+ dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n");
18882214 return -EINVAL;
18892215 }
18902216
18912217 if (type != q->type) {
1892
- dprintk(1, "invalid buffer type\n");
2218
+ dprintk(q, 1, "invalid buffer type\n");
18932219 return -EINVAL;
18942220 }
18952221
18962222 if (index >= q->num_buffers) {
1897
- dprintk(1, "buffer index out of range\n");
2223
+ dprintk(q, 1, "buffer index out of range\n");
18982224 return -EINVAL;
18992225 }
19002226
19012227 vb = q->bufs[index];
19022228
19032229 if (plane >= vb->num_planes) {
1904
- dprintk(1, "buffer plane out of range\n");
2230
+ dprintk(q, 1, "buffer plane out of range\n");
19052231 return -EINVAL;
19062232 }
19072233
19082234 if (vb2_fileio_is_active(q)) {
1909
- dprintk(1, "expbuf: file io in progress\n");
2235
+ dprintk(q, 1, "expbuf: file io in progress\n");
19102236 return -EBUSY;
19112237 }
19122238
....@@ -1915,20 +2241,20 @@
19152241 dbuf = call_ptr_memop(vb, get_dmabuf, vb_plane->mem_priv,
19162242 flags & O_ACCMODE);
19172243 if (IS_ERR_OR_NULL(dbuf)) {
1918
- dprintk(1, "failed to export buffer %d, plane %d\n",
2244
+ dprintk(q, 1, "failed to export buffer %d, plane %d\n",
19192245 index, plane);
19202246 return -EINVAL;
19212247 }
19222248
19232249 ret = dma_buf_fd(dbuf, flags & ~O_ACCMODE);
19242250 if (ret < 0) {
1925
- dprintk(3, "buffer %d, plane %d failed to export (%d)\n",
2251
+ dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n",
19262252 index, plane, ret);
19272253 dma_buf_put(dbuf);
19282254 return ret;
19292255 }
19302256
1931
- dprintk(3, "buffer %d, plane %d exported as %d descriptor\n",
2257
+ dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n",
19322258 index, plane, ret);
19332259 *fd = ret;
19342260
....@@ -1944,40 +2270,30 @@
19442270 int ret;
19452271 unsigned long length;
19462272
1947
- if (q->memory != VB2_MEMORY_MMAP) {
1948
- dprintk(1, "queue is not currently set up for mmap\n");
1949
- return -EINVAL;
1950
- }
1951
-
19522273 /*
19532274 * Check memory area access mode.
19542275 */
19552276 if (!(vma->vm_flags & VM_SHARED)) {
1956
- dprintk(1, "invalid vma flags, VM_SHARED needed\n");
2277
+ dprintk(q, 1, "invalid vma flags, VM_SHARED needed\n");
19572278 return -EINVAL;
19582279 }
19592280 if (q->is_output) {
19602281 if (!(vma->vm_flags & VM_WRITE)) {
1961
- dprintk(1, "invalid vma flags, VM_WRITE needed\n");
2282
+ dprintk(q, 1, "invalid vma flags, VM_WRITE needed\n");
19622283 return -EINVAL;
19632284 }
19642285 } else {
19652286 if (!(vma->vm_flags & VM_READ)) {
1966
- dprintk(1, "invalid vma flags, VM_READ needed\n");
2287
+ dprintk(q, 1, "invalid vma flags, VM_READ needed\n");
19672288 return -EINVAL;
19682289 }
19692290 }
19702291
19712292 mutex_lock(&q->mmap_lock);
19722293
1973
- if (vb2_fileio_is_active(q)) {
1974
- dprintk(1, "mmap: file io in progress\n");
1975
- ret = -EBUSY;
1976
- goto unlock;
1977
- }
1978
-
19792294 /*
1980
- * Find the plane corresponding to the offset passed by userspace.
2295
+ * Find the plane corresponding to the offset passed by userspace. This
2296
+ * will return an error if not MEMORY_MMAP or file I/O is in progress.
19812297 */
19822298 ret = __find_plane_by_offset(q, off, &buffer, &plane);
19832299 if (ret)
....@@ -1992,11 +2308,18 @@
19922308 */
19932309 length = PAGE_ALIGN(vb->planes[plane].length);
19942310 if (length < (vma->vm_end - vma->vm_start)) {
1995
- dprintk(1,
2311
+ dprintk(q, 1,
19962312 "MMAP invalid, as it would overflow buffer length\n");
19972313 ret = -EINVAL;
19982314 goto unlock;
19992315 }
2316
+
2317
+ /*
2318
+ * vm_pgoff is treated in V4L2 API as a 'cookie' to select a buffer,
2319
+ * not as a in-buffer offset. We always want to mmap a whole buffer
2320
+ * from its beginning.
2321
+ */
2322
+ vma->vm_pgoff = 0;
20002323
20012324 ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma);
20022325
....@@ -2005,7 +2328,7 @@
20052328 if (ret)
20062329 return ret;
20072330
2008
- dprintk(3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
2331
+ dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane);
20092332 return 0;
20102333 }
20112334 EXPORT_SYMBOL_GPL(vb2_mmap);
....@@ -2023,22 +2346,25 @@
20232346 void *vaddr;
20242347 int ret;
20252348
2026
- if (q->memory != VB2_MEMORY_MMAP) {
2027
- dprintk(1, "queue is not currently set up for mmap\n");
2028
- return -EINVAL;
2029
- }
2349
+ mutex_lock(&q->mmap_lock);
20302350
20312351 /*
2032
- * Find the plane corresponding to the offset passed by userspace.
2352
+ * Find the plane corresponding to the offset passed by userspace. This
2353
+ * will return an error if not MEMORY_MMAP or file I/O is in progress.
20332354 */
20342355 ret = __find_plane_by_offset(q, off, &buffer, &plane);
20352356 if (ret)
2036
- return ret;
2357
+ goto unlock;
20372358
20382359 vb = q->bufs[buffer];
20392360
20402361 vaddr = vb2_plane_vaddr(vb, plane);
2362
+ mutex_unlock(&q->mmap_lock);
20412363 return vaddr ? (unsigned long)vaddr : -EINVAL;
2364
+
2365
+unlock:
2366
+ mutex_unlock(&q->mmap_lock);
2367
+ return ret;
20422368 }
20432369 EXPORT_SYMBOL_GPL(vb2_get_unmapped_area);
20442370 #endif
....@@ -2057,6 +2383,9 @@
20572383 WARN_ON(!q->ops->buf_queue))
20582384 return -EINVAL;
20592385
2386
+ if (WARN_ON(q->requires_requests && !q->supports_requests))
2387
+ return -EINVAL;
2388
+
20602389 INIT_LIST_HEAD(&q->queued_list);
20612390 INIT_LIST_HEAD(&q->done_list);
20622391 spin_lock_init(&q->done_lock);
....@@ -2072,6 +2401,10 @@
20722401 q->dma_dir = DMA_BIDIRECTIONAL;
20732402 else
20742403 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
2404
+
2405
+ if (q->name[0] == '\0')
2406
+ snprintf(q->name, sizeof(q->name), "%s-%p",
2407
+ q->is_output ? "out" : "cap", q);
20752408
20762409 return 0;
20772410 }
....@@ -2100,6 +2433,8 @@
21002433 return 0;
21012434 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM)))
21022435 return 0;
2436
+
2437
+ poll_wait(file, &q->done_wq, wait);
21032438
21042439 /*
21052440 * Start file I/O emulator only if streaming API has not been used yet.
....@@ -2152,8 +2487,6 @@
21522487 */
21532488 if (q->last_buffer_dequeued)
21542489 return EPOLLIN | EPOLLRDNORM;
2155
-
2156
- poll_wait(file, &q->done_wq, wait);
21572490 }
21582491
21592492 /*
....@@ -2261,7 +2594,7 @@
22612594 */
22622595 count = 1;
22632596
2264
- dprintk(3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
2597
+ dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n",
22652598 (read) ? "read" : "write", count, q->fileio_read_once,
22662599 q->fileio_write_immediately);
22672600
....@@ -2313,7 +2646,7 @@
23132646 * Queue all buffers.
23142647 */
23152648 for (i = 0; i < q->num_buffers; i++) {
2316
- ret = vb2_core_qbuf(q, i, NULL);
2649
+ ret = vb2_core_qbuf(q, i, NULL, NULL);
23172650 if (ret)
23182651 goto err_reqbufs;
23192652 fileio->bufs[i].queued = 1;
....@@ -2359,7 +2692,7 @@
23592692 fileio->count = 0;
23602693 vb2_core_reqbufs(q, fileio->memory, &fileio->count);
23612694 kfree(fileio);
2362
- dprintk(3, "file io emulator closed\n");
2695
+ dprintk(q, 3, "file io emulator closed\n");
23632696 }
23642697 return 0;
23652698 }
....@@ -2388,7 +2721,7 @@
23882721 unsigned index;
23892722 int ret;
23902723
2391
- dprintk(3, "mode %s, offset %ld, count %zd, %sblocking\n",
2724
+ dprintk(q, 3, "mode %s, offset %ld, count %zd, %sblocking\n",
23922725 read ? "read" : "write", (long)*ppos, count,
23932726 nonblock ? "non" : "");
23942727
....@@ -2396,7 +2729,7 @@
23962729 return -EINVAL;
23972730
23982731 if (q->waiting_in_dqbuf) {
2399
- dprintk(3, "another dup()ped fd is %s\n",
2732
+ dprintk(q, 3, "another dup()ped fd is %s\n",
24002733 read ? "reading" : "writing");
24012734 return -EBUSY;
24022735 }
....@@ -2406,7 +2739,7 @@
24062739 */
24072740 if (!vb2_fileio_is_active(q)) {
24082741 ret = __vb2_init_fileio(q, read);
2409
- dprintk(3, "vb2_init_fileio result: %d\n", ret);
2742
+ dprintk(q, 3, "vb2_init_fileio result: %d\n", ret);
24102743 if (ret)
24112744 return ret;
24122745 }
....@@ -2423,7 +2756,7 @@
24232756 * Call vb2_dqbuf to get buffer back.
24242757 */
24252758 ret = vb2_core_dqbuf(q, &index, NULL, nonblock);
2426
- dprintk(5, "vb2_dqbuf result: %d\n", ret);
2759
+ dprintk(q, 5, "vb2_dqbuf result: %d\n", ret);
24272760 if (ret)
24282761 return ret;
24292762 fileio->dq_count += 1;
....@@ -2454,20 +2787,20 @@
24542787 */
24552788 if (buf->pos + count > buf->size) {
24562789 count = buf->size - buf->pos;
2457
- dprintk(5, "reducing read count: %zd\n", count);
2790
+ dprintk(q, 5, "reducing read count: %zd\n", count);
24582791 }
24592792
24602793 /*
24612794 * Transfer data to userspace.
24622795 */
2463
- dprintk(3, "copying %zd bytes - buffer %d, offset %u\n",
2796
+ dprintk(q, 3, "copying %zd bytes - buffer %d, offset %u\n",
24642797 count, index, buf->pos);
24652798 if (read)
24662799 ret = copy_to_user(data, buf->vaddr + buf->pos, count);
24672800 else
24682801 ret = copy_from_user(buf->vaddr + buf->pos, data, count);
24692802 if (ret) {
2470
- dprintk(3, "error copying data\n");
2803
+ dprintk(q, 3, "error copying data\n");
24712804 return -EFAULT;
24722805 }
24732806
....@@ -2487,7 +2820,7 @@
24872820 * Check if this is the last buffer to read.
24882821 */
24892822 if (read && fileio->read_once && fileio->dq_count == 1) {
2490
- dprintk(3, "read limit reached\n");
2823
+ dprintk(q, 3, "read limit reached\n");
24912824 return __vb2_cleanup_fileio(q);
24922825 }
24932826
....@@ -2498,8 +2831,8 @@
24982831
24992832 if (copy_timestamp)
25002833 b->timestamp = ktime_get_ns();
2501
- ret = vb2_core_qbuf(q, index, NULL);
2502
- dprintk(5, "vb2_dbuf result: %d\n", ret);
2834
+ ret = vb2_core_qbuf(q, index, NULL, NULL);
2835
+ dprintk(q, 5, "vb2_dbuf result: %d\n", ret);
25032836 if (ret)
25042837 return ret;
25052838
....@@ -2586,7 +2919,7 @@
25862919 if (!threadio->stop)
25872920 ret = vb2_core_dqbuf(q, &index, NULL, 0);
25882921 call_void_qop(q, wait_prepare, q);
2589
- dprintk(5, "file io: vb2_dqbuf result: %d\n", ret);
2922
+ dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret);
25902923 if (!ret)
25912924 vb = q->bufs[index];
25922925 }
....@@ -2601,7 +2934,7 @@
26012934 if (copy_timestamp)
26022935 vb->timestamp = ktime_get_ns();
26032936 if (!threadio->stop)
2604
- ret = vb2_core_qbuf(q, vb->index, NULL);
2937
+ ret = vb2_core_qbuf(q, vb->index, NULL, NULL);
26052938 call_void_qop(q, wait_prepare, q);
26062939 if (ret || threadio->stop)
26072940 break;
....@@ -2640,7 +2973,7 @@
26402973 threadio->priv = priv;
26412974
26422975 ret = __vb2_init_fileio(q, !q->is_output);
2643
- dprintk(3, "file io: vb2_init_fileio result: %d\n", ret);
2976
+ dprintk(q, 3, "file io: vb2_init_fileio result: %d\n", ret);
26442977 if (ret)
26452978 goto nomem;
26462979 q->threadio = threadio;