Skip to content

Commit 21046fd

Browse files
jic23nunojsa
authored andcommitted
iio: locking: introduce __cleanup() based direct mode claiming infrastructure
Allows use of: iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { } to automatically call iio_device_release_direct_mode() based on scope. Typically seen in combination with local device specific locks which are already have automated cleanup options via guard(mutex)(&st->lock) and scoped_guard(). Using both together allows most error handling to be automated. Reviewed-by: Nuno Sa <nuno.a@analog.com> Link: https://lore.kernel.org/r/20240128150537.44592-2-jic23@kernel.org Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent a0ad40f commit 21046fd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

include/linux/iio/iio.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/device.h>
1111
#include <linux/cdev.h>
12+
#include <linux/cleanup.h>
1213
#include <linux/slab.h>
1314
#include <linux/iio/types.h>
1415
/* IIO TODO LIST */
@@ -638,6 +639,32 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp);
638639
int iio_device_claim_direct_mode(struct iio_dev *indio_dev);
639640
void iio_device_release_direct_mode(struct iio_dev *indio_dev);
640641

642+
/*
643+
* This autocleanup logic is normally used via
644+
* iio_device_claim_direct_scoped().
645+
*/
646+
DEFINE_GUARD(iio_claim_direct, struct iio_dev *, iio_device_claim_direct_mode(_T),
647+
iio_device_release_direct_mode(_T))
648+
649+
DEFINE_GUARD_COND(iio_claim_direct, _try, ({
650+
struct iio_dev *dev;
651+
int d = iio_device_claim_direct_mode(_T);
652+
653+
if (d < 0)
654+
dev = NULL;
655+
else
656+
dev = _T;
657+
dev;
658+
}))
659+
660+
/**
661+
* iio_device_claim_direct_scoped() - Scoped call to iio_device_claim_direct.
662+
* @fail: What to do on failure to claim device.
663+
* @iio_dev: Pointer to the IIO devices structure
664+
*/
665+
#define iio_device_claim_direct_scoped(fail, iio_dev) \
666+
scoped_cond_guard(iio_claim_direct_try, fail, iio_dev)
667+
641668
extern struct bus_type iio_bus_type;
642669

643670
/**

0 commit comments

Comments
 (0)