Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+28
+28
include/linux/iio/iio.h
··· 9 9 10 10 #include <linux/device.h> 11 11 #include <linux/cdev.h> 12 + #include <linux/cleanup.h> 12 13 #include <linux/slab.h> 13 14 #include <linux/iio/types.h> 14 15 /* IIO TODO LIST */ ··· 639 638 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp); 640 639 int iio_device_claim_direct_mode(struct iio_dev *indio_dev); 641 640 void iio_device_release_direct_mode(struct iio_dev *indio_dev); 641 + 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 + 642 668 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev); 643 669 void iio_device_release_buffer_mode(struct iio_dev *indio_dev); 644 670