Skip to contentSkip to conversation

platform/x86: dell-uart-backlight: recover from stray EC response byte

PATCH Superseded [email protected] 3 participants 17 messages Oct 4 – Nov 11 x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: dell-uart-backlight: recover from stray EC response byte

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

Handle this in dell_uart_bl_receive(): on the first checksum failure for a frame longer than the minimum size, drop the byte at the first payload offset, shift the rest down by one, read one more byte as the new checksum, and re-validate. Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error.

Well-formed replies pass on the first check and never enter the recovery path.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 92 +++++++++++++------
 1 file changed, 62 insertions(+), 30 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +62 -30
index f323a667dc2d..068b91c21bf4 100644
@@ -51,7 +51,7 @@
 #define MAX_RESP_LEN			80
 
 struct dell_uart_backlight {
-	struct mutex mutex;
+	struct mutex mutex; /* Protects command + response state below */
 	wait_queue_head_t wait_queue;
 	struct device *dev;
 	struct backlight_device *bl;
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -208,8 +210,10 @@ static const struct backlight_ops dell_uart_backlight_ops = {
 static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
 {
 	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
+	const int max_passes = 2;
 	size_t i;
 	u8 csum;
+	int pass;
 
 	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
 
@@ -220,42 +224,70 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	}
 
 	i = 0;
-	while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
-		dell_bl->resp[dell_bl->resp_idx] = data[i++];
-
-		switch (dell_bl->resp_idx) {
-		case RESP_LEN: /* Length byte */
-			dell_bl->resp_len = dell_bl->resp[RESP_LEN];
-			if (dell_bl->resp_len < MIN_RESP_LEN ||
-			    dell_bl->resp_len > dell_bl->resp_max_len) {
-				dev_err(dell_bl->dev, "Response length %d out if range %d - %d\n",
-					dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len);
-				dell_bl->status = -EIO;
-				goto wakeup;
+	for (pass = 0; pass < max_passes; pass++) {
+		while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
+			dell_bl->resp[dell_bl->resp_idx] = data[i++];
+
+			switch (dell_bl->resp_idx) {
+			case RESP_LEN: /* Length byte */
+				dell_bl->resp_len = dell_bl->resp[RESP_LEN];
+				if (dell_bl->resp_len < MIN_RESP_LEN ||
+				    dell_bl->resp_len > dell_bl->resp_max_len) {
+					dev_err(dell_bl->dev,
+						"Response length %d out if range %d - %d\n",
+						dell_bl->resp_len, MIN_RESP_LEN,
+						dell_bl->resp_max_len);
+					dell_bl->status = -EIO;
+					goto wakeup;
+				}
+				break;
+			case RESP_CMD: /* CMD byte */
+				if (dell_bl->resp[RESP_CMD] != dell_bl->pending_cmd) {
+					dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
+						dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
+					dell_bl->status = -EIO;
+					goto wakeup;
+				}
+				break;
 			}
-			break;
-		case RESP_CMD: /* CMD byte */
-			if (dell_bl->resp[RESP_CMD] != dell_bl->pending_cmd) {
-				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
-					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
-				dell_bl->status = -EIO;
-				goto wakeup;
-			}
-			break;
+			dell_bl->resp_idx++;
 		}
-		dell_bl->resp_idx++;
-	}
 
-	if (dell_bl->resp_idx != dell_bl->resp_len)
-		return len; /* Response not complete yet */
+		if (dell_bl->resp_idx != dell_bl->resp_len)
+			return i; /* Response not complete yet, wait for more data */
+
+		csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
+		if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
+			dell_bl->status = 0; /* Success */
+			dev_dbg(dell_bl->dev,
+				"Response OK for cmd 0x%02x, len %d: %*ph\n",
+				dell_bl->pending_cmd, dell_bl->resp_len,
+				dell_bl->resp_len, dell_bl->resp);
+			goto wakeup;
+		}
+
+		/*
+		 * Some EC firmware inserts a stray byte right after the cmd-echo
+		 * byte. Try once to drop it and re-fetch the checksum byte from the wire.
+		 */
+		if (!dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+			dell_bl->resync_used = true;
+
+			dev_warn(dell_bl->dev,
+				 "Checksum mismatch got 0x%02x expected 0x%02x, dropping stray byte 0x%02x at offset %d\n",
+				 dell_bl->resp[dell_bl->resp_len - 1], csum,
+				 dell_bl->resp[RESP_DATA], RESP_DATA);
+
+			memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+				dell_bl->resp_len - RESP_DATA - 1);
+			dell_bl->resp_idx = dell_bl->resp_len - 1;
+			continue; /* re-fetch the last (checksum) byte and re-validate */
+		}
 
-	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
-	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
-		dell_bl->status = 0; /* Success */
-	} else {
 		dev_err(dell_bl->dev, "Checksum mismatch got 0x%02x expected 0x%02x\n",
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
 		dell_bl->status = -EIO;
+		goto wakeup;
 	}
 wakeup:
 	wake_up(&dell_bl->wait_queue);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 72 lines · On Sun, 4 Oct 2026, [email protected] wrote:

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

Handle this in dell_uart_bl_receive(): on the first checksum failure for a frame longer than the minimum size, drop the byte at the first payload offset, shift the rest down by one, read one more byte as the new checksum, and re-validate. Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error.

Well-formed replies pass on the first check and never enter the recovery path.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 92 +++++++++++++------
 1 file changed, 62 insertions(+), 30 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +5 -5
index f323a667dc2d..068b91c21bf4 100644
@@ -51,7 +51,7 @@
 #define MAX_RESP_LEN			80
 
 struct dell_uart_backlight {
-	struct mutex mutex;
+	struct mutex mutex; /* Protects command + response state below */
 	wait_queue_head_t wait_queue;
 	struct device *dev;
 	struct backlight_device *bl;
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -208,8 +210,10 @@ static const struct backlight_ops dell_uart_backlight_ops = {
 static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
 {
 	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
+	const int max_passes = 2;
 	size_t i;
 	u8 csum;
+	int pass;
 
 	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
 
@@ -220,42 +224,70 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	}
 
 	i = 0;
-	while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
-		dell_bl->resp[dell_bl->resp_idx] = data[i++];
-
-		switch (dell_bl->resp_idx) {

drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 228

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2 -8
-		case RESP_LEN: /* Length byte */
-			dell_bl->resp_len = dell_bl->resp[RESP_LEN];
-			if (dell_bl->resp_len < MIN_RESP_LEN ||
-			    dell_bl->resp_len > dell_bl->resp_max_len) {
-				dev_err(dell_bl->dev, "Response length %d out if range %d - %d\n",
-					dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len);
-				dell_bl->status = -EIO;
-				goto wakeup;
+	for (pass = 0; pass < max_passes; pass++) {
+		while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {

Hi,

Could the inner part be moved into a function so you wouldn't need to add the pass based approach but could just call the function again? I think that would be much easier to follow than adding a loop for retry.

If you end up doing that, please make the function refactoring in a separate change before adding the extra byte handling.

--
i.

Quoted 83 lines
drivers/platform/x86/dell/dell-uart-backlight.c +55 -17
+			dell_bl->resp[dell_bl->resp_idx] = data[i++];
+
+			switch (dell_bl->resp_idx) {
+			case RESP_LEN: /* Length byte */
+				dell_bl->resp_len = dell_bl->resp[RESP_LEN];
+				if (dell_bl->resp_len < MIN_RESP_LEN ||
+				    dell_bl->resp_len > dell_bl->resp_max_len) {
+					dev_err(dell_bl->dev,
+						"Response length %d out if range %d - %d\n",
+						dell_bl->resp_len, MIN_RESP_LEN,
+						dell_bl->resp_max_len);
+					dell_bl->status = -EIO;
+					goto wakeup;
+				}
+				break;
+			case RESP_CMD: /* CMD byte */
+				if (dell_bl->resp[RESP_CMD] != dell_bl->pending_cmd) {
+					dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
+						dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
+					dell_bl->status = -EIO;
+					goto wakeup;
+				}
+				break;
 			}
-			break;
-		case RESP_CMD: /* CMD byte */
-			if (dell_bl->resp[RESP_CMD] != dell_bl->pending_cmd) {
-				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
-					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
-				dell_bl->status = -EIO;
-				goto wakeup;
-			}
-			break;
+			dell_bl->resp_idx++;
 		}
-		dell_bl->resp_idx++;
-	}
 
-	if (dell_bl->resp_idx != dell_bl->resp_len)
-		return len; /* Response not complete yet */
+		if (dell_bl->resp_idx != dell_bl->resp_len)
+			return i; /* Response not complete yet, wait for more data */
+
+		csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
+		if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
+			dell_bl->status = 0; /* Success */
+			dev_dbg(dell_bl->dev,
+				"Response OK for cmd 0x%02x, len %d: %*ph\n",
+				dell_bl->pending_cmd, dell_bl->resp_len,
+				dell_bl->resp_len, dell_bl->resp);
+			goto wakeup;
+		}
+
+		/*
+		 * Some EC firmware inserts a stray byte right after the cmd-echo
+		 * byte. Try once to drop it and re-fetch the checksum byte from the wire.
+		 */
+		if (!dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+			dell_bl->resync_used = true;
+
+			dev_warn(dell_bl->dev,
+				 "Checksum mismatch got 0x%02x expected 0x%02x, dropping stray byte 0x%02x at offset %d\n",
+				 dell_bl->resp[dell_bl->resp_len - 1], csum,
+				 dell_bl->resp[RESP_DATA], RESP_DATA);
+
+			memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+				dell_bl->resp_len - RESP_DATA - 1);
+			dell_bl->resp_idx = dell_bl->resp_len - 1;
+			continue; /* re-fetch the last (checksum) byte and re-validate */
+		}
 
-	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
-	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
-		dell_bl->status = 0; /* Success */
-	} else {
 		dev_err(dell_bl->dev, "Checksum mismatch got 0x%02x expected 0x%02x\n",
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
 		dell_bl->status = -EIO;
+		goto wakeup;
 	}
 wakeup:
 	wake_up(&dell_bl->wait_queue);
Signature · 1 line
 i.
#2 lore raw

v2 cover platform/x86: dell-uart-backlight: recover from stray EC response byte

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames, causing checksum mismatches and -EIO.

Patch 1 extracts the response parsing into a dell_uart_bl_receive_data() helper so it can be called again after resyncing.

Patch 2 adds the stray byte recovery: on checksum mismatch, drop the byte at the first payload position, shift data down, and call the helper again to re-fetch the checksum byte from the wire.

v1 -> v2:
- Extract dell_uart_bl_receive_data() helper instead of using a for-loop with pass counter, call it again after resync (Ilpo)
- Split into two patches: refactor + feature (Ilpo)

Chitransh Mandhaniya (2):
platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data()
helper
platform/x86: dell-uart-backlight: recover from stray EC response byte

 .../platform/x86/dell/dell-uart-backlight.c   | 63 ++++++++++++++-----
 1 file changed, 48 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 1/2 platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data() helper

From: Chitransh Mandhaniya <[email protected]>

Move the response-byte processing and checksum validation out of dell_uart_bl_receive() into a new dell_uart_bl_receive_data() helper.

No functional change. This is a preparatory refactor so that a follow-up patch can call the helper again after resyncing on a stray byte from the EC.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 42 ++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +27 -15
index f323a667dc2d..b61499d7b3f8 100644
@@ -205,20 +205,13 @@ static const struct backlight_ops dell_uart_backlight_ops = {
 	.get_brightness = dell_uart_get_brightness_op,
 };
 
-static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+/* Process received UART data; returns bytes consumed or -1 if incomplete. */
+static int dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
+				     const u8 *data, size_t len)
 {
-	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
 	size_t i;
 	u8 csum;
 
-	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
-
-	/* Throw away unexpected bytes / remainder of response after an error */
-	if (dell_bl->status != -EBUSY) {
-		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
-		return len;
-	}
-
 	i = 0;
 	while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
 		dell_bl->resp[dell_bl->resp_idx] = data[i++];
@@ -231,7 +224,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response length %d out if range %d - %d\n",
 					dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		case RESP_CMD: /* CMD byte */
@@ -239,7 +232,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
 					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		}
@@ -247,7 +240,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	}
 
 	if (dell_bl->resp_idx != dell_bl->resp_len)
-		return len; /* Response not complete yet */
+		return -1; /* Response not complete yet */
 
 	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
 	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
@@ -257,11 +250,30 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
 		dell_bl->status = -EIO;
 	}
