blob: 17d73db1456ebb4009c9dfa396820aa2e2485acd [file] [log] [blame]
Richard Purdiec3bc9952006-03-31 02:31:05 -08001/*
2 * LED Triggers Core
3 *
Richard Purdief8a7c6f2007-07-08 23:19:31 +01004 * Copyright 2005-2007 Openedhand Ltd.
Richard Purdiec3bc9952006-03-31 02:31:05 -08005 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
Paul Gortmaker50237862016-08-15 16:54:48 -040014#include <linux/export.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080015#include <linux/kernel.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080016#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <linux/device.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080019#include <linux/timer.h>
Richard Purdiedc472062007-11-10 13:29:04 +000020#include <linux/rwsem.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080021#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Richard Purdiec3bc9952006-03-31 02:31:05 -080023#include "leds.h"
24
25/*
26 * Nests outside led_cdev->trigger_lock
27 */
Richard Purdiedc472062007-11-10 13:29:04 +000028static DECLARE_RWSEM(triggers_list_lock);
Ezequiel Garciaba93cdc2016-04-28 19:03:38 -030029LIST_HEAD(trigger_list);
Richard Purdiec3bc9952006-03-31 02:31:05 -080030
Németh Márton4d404fd2008-03-09 20:59:57 +000031 /* Used by LED Class */
32
Richard Purdief8a7c6f2007-07-08 23:19:31 +010033ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr,
34 const char *buf, size_t count)
Richard Purdiec3bc9952006-03-31 02:31:05 -080035{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010036 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdiec3bc9952006-03-31 02:31:05 -080037 struct led_trigger *trig;
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070038 int ret = count;
39
40 mutex_lock(&led_cdev->led_access);
41
42 if (led_sysfs_is_disabled(led_cdev)) {
43 ret = -EBUSY;
44 goto unlock;
45 }
Richard Purdiec3bc9952006-03-31 02:31:05 -080046
Heiner Kallweit7296c332016-03-08 23:08:36 +010047 if (sysfs_streq(buf, "none")) {
Németh Márton0013b232008-03-09 20:54:37 +000048 led_trigger_remove(led_cdev);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070049 goto unlock;
Richard Purdiec3bc9952006-03-31 02:31:05 -080050 }
51
Richard Purdiedc472062007-11-10 13:29:04 +000052 down_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080053 list_for_each_entry(trig, &trigger_list, next_trig) {
Heiner Kallweit7296c332016-03-08 23:08:36 +010054 if (sysfs_streq(buf, trig->name)) {
Richard Purdiedc472062007-11-10 13:29:04 +000055 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080056 led_trigger_set(led_cdev, trig);
Richard Purdiedc472062007-11-10 13:29:04 +000057 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080058
Richard Purdiedc472062007-11-10 13:29:04 +000059 up_read(&triggers_list_lock);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070060 goto unlock;
Richard Purdiec3bc9952006-03-31 02:31:05 -080061 }
62 }
Heiner Kallweita3eac762016-07-01 23:08:54 +020063 /* we come here only if buf matches no trigger */
64 ret = -EINVAL;
Richard Purdiedc472062007-11-10 13:29:04 +000065 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080066
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -070067unlock:
68 mutex_unlock(&led_cdev->led_access);
69 return ret;
Richard Purdiec3bc9952006-03-31 02:31:05 -080070}
Németh Márton4d404fd2008-03-09 20:59:57 +000071EXPORT_SYMBOL_GPL(led_trigger_store);
Richard Purdiec3bc9952006-03-31 02:31:05 -080072
Richard Purdief8a7c6f2007-07-08 23:19:31 +010073ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
74 char *buf)
Richard Purdiec3bc9952006-03-31 02:31:05 -080075{
Richard Purdief8a7c6f2007-07-08 23:19:31 +010076 struct led_classdev *led_cdev = dev_get_drvdata(dev);
Richard Purdiec3bc9952006-03-31 02:31:05 -080077 struct led_trigger *trig;
78 int len = 0;
79
Richard Purdiedc472062007-11-10 13:29:04 +000080 down_read(&triggers_list_lock);
81 down_read(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080082
83 if (!led_cdev->trigger)
Nathan Sullivan3b9b9532016-08-15 17:20:14 -050084 len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
Richard Purdiec3bc9952006-03-31 02:31:05 -080085 else
Nathan Sullivan3b9b9532016-08-15 17:20:14 -050086 len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
Richard Purdiec3bc9952006-03-31 02:31:05 -080087
88 list_for_each_entry(trig, &trigger_list, next_trig) {
89 if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
90 trig->name))
Nathan Sullivan3b9b9532016-08-15 17:20:14 -050091 len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
92 trig->name);
Richard Purdiec3bc9952006-03-31 02:31:05 -080093 else
Nathan Sullivan3b9b9532016-08-15 17:20:14 -050094 len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
95 trig->name);
Richard Purdiec3bc9952006-03-31 02:31:05 -080096 }
Richard Purdiedc472062007-11-10 13:29:04 +000097 up_read(&led_cdev->trigger_lock);
98 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -080099
Nathan Sullivan3b9b9532016-08-15 17:20:14 -0500100 len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
Richard Purdiec3bc9952006-03-31 02:31:05 -0800101 return len;
102}
Németh Márton4d404fd2008-03-09 20:59:57 +0000103EXPORT_SYMBOL_GPL(led_trigger_show);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800104
105/* Caller must ensure led_cdev->trigger_lock held */
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200106int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800107{
108 unsigned long flags;
Colin Cross52c47742012-08-27 09:31:49 +0800109 char *event = NULL;
110 char *envp[2];
111 const char *name;
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200112 int ret;
Colin Cross52c47742012-08-27 09:31:49 +0800113
Jacek Anaszewskifbfa1972016-09-18 20:24:29 +0200114 if (!led_cdev->trigger && !trig)
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200115 return 0;
Jacek Anaszewskifbfa1972016-09-18 20:24:29 +0200116
Colin Cross52c47742012-08-27 09:31:49 +0800117 name = trig ? trig->name : "none";
118 event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800119
120 /* Remove any existing trigger */
121 if (led_cdev->trigger) {
122 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
123 list_del(&led_cdev->trig_list);
Németh Márton4d404fd2008-03-09 20:59:57 +0000124 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock,
125 flags);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800126 cancel_work_sync(&led_cdev->set_brightness_work);
127 led_stop_software_blink(led_cdev);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800128 if (led_cdev->trigger->deactivate)
129 led_cdev->trigger->deactivate(led_cdev);
Uwe Kleine-Königa7e7a312018-07-02 22:05:22 +0200130 device_remove_groups(led_cdev->dev, led_cdev->trigger->groups);
Dmitry Baryshkovfe3025b2008-07-16 22:51:14 +0100131 led_cdev->trigger = NULL;
Uwe Kleine-Königa7d59042018-07-02 22:05:23 +0200132 led_cdev->trigger_data = NULL;
133 led_cdev->activated = false;
Shuah Khan19cd67e2012-06-14 04:34:30 +0800134 led_set_brightness(led_cdev, LED_OFF);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800135 }
Bryan Wueb202622012-04-19 11:46:33 +0800136 if (trig) {
137 write_lock_irqsave(&trig->leddev_list_lock, flags);
138 list_add_tail(&led_cdev->trig_list, &trig->led_cdevs);
139 write_unlock_irqrestore(&trig->leddev_list_lock, flags);
140 led_cdev->trigger = trig;
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200141
Bryan Wueb202622012-04-19 11:46:33 +0800142 if (trig->activate)
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200143 ret = trig->activate(led_cdev);
144 else
145 ret = 0;
146
147 if (ret)
148 goto err_activate;
Uwe Kleine-Königa7e7a312018-07-02 22:05:22 +0200149
150 ret = device_add_groups(led_cdev->dev, trig->groups);
151 if (ret) {
152 dev_err(led_cdev->dev, "Failed to add trigger attributes\n");
153 goto err_add_groups;
154 }
Richard Purdiec3bc9952006-03-31 02:31:05 -0800155 }
Colin Cross52c47742012-08-27 09:31:49 +0800156
157 if (event) {
158 envp[0] = event;
159 envp[1] = NULL;
Jacek Anaszewski6f3bad92016-09-19 12:44:50 +0200160 if (kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp))
161 dev_err(led_cdev->dev,
162 "%s: Error sending uevent\n", __func__);
Colin Cross52c47742012-08-27 09:31:49 +0800163 kfree(event);
164 }
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200165
166 return 0;
167
Uwe Kleine-Königa7e7a312018-07-02 22:05:22 +0200168err_add_groups:
169
170 if (trig->deactivate)
171 trig->deactivate(led_cdev);
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200172err_activate:
Uwe Kleine-Königa7e7a312018-07-02 22:05:22 +0200173
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200174 led_cdev->trigger = NULL;
Uwe Kleine-Königa7d59042018-07-02 22:05:23 +0200175 led_cdev->trigger_data = NULL;
Uwe Kleine-König2282e1252018-07-02 22:05:21 +0200176 write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags);
177 list_del(&led_cdev->trig_list);
178 write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags);
179 led_set_brightness(led_cdev, LED_OFF);
180
181 return ret;
Richard Purdiec3bc9952006-03-31 02:31:05 -0800182}
Németh Márton4d404fd2008-03-09 20:59:57 +0000183EXPORT_SYMBOL_GPL(led_trigger_set);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800184
Németh Márton0013b232008-03-09 20:54:37 +0000185void led_trigger_remove(struct led_classdev *led_cdev)
186{
187 down_write(&led_cdev->trigger_lock);
188 led_trigger_set(led_cdev, NULL);
189 up_write(&led_cdev->trigger_lock);
190}
Németh Márton4d404fd2008-03-09 20:59:57 +0000191EXPORT_SYMBOL_GPL(led_trigger_remove);
Németh Márton0013b232008-03-09 20:54:37 +0000192
Richard Purdiec3bc9952006-03-31 02:31:05 -0800193void led_trigger_set_default(struct led_classdev *led_cdev)
194{
195 struct led_trigger *trig;
196
197 if (!led_cdev->default_trigger)
198 return;
199
Richard Purdiedc472062007-11-10 13:29:04 +0000200 down_read(&triggers_list_lock);
201 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800202 list_for_each_entry(trig, &trigger_list, next_trig) {
203 if (!strcmp(led_cdev->default_trigger, trig->name))
204 led_trigger_set(led_cdev, trig);
205 }
Richard Purdiedc472062007-11-10 13:29:04 +0000206 up_write(&led_cdev->trigger_lock);
207 up_read(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800208}
Németh Márton4d404fd2008-03-09 20:59:57 +0000209EXPORT_SYMBOL_GPL(led_trigger_set_default);
210
Fabio Baltieria8df7b12012-11-04 01:54:34 -0800211void led_trigger_rename_static(const char *name, struct led_trigger *trig)
212{
213 /* new name must be on a temporary string to prevent races */
214 BUG_ON(name == trig->name);
215
216 down_write(&triggers_list_lock);
217 /* this assumes that trig->name was originaly allocated to
218 * non constant storage */
219 strcpy((char *)trig->name, name);
220 up_write(&triggers_list_lock);
221}
222EXPORT_SYMBOL_GPL(led_trigger_rename_static);
223
Németh Márton4d404fd2008-03-09 20:59:57 +0000224/* LED Trigger Interface */
Richard Purdiec3bc9952006-03-31 02:31:05 -0800225
Bryan Wueb202622012-04-19 11:46:33 +0800226int led_trigger_register(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800227{
228 struct led_classdev *led_cdev;
Bryan Wueb202622012-04-19 11:46:33 +0800229 struct led_trigger *_trig;
Richard Purdiec3bc9952006-03-31 02:31:05 -0800230
Bryan Wueb202622012-04-19 11:46:33 +0800231 rwlock_init(&trig->leddev_list_lock);
232 INIT_LIST_HEAD(&trig->led_cdevs);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800233
Richard Purdiedc472062007-11-10 13:29:04 +0000234 down_write(&triggers_list_lock);
Adam Nielsen700c6ea2009-02-18 08:18:04 +1000235 /* Make sure the trigger's name isn't already in use */
Bryan Wueb202622012-04-19 11:46:33 +0800236 list_for_each_entry(_trig, &trigger_list, next_trig) {
237 if (!strcmp(_trig->name, trig->name)) {
Adam Nielsen700c6ea2009-02-18 08:18:04 +1000238 up_write(&triggers_list_lock);
239 return -EEXIST;
240 }
241 }
242 /* Add to the list of led triggers */
Bryan Wueb202622012-04-19 11:46:33 +0800243 list_add_tail(&trig->next_trig, &trigger_list);
Richard Purdiedc472062007-11-10 13:29:04 +0000244 up_write(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800245
246 /* Register with any LEDs that have this as a default trigger */
Richard Purdie72f8da32007-12-31 23:09:44 +0000247 down_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800248 list_for_each_entry(led_cdev, &leds_list, node) {
Richard Purdiedc472062007-11-10 13:29:04 +0000249 down_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800250 if (!led_cdev->trigger && led_cdev->default_trigger &&
Bryan Wueb202622012-04-19 11:46:33 +0800251 !strcmp(led_cdev->default_trigger, trig->name))
252 led_trigger_set(led_cdev, trig);
Richard Purdiedc472062007-11-10 13:29:04 +0000253 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800254 }
Richard Purdie72f8da32007-12-31 23:09:44 +0000255 up_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800256
257 return 0;
258}
Németh Márton4d404fd2008-03-09 20:59:57 +0000259EXPORT_SYMBOL_GPL(led_trigger_register);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800260
Bryan Wueb202622012-04-19 11:46:33 +0800261void led_trigger_unregister(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800262{
263 struct led_classdev *led_cdev;
264
Sasha Levin14f57162014-04-04 10:01:13 -0700265 if (list_empty_careful(&trig->next_trig))
266 return;
267
Richard Purdiec3bc9952006-03-31 02:31:05 -0800268 /* Remove from the list of led triggers */
Richard Purdiedc472062007-11-10 13:29:04 +0000269 down_write(&triggers_list_lock);
Sasha Levin14f57162014-04-04 10:01:13 -0700270 list_del_init(&trig->next_trig);
Richard Purdiedc472062007-11-10 13:29:04 +0000271 up_write(&triggers_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800272
273 /* Remove anyone actively using this trigger */
Richard Purdie72f8da32007-12-31 23:09:44 +0000274 down_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800275 list_for_each_entry(led_cdev, &leds_list, node) {
Richard Purdiedc472062007-11-10 13:29:04 +0000276 down_write(&led_cdev->trigger_lock);
Bryan Wueb202622012-04-19 11:46:33 +0800277 if (led_cdev->trigger == trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800278 led_trigger_set(led_cdev, NULL);
Richard Purdiedc472062007-11-10 13:29:04 +0000279 up_write(&led_cdev->trigger_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800280 }
Richard Purdie72f8da32007-12-31 23:09:44 +0000281 up_read(&leds_list_lock);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800282}
Németh Márton4d404fd2008-03-09 20:59:57 +0000283EXPORT_SYMBOL_GPL(led_trigger_unregister);
284
Heiner Kallweit9534cc32016-01-02 01:36:41 +0100285static void devm_led_trigger_release(struct device *dev, void *res)
286{
287 led_trigger_unregister(*(struct led_trigger **)res);
288}
289
290int devm_led_trigger_register(struct device *dev,
291 struct led_trigger *trig)
292{
293 struct led_trigger **dr;
294 int rc;
295
296 dr = devres_alloc(devm_led_trigger_release, sizeof(*dr),
297 GFP_KERNEL);
298 if (!dr)
299 return -ENOMEM;
300
301 *dr = trig;
302
303 rc = led_trigger_register(trig);
304 if (rc)
305 devres_free(dr);
306 else
307 devres_add(dev, dr);
308
309 return rc;
310}
311EXPORT_SYMBOL_GPL(devm_led_trigger_register);
312
Németh Márton4d404fd2008-03-09 20:59:57 +0000313/* Simple LED Tigger Interface */
314
Bryan Wueb202622012-04-19 11:46:33 +0800315void led_trigger_event(struct led_trigger *trig,
Németh Márton4d404fd2008-03-09 20:59:57 +0000316 enum led_brightness brightness)
317{
ZHAO Gang11e043b2013-12-28 07:00:26 -0800318 struct led_classdev *led_cdev;
Németh Márton4d404fd2008-03-09 20:59:57 +0000319
Bryan Wueb202622012-04-19 11:46:33 +0800320 if (!trig)
Németh Márton4d404fd2008-03-09 20:59:57 +0000321 return;
322
Bryan Wueb202622012-04-19 11:46:33 +0800323 read_lock(&trig->leddev_list_lock);
ZHAO Gang11e043b2013-12-28 07:00:26 -0800324 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list)
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800325 led_set_brightness(led_cdev, brightness);
Bryan Wueb202622012-04-19 11:46:33 +0800326 read_unlock(&trig->leddev_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +0000327}
328EXPORT_SYMBOL_GPL(led_trigger_event);
329
Bryan Wu20c0e6b2012-06-15 22:15:27 +0800330static void led_trigger_blink_setup(struct led_trigger *trig,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800331 unsigned long *delay_on,
332 unsigned long *delay_off,
333 int oneshot,
334 int invert)
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000335{
ZHAO Gang11e043b2013-12-28 07:00:26 -0800336 struct led_classdev *led_cdev;
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000337
Bryan Wueb202622012-04-19 11:46:33 +0800338 if (!trig)
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000339 return;
340
Bryan Wueb202622012-04-19 11:46:33 +0800341 read_lock(&trig->leddev_list_lock);
ZHAO Gang11e043b2013-12-28 07:00:26 -0800342 list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) {
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800343 if (oneshot)
344 led_blink_set_oneshot(led_cdev, delay_on, delay_off,
345 invert);
346 else
347 led_blink_set(led_cdev, delay_on, delay_off);
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000348 }
Bryan Wueb202622012-04-19 11:46:33 +0800349 read_unlock(&trig->leddev_list_lock);
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000350}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800351
352void led_trigger_blink(struct led_trigger *trig,
353 unsigned long *delay_on,
354 unsigned long *delay_off)
355{
356 led_trigger_blink_setup(trig, delay_on, delay_off, 0, 0);
357}
Vasily Khoruzhick0b9536c2011-01-07 16:28:16 +0000358EXPORT_SYMBOL_GPL(led_trigger_blink);
359
Fabio Baltieri5bb629c2012-05-27 07:19:22 +0800360void led_trigger_blink_oneshot(struct led_trigger *trig,
361 unsigned long *delay_on,
362 unsigned long *delay_off,
363 int invert)
364{
365 led_trigger_blink_setup(trig, delay_on, delay_off, 1, invert);
366}
367EXPORT_SYMBOL_GPL(led_trigger_blink_oneshot);
368
Németh Márton4d404fd2008-03-09 20:59:57 +0000369void led_trigger_register_simple(const char *name, struct led_trigger **tp)
370{
Bryan Wueb202622012-04-19 11:46:33 +0800371 struct led_trigger *trig;
Németh Márton4d404fd2008-03-09 20:59:57 +0000372 int err;
373
Bryan Wueb202622012-04-19 11:46:33 +0800374 trig = kzalloc(sizeof(struct led_trigger), GFP_KERNEL);
Németh Márton4d404fd2008-03-09 20:59:57 +0000375
Bryan Wueb202622012-04-19 11:46:33 +0800376 if (trig) {
377 trig->name = name;
378 err = led_trigger_register(trig);
Masakazu Mokunocba4c2a2011-10-31 17:11:59 -0700379 if (err < 0) {
Bryan Wueb202622012-04-19 11:46:33 +0800380 kfree(trig);
381 trig = NULL;
Sachin Kamat09f5fe72012-11-25 13:00:44 +0530382 pr_warn("LED trigger %s failed to register (%d)\n",
383 name, err);
Masakazu Mokunocba4c2a2011-10-31 17:11:59 -0700384 }
Sachin Kamat09f5fe72012-11-25 13:00:44 +0530385 } else {
386 pr_warn("LED trigger %s failed to register (no memory)\n",
387 name);
388 }
Bryan Wueb202622012-04-19 11:46:33 +0800389 *tp = trig;
Németh Márton4d404fd2008-03-09 20:59:57 +0000390}
391EXPORT_SYMBOL_GPL(led_trigger_register_simple);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800392
Bryan Wueb202622012-04-19 11:46:33 +0800393void led_trigger_unregister_simple(struct led_trigger *trig)
Richard Purdiec3bc9952006-03-31 02:31:05 -0800394{
Bryan Wueb202622012-04-19 11:46:33 +0800395 if (trig)
396 led_trigger_unregister(trig);
397 kfree(trig);
Richard Purdiec3bc9952006-03-31 02:31:05 -0800398}
Richard Purdiec3bc9952006-03-31 02:31:05 -0800399EXPORT_SYMBOL_GPL(led_trigger_unregister_simple);