Skip to contentSkip to conversation

Input: raydium_i2c_ts - validate report parameters

PATCH Superseded Pooyan Azad 3 participants 15 messages Sep 27 – Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: raydium_i2c_ts - validate report parameters

The controller supplies packet and per-contact sizes used to allocate and parse touch reports. The driver trusts these values without validation.

A packet size smaller than the two-byte checksum makes report_size wrap, allowing the IRQ handler to read beyond the report buffer. A zero or undersized contact size can cause a divide by zero or make the contact parser read beyond a record.

Validate both sizes before publishing them, and reject reports that describe more contacts than the input device has slots.

Allocate the report buffer once valid main firmware information is available, and resize it if a firmware update changes the packet size. This also handles devices that probe in bootloader mode, where the packet size is not known yet.

Finally, return main firmware query failures from initialization so probe and firmware update do not continue with invalid report parameters. Keep bootloader HWID query failures non-fatal so the recovery interface remains available.

Fixes 48a2b783483b ("Input: add Raydium I2C touchscreen driver")

---
This partially overlaps Muhammad Bilal's earlier patch linked above. That patch propagates both main and bootloader query errors. This version keeps bootloader HWID errors non-fatal so recovery remains available, and moves report-buffer allocation into the main query path so size changes can be handled safely.

Compile-tested with:

  make O=/tmp/raydium-build W=1 -j$(nproc) \
    drivers/input/touchscreen/raydium_i2c_ts.o
 drivers/input/touchscreen/raydium_i2c_ts.c | 64 +++++++++++++---------
 1 file changed, 39 insertions(+), 25 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +39 -25
index 0256055abcef..1d7f53d0b9fe 100644
@@ -64,6 +64,7 @@
 #define RM_CONTACT_PRESSURE_POS	5
 #define RM_CONTACT_WIDTH_X_POS	6
 #define RM_CONTACT_WIDTH_Y_POS	7
+#define RM_MIN_CONTACT_SIZE	(RM_CONTACT_WIDTH_Y_POS + 1)
 
 /* Bootloader relative info */
 #define RM_BL_WRT_CMD_SIZE	3	/* bl flash wrt cmd size */
@@ -331,7 +332,10 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 {
 	struct i2c_client *client = ts->client;
 	struct raydium_data_info data_info;
+	struct raydium_info info;
 	__le32 query_bank_addr;
+	u8 *report_data;
+	u8 report_size;
 
 	int error, retry_cnt;
 
@@ -341,26 +345,22 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 		if (error)
 			continue;
 
-		/*
-		 * Warn user if we already allocated memory for reports and
-		 * then the size changed (due to firmware update?) and keep
-		 * old size instead.
-		 */
-		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
-			dev_warn(&client->dev,
-				 "report size changes, was: %d, new: %d\n",
-				 ts->pkg_size, data_info.pkg_size);
-		} else {
-			ts->pkg_size = data_info.pkg_size;
-			ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
+		if (data_info.pkg_size < RM_PACKET_CRC_SIZE) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
 		}
 
-		ts->contact_size = data_info.tp_info_size;
-		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
-
-		dev_dbg(&client->dev,
-			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
-			ts->data_bank_addr, ts->report_size, ts->contact_size);
+		report_size = data_info.pkg_size - RM_PACKET_CRC_SIZE;
+		if (data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
+		    data_info.tp_info_size > report_size ||
+		    report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
+		}
 
 		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 					 &query_bank_addr,
@@ -369,10 +369,29 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 			continue;
 
 		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
-					 &ts->info, sizeof(ts->info));
+					 &info, sizeof(info));
 		if (error)
 			continue;
 
+		if (!ts->report_data || ts->pkg_size != data_info.pkg_size) {
+			report_data = devm_krealloc(&client->dev, ts->report_data,
+						    data_info.pkg_size, GFP_KERNEL);
+			if (!report_data)
+				return -ENOMEM;
+
+			ts->report_data = report_data;
+		}
+
+		ts->pkg_size = data_info.pkg_size;
+		ts->report_size = report_size;
+		ts->contact_size = data_info.tp_info_size;
+		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
+		ts->info = info;
+
+		dev_dbg(&client->dev,
+			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
+			ts->data_bank_addr, ts->report_size, ts->contact_size);
+
 		return 0;
 	}
 
@@ -428,7 +447,7 @@ static int raydium_i2c_initialize(struct raydium_data *ts)
 	if (ts->boot_mode == RAYDIUM_TS_BLDR)
 		raydium_i2c_query_ts_bootloader_info(ts);
 	else