-wakeup:
-	wake_up(&dell_bl->wait_queue);
 	return i;
 }
 
+static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+{
+	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
+	int ret;
+
+	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
+
+	/* Throw away unexpected bytes / remainder of response after an error */
+	if (dell_bl->status != -EBUSY) {
+		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
+		return len;
+	}
+
+	ret = dell_uart_bl_receive_data(dell_bl, data, len);
+	if (ret < 0)
+		return len; /* Response not complete yet */
+
+	wake_up(&dell_bl->wait_queue);
+	return ret;
+}
+
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
 	.receive_buf = dell_uart_bl_receive,
 	.write_wakeup = serdev_device_write_wakeup,
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 2/2 platform/x86: dell-uart-backlight: recover from stray EC response byte

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

On the first checksum failure for a frame longer than the minimum size, drop the byte at the first payload offset, shift the remaining data down by one, and call dell_uart_bl_receive_data() again to re-fetch the checksum byte from the wire and re-validate.

Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error. Well-formed replies pass on the first check and never enter the recovery path.

Tested on Dell OptiPlex AIO 7410 65W (0TKWC4) where the EC inserts a stray byte on every get-brightness response -- resync fixes it transparently.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/platform/x86/dell/dell-uart-backlight.c +22 -1
index b61499d7b3f8..085dcc055554 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -270,8 +272,27 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	if (ret < 0)
 		return len; /* Response not complete yet */
 
