public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional()
@ 2023-11-03 19:53 Andy Shevchenko
  2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Andy Shevchenko @ 2023-11-03 19:53 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij, linux-leds, linux-kernel
  Cc: Pavel Machek, Lee Jones

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.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/leds/trigger/ledtrig-gpio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index 9b7fe5dd5208..d91ae7fde3cf 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -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);
 
-- 
2.40.0.1.gaa8946217a0b


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()
  2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
@ 2023-11-03 19:53 ` Andy Shevchenko
  2023-11-04 22:26   ` Linus Walleij
  2023-11-23 11:05   ` Lee Jones
  2023-11-03 19:53 ` [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf() Andy Shevchenko
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 12+ messages in thread
From: Andy Shevchenko @ 2023-11-03 19:53 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij, linux-leds, linux-kernel
  Cc: Pavel Machek, Lee Jones

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

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index d91ae7fde3cf..8a30f9228186 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -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;
 
-- 
2.40.0.1.gaa8946217a0b


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf()
  2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
  2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
@ 2023-11-03 19:53 ` Andy Shevchenko
  2023-11-04 22:24   ` Linus Walleij
  2023-11-03 19:53 ` [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW() Andy Shevchenko
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Andy Shevchenko @ 2023-11-03 19:53 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij, linux-leds, linux-kernel
  Cc: Pavel Machek, Lee Jones

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.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/leds/trigger/ledtrig-gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index 8a30f9228186..8824be19881f 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -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,
-- 
2.40.0.1.gaa8946217a0b


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW()
  2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
  2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
  2023-11-03 19:53 ` [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf() Andy Shevchenko
@ 2023-11-03 19:53 ` Andy Shevchenko
  2023-11-04 22:26   ` Linus Walleij
  2023-11-04 22:26 ` [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Linus Walleij
  2023-11-23 14:48 ` Lee Jones
  4 siblings, 1 reply; 12+ messages in thread
From: Andy Shevchenko @ 2023-11-03 19:53 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij, linux-leds, linux-kernel
  Cc: Pavel Machek, Lee Jones

Instead of custom wrapper, use DEVICE_ATTR_RW() directly.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/leds/trigger/ledtrig-gpio.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index 8824be19881f..7f6a2352b0ac 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -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,
-- 
2.40.0.1.gaa8946217a0b


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf()
  2023-11-03 19:53 ` [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf() Andy Shevchenko
@ 2023-11-04 22:24   ` Linus Walleij
  0 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2023-11-04 22:24 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-leds, linux-kernel, Pavel Machek, Lee Jones

On Fri, Nov 3, 2023 at 8:53 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> 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.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional()
  2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
                   ` (2 preceding siblings ...)
  2023-11-03 19:53 ` [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW() Andy Shevchenko
@ 2023-11-04 22:26 ` Linus Walleij
  2023-11-23 14:48 ` Lee Jones
  4 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2023-11-04 22:26 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-leds, linux-kernel, Pavel Machek, Lee Jones

On Fri, Nov 3, 2023 at 8:53 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> 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.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

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...
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()
  2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
@ 2023-11-04 22:26   ` Linus Walleij
  2023-11-23 11:05   ` Lee Jones
  1 sibling, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2023-11-04 22:26 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-leds, linux-kernel, Pavel Machek, Lee Jones

On Fri, Nov 3, 2023 at 9:03 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> sscanf() is a heavy one and moreover requires additional boundary checks.
> Convert driver to use kstrtou8() in gpio_trig_inverted_store().
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW()
  2023-11-03 19:53 ` [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW() Andy Shevchenko
@ 2023-11-04 22:26   ` Linus Walleij
  0 siblings, 0 replies; 12+ messages in thread
From: Linus Walleij @ 2023-11-04 22:26 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-leds, linux-kernel, Pavel Machek, Lee Jones

On Fri, Nov 3, 2023 at 9:03 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> Instead of custom wrapper, use DEVICE_ATTR_RW() directly.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()
  2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
  2023-11-04 22:26   ` Linus Walleij
@ 2023-11-23 11:05   ` Lee Jones
  2023-11-23 14:11     ` Andy Shevchenko
  1 sibling, 1 reply; 12+ messages in thread
From: Lee Jones @ 2023-11-23 11:05 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Linus Walleij, linux-leds, linux-kernel, Pavel Machek

On Fri, 03 Nov 2023, Andy Shevchenko wrote:

> sscanf() is a heavy one and moreover requires additional boundary checks.
> Convert driver to use kstrtou8() in gpio_trig_inverted_store().
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
> index d91ae7fde3cf..8a30f9228186 100644
> --- a/drivers/leds/trigger/ledtrig-gpio.c
> +++ b/drivers/leds/trigger/ledtrig-gpio.c
> @@ -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);

Where does 10 come from?

> +	if (ret)
> +		return ret;
>  
>  	gpio_data->desired_brightness = desired_brightness;
>  
> -- 
> 2.40.0.1.gaa8946217a0b
> 

-- 
Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()
  2023-11-23 11:05   ` Lee Jones
@ 2023-11-23 14:11     ` Andy Shevchenko
  2023-11-23 14:47       ` Lee Jones
  0 siblings, 1 reply; 12+ messages in thread
From: Andy Shevchenko @ 2023-11-23 14:11 UTC (permalink / raw)
  To: Lee Jones; +Cc: Linus Walleij, linux-leds, linux-kernel, Pavel Machek

On Thu, Nov 23, 2023 at 11:05:38AM +0000, Lee Jones wrote:
> On Fri, 03 Nov 2023, Andy Shevchenko wrote:

...

> > -	ret = sscanf(buf, "%u", &desired_brightness);

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

...

> > +	ret = kstrtou8(buf, 10, &desired_brightness);
> 
> Where does 10 come from?

See above.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()
  2023-11-23 14:11     ` Andy Shevchenko
@ 2023-11-23 14:47       ` Lee Jones
  0 siblings, 0 replies; 12+ messages in thread
From: Lee Jones @ 2023-11-23 14:47 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Linus Walleij, linux-leds, linux-kernel, Pavel Machek

On Thu, 23 Nov 2023, Andy Shevchenko wrote:

> On Thu, Nov 23, 2023 at 11:05:38AM +0000, Lee Jones wrote:
> > On Fri, 03 Nov 2023, Andy Shevchenko wrote:
> 
> ...
> 
> > > -	ret = sscanf(buf, "%u", &desired_brightness);
> 
> "%u" (see man sscanf() for the details)
> 
> ...
> 
> > > +	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 ...

-- 
Lee Jones [李琼斯]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional()
  2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
                   ` (3 preceding siblings ...)
  2023-11-04 22:26 ` [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Linus Walleij
@ 2023-11-23 14:48 ` Lee Jones
  4 siblings, 0 replies; 12+ messages in thread
From: Lee Jones @ 2023-11-23 14:48 UTC (permalink / raw)
  To: Andy Shevchenko, Linus Walleij, linux-leds, linux-kernel,
	Andy Shevchenko
  Cc: Pavel Machek

On Fri, 03 Nov 2023 21:53:07 +0200, Andy Shevchenko wrote:
> 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 [李琼斯]


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-11-23 14:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-03 19:53 [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Andy Shevchenko
2023-11-03 19:53 ` [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
2023-11-04 22:26   ` Linus Walleij
2023-11-23 11:05   ` Lee Jones
2023-11-23 14:11     ` Andy Shevchenko
2023-11-23 14:47       ` Lee Jones
2023-11-03 19:53 ` [PATCH v1 3/4] leds: trigger: gpio: Use sysfs_emit() to instead of s*printf() Andy Shevchenko
2023-11-04 22:24   ` Linus Walleij
2023-11-03 19:53 ` [PATCH v1 4/4] leds: trigger: gpio: Convert to DEVICE_ATTR_RW() Andy Shevchenko
2023-11-04 22:26   ` Linus Walleij
2023-11-04 22:26 ` [PATCH v1 1/4] leds: trigger: gpio: Replace custom code for gpiod_get_optional() Linus Walleij
2023-11-23 14:48 ` Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox