1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * LED state routines for driver control interface
4 * Copyright (c) 2021 by Jaroslav Kysela <perex@perex.cz>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/leds.h>
10 #include <sound/core.h>
11 #include <sound/control.h>
13 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
14 MODULE_DESCRIPTION("ALSA control interface to LED trigger code.");
15 MODULE_LICENSE("GPL");
17 #define MAX_LED (((SNDRV_CTL_ELEM_ACCESS_MIC_LED - SNDRV_CTL_ELEM_ACCESS_SPK_LED) \
18 >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) + 1)
20 #define to_led_card_dev(_dev) \
21 container_of(_dev, struct snd_ctl_led_card, dev)
23 enum snd_ctl_led_mode {
30 struct snd_ctl_led_card {
33 struct snd_ctl_led *led;
38 struct list_head controls;
41 enum led_audio trigger_type;
42 enum snd_ctl_led_mode mode;
43 struct snd_ctl_led_card *cards[SNDRV_CARDS];
46 struct snd_ctl_led_ctl {
47 struct list_head list;
48 struct snd_card *card;
50 struct snd_kcontrol *kctl;
51 unsigned int index_offset;
54 static DEFINE_MUTEX(snd_ctl_led_mutex);
55 static bool snd_ctl_led_card_valid[SNDRV_CARDS];
56 static struct snd_ctl_led snd_ctl_leds[MAX_LED] = {
59 .group = (SNDRV_CTL_ELEM_ACCESS_SPK_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
60 .trigger_type = LED_AUDIO_MUTE,
61 .mode = MODE_FOLLOW_MUTE,
65 .group = (SNDRV_CTL_ELEM_ACCESS_MIC_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
66 .trigger_type = LED_AUDIO_MICMUTE,
67 .mode = MODE_FOLLOW_MUTE,
71 static void snd_ctl_led_sysfs_add(struct snd_card *card);
72 static void snd_ctl_led_sysfs_remove(struct snd_card *card);
74 #define UPDATE_ROUTE(route, cb) \
78 route = route < 0 ? route2 : (route | route2); \
81 static inline unsigned int access_to_group(unsigned int access)
83 return ((access & SNDRV_CTL_ELEM_ACCESS_LED_MASK) >>
84 SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1;
87 static inline unsigned int group_to_access(unsigned int group)
89 return (group + 1) << SNDRV_CTL_ELEM_ACCESS_LED_SHIFT;
92 static struct snd_ctl_led *snd_ctl_led_get_by_access(unsigned int access)
94 unsigned int group = access_to_group(access);
97 return &snd_ctl_leds[group];
101 * A note for callers:
102 * The two static variables info and value are protected using snd_ctl_led_mutex.
104 static int snd_ctl_led_get(struct snd_ctl_led_ctl *lctl)
106 static struct snd_ctl_elem_info info;
107 static struct snd_ctl_elem_value value;
108 struct snd_kcontrol *kctl = lctl->kctl;
112 memset(&info, 0, sizeof(info));
114 info.id.index += lctl->index_offset;
115 info.id.numid += lctl->index_offset;
116 result = kctl->info(kctl, &info);
119 memset(&value, 0, sizeof(value));
121 result = kctl->get(kctl, &value);
124 if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
125 info.type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
126 for (i = 0; i < info.count; i++)
127 if (value.value.integer.value[i] != info.value.integer.min)
129 } else if (info.type == SNDRV_CTL_ELEM_TYPE_INTEGER64) {
130 for (i = 0; i < info.count; i++)
131 if (value.value.integer64.value[i] != info.value.integer64.min)
137 static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
138 struct snd_kcontrol *kctl, unsigned int ioff)
140 struct snd_ctl_led *led;
141 struct snd_ctl_led_ctl *lctl;
145 led = snd_ctl_led_get_by_access(access);
150 mutex_lock(&snd_ctl_led_mutex);
151 /* the card may not be registered (active) at this point */
152 if (card && !snd_ctl_led_card_valid[card->number]) {
153 mutex_unlock(&snd_ctl_led_mutex);
156 list_for_each_entry(lctl, &led->controls, list) {
157 if (lctl->kctl == kctl && lctl->index_offset == ioff)
159 UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
161 if (!found && kctl && card) {
162 lctl = kzalloc(sizeof(*lctl), GFP_KERNEL);
165 lctl->access = access;
167 lctl->index_offset = ioff;
168 list_add(&lctl->list, &led->controls);
169 UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
172 mutex_unlock(&snd_ctl_led_mutex);
174 case MODE_OFF: route = 1; break;
175 case MODE_ON: route = 0; break;
176 case MODE_FOLLOW_ROUTE: if (route >= 0) route ^= 1; break;
177 case MODE_FOLLOW_MUTE: /* noop */ break;
180 ledtrig_audio_set(led->trigger_type, route ? LED_OFF : LED_ON);
183 static struct snd_ctl_led_ctl *snd_ctl_led_find(struct snd_kcontrol *kctl, unsigned int ioff)
185 struct list_head *controls;
186 struct snd_ctl_led_ctl *lctl;
189 for (group = 0; group < MAX_LED; group++) {
190 controls = &snd_ctl_leds[group].controls;
191 list_for_each_entry(lctl, controls, list)
192 if (lctl->kctl == kctl && lctl->index_offset == ioff)
198 static unsigned int snd_ctl_led_remove(struct snd_kcontrol *kctl, unsigned int ioff,
201 struct snd_ctl_led_ctl *lctl;
202 unsigned int ret = 0;
204 mutex_lock(&snd_ctl_led_mutex);
205 lctl = snd_ctl_led_find(kctl, ioff);
206 if (lctl && (access == 0 || access != lctl->access)) {
208 list_del(&lctl->list);
211 mutex_unlock(&snd_ctl_led_mutex);
215 static void snd_ctl_led_notify(struct snd_card *card, unsigned int mask,
216 struct snd_kcontrol *kctl, unsigned int ioff)
218 struct snd_kcontrol_volatile *vd;
219 unsigned int access, access2;
221 if (mask == SNDRV_CTL_EVENT_MASK_REMOVE) {
222 access = snd_ctl_led_remove(kctl, ioff, 0);
224 snd_ctl_led_set_state(card, access, NULL, 0);
225 } else if (mask & SNDRV_CTL_EVENT_MASK_INFO) {
226 vd = &kctl->vd[ioff];
227 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
228 access2 = snd_ctl_led_remove(kctl, ioff, access);
230 snd_ctl_led_set_state(card, access2, NULL, 0);
232 snd_ctl_led_set_state(card, access, kctl, ioff);
233 } else if ((mask & (SNDRV_CTL_EVENT_MASK_ADD |
234 SNDRV_CTL_EVENT_MASK_VALUE)) != 0) {
235 vd = &kctl->vd[ioff];
236 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
238 snd_ctl_led_set_state(card, access, kctl, ioff);
242 static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,
243 unsigned int group, bool set)
245 struct snd_card *card;
246 struct snd_kcontrol *kctl;
247 struct snd_kcontrol_volatile *vd;
248 unsigned int ioff, access, new_access;
251 card = snd_card_ref(card_number);
253 down_write(&card->controls_rwsem);
254 kctl = snd_ctl_find_id(card, id);
256 ioff = snd_ctl_get_ioff(kctl, id);
257 vd = &kctl->vd[ioff];
258 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
259 if (access != 0 && access != group_to_access(group)) {
263 new_access = vd->access & ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
265 new_access |= group_to_access(group);
266 if (new_access != vd->access) {
267 vd->access = new_access;
268 snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, ioff);
274 up_write(&card->controls_rwsem);
275 snd_card_unref(card);
282 static void snd_ctl_led_refresh(void)
286 for (group = 0; group < MAX_LED; group++)
287 snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
290 static void snd_ctl_led_ctl_destroy(struct snd_ctl_led_ctl *lctl)
292 list_del(&lctl->list);
296 static void snd_ctl_led_clean(struct snd_card *card)
299 struct snd_ctl_led *led;
300 struct snd_ctl_led_ctl *lctl;
302 for (group = 0; group < MAX_LED; group++) {
303 led = &snd_ctl_leds[group];
305 list_for_each_entry(lctl, &led->controls, list)
306 if (!card || lctl->card == card) {
307 snd_ctl_led_ctl_destroy(lctl);
313 static int snd_ctl_led_reset(int card_number, unsigned int group)
315 struct snd_card *card;
316 struct snd_ctl_led *led;
317 struct snd_ctl_led_ctl *lctl;
318 struct snd_kcontrol_volatile *vd;
321 card = snd_card_ref(card_number);
325 mutex_lock(&snd_ctl_led_mutex);
326 if (!snd_ctl_led_card_valid[card_number]) {
327 mutex_unlock(&snd_ctl_led_mutex);
328 snd_card_unref(card);
331 led = &snd_ctl_leds[group];
333 list_for_each_entry(lctl, &led->controls, list)
334 if (lctl->card == card) {
335 vd = &lctl->kctl->vd[lctl->index_offset];
336 vd->access &= ~group_to_access(group);
337 snd_ctl_led_ctl_destroy(lctl);
341 mutex_unlock(&snd_ctl_led_mutex);
343 snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
344 snd_card_unref(card);
348 static void snd_ctl_led_register(struct snd_card *card)
350 struct snd_kcontrol *kctl;
353 if (snd_BUG_ON(card->number < 0 ||
354 card->number >= ARRAY_SIZE(snd_ctl_led_card_valid)))
356 mutex_lock(&snd_ctl_led_mutex);
357 snd_ctl_led_card_valid[card->number] = true;
358 mutex_unlock(&snd_ctl_led_mutex);
359 /* the register callback is already called with held card->controls_rwsem */
360 list_for_each_entry(kctl, &card->controls, list)
361 for (ioff = 0; ioff < kctl->count; ioff++)
362 snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
363 snd_ctl_led_refresh();
364 snd_ctl_led_sysfs_add(card);
367 static void snd_ctl_led_disconnect(struct snd_card *card)
369 snd_ctl_led_sysfs_remove(card);
370 mutex_lock(&snd_ctl_led_mutex);
371 snd_ctl_led_card_valid[card->number] = false;
372 snd_ctl_led_clean(card);
373 mutex_unlock(&snd_ctl_led_mutex);
374 snd_ctl_led_refresh();
377 static void snd_ctl_led_card_release(struct device *dev)
379 struct snd_ctl_led_card *led_card = to_led_card_dev(dev);
384 static void snd_ctl_led_release(struct device *dev)
388 static void snd_ctl_led_dev_release(struct device *dev)
396 static ssize_t show_mode(struct device *dev,
397 struct device_attribute *attr, char *buf)
399 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
403 case MODE_FOLLOW_MUTE: str = "follow-mute"; break;
404 case MODE_FOLLOW_ROUTE: str = "follow-route"; break;
405 case MODE_ON: str = "on"; break;
406 case MODE_OFF: str = "off"; break;
408 return sprintf(buf, "%s\n", str);
411 static ssize_t store_mode(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t count)
414 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
416 size_t l = min(count, sizeof(_buf) - 1);
417 enum snd_ctl_led_mode mode;
419 memcpy(_buf, buf, l);
421 if (strstr(_buf, "mute"))
422 mode = MODE_FOLLOW_MUTE;
423 else if (strstr(_buf, "route"))
424 mode = MODE_FOLLOW_ROUTE;
425 else if (strncmp(_buf, "off", 3) == 0 || strncmp(_buf, "0", 1) == 0)
427 else if (strncmp(_buf, "on", 2) == 0 || strncmp(_buf, "1", 1) == 0)
432 mutex_lock(&snd_ctl_led_mutex);
434 mutex_unlock(&snd_ctl_led_mutex);
436 snd_ctl_led_set_state(NULL, group_to_access(led->group), NULL, 0);
440 static ssize_t show_brightness(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
445 return sprintf(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
448 static DEVICE_ATTR(mode, 0644, show_mode, store_mode);
449 static DEVICE_ATTR(brightness, 0444, show_brightness, NULL);
451 static struct attribute *snd_ctl_led_dev_attrs[] = {
453 &dev_attr_brightness.attr,
457 static const struct attribute_group snd_ctl_led_dev_attr_group = {
458 .attrs = snd_ctl_led_dev_attrs,
461 static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
462 &snd_ctl_led_dev_attr_group,
466 static char *find_eos(char *s)
468 while (*s && *s != ',')
475 static char *parse_uint(char *s, unsigned int *val)
477 unsigned long long res;
478 if (kstrtoull(s, 10, &res))
484 static char *parse_string(char *s, char *val, size_t val_size)
486 if (*s == '"' || *s == '\'') {
489 while (*s && *s != c) {
497 while (*s && *s != ',') {
511 static char *parse_iface(char *s, unsigned int *val)
513 if (!strncasecmp(s, "card", 4))
514 *val = SNDRV_CTL_ELEM_IFACE_CARD;
515 else if (!strncasecmp(s, "mixer", 5))
516 *val = SNDRV_CTL_ELEM_IFACE_MIXER;
521 * These types of input strings are accepted:
523 * unsigned integer - numid (equivaled to numid=UINT)
524 * string - basic mixer name (equivalent to iface=MIXER,name=STR)
526 * [iface=MIXER,][device=UINT,][subdevice=UINT,]name=STR[,index=UINT]
528 static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, size_t count,
531 char buf2[256], *s, *os;
532 size_t len = max(sizeof(s) - 1, count);
533 struct snd_ctl_elem_id id;
536 strncpy(buf2, buf, len);
538 memset(&id, 0, sizeof(id));
539 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
543 if (!strncasecmp(s, "numid=", 6)) {
544 s = parse_uint(s + 6, &id.numid);
545 } else if (!strncasecmp(s, "iface=", 6)) {
546 s = parse_iface(s + 6, &id.iface);
547 } else if (!strncasecmp(s, "device=", 7)) {
548 s = parse_uint(s + 7, &id.device);
549 } else if (!strncasecmp(s, "subdevice=", 10)) {
550 s = parse_uint(s + 10, &id.subdevice);
551 } else if (!strncasecmp(s, "name=", 5)) {
552 s = parse_string(s + 5, id.name, sizeof(id.name));
553 } else if (!strncasecmp(s, "index=", 6)) {
554 s = parse_uint(s + 6, &id.index);
555 } else if (s == buf2) {
557 if (*s < '0' || *s > '9')
562 parse_uint(buf2, &id.numid);
564 for (; *s >= ' '; s++);
566 strlcpy(id.name, buf2, sizeof(id.name));
576 err = snd_ctl_led_set_id(led_card->number, &id, led_card->led->group, attach);
583 static ssize_t parse_attach(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
586 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
587 return set_led_id(led_card, buf, count, true);
590 static ssize_t parse_detach(struct device *dev, struct device_attribute *attr,
591 const char *buf, size_t count)
593 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
594 return set_led_id(led_card, buf, count, false);
597 static ssize_t ctl_reset(struct device *dev, struct device_attribute *attr,
598 const char *buf, size_t count)
600 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
603 if (count > 0 && buf[0] == '1') {
604 err = snd_ctl_led_reset(led_card->number, led_card->led->group);
611 static ssize_t ctl_list(struct device *dev,
612 struct device_attribute *attr, char *buf)
614 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
615 struct snd_card *card;
616 struct snd_ctl_led_ctl *lctl;
620 card = snd_card_ref(led_card->number);
623 down_read(&card->controls_rwsem);
624 mutex_lock(&snd_ctl_led_mutex);
625 if (snd_ctl_led_card_valid[led_card->number]) {
626 list_for_each_entry(lctl, &led_card->led->controls, list)
627 if (lctl->card == card) {
628 if (buf2 - buf > PAGE_SIZE - 16)
632 l = scnprintf(buf2, 15, "%u",
633 lctl->kctl->id.numid +
639 mutex_unlock(&snd_ctl_led_mutex);
640 up_read(&card->controls_rwsem);
641 snd_card_unref(card);
645 static DEVICE_ATTR(attach, 0200, NULL, parse_attach);
646 static DEVICE_ATTR(detach, 0200, NULL, parse_detach);
647 static DEVICE_ATTR(reset, 0200, NULL, ctl_reset);
648 static DEVICE_ATTR(list, 0444, ctl_list, NULL);
650 static struct attribute *snd_ctl_led_card_attrs[] = {
651 &dev_attr_attach.attr,
652 &dev_attr_detach.attr,
653 &dev_attr_reset.attr,
658 static const struct attribute_group snd_ctl_led_card_attr_group = {
659 .attrs = snd_ctl_led_card_attrs,
662 static const struct attribute_group *snd_ctl_led_card_attr_groups[] = {
663 &snd_ctl_led_card_attr_group,
667 static struct device snd_ctl_led_dev;
669 static void snd_ctl_led_sysfs_add(struct snd_card *card)
672 struct snd_ctl_led_card *led_card;
673 struct snd_ctl_led *led;
676 for (group = 0; group < MAX_LED; group++) {
677 led = &snd_ctl_leds[group];
678 led_card = kzalloc(sizeof(*led_card), GFP_KERNEL);
681 led_card->number = card->number;
683 device_initialize(&led_card->dev);
684 led_card->dev.release = snd_ctl_led_card_release;
685 if (dev_set_name(&led_card->dev, "card%d", card->number) < 0)
687 led_card->dev.parent = &led->dev;
688 led_card->dev.groups = snd_ctl_led_card_attr_groups;
689 if (device_add(&led_card->dev))
691 led->cards[card->number] = led_card;
692 snprintf(link_name, sizeof(link_name), "led-%s", led->name);
693 WARN(sysfs_create_link(&card->ctl_dev.kobj, &led_card->dev.kobj, link_name),
694 "can't create symlink to controlC%i device\n", card->number);
695 WARN(sysfs_create_link(&led_card->dev.kobj, &card->card_dev.kobj, "card"),
696 "can't create symlink to card%i\n", card->number);
700 put_device(&led_card->dev);
702 printk(KERN_ERR "snd_ctl_led: unable to add card%d", card->number);
706 static void snd_ctl_led_sysfs_remove(struct snd_card *card)
709 struct snd_ctl_led_card *led_card;
710 struct snd_ctl_led *led;
713 for (group = 0; group < MAX_LED; group++) {
714 led = &snd_ctl_leds[group];
715 led_card = led->cards[card->number];
718 snprintf(link_name, sizeof(link_name), "led-%s", led->name);
719 sysfs_remove_link(&card->ctl_dev.kobj, link_name);
720 sysfs_remove_link(&led_card->dev.kobj, "card");
721 device_unregister(&led_card->dev);
722 led->cards[card->number] = NULL;
727 * Control layer registration
729 static struct snd_ctl_layer_ops snd_ctl_led_lops = {
730 .module_name = SND_CTL_LAYER_MODULE_LED,
731 .lregister = snd_ctl_led_register,
732 .ldisconnect = snd_ctl_led_disconnect,
733 .lnotify = snd_ctl_led_notify,
736 static int __init snd_ctl_led_init(void)
738 struct snd_ctl_led *led;
741 device_initialize(&snd_ctl_led_dev);
742 snd_ctl_led_dev.class = sound_class;
743 snd_ctl_led_dev.release = snd_ctl_led_dev_release;
744 dev_set_name(&snd_ctl_led_dev, "ctl-led");
745 if (device_add(&snd_ctl_led_dev)) {
746 put_device(&snd_ctl_led_dev);
749 for (group = 0; group < MAX_LED; group++) {
750 led = &snd_ctl_leds[group];
751 INIT_LIST_HEAD(&led->controls);
752 device_initialize(&led->dev);
753 led->dev.parent = &snd_ctl_led_dev;
754 led->dev.release = snd_ctl_led_release;
755 led->dev.groups = snd_ctl_led_dev_attr_groups;
756 dev_set_name(&led->dev, led->name);
757 if (device_add(&led->dev)) {
758 put_device(&led->dev);
759 for (; group > 0; group--) {
760 led = &snd_ctl_leds[group - 1];
761 device_unregister(&led->dev);
763 device_unregister(&snd_ctl_led_dev);
767 snd_ctl_register_layer(&snd_ctl_led_lops);
771 static void __exit snd_ctl_led_exit(void)
773 struct snd_ctl_led *led;
774 struct snd_card *card;
775 unsigned int group, card_number;
777 snd_ctl_disconnect_layer(&snd_ctl_led_lops);
778 for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
779 if (!snd_ctl_led_card_valid[card_number])
781 card = snd_card_ref(card_number);
783 snd_ctl_led_sysfs_remove(card);
784 snd_card_unref(card);
787 for (group = 0; group < MAX_LED; group++) {
788 led = &snd_ctl_leds[group];
789 device_unregister(&led->dev);
791 device_unregister(&snd_ctl_led_dev);
792 snd_ctl_led_clean(NULL);
795 module_init(snd_ctl_led_init)
796 module_exit(snd_ctl_led_exit)