Skip to contentSkip to conversation

Input: atmel_mxt_ts: Avoid excess read length on limited controllers

PATCH In review Marek Vasut 2 participants 4 messages Jun 13, 2020 – Nov 9, 2024

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

PATCH 1/1 Input: atmel_mxt_ts: Avoid excess read length on limited controllers

Some I2C controllers have a hard limit on the number of data they can transfer in one transfer (e.g. Xilinx XIIC has 255 bytes). The Atmel MXT touchscreen driver mxt_process_messages_until_invalid() function can trigger a read much longer than that (e.g. 690 bytes in my case). This transfer can however be easily split into multiple shorter ones, esp. since the single T5 message is 10 bytes or so.

This patch adds a check for the quirk presence and if it is present, limits the number of messages read out of the controller such that they are below the quirk limit. This makes it possible for the MXT driver to work even on such limited controllers.

Cc Nick Dyer <[email protected]>
Cc Evan Green <[email protected]>
Cc Dmitry Torokhov <[email protected]>
Cc Sasha Levin <[email protected]>
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 30 ++++++++++++++++++------
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +23 -7
index a2189739e30f5..faa3f3f987d46 100644
@@ -985,21 +985,37 @@ static int mxt_proc_message(struct mxt_data *data, u8 *message)
 
 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
 {
+	const struct i2c_adapter_quirks *q = data->client->adapter->quirks;
 	struct device *dev = &data->client->dev;
-	int ret;
-	int i;
+	int i, ret, offset = 0;
+	u16 rem, chunk = count, total = count;
 	u8 num_valid = 0;
 
 	/* Safety check for msg_buf */
 	if (count > data->max_reportid)
 		return -EINVAL;
 
+	/* Handle controller read-length limitations */
+	if (q && q->max_read_len) {
+		chunk = min((u16)(q->max_read_len / data->T5_msg_size),
+			    (u16)count);
+	}
+
 	/* Process remaining messages if necessary */
-	ret = __mxt_read_reg(data->client, data->T5_address,
-				data->T5_msg_size * count, data->msg_buf);
-	if (ret) {
-		dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
-		return ret;
+	while (total) {
+		rem = min(total, chunk);
+		ret = __mxt_read_reg(data->client, data->T5_address,
+				     data->T5_msg_size * rem,
+				     data->msg_buf +
+					(offset * data->T5_msg_size));
+		if (ret) {
+			dev_err(dev,
+				"Failed to read %u messages (offset %u of total %u) (%d)\n",
+				rem, offset, count, ret);
+			return ret;
+		}
+		total -= rem;
+		offset += rem;
 	}
 
 	for (i = 0;  i < count; i++) {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 60 lines · Marek Vasut

Some I2C controllers have a hard limit on the number of data they can transfer in one transfer (e.g. Xilinx XIIC has 255 bytes). The Atmel MXT touchscreen driver mxt_process_messages_until_invalid() function can trigger a read much longer than that (e.g. 690 bytes in my case). This transfer can however be easily split into multiple shorter ones, esp. since the single T5 message is 10 bytes or so.

This patch adds a check for the quirk presence and if it is present, limits the number of messages read out of the controller such that they are below the quirk limit. This makes it possible for the MXT driver to work even on such limited controllers.

Cc Nick Dyer <[email protected]>
Cc Evan Green <[email protected]>
Cc Dmitry Torokhov <[email protected]>
Cc Sasha Levin <[email protected]>
---
  drivers/input/touchscreen/atmel_mxt_ts.c | 30 ++++++++++++++++++------
  1 file changed, 23 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +16 -7
index a2189739e30f5..faa3f3f987d46 100644
@@ -985,21 +985,37 @@ static int mxt_proc_message(struct mxt_data *data, u8 *message)
 
  static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
  {
+	const struct i2c_adapter_quirks *q = data->client->adapter->quirks;
  	struct device *dev = &data->client->dev;
-	int ret;
-	int i;
+	int i, ret, offset = 0;
+	u16 rem, chunk = count, total = count;
  	u8 num_valid = 0;
 
  	/* Safety check for msg_buf */
  	if (count > data->max_reportid)
  		return -EINVAL;
 
+	/* Handle controller read-length limitations */
+	if (q && q->max_read_len) {
+		chunk = min((u16)(q->max_read_len / data->T5_msg_size),
+			    (u16)count);
+	}
+
  	/* Process remaining messages if necessary */
-	ret = __mxt_read_reg(data->client, data->T5_address,
-				data->T5_msg_size * count, data->msg_buf);
-	if (ret) {
-		dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
-		return ret;
+	while (total) {
+		rem = min(total, chunk);
+		ret = __mxt_read_reg(data->client, data->T5_address,
+				     data->T5_msg_size * rem,
+				     data->msg_buf +
+					(offset * data->T5_msg_size));
+		if (ret) {

drivers/input/touchscreen/atmel_mxt_ts.c static int mxt_proc_message(struct mxt_data *data, u8 *message) · line 1021

Quoted 10 lines
drivers/input/touchscreen/atmel_mxt_ts.c +7 -0
+			dev_err(dev,
+				"Failed to read %u messages (offset %u of total %u) (%d)\n",
+				rem, offset, count, ret);
+			return ret;
+		}
+		total -= rem;
+		offset += rem;
  	}
 
  	for (i = 0;  i < count; i++) {

I just found this patch still in my upstreaming queue. Any input on this? It seems to still apply and it is still needed on Xilinx XIIC I2C.

maintainer
#2 lore raw
In reply to Marek Vasut

Hi Marek,

Quoted 35 lines · Marek Vasut

Some I2C controllers have a hard limit on the number of data they can transfer in one transfer (e.g. Xilinx XIIC has 255 bytes). The Atmel MXT touchscreen driver mxt_process_messages_until_invalid() function can trigger a read much longer than that (e.g. 690 bytes in my case). This transfer can however be easily split into multiple shorter ones, esp. since the single T5 message is 10 bytes or so.

This patch adds a check for the quirk presence and if it is present, limits the number of messages read out of the controller such that they are below the quirk limit. This makes it possible for the MXT driver to work even on such limited controllers.

Cc Nick Dyer <[email protected]>
Cc Evan Green <[email protected]>
Cc Dmitry Torokhov <[email protected]>
Cc Sasha Levin <[email protected]>
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 30 ++++++++++++++++++------
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +3 -2
index a2189739e30f5..faa3f3f987d46 100644
@@ -985,21 +985,37 @@ static int mxt_proc_message(struct mxt_data *data, u8 *message)
 
 static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
 {
+	const struct i2c_adapter_quirks *q = data->client->adapter->quirks;
 	struct device *dev = &data->client->dev;
-	int ret;
-	int i;
+	int i, ret, offset = 0;
+	u16 rem, chunk = count, total = count;

drivers/input/touchscreen/atmel_mxt_ts.c static int mxt_proc_message(struct mxt_data *data, u8 *message) · line 1001

Quoted 10 lines
drivers/input/touchscreen/atmel_mxt_ts.c +4 -0
 	u8 num_valid = 0;
 
 	/* Safety check for msg_buf */
 	if (count > data->max_reportid)
 		return -EINVAL;
 
+	/* Handle controller read-length limitations */
+	if (q && q->max_read_len) {
+		chunk = min((u16)(q->max_read_len / data->T5_msg_size),
+			    (u16)count);

I do not think you need this min() here. The "rem = min(total, chunk);" below will take care of cases where chunk is bigger than total count.

Quoted 28 lines
drivers/input/touchscreen/atmel_mxt_ts.c +16 -5
+	}
+
 	/* Process remaining messages if necessary */
-	ret = __mxt_read_reg(data->client, data->T5_address,
-				data->T5_msg_size * count, data->msg_buf);
-	if (ret) {
-		dev_err(dev, "Failed to read %u messages (%d)\n", count, ret);
-		return ret;
+	while (total) {
+		rem = min(total, chunk);
+		ret = __mxt_read_reg(data->client, data->T5_address,
+				     data->T5_msg_size * rem,
+				     data->msg_buf +
+					(offset * data->T5_msg_size));
+		if (ret) {
+			dev_err(dev,
+				"Failed to read %u messages (offset %u of total %u) (%d)\n",
+				rem, offset, count, ret);
+			return ret;
+		}
+		total -= rem;
+		offset += rem;
 	}
 
 	for (i = 0;  i < count; i++) {
Signature · 1 line
2.26.2

Thanks.

Signature · 1 line
Dmitry
#3 lore raw
Quoted 44 lines · Dmitry Torokhov

Hi Marek,

Quoted 41 lines · Marek Vasut

Some I2C controllers have a hard limit on the number of data they can transfer in one transfer (e.g. Xilinx XIIC has 255 bytes). The Atmel MXT touchscreen driver mxt_process_messages_until_invalid() function can trigger a read much longer than that (e.g. 690 bytes in my case). This transfer can however be easily split into multiple shorter ones, esp. since the single T5 message is 10 bytes or so.

This patch adds a check for the quirk presence and if it is present, limits the number of messages read out of the controller such that they are below the quirk limit. This makes it possible for the MXT driver to work even on such limited controllers.

Cc Nick Dyer <[email protected]>
Cc Evan Green <[email protected]>
Cc Dmitry Torokhov <[email protected]>
Cc Sasha Levin <[email protected]>
---
  drivers/input/touchscreen/atmel_mxt_ts.c | 30 ++++++++++++++++++------
  1 file changed, 23 insertions(+), 7 deletions(-)
drivers/input/touchscreen/atmel_mxt_ts.c +3 -2
index a2189739e30f5..faa3f3f987d46 100644
@@ -985,21 +985,37 @@ static int mxt_proc_message(struct mxt_data *data, u8 *message)
 
  static int mxt_read_and_process_messages(struct mxt_data *data, u8 count)
  {
+	const struct i2c_adapter_quirks *q = data->client->adapter->quirks;
  	struct device *dev = &data->client->dev;
-	int ret;
-	int i;
+	int i, ret, offset = 0;
+	u16 rem, chunk = count, total = count;
  	u8 num_valid = 0;
 
  	/* Safety check for msg_buf */
  	if (count > data->max_reportid)
  		return -EINVAL;
 
Quoted 7 lines

drivers/input/touchscreen/atmel_mxt_ts.c static int mxt_proc_message(struct mxt_data *data, u8 *message) · line 1001

Quoted 4 lines
drivers/input/touchscreen/atmel_mxt_ts.c +4 -0
+	/* Handle controller read-length limitations */
+	if (q && q->max_read_len) {
+		chunk = min((u16)(q->max_read_len / data->T5_msg_size),
+			    (u16)count);

I do not think you need this min() here. The "rem = min(total, chunk);" below will take care of cases where chunk is bigger than total count.

'chunk' here has to be limited to at most as many T5 messages as fit into q->max_read_len limit of the controller, that is the purpose of this 'min' here.