mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: jack: Expose injection debugfs at device register phase
@ 2026-10-16 14:06 Takashi Iwai
  2026-10-16 15:07 ` Cen Zhang
  0 siblings, 1 reply; 2+ messages in thread
From: Takashi Iwai @ 2026-10-16 14:06 UTC (permalink / raw)
  To: linux-sound; +Cc: linux-kernel, Cen Zhang

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: jack: Expose injection debugfs at device register phase
  2026-10-16 14:06 [PATCH] ALSA: jack: Expose injection debugfs at device register phase Takashi Iwai
@ 2026-10-16 15:07 ` Cen Zhang
  0 siblings, 0 replies; 2+ messages in thread
From: Cen Zhang @ 2026-10-16 15:07 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: linux-sound, linux-kernel

Takashi Iwai <tiwai@suse.de> 于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.
>
> 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
>

LGTM.
Reviewed-by: Cen Zhang <zzzccc427@gmail.com>

Best regards,
Cen Zhang

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-16 15:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-16 14:06 [PATCH] ALSA: jack: Expose injection debugfs at device register phase Takashi Iwai
2026-10-16 15:07 ` Cen Zhang

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®