-		raydium_i2c_query_ts_info(ts);
+		error = raydium_i2c_query_ts_info(ts);
 
 	return error;
 }
@@ -1116,11 +1135,6 @@ static int raydium_i2c_probe(struct i2c_client *client)
 		return error;
 	}
 
-	ts->report_data = devm_kmalloc(&client->dev,
-				       ts->pkg_size, GFP_KERNEL);
-	if (!ts->report_data)
-		return -ENOMEM;
-
 	ts->input = devm_input_allocate_device(&client->dev);
 	if (!ts->input) {
 		dev_err(&client->dev, "Failed to allocate input device\n");
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Pooyan,

Nice fix, the size validation and resize-on-update handling both look correct. Two quick questions:

1. Does the IRQ handler independently clamp the per-packet touch count against RM_MAX_TOUCH_NUM, or does it trust the count once these static sizes pass?

2. raydium_i2c_query_ts_info() can rerun after a firmware update. Is the devm_krealloc() of ts->report_data protected from a racing IRQ handler?

Thanks,
Muhammad

Quoted 159 lines · Pooyan Azad

The controller supplies packet and per-contact sizes used to allocate and parse touch reports. The driver trusts these values without validation.

A packet size smaller than the two-byte checksum makes report_size wrap, allowing the IRQ handler to read beyond the report buffer. A zero or undersized contact size can cause a divide by zero or make the contact parser read beyond a record.

Validate both sizes before publishing them, and reject reports that describe more contacts than the input device has slots.

Allocate the report buffer once valid main firmware information is available, and resize it if a firmware update changes the packet size. This also handles devices that probe in bootloader mode, where the packet size is not known yet.

Finally, return main firmware query failures from initialization so probe and firmware update do not continue with invalid report parameters. Keep bootloader HWID query failures non-fatal so the recovery interface remains available.

Fixes 48a2b783483b ("Input: add Raydium I2C touchscreen driver")

---
This partially overlaps Muhammad Bilal's earlier patch linked above. That patch propagates both main and bootloader query errors. This version keeps bootloader HWID errors non-fatal so recovery remains available, and moves report-buffer allocation into the main query path so size changes can be handled safely.

Compile-tested with:

  make O=/tmp/raydium-build W=1 -j$(nproc) \
    drivers/input/touchscreen/raydium_i2c_ts.o
 drivers/input/touchscreen/raydium_i2c_ts.c | 64 +++++++++++++---------
 1 file changed, 39 insertions(+), 25 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +39 -26
index 0256055abcef..1d7f53d0b9fe 100644
@@ -64,6 +64,7 @@
 #define RM_CONTACT_PRESSURE_POS        5
 #define RM_CONTACT_WIDTH_X_POS 6
 #define RM_CONTACT_WIDTH_Y_POS 7
+#define RM_MIN_CONTACT_SIZE    (RM_CONTACT_WIDTH_Y_POS + 1)
 
 /* Bootloader relative info */
 #define RM_BL_WRT_CMD_SIZE     3       /* bl flash wrt cmd size */
@@ -331,7 +332,10 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 {
        struct i2c_client *client = ts->client;
        struct raydium_data_info data_info;
+       struct raydium_info info;
        __le32 query_bank_addr;
+       u8 *report_data;
+       u8 report_size;
 
        int error, retry_cnt;
 
@@ -341,26 +345,22 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
                if (error)
                        continue;
 
-               /*
-                * Warn user if we already allocated memory for reports and
-                * then the size changed (due to firmware update?) and keep
-                * old size instead.
-                */
-               if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
-                       dev_warn(&client->dev,
-                                "report size changes, was: %d, new: %d\n",
-                                ts->pkg_size, data_info.pkg_size);
-               } else {
-                       ts->pkg_size = data_info.pkg_size;
-                       ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
+               if (data_info.pkg_size < RM_PACKET_CRC_SIZE) {
+                       dev_err(&client->dev,
+                               "invalid report sizes: packet=%u contact=%u\n",
+                               data_info.pkg_size, data_info.tp_info_size);
+                       return -EINVAL;
                }
 
-               ts->contact_size = data_info.tp_info_size;
-               ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
-
-               dev_dbg(&client->dev,
-                       "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
-                       ts->data_bank_addr, ts->report_size, ts->contact_size);
+               report_size = data_info.pkg_size - RM_PACKET_CRC_SIZE;
+               if (data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
+                   data_info.tp_info_size > report_size ||
+                   report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
+                       dev_err(&client->dev,
+                               "invalid report sizes: packet=%u contact=%u\n",
+                               data_info.pkg_size, data_info.tp_info_size);
+                       return -EINVAL;
+               }
 
                error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
                                         &query_bank_addr,
@@ -369,10 +369,29 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
                        continue;
 
                error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
-                                        &ts->info, sizeof(ts->info));
+                                        &info, sizeof(info));
                if (error)
                        continue;
 
