Skip to contentSkip to conversation

ALSA: jack: create jack kcontrols for every jack input device

PATCH Superseded Jie Yang 4 participants 9 messages Mar 19, 2015 Sound

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Jie Yang

author
#0 lore raw

PATCH 1/1 ALSA: jack: create jack kcontrols for every jack input device

From: Liam Girdwood <[email protected]>

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/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 sound/core/jack.c
include/sound/control.h +2 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to the same jack*/
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit index */
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);
include/sound/jack.h +2 -0
index 2182350..477dcb4 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;
sound/core/jack.c +87 -2

142 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines · Jie Yang

From: Liam Girdwood <[email protected]>

I cant claim much credit for this as it's mostly your work now :)

Liam

Quoted 67 lines

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/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 sound/core/jack.c
include/sound/control.h +2 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to the same jack*/
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit index */
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);
include/sound/jack.h +2 -0
index 2182350..477dcb4 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;
sound/core/jack.c +1 -0
old mode 100644
new mode 100755
index 8658578..c067b6a
@@ -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,

sound/core/jack.c static int snd_jack_dev_disconnect(struct snd_device *device) · line 62

Quoted 8 lines
sound/core/jack.c +4 -0
@@ -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_kcontrol *kctl;
 
+	list_for_each_entry(kctl, &jack->kctl_list, jack_list) {
+		list_del(&kctl->jack_list);

This is not safe, as we cant iterate over a list when we are removing items using list_for_each_entry(). There are other list methods in list.h to safely iterate and remove items.

Quoted 15 lines
sound/core/jack.c +8 -0
+		snd_ctl_remove(card, kctl);
+	}
 	if (jack->private_free)
 		jack->private_free(jack);
 
@@ -100,6 +107,63 @@ static int snd_jack_dev_register(struct snd_device *device)
 	return err;
 }
 
+
+/* get the first unused/available index number for the given kctl name */
+static int get_available_index(struct snd_card *card, const char *name)
+{
+	struct snd_kcontrol *kctl;
+	int idx = 0;

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 125

Quoted 10 lines
sound/core/jack.c +10 -0
+	int len = strlen(name);
+
+	down_write(&card->controls_rwsem);
+	next:
+	list_for_each_entry(kctl, &card->controls, list) {
+		if (!strncmp(name, kctl->id.name, len) &&
+		    !strcmp(" Jack", kctl->id.name + len) &&
+		    kctl->id.index == idx) {
+			idx++;
+			goto next;

Wont continue work here instead of goto ?

Quoted 18 lines
sound/core/jack.c +18 -0
+		}
+	}
+	up_write(&card->controls_rwsem);
+	return idx;
+}
+
+static void kctl_private_free(struct snd_kcontrol *kctl)
+{
+	list_del(&kctl->jack_list);
+}
+
+static int snd_jack_new_kctl(struct snd_card *card, struct snd_jack *jack, int type)
+{
+	struct snd_kcontrol *kctl;
+	int i, err, index, state = 0 /* use 0 for default state ?? */;
+
+	INIT_LIST_HEAD(&jack->kctl_list);
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 153

Quoted 10 lines
sound/core/jack.c +10 -0
+		int testbit = 1 << i;
+		if (type & testbit) {
+			index = get_available_index(card,jack->id);
+			kctl = snd_kctl_jack_new(jack->id, index, card);
+			if (!kctl)
+				return -ENOMEM;
+			kctl->private_free = kctl_private_free;
+			/* use jack_bit_idx for the kctl type bit */
+			kctl->jack_bit_idx = i;
+

This section above could do with some new lines to break the code into logical chunks.

Quoted 69 lines
sound/core/jack.c +29 -2
+			err = snd_ctl_add(card, kctl);
+			if (err < 0)
+				return err;
+			list_add_tail(&kctl->jack_list, &jack->kctl_list);
+
+			/* set initial jack state */
+			snd_kctl_jack_report(card, kctl, state & testbit);
+		}
+	}
+
+	return 0;
+}
+
 /**
  * snd_jack_new - Create a new jack
  * @card:  the card instance
@@ -138,7 +202,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++)
@@ -150,10 +214,17 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
 	if (err < 0)
 		goto fail_input;
 
+	/* register jack kcontrols  */
+	err = snd_jack_new_kctl(card, jack, type);
+	if (err < 0)
+		goto fail_kctl;
+
 	*jjack = jack;
 
 	return 0;
 
