Skip to contentSkip to conversation

ALSA: jack: Refactoring for jack kctls

PATCH Superseded Jie Yang 3 participants 13 messages Apr 18, 2015 – Apr 21, 2015

Subsystems (4)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jie Yang

author
#0 lore raw

v6 cover ALSA: jack: Refactoring for jack kctls

Currently only hda will create kctls for hda jacks, for asoc, people may need create jack kctls in specific driver.

Here we are introducing kctls for each jack, by creating kctls and add them to jack controls list (considering exist of combo jack). At the same time, we will report events for each control in the list.

With this new implementation, both HDA and soc jack kctls works fine, they are compatible for old HDA jack kctls, too.

For soc,
a. snd_jack_new() with NULL jack_kctl;
b. for each pin, call snd_jack_add_kctls() to add one kctl(it will call snd_ctl_add() to add kctl to card);

For HDA,
With snd_jack_new(), we can use phantom_jack = true for phantom jack creating, and the jack kctl pointer will be filled with the new created kcontrol.

Changes in v6:
1. refine kctl name generating function;
2. integrate HDA jack kctl more deeply, support phantom jack in snd_jack_new();
3. add documentation for Jack kcontrols to explain how to use it.

Changes in v5:
1. remove SND_KCTL_JACK config item, we need jack kctl once CONFIG_SND_JACK is selected.

Changes in v4:
1. use snd_ctl_find_id() to get avaliable index;
2. add initial kctl for snd_jack_new(compatible for HDA);
3. add struct snd_jack_kctl * field to struct hda_jack_tbl;
4. new kctls for soc jack during jack pins creating.
5. add a patch to remove exporting snd_kctl_jack_new().

Changes in v3:
1. replace bit index with bit mask in jack_kctl;
2. add exception for SND_JACK_HEADSET and SND_JACK_AVOUT, only create one jack kctl for these two combo jacks, respectively.
3. add NULL check, mem kfree, and fix some potential risk.

Change in v2:
1. define jack_kctl struct, and put jack kctl related stuff there;
2. add a patch to remove the existing controls for HDA jack.

Jie Yang (6):
ALSA: jack: create jack kcontrols for every jack input
ALSA: jack: extend snd_jack_new to support phantom jack
ALSA: hda - Update to use the new jack kctls method
ASoC: jack: create kctls according to jack pins info
ALSA: jack: remove exporting ctljack functions
ALSA: Docs: Add documentation for Jack kcontrols

 Documentation/sound/alsa/Jack-Controls.txt |  48 +++++++
 include/sound/jack.h                       |  21 +++-
 sound/core/Kconfig                         |   3 -
 sound/core/Makefile                        |   3 +-
 sound/core/ctljack.c                       |   2 -
 sound/core/jack.c                          | 193 +++++++++++++++++++++++++----
 sound/pci/hda/Kconfig                      |   1 -
 sound/pci/hda/hda_jack.c                   |  57 ++++-----
 sound/pci/hda/hda_jack.h                   |   4 +-
 sound/pci/oxygen/xonar_wm87x6.c            |   2 +-
 sound/soc/soc-jack.c                       |   3 +-
 11 files changed, 264 insertions(+), 73 deletions(-)
 create mode 100644 Documentation/sound/alsa/Jack-Controls.txt
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v6 1/6 ALSA: jack: create jack kcontrols for every jack input

Currently the ALSA jack core registers only input devices for each jack registered. These jack input devices are not readable by userspace devices that run as non root.

This patch adds support for additionally registering jack kcontrol devices for every input jack registered. This allows non root userspace to read jack status.

---
 include/sound/jack.h  |  17 +++++-
 sound/core/Kconfig    |   3 --
 sound/core/Makefile   |   3 +-
 sound/core/jack.c     | 141 +++++++++++++++++++++++++++++++++++++++++++++++++-
 sound/pci/hda/Kconfig |   1 -
 5 files changed, 157 insertions(+), 8 deletions(-)
include/sound/jack.h +16 -1
index 2182350..8337000 100644
@@ -73,6 +73,8 @@ enum snd_jack_types {
 
 struct snd_jack {
 	struct input_dev *input_dev;
+	struct list_head kctl_list;
+	struct snd_card *card;
 	int registered;
 	int type;
 	const char *id;
@@ -82,10 +84,19 @@ struct snd_jack {
 	void (*private_free)(struct snd_jack *);
 };
 
+struct snd_jack_kctl {
+	struct snd_kcontrol *kctl;
+	struct list_head list;		/* list of controls belong to the same jack */
+	unsigned int mask_bits;	/* one of the corresponding bits of status change will report to this kctl */
+	void *private_data;
+	void (*private_free)(struct snd_jack_kctl *jack_kctl);
+};
+
 #ifdef CONFIG_SND_JACK
 
 int snd_jack_new(struct snd_card *card, const char *id, int type,
 		 struct snd_jack **jack);
+int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name, int mask);
 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent);
 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
 		     int keytype);
@@ -93,13 +104,17 @@ int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
 void snd_jack_report(struct snd_jack *jack, int status);
 
 #else
-
 static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
 			       struct snd_jack **jack)
 {
 	return 0;
 }
 