+               if (!ts->report_data || ts->pkg_size != data_info.pkg_size) {
+                       report_data = devm_krealloc(&client->dev, ts->report_data,
+                                                   data_info.pkg_size, GFP_KERNEL);
+                       if (!report_data)
+                               return -ENOMEM;
+
+                       ts->report_data = report_data;
+               }
+
+               ts->pkg_size = data_info.pkg_size;
+               ts->report_size = report_size;
+               ts->contact_size = data_info.tp_info_size;
+               ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
+               ts->info = info;
+
+               dev_dbg(&client->dev,
+                       "data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
+                       ts->data_bank_addr, ts->report_size, ts->contact_size);
+
                return 0;
        }
 
@@ -428,7 +447,7 @@ static int raydium_i2c_initialize(struct raydium_data *ts)
        if (ts->boot_mode == RAYDIUM_TS_BLDR)
                raydium_i2c_query_ts_bootloader_info(ts);
        else
-               raydium_i2c_query_ts_info(ts);
+               error = raydium_i2c_query_ts_info(ts);
 
        return error;
 }
@@ -1116,11 +1135,6 @@ static int raydium_i2c_probe(struct i2c_client *client)
                return error;
        }
 
-       ts->report_data = devm_kmalloc(&client->dev,
-                                      ts->pkg_size, GFP_KERNEL);
-       if (!ts->report_data)
-               return -ENOMEM;
-
        ts->input = devm_input_allocate_device(&client->dev);
        if (!ts->input) {
                dev_err(&client->dev, "Failed to allocate input device\n");
--

2.43.0

#2 lore raw

Hi Muhammad,

Thanks for the review.

There is no per-packet touch count in the IRQ path; it uses report_size / contact_size. Those values are validated by the query, including the RM_MAX_TOUCH_NUM limit.

The resize is safe too: query happens before requesting the IRQ at probe, or with the IRQ disabled during firmware update/resume. disable_irq() waits for a running threaded handler to finish.

Thanks,
Pooyan

maintainer
#3 lore raw
In reply to Pooyan Azad

Hi Pooyan,

Quoted 16 lines · Pooyan Azad

The controller supplies packet and per-contact sizes used to allocate and parse touch reports. The driver trusts these values without validation.

A packet size smaller than the two-byte checksum makes report_size wrap, allowing the IRQ handler to read beyond the report buffer. A zero or undersized contact size can cause a divide by zero or make the contact parser read beyond a record.

Validate both sizes before publishing them, and reject reports that describe more contacts than the input device has slots.

Allocate the report buffer once valid main firmware information is available, and resize it if a firmware update changes the packet size. This also handles devices that probe in bootloader mode, where the packet size is not known yet.

Quoted 4 lines

Finally, return main firmware query failures from initialization so probe and firmware update do not continue with invalid report parameters. Keep bootloader HWID query failures non-fatal so the recovery interface remains available.

It looks like there ate 3 somewhat independent changes. Please split the incoming data validation from the buffer management and handling bootloader query failures. I think only the data validation needs to go into stable, the rest are regular behavior improvements.

Thanks.

Signature · 1 line
Dmitry
#4 lore raw

Thanks Dmitry,

Sure, I'll split these up in v2 and keep the validation fix separate for stable.

Thanks,
Pooyan

Quoted 34 lines · Dmitry Torokhov

Hi Pooyan,

Quoted 16 lines · Pooyan Azad

The controller supplies packet and per-contact sizes used to allocate and parse touch reports. The driver trusts these values without validation.

A packet size smaller than the two-byte checksum makes report_size wrap, allowing the IRQ handler to read beyond the report buffer. A zero or undersized contact size can cause a divide by zero or make the contact parser read beyond a record.

Validate both sizes before publishing them, and reject reports that describe more contacts than the input device has slots.

Allocate the report buffer once valid main firmware information is available, and resize it if a firmware update changes the packet size. This also handles devices that probe in bootloader mode, where the packet size is not known yet.

Quoted 4 lines

Finally, return main firmware query failures from initialization so probe and firmware update do not continue with invalid report parameters. Keep bootloader HWID query failures non-fatal so the recovery interface remains available.

It looks like there ate 3 somewhat independent changes. Please split the incoming data validation from the buffer management and handling bootloader query failures. I think only the data validation needs to go into stable, the rest are regular behavior improvements.

Thanks.

--
Dmitry

#5 lore raw

v2 cover Input: raydium_i2c_ts report handling fixes

In reply to Pooyan Azad

Hi Dmitry,

This is v2 of the Raydium report parameter patch, split as requested.

Patch 1 validates the packet and contact sizes supplied by the controller. It also propagates main firmware query errors because otherwise a validation failure would be ignored by initialization. This is the only patch marked for stable.

Patch 2 resizes an existing report buffer when a firmware update changes the packet size. Patch 3 moves the initial allocation to the main firmware query so devices can remain usable for recovery when they probe in bootloader mode.

Changes in v2:
- Split validation, buffer resizing, and bootloader-mode allocation handling into separate patches.
- Mark only the validation fix for stable.
- Rebase onto v7.3-rc5.

The series was compile-tested with:

  make O=/tmp/raydium-build W=1 -j$(nproc) \
       drivers/input/touchscreen/raydium_i2c_ts.o

No runtime testing with a Raydium controller was performed.

Thanks,
Pooyan

Pooyan Azad (3):
Input: raydium_i2c_ts - validate report parameters
Input: raydium_i2c_ts - resize report buffer after firmware update
Input: raydium_i2c_ts - defer report buffer allocation

 drivers/input/touchscreen/raydium_i2c_ts.c | 64 +++++++++++++---------
 1 file changed, 39 insertions(+), 25 deletions(-)
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 1/3 Input: raydium_i2c_ts - validate report parameters

The controller supplies packet and per-contact sizes used to allocate and parse touch reports. The driver trusts these values without validation.

A packet size smaller than the two-byte checksum makes report_size wrap, allowing the IRQ handler to read beyond the report buffer. A zero or undersized contact size can cause a divide by zero or make the contact parser read beyond a record.

Validate both sizes before publishing them, and reject reports that describe more contacts than the input device has slots. Return main firmware query errors from initialization as well; otherwise rejecting invalid parameters would not stop probe or firmware update.

---
 drivers/input/touchscreen/raydium_i2c_ts.c | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +21 -2
index 0256055abcef..03ea0ae62999 100644
@@ -64,6 +64,7 @@
 #define RM_CONTACT_PRESSURE_POS	5
 #define RM_CONTACT_WIDTH_X_POS	6
 #define RM_CONTACT_WIDTH_Y_POS	7
+#define RM_MIN_CONTACT_SIZE	(RM_CONTACT_WIDTH_Y_POS + 1)
 
 /* Bootloader relative info */
 #define RM_BL_WRT_CMD_SIZE	3	/* bl flash wrt cmd size */
@@ -332,6 +333,7 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 	struct i2c_client *client = ts->client;
 	struct raydium_data_info data_info;
 	__le32 query_bank_addr;
+	u8 report_size;
 
 	int error, retry_cnt;
 
@@ -341,6 +343,23 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 		if (error)
 			continue;
 
+		if (data_info.pkg_size < RM_PACKET_CRC_SIZE) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
+		}
+
+		report_size = data_info.pkg_size - RM_PACKET_CRC_SIZE;
+		if (data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
+		    data_info.tp_info_size > report_size ||
+		    report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
+		}
+
 		/*
 		 * Warn user if we already allocated memory for reports and
 		 * then the size changed (due to firmware update?) and keep
@@ -352,7 +371,7 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 				 ts->pkg_size, data_info.pkg_size);
 		} else {
 			ts->pkg_size = data_info.pkg_size;
-			ts->report_size = ts->pkg_size - RM_PACKET_CRC_SIZE;
+			ts->report_size = report_size;
 		}
 
 		ts->contact_size = data_info.tp_info_size;
@@ -428,7 +447,7 @@ static int raydium_i2c_initialize(struct raydium_data *ts)
 	if (ts->boot_mode == RAYDIUM_TS_BLDR)
 		raydium_i2c_query_ts_bootloader_info(ts);
 	else
-		raydium_i2c_query_ts_info(ts);
+		error = raydium_i2c_query_ts_info(ts);
 
 	return error;
 }
Signature · 1 line
2.43.0
maintainer
#7 lore raw

Hi Pooyan,

On Mon, Sep 28, 2026 at 06:26:16PM +0200, Pooyan Azad wrote:
...

Quoted 6 lines
drivers/input/touchscreen/raydium_i2c_ts.c +6 -0
+		if (data_info.pkg_size < RM_PACKET_CRC_SIZE) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
+		}

drivers/input/touchscreen/raydium_i2c_ts.c static int raydium_i2c_query_ts_info(struct raydium_data *ts) · line 361

Quoted 10 lines
drivers/input/touchscreen/raydium_i2c_ts.c +10 -0
+
+		report_size = data_info.pkg_size - RM_PACKET_CRC_SIZE;
+		if (data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
+		    data_info.tp_info_size > report_size ||
+		    report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
+			dev_err(&client->dev,
+				"invalid report sizes: packet=%u contact=%u\n",
+				data_info.pkg_size, data_info.tp_info_size);
+			return -EINVAL;
+		}

I folded these two checks and duplicate error messages using check_sub_overflow():

	if (check_sub_overflow(data_info.pkg_size, RM_PACKET_CRC_SIZE,
			       &report_size) ||
	    data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
	    data_info.tp_info_size > report_size ||
	    report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
		dev_err(&client->dev,
			"invalid report sizes: packet=%u contact=%u\n",
			data_info.pkg_size, data_info.tp_info_size);
		return -EINVAL;
	}

Applied the updated patch to for-linus.

Thanks.

Signature · 1 line
Dmitry
#8 lore raw

Hi Dmitry,

Thanks for applying the first patch and simplifying the validation checks.

I also appreciate your detailed review of patches 2 and 3. I'll rework them into a single patch using a fixed-size buffer and look into the boot_mode issue.

Best,
Pooyan

Quoted 41 lines · Dmitry Torokhov

Hi Pooyan,

On Mon, Sep 28, 2026 at 06:26:16PM +0200, Pooyan Azad wrote:
...

Quoted 8 lines

+ if (data_info.pkg_size < RM_PACKET_CRC_SIZE) {
+ dev_err(&client->dev,
+ "invalid report sizes: packet=%u contact=%u\n",
+ data_info.pkg_size, data_info.tp_info_size);
+ return -EINVAL;
+ }
+
+ report_size = data_info.pkg_size - RM_PACKET_CRC_SIZE;

Quoted 8 lines

+ if (data_info.tp_info_size < RM_MIN_CONTACT_SIZE ||
+ data_info.tp_info_size > report_size ||
+ report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) {
+ dev_err(&client->dev,
+ "invalid report sizes: packet=%u contact=%u\n",
+ data_info.pkg_size, data_info.tp_info_size);
+ return -EINVAL;
+ }

I folded these two checks and duplicate error messages using check_sub_overflow():

if (check_sub_overflow(data_info.pkg_size, RM_PACKET_CRC_SIZE, &report_size) ||
data_info.tp_info_size < RM_MIN_CONTACT_SIZE || data_info.tp_info_size > report_size ||
report_size / data_info.tp_info_size > RM_MAX_TOUCH_NUM) { dev_err(&client->dev,
"invalid report sizes: packet=%u contact=%u\n", data_info.pkg_size, data_info.tp_info_size);
return -EINVAL;
}

Applied the updated patch to for-linus.

Thanks.

--
Dmitry

#9 lore raw

PATCH v2 2/3 Input: raydium_i2c_ts - resize report buffer after firmware update

In reply to Pooyan Azad

A firmware update can change the packet size. The driver currently warns and keeps the old report buffer in that case, while still updating other report parameters. This leaves the buffer and parser state inconsistent.

Resize an existing report buffer after all device information has been read successfully. Keep the new parameters local until the resize succeeds so an error leaves the previous configuration intact.

---
 drivers/input/touchscreen/raydium_i2c_ts.c | 44 +++++++++++-----------
 1 file changed, 22 insertions(+), 22 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +22 -22
index 03ea0ae62999..00990c61010f 100644
@@ -332,7 +332,9 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 {
 	struct i2c_client *client = ts->client;
 	struct raydium_data_info data_info;
+	struct raydium_info info;
 	__le32 query_bank_addr;
+	u8 *report_data;
 	u8 report_size;
 
 	int error, retry_cnt;
@@ -360,27 +362,6 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 			return -EINVAL;
 		}
 
-		/*
-		 * Warn user if we already allocated memory for reports and
-		 * then the size changed (due to firmware update?) and keep
-		 * old size instead.
-		 */
-		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
-			dev_warn(&client->dev,
-				 "report size changes, was: %d, new: %d\n",
-				 ts->pkg_size, data_info.pkg_size);
-		} else {
-			ts->pkg_size = data_info.pkg_size;
-			ts->report_size = report_size;
-		}
-
-		ts->contact_size = data_info.tp_info_size;
-		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
-
-		dev_dbg(&client->dev,
-			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
-			ts->data_bank_addr, ts->report_size, ts->contact_size);
-
 		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 					 &query_bank_addr,
 					 sizeof(query_bank_addr));