+fail_kctl:
+	snd_device_free(card, jack);
 fail_input:
 	input_free_device(jack->input_dev);
 	kfree(jack->id);
@@ -230,6 +301,7 @@ EXPORT_SYMBOL(snd_jack_set_key);
  */
 void snd_jack_report(struct snd_jack *jack, int status)
 {
+	struct snd_kcontrol *kctl;
 	int i;
 
 	if (!jack)
@@ -245,13 +317,26 @@ 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);
+
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {
+		int testbit = 1 << i;
+		if (jack->type & testbit) {
+			list_for_each_entry(kctl, &jack->kctl_list, jack_list) {

sound/core/jack.c void snd_jack_report(struct snd_jack *jack, int status) · line 342

Quoted 10 lines
sound/core/jack.c +7 -0
+				if (kctl->jack_bit_idx == i) {
+					snd_kctl_jack_report(jack->card, kctl,
+								status & testbit);
+				}
+			}
+		}
+	}
 }
 EXPORT_SYMBOL(snd_jack_report);
 

Liam

#2 lore raw
Quoted 7 lines · Liam Girdwood
Quoted 2 lines · Jie Yang

From: Liam Girdwood <[email protected]>

I cant claim much credit for this as it's mostly your work now :)

Liam

Sorry, I had more comments below... also got one more new one

Quoted 90 lines
Quoted 68 lines

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/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 sound/core/jack.c
include/sound/control.h +2 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to the same jack*/
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit index */
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);
include/sound/jack.h +2 -0
index 2182350..477dcb4 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;
sound/core/jack.c +1 -0
old mode 100644
new mode 100755
index 8658578..c067b6a
@@ -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,

sound/core/jack.c static int snd_jack_dev_disconnect(struct snd_device *device) · line 62

Quoted 8 lines
sound/core/jack.c +4 -0
@@ -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_kcontrol *kctl;
 
+	list_for_each_entry(kctl, &jack->kctl_list, jack_list) {
+		list_del(&kctl->jack_list);

This is not safe, as we cant iterate over a list when we are removing items using list_for_each_entry(). There are other list methods in list.h to safely iterate and remove items.

Quoted 9 lines
sound/core/jack.c +2 -0
+		snd_ctl_remove(card, kctl);
+	}
 	if (jack->private_free)
 		jack->private_free(jack);
 
@@ -100,6 +107,63 @@ static int snd_jack_dev_register(struct snd_device *device)
 	return err;
 }
 
Quoted 9 lines

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 118

Quoted 9 lines
sound/core/jack.c +9 -0
+
+/* get the first unused/available index number for the given kctl name */
+static int get_available_index(struct snd_card *card, const char *name)
+{
+	struct snd_kcontrol *kctl;
+	int idx = 0;
+	int len = strlen(name);
+
+	down_write(&card->controls_rwsem);

I think we are only reading the card state below so we can use down_read().

Quoted 105 lines

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 125

Quoted 7 lines
sound/core/jack.c +7 -0
+	next:
+	list_for_each_entry(kctl, &card->controls, list) {
+		if (!strncmp(name, kctl->id.name, len) &&
+		    !strcmp(" Jack", kctl->id.name + len) &&
+		    kctl->id.index == idx) {
+			idx++;
+			goto next;

Wont continue work here instead of goto ?

Quoted 18 lines
sound/core/jack.c +18 -0
+		}
+	}
+	up_write(&card->controls_rwsem);
+	return idx;
+}
+
+static void kctl_private_free(struct snd_kcontrol *kctl)
+{
+	list_del(&kctl->jack_list);
+}
+
+static int snd_jack_new_kctl(struct snd_card *card, struct snd_jack *jack, int type)
+{
+	struct snd_kcontrol *kctl;
+	int i, err, index, state = 0 /* use 0 for default state ?? */;
+
+	INIT_LIST_HEAD(&jack->kctl_list);
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 153

Quoted 10 lines
sound/core/jack.c +10 -0
+		int testbit = 1 << i;
+		if (type & testbit) {
+			index = get_available_index(card,jack->id);
+			kctl = snd_kctl_jack_new(jack->id, index, card);
+			if (!kctl)
+				return -ENOMEM;
+			kctl->private_free = kctl_private_free;
+			/* use jack_bit_idx for the kctl type bit */
+			kctl->jack_bit_idx = i;
+

This section above could do with some new lines to break the code into logical chunks.

Quoted 63 lines
sound/core/jack.c +24 -2
+			err = snd_ctl_add(card, kctl);
+			if (err < 0)
+				return err;
+			list_add_tail(&kctl->jack_list, &jack->kctl_list);
+
+			/* set initial jack state */
+			snd_kctl_jack_report(card, kctl, state & testbit);
+		}
+	}
+
+	return 0;
+}
+
 /**
  * snd_jack_new - Create a new jack
  * @card:  the card instance
@@ -138,7 +202,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++)
@@ -150,10 +214,17 @@ int snd_jack_new(struct snd_card *card, const char *id, int type,
 	if (err < 0)
 		goto fail_input;
 
+	/* register jack kcontrols  */
+	err = snd_jack_new_kctl(card, jack, type);
+	if (err < 0)
+		goto fail_kctl;
+
 	*jjack = jack;
 
 	return 0;
 
