Skip to contentSkip to conversation

gpiolib: Unify expectations about ->request() returned value

PATCH Applied Andy Shevchenko 2 participants 4 messages Oct 21, 2020 – Oct 26, 2020 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpiolib: Unify expectations about ->request() returned value

Half of the code in the GPIO library is written in an expectation that any non-zero value returned from the ->request() callback is an error code, while some code checks only for negative values.

Unify expectations about ->request() returned value to be non-zero for an error and 0 for the success.

---
 drivers/gpio/gpiolib-sysfs.c | 2 +-
 drivers/gpio/gpiolib.c       | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-sysfs.c +1 -1
index fe580d6b8d0d..f3b886e2132c 100644
@@ -476,7 +476,7 @@ static ssize_t export_store(struct class *class,
 	 */
 
 	status = gpiod_request(desc, "sysfs");
-	if (status < 0) {
+	if (status) {
 		if (status == -EPROBE_DEFER)
 			status = -ENODEV;
 		goto done;
drivers/gpio/gpiolib.c +3 -3
index 4df1f1a23a8b..0e6dddce207d 100644
@@ -1985,7 +1985,7 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
 			ret = -EINVAL;
 		spin_lock_irqsave(&gpio_lock, flags);
 
-		if (ret < 0) {
+		if (ret) {
 			desc_set_label(desc, NULL);
 			kfree_const(label);
 			clear_bit(FLAG_REQUESTED, &desc->flags);
@@ -2051,7 +2051,7 @@ int gpiod_request(struct gpio_desc *desc, const char *label)
 
 	if (try_module_get(gdev->owner)) {
 		ret = gpiod_request_commit(desc, label);
-		if (ret < 0)
+		if (ret)
 			module_put(gdev->owner);
 		else
 			get_device(&gdev->dev);
@@ -3968,7 +3968,7 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
 	 * the device name as label
 	 */
 	ret = gpiod_request(desc, con_id ? con_id : devname);
-	if (ret < 0) {
+	if (ret) {
 		if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
 			/*
 			 * This happens when there are several consumers for
Signature · 1 line
2.28.0
#1 lore raw

PATCH 2/2 gpiolib: split error path in gpiod_request_commit()

For better maintenance and micro optimization split error path in the gpiod_request_commit().

---
 drivers/gpio/gpiolib.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpio/gpiolib.c +7 -6
index 0e6dddce207d..43ddcf454b5f 100644
@@ -1968,11 +1968,9 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
 
 	if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
 		desc_set_label(desc, label ? : "?");
-		ret = 0;
 	} else {
-		kfree_const(label);
 		ret = -EBUSY;
-		goto done;
+		goto out_free_unlock;
 	}
 
 	if (gc->request) {
@@ -1987,9 +1985,8 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
 
 		if (ret) {
 			desc_set_label(desc, NULL);
-			kfree_const(label);
 			clear_bit(FLAG_REQUESTED, &desc->flags);
-			goto done;
+			goto out_free_unlock;
 		}
 	}
 	if (gc->get_direction) {
@@ -1998,8 +1995,12 @@ static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
 		gpiod_get_direction(desc);
 		spin_lock_irqsave(&gpio_lock, flags);
 	}
-done:
 	spin_unlock_irqrestore(&gpio_lock, flags);
+	return 0;
+
+out_free_unlock:
+	spin_unlock_irqrestore(&gpio_lock, flags);
+	kfree_const(label);
 	return ret;
 }
 
Signature · 1 line
2.28.0
#2 lore raw

Re: [PATCH v1 2/2] gpiolib: split error path in gpiod_request_commit()

Quoted 7 lines · Andy Shevchenko

For better maintenance and micro optimization split error path in the gpiod_request_commit().

---

Applied, thanks!

Bartosz

#3 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

Half of the code in the GPIO library is written in an expectation that any non-zero value returned from the ->request() callback is an error code, while some code checks only for negative values.

Unify expectations about ->request() returned value to be non-zero for an error and 0 for the success.

---

Applied, thanks!

Bartosz