Skip to contentSkip to conversation

ALSA: control - add generic LED API

PATCH Superseded Jaroslav Kysela 2 participants 9 messages Feb 15, 2021 – Feb 21, 2021 Sound

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jaroslav Kysela

author maintainer
#0 lore raw

v2 cover ALSA: control - add generic LED API

This patchset tries to resolve the diversity in the audio LED control among the ALSA drivers. A new control layer registration is introduced which allows to run additional operations on top of the elementary ALSA sound controls.

A new control access group (three bits in the access flags) was introduced to carry the LED group information for the sound controls. The low-level sound drivers can just mark those controls using this access group. This information is exported to the user space and eventually the user space can create sound controls which can belong to a LED group.

The actual state ('route') evaluation is really easy (the minimal value check for all channels / controls / cards). If there's more complicated logic for a given hardware, the card driver may eventually export a new read-only sound control for the LED group and do the logic itself.

The new LED trigger control code is completely separated and possibly optional (there's no symbol dependency). The full code separation allows eventually to move this LED trigger control to the user space in future. Actually it replaces the already present functionality in the kernel space (HDA drivers) and allows a quick adoption for the recent hardware (SoundWire / ASoC codecs).

# lsmod | grep snd_ctl_led
snd_ctl_led            16384  0

The sound driver implementation is really easy:

1) call snd_ctl_led_request() when control LED layer should be automatically activated
/ it calls module_request("snd-ctl-led") on demand /
2) mark all related kcontrols with SNDRV_CTL_ELEM_ACCESS_SPK_LED or SNDRV_CTL_ELEM_ACCESS_MIC_LED

v2 changes:
- fix the locking - remove the controls_rwsem read lock
in the element get (the consistency is already protected
with the global snd_ctl_led_mutex and possible partial
value writes are catched with the next value change notification callback)
- rename state to brightness and show the brightness
unsigned integer value instead the text on/off string
(sync with the LED core routines)
- remove snd_ctl_led_hello() function (CI warning)
- make snd_ctl_led_get_by_access() function static (CI warning)
- move snd_ctl_layer_rwsem lock before the registraction callback call in snd_ctl_register_layer() - optimization
v1:
- https://lore.kernel.org/alsa-devel/[email protected]/
Original RFC:
- https://lore.kernel.org/alsa-devel/[email protected]/

Cc Hans de Goede <[email protected]>
Cc Perry Yuan <[email protected]>

Jaroslav Kysela (5):
ALSA: control - introduce snd_ctl_notify_one() helper
ALSA: control - add layer registration routines
ALSA: control - add generic LED trigger module as the new control
layer
ALSA: HDA - remove the custom implementation for the audio LED trigger
ALSA: control - add sysfs support to the LED trigger module

 include/sound/control.h         |  27 ++-
 include/uapi/sound/asound.h     |   6 +-
 sound/core/Kconfig              |   6 +
 sound/core/Makefile             |   2 +
 sound/core/control.c            | 173 ++++++++++++---
 sound/core/control_led.c        | 378 ++++++++++++++++++++++++++++++++
 sound/pci/hda/Kconfig           |   4 +-
 sound/pci/hda/hda_codec.c       |  69 +-----
 sound/pci/hda/hda_generic.c     | 162 +++-----------
 sound/pci/hda/hda_generic.h     |  15 +-
 sound/pci/hda/hda_local.h       |  16 +-
 sound/pci/hda/patch_ca0132.c    |   4 +-
 sound/pci/hda/patch_realtek.c   |   2 +-
 sound/pci/hda/patch_sigmatel.c  |   6 +-
 sound/pci/hda/thinkpad_helper.c |   2 +-
 15 files changed, 609 insertions(+), 263 deletions(-)
 create mode 100644 sound/core/control_led.c
Signature · 1 line
2.29.2
author maintainer
#1 lore raw

PATCH v2 1/5 ALSA: control - introduce snd_ctl_notify_one() helper

This helper is required for the following generic LED mute patch. The helper also simplifies some other functions.

---
 include/sound/control.h |  4 +--
 sound/core/control.c    | 68 +++++++++++++++++++++++++++--------------
 2 files changed, 47 insertions(+), 25 deletions(-)
include/sound/control.h +2 -2
index 77d9fa10812d..22f3d48163ff 100644
@@ -115,6 +115,7 @@ typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
 				      unsigned int cmd, unsigned long arg);
 
 void snd_ctl_notify(struct snd_card * card, unsigned int mask, struct snd_ctl_elem_id * id);
+void snd_ctl_notify_one(struct snd_card * card, unsigned int mask, struct snd_kcontrol * kctl, unsigned int ioff);
 
 struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new * kcontrolnew, void * private_data);
 void snd_ctl_free_one(struct snd_kcontrol * kcontrol);
@@ -123,8 +124,7 @@ int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);
 int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, bool add_on_replace);
 int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);
 int snd_ctl_rename_id(struct snd_card * card, struct snd_ctl_elem_id *src_id, struct snd_ctl_elem_id *dst_id);
-int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
-			int active);
+int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id, int active);
 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card * card, unsigned int numid);
 struct snd_kcontrol *snd_ctl_find_id(struct snd_card * card, struct snd_ctl_elem_id *id);
 
sound/core/control.c +45 -23

146 lines not on this page · raw message

Signature · 1 line
2.29.2
author maintainer
#2 lore raw

PATCH v2 2/5 ALSA: control - add layer registration routines

In reply to Jaroslav Kysela

The layer registration allows to handle an extra functionality on top of the control API. It can be used for the audio LED control for example.

---
 include/sound/control.h |  12 +++++
 sound/core/control.c    | 104 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 114 insertions(+), 2 deletions(-)
include/sound/control.h +12 -0
index 22f3d48163ff..175610bfa8c8 100644
@@ -108,6 +108,14 @@ struct snd_ctl_file {
 	struct list_head events;	/* waiting events for read */
 };
 
+struct snd_ctl_layer_ops {
+	struct snd_ctl_layer_ops *next;
+	const char *module_name;
+	void (*lregister)(struct snd_card *card);
+	void (*ldisconnect)(struct snd_card *card);
+	void (*lnotify)(struct snd_card *card, unsigned int mask, struct snd_kcontrol *kctl, unsigned int ioff);
+};
+
 #define snd_ctl_file(n) list_entry(n, struct snd_ctl_file, list)
 
 typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card,
@@ -140,6 +148,10 @@ int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn);
 #define snd_ctl_unregister_ioctl_compat(fcn)
 #endif
 
+int snd_ctl_request_layer(const char *module_name);
+void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops);
+void snd_ctl_disconnect_layer(struct snd_ctl_layer_ops *lops);
+
 int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);
 
 static inline unsigned int snd_ctl_get_ioffnum(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id)
sound/core/control.c +102 -2

149 lines not on this page · raw message

Signature · 1 line
2.29.2
author maintainer
#3 lore raw

PATCH v2 3/5 ALSA: control - add generic LED trigger module as the new control layer

In reply to Jaroslav Kysela

The recent laptops have usually two LEDs assigned to reflect the speaker and microphone mute state. This implementation adds a tiny layer on top of the control API which calculates the state for those LEDs using the driver callbacks.

Two new access flags are introduced to describe the controls which affects the audio path settings (an easy code change for drivers).

The LED resource can be shared with multiple sound cards with this code. The user space controls may be added to the state chain on demand, too.

This code should replace the LED code in the HDA driver and add a possibility to easy extend the other drivers (ASoC codecs etc.).

---
 include/sound/control.h     |  11 ++
 include/uapi/sound/asound.h |   6 +-
 sound/core/Kconfig          |   6 +
 sound/core/Makefile         |   2 +
 sound/core/control.c        |   1 +
 sound/core/control_led.c    | 255 ++++++++++++++++++++++++++++++++++++
 6 files changed, 280 insertions(+), 1 deletion(-)
 create mode 100644 sound/core/control_led.c
include/sound/control.h +11 -0
index 175610bfa8c8..e88f2651fee0 100644
@@ -265,6 +265,17 @@ int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
 						void *arg),
 				    void *arg);
 
+/*
+ * Control LED trigger layer
+ */
+#define SND_CTL_LAYER_MODULE_LED	"snd-ctl-led"
+
+#if IS_MODULE(CONFIG_SND_CTL_LED)
+static inline int snd_ctl_led_request(void) { return snd_ctl_request_layer(SND_CTL_LAYER_MODULE_LED); }
+#else
+static inline int snd_ctl_led_request(void) { return 0; }
+#endif
+
 /*
  * Helper functions for jack-detection controls
  */