+fail_kctl:
+	snd_device_free(card, jack);
 fail_input:
 	input_free_device(jack->input_dev);
 	kfree(jack->id);
@@ -230,6 +301,7 @@ EXPORT_SYMBOL(snd_jack_set_key);
  */
 void snd_jack_report(struct snd_jack *jack, int status)
 {
+	struct snd_kcontrol *kctl;
 	int i;
 
 	if (!jack)
@@ -245,13 +317,26 @@ 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);
+		}
 	}
 
Quoted 16 lines
Quoted 6 lines
sound/core/jack.c +5 -0
 	input_sync(jack->input_dev);
+
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {
+		int testbit = 1 << i;
+		if (jack->type & testbit) {
+			list_for_each_entry(kctl, &jack->kctl_list, jack_list) {

sound/core/jack.c void snd_jack_report(struct snd_jack *jack, int status) · line 342

Quoted 10 lines
sound/core/jack.c +7 -0
+				if (kctl->jack_bit_idx == i) {
+					snd_kctl_jack_report(jack->card, kctl,
+								status & testbit);
+				}
+			}
+		}
+	}
 }
 EXPORT_SYMBOL(snd_jack_report);
 

Liam

---------------------------------------------------------------------
Intel Corporation (UK) Limited
Registered No. 1134945 (England)
Registered Office: Pipers Way, Swindon SN3 1RJ
VAT No: 860 2173 47

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

author
#3 lore raw
In reply to Liam Girdwood
Quoted 9 lines

-----Original Message-----
From: Liam Girdwood [mailto:[email protected]]
Sent: Thursday, March 19, 2015 4:54 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; [email protected]; alsa-devel@alsa- project.org
Subject: Re: [PATCH] ALSA: jack: create jack kcontrols for every jack input device

Quoted 5 lines
Quoted 2 lines · Jie Yang

From: Liam Girdwood <[email protected]>

I cant claim much credit for this as it's mostly your work now :)

[Keyon] OK, I will change it, thanks. :)

Quoted 77 lines

Liam

Quoted 8 lines

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.

Quoted 8 lines
---
 include/sound/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89

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

