[PATCH v4 3/5] iio: light: al3320a: Fix an error handling path in al3320a_probe()

David Heidelberg via B4 Relay posted 5 patches 10 months, 1 week ago
[PATCH v4 3/5] iio: light: al3320a: Fix an error handling path in al3320a_probe()
Posted by David Heidelberg via B4 Relay 10 months, 1 week ago
From: David Heidelberg <david@ixit.cz>

If regmap_write() fails in al3320a_init(), al3320a_set_pwr_off is
not called.

In order to avoid such a situation, move the devm_add_action_or_reset()
which calls al3320a_set_pwr_off right after a successful
al3320a_set_pwr_on.

Signed-off-by: David Heidelberg <david@ixit.cz>
---
 drivers/iio/light/al3320a.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/light/al3320a.c b/drivers/iio/light/al3320a.c
index 1b2b0359ed5dad5e00d2fe584f8f3495c13c997e..1fa693d54ae2a6e5ead3a9c7ac7018afeba9b760 100644
--- a/drivers/iio/light/al3320a.c
+++ b/drivers/iio/light/al3320a.c
@@ -101,6 +101,12 @@ static int al3320a_init(struct al3320a_data *data)
 	if (ret < 0)
 		return ret;
 
+	ret = devm_add_action_or_reset(&data->client->dev,
+				       al3320a_set_pwr_off,
+				       data);
+	if (ret)
+		return dev_err_probe(&data->client->dev, ret, "failed to add action\n");
+
 	ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG_RANGE,
 					FIELD_PREP(AL3320A_GAIN_MASK,
 						   AL3320A_RANGE_3));
@@ -211,10 +217,6 @@ static int al3320a_probe(struct i2c_client *client)
 		return ret;
 	}
 
-	ret = devm_add_action_or_reset(dev, al3320a_set_pwr_off, data);
-	if (ret < 0)
-		return ret;
-
 	return devm_iio_device_register(dev, indio_dev);
 }
 

-- 
2.49.0
Re: [PATCH v4 3/5] iio: light: al3320a: Fix an error handling path in al3320a_probe()
Posted by Jonathan Cameron 10 months, 1 week ago
On Wed, 02 Apr 2025 21:33:26 +0200
David Heidelberg via B4 Relay <devnull+david.ixit.cz@kernel.org> wrote:

> From: David Heidelberg <david@ixit.cz>
> 
> If regmap_write() fails in al3320a_init(), al3320a_set_pwr_off is
> not called.
> 
> In order to avoid such a situation, move the devm_add_action_or_reset()
> which calls al3320a_set_pwr_off right after a successful
> al3320a_set_pwr_on.
> 
> Signed-off-by: David Heidelberg <david@ixit.cz>
> ---
>  drivers/iio/light/al3320a.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/light/al3320a.c b/drivers/iio/light/al3320a.c
> index 1b2b0359ed5dad5e00d2fe584f8f3495c13c997e..1fa693d54ae2a6e5ead3a9c7ac7018afeba9b760 100644
> --- a/drivers/iio/light/al3320a.c
> +++ b/drivers/iio/light/al3320a.c
> @@ -101,6 +101,12 @@ static int al3320a_init(struct al3320a_data *data)
>  	if (ret < 0)
>  		return ret;
>  
> +	ret = devm_add_action_or_reset(&data->client->dev,
> +				       al3320a_set_pwr_off,
> +				       data);
> +	if (ret)
> +		return dev_err_probe(&data->client->dev, ret, "failed to add action\n");
This one I'll drop whilst applying given we are adding a message with little
purpose (or chance of occurring) rather than moving one as we were in patch 2.

I've applied up to this patch with those tweaks. I might well pick up the others but
not looked closely at them yet!
> +
>  	ret = i2c_smbus_write_byte_data(data->client, AL3320A_REG_CONFIG_RANGE,
>  					FIELD_PREP(AL3320A_GAIN_MASK,
>  						   AL3320A_RANGE_3));
> @@ -211,10 +217,6 @@ static int al3320a_probe(struct i2c_client *client)
>  		return ret;
>  	}
>  
> -	ret = devm_add_action_or_reset(dev, al3320a_set_pwr_off, data);
> -	if (ret < 0)
> -		return ret;
> -
>  	return devm_iio_device_register(dev, indio_dev);
>  }
>  
>