Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 2 | /* |
| 3 | * LED Triggers Core |
| 4 | * |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame] | 5 | * Copyright 2005-2007 Openedhand Ltd. |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 6 | * |
| 7 | * Author: Richard Purdie <rpurdie@openedhand.com> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
Paul Gortmaker | 5023786 | 2016-08-15 16:54:48 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 12 | #include <linux/list.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/device.h> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 15 | #include <linux/timer.h> |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 16 | #include <linux/rwsem.h> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 17 | #include <linux/leds.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 19 | #include <linux/mm.h> |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 20 | #include "leds.h" |
| 21 | |
| 22 | /* |
| 23 | * Nests outside led_cdev->trigger_lock |
| 24 | */ |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 25 | static DECLARE_RWSEM(triggers_list_lock); |
Ezequiel Garcia | ba93cdc | 2016-04-28 19:03:38 -0300 | [diff] [blame] | 26 | LIST_HEAD(trigger_list); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 27 | |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 28 | /* Used by LED Class */ |
| 29 | |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 30 | ssize_t led_trigger_write(struct file *filp, struct kobject *kobj, |
| 31 | struct bin_attribute *bin_attr, char *buf, |
| 32 | loff_t pos, size_t count) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 33 | { |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 34 | struct device *dev = kobj_to_dev(kobj); |
Richard Purdie | f8a7c6f | 2007-07-08 23:19:31 +0100 | [diff] [blame] | 35 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 36 | struct led_trigger *trig; |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 37 | int ret = count; |
| 38 | |
| 39 | mutex_lock(&led_cdev->led_access); |
| 40 | |
| 41 | if (led_sysfs_is_disabled(led_cdev)) { |
| 42 | ret = -EBUSY; |
| 43 | goto unlock; |
| 44 | } |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 45 | |
Heiner Kallweit | 7296c33 | 2016-03-08 23:08:36 +0100 | [diff] [blame] | 46 | if (sysfs_streq(buf, "none")) { |
Németh Márton | 0013b23 | 2008-03-09 20:54:37 +0000 | [diff] [blame] | 47 | led_trigger_remove(led_cdev); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 48 | goto unlock; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 51 | down_read(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 52 | list_for_each_entry(trig, &trigger_list, next_trig) { |
Heiner Kallweit | 7296c33 | 2016-03-08 23:08:36 +0100 | [diff] [blame] | 53 | if (sysfs_streq(buf, trig->name)) { |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 54 | down_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 55 | led_trigger_set(led_cdev, trig); |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 56 | up_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 57 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 58 | up_read(&triggers_list_lock); |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 59 | goto unlock; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
Heiner Kallweit | a3eac76 | 2016-07-01 23:08:54 +0200 | [diff] [blame] | 62 | /* we come here only if buf matches no trigger */ |
| 63 | ret = -EINVAL; |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 64 | up_read(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 65 | |
Jacek Anaszewski | acd899e | 2014-09-22 08:21:04 -0700 | [diff] [blame] | 66 | unlock: |
| 67 | mutex_unlock(&led_cdev->led_access); |
| 68 | return ret; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 69 | } |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(led_trigger_write); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 71 | |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 72 | __printf(3, 4) |
| 73 | static int led_trigger_snprintf(char *buf, ssize_t size, const char *fmt, ...) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 74 | { |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 75 | va_list args; |
| 76 | int i; |
| 77 | |
| 78 | va_start(args, fmt); |
| 79 | if (size <= 0) |
| 80 | i = vsnprintf(NULL, 0, fmt, args); |
| 81 | else |
| 82 | i = vscnprintf(buf, size, fmt, args); |
| 83 | va_end(args); |
| 84 | |
| 85 | return i; |
| 86 | } |
| 87 | |
| 88 | static int led_trigger_format(char *buf, size_t size, |
| 89 | struct led_classdev *led_cdev) |
| 90 | { |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 91 | struct led_trigger *trig; |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 92 | int len = led_trigger_snprintf(buf, size, "%s", |
| 93 | led_cdev->trigger ? "none" : "[none]"); |
| 94 | |
| 95 | list_for_each_entry(trig, &trigger_list, next_trig) { |
| 96 | bool hit = led_cdev->trigger && |
| 97 | !strcmp(led_cdev->trigger->name, trig->name); |
| 98 | |
| 99 | len += led_trigger_snprintf(buf + len, size - len, |
| 100 | " %s%s%s", hit ? "[" : "", |
| 101 | trig->name, hit ? "]" : ""); |
| 102 | } |
| 103 | |
| 104 | len += led_trigger_snprintf(buf + len, size - len, "\n"); |
| 105 | |
| 106 | return len; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * It was stupid to create 10000 cpu triggers, but we are stuck with it now. |
| 111 | * Don't make that mistake again. We work around it here by creating binary |
| 112 | * attribute, which is not limited by length. This is _not_ good design, do not |
| 113 | * copy it. |
| 114 | */ |
| 115 | ssize_t led_trigger_read(struct file *filp, struct kobject *kobj, |
| 116 | struct bin_attribute *attr, char *buf, |
| 117 | loff_t pos, size_t count) |
| 118 | { |
| 119 | struct device *dev = kobj_to_dev(kobj); |
| 120 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 121 | void *data; |
| 122 | int len; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 123 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 124 | down_read(&triggers_list_lock); |
| 125 | down_read(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 126 | |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 127 | len = led_trigger_format(NULL, 0, led_cdev); |
| 128 | data = kvmalloc(len + 1, GFP_KERNEL); |
| 129 | if (!data) { |
| 130 | up_read(&led_cdev->trigger_lock); |
| 131 | up_read(&triggers_list_lock); |
| 132 | return -ENOMEM; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 133 | } |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 134 | len = led_trigger_format(data, len + 1, led_cdev); |
| 135 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 136 | up_read(&led_cdev->trigger_lock); |
| 137 | up_read(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 138 | |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 139 | len = memory_read_from_buffer(buf, count, &pos, data, len); |
| 140 | |
| 141 | kvfree(data); |
| 142 | |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 143 | return len; |
| 144 | } |
Akinobu Mita | 11f7000 | 2019-09-29 23:18:49 +0900 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(led_trigger_read); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 146 | |
| 147 | /* Caller must ensure led_cdev->trigger_lock held */ |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 148 | int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 149 | { |
| 150 | unsigned long flags; |
Colin Cross | 52c4774 | 2012-08-27 09:31:49 +0800 | [diff] [blame] | 151 | char *event = NULL; |
| 152 | char *envp[2]; |
| 153 | const char *name; |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 154 | int ret; |
Colin Cross | 52c4774 | 2012-08-27 09:31:49 +0800 | [diff] [blame] | 155 | |
Jacek Anaszewski | fbfa197 | 2016-09-18 20:24:29 +0200 | [diff] [blame] | 156 | if (!led_cdev->trigger && !trig) |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 157 | return 0; |
Jacek Anaszewski | fbfa197 | 2016-09-18 20:24:29 +0200 | [diff] [blame] | 158 | |
Colin Cross | 52c4774 | 2012-08-27 09:31:49 +0800 | [diff] [blame] | 159 | name = trig ? trig->name : "none"; |
| 160 | event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 161 | |
| 162 | /* Remove any existing trigger */ |
| 163 | if (led_cdev->trigger) { |
| 164 | write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); |
| 165 | list_del(&led_cdev->trig_list); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 166 | write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, |
| 167 | flags); |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 168 | cancel_work_sync(&led_cdev->set_brightness_work); |
| 169 | led_stop_software_blink(led_cdev); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 170 | if (led_cdev->trigger->deactivate) |
| 171 | led_cdev->trigger->deactivate(led_cdev); |
Uwe Kleine-König | a7e7a31 | 2018-07-02 22:05:22 +0200 | [diff] [blame] | 172 | device_remove_groups(led_cdev->dev, led_cdev->trigger->groups); |
Dmitry Baryshkov | fe3025b | 2008-07-16 22:51:14 +0100 | [diff] [blame] | 173 | led_cdev->trigger = NULL; |
Uwe Kleine-König | a7d5904 | 2018-07-02 22:05:23 +0200 | [diff] [blame] | 174 | led_cdev->trigger_data = NULL; |
| 175 | led_cdev->activated = false; |
Shuah Khan | 19cd67e | 2012-06-14 04:34:30 +0800 | [diff] [blame] | 176 | led_set_brightness(led_cdev, LED_OFF); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 177 | } |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 178 | if (trig) { |
| 179 | write_lock_irqsave(&trig->leddev_list_lock, flags); |
| 180 | list_add_tail(&led_cdev->trig_list, &trig->led_cdevs); |
| 181 | write_unlock_irqrestore(&trig->leddev_list_lock, flags); |
| 182 | led_cdev->trigger = trig; |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 183 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 184 | if (trig->activate) |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 185 | ret = trig->activate(led_cdev); |
| 186 | else |
| 187 | ret = 0; |
| 188 | |
| 189 | if (ret) |
| 190 | goto err_activate; |
Uwe Kleine-König | a7e7a31 | 2018-07-02 22:05:22 +0200 | [diff] [blame] | 191 | |
| 192 | ret = device_add_groups(led_cdev->dev, trig->groups); |
| 193 | if (ret) { |
| 194 | dev_err(led_cdev->dev, "Failed to add trigger attributes\n"); |
| 195 | goto err_add_groups; |
| 196 | } |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 197 | } |
Colin Cross | 52c4774 | 2012-08-27 09:31:49 +0800 | [diff] [blame] | 198 | |
| 199 | if (event) { |
| 200 | envp[0] = event; |
| 201 | envp[1] = NULL; |
Jacek Anaszewski | 6f3bad9 | 2016-09-19 12:44:50 +0200 | [diff] [blame] | 202 | if (kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp)) |
| 203 | dev_err(led_cdev->dev, |
| 204 | "%s: Error sending uevent\n", __func__); |
Colin Cross | 52c4774 | 2012-08-27 09:31:49 +0800 | [diff] [blame] | 205 | kfree(event); |
| 206 | } |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | |
Uwe Kleine-König | a7e7a31 | 2018-07-02 22:05:22 +0200 | [diff] [blame] | 210 | err_add_groups: |
| 211 | |
| 212 | if (trig->deactivate) |
| 213 | trig->deactivate(led_cdev); |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 214 | err_activate: |
Uwe Kleine-König | a7e7a31 | 2018-07-02 22:05:22 +0200 | [diff] [blame] | 215 | |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 216 | write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); |
| 217 | list_del(&led_cdev->trig_list); |
| 218 | write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags); |
Oleh Kravchenko | 4016ba8 | 2019-09-04 00:18:19 +0300 | [diff] [blame] | 219 | led_cdev->trigger = NULL; |
| 220 | led_cdev->trigger_data = NULL; |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 221 | led_set_brightness(led_cdev, LED_OFF); |
Wenwen Wang | 60e2dde | 2019-08-19 15:41:42 -0500 | [diff] [blame] | 222 | kfree(event); |
Uwe Kleine-König | 2282e125 | 2018-07-02 22:05:21 +0200 | [diff] [blame] | 223 | |
| 224 | return ret; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 225 | } |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(led_trigger_set); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 227 | |
Németh Márton | 0013b23 | 2008-03-09 20:54:37 +0000 | [diff] [blame] | 228 | void led_trigger_remove(struct led_classdev *led_cdev) |
| 229 | { |
| 230 | down_write(&led_cdev->trigger_lock); |
| 231 | led_trigger_set(led_cdev, NULL); |
| 232 | up_write(&led_cdev->trigger_lock); |
| 233 | } |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 234 | EXPORT_SYMBOL_GPL(led_trigger_remove); |
Németh Márton | 0013b23 | 2008-03-09 20:54:37 +0000 | [diff] [blame] | 235 | |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 236 | void led_trigger_set_default(struct led_classdev *led_cdev) |
| 237 | { |
| 238 | struct led_trigger *trig; |
| 239 | |
| 240 | if (!led_cdev->default_trigger) |
| 241 | return; |
| 242 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 243 | down_read(&triggers_list_lock); |
| 244 | down_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 245 | list_for_each_entry(trig, &trigger_list, next_trig) { |
Jacek Anaszewski | c4f7bd4 | 2018-12-10 10:29:57 +0100 | [diff] [blame] | 246 | if (!strcmp(led_cdev->default_trigger, trig->name)) { |
Jacek Anaszewski | 02d3176 | 2018-12-10 10:29:58 +0100 | [diff] [blame] | 247 | led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 248 | led_trigger_set(led_cdev, trig); |
Jacek Anaszewski | c4f7bd4 | 2018-12-10 10:29:57 +0100 | [diff] [blame] | 249 | break; |
| 250 | } |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 251 | } |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 252 | up_write(&led_cdev->trigger_lock); |
| 253 | up_read(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 254 | } |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 255 | EXPORT_SYMBOL_GPL(led_trigger_set_default); |
| 256 | |
Fabio Baltieri | a8df7b1 | 2012-11-04 01:54:34 -0800 | [diff] [blame] | 257 | void led_trigger_rename_static(const char *name, struct led_trigger *trig) |
| 258 | { |
| 259 | /* new name must be on a temporary string to prevent races */ |
| 260 | BUG_ON(name == trig->name); |
| 261 | |
| 262 | down_write(&triggers_list_lock); |
| 263 | /* this assumes that trig->name was originaly allocated to |
| 264 | * non constant storage */ |
| 265 | strcpy((char *)trig->name, name); |
| 266 | up_write(&triggers_list_lock); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(led_trigger_rename_static); |
| 269 | |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 270 | /* LED Trigger Interface */ |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 271 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 272 | int led_trigger_register(struct led_trigger *trig) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 273 | { |
| 274 | struct led_classdev *led_cdev; |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 275 | struct led_trigger *_trig; |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 276 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 277 | rwlock_init(&trig->leddev_list_lock); |
| 278 | INIT_LIST_HEAD(&trig->led_cdevs); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 279 | |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 280 | down_write(&triggers_list_lock); |
Adam Nielsen | 700c6ea | 2009-02-18 08:18:04 +1000 | [diff] [blame] | 281 | /* Make sure the trigger's name isn't already in use */ |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 282 | list_for_each_entry(_trig, &trigger_list, next_trig) { |
| 283 | if (!strcmp(_trig->name, trig->name)) { |
Adam Nielsen | 700c6ea | 2009-02-18 08:18:04 +1000 | [diff] [blame] | 284 | up_write(&triggers_list_lock); |
| 285 | return -EEXIST; |
| 286 | } |
| 287 | } |
| 288 | /* Add to the list of led triggers */ |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 289 | list_add_tail(&trig->next_trig, &trigger_list); |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 290 | up_write(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 291 | |
| 292 | /* Register with any LEDs that have this as a default trigger */ |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 293 | down_read(&leds_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 294 | list_for_each_entry(led_cdev, &leds_list, node) { |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 295 | down_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 296 | if (!led_cdev->trigger && led_cdev->default_trigger && |
Krzysztof Kozlowski | 8146aac | 2018-12-10 10:29:59 +0100 | [diff] [blame] | 297 | !strcmp(led_cdev->default_trigger, trig->name)) { |
| 298 | led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER; |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 299 | led_trigger_set(led_cdev, trig); |
Krzysztof Kozlowski | 8146aac | 2018-12-10 10:29:59 +0100 | [diff] [blame] | 300 | } |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 301 | up_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 302 | } |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 303 | up_read(&leds_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 307 | EXPORT_SYMBOL_GPL(led_trigger_register); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 308 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 309 | void led_trigger_unregister(struct led_trigger *trig) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 310 | { |
| 311 | struct led_classdev *led_cdev; |
| 312 | |
Sasha Levin | 14f5716 | 2014-04-04 10:01:13 -0700 | [diff] [blame] | 313 | if (list_empty_careful(&trig->next_trig)) |
| 314 | return; |
| 315 | |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 316 | /* Remove from the list of led triggers */ |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 317 | down_write(&triggers_list_lock); |
Sasha Levin | 14f5716 | 2014-04-04 10:01:13 -0700 | [diff] [blame] | 318 | list_del_init(&trig->next_trig); |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 319 | up_write(&triggers_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 320 | |
| 321 | /* Remove anyone actively using this trigger */ |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 322 | down_read(&leds_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 323 | list_for_each_entry(led_cdev, &leds_list, node) { |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 324 | down_write(&led_cdev->trigger_lock); |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 325 | if (led_cdev->trigger == trig) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 326 | led_trigger_set(led_cdev, NULL); |
Richard Purdie | dc47206 | 2007-11-10 13:29:04 +0000 | [diff] [blame] | 327 | up_write(&led_cdev->trigger_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 328 | } |
Richard Purdie | 72f8da3 | 2007-12-31 23:09:44 +0000 | [diff] [blame] | 329 | up_read(&leds_list_lock); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 330 | } |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 331 | EXPORT_SYMBOL_GPL(led_trigger_unregister); |
| 332 | |
Heiner Kallweit | 9534cc3 | 2016-01-02 01:36:41 +0100 | [diff] [blame] | 333 | static void devm_led_trigger_release(struct device *dev, void *res) |
| 334 | { |
| 335 | led_trigger_unregister(*(struct led_trigger **)res); |
| 336 | } |
| 337 | |
| 338 | int devm_led_trigger_register(struct device *dev, |
| 339 | struct led_trigger *trig) |
| 340 | { |
| 341 | struct led_trigger **dr; |
| 342 | int rc; |
| 343 | |
| 344 | dr = devres_alloc(devm_led_trigger_release, sizeof(*dr), |
| 345 | GFP_KERNEL); |
| 346 | if (!dr) |
| 347 | return -ENOMEM; |
| 348 | |
| 349 | *dr = trig; |
| 350 | |
| 351 | rc = led_trigger_register(trig); |
| 352 | if (rc) |
| 353 | devres_free(dr); |
| 354 | else |
| 355 | devres_add(dev, dr); |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(devm_led_trigger_register); |
| 360 | |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 361 | /* Simple LED Tigger Interface */ |
| 362 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 363 | void led_trigger_event(struct led_trigger *trig, |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 364 | enum led_brightness brightness) |
| 365 | { |
ZHAO Gang | 11e043b | 2013-12-28 07:00:26 -0800 | [diff] [blame] | 366 | struct led_classdev *led_cdev; |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 367 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 368 | if (!trig) |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 369 | return; |
| 370 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 371 | read_lock(&trig->leddev_list_lock); |
ZHAO Gang | 11e043b | 2013-12-28 07:00:26 -0800 | [diff] [blame] | 372 | list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) |
Fabio Baltieri | d23a22a | 2012-08-15 21:44:34 +0800 | [diff] [blame] | 373 | led_set_brightness(led_cdev, brightness); |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 374 | read_unlock(&trig->leddev_list_lock); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 375 | } |
| 376 | EXPORT_SYMBOL_GPL(led_trigger_event); |
| 377 | |
Bryan Wu | 20c0e6b | 2012-06-15 22:15:27 +0800 | [diff] [blame] | 378 | static void led_trigger_blink_setup(struct led_trigger *trig, |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 379 | unsigned long *delay_on, |
| 380 | unsigned long *delay_off, |
| 381 | int oneshot, |
| 382 | int invert) |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 383 | { |
ZHAO Gang | 11e043b | 2013-12-28 07:00:26 -0800 | [diff] [blame] | 384 | struct led_classdev *led_cdev; |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 385 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 386 | if (!trig) |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 387 | return; |
| 388 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 389 | read_lock(&trig->leddev_list_lock); |
ZHAO Gang | 11e043b | 2013-12-28 07:00:26 -0800 | [diff] [blame] | 390 | list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) { |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 391 | if (oneshot) |
| 392 | led_blink_set_oneshot(led_cdev, delay_on, delay_off, |
| 393 | invert); |
| 394 | else |
| 395 | led_blink_set(led_cdev, delay_on, delay_off); |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 396 | } |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 397 | read_unlock(&trig->leddev_list_lock); |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 398 | } |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 399 | |
| 400 | void led_trigger_blink(struct led_trigger *trig, |
| 401 | unsigned long *delay_on, |
| 402 | unsigned long *delay_off) |
| 403 | { |
| 404 | led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0); |
| 405 | } |
Vasily Khoruzhick | 0b9536c | 2011-01-07 16:28:16 +0000 | [diff] [blame] | 406 | EXPORT_SYMBOL_GPL(led_trigger_blink); |
| 407 | |
Fabio Baltieri | 5bb629c | 2012-05-27 07:19:22 +0800 | [diff] [blame] | 408 | void led_trigger_blink_oneshot(struct led_trigger *trig, |
| 409 | unsigned long *delay_on, |
| 410 | unsigned long *delay_off, |
| 411 | int invert) |
| 412 | { |
| 413 | led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert); |
| 414 | } |
| 415 | EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot); |
| 416 | |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 417 | void led_trigger_register_simple(const char *name, struct led_trigger **tp) |
| 418 | { |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 419 | struct led_trigger *trig; |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 420 | int err; |
| 421 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 422 | trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL); |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 423 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 424 | if (trig) { |
| 425 | trig->name = name; |
| 426 | err = led_trigger_register(trig); |
Masakazu Mokuno | cba4c2a | 2011-10-31 17:11:59 -0700 | [diff] [blame] | 427 | if (err < 0) { |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 428 | kfree(trig); |
| 429 | trig = NULL; |
Sachin Kamat | 09f5fe7 | 2012-11-25 13:00:44 +0530 | [diff] [blame] | 430 | pr_warn("LED trigger %s failed to register (%d)\n", |
| 431 | name, err); |
Masakazu Mokuno | cba4c2a | 2011-10-31 17:11:59 -0700 | [diff] [blame] | 432 | } |
Sachin Kamat | 09f5fe7 | 2012-11-25 13:00:44 +0530 | [diff] [blame] | 433 | } else { |
| 434 | pr_warn("LED trigger %s failed to register (no memory)\n", |
| 435 | name); |
| 436 | } |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 437 | *tp = trig; |
Németh Márton | 4d404fd | 2008-03-09 20:59:57 +0000 | [diff] [blame] | 438 | } |
| 439 | EXPORT_SYMBOL_GPL(led_trigger_register_simple); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 440 | |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 441 | void led_trigger_unregister_simple(struct led_trigger *trig) |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 442 | { |
Bryan Wu | eb20262 | 2012-04-19 11:46:33 +0800 | [diff] [blame] | 443 | if (trig) |
| 444 | led_trigger_unregister(trig); |
| 445 | kfree(trig); |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 446 | } |
Richard Purdie | c3bc995 | 2006-03-31 02:31:05 -0800 | [diff] [blame] | 447 | EXPORT_SYMBOL_GPL(led_trigger_unregister_simple); |