+	/* Stray byte after cmd-echo? Drop it, shift data down, re-fetch checksum. */
+	if (dell_bl->status == -EIO &&
+	    !dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+		dell_bl->resync_used = true;
+		dell_bl->status = -EBUSY;
+
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);
+		dell_bl->resp_idx = dell_bl->resp_len - 1;
+
+		/* Re-fetch checksum byte from remaining data */
+		ret = dell_uart_bl_receive_data(dell_bl, data + ret, len - ret);
+		if (ret < 0)
+			return len; /* Need more data for checksum byte */
+	}
+
 	wake_up(&dell_bl->wait_queue);
-	return ret;
+	return len;
 }
 
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
Signature · 1 line
2.43.0
maintainer
#5 lore raw
Quoted 64 lines · On Thu, 15 Oct 2026, [email protected] wrote:

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

On the first checksum failure for a frame longer than the minimum size, drop the byte at the first payload offset, shift the remaining data down by one, and call dell_uart_bl_receive_data() again to re-fetch the checksum byte from the wire and re-validate.

Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error. Well-formed replies pass on the first check and never enter the recovery path.

Tested on Dell OptiPlex AIO 7410 65W (0TKWC4) where the EC inserts a stray byte on every get-brightness response -- resync fixes it transparently.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/platform/x86/dell/dell-uart-backlight.c +14 -0
index b61499d7b3f8..085dcc055554 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -270,8 +272,27 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	if (ret < 0)
 		return len; /* Response not complete yet */
 
