Skip to contentSkip to conversation

leds: trigger: gpio: GPIO 0 is valid

PATCH In review Andy Shevchenko 3 participants 7 messages Aug 30, 2019 – Sep 2, 2019 LED

lore
Share & tools
lore
#0 lore raw

PATCH v2 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")

---
- set initial GPIO value to -ENOENT (Jacek)

 drivers/leds/trigger/ledtrig-gpio.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +7 -5
index 33cc99a1a16a..dc64679b1a92 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. */
@@ -172,6 +172,8 @@ static int gpio_trig_activate(struct led_classdev *led)
 		return -ENOMEM;
 
 	gpio_data->led = led;
+	gpio_data->gpio = -ENOENT;
+
 	led_set_trigger_data(led, gpio_data);
 
 	return 0;
@@ -181,7 +183,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 v2 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().

While here, check the desired brightness against maximum defined for a certain LED.

---
- check against max_brightness (Jacek)

 drivers/leds/trigger/ledtrig-gpio.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +9 -12
index dc64679b1a92..2d70c35588cb 100644
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 	unsigned desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtouint(buf, 10, &desired_brightness);
+	if (ret || desired_brightness > gpio_data->led->max_brightness) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret ? ret : -EINVAL;
 	}
 
 	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 v2 2/2] leds: trigger: gpio: Convert to use kstrtox()

Quoted 6 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().

While here, check the desired brightness against maximum defined for a certain LED.

One change per patch, please.

Because this one will not end well.

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

Quoted 12 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 	unsigned desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtouint(buf, 10, &desired_brightness);
+	if (ret || desired_brightness > gpio_data->led->max_brightness) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret ? ret : -EINVAL;
 	}

We have people writing 255 into brightness, because that's what we used to do even for on/off LEDS. It is expected to work even for leds with max_brightness of 1.

So... we want to saturate here, not return -EINVAL. (And we will eventually want to switch on/off leds to max_brightness = 1...)

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +1 -3
@@ -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)

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

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -3
+	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. */

So, this accepted 0/1. Now it also accepts true false and many other pairs.

Which... might be ok. But probably should be separated.

Best regards,
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#3 lore raw

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

Hi Pavel,

Quoted 29 lines · Pavel Machek
Quoted 6 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().

While here, check the desired brightness against maximum defined for a certain LED.

One change per patch, please.

Because this one will not end well.

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

Quoted 12 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 	unsigned desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtouint(buf, 10, &desired_brightness);
+	if (ret || desired_brightness > gpio_data->led->max_brightness) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret ? ret : -EINVAL;
 	}

We have people writing 255 into brightness, because that's what we used to do even for on/off LEDS. It is expected to work even for leds with max_brightness of 1.

Quoted 2 lines

So... we want to saturate here, not return -EINVAL. (And we will eventually want to switch on/off leds to max_brightness = 1...)

Good point. We shouldn't fail here but proceed similarly as in case of setting brightness for a LED in led_set_brightness_nosleep(), i.e. here it should be:

desired_brightness = min(desired_brightness,

                         gpio_data->led->->max_brightness);

So the condition should be limited to checking error code.

Quoted 29 lines
Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +1 -3
@@ -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)

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

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -3
+	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. */

So, this accepted 0/1. Now it also accepts true false and many other pairs.

Which... might be ok. But probably should be separated.

Best regards,
Pavel

Signature · 2 lines
Best regards,
Jacek Anaszewski
#4 lore raw

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

Quoted 35 lines · Jacek Anaszewski

Hi Pavel,

Quoted 29 lines · Pavel Machek
Quoted 6 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().

While here, check the desired brightness against maximum defined for a certain LED.

One change per patch, please.

Because this one will not end well.

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

Quoted 12 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 	unsigned desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtouint(buf, 10, &desired_brightness);
+	if (ret || desired_brightness > gpio_data->led->max_brightness) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret ? ret : -EINVAL;
 	}

We have people writing 255 into brightness, because that's what we used to do even for on/off LEDS. It is expected to work even for leds with max_brightness of 1.

Quoted 2 lines

So... we want to saturate here, not return -EINVAL. (And we will eventually want to switch on/off leds to max_brightness = 1...)

Quoted 6 lines

Good point. We shouldn't fail here but proceed similarly as in case of setting brightness for a LED in led_set_brightness_nosleep(), i.e. here it should be:

desired_brightness = min(desired_brightness,

                         gpio_data->led->->max_brightness);

PS. Dropped the patch.

Signature · 2 lines
Best regards,
Jacek Anaszewski
#5 lore raw

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

In reply to Pavel Machek
Quoted 29 lines · Pavel Machek
Quoted 6 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().

While here, check the desired brightness against maximum defined for a certain LED.

One change per patch, please.

Because this one will not end well.

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

Quoted 12 lines
drivers/leds/trigger/ledtrig-gpio.c +3 -3
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 	unsigned desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
+	ret = kstrtouint(buf, 10, &desired_brightness);
+	if (ret || desired_brightness > gpio_data->led->max_brightness) {
 		dev_err(dev, "invalid value\n");
-		return -EINVAL;
+		return ret ? ret : -EINVAL;
 	}

We have people writing 255 into brightness, because that's what we used to do even for on/off LEDS. It is expected to work even for leds with max_brightness of 1.

Quoted 2 lines

So... we want to saturate here, not return -EINVAL. (And we will eventually want to switch on/off leds to max_brightness = 1...)

Agree. Thank you for your review!

Quoted 25 lines
Quoted 11 lines
drivers/leds/trigger/ledtrig-gpio.c +1 -3
@@ -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)

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

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -3
+	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. */

So, this accepted 0/1. Now it also accepts true false and many other pairs.

Which... might be ok. But probably should be separated.

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

Hi Andy,

Thank you for the v2.

Quoted 47 lines · Andy Shevchenko

Allow all valid GPIOs to be used in the driver.

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

---
- set initial GPIO value to -ENOENT (Jacek)

 drivers/leds/trigger/ledtrig-gpio.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +6 -4
index 33cc99a1a16a..dc64679b1a92 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. */
@@ -172,6 +172,8 @@ static int gpio_trig_activate(struct led_classdev *led)
 		return -ENOMEM;
 
 	gpio_data->led = led;
+	gpio_data->gpio = -ENOENT;
+
 	led_set_trigger_data(led, gpio_data);
 
 	return 0;

drivers/leds/trigger/ledtrig-gpio.c static void gpio_trig_deactivate(struct led_classdev *led) · line 189

Quoted 8 lines
drivers/leds/trigger/ledtrig-gpio.c +1 -1
@@ -181,7 +183,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);
 }

Both 1/2 and 2/2 applied.

Signature · 2 lines
Best regards,
Jacek Anaszewski