@@ -388,10 +369,29 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 			continue;
 
 		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
-					 &ts->info, sizeof(ts->info));
+					 &info, sizeof(info));
 		if (error)
 			continue;
 
+		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
+			report_data = devm_krealloc(&client->dev, ts->report_data,
+						    data_info.pkg_size, GFP_KERNEL);
+			if (!report_data)
+				return -ENOMEM;
+
+			ts->report_data = report_data;
+		}
+
+		ts->pkg_size = data_info.pkg_size;
+		ts->report_size = report_size;
+		ts->contact_size = data_info.tp_info_size;
+		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
+		ts->info = info;
+
+		dev_dbg(&client->dev,
+			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
+			ts->data_bank_addr, ts->report_size, ts->contact_size);
+
 		return 0;
 	}
 
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 3/3 Input: raydium_i2c_ts - defer report buffer allocation

In reply to Pooyan Azad

Devices may probe in bootloader mode, where the main firmware packet size is not available yet. Allocating the report buffer from probe in this state requests zero bytes and leaves a ZERO_SIZE_PTR until firmware is updated.

Allocate the buffer from the main firmware information query instead. The query runs before the IRQ is requested during normal probe and with the IRQ disabled after a firmware update, so changing the allocation point does not introduce a race with the IRQ handler.

