mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] media: rc: ir-spi: reallocate buffer dynamically
@ 2025-06-08 18:58 Cosmin Tanislav
  0 siblings, 0 replies; only message in thread
From: Cosmin Tanislav @ 2025-06-08 18:58 UTC (permalink / raw)
  Cc: Sean Young, Mauro Carvalho Chehab, linux-media, linux-kernel,
	Cosmin Tanislav

Replace the static transmit buffer with a dynamically allocated one,
allowing the buffer to grow as needed based on the length of the
message being transmitted.

Introduce a helper function ir_buf_realloc() to manage the allocation
and reallocation of the buffer. Use it during probe to preallocate
a buffer matching the original static buffer, then reallocate it as
needed, with an overhead to avoid frequent reallocations.

Signed-off-by: Cosmin Tanislav <demonsingur@gmail.com>
---
 drivers/media/rc/ir-spi.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/drivers/media/rc/ir-spi.c b/drivers/media/rc/ir-spi.c
index 8fc8e496e6aa..b13efd61fb8c 100644
--- a/drivers/media/rc/ir-spi.c
+++ b/drivers/media/rc/ir-spi.c
@@ -27,7 +27,8 @@ struct ir_spi_data {
 	u32 freq;
 	bool negated;
 
-	u16 tx_buf[IR_SPI_MAX_BUFSIZE];
+	u16 *tx_buf;
+	size_t tx_len;
 	u16 pulse;
 	u16 space;
 
@@ -36,6 +37,28 @@ struct ir_spi_data {
 	struct regulator *regulator;
 };
 
+static int ir_buf_realloc(struct ir_spi_data *idata, size_t len)
+{
+	u16 *tx_buf;
+
+	if (len <= idata->tx_len)
+		return 0;
+
+	len = max(len, idata->tx_len + IR_SPI_MAX_BUFSIZE);
+
+	tx_buf = devm_kmalloc(&idata->spi->dev, len * sizeof(*tx_buf),
+			      GFP_KERNEL);
+	if (!tx_buf)
+		return -ENOMEM;
+
+	devm_kfree(&idata->spi->dev, idata->tx_buf);
+
+	idata->tx_buf = tx_buf;
+	idata->tx_len = len;
+
+	return 0;
+}
+
 static int ir_spi_tx(struct rc_dev *dev, unsigned int *buffer, unsigned int count)
 {
 	int i;
@@ -52,8 +75,9 @@ static int ir_spi_tx(struct rc_dev *dev, unsigned int *buffer, unsigned int coun
 
 		periods = DIV_ROUND_CLOSEST(buffer[i] * idata->freq, 1000000);
 
-		if (len + periods >= IR_SPI_MAX_BUFSIZE)
-			return -EINVAL;
+		ret = ir_buf_realloc(idata, len + periods);
+		if (ret)
+			return ret;
 
 		/*
 		 * The first value in buffer is a pulse, so that 0, 2, 4, ...
@@ -153,6 +177,10 @@ static int ir_spi_probe(struct spi_device *spi)
 
 	idata->freq = IR_SPI_DEFAULT_FREQUENCY;
 
+	ret = ir_buf_realloc(idata, IR_SPI_MAX_BUFSIZE);
+	if (ret)
+		return ret;
+
 	return devm_rc_register_device(dev, idata->rc);
 }
 
-- 
2.49.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-06-08 18:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-08 18:58 [PATCH] media: rc: ir-spi: reallocate buffer dynamically Cosmin Tanislav

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®