@@ -818,6 +818,7 @@ static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
818
818
* Make sure the requested values and current defaults are sane.
819
819
*/
820
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
822
memset (q -> plane_sizes , 0 , sizeof (q -> plane_sizes ));
822
823
memset (q -> alloc_ctx , 0 , sizeof (q -> alloc_ctx ));
823
824
q -> memory = req -> memory ;
@@ -840,10 +841,17 @@ static int __reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
840
841
return - ENOMEM ;
841
842
}
842
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
+
843
851
/*
844
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
855
num_buffers = allocated_buffers ;
848
856
849
857
ret = call_qop (q , queue_setup , q , NULL , & num_buffers ,
@@ -1051,25 +1059,38 @@ EXPORT_SYMBOL_GPL(vb2_plane_cookie);
1051
1059
* vb2_buffer_done() - inform videobuf that an operation on a buffer is finished
1052
1060
* @vb: vb2_buffer returned from the driver
1053
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
1066
* This function should be called by the driver after a hardware operation on
1057
1067
* a buffer is finished and the buffer may be returned to userspace. The driver
1058
1068
* cannot use this buffer anymore until it is queued back to it by videobuf
1059
1069
* by the means of buf_queue callback. Only buffers previously queued to the
1060
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
*/
1062
1077
void vb2_buffer_done (struct vb2_buffer * vb , enum vb2_buffer_state state )
1063
1078
{
1064
1079
struct vb2_queue * q = vb -> vb2_queue ;
1065
1080
unsigned long flags ;
1066
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
1084
return ;
1070
1085
1071
- if (state != VB2_BUF_STATE_DONE && state != VB2_BUF_STATE_ERROR )
1072
- return ;
1086
+ if (!q -> start_streaming_called ) {
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
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
1109
/* Add the buffer to the done buffers list */
1089
1110
spin_lock_irqsave (& q -> done_lock , flags );
1090
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
1114
atomic_dec (& q -> owned_by_drv_count );
1093
1115
spin_unlock_irqrestore (& q -> done_lock , flags );
1094
1116
1117
+ if (state == VB2_BUF_STATE_QUEUED )
1118
+ return ;
1119
+
1095
1120
/* Inform any processes that may be waiting for buffers */
1096
1121
wake_up (& q -> done_wq );
1097
1122
}
@@ -1588,34 +1613,49 @@ EXPORT_SYMBOL_GPL(vb2_prepare_buf);
1588
1613
* vb2_start_streaming() - Attempt to start streaming.
1589
1614
* @q: videobuf2 queue
1590
1615
*
1591
- * If there are not enough buffers, then retry_start_streaming is set to
1592
- * 1 and 0 is returned. The next time a buffer is queued and
1593
- * retry_start_streaming is 1, this function will be called again to
1594
- * retry starting the DMA engine.
1616
+ * Attempt to start streaming. When this function is called there must be
1617
+ * at least q->min_buffers_needed buffers queued up (i.e. the minimum
1618
+ * number of buffers required for the DMA engine to function). If the
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
*/
1596
1623
static int vb2_start_streaming (struct vb2_queue * q )
1597
1624
{
1625
+ struct vb2_buffer * vb ;
1598
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
1607
- * the start_streaming operation will return -ENOBUFS and you have to
1608
- * retry when the next buffer is queued.
1629
+ * If any buffers were queued before streamon,
1630
+ * we can now pass them to driver for processing.
1609
1631
*/
1610
- if (ret == - ENOBUFS ) {
1611
- dprintk (1 , "qbuf: not enough buffers, retry when more buffers are queued.\n" );
1612
- q -> retry_start_streaming = 1 ;
1632
+ list_for_each_entry (vb , & q -> queued_list , queued_entry )
1633
+ __enqueue_in_driver (vb );
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
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
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
1691
* dequeued in dqbuf.
1652
1692
*/
1653
1693
list_add_tail (& vb -> queued_entry , & q -> queued_list );
1694
+ q -> queued_count ++ ;
1654
1695
vb -> state = VB2_BUF_STATE_QUEUED ;
1655
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
1710
* If already streaming, give the buffer to driver for processing.
1670
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
1714
__enqueue_in_driver (vb );
1674
1715
1675
1716
/* Fill buffer information for the userspace */
1676
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
1727
ret = vb2_start_streaming (q );
1680
1728
if (ret )
1681
1729
return ret ;
@@ -1830,7 +1878,7 @@ int vb2_wait_for_all_buffers(struct vb2_queue *q)
1830
1878
return - EINVAL ;
1831
1879
}
1832
1880
1833
- if (! q -> retry_start_streaming )
1881
+ if (q -> start_streaming_called )
1834
1882
wait_event (q -> done_wq , !atomic_read (& q -> owned_by_drv_count ));
1835
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
1939
__fill_v4l2_buffer (vb , b );
1892
1940
/* Remove from videobuf queue */
1893
1941
list_del (& vb -> queued_entry );
1942
+ q -> queued_count -- ;
1894
1943
/* go back to dequeued state */
1895
1944
__vb2_dqbuf (vb );
1896
1945
@@ -1941,18 +1990,23 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
1941
1990
{
1942
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
1994
* Tell driver to stop all transactions and release all queued
1951
1995
* buffers.
1952
1996
*/
1953
- if (q -> streaming )
1997
+ if (q -> start_streaming_called )
1954
1998
call_qop (q , stop_streaming , q );
1955
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
2012
* Remove all buffers from videobuf's list...
@@ -1988,7 +2042,6 @@ static void __vb2_queue_cancel(struct vb2_queue *q)
1988
2042
1989
2043
static int vb2_internal_streamon (struct vb2_queue * q , enum v4l2_buf_type type )
1990
2044
{
1991
- struct vb2_buffer * vb ;
1992
2045
int ret ;
1993
2046
1994
2047
if (type != q -> type ) {
@@ -2010,19 +2063,22 @@ static int vb2_internal_streamon(struct vb2_queue *q, enum v4l2_buf_type type)
2010
2063
dprintk (1 , "streamon: no buffers have been allocated\n" );
2011
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,
2016
- * we can now pass them to driver for processing .
2073
+ * Tell driver to start streaming provided sufficient buffers
2074
+ * are available .
2017
2075
*/
2018
- list_for_each_entry (vb , & q -> queued_list , queued_entry )
2019
- __enqueue_in_driver (vb );
2020
-
2021
- /* Tell driver to start streaming. */
2022
- ret = vb2_start_streaming (q );
2023
- if (ret ) {
2024
- __vb2_queue_cancel (q );
2025
- return ret ;
2076
+ if (q -> queued_count >= q -> min_buffers_needed ) {
2077
+ ret = vb2_start_streaming (q );
2078
+ if (ret ) {
2079
+ __vb2_queue_cancel (q );
2080
+ return ret ;
2081
+ }
2026
2082
}
2027
2083
2028
2084
q -> streaming = 1 ;
0 commit comments