+	/* Stray byte after cmd-echo? Drop it, shift data down, re-fetch checksum. */
+	if (dell_bl->status == -EIO &&
+	    !dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+		dell_bl->resync_used = true;
+		dell_bl->status = -EBUSY;
+
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);
+		dell_bl->resp_idx = dell_bl->resp_len - 1;

drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 295

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +8 -1
+
+		/* Re-fetch checksum byte from remaining data */
+		ret = dell_uart_bl_receive_data(dell_bl, data + ret, len - ret);
+		if (ret < 0)
+			return len; /* Need more data for checksum byte */
+	}
+
 	wake_up(&dell_bl->wait_queue);
-	return ret;
+	return len;

Thanks. Structuring it this way makes it much easier to follow than the loop.

What I'm left a bit unsure is how this last return behaves, and why there is the change from ret -> len in this second patch which should be only about adding the extra byte trickery.

If ret is always expected to be equal to len before this patch, it would make more sense to make that change in patch 1?

(I admit this entire code looks a bit more convoluted than what I'd expect to be needed, so I'm not sure if I can follow all this fully.)

Unrelated to this change/series, there seems to be mutex_init() call in dell_uart_bl_serdev_probe() that should be changed to devm_mutex_init() + error handling so one doesn't need to have to make mutex_destroy() call manually (it is currently missing).

Signature · 1 line
 i.
maintainer
#6 lore raw
In reply to [email protected]
Quoted 53 lines · On Thu, 15 Oct 2026, [email protected] wrote:

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

On the first checksum failure for a frame longer than the minimum size, drop the byte at the first payload offset, shift the remaining data down by one, and call dell_uart_bl_receive_data() again to re-fetch the checksum byte from the wire and re-validate.

Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error. Well-formed replies pass on the first check and never enter the recovery path.

Tested on Dell OptiPlex AIO 7410 65W (0TKWC4) where the EC inserts a stray byte on every get-brightness response -- resync fixes it transparently.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/platform/x86/dell/dell-uart-backlight.c +3 -0
index b61499d7b3f8..085dcc055554 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -270,8 +272,27 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	if (ret < 0)
 		return len; /* Response not complete yet */
 
+	/* Stray byte after cmd-echo? Drop it, shift data down, re-fetch checksum. */

drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 285

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +10 -0
+	if (dell_bl->status == -EIO &&
+	    !dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+		dell_bl->resync_used = true;
+		dell_bl->status = -EBUSY;
+
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);

sashiko.dev (an AI review tool so be reasonably critical to its findings) mentions that if ->resp_len check fails due to len being too large, ->status is -EIO and this may end up moving more than there's room in the buffer.

Quoted 15 lines
drivers/platform/x86/dell/dell-uart-backlight.c +9 -1
+		dell_bl->resp_idx = dell_bl->resp_len - 1;
+
+		/* Re-fetch checksum byte from remaining data */
+		ret = dell_uart_bl_receive_data(dell_bl, data + ret, len - ret);
+		if (ret < 0)
+			return len; /* Need more data for checksum byte */
+	}
+
 	wake_up(&dell_bl->wait_queue);
-	return ret;
+	return len;
 }
 
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
Signature · 1 line
 i.
#7 lore raw

v3 cover platform/x86: dell-uart-backlight: stray byte recovery and cleanup

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames, causing checksum mismatches and -EIO.

Patch 1 extracts the response parsing into a dell_uart_bl_receive_data() helper that always returns bytes consumed. The caller checks dell_bl->status to determine the outcome (-EBUSY = incomplete, 0 = success, -EIO = error).

Patch 2 adds the stray byte recovery: on checksum mismatch for a fully received frame, drop the byte at the first payload position, shift data down, and call the helper again to re-fetch the checksum byte. Total consumed bytes are tracked and returned to serdev exactly. Also switches mutex_init() to devm_mutex_init() to fix a missing mutex_destroy() on the teardown path (noted by Ilpo during v2 review).

v2 -> v3:
- Helper always returns bytes consumed, caller checks

    dell_bl->status instead (Ilpo)

- Track total consumed bytes with a separate variable instead of returning len, so serdev gets the exact byte count (Ilpo)
- Guard resync with resp_idx == resp_len to prevent memmove on truncated/out-of-range length field (sashiko.dev via Ilpo)
- Switch mutex_init() to devm_mutex_init() in patch 2 (Ilpo)

v1 -> v2:
- Extract dell_uart_bl_receive_data() helper instead of using a for-loop with pass counter, call it again after resync (Ilpo)
- Split into two patches: refactor + feature (Ilpo)

Chitransh Mandhaniya (2):
platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data()
helper
platform/x86: dell-uart-backlight: recover from stray EC response byte

 .../platform/x86/dell/dell-uart-backlight.c   | 71 ++++++++++++++-----
 1 file changed, 55 insertions(+), 16 deletions(-)
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 1/2 platform/x86: dell-uart-backlight: extract

From: Chitransh Mandhaniya <[email protected]>

Move the response-byte processing and checksum validation out of dell_uart_bl_receive() into a new dell_uart_bl_receive_data() helper.

The helper always returns the number of bytes consumed from the input buffer. The caller checks dell_bl->status to determine the outcome:
-EBUSY means the response is still incomplete, 0 means success, -EIO means an error occurred. This preserves the original behaviour.

