Skip to contentSkip to conversation

ALSA: jack: Refactoring for jack kctls

PATCH Superseded Jie Yang 2 participants 7 messages Apr 7, 2015 – Apr 12, 2015

Subsystems (3)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jie Yang

author
#0 lore raw

v5 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,
a. call snd_jack_kctl_new()(in __snd_hda_jack_add_kctl()) to new a jack_kctl(also new kctl); and use snd_hda_ctl_add to add kctl to card; b. new a hda_jack_tbl, appoint jack_tbl->jack_kctl = jack_kctl; c. for non-phantom jack, call snd_jack_new() with this jack_kctl, then the jack_kctl will be added to the new created jack;

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 (5):
ALSA: jack: create jack kcontrols for every jack input
ALSA: jack: add a parameter to pass kctl for snd_jack_new
ALSA: hda - Update to use the new jack kctls method
ASoC: jack: create kctls according to jack pins info
ALSA: jack: remove export snd_kctl_jack_new()

 include/sound/jack.h            |  25 ++++++-
 sound/core/Kconfig              |   3 -
 sound/core/Makefile             |   3 +-
 sound/core/ctljack.c            |   1 -
 sound/core/jack.c               | 152 +++++++++++++++++++++++++++++++++++++++-
 sound/pci/hda/Kconfig           |   1 -
 sound/pci/hda/hda_jack.c        |  52 +++++++-------
 sound/pci/hda/hda_jack.h        |   4 +-
 sound/pci/oxygen/xonar_wm87x6.c |   2 +-
 sound/soc/soc-jack.c            |   3 +-
 10 files changed, 201 insertions(+), 45 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v5 1/5 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  |  21 ++++++++
 sound/core/Kconfig    |   3 --
 sound/core/Makefile   |   3 +-
 sound/core/jack.c     | 141 +++++++++++++++++++++++++++++++++++++++++++++++++-
 sound/pci/hda/Kconfig |   1 -
 5 files changed, 162 insertions(+), 7 deletions(-)
include/sound/jack.h +21 -0
index 2182350..07f7035 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,20 @@ 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
 
+struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *kctl_name, unsigned int mask);
 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,6 +105,10 @@ 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 struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *kctl_name, unsigned int mask)
+{
+	return NULL;
+}
 
 static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
 			       struct snd_jack **jack)
@@ -100,6 +116,11 @@ static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
 	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
author
#2 lore raw

PATCH v5 2/5 ALSA: jack: add a parameter to pass kctl for snd_jack_new

In reply to Jie Yang

Currentlly, for HDA jack, we will create jack kctl before calling snd_jack_new(). In our new design, this kctl should belong to the jack, so we add a param(struct snd_jack_kctl *) to pass the kctl, then we can add the kctl to jack during jack creating.

This make our design more compatible with existing HDA jack kctl.

---
 include/sound/jack.h            |  4 ++--
 sound/core/jack.c               | 11 +++++++++--
 sound/pci/hda/hda_jack.c        |  2 +-
 sound/pci/oxygen/xonar_wm87x6.c |  2 +-
 sound/soc/soc-jack.c            |  2 +-
 5 files changed, 14 insertions(+), 7 deletions(-)
include/sound/jack.h +2 -2
index 07f7035..5b580f8 100644
@@ -96,7 +96,7 @@ struct snd_jack_kctl {
 
 struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *kctl_name, unsigned int mask);
 int snd_jack_new(struct snd_card *card, const char *id, int type,
-		 struct snd_jack **jack);
+		 struct snd_jack **jack, struct snd_jack_kctl *jack_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,
@@ -111,7 +111,7 @@ static inline struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, co
 }
 
 static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
-			       struct snd_jack **jack)
+			       struct snd_jack **jack, struct snd_jack_kctl *jack_kctl)
 {
 	return 0;
 }
sound/core/jack.c +9 -2
index c74111f..b6e7be1 100644
@@ -205,6 +205,8 @@ EXPORT_SYMBOL(snd_jack_kctl_new);
  * @type:  a bitmask of enum snd_jack_type values that can be detected by
  *         this jack
  * @jjack: Used to provide the allocated jack object to the caller.
