Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: "Mudit Sharma" <muditsharma.info@gmail.com>,
	"Julien Stephan" <jstephan@baylibre.com>,
	"Mariel Tinaco" <Mariel.Tinaco@analog.com>,
	"Angelo Dureghello" <adureghello@baylibre.com>,
	"Gustavo Silva" <gustavograzs@gmail.com>,
	"Nuno Sa" <nuno.sa@analog.com>,
	"João Paulo Gonçalves" <joao.goncalves@toradex.com>,
	"ChiYuan Huang" <cy_huang@richtek.com>,
	"Ramona Alexandra Nechita" <ramona.nechita@analog.com>,
	"Trevor Gamblin" <tgamblin@baylibre.com>,
	"Guillaume Stols" <gstols@baylibre.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Cosmin Tanislav" <demonsingur@gmail.com>,
	"Marcelo Schmitt" <marcelo.schmitt@analog.com>,
	"Gwendal Grignou" <gwendal@chromium.org>,
	"Antoni Pokusinski" <apokusinski01@gmail.com>,
	"Tomasz Duszynski" <tomasz.duszynski@octakon.com>,
	"Jonathan Cameron" <Jonathan.Cameron@huawei.com>
Subject: [PATCH v2 08/27] iio: accel: adxl367: Stop using iio_device_claim_direct_scoped()
Date: Sun,  9 Feb 2025 18:06:05 +0000	[thread overview]
Message-ID: <20250209180624.701140-9-jic23@kernel.org> (raw)
In-Reply-To: <20250209180624.701140-1-jic23@kernel.org>

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

This complex cleanup.h use case of conditional guards has proved
to be more trouble that it is worth in terms of false positive compiler
warnings and hard to read code.

Move directly to the new claim/release_direct() that allow sparse
to check for unbalanced context

In some cases there is a convenient wrapper function to which
the handling can be moved. Do that instead of introducing
another layer of wrappers. In others an outer wrapper is added
which claims direct mode, runs the original function with the
scoped claim logic removed, releases direct mode and then checks
for errors.

Cc: Cosmin Tanislav <demonsingur@gmail.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/accel/adxl367.c | 194 ++++++++++++++++++++----------------
 1 file changed, 106 insertions(+), 88 deletions(-)

diff --git a/drivers/iio/accel/adxl367.c b/drivers/iio/accel/adxl367.c
index a48ac0d7bd96..add4053e7a02 100644
--- a/drivers/iio/accel/adxl367.c
+++ b/drivers/iio/accel/adxl367.c
@@ -477,45 +477,42 @@ static int adxl367_set_fifo_watermark(struct adxl367_state *st,
 static int adxl367_set_range(struct iio_dev *indio_dev,
 			     enum adxl367_range range)
 {
-	iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
-		struct adxl367_state *st = iio_priv(indio_dev);
-		int ret;
+	struct adxl367_state *st = iio_priv(indio_dev);
+	int ret;
 
-		guard(mutex)(&st->lock);
+	guard(mutex)(&st->lock);
 
-		ret = adxl367_set_measure_en(st, false);
-		if (ret)
-			return ret;
+	ret = adxl367_set_measure_en(st, false);
+	if (ret)
+		return ret;
 
-		ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
-					 ADXL367_FILTER_CTL_RANGE_MASK,
-					 FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
-						    range));
-		if (ret)
-			return ret;
+	ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
+				 ADXL367_FILTER_CTL_RANGE_MASK,
+				 FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
+					    range));
+	if (ret)
+		return ret;
 
-		adxl367_scale_act_thresholds(st, st->range, range);
+	adxl367_scale_act_thresholds(st, st->range, range);
 
-		/* Activity thresholds depend on range */
-		ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
-						 st->act_threshold);
-		if (ret)
-			return ret;
+	/* Activity thresholds depend on range */
+	ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
+					 st->act_threshold);
+	if (ret)
+		return ret;
 
-		ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
-						 st->inact_threshold);
-		if (ret)
-			return ret;
+	ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
+					 st->inact_threshold);
+	if (ret)
+		return ret;
 
-		ret = adxl367_set_measure_en(st, true);
-		if (ret)
-			return ret;
+	ret = adxl367_set_measure_en(st, true);
+	if (ret)
+		return ret;
 
-		st->range = range;
+	st->range = range;
 
-		return 0;
-	}
-	unreachable();
+	return 0;
 }
 
 static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms)
@@ -620,23 +617,20 @@ static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
 
 static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr)
 {
-	iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
-		struct adxl367_state *st = iio_priv(indio_dev);
-		int ret;
+	struct adxl367_state *st = iio_priv(indio_dev);
+	int ret;
 
-		guard(mutex)(&st->lock);
+	guard(mutex)(&st->lock);
 
-		ret = adxl367_set_measure_en(st, false);
-		if (ret)
-			return ret;
+	ret = adxl367_set_measure_en(st, false);
+	if (ret)
+		return ret;
 
-		ret = _adxl367_set_odr(st, odr);
-		if (ret)
-			return ret;
+	ret = _adxl367_set_odr(st, odr);
+	if (ret)
+		return ret;
 
-		return adxl367_set_measure_en(st, true);
-	}
-	unreachable();
+	return adxl367_set_measure_en(st, true);
 }
 
 static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg,
