Skip to contentSkip to conversation

hwmon: (asus-ec-sensors) deduce sensor signess from its type

PATCH Superseded Eugene Shalygin 2 participants 4 messages Feb 11, 2022

Subsystems (2)
lore
Share & tools
lore

Eugene Shalygin

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (asus-ec-sensors) deduce sensor signess from its type

Reading DSDT code for ASUS X470-based boards (the ones served by the asus_wmi_Sensors driver), where ASUS put hardware monitoring functions into the WMI code, reveals that fan and current sensors data is unsigned. For the current sensor that was cofirmed by a user who showed high enough current value for overflow.

Thus let's assume that the signess of the sensors is determined by its type and that only temperature ones provide signed numbers.

---
 drivers/hwmon/asus-ec-sensors.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)
drivers/hwmon/asus-ec-sensors.c +29 -9
index bfac08a5dc57..a1b13fe149ac 100644
@@ -266,6 +266,13 @@ static u8 register_index(u16 reg)
 	return reg & 0x00ff;
 }
 
+static bool is_sensor_data_signed(const struct ec_sensor_info *si)
+{
+	// guessed from WMI functions in DSDT code for boards
+	// of the X470 generation
+	return si->type == hwmon_temp;
+}
+
 static const struct ec_sensor_info *
 get_sensor_info(const struct ec_sensors_data *state, int index)
 {
@@ -420,15 +427,28 @@ static int asus_ec_block_read(const struct device *dev,
 
 static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
 {
-	switch (si->addr.components.size) {
-	case 1:
-		return (s8)*data;
-	case 2:
-		return (s16)get_unaligned_be16(data);
-	case 4:
-		return (s32)get_unaligned_be32(data);
-	default:
-		return 0;
+	if (is_sensor_data_signed(si)) {
+		switch (si->addr.components.size) {
+		case 1:
+			return (s8)*data;
+		case 2:
+			return (s16)get_unaligned_be16(data);
+		case 4:
+			return (s32)get_unaligned_be32(data);
+		default:
+			return 0;
+		}
+	} else {
+		switch (si->addr.components.size) {
+		case 1:
+			return *data;
+		case 2:
+			return get_unaligned_be16(data);
+		case 4:
+			return get_unaligned_be32(data);
+		default:
+			return 0;
+		}
 	}
 }
 
Signature · 1 line
2.35.1
author maintainer
#1 lore raw
Quoted 6 lines · Eugene Shalygin

Reading DSDT code for ASUS X470-based boards (the ones served by the asus_wmi_Sensors driver), where ASUS put hardware monitoring functions into the WMI code, reveals that fan and current sensors data is unsigned. For the current sensor that was confirmed by a user who showed high enough current value for overflow.

Denis, you might be interested in fixing temperature sensors in the asus_wmi_sensors driver too.

Quoted 25 lines

static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data) {

drivers/hwmon/asus-ec-sensors.c +14 -9
-       switch (si->addr.components.size) {
-       case 1:
-               return (s8)*data;
-       case 2:
-               return (s16)get_unaligned_be16(data);
-       case 4:
-               return (s32)get_unaligned_be32(data);
-       default:
-               return 0;
+       if (is_sensor_data_signed(si)) {
+               switch (si->addr.components.size) {
+               case 1:
+                       return (s8)*data;
+               case 2:
+                       return (s16)get_unaligned_be16(data);
+               case 4:
+                       return (s32)get_unaligned_be32(data);
+               default:
+                       return 0;
+               }
+       } else {
+               switch (si->addr.components.size) {
+               case 1:

drivers/hwmon/asus-ec-sensors.c static int asus_ec_block_read(const struct device *dev, · line 453

Quoted 10 lines
drivers/hwmon/asus-ec-sensors.c +8 -0
+                       return *data;
+               case 2:
+                       return get_unaligned_be16(data);
+               case 4:
+                       return get_unaligned_be32(data);
+               default:
+                       return 0;
+               }
        }
 }

I would appreciate it if anyone could help me to collapse this ugliness somehow, without multi-level macros, please.

Thank you,
Eugene

maintainer
#2 lore raw
In reply to Eugene Shalygin
Quoted 7 lines · Eugene Shalygin

Reading DSDT code for ASUS X470-based boards (the ones served by the asus_wmi_Sensors driver), where ASUS put hardware monitoring functions into the WMI code, reveals that fan and current sensors data is unsigned. For the current sensor that was cofirmed by a user who showed high enough current value for overflow.

Thus let's assume that the signess of the sensors is determined by its

signedness

Quoted 12 lines

type and that only temperature ones provide signed numbers.

---
  drivers/hwmon/asus-ec-sensors.c | 38 +++++++++++++++++++++++++--------
  1 file changed, 29 insertions(+), 9 deletions(-)
drivers/hwmon/asus-ec-sensors.c +0 -0
index bfac08a5dc57..a1b13fe149ac 100644

drivers/hwmon/asus-ec-sensors.c static u8 register_index(u16 reg) · line 272

Quoted 7 lines
drivers/hwmon/asus-ec-sensors.c +4 -0
@@ -266,6 +266,13 @@ static u8 register_index(u16 reg)
  	return reg & 0x00ff;
  }
 
+static bool is_sensor_data_signed(const struct ec_sensor_info *si)
+{
+	// guessed from WMI functions in DSDT code for boards
+	// of the X470 generation

/*
* Please lets stick with standard multi-line comments */

Quoted 44 lines
drivers/hwmon/asus-ec-sensors.c +25 -9
+	return si->type == hwmon_temp;
+}
+
  static const struct ec_sensor_info *
  get_sensor_info(const struct ec_sensors_data *state, int index)
  {
@@ -420,15 +427,28 @@ static int asus_ec_block_read(const struct device *dev,
 
  static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
  {
-	switch (si->addr.components.size) {
-	case 1:
-		return (s8)*data;
-	case 2:
-		return (s16)get_unaligned_be16(data);
-	case 4:
-		return (s32)get_unaligned_be32(data);
-	default:
-		return 0;
+	if (is_sensor_data_signed(si)) {
+		switch (si->addr.components.size) {
+		case 1:
+			return (s8)*data;
+		case 2:
+			return (s16)get_unaligned_be16(data);
+		case 4:
+			return (s32)get_unaligned_be32(data);
+		default:
+			return 0;
+		}
+	} else {
+		switch (si->addr.components.size) {
+		case 1:
+			return *data;
+		case 2:
+			return get_unaligned_be16(data);
+		case 4:
+			return get_unaligned_be32(data);
+		default:
+			return 0;
+		}
  	}
  }
 
author maintainer
#3 lore raw

PATCH v2 1/1 hwmon: (asus-ec-sensors) deduce sensor signedness from its type

In reply to Eugene Shalygin

Reading DSDT code for ASUS X470-based boards (the ones served by the asus_wmi_Sensors driver), where ASUS put hardware monitoring functions into the WMI code, reveals that fan and current sensors data is unsigned. For the current sensor that was confirmed by a user who showed high enough current value for overflow.

Thus let's assume that the signedness of the sensors is determined by its type and that only temperature ones provide signed numbers.

---
 drivers/hwmon/asus-ec-sensors.c | 40 +++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 9 deletions(-)
drivers/hwmon/asus-ec-sensors.c +31 -9
index bfac08a5dc57..d2b84578d2af 100644
@@ -266,6 +266,15 @@ static u8 register_index(u16 reg)
 	return reg & 0x00ff;
 }
 
+static bool is_sensor_data_signed(const struct ec_sensor_info *si)
+{
+	/*
+	 * guessed from WMI functions in DSDT code for boards
+	 * of the X470 generation
+	 */
+	return si->type == hwmon_temp;
+}
+
 static const struct ec_sensor_info *
 get_sensor_info(const struct ec_sensors_data *state, int index)
 {
@@ -420,15 +429,28 @@ static int asus_ec_block_read(const struct device *dev,
 
 static inline s32 get_sensor_value(const struct ec_sensor_info *si, u8 *data)
 {
-	switch (si->addr.components.size) {
-	case 1:
-		return (s8)*data;
-	case 2:
-		return (s16)get_unaligned_be16(data);
-	case 4:
-		return (s32)get_unaligned_be32(data);
-	default:
-		return 0;
+	if (is_sensor_data_signed(si)) {
+		switch (si->addr.components.size) {
+		case 1:
+			return (s8)*data;
+		case 2:
+			return (s16)get_unaligned_be16(data);
+		case 4:
+			return (s32)get_unaligned_be32(data);
+		default:
+			return 0;
+		}
+	} else {
+		switch (si->addr.components.size) {
+		case 1:
+			return *data;
+		case 2:
+			return get_unaligned_be16(data);
+		case 4:
+			return get_unaligned_be32(data);
+		default:
+			return 0;
+		}
 	}
 }
 
Signature · 1 line
2.35.1