linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type
@ 2022-02-11  0:36 Eugene Shalygin
  2022-02-11  0:40 ` Eugene Shalygin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eugene Shalygin @ 2022-02-11  0:36 UTC (permalink / raw)
  Cc: Oleksandr Natalenko, Denis Pauk, Eugene Shalygin, Jean Delvare,
	Guenter Roeck, linux-hwmon, linux-kernel

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.

Signed-off-by: Eugene Shalygin <eugene.shalygin@gmail.com>
---
 drivers/hwmon/asus-ec-sensors.c | 38 +++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/drivers/hwmon/asus-ec-sensors.c b/drivers/hwmon/asus-ec-sensors.c
index bfac08a5dc57..a1b13fe149ac 100644
--- a/drivers/hwmon/asus-ec-sensors.c
+++ b/drivers/hwmon/asus-ec-sensors.c
@@ -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;
+		}
 	}
 }
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type
  2022-02-11  0:36 [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type Eugene Shalygin
@ 2022-02-11  0:40 ` Eugene Shalygin
  2022-02-11  4:40 ` Guenter Roeck
  2022-02-11 16:48 ` [PATCH v2] hwmon: (asus-ec-sensors) deduce sensor signedness " Eugene Shalygin
  2 siblings, 0 replies; 4+ messages in thread
From: Eugene Shalygin @ 2022-02-11  0:40 UTC (permalink / raw)
  Cc: Oleksandr Natalenko, Denis Pauk, Jean Delvare, Guenter Roeck,
	linux-hwmon, Linux Kernel Mailing List

On Fri, 11 Feb 2022 at 01:36, Eugene Shalygin <eugene.shalygin@gmail.com> wrote:
>
> 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.

>  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;
> +               }
>         }
>  }

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

Thank you,
Eugene

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type
  2022-02-11  0:36 [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type Eugene Shalygin
  2022-02-11  0:40 ` Eugene Shalygin
@ 2022-02-11  4:40 ` Guenter Roeck
  2022-02-11 16:48 ` [PATCH v2] hwmon: (asus-ec-sensors) deduce sensor signedness " Eugene Shalygin
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2022-02-11  4:40 UTC (permalink / raw)
  To: Eugene Shalygin
  Cc: Oleksandr Natalenko, Denis Pauk, Jean Delvare, linux-hwmon, linux-kernel

On 2/10/22 16:36, Eugene Shalygin wrote:
> 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

> type and that only temperature ones provide signed numbers.
> 
> Signed-off-by: Eugene Shalygin <eugene.shalygin@gmail.com>
> ---
>   drivers/hwmon/asus-ec-sensors.c | 38 +++++++++++++++++++++++++--------
>   1 file changed, 29 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/hwmon/asus-ec-sensors.c b/drivers/hwmon/asus-ec-sensors.c
> index bfac08a5dc57..a1b13fe149ac 100644
> --- a/drivers/hwmon/asus-ec-sensors.c
> +++ b/drivers/hwmon/asus-ec-sensors.c
> @@ -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
  */

> +	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;
> +		}
>   	}
>   }
>   


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] hwmon: (asus-ec-sensors) deduce sensor signedness from its type
  2022-02-11  0:36 [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type Eugene Shalygin
  2022-02-11  0:40 ` Eugene Shalygin
  2022-02-11  4:40 ` Guenter Roeck
@ 2022-02-11 16:48 ` Eugene Shalygin
  2 siblings, 0 replies; 4+ messages in thread
From: Eugene Shalygin @ 2022-02-11 16:48 UTC (permalink / raw)
  Cc: Oleksandr Natalenko, Denis Pauk, Eugene Shalygin, Jean Delvare,
	Guenter Roeck, linux-hwmon, linux-kernel

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.

Signed-off-by: Eugene Shalygin <eugene.shalygin@gmail.com>
---
 drivers/hwmon/asus-ec-sensors.c | 40 +++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/drivers/hwmon/asus-ec-sensors.c b/drivers/hwmon/asus-ec-sensors.c
index bfac08a5dc57..d2b84578d2af 100644
--- a/drivers/hwmon/asus-ec-sensors.c
+++ b/drivers/hwmon/asus-ec-sensors.c
@@ -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;
+		}
 	}
 }
 
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-11 16:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-11  0:36 [PATCH] hwmon: (asus-ec-sensors) deduce sensor signess from its type Eugene Shalygin
2022-02-11  0:40 ` Eugene Shalygin
2022-02-11  4:40 ` Guenter Roeck
2022-02-11 16:48 ` [PATCH v2] hwmon: (asus-ec-sensors) deduce sensor signedness " Eugene Shalygin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).