Commit acd899e4 authored by Jacek Anaszewski's avatar Jacek Anaszewski Committed by Bryan Wu

leds: implement sysfs interface locking mechanism

Add a mechanism for locking LED subsystem sysfs interface.
This patch prepares ground for addition of LED Flash Class
extension, whose API will be integrated with V4L2 Flash API.
Such a fusion enforces introducing a locking scheme, which
will secure consistent access to the LED Flash Class device.

The mechanism being introduced allows for disabling LED
subsystem sysfs interface by calling led_sysfs_disable function
and enabling it by calling led_sysfs_enable. The functions
alter the LED_SYSFS_DISABLE flag state and must be called
under mutex lock. The state of the lock is checked with use
of led_sysfs_is_disabled function. Such a design allows for
providing immediate feedback to the user space on whether
the LED Flash Class device is available or is under V4L2 Flash
sub-device control.
Signed-off-by: default avatarJacek Anaszewski <j.anaszewski@samsung.com>
Acked-by: default avatarKyungmin Park <kyungmin.park@samsung.com>
Cc: Richard Purdie <rpurdie@rpsys.net>
Signed-off-by: default avatarBryan Wu <cooloney@gmail.com>
parent 3f6e42c8
...@@ -40,17 +40,27 @@ static ssize_t brightness_store(struct device *dev, ...@@ -40,17 +40,27 @@ static ssize_t brightness_store(struct device *dev,
{ {
struct led_classdev *led_cdev = dev_get_drvdata(dev); struct led_classdev *led_cdev = dev_get_drvdata(dev);
unsigned long state; unsigned long state;
ssize_t ret = -EINVAL; ssize_t ret;
mutex_lock(&led_cdev->led_access);
if (led_sysfs_is_disabled(led_cdev)) {
ret = -EBUSY;
goto unlock;
}
ret = kstrtoul(buf, 10, &state); ret = kstrtoul(buf, 10, &state);
if (ret) if (ret)
return ret; goto unlock;
if (state == LED_OFF) if (state == LED_OFF)
led_trigger_remove(led_cdev); led_trigger_remove(led_cdev);
__led_set_brightness(led_cdev, state); __led_set_brightness(led_cdev, state);
return size; ret = size;
unlock:
mutex_unlock(&led_cdev->led_access);
return ret;
} }
static DEVICE_ATTR_RW(brightness); static DEVICE_ATTR_RW(brightness);
...@@ -214,6 +224,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev) ...@@ -214,6 +224,7 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
#ifdef CONFIG_LEDS_TRIGGERS #ifdef CONFIG_LEDS_TRIGGERS
init_rwsem(&led_cdev->trigger_lock); init_rwsem(&led_cdev->trigger_lock);
#endif #endif
mutex_init(&led_cdev->led_access);
/* add to the list of leds */ /* add to the list of leds */
down_write(&leds_list_lock); down_write(&leds_list_lock);
list_add_tail(&led_cdev->node, &leds_list); list_add_tail(&led_cdev->node, &leds_list);
...@@ -267,6 +278,8 @@ void led_classdev_unregister(struct led_classdev *led_cdev) ...@@ -267,6 +278,8 @@ void led_classdev_unregister(struct led_classdev *led_cdev)
down_write(&leds_list_lock); down_write(&leds_list_lock);
list_del(&led_cdev->node); list_del(&led_cdev->node);
up_write(&leds_list_lock); up_write(&leds_list_lock);
mutex_destroy(&led_cdev->led_access);
} }
EXPORT_SYMBOL_GPL(led_classdev_unregister); EXPORT_SYMBOL_GPL(led_classdev_unregister);
......
...@@ -143,3 +143,21 @@ int led_update_brightness(struct led_classdev *led_cdev) ...@@ -143,3 +143,21 @@ int led_update_brightness(struct led_classdev *led_cdev)
return ret; return ret;
} }
EXPORT_SYMBOL(led_update_brightness); EXPORT_SYMBOL(led_update_brightness);
/* Caller must ensure led_cdev->led_access held */
void led_sysfs_disable(struct led_classdev *led_cdev)
{
lockdep_assert_held(&led_cdev->led_access);
led_cdev->flags |= LED_SYSFS_DISABLE;
}
EXPORT_SYMBOL_GPL(led_sysfs_disable);
/* Caller must ensure led_cdev->led_access held */
void led_sysfs_enable(struct led_classdev *led_cdev)
{
lockdep_assert_held(&led_cdev->led_access);
led_cdev->flags &= ~LED_SYSFS_DISABLE;
}
EXPORT_SYMBOL_GPL(led_sysfs_enable);
...@@ -37,6 +37,14 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, ...@@ -37,6 +37,14 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
char trigger_name[TRIG_NAME_MAX]; char trigger_name[TRIG_NAME_MAX];
struct led_trigger *trig; struct led_trigger *trig;
size_t len; size_t len;
int ret = count;
mutex_lock(&led_cdev->led_access);
if (led_sysfs_is_disabled(led_cdev)) {
ret = -EBUSY;
goto unlock;
}
trigger_name[sizeof(trigger_name) - 1] = '\0'; trigger_name[sizeof(trigger_name) - 1] = '\0';
strncpy(trigger_name, buf, sizeof(trigger_name) - 1); strncpy(trigger_name, buf, sizeof(trigger_name) - 1);
...@@ -47,7 +55,7 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, ...@@ -47,7 +55,7 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
if (!strcmp(trigger_name, "none")) { if (!strcmp(trigger_name, "none")) {
led_trigger_remove(led_cdev); led_trigger_remove(led_cdev);
return count; goto unlock;
} }
down_read(&triggers_list_lock); down_read(&triggers_list_lock);
...@@ -58,12 +66,14 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, ...@@ -58,12 +66,14 @@ ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
up_write(&led_cdev->trigger_lock); up_write(&led_cdev->trigger_lock);
up_read(&triggers_list_lock); up_read(&triggers_list_lock);
return count; goto unlock;
} }
} }
up_read(&triggers_list_lock); up_read(&triggers_list_lock);
return -EINVAL; unlock:
mutex_unlock(&led_cdev->led_access);
return ret;
} }
EXPORT_SYMBOL_GPL(led_trigger_store); EXPORT_SYMBOL_GPL(led_trigger_store);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#define __LINUX_LEDS_H_INCLUDED #define __LINUX_LEDS_H_INCLUDED
#include <linux/list.h> #include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rwsem.h> #include <linux/rwsem.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/timer.h> #include <linux/timer.h>
...@@ -42,6 +43,7 @@ struct led_classdev { ...@@ -42,6 +43,7 @@ struct led_classdev {
#define LED_BLINK_ONESHOT (1 << 17) #define LED_BLINK_ONESHOT (1 << 17)
#define LED_BLINK_ONESHOT_STOP (1 << 18) #define LED_BLINK_ONESHOT_STOP (1 << 18)
#define LED_BLINK_INVERT (1 << 19) #define LED_BLINK_INVERT (1 << 19)
#define LED_SYSFS_DISABLE (1 << 20)
/* Set LED brightness level */ /* Set LED brightness level */
/* Must not sleep, use a workqueue if needed */ /* Must not sleep, use a workqueue if needed */
...@@ -85,6 +87,9 @@ struct led_classdev { ...@@ -85,6 +87,9 @@ struct led_classdev {
/* true if activated - deactivate routine uses it to do cleanup */ /* true if activated - deactivate routine uses it to do cleanup */
bool activated; bool activated;
#endif #endif
/* Ensures consistent access to the LED Flash Class device */
struct mutex led_access;
}; };
extern int led_classdev_register(struct device *parent, extern int led_classdev_register(struct device *parent,
...@@ -151,6 +156,33 @@ extern void led_set_brightness(struct led_classdev *led_cdev, ...@@ -151,6 +156,33 @@ extern void led_set_brightness(struct led_classdev *led_cdev,
*/ */
extern int led_update_brightness(struct led_classdev *led_cdev); extern int led_update_brightness(struct led_classdev *led_cdev);
/**
* led_sysfs_disable - disable LED sysfs interface
* @led_cdev: the LED to set
*
* Disable the led_cdev's sysfs interface.
*/
extern void led_sysfs_disable(struct led_classdev *led_cdev);
/**
* led_sysfs_enable - enable LED sysfs interface
* @led_cdev: the LED to set
*
* Enable the led_cdev's sysfs interface.
*/
extern void led_sysfs_enable(struct led_classdev *led_cdev);
/**
* led_sysfs_is_disabled - check if LED sysfs interface is disabled
* @led_cdev: the LED to query
*
* Returns: true if the led_cdev's sysfs interface is disabled.
*/
static inline bool led_sysfs_is_disabled(struct led_classdev *led_cdev)
{
return led_cdev->flags & LED_SYSFS_DISABLE;
}
/* /*
* LED Triggers * LED Triggers
*/ */
......
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