---
 drivers/input/touchscreen/raydium_i2c_ts.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +1 -6
index 00990c61010f..1d7f53d0b9fe 100644
@@ -373,7 +373,7 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 		if (error)
 			continue;
 
-		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
+		if (!ts->report_data || ts->pkg_size != data_info.pkg_size) {
 			report_data = devm_krealloc(&client->dev, ts->report_data,
 						    data_info.pkg_size, GFP_KERNEL);
 			if (!report_data)
@@ -1135,11 +1135,6 @@ static int raydium_i2c_probe(struct i2c_client *client)
 		return error;
 	}
 
-	ts->report_data = devm_kmalloc(&client->dev,
-				       ts->pkg_size, GFP_KERNEL);
-	if (!ts->report_data)
-		return -ENOMEM;
-
 	ts->input = devm_input_allocate_device(&client->dev);
 	if (!ts->input) {
 		dev_err(&client->dev, "Failed to allocate input device\n");
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 0/3] Input: raydium_i2c_ts report handling fixes

In reply to Pooyan Azad

Hi Dmitry,

A gentle ping on this v2 series. Please let me know if there is anything else I should address.
Thanks,
Pooyan

Quoted 40 lines · Pooyan Azad

Hi Dmitry,

This is v2 of the Raydium report parameter patch, split as requested.

