Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d7203ad8 authored by Alison Schofield's avatar Alison Schofield Committed by Jonathan Cameron
Browse files

iio: adc: ad7791: claim direct mode when writing frequency



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: default avatarAlison Schofield <amsfield22@gmail.com>
Cc: Daniel Baluta <daniel.baluta@gmail.com>
Acked-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent afa81484
Loading
Loading
Loading
Loading
+14 −22
Original line number Diff line number Diff line
@@ -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)) {
	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;

			mutex_lock(&indio_dev->mlock);
	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);
			mutex_unlock(&indio_dev->mlock);
			ret = 0;
			break;
		}
	}
	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,