No functional change. This is a preparatory refactor so that a follow-up patch can call the helper again after resyncing on a stray byte from the EC.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 42 ++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +27 -15
index f323a667dc2d..af9c5f9a566f 100644
@@ -205,20 +205,13 @@ static const struct backlight_ops dell_uart_backlight_ops = {
 	.get_brightness = dell_uart_get_brightness_op,
 };
 
-static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+/* Process received UART data; returns number of bytes consumed. */
+static size_t dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
+					const u8 *data, size_t len)
 {
-	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
 	size_t i;
 	u8 csum;
 
-	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
-
-	/* Throw away unexpected bytes / remainder of response after an error */
-	if (dell_bl->status != -EBUSY) {
-		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
-		return len;
-	}
-
 	i = 0;
 	while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
 		dell_bl->resp[dell_bl->resp_idx] = data[i++];
@@ -231,7 +224,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response length %d out if range %d - %d\n",
 					dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		case RESP_CMD: /* CMD byte */
@@ -239,7 +232,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
 					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		}
@@ -247,7 +240,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	}
 
 	if (dell_bl->resp_idx != dell_bl->resp_len)
-		return len; /* Response not complete yet */
+		return i; /* Response not complete, status stays -EBUSY */
 
 	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
 	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
@@ -257,11 +250,30 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
 		dell_bl->status = -EIO;
 	}
-wakeup:
-	wake_up(&dell_bl->wait_queue);
 	return i;
 }
 
+static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+{
+	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
+	size_t ret;
+
+	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
+
+	/* Throw away unexpected bytes / remainder of response after an error */
+	if (dell_bl->status != -EBUSY) {
+		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
+		return len;
+	}
+
+	ret = dell_uart_bl_receive_data(dell_bl, data, len);
+	if (dell_bl->status == -EBUSY)
+		return len; /* Response not complete yet */
+
+	wake_up(&dell_bl->wait_queue);
+	return ret;
+}
+
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
 	.receive_buf = dell_uart_bl_receive,
 	.write_wakeup = serdev_device_write_wakeup,
Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 2/2 platform/x86: dell-uart-backlight: recover from stray

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

On the first checksum failure for a fully received frame, drop the byte at the first payload offset, shift the remaining data down by one, and call dell_uart_bl_receive_data() again to re-fetch the checksum byte from the wire. Track total consumed bytes so the exact count is returned to serdev.

The resync is guarded by resp_idx == resp_len to ensure a complete frame was received, preventing memmove on a truncated or out-of-range length field.

Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error. Well-formed replies never enter the recovery path.

While at it, switch mutex_init() to devm_mutex_init() so the mutex is automatically destroyed via devres, fixing a missing mutex_destroy() on the teardown path.

Tested on Dell OptiPlex AIO 7410 65W (0TKWC4) where the EC inserts a stray byte on every get-brightness response -- resync fixes it transparently.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 35 ++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +31 -4
index af9c5f9a566f..b0df479a4658 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -256,7 +258,7 @@ static size_t dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
 static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
 {
 	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
-	size_t ret;
+	size_t consumed;
 
 	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
 
@@ -266,12 +268,35 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 		return len;
 	}
 
-	ret = dell_uart_bl_receive_data(dell_bl, data, len);
+	consumed = dell_uart_bl_receive_data(dell_bl, data, len);
 	if (dell_bl->status == -EBUSY)
 		return len; /* Response not complete yet */
 
+	/*
+	 * Stray byte after cmd-echo? Drop it, shift data down, re-fetch
+	 * checksum. Only attempt when a full frame was received (resp_idx ==
+	 * resp_len) to avoid acting on a truncated length field.
+	 */
+	if (dell_bl->status == -EIO && !dell_bl->resync_used &&
+	    dell_bl->resp_idx == dell_bl->resp_len &&
+	    dell_bl->resp_len > MIN_RESP_LEN) {
+		dell_bl->resync_used = true;
+		dell_bl->status = -EBUSY;
+
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);
+		dell_bl->resp_idx = dell_bl->resp_len - 1;
+
+		consumed += dell_uart_bl_receive_data(dell_bl, data + consumed, len - consumed);
+		if (dell_bl->status == -EBUSY)
+			return len; /* Need more data for checksum byte */
+	}
+
 	wake_up(&dell_bl->wait_queue);
-	return ret;
+	return consumed;
 }
 
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
@@ -291,7 +316,9 @@ static int dell_uart_bl_serdev_probe(struct serdev_device *serdev)
 	if (!dell_bl)
 		return -ENOMEM;
 
-	mutex_init(&dell_bl->mutex);
+	ret = devm_mutex_init(dev, &dell_bl->mutex);
+	if (ret)
+		return ret;
 	init_waitqueue_head(&dell_bl->wait_queue);
 	dell_bl->dev = dev;
 
Signature · 1 line
2.43.0
maintainer
#10 lore raw

Re: [PATCH v3 2/2] platform/x86: dell-uart-backlight: recover from stray

Quoted 27 lines · On Fri, 16 Oct 2026, [email protected] wrote:

From: Chitransh Mandhaniya <[email protected]>

