gpiolib: safer implementation of desc_to_gpio()
PATCH 1/2 gpiolib: safer implementation of desc_to_gpio()
The current implementation of desc_to_gpio() relies on the chip pointer to be set to a valid value in order to compute the GPIO number. This was done in the hope that we can get rid of the gpio_desc global array, but this is not happening anytime soon.
This patch reimplements desc_to_gpio() in a fashion similar to that of gpio_to_desc(). As a result, desc_to_gpio(gpio_to_desc(gpio)) == gpio is now always true. This allows to call desc_to_gpio() on non-initialized descriptors as some error-handling code currently does.
--- drivers/gpio/gpiolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpiolib.c +1 -1
@@ -136,7 +136,7 @@ static struct gpio_desc *gpio_to_desc(unsigned gpio)*/ static int desc_to_gpio (const struct gpio_desc *desc) { -return desc->chip->base +gpio_chip_hwgpio (desc); +return desc - &gpio_desc[0 ]; }
Signature · 1 line
1.8.4
PATCH 2/2 gpiolib: let gpiod_request() return -EPROBE_DEFER
Patch be1a4b brought some improvements to the GPIO error handling code, but also changed the return value of gpiod_request() when called on a not yet initialized GPIO descriptor: it now returns -EINVAL instead of -EPROBE_DEFER, and this affects some drivers.
This patch restores the original behavior for gpiod_request(). It is safe to do so now that desc_to_gpio() does not rely on the GPIO descriptor to be initialized. Other functions changed by patch be1a4b do not see their return value affected, so these are not reverted.
--- drivers/gpio/gpiolib.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +3 -1
@@ -1398,7 +1398,7 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)int status = -EPROBE_DEFER ;unsigned long flags; -if (!desc || !desc->chip) { +if (!desc) {pr_warn ("%s: invalid GPIO\n" , __func__);return -EINVAL ; }
@@ -1406,6 +1406,8 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)spin_lock_irqsave (&gpio_lock, flags); chip = desc->chip; +if (chip ==NULL ) + goto done;if (!try_module_get (chip->owner)) goto done;
Signature · 1 line
1.8.4
Re: [PATCH 2/2] gpiolib: let gpiod_request() return -EPROBE_DEFER
Quoted 5 lines · Alexandre Courbot
Patch be1a4b brought some improvements to the GPIO error handling code, but also changed the return value of
gpiod_request()when called on a not yet initialized GPIO descriptor: it now returns-EINVALinstead of -EPROBE_DEFER, and this affects some drivers.
Quoted 7 lines
This patch restores the original behavior for
gpiod_request(). It is safe to do so now thatdesc_to_gpio()does not rely on the GPIO descriptor to be initialized. Other functions changed by patch be1a4b do not see their return value affected, so these are not reverted.Signed-off-by Alexandre Courbot <[email protected]>Reported-by Dr. H. Nikolaus Schaller <[email protected]>
Patch applied.
Are these two patches regressions that need to go into fixes for v3.12?
Yours,
Linus Walleij
Quoted 5 lines · Alexandre Courbot
The current implementation of
desc_to_gpio()relies on the chip pointer to be set to a valid value in order to compute the GPIO number. This was done in the hope that we can get rid of thegpio_descglobal array, but this is not happening anytime soon.
Quoted 7 lines
This patch reimplements
desc_to_gpio()in a fashion similar to that ofgpio_to_desc(). As a result, desc_to_gpio(gpio_to_desc(gpio)) == gpio is now always true. This allows to calldesc_to_gpio()on non-initialized descriptors as some error-handling code currently does.Signed-off-by Alexandre Courbot <[email protected]>Reported-by Dr. H. Nikolaus Schaller <[email protected]>
Patch applied.
Yours,
Linus Walleij