lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * LED Triggers Core |
| 3 | * |
| 4 | * Copyright 2005-2007 Openedhand Ltd. |
| 5 | * |
| 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 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/timer.h> |
| 21 | #include <linux/rwsem.h> |
| 22 | #include <linux/leds.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include "leds.h" |
| 25 | |
| 26 | /* |
| 27 | * Nests outside led_cdev->trigger_lock |
| 28 | */ |
| 29 | static DECLARE_RWSEM(triggers_list_lock); |
| 30 | static LIST_HEAD(trigger_list); |
| 31 | |
| 32 | /* Used by LED Class */ |
| 33 | |
| 34 | ssize_t led_trigger_store(struct device *dev, struct device_attribute *attr, |
| 35 | const char *buf, size_t count) |
| 36 | { |
| 37 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 38 | char trigger_name[TRIG_NAME_MAX]; |
| 39 | struct led_trigger *trig; |
| 40 | size_t len; |
| 41 | |
| 42 | if(NULL==led_cdev) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | trigger_name[sizeof(trigger_name) - 1] = '\0'; |
| 46 | strncpy(trigger_name, buf, sizeof(trigger_name) - 1); |
| 47 | len = strlen(trigger_name); |
| 48 | |
| 49 | if (len && trigger_name[len - 1] == '\n') |
| 50 | trigger_name[len - 1] = '\0'; |
| 51 | |
| 52 | if (!strcmp(trigger_name, "none")) { |
| 53 | led_trigger_remove(led_cdev); |
| 54 | return count; |
| 55 | } |
| 56 | |
| 57 | down_read(&triggers_list_lock); |
| 58 | list_for_each_entry(trig, &trigger_list, next_trig) { |
| 59 | if (!strcmp(trigger_name, trig->name)) { |
| 60 | down_write(&led_cdev->trigger_lock); |
| 61 | led_trigger_set(led_cdev, trig); |
| 62 | up_write(&led_cdev->trigger_lock); |
| 63 | |
| 64 | up_read(&triggers_list_lock); |
| 65 | return count; |
| 66 | } |
| 67 | } |
| 68 | up_read(&triggers_list_lock); |
| 69 | |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(led_trigger_store); |
| 73 | |
| 74 | ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr, |
| 75 | char *buf) |
| 76 | { |
| 77 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 78 | struct led_trigger *trig; |
| 79 | int len = 0; |
| 80 | |
| 81 | if(NULL==led_cdev) |
| 82 | return -EINVAL; |
| 83 | |
| 84 | down_read(&triggers_list_lock); |
| 85 | down_read(&led_cdev->trigger_lock); |
| 86 | |
| 87 | if (!led_cdev->trigger) |
| 88 | len += sprintf(buf+len, "[none] "); |
| 89 | else |
| 90 | len += sprintf(buf+len, "none "); |
| 91 | |
| 92 | list_for_each_entry(trig, &trigger_list, next_trig) { |
| 93 | if (led_cdev->trigger && !strcmp(led_cdev->trigger->name, |
| 94 | trig->name)) |
| 95 | len += sprintf(buf+len, "[%s] ", trig->name); |
| 96 | else |
| 97 | len += sprintf(buf+len, "%s ", trig->name); |
| 98 | } |
| 99 | up_read(&led_cdev->trigger_lock); |
| 100 | up_read(&triggers_list_lock); |
| 101 | |
| 102 | len += sprintf(len+buf, "\n"); |
| 103 | return len; |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(led_trigger_show); |
| 106 | |
| 107 | /* Caller must ensure led_cdev->trigger_lock held */ |
| 108 | void led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trigger) |
| 109 | { |
| 110 | unsigned long flags; |
| 111 | char *event = NULL; |
| 112 | char *envp[2]; |
| 113 | const char *name; |
| 114 | |
| 115 | name = trigger ? trigger->name : "none"; |
| 116 | event = kasprintf(GFP_KERNEL, "TRIGGER=%s", name); |
| 117 | |
| 118 | /* Remove any existing trigger */ |
| 119 | if (led_cdev->trigger) { |
| 120 | write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); |
| 121 | list_del(&led_cdev->trig_list); |
| 122 | write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, |
| 123 | flags); |
| 124 | if (led_cdev->trigger->deactivate) |
| 125 | led_cdev->trigger->deactivate(led_cdev); |
| 126 | led_cdev->trigger = NULL; |
| 127 | led_brightness_set(led_cdev, LED_OFF); |
| 128 | } |
| 129 | if (trigger) { |
| 130 | write_lock_irqsave(&trigger->leddev_list_lock, flags); |
| 131 | list_add_tail(&led_cdev->trig_list, &trigger->led_cdevs); |
| 132 | write_unlock_irqrestore(&trigger->leddev_list_lock, flags); |
| 133 | led_cdev->trigger = trigger; |
| 134 | if (trigger->activate) |
| 135 | trigger->activate(led_cdev); |
| 136 | } |
| 137 | |
| 138 | if (event) { |
| 139 | envp[0] = event; |
| 140 | envp[1] = NULL; |
| 141 | /*εÏèÓÉϲ㲻Ðè LED netlink֪ͨ£¬Ó¦ÏîĿҪÇó£¬ÔÝʱ¹Ø±Õ....*/ |
| 142 | //kobject_uevent_env(&led_cdev->dev->kobj, KOBJ_CHANGE, envp); |
| 143 | kfree(event); |
| 144 | } |
| 145 | } |
| 146 | EXPORT_SYMBOL_GPL(led_trigger_set); |
| 147 | |
| 148 | void led_trigger_remove(struct led_classdev *led_cdev) |
| 149 | { |
| 150 | down_write(&led_cdev->trigger_lock); |
| 151 | led_trigger_set(led_cdev, NULL); |
| 152 | up_write(&led_cdev->trigger_lock); |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(led_trigger_remove); |
| 155 | |
| 156 | void led_trigger_set_default(struct led_classdev *led_cdev) |
| 157 | { |
| 158 | struct led_trigger *trig; |
| 159 | |
| 160 | if (!led_cdev->default_trigger) |
| 161 | return; |
| 162 | |
| 163 | down_read(&triggers_list_lock); |
| 164 | down_write(&led_cdev->trigger_lock); |
| 165 | list_for_each_entry(trig, &trigger_list, next_trig) { |
| 166 | if (!strcmp(led_cdev->default_trigger, trig->name)) |
| 167 | led_trigger_set(led_cdev, trig); |
| 168 | } |
| 169 | up_write(&led_cdev->trigger_lock); |
| 170 | up_read(&triggers_list_lock); |
| 171 | } |
| 172 | EXPORT_SYMBOL_GPL(led_trigger_set_default); |
| 173 | |
| 174 | /* LED Trigger Interface */ |
| 175 | |
| 176 | int led_trigger_register(struct led_trigger *trigger) |
| 177 | { |
| 178 | struct led_classdev *led_cdev; |
| 179 | struct led_trigger *trig; |
| 180 | |
| 181 | rwlock_init(&trigger->leddev_list_lock); |
| 182 | INIT_LIST_HEAD(&trigger->led_cdevs); |
| 183 | |
| 184 | down_write(&triggers_list_lock); |
| 185 | /* Make sure the trigger's name isn't already in use */ |
| 186 | list_for_each_entry(trig, &trigger_list, next_trig) { |
| 187 | if (!strcmp(trig->name, trigger->name)) { |
| 188 | up_write(&triggers_list_lock); |
| 189 | return -EEXIST; |
| 190 | } |
| 191 | } |
| 192 | /* Add to the list of led triggers */ |
| 193 | list_add_tail(&trigger->next_trig, &trigger_list); |
| 194 | up_write(&triggers_list_lock); |
| 195 | |
| 196 | /* Register with any LEDs that have this as a default trigger */ |
| 197 | down_read(&leds_list_lock); |
| 198 | list_for_each_entry(led_cdev, &leds_list, node) { |
| 199 | down_write(&led_cdev->trigger_lock); |
| 200 | if (!led_cdev->trigger && led_cdev->default_trigger && |
| 201 | !strcmp(led_cdev->default_trigger, trigger->name)) |
| 202 | led_trigger_set(led_cdev, trigger); |
| 203 | up_write(&led_cdev->trigger_lock); |
| 204 | } |
| 205 | up_read(&leds_list_lock); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | EXPORT_SYMBOL_GPL(led_trigger_register); |
| 210 | |
| 211 | void led_trigger_unregister(struct led_trigger *trigger) |
| 212 | { |
| 213 | struct led_classdev *led_cdev; |
| 214 | |
| 215 | /* Remove from the list of led triggers */ |
| 216 | down_write(&triggers_list_lock); |
| 217 | list_del(&trigger->next_trig); |
| 218 | up_write(&triggers_list_lock); |
| 219 | |
| 220 | /* Remove anyone actively using this trigger */ |
| 221 | down_read(&leds_list_lock); |
| 222 | list_for_each_entry(led_cdev, &leds_list, node) { |
| 223 | down_write(&led_cdev->trigger_lock); |
| 224 | if (led_cdev->trigger == trigger) |
| 225 | led_trigger_set(led_cdev, NULL); |
| 226 | up_write(&led_cdev->trigger_lock); |
| 227 | } |
| 228 | up_read(&leds_list_lock); |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(led_trigger_unregister); |
| 231 | |
| 232 | /* Simple LED Tigger Interface */ |
| 233 | |
| 234 | void led_trigger_event(struct led_trigger *trigger, |
| 235 | enum led_brightness brightness) |
| 236 | { |
| 237 | struct list_head *entry; |
| 238 | |
| 239 | if (!trigger) |
| 240 | return; |
| 241 | |
| 242 | read_lock(&trigger->leddev_list_lock); |
| 243 | list_for_each(entry, &trigger->led_cdevs) { |
| 244 | struct led_classdev *led_cdev; |
| 245 | |
| 246 | led_cdev = list_entry(entry, struct led_classdev, trig_list); |
| 247 | led_set_brightness(led_cdev, brightness); |
| 248 | } |
| 249 | read_unlock(&trigger->leddev_list_lock); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(led_trigger_event); |
| 252 | |
| 253 | void led_trigger_blink(struct led_trigger *trigger, |
| 254 | unsigned long *delay_on, |
| 255 | unsigned long *delay_off) |
| 256 | { |
| 257 | struct list_head *entry; |
| 258 | |
| 259 | if (!trigger) |
| 260 | return; |
| 261 | |
| 262 | read_lock(&trigger->leddev_list_lock); |
| 263 | list_for_each(entry, &trigger->led_cdevs) { |
| 264 | struct led_classdev *led_cdev; |
| 265 | |
| 266 | led_cdev = list_entry(entry, struct led_classdev, trig_list); |
| 267 | led_blink_set(led_cdev, delay_on, delay_off); |
| 268 | } |
| 269 | read_unlock(&trigger->leddev_list_lock); |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(led_trigger_blink); |
| 272 | |
| 273 | void led_trigger_register_simple(const char *name, struct led_trigger **tp) |
| 274 | { |
| 275 | struct led_trigger *trigger; |
| 276 | int err; |
| 277 | |
| 278 | trigger = kzalloc(sizeof(struct led_trigger), GFP_KERNEL); |
| 279 | |
| 280 | if (trigger) { |
| 281 | trigger->name = name; |
| 282 | err = led_trigger_register(trigger); |
| 283 | if (err < 0) { |
| 284 | kfree(trigger); |
| 285 | trigger = NULL; |
| 286 | printk(KERN_WARNING "LED trigger %s failed to register" |
| 287 | " (%d)\n", name, err); |
| 288 | } |
| 289 | } else |
| 290 | printk(KERN_WARNING "LED trigger %s failed to register" |
| 291 | " (no memory)\n", name); |
| 292 | |
| 293 | *tp = trigger; |
| 294 | } |
| 295 | EXPORT_SYMBOL_GPL(led_trigger_register_simple); |
| 296 | |
| 297 | void led_trigger_unregister_simple(struct led_trigger *trigger) |
| 298 | { |
| 299 | if (trigger) |
| 300 | led_trigger_unregister(trigger); |
| 301 | kfree(trigger); |
| 302 | } |
| 303 | EXPORT_SYMBOL_GPL(led_trigger_unregister_simple); |
| 304 | |
| 305 | MODULE_AUTHOR("Richard Purdie"); |
| 306 | MODULE_LICENSE("GPL"); |
| 307 | MODULE_DESCRIPTION("LED Triggers Core"); |