@@ -725,32 +719,29 @@ static int adxl367_read_sample(struct iio_dev *indio_dev,
 			       struct iio_chan_spec const *chan,
 			       int *val)
 {
-	iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
-		struct adxl367_state *st = iio_priv(indio_dev);
-		u16 sample;
-		int ret;
+	struct adxl367_state *st = iio_priv(indio_dev);
+	u16 sample;
+	int ret;
 
-		guard(mutex)(&st->lock);
+	guard(mutex)(&st->lock);
 
-		ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
-		if (ret)
-			return ret;
+	ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
+	if (ret)
+		return ret;
 
-		ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
-				       sizeof(st->sample_buf));
-		if (ret)
-			return ret;
+	ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
+			       sizeof(st->sample_buf));
+	if (ret)
+		return ret;
 
-		sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
-		*val = sign_extend32(sample, chan->scan_type.realbits - 1);
+	sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
+	*val = sign_extend32(sample, chan->scan_type.realbits - 1);
 
-		ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
-		if (ret)
-			return ret;
+	ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
+	if (ret)
+		return ret;
 
-		return IIO_VAL_INT;
-	}
-	unreachable();
+	return IIO_VAL_INT;
 }
 
 static int adxl367_get_status(struct adxl367_state *st, u8 *status,
@@ -852,10 +843,15 @@ static int adxl367_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long info)
 {
 	struct adxl367_state *st = iio_priv(indio_dev);
+	int ret;
 
 	switch (info) {
 	case IIO_CHAN_INFO_RAW:
-		return adxl367_read_sample(indio_dev, chan, val);
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+		ret = adxl367_read_sample(indio_dev, chan, val);
+		iio_device_release_direct(indio_dev);
+		return ret;
 	case IIO_CHAN_INFO_SCALE:
 		switch (chan->type) {
 		case IIO_ACCEL: {
@@ -912,7 +908,12 @@ static int adxl367_write_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		return adxl367_set_odr(indio_dev, odr);
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+
+		ret = adxl367_set_odr(indio_dev, odr);
+		iio_device_release_direct(indio_dev);
+		return ret;
 	}
 	case IIO_CHAN_INFO_SCALE: {
 		enum adxl367_range range;
@@ -921,7 +922,12 @@ static int adxl367_write_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		return adxl367_set_range(indio_dev, range);
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+
+		ret = adxl367_set_range(indio_dev, range);
+		iio_device_release_direct(indio_dev);
+		return ret;
 	}
 	default:
 		return -EINVAL;
@@ -1069,13 +1075,15 @@ static int adxl367_read_event_config(struct iio_dev *indio_dev,
 	}
 }
 
-static int adxl367_write_event_config(struct iio_dev *indio_dev,
-				      const struct iio_chan_spec *chan,
-				      enum iio_event_type type,
-				      enum iio_event_direction dir,
-				      bool state)
+static int __adxl367_write_event_config(struct iio_dev *indio_dev,
+					const struct iio_chan_spec *chan,
+					enum iio_event_type type,
+					enum iio_event_direction dir,
+					bool state)
 {
+	struct adxl367_state *st = iio_priv(indio_dev);
 	enum adxl367_activity_type act;
+	int ret;
 
 	switch (dir) {
 	case IIO_EV_DIR_RISING:
@@ -1088,28 +1096,38 @@ static int adxl367_write_event_config(struct iio_dev *indio_dev,
 		return -EINVAL;
 	}
 
-	iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
-		struct adxl367_state *st = iio_priv(indio_dev);
-		int ret;
+	guard(mutex)(&st->lock);
+
+	ret = adxl367_set_measure_en(st, false);
+	if (ret)
+		return ret;
 
-		guard(mutex)(&st->lock);
+	ret = adxl367_set_act_interrupt_en(st, act, state);
+	if (ret)
+		return ret;
 
-		ret = adxl367_set_measure_en(st, false);
-		if (ret)
-			return ret;
+	ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED
+				 : ADXL367_ACT_DISABLED);
+	if (ret)
+		return ret;
 
-		ret = adxl367_set_act_interrupt_en(st, act, state);
-		if (ret)
-			return ret;
+	return adxl367_set_measure_en(st, true);
+}
 
-		ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED
-					 : ADXL367_ACT_DISABLED);
-		if (ret)
-			return ret;
+static int adxl367_write_event_config(struct iio_dev *indio_dev,
+				      const struct iio_chan_spec *chan,
+				      enum iio_event_type type,
+				      enum iio_event_direction dir,
+				      bool state)
+{
+	int ret;
 
-		return adxl367_set_measure_en(st, true);
-	}
-	unreachable();
+	if (!iio_device_claim_direct(indio_dev))
+		return -EBUSY;
+
+	ret = __adxl367_write_event_config(indio_dev, chan, type, dir, state);
+	iio_device_release_direct(indio_dev);
+	return ret;
 }
 
 static ssize_t adxl367_get_fifo_enabled(struct device *dev,
-- 
2.48.1


  parent reply	other threads:[~2025-02-09 18:07 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-09 18:05 [PATCH v2 00/27] iio: improve handling of direct mode claim and release Jonathan Cameron
2025-02-09 18:05 ` [PATCH v2 01/27] iio: core: Rework claim and release of direct mode to work with sparse Jonathan Cameron
2025-02-17 10:38   ` Nuno Sá
2025-02-17 12:57     ` Jonathan Cameron
2025-02-22 15:51   ` Andy Shevchenko
2025-02-22 17:23     ` Jonathan Cameron
2025-02-22 20:58       ` Andy Shevchenko
2025-02-25  6:25         ` Jonathan Cameron
2025-02-25  7:09           ` Andy Shevchenko
2025-02-09 18:05 ` [PATCH v2 02/27] iio: chemical: scd30: Use guard(mutex) to allow early returns Jonathan Cameron
2025-02-17 10:56   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 03/27] iio: chemical: scd30: Switch to sparse friendly claim/release_direct() Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 04/27] iio: temperature: tmp006: Stop using iio_device_claim_direct_scoped() Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 05/27] iio: proximity: sx9310: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 06/27] iio: proximity: sx9324: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 07/27] iio: proximity: sx9360: " Jonathan Cameron
2025-02-09 18:06 ` Jonathan Cameron [this message]
2025-02-17 10:44   ` [PATCH v2 08/27] iio: accel: adxl367: " Nuno Sá
2025-02-09 18:06 ` [PATCH v2 09/27] iio: adc: ad4000: " Jonathan Cameron
2025-02-17 10:45   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 10/27] iio: adc: ad4130: " Jonathan Cameron
2025-02-17 10:45   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 11/27] iio: adc: ad4695: " Jonathan Cameron
2025-02-16 18:19   ` Jonathan Cameron
2025-02-16 19:00     ` David Lechner
2025-02-17 10:48       ` Nuno Sá
2025-02-17 13:04         ` Jonathan Cameron
2025-02-17 10:48   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 12/27] iio: adc: ad7606: " Jonathan Cameron
2025-02-17 10:49   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 13/27] iio: adc: ad7625: " Jonathan Cameron
2025-02-17 10:49   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 14/27] iio: adc: ad7779: " Jonathan Cameron
2025-02-17 10:50   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 15/27] iio: adc: ad9467: " Jonathan Cameron
2025-02-17 10:50   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 16/27] iio: adc: max1363: " Jonathan Cameron
2025-02-17 10:51   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 17/27] iio: adc: rtq6056: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 18/27] iio: adc: ti-adc161s626: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 19/27] iio: adc: ti-ads1119: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 20/27] iio: addac: ad74413r: " Jonathan Cameron
2025-02-17 10:52   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 21/27] iio: chemical: ens160: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 22/27] iio: dac: ad3552r-hs: " Jonathan Cameron
2025-02-17 10:53   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 23/27] iio: dac: ad8460: " Jonathan Cameron
2025-02-17 10:52   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 24/27] iio: dummy: " Jonathan Cameron
2025-02-17 10:55   ` Nuno Sá
2025-02-09 18:06 ` [PATCH v2 25/27] iio: imu: bmi323: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 26/27] iio: light: bh1745: " Jonathan Cameron
2025-02-09 18:06 ` [PATCH v2 27/27] iio: Drop iio_device_claim_direct_scoped() and related infrastructure Jonathan Cameron
2025-02-17 10:57   ` Nuno Sá

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250209180624.701140-9-jic23@kernel.org \
    --to=jic23@kernel.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=Mariel.Tinaco@analog.com \
    --cc=adureghello@baylibre.com \
    --cc=apokusinski01@gmail.com \
    --cc=cy_huang@richtek.com \
    --cc=demonsingur@gmail.com \
    --cc=dlechner@baylibre.com \
    --cc=gstols@baylibre.com \
    --cc=gustavograzs@gmail.com \
    --cc=gwendal@chromium.org \
    --cc=joao.goncalves@toradex.com \
    --cc=jstephan@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=marcelo.schmitt@analog.com \
    --cc=muditsharma.info@gmail.com \
    --cc=nuno.sa@analog.com \
    --cc=ramona.nechita@analog.com \
    --cc=tgamblin@baylibre.com \
    --cc=tomasz.duszynski@octakon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox