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

Configure Feed

Select the types of activity you want to include in your feed.

iio: core: Add cleanup.h support for iio_device_claim_*()

Add guard classes for iio_device_claim_*() conditional locks. This will
aid drivers write safer and cleaner code when dealing with some common
patterns.

These classes are not meant to be used directly by drivers (hence the
__priv__ prefix). Instead, documented wrapper macros are provided to
enforce the use of ACQUIRE() or guard() semantics and avoid the
problematic scoped guard.

Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Acked-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Kurt Borja <kuurtb@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Kurt Borja and committed by
Jonathan Cameron
7a38b75d 2daee817

+65
+65
include/linux/iio/iio.h
··· 10 10 #include <linux/align.h> 11 11 #include <linux/device.h> 12 12 #include <linux/cdev.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/compiler_types.h> 14 15 #include <linux/minmax.h> 15 16 #include <linux/slab.h> ··· 740 739 * Use with iio_device_try_claim_buffer_mode(). 741 740 */ 742 741 #define iio_device_release_buffer_mode(indio_dev) __iio_dev_mode_unlock(indio_dev) 742 + 743 + /* 744 + * These classes are not meant to be used directly by drivers (hence the 745 + * __priv__ prefix). Instead, documented wrapper macros are provided below to 746 + * enforce the use of ACQUIRE() or guard() semantics and avoid the problematic 747 + * scoped guard variants. 748 + */ 749 + DEFINE_GUARD(__priv__iio_dev_mode_lock, struct iio_dev *, 750 + __iio_dev_mode_lock(_T), __iio_dev_mode_unlock(_T)); 751 + DEFINE_GUARD_COND(__priv__iio_dev_mode_lock, _try_direct, 752 + iio_device_claim_direct(_T)); 753 + 754 + /** 755 + * IIO_DEV_ACQUIRE_DIRECT_MODE() - Tries to acquire the direct mode lock with 756 + * automatic release 757 + * @dev: IIO device instance 758 + * @claim: Variable identifier to store acquire result 759 + * 760 + * Tries to acquire the direct mode lock with cleanup ACQUIRE() semantics and 761 + * automatically releases it at the end of the scope. It most be always paired 762 + * with IIO_DEV_ACQUIRE_ERR(), for example (notice the scope braces):: 763 + * 764 + * switch() { 765 + * case IIO_CHAN_INFO_RAW: { 766 + * IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim); 767 + * if (IIO_DEV_ACQUIRE_FAILED(claim)) 768 + * return -EBUSY; 769 + * 770 + * ... 771 + * } 772 + * case IIO_CHAN_INFO_SCALE: 773 + * ... 774 + * ... 775 + * } 776 + * 777 + * Context: Can sleep 778 + */ 779 + #define IIO_DEV_ACQUIRE_DIRECT_MODE(dev, claim) \ 780 + ACQUIRE(__priv__iio_dev_mode_lock_try_direct, claim)(dev) 781 + 782 + /** 783 + * IIO_DEV_ACQUIRE_FAILED() - ACQUIRE_ERR() wrapper 784 + * @claim: The claim variable passed to IIO_DEV_ACQUIRE_*_MODE() 785 + * 786 + * Return: true if failed to acquire the mode, otherwise false. 787 + */ 788 + #define IIO_DEV_ACQUIRE_FAILED(claim) \ 789 + ACQUIRE_ERR(__priv__iio_dev_mode_lock_try_direct, &(claim)) 790 + 791 + /** 792 + * IIO_DEV_GUARD_CURRENT_MODE() - Acquires the mode lock with automatic release 793 + * @dev: IIO device instance 794 + * 795 + * Acquires the mode lock with cleanup guard() semantics. It is usually paired 796 + * with iio_buffer_enabled(). 797 + * 798 + * This should *not* be used to protect internal driver state and it's use in 799 + * general is *strongly* discouraged. Use any of the IIO_DEV_ACQUIRE_*_MODE() 800 + * variants. 801 + * 802 + * Context: Can sleep 803 + */ 804 + #define IIO_DEV_GUARD_CURRENT_MODE(dev) \ 805 + guard(__priv__iio_dev_mode_lock)(dev) 743 806 744 807 extern const struct bus_type iio_bus_type; 745 808