Patch 1 validates the packet and contact sizes supplied by the controller. It also propagates main firmware query errors because otherwise a validation failure would be ignored by initialization. This is the only patch marked for stable.

Patch 2 resizes an existing report buffer when a firmware update changes the packet size. Patch 3 moves the initial allocation to the main firmware query so devices can remain usable for recovery when they probe in bootloader mode.

Changes in v2:
- Split validation, buffer resizing, and bootloader-mode allocation handling into separate patches.
- Mark only the validation fix for stable.
- Rebase onto v7.3-rc5.

The series was compile-tested with:

  make O=/tmp/raydium-build W=1 -j$(nproc) \
       drivers/input/touchscreen/raydium_i2c_ts.o

No runtime testing with a Raydium controller was performed.

Thanks,
Pooyan

Pooyan Azad (3):
Input: raydium_i2c_ts - validate report parameters
Input: raydium_i2c_ts - resize report buffer after firmware update
Input: raydium_i2c_ts - defer report buffer allocation

 drivers/input/touchscreen/raydium_i2c_ts.c | 64 +++++++++++++---------
 1 file changed, 39 insertions(+), 25 deletions(-)

--
2.43.0

#12 lore raw

Re: [PATCH v2 0/3] Input: raydium_i2c_ts report handling fixes

