diff mbox series

[RFC,2/2] hwmon: (scmi) Filter out results wrongly interpreted as negatives

Message ID 20211220174155.40239-3-cristian.marussi@arm.com (mailing list archive)
State New, archived
Headers show
Series Sensor readings fixes | expand

Commit Message

Cristian Marussi Dec. 20, 2021, 5:41 p.m. UTC
SCMI Sensor scmi_reading_get interface can report only unsigned values
while hwmon_ops.read allows for signed negative values to be passed on:
this has the undesired side effect of silently interpreting as negative
any big-enough positive reading reported by the SCMI interface.

Filter out such big positives reporting an error.

Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 drivers/hwmon/scmi-hwmon.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/drivers/hwmon/scmi-hwmon.c b/drivers/hwmon/scmi-hwmon.c
index b1329a58ce40..0924d1b8a9ce 100644
--- a/drivers/hwmon/scmi-hwmon.c
+++ b/drivers/hwmon/scmi-hwmon.c
@@ -73,10 +73,24 @@  static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
 	struct scmi_sensors *scmi_sensors = dev_get_drvdata(dev);
 
 	sensor = *(scmi_sensors->info[type] + channel);
+	/*
+	 * Can fail with EIO if the backing SCMI Sensor FW version tried to
+	 * report a negative value.
+	 */
 	ret = sensor_ops->reading_get(scmi_sensors->ph, sensor->id, &value);
 	if (ret)
 		return ret;
 
+	/*
+	 * Cannot accept either valid positive values so big that would be
+	 * interpreted as negative by HWMON signed long *val return value.
+	 */
+	if (value & BIT(63)) {
+		dev_warn_once(dev,
+			      "Reported unsigned value too big.\n");
+		return -EIO;
+	}
+
 	ret = scmi_hwmon_scale(sensor, &value);
 	if (!ret)
 		*val = value;