2024-06-13 12:12:02

by Joy Chakraborty

[permalink] [raw]
Subject: [PATCH v2] rtc: abx80x: Fix return value of nvmem callback on read

Read callbacks registered with nvmem core expect 0 to be returned on
success and a negative value to be returned on failure.

abx80x_nvmem_xfer() on read calls i2c_smbus_read_i2c_block_data() which
returns the number of bytes read on success as per its api description,
this return value is handled as an error and returned to nvmem even on
success.

Fix to handle all possible values that would be returned by
i2c_smbus_read_i2c_block_data().

Fixes: e90ff8ede777 ("rtc: abx80x: Add nvmem support")
Cc: [email protected]
Signed-off-by: Joy Chakraborty <[email protected]>
---
drivers/rtc/rtc-abx80x.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
index fde2b8054c2e..1298962402ff 100644
--- a/drivers/rtc/rtc-abx80x.c
+++ b/drivers/rtc/rtc-abx80x.c
@@ -705,14 +705,18 @@ static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
if (ret)
return ret;

- if (write)
+ if (write) {
ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
len, val);
- else
+ if (ret)
+ return ret;
+ } else {
ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
len, val);
- if (ret)
- return ret;
+ if (ret <= 0)
+ return ret ? ret : -EIO;
+ len = ret;
+ }

offset += len;
val += len;
--
2.45.2.505.gda0bf45e8d-goog



2024-06-13 12:32:12

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH v2] rtc: abx80x: Fix return value of nvmem callback on read

On Thu, Jun 13, 2024 at 12:07:50PM +0000, Joy Chakraborty wrote:
> Read callbacks registered with nvmem core expect 0 to be returned on
> success and a negative value to be returned on failure.
>
> abx80x_nvmem_xfer() on read calls i2c_smbus_read_i2c_block_data() which
> returns the number of bytes read on success as per its api description,
> this return value is handled as an error and returned to nvmem even on
> success.
>
> Fix to handle all possible values that would be returned by
> i2c_smbus_read_i2c_block_data().
>
> Fixes: e90ff8ede777 ("rtc: abx80x: Add nvmem support")
> Cc: [email protected]
> Signed-off-by: Joy Chakraborty <[email protected]>

Thanks!

Reviewed-by: Dan Carpenter <[email protected]>

regards,
dan carpenter


2024-06-13 15:13:13

by Sean Anderson

[permalink] [raw]
Subject: Re: [PATCH v2] rtc: abx80x: Fix return value of nvmem callback on read

On 6/13/24 08:07, Joy Chakraborty wrote:
> Read callbacks registered with nvmem core expect 0 to be returned on
> success and a negative value to be returned on failure.
>
> abx80x_nvmem_xfer() on read calls i2c_smbus_read_i2c_block_data() which
> returns the number of bytes read on success as per its api description,
> this return value is handled as an error and returned to nvmem even on
> success.

Humm, I wish this were documented in nvmem-provider.h...

> Fix to handle all possible values that would be returned by
> i2c_smbus_read_i2c_block_data().
>
> Fixes: e90ff8ede777 ("rtc: abx80x: Add nvmem support")
> Cc: [email protected]
> Signed-off-by: Joy Chakraborty <[email protected]>
> ---
> drivers/rtc/rtc-abx80x.c | 12 ++++++++----
> 1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
> index fde2b8054c2e..1298962402ff 100644
> --- a/drivers/rtc/rtc-abx80x.c
> +++ b/drivers/rtc/rtc-abx80x.c
> @@ -705,14 +705,18 @@ static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
> if (ret)
> return ret;
>
> - if (write)
> + if (write) {
> ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
> len, val);
> - else
> + if (ret)
> + return ret;
> + } else {
> ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
> len, val);
> - if (ret)
> - return ret;
> + if (ret <= 0)
> + return ret ? ret : -EIO;
> + len = ret;
> + }
>
> offset += len;
> val += len;

Reviewed-by: Sean Anderson <[email protected]>