Skip to contentSkip to conversation

platform/x86: Add new Dell UART backlight driver

PATCH Superseded Hans de Goede 3 participants 13 messages May 13, 2024 x86 platform drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover platform/x86: Add new Dell UART backlight driver

Hi All,

I recently learned that some Dell AIOs (1) use a backlight controller board connected to an UART. Canonical even submitted a driver for this in 2017:
https://lkml.org/lkml/2017/10/26/78

This UART has a DELL0501 HID with CID set to PNP0501 so that the UART is still handled by 8250_pnp.c. Unfortunately there is no separate ACPI device with an UartSerialBusV2() resource to model the backlight-controller. An ACPI quirk has been merged recently to deal with this and create a serdev controller for the UART despite the missing UartSerialBusV2() resource:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=99b572e6136eab69a8c91d72cf8595b256e304b5

Patch 1 in this series adds a driver binding to the "dell-uart-backlight" device created by this quirk. This drivers creates a serdev-device for the DELL0501 serdev-controller and registers a serdev backlight driver which binds to this serdev-device.

Patch 2 contains a small emulator for the UART attached backlight controller found on this Dell AOIs, I wrote and used this to develop the driver since I did not have access to such an AOI myself.

This has been successfully tested by Roman Bogoyev (who originally reported the missing driver to me by email) on a Dell Inspiron 27 7000 (7790) and by Kai-Heng Feng on a newer Dell AOI model.

Changes in v2:
- Address all the review-remarks Ilpo and Andy had on Patch 1/2, thank you for the extensive review Ilpo and Andy

Regards,

Hans

Hans de Goede (2):
platform/x86: Add new Dell UART backlight driver tools arch x86: Add dell-uart-backlight-emulator

 drivers/platform/x86/dell/Kconfig             |  15 +
 drivers/platform/x86/dell/Makefile            |   1 +
 .../platform/x86/dell/dell-uart-backlight.c   | 409 ++++++++++++++++++
 .../dell-uart-backlight-emulator/.gitignore   |   1 +
 .../x86/dell-uart-backlight-emulator/Makefile |  19 +
 .../x86/dell-uart-backlight-emulator/README   |  46 ++
 .../dell-uart-backlight-emulator.c            | 167 +++++++
 7 files changed, 658 insertions(+)
 create mode 100644 drivers/platform/x86/dell/dell-uart-backlight.c
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/.gitignore
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/Makefile
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/README
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c
Signature · 1 line
2.44.0
#1 lore raw

PATCH v2 1/2 platform/x86: Add new Dell UART backlight driver

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

In DSDT this uart port will be defined as:

Name (_HID, "DELL0501")
Name (_CID, EisaId ("PNP0501")

Instead of having a separate ACPI device with an UartSerialBusV2() resource to model the backlight-controller, which would be the standard way to do this.

The acpi_quirk_skip_serdev_enumeration() has special handling for this and it will make the serial port code create a serdev controller device for the UART instead of a /dev/ttyS0 char-dev. It will also create a dell-uart-backlight driver platform device for this driver to bind too.

This new kernel module contains 2 drivers for this:

1. A simple platform driver which creates the actual serdev device (with the serdev controller device as parent)

2. A serdev driver for the created serdev device which exports the backlight functionality uses a standard backlight class device.

---
Changes in v2:
- Address various review remarks from Ilpo and Andy

---
 drivers/platform/x86/dell/Kconfig             |  15 +
 drivers/platform/x86/dell/Makefile            |   1 +
 .../platform/x86/dell/dell-uart-backlight.c   | 409 ++++++++++++++++++
 3 files changed, 425 insertions(+)
 create mode 100644 drivers/platform/x86/dell/dell-uart-backlight.c
drivers/platform/x86/dell/Kconfig +15 -0
index bd9f445974cc..195a8bf532cc 100644
@@ -145,6 +145,21 @@ config DELL_SMO8800
 	  To compile this driver as a module, choose M here: the module will
 	  be called dell-smo8800.
 
+config DELL_UART_BACKLIGHT
+	tristate "Dell AIO UART Backlight driver"
+	depends on ACPI
+	depends on BACKLIGHT_CLASS_DEVICE
+	depends on SERIAL_DEV_BUS
+	help
+	  Say Y here if you want to support Dell AIO UART backlight interface.
+	  The Dell AIO machines released after 2017 come with a UART interface
+	  to communicate with the backlight scalar board. This driver creates
+	  a standard backlight interface and talks to the scalar board through
+	  UART to adjust the AIO screen brightness.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called dell_uart_backlight.
+
 config DELL_WMI
 	tristate "Dell WMI notifications"
 	default m
drivers/platform/x86/dell/Makefile +1 -0
index 1b8942426622..8176a257d9c3 100644
@@ -14,6 +14,7 @@ dell-smbios-objs			:= dell-smbios-base.o
 dell-smbios-$(CONFIG_DELL_SMBIOS_WMI)	+= dell-smbios-wmi.o
 dell-smbios-$(CONFIG_DELL_SMBIOS_SMM)	+= dell-smbios-smm.o
 obj-$(CONFIG_DELL_SMO8800)		+= dell-smo8800.o
+obj-$(CONFIG_DELL_UART_BACKLIGHT)	+= dell-uart-backlight.o
 obj-$(CONFIG_DELL_WMI)			+= dell-wmi.o
 dell-wmi-objs				:= dell-wmi-base.o
 dell-wmi-$(CONFIG_DELL_WMI_PRIVACY)	+= dell-wmi-privacy.o
drivers/platform/x86/dell/dell-uart-backlight.c new file +409 -0

409 lines not on this page · raw message

Signature · 1 line
2.44.0
#2 lore raw
Quoted 24 lines · Hans de Goede

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

In DSDT this uart port will be defined as:

   Name (_HID, "DELL0501")
   Name (_CID, EisaId ("PNP0501")

Instead of having a separate ACPI device with an UartSerialBusV2() resource to model the backlight-controller, which would be the standard way to do this.

The acpi_quirk_skip_serdev_enumeration() has special handling for this and it will make the serial port code create a serdev controller device for the UART instead of a /dev/ttyS0 char-dev. It will also create a dell-uart-backlight driver platform device for this driver to bind too.

This new kernel module contains 2 drivers for this:

1. A simple platform driver which creates the actual serdev device (with the serdev controller device as parent)

2. A serdev driver for the created serdev device which exports the backlight functionality uses a standard backlight class device.

...

Quoted 1 line

Privately? I mean no links to the report?

...

drivers/platform/x86/dell/Kconfig config DELL_SMO8800 · line 150

Quoted 3 lines
drivers/platform/x86/dell/Kconfig +3 -0
+config DELL_UART_BACKLIGHT
+	tristate "Dell AIO UART Backlight driver"
+	depends on ACPI

Can it be compile-tested in non-ACPI kernels?

Quoted 2 lines
drivers/platform/x86/dell/Kconfig +2 -0
+	depends on BACKLIGHT_CLASS_DEVICE
+	depends on SERIAL_DEV_BUS

...

Quoted 1 line

+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

Yeah, I don't like this, can we move it into header itself?

Quoted 14 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +13 -0
+#include <linux/acpi.h>
+#include <linux/backlight.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/serdev.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/wait.h>
 
+#include "../serdev_helpers.h"

...

drivers/platform/x86/dell/dell-uart-backlight.c line 32

Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2 -0
+/* 1st byte Start Of Frame 3 MSB bits: cmd-len + 01010 SOF marker */
+#define SOF(len)			(((len) << 5) | 0x0a)

This kinda too short to be somehow unique, potential collision might be if somebody introduces this in the header which somehow will be chain-included here. Perhaps a namespace? DELL_SOF?

...

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

Quoted 23 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +23 -0
+	if (ret)
+		return ret;
+
+	dell_bl->status = -EBUSY;
+	dell_bl->resp = resp;
+	dell_bl->resp_idx = 0;
+	dell_bl->resp_max_len = resp_max_len;
+	dell_bl->pending_cmd = cmd[1];
+
+	/* 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);
+	if (ret != cmd_len) {
+		dev_err(dell_bl->dev, "Error writing command: %d\n", ret);
+		ret = (ret < 0) ? ret : -EIO;
+		goto out;
+	}
+
+	ret = wait_event_timeout(dell_bl->wait_queue, dell_bl->status != -EBUSY,
+				 DELL_BL_TIMEOUT);
+	if (ret == 0) {
+		dev_err(dell_bl->dev, "Timed out waiting for response.\n");
+		ret = -ETIMEDOUT;
+	} else {

drivers/platform/x86/dell/dell-uart-backlight.c line 128

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +9 -0
+		ret = dell_bl->status;
+	}
+
+out:
+	mutex_unlock(&dell_bl->mutex);
+	return ret;
+}
 
+static int dell_uart_get_brightness(struct dell_uart_backlight *dell_bl)
+{
	struct device *dev = dell_bl->dev;
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4 -0
+	u8 get_brightness[GET_CMD_LEN], resp[GET_RESP_LEN];
+	int ret;
+
+	get_brightness[0] = SOF(GET_CMD_LEN);

drivers/platform/x86/dell/dell-uart-backlight.c line 142

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +9 -0
+	get_brightness[1] = CMD_GET_BRIGHTNESS;
+	get_brightness[2] = dell_uart_checksum(get_brightness, 2);
 
+	ret = dell_uart_bl_command(dell_bl, get_brightness, GET_CMD_LEN, resp, GET_RESP_LEN);
+	if (ret)
+		return ret;
+
+	if (resp[RESP_LEN] != GET_RESP_LEN) {
+		dev_err(dell_bl->dev, "Unexpected get brightness response length: %d\n",
+			resp[RESP_LEN]);
		dev_err(dev, "Unexpected get brightness response length: %d\n", resp[RESP_LEN]);

drivers/platform/x86/dell/dell-uart-backlight.c line 148

Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6 -0
+		return -EIO;
+	}
+
+	if (resp[RESP_DATA] > DELL_BL_MAX_BRIGHTNESS) {
+		dev_err(dell_bl->dev, "Unexpected get brightness response: %d\n",
+			resp[RESP_DATA]);
		dev_err(dev, "Unexpected get brightness response: %d\n", resp[RESP_DATA]);
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5 -0
+		return -EIO;
+	}
+
+	return resp[RESP_DATA];
+}

...

drivers/platform/x86/dell/dell-uart-backlight.c line 249

Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +8 -0
+		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;

No default case?

...

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

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

Hi,

Quoted 23 lines · Andy Shevchenko
Quoted 22 lines · Hans de Goede

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

In DSDT this uart port will be defined as:

   Name (_HID, "DELL0501")
   Name (_CID, EisaId ("PNP0501")

Instead of having a separate ACPI device with an UartSerialBusV2() resource to model the backlight-controller, which would be the standard way to do this.

The acpi_quirk_skip_serdev_enumeration() has special handling for this and it will make the serial port code create a serdev controller device for the UART instead of a /dev/ttyS0 char-dev. It will also create a dell-uart-backlight driver platform device for this driver to bind too.

This new kernel module contains 2 drivers for this:

1. A simple platform driver which creates the actual serdev device (with the serdev controller device as parent)

Quoted 8 lines
Quoted 2 lines

2. A serdev driver for the created serdev device which exports the backlight functionality uses a standard backlight class device.

...

Quoted 1 line

Privately? I mean no links to the report?

Yes by private email.

<snip>

Quoted 6 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 32

Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2 -0
+/* 1st byte Start Of Frame 3 MSB bits: cmd-len + 01010 SOF marker */
+#define SOF(len)			(((len) << 5) | 0x0a)

This kinda too short to be somehow unique, potential collision might be if somebody introduces this in the header which somehow will be chain-included here. Perhaps a namespace? DELL_SOF?

Ack will fix for v3.

Quoted 12 lines

...

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

I prefer to have the locking explicit rather then use cleanup.h .

<snip>

Quoted 22 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int dell_uart_get_brightness(struct dell_uart_backlight *dell_bl)
+{
	struct device *dev = dell_bl->dev;
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4 -0
+	u8 get_brightness[GET_CMD_LEN], resp[GET_RESP_LEN];
+	int ret;
+
+	get_brightness[0] = SOF(GET_CMD_LEN);

drivers/platform/x86/dell/dell-uart-backlight.c line 142

Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +9 -0
+	get_brightness[1] = CMD_GET_BRIGHTNESS;
+	get_brightness[2] = dell_uart_checksum(get_brightness, 2);
 
+	ret = dell_uart_bl_command(dell_bl, get_brightness, GET_CMD_LEN, resp, GET_RESP_LEN);
+	if (ret)
+		return ret;
+
+	if (resp[RESP_LEN] != GET_RESP_LEN) {
+		dev_err(dell_bl->dev, "Unexpected get brightness response length: %d\n",
+			resp[RESP_LEN]);
		dev_err(dev, "Unexpected get brightness response length: %d\n", resp[RESP_LEN]);
Quoted 9 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 148

Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6 -0
+		return -EIO;
+	}
+
+	if (resp[RESP_DATA] > DELL_BL_MAX_BRIGHTNESS) {
+		dev_err(dell_bl->dev, "Unexpected get brightness response: %d\n",
+			resp[RESP_DATA]);
		dev_err(dev, "Unexpected get brightness response: %d\n", resp[RESP_DATA]);

Ack will fix for v3.

Quoted 8 lines
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5 -0
+		return -EIO;
+	}
+
+	return resp[RESP_DATA];
+}

...

Quoted 10 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 249

Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +8 -0
+		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;

No default case?

Nope, this checks the validity of the first 2 bytes / the header. The data is not checked.

Quoted 7 lines

...

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

Right, this is why the existing dev_dbg() already passes a precision and we do want to actually stop if there is a 0 there, which %pE does not do.

Regards,

Hans

Quoted 2 lines
#4 lore raw
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Mon, May 13, 2024 at 01:15:50PM +0200, Hans de Goede wrote:

...

Quoted 11 lines
Quoted 9 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

I prefer to have the locking explicit rather then use cleanup.h .

Hmm... interesting, so you push-back the cleanup.h usage?

<snip>

Quoted 11 lines
Quoted 10 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 249

Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +8 -0
+		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;

No default case?

Quoted 1 line

Nope, this checks the validity of the first 2 bytes / the header. The data is not checked.

Why not

		default:
			/* We do not check the data */
			break;

?

...

Quoted 7 lines
Quoted 4 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

Right, this is why the existing dev_dbg() already passes a precision and we do want to actually stop if there is a 0 there, which %pE does not do.

I'm talking about the opposite, when it might go over the boundary.

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

Hi,

Quoted 16 lines · Andy Shevchenko
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Mon, May 13, 2024 at 01:15:50PM +0200, Hans de Goede wrote:

...

Quoted 10 lines
Quoted 9 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

Quoted 3 lines
Quoted 1 line

I prefer to have the locking explicit rather then use cleanup.h .

Hmm... interesting, so you push-back the cleanup.h usage?

I'm in favor of the guard(mutex)(&smne_mutex); syntax, but this is a mutex_lock_killable() for which that does not work AFAIK.

So in this case AFAICT we would need to use the cleanup stuff manually and in that case I believe that in that case just sticking with the current code is better.

Quoted 16 lines

<snip>

Quoted 11 lines
Quoted 10 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 249

Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +8 -0
+		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;

No default case?

Quoted 1 line

Nope, this checks the validity of the first 2 bytes / the header. The data is not checked.

Quoted 7 lines

Why not

		default:
			/* We do not check the data */
			break;

?

TBH I don't see any added value in adding that.

Quoted 8 lines

...

Quoted 5 lines
Quoted 4 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

Quoted 4 lines
Quoted 2 lines

Right, this is why the existing dev_dbg() already passes a precision and we do want to actually stop if there is a 0 there, which %pE does not do.

I'm talking about the opposite, when it might go over the boundary.

AFAIK the way the precision is used in the current code limits things to at max the boundary, stopping earlier if a 0 is encountered earlier.

Regards,

Hans

#6 lore raw
Quoted 4 lines · Hans de Goede
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Mon, May 13, 2024 at 01:15:50PM +0200, Hans de Goede wrote:

...

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 9 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

I prefer to have the locking explicit rather then use cleanup.h .

Quoted 8 lines
Quoted 1 line

Hmm... interesting, so you push-back the cleanup.h usage?

I'm in favor of the guard(mutex)(&smne_mutex); syntax, but this is a mutex_lock_killable() for which that does not work AFAIK.

So in this case AFAICT we would need to use the cleanup stuff manually and in that case I believe that in that case just sticking with the current code is better.

There is scoped_cond_guard().
But there is no DEFINE_GUARD_COND() for mutex_lock_killable().

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Right, this is why the existing dev_dbg() already passes a precision and we do want to actually stop if there is a 0 there, which %pE does not do.

I'm talking about the opposite, when it might go over the boundary.

AFAIK the way the precision is used in the current code limits things to at max the boundary, stopping earlier if a 0 is encountered earlier.

Indeed, I refreshed my memory about %.*s.

So, the only part left is the potential terminal sequences appear in the message.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Hans de Goede
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko

On Mon, May 13, 2024 at 01:15:50PM +0200, Hans de Goede wrote:

...

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

drivers/platform/x86/dell/dell-uart-backlight.c line 314

Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1 -0
+	dev_dbg(dev, "Firmware version: %.*s\n", resp[RESP_LEN] - 3, resp + RESP_DATA);

I would be on the safest side, i.e. not trusting that it will be NUL-terminated string, hence something like %*pE?

Right, this is why the existing dev_dbg() already passes a precision and we do want to actually stop if there is a 0 there, which %pE does not do.

I'm talking about the opposite, when it might go over the boundary.

Quoted 7 lines
Quoted 2 lines

AFAIK the way the precision is used in the current code limits things to at max the boundary, stopping earlier if a 0 is encountered earlier.

Indeed, I refreshed my memory about %.*s.

So, the only part left is the potential terminal sequences appear in the message.

Just for clarity, the %*pE would require strnlen() call to have similar effect.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw
In reply to Andy Shevchenko
Quoted 23 lines · Andy Shevchenko
Quoted 22 lines · Hans de Goede
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Hans de Goede
Quoted 11 lines · Andy Shevchenko

drivers/platform/x86/dell/dell-uart-backlight.c line 83

Quoted 7 lines · Hans de Goede
drivers/platform/x86/dell/dell-uart-backlight.c new file +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

Quoted 3 lines
Quoted 1 line

I prefer to have the locking explicit rather then use cleanup.h .

Hmm... interesting, so you push-back the cleanup.h usage?

I'm in favor of the guard(mutex)(&smne_mutex); syntax, but this is a mutex_lock_killable() for which that does not work AFAIK.

Quoted 6 lines
Quoted 3 lines

So in this case AFAICT we would need to use the cleanup stuff manually and in that case I believe that in that case just sticking with the current code is better.

There is scoped_cond_guard().
But there is no DEFINE_GUARD_COND() for mutex_lock_killable().

Is there a way to return the original error code with scoped_cond_guard() or does that it force overriding the original return value with a hard-coded one provided by the caller which seems a step backwards?

Signature · 1 line
 i.
#9 lore raw
Quoted 28 lines · Ilpo Järvinen
Quoted 27 lines · Andy Shevchenko
Quoted 25 lines · Hans de Goede
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Hans de Goede
Quoted 11 lines · Andy Shevchenko

Review

Quoted 7 lines · Hans de Goede
(quoted diff) +7 -0
+static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl,
+				const u8 *cmd, int cmd_len,
+				u8 *resp, int resp_max_len)
+{
+	int ret;
+
+	ret = mutex_lock_killable(&dell_bl->mutex);

Can't be called via cleanup.h?

Quoted 3 lines
Quoted 1 line

I prefer to have the locking explicit rather then use cleanup.h .

Hmm... interesting, so you push-back the cleanup.h usage?

I'm in favor of the guard(mutex)(&smne_mutex); syntax, but this is a mutex_lock_killable() for which that does not work AFAIK.

So in this case AFAICT we would need to use the cleanup stuff manually and in that case I believe that in that case just sticking with the current code is better.

Quoted 6 lines
Quoted 2 lines

There is scoped_cond_guard().
But there is no DEFINE_GUARD_COND() for mutex_lock_killable().

Is there a way to return the original error code with scoped_cond_guard() or does that it force overriding the original return value with a hard-coded one provided by the caller which seems a step backwards?

Yeah, that's also a downside. Perhaps you can reply to the original thread about this. But it seems it was developed for boolean type of functions w/o thinking a lot about returned values.

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

PATCH v2 2/2 tools arch x86: Add dell-uart-backlight-emulator

In reply to Hans de Goede

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

Add a small emulator to allow development and testing of the drivers/platform/x86/dell/dell-uart-backlight.c driver for this board, without requiring access to an actual Dell All In One.

---
Changes in v2:
- Ensure clean exit (return 0) when the emulator is quit by Ctrl+C

---
 .../dell-uart-backlight-emulator/.gitignore   |   1 +
 .../x86/dell-uart-backlight-emulator/Makefile |  19 ++
 .../x86/dell-uart-backlight-emulator/README   |  46 +++++
 .../dell-uart-backlight-emulator.c            | 167 ++++++++++++++++++
 4 files changed, 233 insertions(+)
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/.gitignore
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/Makefile
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/README
 create mode 100644 tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c
tools/arch/x86/dell-uart-backlight-emulator/.gitignore new file +1 -0
new file mode 100644
index 000000000000..5c8cad8d72b9
@@ -0,0 +1 @@
+dell-uart-backlight-emulator
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +19 -0
new file mode 100644
index 000000000000..6ea1d9fd534b
@@ -0,0 +1,19 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for Intel Software Defined Silicon provisioning tool
+
+dell-uart-backlight-emulator: dell-uart-backlight-emulator.c
+
+BINDIR ?= /usr/bin
+
+override CFLAGS += -O2 -Wall
+
+%: %.c
+	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+
+.PHONY : clean
+clean :
+	@rm -f dell-uart-backlight-emulator
+
+install : dell-uart-backlight-emulator
+	install -d $(DESTDIR)$(BINDIR)
+	install -m 755 -p dell-uart-backlight-emulator $(DESTDIR)$(BINDIR)/dell-uart-backlight-emulator
tools/arch/x86/dell-uart-backlight-emulator/README new file +46 -0

46 lines not on this page · raw message

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +167 -0

167 lines not on this page · raw message

Signature · 1 line
2.44.0
#11 lore raw

Re: [PATCH v2 2/2] tools arch x86: Add dell-uart-backlight-emulator

Quoted 6 lines · Hans de Goede

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

Add a small emulator to allow development and testing of the drivers/platform/x86/dell/dell-uart-backlight.c driver for this board, without requiring access to an actual Dell All In One.

...

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 56

Quoted 10 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +10 -0
+	if (argc != 2) {
+		fprintf(stderr, "Invalid or missing arguments\n");
+		fprintf(stderr, "Usage: %s <serial-port>\n", argv[0]);
+		return 1;
+	}
+
+	serial_fd = open(argv[1], O_RDWR | O_NOCTTY);
+	if (serial_fd == -1) {
+		fprintf(stderr, "Error opening %s: %s\n", argv[1], strerror(errno));
+		return 1;

So, looking at the man error it works like your custom approach with an additional things.

Also, don't you want to use either different error codes (above is +1 and all below seems using -1), or be consistent and return -1 always?

Quoted 1 line
(quoted diff) +1 -0
+	}

...

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 155

Quoted 7 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +6 -0
+		/* Respond with <total-len> <cmd> <data...> <csum> */
+		response[0] = len + 3; /* response length in bytes */
+		response[1] = buf[1];  /* ack cmd */
+		csum = dell_uart_checksum(response, len + 2);
+		response[len + 2] = csum;
 
+		ret = write(serial_fd, response, len + 3);

response[0] can be reused here.

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 156

Quoted 1 line
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +1 -0
+		if (ret != (len + 3))

And here.

Quoted 3 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +3 -0
+			fprintf(stderr, "Error writing %d bytes: %d\n",
+				len + 3, ret);
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH v2 2/2] tools arch x86: Add dell-uart-backlight-emulator

Hi,

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Hans de Goede

Dell All In One (AIO) models released after 2017 use a backlight controller board connected to an UART.

Add a small emulator to allow development and testing of the drivers/platform/x86/dell/dell-uart-backlight.c driver for this board, without requiring access to an actual Dell All In One.

...

Quoted 13 lines

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 56

Quoted 10 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +10 -0
+	if (argc != 2) {
+		fprintf(stderr, "Invalid or missing arguments\n");
+		fprintf(stderr, "Usage: %s <serial-port>\n", argv[0]);
+		return 1;
+	}
+
+	serial_fd = open(argv[1], O_RDWR | O_NOCTTY);
+	if (serial_fd == -1) {
+		fprintf(stderr, "Error opening %s: %s\n", argv[1], strerror(errno));
+		return 1;

So, looking at the man error it works like your custom approach with an additional things.

But that is a GNU / glibc extension. I would prefer to stick with plain libc here, so that this can be build with other libc-s too.

Quoted 2 lines

Also, don't you want to use either different error codes (above is +1 and all below seems using -1), or be consistent and return -1 always?

the +1 code is for when the cmdline argument is likely wrong , -1 / 255 is for IO errors.

Quoted 11 lines
Quoted 2 lines
(quoted diff) +1 -0
+	}

...

Quoted 5 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +5 -0
+		/* Respond with <total-len> <cmd> <data...> <csum> */
+		response[0] = len + 3; /* response length in bytes */
+		response[1] = buf[1];  /* ack cmd */
+		csum = dell_uart_checksum(response, len + 2);
+		response[len + 2] = csum;
Quoted 7 lines

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 155

Quoted 1 line
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +1 -0
+		ret = write(serial_fd, response, len + 3);

response[0] can be reused here.

tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 156

Quoted 1 line
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +1 -0
+		if (ret != (len + 3))

And here.

Ack will fix.

Regards,

Hans

Quoted 5 lines
Quoted 4 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +3 -0
+			fprintf(stderr, "Error writing %d bytes: %d\n",
+				len + 3, ret);
+	}