include/sound/control.h struct snd_kcontrol_volatile { · line 69

Quoted 12 lines
 3 files changed, 91 insertions(+), 2 deletions(-)  mode change 100644

=> 100755 sound/core/jack.c

include/sound/control.h index renamed from include/sound/control.h +0 -0

75f3054..023b70a 100644

include/sound/control.h +1 -0
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to

the same jack*/

include/sound/control.h struct snd_kcontrol { · line 79

Quoted 8 lines
include/sound/control.h +1 -0
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit

index */

Quoted 35 lines
include/sound/control.h +0 -0
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol); diff --git

a/include/sound/jack.h b/include/sound/jack.h index 2182350..477dcb4 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;
sound/core/jack.c old mode 100644 new renamed from sound/core/jack.c +0 -0

mode 100755 index 8658578..c067b6a

sound/core/jack.c +1 -0
@@ -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) {

sound/core/jack.c +2 -0
 	struct snd_jack *jack = device->device_data;
+	struct snd_card *card = device->card;
+	struct snd_kcontrol *kctl;
Quoted 6 lines

sound/core/jack.c static int snd_jack_dev_disconnect(struct snd_device *device) · line 62

Quoted 2 lines
sound/core/jack.c +2 -0
+	list_for_each_entry(kctl, &jack->kctl_list, jack_list) {
+		list_del(&kctl->jack_list);

This is not safe, as we cant iterate over a list when we are removing items using list_for_each_entry(). There are other list methods in list.h to safely iterate and remove items.

[Keyon] thanks for pointing out this, will modify it.

Quoted 21 lines

sound/core/jack.c static int snd_jack_dev_register(struct snd_device

Quoted 7 lines
sound/core/jack.c +2 -0
+		snd_ctl_remove(card, kctl);
+	}
 	if (jack->private_free)
 		jack->private_free(jack);
@@ -100,6 +107,63 @@ static int snd_jack_dev_register(struct snd_device

*device)

Quoted 13 lines
sound/core/jack.c +10 -0
 	return err;
 }
 
+
+/* get the first unused/available index number for the given kctl
+name */ static int get_available_index(struct snd_card *card, const
+char *name) {
+	struct snd_kcontrol *kctl;
+	int idx = 0;
+	int len = strlen(name);
+
+	down_write(&card->controls_rwsem);
+	next:
Quoted 8 lines

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 125

Quoted 6 lines
sound/core/jack.c +6 -0
+	list_for_each_entry(kctl, &card->controls, list) {
+		if (!strncmp(name, kctl->id.name, len) &&
+		    !strcmp(" Jack", kctl->id.name + len) &&
+		    kctl->id.index == idx) {
+			idx++;
+			goto next;

Wont continue work here instead of goto ?

[Keyon] we need goto to iterate the list again with bigger idx.

Quoted 23 lines
Quoted 23 lines
sound/core/jack.c +22 -0
+		}
+	}
+	up_write(&card->controls_rwsem);
+	return idx;
+}
+
+static void kctl_private_free(struct snd_kcontrol *kctl) {
+	list_del(&kctl->jack_list);
+}
+
+static int snd_jack_new_kctl(struct snd_card *card, struct snd_jack
+*jack, int type) {
+	struct snd_kcontrol *kctl;
+	int i, err, index, state = 0 /* use 0 for default state ?? */;
+
+	INIT_LIST_HEAD(&jack->kctl_list);
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {
+		int testbit = 1 << i;
+		if (type & testbit) {
+			index = get_available_index(card,jack->id);
+			kctl = snd_kctl_jack_new(jack->id, index, card);
+			if (!kctl)
Quoted 8 lines

sound/core/jack.c static int snd_jack_dev_register(struct snd_device *device) · line 153

Quoted 5 lines
sound/core/jack.c +5 -0
+				return -ENOMEM;
+			kctl->private_free = kctl_private_free;
+			/* use jack_bit_idx for the kctl type bit */
+			kctl->jack_bit_idx = i;
+

This section above could do with some new lines to break the code into logical chunks.

[Keyon] OK.

Quoted 85 lines

sound/core/jack.c int snd_jack_new(struct snd_card *card, const char

Quoted 18 lines
sound/core/jack.c +13 -0
+			err = snd_ctl_add(card, kctl);
+			if (err < 0)
+				return err;
+			list_add_tail(&kctl->jack_list, &jack->kctl_list);
+
+			/* set initial jack state */
+			snd_kctl_jack_report(card, kctl, state & testbit);
+		}
+	}
+
+	return 0;
+}
+
 /**
  * snd_jack_new - Create a new jack
  * @card:  the card instance
@@ -138,7 +202,7 @@ int snd_jack_new(struct snd_card *card, const char

*id, int type,

sound/core/jack.c int snd_jack_new(struct snd_card *card, const char *id, int type,

Quoted 8 lines
sound/core/jack.c +1 -1
 	}
 
 	jack->input_dev->phys = "ALSA";
-
+	jack->card = card;
 	jack->type = type;
 
 	for (i = 0; i < SND_JACK_SWITCH_TYPES; i++) @@ -150,10 +214,17

@@

Quoted 37 lines

int snd_jack_new(struct snd_card *card, const char *id, int type,

sound/core/jack.c +10 -1
 	if (err < 0)
 		goto fail_input;
 
+	/* register jack kcontrols  */
+	err = snd_jack_new_kctl(card, jack, type);
+	if (err < 0)
+		goto fail_kctl;
+
 	*jjack = jack;
 
 	return 0;
 
+fail_kctl:
+	snd_device_free(card, jack);
 fail_input:
 	input_free_device(jack->input_dev);
 	kfree(jack->id);
@@ -230,6 +301,7 @@ EXPORT_SYMBOL(snd_jack_set_key);
  */
 void snd_jack_report(struct snd_jack *jack, int status)  {
+	struct snd_kcontrol *kctl;
 	int i;
 
 	if (!jack)
@@ -245,13 +317,26 @@ 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);
+		}
 	}