+static inline int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name, int mask)
+{
+	return 0;
+}
+
 static inline void snd_jack_set_parent(struct snd_jack *jack,
 				       struct device *parent)
 {
sound/core/Kconfig +0 -3
index 313f22e..63cc2e9 100644
@@ -221,9 +221,6 @@ config SND_PCM_XRUN_DEBUG
 config SND_VMASTER
 	bool
 
-config SND_KCTL_JACK
-	bool
-
 config SND_DMA_SGBUF
 	def_bool y
 	depends on X86
sound/core/Makefile +1 -2
index 4daf2f5..e041dc2 100644
@@ -7,8 +7,7 @@ snd-y     := sound.o init.o memory.o info.o control.o misc.o device.o
 snd-$(CONFIG_ISA_DMA_API) += isadma.o
 snd-$(CONFIG_SND_OSSEMUL) += sound_oss.o info_oss.o
 snd-$(CONFIG_SND_VMASTER) += vmaster.o
-snd-$(CONFIG_SND_KCTL_JACK) += ctljack.o
-snd-$(CONFIG_SND_JACK)	  += jack.o
+snd-$(CONFIG_SND_JACK)	  += ctljack.o jack.o
 
 snd-pcm-y := pcm.o pcm_native.o pcm_lib.o pcm_timer.o pcm_misc.o \
 		pcm_memory.o memalloc.o
sound/core/jack.c +140 -1

184 lines not on this page · raw message

sound/pci/hda/Kconfig +0 -1
index 7f0f2c5..363f365 100644
@@ -4,7 +4,6 @@ config SND_HDA
 	tristate
 	select SND_PCM
 	select SND_VMASTER
-	select SND_KCTL_JACK
 
 config SND_HDA_INTEL
 	tristate "HD Audio PCI"
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

At Sat, 18 Apr 2015 18:04:15 +0800,

Quoted 8 lines · Jie Yang

Currently the ALSA jack core registers only input devices for each jack registered. These jack input devices are not readable by userspace devices that run as non root.

This patch adds support for additionally registering jack kcontrol devices for every input jack registered. This allows non root userspace to read jack status.

Please give more description what you really changed. It's not only doing the additional kctl creation. Also, a brief introduction about what will follow in the patchset would be nice for readers.

Quoted 27 lines
---
 include/sound/jack.h  |  17 +++++-
 sound/core/Kconfig    |   3 --
 sound/core/Makefile   |   3 +-
 sound/core/jack.c     | 141 +++++++++++++++++++++++++++++++++++++++++++++++++-
 sound/pci/hda/Kconfig |   1 -
 5 files changed, 157 insertions(+), 8 deletions(-)
include/sound/jack.h +2 -0
index 2182350..8337000 100644
@@ -73,6 +73,8 @@ enum snd_jack_types {
 
 struct snd_jack {
 	struct input_dev *input_dev;
+	struct list_head kctl_list;
+	struct snd_card *card;
 	int registered;
 	int type;
 	const char *id;

include/sound/jack.h struct snd_jack { · line 93

Quoted 10 lines
include/sound/jack.h +7 -0
@@ -82,10 +84,19 @@ struct snd_jack {
 	void (*private_free)(struct snd_jack *);
 };
 
+struct snd_jack_kctl {
+	struct snd_kcontrol *kctl;
+	struct list_head list;		/* list of controls belong to the same jack */
+	unsigned int mask_bits;	/* one of the corresponding bits of status change will report to this kctl */
+	void *private_data;
+	void (*private_free)(struct snd_jack_kctl *jack_kctl);
+};

You don't have to expose this. The hda_jack.c doesn't actually need these whole bits, as it supposes 1:1 as jack:kctl mapping. You can get rid of hda_jack->kctl since snd_jack_report() does it already, thus the private_data and the destructor can be dropped, too.

That said, the kctl management internal isn't the thing the driver needs to care.

include/sound/jack.h struct snd_jack { · line 99

Quoted 5 lines
include/sound/jack.h +1 -0
 #ifdef CONFIG_SND_JACK
 
 int snd_jack_new(struct snd_card *card, const char *id, int type,
 		 struct snd_jack **jack);
+int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name, int mask);

It adds a single kctl. Using a plural (kctl"s") is confusing.

include/sound/jack.h struct snd_jack {

Quoted 13 lines
include/sound/jack.h +13 -0
+static int snd_jack_get_available_index(struct snd_card *card, const char *name)
+{
+	struct snd_ctl_elem_id sid;
+
+	memset(&sid, 0, sizeof(sid));
+
+	sid.index = 0;
+	sid.iface = SNDRV_CTL_ELEM_IFACE_CARD;
+	strlcpy(sid.name, name, sizeof(sid.name));
+
+	while (snd_ctl_find_id(card, &sid)) {
+		sid.index++;
+	}

Drop braces for a single line loop.

include/sound/jack.h

Quoted 2 lines
include/sound/jack.h +2 -0
+	return sid.index;
+}

IMO, this function should be rather put to ctljack.c. It's kctl specific, and not really defining the jack implementation itself.

include/sound/jack.h

Quoted 6 lines
include/sound/jack.h +6 -0
+
+static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
+{
+	struct snd_jack_kctl *jack_kctl;
+
+	if (kctl) {

This NULL check is superfluous.

Quoted 13 lines
include/sound/jack.h +13 -0
+		jack_kctl = kctl->private_data;
+		if (jack_kctl) {
+			if (jack_kctl->private_free)
+				jack_kctl->private_free(jack_kctl);
+			list_del(&jack_kctl->list);
+			kfree(jack_kctl);
+		}
+	}
+
+}
+
+static void snd_jack_kctl_name_gen(char *name, const char *jack_id, int size)
+{

include/sound/jack.h

Quoted 10 lines
include/sound/jack.h +10 -0
+	size_t count = strlen(jack_id);
+
+	/* remove redundant " Jack" from jack_id */
+	if (count >= 5)
+		count = strncmp(&jack_id[count - 5], " Jack", 5) ? count : count - 5;
+
+	count = (size > count) ? count + 1 : size;
+	/* name[count] will be filled to '\0' */
+	strlcpy(name, jack_id, count);
+}

This function should be moved to ctljack.c.

That is, split the code finding a unique index and the removal of superfluous jack suffix as another patch. There fold that code into snd_kctl_jack_new() itself. At the same time, the code doing the same thing in hda_jack.c can be removed. Then the index argument of snd_kctl_jack_new() can be dropped.

As a bonus, you can save the stack size, too, by using a single string buffer for the name string. "Jack" suffix manipulation can be simplified there.

Quoted 38 lines
include/sound/jack.h +38 -0
+
+static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
+{
+	list_add_tail(&jack_kctl->list, &jack->kctl_list);
+}
+
+/**
+ * snd_jack_kctl_new - Create a new snd_jack_kctl and return it
+ * @card:  the card instance
+ * @kctl_name:  the name for the snd_kcontrol object
+ * @mask:  a bitmask of enum snd_jack_type values that can be detected
+ *         by this snd_jack_kctl object.
+ *
+ * Creates a new snd_kcontrol object, and assign it to the new created
+ *	     snd_jack_kctl object.
+ *
+ * Return: The new created snd_jack_kctl object, or NULL if failed.
+ */
+static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
+{
+	struct snd_kcontrol *kctl;
+	struct snd_jack_kctl *jack_kctl;
+	int index, err;
+	char jack_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
+
+	index = snd_jack_get_available_index(card, name);
+	snd_jack_kctl_name_gen(jack_name, name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+	kctl = snd_kctl_jack_new(jack_name, index, card);
+	if (!kctl)
+		return NULL;
+
+	jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
+
+	if (!jack_kctl)
+		goto error;
+
+	jack_kctl->kctl = kctl;
+	jack_kctl->mask_bits = mask;

include/sound/jack.h

Quoted 10 lines
include/sound/jack.h +10 -0
+
+	kctl->private_data = jack_kctl;
+	kctl->private_free = snd_jack_kctl_private_free;
+
+	err = snd_ctl_add(card, jack_kctl->kctl);
+	if (err < 0)
+		goto error;
+	return jack_kctl;
+error:
+	snd_ctl_free_one(kctl);

This will be a double-free. snd_ctl_add() itself frees the object at error path.

Takashi

author
#3 lore raw

Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

Quoted 16 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Sunday, April 19, 2015 4:26 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; Girdwood, Liam R; [email protected]; Liam Girdwood
Subject: Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

At Sat, 18 Apr 2015 18:04:15 +0800,

Quoted 5 lines · Jie Yang

Currently the ALSA jack core registers only input devices for each jack registered. These jack input devices are not readable by userspace devices that run as non root.

Quoted 7 lines
Quoted 3 lines

This patch adds support for additionally registering jack kcontrol devices for every input jack registered. This allows non root userspace to read jack status.

Please give more description what you really changed. It's not only doing the additional kctl creation. Also, a brief introduction about what will follow in the patchset would be nice for readers.

Ah, forgot to update this as it has been change a lot from the v1, will do that.

Quoted 37 lines
Quoted 11 lines
---
 include/sound/jack.h  |  17 +++++-
 sound/core/Kconfig    |   3 --
 sound/core/Makefile   |   3 +-
 sound/core/jack.c     | 141

+++++++++++++++++++++++++++++++++++++++++++++++++-

Quoted 17 lines
 sound/pci/hda/Kconfig |   1 -
 5 files changed, 157 insertions(+), 8 deletions(-)
include/sound/jack.h index renamed from include/sound/jack.h +0 -0

2182350..8337000 100644

include/sound/jack.h +2 -0
@@ -73,6 +73,8 @@ enum snd_jack_types {
 
 struct snd_jack {
 	struct input_dev *input_dev;
+	struct list_head kctl_list;
+	struct snd_card *card;
 	int registered;
 	int type;
 	const char *id;

include/sound/jack.h struct snd_jack { · line 88

Quoted 5 lines
include/sound/jack.h +3 -0
@@ -82,10 +84,19 @@ struct snd_jack {
 	void (*private_free)(struct snd_jack *);  };
 
+struct snd_jack_kctl {
+	struct snd_kcontrol *kctl;
+	struct list_head list;		/* list of controls belong to the same

jack */

include/sound/jack.h struct snd_jack { · line 89

Quoted 1 line
include/sound/jack.h +1 -0
+	unsigned int mask_bits;	/* one of the corresponding bits of

status change will report to this kctl */

Quoted 7 lines

include/sound/jack.h struct snd_jack { · line 91

Quoted 2 lines
include/sound/jack.h +2 -0
+	void *private_data;
+	void (*private_free)(struct snd_jack_kctl *jack_kctl); };

You don't have to expose this. The hda_jack.c doesn't actually need these whole bits, as it supposes 1:1 as jack:kctl mapping. You can get rid of hda_jack->kctl since snd_jack_report() does it already, thus the private_data and the destructor can be dropped, too.

The patch 3/6 removes had_jack->kctl, here I added priate destructor for HDA jack to reset hda_jack_tbl jacks->nid and jacks->jack to 0. Seems these are needed for HDA jack logic, otherwise, I can already dropped them. :(

Quoted 5 lines

That said, the kctl management internal isn't the thing the driver needs to care.

Quoted 8 lines

include/sound/jack.h struct snd_jack {

Quoted 6 lines
include/sound/jack.h +2 -0
 #ifdef CONFIG_SND_JACK
 
 int snd_jack_new(struct snd_card *card, const char *id, int type,
 		 struct snd_jack **jack);
+int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name,
+int mask);

It adds a single kctl. Using a plural (kctl"s") is confusing.

OK, will change to 'kctl'.

Quoted 17 lines
Quoted 5 lines
include/sound/jack.h +3 -0
+static int snd_jack_get_available_index(struct snd_card *card, const
+char *name) {
+	struct snd_ctl_elem_id sid;

include/sound/jack.h struct snd_jack {

Quoted 10 lines
include/sound/jack.h +10 -0
+
+	memset(&sid, 0, sizeof(sid));
+
+	sid.index = 0;
+	sid.iface = SNDRV_CTL_ELEM_IFACE_CARD;
+	strlcpy(sid.name, name, sizeof(sid.name));
+
+	while (snd_ctl_find_id(card, &sid)) {
+		sid.index++;
+	}

Drop braces for a single line loop.

OK.

Quoted 6 lines

include/sound/jack.h

Quoted 3 lines
include/sound/jack.h +2 -0
+	return sid.index;
+}

IMO, this function should be rather put to ctljack.c. It's kctl specific, and not really defining the jack implementation itself.

Good idea, will consider this in next version.

Quoted 9 lines

include/sound/jack.h

Quoted 7 lines
include/sound/jack.h +5 -0
+
+static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl) {
+	struct snd_jack_kctl *jack_kctl;
+
+	if (kctl) {

This NULL check is superfluous.

OK, will remove it.

Quoted 29 lines
Quoted 9 lines
include/sound/jack.h +8 -0
+		jack_kctl = kctl->private_data;
+		if (jack_kctl) {
+			if (jack_kctl->private_free)
+				jack_kctl->private_free(jack_kctl);
+			list_del(&jack_kctl->list);
+			kfree(jack_kctl);
+		}
+	}

include/sound/jack.h

Quoted 10 lines
include/sound/jack.h +10 -0
+
+}
+
+static void snd_jack_kctl_name_gen(char *name, const char *jack_id,
+int size) {
+	size_t count = strlen(jack_id);
+
+	/* remove redundant " Jack" from jack_id */
+	if (count >= 5)
+		count = strncmp(&jack_id[count - 5], " Jack", 5) ? count :

count -

include/sound/jack.h

Quoted 6 lines

+5;

include/sound/jack.h +5 -0
+
+	count = (size > count) ? count + 1 : size;
+	/* name[count] will be filled to '\0' */
+	strlcpy(name, jack_id, count);
+}

This function should be moved to ctljack.c.

Quoted 2 lines

That is, split the code finding a unique index and the removal of superfluous jack suffix as another patch. There fold that code into

Agree, will implement this in next version.

Quoted 2 lines

snd_kctl_jack_new() itself. At the same time, the code doing the same thing in hda_jack.c can be removed. Then the index argument of

A little concern that there may be a little difference here:
hda_jack.c:get_unique_index() check the codec kctl list,
but in my patch we check card kctl list, I am afraid that it may influence the exist HDA usage, wo just keep what HDA did before for HDA jack.

Takashi, do you think it's a risk to change from codec kctl list to card kctl list for HDA part?

Quoted 4 lines

snd_kctl_jack_new() can be dropped.

As a bonus, you can save the stack size, too, by using a single string buffer for the name string. "Jack" suffix manipulation can be simplified there.

Agree.

Quoted 43 lines
Quoted 17 lines
include/sound/jack.h +16 -0
+
+static void snd_jack_kctl_add(struct snd_jack *jack, struct
+snd_jack_kctl *jack_kctl) {
+	list_add_tail(&jack_kctl->list, &jack->kctl_list); }
+
+/**
+ * snd_jack_kctl_new - Create a new snd_jack_kctl and return it
+ * @card:  the card instance
+ * @kctl_name:  the name for the snd_kcontrol object
+ * @mask:  a bitmask of enum snd_jack_type values that can be detected
+ *         by this snd_jack_kctl object.
+ *
+ * Creates a new snd_kcontrol object, and assign it to the new created
+ *	     snd_jack_kctl object.
+ *
+ * Return: The new created snd_jack_kctl object, or NULL if failed.

include/sound/jack.h

Quoted 10 lines
include/sound/jack.h +10 -0
+ */
+static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card
+*card, const char *name, unsigned int mask) {
+	struct snd_kcontrol *kctl;
+	struct snd_jack_kctl *jack_kctl;
+	int index, err;
+	char jack_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
+
+	index = snd_jack_get_available_index(card, name);
+	snd_jack_kctl_name_gen(jack_name, name,

SNDRV_CTL_ELEM_ID_NAME_MAXLEN);

Quoted 15 lines
include/sound/jack.h +15 -0
+	kctl = snd_kctl_jack_new(jack_name, index, card);
+	if (!kctl)
+		return NULL;
+
+	jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
+
+	if (!jack_kctl)
+		goto error;
+
+	jack_kctl->kctl = kctl;
+	jack_kctl->mask_bits = mask;
+
+	kctl->private_data = jack_kctl;
+	kctl->private_free = snd_jack_kctl_private_free;
+
Quoted 8 lines

include/sound/jack.h

Quoted 6 lines
include/sound/jack.h +6 -0
+	err = snd_ctl_add(card, jack_kctl->kctl);
+	if (err < 0)
+		goto error;
+	return jack_kctl;
+error:
+	snd_ctl_free_one(kctl);

This will be a double-free. snd_ctl_add() itself frees the object at error path.

Thanks for reminding. Snd_ctl_free_one() for the first goto is needed, I will remove it for the second goto.

Quoted 3 lines

Takashi

#4 lore raw

Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

At Mon, 20 Apr 2015 07:52:20 +0000,

Quoted 18 lines · Yang
Quoted 10 lines

include/sound/jack.h struct snd_jack { · line 88

Quoted 7 lines
include/sound/jack.h +3 -0
@@ -82,10 +84,19 @@ struct snd_jack {
 	void (*private_free)(struct snd_jack *);  };
 
+struct snd_jack_kctl {
+	struct snd_kcontrol *kctl;
+	struct list_head list;		/* list of controls belong to the same

jack */

Review struct snd_jack { · line 89

Quoted 1 line
(quoted diff) +1 -0
+	unsigned int mask_bits;	/* one of the corresponding bits of

status change will report to this kctl */

Quoted 7 lines

Review struct snd_jack { · line 91

Quoted 2 lines
(quoted diff) +2 -0
+	void *private_data;
+	void (*private_free)(struct snd_jack_kctl *jack_kctl); };

You don't have to expose this. The hda_jack.c doesn't actually need these whole bits, as it supposes 1:1 as jack:kctl mapping. You can get rid of hda_jack->kctl since snd_jack_report() does it already, thus the private_data and the destructor can be dropped, too.

Quoted 4 lines

The patch 3/6 removes had_jack->kctl, here I added priate destructor for HDA jack to reset hda_jack_tbl jacks->nid and jacks->jack to 0. Seems these are needed for HDA jack logic, otherwise, I can already dropped them. :(

It's not needed, as far as I understand your code.

Try to look from from the top-level POV: the hda driver just needs to register a jack object via snd_jack_new(), reports it, and remove it in destructor. The private_data was needed for tracking the codec-local kctls, but now they are managed in jack object itself, thus we don't have to keep eyes on it. The private_free is for removing the kctl list, and since this is gone, we don't have to take it, either. In other words, we don't have to take care of kctls at all in hda driver side. This would be the ideal situation.

(snip)

Quoted 10 lines
Quoted 2 lines

snd_kctl_jack_new() itself. At the same time, the code doing the same thing in hda_jack.c can be removed. Then the index argument of

A little concern that there may be a little difference here:
hda_jack.c:get_unique_index() check the codec kctl list,
but in my patch we check card kctl list, I am afraid that it may influence the exist HDA usage, wo just keep what HDA did before for HDA jack.

Takashi, do you think it's a risk to change from codec kctl list to card kctl list for HDA part?

No, there shouldn't. The id must be unique for the whole card, not specific to codecs. That is, the code looking through snd_ctl_find_id() is even safer than the current version.

thanks,

Takashi

author
#5 lore raw

Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

Quoted 36 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Monday, April 20, 2015 4:06 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; Girdwood, Liam R; [email protected]; Liam Girdwood
Subject: Re: [PATCH v6 1/6] ALSA: jack: create jack kcontrols for every jack input

At Mon, 20 Apr 2015 07:52:20 +0000,

Quoted 18 lines · Yang
Quoted 10 lines

include/sound/jack.h struct snd_jack { · line 88

Quoted 7 lines
include/sound/jack.h +3 -0
@@ -82,10 +84,19 @@ struct snd_jack {
 	void (*private_free)(struct snd_jack *);  };
 
+struct snd_jack_kctl {
+	struct snd_kcontrol *kctl;
+	struct list_head list;		/* list of controls belong to the same

jack */

Review struct snd_jack { · line 89

Quoted 1 line
(quoted diff) +1 -0
+	unsigned int mask_bits;	/* one of the corresponding bits of

status change will report to this kctl */

Quoted 7 lines

Review struct snd_jack { · line 91

Quoted 2 lines
(quoted diff) +2 -0
+	void *private_data;
+	void (*private_free)(struct snd_jack_kctl *jack_kctl); };

You don't have to expose this. The hda_jack.c doesn't actually need these whole bits, as it supposes 1:1 as jack:kctl mapping. You can get rid of hda_jack->kctl since snd_jack_report() does it already, thus the private_data and the destructor can be dropped, too.

Quoted 4 lines

The patch 3/6 removes had_jack->kctl, here I added priate destructor for HDA jack to reset hda_jack_tbl jacks->nid and jacks->jack to 0. Seems these are needed for HDA jack logic, otherwise, I can already dropped them. :(

It's not needed, as far as I understand your code.

Quoted 7 lines

Try to look from from the top-level POV: the hda driver just needs to register a jack object via snd_jack_new(), reports it, and remove it in destructor. The private_data was needed for tracking the codec-local kctls, but now they are managed in jack object itself, thus we don't have to keep eyes on it. The private_free is for removing the kctl list, and since this is gone, we don't have to take it, either. In other words, we don't have to take care of kctls at all in hda driver side. This would be the ideal situation.

Thanks for explaining it. Finally I make clear that jack private_free will call hda_free_jack_priv() to reset jacks->nid/jack, so I have no worry now. :)

Quoted 10 lines

(snip)

Quoted 8 lines
Quoted 2 lines

snd_kctl_jack_new() itself. At the same time, the code doing the same thing in hda_jack.c can be removed. Then the index argument of

A little concern that there may be a little difference here:
hda_jack.c:get_unique_index() check the codec kctl list, but in my patch we check card kctl list, I am afraid that it may influence the exist HDA usage, wo just keep what HDA did before for HDA jack.

Quoted 6 lines
Quoted 2 lines

Takashi, do you think it's a risk to change from codec kctl list to card kctl list for HDA part?

No, there shouldn't. The id must be unique for the whole card, not specific to codecs. That is, the code looking through
snd_ctl_find_id() is even safer than the current version.

OK, then I feel reassured to replace it. :)

Quoted 5 lines

thanks,

Takashi

author
#6 lore raw

PATCH v6 2/6 ALSA: jack: extend snd_jack_new to support phantom jack

In reply to Jie Yang

For phantom jack, we won't create real jack device, but jack kctl may be needed.

Here, we extend snd_jack_new() to support phantom jack creating:
pass in a bool param for [non-]phantom flag, and a non-Null param (struct snd_jack_kctl **) to indicate that we need create kctl at this jack creating stage.

We can also add kctl to a jack after the jack is created.

This make the integrating the existing HDA jack kctl and soc jack kctl possible.

---
 include/sound/jack.h            |  4 +--
 sound/core/jack.c               | 56 +++++++++++++++++++++++++----------------
 sound/pci/hda/hda_jack.c        |  2 +-
 sound/pci/oxygen/xonar_wm87x6.c |  2 +-
 sound/soc/soc-jack.c            |  2 +-
 5 files changed, 39 insertions(+), 27 deletions(-)
include/sound/jack.h +2 -2
index 8337000..11d772f 100644
@@ -95,7 +95,7 @@ struct snd_jack_kctl {
 #ifdef CONFIG_SND_JACK
 
 int snd_jack_new(struct snd_card *card, const char *id, int type,
-		 struct snd_jack **jack);
+		 struct snd_jack **jack, bool phantom_jack, struct snd_jack_kctl **jjack_kctl);
 int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name, int mask);
 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent);
 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
@@ -105,7 +105,7 @@ void snd_jack_report(struct snd_jack *jack, int status);
 
 #else
 static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
-			       struct snd_jack **jack)
+			       struct snd_jack **jack, bool phantom_jack, struct snd_jack_kctl **jjack_kctl)
 {
 	return 0;
 }
sound/core/jack.c +34 -22

82 lines not on this page · raw message

sound/pci/hda/hda_jack.c +1 -1
index e664307..2136670 100644
@@ -417,7 +417,7 @@ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
 	if (!phantom_jack) {
 		jack->type = get_input_jack_type(codec, nid);
 		err = snd_jack_new(codec->bus->card, name, jack->type,
-				   &jack->jack);
+				   &jack->jack, false, NULL);
 		if (err < 0)
 			return err;
 		jack->jack->private_data = jack;
sound/pci/oxygen/xonar_wm87x6.c +1 -1
index 6ce6860..a37450b 100644
@@ -286,7 +286,7 @@ static void xonar_ds_init(struct oxygen *chip)
 	xonar_enable_output(chip);
 
 	snd_jack_new(chip->card, "Headphone",
-		     SND_JACK_HEADPHONE, &data->hp_jack);
+		     SND_JACK_HEADPHONE, &data->hp_jack, false, NULL);
 	xonar_ds_handle_hp_jack(chip);
 
 	snd_component_add(chip->card, "WM8776");
sound/soc/soc-jack.c +1 -1
index 9f60c25..836368b 100644
@@ -48,7 +48,7 @@ int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
 	INIT_LIST_HEAD(&jack->jack_zones);
 	BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
 
-	ret = snd_jack_new(card->snd_card, id, type, &jack->jack);
+	ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, NULL);
 	if (ret)
 		return ret;
 
Signature · 1 line
1.9.1
author
#7 lore raw

PATCH v6 3/6 ALSA: hda - Update to use the new jack kctls method

In reply to Jie Yang

In the new jack kctls design, snd_kcontrol can be created during snd_jack_new(), or calling snd_jack_add_new_kctls() to create and attach to the created jack later.

Here we create jack kctls at the jack creating stage, for both phantom/non-phantom jack.

---
 sound/pci/hda/hda_jack.c | 57 ++++++++++++++++++------------------------------
 sound/pci/hda/hda_jack.h |  4 +---
 2 files changed, 22 insertions(+), 39 deletions(-)
sound/pci/hda/hda_jack.c +21 -36
index 2136670..52f4a4b 100644
@@ -132,11 +132,11 @@ void snd_hda_jack_tbl_clear(struct hda_codec *codec)
 
 	for (i = 0; i < codec->jacktbl.used; i++, jack++) {
 		struct hda_jack_callback *cb, *next;
-#ifdef CONFIG_SND_HDA_INPUT_JACK
+
 		/* free jack instances manually when clearing/reconfiguring */
 		if (!codec->bus->shutdown && jack->jack)
 			snd_device_free(codec->bus->card, jack->jack);
-#endif
+
 		for (cb = jack->callback; cb; cb = next) {
 			next = cb->next;
 			kfree(cb);
@@ -337,20 +337,16 @@ void snd_hda_jack_report_sync(struct hda_codec *codec)
 	jack = codec->jacktbl.list;
 	for (i = 0; i < codec->jacktbl.used; i++, jack++)
 		if (jack->nid) {
-			if (!jack->kctl || jack->block_report)
+			if (!jack->jack_kctl || jack->block_report)
 				continue;
 			state = get_jack_plug_state(jack->pin_sense);
-			snd_kctl_jack_report(codec->bus->card, jack->kctl, state);
-#ifdef CONFIG_SND_HDA_INPUT_JACK
 			if (jack->jack)
 				snd_jack_report(jack->jack,
 						state ? jack->type : 0);
-#endif
 		}
 }
 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
 
-#ifdef CONFIG_SND_HDA_INPUT_JACK
 /* guess the jack type from the pin-config */
 static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
 {
@@ -371,13 +367,12 @@ static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
 	}
 }
 
-static void hda_free_jack_priv(struct snd_jack *jack)
+static void hda_free_jack_priv(struct snd_jack_kctl *jack_kctl)
 {
-	struct hda_jack_tbl *jacks = jack->private_data;
+	struct hda_jack_tbl *jacks = jack_kctl->private_data;
 	jacks->nid = 0;
 	jacks->jack = NULL;
 }
-#endif
 
 /**
  * snd_hda_jack_add_kctl - Add a kctl for the given pin
@@ -394,37 +389,27 @@ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
 			  const char *name, int idx, bool phantom_jack)
 {
 	struct hda_jack_tbl *jack;
-	struct snd_kcontrol *kctl;
-	int err, state;
+	int err, state, type;
 
 	jack = snd_hda_jack_tbl_new(codec, nid);
 	if (!jack)
 		return 0;
-	if (jack->kctl)
+	if (jack->jack_kctl)
 		return 0; /* already created */
-	kctl = snd_kctl_jack_new(name, idx, codec);
-	if (!kctl)
-		return -ENOMEM;
-	err = snd_hda_ctl_add(codec, nid, kctl);
+
+	type = get_input_jack_type(codec, nid);
+	err = snd_jack_new(codec->bus->card, name, type,
+			   &jack->jack, phantom_jack, &jack->jack_kctl);
 	if (err < 0)
 		return err;
-	jack->kctl = kctl;
-	jack->phantom_jack = !!phantom_jack;
 
+	jack->phantom_jack = !!phantom_jack;
+	jack->type = type;
+	jack->jack_kctl->private_data = jack;
+	jack->jack_kctl->private_free = hda_free_jack_priv;
 	state = snd_hda_jack_detect(codec, nid);
-	snd_kctl_jack_report(codec->bus->card, kctl, state);
-#ifdef CONFIG_SND_HDA_INPUT_JACK
-	if (!phantom_jack) {
-		jack->type = get_input_jack_type(codec, nid);
-		err = snd_jack_new(codec->bus->card, name, jack->type,
-				   &jack->jack, false, NULL);
-		if (err < 0)
-			return err;
-		jack->jack->private_data = jack;
-		jack->jack->private_free = hda_free_jack_priv;
-		snd_jack_report(jack->jack, state ? jack->type : 0);
-	}
-#endif
+	snd_jack_report(jack->jack, state ? jack->type : 0);
+
 	return 0;
 }
 
@@ -453,10 +438,10 @@ static int get_unique_index(struct hda_codec *codec, const char *name, int idx)
 	jack = codec->jacktbl.list;
 	for (i = 0; i < codec->jacktbl.used; i++, jack++) {
 		/* jack->kctl.id contains "XXX Jack" name string with index */
-		if (jack->kctl &&
-		    !strncmp(name, jack->kctl->id.name, len) &&
-		    !strcmp(" Jack", jack->kctl->id.name + len) &&
-		    jack->kctl->id.index == idx) {
+		if (jack->jack_kctl &&
+		    !strncmp(name, jack->jack_kctl->kctl->id.name, len) &&
+		    !strcmp(" Jack", jack->jack_kctl->kctl->id.name + len) &&
+		    jack->jack_kctl->kctl->id.index == idx) {
 			idx++;
 			goto again;
 		}
sound/pci/hda/hda_jack.h +1 -3
index b279e32..346a639 100644
@@ -39,11 +39,9 @@ struct hda_jack_tbl {
 	unsigned int block_report:1;    /* in a transitional state - do not report to userspace */
 	hda_nid_t gating_jack;		/* valid when gating jack plugged */
 	hda_nid_t gated_jack;		/* gated is dependent on this jack */
-	struct snd_kcontrol *kctl;	/* assigned kctl for jack-detection */
-#ifdef CONFIG_SND_HDA_INPUT_JACK
 	int type;
+	struct snd_jack_kctl *jack_kctl;
 	struct snd_jack *jack;
-#endif
 };
 
 struct hda_jack_tbl *
Signature · 1 line
1.9.1
author
#8 lore raw

PATCH v6 4/6 ASoC: jack: create kctls according to jack pins info

In reply to Jie Yang

In the new jack kctls design, we can create kctls according to pins info:

1. during jack creating, snd_jack_new() with NULL jack_kctl;
2. for each pin, call snd_jack_add_kctls() to add one kctl (it will call snd_ctl_add() to add kctl to card);

---
 sound/soc/soc-jack.c | 1 +
 1 file changed, 1 insertion(+)
sound/soc/soc-jack.c +1 -0
index 836368b..44b3c8c 100644
@@ -197,6 +197,7 @@ int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
 
 		INIT_LIST_HEAD(&pins[i].list);
 		list_add(&(pins[i].list), &jack->pins);
+		snd_jack_add_new_kctls(jack->jack, pins[i].pin, pins[i].mask);
 	}
 
 	/* Update to reflect the last reported status; canned jack
Signature · 1 line
1.9.1
maintainer
#9 lore raw

Re: [PATCH v6 4/6] ASoC: jack: create kctls according to jack pins info

Quoted 6 lines · Jie Yang

In the new jack kctls design, we can create kctls according to pins info:

1. during jack creating, snd_jack_new() with NULL jack_kctl;
2. for each pin, call snd_jack_add_kctls() to add one kctl (it will call snd_ctl_add() to add kctl to card);

I'm a little confused why this isn't done by the jack core?

author
#10 lore raw

Re: [PATCH v6 4/6] ASoC: jack: create kctls according to jack pins info

Quoted 9 lines

-----Original Message-----
From: Mark Brown [mailto:[email protected]]
Sent: Tuesday, April 21, 2015 5:21 AM
To: Jie, Yang
Cc: [email protected]; [email protected]; Girdwood, Liam R; [email protected]
Subject: Re: [PATCH v6 4/6] ASoC: jack: create kctls according to jack pins info

On Sat, Apr 18, 2015 at 06:04:18PM +0800, Jie Yang wrote:

Quoted 8 lines
Quoted 6 lines

In the new jack kctls design, we can create kctls according to pins info:

1. during jack creating, snd_jack_new() with NULL jack_kctl; 2. for each pin, call snd_jack_add_kctls() to add one kctl (it will call snd_ctl_add() to add kctl to card);

I'm a little confused why this isn't done by the jack core?

The kctl creating and adding to jack is actually done by the jack core(inside snd_jack_add_kctls()).

Mark, do you mean that you prefer doing this inside snd_jack_new()?

For we may need create multiple kctls for a jack, and I found we can reuse the pins' info for those kctls' creating.

For example, in broadwell.c, the jack broadwell_headset have 2 pins:
"Mic Jack" and "Headphone Jack", then 2 kctls with same names will be created, and they are visible for PA, can be switched on/off by PA separately.

~Keyon

author
#11 lore raw

PATCH v6 5/6 ALSA: jack: remove exporting ctljack functions

In reply to Jie Yang

After created jack embedded kctls and removed hda jack kctls, the funcs snd_kctl_jack_new() and snd_kctl_jack_report() are merely internal called, so here remove exporting them.

---
 sound/core/ctljack.c | 2 --
 1 file changed, 2 deletions(-)
sound/core/ctljack.c +0 -2
index e4b38fb..7a271f5 100644
@@ -43,7 +43,6 @@ snd_kctl_jack_new(const char *name, int idx, void *private_data)
 	kctl->private_value = 0;
 	return kctl;
 }
-EXPORT_SYMBOL_GPL(snd_kctl_jack_new);
 
 void snd_kctl_jack_report(struct snd_card *card,
 			  struct snd_kcontrol *kctl, bool status)
@@ -53,4 +52,3 @@ void snd_kctl_jack_report(struct snd_card *card,
 	kctl->private_value = status;
 	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
 }
-EXPORT_SYMBOL_GPL(snd_kctl_jack_report);
Signature · 1 line
1.9.1
author
#12 lore raw

PATCH v6 6/6 ALSA: Docs: Add documentation for Jack kcontrols

In reply to Jie Yang

Add documentation describing Jack embedded kcontrols, and how to use it on HD-Audio and ASoC case.

---
 Documentation/sound/alsa/Jack-Controls.txt | 48 ++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 Documentation/sound/alsa/Jack-Controls.txt
Documentation/sound/alsa/Jack-Controls.txt new file +48 -0
new file mode 100644
index 0000000..9e93947
@@ -0,0 +1,48 @@
+Why we need kcontrols for Jack?
+===============================
+
+ALSA using kcontrols to export controlling(switch, volume, Mux, ...) to
+user space, e.g. pulseaudio can switch off headphone and switch on
+speaker when no heaphone is pluged in.
+
+For each physical jack, there may be dynamic pluged in/out status which
+upper layer may be interested in. Currently the ALSA jack core registers
+only input devices for each jack registered. These jack input devices
+are not readable by userspace devices that run as non root.
+
+So we create embeded kcontrols for each jack, report jack event via
+these kcontrols, userspace switch them to ack on these event, to
+implement what they are wanna to.
+
+Combining with ucm machanism, userpace can do fantasic controlling as
+they want to.
+
+Jack and kcontrols
+==================
+
+Each jack will have a kcontrol list, we can create a kcontrol and attach
+it to the jack, at jack creating stage. Also, we can add kcontrol to an
+exist jack, at anytime when you are happy.
+
+Those kcontrols will be freed automatically when the Jack is freed.
+
+How to create kcontrol embedded jack?
+=====================================
+
+To be compatible with previous HD-Audio jack and ASoC jack, we modify
+snd_jack_new() with adding two params:
+
+ - @phantom_jack: for phantom jack, only create needed kctl, won't create
+	real jack device.
+ - @jjack_kctl: create kctl if non-NULL pointer passed in, and provide
+	it to the caller. also add it to the non-phantom jack kctl list.
+
+For HDA jack, we can use phantom_jack = true for phantom jack creating,
+and the jack kctl pointer will be filled with the new created kcontrol.
+
+For ASoC jack, usually, we create kcontrols after jack is created, with
+calling snd_hda_jack_add_kctl() for each jack pins. And, the pin name
+will be assigned to the corresponding kcontrol name. So, to make
+userspace understand and recognize it, please assign proper name for
+each jack pin.
+
Signature · 1 line
1.9.1