On some Dell AIO machines the EC occasionally inserts a spurious byte after the cmd-echo in UART backlight response frames. A get-brightness reply "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser consumes the stray byte as payload and mistakes the real payload for the checksum, returning -EIO.

On the first checksum failure for a fully received frame, drop the byte at the first payload offset, shift the remaining data down by one, and call dell_uart_bl_receive_data() again to re-fetch the checksum byte from the wire. Track total consumed bytes so the exact count is returned to serdev.

The resync is guarded by resp_idx == resp_len to ensure a complete frame was received, preventing memmove on a truncated or out-of-range length field.

Limit this single-byte resync to once per command via a resync_used flag cleared at the start of each transaction. A second mismatch is still a hard error. Well-formed replies never enter the recovery path.

While at it, switch mutex_init() to devm_mutex_init() so the mutex is automatically destroyed via devres, fixing a missing mutex_destroy() on the teardown path.

Quoted 5 lines

Tested on Dell OptiPlex AIO 7410 65W (0TKWC4) where the EC inserts a stray byte on every get-brightness response -- resync fixes it transparently.

Suggested-by [email protected]

This is not the correct formatting for a tag (please see Documentation/process/submitting-patches.rst). And my Suggested-by should only be in the devm_mutex_init() change (more on this below).

Quoted 35 lines
---
 .../platform/x86/dell/dell-uart-backlight.c   | 35 ++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +3 -1
index af9c5f9a566f..b0df479a4658 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -256,7 +258,7 @@ static size_t dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
 static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
 {
 	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
-	size_t ret;
+	size_t consumed;
 
 	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
 

drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 273

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c +1 -1
@@ -266,12 +268,35 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 		return len;
 	}
 
-	ret = dell_uart_bl_receive_data(dell_bl, data, len);
+	consumed = dell_uart_bl_receive_data(dell_bl, data, len);
 	if (dell_bl->status == -EBUSY)
 		return len; /* Response not complete yet */

So again I'm not following logic here. dell_uart_bl_receive_data() consumed all of len but response is still not complete, right? Why we return len and not consumed? I'd expect them to be same in that case but is there a case where they differ?

Subtle things like this may make the code harder to follow, because reader will have to ask this kind of questions and then attempt to figure out what case he/she doesn't understand.

Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c +11 -0
+	/*
+	 * Stray byte after cmd-echo? Drop it, shift data down, re-fetch
+	 * checksum. Only attempt when a full frame was received (resp_idx ==
+	 * resp_len) to avoid acting on a truncated length field.
+	 */
+	if (dell_bl->status == -EIO && !dell_bl->resync_used &&
+	    dell_bl->resp_idx == dell_bl->resp_len &&
+	    dell_bl->resp_len > MIN_RESP_LEN) {
+		dell_bl->resync_used = true;
+		dell_bl->status = -EBUSY;
+

drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 295

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +10 -0
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);
+		dell_bl->resp_idx = dell_bl->resp_len - 1;
+
+		consumed += dell_uart_bl_receive_data(dell_bl, data + consumed, len - consumed);
+		if (dell_bl->status == -EBUSY)
+			return len; /* Need more data for checksum byte */

I don't follow why consumed is what should be returned because we've eaten everything of len but still are short of bytes? So consumed should be same as len by my own logic but would be more logical to return (if they're equal).

Please don't just mindlessly change these to consumed, I could be wrong as I don't fully understand the related logic as it's somewhat messy so following all control flow paths is bit of a challenge (to put it mildly).

Quoted 9 lines
drivers/platform/x86/dell/dell-uart-backlight.c +3 -1
+	}
+
 	wake_up(&dell_bl->wait_queue);
-	return ret;
+	return consumed;
 }
 
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {

drivers/platform/x86/dell/dell-uart-backlight.c static int dell_uart_bl_serdev_probe(struct serdev_device *serdev) · line 321

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c +3 -1
@@ -291,7 +316,9 @@ static int dell_uart_bl_serdev_probe(struct serdev_device *serdev)
 	if (!dell_bl)
 		return -ENOMEM;
 
-	mutex_init(&dell_bl->mutex);
+	ret = devm_mutex_init(dev, &dell_bl->mutex);
+	if (ret)
+		return ret;

Thanks but when I says something is "unrelated" to the patch/series. It means I've just noticed another issue while reviewing the patch/series, such extra changes should be made in own patch(es).

We try to make all changes such that they contain only one logical change in its minimal form, and no more. In rare cases we need to compromise from that rule to not break anything but this is not such a case and could definitely be in own patch.

When you have to write in changelog an itemized list or say "while at it", "in addition", etc. it's often indicator that you're combining more than one logical change. Always when writing such phrase it's good to stop for a moment, and ask oneself if that thing could be safely moved to own patch instead. Very often the answer is yes.

Signature · 1 line
 i.
#11 lore raw

v4 cover platform/x86: dell-uart-backlight: handle stray EC byte in responses

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell All-in-One machines the EC sporadically inserts a spurious byte after the command echo in UART backlight response frames. This causes a checksum mismatch and the driver returns -EIO to userspace.

Patch 1 is a preparatory refactor: extract the byte-level response parsing into a dedicated dell_uart_bl_receive_data() helper with no functional change.

Patch 2 adds single-attempt stray-byte recovery inside that helper. When a checksum mismatch occurs on a frame longer than the minimum size, the suspect byte is dropped, the buffer is shifted down, and status remains -EBUSY so serdev re-delivers the real checksum byte naturally.

Tested on Dell OptiPlex AIO 7410 65W (board 0TKWC4) where the EC inserts a stray byte on every get-brightness response.

v3 -> v4:
- Perform resync inside dell_uart_bl_receive_data() rather than in the wrapper; status stays -EBUSY so serdev re-delivers the checksum byte without additional handling in the caller

v2 -> v3:
- Guard resync with resp_idx == resp_len to prevent memmove on a truncated or out-of-range length field

v1 -> v2:
- Extract dell_uart_bl_receive_data() helper instead of using a for-loop with a pass counter
- Split into two patches: refactor then feature

Chitransh Mandhaniya (2):
platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data()
helper
platform/x86: dell-uart-backlight: recover from stray EC response byte

 .../platform/x86/dell/dell-uart-backlight.c   | 52 +++++++++++++------
 1 file changed, 37 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#12 lore raw

PATCH v4 1/2 platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data() helper

From: Chitransh Mandhaniya <[email protected]>

Factor the response-byte accumulation and checksum validation out of dell_uart_bl_receive() into a standalone dell_uart_bl_receive_data() helper.

The helper returns the number of bytes consumed from the input buffer. The caller inspects dell_bl->status for the outcome:

  -EBUSY  frame still incomplete
   0      frame received and checksum valid
  -EIO    checksum mismatch

This is a preparatory refactor for adding stray-byte recovery in the next patch.

No functional change.

---
 .../platform/x86/dell/dell-uart-backlight.c   | 42 ++++++++++++-------
 1 file changed, 27 insertions(+), 15 deletions(-)
drivers/platform/x86/dell/dell-uart-backlight.c +27 -15
index f323a667dc2d..5f01ad108338 100644
@@ -205,20 +205,13 @@ static const struct backlight_ops dell_uart_backlight_ops = {
 	.get_brightness = dell_uart_get_brightness_op,
 };
 
-static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+/* Process received UART data; returns number of bytes consumed. */
+static size_t dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
+					const u8 *data, size_t len)
 {
-	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
 	size_t i;
 	u8 csum;
 
-	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
-
-	/* Throw away unexpected bytes / remainder of response after an error */
-	if (dell_bl->status != -EBUSY) {
-		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
-		return len;
-	}
-
 	i = 0;
 	while (i < len && dell_bl->resp_idx != dell_bl->resp_len) {
 		dell_bl->resp[dell_bl->resp_idx] = data[i++];
@@ -231,7 +224,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response length %d out if range %d - %d\n",
 					dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		case RESP_CMD: /* CMD byte */
@@ -239,7 +232,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 				dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n",
 					dell_bl->resp[RESP_CMD], dell_bl->pending_cmd);
 				dell_bl->status = -EIO;
-				goto wakeup;
+				return i;
 			}
 			break;
 		}
@@ -247,7 +240,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 	}
 
 	if (dell_bl->resp_idx != dell_bl->resp_len)
-		return len; /* Response not complete yet */
+		return i; /* Response not complete, status stays -EBUSY */
 
 	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
 	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
@@ -257,11 +250,30 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
 		dell_bl->status = -EIO;
 	}
-wakeup:
-	wake_up(&dell_bl->wait_queue);
 	return i;
 }
 