sound/core/jack.c void snd_jack_report(struct snd_jack *jack, int · line 336

Quoted 10 lines
sound/core/jack.c +8 -0
 
 	input_sync(jack->input_dev);
+
+	for (i = 0; i < fls(SND_JACK_BTN_0); i++) {
+		int testbit = 1 << i;
+		if (jack->type & testbit) {
+			list_for_each_entry(kctl, &jack->kctl_list, jack_list) {
+				if (kctl->jack_bit_idx == i) {
+					snd_kctl_jack_report(jack->card, kctl,
+								status &

testbit);

sound/core/jack.c void snd_jack_report(struct snd_jack *jack, int status) · line 341

Quoted 7 lines
sound/core/jack.c +4 -0
+				}
+			}
+		}
+	}
 }
 EXPORT_SYMBOL(snd_jack_report);

Liam

#4 lore raw
In reply to Jie Yang

At Thu, 19 Mar 2015 16:40:18 +0800,

Quoted 35 lines · Jie Yang

From: Liam Girdwood <[email protected]>

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/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 sound/core/jack.c
include/sound/control.h +1 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to the same jack*/
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;

include/sound/control.h struct snd_kcontrol { · line 82

Quoted 7 lines
include/sound/control.h +1 -0
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit index */
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);

NAK for these additions. There must be a way to implement without such *generic* additions in kcontrol.

Takashi

#5 lore raw

At Thu, 19 Mar 2015 11:51:23 +0100,

Quoted 40 lines · Takashi Iwai

At Thu, 19 Mar 2015 16:40:18 +0800,

Quoted 37 lines · Jie Yang

From: Liam Girdwood <[email protected]>

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/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89 +++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 2 deletions(-)
 mode change 100644 => 100755 sound/core/jack.c
include/sound/control.h +1 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to the same jack*/
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
Quoted 8 lines

include/sound/control.h struct snd_kcontrol { · line 82

Quoted 5 lines
include/sound/control.h +1 -0
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit index */
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);

NAK for these additions. There must be a way to implement without such *generic* additions in kcontrol.

IOW, you can allocate its own record in kctl->private_data. Currently hda_jack.c is the only user of snd_kctl_jack_*(). So, if we remove these callers, we can assign kctl->private_data for jack's own purpose. There you'll allocate own record for keeping tracking the list and bitmap, etc.

Takashi

author
#6 lore raw
In reply to Takashi Iwai
Quoted 53 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Thursday, March 19, 2015 6:51 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; [email protected]; Girdwood, Liam R; Liam Girdwood
Subject: Re: [PATCH] ALSA: jack: create jack kcontrols for every jack input device

At Thu, 19 Mar 2015 16:40:18 +0800,

Quoted 11 lines · Jie Yang

From: Liam Girdwood <[email protected]>

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.

Quoted 8 lines
---
 include/sound/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89

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