include/uapi/sound/asound.h +5 -1
index 535a7229e1d9..a1aff0a8ec31 100644
@@ -936,7 +936,7 @@ struct snd_timer_tread {
  *                                                                          *
  ****************************************************************************/
 
-#define SNDRV_CTL_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 8)
+#define SNDRV_CTL_VERSION		SNDRV_PROTOCOL_VERSION(2, 0, 9)
 
 struct snd_ctl_card_info {
 	int card;			/* card number */
@@ -982,6 +982,10 @@ typedef int __bitwise snd_ctl_elem_iface_t;
 #define SNDRV_CTL_ELEM_ACCESS_INACTIVE		(1<<8)	/* control does actually nothing, but may be updated */
 #define SNDRV_CTL_ELEM_ACCESS_LOCK		(1<<9)	/* write lock */
 #define SNDRV_CTL_ELEM_ACCESS_OWNER		(1<<10)	/* write lock owner */
+#define SNDRV_CTL_ELEM_ACCESS_LED_SHIFT		11
+#define SNDRV_CTL_ELEM_ACCESS_LED_MASK		(7<<11)	/* three bits - LED group */
+#define SNDRV_CTL_ELEM_ACCESS_SPK_LED		(1<<11)	/* speaker (output) LED flag */
+#define SNDRV_CTL_ELEM_ACCESS_MIC_LED		(2<<11)	/* microphone (input) LED flag */
 #define SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK	(1<<28)	/* kernel use a TLV callback */
 #define SNDRV_CTL_ELEM_ACCESS_USER		(1<<29) /* user space element */
 /* bits 30 and 31 are obsoleted (for indirect access) */
sound/core/Kconfig +6 -0
index a4050f87f230..db2e3c63ff41 100644
@@ -203,4 +203,10 @@ config SND_DMA_SGBUF
 	def_bool y
 	depends on X86
 
+config SND_CTL_LED
+	tristate
+	select NEW_LEDS if SND_CTL_LED
+	select LEDS_TRIGGERS if SND_CTL_LED
+	select LEDS_TRIGGER_AUDIO if SND_CTL_LED
+
 source "sound/core/seq/Kconfig"
sound/core/Makefile +2 -0
index ee4a4a6b99ba..d774792850f3 100644
@@ -27,6 +27,7 @@ CFLAGS_pcm_native.o := -I$(src)
 
 snd-pcm-dmaengine-objs := pcm_dmaengine.o
 
+snd-ctl-led-objs  := control_led.o
 snd-rawmidi-objs  := rawmidi.o
 snd-timer-objs    := timer.o
 snd-hrtimer-objs  := hrtimer.o
@@ -37,6 +38,7 @@ snd-seq-device-objs := seq_device.o
 snd-compress-objs := compress_offload.o
 
 obj-$(CONFIG_SND) 		+= snd.o
+obj-$(CONFIG_SND_CTL_LED)	+= snd-ctl-led.o
 obj-$(CONFIG_SND_HWDEP)		+= snd-hwdep.o
 obj-$(CONFIG_SND_TIMER)		+= snd-timer.o
 obj-$(CONFIG_SND_HRTIMER)	+= snd-hrtimer.o
sound/core/control.c +1 -0
index c0a0725ff492..c9f062fada0a 100644
@@ -278,6 +278,7 @@ struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
 		   SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
 		   SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND |
 		   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK |
+		   SNDRV_CTL_ELEM_ACCESS_LED_MASK |
 		   SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK);
 
 	err = snd_ctl_new(&kctl, count, access, NULL);
sound/core/control_led.c new file +255 -0

255 lines not on this page · raw message

Signature · 1 line
2.29.2
author maintainer
#4 lore raw

PATCH v2 4/5 ALSA: HDA - remove the custom implementation for the audio LED trigger

In reply to Jaroslav Kysela

With the new snd-ctl-led module, we have a generic way to trigger audio LEDs based on the sound control changes.

Remove the custom implementation from the HDA driver.

Move the LED initialization before snd_hda_gen_parse_auto_config() call in all drivers to create marked controls there.

---
 sound/pci/hda/Kconfig           |   4 +-
 sound/pci/hda/hda_codec.c       |  69 ++------------
 sound/pci/hda/hda_generic.c     | 162 +++++---------------------------
 sound/pci/hda/hda_generic.h     |  15 +--
 sound/pci/hda/hda_local.h       |  16 +---
 sound/pci/hda/patch_ca0132.c    |   4 +-
 sound/pci/hda/patch_realtek.c   |   2 +-
 sound/pci/hda/patch_sigmatel.c  |   6 +-
 sound/pci/hda/thinkpad_helper.c |   2 +-
 9 files changed, 45 insertions(+), 235 deletions(-)
sound/pci/hda/Kconfig +1 -3
index 90759391cbac..c4360cdbc728 100644
@@ -221,10 +221,8 @@ comment "Set to Y if you want auto-loading the codec driver"
 
 config SND_HDA_GENERIC
 	tristate "Enable generic HD-audio codec parser"
-	select NEW_LEDS if SND_HDA_GENERIC_LEDS
+	select SND_CTL_LED if SND_HDA_GENERIC_LEDS
 	select LEDS_CLASS if SND_HDA_GENERIC_LEDS
-	select LEDS_TRIGGERS if SND_HDA_GENERIC_LEDS
-	select LEDS_TRIGGER_AUDIO if SND_HDA_GENERIC_LEDS
 	help
 	  Say Y or M here to enable the generic HD-audio codec parser
 	  in snd-hda-intel driver.
sound/pci/hda/hda_codec.c +6 -63

106 lines not on this page · raw message

sound/pci/hda/hda_generic.c +25 -137

237 lines not on this page · raw message

sound/pci/hda/hda_generic.h +2 -13
index 0886bc81f40b..d4dd1b8a2e7e 100644
@@ -84,15 +84,6 @@ struct badness_table {
 extern const struct badness_table hda_main_out_badness;
 extern const struct badness_table hda_extra_out_badness;
 
-struct hda_micmute_hook {
-	unsigned int led_mode;
-	unsigned int capture;
-	unsigned int led_value;
-	void (*old_hook)(struct hda_codec *codec,
-			 struct snd_kcontrol *kcontrol,
-			 struct snd_ctl_elem_value *ucontrol);
-};
-
 struct hda_gen_spec {
 	char stream_name_analog[32];	/* analog PCM stream */
 	const struct hda_pcm_stream *stream_analog_playback;
@@ -229,7 +220,8 @@ struct hda_gen_spec {
 	unsigned int inv_dmic_split:1; /* inverted dmic w/a for conexant */
 	unsigned int own_eapd_ctl:1; /* set EAPD by own function */
 	unsigned int keep_eapd_on:1; /* don't turn off EAPD automatically */
-	unsigned int vmaster_mute_enum:1; /* add vmaster mute mode enum */
+	unsigned int vmaster_mute_led:1; /* add SPK-LED flag to vmaster mute switch */
+	unsigned int mic_mute_led:1; /* add MIC-LED flag to capture mute switch */
 	unsigned int indep_hp:1; /* independent HP supported */
 	unsigned int prefer_hp_amp:1; /* enable HP amp for speaker if any */
 	unsigned int add_stereo_mix_input:2; /* add aamix as a capture src */
@@ -285,9 +277,6 @@ struct hda_gen_spec {
 			      struct snd_kcontrol *kcontrol,
 			      struct snd_ctl_elem_value *ucontrol);
 
-	/* mic mute LED hook; called via cap_sync_hook */
-	struct hda_micmute_hook micmute_led;
-
 	/* PCM hooks */
 	void (*pcm_playback_hook)(struct hda_pcm_stream *hinfo,
 				  struct hda_codec *codec,
sound/pci/hda/hda_local.h +4 -12
index 5beb8aa44ecd..06d4486a4961 100644
@@ -131,21 +131,15 @@ struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
 			  unsigned int *tlv, const char * const *followers,
 			  const char *suffix, bool init_follower_vol,
-			  struct snd_kcontrol **ctl_ret);
-#define snd_hda_add_vmaster(codec, name, tlv, followers, suffix) \
-	__snd_hda_add_vmaster(codec, name, tlv, followers, suffix, true, NULL)
+			  unsigned int access, struct snd_kcontrol **ctl_ret);
+#define snd_hda_add_vmaster(codec, name, tlv, followers, suffix, access) \
+	__snd_hda_add_vmaster(codec, name, tlv, followers, suffix, true, access, NULL)
 int snd_hda_codec_reset(struct hda_codec *codec);
 void snd_hda_codec_register(struct hda_codec *codec);
 void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec);
 
 #define snd_hda_regmap_sync(codec)	snd_hdac_regmap_sync(&(codec)->core)
 
-enum {
-	HDA_VMUTE_OFF,
-	HDA_VMUTE_ON,
-	HDA_VMUTE_FOLLOW_MASTER,
-};
-
 struct hda_vmaster_mute_hook {
 	/* below two fields must be filled by the caller of
 	 * snd_hda_add_vmaster_hook() beforehand
@@ -153,13 +147,11 @@ struct hda_vmaster_mute_hook {
 	struct snd_kcontrol *sw_kctl;
 	void (*hook)(void *, int);
 	/* below are initialized automatically */
-	unsigned int mute_mode; /* HDA_VMUTE_XXX */
 	struct hda_codec *codec;
 };
 
 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
-			     struct hda_vmaster_mute_hook *hook,
-			     bool expose_enum_ctl);
+			     struct hda_vmaster_mute_hook *hook);
 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook);
 
 /* amp value bits */