Hi Dmitry,

Just following up on the Raydium v2 series from September 28.

I split the changes as you suggested, separating the validation fix from the buffer management improvements.

Is there anything else you'd like me to address?

If it would make things easier, would you prefer taking the validation fix (1/3) separately from the other two patches?

Thanks for your time and review.

Best,
Pooyan

Quoted 48 lines · Pooyan Azadparvar

Hi Dmitry,

A gentle ping on this v2 series. Please let me know if there is anything else I should address.
Thanks,
Pooyan

Quoted 40 lines · Pooyan Azad

Hi Dmitry,

This is v2 of the Raydium report parameter patch, split as requested.

Patch 1 validates the packet and contact sizes supplied by the controller. It also propagates main firmware query errors because otherwise a validation failure would be ignored by initialization. This is the only patch marked for stable.

Patch 2 resizes an existing report buffer when a firmware update changes the packet size. Patch 3 moves the initial allocation to the main firmware query so devices can remain usable for recovery when they probe in bootloader mode.

Changes in v2:
- Split validation, buffer resizing, and bootloader-mode allocation handling into separate patches.
- Mark only the validation fix for stable.
- Rebase onto v7.3-rc5.

The series was compile-tested with:

make O=/tmp/raydium-build W=1 -j$(nproc) \
drivers/input/touchscreen/raydium_i2c_ts.o

No runtime testing with a Raydium controller was performed.

Thanks,
Pooyan

Pooyan Azad (3):
Input: raydium_i2c_ts - validate report parameters
Input: raydium_i2c_ts - resize report buffer after firmware update
Input: raydium_i2c_ts - defer report buffer allocation

drivers/input/touchscreen/raydium_i2c_ts.c | 64 +++++++++++++---------

1 file changed, 39 insertions(+), 25 deletions(-)

--
2.43.0

maintainer
#13 lore raw

Re: [PATCH v2 0/3] Input: raydium_i2c_ts report handling fixes

In reply to Pooyan Azad

Hi Pooyan,

On Mon, Sep 28, 2026 at 06:26:15PM +0200, Pooyan Azad wrote:
...

Quoted 3 lines

Patch 2 resizes an existing report buffer when a firmware update changes the packet size. Patch 3 moves the initial allocation to the main firmware query so devices can remain usable for recovery when they probe in bootloader mode.

There is a teardown ordering issue with using devm_krealloc() in patches 2 and 3.

When a device probes in bootloader mode, ts->report_data has no devres entry before devm_request_threaded_irq() is called: in patch 2, devm_kmalloc() with size 0 returns ZERO_SIZE_PTR without calling devres_add(), and in patch 3, ts->report_data is NULL. When a later firmware update transitions the controller to main mode and runs raydium_i2c_query_ts_info(), devm_krealloc() takes its ZERO_OR_NULL_PTR() fallback to devm_kmalloc() and appends the buffer to the devres list after the IRQ. On driver unbind, devres releases resources in reverse order, freeing ts->report_data before free_irq() runs while raydium_i2c_irq() can still execute.

I do not think we need dynamic allocation for the report buffer at all. Since pkg_size is a u8 (at most 255 bytes) and raydium_i2c_read() uses ordinary i2c_transfer() rather than DMA-safe buffers, we can simply embed "u8 report_data[U8_MAX];" in struct raydium_data. That eliminates the separate devres allocation, avoids -ENOMEM during firmware updates and ZERO_SIZE_PTR/NULL handling in bootloader mode, and guarantees the buffer outlives the IRQ. Patches 2 and 3 then collapse into a single patch keeping local staging of info/report parameters in raydium_i2c_query_ts_info() and switching report_data to a fixed-size array.

While looking at bootloader recovery, note that raydium_i2c_fw_update() also unconditionally sets ts->boot_mode = RAYDIUM_TS_MAIN even if raydium_i2c_initialize() returns 0 after seeing RAYDIUM_TS_BLDR, which can leave contact_size at 0 after a bootloader-mode probe.

Thanks.

Signature · 1 line
Dmitry
#14 lore raw

PATCH v3 1/1 Input: raydium_i2c_ts - use a fixed-size report buffer

A firmware update can change the touch report size. The driver keeps its original packet and report sizes while updating the contact size and data bank address. The IRQ handler can then read and parse reports using parameters from different firmware versions.

