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