Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  LED state routines for driver control interface
  4 *  Copyright (c) 2021 by Jaroslav Kysela <perex@perex.cz>
  5 */
  6
  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>
 12
 13MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
 14MODULE_DESCRIPTION("ALSA control interface to LED trigger code.");
 15MODULE_LICENSE("GPL");
 16
 17#define MAX_LED (((SNDRV_CTL_ELEM_ACCESS_MIC_LED - SNDRV_CTL_ELEM_ACCESS_SPK_LED) \
 18			>> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) + 1)
 19
 20#define to_led_card_dev(_dev) \
 21	container_of(_dev, struct snd_ctl_led_card, dev)
 22
 23enum snd_ctl_led_mode {
 24	 MODE_FOLLOW_MUTE = 0,
 25	 MODE_FOLLOW_ROUTE,
 26	 MODE_OFF,
 27	 MODE_ON,
 28};
 29
 30struct snd_ctl_led_card {
 31	struct device dev;
 32	int number;
 33	struct snd_ctl_led *led;
 34};
 35
 36struct snd_ctl_led {
 37	struct device dev;
 38	struct list_head controls;
 39	const char *name;
 40	unsigned int group;
 41	enum led_audio trigger_type;
 42	enum snd_ctl_led_mode mode;
 43	struct snd_ctl_led_card *cards[SNDRV_CARDS];
 44};
 45
 46struct snd_ctl_led_ctl {
 47	struct list_head list;
 48	struct snd_card *card;
 49	unsigned int access;
 50	struct snd_kcontrol *kctl;
 51	unsigned int index_offset;
 52};
 53
 54static DEFINE_MUTEX(snd_ctl_led_mutex);
 55static bool snd_ctl_led_card_valid[SNDRV_CARDS];
 56static struct led_trigger *snd_ctl_ledtrig_audio[NUM_AUDIO_LEDS];
 57static struct snd_ctl_led snd_ctl_leds[MAX_LED] = {
 58	{
 59		.name = "speaker",
 60		.group = (SNDRV_CTL_ELEM_ACCESS_SPK_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
 61		.trigger_type = LED_AUDIO_MUTE,
 62		.mode = MODE_FOLLOW_MUTE,
 63	},
 64	{
 65		.name = "mic",
 66		.group = (SNDRV_CTL_ELEM_ACCESS_MIC_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
 67		.trigger_type = LED_AUDIO_MICMUTE,
 68		.mode = MODE_FOLLOW_MUTE,
 69	},
 70};
 71
 72static void snd_ctl_led_sysfs_add(struct snd_card *card);
 73static void snd_ctl_led_sysfs_remove(struct snd_card *card);
 74
 75#define UPDATE_ROUTE(route, cb) \
 76	do { \
 77		int route2 = (cb); \
 78		if (route2 >= 0) \
 79			route = route < 0 ? route2 : (route | route2); \
 80	} while (0)
 81
 82static inline unsigned int access_to_group(unsigned int access)
 83{
 84	return ((access & SNDRV_CTL_ELEM_ACCESS_LED_MASK) >>
 85				SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1;
 86}
 87
 88static inline unsigned int group_to_access(unsigned int group)
 89{
 90	return (group + 1) << SNDRV_CTL_ELEM_ACCESS_LED_SHIFT;
 91}
 92
 93static struct snd_ctl_led *snd_ctl_led_get_by_access(unsigned int access)
 94{
 95	unsigned int group = access_to_group(access);
 96	if (group >= MAX_LED)
 97		return NULL;
 98	return &snd_ctl_leds[group];
 99}
100
101/*
102 * A note for callers:
103 *   The two static variables info and value are protected using snd_ctl_led_mutex.
104 */
105static int snd_ctl_led_get(struct snd_ctl_led_ctl *lctl)
106{
107	static struct snd_ctl_elem_info info;
108	static struct snd_ctl_elem_value value;
109	struct snd_kcontrol *kctl = lctl->kctl;
110	unsigned int i;
111	int result;
112
113	memset(&info, 0, sizeof(info));
114	info.id = kctl->id;
115	info.id.index += lctl->index_offset;
116	info.id.numid += lctl->index_offset;
117	result = kctl->info(kctl, &info);
118	if (result < 0)
119		return -1;
120	memset(&value, 0, sizeof(value));
121	value.id = info.id;
122	result = kctl->get(kctl, &value);
123	if (result < 0)
124		return -1;
125	if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
126	    info.type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
127		for (i = 0; i < info.count; i++)
128			if (value.value.integer.value[i] != info.value.integer.min)
129				return 1;
130	} else if (info.type == SNDRV_CTL_ELEM_TYPE_INTEGER64) {
131		for (i = 0; i < info.count; i++)
132			if (value.value.integer64.value[i] != info.value.integer64.min)
133				return 1;
134	}
135	return 0;
136}
137
138static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
139				  struct snd_kcontrol *kctl, unsigned int ioff)
140{
141	struct snd_ctl_led *led;
142	struct snd_ctl_led_ctl *lctl;
143	int route;
144	bool found;
145
146	led = snd_ctl_led_get_by_access(access);
147	if (!led)
148		return;
149	route = -1;
150	found = false;
151	scoped_guard(mutex, &snd_ctl_led_mutex) {
152		/* the card may not be registered (active) at this point */
153		if (card && !snd_ctl_led_card_valid[card->number])
154			return;
155		list_for_each_entry(lctl, &led->controls, list) {
156			if (lctl->kctl == kctl && lctl->index_offset == ioff)
157				found = true;
 
 
 
 
 
 
 
 
 
 
 
 
158			UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
159		}
160		if (!found && kctl && card) {
161			lctl = kzalloc(sizeof(*lctl), GFP_KERNEL);
162			if (lctl) {
163				lctl->card = card;
164				lctl->access = access;
165				lctl->kctl = kctl;
166				lctl->index_offset = ioff;
167				list_add(&lctl->list, &led->controls);
168				UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
169			}
170		}
171	}
 
172	switch (led->mode) {
173	case MODE_OFF:		route = 1; break;
174	case MODE_ON:		route = 0; break;
175	case MODE_FOLLOW_ROUTE:	if (route >= 0) route ^= 1; break;
176	case MODE_FOLLOW_MUTE:	/* noop */ break;
177	}
178	if (route >= 0) {
179		struct led_trigger *trig = snd_ctl_ledtrig_audio[led->trigger_type];
180
181		led_trigger_event(trig, route ? LED_OFF : LED_ON);
182	}
183}
184
185static struct snd_ctl_led_ctl *snd_ctl_led_find(struct snd_kcontrol *kctl, unsigned int ioff)
186{
187	struct list_head *controls;
188	struct snd_ctl_led_ctl *lctl;
189	unsigned int group;
190
191	for (group = 0; group < MAX_LED; group++) {
192		controls = &snd_ctl_leds[group].controls;
193		list_for_each_entry(lctl, controls, list)
194			if (lctl->kctl == kctl && lctl->index_offset == ioff)
195				return lctl;
196	}
197	return NULL;
198}
199
200static unsigned int snd_ctl_led_remove(struct snd_kcontrol *kctl, unsigned int ioff,
201				       unsigned int access)
202{
203	struct snd_ctl_led_ctl *lctl;
204	unsigned int ret = 0;
205
206	guard(mutex)(&snd_ctl_led_mutex);
207	lctl = snd_ctl_led_find(kctl, ioff);
208	if (lctl && (access == 0 || access != lctl->access)) {
209		ret = lctl->access;
210		list_del(&lctl->list);
211		kfree(lctl);
212	}
 
213	return ret;
214}
215
216static void snd_ctl_led_notify(struct snd_card *card, unsigned int mask,
217			       struct snd_kcontrol *kctl, unsigned int ioff)
218{
219	struct snd_kcontrol_volatile *vd;
220	unsigned int access, access2;
221
222	if (mask == SNDRV_CTL_EVENT_MASK_REMOVE) {
223		access = snd_ctl_led_remove(kctl, ioff, 0);
224		if (access)
225			snd_ctl_led_set_state(card, access, NULL, 0);
226	} else if (mask & SNDRV_CTL_EVENT_MASK_INFO) {
227		vd = &kctl->vd[ioff];
228		access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
229		access2 = snd_ctl_led_remove(kctl, ioff, access);
230		if (access2)
231			snd_ctl_led_set_state(card, access2, NULL, 0);
232		if (access)
233			snd_ctl_led_set_state(card, access, kctl, ioff);
234	} else if ((mask & (SNDRV_CTL_EVENT_MASK_ADD |
235			    SNDRV_CTL_EVENT_MASK_VALUE)) != 0) {
236		vd = &kctl->vd[ioff];
237		access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
238		if (access)
239			snd_ctl_led_set_state(card, access, kctl, ioff);
240	}
241}
242
243DEFINE_FREE(snd_card_unref, struct snd_card *, if (_T) snd_card_unref(_T))
244
245static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,
246			      unsigned int group, bool set)
247{
248	struct snd_card *card __free(snd_card_unref) = NULL;
249	struct snd_kcontrol *kctl;
250	struct snd_kcontrol_volatile *vd;
251	unsigned int ioff, access, new_access;
 
252
253	card = snd_card_ref(card_number);
254	if (!card)
255		return -ENXIO;
256	guard(rwsem_write)(&card->controls_rwsem);
257	kctl = snd_ctl_find_id(card, id);
258	if (!kctl)
259		return -ENOENT;
260	ioff = snd_ctl_get_ioff(kctl, id);
261	vd = &kctl->vd[ioff];
262	access = vd->access & SNDRV_CTL_ELEM_ACCESS_LED_MASK;
263	if (access != 0 && access != group_to_access(group))
264		return -EXDEV;
265	new_access = vd->access & ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
266	if (set)
267		new_access |= group_to_access(group);
268	if (new_access != vd->access) {
269		vd->access = new_access;
270		snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_INFO, kctl, ioff);
 
 
 
 
 
 
 
 
 
271	}
272	return 0;
273}
274
275static void snd_ctl_led_refresh(void)
276{
277	unsigned int group;
278
279	for (group = 0; group < MAX_LED; group++)
280		snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
281}
282
283static void snd_ctl_led_ctl_destroy(struct snd_ctl_led_ctl *lctl)
284{
285	list_del(&lctl->list);
286	kfree(lctl);
287}
288
289static void snd_ctl_led_clean(struct snd_card *card)
290{
291	unsigned int group;
292	struct snd_ctl_led_ctl *lctl, *_lctl;
293	struct snd_ctl_led *led;
 
294
295	for (group = 0; group < MAX_LED; group++) {
296		led = &snd_ctl_leds[group];
297		list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
298			if (!card || lctl->card == card)
 
299				snd_ctl_led_ctl_destroy(lctl);
 
 
300	}
301}
302
303static int snd_ctl_led_reset(int card_number, unsigned int group)
304{
305	struct snd_card *card __free(snd_card_unref) = NULL;
306	struct snd_ctl_led_ctl *lctl, *_lctl;
307	struct snd_ctl_led *led;
 
308	struct snd_kcontrol_volatile *vd;
309	bool change = false;
310
311	card = snd_card_ref(card_number);
312	if (!card)
313		return -ENXIO;
314
315	scoped_guard(mutex, &snd_ctl_led_mutex) {
316		if (!snd_ctl_led_card_valid[card_number])
317			return -ENXIO;
318		led = &snd_ctl_leds[group];
319		list_for_each_entry_safe(lctl, _lctl, &led->controls, list)
320			if (lctl->card == card) {
321				vd = &lctl->kctl->vd[lctl->index_offset];
322				vd->access &= ~group_to_access(group);
323				snd_ctl_led_ctl_destroy(lctl);
324				change = true;
325			}
326	}
 
 
 
 
 
 
 
 
 
 
 
327	if (change)
328		snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
 
329	return 0;
330}
331
332static void snd_ctl_led_register(struct snd_card *card)
333{
334	struct snd_kcontrol *kctl;
335	unsigned int ioff;
336
337	if (snd_BUG_ON(card->number < 0 ||
338		       card->number >= ARRAY_SIZE(snd_ctl_led_card_valid)))
339		return;
340	scoped_guard(mutex, &snd_ctl_led_mutex)
341		snd_ctl_led_card_valid[card->number] = true;
 
342	/* the register callback is already called with held card->controls_rwsem */
343	list_for_each_entry(kctl, &card->controls, list)
344		for (ioff = 0; ioff < kctl->count; ioff++)
345			snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
346	snd_ctl_led_refresh();
347	snd_ctl_led_sysfs_add(card);
348}
349
350static void snd_ctl_led_disconnect(struct snd_card *card)
351{
352	snd_ctl_led_sysfs_remove(card);
353	scoped_guard(mutex, &snd_ctl_led_mutex) {
354		snd_ctl_led_card_valid[card->number] = false;
355		snd_ctl_led_clean(card);
356	}
357	snd_ctl_led_refresh();
358}
359
360static void snd_ctl_led_card_release(struct device *dev)
361{
362	struct snd_ctl_led_card *led_card = to_led_card_dev(dev);
363
364	kfree(led_card);
365}
366
367static void snd_ctl_led_release(struct device *dev)
368{
369}
370
371static void snd_ctl_led_dev_release(struct device *dev)
372{
373}
374
375/*
376 * sysfs
377 */
378
379static ssize_t mode_show(struct device *dev,
380			 struct device_attribute *attr, char *buf)
381{
382	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
383	const char *str = NULL;
384
385	switch (led->mode) {
386	case MODE_FOLLOW_MUTE:	str = "follow-mute"; break;
387	case MODE_FOLLOW_ROUTE:	str = "follow-route"; break;
388	case MODE_ON:		str = "on"; break;
389	case MODE_OFF:		str = "off"; break;
390	}
391	return sysfs_emit(buf, "%s\n", str);
392}
393
394static ssize_t mode_store(struct device *dev,
395			  struct device_attribute *attr,
396			  const char *buf, size_t count)
397{
398	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
399	char _buf[16];
400	size_t l = min(count, sizeof(_buf) - 1);
401	enum snd_ctl_led_mode mode;
402
403	memcpy(_buf, buf, l);
404	_buf[l] = '\0';
405	if (strstr(_buf, "mute"))
406		mode = MODE_FOLLOW_MUTE;
407	else if (strstr(_buf, "route"))
408		mode = MODE_FOLLOW_ROUTE;
409	else if (strncmp(_buf, "off", 3) == 0 || strncmp(_buf, "0", 1) == 0)
410		mode = MODE_OFF;
411	else if (strncmp(_buf, "on", 2) == 0 || strncmp(_buf, "1", 1) == 0)
412		mode = MODE_ON;
413	else
414		return count;
415
416	scoped_guard(mutex, &snd_ctl_led_mutex)
417		led->mode = mode;
 
418
419	snd_ctl_led_set_state(NULL, group_to_access(led->group), NULL, 0);
420	return count;
421}
422
423static ssize_t brightness_show(struct device *dev,
424			       struct device_attribute *attr, char *buf)
425{
426	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
427	struct led_trigger *trig = snd_ctl_ledtrig_audio[led->trigger_type];
428
429	return sysfs_emit(buf, "%u\n", led_trigger_get_brightness(trig));
430}
431
432static DEVICE_ATTR_RW(mode);
433static DEVICE_ATTR_RO(brightness);
434
435static struct attribute *snd_ctl_led_dev_attrs[] = {
436	&dev_attr_mode.attr,
437	&dev_attr_brightness.attr,
438	NULL,
439};
440
441static const struct attribute_group snd_ctl_led_dev_attr_group = {
442	.attrs = snd_ctl_led_dev_attrs,
443};
444
445static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
446	&snd_ctl_led_dev_attr_group,
447	NULL,
448};
449
450static char *find_eos(char *s)
451{
452	while (*s && *s != ',')
453		s++;
454	if (*s)
455		s++;
456	return s;
457}
458
459static char *parse_uint(char *s, unsigned int *val)
460{
461	unsigned long long res;
462	if (kstrtoull(s, 10, &res))
463		res = 0;
464	*val = res;
465	return find_eos(s);
466}
467
468static char *parse_string(char *s, char *val, size_t val_size)
469{
470	if (*s == '"' || *s == '\'') {
471		char c = *s;
472		s++;
473		while (*s && *s != c) {
474			if (val_size > 1) {
475				*val++ = *s;
476				val_size--;
477			}
478			s++;
479		}
480	} else {
481		while (*s && *s != ',') {
482			if (val_size > 1) {
483				*val++ = *s;
484				val_size--;
485			}
486			s++;
487		}
488	}
489	*val = '\0';
490	if (*s)
491		s++;
492	return s;
493}
494
495static char *parse_iface(char *s, snd_ctl_elem_iface_t *val)
496{
497	if (!strncasecmp(s, "card", 4))
498		*val = SNDRV_CTL_ELEM_IFACE_CARD;
499	else if (!strncasecmp(s, "mixer", 5))
500		*val = SNDRV_CTL_ELEM_IFACE_MIXER;
501	return find_eos(s);
502}
503
504/*
505 * These types of input strings are accepted:
506 *
507 *   unsigned integer - numid (equivaled to numid=UINT)
508 *   string - basic mixer name (equivalent to iface=MIXER,name=STR)
509 *   numid=UINT
510 *   [iface=MIXER,][device=UINT,][subdevice=UINT,]name=STR[,index=UINT]
511 */
512static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, size_t count,
513			  bool attach)
514{
515	char buf2[256], *s, *os;
516	struct snd_ctl_elem_id id;
517	int err;
518
519	if (strscpy(buf2, buf, sizeof(buf2)) < 0)
520		return -E2BIG;
521	memset(&id, 0, sizeof(id));
522	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
523	s = buf2;
524	while (*s) {
525		os = s;
526		if (!strncasecmp(s, "numid=", 6)) {
527			s = parse_uint(s + 6, &id.numid);
528		} else if (!strncasecmp(s, "iface=", 6)) {
529			s = parse_iface(s + 6, &id.iface);
530		} else if (!strncasecmp(s, "device=", 7)) {
531			s = parse_uint(s + 7, &id.device);
532		} else if (!strncasecmp(s, "subdevice=", 10)) {
533			s = parse_uint(s + 10, &id.subdevice);
534		} else if (!strncasecmp(s, "name=", 5)) {
535			s = parse_string(s + 5, id.name, sizeof(id.name));
536		} else if (!strncasecmp(s, "index=", 6)) {
537			s = parse_uint(s + 6, &id.index);
538		} else if (s == buf2) {
539			while (*s) {
540				if (*s < '0' || *s > '9')
541					break;
542				s++;
543			}
544			if (*s == '\0')
545				parse_uint(buf2, &id.numid);
546			else {
547				for (; *s >= ' '; s++);
548				*s = '\0';
549				strscpy(id.name, buf2, sizeof(id.name));
550			}
551			break;
552		}
553		if (*s == ',')
554			s++;
555		if (s == os)
556			break;
557	}
558
559	err = snd_ctl_led_set_id(led_card->number, &id, led_card->led->group, attach);
560	if (err < 0)
561		return err;
562
563	return count;
564}
565
566static ssize_t attach_store(struct device *dev,
567			    struct device_attribute *attr,
568			    const char *buf, size_t count)
569{
570	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
571	return set_led_id(led_card, buf, count, true);
572}
573
574static ssize_t detach_store(struct device *dev,
575			    struct device_attribute *attr,
576			    const char *buf, size_t count)
577{
578	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
579	return set_led_id(led_card, buf, count, false);
580}
581
582static ssize_t reset_store(struct device *dev,
583			   struct device_attribute *attr,
584			   const char *buf, size_t count)
585{
586	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
587	int err;
588
589	if (count > 0 && buf[0] == '1') {
590		err = snd_ctl_led_reset(led_card->number, led_card->led->group);
591		if (err < 0)
592			return err;
593	}
594	return count;
595}
596
597static ssize_t list_show(struct device *dev,
598			 struct device_attribute *attr, char *buf)
599{
600	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
601	struct snd_card *card __free(snd_card_unref) = NULL;
602	struct snd_ctl_led_ctl *lctl;
603	size_t l = 0;
604
605	card = snd_card_ref(led_card->number);
606	if (!card)
607		return -ENXIO;
608	guard(rwsem_read)(&card->controls_rwsem);
609	guard(mutex)(&snd_ctl_led_mutex);
610	if (snd_ctl_led_card_valid[led_card->number]) {
611		list_for_each_entry(lctl, &led_card->led->controls, list) {
612			if (lctl->card != card)
613				continue;
614			if (l)
615				l += sysfs_emit_at(buf, l, " ");
616			l += sysfs_emit_at(buf, l, "%u",
617					   lctl->kctl->id.numid + lctl->index_offset);
618		}
619	}
 
 
 
620	return l;
621}
622
623static DEVICE_ATTR_WO(attach);
624static DEVICE_ATTR_WO(detach);
625static DEVICE_ATTR_WO(reset);
626static DEVICE_ATTR_RO(list);
627
628static struct attribute *snd_ctl_led_card_attrs[] = {
629	&dev_attr_attach.attr,
630	&dev_attr_detach.attr,
631	&dev_attr_reset.attr,
632	&dev_attr_list.attr,
633	NULL,
634};
635
636static const struct attribute_group snd_ctl_led_card_attr_group = {
637	.attrs = snd_ctl_led_card_attrs,
638};
639
640static const struct attribute_group *snd_ctl_led_card_attr_groups[] = {
641	&snd_ctl_led_card_attr_group,
642	NULL,
643};
644
645static struct device snd_ctl_led_dev;
646
647static void snd_ctl_led_sysfs_add(struct snd_card *card)
648{
649	unsigned int group;
650	struct snd_ctl_led_card *led_card;
651	struct snd_ctl_led *led;
652	char link_name[32];
653
654	for (group = 0; group < MAX_LED; group++) {
655		led = &snd_ctl_leds[group];
656		led_card = kzalloc(sizeof(*led_card), GFP_KERNEL);
657		if (!led_card)
658			goto cerr2;
659		led_card->number = card->number;
660		led_card->led = led;
661		device_initialize(&led_card->dev);
662		led_card->dev.release = snd_ctl_led_card_release;
663		if (dev_set_name(&led_card->dev, "card%d", card->number) < 0)
664			goto cerr;
665		led_card->dev.parent = &led->dev;
666		led_card->dev.groups = snd_ctl_led_card_attr_groups;
667		if (device_add(&led_card->dev))
668			goto cerr;
669		led->cards[card->number] = led_card;
670		snprintf(link_name, sizeof(link_name), "led-%s", led->name);
671		if (sysfs_create_link(&card->ctl_dev->kobj, &led_card->dev.kobj,
672				      link_name))
673			dev_err(card->dev,
674				"%s: can't create symlink to controlC%i device\n",
675				 __func__, card->number);
676		if (sysfs_create_link(&led_card->dev.kobj, &card->card_dev.kobj,
677				      "card"))
678			dev_err(card->dev,
679				"%s: can't create symlink to card%i\n",
680				__func__, card->number);
681
682		continue;
683cerr:
684		put_device(&led_card->dev);
685cerr2:
686		dev_err(card->dev, "snd_ctl_led: unable to add card%d", card->number);
687	}
688}
689
690static void snd_ctl_led_sysfs_remove(struct snd_card *card)
691{
692	unsigned int group;
693	struct snd_ctl_led_card *led_card;
694	struct snd_ctl_led *led;
695	char link_name[32];
696
697	for (group = 0; group < MAX_LED; group++) {
698		led = &snd_ctl_leds[group];
699		led_card = led->cards[card->number];
700		if (!led_card)
701			continue;
702		snprintf(link_name, sizeof(link_name), "led-%s", led->name);
703		sysfs_remove_link(&card->ctl_dev->kobj, link_name);
704		sysfs_remove_link(&led_card->dev.kobj, "card");
705		device_unregister(&led_card->dev);
706		led->cards[card->number] = NULL;
707	}
708}
709
710/*
711 * Control layer registration
712 */
713static struct snd_ctl_layer_ops snd_ctl_led_lops = {
714	.module_name = SND_CTL_LAYER_MODULE_LED,
715	.lregister = snd_ctl_led_register,
716	.ldisconnect = snd_ctl_led_disconnect,
717	.lnotify = snd_ctl_led_notify,
718};
719
720static int __init snd_ctl_led_init(void)
721{
722	struct snd_ctl_led *led;
723	unsigned int group;
724
725	led_trigger_register_simple("audio-mute", &snd_ctl_ledtrig_audio[LED_AUDIO_MUTE]);
726	led_trigger_register_simple("audio-micmute", &snd_ctl_ledtrig_audio[LED_AUDIO_MICMUTE]);
727
728	device_initialize(&snd_ctl_led_dev);
729	snd_ctl_led_dev.class = &sound_class;
730	snd_ctl_led_dev.release = snd_ctl_led_dev_release;
731	dev_set_name(&snd_ctl_led_dev, "ctl-led");
732	if (device_add(&snd_ctl_led_dev)) {
733		put_device(&snd_ctl_led_dev);
734		return -ENOMEM;
735	}
736	for (group = 0; group < MAX_LED; group++) {
737		led = &snd_ctl_leds[group];
738		INIT_LIST_HEAD(&led->controls);
739		device_initialize(&led->dev);
740		led->dev.parent = &snd_ctl_led_dev;
741		led->dev.release = snd_ctl_led_release;
742		led->dev.groups = snd_ctl_led_dev_attr_groups;
743		dev_set_name(&led->dev, led->name);
744		if (device_add(&led->dev)) {
745			put_device(&led->dev);
746			for (; group > 0; group--) {
747				led = &snd_ctl_leds[group - 1];
748				device_unregister(&led->dev);
749			}
750			device_unregister(&snd_ctl_led_dev);
751			return -ENOMEM;
752		}
753	}
754	snd_ctl_register_layer(&snd_ctl_led_lops);
755	return 0;
756}
757
758static void __exit snd_ctl_led_exit(void)
759{
760	struct snd_ctl_led *led;
761	struct snd_card *card;
762	unsigned int group, card_number;
763
764	snd_ctl_disconnect_layer(&snd_ctl_led_lops);
765	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
766		if (!snd_ctl_led_card_valid[card_number])
767			continue;
768		card = snd_card_ref(card_number);
769		if (card) {
770			snd_ctl_led_sysfs_remove(card);
771			snd_card_unref(card);
772		}
773	}
774	for (group = 0; group < MAX_LED; group++) {
775		led = &snd_ctl_leds[group];
776		device_unregister(&led->dev);
777	}
778	device_unregister(&snd_ctl_led_dev);
779	snd_ctl_led_clean(NULL);
780
781	led_trigger_unregister_simple(snd_ctl_ledtrig_audio[LED_AUDIO_MUTE]);
782	led_trigger_unregister_simple(snd_ctl_ledtrig_audio[LED_AUDIO_MICMUTE]);
783}
784
785module_init(snd_ctl_led_init)
786module_exit(snd_ctl_led_exit)
787
788MODULE_ALIAS("ledtrig:audio-mute");
789MODULE_ALIAS("ledtrig:audio-micmute");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  LED state routines for driver control interface
  4 *  Copyright (c) 2021 by Jaroslav Kysela <perex@perex.cz>
  5 */
  6
  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>
 12
 13MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
 14MODULE_DESCRIPTION("ALSA control interface to LED trigger code.");
 15MODULE_LICENSE("GPL");
 16
 17#define MAX_LED (((SNDRV_CTL_ELEM_ACCESS_MIC_LED - SNDRV_CTL_ELEM_ACCESS_SPK_LED) \
 18			>> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) + 1)
 19
 20#define to_led_card_dev(_dev) \
 21	container_of(_dev, struct snd_ctl_led_card, dev)
 22
 23enum snd_ctl_led_mode {
 24	 MODE_FOLLOW_MUTE = 0,
 25	 MODE_FOLLOW_ROUTE,
 26	 MODE_OFF,
 27	 MODE_ON,
 28};
 29
 30struct snd_ctl_led_card {
 31	struct device dev;
 32	int number;
 33	struct snd_ctl_led *led;
 34};
 35
 36struct snd_ctl_led {
 37	struct device dev;
 38	struct list_head controls;
 39	const char *name;
 40	unsigned int group;
 41	enum led_audio trigger_type;
 42	enum snd_ctl_led_mode mode;
 43	struct snd_ctl_led_card *cards[SNDRV_CARDS];
 44};
 45
 46struct snd_ctl_led_ctl {
 47	struct list_head list;
 48	struct snd_card *card;
 49	unsigned int access;
 50	struct snd_kcontrol *kctl;
 51	unsigned int index_offset;
 52};
 53
 54static DEFINE_MUTEX(snd_ctl_led_mutex);
 55static bool snd_ctl_led_card_valid[SNDRV_CARDS];
 
 56static struct snd_ctl_led snd_ctl_leds[MAX_LED] = {
 57	{
 58		.name = "speaker",
 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,
 62	},
 63	{
 64		.name = "mic",
 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,
 68	},
 69};
 70
 71static void snd_ctl_led_sysfs_add(struct snd_card *card);
 72static void snd_ctl_led_sysfs_remove(struct snd_card *card);
 73
 74#define UPDATE_ROUTE(route, cb) \
 75	do { \
 76		int route2 = (cb); \
 77		if (route2 >= 0) \
 78			route = route < 0 ? route2 : (route | route2); \
 79	} while (0)
 80
 81static inline unsigned int access_to_group(unsigned int access)
 82{
 83	return ((access & SNDRV_CTL_ELEM_ACCESS_LED_MASK) >>
 84				SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1;
 85}
 86
 87static inline unsigned int group_to_access(unsigned int group)
 88{
 89	return (group + 1) << SNDRV_CTL_ELEM_ACCESS_LED_SHIFT;
 90}
 91
 92static struct snd_ctl_led *snd_ctl_led_get_by_access(unsigned int access)
 93{
 94	unsigned int group = access_to_group(access);
 95	if (group >= MAX_LED)
 96		return NULL;
 97	return &snd_ctl_leds[group];
 98}
 99
100/*
101 * A note for callers:
102 *   The two static variables info and value are protected using snd_ctl_led_mutex.
103 */
104static int snd_ctl_led_get(struct snd_ctl_led_ctl *lctl)
105{
106	static struct snd_ctl_elem_info info;
107	static struct snd_ctl_elem_value value;
108	struct snd_kcontrol *kctl = lctl->kctl;
109	unsigned int i;
110	int result;
111
112	memset(&info, 0, sizeof(info));
113	info.id = kctl->id;
114	info.id.index += lctl->index_offset;
115	info.id.numid += lctl->index_offset;
116	result = kctl->info(kctl, &info);
117	if (result < 0)
118		return -1;
119	memset(&value, 0, sizeof(value));
120	value.id = info.id;
121	result = kctl->get(kctl, &value);
122	if (result < 0)
123		return -1;
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)
128				return 1;
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)
132				return 1;
133	}
134	return 0;
135}
136
137static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
138				  struct snd_kcontrol *kctl, unsigned int ioff)
139{
140	struct snd_ctl_led *led;
141	struct snd_ctl_led_ctl *lctl;
142	int route;
143	bool found;
144
145	led = snd_ctl_led_get_by_access(access);
146	if (!led)
147		return;
148	route = -1;
149	found = false;
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);
154		return;
155	}
156	list_for_each_entry(lctl, &led->controls, list) {
157		if (lctl->kctl == kctl && lctl->index_offset == ioff)
158			found = true;
159		UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
160	}
161	if (!found && kctl && card) {
162		lctl = kzalloc(sizeof(*lctl), GFP_KERNEL);
163		if (lctl) {
164			lctl->card = card;
165			lctl->access = access;
166			lctl->kctl = kctl;
167			lctl->index_offset = ioff;
168			list_add(&lctl->list, &led->controls);
169			UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
170		}
 
 
 
 
 
 
 
 
 
 
 
171	}
172	mutex_unlock(&snd_ctl_led_mutex);
173	switch (led->mode) {
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;
178	}
179	if (route >= 0)
180		ledtrig_audio_set(led->trigger_type, route ? LED_OFF : LED_ON);
 
 
 
