Driver was checking for direct mode and trying to lock it, but
left a gap where mode could change before the desired operation.
Use iio_device_claim_direct_mode() to guarantee device stays in
direct mode.

Refactor function to clarify look-up followed by lock sequence.

Signed-off-by: Alison Schofield <amsfiel...@gmail.com>
Cc: Daniel Baluta <daniel.bal...@gmail.com>
---
Changes in v2
Refactored function per reviewers comments
Updated changelog

 drivers/iio/adc/ad7791.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

diff --git a/drivers/iio/adc/ad7791.c b/drivers/iio/adc/ad7791.c
index cf172d58..1dfe641 100644
--- a/drivers/iio/adc/ad7791.c
+++ b/drivers/iio/adc/ad7791.c
@@ -272,30 +272,22 @@ static ssize_t ad7791_write_frequency(struct device *dev,
        struct ad7791_state *st = iio_priv(indio_dev);
        int i, ret;
 
-       mutex_lock(&indio_dev->mlock);
-       if (iio_buffer_enabled(indio_dev)) {
-               mutex_unlock(&indio_dev->mlock);
-               return -EBUSY;
-       }
-       mutex_unlock(&indio_dev->mlock);
-
-       ret = -EINVAL;
-
-       for (i = 0; i < ARRAY_SIZE(ad7791_sample_freq_avail); i++) {
-               if (sysfs_streq(ad7791_sample_freq_avail[i], buf)) {
-
-                       mutex_lock(&indio_dev->mlock);
-                       st->filter &= ~AD7791_FILTER_RATE_MASK;
-                       st->filter |= i;
-                       ad_sd_write_reg(&st->sd, AD7791_REG_FILTER,
-                                        sizeof(st->filter), st->filter);
-                       mutex_unlock(&indio_dev->mlock);
-                       ret = 0;
+       for (i = 0; i < ARRAY_SIZE(ad7791_sample_freq_avail); i++)
+               if (sysfs_streq(ad7791_sample_freq_avail[i], buf))
                        break;
-               }
-       }
+       if (i == ARRAY_SIZE(ad7791_sample_freq_avail))
+               return -EINVAL;
+
+       ret = iio_device_claim_direct_mode(indio_dev);
+       if (ret)
+               return ret;
+       st->filter &= ~AD7791_FILTER_RATE_MASK;
+       st->filter |= i;
+       ad_sd_write_reg(&st->sd, AD7791_REG_FILTER, sizeof(st->filter),
+                       st->filter);
+       iio_device_release_direct_mode(indio_dev);
 
-       return ret ? ret : len;
+       return len;
 }
 
 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
-- 
2.1.4

Reply via email to