sound/pci/hda/patch_ca0132.c +2 -2
index 7e62aed172a9..bbcef0869a59 100644
@@ -7041,11 +7041,11 @@ static int ca0132_build_controls(struct hda_codec *codec)
 					spec->tlv);
 		snd_hda_add_vmaster(codec, "Master Playback Volume",
 					spec->tlv, ca0132_alt_follower_pfxs,
-					"Playback Volume");
+					"Playback Volume", 0);
 		err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
 					    NULL, ca0132_alt_follower_pfxs,
 					    "Playback Switch",
-					    true, &spec->vmaster_mute.sw_kctl);
+					    true, 0, &spec->vmaster_mute.sw_kctl);
 		if (err < 0)
 			return err;
 	}
sound/pci/hda/patch_realtek.c +1 -1
index 52907506e16e..bc1b268a5289 100644
@@ -292,7 +292,7 @@ static void alc_fixup_gpio4(struct hda_codec *codec,
 static void alc_fixup_micmute_led(struct hda_codec *codec,
 				  const struct hda_fixup *fix, int action)
 {
-	if (action == HDA_FIXUP_ACT_PROBE)
+	if (action == HDA_FIXUP_ACT_PRE_PROBE)
 		snd_hda_gen_add_micmute_led_cdev(codec, NULL);
 }
 
sound/pci/hda/patch_sigmatel.c +3 -3
index c662431bf13a..3bd592e126a3 100644
@@ -4277,6 +4277,9 @@ static int stac_parse_auto_config(struct hda_codec *codec)
 
 	spec->gen.automute_hook = stac_update_outputs;
 
+	if (spec->gpio_led)
+		snd_hda_gen_add_mute_led_cdev(codec, stac_vmaster_hook);
+
 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
 	if (err < 0)
 		return err;
@@ -4318,9 +4321,6 @@ static int stac_parse_auto_config(struct hda_codec *codec)
 	}
 #endif
 
-	if (spec->gpio_led)
-		snd_hda_gen_add_mute_led_cdev(codec, stac_vmaster_hook);
-
 	if (spec->aloopback_ctl &&
 	    snd_hda_get_bool_hint(codec, "loopback") == 1) {
 		unsigned int wr_verb =
sound/pci/hda/thinkpad_helper.c +1 -1
index 6698ae241efc..de4d8deed102 100644
@@ -18,7 +18,7 @@ static bool is_thinkpad(struct hda_codec *codec)
 static void hda_fixup_thinkpad_acpi(struct hda_codec *codec,
 				    const struct hda_fixup *fix, int action)
 {
-	if (action == HDA_FIXUP_ACT_PROBE) {
+	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
 		if (!is_thinkpad(codec))
 			return;
 		snd_hda_gen_add_mute_led_cdev(codec, NULL);
Signature · 1 line
2.29.2
author maintainer
#5 lore raw

PATCH v2 5/5 ALSA: control - add sysfs support to the LED trigger module

In reply to Jaroslav Kysela

Create SYSFS/devices/virtual/sound/ctl-led tree
(with SYSFS/class/sound/ctl-led symlink).

speaker/

    +-- mode
    +-- brightness
  mic/
    +-- mode
    +-- brightness

Copy the idea from the HDA driver and allow to set the audio LEDs based on the various modes:

- follow mute
- follow moute (inverted to follow mute)
- off
- on

Also, the actual LED state is exposed.

---
 sound/core/control_led.c | 183 ++++++++++++++++++++++++++++++++-------
 1 file changed, 153 insertions(+), 30 deletions(-)
sound/core/control_led.c +153 -30
index 47dfa48253f2..cafe4c82ca35 100644
@@ -17,7 +17,23 @@ MODULE_LICENSE("GPL");
 #define MAX_LED (((SNDRV_CTL_ELEM_ACCESS_MIC_LED - SNDRV_CTL_ELEM_ACCESS_SPK_LED) \
 			>> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) + 1)
 
+enum snd_ctl_led_mode {
+	 MODE_FOLLOW_MUTE = 0,
+	 MODE_FOLLOW_ROUTE,
+	 MODE_OFF,
+	 MODE_ON,
+};
+
 struct snd_ctl_led {
+	struct device dev;
+	struct list_head controls;
+	const char *name;
+	unsigned int group;
+	enum led_audio trigger_type;
+	enum snd_ctl_led_mode mode;
+};
+
+struct snd_ctl_led_ctl {
 	struct list_head list;
 	struct snd_card *card;
 	struct snd_kcontrol *kctl;
@@ -25,8 +41,21 @@ struct snd_ctl_led {
 };
 
 static DEFINE_MUTEX(snd_ctl_led_mutex);
-static struct list_head snd_ctl_led_controls[MAX_LED];
 static bool snd_ctl_led_card_valid[SNDRV_CARDS];
+static struct snd_ctl_led snd_ctl_leds[MAX_LED] = {
+	{
+		.name = "speaker",
+		.group = (SNDRV_CTL_ELEM_ACCESS_SPK_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
+		.trigger_type = LED_AUDIO_MUTE,
+		.mode = MODE_FOLLOW_MUTE,
+	},
+	{
+		.name = "mic",
+		.group = (SNDRV_CTL_ELEM_ACCESS_MIC_LED >> SNDRV_CTL_ELEM_ACCESS_LED_SHIFT) - 1,
+		.trigger_type = LED_AUDIO_MICMUTE,
+		.mode = MODE_FOLLOW_MUTE,
+	},
+};
 
 #define UPDATE_ROUTE(route, cb) \
 	do { \
@@ -46,15 +75,15 @@ static inline unsigned int group_to_access(unsigned int group)
 	return (group + 1) << SNDRV_CTL_ELEM_ACCESS_LED_SHIFT;
 }
 
-static struct list_head *snd_ctl_led_controls_by_access(unsigned int access)
+static struct snd_ctl_led *snd_ctl_led_get_by_access(unsigned int access)
 {
 	unsigned int group = access_to_group(access);
 	if (group >= MAX_LED)
 		return NULL;
-	return &snd_ctl_led_controls[group];
+	return &snd_ctl_leds[group];
 }
 
-static int snd_ctl_led_get(struct snd_ctl_led *lctl)
+static int snd_ctl_led_get(struct snd_ctl_led_ctl *lctl)
 {
 	struct snd_kcontrol *kctl = lctl->kctl;
 	struct snd_ctl_elem_info info;
@@ -90,22 +119,14 @@ static int snd_ctl_led_get(struct snd_ctl_led *lctl)
 static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 				  struct snd_kcontrol *kctl, unsigned int ioff)
 {
-	struct list_head *controls;
-	struct snd_ctl_led *lctl;
-	enum led_audio led_trigger_type;
+	struct snd_ctl_led *led;
+	struct snd_ctl_led_ctl *lctl;
 	int route;
 	bool found;
 
-	controls = snd_ctl_led_controls_by_access(access);
-	if (!controls)
+	led = snd_ctl_led_get_by_access(access);
+	if (!led)
 		return;
-	if (access == SNDRV_CTL_ELEM_ACCESS_SPK_LED) {
-		led_trigger_type = LED_AUDIO_MUTE;
-	} else if (access == SNDRV_CTL_ELEM_ACCESS_MIC_LED) {
-		led_trigger_type = LED_AUDIO_MICMUTE;
-	} else {
-		return;
-	}
 	route = -1;
 	found = false;
 	mutex_lock(&snd_ctl_led_mutex);
@@ -114,7 +135,7 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 		mutex_unlock(&snd_ctl_led_mutex);
 		return;
 	}
-	list_for_each_entry(lctl, controls, list) {
+	list_for_each_entry(lctl, &led->controls, list) {
 		if (lctl->kctl == kctl && lctl->index_offset == ioff)
 			found = true;
 		UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
@@ -125,27 +146,33 @@ static void snd_ctl_led_set_state(struct snd_card *card, unsigned int access,
 			lctl->card = card;
 			lctl->kctl = kctl;
 			lctl->index_offset = ioff;
-			list_add(&lctl->list, controls);
+			list_add(&lctl->list, &led->controls);
 			UPDATE_ROUTE(route, snd_ctl_led_get(lctl));
 		}
 	}
 	mutex_unlock(&snd_ctl_led_mutex);
+	switch (led->mode) {
+	case MODE_OFF:		route = 1; break;
+	case MODE_ON:		route = 0; break;
+	case MODE_FOLLOW_ROUTE:	if (route >= 0) route ^= 1; break;
+	case MODE_FOLLOW_MUTE:	/* noop */ break;
+	}
 	if (route >= 0)
-		ledtrig_audio_set(led_trigger_type, route ? LED_OFF : LED_ON);
+		ledtrig_audio_set(led->trigger_type, route ? LED_OFF : LED_ON);
 }
 
 static void snd_ctl_led_remove(struct snd_kcontrol *kctl, unsigned int ioff)
 {
 	struct snd_kcontrol_volatile *vd;
-	struct list_head *controls;
-	struct snd_ctl_led *lctl;
+	struct snd_ctl_led *led;
+	struct snd_ctl_led_ctl *lctl;
 
 	vd = &kctl->vd[ioff];
-	controls = snd_ctl_led_controls_by_access(vd->access);
-	if (!controls)
+	led = snd_ctl_led_get_by_access(vd->access);
+	if (!led)
 		return;
 	mutex_lock(&snd_ctl_led_mutex);
-	list_for_each_entry(lctl, controls, list)
+	list_for_each_entry(lctl, &led->controls, list)
 		if (lctl->kctl == kctl && lctl->index_offset == ioff) {
 			list_del(&lctl->list);
 			kfree(lctl);
@@ -182,13 +209,13 @@ static void snd_ctl_led_refresh(void)
 static void snd_ctl_led_clean(struct snd_card *card)
 {
 	unsigned int group;
-	struct list_head *controls;
-	struct snd_ctl_led *lctl;
+	struct snd_ctl_led *led;
+	struct snd_ctl_led_ctl *lctl;
 
 	for (group = 0; group < MAX_LED; group++) {
-		controls = &snd_ctl_led_controls[group];
+		led = &snd_ctl_leds[group];
 repeat:
-		list_for_each_entry(lctl, controls, list)
+		list_for_each_entry(lctl, &led->controls, list)
 			if (!card || lctl->card == card) {
 				list_del(&lctl->list);
 				kfree(lctl);
@@ -225,6 +252,82 @@ static void snd_ctl_led_disconnect(struct snd_card *card)
 	snd_ctl_led_refresh();
 }
 
+/*
+ * sysfs
+ */
+
+static ssize_t show_mode(struct device *dev,
+			 struct device_attribute *attr, char *buf)
+{
+	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
+	const char *str;
+
+	switch (led->mode) {
+	case MODE_FOLLOW_MUTE:	str = "follow-mute"; break;
+	case MODE_FOLLOW_ROUTE:	str = "follow-route"; break;
+	case MODE_ON:		str = "on"; break;
+	case MODE_OFF:		str = "off"; break;
+	}
+	return sprintf(buf, "%s\n", str);
+}
+
+static ssize_t store_mode(struct device *dev, struct device_attribute *attr,
+			  const char *buf, size_t count)
+{
+	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
+	char _buf[16];
+	size_t l = min(count, sizeof(_buf) - 1) + 1;
+	enum snd_ctl_led_mode mode;
+
+	memcpy(_buf, buf, l);
+	_buf[l] = '\0';
+	if (strstr(_buf, "mute"))
+		mode = MODE_FOLLOW_MUTE;
+	else if (strstr(_buf, "route"))
+		mode = MODE_FOLLOW_ROUTE;
+	else if (strncmp(_buf, "off", 3) == 0 || strncmp(_buf, "0", 1) == 0)
+		mode = MODE_OFF;
+	else if (strncmp(_buf, "on", 2) == 0 || strncmp(_buf, "1", 1) == 0)
+		mode = MODE_ON;
+	else
+		return count;
+
+	mutex_lock(&snd_ctl_led_mutex);
+	led->mode = mode;
+	mutex_unlock(&snd_ctl_led_mutex);
+
+	snd_ctl_led_set_state(NULL, group_to_access(led->group), NULL, 0);
+	return count;
+}
+
+static ssize_t show_brightness(struct device *dev,
+			       struct device_attribute *attr, char *buf)
+{
+	struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
+
+	return sprintf(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
+}
+
+static DEVICE_ATTR(mode, 0644, show_mode, store_mode);
+static DEVICE_ATTR(brightness, 0444, show_brightness, NULL);
+
+static struct attribute *snd_ctl_led_dev_attrs[] = {
+	&dev_attr_mode.attr,
+	&dev_attr_brightness.attr,
+	NULL,
+};
+
+static const struct attribute_group snd_ctl_led_dev_attr_group = {
+	.attrs = snd_ctl_led_dev_attrs,
+};
+
+static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
+	&snd_ctl_led_dev_attr_group,
+	NULL,
+};
+
+static struct device snd_ctl_led_dev;
+
 /*
  * Control layer registration
  */
@@ -237,16 +340,36 @@ static struct snd_ctl_layer_ops snd_ctl_led_lops = {
 
 static int __init snd_ctl_led_init(void)
 {
+	struct snd_ctl_led *led;
 	unsigned int group;
 
-	for (group = 0; group < MAX_LED; group++)
-		INIT_LIST_HEAD(&snd_ctl_led_controls[group]);
+	device_initialize(&snd_ctl_led_dev);
+	snd_ctl_led_dev.class = sound_class;
+	dev_set_name(&snd_ctl_led_dev, "ctl-led");
+	if (device_add(&snd_ctl_led_dev)) {
+		put_device(&snd_ctl_led_dev);
+		return -ENOMEM;
+	}
+	for (group = 0; group < MAX_LED; group++) {
+		led = &snd_ctl_leds[group];
+		INIT_LIST_HEAD(&led->controls);
+		device_initialize(&led->dev);
+		led->dev.parent = &snd_ctl_led_dev;
+		led->dev.groups = snd_ctl_led_dev_attr_groups;
+		dev_set_name(&led->dev, led->name);
+		if (device_add(&led->dev)) {
+			put_device(&led->dev);
+			device_del(&snd_ctl_led_dev);
+			return -ENOMEM;
+		}
+	}
 	snd_ctl_register_layer(&snd_ctl_led_lops);
 	return 0;
 }
 
 static void __exit snd_ctl_led_exit(void)
 {
+	device_del(&snd_ctl_led_dev);
 	snd_ctl_disconnect_layer(&snd_ctl_led_lops);
 	snd_ctl_led_clean(NULL);
 }
Signature · 1 line
2.29.2
#6 lore raw
In reply to Jaroslav Kysela

Hi,

Quoted 38 lines · Jaroslav Kysela

This patchset tries to resolve the diversity in the audio LED control among the ALSA drivers. A new control layer registration is introduced which allows to run additional operations on top of the elementary ALSA sound controls.

A new control access group (three bits in the access flags) was introduced to carry the LED group information for the sound controls. The low-level sound drivers can just mark those controls using this access group. This information is exported to the user space and eventually the user space can create sound controls which can belong to a LED group.

The actual state ('route') evaluation is really easy (the minimal value check for all channels / controls / cards). If there's more complicated logic for a given hardware, the card driver may eventually export a new read-only sound control for the LED group and do the logic itself.

The new LED trigger control code is completely separated and possibly optional (there's no symbol dependency). The full code separation allows eventually to move this LED trigger control to the user space in future. Actually it replaces the already present functionality in the kernel space (HDA drivers) and allows a quick adoption for the recent hardware (SoundWire / ASoC codecs).

# lsmod | grep snd_ctl_led
snd_ctl_led            16384  0

The sound driver implementation is really easy:

1) call snd_ctl_led_request() when control LED layer should be automatically activated
/ it calls module_request("snd-ctl-led") on demand /
2) mark all related kcontrols with SNDRV_CTL_ELEM_ACCESS_SPK_LED or SNDRV_CTL_ELEM_ACCESS_MIC_LED

Quoted 6 lines

v2 changes:
- fix the locking - remove the controls_rwsem read lock in the element get (the consistency is already protected with the global snd_ctl_led_mutex and possible partial value writes are catched with the next value change notification callback)

I'm afraid that lockdep still is unhappy. With v2 there is a new (different) lockdep warning.

In case you don't know, lockdep disables itself after catching the first problem, to avoid spamming the logs too much. So the previous issue was masking this one (or it could be new):

[   22.213898] ======================================================
[   22.213903] WARNING: possible circular locking dependency detected
[   22.213909] 5.11.0+ #255 Tainted: G            E
[   22.213914] ------------------------------------------------------
[   22.213918] systemd-udevd/390 is trying to acquire lock:
[   22.213923] ffff8a7f03d5a720 (&card->controls_rwsem){++++}-{3:3}, at: 0xffffffffc0861896
[   22.213948] 
               but task is already holding lock:
[   22.213952] ffffffffc07772b0 (snd_ctl_layer_rwsem){++++}-{3:3}, at: snd_ctl_register_layer+0x16b/0x360 [snd]
[   22.213977] 
               which lock already depends on the new lock.
[   22.213981] 
               the existing dependency chain (in reverse order) is:
[   22.213985] 
               -> #1 (snd_ctl_layer_rwsem){++++}-{3:3}:
[   22.213998]        down_read+0x40/0x50
[   22.214009]        snd_ctl_notify_one+0x8d/0x150 [snd]
[   22.214022]        snd_ctl_find_id+0x24e/0x350 [snd]
[   22.214034]        snd_ctl_find_id+0x2f3/0x350 [snd]
[   22.214047]        init_module+0x5bbab/0x5da91 [snd_hdmi_lpe_audio]
[   22.214057]        platform_probe+0x3f/0x90
[   22.214065]        really_probe+0xf2/0x440
[   22.214073]        driver_probe_device+0xe1/0x150
[   22.214080]        device_driver_attach+0xa8/0xb0
[   22.214087]        __driver_attach+0x8c/0x150
[   22.214095]        bus_for_each_dev+0x78/0xa0
[   22.214102]        bus_add_driver+0x12e/0x1f0
[   22.214109]        driver_register+0x8f/0xe0
[   22.214116]        do_one_initcall+0x6e/0x2e0
[   22.214125]        do_init_module+0x5c/0x260
[   22.214135]        load_module+0x2570/0x27e0
[   22.214143]        __do_sys_init_module+0x130/0x190
[   22.214151]        do_syscall_64+0x33/0x40
[   22.214158]        entry_SYSCALL_64_after_hwframe+0x44/0xa9
[   22.214166]
               -> #0 (&card->controls_rwsem){++++}-{3:3}:
[   22.214179]        __lock_acquire+0x113d/0x1e10
[   22.214187]        lock_acquire+0xe4/0x390
[   22.214194]        down_read+0x40/0x50
[   22.214200]        0xffffffffc0861896
[   22.214207]        snd_ctl_register_layer+0x183/0x360 [snd]
[   22.214220]        snd_device_register_all+0x4c/0x60 [snd]
[   22.214232]        snd_card_register+0x74/0x1d0 [snd]
[   22.214244]        init_module+0x5bccb/0x5da91 [snd_hdmi_lpe_audio]
[   22.214253]        platform_probe+0x3f/0x90
[   22.214259]        really_probe+0xf2/0x440
[   22.214266]        driver_probe_device+0xe1/0x150
[   22.214273]        device_driver_attach+0xa8/0xb0
[   22.214281]        __driver_attach+0x8c/0x150
[   22.214288]        bus_for_each_dev+0x78/0xa0
[   22.214295]        bus_add_driver+0x12e/0x1f0
[   22.214302]        driver_register+0x8f/0xe0
[   22.214309]        do_one_initcall+0x6e/0x2e0
[   22.214316]        do_init_module+0x5c/0x260
[   22.214324]        load_module+0x2570/0x27e0
[   22.214332]        __do_sys_init_module+0x130/0x190
[   22.214339]        do_syscall_64+0x33/0x40
[   22.214346]        entry_SYSCALL_64_after_hwframe+0x44/0xa9
[   22.214354] 
               other info that might help us debug this:
[   22.214357]  Possible unsafe locking scenario:
[   22.214361]        CPU0                    CPU1
[   22.214365]        ----                    ----
[   22.214368]   lock(snd_ctl_layer_rwsem);
[   22.214376]                                lock(&card->controls_rwsem);
[   22.214383]                                lock(snd_ctl_layer_rwsem);
[   22.214390]   lock(&card->controls_rwsem);
[   22.214397] 
                *** DEADLOCK ***
[   22.214401] 2 locks held by systemd-udevd/390:
[   22.214406]  #0: ffff8a7f0768f188 (&dev->mutex){....}-{3:3}, at: device_driver_attach+0x3b/0xb0
[   22.214426]  #1: ffffffffc07772b0 (snd_ctl_layer_rwsem){++++}-{3:3}, at: snd_ctl_register_layer+0x16b/0x360 [snd]
[   22.214450] 
               stack backtrace:
[   22.214455] CPU: 0 PID: 390 Comm: systemd-udevd Tainted: G            E     5.11.0+ #255
[   22.214463] Hardware name: HP HP x2 Detachable 10-p0XX/827C, BIOS F.11 09/30/2016
[   22.214468] Call Trace:
[   22.214477]  dump_stack+0x8b/0xb0
[   22.214491]  check_noncircular+0xfb/0x110
[   22.214505]  __lock_acquire+0x113d/0x1e10
[   22.214518]  lock_acquire+0xe4/0x390
[   22.214527]  ? 0xffffffffc0861896
[   22.214539]  down_read+0x40/0x50
[   22.214547]  ? 0xffffffffc0861896
[   22.214554]  0xffffffffc0861896
[   22.214562]  snd_ctl_register_layer+0x183/0x360 [snd]
[   22.214578]  snd_device_register_all+0x4c/0x60 [snd]
[   22.214593]  snd_card_register+0x74/0x1d0 [snd]
[   22.214608]  init_module+0x5bccb/0x5da91 [snd_hdmi_lpe_audio]
[   22.214624]  platform_probe+0x3f/0x90
[   22.214633]  really_probe+0xf2/0x440
[   22.214643]  driver_probe_device+0xe1/0x150
[   22.214652]  device_driver_attach+0xa8/0xb0
[   22.214662]  __driver_attach+0x8c/0x150
[   22.214670]  ? device_driver_attach+0xb0/0xb0
[   22.214678]  ? device_driver_attach+0xb0/0xb0
[   22.214687]  bus_for_each_dev+0x78/0xa0
[   22.214696]  bus_add_driver+0x12e/0x1f0
[   22.214706]  driver_register+0x8f/0xe0
[   22.214715]  ? 0xffffffffc06f5000
[   22.214721]  do_one_initcall+0x6e/0x2e0
[   22.214735]  do_init_module+0x5c/0x260
[   22.214745]  load_module+0x2570/0x27e0
[   22.214768]  ? __do_sys_init_module+0x130/0x190
[   22.214776]  __do_sys_init_module+0x130/0x190
[   22.214791]  do_syscall_64+0x33/0x40
[   22.214799]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[   22.214809] RIP: 0033:0x7fbdcf3cc6be
[   22.214817] Code: 48 8b 0d bd 27 0c 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 49 89 ca b8 af 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 8a 27
[   22.214825] RSP: 002b:00007ffea888abe8 EFLAGS: 00000246 ORIG_RAX: 00000000000000af
[   22.214835] RAX: ffffffffffffffda RBX: 000055bdd044d9f0 RCX: 00007fbdcf3cc6be
[   22.214841] RDX: 00007fbdcf50235a RSI: 000000000000b1f8 RDI: 000055bdd0c549a0
[   22.214846] RBP: 000055bdd0c549a0 R08: 000055bdd0c549a0 R09: 00007ffea8887c88
[   22.214852] R10: 000055bdd0143010 R11: 0000000000000246 R12: 00007fbdcf50235a
[   22.214857] R13: 000055bdd042af40 R14: 0000000000000007 R15: 000055bdd0430bf0

I did not get around to testing your fixup-diff before you send out v2 last time.

If you can send me another fixup-diff then I'll make sure to test this before you do a v3, so that we can be sure that all cases which my setup catches are resolved before sending out v3.

Regards,

Hans

author maintainer
#7 lore raw

Dne 21. 02. 21 v 14:14 Hans de Goede napsal(a):

Quoted 7 lines
Quoted 6 lines

v2 changes:
- fix the locking - remove the controls_rwsem read lock in the element get (the consistency is already protected with the global snd_ctl_led_mutex and possible partial value writes are catched with the next value change notification callback)

Quoted 7 lines

I'm afraid that lockdep still is unhappy. With v2 there is a new (different) lockdep warning.

If you can send me another fixup-diff then I'll make sure to test this before you do a v3, so that we can be sure that all cases which my setup catches are resolved before sending out v3.

Thank you for your test. This change (on top of v2) should resolve this remaining lockdep:

sound/core/control.c +4 -0
index c9f062fada0a..494f0154e8be 100644
@@ -2051,7 +2051,9 @@ void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
 	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
 		card = snd_card_ref(card_number);
 		if (card) {
+			down_read(&card->controls_rwsem);
 			lops->lregister(card);
+			up_read(&card->controls_rwsem);
 			snd_card_unref(card);
 		}
 	}
@@ -2113,10 +2115,12 @@ static int snd_ctl_dev_register(struct snd_device *device)
 				  &snd_ctl_f_ops, card, &card->ctl_dev);
 	if (err < 0)
 		return err;
+	down_read(&card->controls_rwsem);
 	down_read(&snd_ctl_layer_rwsem);
 	for (lops = snd_ctl_layer; lops; lops = lops->next)
 		lops->lregister(card);
 	up_read(&snd_ctl_layer_rwsem);
+	up_read(&card->controls_rwsem);
 	return 0;
 }
 
@@ -2137,10 +2141,12 @@ static int snd_ctl_dev_disconnect(struct snd_device

*device)
} read_unlock_irqrestore(&card->ctl_files_rwlock, flags);

+ down_read(&card->controls_rwsem);
down_read(&snd_ctl_layer_rwsem);
for (lops = snd_ctl_layer; lops; lops = lops->next)
lops->ldisconnect(card);
up_read(&snd_ctl_layer_rwsem);
+ up_read(&card->controls_rwsem);

return snd_unregister_device(&card->ctl_dev);
}

sound/core/control_led.c +1 -2
index cafe4c82ca35..b8bb8fd46686 100644
@@ -235,11 +235,10 @@ static void snd_ctl_led_register(struct snd_card *card)
 	mutex_lock(&snd_ctl_led_mutex);
 	snd_ctl_led_card_valid[card->number] = true;
 	mutex_unlock(&snd_ctl_led_mutex);
-	down_read(&card->controls_rwsem);
+	/* the register callback is already called with held card->controls_rwsem */
 	list_for_each_entry(kctl, &card->controls, list)
 		for (ioff = 0; ioff < kctl->count; ioff++)
 			snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
-	up_read(&card->controls_rwsem);
 	snd_ctl_led_refresh();
 }
 

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#8 lore raw

Hi,

Quoted 12 lines · Jaroslav Kysela

Dne 21. 02. 21 v 14:14 Hans de Goede napsal(a):

Quoted 9 lines
Quoted 6 lines

v2 changes:
- fix the locking - remove the controls_rwsem read lock in the element get (the consistency is already protected with the global snd_ctl_led_mutex and possible partial value writes are catched with the next value change notification callback)

I'm afraid that lockdep still is unhappy. With v2 there is a new (different) lockdep warning.

Quoted 7 lines
Quoted 4 lines

If you can send me another fixup-diff then I'll make sure to test this before you do a v3, so that we can be sure that all cases which my setup catches are resolved before sending out v3.

Thank you for your test. This change (on top of v2) should resolve this remaining lockdep:

I can confirm that I'm not seeing any more lockdeps warning after applying this on top of v2 of the series.

Regards,

Hans

Quoted 61 lines
sound/core/control.c +6 -0
index c9f062fada0a..494f0154e8be 100644
@@ -2051,7 +2051,9 @@ void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
 	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
 		card = snd_card_ref(card_number);
 		if (card) {
+			down_read(&card->controls_rwsem);
 			lops->lregister(card);
+			up_read(&card->controls_rwsem);
 			snd_card_unref(card);
 		}
 	}
