1 From 6242eec33d0b49a6ea12c60cf98693dc5abc5577 Mon Sep 17 00:00:00 2001
2 From: John Cox <jc@kynesim.co.uk>
3 Date: Sat, 3 Apr 2021 16:27:03 +0100
4 Subject: [PATCH] media: rpivid: Improve stream_on/off conformance &
7 Fix stream on & off such that failures leave the driver in the correct
8 state. Ensure that the clock is on when we are streaming and off when
9 all contexts attached to this device have stopped streaming.
11 Signed-off-by: John Cox <jc@kynesim.co.uk>
13 drivers/staging/media/rpivid/rpivid.c | 8 +-
14 drivers/staging/media/rpivid/rpivid.h | 15 ++-
15 drivers/staging/media/rpivid/rpivid_hw.c | 1 +
16 drivers/staging/media/rpivid/rpivid_video.c | 103 +++++++++++++++-----
17 4 files changed, 101 insertions(+), 26 deletions(-)
19 --- a/drivers/staging/media/rpivid/rpivid.c
20 +++ b/drivers/staging/media/rpivid/rpivid.c
21 @@ -212,9 +212,12 @@ static int rpivid_open(struct file *file
22 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
24 mutex_unlock(&dev->dev_mutex);
30 + mutex_init(&ctx->ctx_mutex);
32 v4l2_fh_init(&ctx->fh, video_devdata(file));
33 file->private_data = &ctx->fh;
35 @@ -245,7 +248,9 @@ static int rpivid_open(struct file *file
37 v4l2_ctrl_handler_free(&ctx->hdl);
39 + mutex_destroy(&ctx->ctx_mutex);
42 mutex_unlock(&dev->dev_mutex);
45 @@ -266,6 +271,7 @@ static int rpivid_release(struct file *f
48 v4l2_fh_exit(&ctx->fh);
49 + mutex_destroy(&ctx->ctx_mutex);
53 --- a/drivers/staging/media/rpivid/rpivid.h
54 +++ b/drivers/staging/media/rpivid/rpivid.h
55 @@ -84,6 +84,11 @@ struct rpivid_q_aux;
56 #define RPIVID_AUX_ENT_COUNT VB2_MAX_FRAME
59 +#define RPIVID_CTX_STATE_STOPPED 0 /* stream_off */
60 +#define RPIVID_CTX_STATE_STREAM_ON 1 /* decoding */
61 +#define RPIVID_CTX_STATE_STREAM_STOP 2 /* in stream_off */
62 +#define RPIVID_CTX_STATE_STREAM_ERR 3 /* stream_on but broken */
66 struct rpivid_dev *dev;
67 @@ -91,10 +96,19 @@ struct rpivid_ctx {
68 struct v4l2_pix_format_mplane src_fmt;
69 struct v4l2_pix_format_mplane dst_fmt;
72 + atomic_t stream_state;
73 + struct clk_request *clk_req;
77 // fatal_err is set if an error has occurred s.t. decode cannot
78 // continue (such as running out of CMA)
81 + /* Lock for queue operations */
82 + struct mutex ctx_mutex;
84 struct v4l2_ctrl_handler hdl;
85 struct v4l2_ctrl **ctrls;
87 @@ -180,7 +194,6 @@ struct rpivid_dev {
88 void __iomem *base_h265;
91 - struct clk_request *hevc_req;
95 --- a/drivers/staging/media/rpivid/rpivid_hw.c
96 +++ b/drivers/staging/media/rpivid/rpivid_hw.c
97 @@ -359,6 +359,7 @@ int rpivid_hw_probe(struct rpivid_dev *d
98 void rpivid_hw_remove(struct rpivid_dev *dev)
100 // IRQ auto freed on unload so no need to do it here
101 + // ioremap auto freed on unload
102 ictl_uninit(&dev->ic_active1);
103 ictl_uninit(&dev->ic_active2);
105 --- a/drivers/staging/media/rpivid/rpivid_video.c
106 +++ b/drivers/staging/media/rpivid/rpivid_video.c
108 #include <media/v4l2-mem2mem.h>
111 +#include "rpivid_hw.h"
112 #include "rpivid_video.h"
113 #include "rpivid_dec.h"
115 @@ -533,33 +534,85 @@ static int rpivid_buf_prepare(struct vb2
119 +/* Only stops the clock if streaom off on both output & capture */
120 +static void stop_clock(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
122 + if (ctx->src_stream_on ||
123 + ctx->dst_stream_on ||
127 + clk_request_done(ctx->clk_req);
128 + ctx->clk_req = NULL;
130 + clk_disable_unprepare(dev->clock);
133 +/* Always starts the clock if it isn't already on this ctx */
134 +static int start_clock(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
136 + long max_hevc_clock;
142 + max_hevc_clock = clk_round_rate(dev->clock, ULONG_MAX);
144 + ctx->clk_req = clk_request_start(dev->clock, max_hevc_clock);
145 + if (!ctx->clk_req) {
146 + dev_err(dev->dev, "Failed to set clock rate\n");
150 + rv = clk_prepare_enable(dev->clock);
152 + dev_err(dev->dev, "Failed to enable clock\n");
153 + clk_request_done(ctx->clk_req);
154 + ctx->clk_req = NULL;
161 static int rpivid_start_streaming(struct vb2_queue *vq, unsigned int count)
163 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
164 struct rpivid_dev *dev = ctx->dev;
165 - long max_hevc_clock = clk_round_rate(dev->clock, ULONG_MAX);
168 - if (ctx->src_fmt.pixelformat != V4L2_PIX_FMT_HEVC_SLICE)
171 - if (V4L2_TYPE_IS_OUTPUT(vq->type) && dev->dec_ops->start)
172 - ret = dev->dec_ops->start(ctx);
173 + if (!V4L2_TYPE_IS_OUTPUT(vq->type)) {
174 + ctx->dst_stream_on = 1;
178 - dev->hevc_req = clk_request_start(dev->clock, max_hevc_clock);
179 - if (!dev->hevc_req) {
180 - dev_err(dev->dev, "Failed to set clock rate\n");
182 + if (ctx->src_fmt.pixelformat != V4L2_PIX_FMT_HEVC_SLICE) {
187 - ret = clk_prepare_enable(dev->clock);
188 + if (ctx->src_stream_on)
191 + ret = start_clock(dev, ctx);
193 - dev_err(dev->dev, "Failed to enable clock\n");
197 + if (dev->dec_ops->start)
198 + ret = dev->dec_ops->start(ctx);
200 - rpivid_queue_cleanup(vq, VB2_BUF_STATE_QUEUED);
201 + goto fail_stop_clock;
203 + ctx->src_stream_on = 1;
208 + stop_clock(dev, ctx);
210 + v4l2_err(&dev->v4l2_dev, "%s: qtype=%d: FAIL\n", __func__, vq->type);
211 + rpivid_queue_cleanup(vq, VB2_BUF_STATE_QUEUED);
215 @@ -568,17 +621,19 @@ static void rpivid_stop_streaming(struct
216 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
217 struct rpivid_dev *dev = ctx->dev;
219 - if (V4L2_TYPE_IS_OUTPUT(vq->type) && dev->dec_ops->stop)
220 - dev->dec_ops->stop(ctx);
221 + if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
222 + ctx->src_stream_on = 0;
223 + if (dev->dec_ops->stop)
224 + dev->dec_ops->stop(ctx);
226 + ctx->dst_stream_on = 0;
229 rpivid_queue_cleanup(vq, VB2_BUF_STATE_ERROR);
233 - clk_request_done(dev->hevc_req);
234 - dev->hevc_req = NULL;
236 - clk_disable_unprepare(dev->clock);
237 + vb2_wait_for_all_buffers(vq);
239 + stop_clock(dev, ctx);
242 static void rpivid_buf_queue(struct vb2_buffer *vb)
243 @@ -622,7 +677,7 @@ int rpivid_queue_init(void *priv, struct
244 src_vq->ops = &rpivid_qops;
245 src_vq->mem_ops = &vb2_dma_contig_memops;
246 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
247 - src_vq->lock = &ctx->dev->dev_mutex;
248 + src_vq->lock = &ctx->ctx_mutex;
249 src_vq->dev = ctx->dev->dev;
250 src_vq->supports_requests = true;
251 src_vq->requires_requests = true;
252 @@ -639,7 +694,7 @@ int rpivid_queue_init(void *priv, struct
253 dst_vq->ops = &rpivid_qops;
254 dst_vq->mem_ops = &vb2_dma_contig_memops;
255 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
256 - dst_vq->lock = &ctx->dev->dev_mutex;
257 + dst_vq->lock = &ctx->ctx_mutex;
258 dst_vq->dev = ctx->dev->dev;
260 return vb2_queue_init(dst_vq);