Skip to content

Commit b8dd086

Browse files
keyonjietiwai
authored andcommitted
ALSA: Jack: handle jack embedded kcontrol creating within ctljack
This patch adds a static method get_available_index() to allocate the index of new jack kcontrols and also adds jack_kctl_name_gen() which is used to ensure compatibility with jack naming by removing " Jack" from some incorrectly passed names. Signed-off-by: Jie Yang <yang.jie@intel.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 9058cbe commit b8dd086

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎include/sound/control.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only);
252252
* Helper functions for jack-detection controls
253253
*/
254254
struct snd_kcontrol *
255-
snd_kctl_jack_new(const char *name, int idx, void *private_data);
255+
snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card);
256256
void snd_kctl_jack_report(struct snd_card *card,
257257
struct snd_kcontrol *kctl, bool status);
258258

‎sound/core/ctljack.c‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,46 @@ static struct snd_kcontrol_new jack_detect_kctl = {
3131
.get = jack_detect_kctl_get,
3232
};
3333

34+
static int get_available_index(struct snd_card *card, const char *name)
35+
{
36+
struct snd_ctl_elem_id sid;
37+
38+
memset(&sid, 0, sizeof(sid));
39+
40+
sid.index = 0;
41+
sid.iface = SNDRV_CTL_ELEM_IFACE_CARD;
42+
strlcpy(sid.name, name, sizeof(sid.name));
43+
44+
while (snd_ctl_find_id(card, &sid))
45+
sid.index++;
46+
47+
return sid.index;
48+
}
49+
50+
static void jack_kctl_name_gen(char *name, const char *src_name, int size)
51+
{
52+
size_t count = strlen(src_name);
53+
bool need_cat = true;
54+
55+
/* remove redundant " Jack" from src_name */
56+
if (count >= 5)
57+
need_cat = strncmp(&src_name[count - 5], " Jack", 5) ? true : false;
58+
59+
snprintf(name, size, need_cat ? "%s Jack" : "%s", src_name);
60+
61+
}
62+
3463
struct snd_kcontrol *
35-
snd_kctl_jack_new(const char *name, int idx, void *private_data)
64+
snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card)
3665
{
3766
struct snd_kcontrol *kctl;
67+
3868
kctl = snd_ctl_new1(&jack_detect_kctl, private_data);
3969
if (!kctl)
4070
return NULL;
41-
snprintf(kctl->id.name, sizeof(kctl->id.name), "%s Jack", name);
42-
kctl->id.index = idx;
71+
72+
jack_kctl_name_gen(kctl->id.name, name, sizeof(kctl->id.name));
73+
kctl->id.index = get_available_index(card, name);
4374
kctl->private_value = 0;
4475
return kctl;
4576
}

‎sound/core/jack.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const cha
135135
struct snd_jack_kctl *jack_kctl;
136136
int err;
137137

138-
kctl = snd_kctl_jack_new(name, 0, card);
138+
kctl = snd_kctl_jack_new(name, card, card);
139139
if (!kctl)
140140
return NULL;
141141

‎sound/pci/hda/hda_jack.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
402402
return 0;
403403
if (jack->kctl)
404404
return 0; /* already created */
405-
kctl = snd_kctl_jack_new(name, idx, codec);
405+
kctl = snd_kctl_jack_new(name, codec, codec->card);
406406
if (!kctl)
407407
return -ENOMEM;
408408
err = snd_hda_ctl_add(codec, nid, kctl);

0 commit comments

Comments
 (0)