[v2,1/2] hwmon: (nct6775) Directly call ASUS ACPI WMI method

Message ID 20230109211508.4969-1-pauk.denis@gmail.com
State New
Headers
Series [v2,1/2] hwmon: (nct6775) Directly call ASUS ACPI WMI method |

Commit Message

Denis Pauk Jan. 9, 2023, 9:15 p.m. UTC
  New ASUS B650/B660/X670 boards firmware have not exposed WMI monitoring
GUID  and entrypoint method WMBD could be implemented for different device
UID.

Implement the direct call to entrypoint method for monitoring the device
UID of B550/X570 boards.

BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=204807
Signed-off-by: Denis Pauk <pauk.denis@gmail.com>
Co-developed-by: Ahmad Khalifa <ahmad@khalifa.ws>
Signed-off-by: Ahmad Khalifa <ahmad@khalifa.ws>
---
Changes:
v1:
  rename each_port_arg to each_device_arg
  rename nct6775_find_asus_acpi to nct6775_asuswmi_device_match
  remove unrequired return -EEXIST, and iterate whole list of devices
  make asus_acpi_dev static

 drivers/hwmon/Kconfig            |  2 +-
 drivers/hwmon/nct6775-platform.c | 97 ++++++++++++++++++++++----------
 2 files changed, 69 insertions(+), 30 deletions(-)


base-commit: b0587c87abc891e313d63946ff8c9f4939d1ea1a
  

Comments

kernel test robot Jan. 10, 2023, 1:14 a.m. UTC | #1
Hi Denis,

I love your patch! Yet something to improve:

[auto build test ERROR on b0587c87abc891e313d63946ff8c9f4939d1ea1a]

url:    https://github.com/intel-lab-lkp/linux/commits/Denis-Pauk/hwmon-nct6775-B650-B660-X670-ASUS-boards-support/20230110-051917
base:   b0587c87abc891e313d63946ff8c9f4939d1ea1a
patch link:    https://lore.kernel.org/r/20230109211508.4969-1-pauk.denis%40gmail.com
patch subject: [PATCH v2 1/2] hwmon: (nct6775) Directly call ASUS ACPI WMI method
config: x86_64-buildonly-randconfig-r004-20230109
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/7cc524a49c17a240b048e24a91305c6159131aed
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Denis-Pauk/hwmon-nct6775-B650-B660-X670-ASUS-boards-support/20230110-051917
        git checkout 7cc524a49c17a240b048e24a91305c6159131aed
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/hwmon/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All error/warnings (new ones prefixed by >>):

>> drivers/hwmon/nct6775-platform.c:1116:29: error: implicit declaration of function 'to_acpi_device' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           struct acpi_device *adev = to_acpi_device(dev);
                                      ^
>> drivers/hwmon/nct6775-platform.c:1116:22: warning: incompatible integer to pointer conversion initializing 'struct acpi_device *' with an expression of type 'int' [-Wint-conversion]
           struct acpi_device *adev = to_acpi_device(dev);
                               ^      ~~~~~~~~~~~~~~~~~~~
>> drivers/hwmon/nct6775-platform.c:1117:20: error: implicit declaration of function 'acpi_device_uid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           const char *uid = acpi_device_uid(adev);
                             ^
>> drivers/hwmon/nct6775-platform.c:1117:14: warning: incompatible integer to pointer conversion initializing 'const char *' with an expression of type 'int' [-Wint-conversion]
           const char *uid = acpi_device_uid(adev);
                       ^     ~~~~~~~~~~~~~~~~~~~~~
>> drivers/hwmon/nct6775-platform.c:1118:20: error: implicit declaration of function 'acpi_device_hid' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           const char *hid = acpi_device_hid(adev);
                             ^
   drivers/hwmon/nct6775-platform.c:1118:20: note: did you mean 'acpi_device_uid'?
   drivers/hwmon/nct6775-platform.c:1117:20: note: 'acpi_device_uid' declared here
           const char *uid = acpi_device_uid(adev);
                             ^
   drivers/hwmon/nct6775-platform.c:1118:14: warning: incompatible integer to pointer conversion initializing 'const char *' with an expression of type 'int' [-Wint-conversion]
           const char *hid = acpi_device_hid(adev);
                       ^     ~~~~~~~~~~~~~~~~~~~~~
