Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH] hwmon: (pcf8591) Propagate channel access errors
@ 2026-11-17 11:38 Siqi Chen
  2026-11-17 11:48 ` sashiko-bot
  2026-11-17 16:07 ` Guenter Roeck
  0 siblings, 2 replies; 3+ messages in thread
From: Siqi Chen @ 2026-11-17 11:38 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: linux-hwmon, linux-kernel, Siqi Chen, stable

The SMBus channel operations return negative error codes on failure.
The final read result is stored in a u8, causing an error to be
truncated and exposed through sysfs as a valid ADC value. Errors from
the channel selection and dummy read are also ignored.

Check all three operations and propagate their errors through sysfs.
Keep the cached channel invalid until both channel selection and the
dummy read succeed so that a later read retries synchronization after
a failure.

An AI coding assistant was used to identify the issue and help develop
the fix.

The change was compile-tested with W=1. No hardware testing was
performed.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Siqi Chen <chensiqi@uniontech.com>
---
 drivers/hwmon/pcf8591.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/hwmon/pcf8591.c b/drivers/hwmon/pcf8591.c
index ec98e6b66c49..602d0caeaced 100644
--- a/drivers/hwmon/pcf8591.c
+++ b/drivers/hwmon/pcf8591.c
@@ -68,6 +68,7 @@ struct pcf8591_data {
 	struct device *hwmon_dev;
 	struct mutex update_lock;
 
+	bool channel_valid;
 	u8 control;
 	u8 aout;
 };
@@ -81,7 +82,11 @@ static ssize_t show_in##channel##_input(struct device *dev,		\
 					struct device_attribute *attr,	\
 					char *buf)			\
 {									\
-	return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
+	int value = pcf8591_read_channel(dev, channel);			\
+									\
+	if (value < 0)						\
+		return value;						\
+	return sysfs_emit(buf, "%d\n", value);				\
 }									\
 static DEVICE_ATTR(in##channel##_input, S_IRUGO,			\
 		   show_in##channel##_input, NULL);
@@ -255,27 +260,40 @@ static void pcf8591_init_client(struct i2c_client *client)
 
 static int pcf8591_read_channel(struct device *dev, int channel)
 {
-	u8 value;
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf8591_data *data = i2c_get_clientdata(client);
+	u8 control;
+	int value;
 
 	mutex_lock(&data->update_lock);
 
-	if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
-		data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
-			      | channel;
-		i2c_smbus_write_byte(client, data->control);
+	if (!data->channel_valid ||
+	    (data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
+		control = (data->control & ~PCF8591_CONTROL_AICH_MASK) | channel;
+		data->channel_valid = false;
+		value = i2c_smbus_write_byte(client, control);
+		if (value < 0)
+			goto out;
 
 		/*
 		 * The first byte transmitted contains the conversion code of
 		 * the previous read cycle. FLUSH IT!
 		 */
-		i2c_smbus_read_byte(client);
+		value = i2c_smbus_read_byte(client);
+		if (value < 0)
+			goto out;
+
+		data->control = control;
+		data->channel_valid = true;
 	}
 	value = i2c_smbus_read_byte(client);
 
+out:
 	mutex_unlock(&data->update_lock);
 
+	if (value < 0)
+		return value;
+
 	if ((channel == 2 && input_mode == 2) ||
 	    (channel != 3 && (input_mode == 1 || input_mode == 3)))
 		return 10 * REG_TO_SIGNED(value);
-- 
2.20.1


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

end of thread, other threads:[~2026-11-17 16:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 11:38 [PATCH] hwmon: (pcf8591) Propagate channel access errors Siqi Chen
2026-11-17 11:48 ` sashiko-bot
2026-11-17 16:07 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox