Skip to contentSkip to conversation

ALSA: jack: Expose injection debugfs at device register phase

PATCH In review Takashi Iwai 2 participants 2 messages Oct 16 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: jack: Expose injection debugfs at device register phase

Currently we add debugfs interfaces for jack injection (with CONFIG_SND_JACK_INJECTION_DEBUG) at creating jack kctls in snd_jack_kctl_add(). This means that those debugfs interfaces are already exposed to user-space at the early probe phase -- that is, it allows user-space already fiddling with it, while other components are still being initialized and hidden until snd_card_register(). Depending on the driver implementation, such behavior can be an unpleasant surprise.

For aligning the device exposure behavior, move the debugfs registration at the device register phase, too. snd_jack_kctl_add() may still add the corresponding debugfs interfaces (for the dynamically added jacks, if any), but it's done only conditionally after the card has been registered.

---
 include/sound/jack.h |  2 +-
 sound/core/jack.c    | 56 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 45 insertions(+), 13 deletions(-)
include/sound/jack.h +1 -1
index 715b95983188..cf19c333e2eb 100644
@@ -62,10 +62,10 @@ struct snd_jack {
 	struct list_head kctl_list;
 	struct snd_card *card;
 	const char *id;
+	bool registered;
 #ifdef CONFIG_SND_JACK_INPUT_DEV
 	struct input_dev *input_dev;
 	struct mutex input_dev_lock;
-	int registered;
 	int type;
 	char name[100];
 	unsigned int key[6];   /* Keep in sync with definitions above */
sound/core/jack.c +44 -12

109 lines not on this page · raw message

Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 160 lines · Takashi Iwai <[email protected]> 于2026年10月16日周五 22:06写道:

Currently we add debugfs interfaces for jack injection (with CONFIG_SND_JACK_INJECTION_DEBUG) at creating jack kctls in snd_jack_kctl_add(). This means that those debugfs interfaces are already exposed to user-space at the early probe phase -- that is, it allows user-space already fiddling with it, while other components are still being initialized and hidden until snd_card_register(). Depending on the driver implementation, such behavior can be an unpleasant surprise.

For aligning the device exposure behavior, move the debugfs registration at the device register phase, too. snd_jack_kctl_add() may still add the corresponding debugfs interfaces (for the dynamically added jacks, if any), but it's done only conditionally after the card has been registered.

---
 include/sound/jack.h |  2 +-
 sound/core/jack.c    | 56 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 45 insertions(+), 13 deletions(-)
include/sound/jack.h +1 -1
index 715b95983188..cf19c333e2eb 100644
@@ -62,10 +62,10 @@ struct snd_jack {
        struct list_head kctl_list;
        struct snd_card *card;
        const char *id;
+       bool registered;
 #ifdef CONFIG_SND_JACK_INPUT_DEV
        struct input_dev *input_dev;
        struct mutex input_dev_lock;
-       int registered;
        int type;
        char name[100];
        unsigned int key[6];   /* Keep in sync with definitions above */
sound/core/jack.c +44 -13
index d9132b885ea3..a81a64b3bfe7 100644
@@ -38,6 +38,7 @@ static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
 };
 #endif /* CONFIG_SND_JACK_INPUT_DEV */
 
+static void snd_jack_add_debugfs(struct snd_jack *jack);
 static void snd_jack_remove_debugfs(struct snd_jack *jack);
 
 static int snd_jack_dev_disconnect(struct snd_device *device)
@@ -88,11 +89,10 @@ static int snd_jack_dev_free(struct snd_device *device)
 }
 
 #ifdef CONFIG_SND_JACK_INPUT_DEV
-static int snd_jack_dev_register(struct snd_device *device)
+static int snd_jack_input_dev_register(struct snd_card *card,
+                                      struct snd_jack *jack)
 {
-       struct snd_jack *jack = device->device_data;
-       struct snd_card *card = device->card;
-       int err, i;
+       int i;
 
        snprintf(jack->name, sizeof(jack->name), "%s %s",
                 card->shortname, jack->id);
@@ -120,14 +120,32 @@ static int snd_jack_dev_register(struct snd_device *device)
                input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
        }
 
-       err = input_register_device(jack->input_dev);
-       if (err == 0)
-               jack->registered = 1;
-
-       return err;
+       return input_register_device(jack->input_dev);
+}
+#else
+static inline int snd_jack_input_dev_register(struct snd_card *card,
+                                             struct snd_jack *jack)
+{
+       return 0;
 }
 #endif /* CONFIG_SND_JACK_INPUT_DEV */
 
+static int snd_jack_dev_register(struct snd_device *device)
+{
+       struct snd_card *card = device->card;
+       struct snd_jack *jack = device->device_data;
+       int err;
+
+       err = snd_jack_input_dev_register(card, jack);
+       if (err < 0)
+               return err;
+
+       snd_jack_add_debugfs(jack);
+
+       jack->registered = true;
+       return 0;
+}
+
 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
 static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
 {
@@ -349,6 +367,9 @@ static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
        char *tname;
        int i;
 
+       if (jack_kctl->jack_debugfs_root)
+               return 0;
+
        /* Don't create injection interface for Phantom jacks */
        if (strstr(jack_kctl->kctl->id.name, "Phantom"))
                return 0;
@@ -387,6 +408,14 @@ static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
        return 0;
 }
 
+static void snd_jack_add_debugfs(struct snd_jack *jack)
+{
+       struct snd_jack_kctl *jack_kctl;
+
+       list_for_each_entry(jack_kctl, &jack->kctl_list, list)
+               snd_jack_debugfs_add_inject_node(jack, jack_kctl);
+}
+
 static void snd_jack_remove_debugfs(struct snd_jack *jack)
 {
        struct snd_jack_kctl *jack_kctl;
@@ -403,6 +432,10 @@ static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
        return 0;
 }
 
+static void snd_jack_add_debugfs(struct snd_jack *jack)
+{
+}
+
 static void snd_jack_remove_debugfs(struct snd_jack *jack)
 {
 }
@@ -423,7 +456,8 @@ static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_
 {
        jack_kctl->jack = jack;
        list_add_tail(&jack_kctl->list, &jack->kctl_list);
-       snd_jack_debugfs_add_inject_node(jack, jack_kctl);
+       if (jack->registered)
+               snd_jack_debugfs_add_inject_node(jack, jack_kctl);
 }
 
 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
@@ -504,9 +538,7 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
        int err;
        static const struct snd_device_ops ops = {
                .dev_free = snd_jack_dev_free,
-#ifdef CONFIG_SND_JACK_INPUT_DEV
                .dev_register = snd_jack_dev_register,
-#endif /* CONFIG_SND_JACK_INPUT_DEV */
                .dev_disconnect = snd_jack_dev_disconnect,
        };
 
--
Quoted 2 lines

2.55.0

LGTM.

Best regards,
Cen Zhang