Skip to contentSkip to conversation

gpiolib: improve coding style for local variables

PATCH Superseded Bartosz Golaszewski 3 participants 13 messages Nov 18, 2021 – Nov 22, 2021 GPIO

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 gpiolib: improve coding style for local variables

Drop unneeded whitespaces and put the variables of the same type together for consistency with the rest of the code.

---
 drivers/gpio/gpiolib.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +3 -4
index abfbf546d159..20d63028b85c 100644
@@ -594,11 +594,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 			       struct lock_class_key *request_key)
 {
 	struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
-	unsigned long	flags;
-	int		ret = 0;
-	unsigned	i;
-	int		base = gc->base;
+	int ret = 0, base = gc->base;
 	struct gpio_device *gdev;
+	unsigned long flags;
+	unsigned int i;
 
 	/*
 	 * First: allocate and populate the internal stat container, and
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 2/2 gpiolib: check the 'ngpios' property in core gpiolib code

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

---
v1 -> v2:
- use device_property_read_u32() instead of fwnode_property_read_u32()
- reverse the error check logic

 drivers/gpio/gpiolib.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +9 -3
index 20d63028b85c..0746615073c6 100644
@@ -598,6 +598,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	struct gpio_device *gdev;
 	unsigned long flags;
 	unsigned int i;
+	u32 ngpios;
 
 	/*
 	 * First: allocate and populate the internal stat container, and
@@ -646,9 +647,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	}
 
 	if (gc->ngpio == 0) {
-		chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-		ret = -EINVAL;
-		goto err_free_descs;
+		ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+		if (ret) {
+			chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+			ret = -EINVAL;
+			goto err_free_descs;
+		}
+
+		gc->ngpio = ngpios;
 	}
 
 	if (gc->ngpio > FASTPATH_NGPIO)
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Thanks for update, my comment below.

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,

Quoted 9 lines
drivers/gpio/gpiolib.c +4 -3
 	if (gc->ngpio == 0) {
-		chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-		ret = -EINVAL;
-		goto err_free_descs;
+		ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+		if (ret) {
+			chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
 
+			ret = -EINVAL;

Sorry, forgot to ask, why this is needed?

Quoted 5 lines
drivers/gpio/gpiolib.c +4 -0
+			goto err_free_descs;
+		}
+
+		gc->ngpio = ngpios;
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 20 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Thanks for update, my comment below.

...

Quoted 7 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +3 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
Quoted 4 lines
Quoted 1 line

+ ret = -EINVAL;

Sorry, forgot to ask, why this is needed?

What do you mean? 0 lines doesn't sound like a valid value so -EINVAL is in order.

Bart

#4 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 22 lines · Bartosz Golaszewski
Quoted 20 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

Thanks for update, my comment below.

...

Quoted 7 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +3 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ ret = -EINVAL;

Sorry, forgot to ask, why this is needed?

What do you mean? 0 lines doesn't sound like a valid value so -EINVAL is in order.

What is so special about -EINVAL? Why ret can't be returned?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Thu, Nov 18, 2021 at 02:23:17PM +0100, Bartosz Golaszewski wrote:

...

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,

Quoted 3 lines
drivers/gpio/gpiolib.c +2 -0
+             if (ret) {
 
+                     ret = -EINVAL;

Sorry, forgot to ask, why this is needed?

What do you mean? 0 lines doesn't sound like a valid value so -EINVAL is in order.

What is so special about -EINVAL? Why ret can't be returned?

See another mail, I explained how the code should be.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

In reply to Bartosz Golaszewski
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 13 lines
drivers/gpio/gpiolib.c +8 -3
 	if (gc->ngpio == 0) {
-		chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-		ret = -EINVAL;
-		goto err_free_descs;
+		ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+		if (ret) {
+			chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+			ret = -EINVAL;
+			goto err_free_descs;
+		}
+
+		gc->ngpio = ngpios;
 	}

This should be

	if (gc->ngpio == 0) {
		ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
		if (ret)
			return ret;

		gc->ngpio = ngpios;
	}

	if (gc->ngpio == 0) {
		chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
		ret = -EINVAL;
		goto err_free_descs;
	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 24 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 13 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +8 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                     ret = -EINVAL;
+                     goto err_free_descs;
+             }
+
+             gc->ngpio = ngpios;
      }
Quoted 6 lines

This should be

        if (gc->ngpio == 0) {
                ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
                if (ret)
                        return ret;

But device_property_read_u32() returning -ENODATA means there's no such property, which should actually be converted to -EINVAL as the caller wanting to create the chip provided invalid configuration - in this case: a chip with 0 lines. In case of the non-array variant of read_u32 that's also the only error that can be returned so this bit looks right to me.

Bart

Quoted 15 lines
                gc->ngpio = ngpios;
        }

        if (gc->ngpio == 0) {
                chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
                ret = -EINVAL;
                goto err_free_descs;
        }

--
With Best Regards,
Andy Shevchenko

#8 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 32 lines · Bartosz Golaszewski
Quoted 23 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 13 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +8 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                     ret = -EINVAL;
+                     goto err_free_descs;
+             }
+
+             gc->ngpio = ngpios;
      }
Quoted 6 lines

This should be

        if (gc->ngpio == 0) {
                ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
                if (ret)
                        return ret;
Quoted 6 lines

But device_property_read_u32() returning -ENODATA means there's no such property, which should actually be converted to -EINVAL as the caller wanting to create the chip provided invalid configuration - in this case: a chip with 0 lines. In case of the non-array variant of read_u32 that's also the only error that can be returned so this bit looks right to me.

So, what is so special about -EINVAL? Why -ENODATA is not good enough which will exactly explain to the caller what's going on, no?

Quoted 7 lines
Quoted 7 lines
                gc->ngpio = ngpios;
        }

        if (gc->ngpio == 0) {
                chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
                ret = -EINVAL;
                goto err_free_descs;

When the caller intended to create a chip with 0 GPIOs they will get an error as you wish with an error message.

Quoted 1 line
Quoted 1 line
        }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 41 lines · Andy Shevchenko
Quoted 32 lines · Bartosz Golaszewski
Quoted 23 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out.

...

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 13 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +8 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                     ret = -EINVAL;
+                     goto err_free_descs;
+             }
+
+             gc->ngpio = ngpios;
      }
Quoted 6 lines

This should be

        if (gc->ngpio == 0) {
                ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
                if (ret)
                        return ret;
Quoted 6 lines

But device_property_read_u32() returning -ENODATA means there's no such property, which should actually be converted to -EINVAL as the caller wanting to create the chip provided invalid configuration - in this case: a chip with 0 lines. In case of the non-array variant of read_u32 that's also the only error that can be returned so this bit looks right to me.

Quoted 3 lines

So, what is so special about -EINVAL? Why -ENODATA is not good enough which will exactly explain to the caller what's going on, no?

Let's imagine the user sets gc->ngpio = 0 incorrectly thinking it'll make gpiolib set it to some sane default. Then gpiochip_add_data() returns -ENODATA (No data available). This is confusing IMO. But if we convert it to -EINVAL, it now says "Invalid value" which points to the wrong configuration.

ENODATA means "device tree property is not present" in this case but the problem is that user supplies the gpiolib with invalid configuration. EINVAL is the right error here.

Bart

Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
                gc->ngpio = ngpios;
        }

        if (gc->ngpio == 0) {
                chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
                ret = -EINVAL;
                goto err_free_descs;

When the caller intended to create a chip with 0 GPIOs they will get an error as you wish with an error message.

Yes, as it was before.

Bart

Quoted 7 lines
Quoted 1 line
Quoted 1 line
        }

--
With Best Regards,
Andy Shevchenko

#10 lore raw

Re: [PATCH v2 2/2] gpiolib: check the 'ngpios' property in core gpiolib code

Quoted 6 lines · Bartosz Golaszewski
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Thu, Nov 18, 2021 at 02:23:17PM +0100, Bartosz Golaszewski wrote:

...

Quoted 38 lines
Quoted 28 lines
Quoted 21 lines
Quoted 14 lines

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 658

Quoted 13 lines
      if (gc->ngpio == 0) {
drivers/gpio/gpiolib.c +8 -3
-             chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
-             ret = -EINVAL;
-             goto err_free_descs;
+             ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
+             if (ret) {
+                     chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
+                     ret = -EINVAL;
+                     goto err_free_descs;
+             }
+
+             gc->ngpio = ngpios;
      }
Quoted 6 lines

This should be

        if (gc->ngpio == 0) {
                ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
                if (ret)
                        return ret;
Quoted 6 lines

But device_property_read_u32() returning -ENODATA means there's no such property, which should actually be converted to -EINVAL as the caller wanting to create the chip provided invalid configuration - in this case: a chip with 0 lines. In case of the non-array variant of read_u32 that's also the only error that can be returned so this bit looks right to me.

Quoted 3 lines

So, what is so special about -EINVAL? Why -ENODATA is not good enough which will exactly explain to the caller what's going on, no?

Let's imagine the user sets gc->ngpio = 0 incorrectly thinking it'll make gpiolib set it to some sane default. Then gpiochip_add_data() returns -ENODATA (No data available). This is confusing IMO. But if we convert it to -EINVAL, it now says "Invalid value" which points to the wrong configuration.

Quoted 3 lines

ENODATA means "device tree property is not present" in this case but the problem is that user supplies the gpiolib with invalid configuration. EINVAL is the right error here.

Then be explicit, don't shadow other error codes from fwnode API.

	if (ret && ret != -ENODATA)
Quoted 12 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
                gc->ngpio = ngpios;
        }

        if (gc->ngpio == 0) {
                chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
                ret = -EINVAL;
                goto err_free_descs;

When the caller intended to create a chip with 0 GPIOs they will get an error as you wish with an error message.

Yes, as it was before.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 1/2
#11 lore raw
In reply to Bartosz Golaszewski
Quoted 2 lines · Bartosz Golaszewski

Drop unneeded whitespaces and put the variables of the same type together for consistency with the rest of the code.

Quoted 28 lines
---
 drivers/gpio/gpiolib.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +3 -4
index abfbf546d159..20d63028b85c 100644
@@ -594,11 +594,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 			       struct lock_class_key *request_key)
 {
 	struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
-	unsigned long	flags;
-	int		ret = 0;
-	unsigned	i;
-	int		base = gc->base;
+	int ret = 0, base = gc->base;
 	struct gpio_device *gdev;
+	unsigned long flags;
+	unsigned int i;
 
 	/*
 	 * First: allocate and populate the internal stat container, and
Signature · 1 line
2.25.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw
In reply to Bartosz Golaszewski
Quoted 14 lines · Bartosz Golaszewski

Drop unneeded whitespaces and put the variables of the same type together for consistency with the rest of the code.

---
 drivers/gpio/gpiolib.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib.c +0 -0
index abfbf546d159..20d63028b85c 100644

drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 597

Quoted 8 lines
drivers/gpio/gpiolib.c +1 -4
@@ -594,11 +594,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 			       struct lock_class_key *request_key)
 {
 	struct fwnode_handle *fwnode = gc->parent ? dev_fwnode(gc->parent) : NULL;
-	unsigned long	flags;
-	int		ret = 0;
-	unsigned	i;
-	int		base = gc->base;
+	int ret = 0, base = gc->base;

This only makes the code harder to read for no good reason.

Keep declarations on separate lines, especially if also initialising.

Note that all but one function in this file initialises return value variables on their own lines too (as they should).

Quoted 7 lines
drivers/gpio/gpiolib.c +2 -0
 	struct gpio_device *gdev;
+	unsigned long flags;
+	unsigned int i;
 
 
 	/*

* First: allocate and populate the internal stat container, and

Johan