@@ -2113,10 +2115,12 @@ static int snd_ctl_dev_register(struct snd_device *device)
 				  &snd_ctl_f_ops, card, &card->ctl_dev);
 	if (err < 0)
 		return err;
+	down_read(&card->controls_rwsem);
 	down_read(&snd_ctl_layer_rwsem);
 	for (lops = snd_ctl_layer; lops; lops = lops->next)
 		lops->lregister(card);
 	up_read(&snd_ctl_layer_rwsem);
+	up_read(&card->controls_rwsem);
 	return 0;
 }
 
@@ -2137,10 +2141,12 @@ static int snd_ctl_dev_disconnect(struct snd_device
 *device)
 	}
 	read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
 
+	down_read(&card->controls_rwsem);
 	down_read(&snd_ctl_layer_rwsem);
 	for (lops = snd_ctl_layer; lops; lops = lops->next)
 		lops->ldisconnect(card);
 	up_read(&snd_ctl_layer_rwsem);
+	up_read(&card->controls_rwsem);
 
 	return snd_unregister_device(&card->ctl_dev);

}

sound/core/control_led.c +1 -2
index cafe4c82ca35..b8bb8fd46686 100644
@@ -235,11 +235,10 @@ static void snd_ctl_led_register(struct snd_card *card)
 	mutex_lock(&snd_ctl_led_mutex);
 	snd_ctl_led_card_valid[card->number] = true;
 	mutex_unlock(&snd_ctl_led_mutex);
-	down_read(&card->controls_rwsem);
+	/* the register callback is already called with held card->controls_rwsem */
 	list_for_each_entry(kctl, &card->controls, list)
 		for (ioff = 0; ioff < kctl->count; ioff++)
 			snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
-	up_read(&card->controls_rwsem);
 	snd_ctl_led_refresh();
 }
 

Jaroslav