Pull LED updates from Jacek Anaszewski: - finalize previously announced support for initialization of pattern triggers from Device Tree - fix for null deref on firmware load failure in leds-lp55xx-common.c * tag 'leds-for-5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds: leds: lp55xx: fix null deref on firmware load failure leds: trigger: timer: Add initialization from Device Tree leds: trigger: oneshot: Add initialization from Device Tree leds: trigger: pattern: Add pattern initialization from Device Tree leds: Add helper for getting default pattern from Device Tree dt-bindings: leds: Add pattern initialization from Device Tree
Unified · Split
| @@ -7,55 +7,10 @@ Description: · 6 unmodified lines | ||
| 7 | 7 | timer. It can do gradual dimming and step change of brightness. |
| 8 | 8 | |
| 9 | 9 | The pattern is given by a series of tuples, of brightness and |
| 10 | − duration (ms). The LED is expected to traverse the series and | |
| 11 | − each brightness value for the specified duration. Duration of | |
| 12 | − 0 means brightness should immediately change to new value, and | |
| 13 | − writing malformed pattern deactivates any active one. | |
| 10 | + duration (ms). | |
| 14 | 11 | |
| 15 | − 1. For gradual dimming, the dimming interval now is set as 50 | |
| 16 | − milliseconds. So the tuple with duration less than dimming | |
| 17 | − interval (50ms) is treated as a step change of brightness, | |
| 18 | − i.e. the subsequent brightness will be applied without adding | |
| 19 | − intervening dimming intervals. | |
| 20 | − | |
| 21 | − The gradual dimming format of the software pattern values should be: | |
| 22 | − "brightness_1 duration_1 brightness_2 duration_2 brightness_3 | |
| 23 | − duration_3 ...". For example: | |
| 24 | − | |
| 25 | − echo 0 1000 255 2000 > pattern | |
| 26 | − | |
| 27 | − It will make the LED go gradually from zero-intensity to max (255) | |
| 28 | − intensity in 1000 milliseconds, then back to zero intensity in 2000 | |
| 29 | − milliseconds: | |
| 30 | − | |
| 31 | − LED brightness | |
| 32 | − ^ | |
| 33 | − 255-| / \ / \ / | |
| 34 | − | / \ / \ / | |
| 35 | − | / \ / \ / | |
| 36 | − | / \ / \ / | |
| 37 | − 0-| / \/ \/ | |
| 38 | − +---0----1----2----3----4----5----6------------> time (s) | |
| 39 | − | |
| 40 | − 2. To make the LED go instantly from one brightness value to another, | |
| 41 | − we should use zero-time lengths (the brightness must be same as | |
| 42 | − the previous tuple's). So the format should be: | |
| 43 | − "brightness_1 duration_1 brightness_1 0 brightness_2 duration_2 | |
| 44 | − brightness_2 0 ...". For example: | |
| 45 | − | |
| 46 | − echo 0 1000 0 0 255 2000 255 0 > pattern | |
| 47 | − | |
| 48 | − It will make the LED stay off for one second, then stay at max brightness | |
| 49 | − for two seconds: | |
| 50 | − | |
| 51 | − LED brightness | |
| 52 | − ^ | |
| 53 | − 255-| +---------+ +---------+ | |
| 54 | − | | | | | | |
| 55 | − | | | | | | |
| 56 | − | | | | | | |
| 57 | − 0-| -----+ +----+ +---- | |
| 58 | − +---0----1----2----3----4----5----6------------> time (s) | |
| 12 | + The exact format is described in: | |
| 13 | + Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt | |
| 59 | 14 | |
| 60 | 15 | What: /sys/class/leds/<led>/hw_pattern |
| 61 | 16 | Date: September 2018 |
| @@ -37,6 +37,18 @@ Optional properties for child nodes: · 36 unmodified lines | ||
| 37 | 37 | "ide-disk" - LED indicates IDE disk activity (deprecated), |
| 38 | 38 | in new implementations use "disk-activity" |
| 39 | 39 | "timer" - LED flashes at a fixed, configurable rate |
| 40 | + "pattern" - LED alters the brightness for the specified duration with one | |
| 41 | + software timer (requires "led-pattern" property) | |
| 42 | + | |
| 43 | +- led-pattern : Array of integers with default pattern for certain triggers. | |
| 44 | + Each trigger may parse this property differently: | |
| 45 | + - one-shot : two numbers specifying delay on and delay off (in ms), | |
| 46 | + - timer : two numbers specifying delay on and delay off (in ms), | |
| 47 | + - pattern : the pattern is given by a series of tuples, of | |
| 48 | + brightness and duration (in ms). The exact format is | |
| 49 | + described in: | |
| 50 | + Documentation/devicetree/bindings/leds/leds-trigger-pattern.txt | |
| 51 | + | |
| 40 | 52 | |
| 41 | 53 | - led-max-microamp : Maximum LED supply current in microamperes. This property |
| 42 | 54 | can be made mandatory for the board configurations |
| @@ -0,0 +1,49 @@ | ||
| 1 | +* Pattern format for LED pattern trigger | |
| 2 | + | |
| 3 | +The pattern is given by a series of tuples, of brightness and duration (ms). | |
| 4 | +The LED is expected to traverse the series and each brightness value for the | |
| 5 | +specified duration. Duration of 0 means brightness should immediately change to | |
| 6 | +new value, and writing malformed pattern deactivates any active one. | |
| 7 | + | |
| 8 | +1. For gradual dimming, the dimming interval now is set as 50 milliseconds. So | |
| 9 | +the tuple with duration less than dimming interval (50ms) is treated as a step | |
| 10 | +change of brightness, i.e. the subsequent brightness will be applied without | |
| 11 | +adding intervening dimming intervals. | |
| 12 | + | |
| 13 | +The gradual dimming format of the software pattern values should be: | |
| 14 | +"brightness_1 duration_1 brightness_2 duration_2 brightness_3 duration_3 ...". | |
| 15 | +For example (using sysfs interface): | |
| 16 | + | |
| 17 | +echo 0 1000 255 2000 > pattern | |
| 18 | + | |
| 19 | +It will make the LED go gradually from zero-intensity to max (255) intensity in | |
| 20 | +1000 milliseconds, then back to zero intensity in 2000 milliseconds: | |
| 21 | + | |
| 22 | +LED brightness | |
| 23 | + ^ | |
| 24 | +255-| / \ / \ / | |
| 25 | + | / \ / \ / | |
| 26 | + | / \ / \ / | |
| 27 | + | / \ / \ / | |
| 28 | + 0-| / \/ \/ | |
| 29 | + +---0----1----2----3----4----5----6------------> time (s) | |
| 30 | + | |
| 31 | +2. To make the LED go instantly from one brightness value to another, we should | |
| 32 | +use zero-time lengths (the brightness must be same as the previous tuple's). So | |
| 33 | +the format should be: "brightness_1 duration_1 brightness_1 0 brightness_2 | |
| 34 | +duration_2 brightness_2 0 ...". | |
| 35 | +For example (using sysfs interface): | |
| 36 | + | |
| 37 | +echo 0 1000 0 0 255 2000 255 0 > pattern | |
| 38 | + | |
| 39 | +It will make the LED stay off for one second, then stay at max brightness for | |
| 40 | +two seconds: | |
| 41 | + | |
| 42 | +LED brightness | |
| 43 | + ^ | |
| 44 | +255-| +---------+ +---------+ | |
| 45 | + | | | | | | |
| 46 | + | | | | | | |
| 47 | + | | | | | | |
| 48 | + 0-| -----+ +----+ +---- | |
| 49 | + +---0----1----2----3----4----5----6------------> time (s) | |
| @@ -16,7 +16,9 @@ · 15 unmodified lines | ||
| 16 | 16 | #include <linux/list.h> |
| 17 | 17 | #include <linux/module.h> |
| 18 | 18 | #include <linux/mutex.h> |
| 19 | +#include <linux/of.h> | |
| 19 | 20 | #include <linux/rwsem.h> |
| 21 | +#include <linux/slab.h> | |
| 20 | 22 | #include "leds.h" |
| 21 | 23 | |
| 22 | 24 | DECLARE_RWSEM(leds_list_lock); |
| @@ -310,6 +312,34 @@ int led_update_brightness(struct led_classdev *led_cdev) · 287 unmodified lines | ||
| 310 | 312 | } |
| 311 | 313 | EXPORT_SYMBOL_GPL(led_update_brightness); |
| 312 | 314 | |
| 315 | +u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size) | |
| 316 | +{ | |
| 317 | + struct device_node *np = dev_of_node(led_cdev->dev); | |
| 318 | + u32 *pattern; | |
| 319 | + int count; | |
| 320 | + | |
| 321 | + if (!np) | |
| 322 | + return NULL; | |
| 323 | + | |
| 324 | + count = of_property_count_u32_elems(np, "led-pattern"); | |
| 325 | + if (count < 0) | |
| 326 | + return NULL; | |
| 327 | + | |
| 328 | + pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL); | |
| 329 | + if (!pattern) | |
| 330 | + return NULL; | |
| 331 | + | |
| 332 | + if (of_property_read_u32_array(np, "led-pattern", pattern, count)) { | |
| 333 | + kfree(pattern); | |
| 334 | + return NULL; | |
| 335 | + } | |
| 336 | + | |
| 337 | + *size = count; | |
| 338 | + | |
| 339 | + return pattern; | |
| 340 | +} | |
| 341 | +EXPORT_SYMBOL_GPL(led_get_default_pattern); | |
| 342 | + | |
| 313 | 343 | /* Caller must ensure led_cdev->led_access held */ |
| 314 | 344 | void led_sysfs_disable(struct led_classdev *led_cdev) |
| 315 | 345 | { |
| @@ -201,7 +201,7 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) · 200 unmodified lines | ||
| 201 | 201 | |
| 202 | 202 | if (!fw) { |
| 203 | 203 | dev_err(dev, "firmware request failed\n"); |
| 204 | − goto out; | |
| 204 | + return; | |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /* handling firmware data is chip dependent */ |
| @@ -214,9 +214,9 @@ static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) · 6 unmodified lines | ||
| 214 | 214 | |
| 215 | 215 | mutex_unlock(&chip->lock); |
| 216 | 216 | |
| 217 | −out: | |
| 218 | 217 | /* firmware should be released for other channel use */ |
| 219 | 218 | release_firmware(chip->fw); |
| 219 | + chip->fw = NULL; | |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | static int lp55xx_request_firmware(struct lp55xx_chip *chip) |
| @@ -130,6 +130,34 @@ static struct attribute *oneshot_trig_attrs[] = { · 129 unmodified lines | ||
| 130 | 130 | }; |
| 131 | 131 | ATTRIBUTE_GROUPS(oneshot_trig); |
| 132 | 132 | |
| 133 | +static void pattern_init(struct led_classdev *led_cdev) | |
| 134 | +{ | |
| 135 | + u32 *pattern; | |
| 136 | + unsigned int size = 0; | |
| 137 | + | |
| 138 | + pattern = led_get_default_pattern(led_cdev, &size); | |
| 139 | + if (!pattern) | |
| 140 | + goto out_default; | |
| 141 | + | |
| 142 | + if (size != 2) { | |
| 143 | + dev_warn(led_cdev->dev, | |
| 144 | + "Expected 2 but got %u values for delays pattern\n", | |
| 145 | + size); | |
| 146 | + goto out_default; | |
| 147 | + } | |
| 148 | + | |
| 149 | + led_cdev->blink_delay_on = pattern[0]; | |
| 150 | + led_cdev->blink_delay_off = pattern[1]; | |
| 151 | + kfree(pattern); | |
| 152 | + | |
| 153 | + return; | |
| 154 | + | |
| 155 | +out_default: | |
| 156 | + kfree(pattern); | |
| 157 | + led_cdev->blink_delay_on = DEFAULT_DELAY; | |
| 158 | + led_cdev->blink_delay_off = DEFAULT_DELAY; | |
| 159 | +} | |
| 160 | + | |
| 133 | 161 | static int oneshot_trig_activate(struct led_classdev *led_cdev) |
| 134 | 162 | { |
| 135 | 163 | struct oneshot_trig_data *oneshot_data; |
| @@ -140,8 +168,14 @@ static int oneshot_trig_activate(struct led_classdev *led_cdev) · 4 unmodified lines | ||
| 140 | 168 | |
| 141 | 169 | led_set_trigger_data(led_cdev, oneshot_data); |
| 142 | 170 | |
| 143 | − led_cdev->blink_delay_on = DEFAULT_DELAY; | |
| 144 | − led_cdev->blink_delay_off = DEFAULT_DELAY; | |
| 171 | + if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) { | |
| 172 | + pattern_init(led_cdev); | |
| 173 | + /* | |
| 174 | + * Mark as initialized even on pattern_init() error because | |
| 175 | + * any consecutive call to it would produce the same error. | |
| 176 | + */ | |
| 177 | + led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER; | |
| 178 | + } | |
| 145 | 179 | |
| 146 | 180 | return 0; |
| 147 | 181 | } |
| @@ -220,22 +220,10 @@ out: · 219 unmodified lines | ||
| 220 | 220 | return count; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | −static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, | |
| 224 | − const char *buf, size_t count, | |
| 225 | − bool hw_pattern) | |
| 223 | +static int pattern_trig_store_patterns_string(struct pattern_trig_data *data, | |
| 224 | + const char *buf, size_t count) | |
| 226 | 225 | { |
| 227 | − struct pattern_trig_data *data = led_cdev->trigger_data; | |
| 228 | − int ccount, cr, offset = 0, err = 0; | |
| 229 | − | |
| 230 | − mutex_lock(&data->lock); | |
| 231 | − | |
| 232 | − del_timer_sync(&data->timer); | |
| 233 | − | |
| 234 | − if (data->is_hw_pattern) | |
| 235 | − led_cdev->pattern_clear(led_cdev); | |
| 236 | − | |
| 237 | − data->is_hw_pattern = hw_pattern; | |
| 238 | − data->npatterns = 0; | |
| 226 | + int ccount, cr, offset = 0; | |
| 239 | 227 | |
| 240 | 228 | while (offset < count - 1 && data->npatterns < MAX_PATTERNS) { |
| 241 | 229 | cr = 0; |
| @@ -244,14 +232,54 @@ static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, · 2 unmodified lines | ||
| 244 | 232 | &data->patterns[data->npatterns].delta_t, &cr); |
| 245 | 233 | if (ccount != 2) { |
| 246 | 234 | data->npatterns = 0; |
| 247 | − err = -EINVAL; | |
| 248 | − goto out; | |
| 235 | + return -EINVAL; | |
| 249 | 236 | } |
| 250 | 237 | |
| 251 | 238 | offset += cr; |
| 252 | 239 | data->npatterns++; |
| 253 | 240 | } |
| 254 | 241 | |
| 242 | + return 0; | |
| 243 | +} | |
| 244 | + | |
| 245 | +static int pattern_trig_store_patterns_int(struct pattern_trig_data *data, | |
| 246 | + const u32 *buf, size_t count) | |
| 247 | +{ | |
| 248 | + unsigned int i; | |
| 249 | + | |
| 250 | + for (i = 0; i < count; i += 2) { | |
| 251 | + data->patterns[data->npatterns].brightness = buf[i]; | |
| 252 | + data->patterns[data->npatterns].delta_t = buf[i + 1]; | |
| 253 | + data->npatterns++; | |
| 254 | + } | |
| 255 | + | |
| 256 | + return 0; | |
| 257 | +} | |
| 258 | + | |
| 259 | +static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, | |
| 260 | + const char *buf, const u32 *buf_int, | |
| 261 | + size_t count, bool hw_pattern) | |
| 262 | +{ | |
| 263 | + struct pattern_trig_data *data = led_cdev->trigger_data; | |
| 264 | + int err = 0; | |
| 265 | + | |
| 266 | + mutex_lock(&data->lock); | |
| 267 | + | |
| 268 | + del_timer_sync(&data->timer); | |
| 269 | + | |
| 270 | + if (data->is_hw_pattern) | |
| 271 | + led_cdev->pattern_clear(led_cdev); | |
| 272 | + | |
| 273 | + data->is_hw_pattern = hw_pattern; | |
| 274 | + data->npatterns = 0; | |
| 275 | + | |
| 276 | + if (buf) | |
| 277 | + err = pattern_trig_store_patterns_string(data, buf, count); | |
| 278 | + else | |
| 279 | + err = pattern_trig_store_patterns_int(data, buf_int, count); | |
| 280 | + if (err) | |
| 281 | + goto out; | |
| 282 | + | |
| 255 | 283 | err = pattern_trig_start_pattern(led_cdev); |
| 256 | 284 | if (err) |
| 257 | 285 | data->npatterns = 0; |
| @@ -275,7 +303,7 @@ static ssize_t pattern_store(struct device *dev, struct device_attribute *attr, · 17 unmodified lines | ||
| 275 | 303 | { |
| 276 | 304 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 277 | 305 | |
| 278 | − return pattern_trig_store_patterns(led_cdev, buf, count, false); | |
| 306 | + return pattern_trig_store_patterns(led_cdev, buf, NULL, count, false); | |
| 279 | 307 | } |
| 280 | 308 | |
| 281 | 309 | static DEVICE_ATTR_RW(pattern); |
| @@ -295,7 +323,7 @@ static ssize_t hw_pattern_store(struct device *dev, · 13 unmodified lines | ||
| 295 | 323 | { |
| 296 | 324 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 297 | 325 | |
| 298 | − return pattern_trig_store_patterns(led_cdev, buf, count, true); | |
| 326 | + return pattern_trig_store_patterns(led_cdev, buf, NULL, count, true); | |
| 299 | 327 | } |
| 300 | 328 | |
| 301 | 329 | static DEVICE_ATTR_RW(hw_pattern); |
| @@ -331,6 +359,30 @@ static const struct attribute_group *pattern_trig_groups[] = { · 29 unmodified lines | ||
| 331 | 359 | NULL, |
| 332 | 360 | }; |
| 333 | 361 | |
| 362 | +static void pattern_init(struct led_classdev *led_cdev) | |
| 363 | +{ | |
| 364 | + unsigned int size = 0; | |
| 365 | + u32 *pattern; | |
| 366 | + int err; | |
| 367 | + | |
| 368 | + pattern = led_get_default_pattern(led_cdev, &size); | |
| 369 | + if (!pattern) | |
| 370 | + return; | |
| 371 | + | |
| 372 | + if (size % 2) { | |
| 373 | + dev_warn(led_cdev->dev, "Expected pattern of tuples\n"); | |
| 374 | + goto out; | |
| 375 | + } | |
| 376 | + | |
| 377 | + err = pattern_trig_store_patterns(led_cdev, NULL, pattern, size, false); | |
| 378 | + if (err < 0) | |
| 379 | + dev_warn(led_cdev->dev, | |
| 380 | + "Pattern initialization failed with error %d\n", err); | |
| 381 | + | |
| 382 | +out: | |
| 383 | + kfree(pattern); | |
| 384 | +} | |
| 385 | + | |
| 334 | 386 | static int pattern_trig_activate(struct led_classdev *led_cdev) |
| 335 | 387 | { |
| 336 | 388 | struct pattern_trig_data *data; |
| @@ -354,6 +406,15 @@ static int pattern_trig_activate(struct led_classdev *led_cdev) · 17 unmodified lines | ||
| 354 | 406 | timer_setup(&data->timer, pattern_trig_timer_function, 0); |
| 355 | 407 | led_cdev->activated = true; |
| 356 | 408 | |
| 409 | + if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) { | |
| 410 | + pattern_init(led_cdev); | |
| 411 | + /* | |
| 412 | + * Mark as initialized even on pattern_init() error because | |
| 413 | + * any consecutive call to it would produce the same error. | |
| 414 | + */ | |
| 415 | + led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER; | |
| 416 | + } | |
| 417 | + | |
| 357 | 418 | return 0; |
| 358 | 419 | } |
| 359 | 420 | |
| @@ -15,6 +15,7 @@ · 14 unmodified lines | ||
| 15 | 15 | #include <linux/init.h> |
| 16 | 16 | #include <linux/device.h> |
| 17 | 17 | #include <linux/ctype.h> |
| 18 | +#include <linux/slab.h> | |
| 18 | 19 | #include <linux/leds.h> |
| 19 | 20 | |
| 20 | 21 | static ssize_t led_delay_on_show(struct device *dev, |
| @@ -77,8 +78,41 @@ static struct attribute *timer_trig_attrs[] = { · 56 unmodified lines | ||
| 77 | 78 | }; |
| 78 | 79 | ATTRIBUTE_GROUPS(timer_trig); |
| 79 | 80 | |
| 81 | +static void pattern_init(struct led_classdev *led_cdev) | |
| 82 | +{ | |
| 83 | + u32 *pattern; | |
| 84 | + unsigned int size = 0; | |
| 85 | + | |
| 86 | + pattern = led_get_default_pattern(led_cdev, &size); | |
| 87 | + if (!pattern) | |
| 88 | + return; | |
| 89 | + | |
| 90 | + if (size != 2) { | |
| 91 | + dev_warn(led_cdev->dev, | |
| 92 | + "Expected 2 but got %u values for delays pattern\n", | |
| 93 | + size); | |
| 94 | + goto out; | |
| 95 | + } | |
| 96 | + | |
| 97 | + led_cdev->blink_delay_on = pattern[0]; | |
| 98 | + led_cdev->blink_delay_off = pattern[1]; | |
| 99 | + /* led_blink_set() called by caller */ | |
| 100 | + | |
| 101 | +out: | |
| 102 | + kfree(pattern); | |
| 103 | +} | |
| 104 | + | |
| 80 | 105 | static int timer_trig_activate(struct led_classdev *led_cdev) |
| 81 | 106 | { |
| 107 | + if (led_cdev->flags & LED_INIT_DEFAULT_TRIGGER) { | |
| 108 | + pattern_init(led_cdev); | |
| 109 | + /* | |
| 110 | + * Mark as initialized even on pattern_init() error because | |
| 111 | + * any consecutive call to it would produce the same error. | |
| 112 | + */ | |
| 113 | + led_cdev->flags &= ~LED_INIT_DEFAULT_TRIGGER; | |
| 114 | + } | |
| 115 | + | |
| 82 | 116 | led_blink_set(led_cdev, &led_cdev->blink_delay_on, |
| 83 | 117 | &led_cdev->blink_delay_off); |
| 84 | 118 | |
| @@ -218,6 +218,19 @@ extern int led_set_brightness_sync(struct led_classdev *led_cdev, · 217 unmodified lines | ||
| 218 | 218 | */ |
| 219 | 219 | extern int led_update_brightness(struct led_classdev *led_cdev); |
| 220 | 220 | |
| 221 | +/** | |
| 222 | + * led_get_default_pattern - return default pattern | |
| 223 | + * | |
| 224 | + * @led_cdev: the LED to get default pattern for | |
| 225 | + * @size: pointer for storing the number of elements in returned array, | |
| 226 | + * modified only if return != NULL | |
| 227 | + * | |
| 228 | + * Return: Allocated array of integers with default pattern from device tree | |
| 229 | + * or NULL. Caller is responsible for kfree(). | |
| 230 | + */ | |
| 231 | +extern u32 *led_get_default_pattern(struct led_classdev *led_cdev, | |
| 232 | + unsigned int *size); | |
| 233 | + | |
| 221 | 234 | /** |
| 222 | 235 | * led_sysfs_disable - disable LED sysfs interface |
| 223 | 236 | * @led_cdev: the LED to set |