staging: iio: frequency: ad9832: use guard(mutex)
Subsystems (4)
PATCH 1/1 staging: iio: frequency: ad9832: use guard(mutex)
Use guard(mutex) to automatically manage the state lock in ad9832_write(). This simplifies error handling by allowing direct returns from each switch case and error branch, eliminating the manual mutex_unlock() calls and the goto error label.
--- drivers/staging/iio/frequency/ad9832.c | 47 +++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-)
drivers/staging/iio/frequency/ad9832.c +31 -16
@@ -8,6 +8,7 @@#include <linux/array_size.h> #include <linux/bitfield.h> #include <linux/bitops.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/dev_printk.h> #include <linux/err.h>
@@ -188,52 +189,65 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, ret =kstrtoul (buf,10 , &val);if (ret) - goto error_ret; +return ret; + +guard (mutex)(&st->lock); -mutex_lock (&st->lock);switch ((u32 )this_attr->address) {case AD9832_FREQ0HM :case AD9832_FREQ1HM : ret =ad9832_write_frequency (st, this_attr->address, val); +if (ret) +return ret; +break ;case AD9832_PHASE0H :case AD9832_PHASE1H :case AD9832_PHASE2H :case AD9832_PHASE3H : ret =ad9832_write_phase (st, this_attr->address, val); +if (ret) +return ret; +break ;case AD9832_PINCTRL_EN : st->ctrl_ss &= ~AD9832_SELSRC ; st->ctrl_ss |=FIELD_PREP (AD9832_SELSRC , val ?0 :1 ); st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SYNCSELSRC ) | - st->ctrl_ss); + st->ctrl_ss); ret =spi_sync (st->spi, &st->msg); +if (ret) +return ret; +break ;case AD9832_FREQ_SYM :if (val ==1 || val ==0 ) { st->ctrl_fp &= ~AD9832_FREQ ; st->ctrl_fp |=FIELD_PREP (AD9832_FREQ , val ?1 :0 ); }else { - ret = -EINVAL ; -break ; +return -EINVAL ; } st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | - st->ctrl_fp); + st->ctrl_fp); ret =spi_sync (st->spi, &st->msg); +if (ret) +return ret; +break ;case AD9832_PHASE_SYM : -if (val >3 ) { - ret = -EINVAL ; -break ; - } +if (val >3 ) +return -EINVAL ; st->ctrl_fp &= ~AD9832_PHASE_MASK ; st->ctrl_fp |=FIELD_PREP (AD9832_PHASE_MASK , val); st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_FPSELECT ) | - st->ctrl_fp); + st->ctrl_fp); ret =spi_sync (st->spi, &st->msg); +if (ret) +return ret; +break ;case AD9832_OUTPUT_EN :if (val)
@@ -242,16 +256,17 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, st->ctrl_src |=FIELD_PREP (AD9832_RESET ,1 ); st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SLEEPRESCLR ) | - st->ctrl_src); + st->ctrl_src); ret =spi_sync (st->spi, &st->msg); +if (ret) +return ret; +break ;default : - ret = -ENODEV ; +return -ENODEV ; } -mutex_unlock (&st->lock); -error_ret: -return ret ? ret : len; +return len; }/*
Signature · 1 line
2.53.0
Quoted 4 lines · Nithishvelan M
Use guard(mutex) to automatically manage the state lock in
ad9832_write(). This simplifies error handling by allowing direct returns from each switch case and error branch, eliminating the manualmutex_unlock()calls and the goto error label.
Patch is okay, below some ideas for further improvements.
...
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 215
Quoted 3 lines
drivers/staging/iio/frequency/ad9832.c +0
-0case AD9832_PINCTRL_EN : st->ctrl_ss &= ~AD9832_SELSRC ; st->ctrl_ss |=FIELD_PREP (AD9832_SELSRC , val ?0 :1 );
This is basically FIELD_MODIFY() (and in many similar places in the driver).
...
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 227
Quoted 4 lines
drivers/staging/iio/frequency/ad9832.c +0
-0case AD9832_FREQ_SYM :if (val ==1 || val ==0 ) { st->ctrl_fp &= ~AD9832_FREQ ; st->ctrl_fp |=FIELD_PREP (AD9832_FREQ , val ?1 :0 );
Here ternary is unneeded, however might be still okay to have to be consistent with the above case(s).
...
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 240
Quoted 7 lines
drivers/staging/iio/frequency/ad9832.c +2
-4case AD9832_PHASE_SYM : -if (val >3 ) { - ret = -EINVAL ; -break ; - } +if (val >3 ) +return -EINVAL ;
In the above and here we might use FIELD_FIT(), but I haven't checked the actual ranges.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 46 lines · Nithishvelan M
Use guard(mutex) to automatically manage the state lock in
ad9832_write(). This simplifies error handling by allowing direct returns from each switch case and error branch, eliminating the manualmutex_unlock()calls and the goto error label.Signed-off-by Nithishvelan M <[email protected]>--- drivers/staging/iio/frequency/ad9832.c | 47 +++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-)drivers/staging/iio/frequency/ad9832.c +8
-2@@ -8,6 +8,7 @@#include <linux/array_size.h> #include <linux/bitfield.h> #include <linux/bitops.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/dev_printk.h> #include <linux/err.h> @@ -188,52 +189,65 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, ret =kstrtoul (buf,10 , &val);if (ret) - goto error_ret; +return ret; + +guard (mutex)(&st->lock); -mutex_lock (&st->lock);switch ((u32 )this_attr->address) {case AD9832_FREQ0HM :case AD9832_FREQ1HM : ret =ad9832_write_frequency (st, this_attr->address, val); +if (ret) +return ret; +break ;case AD9832_PHASE0H :case AD9832_PHASE1H :case AD9832_PHASE2H :case AD9832_PHASE3H : ret =ad9832_write_phase (st, this_attr->address, val); +if (ret)
drivers/staging/iio/frequency/ad9832.c static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, · line 218
Quoted 10 lines
drivers/staging/iio/frequency/ad9832.c +3
-1+return ret; +break ;case AD9832_PINCTRL_EN : st->ctrl_ss &= ~AD9832_SELSRC ; st->ctrl_ss |=FIELD_PREP (AD9832_SELSRC , val ?0 :1 ); st->data =cpu_to_be16 (FIELD_PREP (AD9832_CMD_MSK ,AD9832_CMD_SYNCSELSRC ) | - st->ctrl_ss); + st->ctrl_ss);
Please don't make these sorts of unrelated white space changes. They just make the review more complicated.
regards,
dan carpenter