Skip to contentSkip to conversation

leds: trigger: gpio: GPIO 0 is valid

PATCH Superseded Andy Shevchenko 2 participants 7 messages Aug 21, 2019 – Aug 30, 2019 LED

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 leds: trigger: gpio: GPIO 0 is valid

Allow all valid GPIOs to be used in the driver.

Fixes 17354bfe8527 ("leds: Add gpio-led trigger")
---
 drivers/leds/trigger/ledtrig-gpio.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +5 -5
index 33cc99a1a16a..31f456dd4417 100644
@@ -131,10 +131,10 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	if (gpio_data->gpio == gpio)
 		return n;
 
-	if (!gpio) {
-		if (gpio_data->gpio != 0)
+	if (!gpio_is_valid(gpio)) {
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
-		gpio_data->gpio = 0;
+		gpio_data->gpio = gpio;
 		return n;
 	}
 
@@ -144,7 +144,7 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	if (ret) {
 		dev_err(dev, "request_irq failed with error %d\n", ret);
 	} else {
-		if (gpio_data->gpio != 0)
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
 		gpio_data->gpio = gpio;
 		/* After changing the GPIO, we need to update the LED. */
@@ -181,7 +181,7 @@ static void gpio_trig_deactivate(struct led_classdev *led)
 {
 	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
 
-	if (gpio_data->gpio != 0)
+	if (gpio_is_valid(gpio_data->gpio))
 		free_irq(gpio_to_irq(gpio_data->gpio), led);
 	kfree(gpio_data);
 }
Signature · 1 line
2.23.0.rc1
#1 lore raw

PATCH 2/2 leds: trigger: gpio: Convert to use kstrtox()

sscanf() is a heavy one and moreover requires additional boundary checks. Convert driver to use kstrtox() and replace kstrtoul() by kstrtobool() in gpio_trig_inverted_store()

---
 drivers/leds/trigger/ledtrig-gpio.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +10 -13
index 31f456dd4417..b01862b94c99 100644
@@ -57,13 +57,13 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t n)
 {
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
-	unsigned desired_brightness;
+	u8 desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtou8(buf, 10, &desired_brightness);
+	if (ret) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret;
 	}
 
 	gpio_data->desired_brightness = desired_brightness;
@@ -86,16 +86,13 @@ static ssize_t gpio_trig_inverted_store(struct device *dev,
 {
 	struct led_classdev *led = led_trigger_get_led(dev);
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
-	unsigned long inverted;
+	bool inverted;
 	int ret;
 
-	ret = kstrtoul(buf, 10, &inverted);
-	if (ret < 0)
+	ret = kstrtobool(buf, &inverted);
+	if (ret)
 		return ret;
 
-	if (inverted > 1)
-		return -EINVAL;
-
 	gpio_data->inverted = inverted;
 
 	/* After inverting, we need to update the LED. */
@@ -122,10 +119,10 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	unsigned gpio;
 	int ret;
 
-	ret = sscanf(buf, "%u", &gpio);
-	if (ret < 1) {
+	ret = kstrtouint(buf, 10, &gpio);
+	if (ret) {
 		dev_err(dev, "couldn't read gpio number\n");
-		return -EINVAL;
+		return ret;
 	}
 
 	if (gpio_data->gpio == gpio)
Signature · 1 line
2.23.0.rc1
#2 lore raw

Re: [PATCH v1 2/2] leds: trigger: gpio: Convert to use kstrtox()

Hi Andy,

Thank you for the patch.

Quoted 14 lines · Andy Shevchenko

sscanf() is a heavy one and moreover requires additional boundary checks. Convert driver to use kstrtox() and replace kstrtoul() by kstrtobool() in gpio_trig_inverted_store()

---
 drivers/leds/trigger/ledtrig-gpio.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +0 -0
index 31f456dd4417..b01862b94c99 100644

drivers/leds/trigger/ledtrig-gpio.c static ssize_t gpio_trig_brightness_store(struct device *dev, · line 64

Quoted 9 lines
drivers/leds/trigger/ledtrig-gpio.c +1 -3
@@ -57,13 +57,13 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 		struct device_attribute *attr, const char *buf, size_t n)
 {
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
-	unsigned desired_brightness;
+	u8 desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {

LED_FULL (255) is already not a hard limit for quite a long time. While we are at it we could change that to led->max_brightness.

Quoted 43 lines
drivers/leds/trigger/ledtrig-gpio.c +9 -10
+	ret = kstrtou8(buf, 10, &desired_brightness);
+	if (ret) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret;
 	}
 
 	gpio_data->desired_brightness = desired_brightness;
@@ -86,16 +86,13 @@ static ssize_t gpio_trig_inverted_store(struct device *dev,
 {
 	struct led_classdev *led = led_trigger_get_led(dev);
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
-	unsigned long inverted;
+	bool inverted;
 	int ret;
 
-	ret = kstrtoul(buf, 10, &inverted);
-	if (ret < 0)
+	ret = kstrtobool(buf, &inverted);
+	if (ret)
 		return ret;
 
-	if (inverted > 1)
-		return -EINVAL;
-
 	gpio_data->inverted = inverted;
 
 	/* After inverting, we need to update the LED. */
@@ -122,10 +119,10 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	unsigned gpio;
 	int ret;
 
-	ret = sscanf(buf, "%u", &gpio);
-	if (ret < 1) {
+	ret = kstrtouint(buf, 10, &gpio);
+	if (ret) {
 		dev_err(dev, "couldn't read gpio number\n");
-		return -EINVAL;
+		return ret;
 	}
 
 	if (gpio_data->gpio == gpio)
Signature · 2 lines
Best regards,
Jacek Anaszewski
#3 lore raw
In reply to Andy Shevchenko

Hi Andy,

Thank you for the patch.

Quoted 13 lines · Andy Shevchenko

Allow all valid GPIOs to be used in the driver.

Fixes 17354bfe8527 ("leds: Add gpio-led trigger")
---
 drivers/leds/trigger/ledtrig-gpio.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +0 -0
index 33cc99a1a16a..31f456dd4417 100644

drivers/leds/trigger/ledtrig-gpio.c static ssize_t gpio_trig_gpio_store(struct device *dev, · line 137

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
@@ -131,10 +131,10 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	if (gpio_data->gpio == gpio)
 		return n;
 
-	if (!gpio) {
-		if (gpio_data->gpio != 0)
+	if (!gpio_is_valid(gpio)) {
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
-		gpio_data->gpio = 0;
+		gpio_data->gpio = gpio;

It looks odd to me. I'd just assign invalid constant gpio number e.g. -1.

Note that we should also do that in gpio_trig_activate(), where gpio_data->gpio is initialized to 0 by kzalloc(). That later can have nasty side effect in gpio_trig_gpio_store() when gpio to set is 0. Then the condition "if (gpio_data->gpio == gpio)" will evaluate to true and gpio_trig_irq() handler will not be registered.

Quoted 22 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -2
 		return n;
 	}
 
@@ -144,7 +144,7 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
 	if (ret) {
 		dev_err(dev, "request_irq failed with error %d\n", ret);
 	} else {
-		if (gpio_data->gpio != 0)
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
 		gpio_data->gpio = gpio;
 		/* After changing the GPIO, we need to update the LED. */
@@ -181,7 +181,7 @@ static void gpio_trig_deactivate(struct led_classdev *led)
 {
 	struct gpio_trig_data *gpio_data = led_get_trigger_data(led);
 
-	if (gpio_data->gpio != 0)
+	if (gpio_is_valid(gpio_data->gpio))
 		free_irq(gpio_to_irq(gpio_data->gpio), led);
 	kfree(gpio_data);
 }
Signature · 2 lines
Best regards,
Jacek Anaszewski
#4 lore raw
Quoted 6 lines · Jacek Anaszewski
Quoted 4 lines · Andy Shevchenko

Allow all valid GPIOs to be used in the driver.

Fixes 17354bfe8527 ("leds: Add gpio-led trigger")
Quoted 10 lines

drivers/leds/trigger/ledtrig-gpio.c static ssize_t gpio_trig_gpio_store(struct device *dev, · line 137

Quoted 7 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
-	if (!gpio) {
-		if (gpio_data->gpio != 0)
+	if (!gpio_is_valid(gpio)) {
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
-		gpio_data->gpio = 0;
+		gpio_data->gpio = gpio;

It looks odd to me. I'd just assign invalid constant gpio number e.g. -1.

Current ABI (unsigned) doesn't allow us to do this. Internally we can redefine invalid GPIO line number to -1 or so, but does it worth it? And actually I would prefer -EINVAL or -ENOENT in such cases.

Quoted 5 lines

Note that we should also do that in gpio_trig_activate(), where gpio_data->gpio is initialized to 0 by kzalloc(). That later can have nasty side effect in gpio_trig_gpio_store() when gpio to set is 0. Then the condition "if (gpio_data->gpio == gpio)" will evaluate to true and gpio_trig_irq() handler will not be registered.

Thanks for spotting this!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 18 lines · Andy Shevchenko
Quoted 6 lines · Jacek Anaszewski
Quoted 4 lines · Andy Shevchenko

Allow all valid GPIOs to be used in the driver.

Fixes 17354bfe8527 ("leds: Add gpio-led trigger")
Quoted 10 lines

drivers/leds/trigger/ledtrig-gpio.c static ssize_t gpio_trig_gpio_store(struct device *dev, · line 137

Quoted 7 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
-	if (!gpio) {
-		if (gpio_data->gpio != 0)
+	if (!gpio_is_valid(gpio)) {
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
-		gpio_data->gpio = 0;
+		gpio_data->gpio = gpio;

It looks odd to me. I'd just assign invalid constant gpio number e.g. -1.

Quoted 1 line

Current ABI (unsigned) doesn't allow us to do this. Internally we can redefine

Ah, right, missed that.

Quoted 2 lines

invalid GPIO line number to -1 or so, but does it worth it? And actually I would prefer -EINVAL or -ENOENT in such cases.

OK, we can keep your "= gpio" assignment in view of the above, but need to return error instead of "n".

Quoted 8 lines
Quoted 5 lines

Note that we should also do that in gpio_trig_activate(), where gpio_data->gpio is initialized to 0 by kzalloc(). That later can have nasty side effect in gpio_trig_gpio_store() when gpio to set is 0. Then the condition "if (gpio_data->gpio == gpio)" will evaluate to true and gpio_trig_irq() handler will not be registered.

Thanks for spotting this!

Signature · 2 lines
Best regards,
Jacek Anaszewski
#6 lore raw
Quoted 21 lines · Jacek Anaszewski
Quoted 18 lines · Andy Shevchenko
Quoted 6 lines · Jacek Anaszewski
Quoted 4 lines · Andy Shevchenko

Allow all valid GPIOs to be used in the driver.

Fixes 17354bfe8527 ("leds: Add gpio-led trigger")
Quoted 10 lines

drivers/leds/trigger/ledtrig-gpio.c static ssize_t gpio_trig_gpio_store(struct device *dev, · line 137

Quoted 7 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
-	if (!gpio) {
-		if (gpio_data->gpio != 0)
+	if (!gpio_is_valid(gpio)) {
+		if (gpio_is_valid(gpio_data->gpio))
 			free_irq(gpio_to_irq(gpio_data->gpio), led);
-		gpio_data->gpio = 0;
+		gpio_data->gpio = gpio;

It looks odd to me. I'd just assign invalid constant gpio number e.g. -1.

Quoted 1 line

Current ABI (unsigned) doesn't allow us to do this. Internally we can redefine

Quoted 7 lines

Ah, right, missed that.

Quoted 2 lines

invalid GPIO line number to -1 or so, but does it worth it? And actually I would prefer -EINVAL or -ENOENT in such cases.

OK, we can keep your "= gpio" assignment in view of the above, but need to return error instead of "n".

Then we will break an ABI, where user expects no error it suddenly will be one.

Signature · 2 lines
With Best Regards,
Andy Shevchenko