Commit 48cccd26 authored by Thadeu Lima de Souza Cascardo's avatar Thadeu Lima de Souza Cascardo Committed by Linus Torvalds

leds: fix multiple requests and releases of IRQ for GPIO LED Trigger

When setting the same GPIO number, multiple IRQ shared requests will be
done without freing the previous request.  It will also try to free a
failed request or an already freed IRQ if 0 was written to the gpio file.

All these oops and leaks were fixed with the following solution: keep the
previous allocated GPIO (if any) still allocated in case the new request
fails.  The alternative solution would desallocate the previous allocated
GPIO and set gpio as 0.
Signed-off-by: default avatarThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Signed-off-by: default avatarSamuel R. C. Vale <srcvale@holoscopio.com>
Cc: Richard Purdie <rpurdie@rpsys.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent bdf57de4
...@@ -146,20 +146,26 @@ static ssize_t gpio_trig_gpio_store(struct device *dev, ...@@ -146,20 +146,26 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
return -EINVAL; return -EINVAL;
} }
if (gpio_data->gpio == gpio)
return n;
if (!gpio) { if (!gpio) {
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led); free_irq(gpio_to_irq(gpio_data->gpio), led);
gpio_data->gpio = 0;
return n; return n;
} }
if (gpio_data->gpio > 0 && gpio_data->gpio != gpio)
free_irq(gpio_to_irq(gpio_data->gpio), led);
gpio_data->gpio = gpio;
ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq, ret = request_irq(gpio_to_irq(gpio), gpio_trig_irq,
IRQF_SHARED | IRQF_TRIGGER_RISING IRQF_SHARED | IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led);
if (ret) if (ret) {
dev_err(dev, "request_irq failed with error %d\n", ret); dev_err(dev, "request_irq failed with error %d\n", ret);
} else {
if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
gpio_data->gpio = gpio;
}
return ret ? ret : n; return ret ? ret : n;
} }
...@@ -211,7 +217,8 @@ static void gpio_trig_deactivate(struct led_classdev *led) ...@@ -211,7 +217,8 @@ static void gpio_trig_deactivate(struct led_classdev *led)
device_remove_file(led->dev, &dev_attr_inverted); device_remove_file(led->dev, &dev_attr_inverted);
device_remove_file(led->dev, &dev_attr_desired_brightness); device_remove_file(led->dev, &dev_attr_desired_brightness);
flush_work(&gpio_data->work); flush_work(&gpio_data->work);
free_irq(gpio_to_irq(gpio_data->gpio),led); if (gpio_data->gpio != 0)
free_irq(gpio_to_irq(gpio_data->gpio), led);
kfree(gpio_data); kfree(gpio_data);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment