platform/x86: Add new Dell UART backlight driver
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:
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.
Regards,
Hans
1) All In One a monitor with a PC builtin
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 | 166 +++++++ 7 files changed, 657 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
PATCH 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.
--- 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
@@ -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
@@ -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
Quoted 25 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/ttyS0char-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
+#define
pr_fmt(fmt)KBUILD_MODNAME": " fmt
How is this being used?
...
Quoted 1 line
+#include <
linux/acpi.h>
+ array_size.h
drivers/platform/x86/dell/dell-uart-backlight.c line 13
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2
-0+#include <linux/backlight.h> +#include <linux/delay.h>
+ device.h // devm_kzalloc(), dev_err() et al.
+ err.h
drivers/platform/x86/dell/dell-uart-backlight.c line 17
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/platform_device.h> +#include <linux/serdev.h>
+ string.h
+ types.h
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +3
-0+#include <linux/wait.h> +/* The backlight controller must respond within 1 second */ +#define DELL_BL_TIMEOUT msecs_to_jiffies(1000)
...
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+static int dell_uart_get_brightness (struct dell_uart_backlight *dell_bl) +{ +/* +* Get Brightness level: Application uses this command to get brightness. +* Command: 0x6A 0x0C 0x89 (Length:3 Type:0x0A Cmd:0x0C Checksum:0x89) +* Return data: 0x04 0x0C Data Checksum +* (Length:4 Cmd:0x0C Data:<brightness level> +* Checksum: SUM(Length and Cmd and Data) xor 0xFF) +* <brightness level> ranges from 0~100. +*/
drivers/platform/x86/dell/dell-uart-backlight.c line 127
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+const u8 get_brightness[] = {0x6A ,0x0C ,0x89 }; +u8 resp[4 ]; +int ret; + + ret =dell_uart_bl_command (dell_bl, get_brightness,ARRAY_SIZE (get_brightness), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + +if (resp[0 ] !=4 ) {
ARRAY_SIZE() as you used it in many other similar places.
drivers/platform/x86/dell/dell-uart-backlight.c line 132
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+dev_err (dell_bl->dev,"Unexpected get brightness response length: %d\n" , resp[0 ]); +return -EIO ; + } +if (resp[2 ] >100 ) {
(see also below about this number)
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5
-0+dev_err (dell_bl->dev,"Unexpected get brightness response: %d\n" , resp[2 ]); +return -EIO ; + } + +return resp[2 ];
drivers/platform/x86/dell/dell-uart-backlight.c line 147
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+} + +static int dell_uart_set_bl_power (struct dell_uart_backlight *dell_bl,int power) +{ +/* +* Screen ON/OFF Control: Application uses this command to control screen ON or OFF. +* Command: 0x8A 0x0E Data Checksum (Length:4 Type:0x0A Cmd:0x0E) where +* Data=0 to turn OFF the screen. +* Data=1 to turn ON the screen. +* Other value of Data is reserved and invalid.
values
are reserved
Quoted 17 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +17
-0+* Return data: 0x03 0x0E 0xEE (Length:3 Cmd:0x0E Checksum:0xEE) +*/ +u8 set_power[] = {0x8A ,0x0E ,0x00 ,0x00 }; +u8 resp[3 ]; +int ret; + + set_power[2 ] = (power ==FB_BLANK_UNBLANK ) ?1 :0 ; + set_power[3 ] =dell_uart_checksum (set_power,3 ); + + ret =dell_uart_bl_command (dell_bl, set_power,ARRAY_SIZE (set_power), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + + dell_bl->power = power; +return 0 ; +}
...
drivers/platform/x86/dell/dell-uart-backlight.c line 186
Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+static int dell_uart_update_status (struct backlight_device *bd) +{ +struct dell_uart_backlight *dell_bl =bl_get_data (bd); +int ret; + + ret =dell_uart_set_brightness (dell_bl, bd->props.brightness); +if (ret) +return ret; + +if (bd->props.power !=dell_uart_get_bl_power (dell_bl)) + ret =dell_uart_set_bl_power (dell_bl, bd->props.power);
return ...;
Quoted 1 line
+ return ret;
return 0;
?
Quoted 1 line
+}
...
Quoted 1 line
+ props.max_brightness = 100;
Isn't it the same number (semantically) that is used in one of the above functions? Perhaps define it?
...
drivers/platform/x86/dell/dell-uart-backlight.c line 335
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+if (IS_ERR (dell_bl->bl)) +return PTR_ERR (dell_bl->bl); + +return 0 ;
return PTR_ERR_OR_ZERO(...);
...
Haven't noticed MODULE_DEVICE_TABLE(). Is it supposed to be autoloaded? If so, how would it happen? Ah, okay, you are using MODULE_ALIAS().
--
With Best Regards,
Andy Shevchenko
Hi,
Quoted 24 lines · Andy Shevchenko
Quoted 23 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/ttyS0char-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
+#define
pr_fmt(fmt)KBUILD_MODNAME": " fmtHow is this being used?
#include "../serdev_helpers.h"
uses this.
Quoted 14 lines
...
Quoted 1 line
+#include <
linux/acpi.h>+
array_size.hdrivers/platform/x86/dell/dell-uart-backlight.c line 13
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2
-0+#include <linux/backlight.h> +#include <linux/delay.h> +
device.h//devm_kzalloc(),dev_err()et al.+
err.h
Quoted 9 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 17
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/platform_device.h> +#include <linux/serdev.h> +
string.h
+types.hQuoted 1 line
+#include <
linux/wait.h>
Ack, all includes added, thanks.
<snip>
Quoted 14 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 186
Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+static int dell_uart_update_status (struct backlight_device *bd) +{ +struct dell_uart_backlight *dell_bl =bl_get_data (bd); +int ret; + + ret =dell_uart_set_brightness (dell_bl, bd->props.brightness); +if (ret) +return ret; + +if (bd->props.power !=dell_uart_get_bl_power (dell_bl)) + ret =dell_uart_set_bl_power (dell_bl, bd->props.power);return ...;
Quoted 5 lines
Quoted 1 line
+ return ret;
return 0;
?
Ack, fixed for v2.
Regards,
Hans
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko
On Sun, May 12, 2024 at 7:24 PM Hans de Goede <[email protected]> wrote:
...
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
+#define
pr_fmt(fmt)KBUILD_MODNAME": " fmtHow is this being used?
#include "
../serdev_helpers.h"uses this.
Yet another evidence why C code in the *.h is not a good idea.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Ilpo Järvinen
maintainerQuoted 8 lines · Andy Shevchenko
Quoted 2 lines · Hans de Goede
Quoted 1 line · Andy Shevchenko
On Sun, May 12, 2024 at 7:24 PM Hans de Goede <[email protected]> wrote:
...
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
+#define
pr_fmt(fmt)KBUILD_MODNAME": " fmt
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
How is this being used?
#include "
../serdev_helpers.h"uses this.
Yet another evidence why C code in the *.h is not a good idea.
get_serdev_controller() function is quite complex anyway to be inlined, and if there's going to be another user now, it should be uninlined.
Signature · 1 line
i.
Ilpo Järvinen
maintainerQuoted 153 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/ttyS0char-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.
Co-developed-by AceLan Kao <[email protected]>Signed-off-by AceLan Kao <[email protected]>Tested-by Kai-Heng Feng <[email protected]>Signed-off-by Hans de Goede <[email protected]>--- 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.cdrivers/platform/x86/dell/Kconfig +15
-0@@ -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 mdrivers/platform/x86/dell/Makefile +1
-0@@ -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.odrivers/platform/x86/dell/dell-uart-backlight.c new file +73
-0@@ -0,0 +1,409 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Dell AIO Serial Backlight Driver +* +* Copyright (C) 2024 Hans de Goede <[email protected]> +* Copyright (C) 2017 AceLan Kao <[email protected]> +*/ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/acpi.h> +#include <linux/backlight.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/platform_device.h> +#include <linux/serdev.h> +#include <linux/wait.h> +#include "../serdev_helpers.h" + +/* The backlight controller must respond within 1 second */ +#define DELL_BL_TIMEOUT msecs_to_jiffies(1000) +#define DELL_BL_MIN_RESP_SIZE 3 + +struct dell_uart_backlight { +struct mutex mutex; +wait_queue_head_t wait_queue; +struct device *dev; +struct backlight_device *bl; +u8 *resp; +u8 resp_idx; +u8 resp_len; +u8 resp_max_len; +u8 pending_cmd; +int status; +int power; +}; + +/* Checksum: SUM(Length and Cmd and Data) xor 0xFF */ +static u8 dell_uart_checksum (u8 *buf,int len) +{ +u8 val =0 ; + +while (len-- >0 ) + val += buf[len]; + +return val ^0xff ; +} + +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); +if (ret) +return ret; + + dell_bl->status =0 ; + 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; + } +
drivers/platform/x86/dell/dell-uart-backlight.c line 83
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+ ret =wait_event_timeout (dell_bl->wait_queue, dell_bl->status,DELL_BL_TIMEOUT ); +if (ret ==0 ) { +dev_err (dell_bl->dev,"Timed out waiting for response.\n" ); + dell_bl->status = -ETIMEDOUT ; + } + +if (dell_bl->status ==1 ) + ret =0 ; +else + ret = dell_bl->status;
I wonder if it would make dell_bl->status easier to follow if you'd first make it -EBUSY instead of 0 and set it to 0 on success?
It would basically be normal errno behavior without extra values then and you wouldn't need to map it into return value here.
drivers/platform/x86/dell/dell-uart-backlight.c line 95
Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+out: +mutex_unlock (&dell_bl->mutex); +return ret; +} + +static int dell_uart_set_brightness (struct dell_uart_backlight *dell_bl,int brightness) +{ +/* +* Set Brightness level: Application uses this command to set brightness. +* Command: 0x8A 0x0B <brightness-level> Checksum (Length:4 Type:0x0A Cmd:0x0B) +* <brightness-level> ranges from 0~100.
Why ~ character, is this just - ?
drivers/platform/x86/dell/dell-uart-backlight.c line 96
Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+* Return data: 0x03 0x0B 0xF1 (Length:3 Cmd:0x0B Checksum:0xF1)
All these commands return header + echo cmd + (optional) data + checksum. I'm not sure why they all need a comment about it...
It's also slightly misleading to call it "Return data" which can be misinterpreted to mean the return value of this function which is not correct (code calls it resp(onse) anyway so if it's necessary, use response data instead).
drivers/platform/x86/dell/dell-uart-backlight.c line 98
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2
-0+*/ +u8 set_brightness[] = {0x8A ,0x0B ,0x00 ,0x00 };
Use #defines instead of literals.
I think it makes the entire comments about the commands mostly useless when these are converted into properly named defines.
drivers/platform/x86/dell/dell-uart-backlight.c line 102
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+u8 resp[3 ]; + + set_brightness[2 ] = brightness; + set_brightness[3 ] =dell_uart_checksum (set_brightness,3 );
Also, couldn't these be accessed through a struct to eliminate most of the magic indexes?
drivers/platform/x86/dell/dell-uart-backlight.c line 116
Quoted 13 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +13
-0+return dell_uart_bl_command (dell_bl, set_brightness,ARRAY_SIZE (set_brightness), + resp,ARRAY_SIZE (resp)); +} + +static int dell_uart_get_brightness (struct dell_uart_backlight *dell_bl) +{ +/* +* Get Brightness level: Application uses this command to get brightness. +* Command: 0x6A 0x0C 0x89 (Length:3 Type:0x0A Cmd:0x0C Checksum:0x89) +* Return data: 0x04 0x0C Data Checksum +* (Length:4 Cmd:0x0C Data:<brightness level> +* Checksum: SUM(Length and Cmd and Data) xor 0xFF) +* <brightness level> ranges from 0~100.
~ -> - ?
drivers/platform/x86/dell/dell-uart-backlight.c line 127
Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+*/ +const u8 get_brightness[] = {0x6A ,0x0C ,0x89 }; +u8 resp[4 ]; +int ret; + + ret =dell_uart_bl_command (dell_bl, get_brightness,ARRAY_SIZE (get_brightness), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + +if (resp[0 ] !=4 ) {
sizeof(resp), but isn't this already checked when reading it??
drivers/platform/x86/dell/dell-uart-backlight.c line 132
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5
-0+dev_err (dell_bl->dev,"Unexpected get brightness response length: %d\n" , resp[0 ]); +return -EIO ; + } + +if (resp[2 ] >100 ) {
Add #define.
Quoted 27 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +27
-0+dev_err (dell_bl->dev,"Unexpected get brightness response: %d\n" , resp[2 ]); +return -EIO ; + } + +return resp[2 ]; +} + +static int dell_uart_set_bl_power (struct dell_uart_backlight *dell_bl,int power) +{ +/* +* Screen ON/OFF Control: Application uses this command to control screen ON or OFF. +* Command: 0x8A 0x0E Data Checksum (Length:4 Type:0x0A Cmd:0x0E) where +* Data=0 to turn OFF the screen. +* Data=1 to turn ON the screen. +* Other value of Data is reserved and invalid. +* Return data: 0x03 0x0E 0xEE (Length:3 Cmd:0x0E Checksum:0xEE) +*/ +u8 set_power[] = {0x8A ,0x0E ,0x00 ,0x00 }; +u8 resp[3 ]; +int ret; + + set_power[2 ] = (power ==FB_BLANK_UNBLANK ) ?1 :0 ; + set_power[3 ] =dell_uart_checksum (set_power,3 ); + + ret =dell_uart_bl_command (dell_bl, set_power,ARRAY_SIZE (set_power), + resp,ARRAY_SIZE (resp)); +if (ret)
drivers/platform/x86/dell/dell-uart-backlight.c line 169
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+return ret; + + dell_bl->power = power; +return 0 ; +} + +/* +* There is no command to get backlight power status, +* so we set the backlight power to "on" while initializing, +* and then track and report its status by power variable
Missing .
Quoted 31 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +31
-0+*/ +static int dell_uart_get_bl_power (struct dell_uart_backlight *dell_bl) +{ +return dell_bl->power; +} + +static int dell_uart_update_status (struct backlight_device *bd) +{ +struct dell_uart_backlight *dell_bl =bl_get_data (bd); +int ret; + + ret =dell_uart_set_brightness (dell_bl, bd->props.brightness); +if (ret) +return ret; + +if (bd->props.power !=dell_uart_get_bl_power (dell_bl)) + ret =dell_uart_set_bl_power (dell_bl, bd->props.power); + +return ret; +} + +static int dell_uart_get_brightness_op (struct backlight_device *bd) +{ +return dell_uart_get_brightness (bl_get_data (bd)); +} + +static const struct backlight_ops dell_uart_backlight_ops = { + .update_status = dell_uart_update_status, + .get_brightness = dell_uart_get_brightness_op, +}; +
drivers/platform/x86/dell/dell-uart-backlight.c line 210
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+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 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) {
As mentioned above, != -EBUSY ?
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+dev_warn (dell_bl->dev,"Bytes received out of band, dropping them.\n" ); +return len; + } + +for (i =0 ; i < len; i++) { + dell_bl->resp[dell_bl->resp_idx] = data[i];
drivers/platform/x86/dell/dell-uart-backlight.c line 226
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+ +switch (dell_bl->resp_idx) { +case 0 :/* Length byte */ + dell_bl->resp_len = dell_bl->resp[0 ]; +if (dell_bl->resp_len <DELL_BL_MIN_RESP_SIZE ) { +dev_err (dell_bl->dev,"Response length too small %d < %d\n" , + dell_bl->resp_len,DELL_BL_MIN_RESP_SIZE ); + dell_bl->status = -EIO ; + goto wakeup; + }else if (dell_bl->resp_len > dell_bl->resp_max_len) {
Unnecessary else because of the goto.
Quoted 17 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +17
-0+dev_err (dell_bl->dev,"Response length too big %d > %d\n" , + dell_bl->resp_len, dell_bl->resp_max_len); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; +case 1 :/* CMD byte */ +if (dell_bl->resp[1 ] != dell_bl->pending_cmd) { +dev_err (dell_bl->dev,"Response cmd 0x%02x != pending 0x%02x\n" , + dell_bl->resp[1 ], dell_bl->pending_cmd); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; + } + + dell_bl->resp_idx++;
drivers/platform/x86/dell/dell-uart-backlight.c line 253
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+if (dell_bl->resp_idx < dell_bl->resp_len) +continue ; + + csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 ); +if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) { +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; + }
Why is the checksum calculation and check inside the loop??
drivers/platform/x86/dell/dell-uart-backlight.c line 255
Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+ dell_bl->status =1 ;/* Success */
As mentioned above, change this to = 0 ?
drivers/platform/x86/dell/dell-uart-backlight.c line 253
Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+ goto wakeup;
Huh? Now I'm totally lost how the control flow is supposed to go in this function. Can you rethink this loop so it actual makes sense and doesn't misuse gotos like this?
Quoted 56 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +56
-0+ } + +return len; + +wakeup: +wake_up (&dell_bl->wait_queue); +return i +1 ; +} + +static const struct serdev_device_ops dell_uart_bl_serdev_ops = { + .receive_buf = dell_uart_bl_receive, + .write_wakeup = serdev_device_write_wakeup, +}; + +static int dell_uart_bl_serdev_probe (struct serdev_device *serdev) +{ +/* +* Get Firmware Version: Tool uses this command to get firmware version. +* Command: 0x6A 0x06 0x8F (Length:3 Type:0x0A Cmd:6 Checksum:0x8F) +* Return data: 0x0D 0x06 Data Checksum (Length:13 Cmd:0x06 +* Data:F/W version(APRILIA=APR27-Vxxx/PHINE=PHI23-Vxxx) +* Checksum:SUM(Length and Cmd and Data) xor 0xFF) +*/ +const u8 get_firmware_ver[] = {0x6A ,0x06 ,0x8F }; +struct dell_uart_backlight *dell_bl; +struct backlight_properties props; +struct device *dev = &serdev->dev; +u8 get_firmware_ver_resp[80 ]; +int ret; + + dell_bl =devm_kzalloc (dev,sizeof (*dell_bl),GFP_KERNEL ); +if (!dell_bl) +return -ENOMEM ; + +mutex_init (&dell_bl->mutex); +init_waitqueue_head (&dell_bl->wait_queue); + dell_bl->dev = dev; + + ret =devm_serdev_device_open (dev, serdev); +if (ret) +return dev_err_probe (dev, ret,"opening UART device\n" ); + +/* 9600 bps, no flow control, these are the default but set them to be sure */ +serdev_device_set_baudrate (serdev,9600 ); +serdev_device_set_flow_control (serdev, false); +serdev_device_set_drvdata (serdev, dell_bl); +serdev_device_set_client_ops (serdev, &dell_uart_bl_serdev_ops); + + ret =dell_uart_bl_command (dell_bl, get_firmware_ver,ARRAY_SIZE (get_firmware_ver), + get_firmware_ver_resp,ARRAY_SIZE (get_firmware_ver_resp)); +if (ret) +return dev_err_probe (dev, ret,"getting firmware version\n" ); + +dev_dbg (dev,"Firmware version: %.*s\n" , get_firmware_ver_resp[0 ] -3 , + get_firmware_ver_resp +2 ); +
drivers/platform/x86/dell/dell-uart-backlight.c line 322
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+/* Initialize bl_power to a known value */ + ret =dell_uart_set_bl_power (dell_bl,FB_BLANK_UNBLANK ); +if (ret) +return ret; + + ret =dell_uart_get_brightness (dell_bl); +if (ret <0 ) +return ret; + +memset (&props,0 ,sizeof (struct backlight_properties ));
Just assigned = {} when declaring.
drivers/platform/x86/dell/dell-uart-backlight.c line 325
Quoted 3 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +3
-0+ props.type =BACKLIGHT_PLATFORM ; + props.brightness = ret; + props.max_brightness =100 ;
Use #define.
Quoted 47 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +47
-0+ props.power = dell_bl->power; + + dell_bl->bl =devm_backlight_device_register (dev,"dell_uart_backlight" , + dev, dell_bl, + &dell_uart_backlight_ops, + &props); +if (IS_ERR (dell_bl->bl)) +return PTR_ERR (dell_bl->bl); + +return 0 ; +} + +struct serdev_device_driver dell_uart_bl_serdev_driver = { + .probe = dell_uart_bl_serdev_probe, + .driver = { + .name =KBUILD_MODNAME , + }, +}; + +static int dell_uart_bl_pdev_probe (struct platform_device *pdev) +{ +struct serdev_device *serdev; +struct device *ctrl_dev; +int ret; + + ctrl_dev =get_serdev_controller ("DELL0501" ,NULL ,0 ,"serial0" ); +if (IS_ERR (ctrl_dev)) +return PTR_ERR (ctrl_dev); + + serdev =serdev_device_alloc (to_serdev_controller (ctrl_dev)); +put_device (ctrl_dev); +if (!serdev) +return -ENOMEM ; + + ret =serdev_device_add (serdev); +if (ret) { +dev_err (&pdev->dev,"error %d adding serdev\n" , ret); +serdev_device_put (serdev); +return ret; + } + + ret =serdev_device_driver_register (&dell_uart_bl_serdev_driver); +if (ret) { +serdev_device_remove (serdev); +return ret; + } +
drivers/platform/x86/dell/dell-uart-backlight.c line 382
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+/* +* serdev device <-> driver matching relies on OF or ACPI matches and +* neither is available here, manually bind the driver. +*/ + ret =device_driver_attach (&dell_uart_bl_serdev_driver.driver, &serdev->dev); +if (ret) { +serdev_device_driver_unregister (&dell_uart_bl_serdev_driver); +serdev_device_remove (serdev); +return ret; + }
The last two error branch could use normal rollback with goto.
Signature · 1 line
i.
Quoted 28 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +27
-0+ +/* So that dell_uart_bl_pdev_remove() can remove the serdev */ +platform_set_drvdata (pdev, serdev); +return 0 ; +} + +static void dell_uart_bl_pdev_remove (struct platform_device *pdev) +{ +struct serdev_device *serdev =platform_get_drvdata (pdev); + +serdev_device_driver_unregister (&dell_uart_bl_serdev_driver); +serdev_device_remove (serdev); +} + +static struct platform_driver dell_uart_bl_pdev_driver = { + .probe = dell_uart_bl_pdev_probe, + .remove_new = dell_uart_bl_pdev_remove, + .driver = { + .name ="dell-uart-backlight" , + }, +}; +module_platform_driver (dell_uart_bl_pdev_driver); + +MODULE_ALIAS ("platform:dell-uart-backlight" ); +MODULE_DESCRIPTION ("Dell AIO Serial Backlight driver" ); +MODULE_AUTHOR ("Hans de Goede <[email protected]>" ); +MODULE_LICENSE ("GPL" );
Hi Ilpo,
Thank you for the review.
Quoted 1 line · Ilpo Järvinen
On Sun, 12 May 2024, Hans de Goede wrote:
<snip>
Quoted 28 lines
Quoted 14 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +14
-0+ dell_bl->status =0 ; + 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; + } +drivers/platform/x86/dell/dell-uart-backlight.c line 83
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+ ret =wait_event_timeout (dell_bl->wait_queue, dell_bl->status,DELL_BL_TIMEOUT ); +if (ret ==0 ) { +dev_err (dell_bl->dev,"Timed out waiting for response.\n" ); + dell_bl->status = -ETIMEDOUT ; + } + +if (dell_bl->status ==1 ) + ret =0 ; +else + ret = dell_bl->status;I wonder if it would make
dell_bl->statuseasier to follow if you'd first make it-EBUSYinstead of 0 and set it to 0 on success?
Quoted 2 lines
It would basically be normal errno behavior without extra values then and you wouldn't need to map it into return value here.
Ack, done for v2.
Quoted 14 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 95
Quoted 12 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+out: +mutex_unlock (&dell_bl->mutex); +return ret; +} + +static int dell_uart_set_brightness (struct dell_uart_backlight *dell_bl,int brightness) +{ +/* +* Set Brightness level: Application uses this command to set brightness. +* Command: 0x8A 0x0B <brightness-level> Checksum (Length:4 Type:0x0A Cmd:0x0B) +* <brightness-level> ranges from 0~100. Why ~ character, is this just - ?
Yes just -, the ~ came from the original driver I based this on I'll fix this for v2.
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 96
Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+* Return data: 0x03 0x0B 0xF1 (Length:3 Cmd:0x0B Checksum:0xF1) All these commands return header + echo cmd + (optional) data + checksum. I'm not sure why they all need a comment about it...
It's also slightly misleading to call it "Return data" which can be misinterpreted to mean the return value of this function which is not correct (code calls it resp(onse) anyway so if it's necessary, use response data instead).
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 98
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2
-0+*/ +u8 set_brightness[] = {0x8A ,0x0B ,0x00 ,0x00 };Use #defines instead of literals.
Ack, will fix for v2.
Quoted 3 lines
I think it makes the entire comments about the commands mostly useless when these are converted into properly named defines.
Ack, will drop.
Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 102
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+u8 resp[3 ]; + + set_brightness[2 ] = brightness; + set_brightness[3 ] =dell_uart_checksum (set_brightness,3 );Also, couldn't these be accessed through a struct to eliminate most of the magic indexes?
With the checksum at the end, this would require a VLA in the middle of the struct (get_version return buffer contains more then 1 dat byte) We could treat the checksum as an extra data byte, but then we are mixing struct usage for some fields + using an array of bytes approach for the data + checksum. For consistency I prefer to just stick with one approach which means using the array of bytes approach for everything.
Quoted 17 lines
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +3
-0+return dell_uart_bl_command (dell_bl, set_brightness,ARRAY_SIZE (set_brightness), + resp,ARRAY_SIZE (resp)); +}drivers/platform/x86/dell/dell-uart-backlight.c line 116
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+ +static int dell_uart_get_brightness (struct dell_uart_backlight *dell_bl) +{ +/* +* Get Brightness level: Application uses this command to get brightness. +* Command: 0x6A 0x0C 0x89 (Length:3 Type:0x0A Cmd:0x0C Checksum:0x89) +* Return data: 0x04 0x0C Data Checksum +* (Length:4 Cmd:0x0C Data:<brightness level> +* Checksum: SUM(Length and Cmd and Data) xor 0xFF) +* <brightness level> ranges from 0~100. ~ -> - ?
Quoted 13 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 127
Quoted 11 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +11
-0+*/ +const u8 get_brightness[] = {0x6A ,0x0C ,0x89 }; +u8 resp[4 ]; +int ret; + + ret =dell_uart_bl_command (dell_bl, get_brightness,ARRAY_SIZE (get_brightness), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + +if (resp[0 ] !=4 ) {sizeof(resp)
Ack.
Quoted 1 line
but isn't this already checked when reading it??
No dell_uart_bl_receive() checks that the response will fit in the supplied buffer and that it has a valid checksum but the controller may send a response smaller then the passed in buffer and it will actually do this for the get_version command.
Quoted 7 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 132
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5
-0+dev_err (dell_bl->dev,"Unexpected get brightness response length: %d\n" , resp[0 ]); +return -EIO ; + } + +if (resp[2 ] >100 ) {Add #define.
Ack.
<snip>
Quoted 6 lines
Quoted 4 lines
+/*
+ * There is no command to get backlight power status,
+ * so we set the backlight power to "on" while initializing,
+ * and then track and report its status by power variableMissing .
Ack.
<snip>
Quoted 4 lines
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+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 i;
Quoted 9 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 210
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+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) {As mentioned above, !=
-EBUSY?
Ack, done for v2.
Quoted 40 lines
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+dev_warn (dell_bl->dev,"Bytes received out of band, dropping them.\n" ); +return len; + } + +for (i =0 ; i < len; i++) { + dell_bl->resp[dell_bl->resp_idx] = data[i];drivers/platform/x86/dell/dell-uart-backlight.c line 226
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +10
-0+ +switch (dell_bl->resp_idx) { +case 0 :/* Length byte */ + dell_bl->resp_len = dell_bl->resp[0 ]; +if (dell_bl->resp_len <DELL_BL_MIN_RESP_SIZE ) { +dev_err (dell_bl->dev,"Response length too small %d < %d\n" , + dell_bl->resp_len,DELL_BL_MIN_RESP_SIZE ); + dell_bl->status = -EIO ; + goto wakeup; + }else if (dell_bl->resp_len > dell_bl->resp_max_len) {Unnecessary else because of the goto.
Quoted 21 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +21
-0+dev_err (dell_bl->dev,"Response length too big %d > %d\n" , + dell_bl->resp_len, dell_bl->resp_max_len); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; +case 1 :/* CMD byte */ +if (dell_bl->resp[1 ] != dell_bl->pending_cmd) { +dev_err (dell_bl->dev,"Response cmd 0x%02x != pending 0x%02x\n" , + dell_bl->resp[1 ], dell_bl->pending_cmd); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; + } + + dell_bl->resp_idx++; +if (dell_bl->resp_idx < dell_bl->resp_len) +continue ; + + csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );
Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 253
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) { +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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;
continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 255
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+ dell_bl->status =1 ;/* Success */ As mentioned above, change this to = 0
Ack, done.
?
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ goto wakeup;Huh? Now I'm totally lost how the control flow is supposed to go in this function. Can you rethink this loop so it actual makes sense and doesn't misuse gotos like this?
This is the receive() callback from the UART the loop consumes bytes received by the UART. The gotos stop consuming bytes in 2 cases:
1. An error (unexpected data) is encountered.
2. A complete frame has been successfully received.
The checking of the checksum + goto wakeup at the end of the loop is for 2.
The:
return len;
after the loop indicates to the UART / tty-layer that all passed data has been consumed and this path gets hit when the driver needs to wait for more data because the response is not complete yet.
Quoted 6 lines
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+ +wakeup: +wake_up (&dell_bl->wait_queue); +return i +1 ; +} +
<snip>
Quoted 3 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 322
Quoted 1 line
drivers/platform/x86/dell/dell-uart-backlight.c new file +1
-0+memset (&props,0 ,sizeof (struct backlight_properties ));Just assigned = {} when declaring.
Ack.
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 325
Quoted 3 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +3
-0+ props.type =BACKLIGHT_PLATFORM ; + props.brightness = ret; + props.max_brightness =100 ;Use #define.
Ack.
<snip>
Quoted 17 lines
Quoted 17 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +17
-0+ ret =serdev_device_add (serdev); +if (ret) { +dev_err (&pdev->dev,"error %d adding serdev\n" , ret); +serdev_device_put (serdev); +return ret; + } + + ret =serdev_device_driver_register (&dell_uart_bl_serdev_driver); +if (ret) { +serdev_device_remove (serdev); +return ret; + } + +/* +* serdev device <-> driver matching relies on OF or ACPI matches and +* neither is available here, manually bind the driver. +*/
Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 382
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+ ret =device_driver_attach (&dell_uart_bl_serdev_driver.driver, &serdev->dev); +if (ret) { +serdev_device_driver_unregister (&dell_uart_bl_serdev_driver); +serdev_device_remove (serdev); +return ret; + }The last two error branch could use normal rollback with goto.
Ack, will fix for v2.
Regards,
Hans
Ilpo Järvinen
maintainerQuoted 15 lines · Hans de Goede
Hi Ilpo,
Thank you for the review.
Quoted 9 lines · Ilpo Järvinen
drivers/platform/x86/dell/dell-uart-backlight.c line 102
Quoted 4 lines · Hans de Goede
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+u8 resp[3 ]; + + set_brightness[2 ] = brightness; + set_brightness[3 ] =dell_uart_checksum (set_brightness,3 );Also, couldn't these be accessed through a struct to eliminate most of the magic indexes?
Quoted 7 lines
With the checksum at the end, this would require a VLA in the middle of the struct (
get_versionreturn buffer contains more then 1 dat byte) We could treat the checksum as an extra data byte, but then we are mixing struct usage for some fields + using an array of bytes approach for the data + checksum. For consistency I prefer to just stick with one approach which means using the array of bytes approach for everything.
Ok.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+const u8 get_brightness[] = {0x6A ,0x0C ,0x89 }; +u8 resp[4 ]; +int ret; +Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 127
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+ ret =dell_uart_bl_command (dell_bl, get_brightness,ARRAY_SIZE (get_brightness), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + +if (resp[0 ] !=4 ) {sizeof(resp)
Quoted 8 lines
Ack.
Quoted 1 line
but isn't this already checked when reading it??
No
dell_uart_bl_receive()checks that the response will fit in the supplied buffer and that it has a valid checksum but the controller may send a response smaller then the passed in buffer and it will actually do this for theget_versioncommand.
Ah, I see now that it checked against constant rather than the actual value.
Quoted 49 lines
Quoted 37 lines
Quoted 37 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +37
-0+dev_warn (dell_bl->dev,"Bytes received out of band, dropping them.\n" ); +return len; + } + +for (i =0 ; i < len; i++) { + dell_bl->resp[dell_bl->resp_idx] = data[i]; + +switch (dell_bl->resp_idx) { +case 0 :/* Length byte */ + dell_bl->resp_len = dell_bl->resp[0 ]; +if (dell_bl->resp_len <DELL_BL_MIN_RESP_SIZE ) { +dev_err (dell_bl->dev,"Response length too small %d < %d\n" , + dell_bl->resp_len,DELL_BL_MIN_RESP_SIZE ); + dell_bl->status = -EIO ; + goto wakeup; + }else if (dell_bl->resp_len > dell_bl->resp_max_len) { +dev_err (dell_bl->dev,"Response length too big %d > %d\n" , + dell_bl->resp_len, dell_bl->resp_max_len); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; +case 1 :/* CMD byte */ +if (dell_bl->resp[1 ] != dell_bl->pending_cmd) { +dev_err (dell_bl->dev,"Response cmd 0x%02x != pending 0x%02x\n" , + dell_bl->resp[1 ], dell_bl->pending_cmd); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; + } + + dell_bl->resp_idx++; +if (dell_bl->resp_idx < dell_bl->resp_len) +continue ; + + csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 253
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) { +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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a) dell_bl->resp_idx == dell_bl->resp_len (complete frame)
a) if i == len (not yet received a full frame)
Why not code those rather than the current goto & continue madness?
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) {
// calc checksum, etc.
}?
Quoted 28 lines
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 256
Quoted 2 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +2
-0+ dell_bl->status =1 ;/* Success */ + goto wakeup;Huh? Now I'm totally lost how the control flow is supposed to go in this function. Can you rethink this loop so it actual makes sense and doesn't misuse gotos like this?
This is the
receive()callback from the UART the loop consumes bytes received by the UART. The gotos stop consuming bytes in 2 cases:1. An error (unexpected data) is encountered.
2. A complete frame has been successfully received.The checking of the checksum + goto wakeup at the end of the loop is for 2.
The:
return len;after the loop indicates to the UART / tty-layer that all passed data has been consumed and this path gets hit when the driver needs to wait for more data because the response is not complete yet.
Quoted 5 lines
Quoted 5 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +5
-0+ +wakeup: +wake_up (&dell_bl->wait_queue); +return i +1 ; +}
Signature · 1 line
i.
Ilpo Järvinen
maintainerQuoted 117 lines · Ilpo Järvinen
Quoted 15 lines · Hans de Goede
Hi Ilpo,
Thank you for the review.
Quoted 9 lines · Ilpo Järvinen
drivers/platform/x86/dell/dell-uart-backlight.c line 102
Quoted 4 lines · Hans de Goede
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+u8 resp[3 ]; + + set_brightness[2 ] = brightness; + set_brightness[3 ] =dell_uart_checksum (set_brightness,3 );Also, couldn't these be accessed through a struct to eliminate most of the magic indexes?
Quoted 7 lines
With the checksum at the end, this would require a VLA in the middle of the struct (
get_versionreturn buffer contains more then 1 dat byte) We could treat the checksum as an extra data byte, but then we are mixing struct usage for some fields + using an array of bytes approach for the data + checksum. For consistency I prefer to just stick with one approach which means using the array of bytes approach for everything.Ok.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +4
-0+const u8 get_brightness[] = {0x6A ,0x0C ,0x89 }; +u8 resp[4 ]; +int ret; +Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 127
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+ ret =dell_uart_bl_command (dell_bl, get_brightness,ARRAY_SIZE (get_brightness), + resp,ARRAY_SIZE (resp)); +if (ret) +return ret; + +if (resp[0 ] !=4 ) {sizeof(resp)
Quoted 8 lines
Ack.
Quoted 1 line
but isn't this already checked when reading it??
No
dell_uart_bl_receive()checks that the response will fit in the supplied buffer and that it has a valid checksum but the controller may send a response smaller then the passed in buffer and it will actually do this for theget_versioncommand.Ah, I see now that it checked against constant rather than the actual value.
Quoted 49 lines
Quoted 37 lines
Quoted 37 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +37
-0+dev_warn (dell_bl->dev,"Bytes received out of band, dropping them.\n" ); +return len; + } + +for (i =0 ; i < len; i++) { + dell_bl->resp[dell_bl->resp_idx] = data[i]; + +switch (dell_bl->resp_idx) { +case 0 :/* Length byte */ + dell_bl->resp_len = dell_bl->resp[0 ]; +if (dell_bl->resp_len <DELL_BL_MIN_RESP_SIZE ) { +dev_err (dell_bl->dev,"Response length too small %d < %d\n" , + dell_bl->resp_len,DELL_BL_MIN_RESP_SIZE ); + dell_bl->status = -EIO ; + goto wakeup; + }else if (dell_bl->resp_len > dell_bl->resp_max_len) { +dev_err (dell_bl->dev,"Response length too big %d > %d\n" , + dell_bl->resp_len, dell_bl->resp_max_len); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; +case 1 :/* CMD byte */ +if (dell_bl->resp[1 ] != dell_bl->pending_cmd) { +dev_err (dell_bl->dev,"Response cmd 0x%02x != pending 0x%02x\n" , + dell_bl->resp[1 ], dell_bl->pending_cmd); + dell_bl->status = -EIO ; + goto wakeup; + } +break ; + } + + dell_bl->resp_idx++; +if (dell_bl->resp_idx < dell_bl->resp_len) +continue ; + + csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c line 253
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c new file +6
-0+if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) { +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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
+ the len has been received check:
if (dell_bl->resp_idx && dell_bl->resp_idx == dell_bl->resp_len) {
...
}Signature · 1 line
i.
Hi,
Quoted 117 lines · Ilpo Järvinen
Quoted 15 lines · Hans de Goede
Hi Ilpo,
Thank you for the review.
Quoted 9 lines · Ilpo Järvinen
Review
Quoted 4 lines · Hans de Goede
(quoted diff) +4
-0+ u8 resp[3]; + + set_brightness[2] = brightness; + set_brightness[3] = dell_uart_checksum(set_brightness, 3);Also, couldn't these be accessed through a struct to eliminate most of the magic indexes?
Quoted 7 lines
With the checksum at the end, this would require a VLA in the middle of the struct (
get_versionreturn buffer contains more then 1 dat byte) We could treat the checksum as an extra data byte, but then we are mixing struct usage for some fields + using an array of bytes approach for the data + checksum. For consistency I prefer to just stick with one approach which means using the array of bytes approach for everything.Ok.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4
-0+ const u8 get_brightness[] = { 0x6A, 0x0C, 0x89 }; + u8 resp[4]; + int ret; +Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ ret = dell_uart_bl_command(dell_bl, get_brightness, ARRAY_SIZE(get_brightness), + resp, ARRAY_SIZE(resp)); + if (ret) + return ret; + + if (resp[0] != 4) {sizeof(resp)
Quoted 8 lines
Ack.
Quoted 1 line
but isn't this already checked when reading it??
No
dell_uart_bl_receive()checks that the response will fit in the supplied buffer and that it has a valid checksum but the controller may send a response smaller then the passed in buffer and it will actually do this for theget_versioncommand.Ah, I see now that it checked against constant rather than the actual value.
Quoted 49 lines
Quoted 37 lines
Quoted 37 lines
(quoted diff) +37
-0+ dev_warn(dell_bl->dev, "Bytes received out of band, dropping them.\n"); + return len; + } + + for (i = 0; i < len; i++) { + dell_bl->resp[dell_bl->resp_idx] = data[i]; + + switch (dell_bl->resp_idx) { + case 0: /* Length byte */ + dell_bl->resp_len = dell_bl->resp[0]; + if (dell_bl->resp_len < DELL_BL_MIN_RESP_SIZE) { + dev_err(dell_bl->dev, "Response length too small %d < %d\n", + dell_bl->resp_len, DELL_BL_MIN_RESP_SIZE); + dell_bl->status = -EIO; + goto wakeup; + } else if (dell_bl->resp_len > dell_bl->resp_max_len) { + dev_err(dell_bl->dev, "Response length too big %d > %d\n", + dell_bl->resp_len, dell_bl->resp_max_len); + dell_bl->status = -EIO; + goto wakeup; + } + break; + case 1: /* CMD byte */ + if (dell_bl->resp[1] != dell_bl->pending_cmd) { + dev_err(dell_bl->dev, "Response cmd 0x%02x != pending 0x%02x\n", + dell_bl->resp[1], dell_bl->pending_cmd); + dell_bl->status = -EIO; + goto wakeup; + } + break; + } + + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1);Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +12 -16
@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];
@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {
@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len; -wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }
Regards,
Hans
Quoted 31 lines
Quoted 29 lines
Quoted 7 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ dell_bl->status = 1; /* Success */ + goto wakeup;Huh? Now I'm totally lost how the control flow is supposed to go in this function. Can you rethink this loop so it actual makes sense and doesn't misuse gotos like this?
This is the
receive()callback from the UART the loop consumes bytes received by the UART. The gotos stop consuming bytes in 2 cases:1. An error (unexpected data) is encountered.
2. A complete frame has been successfully received.The checking of the checksum + goto wakeup at the end of the loop is for 2.
The:
return len;after the loop indicates to the UART / tty-layer that all passed data has been consumed and this path gets hit when the driver needs to wait for more data because the response is not complete yet.
Quoted 5 lines
Quoted 5 lines
(quoted diff) +5
-0+ +wakeup: + wake_up(&dell_bl->wait_queue); + return i + 1; +}
Ilpo Järvinen
maintainerQuoted 117 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i;
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 10 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1}return len; -wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }
Thanks, this is way easier to follow.
Signature · 1 line
i.
Hi,
Quoted 122 lines · Ilpo Järvinen
Quoted 121 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len;
Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1-wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }Thanks, this is way easier to follow.
I'm glad you like it.
There is a little bug in this version though, the goto error on the checksum fail case returns i + i, which should be i in that case, I'll just drop the goto there and instead always use the return i already present at the end of the
"if (dell_bl->resp_idx == dell_bl->resp_len) { }" block.
Regards,
hans
Quoted 1 line
Ilpo Järvinen
maintainerQuoted 134 lines · Hans de Goede
Hi,
Quoted 122 lines · Ilpo Järvinen
Quoted 121 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len;Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1-wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }Thanks, this is way easier to follow.
Quoted 6 lines
I'm glad you like it.
There is a little bug in this version though, the goto error on the checksum fail case returns i + i, which should be i in that case, I'll just drop the goto there and instead always use the return i already present at the end of the
"if (dell_bl->resp_idx==dell_bl->resp_len) { }" block.
It could have been solved more logically incrementing i and resp_idx here:
dell_bl->resp[dell_bl->resp_idx] = data[i]; dell_bl->resp_idx++; i++;
so that the inconsistent state is eliminated.
I also realized (I know I was the one who suggested it) that reverse logic would be better for the incomplete frame check:
if (dell_bl->resp_idx < dell_bl->resp_len) return len; // checksum logic...
Perhaps the success and error return paths could then be merged too.
Signature · 1 line
i.
Hi,
Quoted 154 lines · Ilpo Järvinen
Quoted 134 lines · Hans de Goede
Hi,
Quoted 122 lines · Ilpo Järvinen
Quoted 121 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len;Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1-wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }Thanks, this is way easier to follow.
Quoted 6 lines
I'm glad you like it.
There is a little bug in this version though, the goto error on the checksum fail case returns i + i, which should be i in that case, I'll just drop the goto there and instead always use the return i already present at the end of the
"if (dell_bl->resp_idx==dell_bl->resp_len) { }" block.It could have been solved more logically incrementing i and
resp_idxhere:dell_bl->resp[dell_bl->resp_idx] = data[i]; dell_bl->resp_idx++; i++;so that the inconsistent state is eliminated.
I also realized (I know I was the one who suggested it) that reverse logic would be better for the incomplete frame check:
Quoted 6 lines
if (dell_bl->resp_idx < dell_bl->resp_len) return len; // checksum logic...Perhaps the success and error return paths could then be merged too.
Interesting suggestion, I also realized that the 2 response-length checks are a range check so I've folded those together. So here is what I have now for v3, note that the i++ is now done when copying data over:
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 (!in_range(dell_bl->resp_len, MIN_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;
}
dell_bl->resp_idx++;
}
if (dell_bl->resp_idx != dell_bl->resp_len)
return len; /* 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) {
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;
}
wakeup:
wake_up(&dell_bl->wait_queue);
return i;
}Regards,
Hans
Ilpo Järvinen
maintainerQuoted 163 lines · Hans de Goede
Quoted 151 lines · Ilpo Järvinen
Quoted 132 lines · Hans de Goede
Quoted 122 lines · Ilpo Järvinen
Quoted 121 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len;Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1-wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }Thanks, this is way easier to follow.
Quoted 6 lines
I'm glad you like it.
There is a little bug in this version though, the goto error on the checksum fail case returns i + i, which should be i in that case, I'll just drop the goto there and instead always use the return i already present at the end of the
"if (dell_bl->resp_idx==dell_bl->resp_len) { }" block.It could have been solved more logically incrementing i and
resp_idxhere:dell_bl->resp[dell_bl->resp_idx] = data[i]; dell_bl->resp_idx++; i++;so that the inconsistent state is eliminated.
I also realized (I know I was the one who suggested it) that reverse logic would be better for the incomplete frame check:
Quoted 6 lines
if (dell_bl->resp_idx < dell_bl->resp_len) return len; // checksum logic...Perhaps the success and error return paths could then be merged too.
Interesting suggestion, I also realized that the 2 response-length checks are a range check so I've folded those together. So here is what I have now for v3, note that the i++ is now done when copying data over:
Quoted 8 lines
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 (!in_range(dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len)) {in_range() takes start and len, not start and end. I really hate that helper because it has that trap and would often require "+ min" to be added.
Quoted 8 lines
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) {Quoted 8 lines
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++;
Good, I didn't realize the switch used the index.
Signature · 1 line
i.
Quoted 23 lines
} if (dell_bl->resp_idx != dell_bl->resp_len) return len; /* 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) { 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; } wakeup: wake_up(&dell_bl->wait_queue); return i; }Regards,
Hans
Hi,
Quoted 168 lines · Ilpo Järvinen
Quoted 167 lines · Hans de Goede
Quoted 151 lines · Ilpo Järvinen
Quoted 132 lines · Hans de Goede
Quoted 122 lines · Ilpo Järvinen
Quoted 121 lines · Hans de Goede
Quoted 38 lines · Ilpo Järvinen
Quoted 21 lines · Hans de Goede
Quoted 16 lines · Ilpo Järvinen
Review
Quoted 12 lines · Hans de Goede
(quoted diff) +12
-0+ + dell_bl->resp_idx++; + if (dell_bl->resp_idx < dell_bl->resp_len) + continue; + + csum = dell_uart_checksum(dell_bl->resp, dell_bl->resp_len - 1); + if (dell_bl->resp[dell_bl->resp_len - 1] != csum) { + 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; + }Why is the checksum calculation and check inside the loop??
The loop iterates over received bytes, which may contain extra data after the response, the:
Quoted 8 lines
dell_bl->resp_idx++; if (dell_bl->resp_idx < dell_bl->resp_len) continue;continues looping until we have received all the expected bytes. So here, past this check, we are are at the point where we have a complete response and then we verify it.
And on successful verification wake-up any waiters.
So effectively you want to terminate the loop on two conditions here:
a)
dell_bl->resp_idx==dell_bl->resp_len(complete frame)
a) if i == len (not yet received a full frame)Why not code those rather than the current goto & continue madness?
Quoted 7 lines
Then, after the loop, you can test:
if (dell_bl->resp_idx == dell_bl->resp_len) { // calc checksum, etc. }?
Ok, I've added the following change for v3:
drivers/platform/x86/dell/dell-uart-backlight.c +10
-15@@ -87,6 +87,7 @@ static int dell_uart_bl_command(struct dell_uart_backlight *dell_bl, dell_bl->status = -EBUSY ; dell_bl->resp = resp; dell_bl->resp_idx =0 ; + dell_bl->resp_len =-1 ;/* Invalid / unset */ dell_bl->resp_max_len = resp_max_len; dell_bl->pending_cmd = cmd[1 ];@@ -219,7 +219,7 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,return len; } -for (i =0 ; i < len; i++) { +for (i =0 ; i < len && dell_bl->resp_idx != dell_bl->resp_len; i++, dell_bl->resp_idx++) { dell_bl->resp[dell_bl->resp_idx] = data[i];switch (dell_bl->resp_idx) {@@ -228,46 +228,41 @@ static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data,if (dell_bl->resp_len <MIN_RESP_LEN ) {dev_err (dell_bl->dev,"Response length too small %d < %d\n" , dell_bl->resp_len,MIN_RESP_LEN ); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }if (dell_bl->resp_len > dell_bl->resp_max_len) {dev_err (dell_bl->dev,"Response length too big %d > %d\n" , dell_bl->resp_len, dell_bl->resp_max_len); - dell_bl->status = -EIO ; - goto wakeup; + goto error; }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; + goto error; }break ; } + } - dell_bl->resp_idx++; -if (dell_bl->resp_idx < dell_bl->resp_len) -continue ; - +if (dell_bl->resp_idx == dell_bl->resp_len) { csum =dell_uart_checksum (dell_bl->resp, dell_bl->resp_len -1 );if (dell_bl->resp[dell_bl->resp_len -1 ] != csum) {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; + goto error; } - dell_bl->status =0 ;/* Success */ - goto wakeup; +wake_up (&dell_bl->wait_queue); +return i; }return len;Quoted 8 lines
drivers/platform/x86/dell/dell-uart-backlight.c static size_t dell_uart_bl_receive(struct serdev_device *serdev, const u8 *data, · line 268
Quoted 6 lines
drivers/platform/x86/dell/dell-uart-backlight.c +2
-1-wakeup: +error: + dell_bl->status = -EIO ;wake_up (&dell_bl->wait_queue);return i +1 ; }Thanks, this is way easier to follow.
Quoted 6 lines
I'm glad you like it.
There is a little bug in this version though, the goto error on the checksum fail case returns i + i, which should be i in that case, I'll just drop the goto there and instead always use the return i already present at the end of the
"if (dell_bl->resp_idx==dell_bl->resp_len) { }" block.It could have been solved more logically incrementing i and
resp_idxhere:dell_bl->resp[dell_bl->resp_idx] = data[i]; dell_bl->resp_idx++; i++;so that the inconsistent state is eliminated.
I also realized (I know I was the one who suggested it) that reverse logic would be better for the incomplete frame check:
Quoted 6 lines
if (dell_bl->resp_idx < dell_bl->resp_len) return len; // checksum logic...Perhaps the success and error return paths could then be merged too.
Interesting suggestion, I also realized that the 2 response-length checks are a range check so I've folded those together. So here is what I have now for v3, note that the i++ is now done when copying data over:
i = 0; while (i < len && dell_bl->resp_idx != dell_bl->resp_len) { dell_bl->resp[dell_bl->resp_idx] = data[i++];
Quoted 8 lines
Quoted 4 lines
switch (dell_bl->resp_idx) { case RESP_LEN: /* Length byte */ dell_bl->resp_len = dell_bl->resp[RESP_LEN]; if (!in_range(dell_bl->resp_len, MIN_RESP_LEN, dell_bl->resp_max_len)) {
in_range()takes start and len, not start and end. I really hate that helper because it has that trap and would often require "+ min" to be added.
Thank you for caching that.
I'll just switch to open coding the range check then for v3.
Regards,
Hans
PATCH 2/2 tools arch x86: Add dell-uart-backlight-emulator
In reply to Hans de GoedeDell 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.
--- .../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 | 166 ++++++++++++++++++ 4 files changed, 232 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
@@ -0,0 +1 @@ +dell-uart-backlight-emulator
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +19 -0
@@ -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 +166 -0
166 lines not on this page · raw message
Signature · 1 line
2.44.0
Re: [PATCH 2/2] tools arch x86: Add dell-uart-backlight-emulator
Quoted 7 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.cdriver for this board, without requiring access to an actual Dell All In One.
...
Quoted 11 lines
+++ b/tools/arch/x86/dell-uart-backlight-emulator/Makefile
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +9
-0@@ -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 +
tools/arch/x86/dell-uart-backlight-emulator/Makefile line 19
Quoted 10 lines
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +10
-0+%: %.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
Is it possible to fix this to (at least) honour make O=... cases? (See, e.g., tools/gpio.)
...
Quoted 1 line
+/*
read()will return -1 on SIGINT / SIGTERM causing the mainloop to cleanly exit */
Interesting... usually we handle error codes, such as EAGAIN and EINTR from read() syscall separately.
Quoted 3 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +3
-0+void signalhdlr (int signum) +{ +}
...
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 61
Quoted 3 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +2
-0+fprintf (stderr,"Error opening %s: %s\n" , argv[1 ],strerror (errno)); +fprintf (stderr,"Error getting tcattr: %s\n" ,strerror (errno));
(and so on)
Wouldn't perror() call be better?
...
Quoted 1 line
+ switch ((buf[0] << 8) | buf[1]) {
byteorder.h is part of UAPI, you can use it, but OTOH it might be too complicated for the small thing like this.
Quoted 1 line
+ }
...
Quoted 1 line
+ return ret;
Hmm... Hopefully you checked the possible returned codes, in user space it's only a positive 8-bit value used.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 2/2] tools arch x86: Add dell-uart-backlight-emulator
Hi,
Quoted 27 lines · Andy Shevchenko
Quoted 7 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.cdriver for this board, without requiring access to an actual Dell All In One....
Quoted 16 lines
+++ b/tools/arch/x86/dell-uart-backlight-emulator/Makefile
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +14
-0@@ -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 :
Quoted 8 lines
tools/arch/x86/dell-uart-backlight-emulator/Makefile line 19
Quoted 5 lines
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +5
-0+ @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-emulatorIs it possible to fix this to (at least) honour
make O=...cases? (See, e.g.,tools/gpio.)
I'll take a look at what the tools/gpio Makefile is doing.
Quoted 7 lines
...
Quoted 1 line
+/*
read()will return -1 on SIGINT / SIGTERM causing the mainloop to cleanly exit */Interesting... usually we handle error codes, such as EAGAIN and EINTR from
read()syscall separately.
EAGAIN cannot happen since the fd is kept in its default blocking mode. Other errors are also not expected to happen and would likely lead to aborting the program anyway.
So just having an empty signal handler and then exit on the EINTR error from read() is a nice KISS way to exit the main loop.
Quoted 7 lines
Quoted 4 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +3
-0+void signalhdlr (int signum) +{ +}...
Quoted 7 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c line 61
Quoted 3 lines
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c new file +2
-0+fprintf (stderr,"Error opening %s: %s\n" , argv[1 ],strerror (errno)); +fprintf (stderr,"Error getting tcattr: %s\n" ,strerror (errno));(and so on)
Wouldn't
perror()call be better?
perror() takes a fixed string, so for your first example it won't work since that requires printf style formatted string support and once I made the choice there to use fprintf(stderr, ) I used it everywhere for consistency.
Quoted 8 lines
...
Quoted 1 line
+ switch ((buf[0] << 8) | buf[1]) {
byteorder.his part of UAPI, you can use it, but OTOH it might be too complicated for the small thing like this.
Quoted 8 lines
Quoted 1 line
+ }
...
Quoted 1 line
+ return ret;
Hmm... Hopefully you checked the possible returned codes, in user space it's only a positive 8-bit value used.
That is a good point, actually in normal use ret will be (len + 3) from the last write() call done in the loop. So you're right that needs some work.
Regards,
Hans
Re: [PATCH 2/2] tools arch x86: Add dell-uart-backlight-emulator
In reply to Andy ShevchenkoHi,
Quoted 27 lines · Andy Shevchenko
Quoted 7 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.cdriver for this board, without requiring access to an actual Dell All In One....
Quoted 16 lines
+++ b/tools/arch/x86/dell-uart-backlight-emulator/Makefile
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +14
-0@@ -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 :
Quoted 8 lines
tools/arch/x86/dell-uart-backlight-emulator/Makefile line 19
Quoted 5 lines
tools/arch/x86/dell-uart-backlight-emulator/Makefile new file +5
-0+ @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-emulatorIs it possible to fix this to (at least) honour
make O=...cases? (See, e.g.,tools/gpio.)
I have taken a look but this only seems applicable to tools which are listed in tools/Makefile which includes scripts/Makefile.include. this are mostly tools which distributions may want to package as part of a kernel-tools package. I don't expect distros to package this, so this seems to not be applicable to this emulator.
Regards,
hans