>> drivers/hwmon/nct6775-platform.c:1135:2: error: implicit declaration of function 'acpi_bus_for_each_dev' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           acpi_bus_for_each_dev(nct6775_asuswmi_device_match, &arg);
           ^
   drivers/hwmon/nct6775-platform.c:1135:2: note: did you mean 'bus_for_each_dev'?
   include/linux/device/bus.h:164:5: note: 'bus_for_each_dev' declared here
   int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
       ^
   3 warnings and 4 errors generated.


vim +/to_acpi_device +1116 drivers/hwmon/nct6775-platform.c

  1109	
  1110	/*
  1111	 * Callback for acpi_bus_for_each_dev() to find the right device
  1112	 * by _UID and _HID and store to each_device_arg.
  1113	 */
  1114	static int nct6775_asuswmi_device_match(struct device *dev, void *data)
  1115	{
> 1116		struct acpi_device *adev = to_acpi_device(dev);
> 1117		const char *uid = acpi_device_uid(adev);
> 1118		const char *hid = acpi_device_hid(adev);
  1119		struct each_device_arg *arg = data;
  1120	
  1121		if (hid && !strcmp(hid, ASUSWMI_DEVICE_HID) &&
  1122			uid && !strcmp(uid, arg->match)) {
  1123			arg->adev = adev;
  1124		}
  1125	
  1126		return 0;
  1127	}
  1128	
  1129	static enum sensor_access nct6775_determine_access(const char *device_uid)
  1130	{
  1131		struct each_device_arg arg;
  1132		u8 tmp;
  1133	
  1134		arg.match = device_uid;
> 1135		acpi_bus_for_each_dev(nct6775_asuswmi_device_match, &arg);
  1136		if (!arg.adev)
  1137			return access_direct;
  1138	
  1139		asus_acpi_dev = arg.adev;
  1140		/* if reading chip id via ACPI succeeds, use WMI "WMBD" method for access */
  1141		if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) {
  1142			pr_debug("Using Asus WMBD method of %s to access %#x chip.\n", device_uid, tmp);
  1143			return access_asuswmi;
  1144		}
  1145	
  1146		return access_direct;
  1147	}
  1148
  

Patch

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 3176c33af6c6..300ce8115ce4 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1516,7 +1516,7 @@  config SENSORS_NCT6775_CORE
 config SENSORS_NCT6775
 	tristate "Platform driver for Nuvoton NCT6775F and compatibles"
 	depends on !PPC
-	depends on ACPI_WMI || ACPI_WMI=n
+	depends on ACPI || ACPI=n
 	select HWMON_VID
 	select SENSORS_NCT6775_CORE
 	help
diff --git a/drivers/hwmon/nct6775-platform.c b/drivers/hwmon/nct6775-platform.c
index bf43f73dc835..1f7885af524e 100644
--- a/drivers/hwmon/nct6775-platform.c
+++ b/drivers/hwmon/nct6775-platform.c
@@ -17,7 +17,6 @@ 
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
-#include <linux/wmi.h>
 
 #include "nct6775.h"
 
@@ -107,40 +106,44 @@  struct nct6775_sio_data {
 	void (*sio_exit)(struct nct6775_sio_data *sio_data);
 };
 
-#define ASUSWMI_MONITORING_GUID		"466747A0-70EC-11DE-8A39-0800200C9A66"
+#define ASUSWMI_METHOD			"WMBD"
 #define ASUSWMI_METHODID_RSIO		0x5253494F
 #define ASUSWMI_METHODID_WSIO		0x5753494F
 #define ASUSWMI_METHODID_RHWM		0x5248574D
 #define ASUSWMI_METHODID_WHWM		0x5748574D
 #define ASUSWMI_UNSUPPORTED_METHOD	0xFFFFFFFE
