Skip to contentSkip to conversation

platform/x86: wmi: Support reading/writing 16 bit EC values

PATCH Superseded Armin Wolf 2 participants 3 messages Mar 8, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Armin Wolf

author
#0 lore raw

PATCH v3 1/2 platform/x86: wmi: Support reading/writing 16 bit EC values

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Chnages since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
 drivers/platform/x86/wmi.c | 47 ++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +37 -10
index 1920e115da89..e6cab1cf611a 100644
@@ -1153,6 +1153,32 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
 	return 0;
 }
 
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	int i, ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	int i, ret;
+
+	for (i = 0; i < bytes; i++) {
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
 /*
  * WMI can have EmbeddedControl access regions. In which case, we just want to
  * hand these off to the EC driver.
@@ -1162,27 +1188,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 			  u32 bits, u64 *value,
 			  void *handler_context, void *region_context)
 {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
 		return AE_BAD_PARAMETER;
 
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
 		return AE_BAD_PARAMETER;
 
-	if (bits != 8)
+	if (function != ACPI_READ && function != ACPI_WRITE)
 		return AE_BAD_PARAMETER;
 
 	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
 	} else {
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
 	}
 
-	switch (result) {
+	switch (ret) {
 	case -EINVAL:
 		return AE_BAD_PARAMETER;
 	case -ENODEV:

--
2.39.2

author
#1 lore raw

PATCH v3 2/2 platform/x86: wmi: Avoid returning AE_OK upon unknown error

If an error code other than EINVAL, ENODEV or ETIME is returned by ec_read()/ec_write(), then AE_OK is wrongly returned.

Fix this by only returning AE_OK if the return code is 0, and return AE_ERROR otherwise.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
 drivers/platform/x86/wmi.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/platform/x86/wmi.c +3 -1
index e6cab1cf611a..3f75220558aa 100644
@@ -1216,8 +1216,10 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
 		return AE_NOT_FOUND;
 	case -ETIME:
 		return AE_TIME;
-	default:
+	case 0:
 		return AE_OK;
+	default:
+		return AE_ERROR;
 	}
 }
 

--
2.39.2

#2 lore raw
In reply to Armin Wolf
Quoted 27 lines · Armin Wolf

The ACPI EC address space handler currently only supports reading/writing 8 bit values. Some firmware implementations however want to access for example 16 bit values, which is prefectly legal according to the ACPI spec.

Add support for reading/writing such values.

Tested on a Dell Inspiron 3505 and a Asus Prime B650-Plus.

---
Chnages since v2:
- fix address overflow check

Changes since v1:
- use BITS_PER_BYTE
- validate that number of bytes to read/write does not overflow the address

---
  drivers/platform/x86/wmi.c | 47 ++++++++++++++++++++++++++++++--------
  1 file changed, 37 insertions(+), 10 deletions(-)
drivers/platform/x86/wmi.c +0 -0
index 1920e115da89..e6cab1cf611a 100644

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1160

Quoted 8 lines
drivers/platform/x86/wmi.c +5 -0
@@ -1153,6 +1153,32 @@ static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev)
  	return 0;
  }
 
+static int ec_read_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	int i, ret;
+
+	for (i = 0; i < bytes; i++) {

To make sure that you're comparing against the same size data you should either i "size_t" instead of "int".

drivers/platform/x86/wmi.c static int parse_wdg(struct device *wmi_bus_dev, struct platform_device *pdev) · line 1173

Quoted 13 lines
drivers/platform/x86/wmi.c +13 -0
+		ret = ec_read(address + i, &buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int ec_write_multiple(u8 address, u8 *buffer, size_t bytes)
+{
+	int i, ret;
+
+	for (i = 0; i < bytes; i++) {

Same comment as above.

Quoted 54 lines
drivers/platform/x86/wmi.c +19 -11
+		ret = ec_write(address + i, buffer[i]);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
  /*
   * WMI can have EmbeddedControl access regions. In which case, we just want to
   * hand these off to the EC driver.
@@ -1162,27 +1188,28 @@ acpi_wmi_ec_space_handler(u32 function, acpi_physical_address address,
  			  u32 bits, u64 *value,
  			  void *handler_context, void *region_context)
  {
-	int result = 0;
-	u8 temp = 0;
+	int bytes = bits / BITS_PER_BYTE;
+	int ret;
+
+	if (!value)
+		return AE_NULL_ENTRY;
 
-	if ((address > 0xFF) || !value)
+	if (!bytes || bytes > sizeof(*value))
  		return AE_BAD_PARAMETER;
 
-	if (function != ACPI_READ && function != ACPI_WRITE)
+	if (address > U8_MAX || address + bytes - 1 > U8_MAX)
  		return AE_BAD_PARAMETER;
 
-	if (bits != 8)
+	if (function != ACPI_READ && function != ACPI_WRITE)
  		return AE_BAD_PARAMETER;
 
  	if (function == ACPI_READ) {
-		result = ec_read(address, &temp);
-		*value = temp;
+		ret = ec_read_multiple(address, (u8 *)value, bytes);
  	} else {
-		temp = 0xff & *value;
-		result = ec_write(address, temp);
+		ret = ec_write_multiple(address, (u8 *)value, bytes);
  	}
 
-	switch (result) {
+	switch (ret) {
  	case -EINVAL:
  		return AE_BAD_PARAMETER;
  	case -ENODEV:
--

2.39.2