Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures
@ 2025-05-07 18:00 Octávio Carneiro
  2025-05-08  5:34 ` Marcelo Schmitt
  0 siblings, 1 reply; 2+ messages in thread
From: Octávio Carneiro @ 2025-05-07 18:00 UTC (permalink / raw)
  To: marcelo.schmitt, linux-iio; +Cc: ocarneiro1, fernandolimabusiness, eijiuchyama

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.

Signed-off-by: Fernando Lima <fernandolimabusiness@gmail.com>
Co-developed-by: Octávio Carneiro <ocarneiro1@gmail.com>
Signed-off-by: Octávio Carneiro <ocarneiro1@gmail.com>
Co-developed-by: Lucas Eiji <eijiuchyama@usp.br>
Signed-off-by: Lucas Eiji <eijiuchyama@usp.br>
---
 drivers/iio/adc/ad4000.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iio/adc/ad4000.c b/drivers/iio/adc/ad4000.c
index 4fe8dee48..55ceee6e9 100644
--- a/drivers/iio/adc/ad4000.c
+++ b/drivers/iio/adc/ad4000.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures
  2025-05-07 18:00 [PATCH] iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures Octávio Carneiro
@ 2025-05-08  5:34 ` Marcelo Schmitt
  0 siblings, 0 replies; 2+ messages in thread
From: Marcelo Schmitt @ 2025-05-08  5:34 UTC (permalink / raw)
  To: Octávio Carneiro
  Cc: marcelo.schmitt, linux-iio, fernandolimabusiness, eijiuchyama

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.

On 05/07, Octávio Carneiro wrote:
> 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.
> 
> Signed-off-by: Fernando Lima <fernandolimabusiness@gmail.com>
> Co-developed-by: Octávio Carneiro <ocarneiro1@gmail.com>
> Signed-off-by: Octávio Carneiro <ocarneiro1@gmail.com>
> Co-developed-by: Lucas Eiji <eijiuchyama@usp.br>
> Signed-off-by: Lucas Eiji <eijiuchyama@usp.br>
> ---
>  drivers/iio/adc/ad4000.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/iio/adc/ad4000.c b/drivers/iio/adc/ad4000.c
> index 4fe8dee48..55ceee6e9 100644
> --- a/drivers/iio/adc/ad4000.c
> +++ b/drivers/iio/adc/ad4000.c
> @@ -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.

>  		return IIO_VAL_INT_PLUS_NANO;
>  	default:
>  		return IIO_VAL_INT_PLUS_MICRO;

With best regards,
Marcelo

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-05-08  5:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-07 18:00 [PATCH] iio: adc: ad4000: Add iio_device_claim_direct() to protect buffered captures Octávio Carneiro
2025-05-08  5:34 ` Marcelo Schmitt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox