Skip to content

Commit 265daff

Browse files
author
Bartosz Golaszewski
committed
gpio: provide gpiod_is_equal()
There are users in the kernel that directly compare raw GPIO descriptor pointers in order to determine whether they refer to the same physical GPIO pin. This accidentally works like this but is not guaranteed by any API contract. Let's provide a comparator function that hides the actual logic. Reviewed-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 0af2f6b commit 265daff

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ struct gpio_device *gpiod_to_gpio_device(struct gpio_desc *desc)
265265
}
266266
EXPORT_SYMBOL_GPL(gpiod_to_gpio_device);
267267

268+
/**
269+
* gpiod_is_equal() - Check if two GPIO descriptors refer to the same pin.
270+
* @desc: Descriptor to compare.
271+
* @other: The second descriptor to compare against.
272+
*
273+
* Returns:
274+
* True if the descriptors refer to the same physical pin. False otherwise.
275+
*/
276+
bool gpiod_is_equal(struct gpio_desc *desc, struct gpio_desc *other)
277+
{
278+
return desc == other;
279+
}
280+
EXPORT_SYMBOL_GPL(gpiod_is_equal);
281+
268282
/**
269283
* gpio_device_get_base() - Get the base GPIO number allocated by this device
270284
* @gdev: GPIO device

include/linux/gpio/consumer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
180180
enum gpiod_flags flags,
181181
const char *label);
182182

183+
bool gpiod_is_equal(struct gpio_desc *desc, struct gpio_desc *other);
184+
183185
#else /* CONFIG_GPIOLIB */
184186

185187
#include <linux/bug.h>
@@ -547,6 +549,13 @@ struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
547549
return ERR_PTR(-ENOSYS);
548550
}
549551

552+
static inline bool
553+
gpiod_is_equal(struct gpio_desc *desc, struct gpio_desc *other)
554+
{
555+
WARN_ON(desc || other);
556+
return false;
557+
}
558+
550559
#endif /* CONFIG_GPIOLIB */
551560

552561
#if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_HTE)

0 commit comments

Comments
 (0)