diff mbox

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

Message ID 1429351460-15932-2-git-send-email-yang.jie@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jie, Yang April 18, 2015, 10:04 a.m. UTC
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.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Modified-by: Jie Yang <yang.jie@intel.com>
Signed-off-by: Jie Yang <yang.jie@intel.com>
Reveiwed-by: Mark Brown <broonie@kernel.org>
---
 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(-)

Comments

Takashi Iwai April 19, 2015, 8:25 a.m. UTC | #1
At Sat, 18 Apr 2015 18:04:15 +0800,
Jie Yang wrote:
> 
> 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.

> 
> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
> Modified-by: Jie Yang <yang.jie@intel.com>
> Signed-off-by: Jie Yang <yang.jie@intel.com>
> Reveiwed-by: Mark Brown <broonie@kernel.org>
> ---
>  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(-)
> 
> diff --git a/include/sound/jack.h b/include/sound/jack.h
> index 2182350..8337000 100644
> --- a/include/sound/jack.h
> +++ b/include/sound/jack.h
> @@ -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);
> +};

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.


>  #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.


> +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.

> +	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.


> +
> +static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
> +{
> +	struct snd_jack_kctl *jack_kctl;
> +
> +	if (kctl) {

This NULL check is superfluous.

> +		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)
> +{
> +	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.

> +
> +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;
> +
> +	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
Jie, Yang April 20, 2015, 7:52 a.m. UTC | #2
> -----Original Message-----
> From: Takashi Iwai [mailto:tiwai@suse.de]
> Sent: Sunday, April 19, 2015 4:26 PM
> To: Jie, Yang
> Cc: broonie@kernel.org; alsa-devel@alsa-project.org; Girdwood, Liam R;
> tanu.kaskinen@linux.intel.com; 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,
> Jie Yang wrote:
> >
> > 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.
 
Ah, forgot to update this as it has been change a lot from the v1, will do that.

> 
> >
> > Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
> > Modified-by: Jie Yang <yang.jie@intel.com>
> > Signed-off-by: Jie Yang <yang.jie@intel.com>
> > Reveiwed-by: Mark Brown <broonie@kernel.org>
> > ---
> >  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(-)
> >
> > diff --git a/include/sound/jack.h b/include/sound/jack.h index
> > 2182350..8337000 100644
> > --- a/include/sound/jack.h
> > +++ b/include/sound/jack.h
> > @@ -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); };
> 
> 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. :(

> 
> That said, the kctl management internal isn't the thing the driver needs to
> care.
> 
> 
> >  #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'.

> 
> 
> > +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.
 
OK.

> 
> > +	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.

> 
> 
> > +
> > +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. 

> 
> > +		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) {
> > +	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
 
Agree, will implement this in next version.

> 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? 

> 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.

> 
> > +
> > +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;
> > +
> > +	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.
 
Thanks for reminding. Snd_ctl_free_one() for the first goto is needed, I will
remove it for the second goto.

> 
> 
> Takashi
Takashi Iwai April 20, 2015, 8:06 a.m. UTC | #3
At Mon, 20 Apr 2015 07:52:20 +0000,
Jie, Yang wrote:
> 
> > > @@ -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.
>  
> 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)
> > 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
Jie, Yang April 20, 2015, 8:38 a.m. UTC | #4
> -----Original Message-----
> From: Takashi Iwai [mailto:tiwai@suse.de]
> Sent: Monday, April 20, 2015 4:06 PM
> To: Jie, Yang
> Cc: broonie@kernel.org; alsa-devel@alsa-project.org; Girdwood, Liam R;
> tanu.kaskinen@linux.intel.com; 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,
> Jie, Yang wrote:
> >
> > > > @@ -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.
> >
> > 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.
 
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. :)

> 
> (snip)
> > > 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.
 
OK, then I feel reassured to replace it. :)

> 
> 
> thanks,
> 
> Takashi
diff mbox

Patch

