[PATCH] ALSA: jack: Expose injection debugfs at device register phase

From: Takashi Iwai

Date: Fri Oct 16 2026 - 10:07:58 EST


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.

Signed-off-by: Takashi Iwai <tiwai@xxxxxxx>
---
include/sound/jack.h | 2 +-
sound/core/jack.c | 56 ++++++++++++++++++++++++++++++++++----------
2 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/include/sound/jack.h b/include/sound/jack.h
index 715b95983188..cf19c333e2eb 100644
--- a/include/sound/jack.h
+++ b/include/sound/jack.h
@@ -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 */
diff --git a/sound/core/jack.c b/sound/core/jack.c
index d9132b885ea3..a81a64b3bfe7 100644
--- a/sound/core/jack.c
+++ b/sound/core/jack.c
@@ -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,
};

--
2.55.0