include/sound/control.h struct snd_kcontrol_volatile { · line 69

Quoted 12 lines
 3 files changed, 91 insertions(+), 2 deletions(-)  mode change 100644

=> 100755 sound/core/jack.c

include/sound/control.h index renamed from include/sound/control.h +0 -0

75f3054..023b70a 100644

include/sound/control.h +1 -0
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to

the same jack*/

include/sound/control.h struct snd_kcontrol { · line 79

Quoted 8 lines
include/sound/control.h +1 -0
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit

index */

Quoted 6 lines

include/sound/control.h struct snd_kcontrol { · line 82

Quoted 3 lines
include/sound/control.h +0 -0
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);

NAK for these additions. There must be a way to implement without such *generic* additions in kcontrol.

[Keyon] how about define another struct jack_kcontrol in jack.h like below?

struct jack_kcontrol {
	struct snd_kcontrol * kctl;
	struct list_head jack_list;		/* list of controls belong to

the same jack*/
unsigned int jack_bit_idx; /*the corresponding jack type bit
index */
}

Quoted 3 lines

Takashi

#7 lore raw

At Thu, 19 Mar 2015 13:53:17 +0000,

Quoted 62 lines · Yang
Quoted 54 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Thursday, March 19, 2015 6:51 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; [email protected]; Girdwood, Liam R; Liam Girdwood
Subject: Re: [PATCH] ALSA: jack: create jack kcontrols for every jack input device

At Thu, 19 Mar 2015 16:40:18 +0800,

Quoted 11 lines · Jie Yang

From: Liam Girdwood <[email protected]>

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.

Quoted 8 lines
---
 include/sound/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89

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

include/sound/control.h struct snd_kcontrol_volatile { · line 69

Quoted 12 lines
 3 files changed, 91 insertions(+), 2 deletions(-)  mode change 100644

=> 100755 sound/core/jack.c

include/sound/control.h index renamed from include/sound/control.h +0 -0

75f3054..023b70a 100644

include/sound/control.h +1 -0
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to

the same jack*/

include/sound/control.h struct snd_kcontrol { · line 79

Quoted 8 lines
include/sound/control.h +1 -0
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit

index */

Quoted 6 lines

include/sound/control.h struct snd_kcontrol { · line 82

Quoted 3 lines
include/sound/control.h +0 -0
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);

NAK for these additions. There must be a way to implement without such *generic* additions in kcontrol.

[Keyon] how about define another struct jack_kcontrol in jack.h like below?

Quoted 7 lines
struct jack_kcontrol {
	struct snd_kcontrol * kctl;
	struct list_head jack_list;		/* list of controls belong to

the same jack*/
unsigned int jack_bit_idx; /*the corresponding jack type bit
index */
}

Yes, and point it from kctl->private_data.

Takashi

author
#8 lore raw
Quoted 82 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Thursday, March 19, 2015 10:02 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; [email protected]; Girdwood, Liam R; Liam Girdwood
Subject: Re: [PATCH] ALSA: jack: create jack kcontrols for every jack input device

At Thu, 19 Mar 2015 13:53:17 +0000,

Quoted 63 lines · Yang
Quoted 54 lines

-----Original Message-----
From: Takashi Iwai [mailto:[email protected]]
Sent: Thursday, March 19, 2015 6:51 PM
To: Jie, Yang
Cc: [email protected]; [email protected]; [email protected]; Girdwood, Liam R; Liam Girdwood
Subject: Re: [PATCH] ALSA: jack: create jack kcontrols for every jack input device

At Thu, 19 Mar 2015 16:40:18 +0800,

Quoted 11 lines · Jie Yang

From: Liam Girdwood <[email protected]>

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.

Quoted 8 lines
---
 include/sound/control.h |  2 ++
 include/sound/jack.h    |  2 ++
 sound/core/jack.c       | 89

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

include/sound/control.h struct snd_kcontrol_volatile { · line 69

Quoted 12 lines
 3 files changed, 91 insertions(+), 2 deletions(-)  mode change

100644 => 100755 sound/core/jack.c

include/sound/control.h +1 -0
index 75f3054..023b70a 100644
@@ -66,6 +66,7 @@ struct snd_kcontrol_volatile {
 
 struct snd_kcontrol {
 	struct list_head list;		/* list of controls */
+	struct list_head jack_list;		/* list of controls belong to

the same jack*/

include/sound/control.h struct snd_kcontrol { · line 79

Quoted 8 lines
include/sound/control.h +1 -0
 	struct snd_ctl_elem_id id;
 	unsigned int count;		/* count of same elements */
 	snd_kcontrol_info_t *info;
@@ -75,6 +76,7 @@ struct snd_kcontrol {
 		snd_kcontrol_tlv_rw_t *c;
 		const unsigned int *p;
 	} tlv;
+	unsigned int jack_bit_idx;	/*the corresponding jack type bit

index */

Quoted 7 lines

include/sound/control.h struct snd_kcontrol { · line 82

Quoted 3 lines
include/sound/control.h +0 -0
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);

NAK for these additions. There must be a way to implement without such
*generic* additions in kcontrol.

[Keyon] how about define another struct jack_kcontrol in jack.h like below?

Quoted 7 lines
struct jack_kcontrol {
	struct snd_kcontrol * kctl;
	struct list_head jack_list;		/* list of controls belong to

the same jack*/
unsigned int jack_bit_idx; /*the corresponding jack type bit
index */
}

Quoted 1 line

Yes, and point it from kctl->private_data.

[Keyon] OK, will change like that in v2. Thanks.

Quoted 3 lines

Takashi