iio: light: gp2ap020a00f: General cleanup and
v4 cover iio: light: gp2ap020a00f: General cleanup and
This series performs a general cleanup of the gp2ap020a00f IIO driver.
It integrates my original work switching to the guard() cleanup handler, fixing a signedness warning from Smatch, and cleanups provided by Andy Shevchenko [1].
v4:
- Integrated Andy Shevchenko's 7-patch cleanup series.
- Patch 1: Removed redundant 'err' variable and whitespace issues as suggested by Andy.
- Patch 2: Added Reviewed-by tag from Andy Shevchenko.
- Patch 5: Updated commit message to describe the pre-existing bug fix in gp2ap020a00f_buffer_predisable() where errors could be swallowed.
v3:
- Addressed initial feedback and simplified locking logic.
[1] https://lore.kernel.org/all/[email protected]/
Andy Shevchenko (7):
iio: light: gp2ap020a00f: Use correct types for 16-bit LE data
iio: light: gp2ap020a00f: Replace custom implementation of min()
iio: light: gp2ap020a00f: Return directly from the switch cases
iio: light: gp2ap020a00f: Use temporary variable for struct device
iio: light: gp2ap020a00f: Explicitly use string literal for driver
name
iio: light: gp2ap020a00f: Remove trailing comma in termination entry
iio: light: gp2ap020a00f: Join some lines of code to be a single line
Ethan Tidmore (2):
iio: light: gp2ap020a00f: simplify locking with guard()
iio: light: gp2ap020a00f: correct return type to int
drivers/iio/light/gp2ap020a00f.c | 287 ++++++++++++------------------- 1 file changed, 112 insertions(+), 175 deletions(-)
Signature · 1 line
2.53.0
PATCH v4 1/9 iio: light: gp2ap020a00f: simplify locking with guard()
Use the guard() cleanup handler to manage the device lock. This simplifies the code by removing the need for manual unlocking and goto error handling paths.
---
v4:
- Remove whitespace between assignment and check. v3:
- Remove Fixes: tag.
- Added Smatch warning.
v2:
- Fixed gp2ap020a00f_get_thresh_reg() parameter alignment.
- Removed unneeded whitespace between assignment and check.
drivers/iio/light/gp2ap020a00f.c | 66 ++++++++++---------------------- 1 file changed, 20 insertions(+), 46 deletions(-)
drivers/iio/light/gp2ap020a00f.c +20 -46
@@ -31,6 +31,7 @@* the other one. */ +#include <linux/cleanup.h> #include <linux/debugfs.h> #include <linux/delay.h> #include <linux/i2c.h>
@@ -1024,17 +1025,13 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev,bool event_en = false;u8 thresh_val_id;u8 thresh_reg_l; -int err =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); thresh_reg_l =gp2ap020a00f_get_thresh_reg (chan, dir); thresh_val_id =GP2AP020A00F_THRESH_VAL_ID (thresh_reg_l); - -if (thresh_val_id >GP2AP020A00F_THRESH_PH ) { - err = -EINVAL ; - goto error_unlock; - } +if (thresh_val_id >GP2AP020A00F_THRESH_PH ) +return -EINVAL ;switch (thresh_reg_l) {case GP2AP020A00F_TH_L_REG :
@@ -1046,30 +1043,23 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev, &data->flags);break ;case GP2AP020A00F_PH_L_REG : -if (val ==0 ) { - err = -EINVAL ; - goto error_unlock; - } +if (val ==0 ) +return -EINVAL ; + event_en =test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags);break ;case GP2AP020A00F_PL_L_REG : -if (val ==0 ) { - err = -EINVAL ; - goto error_unlock; - } +if (val ==0 ) +return -EINVAL ; + event_en =test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags);break ; } data->thresh_val[thresh_val_id] = val; - err =gp2ap020a00f_write_event_threshold (data, thresh_val_id, - event_en); -error_unlock: -mutex_unlock (&data->lock); - -return err; +return gp2ap020a00f_write_event_threshold (data, thresh_val_id, event_en); } static int gp2ap020a00f_read_event_val(struct iio_dev *indio_dev,
@@ -1081,23 +1071,16 @@ static int gp2ap020a00f_read_event_val(struct iio_dev *indio_dev, {struct gp2ap020a00f_data *data =iio_priv (indio_dev);u8 thresh_reg_l; -int err =IIO_VAL_INT ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock); thresh_reg_l =gp2ap020a00f_get_thresh_reg (chan, dir); - -if (thresh_reg_l >GP2AP020A00F_PH_L_REG ) { - err = -EINVAL ; - goto error_unlock; - } +if (thresh_reg_l >GP2AP020A00F_PH_L_REG ) +return -EINVAL ; *val = data->thresh_val[GP2AP020A00F_THRESH_VAL_ID (thresh_reg_l)]; -error_unlock: -mutex_unlock (&data->lock); - -return err; +return IIO_VAL_INT ; } static int gp2ap020a00f_write_prox_event_config(struct iio_dev *indio_dev,
@@ -1165,7 +1148,7 @@ static int gp2ap020a00f_write_event_config(struct iio_dev *indio_dev,enum gp2ap020a00f_cmd cmd;int err; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);switch (chan->type) {case IIO_PROXIMITY :
@@ -1186,8 +1169,6 @@ static int gp2ap020a00f_write_event_config(struct iio_dev *indio_dev, err = -EINVAL ; } -mutex_unlock (&data->lock); -return err; }
@@ -1199,7 +1180,7 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev,struct gp2ap020a00f_data *data =iio_priv (indio_dev);int event_en =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);switch (chan->type) {case IIO_PROXIMITY :
@@ -1223,8 +1204,6 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev,break ; } -mutex_unlock (&data->lock); -return event_en; }
@@ -1385,7 +1364,7 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev)struct gp2ap020a00f_data *data =iio_priv (indio_dev);int i, err =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);/* * Enable triggers according to the scan_mask. Enabling either
@@ -1413,15 +1392,12 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) }if (err <0 ) - goto error_unlock; +return err; data->buffer =kmalloc (indio_dev->scan_bytes,GFP_KERNEL );if (!data->buffer) err = -ENOMEM ; -error_unlock: -mutex_unlock (&data->lock); -return err; }
@@ -1430,7 +1406,7 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev)struct gp2ap020a00f_data *data =iio_priv (indio_dev);int i, err =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);iio_for_each_active_channel (indio_dev, i) {switch (i) {
@@ -1452,8 +1428,6 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev)if (err ==0 )kfree (data->buffer); -mutex_unlock (&data->lock); -return err; }
Signature · 1 line
2.53.0
PATCH v4 2/9 iio: light: gp2ap020a00f: correct return type to int
In reply to Ethan TidmoreThe function gp2ap020a00f_get_thresh_reg() can return -EINVAL in its error path. Yet, the function has return type of u8. Added error checking for gp2ap020a00f_get_thresh_reg() return value.
Detected by Smatch:drivers/iio/light/gp2ap020a00f.c:1013 gp2ap020a00f_get_thresh_reg() warn:
signedness bug returning '(-22)'
---
v4:
- Added Reviewed-by: tag from Andy Shevchenko.
v3:
- Remove Fixes: tag.
- Added Smatch warning.
v2:
- Fixed gp2ap020a00f_get_thresh_reg() parameter alignment.
- Removed unneeded whitespace between assignment and check.
drivers/iio/light/gp2ap020a00f.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/iio/light/gp2ap020a00f.c +9 -4
@@ -993,8 +993,8 @@ static irqreturn_t gp2ap020a00f_trigger_handler(int irq, void *data)return IRQ_HANDLED ; } -static u8 gp2ap020a00f_get_thresh_reg (const struct iio_chan_spec *chan, -enum iio_event_direction event_dir) +static int gp2ap020a00f_get_thresh_reg (const struct iio_chan_spec *chan, +enum iio_event_direction event_dir) {switch (chan->type) {case IIO_PROXIMITY :
@@ -1024,11 +1024,14 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev,struct gp2ap020a00f_data *data =iio_priv (indio_dev);bool event_en = false;u8 thresh_val_id; -u8 thresh_reg_l; +int thresh_reg_l;guard (mutex)(&data->lock); thresh_reg_l =gp2ap020a00f_get_thresh_reg (chan, dir); +if (thresh_reg_l <0 ) +return thresh_reg_l; + thresh_val_id =GP2AP020A00F_THRESH_VAL_ID (thresh_reg_l);if (thresh_val_id >GP2AP020A00F_THRESH_PH )return -EINVAL ;
@@ -1070,11 +1073,13 @@ static int gp2ap020a00f_read_event_val(struct iio_dev *indio_dev, int *val, int *val2) { struct gp2ap020a00f_data *data = iio_priv(indio_dev); -u8 thresh_reg_l; +int thresh_reg_l;guard (mutex)(&data->lock); thresh_reg_l =gp2ap020a00f_get_thresh_reg (chan, dir); +if (thresh_reg_l <0 ) +return thresh_reg_l;if (thresh_reg_l >GP2AP020A00F_PH_L_REG )return -EINVAL ;
Signature · 1 line
2.53.0
PATCH v4 3/9 iio: light: gp2ap020a00f: Use correct types for 16-bit LE data
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
Instead of using byte arrays and then explicit castings, change the types of byte arrays to be __le16 and update the endianness conversions accordingly.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/iio/light/gp2ap020a00f.c +11 -12
@@ -462,7 +462,7 @@ static int gp2ap020a00f_write_event_threshold(struct gp2ap020a00f_data *data,return regmap_bulk_write (data->regmap,GP2AP020A00F_THRESH_REG (th_val_id), - (u8 *)&thresh_buf,2 ); + &thresh_buf,sizeof (thresh_buf)); } static int gp2ap020a00f_alter_opmode(struct gp2ap020a00f_data *data,
@@ -698,18 +698,18 @@ static int wait_conversion_complete_irq(struct gp2ap020a00f_data *data)static int gp2ap020a00f_read_output (struct gp2ap020a00f_data *data,unsigned int output_reg,int *val) { -u8 reg_buf[2 ]; +__le16 reg_buf;int err; err =wait_conversion_complete_irq (data);if (err <0 )dev_dbg (&data->client->dev,"data ready timeout\n" ); - err =regmap_bulk_read (data->regmap, output_reg, reg_buf,2 ); + err =regmap_bulk_read (data->regmap, output_reg, ®_buf,sizeof (reg_buf));if (err <0 )return err; - *val =le16_to_cpup ((__le16 *)reg_buf); + *val =le16_to_cpu (reg_buf);return err; }
@@ -867,8 +867,9 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data) {struct iio_dev *indio_dev = data;struct gp2ap020a00f_data *priv =iio_priv (indio_dev); -u8 op_reg_flags, d0_reg_buf[2 ];unsigned int output_val, op_reg_val; +__le16 d0_reg_buf; +u8 op_reg_flags;int thresh_val_id, ret;/* Read interrupt flags */
@@ -896,11 +897,11 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data)* transition is required. */ ret =regmap_bulk_read (priv->regmap,GP2AP020A00F_D0_L_REG , - d0_reg_buf,2 ); + &d0_reg_buf,sizeof (d0_reg_buf));if (ret <0 ) goto done; - output_val =le16_to_cpup ((__le16 *)d0_reg_buf); + output_val =le16_to_cpu (d0_reg_buf);if (gp2ap020a00f_adjust_lux_mode (priv, output_val)) goto done;
@@ -967,17 +968,15 @@ static irqreturn_t gp2ap020a00f_trigger_handler(int irq, void *data)int i, out_val, ret;iio_for_each_active_channel (indio_dev, i) { - ret =regmap_bulk_read (priv->regmap, -GP2AP020A00F_DATA_REG (i), - &priv->buffer[d_size],2 ); + ret =regmap_bulk_read (priv->regmap,GP2AP020A00F_DATA_REG (i), + &priv->buffer[d_size],2 );if (ret <0 ) goto done;if (i ==GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR || i ==GP2AP020A00F_SCAN_MODE_LIGHT_IR ) { - out_val =le16_to_cpup ((__le16 *)&priv->buffer[d_size]); + out_val =get_unaligned_le16 (&priv->buffer[d_size]);gp2ap020a00f_output_to_lux (priv, &out_val); -put_unaligned_le32 (out_val, &priv->buffer[d_size]); d_size +=4 ; }else {
Signature · 1 line
2.53.0
PATCH v4 4/9 iio: light: gp2ap020a00f: Replace custom implementation of min()
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
Replace custom implementation of min() to save a few lines of code.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iio/light/gp2ap020a00f.c +3 -3
@@ -38,6 +38,7 @@#include <linux/interrupt.h> #include <linux/irq.h> #include <linux/irq_work.h> +#include <linux/minmax.h> #include <linux/module.h> #include <linux/mod_devicetable.h> #include <linux/mutex.h>
@@ -45,6 +46,7 @@#include <linux/regulator/consumer.h> #include <linux/slab.h> #include <linux/unaligned.h> +#include <linux/iio/buffer.h> #include <linux/iio/events.h> #include <linux/iio/iio.h>
@@ -454,9 +456,7 @@ static int gp2ap020a00f_write_event_threshold(struct gp2ap020a00f_data *data,*/ thresh_reg_val = data->thresh_val[th_val_id] /16 ; else - thresh_reg_val = data->thresh_val[th_val_id] >16000 ? -16000 : - data->thresh_val[th_val_id]; + thresh_reg_val =min (data->thresh_val[th_val_id],16000U ); thresh_buf =cpu_to_le16 (thresh_reg_val);
Signature · 1 line
2.53.0
PATCH v4 5/9 iio: light: gp2ap020a00f: Return directly from the switch cases
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
Return directly from the switch cases which makes code easier to follow. In some cases convert pieces to the standard pattern which also unifies it with the accepted kernel practices.
By doing this, it appears to have fixed a preexisting bug, the variable err in gp2ap020a00f_buffer_predisable() is only checked once for errors after the for loop is finished. This could allow errors to be swallowed.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 96 ++++++++++++-------------------- 1 file changed, 37 insertions(+), 59 deletions(-)
drivers/iio/light/gp2ap020a00f.c +37 -59
@@ -494,27 +494,24 @@ static int gp2ap020a00f_alter_opmode(struct gp2ap020a00f_data *data,static int gp2ap020a00f_exec_cmd (struct gp2ap020a00f_data *data,enum gp2ap020a00f_cmd cmd) { -int err =0 ; +int err;switch (cmd) {case GP2AP020A00F_CMD_READ_RAW_CLEAR :if (data->cur_opmode !=GP2AP020A00F_OPMODE_SHUTDOWN )return -EBUSY ; - err =gp2ap020a00f_set_operation_mode (data, +return gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_READ_RAW_CLEAR ); -break ;case GP2AP020A00F_CMD_READ_RAW_IR :if (data->cur_opmode !=GP2AP020A00F_OPMODE_SHUTDOWN )return -EBUSY ; - err =gp2ap020a00f_set_operation_mode (data, +return gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_READ_RAW_IR ); -break ;case GP2AP020A00F_CMD_READ_RAW_PROXIMITY :if (data->cur_opmode !=GP2AP020A00F_OPMODE_SHUTDOWN )return -EBUSY ; - err =gp2ap020a00f_set_operation_mode (data, +return gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY ); -break ;case GP2AP020A00F_CMD_TRIGGER_CLEAR_EN :if (data->cur_opmode ==GP2AP020A00F_OPMODE_PROX_DETECT )return -EBUSY ;
@@ -522,16 +519,17 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data, err =gp2ap020a00f_alter_opmode (data,GP2AP020A00F_OPMODE_ALS ,GP2AP020A00F_ADD_MODE ); + else + err =0 ;set_bit (GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER , &data->flags); -break ; +return err;case GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS :clear_bit (GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER , &data->flags);if (gp2ap020a00f_als_enabled (data))break ; - err =gp2ap020a00f_alter_opmode (data, +return gp2ap020a00f_alter_opmode (data,GP2AP020A00F_OPMODE_ALS ,GP2AP020A00F_SUBTRACT_MODE ); -break ;case GP2AP020A00F_CMD_TRIGGER_IR_EN :if (data->cur_opmode ==GP2AP020A00F_OPMODE_PROX_DETECT )return -EBUSY ;
@@ -539,16 +537,17 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data, err =gp2ap020a00f_alter_opmode (data,GP2AP020A00F_OPMODE_ALS ,GP2AP020A00F_ADD_MODE ); + else + err =0 ;set_bit (GP2AP020A00F_FLAG_ALS_IR_TRIGGER , &data->flags); -break ; +return err;case GP2AP020A00F_CMD_TRIGGER_IR_DIS :clear_bit (GP2AP020A00F_FLAG_ALS_IR_TRIGGER , &data->flags);if (gp2ap020a00f_als_enabled (data))break ; - err =gp2ap020a00f_alter_opmode (data, +return gp2ap020a00f_alter_opmode (data,GP2AP020A00F_OPMODE_ALS ,GP2AP020A00F_SUBTRACT_MODE ); -break ;case GP2AP020A00F_CMD_TRIGGER_PROX_EN :if (data->cur_opmode ==GP2AP020A00F_OPMODE_PROX_DETECT )return -EBUSY ;
@@ -556,13 +555,12 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,GP2AP020A00F_OPMODE_PS , GP2AP020A00F_ADD_MODE);set_bit (GP2AP020A00F_FLAG_PROX_TRIGGER , &data->flags); -break ; +return err;case GP2AP020A00F_CMD_TRIGGER_PROX_DIS :clear_bit (GP2AP020A00F_FLAG_PROX_TRIGGER , &data->flags); - err =gp2ap020a00f_alter_opmode (data, +return gp2ap020a00f_alter_opmode (data,GP2AP020A00F_OPMODE_PS ,GP2AP020A00F_SUBTRACT_MODE ); -break ;case GP2AP020A00F_CMD_ALS_HIGH_EV_EN :if (test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , &data->flags))return 0 ;
@@ -576,9 +574,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,return err; }set_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , &data->flags); - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_TH , true); -break ;case GP2AP020A00F_CMD_ALS_HIGH_EV_DIS :if (!test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , &data->flags))return 0 ;
@@ -590,9 +587,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,if (err <0 )return err; } - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_TH , false); -break ;case GP2AP020A00F_CMD_ALS_LOW_EV_EN :if (test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , &data->flags))return 0 ;
@@ -606,9 +602,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,return err; }set_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , &data->flags); - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_TL , true); -break ;case GP2AP020A00F_CMD_ALS_LOW_EV_DIS :if (!test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , &data->flags))return 0 ;
@@ -620,9 +615,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,if (err <0 )return err; } - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_TL , false); -break ;case GP2AP020A00F_CMD_PROX_HIGH_EV_EN :if (test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags))return 0 ;
@@ -636,9 +630,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,return err; }set_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags); - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_PH , true); -break ;case GP2AP020A00F_CMD_PROX_HIGH_EV_DIS :if (!test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags))return 0 ;
@@ -647,9 +640,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data, GP2AP020A00F_OPMODE_SHUTDOWN);if (err <0 )return err; - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_PH , false); -break ;case GP2AP020A00F_CMD_PROX_LOW_EV_EN :if (test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags))return 0 ;
@@ -663,9 +655,8 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data,return err; }set_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags); - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_PL , true); -break ;case GP2AP020A00F_CMD_PROX_LOW_EV_DIS :if (!test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags))return 0 ;
@@ -674,12 +665,11 @@ static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data *data, GP2AP020A00F_OPMODE_SHUTDOWN);if (err <0 )return err; - err =gp2ap020a00f_write_event_threshold (data, +return gp2ap020a00f_write_event_threshold (data,GP2AP020A00F_THRESH_PL , false); -break ; } -return err; +return 0 ; }static int wait_conversion_complete_irq (struct gp2ap020a00f_data *data)
@@ -1007,10 +997,8 @@ static int gp2ap020a00f_get_thresh_reg(const struct iio_chan_spec *chan,else return GP2AP020A00F_TL_L_REG ;default : -break ; +return -EINVAL ; } - -return -EINVAL ; } static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev,
@@ -1182,33 +1170,23 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev, enum iio_event_direction dir) {struct gp2ap020a00f_data *data =iio_priv (indio_dev); -int event_en =0 ;guard (mutex)(&data->lock);switch (chan->type) {case IIO_PROXIMITY :if (dir ==IIO_EV_DIR_RISING ) - event_en =test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , - &data->flags); +return test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags);else - event_en =test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , - &data->flags); -break ; +return test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags);case IIO_LIGHT :if (dir ==IIO_EV_DIR_RISING ) - event_en =test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , - &data->flags); +return test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , &data->flags);else - event_en =test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , - &data->flags); -break ; +return test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , &data->flags);default : - event_en = -EINVAL ; -break ; +return -EINVAL ; } - -return event_en; } static int gp2ap020a00f_read_channel(struct gp2ap020a00f_data *data,
@@ -1366,7 +1344,7 @@ static const struct iio_info gp2ap020a00f_info = {static int gp2ap020a00f_buffer_postenable (struct iio_dev *indio_dev) {struct gp2ap020a00f_data *data =iio_priv (indio_dev); -int i, err =0 ; +int i, err;guard (mutex)(&data->lock);
@@ -1400,15 +1378,15 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) data->buffer =kmalloc (indio_dev->scan_bytes,GFP_KERNEL );if (!data->buffer) - err = -ENOMEM ; +return -ENOMEM ; -return err; +return 0 ; }static int gp2ap020a00f_buffer_predisable (struct iio_dev *indio_dev) {struct gp2ap020a00f_data *data =iio_priv (indio_dev); -int i, err =0 ; +int i, err;guard (mutex)(&data->lock);
@@ -1427,12 +1405,12 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev) GP2AP020A00F_CMD_TRIGGER_PROX_DIS);break ; } +if (err) +return err; } -if (err ==0 ) -kfree (data->buffer); - -return err; +kfree (data->buffer); +return 0 ; }static const struct iio_buffer_setup_ops gp2ap020a00f_buffer_setup_ops = {
Signature · 1 line
2.53.0
PATCH v4 6/9 iio: light: gp2ap020a00f: Use temporary variable for struct device
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
Use temporary variable for struct device to make code neater.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 34 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-)
drivers/iio/light/gp2ap020a00f.c +16 -18
@@ -1192,6 +1192,7 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev,static int gp2ap020a00f_read_channel (struct gp2ap020a00f_data *data,struct iio_chan_spec const *chan,int *val) { +struct device *dev = &data->client->dev;enum gp2ap020a00f_cmd cmd;int err;
@@ -1211,27 +1212,23 @@ static int gp2ap020a00f_read_channel(struct gp2ap020a00f_data *data, err =gp2ap020a00f_exec_cmd (data, cmd);if (err <0 ) { -dev_err (&data->client->dev, -"gp2ap020a00f_exec_cmd failed\n" ); - goto error_ret; +dev_err (dev,"gp2ap020a00f_exec_cmd failed\n" ); +return err; } err =gp2ap020a00f_read_output (data, chan->address, val);if (err <0 ) -dev_err (&data->client->dev, -"gp2ap020a00f_read_output failed\n" ); +dev_err (dev,"gp2ap020a00f_read_output failed\n" ); err =gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_SHUTDOWN );if (err <0 ) -dev_err (&data->client->dev, -"Failed to shut down the device.\n" ); +dev_err (dev,"Failed to shut down the device.\n" );if (cmd ==GP2AP020A00F_CMD_READ_RAW_CLEAR || cmd ==GP2AP020A00F_CMD_READ_RAW_IR )gp2ap020a00f_output_to_lux (data, val); -error_ret:return err; }
@@ -1421,18 +1418,19 @@ static const struct iio_buffer_setup_ops gp2ap020a00f_buffer_setup_ops = {static int gp2ap020a00f_probe (struct i2c_client *client) {const struct i2c_device_id *id =i2c_client_get_device_id (client); +struct device *dev = &client->dev;struct gp2ap020a00f_data *data;struct iio_dev *indio_dev;struct regmap *regmap;int err; - indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); + indio_dev =devm_iio_device_alloc (dev,sizeof (*data));if (!indio_dev)return -ENOMEM ; data =iio_priv (indio_dev); - data->vled_reg =devm_regulator_get (&client->dev,"vled" ); + data->vled_reg =devm_regulator_get (dev,"vled" );if (IS_ERR (data->vled_reg))return PTR_ERR (data->vled_reg);
@@ -1442,7 +1440,7 @@ static int gp2ap020a00f_probe(struct i2c_client *client) regmap =devm_regmap_init_i2c (client, &gp2ap020a00f_regmap_config);if (IS_ERR (regmap)) { -dev_err (&client->dev,"Regmap initialization failed.\n" ); +dev_err (dev,"Regmap initialization failed.\n" ); err =PTR_ERR (regmap); goto error_regulator_disable; }
@@ -1453,7 +1451,7 @@ static int gp2ap020a00f_probe(struct i2c_client *client) ARRAY_SIZE(gp2ap020a00f_reg_init_tab));if (err <0 ) { -dev_err (&client->dev,"Device initialization failed.\n" ); +dev_err (dev,"Device initialization failed.\n" ); goto error_regulator_disable; }
@@ -1478,11 +1476,10 @@ static int gp2ap020a00f_probe(struct i2c_client *client) goto error_regulator_disable;/* Allocate trigger */ - data->trig =devm_iio_trigger_alloc (&client->dev,"%s-trigger" , - indio_dev->name); + data->trig =devm_iio_trigger_alloc (dev,"%s-trigger" , indio_dev->name);if (data->trig ==NULL ) { err = -ENOMEM ; -dev_err (&indio_dev->dev,"Failed to allocate iio trigger.\n" ); +dev_err (dev,"Failed to allocate iio trigger.\n" ); goto error_uninit_buffer; }
@@ -1494,7 +1491,7 @@ static int gp2ap020a00f_probe(struct i2c_client *client)"gp2ap020a00f_als_event" , indio_dev);if (err <0 ) { -dev_err (&client->dev,"Irq request failed.\n" ); +dev_err (dev,"Irq request failed.\n" ); goto error_uninit_buffer; }
@@ -1502,7 +1499,7 @@ static int gp2ap020a00f_probe(struct i2c_client *client) err =iio_trigger_register (data->trig);if (err <0 ) { -dev_err (&client->dev,"Failed to register iio trigger.\n" ); +dev_err (dev,"Failed to register iio trigger.\n" ); goto error_free_irq; }
@@ -1528,12 +1525,13 @@ static void gp2ap020a00f_remove(struct i2c_client *client) {struct iio_dev *indio_dev =i2c_get_clientdata (client);struct gp2ap020a00f_data *data =iio_priv (indio_dev); +struct device *dev = &client->dev;int err; err =gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_SHUTDOWN );if (err <0 ) -dev_err (&indio_dev->dev,"Failed to power off the device.\n" ); +dev_err (dev,"Failed to power off the device.\n" );iio_device_unregister (indio_dev);iio_trigger_unregister (data->trig);
Signature · 1 line
2.53.0
PATCH v4 7/9 iio: light: gp2ap020a00f: Explicitly use string literal for driver name
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
The driver name should be easily greppable and clearly spelled. Replace a level of indirection and explicitly use string literal.
While at it, remove useless blank lines before module_*() and MODULE_*() macros.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/iio/light/gp2ap020a00f.c +2 -6
@@ -55,8 +55,6 @@#include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h> -#define GP2A_I2C_NAME "gp2ap020a00f" -/* Registers */ #define GP2AP020A00F_OP_REG 0x00/* Basic operations */ #define GP2AP020A00F_ALS_REG 0x01/* ALS related settings */
@@ -1541,10 +1539,9 @@ static void gp2ap020a00f_remove(struct i2c_client *client) }static const struct i2c_device_id gp2ap020a00f_id[] = { - {GP2A_I2C_NAME }, + {"gp2ap020a00f" }, { } }; -MODULE_DEVICE_TABLE (i2c, gp2ap020a00f_id);static const struct of_device_id gp2ap020a00f_of_match[] = {
@@ -1555,14 +1552,13 @@ MODULE_DEVICE_TABLE(of, gp2ap020a00f_of_match);static struct i2c_driver gp2ap020a00f_driver = { .driver = { - .name =GP2A_I2C_NAME , + .name ="gp2ap020a00f" , .of_match_table = gp2ap020a00f_of_match, }, .probe = gp2ap020a00f_probe, .remove = gp2ap020a00f_remove, .id_table = gp2ap020a00f_id, }; -module_i2c_driver (gp2ap020a00f_driver);MODULE_AUTHOR ("Jacek Anaszewski <[email protected]>" );
Signature · 1 line
2.53.0
PATCH v4 8/9 iio: light: gp2ap020a00f: Remove trailing comma in termination entry
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
Termination entry by definition should be the last one, hence remove stray comma after it.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/gp2ap020a00f.c +1 -1
@@ -195,7 +195,7 @@ enum gp2ap020a00f_opmode {GP2AP020A00F_OPMODE_ALS_AND_PS ,GP2AP020A00F_OPMODE_PROX_DETECT ,GP2AP020A00F_OPMODE_SHUTDOWN , - GP2AP020A00F_NUM_OPMODES, +GP2AP020A00F_NUM_OPMODES }; enum gp2ap020a00f_cmd {
Signature · 1 line
2.53.0
PATCH v4 9/9 iio: light: gp2ap020a00f: Join some lines of code to be a single line
In reply to Ethan TidmoreFrom: Andy Shevchenko <[email protected]>
In some cases the wrapped lines are harder to follow. Join them despite being longer than 80 characters in some cases.
---
v4:
- Integrate Andy Shevchenko's cleanups.
drivers/iio/light/gp2ap020a00f.c | 39 +++++++++++--------------------- 1 file changed, 13 insertions(+), 26 deletions(-)
drivers/iio/light/gp2ap020a00f.c +13 -26
@@ -175,10 +175,8 @@#define GP2AP020A00F_CHAN_TIMESTAMP 3#define GP2AP020A00F_DATA_READY_TIMEOUT msecs_to_jiffies(1000) -#define GP2AP020A00F_DATA_REG (chan) (GP2AP020A00F_D0_L_REG + \ - (chan) * 2) -#define GP2AP020A00F_THRESH_REG (th_val_id) (GP2AP020A00F_TL_L_REG + \ - (th_val_id) * 2) +#define GP2AP020A00F_DATA_REG (chan) (GP2AP020A00F_D0_L_REG + (chan) * 2) +#define GP2AP020A00F_THRESH_REG (th_val_id) (GP2AP020A00F_TL_L_REG + (th_val_id) * 2)#define GP2AP020A00F_THRESH_VAL_ID (reg_addr) ((reg_addr - 4) / 2)#define GP2AP020A00F_SUBTRACT_MODE 0
@@ -390,20 +388,17 @@ static int gp2ap020a00f_set_operation_mode(struct gp2ap020a00f_data *data, } err =regmap_update_bits (data->regmap,GP2AP020A00F_ALS_REG , -GP2AP020A00F_PRST_MASK , opmode_regs_settings[op] - .als_reg); +GP2AP020A00F_PRST_MASK , opmode_regs_settings[op].als_reg);if (err <0 )return err; err =regmap_update_bits (data->regmap,GP2AP020A00F_PS_REG , -GP2AP020A00F_INTTYPE_MASK , opmode_regs_settings[op] - .ps_reg); +GP2AP020A00F_INTTYPE_MASK , opmode_regs_settings[op].ps_reg);if (err <0 )return err; err =regmap_update_bits (data->regmap,GP2AP020A00F_LED_REG , -GP2AP020A00F_PIN_MASK , opmode_regs_settings[op] - .led_reg); +GP2AP020A00F_PIN_MASK , opmode_regs_settings[op].led_reg);if (err <0 )return err; }
@@ -861,8 +856,7 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data)int thresh_val_id, ret;/* Read interrupt flags */ - ret =regmap_read (priv->regmap,GP2AP020A00F_OP_REG , - &op_reg_val); + ret =regmap_read (priv->regmap,GP2AP020A00F_OP_REG , &op_reg_val);if (ret <0 ) goto done;
@@ -874,8 +868,7 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data)/* Clear interrupt flags (if not in INTTYPE_PULSE mode) */ if (priv->cur_opmode !=GP2AP020A00F_OPMODE_PROX_DETECT ) { - ret =regmap_write (priv->regmap,GP2AP020A00F_OP_REG , - op_reg_val); + ret =regmap_write (priv->regmap,GP2AP020A00F_OP_REG , op_reg_val);if (ret <0 ) goto done; }
@@ -972,8 +965,7 @@ static irqreturn_t gp2ap020a00f_trigger_handler(int irq, void *data) } } -iio_push_to_buffers_with_timestamp (indio_dev, priv->buffer, - pf->timestamp); +iio_push_to_buffers_with_timestamp (indio_dev, priv->buffer, pf->timestamp); done:iio_trigger_notify_done (indio_dev->trig);
@@ -1023,26 +1015,22 @@ static int gp2ap020a00f_write_event_val(struct iio_dev *indio_dev,switch (thresh_reg_l) {case GP2AP020A00F_TH_L_REG : - event_en =test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , - &data->flags); + event_en =test_bit (GP2AP020A00F_FLAG_ALS_RISING_EV , &data->flags);break ;case GP2AP020A00F_TL_L_REG : - event_en =test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , - &data->flags); + event_en =test_bit (GP2AP020A00F_FLAG_ALS_FALLING_EV , &data->flags);break ;case GP2AP020A00F_PH_L_REG :if (val ==0 )return -EINVAL ; - event_en =test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , - &data->flags); + event_en =test_bit (GP2AP020A00F_FLAG_PROX_RISING_EV , &data->flags);break ;case GP2AP020A00F_PL_L_REG :if (val ==0 )return -EINVAL ; - event_en =test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , - &data->flags); + event_en =test_bit (GP2AP020A00F_FLAG_PROX_FALLING_EV , &data->flags);break ; }
@@ -1526,8 +1514,7 @@ static void gp2ap020a00f_remove(struct i2c_client *client)struct device *dev = &client->dev;int err; - err =gp2ap020a00f_set_operation_mode (data, -GP2AP020A00F_OPMODE_SHUTDOWN ); + err =gp2ap020a00f_set_operation_mode (data,GP2AP020A00F_OPMODE_SHUTDOWN );if (err <0 )dev_err (dev,"Failed to power off the device.\n" );
Signature · 1 line
2.53.0
Re: [PATCH v4 1/9] iio: light: gp2ap020a00f: simplify locking with guard()
In reply to Ethan TidmoreQuoted 12 lines · Ethan Tidmore
Use the
guard()cleanup handler to manage the device lock. This simplifies the code by removing the need for manual unlocking and goto error handling paths.static int gp2ap020a00f_write_event_config(struct
iio_dev*indio_dev,drivers/iio/light/gp2ap020a00f.c +1
-1enum gp2ap020a00f_cmd cmd;int err; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);
drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_write_event_config(struct iio_dev *indio_dev,
Quoted 10 lines
drivers/iio/light/gp2ap020a00f.c +0
-2switch (chan->type) {case IIO_PROXIMITY : err = -EINVAL ; } -mutex_unlock (&data->lock); -return err; }
What I meant in the my cover letter is that you need to take some pieces from my patches and incorporate them here, so this becomes
return 0;
...
Quoted 11 lines
static int gp2ap020a00f_read_event_config(struct
iio_dev*indio_dev,drivers/iio/light/gp2ap020a00f.c +1
-1struct gp2ap020a00f_data *data =iio_priv (indio_dev);int event_en =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);switch (chan->type) {case IIO_PROXIMITY :
drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev, · line 1207
Quoted 6 lines
drivers/iio/light/gp2ap020a00f.c +0
-2@@ -1223,8 +1204,6 @@ static int gp2ap020a00f_read_event_config(struct iio_dev *indio_dev,break ; } -mutex_unlock (&data->lock); -return event_en;
Same here, now event_en is redundant.
Quoted 1 line
(quoted diff) +0
-0}
...
Review
Quoted 4 lines
static int
gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev)(quoted diff) +0
-0struct gp2ap020a00f_data *data = iio_priv(indio_dev); int i, err = 0;
(Do you need this '= 0'? Check it).
Quoted 10 lines
drivers/iio/light/gp2ap020a00f.c +2
-2-mutex_lock (&data->lock); +guard (mutex)(&data->lock); }if (err <0 ) - goto error_unlock; +return err;
drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev)
Quoted 10 lines
drivers/iio/light/gp2ap020a00f.c +0
-3data->buffer =kmalloc (indio_dev->scan_bytes,GFP_KERNEL );if (!data->buffer) err = -ENOMEM ; -error_unlock: -mutex_unlock (&data->lock); -return err;
And here, this should become
return -ENOMEM; return 0;
Quoted 1 line
}
...
Quoted 11 lines
static int
gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev)drivers/iio/light/gp2ap020a00f.c +1
-1struct gp2ap020a00f_data *data =iio_priv (indio_dev);int i, err =0 ; -mutex_lock (&data->lock); +guard (mutex)(&data->lock);iio_for_each_active_channel (indio_dev, i) {switch (i) {
drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev) · line 1432
Quoted 7 lines
drivers/iio/light/gp2ap020a00f.c +0
-2@@ -1452,8 +1428,6 @@ static int gp2ap020a00f_buffer_predisable(struct iio_dev *indio_dev)if (err ==0 )kfree (data->buffer); -mutex_unlock (&data->lock); -return err; }
Same here.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 5 lines · Ethan Tidmore
This series performs a general cleanup of the gp2ap020a00f IIO driver.
It integrates my original work switching to the
guard()cleanup handler, fixing a signedness warning from Smatch, and cleanups provided by Andy Shevchenko [1].
Thanks, my comments below and in patch 1/9.
Quoted 8 lines
v4:
- Integrated Andy Shevchenko's 7-patch cleanup series.
- Patch 1: Removed redundant 'err' variable and whitespace issues as suggested by Andy.
- Patch 2: Added Reviewed-by tag from Andy Shevchenko.- Patch 5: Updated commit message to describe the pre-existing bug fix in
gp2ap020a00f_buffer_predisable()where errors could be swallowed.
As I mentioned this perhaps better to split to a separate change as a prerequisite to the mentioned patch.
Quoted 14 lines
v3:
- Addressed initial feedback and simplified locking logic.[1] https://lore.kernel.org/all/[email protected]/
Andy Shevchenko (7):
iio: light: gp2ap020a00f: Use correct types for 16-bit LE data
iio: light: gp2ap020a00f: Replace custom implementation ofmin()
iio: light: gp2ap020a00f: Return directly from the switch cases
iio: light: gp2ap020a00f: Use temporary variable for struct device
iio: light: gp2ap020a00f: Explicitly use string literal for driver
name
iio: light: gp2ap020a00f: Remove trailing comma in termination entry
iio: light: gp2ap020a00f: Join some lines of code to be a single line
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 5/9] iio: light: gp2ap020a00f: Return directly from the switch cases
In reply to Ethan TidmoreQuoted 18 lines · Ethan Tidmore
From: Andy Shevchenko <[email protected]>
Return directly from the switch cases which makes code easier to follow. In some cases convert pieces to the standard pattern which also unifies it with the accepted kernel practices.
By doing this, it appears to have fixed a preexisting bug, the variable err in
gp2ap020a00f_buffer_predisable()is only checked once for errors after the for loop is finished. This could allow errors to be swallowed.Signed-off-by Andy Shevchenko <[email protected]>Signed-off-by Ethan Tidmore <[email protected]>---
v4:
- Integrate Andy Shevchenko's cleanups.drivers/iio/light/gp2ap020a00f.c | 96 ++++++++++++-------------------- 1 file changed, 37 insertions(+), 59 deletions(-)
...
Quoted 11 lines
drivers/iio/light/gp2ap020a00f.c +1
-1@@ -1366,7 +1344,7 @@ static const struct iio_info gp2ap020a00f_info = {static int gp2ap020a00f_buffer_postenable (struct iio_dev *indio_dev) {struct gp2ap020a00f_data *data =iio_priv (indio_dev); -int i, err =0 ; +int i, err;guard (mutex)(&data->lock);
drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) · line 1385
Quoted 10 lines
drivers/iio/light/gp2ap020a00f.c +2
-2@@ -1400,15 +1378,15 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) data->buffer =kmalloc (indio_dev->scan_bytes,GFP_KERNEL );if (!data->buffer) - err = -ENOMEM ; +return -ENOMEM ; -return err; +return 0 ; }
Looking over the code again it looks like gp2ap020a00f_buffer_postenable() contains the same bug you mentioned where the error check should have been inside of the loop?
iio_for_each_active_channel(indio_dev, i) {
switch (i) {
case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_CLEAR_EN);
break;
case GP2AP020A00F_SCAN_MODE_LIGHT_IR:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_IR_EN);
break;
case GP2AP020A00F_SCAN_MODE_PROXIMITY:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_PROX_EN);
break;
}
}
if (err < 0)
goto error_unlock;Just wanted to confirm before putting it in the v5.
Thanks,
ET
Re: [PATCH v4 5/9] iio: light: gp2ap020a00f: Return directly from the switch cases
Quoted 1 line · Ethan Tidmore
On Tue Feb 17, 2026 at 10:37 PM CST, Ethan Tidmore wrote:
...
Quoted 3 lines
Quoted 3 lines
By doing this, it appears to have fixed a preexisting bug, the variable err in
gp2ap020a00f_buffer_predisable()is only checked once for errors after the for loop is finished. This could allow errors to be swallowed.
...
Quoted 44 lines
Quoted 9 lines
static int
gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev){drivers/iio/light/gp2ap020a00f.c +1
-1struct gp2ap020a00f_data *data =iio_priv (indio_dev); -int i, err =0 ; +int i, err;guard (mutex)(&data->lock);drivers/iio/light/gp2ap020a00f.c static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) · line 1384
Quoted 9 lines
drivers/iio/light/gp2ap020a00f.c +2
-2@@ -1400,15 +1378,15 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev) data->buffer =kmalloc (indio_dev->scan_bytes,GFP_KERNEL );if (!data->buffer) - err = -ENOMEM ; +return -ENOMEM ; -return err; +return 0 ; }Looking over the code again it looks like
gp2ap020a00f_buffer_postenable()contains the same bug you mentioned where the error check should have been inside of the loop?iio_for_each_active_channel(indio_dev, i) { switch (i) { case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR: err = gp2ap020a00f_exec_cmd(data, GP2AP020A00F_CMD_TRIGGER_CLEAR_EN); break; case GP2AP020A00F_SCAN_MODE_LIGHT_IR: err = gp2ap020a00f_exec_cmd(data, GP2AP020A00F_CMD_TRIGGER_IR_EN); break; case GP2AP020A00F_SCAN_MODE_PROXIMITY: err = gp2ap020a00f_exec_cmd(data, GP2AP020A00F_CMD_TRIGGER_PROX_EN); break; } } if (err < 0) goto error_unlock;
Quoted 1 line
Just wanted to confirm before putting it in the v5.
Good catch! So, these two should be split into a separate change explaining the change in behaviour.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 1/9] iio: light: gp2ap020a00f: simplify locking with guard()
In reply to Andy ShevchenkoQuoted 5 lines · Andy Shevchenko
Quoted 3 lines · Ethan Tidmore
Use the
guard()cleanup handler to manage the device lock. This simplifies the code by removing the need for manual unlocking and goto error handling paths.
...
Quoted 6 lines
What I meant in the my cover letter is that you need to take some pieces from my patches and incorporate them here, so this becomes
return 0;
Understood, so with the first patch how should I credit you? With the patches I'm splitting up that you gave, I know to just put my SOB. Just unsure about the first.
Thanks,
ET
Re: [PATCH v4 1/9] iio: light: gp2ap020a00f: simplify locking with guard()
Quoted 2 lines · Ethan Tidmore
Quoted 1 line · Andy Shevchenko
On Tue, Feb 17, 2026 at 10:37:20PM -0600, Ethan Tidmore wrote:
...
Quoted 8 lines
Quoted 4 lines
What I meant in the my cover letter is that you need to take some pieces from my patches and incorporate them here, so this becomes
return 0;Understood, so with the first patch how should I credit you? With the patches I'm splitting up that you gave, I know to just put my SOB. Just unsure about the first.
No need for a special credits. It's already enough what you put in the cover letter, that the series incorporates the changes I provided.
Signature · 2 lines
With Best Regards, Andy Shevchenko