From 9058cbe1eed29381f84dec9f96980f5a4ea1025f Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:20:56 +0800 Subject: ALSA: jack: implement kctl creating for jack devices 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 series will implement kctls inside the core jack part, including kctls creating, status changing report, for both HD-Audio and ASoC jack. This allows non root userspace to read jack status and act on it. This patch adds a new API called snd_jack_add_new_kctl(), which will create a kcontrol, add it to the card, and also attach it to the jack kctl list. This patch also initialises the jack kctl list after jack is newed, and reports kctl status when jack insertion/removal events occur. snd_jack_new() is updated in the following patches to also support creating phantom jacks and jack kcontrols. We then remove these duplicated features from HDA jack and have jack kctls handled by core throughout HDA and ASoC. Signed-off-by: Liam Girdwood Modified-by: Jie Yang Signed-off-by: Jie Yang Reveiwed-by: Mark Brown Signed-off-by: Takashi Iwai diff --git a/include/sound/jack.h b/include/sound/jack.h index 2182350..433b13b 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; @@ -86,6 +88,7 @@ struct snd_jack { int snd_jack_new(struct snd_card *card, const char *id, int type, struct snd_jack **jack); +int snd_jack_add_new_kctl(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 +96,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_kctl(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..db69ecc 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -24,6 +24,13 @@ #include #include #include +#include + +struct snd_jack_kctl { + struct snd_kcontrol *kctl; + struct list_head list; /* list of controls belong to the same jack */ + unsigned int mask_bits; /* only masked status bits are reported via kctl */ +}; static int jack_switch_types[SND_JACK_SWITCH_TYPES] = { SW_HEADPHONE_INSERT, @@ -54,7 +61,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 +113,77 @@ static int snd_jack_dev_register(struct snd_device *device) return err; } +static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl) +{ + struct snd_jack_kctl *jack_kctl; + + jack_kctl = kctl->private_data; + if (jack_kctl) { + list_del(&jack_kctl->list); + kfree(jack_kctl); + } +} + +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); +} + +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 err; + + kctl = snd_kctl_jack_new(name, 0, card); + if (!kctl) + return NULL; + + err = snd_ctl_add(card, kctl); + if (err < 0) + 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; + + return jack_kctl; +error: + snd_ctl_free_one(kctl); + return NULL; +} + +/** + * snd_jack_add_new_kctl - 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 adds it to the jack kctl_list. + * + * Return: Zero if successful, or a negative error code on failure. + */ +int snd_jack_add_new_kctl(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_kctl); + /** * snd_jack_new - Create a new jack * @card: the card instance @@ -150,6 +234,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 +317,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) @@ -252,6 +340,11 @@ void snd_jack_report(struct snd_jack *jack, int status) } 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 a5ed1c1..4d3d474 100644 --- a/sound/pci/hda/Kconfig +++ b/sound/pci/hda/Kconfig @@ -4,7 +4,7 @@ config SND_HDA tristate select SND_PCM select SND_VMASTER - select SND_KCTL_JACK + select SND_JACK select SND_HDA_CORE config SND_HDA_INTEL -- cgit v0.10.2 From b8dd086674cfbfc246a5b9d7d7ff37f62350a878 Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:20:57 +0800 Subject: 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 Signed-off-by: Takashi Iwai diff --git a/include/sound/control.h b/include/sound/control.h index 95aad6d..f50e2e9 100644 --- a/include/sound/control.h +++ b/include/sound/control.h @@ -252,7 +252,7 @@ void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only); * Helper functions for jack-detection controls */ struct snd_kcontrol * -snd_kctl_jack_new(const char *name, int idx, void *private_data); +snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card); void snd_kctl_jack_report(struct snd_card *card, struct snd_kcontrol *kctl, bool status); diff --git a/sound/core/ctljack.c b/sound/core/ctljack.c index e4b38fb..df7c6cd 100644 --- a/sound/core/ctljack.c +++ b/sound/core/ctljack.c @@ -31,15 +31,46 @@ static struct snd_kcontrol_new jack_detect_kctl = { .get = jack_detect_kctl_get, }; +static int 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 jack_kctl_name_gen(char *name, const char *src_name, int size) +{ + size_t count = strlen(src_name); + bool need_cat = true; + + /* remove redundant " Jack" from src_name */ + if (count >= 5) + need_cat = strncmp(&src_name[count - 5], " Jack", 5) ? true : false; + + snprintf(name, size, need_cat ? "%s Jack" : "%s", src_name); + +} + struct snd_kcontrol * -snd_kctl_jack_new(const char *name, int idx, void *private_data) +snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card) { struct snd_kcontrol *kctl; + kctl = snd_ctl_new1(&jack_detect_kctl, private_data); if (!kctl) return NULL; - snprintf(kctl->id.name, sizeof(kctl->id.name), "%s Jack", name); - kctl->id.index = idx; + + jack_kctl_name_gen(kctl->id.name, name, sizeof(kctl->id.name)); + kctl->id.index = get_available_index(card, name); kctl->private_value = 0; return kctl; } diff --git a/sound/core/jack.c b/sound/core/jack.c index db69ecc..067d37d 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -135,7 +135,7 @@ static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const cha struct snd_jack_kctl *jack_kctl; int err; - kctl = snd_kctl_jack_new(name, 0, card); + kctl = snd_kctl_jack_new(name, card, card); if (!kctl) return NULL; diff --git a/sound/pci/hda/hda_jack.c b/sound/pci/hda/hda_jack.c index d7cfe7b..05b49aa 100644 --- a/sound/pci/hda/hda_jack.c +++ b/sound/pci/hda/hda_jack.c @@ -402,7 +402,7 @@ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, return 0; if (jack->kctl) return 0; /* already created */ - kctl = snd_kctl_jack_new(name, idx, codec); + kctl = snd_kctl_jack_new(name, codec, codec->card); if (!kctl) return -ENOMEM; err = snd_hda_ctl_add(codec, nid, kctl); -- cgit v0.10.2 From 4e3f0dc65883cac95807549b2f7a3ac183686bcb Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:20:58 +0800 Subject: ALSA: jack: extend snd_jack_new to support phantom jack Dont create input devices for phantom jacks. Here, we extend snd_jack_new() to support phantom jack creating: pass in a bool param for [non-]phantom flag, and a bool param initial_jack to indicate whether we need to create a kctl at this stage. We can also add a kctl to the jack after its created meaning we can now integrate the HDA and ASoC jacks. Signed-off-by: Jie Yang Signed-off-by: Takashi Iwai diff --git a/include/sound/jack.h b/include/sound/jack.h index 433b13b..23bede1 100644 --- a/include/sound/jack.h +++ b/include/sound/jack.h @@ -87,7 +87,7 @@ struct snd_jack { #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 initial_kctl, bool phantom_jack); int snd_jack_add_new_kctl(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, @@ -97,7 +97,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 initial_kctl, bool phantom_jack) { return 0; } diff --git a/sound/core/jack.c b/sound/core/jack.c index 067d37d..b34187b 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -191,6 +191,8 @@ EXPORT_SYMBOL(snd_jack_add_new_kctl); * @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. + * @initial_kctl: if true, create a kcontrol and add it to the jack list. + * @phantom_jack: Don't create a input device for phantom jacks. * * Creates a new jack object. * @@ -198,9 +200,10 @@ EXPORT_SYMBOL(snd_jack_add_new_kctl); * 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, bool initial_kctl, bool phantom_jack) { struct snd_jack *jack; + struct snd_jack_kctl *jack_kctl = NULL; int err; int i; static struct snd_device_ops ops = { @@ -209,26 +212,36 @@ int snd_jack_new(struct snd_card *card, const char *id, int type, .dev_disconnect = snd_jack_dev_disconnect, }; + if (initial_kctl) { + jack_kctl = snd_jack_kctl_new(card, id, type); + if (!jack_kctl) + return -ENOMEM; + } + jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL); if (jack == NULL) return -ENOMEM; jack->id = kstrdup(id, GFP_KERNEL); - jack->input_dev = input_allocate_device(); - if (jack->input_dev == NULL) { - err = -ENOMEM; - goto fail_input; - } + /* don't creat input device for phantom jack */ + if (!phantom_jack) { + jack->input_dev = input_allocate_device(); + if (jack->input_dev == NULL) { + err = -ENOMEM; + goto fail_input; + } - jack->input_dev->phys = "ALSA"; + jack->input_dev->phys = "ALSA"; - jack->type = type; + jack->type = type; - for (i = 0; i < SND_JACK_SWITCH_TYPES; i++) - if (type & (1 << i)) - input_set_capability(jack->input_dev, EV_SW, - jack_switch_types[i]); + for (i = 0; i < SND_JACK_SWITCH_TYPES; i++) + if (type & (1 << i)) + input_set_capability(jack->input_dev, EV_SW, + jack_switch_types[i]); + + } err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops); if (err < 0) @@ -237,6 +250,9 @@ int snd_jack_new(struct snd_card *card, const char *id, int type, jack->card = card; INIT_LIST_HEAD(&jack->kctl_list); + if (initial_kctl) + snd_jack_kctl_add(jack, jack_kctl); + *jjack = jack; return 0; diff --git a/sound/pci/hda/hda_jack.c b/sound/pci/hda/hda_jack.c index 05b49aa..16281c6 100644 --- a/sound/pci/hda/hda_jack.c +++ b/sound/pci/hda/hda_jack.c @@ -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->card, name, jack->type, - &jack->jack); + &jack->jack, false, false); if (err < 0) return err; jack->jack->private_data = jack; diff --git a/sound/pci/oxygen/xonar_wm87x6.c b/sound/pci/oxygen/xonar_wm87x6.c index 6ce6860..90ac479 100644 --- a/sound/pci/oxygen/xonar_wm87x6.c +++ b/sound/pci/oxygen/xonar_wm87x6.c @@ -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, false); xonar_ds_handle_hp_jack(chip); snd_component_add(chip->card, "WM8776"); diff --git a/sound/soc/soc-jack.c b/sound/soc/soc-jack.c index 9f60c25..70a9bdd 100644 --- a/sound/soc/soc-jack.c +++ b/sound/soc/soc-jack.c @@ -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, false); if (ret) return ret; -- cgit v0.10.2 From 2ba2dfa1fcc7ce5d2bf1716ec3d32b6fa0882e68 Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:20:59 +0800 Subject: ALSA: hda - Update to use the new jack kctls method Jack snd_kcontrols can now be created during snd_jack_new() or by later calling snd_jack_add_new_kctls(). This patch creates the jacks during the initialisation stage for both phantom and non phantom jacks. Signed-off-by: Jie Yang Signed-off-by: Takashi Iwai diff --git a/include/sound/control.h b/include/sound/control.h index f50e2e9..21d047f 100644 --- a/include/sound/control.h +++ b/include/sound/control.h @@ -252,7 +252,7 @@ void snd_ctl_sync_vmaster(struct snd_kcontrol *kctl, bool hook_only); * Helper functions for jack-detection controls */ struct snd_kcontrol * -snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card); +snd_kctl_jack_new(const char *name, struct snd_card *card); void snd_kctl_jack_report(struct snd_card *card, struct snd_kcontrol *kctl, bool status); diff --git a/sound/core/ctljack.c b/sound/core/ctljack.c index df7c6cd..6435e82 100644 --- a/sound/core/ctljack.c +++ b/sound/core/ctljack.c @@ -61,11 +61,11 @@ static void jack_kctl_name_gen(char *name, const char *src_name, int size) } struct snd_kcontrol * -snd_kctl_jack_new(const char *name, void *private_data, struct snd_card *card) +snd_kctl_jack_new(const char *name, struct snd_card *card) { struct snd_kcontrol *kctl; - kctl = snd_ctl_new1(&jack_detect_kctl, private_data); + kctl = snd_ctl_new1(&jack_detect_kctl, NULL); if (!kctl) return NULL; diff --git a/sound/core/jack.c b/sound/core/jack.c index b34187b..e8b51f5 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -135,7 +135,7 @@ static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const cha struct snd_jack_kctl *jack_kctl; int err; - kctl = snd_kctl_jack_new(name, card, card); + kctl = snd_kctl_jack_new(name, card); if (!kctl) return NULL; diff --git a/sound/pci/hda/hda_jack.c b/sound/pci/hda/hda_jack.c index 16281c6..366efbf 100644 --- a/sound/pci/hda/hda_jack.c +++ b/sound/pci/hda/hda_jack.c @@ -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->card, jack->jack); -#endif + for (cb = jack->callback; cb; cb = next) { next = cb->next; kfree(cb); @@ -337,20 +337,15 @@ 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 || jack->block_report) continue; state = get_jack_plug_state(jack->pin_sense); - snd_kctl_jack_report(codec->card, jack->kctl, state); -#ifdef CONFIG_SND_HDA_INPUT_JACK - if (jack->jack) - snd_jack_report(jack->jack, - state ? jack->type : 0); -#endif + snd_jack_report(jack->jack, + state ? jack->type : 0); } } 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) { @@ -377,54 +372,42 @@ static void hda_free_jack_priv(struct snd_jack *jack) jacks->nid = 0; jacks->jack = NULL; } -#endif /** * snd_hda_jack_add_kctl - Add a kctl for the given pin * @codec: the HDA codec * @nid: pin NID to assign * @name: string name for the jack - * @idx: index number for the jack * @phantom_jack: flag to deal as a phantom jack * * This assigns a jack-detection kctl to the given pin. The kcontrol * will have the given name and index. */ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, - const char *name, int idx, bool phantom_jack) + const char *name, 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) return 0; /* already created */ - kctl = snd_kctl_jack_new(name, codec, codec->card); - if (!kctl) - return -ENOMEM; - err = snd_hda_ctl_add(codec, nid, kctl); + + type = get_input_jack_type(codec, nid); + err = snd_jack_new(codec->card, name, type, + &jack->jack, true, phantom_jack); if (err < 0) return err; - jack->kctl = kctl; - jack->phantom_jack = !!phantom_jack; + jack->phantom_jack = !!phantom_jack; + jack->type = type; + jack->jack->private_data = jack; + jack->jack->private_free = hda_free_jack_priv; state = snd_hda_jack_detect(codec, nid); - snd_kctl_jack_report(codec->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->card, name, jack->type, - &jack->jack, false, false); - 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; } @@ -433,44 +416,23 @@ static int __snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, * @codec: the HDA codec * @nid: pin NID * @name: the name string for the jack ctl - * @idx: the ctl index for the jack ctl * * This is a simple helper calling __snd_hda_jack_add_kctl(). */ int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, - const char *name, int idx) + const char *name) { - return __snd_hda_jack_add_kctl(codec, nid, name, idx, false); + return __snd_hda_jack_add_kctl(codec, nid, name, false); } EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl); -/* get the unique index number for the given kctl name */ -static int get_unique_index(struct hda_codec *codec, const char *name, int idx) -{ - struct hda_jack_tbl *jack; - int i, len = strlen(name); - again: - 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) { - idx++; - goto again; - } - } - return idx; -} - static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, const struct auto_pin_cfg *cfg, const char *base_name) { unsigned int def_conf, conn; char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; - int idx, err; + int err; bool phantom_jack; if (!nid) @@ -482,16 +444,14 @@ static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid, phantom_jack = (conn != AC_JACK_PORT_COMPLEX) || !is_jack_detectable(codec, nid); - if (base_name) { + if (base_name) strlcpy(name, base_name, sizeof(name)); - idx = 0; - } else - snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), &idx); + else + snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL); if (phantom_jack) /* Example final name: "Internal Mic Phantom Jack" */ strncat(name, " Phantom", sizeof(name) - strlen(name) - 1); - idx = get_unique_index(codec, name, idx); - err = __snd_hda_jack_add_kctl(codec, nid, name, idx, phantom_jack); + err = __snd_hda_jack_add_kctl(codec, nid, name, phantom_jack); if (err < 0) return err; diff --git a/sound/pci/hda/hda_jack.h b/sound/pci/hda/hda_jack.h index b279e32..387d309 100644 --- a/sound/pci/hda/hda_jack.h +++ b/sound/pci/hda/hda_jack.h @@ -39,11 +39,8 @@ 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 *jack; -#endif }; struct hda_jack_tbl * @@ -85,7 +82,7 @@ static inline bool snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid) bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid); int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid, - const char *name, int idx); + const char *name); int snd_hda_jack_add_kctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg); diff --git a/sound/pci/hda/patch_hdmi.c b/sound/pci/hda/patch_hdmi.c index 5f44f60..67a85ca 100644 --- a/sound/pci/hda/patch_hdmi.c +++ b/sound/pci/hda/patch_hdmi.c @@ -2081,7 +2081,7 @@ static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx) strncat(hdmi_str, " Phantom", sizeof(hdmi_str) - strlen(hdmi_str) - 1); - return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0); + return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str); } static int generic_hdmi_build_controls(struct hda_codec *codec) -- cgit v0.10.2 From f63e8581e277cc0561ed9f93878c674c1a36712a Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:21:00 +0800 Subject: ASoC: jack: create kctls according to jack pins info Jack kctls are now created according to the pin info. Signed-off-by: Jie Yang Acked-by: Mark Brown Signed-off-by: Takashi Iwai diff --git a/sound/soc/soc-jack.c b/sound/soc/soc-jack.c index 70a9bdd..87ca980 100644 --- a/sound/soc/soc-jack.c +++ b/sound/soc/soc-jack.c @@ -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_kctl(jack->jack, pins[i].pin, pins[i].mask); } /* Update to reflect the last reported status; canned jack -- cgit v0.10.2 From 807845e3641d3dc4c41ec84c5e576250c7218abf Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:21:01 +0800 Subject: ALSA: jack: remove exporting ctljack functions snd_kctl_jack_new() and snd_kctl_jack_report() are internal only now so make them static. Signed-off-by: Jie Yang Signed-off-by: Takashi Iwai diff --git a/sound/core/ctljack.c b/sound/core/ctljack.c index 6435e82..8f8d103 100644 --- a/sound/core/ctljack.c +++ b/sound/core/ctljack.c @@ -74,7 +74,6 @@ snd_kctl_jack_new(const char *name, struct snd_card *card) 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) @@ -84,4 +83,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); -- cgit v0.10.2 From 12e180a27f3e066a4ed4a446d428fd117f168beb Mon Sep 17 00:00:00 2001 From: Jie Yang Date: Mon, 27 Apr 2015 21:21:02 +0800 Subject: ALSA: Docs: Add documentation for Jack kcontrols Add documentation describing Jack kcontrols and how to use them with HD-Audio and ASoC. Signed-off-by: Jie Yang Signed-off-by: Takashi Iwai diff --git a/Documentation/sound/alsa/Jack-Controls.txt b/Documentation/sound/alsa/Jack-Controls.txt new file mode 100644 index 0000000..fe1c5e0 --- /dev/null +++ b/Documentation/sound/alsa/Jack-Controls.txt @@ -0,0 +1,43 @@ +Why we need Jack kcontrols +========================== + +ALSA uses kcontrols to export audio controls(switch, volume, Mux, ...) +to user space. This means userspace applications like pulseaudio can +switch off headphones and switch on speakers when no headphones are +pluged in. + +The old ALSA jack code only created input devices for each registered +jack. These jack input devices are not readable by userspace devices +that run as non root. + +The new jack code creates embedded jack kcontrols for each jack that +can be read by any process. + +This can be combined with UCM to allow userspace to route audio more +intelligently based on jack insertion or removal events. + +Jack Kcontrol Internals +======================= + +Each jack will have a kcontrol list, so that we can create a kcontrol +and attach it to the jack, at jack creation stage. We can also add a +kcontrol to an existing jack, at anytime when required. + +Those kcontrols will be freed automatically when the Jack is freed. + +How to use jack kcontrols +========================= + +In order to keep compatibility, snd_jack_new() has been modified by +adding two params :- + + - @initial_kctl: if true, create a kcontrol and add it to the jack + list. + - @phantom_jack: Don't create a input device for phantom jacks. + +HDA jacks can set phantom_jack to true in order to create a phantom +jack and set initial_kctl to true to create an initial kcontrol with +the correct id. + +ASoC jacks should set initial_kctl as false. The pin name will be +assigned as the jack kcontrol name. -- cgit v0.10.2