2 Copyright(c) 2014-2015 Intel Corporation
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 Authors: Mengdong Lin <mengdong.lin@intel.com>
16 Yao Jin <yao.jin@intel.com>
17 Liam Girdwood <liam.r.girdwood@linux.intel.com>
21 #include "tplg_local.h"
23 /* mapping of kcontrol text names to types */
24 static const struct map_elem control_map[] = {
25 {"volsw", SND_SOC_TPLG_CTL_VOLSW},
26 {"volsw_sx", SND_SOC_TPLG_CTL_VOLSW_SX},
27 {"volsw_xr_sx", SND_SOC_TPLG_CTL_VOLSW_XR_SX},
28 {"enum", SND_SOC_TPLG_CTL_ENUM},
29 {"bytes", SND_SOC_TPLG_CTL_BYTES},
30 {"enum_value", SND_SOC_TPLG_CTL_ENUM_VALUE},
31 {"range", SND_SOC_TPLG_CTL_RANGE},
32 {"strobe", SND_SOC_TPLG_CTL_STROBE},
35 static int lookup_ops(const char *c)
39 for (i = 0; i < ARRAY_SIZE(control_map); i++) {
40 if (strcmp(control_map[i].name, c) == 0)
41 return control_map[i].id;
44 /* cant find string name in our table so we use its ID number */
45 return strtol(c, NULL, 0);
48 const char *tplg_ops_name(int type)
52 for (i = 0; i < ARRAY_SIZE(control_map); i++) {
53 if (control_map[i].id == type)
54 return control_map[i].name;
60 /* Parse Control operations. Ops can come from standard names above or
61 * bespoke driver controls with numbers >= 256
63 int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, snd_config_t *cfg,
66 snd_config_iterator_t i, next;
68 struct snd_soc_tplg_ctl_hdr *hdr = private;
69 const char *id, *value;
73 hdr->size = sizeof(*hdr);
75 snd_config_for_each(i, next, cfg) {
77 n = snd_config_iterator_entry(i);
80 if (snd_config_get_id(n, &id) < 0)
83 /* get value - try strings then ints */
84 if (snd_config_get_type(n) == SND_CONFIG_TYPE_STRING) {
85 if (snd_config_get_string(n, &value) < 0)
87 ival = lookup_ops(value);
89 if (tplg_get_integer(n, &ival, 0))
93 if (strcmp(id, "info") == 0)
95 else if (strcmp(id, "put") == 0)
97 else if (strcmp(id, "get") == 0)
100 tplg_dbg("\t\t%s = %d", id, ival);
106 /* save control operations */
107 int tplg_save_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
108 struct snd_soc_tplg_ctl_hdr *hdr, char **dst,
114 if (hdr->ops.info + hdr->ops.get + hdr->ops.put == 0)
116 err = tplg_save_printf(dst, pfx, "ops.0 {\n");
117 if (err >= 0 && hdr->ops.info > 0) {
118 s = tplg_ops_name(hdr->ops.info);
120 err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
123 err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
125 if (err >= 0 && hdr->ops.get > 0) {
126 s = tplg_ops_name(hdr->ops.get);
128 err = tplg_save_printf(dst, pfx, "\tget %u\n",
131 err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
133 if (err >= 0 && hdr->ops.put > 0) {
134 s = tplg_ops_name(hdr->ops.put);
136 err = tplg_save_printf(dst, pfx, "\tput %u\n",
139 err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
142 err = tplg_save_printf(dst, pfx, "}\n");
146 /* Parse External Control operations. Ops can come from standard names above or
147 * bespoke driver controls with numbers >= 256
149 int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
150 snd_config_t *cfg, void *private)
152 snd_config_iterator_t i, next;
154 struct snd_soc_tplg_bytes_control *be = private;
155 const char *id, *value;
158 tplg_dbg("\tExt Ops");
160 snd_config_for_each(i, next, cfg) {
162 n = snd_config_iterator_entry(i);
165 if (snd_config_get_id(n, &id) < 0)
168 /* get value - try strings then ints */
169 if (snd_config_get_type(n) == SND_CONFIG_TYPE_STRING) {
170 if (snd_config_get_string(n, &value) < 0)
172 ival = lookup_ops(value);
174 if (tplg_get_integer(n, &ival, 0))
178 if (strcmp(id, "info") == 0)
179 be->ext_ops.info = ival;
180 else if (strcmp(id, "put") == 0)
181 be->ext_ops.put = ival;
182 else if (strcmp(id, "get") == 0)
183 be->ext_ops.get = ival;
185 tplg_dbg("\t\t%s = %s", id, value);
191 /* save external control operations */
192 int tplg_save_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
193 struct snd_soc_tplg_bytes_control *be,
194 char **dst, const char *pfx)
199 if (be->ext_ops.info + be->ext_ops.get + be->ext_ops.put == 0)
201 err = tplg_save_printf(dst, pfx, "extops.0 {\n");
202 if (err >= 0 && be->ext_ops.info > 0) {
203 s = tplg_ops_name(be->ext_ops.info);
205 err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
208 err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
210 if (err >= 0 && be->ext_ops.get > 0) {
211 s = tplg_ops_name(be->ext_ops.get);
213 err = tplg_save_printf(dst, pfx, "\tget %u\n",
216 err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
218 if (err >= 0 && be->ext_ops.put > 0) {
219 s = tplg_ops_name(be->ext_ops.put);
221 err = tplg_save_printf(dst, pfx, "\tput %u\n",
224 err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
227 err = tplg_save_printf(dst, pfx, "}\n");