Skip to contentSkip to conversation

leds: trigger: gpio: Replace custom code for gpiod_get_optional()

PATCH Applied Andy Shevchenko 3 participants 12 messages Nov 3, 2023 – Nov 23, 2023 LED

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 leds: trigger: gpio: Replace custom code for gpiod_get_optional()

gpiod_get_optional() and currently used fwnode_gpiod_get_index() are both wrappers against the same engine internally. Since we have a pointer to struct device there is no reason to use fwnode type of GPIO call. So, replace the current fwnode call by respective gpiod ones.

---
 drivers/leds/trigger/ledtrig-gpio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +3 -4
index 9b7fe5dd5208..d91ae7fde3cf 100644
@@ -89,10 +89,7 @@ static int gpio_trig_activate(struct led_classdev *led)
 	 * The generic property "trigger-sources" is followed,
 	 * and we hope that this is a GPIO.
 	 */
-	gpio_data->gpiod = fwnode_gpiod_get_index(dev->fwnode,
-						  "trigger-sources",
-						  0, GPIOD_IN,
-						  "led-trigger");
+	gpio_data->gpiod = gpiod_get_optional(dev, "trigger-sources", GPIOD_IN);
 	if (IS_ERR(gpio_data->gpiod)) {
 		ret = PTR_ERR(gpio_data->gpiod);
 		kfree(gpio_data);
@@ -104,6 +101,8 @@ static int gpio_trig_activate(struct led_classdev *led)
 		return -EINVAL;
 	}
 
+	gpiod_set_consumer_name(gpio_data->gpiod, "led-trigger");
+
 	gpio_data->led = led;
 	led_set_trigger_data(led, gpio_data);
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

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

sscanf() is a heavy one and moreover requires additional boundary checks. Convert driver to use kstrtou8() in gpio_trig_inverted_store().

---
 drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +4 -6
index d91ae7fde3cf..8a30f9228186 100644
@@ -53,14 +53,12 @@ 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) {
-		dev_err(dev, "invalid value\n");
-		return -EINVAL;
-	}
+	ret = kstrtou8(buf, 10, &desired_brightness);
+	if (ret)
+		return ret;
 
 	gpio_data->desired_brightness = desired_brightness;
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
maintainer
#3 lore raw

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

In reply to Andy Shevchenko
Quoted 16 lines · Andy Shevchenko

sscanf() is a heavy one and moreover requires additional boundary checks. Convert driver to use kstrtou8() in gpio_trig_inverted_store().

---
 drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +0 -0
index d91ae7fde3cf..8a30f9228186 100644
@@ -53,14 +53,12 @@ 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);

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

Quoted 10 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -6
-	unsigned desired_brightness;
+	u8 desired_brightness;
 	int ret;
 
-	ret = sscanf(buf, "%u", &desired_brightness);
-	if (ret < 1 || desired_brightness > 255) {
-		dev_err(dev, "invalid value\n");
-		return -EINVAL;
-	}
+	ret = kstrtou8(buf, 10, &desired_brightness);

Where does 10 come from?

Quoted 8 lines
drivers/leds/trigger/ledtrig-gpio.c +2 -0
+	if (ret)
+		return ret;
 
 	gpio_data->desired_brightness = desired_brightness;
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
Signature · 1 line
Lee Jones [李琼斯]
#4 lore raw

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

Quoted 1 line · Lee Jones

On Fri, 03 Nov 2023, Andy Shevchenko wrote:

...

Quoted 1 line

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

Quoted 1 line
drivers/leds/trigger/ledtrig-gpio.c +0 -1
-	ret = sscanf(buf, "%u", &desired_brightness);

"%u" (see man sscanf() for the details)

...

Quoted 3 lines

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

Quoted 1 line
drivers/leds/trigger/ledtrig-gpio.c +1 -0
+	ret = kstrtou8(buf, 10, &desired_brightness);

Where does 10 come from?

See above.

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

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

Quoted 9 lines · Andy Shevchenko
Quoted 1 line · Lee Jones

On Fri, 03 Nov 2023, Andy Shevchenko wrote:

...

Quoted 1 line

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

Quoted 1 line
drivers/leds/trigger/ledtrig-gpio.c +0 -1
-	ret = sscanf(buf, "%u", &desired_brightness);

"%u" (see man sscanf() for the details)

Quoted 7 lines

...

Quoted 3 lines

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

Quoted 1 line
drivers/leds/trigger/ledtrig-gpio.c +1 -0
+	ret = kstrtou8(buf, 10, &desired_brightness);

Where does 10 come from?

See above.

Hmmm ...

I see that this is generally accepted. Although is looks like a recipe for bugs to me. It's a shame we don't have something that can take a variable, derives its type, then calculates the maximum length if converted to a string.

Anyway, I'm probably babbling now ...

Signature · 1 line
Lee Jones [李琼斯]
#6 lore raw

PATCH 3/4 leds: trigger: gpio: Use sysfs_emit() to instead of s*printf()

In reply to Andy Shevchenko

Follow the advice of the Documentation/filesystems/sysfs.rst and show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space.

---
 drivers/leds/trigger/ledtrig-gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/leds/trigger/ledtrig-gpio.c +1 -1
index 8a30f9228186..8824be19881f 100644
@@ -46,7 +46,7 @@ static ssize_t gpio_trig_brightness_show(struct device *dev,
 {
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
 
-	return sprintf(buf, "%u\n", gpio_data->desired_brightness);
+	return sysfs_emit(buf, "%u\n", gpio_data->desired_brightness);
 }
 
 static ssize_t gpio_trig_brightness_store(struct device *dev,
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf()

Quoted 5 lines · Andy Shevchenko

Follow the advice of the Documentation/filesystems/sysfs.rst and show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space.

Yours,
Linus Walleij

#8 lore raw

PATCH 4/4 leds: trigger: gpio: Convert to DEVICE_ATTR_RW()

In reply to Andy Shevchenko

Instead of custom wrapper, use DEVICE_ATTR_RW() directly.

---
 drivers/leds/trigger/ledtrig-gpio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/leds/trigger/ledtrig-gpio.c +3 -4
index 8824be19881f..7f6a2352b0ac 100644
@@ -41,7 +41,7 @@ static irqreturn_t gpio_trig_irq(int irq, void *_led)
 	return IRQ_HANDLED;
 }
 
-static ssize_t gpio_trig_brightness_show(struct device *dev,
+static ssize_t desired_brightness_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
 	struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
@@ -49,7 +49,7 @@ static ssize_t gpio_trig_brightness_show(struct device *dev,
 	return sysfs_emit(buf, "%u\n", gpio_data->desired_brightness);
 }
 
-static ssize_t gpio_trig_brightness_store(struct device *dev,
+static ssize_t desired_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);
@@ -64,8 +64,7 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
 
 	return n;
 }
-static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show,
-		gpio_trig_brightness_store);
+static DEVICE_ATTR_RW(desired_brightness);
 
 static struct attribute *gpio_trig_attrs[] = {
 	&dev_attr_desired_brightness.attr,
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v1 1/4
#10 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko

gpiod_get_optional() and currently used fwnode_gpiod_get_index() are both wrappers against the same engine internally. Since we have a pointer to struct device there is no reason to use fwnode type of GPIO call. So, replace the current fwnode call by respective gpiod ones.

I don't know what I was thinking here. Probably I was thinking that the nodes doesn't always have a device, but hey, it does...

Yours,
Linus Walleij

maintainer Applied
#11 lore raw
In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko

gpiod_get_optional() and currently used fwnode_gpiod_get_index() are both wrappers against the same engine internally. Since we have a pointer to struct device there is no reason to use fwnode type of GPIO call. So, replace the current fwnode call by respective gpiod ones.

[...]

Applied, thanks!

[1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional()

      commit: cafe2f8ed515e586b020a60df44e9f251f4d6248
[2/4] leds: trigger: gpio: Convert to use kstrtox()
      commit: 001813a6f0c7b05c139cde5d5177a4a9bfe39b09
[3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf()
      commit: bd4f0709c66394bb746c985872c4153ac93877b7
[4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW()
      commit: 0603a253b18421a336c3be14bdcd1c5d75b6503b

--
Lee Jones [李琼斯]