Skip to contentSkip to conversation

gpio: sysfs: repair export returning -EPERM on 1st attempt

PATCH In review Alexander Sverdlin 2 participants 2 messages Mar 7, 2024 – Mar 8, 2024 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: sysfs: repair export returning -EPERM on 1st attempt

It would make sense to return -EPERM if the bit was already set (already used), not if it was cleared. Before this fix pins can only be exported on the 2nd attempt:

$ echo 522 > /sys/class/gpio/export 
sh: write error: Operation not permitted
$ echo 522 > /sys/class/gpio/export 
Fixes 35b545332b80 ("gpio: remove gpio_lock")
---
 drivers/gpio/gpiolib-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-sysfs.c +1 -1
index 67fc09a57f26..6853ecd98bcb 100644
@@ -593,7 +593,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	if (!guard.gc)
 		return -ENODEV;
 
-	if (!test_and_set_bit(FLAG_EXPORT, &desc->flags))
+	if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
 		return -EPERM;
 
 	gdev = desc->gdev;
Signature · 1 line
2.43.2
#1 lore raw
Quoted 29 lines · Alexander Sverdlin

It would make sense to return -EPERM if the bit was already set (already used), not if it was cleared. Before this fix pins can only be exported on the 2nd attempt:

$ echo 522 > /sys/class/gpio/export
sh: write error: Operation not permitted
$ echo 522 > /sys/class/gpio/export
Fixes 35b545332b80 ("gpio: remove gpio_lock")
---
 drivers/gpio/gpiolib-sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib-sysfs.c +1 -2
index 67fc09a57f26..6853ecd98bcb 100644
@@ -593,7 +593,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
        if (!guard.gc)
                return -ENODEV;
 
-       if (!test_and_set_bit(FLAG_EXPORT, &desc->flags))
+       if (test_and_set_bit(FLAG_EXPORT, &desc->flags))
                return -EPERM;
 
        gdev = desc->gdev;
--
Quoted 2 lines

2.43.2

That's of course correct. Applied.

Bart