Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Helper functions for jack-detection kcontrols
0004  *
0005  * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/export.h>
0010 #include <sound/core.h>
0011 #include <sound/control.h>
0012 
0013 #define jack_detect_kctl_info   snd_ctl_boolean_mono_info
0014 
0015 static int jack_detect_kctl_get(struct snd_kcontrol *kcontrol,
0016                 struct snd_ctl_elem_value *ucontrol)
0017 {
0018     ucontrol->value.integer.value[0] = kcontrol->private_value;
0019     return 0;
0020 }
0021 
0022 static const struct snd_kcontrol_new jack_detect_kctl = {
0023     /* name is filled later */
0024     .iface = SNDRV_CTL_ELEM_IFACE_CARD,
0025     .access = SNDRV_CTL_ELEM_ACCESS_READ,
0026     .info = jack_detect_kctl_info,
0027     .get = jack_detect_kctl_get,
0028 };
0029 
0030 static int get_available_index(struct snd_card *card, const char *name)
0031 {
0032     struct snd_ctl_elem_id sid;
0033 
0034     memset(&sid, 0, sizeof(sid));
0035 
0036     sid.index = 0;
0037     sid.iface = SNDRV_CTL_ELEM_IFACE_CARD;
0038     strscpy(sid.name, name, sizeof(sid.name));
0039 
0040     while (snd_ctl_find_id(card, &sid)) {
0041         sid.index++;
0042         /* reset numid; otherwise snd_ctl_find_id() hits this again */
0043         sid.numid = 0;
0044     }
0045 
0046     return sid.index;
0047 }
0048 
0049 static void jack_kctl_name_gen(char *name, const char *src_name, int size)
0050 {
0051     size_t count = strlen(src_name);
0052     bool need_cat = true;
0053 
0054     /* remove redundant " Jack" from src_name */
0055     if (count >= 5)
0056         need_cat = strncmp(&src_name[count - 5], " Jack", 5) ? true : false;
0057 
0058     snprintf(name, size, need_cat ? "%s Jack" : "%s", src_name);
0059 
0060 }
0061 
0062 struct snd_kcontrol *
0063 snd_kctl_jack_new(const char *name, struct snd_card *card)
0064 {
0065     struct snd_kcontrol *kctl;
0066 
0067     kctl = snd_ctl_new1(&jack_detect_kctl, NULL);
0068     if (!kctl)
0069         return NULL;
0070 
0071     jack_kctl_name_gen(kctl->id.name, name, sizeof(kctl->id.name));
0072     kctl->id.index = get_available_index(card, kctl->id.name);
0073     kctl->private_value = 0;
0074     return kctl;
0075 }
0076 
0077 void snd_kctl_jack_report(struct snd_card *card,
0078               struct snd_kcontrol *kctl, bool status)
0079 {
0080     if (kctl->private_value == status)
0081         return;
0082     kctl->private_value = status;
0083     snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
0084 }