iio: light: vcnl4000: use lock guard()
PATCH 1/1 iio: light: vcnl4000: use lock guard()
From: Raffael Raiel Trindade <[email protected]>
Use guard() for handling mutex lock instead of manually locking and unlocking. Remove gotos in error handling logic. This prevents forgotten locks on early exits.
Signed-off-by Raffael Raiel Trindade <[email protected]>
Co-developed-by Kim Carvalho <[email protected]>
Signed-off-by Kim Carvalho <[email protected]>
--- drivers/iio/light/vcnl4000.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-)
drivers/iio/light/vcnl4000.c +10 -20
@@ -18,6 +18,7 @@*/ #include <linux/bitfield.h> +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/err.h> #include <linux/i2c.h>
@@ -288,11 +289,11 @@ static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en) {int ret; -mutex_lock (&data->vcnl4000_lock); +guard (mutex)(&data->vcnl4000_lock); ret =i2c_smbus_read_word_data (data->client,VCNL4200_AL_CONF );if (ret <0 ) - goto out; +return ret;if (en) ret &= ~VCNL4040_ALS_CONF_ALS_SHUTDOWN ;
@@ -301,9 +302,6 @@ static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en) ret =i2c_smbus_write_word_data (data->client,VCNL4200_AL_CONF , ret); -out: -mutex_unlock (&data->vcnl4000_lock); -return ret; }
@@ -311,11 +309,11 @@ static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en) {int ret; -mutex_lock (&data->vcnl4000_lock); +guard (mutex)(&data->vcnl4000_lock); ret =i2c_smbus_read_word_data (data->client,VCNL4200_PS_CONF1 );if (ret <0 ) - goto out; +return ret;if (en) ret &= ~VCNL4040_PS_CONF1_PS_SHUTDOWN ;
@@ -324,9 +322,6 @@ static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en) ret =i2c_smbus_write_word_data (data->client,VCNL4200_PS_CONF1 , ret); -out: -mutex_unlock (&data->vcnl4000_lock); -return ret; }
@@ -680,19 +675,17 @@ static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val) data->vcnl4200_ps.sampling_rate =ktime_set (0 , val *60 *NSEC_PER_USEC ); -mutex_lock (&data->vcnl4000_lock); +guard (mutex)(&data->vcnl4000_lock); ret =i2c_smbus_read_word_data (data->client,VCNL4200_PS_CONF1 );if (ret <0 ) - goto out; +return ret; regval = (ret & ~VCNL4040_PS_CONF2_PS_IT ) |FIELD_PREP (VCNL4040_PS_CONF2_PS_IT , index); ret =i2c_smbus_write_word_data (data->client,VCNL4200_PS_CONF1 , regval); -out: -mutex_unlock (&data->vcnl4000_lock);return ret; }
@@ -1500,13 +1493,13 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,u16 val, mask;struct vcnl4000_data *data =iio_priv (indio_dev); -mutex_lock (&data->vcnl4000_lock); +guard (mutex)(&data->vcnl4000_lock);switch (chan->type) {case IIO_LIGHT : ret =i2c_smbus_read_word_data (data->client,VCNL4200_AL_CONF );if (ret <0 ) - goto out; +return ret; mask =VCNL4040_ALS_CONF_INT_EN ;if (state)
@@ -1521,7 +1514,7 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,case IIO_PROXIMITY : ret =i2c_smbus_read_word_data (data->client,VCNL4200_PS_CONF1 );if (ret <0 ) - goto out; +return ret;if (dir ==IIO_EV_DIR_RISING ) mask =VCNL4040_PS_IF_AWAY ;
@@ -1538,9 +1531,6 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,break ; } -out: -mutex_unlock (&data->vcnl4000_lock); -return ret; }
Signature · 1 line
2.51.0
Quoted 3 lines · Raffael Raiel Trindade
Use
guard()for handling mutex lock instead of manually locking and unlocking. Remove gotos in error handling logic. This prevents forgotten locks on early exits.
...
drivers/iio/light/vcnl4000.c static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en) · line 305
Quoted 6 lines
drivers/iio/light/vcnl4000.c +0
-3ret =i2c_smbus_write_word_data (data->client,VCNL4200_AL_CONF , ret); -out: -mutex_unlock (&data->vcnl4000_lock); -return ret;
It's now
return i2c_smbus_...;
Ditto for other similar cases below.
Signature · 2 lines
With Best Regards, Andy Shevchenko