gpiolib: A fix and a few cleanups
v3 cover gpiolib: A fix and a few cleanups
Patch 1 is a fix for wrong error code to user space. Patches 2-4 are small cleanups.
Can be routed via my tree, or directly into GPIO, whatever maintainers prefer.
Changelog v3:
- joined two patches into a single fix (Bart)
- added Rb tag (Linus)
- renamed for_each_gpio_desc_with_flag() macro to be descriptive (Johan)
- added two new cleanups
Andy Shevchenko (4):
gpiolib: Never return internal error codes to user space
gpiolib: Introduce for_each_gpio_desc_with_flag() macro
gpiolib: Use short form of ternary operator in gpiod_get_index()
gpiolib: Simplify error path in gpiod_get_index() when requesting GPIO
drivers/gpio/gpiolib-cdev.c | 6 +++--- drivers/gpio/gpiolib-of.c | 10 ++++------ drivers/gpio/gpiolib-sysfs.c | 14 ++++---------- drivers/gpio/gpiolib.c | 35 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+), 38 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/4 gpiolib: Never return internal error codes to user space
Currently it's possible that character device interface may return the error codes which are not supposed to be seen by user space. In this case it's EPROBE_DEFER.
Wrap it to return -ENODEV instead as sysfs does.
--- drivers/gpio/gpiolib-cdev.c | 6 +++--- drivers/gpio/gpiolib-sysfs.c | 7 ++----- drivers/gpio/gpiolib.h | 12 ++++++++++++ 3 files changed, 17 insertions(+), 8 deletions(-)
drivers/gpio/gpiolib-cdev.c +3 -3
@@ -330,7 +330,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) goto out_free_lh; } - ret =gpiod_request (desc, lh->label); + ret =gpiod_request_user (desc, lh->label);if (ret) goto out_free_lh; lh->descs[i] = desc;
@@ -1378,7 +1378,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) goto out_free_linereq; } - ret =gpiod_request (desc, lr->label); + ret =gpiod_request_user (desc, lr->label);if (ret) goto out_free_linereq;
@@ -1764,7 +1764,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) } } - ret =gpiod_request (desc, le->label); + ret =gpiod_request_user (desc, le->label);if (ret) goto out_free_le; le->desc = desc;
drivers/gpio/gpiolib-sysfs.c +2 -5
@@ -475,12 +475,9 @@ static ssize_t export_store(struct class *class,* they may be undone on its behalf too. */ - status =gpiod_request (desc,"sysfs" ); -if (status) { -if (status == -EPROBE_DEFER ) - status = -ENODEV ; + status =gpiod_request_user (desc,"sysfs" ); +if (status) goto done; - } status =gpiod_set_transitory (desc, false);if (!status) {
drivers/gpio/gpiolib.h +12 -0
@@ -135,6 +135,18 @@ struct gpio_desc {int gpiod_request (struct gpio_desc *desc,const char *label);void gpiod_free (struct gpio_desc *desc); + +static inline int gpiod_request_user (struct gpio_desc *desc,const char *label) +{ +int ret; + + ret =gpiod_request (desc, label); +if (ret == -EPROBE_DEFER ) + ret = -ENODEV ; + +return ret; +} +int gpiod_configure_flags (struct gpio_desc *desc,const char *con_id,unsigned long lflags,enum gpiod_flags dflags);int gpio_set_debounce_timeout (struct gpio_desc *desc,unsigned int debounce);
Signature · 1 line
2.34.1
Bartosz Golaszewski
AppliedRe: [PATCH v3 1/4] gpiolib: Never return internal error codes to user space
Quoted 92 lines · Andy Shevchenko
Currently it's possible that character device interface may return the error codes which are not supposed to be seen by user space. In this case it's
EPROBE_DEFER.Wrap it to return
-ENODEVinstead as sysfs does.Fixes d7c51b47ac11 ("gpio: userspace ABI for reading/writing GPIO lines")Fixes 61f922db7221 ("gpio: userspace ABI for reading GPIO line events")Fixes 3c0d9c635ae2 ("gpiolib: cdev: support GPIO_V2_GET_LINE_IOCTL and GPIO_V2_LINE_GET_VALUES_IOCTL")Reported-by Suresh Balakrishnan <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>--- drivers/gpio/gpiolib-cdev.c | 6 +++--- drivers/gpio/gpiolib-sysfs.c | 7 ++----- drivers/gpio/gpiolib.h | 12 ++++++++++++ 3 files changed, 17 insertions(+), 8 deletions(-)drivers/gpio/gpiolib-cdev.c +3
-3@@ -330,7 +330,7 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip) goto out_free_lh; } - ret =gpiod_request (desc, lh->label); + ret =gpiod_request_user (desc, lh->label);if (ret) goto out_free_lh; lh->descs[i] = desc;@@ -1378,7 +1378,7 @@ static int linereq_create(struct gpio_device *gdev, void __user *ip) goto out_free_linereq; } - ret =gpiod_request (desc, lr->label); + ret =gpiod_request_user (desc, lr->label);if (ret) goto out_free_linereq;@@ -1764,7 +1764,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) } } - ret =gpiod_request (desc, le->label); + ret =gpiod_request_user (desc, le->label);if (ret) goto out_free_le; le->desc = desc;drivers/gpio/gpiolib-sysfs.c +2
-5@@ -475,12 +475,9 @@ static ssize_t export_store(struct class *class,* they may be undone on its behalf too. */ - status =gpiod_request (desc,"sysfs" ); -if (status) { -if (status == -EPROBE_DEFER ) - status = -ENODEV ; + status =gpiod_request_user (desc,"sysfs" ); +if (status) goto done; - } status =gpiod_set_transitory (desc, false);if (!status) {drivers/gpio/gpiolib.h +12
-1@@ -135,6 +135,18 @@ struct gpio_desc {int gpiod_request (struct gpio_desc *desc,const char *label);void gpiod_free (struct gpio_desc *desc); + +static inline int gpiod_request_user (struct gpio_desc *desc,const char *label) +{ +int ret; + + ret =gpiod_request (desc, label); +if (ret == -EPROBE_DEFER ) + ret = -ENODEV ; + +return ret; +} +int gpiod_configure_flags (struct gpio_desc *desc,const char *con_id,unsigned long lflags,enum gpiod_flags dflags);int gpio_set_debounce_timeout (struct gpio_desc *desc,unsigned int debounce); --
Quoted 2 lines
2.34.1
Queued for fixes, thanks!
Bart
PATCH v3 2/4 gpiolib: Introduce for_each_gpio_desc_with_flag() macro
In reply to Andy ShevchenkoIn a few places we are using a loop against all GPIO descriptors with a given flag for a given device. Replace it with a consolidated for_each type of macro.
--- drivers/gpio/gpiolib-of.c | 10 ++++------ drivers/gpio/gpiolib-sysfs.c | 7 ++----- drivers/gpio/gpiolib.c | 7 +++---- drivers/gpio/gpiolib.h | 7 +++++++ 4 files changed, 16 insertions(+), 15 deletions(-)
drivers/gpio/gpiolib-of.c +4 -6
@@ -711,14 +711,12 @@ static int of_gpiochip_scan_gpios(struct gpio_chip *chip)static void of_gpiochip_remove_hog (struct gpio_chip *chip,struct device_node *hog) { -struct gpio_desc *descs = chip->gpiodev->descs; +struct gpio_desc *desc;unsigned int i; -for (i =0 ; i < chip->ngpio; i++) { -if (test_bit (FLAG_IS_HOGGED , &descs[i].flags) && - descs[i].hog == hog) -gpiochip_free_own_desc (&descs[i]); - } +for_each_gpio_desc_with_flag (i, chip, desc,FLAG_IS_HOGGED ) +if (desc->hog == hog) +gpiochip_free_own_desc (desc); }static int of_gpiochip_match_node (struct gpio_chip *chip,void *data)
drivers/gpio/gpiolib-sysfs.c +2 -5
@@ -790,11 +790,8 @@ void gpiochip_sysfs_unregister(struct gpio_device *gdev)mutex_unlock (&sysfs_lock);/* unregister gpiod class devices owned by sysfs */ -for (i =0 ; i < chip->ngpio; i++) { - desc = &gdev->descs[i]; -if (test_and_clear_bit (FLAG_SYSFS , &desc->flags)) -gpiod_free (desc); - } +for_each_gpio_desc_with_flag (i, chip, desc,FLAG_SYSFS ) +gpiod_free (desc); }static int __init gpiolib_sysfs_init (void )
drivers/gpio/gpiolib.c +3 -4
@@ -4102,12 +4102,11 @@ int gpiod_hog(struct gpio_desc *desc, const char *name,*/ static void gpiochip_free_hogs (struct gpio_chip *gc) { +struct gpio_desc *desc;int id; -for (id =0 ; id < gc->ngpio; id++) { -if (test_bit (FLAG_IS_HOGGED , &gc->gpiodev->descs[id].flags)) -gpiochip_free_own_desc (&gc->gpiodev->descs[id]); - } +for_each_gpio_desc_with_flag (id, gc, desc,FLAG_IS_HOGGED ) +gpiochip_free_own_desc (desc); }/**
drivers/gpio/gpiolib.h +7 -0
@@ -82,6 +82,13 @@ struct gpio_array { };struct gpio_desc *gpiochip_get_desc (struct gpio_chip *gc,unsigned int hwnum); + +#define for_each_gpio_desc_with_flag (i, gc, desc, flag) \ + for (i = 0, desc = gpiochip_get_desc(gc, i); \ + i < gc->ngpio; \ + i++, desc = gpiochip_get_desc(gc, i)) \ + if (!test_bit(flag, &desc->flags)) {} else + int gpiod_get_array_value_complex(bool raw, bool can_sleep, unsigned int array_size, struct gpio_desc **desc_array,
Signature · 1 line
2.34.1
PATCH v3 3/4 gpiolib: Use short form of ternary operator in gpiod_get_index()
In reply to Andy ShevchenkoInstead of repeating first argument for true branch, use short form of the ternary operator, i.e. ?:.
While at it, fix a typo in the comment.
--- drivers/gpio/gpiolib.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +3 -4
@@ -3931,19 +3931,18 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,* If a connection label was passed use that, else attempt to use * the device name as label */ - ret =gpiod_request (desc, con_id ? con_id : devname); + ret =gpiod_request (desc, con_id ?: devname);if (ret) {if (ret == -EBUSY && flags &GPIOD_FLAGS_BIT_NONEXCLUSIVE ) {/* * This happens when there are several consumers for * the same GPIO line: we just return here without -* further initialization. It is a bit if a hack. +* further initialization. It is a bit of a hack. * This is necessary to support fixed regulators. * * FIXME: Make this more sane and safe. */ -dev_info (dev,"nonexclusive access to GPIO for %s\n" , - con_id ? con_id : devname); +dev_info (dev,"nonexclusive access to GPIO for %s\n" , con_id ?: devname);return desc; }else {return ERR_PTR (ret);
Signature · 1 line
2.34.1
PATCH v3 4/4 gpiolib: Simplify error path in gpiod_get_index() when requesting GPIO
In reply to Andy ShevchenkoSimplify error path in the gpiod_get_index() when requesting a GPIO line by:
- checking for error condition first
- dropping redundant 'else'
As a result, decrease the indentation level for better readability.
--- drivers/gpio/gpiolib.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/gpio/gpiolib.c +12 -13
@@ -3933,20 +3933,19 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,*/ ret =gpiod_request (desc, con_id ?: devname);if (ret) { -if (ret == -EBUSY && flags &GPIOD_FLAGS_BIT_NONEXCLUSIVE ) { -/* -* This happens when there are several consumers for -* the same GPIO line: we just return here without -* further initialization. It is a bit of a hack. -* This is necessary to support fixed regulators. -* -* FIXME: Make this more sane and safe. -*/ -dev_info (dev,"nonexclusive access to GPIO for %s\n" , con_id ?: devname); -return desc; - }else { +if (!(ret == -EBUSY && flags &GPIOD_FLAGS_BIT_NONEXCLUSIVE ))return ERR_PTR (ret); - } + +/* +* This happens when there are several consumers for +* the same GPIO line: we just return here without +* further initialization. It is a bit of a hack. +* This is necessary to support fixed regulators. +* +* FIXME: Make this more sane and safe. +*/ +dev_info (dev,"nonexclusive access to GPIO for %s\n" , con_id ?: devname); +return desc; } ret =gpiod_configure_flags (desc, con_id, lookupflags, flags);
Signature · 1 line
2.34.1
Quoted 5 lines · Andy Shevchenko
Patch 1 is a fix for wrong error code to user space. Patches 2-4 are small cleanups.
Can be routed via my tree, or directly into GPIO, whatever maintainers prefer.
Bart, if the series is okay for you tell me which route to take?
Quoted 18 lines
Changelog v3:
- joined two patches into a single fix (Bart)
- added Rb tag (Linus)
- renamedfor_each_gpio_desc_with_flag()macro to be descriptive (Johan)
- added two new cleanupsAndy Shevchenko (4):
gpiolib: Never return internal error codes to user space
gpiolib: Introducefor_each_gpio_desc_with_flag()macro
gpiolib: Use short form of ternary operator ingpiod_get_index()
gpiolib: Simplify error path ingpiod_get_index()when requesting GPIOdrivers/gpio/gpiolib-cdev.c | 6 +++--- drivers/gpio/gpiolib-of.c | 10 ++++------ drivers/gpio/gpiolib-sysfs.c | 14 ++++---------- drivers/gpio/gpiolib.c | 35 ++++++++++++++++------------------- drivers/gpio/gpiolib.h | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+), 38 deletions(-)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Patch 1 is a fix for wrong error code to user space. Patches 2-4 are small cleanups.
Can be routed via my tree, or directly into GPIO, whatever maintainers prefer.
Bart, if the series is okay for you tell me which route to take?
Queued the first one for fixes and 2-4 for v5.18.
Bart
Quoted 6 lines · Bartosz Golaszewski
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko
Patch 1 is a fix for wrong error code to user space. Patches 2-4 are small cleanups.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Can be routed via my tree, or directly into GPIO, whatever maintainers prefer.
Bart, if the series is okay for you tell me which route to take?
Queued the first one for fixes and 2-4 for v5.18.
Thank you!
Signature · 2 lines
With Best Regards, Andy Shevchenko