+#define ASUSWMI_DEVICE_HID		"PNP0C14"
+#define ASUSWMI_DEVICE_UID		"ASUSWMI"
+
+static struct acpi_device *asus_acpi_dev;
 
 static int nct6775_asuswmi_evaluate_method(u32 method_id, u8 bank, u8 reg, u8 val, u32 *retval)
 {
-#if IS_ENABLED(CONFIG_ACPI_WMI)
+#if IS_ENABLED(CONFIG_ACPI)
+	acpi_handle handle = acpi_device_handle(asus_acpi_dev);
 	u32 args = bank | (reg << 8) | (val << 16);
-	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
-	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
+	struct acpi_object_list input;
+	union acpi_object params[3];
+	unsigned long long result;
 	acpi_status status;
-	union acpi_object *obj;
-	u32 tmp = ASUSWMI_UNSUPPORTED_METHOD;
-
-	status = wmi_evaluate_method(ASUSWMI_MONITORING_GUID, 0,
-				     method_id, &input, &output);
 
+	params[0].type = ACPI_TYPE_INTEGER;
+	params[0].integer.value = 0;
+	params[1].type = ACPI_TYPE_INTEGER;
+	params[1].integer.value = method_id;
+	params[2].type = ACPI_TYPE_BUFFER;
+	params[2].buffer.length = sizeof(args);
+	params[2].buffer.pointer = (void *)&args;
+	input.count = 3;
+	input.pointer = params;
+
+	status = acpi_evaluate_integer(handle, ASUSWMI_METHOD, &input, &result);
 	if (ACPI_FAILURE(status))
 		return -EIO;
 
-	obj = output.pointer;
-	if (obj && obj->type == ACPI_TYPE_INTEGER)
-		tmp = obj->integer.value;
-
 	if (retval)
-		*retval = tmp;
-
-	kfree(obj);
+		*retval = (u32)result & 0xFFFFFFFF;
 
-	if (tmp == ASUSWMI_UNSUPPORTED_METHOD)
-		return -ENODEV;
 	return 0;
 #else
 	return -EOPNOTSUPP;
@@ -1099,6 +1102,50 @@  static const char * const asus_wmi_boards[] = {
 	"TUF GAMING Z490-PLUS (WI-FI)",
 };
 
+struct each_device_arg {
+	struct acpi_device *adev;
+	const char *match;
+};
+
+/*
+ * Callback for acpi_bus_for_each_dev() to find the right device
+ * by _UID and _HID and store to each_device_arg.
+ */
+static int nct6775_asuswmi_device_match(struct device *dev, void *data)
+{
+	struct acpi_device *adev = to_acpi_device(dev);
+	const char *uid = acpi_device_uid(adev);
+	const char *hid = acpi_device_hid(adev);
+	struct each_device_arg *arg = data;
+
+	if (hid && !strcmp(hid, ASUSWMI_DEVICE_HID) &&
+		uid && !strcmp(uid, arg->match)) {
+		arg->adev = adev;
+	}
+
+	return 0;
+}
+
+static enum sensor_access nct6775_determine_access(const char *device_uid)
+{
+	struct each_device_arg arg;
+	u8 tmp;
+
+	arg.match = device_uid;
+	acpi_bus_for_each_dev(nct6775_asuswmi_device_match, &arg);
+	if (!arg.adev)
+		return access_direct;
+
+	asus_acpi_dev = arg.adev;
+	/* if reading chip id via ACPI succeeds, use WMI "WMBD" method for access */
+	if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) {
+		pr_debug("Using Asus WMBD method of %s to access %#x chip.\n", device_uid, tmp);
+		return access_asuswmi;
+	}
+
+	return access_direct;
+}
+
 static int __init sensors_nct6775_platform_init(void)
 {
 	int i, err;
@@ -1109,7 +1156,6 @@  static int __init sensors_nct6775_platform_init(void)
 	int sioaddr[2] = { 0x2e, 0x4e };
 	enum sensor_access access = access_direct;
 	const char *board_vendor, *board_name;
-	u8 tmp;
 
 	err = platform_driver_register(&nct6775_driver);
 	if (err)
@@ -1122,15 +1168,8 @@  static int __init sensors_nct6775_platform_init(void)
 	    !strcmp(board_vendor, "ASUSTeK COMPUTER INC.")) {
 		err = match_string(asus_wmi_boards, ARRAY_SIZE(asus_wmi_boards),
 				   board_name);
-		if (err >= 0) {
-			/* if reading chip id via WMI succeeds, use WMI */
-			if (!nct6775_asuswmi_read(0, NCT6775_PORT_CHIPID, &tmp) && tmp) {
-				pr_info("Using Asus WMI to access %#x chip.\n", tmp);
-				access = access_asuswmi;
-			} else {
-				pr_err("Can't read ChipID by Asus WMI.\n");
-			}
-		}
+		if (err >= 0)
+			access = nct6775_determine_access(ASUSWMI_DEVICE_UID);
 	}
 
 	/*