mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Cen Zhang <zzzccc427@gmail.com>
Subject: [PATCH] ALSA: jack: Expose injection debugfs at device register phase
Date: Fri, 16 Oct 2026 16:06:09 +0200	[thread overview]
Message-ID: <20261016140653.4406-1-tiwai@suse.de> (raw)

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@suse.de>
---
 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


             reply	other threads:[~2026-10-16 14:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 14:06 Takashi Iwai [this message]
2026-10-16 15:07 ` Cen Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261016140653.4406-1-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=zzzccc427@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®