[PATCH] media: atomisp: ov2722: flush buffered writes before they overflow

From: Pengpeng Hou

Date: Mon Mar 23 2026 - 08:25:58 EST


__ov2722_buf_reg_array() appends 8-bit or 16-bit values to the buffered
register-write payload and only checks whether it should flush after the
new value has already been written. When ctrl->index points at the last
byte of the fixed 30-byte data buffer and the next register is 16-bit,
the helper writes one byte past the end of the local buffer before the
flush threshold check runs.

Check whether the next value fits before writing it. If not, flush the
current buffered write first and then append the new value.

Signed-off-by: Pengpeng Hou <pengpeng@xxxxxxxxxxx>
---
.../media/atomisp/i2c/atomisp-ov2722.c | 22 ++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/media/atomisp/i2c/atomisp-ov2722.c b/drivers/staging/media/atomisp/i2c/atomisp-ov2722.c
index 2c41c496daa6..691192d096fe 100644
--- a/drivers/staging/media/atomisp/i2c/atomisp-ov2722.c
+++ b/drivers/staging/media/atomisp/i2c/atomisp-ov2722.c
@@ -169,26 +169,42 @@ static int __ov2722_buf_reg_array(struct i2c_client *client,
const struct ov2722_reg *next)
{
int size;
+ int ret;
__be16 *data16;

switch (next->type) {
case OV2722_8BIT:
size = 1;
- ctrl->buffer.data[ctrl->index] = (u8)next->val;
break;
case OV2722_16BIT:
size = 2;
- data16 = (void *)&ctrl->buffer.data[ctrl->index];
- *data16 = cpu_to_be16((u16)next->val);
break;
default:
return -EINVAL;
}

+ if (ctrl->index + size > OV2722_MAX_WRITE_BUF_SIZE) {
+ ret = __ov2722_flush_reg_array(client, ctrl);
+ if (ret)
+ return ret;
+ }
+
/* When first item is added, we need to store its starting address */
if (ctrl->index == 0)
ctrl->buffer.addr = next->reg;

+ switch (next->type) {
+ case OV2722_8BIT:
+ ctrl->buffer.data[ctrl->index] = (u8)next->val;
+ break;
+ case OV2722_16BIT:
+ data16 = (void *)&ctrl->buffer.data[ctrl->index];
+ *data16 = cpu_to_be16((u16)next->val);
+ break;
+ default:
+ return -EINVAL;
+ }
+
ctrl->index += size;

/*
--
2.50.1 (Apple Git-155)