2013-10-04 18:00:15

by Alexandre Courbot

[permalink] [raw]
Subject: [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.

Signed-off-by: Alexandre Courbot <[email protected]>
Reported-by: Dr. H. Nikolaus Schaller <[email protected]>
---
drivers/gpio/gpiolib.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 86ef346..f330631 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -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];
}


--
1.8.4


2013-10-04 18:00:17

by Alexandre Courbot

[permalink] [raw]
Subject: [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.

Signed-off-by: Alexandre Courbot <[email protected]>
Reported-by: Dr. H. Nikolaus Schaller <[email protected]>
---
drivers/gpio/gpiolib.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index f330631..0dee0e0 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -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;
--
1.8.4

2013-10-11 14:25:17

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 1/2] gpiolib: safer implementation of desc_to_gpio()

On Fri, Oct 4, 2013 at 7:59 PM, Alexandre Courbot <[email protected]> wrote:

> 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.
>
> Signed-off-by: Alexandre Courbot <[email protected]>
> Reported-by: Dr. H. Nikolaus Schaller <[email protected]>

Patch applied.

Yours,
Linus Walleij

2013-10-11 14:28:08

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH 2/2] gpiolib: let gpiod_request() return -EPROBE_DEFER

On Fri, Oct 4, 2013 at 7:59 PM, Alexandre Courbot <[email protected]> wrote:

> 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.
>
> 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