+ * @kctl: if kctl != NULL, it means we don't need create new kcontrol for this
+ * 	     jack, just assign it to the jack.
  *
  * Creates a new jack object.
  *
@@ -212,7 +214,7 @@ EXPORT_SYMBOL(snd_jack_kctl_new);
  * On success @jjack will be initialised.
  */
 int snd_jack_new(struct snd_card *card, const char *id, int type,
-		 struct snd_jack **jjack)
+		 struct snd_jack **jjack, struct snd_jack_kctl *jack_kctl)
 {
 	struct snd_jack *jack;
 	int err;
@@ -236,7 +238,7 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
 	}
 
 	jack->input_dev->phys = "ALSA";
-
+	jack->card = card;
 	jack->type = type;
 
 	for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
@@ -248,6 +250,11 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
 	if (err < 0)
 		goto fail_input;
 
+	INIT_LIST_HEAD(&jack->kctl_list);
+
+	if (jack_kctl)
+		snd_jack_kctl_add(jack, jack_kctl);
+
 	*jjack = jack;
 
 	return 0;
sound/pci/hda/hda_jack.c +1 -1
index e664307..9397250 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, NULL);
 		if (err < 0)
 			return err;
 		jack->jack->private_data = jack;
sound/pci/oxygen/xonar_wm87x6.c +1 -1
index 6ce6860..d30ec63 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, NULL);
 	xonar_ds_handle_hp_jack(chip);
 
 	snd_component_add(chip->card, "WM8776");
sound/soc/soc-jack.c +1 -1
index 9f60c25..3c87e32 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, NULL);
 	if (ret)
 		return ret;
 
Signature · 1 line
1.9.1
author
#3 lore raw

PATCH v5 3/5 ALSA: hda - Update to use the new jack kctls method

In reply to Jie Yang

In the new jack kctls design, snd_kcontrol is created during snd_jack_kctl creating, then, we can wrap it in struct hda_jack_tbl. The new method to create HDA jack kctl as below:

1. call snd_jack_kctl_new()(in __snd_hda_jack_add_kctl()) to new a jack_kctl(also new kctl), and use snd_hda_ctl_add to add kctl to card;
2. new a hda_jack_tbl, appoint jack_kctl to jack_tbl->jack_kctl;
3. for non-phantom jack, call snd_jack_new() with this jack_kctl, then the jack_kctl will be added to the new created jack;

---
 sound/pci/hda/hda_jack.c | 52 ++++++++++++++++++++++--------------------------
 sound/pci/hda/hda_jack.h |  4 +---
 2 files changed, 25 insertions(+), 31 deletions(-)
sound/pci/hda/hda_jack.c +24 -28
index 9397250..9a1ba35 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,38 @@ 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;
+	struct snd_jack_kctl *jack_kctl;
+	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)
+
+	type = get_input_jack_type(codec, nid);
+	jack_kctl = snd_jack_kctl_new(codec->bus->card, name, type);
+	if (!jack_kctl)
 		return -ENOMEM;
-	err = snd_hda_ctl_add(codec, nid, kctl);
+
+	err = snd_hda_ctl_add(codec, nid, jack_kctl->kctl);
 	if (err < 0)
 		return err;
-	jack->kctl = kctl;
+	jack->jack_kctl = jack_kctl;
 	jack->phantom_jack = !!phantom_jack;
 
 	state = snd_hda_jack_detect(codec, nid);
-	snd_kctl_jack_report(codec->bus->card, kctl, state);
-#ifdef CONFIG_SND_HDA_INPUT_JACK
+	snd_kctl_jack_report(codec->bus->card, jack_kctl->kctl, state);
 	if (!phantom_jack) {
-		jack->type = get_input_jack_type(codec, nid);
+		jack->type = type;
 		err = snd_jack_new(codec->bus->card, name, jack->type,
-				   &jack->jack, NULL);
+				   &jack->jack, jack_kctl);
 		if (err < 0)
 			return err;
