mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Tabrez Ahmed <tabreztalks@gmail.com>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
	Shuah Khan <shuah@kernel.org>,
	Brigham Campbell <me@brighamcampbell.com>
Subject: Re: [PATCH v2] hwmon: (ads7871) Fix incorrect error code in voltage_show
Date: Sat, 7 Mar 2026 17:21:58 -0800	[thread overview]
Message-ID: <bd4eaebe-67b3-4730-89d7-b680a1ec4692@roeck-us.net> (raw)
In-Reply-To: <20260307115226.25757-1-tabreztalks@gmail.com>

On Sat, Mar 07, 2026 at 05:22:26PM +0530, Tabrez Ahmed wrote:
> The voltage_show() function returns -1 when the A/D conversion
> fails to complete within the polling loop. -1 maps to -EPERM
> (operation not permitted), which does not describe the actual
> failure.
> 
> Replace this -1 error code with -ETIMEDOUT to better indicate
> the timeout condition to userspace.
> 
> Drop the else block after return.
> 
> Note: not runtime tested due to lack of hardware.
> 
> Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
> ---
> Changes in v2:
> - Dropped unnecessary 'else' block after return as suggested by Guenter Roeck.
> 
> Note: This patch applies on top of my previously submitted patch:
> "hwmon: (ads7871) Replace sprintf() with sysfs_emit()"
> 
>  drivers/hwmon/ads7871.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
> index b84426c940c5e..753bf77ce19b4 100644
> --- a/drivers/hwmon/ads7871.c
> +++ b/drivers/hwmon/ads7871.c
> @@ -125,9 +125,9 @@ static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
>  		/*result in volts*10000 = (val/8192)*2.5*10000*/
>  		val = ((val >> 2) * 25000) / 8192;
>  		return sysfs_emit(buf, "%d\n", val);
> -	} else {
> -		return -1;
>  	}
> +
> +	return -ETIMEDOUT;

AI feedback below. Somewhat unrelated, but it turns out the driver has a number
of problems. I'll apply the two patches, but that won't fix the real problems
with the driver.

  Does this code mask actual SPI errors?
  If ads7871_read_reg8() fails, it returns a negative error code (e.g., -EIO).
  Since MUX_CNV_BM is 0x80, any standard negative error code will have the 7th bit
  set, meaning mux_cnv evaluates to 1:
	ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
	mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
  This causes the polling loop to continue and eventually return -ETIMEDOUT
  here. Should we check if ret < 0 and propagate the actual SPI error code
  instead of unconditionally returning -ETIMEDOUT?
  Also, since this attribute is registered via devm_hwmon_device_register_with_groups()
  rather than the _with_info() variant, the hwmon core does not serialize accesses.
  If userspace reads multiple channels concurrently, could they overwrite each
  other's REG_GAIN_MUX writes before the conversion completes? Should we add
  locking to serialize this function?
  Additionally, since we are improving error reporting, should we also check the
  return values of ads7871_write_reg8() and ads7871_read_reg16() to prevent
  operating on or returning bogus data when SPI transfers fail?

That driver would really benefit from a conversion to the with_info API.

Thanks,
Guenter

  reply	other threads:[~2026-03-08  1:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-07 11:52 Tabrez Ahmed
2026-03-08  1:21 ` Guenter Roeck [this message]
2026-03-08  1:24 ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bd4eaebe-67b3-4730-89d7-b680a1ec4692@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@brighamcampbell.com \
    --cc=shuah@kernel.org \
    --cc=tabreztalks@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®