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

Andy Shevchenko
2019-08-21T17:17:32Z

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()

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

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index 31f456dd4417..b01862b94c99 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -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)
--
2.23.0.rc1