Commit 9780a23e authored by Claudiu Beznea's avatar Claudiu Beznea Committed by Jonathan Cameron

iio: adc: at91-sama5d2_adc: lock around oversampling and sample freq

.read_raw()/.write_raw() could be called asynchronously from user space
or other in kernel drivers. Without locking on st->lock these could be
called asynchronously while there is a conversion in progress. Read will
be harmless but changing registers while conversion is in progress may
lead to inconsistent results. Thus, to avoid this lock st->lock.

Fixes: 27e17719 ("iio:adc:at91_adc8xx: introduce new atmel adc driver")
Fixes: 6794e23f ("iio: adc: at91-sama5d2_adc: add support for oversampling resolution")
Signed-off-by: default avatarClaudiu Beznea <claudiu.beznea@microchip.com>
Link: https://lore.kernel.org/r/20220803102855.2191070-4-claudiu.beznea@microchip.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent d84ace94
...@@ -1542,10 +1542,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev, ...@@ -1542,10 +1542,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
ret = at91_adc_read_position(st, chan->channel, ret = at91_adc_read_position(st, chan->channel,
&tmp_val); &tmp_val);
*val = tmp_val; *val = tmp_val;
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
if (ret > 0) if (ret > 0)
ret = at91_adc_adjust_val_osr(st, val); ret = at91_adc_adjust_val_osr(st, val);
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
return ret; return ret;
} }
...@@ -1558,10 +1558,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev, ...@@ -1558,10 +1558,10 @@ static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
ret = at91_adc_read_pressure(st, chan->channel, ret = at91_adc_read_pressure(st, chan->channel,
&tmp_val); &tmp_val);
*val = tmp_val; *val = tmp_val;
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
if (ret > 0) if (ret > 0)
ret = at91_adc_adjust_val_osr(st, val); ret = at91_adc_adjust_val_osr(st, val);
mutex_unlock(&st->lock);
iio_device_release_direct_mode(indio_dev);
return ret; return ret;
} }
...@@ -1650,16 +1650,20 @@ static int at91_adc_write_raw(struct iio_dev *indio_dev, ...@@ -1650,16 +1650,20 @@ static int at91_adc_write_raw(struct iio_dev *indio_dev,
/* if no change, optimize out */ /* if no change, optimize out */
if (val == st->oversampling_ratio) if (val == st->oversampling_ratio)
return 0; return 0;
mutex_lock(&st->lock);
st->oversampling_ratio = val; st->oversampling_ratio = val;
/* update ratio */ /* update ratio */
at91_adc_config_emr(st); at91_adc_config_emr(st);
mutex_unlock(&st->lock);
return 0; return 0;
case IIO_CHAN_INFO_SAMP_FREQ: case IIO_CHAN_INFO_SAMP_FREQ:
if (val < st->soc_info.min_sample_rate || if (val < st->soc_info.min_sample_rate ||
val > st->soc_info.max_sample_rate) val > st->soc_info.max_sample_rate)
return -EINVAL; return -EINVAL;
mutex_lock(&st->lock);
at91_adc_setup_samp_freq(indio_dev, val); at91_adc_setup_samp_freq(indio_dev, val);
mutex_unlock(&st->lock);
return 0; return 0;
default: default:
return -EINVAL; return -EINVAL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment