Skip to contentSkip to conversation

gpiolib: use better errno if get_direction is not available

PATCH In review Wolfram Sang 5 participants 11 messages Jul 11, 2018 – Sep 14, 2018 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: use better errno if get_direction is not available

EINVAL is very generic, use ENOTSUPP in case the gpiochip does not provide this function. While removing the assignment from the 'status' variable, use better indentation in the declaration block.

---

I got puzzled by the EINVAL until I found out that gpio-rcar simply does not implement it.

@Geert: any reason gpio-rcar is missing it? I would need it for the i2c-gpio-fault-injector.

 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
index e11a3bb03820..18719f64e80b 100644
@@ -210,15 +210,15 @@ static int gpiochip_find_base(int ngpio)
  */
 int gpiod_get_direction(struct gpio_desc *desc)
 {
-	struct gpio_chip	*chip;
-	unsigned		offset;
-	int			status = -EINVAL;
+	struct gpio_chip *chip;
+	unsigned offset;
+	int status;
 
 	chip = gpiod_to_chip(desc);
 	offset = gpio_chip_hwgpio(desc);
 
 	if (!chip->get_direction)
-		return status;
+		return -ENOTSUPP;
 
 	status = chip->get_direction(chip, offset);
 	if (status > 0) {
Signature · 1 line
2.11.0
#1 lore raw

Hi Wolfram,

Quoted 2 lines · Wolfram Sang

@Geert: any reason gpio-rcar is missing it? I would need it for the i2c-gpio-fault-injector.

Because so far no one had a need for it?
/me thought gpiolib would return a cached value from a previous set_direction_{in,out}put(), but apparently that must have been a dream...

Do you want me to implement it?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw
Quoted 2 lines

Do you want me to implement it?

Yes, please.

Thanks a lot!

#3 lore raw
In reply to Wolfram Sang
Quoted 5 lines · Wolfram Sang

EINVAL is very generic, use ENOTSUPP in case the gpiochip does not provide this function. While removing the assignment from the 'status' variable, use better indentation in the declaration block.

So, Geert implemented get_direction for gpio-rcar, but this doesn't help my case sadly. Because I2C is open drain, get_direction returns 'input' :( As mentioned in a previous discussion, returning '0' and '1' from get_direction() is confusing. But as it looks now, it also is not enough. Or we'd need another function from which I could determine the OUT_OPEN_DRAIN state.

For the short term, I will remove the sanity check from the I2C core and hope the user properly set up the GPIO. It would be nice to check for it somewhen in the future, though.

That all being said, I think this patch is still useful as is.

Thanks,

Wolfram

Quoted 38 lines

---

I got puzzled by the EINVAL until I found out that gpio-rcar simply does not implement it.

@Geert: any reason gpio-rcar is missing it? I would need it for the i2c-gpio-fault-injector.

 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
index e11a3bb03820..18719f64e80b 100644
@@ -210,15 +210,15 @@ static int gpiochip_find_base(int ngpio)
  */
 int gpiod_get_direction(struct gpio_desc *desc)
 {
-	struct gpio_chip	*chip;
-	unsigned		offset;
-	int			status = -EINVAL;
+	struct gpio_chip *chip;
+	unsigned offset;
+	int status;
 
 	chip = gpiod_to_chip(desc);
 	offset = gpio_chip_hwgpio(desc);
 
 	if (!chip->get_direction)
-		return status;
+		return -ENOTSUPP;
 
 	status = chip->get_direction(chip, offset);
 	if (status > 0) {
Signature · 1 line
2.11.0
Reviewed-by
#6 lore raw
Quoted 3 lines · Wolfram Sang
Quoted 1 line

That all being said, I think this patch is still useful as is.

Linus, do you have time to comment on this?

This looks like a good solution to me.

Yours,
Linus Walleij

#7 lore raw

Hi Linus,

Quoted 7 lines · Linus Walleij
Quoted 3 lines · Wolfram Sang
Quoted 1 line

That all being said, I think this patch is still useful as is.

Linus, do you have time to comment on this?

This looks like a good solution to me.

Any plans to apply it? drivers/gpio/gpiolib.c is your territory, I believe ;-)

Thanks!

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#8 lore raw
Quoted 9 lines · Geert Uytterhoeven
Quoted 6 lines · Wolfram Sang
Quoted 3 lines
Quoted 1 line

That all being said, I think this patch is still useful as is.

Linus, do you have time to comment on this?

This looks like a good solution to me.

Any plans to apply it? drivers/gpio/gpiolib.c is your territory, I believe ;-)

Ooops sorry.

Patch applied.

Yours,
Linus Walleij

#9 lore raw
Quoted 6 lines
Quoted 2 lines

Any plans to apply it? drivers/gpio/gpiolib.c is your territory, I believe ;-)

Ooops sorry.

Patch applied.

Thanks Geert & Linus!

Reviewed-by on v1 1/1
#10 lore raw
In reply to Wolfram Sang
Quoted 5 lines · Wolfram Sang

EINVAL is very generic, use ENOTSUPP in case the gpiochip does not provide this function. While removing the assignment from the 'status' variable, use better indentation in the declaration block.

Quoted 38 lines

---

I got puzzled by the EINVAL until I found out that gpio-rcar simply does not implement it.

@Geert: any reason gpio-rcar is missing it? I would need it for the i2c-gpio-fault-injector.

 drivers/gpio/gpiolib.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +4 -4
index e11a3bb03820..18719f64e80b 100644
@@ -210,15 +210,15 @@ static int gpiochip_find_base(int ngpio)
  */
 int gpiod_get_direction(struct gpio_desc *desc)
 {
-	struct gpio_chip	*chip;
-	unsigned		offset;
-	int			status = -EINVAL;
+	struct gpio_chip *chip;
+	unsigned offset;
+	int status;
 
 	chip = gpiod_to_chip(desc);
 	offset = gpio_chip_hwgpio(desc);
 
 	if (!chip->get_direction)
-		return status;
+		return -ENOTSUPP;
 
 	status = chip->get_direction(chip, offset);
 	if (status > 0) {
Signature · 1 line
2.11.0