Re: [PATCH v3 2/4] hwmon: (aquacomputer_d5next) Avoid truncating scaled sensor readings
From: Guenter Roeck <linux@roeck-us.net>
Date: 2026-09-15 04:38:20
Also in:
linux-doc, linux-hwmon, lkml
On 9/14/26 16:37, Vas Zayarskiy wrote:
High Flow Next power is reported in watts. Multiplying the 16-bit value by one million can overflow both the signed intermediate and the u32 cache. Store power in long, perform the conversion in u64, and clamp to LONG_MAX. The other existing power conversions fit in a signed 32-bit value. Return ENODATA for unavailable power instead of exposing the
-ENODATA
stored error as a reading. Widen the shared current cache to u32. The Aquastream XT calibration can exceed 65535 mA; clamp its negative calibration results to zero before
That isn't a calibration. It is a conversion or scaling to hwmon ABI units.
quoted hunk ↗ jump to hunk
assigning to the unsigned cache. This also permits devices with scaled aggregate currents to store milliamperes directly. Assisted-by: LLM sparse Signed-off-by: Vas Zayarskiy <redacted> --- drivers/hwmon/aquacomputer_d5next.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)diff --git a/drivers/hwmon/aquacomputer_d5next.c b/drivers/hwmon/aquacomputer_d5next.c index 1ca70e726..9178ac4cb 100644 --- a/drivers/hwmon/aquacomputer_d5next.c +++ b/drivers/hwmon/aquacomputer_d5next.c@@ -19,6 +19,7 @@ #include <linux/hwmon.h> #include <linux/jiffies.h> #include <linux/ktime.h> +#include <linux/limits.h> #include <linux/module.h> #include <linux/seq_file.h> #include <linux/unaligned.h>@@ -599,9 +600,9 @@ struct aqc_data { u32 speed_input_min[1]; u32 speed_input_target[1]; u32 speed_input_max[1]; - u32 power_input[8]; + long power_input[8]; u16 voltage_input[8]; - u16 current_input[8]; + u32 current_input[8]; /* Label values */ const char *const *temp_label;@@ -976,7 +977,7 @@ static int aqc_legacy_read(struct aqc_data *priv) /* Calculation derived from linear regression */ sensor_value = get_unaligned_le16(priv->buffer + AQUASTREAMXT_PUMP_CURR_OFFSET); - priv->current_input[0] = DIV_ROUND_CLOSEST(sensor_value * 176, 100) - 52; + priv->current_input[0] = max(DIV_ROUND_CLOSEST(sensor_value * 176, 100) - 52, 0); sensor_value = get_unaligned_le16(priv->buffer + AQUASTREAMXT_PUMP_VOLTAGE_OFFSET); priv->voltage_input[0] = DIV_ROUND_CLOSEST(sensor_value * 1000, 61);@@ -1070,6 +1071,8 @@ static int aqc_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, } break; case hwmon_power: + if (priv->power_input[channel] == -ENODATA) + return -ENODATA; *val = priv->power_input[channel]; break; case hwmon_pwm:@@ -1426,7 +1429,8 @@ static int aqc_raw_event(struct hid_device *hdev, struct hid_report *report, u8 priv->power_input[0] = -ENODATA; else priv->power_input[0] = - get_unaligned_be16(data + HIGHFLOWNEXT_POWER) * 1000000; + min_t(u64, get_unaligned_be16(data + HIGHFLOWNEXT_POWER) * 1000000ULL, + LONG_MAX); priv->voltage_input[0] = get_unaligned_be16(data + HIGHFLOWNEXT_5V_VOLTAGE) * 10; priv->voltage_input[1] =