Skip to contentSkip to conversation

iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures

PATCH In review Octávio Carneiro 2 participants 2 messages May 7, 2025 – May 8, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures

Add iio_device_claim_direct() to protect buffered captures. In ad4000_write_raw_get_fmt, data reads are protected by the function call to avoid possible errors caused by concurrent access.

---
 drivers/iio/adc/ad4000.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iio/adc/ad4000.c +3 -0
index 4fe8dee48..55ceee6e9 100644
@@ -583,6 +583,9 @@ static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev,
 {
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+		iio_device_release_direct(indio_dev);
 		return IIO_VAL_INT_PLUS_NANO;
 	default:
 		return IIO_VAL_INT_PLUS_MICRO;
Signature · 1 line
2.34.1
#1 lore raw

Hi, Fernando, Octávio, Lucas,

I think you forgot to run get_maintainer.pl to get all the expected recipients for this patch. Also, even though the code seems to be syntactically correct, the proposed changes are not suitable for the IIO driver. See comments inline.

Quoted 19 lines · Octávio Carneiro

Add iio_device_claim_direct() to protect buffered captures. In ad4000_write_raw_get_fmt, data reads are protected by the function call to avoid possible errors caused by concurrent access.

---
 drivers/iio/adc/ad4000.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iio/adc/ad4000.c +0 -0
index 4fe8dee48..55ceee6e9 100644

drivers/iio/adc/ad4000.c static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev, · line 588

Quoted 6 lines
drivers/iio/adc/ad4000.c +3 -0
@@ -583,6 +583,9 @@ static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev,
 {
 	switch (mask) {
 	case IIO_CHAN_INFO_SCALE:
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+		iio_device_release_direct(indio_dev);

In the ad4000 driver, the main purpose of claiming IIO direct mode is to keep the integrity of buffered data capture. Basically, we want to avoid changes of device state properties and the issue of concurrent readings. Note that under the hood, iio_device_claim_direct() acquires a mutex lock. Also, it is good practice to protect only the critical paths and to not keep locks of access control mechanisms for longer than what's really needed. Thus, we claim IIO direct mode at ad4000_read_raw() and at ad4000_write_raw(), right before running ADC data or register access transfers.

So, the proposed addition is not needed and doesn't really follow best programming practices.

drivers/iio/adc/ad4000.c static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev, · line 591

Quoted 3 lines
drivers/iio/adc/ad4000.c +0 -0
 		return IIO_VAL_INT_PLUS_NANO;
 	default:
 		return IIO_VAL_INT_PLUS_MICRO;

With best regards,
Marcelo