+static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, size_t len)
+{
+	struct dell_uart_backlight *dell_bl = serdev_device_get_drvdata(serdev);
+	size_t ret;
+
+	dev_dbg(dell_bl->dev, "Recv: %*ph\n", (int)len, data);
+
+	/* Throw away unexpected bytes / remainder of response after an error */
+	if (dell_bl->status != -EBUSY) {
+		dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n");
+		return len;
+	}
+
+	ret = dell_uart_bl_receive_data(dell_bl, data, len);
+	if (dell_bl->status == -EBUSY)
+		return ret; /* Response not complete yet, waiting for more data */
+
+	wake_up(&dell_bl->wait_queue);
+	return ret;
+}
+
 static const struct serdev_device_ops dell_uart_bl_serdev_ops = {
 	.receive_buf = dell_uart_bl_receive,
 	.write_wakeup = serdev_device_write_wakeup,
Signature · 1 line
2.43.0
#13 lore raw

PATCH v4 2/2 platform/x86: dell-uart-backlight: recover from stray EC response byte

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

On some Dell All-in-One machines the EC occasionally inserts a spurious byte after the command echo in UART backlight response frames. For example a get-brightness reply that should be "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser treats the stray byte as payload and the real payload as the checksum, producing a spurious -EIO.

Add single-attempt recovery inside dell_uart_bl_receive_data(): on a checksum mismatch for a frame longer than the minimum size, drop the byte at the first payload offset, shift the remaining data down by one, and decrement resp_idx so serdev re-delivers the real checksum byte on the next callback. Status remains -EBUSY, so no special handling is needed in the caller.

A per-transaction resync_used flag, cleared at the start of each command, limits recovery to one attempt. A second mismatch is a hard -EIO. Well-formed frames never enter the recovery path.

Tested on Dell OptiPlex AIO 7410 65W (board 0TKWC4) where the EC inserts a stray byte on every get-brightness response.

---
 drivers/platform/x86/dell/dell-uart-backlight.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/platform/x86/dell/dell-uart-backlight.c +10 -0
index 5f01ad108338..1243c191c5b0 100644
@@ -60,6 +60,7 @@ struct dell_uart_backlight {
 	u8 resp_len;
 	u8 resp_max_len;
 	u8 pending_cmd;
+	bool resync_used;
 	int status;
 	int power;
 };
@@ -91,6 +92,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
 	dell_bl->resp_len = -1; /* Invalid / unset */
 	dell_bl->resp_max_len = resp_max_len;
 	dell_bl->pending_cmd = cmd[1];
+	dell_bl->resync_used = false;
 
 	/* The TTY buffer should be big enough to take the entire cmd in one go */
 	ret = serdev_device_write_buf(to_serdev_device(dell_bl->dev), cmd, cmd_len);
@@ -245,6 +247,14 @@ static size_t dell_uart_bl_receive_data(struct dell_uart_backlight *dell_bl,
 	csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);
 	if (dell_bl->resp[dell_bl->resp_len - 1] == csum) {
 		dell_bl->status = 0; /* Success */
+	} else if (!dell_bl->resync_used && dell_bl->resp_len > MIN_RESP_LEN) {
+		/* Stray byte after cmd-echo? Drop it, let serdev re-deliver checksum. */
+		dell_bl->resync_used = true;
+		dev_dbg(dell_bl->dev, "Resync: dropping stray byte 0x%02x at offset %d\n",
+			dell_bl->resp[RESP_DATA], RESP_DATA);
+		memmove(&dell_bl->resp[RESP_DATA], &dell_bl->resp[RESP_DATA + 1],
+			dell_bl->resp_len - RESP_DATA - 1);
+		dell_bl->resp_idx = dell_bl->resp_len - 1;
 	} else {
 		dev_err(dell_bl->dev, "Checksum mismatch got 0x%02x expected 0x%02x\n",
 			dell_bl->resp[dell_bl->resp_len - 1], csum);
Signature · 1 line
2.43.0
#14 lore raw
Quoted 7 lines · On Sat, Oct 17, 2026 at 01:07:04PM +0530, [email protected] wrote:

On some Dell All-in-One machines the EC occasionally inserts a spurious byte after the command echo in UART backlight response frames. For example a get-brightness reply that should be "04 0c 0a e5" arrives as "04 0c 0d 0a e5". The length field still reflects the original frame size, so the parser treats the stray byte as payload and the real payload as the checksum, producing a spurious -EIO.

This is very good example of the "bad" data. The character I believe is not spurious at all. This is an UART communication which may be raw or line-based. If you think of the UART line discipline, then the inserted character makes a lot of sense, id est \n is replaced by \r\n.

Can you confirm that the only problematic one is 0x0a when we get 0x0d before? If so, I think the issue can be addressed in a better way.

Quoted 13 lines

Add single-attempt recovery inside dell_uart_bl_receive_data(): on a checksum mismatch for a frame longer than the minimum size, drop the byte at the first payload offset, shift the remaining data down by one, and decrement resp_idx so serdev re-delivers the real checksum byte on the next callback. Status remains -EBUSY, so no special handling is needed in the caller.

A per-transaction resync_used flag, cleared at the start of each command, limits recovery to one attempt. A second mismatch is a hard -EIO. Well-formed frames never enter the recovery path.

Tested on Dell OptiPlex AIO 7410 65W (board 0TKWC4) where the EC inserts a stray byte on every get-brightness response.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#15 lore raw

Re: [PATCH v4 0/2] platform/x86: dell-uart-backlight: handle stray EC byte in responses

In reply to [email protected]

From: Chitransh Mandhaniya <[email protected]>

Hi Ilpo,

Hope you are doing well. Just a friendly follow-up on this series at your convenience. Please let me know if there is anything I should revise.

Thanks,
Chitransh
---

maintainer
#16 lore raw

Re: [PATCH v4 0/2] platform/x86: dell-uart-backlight: handle stray EC byte in responses

In reply to [email protected]
Quoted 9 lines · On Sat, 17 Oct 2026 13:07:02 +0530, [email protected] wrote:

On some Dell All-in-One machines the EC sporadically inserts a spurious byte after the command echo in UART backlight response frames. This causes a checksum mismatch and the driver returns -EIO to userspace.

Patch 1 is a preparatory refactor: extract the byte-level response parsing into a dedicated dell_uart_bl_receive_data() helper with no functional change.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

FYI [if applicable to your patch], as per Linus' policy change, also fixes are mostly routed through for-next unless the fix is for a commit introduced in the most recent cycle or is clearly a regression fix.

The list of commits applied:
[1/2] platform/x86: dell-uart-backlight: extract dell_uart_bl_receive_data() helper

      commit: c7d2d8e9b177c3542a566a043c055c520ebad8ca
[2/2] platform/x86: dell-uart-backlight: recover from stray EC response byte
      commit: a71b1ac1169ff29414d1a0a1d0341e3d0a9fce63

--
i.