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 enum snd_ctl_led_mode {
27 struct snd_ctl_led_card {
30 struct snd_ctl_led *led;
35 struct list_head controls;
38 enum led_audio trigger_type;
39 enum snd_ctl_led_mode mode;
40 struct snd_ctl_led_card *cards[SNDRV_CARDS];
43 struct snd_ctl_led_ctl {
44 struct list_head list;
45 struct snd_card *card;
47 struct snd_kcontrol *kctl;
48 unsigned int index_offset;
51 static DEFINE_MUTEX(snd_ctl_led_mutex);
52 static bool snd_ctl_led_card_valid[SNDRV_CARDS];
53 static struct snd_ctl_led snd_ctl_leds[MAX_LED] = {
56 .group = (SNDRV_CTL_ELEM_ACCESS_SPK_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
57 .trigger_type = LED_AUDIO_MUTE,
58 .mode = MODE_FOLLOW_MUTE,
62 .group = (SNDRV_CTL_ELEM_ACCESS_MIC_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
63 .trigger_type = LED_AUDIO_MICMUTE,
64 .mode = MODE_FOLLOW_MUTE,
68 static void snd_ctl_led_sysfs_add(struct snd_card *card);
69 static void snd_ctl_led_sysfs_remove(struct snd_card *card);
71 #define UPDATE_ROUTE(route, cb) \
75 route = route < 0 ? route2 : (route | route2); \
78 static inline unsigned int access_to_group(unsigned int access)
80 return ((access & SNDRV_CTL_ELEM_ACCESS_LED_MASK) >>
81 SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1;
84 static inline unsigned int group_to_access(unsigned int group)
86 return (group + 1) << SNDRV_CTL_ELEM_ACCESS_LED_SHIFT;
89 static struct snd_ctl_led *snd_ctl_led_get_by_access(unsigned int access)
91 unsigned int group = access_to_group(access);
94 return &snd_ctl_leds[group];
99 * The two static variables info and value are protected using snd_ctl_led_mutex.
101 static int snd_ctl_led_get(struct snd_ctl_led_ctl *lctl)
103 static struct snd_ctl_elem_info info;
104 static struct snd_ctl_elem_value value;
105 struct snd_kcontrol *kctl = lctl->kctl;
109 memset(&info, 0, sizeof(info));
111 info.id.index += lctl->index_offset;
112 info.id.numid += lctl->index_offset;
113 result = kctl->info(kctl, &info);
116 memset(&value, 0, sizeof(value));
118 result = kctl->get(kctl, &value);
121 if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
122 info.type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
123 for (i = 0; i < info.count; i++)
124 if (value.value.integer.value[i] != info.value.integer.min)
126 } else if (info.type == SNDRV_CTL_ELEM_TYPE_INTEGER64) {
127 for (i = 0; i < info.count; i++)
128 if (value.value.integer64.value[i] != info.value.integer64.min)
134 static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
135 struct snd_kcontrol *kctl, unsigned int ioff)
137 struct snd_ctl_led *led;
138 struct snd_ctl_led_ctl *lctl;
142 led = snd_ctl_led_get_by_access(access);
147 mutex_lock(&snd_ctl_led_mutex);
148 /* the card may not be registered (active) at this point */
149 if (card && !snd_ctl_led_card_valid[card->number]) {
150 mutex_unlock(&snd_ctl_led_mutex);
153 list_for_each_entry(lctl, &led->controls, list) {
154 if (lctl->kctl == kctl && lctl->index_offset == ioff)
156 UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
158 if (!found && kctl && card) {
159 lctl = kzalloc(sizeof(*lctl), GFP_KERNEL);
162 lctl->access = access;
164 lctl->index_offset = ioff;
165 list_add(&lctl->list, &led->controls);
166 UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
169 mutex_unlock(&snd_ctl_led_mutex);
171 case MODE_OFF: route = 1; break;
172 case MODE_ON: route = 0; break;
173 case MODE_FOLLOW_ROUTE: if (route >= 0) route ^= 1; break;
174 case MODE_FOLLOW_MUTE: /* noop */ break;
177 ledtrig_audio_set(led->trigger_type, route ? LED_OFF : LED_ON);
180 static struct snd_ctl_led_ctl *snd_ctl_led_find(struct snd_kcontrol *kctl, unsigned int ioff)
182 struct list_head *controls;
183 struct snd_ctl_led_ctl *lctl;
186 for (group = 0; group < MAX_LED; group++) {
187 controls = &snd_ctl_leds[group].controls;
188 list_for_each_entry(lctl, controls, list)
189 if (lctl->kctl == kctl && lctl->index_offset == ioff)
195 static unsigned int snd_ctl_led_remove(struct snd_kcontrol *kctl, unsigned int ioff,
198 struct snd_ctl_led_ctl *lctl;
199 unsigned int ret = 0;
201 mutex_lock(&snd_ctl_led_mutex);
202 lctl = snd_ctl_led_find(kctl, ioff);
203 if (lctl && (access == 0 || access != lctl->access)) {
205 list_del(&lctl->list);
208 mutex_unlock(&snd_ctl_led_mutex);
212 static void snd_ctl_led_notify(struct snd_card *card, unsigned int mask,
213 struct snd_kcontrol *kctl, unsigned int ioff)
215 struct snd_kcontrol_volatile *vd;
216 unsigned int access, access2;
218 if (mask == SNDRV_CTL_EVENT_MASK_REMOVE) {
219 access = snd_ctl_led_remove(kctl, ioff, 0);
221 snd_ctl_led_set_state(card, access, NULL, 0);
222 } else if (mask & SNDRV_CTL_EVENT_MASK_INFO) {
223 vd = &kctl->vd[ioff];
224 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
225 access2 = snd_ctl_led_remove(kctl, ioff, access);
227 snd_ctl_led_set_state(card, access2, NULL, 0);
229 snd_ctl_led_set_state(card, access, kctl, ioff);
230 } else if ((mask & (SNDRV_CTL_EVENT_MASK_ADD |
231 SNDRV_CTL_EVENT_MASK_VALUE)) != 0) {
232 vd = &kctl->vd[ioff];
233 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
235 snd_ctl_led_set_state(card, access, kctl, ioff);
239 static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,
240 unsigned int group, bool set)
242 struct snd_card *card;
243 struct snd_kcontrol *kctl;
244 struct snd_kcontrol_volatile *vd;
245 unsigned int ioff, access, new_access;
248 card = snd_card_ref(card_number);
250 down_write(&card->controls_rwsem);
251 kctl = snd_ctl_find_id(card, id);
253 ioff = snd_ctl_get_ioff(kctl, id);
254 vd = &kctl->vd[ioff];
255 access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
256 if (access != 0 && access != group_to_access(group)) {
260 new_access = vd->access & ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
262 new_access |= group_to_access(group);
263 if (new_access != vd->access) {
264 vd->access = new_access;
265 snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, ioff);
271 up_write(&card->controls_rwsem);
272 snd_card_unref(card);
279 static void snd_ctl_led_refresh(void)
283 for (group = 0; group < MAX_LED; group++)
284 snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
287 static void snd_ctl_led_ctl_destroy(struct snd_ctl_led_ctl *lctl)
289 list_del(&lctl->list);
293 static void snd_ctl_led_clean(struct snd_card *card)
296 struct snd_ctl_led *led;
297 struct snd_ctl_led_ctl *lctl;
299 for (group = 0; group < MAX_LED; group++) {
300 led = &snd_ctl_leds[group];
302 list_for_each_entry(lctl, &led->controls, list)
303 if (!card || lctl->card == card) {
304 snd_ctl_led_ctl_destroy(lctl);
310 static int snd_ctl_led_reset(int card_number, unsigned int group)
312 struct snd_card *card;
313 struct snd_ctl_led *led;
314 struct snd_ctl_led_ctl *lctl;
315 struct snd_kcontrol_volatile *vd;
318 card = snd_card_ref(card_number);
322 mutex_lock(&snd_ctl_led_mutex);
323 if (!snd_ctl_led_card_valid[card_number]) {
324 mutex_unlock(&snd_ctl_led_mutex);
325 snd_card_unref(card);
328 led = &snd_ctl_leds[group];
330 list_for_each_entry(lctl, &led->controls, list)
331 if (lctl->card == card) {
332 vd = &lctl->kctl->vd[lctl->index_offset];
333 vd->access &= ~group_to_access(group);
334 snd_ctl_led_ctl_destroy(lctl);
338 mutex_unlock(&snd_ctl_led_mutex);
340 snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
341 snd_card_unref(card);
345 static void snd_ctl_led_register(struct snd_card *card)
347 struct snd_kcontrol *kctl;
350 if (snd_BUG_ON(card->number < 0 ||
351 card->number >= ARRAY_SIZE(snd_ctl_led_card_valid)))
353 mutex_lock(&snd_ctl_led_mutex);
354 snd_ctl_led_card_valid[card->number] = true;
355 mutex_unlock(&snd_ctl_led_mutex);
356 /* the register callback is already called with held card->controls_rwsem */
357 list_for_each_entry(kctl, &card->controls, list)
358 for (ioff = 0; ioff < kctl->count; ioff++)
359 snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
360 snd_ctl_led_refresh();
361 snd_ctl_led_sysfs_add(card);
364 static void snd_ctl_led_disconnect(struct snd_card *card)
366 snd_ctl_led_sysfs_remove(card);
367 mutex_lock(&snd_ctl_led_mutex);
368 snd_ctl_led_card_valid[card->number] = false;
369 snd_ctl_led_clean(card);
370 mutex_unlock(&snd_ctl_led_mutex);
371 snd_ctl_led_refresh();
378 static ssize_t show_mode(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
385 case MODE_FOLLOW_MUTE: str = "follow-mute"; break;
386 case MODE_FOLLOW_ROUTE: str = "follow-route"; break;
387 case MODE_ON: str = "on"; break;
388 case MODE_OFF: str = "off"; break;
390 return sprintf(buf, "%s\n", str);
393 static ssize_t store_mode(struct device *dev, struct device_attribute *attr,
394 const char *buf, size_t count)
396 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
398 size_t l = min(count, sizeof(_buf) - 1);
399 enum snd_ctl_led_mode mode;
401 memcpy(_buf, buf, l);
403 if (strstr(_buf, "mute"))
404 mode = MODE_FOLLOW_MUTE;
405 else if (strstr(_buf, "route"))
406 mode = MODE_FOLLOW_ROUTE;
407 else if (strncmp(_buf, "off", 3) == 0 || strncmp(_buf, "0", 1) == 0)
409 else if (strncmp(_buf, "on", 2) == 0 || strncmp(_buf, "1", 1) == 0)
414 mutex_lock(&snd_ctl_led_mutex);
416 mutex_unlock(&snd_ctl_led_mutex);
418 snd_ctl_led_set_state(NULL, group_to_access(led->group), NULL, 0);
422 static ssize_t show_brightness(struct device *dev,
423 struct device_attribute *attr, char *buf)
425 struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
427 return sprintf(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
430 static DEVICE_ATTR(mode, 0644, show_mode, store_mode);
431 static DEVICE_ATTR(brightness, 0444, show_brightness, NULL);
433 static struct attribute *snd_ctl_led_dev_attrs[] = {
435 &dev_attr_brightness.attr,
439 static const struct attribute_group snd_ctl_led_dev_attr_group = {
440 .attrs = snd_ctl_led_dev_attrs,
443 static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
444 &snd_ctl_led_dev_attr_group,
448 static char *find_eos(char *s)
450 while (*s && *s != ',')
457 static char *parse_uint(char *s, unsigned int *val)
459 unsigned long long res;
460 if (kstrtoull(s, 10, &res))
466 static char *parse_string(char *s, char *val, size_t val_size)
468 if (*s == '"' || *s == '\'') {
471 while (*s && *s != c) {
479 while (*s && *s != ',') {
493 static char *parse_iface(char *s, unsigned int *val)
495 if (!strncasecmp(s, "card", 4))
496 *val = SNDRV_CTL_ELEM_IFACE_CARD;
497 else if (!strncasecmp(s, "mixer", 5))
498 *val = SNDRV_CTL_ELEM_IFACE_MIXER;
503 * These types of input strings are accepted:
505 * unsigned integer - numid (equivaled to numid=UINT)
506 * string - basic mixer name (equivalent to iface=MIXER,name=STR)
508 * [iface=MIXER,][device=UINT,][subdevice=UINT,]name=STR[,index=UINT]
510 static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, size_t count,
513 char buf2[256], *s, *os;
514 size_t len = max(sizeof(s) - 1, count);
515 struct snd_ctl_elem_id id;
518 strncpy(buf2, buf, len);
520 memset(&id, 0, sizeof(id));
521 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
525 if (!strncasecmp(s, "numid=", 6)) {
526 s = parse_uint(s + 6, &id.numid);
527 } else if (!strncasecmp(s, "iface=", 6)) {
528 s = parse_iface(s + 6, &id.iface);
529 } else if (!strncasecmp(s, "device=", 7)) {
530 s = parse_uint(s + 7, &id.device);
531 } else if (!strncasecmp(s, "subdevice=", 10)) {
532 s = parse_uint(s + 10, &id.subdevice);
533 } else if (!strncasecmp(s, "name=", 5)) {
534 s = parse_string(s + 5, id.name, sizeof(id.name));
535 } else if (!strncasecmp(s, "index=", 6)) {
536 s = parse_uint(s + 6, &id.index);
537 } else if (s == buf2) {
539 if (*s < '0' || *s > '9')
544 parse_uint(buf2, &id.numid);
546 for (; *s >= ' '; s++);
548 strlcpy(id.name, buf2, sizeof(id.name));
558 err = snd_ctl_led_set_id(led_card->number, &id, led_card->led->group, attach);
565 static ssize_t parse_attach(struct device *dev, struct device_attribute *attr,
566 const char *buf, size_t count)
568 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
569 return set_led_id(led_card, buf, count, true);
572 static ssize_t parse_detach(struct device *dev, struct device_attribute *attr,
573 const char *buf, size_t count)
575 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
576 return set_led_id(led_card, buf, count, false);
579 static ssize_t ctl_reset(struct device *dev, struct device_attribute *attr,
580 const char *buf, size_t count)
582 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
585 if (count > 0 && buf[0] == '1') {
586 err = snd_ctl_led_reset(led_card->number, led_card->led->group);
593 static ssize_t ctl_list(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
597 struct snd_card *card;
598 struct snd_ctl_led_ctl *lctl;
602 card = snd_card_ref(led_card->number);
605 down_read(&card->controls_rwsem);
606 mutex_lock(&snd_ctl_led_mutex);
607 if (snd_ctl_led_card_valid[led_card->number]) {
608 list_for_each_entry(lctl, &led_card->led->controls, list)
609 if (lctl->card == card) {
610 if (buf2 - buf > PAGE_SIZE - 16)
614 l = scnprintf(buf2, 15, "%u",
615 lctl->kctl->id.numid +
621 mutex_unlock(&snd_ctl_led_mutex);
622 up_read(&card->controls_rwsem);
623 snd_card_unref(card);
627 static DEVICE_ATTR(attach, 0200, NULL, parse_attach);
628 static DEVICE_ATTR(detach, 0200, NULL, parse_detach);
629 static DEVICE_ATTR(reset, 0200, NULL, ctl_reset);
630 static DEVICE_ATTR(list, 0444, ctl_list, NULL);
632 static struct attribute *snd_ctl_led_card_attrs[] = {
633 &dev_attr_attach.attr,
634 &dev_attr_detach.attr,
635 &dev_attr_reset.attr,
640 static const struct attribute_group snd_ctl_led_card_attr_group = {
641 .attrs = snd_ctl_led_card_attrs,
644 static const struct attribute_group *snd_ctl_led_card_attr_groups[] = {
645 &snd_ctl_led_card_attr_group,
649 static struct device snd_ctl_led_dev;
651 static void snd_ctl_led_sysfs_add(struct snd_card *card)
654 struct snd_ctl_led_card *led_card;
655 struct snd_ctl_led *led;
658 for (group = 0; group < MAX_LED; group++) {
659 led = &snd_ctl_leds[group];
660 led_card = kzalloc(sizeof(*led_card), GFP_KERNEL);
663 led_card->number = card->number;
665 device_initialize(&led_card->dev);
666 if (dev_set_name(&led_card->dev, "card%d", card->number) < 0)
668 led_card->dev.parent = &led->dev;
669 led_card->dev.groups = snd_ctl_led_card_attr_groups;
670 if (device_add(&led_card->dev))
672 led->cards[card->number] = led_card;
673 snprintf(link_name, sizeof(link_name), "led-%s", led->name);
674 WARN(sysfs_create_link(&card->ctl_dev.kobj, &led_card->dev.kobj, link_name),
675 "can't create symlink to controlC%i device\n", card->number);
676 WARN(sysfs_create_link(&led_card->dev.kobj, &card->card_dev.kobj, "card"),
677 "can't create symlink to card%i\n", card->number);
681 put_device(&led_card->dev);
683 printk(KERN_ERR "snd_ctl_led: unable to add card%d", card->number);
688 static void snd_ctl_led_sysfs_remove(struct snd_card *card)
691 struct snd_ctl_led_card *led_card;
692 struct snd_ctl_led *led;
695 for (group = 0; group < MAX_LED; group++) {
696 led = &snd_ctl_leds[group];
697 led_card = led->cards[card->number];
700 snprintf(link_name, sizeof(link_name), "led-%s", led->name);
701 sysfs_remove_link(&card->ctl_dev.kobj, link_name);
702 sysfs_remove_link(&led_card->dev.kobj, "card");
703 device_del(&led_card->dev);
705 led->cards[card->number] = NULL;
710 * Control layer registration
712 static struct snd_ctl_layer_ops snd_ctl_led_lops = {
713 .module_name = SND_CTL_LAYER_MODULE_LED,
714 .lregister = snd_ctl_led_register,
715 .ldisconnect = snd_ctl_led_disconnect,
716 .lnotify = snd_ctl_led_notify,
719 static int __init snd_ctl_led_init(void)
721 struct snd_ctl_led *led;
724 device_initialize(&snd_ctl_led_dev);
725 snd_ctl_led_dev.class = sound_class;
726 dev_set_name(&snd_ctl_led_dev, "ctl-led");
727 if (device_add(&snd_ctl_led_dev)) {
728 put_device(&snd_ctl_led_dev);
731 for (group = 0; group < MAX_LED; group++) {
732 led = &snd_ctl_leds[group];
733 INIT_LIST_HEAD(&led->controls);
734 device_initialize(&led->dev);
735 led->dev.parent = &snd_ctl_led_dev;
736 led->dev.groups = snd_ctl_led_dev_attr_groups;
737 dev_set_name(&led->dev, led->name);
738 if (device_add(&led->dev)) {
739 put_device(&led->dev);
740 for (; group > 0; group--) {
741 led = &snd_ctl_leds[group - 1];
742 device_del(&led->dev);
744 device_del(&snd_ctl_led_dev);
748 snd_ctl_register_layer(&snd_ctl_led_lops);
752 static void __exit snd_ctl_led_exit(void)
754 struct snd_ctl_led *led;
755 struct snd_card *card;
756 unsigned int group, card_number;
758 snd_ctl_disconnect_layer(&snd_ctl_led_lops);
759 for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
760 if (!snd_ctl_led_card_valid[card_number])
762 card = snd_card_ref(card_number);
764 snd_ctl_led_sysfs_remove(card);
765 snd_card_unref(card);
768 for (group = 0; group < MAX_LED; group++) {
769 led = &snd_ctl_leds[group];
770 device_del(&led->dev);
772 device_del(&snd_ctl_led_dev);
773 snd_ctl_led_clean(NULL);
776 module_init(snd_ctl_led_init)
777 module_exit(snd_ctl_led_exit)