-		jack->jack->private_data = jack;
-		jack->jack->private_free = hda_free_jack_priv;
+		jack->jack_kctl->private_data = jack;
+		jack->jack_kctl->private_free = hda_free_jack_priv;
 		snd_jack_report(jack->jack, state ? jack->type : 0);
 	}
-#endif
 	return 0;
 }
 
@@ -453,10 +449,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->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
#4 lore raw

PATCH v5 4/5 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 3c87e32..7099222 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
author
#5 lore raw

PATCH v5 5/5 ALSA: jack: remove export snd_kctl_jack_new()

In reply to Jie Yang

After created jack embedded kctls and removed hda jack kctls, the func snd_kctl_jack_new() is merely internal called, so here remove exporting it.

---
 sound/core/ctljack.c | 1 -
 1 file changed, 1 deletion(-)
sound/core/ctljack.c +0 -1
index e4b38fb..8702fb1 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)
Signature · 1 line
1.9.1
#6 lore raw
In reply to Jie Yang
Quoted 16 lines

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);

Quoted 6 lines

For hda,
a. call snd_jack_kctl_new()(in __snd_hda_jack_add_kctl()) to new a jack_kctl(also new kctl); and use snd_hda_ctl_add to add kctl to card; b. new a hda_jack_tbl, appoint jack_tbl->jack_kctl = jack_kctl; c. for non-phantom jack, call snd_jack_new() with this jack_kctl, then the jack_kctl will be added to the new created jack;

Do the user space application (e.g. pulseaudio) expect to driver to provide a mute switch which can mute the speaker when the driver create headphone jack control ?

Seem some hda codecs (e.g. cs4213) only have volume control and mute switch at the audio output ,

do pulseaudio still need the driver to create headphone jack kctl ? or pulseaudio is smart enough to ignore the headphone jack kctl

the driver can mute speaker by set pin ctl of speaker pin to zero but does not create speaker playback switch or headphone playback switch when there is no amp out mute in pin complex

Node 0x02 [Audio Output] wcaps 0xb041d: Stereo Amp-Out
Control: name="PCM Playback Volume", index=0, device=0

     ControlAmp: chs=3, dir=Out, idx=0, ofs=0
   Control: name="PCM Playback Switch", index=0, device=0
     ControlAmp: chs=3, dir=Out, idx=0, ofs=0
   Device: name="CS4213 Analog", type="Audio", device=0
   Amp-Out caps: ofs=0x73, nsteps=0x7f, stepsize=0x01, mute=1
   Amp-Out vals: [0x73 0x73]
   Converter: stream=8, channel=0
   PCM:
     rates [0x560]: 44100 48000 96000 192000
     bits [0xe]: 16 20 24
     formats [0x1]: PCM

Power states: D0 D3 EPSS
Power: setting=D0, actual=D0
Delay: 11 samples

Node 0x04 [Pin Complex] wcaps 0x410581: Stereo
Control: name="Headphone Jack", index=0, device=0 Pincap 0x0000001c: OUT HP Detect
Pin Default 0x0421101f: [Jack] HP Out at Ext Right
Conn = 1/8, Color = Black
DefAssociation = 0x1, Sequence = 0xf
Pin-ctls: 0xc0: OUT HP
Unsolicited: tag=01, enabled=1
Power states: D0 D3 EPSS
Power: setting=D0, actual=D0
Delay: 1 samples
Connection: 1

      0x02

Node 0x05 [Pin Complex] wcaps 0x420581: Stereo
Control: name="Speaker Phantom Jack", index=0, device=0 Pincap 0x00000050: OUT Balanced
Pin Default 0x90170010: [Fixed] Speaker at Int N/A
Conn = Analog, Color = Unknown DefAssociation = 0x1, Sequence = 0x0
Pin-ctls: 0x00:
Unsolicited: tag=00, enabled=0
Power states: D0 D3 EPSS
Power: setting=D0, actual=D0
Delay: 2 samples
Connection: 1

      0x02