Embed a buffer large enough for any value of the u8 packet size. The managed IRQ is requested after the driver state is allocated, so devres releases the IRQ before freeing the buffer. An embedded buffer also lets a controller that probed in bootloader mode accept reports after a successful firmware update.

Stage the firmware information and report parameters locally. Publish them together after validation and after all reads succeed.

Fixes 48a2b783483b ("Input: add Raydium I2C touchscreen driver")
Assisted-by Codex:gpt-6-luna

---
Changes in v3

- Dmitry already applied v2 patch 1 to for-linus.
- Combine v2 patches 2 and 3 into one patch.
- Replace dynamic report buffer allocation with an embedded fixed-size array.
- Address Dmitry's devres concern by tying the buffer to the driver state, which outlives the managed IRQ.

Testing

- x86-64 Raydium driver object built with W=1 using GCC 11.4.0.
- Seven mocked cases passed, covering report size limits, query failures, firmware updates, bootloader recovery, and IRQ handling.
- No physical Raydium hardware was tested.

 drivers/input/touchscreen/raydium_i2c_ts.c | 43 +++++++---------------
 1 file changed, 14 insertions(+), 29 deletions(-)
drivers/input/touchscreen/raydium_i2c_ts.c +14 -29
index 68ada037f694..fa6e93bec9d2 100644
@@ -19,6 +19,7 @@
 #include <linux/input.h>
 #include <linux/input/mt.h>
 #include <linux/interrupt.h>
+#include <linux/limits.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/overflow.h>
@@ -129,14 +130,13 @@ struct raydium_data {
 
 	struct mutex sysfs_mutex;
 
-	u8 *report_data;
-
 	u32 data_bank_addr;
 	u8 report_size;
 	u8 contact_size;
 	u8 pkg_size;
 
 	enum raydium_boot_mode boot_mode;
+	u8 report_data[U8_MAX];
 };
 
 /*
@@ -333,6 +333,7 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 {
 	struct i2c_client *client = ts->client;
 	struct raydium_data_info data_info;
+	struct raydium_info info;
 	__le32 query_bank_addr;
 	u8 report_size;
 
@@ -355,27 +356,6 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 			return -EINVAL;
 		}
 
-		/*
-		 * Warn user if we already allocated memory for reports and
-		 * then the size changed (due to firmware update?) and keep
-		 * old size instead.
-		 */
-		if (ts->report_data && ts->pkg_size != data_info.pkg_size) {
-			dev_warn(&client->dev,
-				 "report size changes, was: %d, new: %d\n",
-				 ts->pkg_size, data_info.pkg_size);
-		} else {
-			ts->pkg_size = data_info.pkg_size;
-			ts->report_size = report_size;
-		}
-
-		ts->contact_size = data_info.tp_info_size;
-		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
-
-		dev_dbg(&client->dev,
-			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
-			ts->data_bank_addr, ts->report_size, ts->contact_size);
-
 		error = raydium_i2c_read(client, RM_CMD_QUERY_BANK,
 					 &query_bank_addr,
 					 sizeof(query_bank_addr));
@@ -383,10 +363,20 @@ static int raydium_i2c_query_ts_info(struct raydium_data *ts)
 			continue;
 
 		error = raydium_i2c_read(client, le32_to_cpu(query_bank_addr),
-					 &ts->info, sizeof(ts->info));
+					 &info, sizeof(info));
 		if (error)
 			continue;
 
+		ts->info = info;
+		ts->pkg_size = data_info.pkg_size;
+		ts->report_size = report_size;
+		ts->contact_size = data_info.tp_info_size;
+		ts->data_bank_addr = le32_to_cpu(data_info.data_bank_addr);
+
+		dev_dbg(&client->dev,
+			"data_bank_addr: %#08x, report_size: %d, contact_size: %d\n",
+			ts->data_bank_addr, ts->report_size, ts->contact_size);
+
 		return 0;
 	}
 
@@ -1130,11 +1120,6 @@ static int raydium_i2c_probe(struct i2c_client *client)
 		return error;
 	}
 
-	ts->report_data = devm_kmalloc(&client->dev,
-				       ts->pkg_size, GFP_KERNEL);
-	if (!ts->report_data)
-		return -ENOMEM;
-
 	ts->input = devm_input_allocate_device(&client->dev);
 	if (!ts->input) {
 		dev_err(&client->dev, "Failed to allocate input device\n");

base-commit: a74c1c7406d06839d0eb15b69bca344e62e729fc

Signature · 1 line
2.34.1