181}
182
183static struct snd_ctl_led_ctl *snd_ctl_led_find(struct snd_kcontrol *kctl, unsigned int ioff)
184{
185	struct list_head *controls;
186	struct snd_ctl_led_ctl *lctl;
187	unsigned int group;
188
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)
193				return lctl;
194	}
195	return NULL;
196}
197
198static unsigned int snd_ctl_led_remove(struct snd_kcontrol *kctl, unsigned int ioff,
199				       unsigned int access)
200{
201	struct snd_ctl_led_ctl *lctl;
202	unsigned int ret = 0;
203
204	mutex_lock(&snd_ctl_led_mutex);
205	lctl = snd_ctl_led_find(kctl, ioff);
206	if (lctl && (access == 0 || access != lctl->access)) {
207		ret = lctl->access;
208		list_del(&lctl->list);
209		kfree(lctl);
210	}
211	mutex_unlock(&snd_ctl_led_mutex);
212	return ret;
213}
214
215static void snd_ctl_led_notify(struct snd_card *card, unsigned int mask,
216			       struct snd_kcontrol *kctl, unsigned int ioff)
217{
218	struct snd_kcontrol_volatile *vd;
219	unsigned int access, access2;
220
221	if (mask == SNDRV_CTL_EVENT_MASK_REMOVE) {
222		access = snd_ctl_led_remove(kctl, ioff, 0);
223		if (access)
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);
229		if (access2)
230			snd_ctl_led_set_state(card, access2, NULL, 0);
231		if (access)
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;
237		if (access)
238			snd_ctl_led_set_state(card, access, kctl, ioff);
239	}
240}
241
 
 
242static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,
243			      unsigned int group, bool set)
244{
245	struct snd_card *card;
246	struct snd_kcontrol *kctl;
247	struct snd_kcontrol_volatile *vd;
248	unsigned int ioff, access, new_access;
249	int err = 0;
250
251	card = snd_card_ref(card_number);
252	if (card) {
253		down_write(&card->controls_rwsem);
254		kctl = snd_ctl_find_id(card, id);
255		if (kctl) {
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)) {
260				err = -EXDEV;
261				goto unlock;
262			}
263			new_access = vd->access & ~SNDRV_CTL_ELEM_ACCESS_LED_MASK;
264			if (set)
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);
269			}
270		} else {
271			err = -ENOENT;
272		}
273unlock:
274		up_write(&card->controls_rwsem);
275		snd_card_unref(card);
276	} else {
277		err = -ENXIO;
278	}
279	return err;
280}
281
282static void snd_ctl_led_refresh(void)
283{
284	unsigned int group;
285
286	for (group = 0; group < MAX_LED; group++)
287		snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
288}
289
290static void snd_ctl_led_ctl_destroy(struct snd_ctl_led_ctl *lctl)
291{
292	list_del(&lctl->list);
293	kfree(lctl);
294}
295
296static void snd_ctl_led_clean(struct snd_card *card)
297{
298	unsigned int group;
 
299	struct snd_ctl_led *led;
300	struct snd_ctl_led_ctl *lctl;
301
302	for (group = 0; group < MAX_LED; group++) {
303		led = &snd_ctl_leds[group];
304repeat:
305		list_for_each_entry(lctl, &led->controls, list)
306			if (!card || lctl->card == card) {
307				snd_ctl_led_ctl_destroy(lctl);
308				goto repeat;
309			}
310	}
311}
312
313static int snd_ctl_led_reset(int card_number, unsigned int group)
314{
315	struct snd_card *card;
 
316	struct snd_ctl_led *led;
317	struct snd_ctl_led_ctl *lctl;
318	struct snd_kcontrol_volatile *vd;
319	bool change = false;
320
321	card = snd_card_ref(card_number);
322	if (!card)
323		return -ENXIO;
324
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);
329		return -ENXIO;
 
 
 
 
 
 
330	}
331	led = &snd_ctl_leds[group];
332repeat:
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);
338			change = true;
339			goto repeat;
340		}
341	mutex_unlock(&snd_ctl_led_mutex);
342	if (change)
343		snd_ctl_led_set_state(NULL, group_to_access(group), NULL, 0);
344	snd_card_unref(card);
345	return 0;
346}
347
348static void snd_ctl_led_register(struct snd_card *card)
349{
350	struct snd_kcontrol *kctl;
351	unsigned int ioff;
352
353	if (snd_BUG_ON(card->number < 0 ||
354		       card->number >= ARRAY_SIZE(snd_ctl_led_card_valid)))
355		return;
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);
365}
366
367static void snd_ctl_led_disconnect(struct snd_card *card)
368{
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();
375}
376
377static void snd_ctl_led_card_release(struct device *dev)
378{
379	struct snd_ctl_led_card *led_card = to_led_card_dev(dev);
380
381	kfree(led_card);
382}
383
384static void snd_ctl_led_release(struct device *dev)
385{
386}
387
388static void snd_ctl_led_dev_release(struct device *dev)
389{
390}
391
392/*
393 * sysfs
394 */
395
396static ssize_t mode_show(struct device *dev,
397			 struct device_attribute *attr, char *buf)
398{
399	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
400	const char *str = NULL;
401
402	switch (led->mode) {
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;
407	}
408	return sysfs_emit(buf, "%s\n", str);
409}
410
411static ssize_t mode_store(struct device *dev,
412			  struct device_attribute *attr,
413			  const char *buf, size_t count)
414{
415	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
416	char _buf[16];
417	size_t l = min(count, sizeof(_buf) - 1);
418	enum snd_ctl_led_mode mode;
419
420	memcpy(_buf, buf, l);
421	_buf[l] = '\0';
422	if (strstr(_buf, "mute"))
423		mode = MODE_FOLLOW_MUTE;
424	else if (strstr(_buf, "route"))
425		mode = MODE_FOLLOW_ROUTE;
426	else if (strncmp(_buf, "off", 3) == 0 || strncmp(_buf, "0", 1) == 0)
427		mode = MODE_OFF;
428	else if (strncmp(_buf, "on", 2) == 0 || strncmp(_buf, "1", 1) == 0)
429		mode = MODE_ON;
430	else
431		return count;
432
433	mutex_lock(&snd_ctl_led_mutex);
434	led->mode = mode;
435	mutex_unlock(&snd_ctl_led_mutex);
436
437	snd_ctl_led_set_state(NULL, group_to_access(led->group), NULL, 0);
438	return count;
439}
440
441static ssize_t brightness_show(struct device *dev,
442			       struct device_attribute *attr, char *buf)
443{
444	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
 
445
446	return sysfs_emit(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
447}
448
449static DEVICE_ATTR_RW(mode);
450static DEVICE_ATTR_RO(brightness);
451
452static struct attribute *snd_ctl_led_dev_attrs[] = {
453	&dev_attr_mode.attr,
454	&dev_attr_brightness.attr,
455	NULL,
456};
457
458static const struct attribute_group snd_ctl_led_dev_attr_group = {
459	.attrs = snd_ctl_led_dev_attrs,
460};
461
462static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
463	&snd_ctl_led_dev_attr_group,
464	NULL,
465};
466
467static char *find_eos(char *s)
468{
469	while (*s && *s != ',')
470		s++;
471	if (*s)
472		s++;
473	return s;
474}
475
476static char *parse_uint(char *s, unsigned int *val)
477{
478	unsigned long long res;
479	if (kstrtoull(s, 10, &res))
480		res = 0;
481	*val = res;
482	return find_eos(s);
483}
484
485static char *parse_string(char *s, char *val, size_t val_size)
486{
487	if (*s == '"' || *s == '\'') {
488		char c = *s;
489		s++;
490		while (*s && *s != c) {
491			if (val_size > 1) {
492				*val++ = *s;
493				val_size--;
494			}
495			s++;
496		}
497	} else {
498		while (*s && *s != ',') {
499			if (val_size > 1) {
500				*val++ = *s;
501				val_size--;
502			}
503			s++;
504		}
505	}
506	*val = '\0';
507	if (*s)
508		s++;
509	return s;
510}
511
512static char *parse_iface(char *s, snd_ctl_elem_iface_t *val)
513{
514	if (!strncasecmp(s, "card", 4))
515		*val = SNDRV_CTL_ELEM_IFACE_CARD;
516	else if (!strncasecmp(s, "mixer", 5))
517		*val = SNDRV_CTL_ELEM_IFACE_MIXER;
518	return find_eos(s);
519}
520
521/*
522 * These types of input strings are accepted:
523 *
524 *   unsigned integer - numid (equivaled to numid=UINT)
525 *   string - basic mixer name (equivalent to iface=MIXER,name=STR)
526 *   numid=UINT
527 *   [iface=MIXER,][device=UINT,][subdevice=UINT,]name=STR[,index=UINT]
528 */
529static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, size_t count,
530			  bool attach)
531{
532	char buf2[256], *s, *os;
533	struct snd_ctl_elem_id id;
534	int err;
535
536	if (strscpy(buf2, buf, sizeof(buf2)) < 0)
537		return -E2BIG;
538	memset(&id, 0, sizeof(id));
539	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
540	s = buf2;
541	while (*s) {
542		os = s;
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) {
556			while (*s) {
557				if (*s < '0' || *s > '9')
558					break;
559				s++;
560			}
561			if (*s == '\0')
562				parse_uint(buf2, &id.numid);
563			else {
564				for (; *s >= ' '; s++);
565				*s = '\0';
566				strscpy(id.name, buf2, sizeof(id.name));
567			}
568			break;
569		}
570		if (*s == ',')
571			s++;
572		if (s == os)
573			break;
574	}
575
576	err = snd_ctl_led_set_id(led_card->number, &id, led_card->led->group, attach);
577	if (err < 0)
578		return err;
579
580	return count;
581}
582
583static ssize_t attach_store(struct device *dev,
584			    struct device_attribute *attr,
585			    const char *buf, size_t count)
586{
587	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
588	return set_led_id(led_card, buf, count, true);
589}
590
591static ssize_t detach_store(struct device *dev,
592			    struct device_attribute *attr,
593			    const char *buf, size_t count)
594{
595	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
596	return set_led_id(led_card, buf, count, false);
597}
598
599static ssize_t reset_store(struct device *dev,
600			   struct device_attribute *attr,
601			   const char *buf, size_t count)
602{
603	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
604	int err;
605
606	if (count > 0 && buf[0] == '1') {
607		err = snd_ctl_led_reset(led_card->number, led_card->led->group);
608		if (err < 0)
609			return err;
610	}
611	return count;
612}
613
614static ssize_t list_show(struct device *dev,
615			 struct device_attribute *attr, char *buf)
616{
617	struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
618	struct snd_card *card;
619	struct snd_ctl_led_ctl *lctl;
620	size_t l = 0;
621
622	card = snd_card_ref(led_card->number);
623	if (!card)
624		return -ENXIO;
625	down_read(&card->controls_rwsem);
626	mutex_lock(&snd_ctl_led_mutex);
627	if (snd_ctl_led_card_valid[led_card->number]) {
628		list_for_each_entry(lctl, &led_card->led->controls, list) {
629			if (lctl->card != card)
630				continue;
631			if (l)
632				l += sysfs_emit_at(buf, l, " ");
633			l += sysfs_emit_at(buf, l, "%u",
634					   lctl->kctl->id.numid + lctl->index_offset);
635		}
636	}
637	mutex_unlock(&snd_ctl_led_mutex);
638	up_read(&card->controls_rwsem);
639	snd_card_unref(card);
640	return l;
641}
642
643static DEVICE_ATTR_WO(attach);
644static DEVICE_ATTR_WO(detach);
645static DEVICE_ATTR_WO(reset);
646static DEVICE_ATTR_RO(list);
647
648static struct attribute *snd_ctl_led_card_attrs[] = {
649	&dev_attr_attach.attr,
650	&dev_attr_detach.attr,
651	&dev_attr_reset.attr,
652	&dev_attr_list.attr,
653	NULL,
654};
655
656static const struct attribute_group snd_ctl_led_card_attr_group = {
657	.attrs = snd_ctl_led_card_attrs,
658};
659
660static const struct attribute_group *snd_ctl_led_card_attr_groups[] = {
661	&snd_ctl_led_card_attr_group,
662	NULL,
663};
664
665static struct device snd_ctl_led_dev;
666
667static void snd_ctl_led_sysfs_add(struct snd_card *card)
668{
669	unsigned int group;
670	struct snd_ctl_led_card *led_card;
671	struct snd_ctl_led *led;
672	char link_name[32];
673
674	for (group = 0; group < MAX_LED; group++) {
675		led = &snd_ctl_leds[group];
676		led_card = kzalloc(sizeof(*led_card), GFP_KERNEL);
677		if (!led_card)
678			goto cerr2;
679		led_card->number = card->number;
680		led_card->led = led;
681		device_initialize(&led_card->dev);
682		led_card->dev.release = snd_ctl_led_card_release;
683		if (dev_set_name(&led_card->dev, "card%d", card->number) < 0)
684			goto cerr;
685		led_card->dev.parent = &led->dev;
686		led_card->dev.groups = snd_ctl_led_card_attr_groups;
687		if (device_add(&led_card->dev))
688			goto cerr;
689		led->cards[card->number] = led_card;
690		snprintf(link_name, sizeof(link_name), "led-%s", led->name);
691		WARN(sysfs_create_link(&card->ctl_dev.kobj, &led_card->dev.kobj, link_name),
692			"can't create symlink to controlC%i device\n", card->number);
693		WARN(sysfs_create_link(&led_card->dev.kobj, &card->card_dev.kobj, "card"),
694			"can't create symlink to card%i\n", card->number);
 
 
 
 
 
 
695
696		continue;
697cerr:
698		put_device(&led_card->dev);
699cerr2:
700		printk(KERN_ERR "snd_ctl_led: unable to add card%d", card->number);
701	}
702}
703
704static void snd_ctl_led_sysfs_remove(struct snd_card *card)
705{
706	unsigned int group;
707	struct snd_ctl_led_card *led_card;
708	struct snd_ctl_led *led;
709	char link_name[32];
710
711	for (group = 0; group < MAX_LED; group++) {
712		led = &snd_ctl_leds[group];
713		led_card = led->cards[card->number];
714		if (!led_card)
715			continue;
716		snprintf(link_name, sizeof(link_name), "led-%s", led->name);
717		sysfs_remove_link(&card->ctl_dev.kobj, link_name);
718		sysfs_remove_link(&led_card->dev.kobj, "card");
719		device_unregister(&led_card->dev);
720		led->cards[card->number] = NULL;
721	}
722}
723
724/*
725 * Control layer registration
726 */
727static struct snd_ctl_layer_ops snd_ctl_led_lops = {
728	.module_name = SND_CTL_LAYER_MODULE_LED,
729	.lregister = snd_ctl_led_register,
730	.ldisconnect = snd_ctl_led_disconnect,
731	.lnotify = snd_ctl_led_notify,
732};
733
734static int __init snd_ctl_led_init(void)
735{
736	struct snd_ctl_led *led;
737	unsigned int group;
738
 
 
 
739	device_initialize(&snd_ctl_led_dev);
740	snd_ctl_led_dev.class = sound_class;
741	snd_ctl_led_dev.release = snd_ctl_led_dev_release;
742	dev_set_name(&snd_ctl_led_dev, "ctl-led");
743	if (device_add(&snd_ctl_led_dev)) {
744		put_device(&snd_ctl_led_dev);
745		return -ENOMEM;
746	}
747	for (group = 0; group < MAX_LED; group++) {
748		led = &snd_ctl_leds[group];
749		INIT_LIST_HEAD(&led->controls);
750		device_initialize(&led->dev);
751		led->dev.parent = &snd_ctl_led_dev;
752		led->dev.release = snd_ctl_led_release;
753		led->dev.groups = snd_ctl_led_dev_attr_groups;
754		dev_set_name(&led->dev, led->name);
755		if (device_add(&led->dev)) {
756			put_device(&led->dev);
757			for (; group > 0; group--) {
758				led = &snd_ctl_leds[group - 1];
759				device_unregister(&led->dev);
760			}
761			device_unregister(&snd_ctl_led_dev);
762			return -ENOMEM;
763		}
764	}
765	snd_ctl_register_layer(&snd_ctl_led_lops);
766	return 0;
767}
768
769static void __exit snd_ctl_led_exit(void)
770{
771	struct snd_ctl_led *led;
772	struct snd_card *card;
773	unsigned int group, card_number;
774
775	snd_ctl_disconnect_layer(&snd_ctl_led_lops);
776	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
777		if (!snd_ctl_led_card_valid[card_number])
778			continue;
779		card = snd_card_ref(card_number);
780		if (card) {
781			snd_ctl_led_sysfs_remove(card);
782			snd_card_unref(card);
783		}
784	}
785	for (group = 0; group < MAX_LED; group++) {
786		led = &snd_ctl_leds[group];
787		device_unregister(&led->dev);
788	}
789	device_unregister(&snd_ctl_led_dev);
790	snd_ctl_led_clean(NULL);
 
 
 
791}
792
793module_init(snd_ctl_led_init)
794module_exit(snd_ctl_led_exit)