From e3feb9db3003d5461be4c186c12225120af3883c Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Thu, 16 Sep 2021 16:32:53 +0100
Subject: [PATCH] staging: bcm2835-codec: Format changed should trigger
 drain

When a format changed event occurs, the spec says that it
triggers an implicit drain, and that needs to be signalled
via -EPIPE.

For BCM2835, the format changed event happens at the point
the format change occurs, so no further buffers exist from
before the resolution changed point. We therefore signal the
last buffer immediately.
We don't have a V4L2 available to us at this point, so set
the videobuf2 queue last_buffer_dequeued flag directly.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 .../staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c | 5 +++++
 1 file changed, 5 insertions(+)

--- a/drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c
+++ b/drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c
@@ -1005,6 +1005,7 @@ static void handle_fmt_changed(struct bc
 		(struct mmal_msg_event_format_changed *)mmal_buf->buffer;
 	struct mmal_parameter_video_interlace_type interlace;
 	int interlace_size = sizeof(interlace);
+	struct vb2_queue *vq;
 	int ret;
 
 	v4l2_dbg(1, debug, &ctx->dev->v4l2_dev, "%s: Format changed: buff size min %u, rec %u, buff num min %u, rec %u\n",
@@ -1074,6 +1075,10 @@ static void handle_fmt_changed(struct bc
 		q_data->field = V4L2_FIELD_NONE;
 	}
 
+	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
+	if (vq->streaming)
+		vq->last_buffer_dequeued = true;
+
 	queue_res_chg_event(ctx);
 }
 
