brcm2708: update to latest patches from RPi foundation
[oweals/openwrt.git] / target / linux / brcm2708 / patches-4.19 / 950-0211-staging-bcm2835-camera-Return-early-on-errors.patch
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
5
6 Fix several instances where it is easier to return
7 early on error conditions than handle it as an else
8 clause.
9 As requested by Mauro.
10
11 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
12 ---
13  .../bcm2835-camera/bcm2835-camera.c           | 137 +++++++++---------
14  1 file changed, 71 insertions(+), 66 deletions(-)
15
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);
20                 }
21                 return;
22 -       } else if (length == 0) {
23 +       }
24 +
25 +       if (length == 0) {
26                 /* stream ended */
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);
32                 }
33 -       } else {
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);
40 -
41 -                       } else if (pts != 0) {
42 -                               ktime_t timestamp;
43 -                               s64 runtime_us = pts -
44 -                                   dev->capture.vc_start_timestamp;
45 -                               timestamp = ktime_add_us(dev->capture.kernel_start_ts,
46 -                                                        runtime_us);
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);
53 -                       } else {
54 -                               if (dev->capture.last_timestamp) {
55 -                                       buf->vb.vb2_buf.timestamp =
56 -                                               dev->capture.last_timestamp;
57 -                                       v4l2_dbg(1, bcm2835_v4l2_debug,
58 -                                                &dev->v4l2_dev,
59 -                                                "Buffer time set as last timestamp - %lld",
60 -                                                buf->vb.vb2_buf.timestamp);
61 -                               } else {
62 -                                       buf->vb.vb2_buf.timestamp =
63 -                                               ktime_to_ns(dev->capture.kernel_start_ts);
64 -                                       v4l2_dbg(1, bcm2835_v4l2_debug,
65 -                                                &dev->v4l2_dev,
66 -                                                "Buffer time set as start timestamp - %lld",
67 -                                                buf->vb.vb2_buf.timestamp);
68 -                               }
69 -                       }
70 -                       dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
71 +               return;
72 +       }
73  
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);
81 +               return;
82 +       }
83  
84 +       if (dev->capture.vc_start_timestamp == -1) {
85 +               /*
86 +                * VPU doesn't support MMAL_PARAMETER_SYSTEM_TIME, rely on
87 +                * kernel time, and have no latency compensation.
88 +                */
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) {
94 +               ktime_t timestamp;
95 +               s64 runtime_us = pts -
96 +                   dev->capture.vc_start_timestamp;
97 +               timestamp = ktime_add_us(dev->capture.kernel_start_ts,
98 +                                        runtime_us);
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);
105 +       } else {
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);
112 -
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(
118 -                                       instance,
119 -                                       dev->capture.camera_port,
120 -                                       MMAL_PARAMETER_CAPTURE,
121 -                                       &dev->capture.frame_count,
122 -                                       sizeof(dev->capture.frame_count));
123 -                       }
124 +                                "Buffer time set as last timestamp - %lld",
125 +                                buf->vb.vb2_buf.timestamp);
126                 } else {
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);
135                 }
136         }
137 +       dev->capture.last_timestamp = buf->vb.vb2_buf.timestamp;
138 +
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;
142 +
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);
147 +
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));
157 +       }
158  }
159  
160  static int enable_camera(struct bm2835_mmal_dev *dev)
161 @@ -815,27 +818,29 @@ static int vidioc_overlay(struct file *f
162  
163         ret = vchiq_mmal_port_set_format(dev->instance, src);
164         if (ret < 0)
165 -               goto error;
166 +               return ret;
167  
168         ret = set_overlay_params(dev, dst);
169         if (ret < 0)
170 -               goto error;
171 +               return ret;
172  
173 -       if (enable_camera(dev) < 0)
174 -               goto error;
175 +       if (enable_camera(dev) < 0) {
176 +               ret = -EINVAL;
177 +               return ret;
178 +       }
179  
180         ret = vchiq_mmal_component_enable(
181                         dev->instance,
182                         dev->component[MMAL_COMPONENT_PREVIEW]);
183         if (ret < 0)
184 -               goto error;
185 +               return ret;
186  
187         v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
188                  src, dst);
189         ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
190         if (!ret)
191                 ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
192 -error:
193 +
194         return ret;
195  }
196