From: Jonathan Cameron <jic23@kernel.org>
To: linux-iio@vger.kernel.org
Cc: Peter Zijlstra <peterz@infradead.org>,
Cosmin Tanislav <demonsingur@gmail.com>,
Jagath Jog J <jagathjog1996@gmail.com>,
Gwendal Grignou <gwendal@chromium.org>,
Daniel Campello <campello@chromium.org>,
gregkh@linuxfoundation.org,
Jonathan Cameron <Jonathan.Cameron@huawei.com>
Subject: [RFC PATCH 2/8] iio: dummy: Add use of new automated cleanup of locks and direct mode claiming.
Date: Sun, 22 Oct 2023 16:47:04 +0100 [thread overview]
Message-ID: <20231022154710.402590-3-jic23@kernel.org> (raw)
In-Reply-To: <20231022154710.402590-1-jic23@kernel.org>
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
As this driver is faking buffered access there isn't strictly any need
to prevent direct access whilst it is running. However, we do want this
to look like real devices where such restrictions are commonly needed.
Adding such protections also allow testing the automated cleanup via
CLASS(iio_claim_direct, claimed_dev)(indio_dev);
if (IS_ERR(claimed_dev))
return PTR_ERR(claimed_dev);
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/iio/dummy/iio_simple_dummy.c | 145 ++++++++++++++-------------
1 file changed, 74 insertions(+), 71 deletions(-)
diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c
index c24f609c2ade..e412f6a84fc3 100644
--- a/drivers/iio/dummy/iio_simple_dummy.c
+++ b/drivers/iio/dummy/iio_simple_dummy.c
@@ -282,66 +282,73 @@ static int iio_dummy_read_raw(struct iio_dev *indio_dev,
int *val2,
long mask)
{
+ /*
+ * Whilst it can be elegant to use the claimed device for this, it's not necessary
+ * where we have a mixture of paths accessing under that protection, to prevent
+ * access that might disrupt the buffered flow, and those that only care about
+ * protection of the device specific state.
+ */
struct iio_dummy_state *st = iio_priv(indio_dev);
- int ret = -EINVAL;
- mutex_lock(&st->lock);
switch (mask) {
- case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
+ case IIO_CHAN_INFO_RAW: { /* magic value - channel value read */
+ CLASS(iio_claim_direct, claimed_dev)(indio_dev);
+ if (IS_ERR(claimed_dev))
+ return PTR_ERR(claimed_dev);
+ guard(mutex)(&st->lock);
+
switch (chan->type) {
case IIO_VOLTAGE:
if (chan->output) {
/* Set integer part to cached value */
*val = st->dac_val;
- ret = IIO_VAL_INT;
+ return IIO_VAL_INT;
} else if (chan->differential) {
if (chan->channel == 1)
*val = st->differential_adc_val[0];
else
*val = st->differential_adc_val[1];
- ret = IIO_VAL_INT;
+ return IIO_VAL_INT;
} else {
*val = st->single_ended_adc_val;
- ret = IIO_VAL_INT;
+ return IIO_VAL_INT;
}
- break;
+
case IIO_ACCEL:
*val = st->accel_val;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
default:
- break;
+ return -EINVAL;
}
- break;
- case IIO_CHAN_INFO_PROCESSED:
+ }
+ case IIO_CHAN_INFO_PROCESSED: {
+ CLASS(iio_claim_direct, claimed_dev)(indio_dev);
+ if (IS_ERR(claimed_dev))
+ return PTR_ERR(claimed_dev);
+ guard(mutex)(&st->lock);
switch (chan->type) {
case IIO_STEPS:
*val = st->steps;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
case IIO_ACTIVITY:
switch (chan->channel2) {
case IIO_MOD_RUNNING:
*val = st->activity_running;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
case IIO_MOD_WALKING:
*val = st->activity_walking;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
default:
- break;
+ return -EINVAL;
}
- break;
default:
- break;
+ return -EINVAL;
}
- break;
+ }
case IIO_CHAN_INFO_OFFSET:
/* only single ended adc -> 7 */
*val = 7;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_VOLTAGE:
@@ -350,60 +357,57 @@ static int iio_dummy_read_raw(struct iio_dev *indio_dev,
/* only single ended adc -> 0.001333 */
*val = 0;
*val2 = 1333;
- ret = IIO_VAL_INT_PLUS_MICRO;
- break;
+ return IIO_VAL_INT_PLUS_MICRO;
case 1:
/* all differential adc -> 0.000001344 */
*val = 0;
*val2 = 1344;
- ret = IIO_VAL_INT_PLUS_NANO;
+ return IIO_VAL_INT_PLUS_NANO;
+ default:
+ return -EINVAL;
}
- break;
default:
- break;
+ return -EINVAL;
}
- break;
- case IIO_CHAN_INFO_CALIBBIAS:
+ case IIO_CHAN_INFO_CALIBBIAS: {
+ guard(mutex)(&st->lock);
/* only the acceleration axis - read from cache */
*val = st->accel_calibbias;
- ret = IIO_VAL_INT;
- break;
- case IIO_CHAN_INFO_CALIBSCALE:
+ return IIO_VAL_INT;
+ }
+ case IIO_CHAN_INFO_CALIBSCALE: {
+ guard(mutex)(&st->lock);
*val = st->accel_calibscale->val;
*val2 = st->accel_calibscale->val2;
- ret = IIO_VAL_INT_PLUS_MICRO;
- break;
+ return IIO_VAL_INT_PLUS_MICRO;
+ }
case IIO_CHAN_INFO_SAMP_FREQ:
*val = 3;
*val2 = 33;
- ret = IIO_VAL_INT_PLUS_NANO;
- break;
- case IIO_CHAN_INFO_ENABLE:
+ return IIO_VAL_INT_PLUS_NANO;
+ case IIO_CHAN_INFO_ENABLE: {
+ guard(mutex)(&st->lock);
switch (chan->type) {
case IIO_STEPS:
*val = st->steps_enabled;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
default:
- break;
+ return -EINVAL;
}
- break;
- case IIO_CHAN_INFO_CALIBHEIGHT:
+ }
+ case IIO_CHAN_INFO_CALIBHEIGHT: {
+ guard(mutex)(&st->lock);
switch (chan->type) {
case IIO_STEPS:
*val = st->height;
- ret = IIO_VAL_INT;
- break;
+ return IIO_VAL_INT;
default:
- break;
+ return -EINVAL;
}
- break;
-
+ }
default:
- break;
+ return -EINVAL;
}
- mutex_unlock(&st->lock);
- return ret;
}
/**
@@ -436,10 +440,10 @@ static int iio_dummy_write_raw(struct iio_dev *indio_dev,
if (chan->output == 0)
return -EINVAL;
- /* Locking not required as writing single value */
- mutex_lock(&st->lock);
- st->dac_val = val;
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ /* Locking not required as writing single value */
+ st->dac_val = val;
+ }
return 0;
default:
return -EINVAL;
@@ -447,9 +451,9 @@ static int iio_dummy_write_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_PROCESSED:
switch (chan->type) {
case IIO_STEPS:
- mutex_lock(&st->lock);
- st->steps = val;
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ st->steps = val;
+ }
return 0;
case IIO_ACTIVITY:
if (val < 0)
@@ -470,30 +474,29 @@ static int iio_dummy_write_raw(struct iio_dev *indio_dev,
default:
return -EINVAL;
}
- case IIO_CHAN_INFO_CALIBSCALE:
- mutex_lock(&st->lock);
+ case IIO_CHAN_INFO_CALIBSCALE: {
+ guard(mutex)(&st->lock);
/* Compare against table - hard matching here */
for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
if (val == dummy_scales[i].val &&
val2 == dummy_scales[i].val2)
break;
if (i == ARRAY_SIZE(dummy_scales))
- ret = -EINVAL;
- else
- st->accel_calibscale = &dummy_scales[i];
- mutex_unlock(&st->lock);
+ return -EINVAL;
+ st->accel_calibscale = &dummy_scales[i];
return ret;
+ }
case IIO_CHAN_INFO_CALIBBIAS:
- mutex_lock(&st->lock);
- st->accel_calibbias = val;
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ st->accel_calibbias = val;
+ }
return 0;
case IIO_CHAN_INFO_ENABLE:
switch (chan->type) {
case IIO_STEPS:
- mutex_lock(&st->lock);
- st->steps_enabled = val;
- mutex_unlock(&st->lock);
+ scoped_guard(mutex, &st->lock) {
+ st->steps_enabled = val;
+ }
return 0;
default:
return -EINVAL;
--
2.42.0
next prev parent reply other threads:[~2023-10-22 15:47 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-22 15:47 [RFC PATCH 0/8] IIO: Use the new cleanup.h magic Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 1/8] iio: locking: introduce __cleanup() based direct mode claiming infrastructure Jonathan Cameron
2023-10-22 21:10 ` David Lechner
2023-10-23 8:55 ` Nuno Sá
2023-10-23 9:53 ` Jonathan Cameron
2023-10-23 11:51 ` Nuno Sá
2023-10-23 14:34 ` Jonathan Cameron
2023-10-23 14:58 ` Nuno Sá
2023-10-24 12:23 ` Jonathan Cameron
2023-10-25 7:24 ` Nuno Sá
2023-10-24 15:11 ` Peter Zijlstra
2023-10-24 15:28 ` Peter Zijlstra
2023-10-28 16:59 ` Jonathan Cameron
2023-11-02 10:48 ` Peter Zijlstra
2023-11-03 15:19 ` Jonathan Cameron
2023-10-23 9:49 ` Jonathan Cameron
2023-10-22 15:47 ` Jonathan Cameron [this message]
2023-10-22 15:47 ` [RFC PATCH 3/8] iio: accel: adxl367: Use automated cleanup for locks and iio direct mode Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 4/8] iio: imu: bmi323: Use cleanup handling for iio_device_claim_direct_mode() Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 5/8] iio: adc: max1363: Use automatic cleanup for locks and iio mode claiming Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 6/8] iio: proximity: sx9360: Use automated cleanup for locks and IIO " Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 7/8] iio: proximity: sx9324: " Jonathan Cameron
2023-10-22 15:47 ` [RFC PATCH 8/8] iio: proximity: sx9310: " Jonathan Cameron
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=20231022154710.402590-3-jic23@kernel.org \
--to=jic23@kernel.org \
--cc=Jonathan.Cameron@huawei.com \
--cc=campello@chromium.org \
--cc=demonsingur@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=gwendal@chromium.org \
--cc=jagathjog1996@gmail.com \
--cc=linux-iio@vger.kernel.org \
--cc=peterz@infradead.org \
/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