aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Verkuil <hans.verkuil@cisco.com>2014-02-24 11:51:03 -0500
committerMauro Carvalho Chehab <m.chehab@samsung.com>2014-03-11 05:56:41 -0400
commitb3379c6201bb3555298cdbf0aa004af260f2a6a4 (patch)
tree936271737c6bdcf27a40d0af2ad9ab0d2f5533fa
parenta7afcaccfab2fb012841852eaead79861dc9cb5f (diff)
[media] vb2: only call start_streaming if sufficient buffers are queued
In commit 02f142ecd24aaf891324ffba8527284c1731b561 support was added to start_streaming to return -ENOBUFS if insufficient buffers were queued for the DMA engine to start. The vb2 core would attempt calling start_streaming again if another buffer would be queued up. Later analysis uncovered problems with the queue management if start_streaming would return an error: the buffers are enqueued to the driver before the start_streaming op is called, so after an error they are never returned to the vb2 core. The solution for this is to let the driver return them to the vb2 core in case of an error while starting the DMA engine. However, in the case of -ENOBUFS that would be weird: it is not a real error, it just says that more buffers are needed. Requiring start_streaming to give them back only to have them requeued again the next time the application calls QBUF is inefficient. This patch changes this mechanism: it adds a 'min_buffers_needed' field to vb2_queue that drivers can set with the minimum number of buffers required to start the DMA engine. The start_streaming op is only called if enough buffers are queued. The -ENOBUFS handling has been dropped in favor of this new method. Drivers are expected to return buffers back to vb2 core with state QUEUED if start_streaming would return an error. The vb2 core checks for this and produces a warning if that didn't happen and it will forcefully reclaim such buffers to ensure that the internal vb2 core state remains consistent and all buffer-related resources have been correctly freed and all op calls have been balanced. __reqbufs() has been updated to check that at least min_buffers_needed buffers could be allocated. If fewer buffers were allocated then __reqbufs will free what was allocated and return -ENOMEM. Based on a suggestion from Pawel Osciak. __create_bufs() doesn't do that check, since the use of __create_bufs assumes some advance scenario where the user might want more control. Instead streamon will check if enough buffers were allocated to prevent streaming with fewer than the minimum required number of buffers. Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
-rw-r--r--drivers/media/platform/davinci/vpbe_display.c6
-rw-r--r--drivers/media/platform/davinci/vpif_capture.c7
-rw-r--r--drivers/media/platform/davinci/vpif_display.c7
-rw-r--r--drivers/media/platform/s5p-tv/mixer_video.c6
-rw-r--r--drivers/media/v4l2-core/videobuf2-core.c146
-rw-r--r--drivers/staging/media/davinci_vpfe/vpfe_video.c3
-rw-r--r--include/media/videobuf2-core.h14
7 files changed, 116 insertions, 73 deletions
diff --git a/drivers/media/platform/davinci/vpbe_display.c b/drivers/media/platform/davinci/vpbe_display.c
index e512767cf7ea..7a0e40ee60e3 100644
--- a/drivers/media/platform/davinci/vpbe_display.c
+++ b/drivers/media/platform/davinci/vpbe_display.c
@@ -344,11 +344,6 @@ static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
344 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev; 344 struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
345 int ret; 345 int ret;
346 346
347 /* If buffer queue is empty, return error */
348 if (list_empty(&layer->dma_queue)) {
349 v4l2_err(&vpbe_dev->v4l2_dev, "buffer queue is empty\n");
350 return -ENOBUFS;
351 }
352 /* Get the next frame from the buffer queue */ 347 /* Get the next frame from the buffer queue */
353 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next, 348 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
354 struct vpbe_disp_buffer, list); 349 struct vpbe_disp_buffer, list);
@@ -1416,6 +1411,7 @@ static int vpbe_display_reqbufs(struct file *file, void *priv,
1416 q->mem_ops = &vb2_dma_contig_memops; 1411 q->mem_ops = &vb2_dma_contig_memops;
1417 q->buf_struct_size = sizeof(struct vpbe_disp_buffer); 1412 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1418 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1413 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1414 q->min_buffers_needed = 1;
1419 1415
1420 ret = vb2_queue_init(q); 1416 ret = vb2_queue_init(q);
1421 if (ret) { 1417 if (ret) {
diff --git a/drivers/media/platform/davinci/vpif_capture.c b/drivers/media/platform/davinci/vpif_capture.c
index cd6da8b78108..756da78bac23 100644
--- a/drivers/media/platform/davinci/vpif_capture.c
+++ b/drivers/media/platform/davinci/vpif_capture.c
@@ -272,13 +272,7 @@ static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
272 unsigned long flags; 272 unsigned long flags;
273 int ret; 273 int ret;
274 274
275 /* If buffer queue is empty, return error */
276 spin_lock_irqsave(&common->irqlock, flags); 275 spin_lock_irqsave(&common->irqlock, flags);
277 if (list_empty(&common->dma_queue)) {
278 spin_unlock_irqrestore(&common->irqlock, flags);
279 vpif_dbg(1, debug, "buffer queue is empty\n");
280 return -ENOBUFS;
281 }
282 276
283 /* Get the next frame from the buffer queue */ 277 /* Get the next frame from the buffer queue */
284 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next, 278 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
@@ -1024,6 +1018,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
1024 q->mem_ops = &vb2_dma_contig_memops; 1018 q->mem_ops = &vb2_dma_contig_memops;
1025 q->buf_struct_size = sizeof(struct vpif_cap_buffer); 1019 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1026 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1020 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1021 q->min_buffers_needed = 1;
1027 1022
1028 ret = vb2_queue_init(q); 1023 ret = vb2_queue_init(q);
1029 if (ret) { 1024 if (ret) {
diff --git a/drivers/media/platform/davinci/vpif_display.c b/drivers/media/platform/davinci/vpif_display.c
index fd68236657c2..0ac841e35aa4 100644
--- a/drivers/media/platform/davinci/vpif_display.c
+++ b/drivers/media/platform/davinci/vpif_display.c
@@ -234,13 +234,7 @@ static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
234 unsigned long flags; 234 unsigned long flags;
235 int ret; 235 int ret;
236 236
237 /* If buffer queue is empty, return error */
238 spin_lock_irqsave(&common->irqlock, flags); 237 spin_lock_irqsave(&common->irqlock, flags);
239 if (list_empty(&common->dma_queue)) {
240 spin_unlock_irqrestore(&common->irqlock, flags);
241 vpif_err("buffer queue is empty\n");
242 return -ENOBUFS;
243 }
244 238
245 /* Get the next frame from the buffer queue */ 239 /* Get the next frame from the buffer queue */
246 common->next_frm = common->cur_frm = 240 common->next_frm = common->cur_frm =
@@ -984,6 +978,7 @@ static int vpif_reqbufs(struct file *file, void *priv,
984 q->mem_ops = &vb2_dma_contig_memops; 978 q->mem_ops = &vb2_dma_contig_memops;
985 q->buf_struct_size = sizeof(struct vpif_disp_buffer); 979 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
986 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 980 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
981 q->min_buffers_needed = 1;
987 982
988 ret = vb2_queue_init(q); 983 ret = vb2_queue_init(q);
989 if (ret) { 984 if (ret) {
diff --git a/drivers/media/platform/s5p-tv/mixer_video.c b/drivers/media/platform/s5p-tv/mixer_video.c
index c5059ba0d733..a1ce55fd30f3 100644
--- a/drivers/media/platform/s5p-tv/mixer_video.c
+++ b/drivers/media/platform/s5p-tv/mixer_video.c
@@ -946,11 +946,6 @@ static int start_streaming(struct vb2_queue *vq, unsigned int count)
946 946
947 mxr_dbg(mdev, "%s\n", __func__); 947 mxr_dbg(mdev, "%s\n", __func__);
948 948
949 if (count == 0) {
950 mxr_dbg(mdev, "no output buffers queued\n");
951 return -ENOBUFS;
952 }
953
954 /* block any changes in output configuration */ 949 /* block any changes in output configuration */
955 mxr_output_get(mdev); 950 mxr_output_get(mdev);
956 951
@@ -1124,6 +1119,7 @@ struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
1124 .drv_priv = layer, 1119 .drv_priv = layer,
1125 .buf_struct_size = sizeof(struct mxr_buffer), 1120 .buf_struct_size = sizeof(struct mxr_buffer),
1126 .ops = &mxr_video_qops, 1121 .ops = &mxr_video_qops,
1122 .min_buffers_needed = 1,
1127 .mem_ops = &vb2_dma_contig_memops, 1123 .mem_ops = &vb2_dma_contig_memops,
1128 }; 1124 };
1129 1125
diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
index 4e5004335309..ce308f6d2095 100644
--- a/drivers/media/v4l2-core/videobuf2-core.c
+++ b/drivers/media/v4l2-core/videobuf2-core.c
@@ -818,6 +818,7 @@ static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
818 * Make sure the requested values and current defaults are sane. 818 * Make sure the requested values and current defaults are sane.
819 */ 819 */
820 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME); 820 num_buffers = min_t(unsigned int, req->count, VIDEO_MAX_FRAME);
821 num_buffers = max_t(unsigned int, req->count, q->min_buffers_needed);
821 memset(q->plane_sizes, 0, sizeof(q->plane_sizes)); 822 memset(q->plane_sizes, 0, sizeof(q->plane_sizes));
822 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx)); 823 memset(q->alloc_ctx, 0, sizeof(q->alloc_ctx));
823 q->memory = req->memory; 824 q->memory = req->memory;
@@ -841,9 +842,16 @@ static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
841 } 842 }
842 843
843 /* 844 /*
845 * There is no point in continuing if we can't allocate the minimum
846 * number of buffers needed by this vb2_queue.
847 */
848 if (allocated_buffers < q->min_buffers_needed)
849 ret = -ENOMEM;
850
851 /*
844 * Check if driver can handle the allocated number of buffers. 852 * Check if driver can handle the allocated number of buffers.
845 */ 853 */
846 if (allocated_buffers < num_buffers) { 854 if (!ret && allocated_buffers < num_buffers) {
847 num_buffers = allocated_buffers; 855 num_buffers = allocated_buffers;
848 856
849 ret = call_qop(q, queue_setup, q, NULL, &num_buffers, 857 ret = call_qop(q, queue_setup, q, NULL, &num_buffers,
@@ -1051,13 +1059,20 @@ EXPORT_SYMBOL_GPL(vb2_plane_cookie);
1051 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished 1059 * vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1052 * @vb: vb2_buffer returned from the driver 1060 * @vb: vb2_buffer returned from the driver
1053 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully 1061 * @state: either VB2_BUF_STATE_DONE if the operation finished successfully
1054 * or VB2_BUF_STATE_ERROR if the operation finished with an error 1062 * or VB2_BUF_STATE_ERROR if the operation finished with an error.
1063 * If start_streaming fails then it should return buffers with state
1064 * VB2_BUF_STATE_QUEUED to put them back into the queue.
1055 * 1065 *
1056 * This function should be called by the driver after a hardware operation on 1066 * This function should be called by the driver after a hardware operation on
1057 * a buffer is finished and the buffer may be returned to userspace. The driver 1067 * a buffer is finished and the buffer may be returned to userspace. The driver
1058 * cannot use this buffer anymore until it is queued back to it by videobuf 1068 * cannot use this buffer anymore until it is queued back to it by videobuf
1059 * by the means of buf_queue callback. Only buffers previously queued to the 1069 * by the means of buf_queue callback. Only buffers previously queued to the
1060 * driver by buf_queue can be passed to this function. 1070 * driver by buf_queue can be passed to this function.
1071 *
1072 * While streaming a buffer can only be returned in state DONE or ERROR.
1073 * The start_streaming op can also return them in case the DMA engine cannot
1074 * be started for some reason. In that case the buffers should be returned with
1075 * state QUEUED.
1061 */ 1076 */
1062void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state) 1077void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1063{ 1078{
@@ -1065,11 +1080,17 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1065 unsigned long flags; 1080 unsigned long flags;
1066 unsigned int plane; 1081 unsigned int plane;
1067 1082
1068 if (vb->state != VB2_BUF_STATE_ACTIVE) 1083 if (WARN_ON(vb->state != VB2_BUF_STATE_ACTIVE))
1069 return; 1084 return;
1070 1085
1071 if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR) 1086 if (!q->start_streaming_called) {
1072 return; 1087 if (WARN_ON(state != VB2_BUF_STATE_QUEUED))
1088 state = VB2_BUF_STATE_QUEUED;
1089 } else if (!WARN_ON(!q->start_streaming_called)) {
1090 if (WARN_ON(state != VB2_BUF_STATE_DONE &&
1091 state != VB2_BUF_STATE_ERROR))
1092 state = VB2_BUF_STATE_ERROR;
1093 }
1073 1094
1074#ifdef CONFIG_VIDEO_ADV_DEBUG 1095#ifdef CONFIG_VIDEO_ADV_DEBUG
1075 /* 1096 /*
@@ -1088,10 +1109,14 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
1088 /* Add the buffer to the done buffers list */ 1109 /* Add the buffer to the done buffers list */
1089 spin_lock_irqsave(&q->done_lock, flags); 1110 spin_lock_irqsave(&q->done_lock, flags);
1090 vb->state = state; 1111 vb->state = state;
1091 list_add_tail(&vb->done_entry, &q->done_list); 1112 if (state != VB2_BUF_STATE_QUEUED)
1113 list_add_tail(&vb->done_entry, &q->done_list);
1092 atomic_dec(&q->owned_by_drv_count); 1114 atomic_dec(&q->owned_by_drv_count);
1093 spin_unlock_irqrestore(&q->done_lock, flags); 1115 spin_unlock_irqrestore(&q->done_lock, flags);
1094 1116
1117 if (state == VB2_BUF_STATE_QUEUED)
1118 return;
1119
1095 /* Inform any processes that may be waiting for buffers */ 1120 /* Inform any processes that may be waiting for buffers */
1096 wake_up(&q->done_wq); 1121 wake_up(&q->done_wq);
1097} 1122}
@@ -1588,34 +1613,49 @@ EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1588 * vb2_start_streaming() - Attempt to start streaming. 1613 * vb2_start_streaming() - Attempt to start streaming.
1589 * @q: videobuf2 queue 1614 * @q: videobuf2 queue
1590 * 1615 *
1591 * If there are not enough buffers, then retry_start_streaming is set to 1616 * Attempt to start streaming. When this function is called there must be
1592 * 1 and 0 is returned. The next time a buffer is queued and 1617 * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1593 * retry_start_streaming is 1, this function will be called again to 1618 * number of buffers required for the DMA engine to function). If the
1594 * retry starting the DMA engine. 1619 * @start_streaming op fails it is supposed to return all the driver-owned
1620 * buffers back to vb2 in state QUEUED. Check if that happened and if
1621 * not warn and reclaim them forcefully.
1595 */ 1622 */
1596static int vb2_start_streaming(struct vb2_queue *q) 1623static int vb2_start_streaming(struct vb2_queue *q)
1597{ 1624{
1625 struct vb2_buffer *vb;
1598 int ret; 1626 int ret;
1599 1627
1600 /* Tell the driver to start streaming */
1601 ret = call_qop(q, start_streaming, q, atomic_read(&q->owned_by_drv_count));
1602 if (ret)
1603 fail_qop(q, start_streaming);
1604
1605 /* 1628 /*
1606 * If there are not enough buffers queued to start streaming, then 1629 * If any buffers were queued before streamon,
1607 * the start_streaming operation will return -ENOBUFS and you have to 1630 * we can now pass them to driver for processing.
1608 * retry when the next buffer is queued.
1609 */ 1631 */
1610 if (ret == -ENOBUFS) { 1632 list_for_each_entry(vb, &q->queued_list, queued_entry)
1611 dprintk(1, "qbuf: not enough buffers, retry when more buffers are queued.\n"); 1633 __enqueue_in_driver(vb);
1612 q->retry_start_streaming = 1; 1634
1635 /* Tell the driver to start streaming */
1636 ret = call_qop(q, start_streaming, q,
1637 atomic_read(&q->owned_by_drv_count));
1638 q->start_streaming_called = ret == 0;
1639 if (!ret)
1613 return 0; 1640 return 0;
1641
1642 fail_qop(q, start_streaming);
1643 dprintk(1, "qbuf: driver refused to start streaming\n");
1644 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
1645 unsigned i;
1646
1647 /*
1648 * Forcefully reclaim buffers if the driver did not
1649 * correctly return them to vb2.
1650 */
1651 for (i = 0; i < q->num_buffers; ++i) {
1652 vb = q->bufs[i];
1653 if (vb->state == VB2_BUF_STATE_ACTIVE)
1654 vb2_buffer_done(vb, VB2_BUF_STATE_QUEUED);
1655 }
1656 /* Must be zero now */
1657 WARN_ON(atomic_read(&q->owned_by_drv_count));
1614 } 1658 }
1615 if (ret)
1616 dprintk(1, "qbuf: driver refused to start streaming\n");
1617 else
1618 q->retry_start_streaming = 0;
1619 return ret; 1659 return ret;
1620} 1660}
1621 1661
@@ -1651,6 +1691,7 @@ static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1651 * dequeued in dqbuf. 1691 * dequeued in dqbuf.
1652 */ 1692 */
1653 list_add_tail(&vb->queued_entry, &q->queued_list); 1693 list_add_tail(&vb->queued_entry, &q->queued_list);
1694 q->queued_count++;
1654 vb->state = VB2_BUF_STATE_QUEUED; 1695 vb->state = VB2_BUF_STATE_QUEUED;
1655 if (V4L2_TYPE_IS_OUTPUT(q->type)) { 1696 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1656 /* 1697 /*
@@ -1669,13 +1710,20 @@ static int vb2_internal_qbuf(struct vb2_queue *q, struct v4l2_buffer *b)
1669 * If already streaming, give the buffer to driver for processing. 1710 * If already streaming, give the buffer to driver for processing.
1670 * If not, the buffer will be given to driver on next streamon. 1711 * If not, the buffer will be given to driver on next streamon.
1671 */ 1712 */
1672 if (q->streaming) 1713 if (q->start_streaming_called)
1673 __enqueue_in_driver(vb); 1714 __enqueue_in_driver(vb);
1674 1715
1675 /* Fill buffer information for the userspace */ 1716 /* Fill buffer information for the userspace */
1676 __fill_v4l2_buffer(vb, b); 1717 __fill_v4l2_buffer(vb, b);
1677 1718
1678 if (q->retry_start_streaming) { 1719 /*
1720 * If streamon has been called, and we haven't yet called
1721 * start_streaming() since not enough buffers were queued, and
1722 * we now have reached the minimum number of queued buffers,
1723 * then we can finally call start_streaming().
1724 */
1725 if (q->streaming && !q->start_streaming_called &&
1726 q->queued_count >= q->min_buffers_needed) {
1679 ret = vb2_start_streaming(q); 1727 ret = vb2_start_streaming(q);
1680 if (ret) 1728 if (ret)
1681 return ret; 1729 return ret;
@@ -1830,7 +1878,7 @@ int vb2_wait_for_all_buffers(struct vb2_queue *q)
1830 return -EINVAL; 1878 return -EINVAL;
1831 } 1879 }
1832 1880
1833 if (!q->retry_start_streaming) 1881 if (q->start_streaming_called)
1834 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count)); 1882 wait_event(q->done_wq, !atomic_read(&q->owned_by_drv_count));
1835 return 0; 1883 return 0;
1836} 1884}
@@ -1891,6 +1939,7 @@ static int vb2_internal_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool n
1891 __fill_v4l2_buffer(vb, b); 1939 __fill_v4l2_buffer(vb, b);
1892 /* Remove from videobuf queue */ 1940 /* Remove from videobuf queue */
1893 list_del(&vb->queued_entry); 1941 list_del(&vb->queued_entry);
1942 q->queued_count--;
1894 /* go back to dequeued state */ 1943 /* go back to dequeued state */
1895 __vb2_dqbuf(vb); 1944 __vb2_dqbuf(vb);
1896 1945
@@ -1941,18 +1990,23 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
1941{ 1990{
1942 unsigned int i; 1991 unsigned int i;
1943 1992
1944 if (q->retry_start_streaming) {
1945 q->retry_start_streaming = 0;
1946 q->streaming = 0;
1947 }
1948
1949 /* 1993 /*
1950 * Tell driver to stop all transactions and release all queued 1994 * Tell driver to stop all transactions and release all queued
1951 * buffers. 1995 * buffers.
1952 */ 1996 */
1953 if (q->streaming) 1997 if (q->start_streaming_called)
1954 call_qop(q, stop_streaming, q); 1998 call_qop(q, stop_streaming, q);
1955 q->streaming = 0; 1999 q->streaming = 0;
2000 q->start_streaming_called = 0;
2001 q->queued_count = 0;
2002
2003 if (WARN_ON(atomic_read(&q->owned_by_drv_count))) {
2004 for (i = 0; i < q->num_buffers; ++i)
2005 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE)
2006 vb2_buffer_done(q->bufs[i], VB2_BUF_STATE_ERROR);
2007 /* Must be zero now */
2008 WARN_ON(atomic_read(&q->owned_by_drv_count));
2009 }
1956 2010
1957 /* 2011 /*
1958 * Remove all buffers from videobuf's list... 2012 * Remove all buffers from videobuf's list...
@@ -1988,7 +2042,6 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
1988 2042
1989static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type) 2043static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
1990{ 2044{
1991 struct vb2_buffer *vb;
1992 int ret; 2045 int ret;
1993 2046
1994 if (type != q->type) { 2047 if (type != q->type) {
@@ -2010,19 +2063,22 @@ static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
2010 dprintk(1, "streamon: no buffers have been allocated\n"); 2063 dprintk(1, "streamon: no buffers have been allocated\n");
2011 return -EINVAL; 2064 return -EINVAL;
2012 } 2065 }
2066 if (q->num_buffers < q->min_buffers_needed) {
2067 dprintk(1, "streamon: need at least %u allocated buffers\n",
2068 q->min_buffers_needed);
2069 return -EINVAL;
2070 }
2013 2071
2014 /* 2072 /*
2015 * If any buffers were queued before streamon, 2073 * Tell driver to start streaming provided sufficient buffers
2016 * we can now pass them to driver for processing. 2074 * are available.
2017 */ 2075 */
2018 list_for_each_entry(vb, &q->queued_list, queued_entry) 2076 if (q->queued_count >= q->min_buffers_needed) {
2019 __enqueue_in_driver(vb); 2077 ret = vb2_start_streaming(q);
2020 2078 if (ret) {
2021 /* Tell driver to start streaming. */ 2079 __vb2_queue_cancel(q);
2022 ret = vb2_start_streaming(q); 2080 return ret;
2023 if (ret) { 2081 }
2024 __vb2_queue_cancel(q);
2025 return ret;
2026 } 2082 }
2027 2083
2028 q->streaming = 1; 2084 q->streaming = 1;
diff --git a/drivers/staging/media/davinci_vpfe/vpfe_video.c b/drivers/staging/media/davinci_vpfe/vpfe_video.c
index 1f3b0f9a8d10..8c101cbbee97 100644
--- a/drivers/staging/media/davinci_vpfe/vpfe_video.c
+++ b/drivers/staging/media/davinci_vpfe/vpfe_video.c
@@ -1201,8 +1201,6 @@ static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1201 unsigned long addr; 1201 unsigned long addr;
1202 int ret; 1202 int ret;
1203 1203
1204 if (count == 0)
1205 return -ENOBUFS;
1206 ret = mutex_lock_interruptible(&video->lock); 1204 ret = mutex_lock_interruptible(&video->lock);
1207 if (ret) 1205 if (ret)
1208 goto streamoff; 1206 goto streamoff;
@@ -1327,6 +1325,7 @@ static int vpfe_reqbufs(struct file *file, void *priv,
1327 q->type = req_buf->type; 1325 q->type = req_buf->type;
1328 q->io_modes = VB2_MMAP | VB2_USERPTR; 1326 q->io_modes = VB2_MMAP | VB2_USERPTR;
1329 q->drv_priv = fh; 1327 q->drv_priv = fh;
1328 q->min_buffers_needed = 1;
1330 q->ops = &video_qops; 1329 q->ops = &video_qops;
1331 q->mem_ops = &vb2_dma_contig_memops; 1330 q->mem_ops = &vb2_dma_contig_memops;
1332 q->buf_struct_size = sizeof(struct vpfe_cap_buffer); 1331 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 36e3e8e2d457..af4621109726 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -356,20 +356,24 @@ struct v4l2_fh;
356 * @gfp_flags: additional gfp flags used when allocating the buffers. 356 * @gfp_flags: additional gfp flags used when allocating the buffers.
357 * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32 357 * Typically this is 0, but it may be e.g. GFP_DMA or __GFP_DMA32
358 * to force the buffer allocation to a specific memory zone. 358 * to force the buffer allocation to a specific memory zone.
359 * @min_buffers_needed: the minimum number of buffers needed before
360 * start_streaming() can be called. Used when a DMA engine
361 * cannot be started unless at least this number of buffers
362 * have been queued into the driver.
359 * 363 *
360 * @memory: current memory type used 364 * @memory: current memory type used
361 * @bufs: videobuf buffer structures 365 * @bufs: videobuf buffer structures
362 * @num_buffers: number of allocated/used buffers 366 * @num_buffers: number of allocated/used buffers
363 * @queued_list: list of buffers currently queued from userspace 367 * @queued_list: list of buffers currently queued from userspace
368 * @queued_count: number of buffers queued and ready for streaming.
364 * @owned_by_drv_count: number of buffers owned by the driver 369 * @owned_by_drv_count: number of buffers owned by the driver
365 * @done_list: list of buffers ready to be dequeued to userspace 370 * @done_list: list of buffers ready to be dequeued to userspace
366 * @done_lock: lock to protect done_list list 371 * @done_lock: lock to protect done_list list
367 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued 372 * @done_wq: waitqueue for processes waiting for buffers ready to be dequeued
368 * @alloc_ctx: memory type/allocator-specific contexts for each plane 373 * @alloc_ctx: memory type/allocator-specific contexts for each plane
369 * @streaming: current streaming state 374 * @streaming: current streaming state
370 * @retry_start_streaming: start_streaming() was called, but there were not enough 375 * @start_streaming_called: start_streaming() was called successfully and we
371 * buffers queued. If set, then retry calling start_streaming when 376 * started streaming.
372 * queuing a new buffer.
373 * @fileio: file io emulator internal data, used only if emulator is active 377 * @fileio: file io emulator internal data, used only if emulator is active
374 */ 378 */
375struct vb2_queue { 379struct vb2_queue {
@@ -385,6 +389,7 @@ struct vb2_queue {
385 unsigned int buf_struct_size; 389 unsigned int buf_struct_size;
386 u32 timestamp_flags; 390 u32 timestamp_flags;
387 gfp_t gfp_flags; 391 gfp_t gfp_flags;
392 u32 min_buffers_needed;
388 393
389/* private: internal use only */ 394/* private: internal use only */
390 enum v4l2_memory memory; 395 enum v4l2_memory memory;
@@ -392,6 +397,7 @@ struct vb2_queue {
392 unsigned int num_buffers; 397 unsigned int num_buffers;
393 398
394 struct list_head queued_list; 399 struct list_head queued_list;
400 unsigned int queued_count;
395 401
396 atomic_t owned_by_drv_count; 402 atomic_t owned_by_drv_count;
397 struct list_head done_list; 403 struct list_head done_list;
@@ -402,7 +408,7 @@ struct vb2_queue {
402 unsigned int plane_sizes[VIDEO_MAX_PLANES]; 408 unsigned int plane_sizes[VIDEO_MAX_PLANES];
403 409
404 unsigned int streaming:1; 410 unsigned int streaming:1;
405 unsigned int retry_start_streaming:1; 411 unsigned int start_streaming_called:1;
406 412
407 struct vb2_fileio_data *fileio; 413 struct vb2_fileio_data *fileio;
408 414