* [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
* Re: [PATCH] hwmon: (pcf8591) Propagate channel access errors
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
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-17 11:48 UTC (permalink / raw)
To: Siqi Chen; +Cc: linux-hwmon
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] hwmon: (pcf8591) negative ADC readings treated as errors in
show_in_channel()
--
commit d99106187d0cf00e871df0cbaf44bbf27b3630ed
Author: Siqi Chen <chensiqi@uniontech.com>
hwmon: (pcf8591) Propagate channel access errors
This commit checks SMBus operations in pcf8591_read_channel() for errors,
invalidates the cached channel until selection and the dummy read succeed,
and propagates negative error codes through the sysfs show callbacks.
> diff --git a/drivers/hwmon/pcf8591.c b/drivers/hwmon/pcf8591.c
> index ec98e6b66c49b..602d0caeaced6 100644
> --- a/drivers/hwmon/pcf8591.c
> +++ b/drivers/hwmon/pcf8591.c
[ ... ]
> @@ -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);
[Severity: High]
Does checking value < 0 here reject valid negative differential ADC
readings as errors?
In differential modes (input_mode 1, 2, or 3), pcf8591_read_channel()
converts two's-complement readings via 10 * REG_TO_SIGNED(value), which
legitimately returns values in [-1280, -10] when bit 7 of the ADC byte is
set. Treating any negative return value as an error causes sysfs reads of
in0_input, in1_input, or in2_input to fail with the millivolt reading as
the errno whenever the differential input voltage is negative. Returning
the channel reading through an int * output parameter while returning 0 or
a negative errno from pcf8591_read_channel() would keep the two separate.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117113819.3336050-1-chensiqi@uniontech.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] hwmon: (pcf8591) Propagate channel access errors
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
1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2026-11-17 16:07 UTC (permalink / raw)
To: Siqi Chen; +Cc: linux-hwmon, linux-kernel, stable
On 11/17/26 03:38, Siqi Chen wrote:
> 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>
This is an old driver, using a long since deprecated hwmon API which
generates a warning message during boot. Such drivers will need to
be converted to a supported API, and the result will need to be tested
at the very least on simulated hardware, before accepting any other
changes.
Thanks,
Guenter
^ permalink raw reply [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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.