Lines Matching refs:q

37 #define dprintk(q, level, fmt, arg...)					\  argument
40 pr_info("[%s] %s: " fmt, (q)->name, __func__, \
93 #define log_qop(q, op) \ argument
94 dprintk(q, 2, "call_qop(%s)%s\n", #op, \
95 (q)->ops->op ? "" : " (nop)")
97 #define call_qop(q, op, args...) \ argument
101 log_qop(q, op); \
102 err = (q)->ops->op ? (q)->ops->op(args) : 0; \
104 (q)->cnt_ ## op++; \
108 #define call_void_qop(q, op, args...) \ argument
110 log_qop(q, op); \
111 if ((q)->ops->op) \
112 (q)->ops->op(args); \
113 (q)->cnt_ ## op++; \
157 #define call_qop(q, op, args...) \ argument
158 ((q)->ops->op ? (q)->ops->op(args) : 0)
160 #define call_void_qop(q, op, args...) \ argument
162 if ((q)->ops->op) \
163 (q)->ops->op(args); \
177 #define call_bufop(q, op, args...) \ argument
180 if (q && q->buf_ops && q->buf_ops->op) \
181 ret = q->buf_ops->op(args); \
185 #define call_void_bufop(q, op, args...) \ argument
187 if (q && q->buf_ops && q->buf_ops->op) \
188 q->buf_ops->op(args); \
191 static void __vb2_queue_cancel(struct vb2_queue *q);
216 struct vb2_queue *q = vb->vb2_queue; in __vb2_buf_mem_alloc() local
235 q->alloc_devs[plane] ? : q->dev, in __vb2_buf_mem_alloc()
357 struct vb2_queue *q = vb->vb2_queue; in __setup_offsets() local
362 struct vb2_buffer *prev = q->bufs[vb->index - 1]; in __setup_offsets()
371 dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n", in __setup_offsets()
379 static void init_buffer_cache_hints(struct vb2_queue *q, struct vb2_buffer *vb) in init_buffer_cache_hints() argument
386 if (q->memory == VB2_MEMORY_DMABUF) { in init_buffer_cache_hints()
396 if (q->dma_dir == DMA_TO_DEVICE) in init_buffer_cache_hints()
407 static int __vb2_queue_alloc(struct vb2_queue *q, enum vb2_memory memory, in __vb2_queue_alloc() argument
417 VB2_MAX_FRAME - q->num_buffers); in __vb2_queue_alloc()
421 vb = kzalloc(q->buf_struct_size, GFP_KERNEL); in __vb2_queue_alloc()
423 dprintk(q, 1, "memory alloc for buffer struct failed\n"); in __vb2_queue_alloc()
428 vb->vb2_queue = q; in __vb2_queue_alloc()
430 vb->index = q->num_buffers + buffer; in __vb2_queue_alloc()
431 vb->type = q->type; in __vb2_queue_alloc()
433 init_buffer_cache_hints(q, vb); in __vb2_queue_alloc()
438 call_void_bufop(q, init_buffer, vb); in __vb2_queue_alloc()
440 q->bufs[vb->index] = vb; in __vb2_queue_alloc()
446 dprintk(q, 1, "failed allocating memory for buffer %d\n", in __vb2_queue_alloc()
448 q->bufs[vb->index] = NULL; in __vb2_queue_alloc()
460 dprintk(q, 1, "buffer %d %p initialization failed\n", in __vb2_queue_alloc()
463 q->bufs[vb->index] = NULL; in __vb2_queue_alloc()
470 dprintk(q, 3, "allocated %d buffers, %d plane(s) each\n", in __vb2_queue_alloc()
479 static void __vb2_free_mem(struct vb2_queue *q, unsigned int buffers) in __vb2_free_mem() argument
484 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_free_mem()
486 vb = q->bufs[buffer]; in __vb2_free_mem()
491 if (q->memory == VB2_MEMORY_MMAP) in __vb2_free_mem()
493 else if (q->memory == VB2_MEMORY_DMABUF) in __vb2_free_mem()
505 static void __vb2_queue_free(struct vb2_queue *q, unsigned int buffers) in __vb2_queue_free() argument
509 lockdep_assert_held(&q->mmap_lock); in __vb2_queue_free()
512 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_queue_free()
514 struct vb2_buffer *vb = q->bufs[buffer]; in __vb2_queue_free()
521 __vb2_free_mem(q, buffers); in __vb2_queue_free()
529 if (q->num_buffers) { in __vb2_queue_free()
530 bool unbalanced = q->cnt_start_streaming != q->cnt_stop_streaming || in __vb2_queue_free()
531 q->cnt_prepare_streaming != q->cnt_unprepare_streaming || in __vb2_queue_free()
532 q->cnt_wait_prepare != q->cnt_wait_finish; in __vb2_queue_free()
535 pr_info("counters for queue %p:%s\n", q, in __vb2_queue_free()
538 q->cnt_queue_setup, q->cnt_start_streaming, in __vb2_queue_free()
539 q->cnt_stop_streaming); in __vb2_queue_free()
541 q->cnt_prepare_streaming, q->cnt_unprepare_streaming); in __vb2_queue_free()
543 q->cnt_wait_prepare, q->cnt_wait_finish); in __vb2_queue_free()
545 q->cnt_queue_setup = 0; in __vb2_queue_free()
546 q->cnt_wait_prepare = 0; in __vb2_queue_free()
547 q->cnt_wait_finish = 0; in __vb2_queue_free()
548 q->cnt_prepare_streaming = 0; in __vb2_queue_free()
549 q->cnt_start_streaming = 0; in __vb2_queue_free()
550 q->cnt_stop_streaming = 0; in __vb2_queue_free()
551 q->cnt_unprepare_streaming = 0; in __vb2_queue_free()
553 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __vb2_queue_free()
554 struct vb2_buffer *vb = q->bufs[buffer]; in __vb2_queue_free()
566 q, buffer, unbalanced ? " UNBALANCED!" : ""); in __vb2_queue_free()
592 for (buffer = q->num_buffers - buffers; buffer < q->num_buffers; in __vb2_queue_free()
594 kfree(q->bufs[buffer]); in __vb2_queue_free()
595 q->bufs[buffer] = NULL; in __vb2_queue_free()
598 q->num_buffers -= buffers; in __vb2_queue_free()
599 if (!q->num_buffers) { in __vb2_queue_free()
600 q->memory = VB2_MEMORY_UNKNOWN; in __vb2_queue_free()
601 INIT_LIST_HEAD(&q->queued_list); in __vb2_queue_free()
605 bool vb2_buffer_in_use(struct vb2_queue *q, struct vb2_buffer *vb) in vb2_buffer_in_use() argument
627 static bool __buffers_in_use(struct vb2_queue *q) in __buffers_in_use() argument
630 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __buffers_in_use()
631 if (vb2_buffer_in_use(q, q->bufs[buffer])) in __buffers_in_use()
637 void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb) in vb2_core_querybuf() argument
639 call_void_bufop(q, fill_user_buffer, q->bufs[index], pb); in vb2_core_querybuf()
647 static int __verify_userptr_ops(struct vb2_queue *q) in __verify_userptr_ops() argument
649 if (!(q->io_modes & VB2_USERPTR) || !q->mem_ops->get_userptr || in __verify_userptr_ops()
650 !q->mem_ops->put_userptr) in __verify_userptr_ops()
660 static int __verify_mmap_ops(struct vb2_queue *q) in __verify_mmap_ops() argument
662 if (!(q->io_modes & VB2_MMAP) || !q->mem_ops->alloc || in __verify_mmap_ops()
663 !q->mem_ops->put || !q->mem_ops->mmap) in __verify_mmap_ops()
673 static int __verify_dmabuf_ops(struct vb2_queue *q) in __verify_dmabuf_ops() argument
675 if (!(q->io_modes & VB2_DMABUF) || !q->mem_ops->attach_dmabuf || in __verify_dmabuf_ops()
676 !q->mem_ops->detach_dmabuf || !q->mem_ops->map_dmabuf || in __verify_dmabuf_ops()
677 !q->mem_ops->unmap_dmabuf) in __verify_dmabuf_ops()
683 int vb2_verify_memory_type(struct vb2_queue *q, in vb2_verify_memory_type() argument
688 dprintk(q, 1, "unsupported memory type\n"); in vb2_verify_memory_type()
692 if (type != q->type) { in vb2_verify_memory_type()
693 dprintk(q, 1, "requested type is incorrect\n"); in vb2_verify_memory_type()
701 if (memory == VB2_MEMORY_MMAP && __verify_mmap_ops(q)) { in vb2_verify_memory_type()
702 dprintk(q, 1, "MMAP for current setup unsupported\n"); in vb2_verify_memory_type()
706 if (memory == VB2_MEMORY_USERPTR && __verify_userptr_ops(q)) { in vb2_verify_memory_type()
707 dprintk(q, 1, "USERPTR for current setup unsupported\n"); in vb2_verify_memory_type()
711 if (memory == VB2_MEMORY_DMABUF && __verify_dmabuf_ops(q)) { in vb2_verify_memory_type()
712 dprintk(q, 1, "DMABUF for current setup unsupported\n"); in vb2_verify_memory_type()
721 if (vb2_fileio_is_active(q)) { in vb2_verify_memory_type()
722 dprintk(q, 1, "file io in progress\n"); in vb2_verify_memory_type()
729 static void set_queue_coherency(struct vb2_queue *q, bool non_coherent_mem) in set_queue_coherency() argument
731 q->non_coherent_mem = 0; in set_queue_coherency()
733 if (!vb2_queue_allows_cache_hints(q)) in set_queue_coherency()
735 q->non_coherent_mem = non_coherent_mem; in set_queue_coherency()
738 static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem) in verify_coherency_flags() argument
740 if (non_coherent_mem != q->non_coherent_mem) { in verify_coherency_flags()
741 dprintk(q, 1, "memory coherency model mismatch\n"); in verify_coherency_flags()
747 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, in vb2_core_reqbufs() argument
756 if (q->streaming) { in vb2_core_reqbufs()
757 dprintk(q, 1, "streaming active\n"); in vb2_core_reqbufs()
761 if (q->waiting_in_dqbuf && *count) { in vb2_core_reqbufs()
762 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in vb2_core_reqbufs()
766 if (*count == 0 || q->num_buffers != 0 || in vb2_core_reqbufs()
767 (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) || in vb2_core_reqbufs()
768 !verify_coherency_flags(q, non_coherent_mem)) { in vb2_core_reqbufs()
773 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
774 if (debug && q->memory == VB2_MEMORY_MMAP && in vb2_core_reqbufs()
775 __buffers_in_use(q)) in vb2_core_reqbufs()
776 dprintk(q, 1, "memory in use, orphaning buffers\n"); in vb2_core_reqbufs()
783 __vb2_queue_cancel(q); in vb2_core_reqbufs()
784 __vb2_queue_free(q, q->num_buffers); in vb2_core_reqbufs()
785 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
798 WARN_ON(q->min_buffers_needed > VB2_MAX_FRAME); in vb2_core_reqbufs()
799 num_buffers = max_t(unsigned int, *count, q->min_buffers_needed); in vb2_core_reqbufs()
801 memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); in vb2_core_reqbufs()
806 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
807 q->memory = memory; in vb2_core_reqbufs()
808 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
809 set_queue_coherency(q, non_coherent_mem); in vb2_core_reqbufs()
815 ret = call_qop(q, queue_setup, q, &num_buffers, &num_planes, in vb2_core_reqbufs()
816 plane_sizes, q->alloc_devs); in vb2_core_reqbufs()
834 __vb2_queue_alloc(q, memory, num_buffers, num_planes, plane_sizes); in vb2_core_reqbufs()
836 dprintk(q, 1, "memory allocation failed\n"); in vb2_core_reqbufs()
845 if (allocated_buffers < q->min_buffers_needed) in vb2_core_reqbufs()
861 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_reqbufs()
862 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_reqbufs()
873 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
874 q->num_buffers = allocated_buffers; in vb2_core_reqbufs()
882 __vb2_queue_free(q, allocated_buffers); in vb2_core_reqbufs()
883 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
886 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
893 q->waiting_for_buffers = !q->is_output; in vb2_core_reqbufs()
898 mutex_lock(&q->mmap_lock); in vb2_core_reqbufs()
899 q->memory = VB2_MEMORY_UNKNOWN; in vb2_core_reqbufs()
900 mutex_unlock(&q->mmap_lock); in vb2_core_reqbufs()
905 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, in vb2_core_create_bufs() argument
913 bool no_previous_buffers = !q->num_buffers; in vb2_core_create_bufs()
916 if (q->num_buffers == VB2_MAX_FRAME) { in vb2_core_create_bufs()
917 dprintk(q, 1, "maximum number of buffers already allocated\n"); in vb2_core_create_bufs()
922 if (q->waiting_in_dqbuf && *count) { in vb2_core_create_bufs()
923 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in vb2_core_create_bufs()
926 memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); in vb2_core_create_bufs()
931 mutex_lock(&q->mmap_lock); in vb2_core_create_bufs()
932 q->memory = memory; in vb2_core_create_bufs()
933 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
934 q->waiting_for_buffers = !q->is_output; in vb2_core_create_bufs()
935 set_queue_coherency(q, non_coherent_mem); in vb2_core_create_bufs()
937 if (q->memory != memory) { in vb2_core_create_bufs()
938 dprintk(q, 1, "memory model mismatch\n"); in vb2_core_create_bufs()
941 if (!verify_coherency_flags(q, non_coherent_mem)) in vb2_core_create_bufs()
945 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers); in vb2_core_create_bufs()
956 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_create_bufs()
957 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_create_bufs()
962 allocated_buffers = __vb2_queue_alloc(q, memory, num_buffers, in vb2_core_create_bufs()
965 dprintk(q, 1, "memory allocation failed\n"); in vb2_core_create_bufs()
980 ret = call_qop(q, queue_setup, q, &num_buffers, in vb2_core_create_bufs()
981 &num_planes, plane_sizes, q->alloc_devs); in vb2_core_create_bufs()
992 mutex_lock(&q->mmap_lock); in vb2_core_create_bufs()
993 q->num_buffers += allocated_buffers; in vb2_core_create_bufs()
1001 __vb2_queue_free(q, allocated_buffers); in vb2_core_create_bufs()
1002 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
1005 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
1017 mutex_lock(&q->mmap_lock); in vb2_core_create_bufs()
1018 q->memory = VB2_MEMORY_UNKNOWN; in vb2_core_create_bufs()
1019 mutex_unlock(&q->mmap_lock); in vb2_core_create_bufs()
1046 struct vb2_queue *q = vb->vb2_queue; in vb2_buffer_done() local
1064 dprintk(q, 4, "done processing on buffer %d, state: %s\n", in vb2_buffer_done()
1070 spin_lock_irqsave(&q->done_lock, flags); in vb2_buffer_done()
1075 list_add_tail(&vb->done_entry, &q->done_list); in vb2_buffer_done()
1078 atomic_dec(&q->owned_by_drv_count); in vb2_buffer_done()
1085 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_buffer_done()
1087 trace_vb2_buf_done(q, vb); in vb2_buffer_done()
1094 wake_up(&q->done_wq); in vb2_buffer_done()
1100 void vb2_discard_done(struct vb2_queue *q) in vb2_discard_done() argument
1105 spin_lock_irqsave(&q->done_lock, flags); in vb2_discard_done()
1106 list_for_each_entry(vb, &q->done_list, done_entry) in vb2_discard_done()
1108 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_discard_done()
1130 struct vb2_queue *q = vb->vb2_queue; in __prepare_userptr() local
1150 dprintk(q, 3, "userspace address for plane %d changed, reacquiring memory\n", in __prepare_userptr()
1155 dprintk(q, 1, "provided buffer size %u is less than setup size %u for plane %d\n", in __prepare_userptr()
1182 q->alloc_devs[plane] ? : q->dev, in __prepare_userptr()
1186 dprintk(q, 1, "failed acquiring userspace memory for plane %d\n", in __prepare_userptr()
1213 dprintk(q, 1, "buffer initialization failed\n"); in __prepare_userptr()
1220 dprintk(q, 1, "buffer preparation failed\n"); in __prepare_userptr()
1246 struct vb2_queue *q = vb->vb2_queue; in __prepare_dmabuf() local
1263 dprintk(q, 1, "invalid dmabuf fd for plane %d\n", in __prepare_dmabuf()
1274 dprintk(q, 1, "invalid dmabuf length %u for plane %d, minimum length %u\n", in __prepare_dmabuf()
1289 dprintk(q, 3, "buffer for plane %d changed\n", plane); in __prepare_dmabuf()
1307 q->alloc_devs[plane] ? : q->dev, in __prepare_dmabuf()
1311 dprintk(q, 1, "failed to attach dmabuf\n"); in __prepare_dmabuf()
1332 dprintk(q, 1, "failed to map dmabuf for plane %d\n", in __prepare_dmabuf()
1357 dprintk(q, 1, "buffer initialization failed\n"); in __prepare_dmabuf()
1364 dprintk(q, 1, "buffer preparation failed\n"); in __prepare_dmabuf()
1382 struct vb2_queue *q = vb->vb2_queue; in __enqueue_in_driver() local
1385 atomic_inc(&q->owned_by_drv_count); in __enqueue_in_driver()
1387 trace_vb2_buf_queue(q, vb); in __enqueue_in_driver()
1394 struct vb2_queue *q = vb->vb2_queue; in __buf_prepare() local
1398 if (q->error) { in __buf_prepare()
1399 dprintk(q, 1, "fatal error occurred on queue\n"); in __buf_prepare()
1407 if (q->is_output) { in __buf_prepare()
1410 dprintk(q, 1, "buffer validation failed\n"); in __buf_prepare()
1417 switch (q->memory) { in __buf_prepare()
1434 dprintk(q, 1, "buffer preparation failed: %d\n", ret); in __buf_prepare()
1473 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb,
1545 int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb) in vb2_core_prepare_buf() argument
1550 vb = q->bufs[index]; in vb2_core_prepare_buf()
1552 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_prepare_buf()
1557 dprintk(q, 1, "buffer already prepared\n"); in vb2_core_prepare_buf()
1566 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_prepare_buf()
1568 dprintk(q, 2, "prepare of buffer %d succeeded\n", vb->index); in vb2_core_prepare_buf()
1585 static int vb2_start_streaming(struct vb2_queue *q) in vb2_start_streaming() argument
1594 list_for_each_entry(vb, &q->queued_list, queued_entry) in vb2_start_streaming()
1598 q->start_streaming_called = 1; in vb2_start_streaming()
1599 ret = call_qop(q, start_streaming, q, in vb2_start_streaming()
1600 atomic_read(&q->owned_by_drv_count)); in vb2_start_streaming()
1604 q->start_streaming_called = 0; in vb2_start_streaming()
1606 dprintk(q, 1, "driver refused to start streaming\n"); in vb2_start_streaming()
1613 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { in vb2_start_streaming()
1620 for (i = 0; i < q->num_buffers; ++i) { in vb2_start_streaming()
1621 vb = q->bufs[i]; in vb2_start_streaming()
1626 WARN_ON(atomic_read(&q->owned_by_drv_count)); in vb2_start_streaming()
1633 WARN_ON(!list_empty(&q->done_list)); in vb2_start_streaming()
1637 int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb, in vb2_core_qbuf() argument
1644 if (q->error) { in vb2_core_qbuf()
1645 dprintk(q, 1, "fatal error occurred on queue\n"); in vb2_core_qbuf()
1649 vb = q->bufs[index]; in vb2_core_qbuf()
1652 q->requires_requests) { in vb2_core_qbuf()
1653 dprintk(q, 1, "qbuf requires a request\n"); in vb2_core_qbuf()
1657 if ((req && q->uses_qbuf) || in vb2_core_qbuf()
1659 q->uses_requests)) { in vb2_core_qbuf()
1660 dprintk(q, 1, "queue in wrong mode (qbuf vs requests)\n"); in vb2_core_qbuf()
1667 q->uses_requests = 1; in vb2_core_qbuf()
1669 dprintk(q, 1, "buffer %d not in dequeued state\n", in vb2_core_qbuf()
1674 if (q->is_output && !vb->prepared) { in vb2_core_qbuf()
1677 dprintk(q, 1, "buffer validation failed\n"); in vb2_core_qbuf()
1689 q, true, &vb->req_obj); in vb2_core_qbuf()
1709 call_void_bufop(q, copy_timestamp, vb, pb); in vb2_core_qbuf()
1710 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_qbuf()
1713 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index); in vb2_core_qbuf()
1718 q->uses_qbuf = 1; in vb2_core_qbuf()
1730 dprintk(q, 1, "buffer still being prepared\n"); in vb2_core_qbuf()
1733 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_qbuf()
1743 list_add_tail(&vb->queued_entry, &q->queued_list); in vb2_core_qbuf()
1744 q->queued_count++; in vb2_core_qbuf()
1745 q->waiting_for_buffers = false; in vb2_core_qbuf()
1749 call_void_bufop(q, copy_timestamp, vb, pb); in vb2_core_qbuf()
1751 trace_vb2_qbuf(q, vb); in vb2_core_qbuf()
1757 if (q->start_streaming_called) in vb2_core_qbuf()
1762 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_qbuf()
1770 if (q->streaming && !q->start_streaming_called && in vb2_core_qbuf()
1771 q->queued_count >= q->min_buffers_needed) { in vb2_core_qbuf()
1772 ret = vb2_start_streaming(q); in vb2_core_qbuf()
1780 q->queued_count--; in vb2_core_qbuf()
1786 dprintk(q, 2, "qbuf of buffer %d succeeded\n", vb->index); in vb2_core_qbuf()
1797 static int __vb2_wait_for_done_vb(struct vb2_queue *q, int nonblocking) in __vb2_wait_for_done_vb() argument
1811 if (q->waiting_in_dqbuf) { in __vb2_wait_for_done_vb()
1812 dprintk(q, 1, "another dup()ped fd is waiting for a buffer\n"); in __vb2_wait_for_done_vb()
1816 if (!q->streaming) { in __vb2_wait_for_done_vb()
1817 dprintk(q, 1, "streaming off, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1821 if (q->error) { in __vb2_wait_for_done_vb()
1822 dprintk(q, 1, "Queue in error state, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1826 if (q->last_buffer_dequeued) { in __vb2_wait_for_done_vb()
1827 dprintk(q, 3, "last buffer dequeued already, will not wait for buffers\n"); in __vb2_wait_for_done_vb()
1831 if (!list_empty(&q->done_list)) { in __vb2_wait_for_done_vb()
1839 dprintk(q, 3, "nonblocking and no buffers to dequeue, will not wait\n"); in __vb2_wait_for_done_vb()
1843 q->waiting_in_dqbuf = 1; in __vb2_wait_for_done_vb()
1849 call_void_qop(q, wait_prepare, q); in __vb2_wait_for_done_vb()
1854 dprintk(q, 3, "will sleep waiting for buffers\n"); in __vb2_wait_for_done_vb()
1855 ret = wait_event_interruptible(q->done_wq, in __vb2_wait_for_done_vb()
1856 !list_empty(&q->done_list) || !q->streaming || in __vb2_wait_for_done_vb()
1857 q->error); in __vb2_wait_for_done_vb()
1863 call_void_qop(q, wait_finish, q); in __vb2_wait_for_done_vb()
1864 q->waiting_in_dqbuf = 0; in __vb2_wait_for_done_vb()
1866 dprintk(q, 1, "sleep was interrupted\n"); in __vb2_wait_for_done_vb()
1878 static int __vb2_get_done_vb(struct vb2_queue *q, struct vb2_buffer **vb, in __vb2_get_done_vb() argument
1887 ret = __vb2_wait_for_done_vb(q, nonblocking); in __vb2_get_done_vb()
1895 spin_lock_irqsave(&q->done_lock, flags); in __vb2_get_done_vb()
1896 *vb = list_first_entry(&q->done_list, struct vb2_buffer, done_entry); in __vb2_get_done_vb()
1903 ret = call_bufop(q, verify_planes_array, *vb, pb); in __vb2_get_done_vb()
1906 spin_unlock_irqrestore(&q->done_lock, flags); in __vb2_get_done_vb()
1911 int vb2_wait_for_all_buffers(struct vb2_queue *q) in vb2_wait_for_all_buffers() argument
1913 if (!q->streaming) { in vb2_wait_for_all_buffers()
1914 dprintk(q, 1, "streaming off, will not wait for buffers\n"); in vb2_wait_for_all_buffers()
1918 if (q->start_streaming_called) in vb2_wait_for_all_buffers()
1919 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count)); in vb2_wait_for_all_buffers()
1929 struct vb2_queue *q = vb->vb2_queue; in __vb2_dqbuf() local
1937 call_void_bufop(q, init_buffer, vb); in __vb2_dqbuf()
1940 int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb, in vb2_core_dqbuf() argument
1946 ret = __vb2_get_done_vb(q, &vb, pb, nonblocking); in vb2_core_dqbuf()
1952 dprintk(q, 3, "returning done buffer\n"); in vb2_core_dqbuf()
1955 dprintk(q, 3, "returning done buffer with errors\n"); in vb2_core_dqbuf()
1958 dprintk(q, 1, "invalid buffer state %s\n", in vb2_core_dqbuf()
1971 call_void_bufop(q, fill_user_buffer, vb, pb); in vb2_core_dqbuf()
1975 q->queued_count--; in vb2_core_dqbuf()
1977 trace_vb2_dqbuf(q, vb); in vb2_core_dqbuf()
1990 dprintk(q, 2, "dqbuf of buffer %d, state: %s\n", in vb2_core_dqbuf()
2004 static void __vb2_queue_cancel(struct vb2_queue *q) in __vb2_queue_cancel() argument
2012 if (q->start_streaming_called) in __vb2_queue_cancel()
2013 call_void_qop(q, stop_streaming, q); in __vb2_queue_cancel()
2015 if (q->streaming) in __vb2_queue_cancel()
2016 call_void_qop(q, unprepare_streaming, q); in __vb2_queue_cancel()
2024 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) { in __vb2_queue_cancel()
2025 for (i = 0; i < q->num_buffers; ++i) in __vb2_queue_cancel()
2026 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) { in __vb2_queue_cancel()
2028 q->bufs[i]); in __vb2_queue_cancel()
2029 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR); in __vb2_queue_cancel()
2032 WARN_ON(atomic_read(&q->owned_by_drv_count)); in __vb2_queue_cancel()
2035 q->streaming = 0; in __vb2_queue_cancel()
2036 q->start_streaming_called = 0; in __vb2_queue_cancel()
2037 q->queued_count = 0; in __vb2_queue_cancel()
2038 q->error = 0; in __vb2_queue_cancel()
2039 q->uses_requests = 0; in __vb2_queue_cancel()
2040 q->uses_qbuf = 0; in __vb2_queue_cancel()
2045 INIT_LIST_HEAD(&q->queued_list); in __vb2_queue_cancel()
2050 INIT_LIST_HEAD(&q->done_list); in __vb2_queue_cancel()
2051 atomic_set(&q->owned_by_drv_count, 0); in __vb2_queue_cancel()
2052 wake_up_all(&q->done_wq); in __vb2_queue_cancel()
2063 for (i = 0; i < q->num_buffers; ++i) { in __vb2_queue_cancel()
2064 struct vb2_buffer *vb = q->bufs[i]; in __vb2_queue_cancel()
2104 int vb2_core_streamon(struct vb2_queue *q, unsigned int type) in vb2_core_streamon() argument
2108 if (type != q->type) { in vb2_core_streamon()
2109 dprintk(q, 1, "invalid stream type\n"); in vb2_core_streamon()
2113 if (q->streaming) { in vb2_core_streamon()
2114 dprintk(q, 3, "already streaming\n"); in vb2_core_streamon()
2118 if (!q->num_buffers) { in vb2_core_streamon()
2119 dprintk(q, 1, "no buffers have been allocated\n"); in vb2_core_streamon()
2123 if (q->num_buffers < q->min_buffers_needed) { in vb2_core_streamon()
2124 dprintk(q, 1, "need at least %u allocated buffers\n", in vb2_core_streamon()
2125 q->min_buffers_needed); in vb2_core_streamon()
2129 ret = call_qop(q, prepare_streaming, q); in vb2_core_streamon()
2137 if (q->queued_count >= q->min_buffers_needed) { in vb2_core_streamon()
2138 ret = vb2_start_streaming(q); in vb2_core_streamon()
2143 q->streaming = 1; in vb2_core_streamon()
2145 dprintk(q, 3, "successful\n"); in vb2_core_streamon()
2149 call_void_qop(q, unprepare_streaming, q); in vb2_core_streamon()
2154 void vb2_queue_error(struct vb2_queue *q) in vb2_queue_error() argument
2156 q->error = 1; in vb2_queue_error()
2158 wake_up_all(&q->done_wq); in vb2_queue_error()
2162 int vb2_core_streamoff(struct vb2_queue *q, unsigned int type) in vb2_core_streamoff() argument
2164 if (type != q->type) { in vb2_core_streamoff()
2165 dprintk(q, 1, "invalid stream type\n"); in vb2_core_streamoff()
2178 __vb2_queue_cancel(q); in vb2_core_streamoff()
2179 q->waiting_for_buffers = !q->is_output; in vb2_core_streamoff()
2180 q->last_buffer_dequeued = false; in vb2_core_streamoff()
2182 dprintk(q, 3, "successful\n"); in vb2_core_streamoff()
2190 static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, in __find_plane_by_offset() argument
2200 lockdep_assert_held(&q->mmap_lock); in __find_plane_by_offset()
2202 if (q->memory != VB2_MEMORY_MMAP) { in __find_plane_by_offset()
2203 dprintk(q, 1, "queue is not currently set up for mmap\n"); in __find_plane_by_offset()
2207 if (vb2_fileio_is_active(q)) { in __find_plane_by_offset()
2208 dprintk(q, 1, "file io in progress\n"); in __find_plane_by_offset()
2217 for (buffer = 0; buffer < q->num_buffers; ++buffer) { in __find_plane_by_offset()
2218 vb = q->bufs[buffer]; in __find_plane_by_offset()
2232 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, in vb2_core_expbuf() argument
2240 if (q->memory != VB2_MEMORY_MMAP) { in vb2_core_expbuf()
2241 dprintk(q, 1, "queue is not currently set up for mmap\n"); in vb2_core_expbuf()
2245 if (!q->mem_ops->get_dmabuf) { in vb2_core_expbuf()
2246 dprintk(q, 1, "queue does not support DMA buffer exporting\n"); in vb2_core_expbuf()
2251 dprintk(q, 1, "queue does support only O_CLOEXEC and access mode flags\n"); in vb2_core_expbuf()
2255 if (type != q->type) { in vb2_core_expbuf()
2256 dprintk(q, 1, "invalid buffer type\n"); in vb2_core_expbuf()
2260 if (index >= q->num_buffers) { in vb2_core_expbuf()
2261 dprintk(q, 1, "buffer index out of range\n"); in vb2_core_expbuf()
2265 vb = q->bufs[index]; in vb2_core_expbuf()
2268 dprintk(q, 1, "buffer plane out of range\n"); in vb2_core_expbuf()
2272 if (vb2_fileio_is_active(q)) { in vb2_core_expbuf()
2273 dprintk(q, 1, "expbuf: file io in progress\n"); in vb2_core_expbuf()
2284 dprintk(q, 1, "failed to export buffer %d, plane %d\n", in vb2_core_expbuf()
2291 dprintk(q, 3, "buffer %d, plane %d failed to export (%d)\n", in vb2_core_expbuf()
2297 dprintk(q, 3, "buffer %d, plane %d exported as %d descriptor\n", in vb2_core_expbuf()
2305 int vb2_mmap(struct vb2_queue *q, struct vm_area_struct *vma) in vb2_mmap() argument
2317 dprintk(q, 1, "invalid vma flags, VM_SHARED needed\n"); in vb2_mmap()
2320 if (q->is_output) { in vb2_mmap()
2322 dprintk(q, 1, "invalid vma flags, VM_WRITE needed\n"); in vb2_mmap()
2327 dprintk(q, 1, "invalid vma flags, VM_READ needed\n"); in vb2_mmap()
2332 mutex_lock(&q->mmap_lock); in vb2_mmap()
2338 ret = __find_plane_by_offset(q, off, &buffer, &plane); in vb2_mmap()
2342 vb = q->bufs[buffer]; in vb2_mmap()
2351 dprintk(q, 1, in vb2_mmap()
2367 mutex_unlock(&q->mmap_lock); in vb2_mmap()
2371 dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane); in vb2_mmap()
2377 unsigned long vb2_get_unmapped_area(struct vb2_queue *q, in vb2_get_unmapped_area() argument
2389 mutex_lock(&q->mmap_lock); in vb2_get_unmapped_area()
2395 ret = __find_plane_by_offset(q, off, &buffer, &plane); in vb2_get_unmapped_area()
2399 vb = q->bufs[buffer]; in vb2_get_unmapped_area()
2402 mutex_unlock(&q->mmap_lock); in vb2_get_unmapped_area()
2406 mutex_unlock(&q->mmap_lock); in vb2_get_unmapped_area()
2412 int vb2_core_queue_init(struct vb2_queue *q) in vb2_core_queue_init() argument
2417 if (WARN_ON(!q) || in vb2_core_queue_init()
2418 WARN_ON(!q->ops) || in vb2_core_queue_init()
2419 WARN_ON(!q->mem_ops) || in vb2_core_queue_init()
2420 WARN_ON(!q->type) || in vb2_core_queue_init()
2421 WARN_ON(!q->io_modes) || in vb2_core_queue_init()
2422 WARN_ON(!q->ops->queue_setup) || in vb2_core_queue_init()
2423 WARN_ON(!q->ops->buf_queue)) in vb2_core_queue_init()
2426 if (WARN_ON(q->requires_requests && !q->supports_requests)) in vb2_core_queue_init()
2437 if (WARN_ON(q->supports_requests && q->min_buffers_needed)) in vb2_core_queue_init()
2440 INIT_LIST_HEAD(&q->queued_list); in vb2_core_queue_init()
2441 INIT_LIST_HEAD(&q->done_list); in vb2_core_queue_init()
2442 spin_lock_init(&q->done_lock); in vb2_core_queue_init()
2443 mutex_init(&q->mmap_lock); in vb2_core_queue_init()
2444 init_waitqueue_head(&q->done_wq); in vb2_core_queue_init()
2446 q->memory = VB2_MEMORY_UNKNOWN; in vb2_core_queue_init()
2448 if (q->buf_struct_size == 0) in vb2_core_queue_init()
2449 q->buf_struct_size = sizeof(struct vb2_buffer); in vb2_core_queue_init()
2451 if (q->bidirectional) in vb2_core_queue_init()
2452 q->dma_dir = DMA_BIDIRECTIONAL; in vb2_core_queue_init()
2454 q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; in vb2_core_queue_init()
2456 if (q->name[0] == '\0') in vb2_core_queue_init()
2457 snprintf(q->name, sizeof(q->name), "%s-%p", in vb2_core_queue_init()
2458 q->is_output ? "out" : "cap", q); in vb2_core_queue_init()
2464 static int __vb2_init_fileio(struct vb2_queue *q, int read);
2465 static int __vb2_cleanup_fileio(struct vb2_queue *q);
2466 void vb2_core_queue_release(struct vb2_queue *q) in vb2_core_queue_release() argument
2468 __vb2_cleanup_fileio(q); in vb2_core_queue_release()
2469 __vb2_queue_cancel(q); in vb2_core_queue_release()
2470 mutex_lock(&q->mmap_lock); in vb2_core_queue_release()
2471 __vb2_queue_free(q, q->num_buffers); in vb2_core_queue_release()
2472 mutex_unlock(&q->mmap_lock); in vb2_core_queue_release()
2476 __poll_t vb2_core_poll(struct vb2_queue *q, struct file *file, in vb2_core_poll() argument
2490 poll_wait(file, &q->done_wq, wait); in vb2_core_poll()
2492 if (!q->is_output && !(req_events & (EPOLLIN | EPOLLRDNORM))) in vb2_core_poll()
2494 if (q->is_output && !(req_events & (EPOLLOUT | EPOLLWRNORM))) in vb2_core_poll()
2500 if (q->num_buffers == 0 && !vb2_fileio_is_active(q)) { in vb2_core_poll()
2501 if (!q->is_output && (q->io_modes & VB2_READ) && in vb2_core_poll()
2503 if (__vb2_init_fileio(q, 1)) in vb2_core_poll()
2506 if (q->is_output && (q->io_modes & VB2_WRITE) && in vb2_core_poll()
2508 if (__vb2_init_fileio(q, 0)) in vb2_core_poll()
2521 if (!vb2_is_streaming(q) || q->error) in vb2_core_poll()
2530 if (q->quirk_poll_must_check_waiting_for_buffers && in vb2_core_poll()
2531 q->waiting_for_buffers && (req_events & (EPOLLIN | EPOLLRDNORM))) in vb2_core_poll()
2538 if (q->is_output && q->fileio && q->queued_count < q->num_buffers) in vb2_core_poll()
2541 if (list_empty(&q->done_list)) { in vb2_core_poll()
2546 if (q->last_buffer_dequeued) in vb2_core_poll()
2553 spin_lock_irqsave(&q->done_lock, flags); in vb2_core_poll()
2554 if (!list_empty(&q->done_list)) in vb2_core_poll()
2555 vb = list_first_entry(&q->done_list, struct vb2_buffer, in vb2_core_poll()
2557 spin_unlock_irqrestore(&q->done_lock, flags); in vb2_core_poll()
2561 return (q->is_output) ? in vb2_core_poll()
2625 static int __vb2_init_fileio(struct vb2_queue *q, int read) in __vb2_init_fileio() argument
2634 if (WARN_ON((read && !(q->io_modes & VB2_READ)) || in __vb2_init_fileio()
2635 (!read && !(q->io_modes & VB2_WRITE)))) in __vb2_init_fileio()
2641 if (!q->mem_ops->vaddr) in __vb2_init_fileio()
2647 if (q->streaming || q->num_buffers > 0) in __vb2_init_fileio()
2655 dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", in __vb2_init_fileio()
2656 (read) ? "read" : "write", count, q->fileio_read_once, in __vb2_init_fileio()
2657 q->fileio_write_immediately); in __vb2_init_fileio()
2663 fileio->read_once = q->fileio_read_once; in __vb2_init_fileio()
2664 fileio->write_immediately = q->fileio_write_immediately; in __vb2_init_fileio()
2672 fileio->type = q->type; in __vb2_init_fileio()
2673 q->fileio = fileio; in __vb2_init_fileio()
2674 ret = vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_init_fileio()
2682 if (q->bufs[0]->num_planes != 1) { in __vb2_init_fileio()
2690 for (i = 0; i < q->num_buffers; i++) { in __vb2_init_fileio()
2691 fileio->bufs[i].vaddr = vb2_plane_vaddr(q->bufs[i], 0); in __vb2_init_fileio()
2696 fileio->bufs[i].size = vb2_plane_size(q->bufs[i], 0); in __vb2_init_fileio()
2706 for (i = 0; i < q->num_buffers; i++) { in __vb2_init_fileio()
2707 ret = vb2_core_qbuf(q, i, NULL, NULL); in __vb2_init_fileio()
2716 fileio->initial_index = q->num_buffers; in __vb2_init_fileio()
2717 fileio->cur_index = q->num_buffers; in __vb2_init_fileio()
2723 ret = vb2_core_streamon(q, q->type); in __vb2_init_fileio()
2731 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_init_fileio()
2734 q->fileio = NULL; in __vb2_init_fileio()
2743 static int __vb2_cleanup_fileio(struct vb2_queue *q) in __vb2_cleanup_fileio() argument
2745 struct vb2_fileio_data *fileio = q->fileio; in __vb2_cleanup_fileio()
2748 vb2_core_streamoff(q, q->type); in __vb2_cleanup_fileio()
2749 q->fileio = NULL; in __vb2_cleanup_fileio()
2751 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count); in __vb2_cleanup_fileio()
2753 dprintk(q, 3, "file io emulator closed\n"); in __vb2_cleanup_fileio()
2767 static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_t count, in __vb2_perform_fileio() argument
2772 bool is_multiplanar = q->is_multiplanar; in __vb2_perform_fileio()
2778 bool copy_timestamp = !read && q->copy_timestamp; in __vb2_perform_fileio()
2782 dprintk(q, 3, "mode %s, offset %ld, count %zd, %sblocking\n", in __vb2_perform_fileio()
2789 if (q->waiting_in_dqbuf) { in __vb2_perform_fileio()
2790 dprintk(q, 3, "another dup()ped fd is %s\n", in __vb2_perform_fileio()
2798 if (!vb2_fileio_is_active(q)) { in __vb2_perform_fileio()
2799 ret = __vb2_init_fileio(q, read); in __vb2_perform_fileio()
2800 dprintk(q, 3, "vb2_init_fileio result: %d\n", ret); in __vb2_perform_fileio()
2804 fileio = q->fileio; in __vb2_perform_fileio()
2810 if (index >= q->num_buffers) { in __vb2_perform_fileio()
2816 ret = vb2_core_dqbuf(q, &index, NULL, nonblock); in __vb2_perform_fileio()
2817 dprintk(q, 5, "vb2_dqbuf result: %d\n", ret); in __vb2_perform_fileio()
2824 b = q->bufs[index]; in __vb2_perform_fileio()
2831 buf->size = read ? vb2_get_plane_payload(q->bufs[index], 0) in __vb2_perform_fileio()
2832 : vb2_plane_size(q->bufs[index], 0); in __vb2_perform_fileio()
2848 dprintk(q, 5, "reducing read count: %zd\n", count); in __vb2_perform_fileio()
2854 dprintk(q, 3, "copying %zd bytes - buffer %d, offset %u\n", in __vb2_perform_fileio()
2861 dprintk(q, 3, "error copying data\n"); in __vb2_perform_fileio()
2875 struct vb2_buffer *b = q->bufs[index]; in __vb2_perform_fileio()
2881 dprintk(q, 3, "read limit reached\n"); in __vb2_perform_fileio()
2882 return __vb2_cleanup_fileio(q); in __vb2_perform_fileio()
2892 ret = vb2_core_qbuf(q, index, NULL, NULL); in __vb2_perform_fileio()
2893 dprintk(q, 5, "vb2_dbuf result: %d\n", ret); in __vb2_perform_fileio()
2902 buf->size = vb2_plane_size(q->bufs[index], 0); in __vb2_perform_fileio()
2908 if (fileio->initial_index < q->num_buffers) in __vb2_perform_fileio()
2928 size_t vb2_read(struct vb2_queue *q, char __user *data, size_t count, in vb2_read() argument
2931 return __vb2_perform_fileio(q, data, count, ppos, nonblocking, 1); in vb2_read()
2935 size_t vb2_write(struct vb2_queue *q, const char __user *data, size_t count, in vb2_write() argument
2938 return __vb2_perform_fileio(q, (char __user *) data, count, in vb2_write()
2952 struct vb2_queue *q = data; in vb2_thread() local
2953 struct vb2_threadio_data *threadio = q->threadio; in vb2_thread()
2959 if (q->is_output) { in vb2_thread()
2960 prequeue = q->num_buffers; in vb2_thread()
2961 copy_timestamp = q->copy_timestamp; in vb2_thread()
2973 vb = q->bufs[index++]; in vb2_thread()
2976 call_void_qop(q, wait_finish, q); in vb2_thread()
2978 ret = vb2_core_dqbuf(q, &index, NULL, 0); in vb2_thread()
2979 call_void_qop(q, wait_prepare, q); in vb2_thread()
2980 dprintk(q, 5, "file io: vb2_dqbuf result: %d\n", ret); in vb2_thread()
2982 vb = q->bufs[index]; in vb2_thread()
2991 call_void_qop(q, wait_finish, q); in vb2_thread()
2995 ret = vb2_core_qbuf(q, vb->index, NULL, NULL); in vb2_thread()
2996 call_void_qop(q, wait_prepare, q); in vb2_thread()
3014 int vb2_thread_start(struct vb2_queue *q, vb2_thread_fnc fnc, void *priv, in vb2_thread_start() argument
3020 if (q->threadio) in vb2_thread_start()
3022 if (vb2_is_busy(q)) in vb2_thread_start()
3024 if (WARN_ON(q->fileio)) in vb2_thread_start()
3033 ret = __vb2_init_fileio(q, !q->is_output); in vb2_thread_start()
3034 dprintk(q, 3, "file io: vb2_init_fileio result: %d\n", ret); in vb2_thread_start()
3037 q->threadio = threadio; in vb2_thread_start()
3038 threadio->thread = kthread_run(vb2_thread, q, "vb2-%s", thread_name); in vb2_thread_start()
3047 __vb2_cleanup_fileio(q); in vb2_thread_start()
3054 int vb2_thread_stop(struct vb2_queue *q) in vb2_thread_stop() argument
3056 struct vb2_threadio_data *threadio = q->threadio; in vb2_thread_stop()
3063 vb2_queue_error(q); in vb2_thread_stop()
3065 __vb2_cleanup_fileio(q); in vb2_thread_stop()
3068 q->threadio = NULL; in vb2_thread_stop()