diff --git a/include/sound/jack.h b/include/sound/jack.h
index 2182350..8337000 100644
--- a/include/sound/jack.h
+++ b/include/sound/jack.h
@@ -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)
 {
diff --git a/sound/core/Kconfig b/sound/core/Kconfig
index 313f22e..63cc2e9 100644
--- a/sound/core/Kconfig
+++ b/sound/core/Kconfig
@@ -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
diff --git a/sound/core/Makefile b/sound/core/Makefile
index 4daf2f5..e041dc2 100644
--- a/sound/core/Makefile
+++ b/sound/core/Makefile
@@ -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
diff --git a/sound/core/jack.c b/sound/core/jack.c
index 8658578..a03fc93 100644
--- a/sound/core/jack.c
+++ b/sound/core/jack.c
@@ -24,6 +24,7 @@ 
 #include <linux/module.h>
 #include <sound/jack.h>
 #include <sound/core.h>
+#include <sound/control.h>
 
 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
 	SW_HEADPHONE_INSERT,
@@ -54,7 +55,13 @@  static int snd_jack_dev_disconnect(struct snd_device *device)
 static int snd_jack_dev_free(struct snd_device *device)
 {
 	struct snd_jack *jack = device->device_data;
+	struct snd_card *card = device->card;
+	struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
 
+	list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
+		list_del_init(&jack_kctl->list);
+		snd_ctl_remove(card, jack_kctl->kctl);
+	}
 	if (jack->private_free)
 		jack->private_free(jack);
 
@@ -100,6 +107,127 @@  static int snd_jack_dev_register(struct snd_device *device)
 	return err;
 }
 
+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++;
+	}
+	return sid.index;
+}
+
+static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
+{
+	struct snd_jack_kctl *jack_kctl;
+
+	if (kctl) {
+		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)
+{
+	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);
+
+}
+
+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;
+
+	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);
+	return NULL;
+}
+
+/**
+ * snd_jack_add_new_kctls - Create a new snd_jack_kctl and add it to jack
+ * @jack:  the jack instance which the kctl will attaching to
+ * @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, then add it to the jack kctl_list.
+ *
+ * Return: Zero if successful, or a negative error code on failure.
+ */
+int snd_jack_add_new_kctls(struct snd_jack *jack, const char * name, int mask)
+{
+	struct snd_jack_kctl *jack_kctl;
+
+	jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
+	if (!jack_kctl)
+		return -ENOMEM;
+
+	snd_jack_kctl_add(jack, jack_kctl);
+	return 0;
+}
+EXPORT_SYMBOL(snd_jack_add_new_kctls);
+
 /**
  * snd_jack_new - Create a new jack
  * @card:  the card instance
@@ -150,6 +278,9 @@  int snd_jack_new(struct snd_card *card, const char *id, int type,
 	if (err < 0)
 		goto fail_input;
 
+	jack->card = card;
+	INIT_LIST_HEAD(&jack->kctl_list);
+
 	*jjack = jack;
 
 	return 0;
@@ -230,6 +361,7 @@  EXPORT_SYMBOL(snd_jack_set_key);
  */
 void snd_jack_report(struct snd_jack *jack, int status)
 {
+	struct snd_jack_kctl *jack_kctl;
 	int i;
 
 	if (!jack)
@@ -245,13 +377,20 @@  void snd_jack_report(struct snd_jack *jack, int status)
 
 	for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
 		int testbit = 1 << i;
-		if (jack->type & testbit)
+		if (jack->type & testbit) {
 			input_report_switch(jack->input_dev,
 					    jack_switch_types[i],
 					    status & testbit);
+		}
 	}
 
 	input_sync(jack->input_dev);
+
+	list_for_each_entry(jack_kctl, &jack->kctl_list, list) {
+		snd_kctl_jack_report(jack->card, jack_kctl->kctl,
+					status & jack_kctl->mask_bits);
+	}
+
 }
 EXPORT_SYMBOL(snd_jack_report);
 
diff --git a/sound/pci/hda/Kconfig b/sound/pci/hda/Kconfig
index 7f0f2c5..363f365 100644
--- a/sound/pci/hda/Kconfig
+++ b/sound/pci/hda/Kconfig
@@ -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"