1 From 3cb19cb6b4d6dc86582abef6200c0fc663ae3f2a Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Fri, 10 Mar 2017 17:27:56 +0000
4 Subject: [PATCH] staging: bcm2835-camera: Return early on errors
6 Fix several instances where it is easier to return
7 early on error conditions than handle it as an else
11 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
13 .../bcm2835-camera/bcm2835-camera.c | 137 +++++++++---------
14 1 file changed, 71 insertions(+), 66 deletions(-)
16 --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
17 +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
18 @@ -335,7 +335,9 @@ static void buffer_cb(struct vchiq_mmal_
19 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
22 - } else if (length == 0) {
27 if (dev->capture.frame_count) {
28 /* empty buffer whilst capturing - expected to be an
29 @@ -361,71 +363,72 @@ static void buffer_cb(struct vchiq_mmal_
30 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
31 complete(&dev->capture.frame_cmplt);
34 - if (dev->capture.frame_count) {
35 - if (dev->capture.vc_start_timestamp == -1) {
36 - buf->vb.vb2_buf.timestamp = ktime_get_ns();
37 - v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
38 - "Buffer time set as current time - %lld",
39 - buf->vb.vb2_buf.timestamp);
41 - } else if (pts != 0) {
43 - s64 runtime_us = pts -
44 - dev->capture.vc_start_timestamp;
45 - timestamp = ktime_add_us(dev->capture.kernel_start_ts,
47 - v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
48 - "Convert start time %llu and %llu with offset %llu to %llu\n",
49 - ktime_to_ns(dev->capture.kernel_start_ts),
50 - dev->capture.vc_start_timestamp, pts,
51 - ktime_to_ns(timestamp));
52 - buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
54 - if (dev->capture.last_timestamp) {
55 - buf->vb.vb2_buf.timestamp =
56 - dev->capture.last_timestamp;
57 - v4l2_dbg(1, bcm2835_v4l2_debug,
59 - "Buffer time set as last timestamp - %lld",
60 - buf->vb.vb2_buf.timestamp);
62 - buf->vb.vb2_buf.timestamp =
63 - ktime_to_ns(dev->capture.kernel_start_ts);
64 - v4l2_dbg(1, bcm2835_v4l2_debug,
66 - "Buffer time set as start timestamp - %lld",
67 - buf->vb.vb2_buf.timestamp);
70 - dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
74 - vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
75 - if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
76 - buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
77 + if (!dev->capture.frame_count) {
78 + /* signal frame completion */
79 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
80 + complete(&dev->capture.frame_cmplt);
84 + if (dev->capture.vc_start_timestamp == -1) {
86 + * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
87 + * kernel time, and have no latency compensation.
89 + buf->vb.vb2_buf.timestamp = ktime_get_ns();
90 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
91 + "Buffer time set as current time - %lld",
92 + buf->vb.vb2_buf.timestamp);
93 + } else if (pts != 0) {
95 + s64 runtime_us = pts -
96 + dev->capture.vc_start_timestamp;
97 + timestamp = ktime_add_us(dev->capture.kernel_start_ts,
99 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
100 + "Convert start time %llu and %llu with offset %llu to %llu\n",
101 + ktime_to_ns(dev->capture.kernel_start_ts),
102 + dev->capture.vc_start_timestamp, pts,
103 + ktime_to_ns(timestamp));
104 + buf->vb.vb2_buf.timestamp = ktime_to_ns(timestamp);
106 + if (dev->capture.last_timestamp) {
107 + buf->vb.vb2_buf.timestamp = dev->capture.last_timestamp;
108 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
109 - "Buffer has ts %llu",
110 - dev->capture.last_timestamp);
111 - vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
113 - if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
114 - is_capturing(dev)) {
115 - v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
116 - "Grab another frame as buffer has EOS");
117 - vchiq_mmal_port_parameter_set(
119 - dev->capture.camera_port,
120 - MMAL_PARAMETER_CAPTURE,
121 - &dev->capture.frame_count,
122 - sizeof(dev->capture.frame_count));
124 + "Buffer time set as last timestamp - %lld",
125 + buf->vb.vb2_buf.timestamp);
127 - /* signal frame completion */
128 - vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
129 - complete(&dev->capture.frame_cmplt);
130 + buf->vb.vb2_buf.timestamp =
131 + ktime_to_ns(dev->capture.kernel_start_ts);
132 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
133 + "Buffer time set as start timestamp - %lld",
134 + buf->vb.vb2_buf.timestamp);
137 + dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
139 + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, length);
140 + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_KEYFRAME)
141 + buf->vb.flags |= V4L2_BUF_FLAG_KEYFRAME;
143 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
144 + "Buffer has ts %llu",
145 + dev->capture.last_timestamp);
146 + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
148 + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
149 + is_capturing(dev)) {
150 + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
151 + "Grab another frame as buffer has EOS");
152 + vchiq_mmal_port_parameter_set(instance,
153 + dev->capture.camera_port,
154 + MMAL_PARAMETER_CAPTURE,
155 + &dev->capture.frame_count,
156 + sizeof(dev->capture.frame_count));
160 static int enable_camera(struct bm2835_mmal_dev *dev)
161 @@ -815,27 +818,29 @@ static int vidioc_overlay(struct file *f
163 ret = vchiq_mmal_port_set_format(dev->instance, src);
168 ret = set_overlay_params(dev, dst);
173 - if (enable_camera(dev) < 0)
175 + if (enable_camera(dev) < 0) {
180 ret = vchiq_mmal_component_enable(
182 dev->component[MMAL_COMPONENT_PREVIEW]);
187 v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
189 ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
191 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);