1 /*
2 Copyright(c) 2014-2015 Intel Corporation
3 All rights reserved.
4
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.
9
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.
14
15 Authors: Mengdong Lin <mengdong.lin@intel.com>
16 Yao Jin <yao.jin@intel.com>
17 Liam Girdwood <liam.r.girdwood@linux.intel.com>
18 */
19
20 #include "list.h"
21 #include "tplg_local.h"
22
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},
33 };
34
lookup_ops(const char * c)35 static int lookup_ops(const char *c)
36 {
37 int i;
38 long ret;
39
40 for (i = 0; i < (int)ARRAY_SIZE(control_map); i++) {
41 if (strcmp(control_map[i].name, c) == 0)
42 return control_map[i].id;
43 }
44
45 /* cant find string name in our table so we use its ID number */
46 i = safe_strtol(c, &ret);
47 if (i < 0) {
48 SNDERR("wrong kcontrol ops value string '%s'", c);
49 return i;
50 }
51
52 return ret;
53 }
54
tplg_ops_name(int type)55 const char *tplg_ops_name(int type)
56 {
57 unsigned int i;
58
59 for (i = 0; i < ARRAY_SIZE(control_map); i++) {
60 if (control_map[i].id == type)
61 return control_map[i].name;
62 }
63
64 return NULL;
65 }
66
67 /* Parse Control operations. Ops can come from standard names above or
68 * bespoke driver controls with numbers >= 256
69 */
tplg_parse_ops(snd_tplg_t * tplg ATTRIBUTE_UNUSED,snd_config_t * cfg,void * private)70 int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, snd_config_t *cfg,
71 void *private)
72 {
73 snd_config_iterator_t i, next;
74 snd_config_t *n;
75 struct snd_soc_tplg_ctl_hdr *hdr = private;
76 const char *id, *value;
77 int ival;
78
79 tplg_dbg("\tOps");
80 hdr->size = sizeof(*hdr);
81
82 snd_config_for_each(i, next, cfg) {
83
84 n = snd_config_iterator_entry(i);
85
86 /* get id */
87 if (snd_config_get_id(n, &id) < 0)
88 continue;
89
90 /* get value - try strings then ints */
91 if (snd_config_get_type(n) == SND_CONFIG_TYPE_STRING) {
92 if (snd_config_get_string(n, &value) < 0)
93 continue;
94 ival = lookup_ops(value);
95 } else {
96 if (tplg_get_integer(n, &ival, 0))
97 continue;
98 }
99
100 if (strcmp(id, "info") == 0)
101 hdr->ops.info = ival;
102 else if (strcmp(id, "put") == 0)
103 hdr->ops.put = ival;
104 else if (strcmp(id, "get") == 0)
105 hdr->ops.get = ival;
106
107 tplg_dbg("\t\t%s = %d", id, ival);
108 }
109
110 return 0;
111 }
112
113 /* save control operations */
tplg_save_ops(snd_tplg_t * tplg ATTRIBUTE_UNUSED,struct snd_soc_tplg_ctl_hdr * hdr,struct tplg_buf * dst,const char * pfx)114 int tplg_save_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
115 struct snd_soc_tplg_ctl_hdr *hdr,
116 struct tplg_buf *dst, const char *pfx)
117 {
118 const char *s;
119 int err;
120
121 if (hdr->ops.info + hdr->ops.get + hdr->ops.put == 0)
122 return 0;
123 err = tplg_save_printf(dst, pfx, "ops.0 {\n");
124 if (err >= 0 && hdr->ops.info > 0) {
125 s = tplg_ops_name(hdr->ops.info);
126 if (s == NULL)
127 err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
128 hdr->ops.info);
129 else
130 err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
131 }
132 if (err >= 0 && hdr->ops.get > 0) {
133 s = tplg_ops_name(hdr->ops.get);
134 if (s == NULL)
135 err = tplg_save_printf(dst, pfx, "\tget %u\n",
136 hdr->ops.get);
137 else
138 err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
139 }
140 if (err >= 0 && hdr->ops.put > 0) {
141 s = tplg_ops_name(hdr->ops.put);
142 if (s == NULL)
143 err = tplg_save_printf(dst, pfx, "\tput %u\n",
144 hdr->ops.put);
145 else
146 err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
147 }
148 if (err >= 0)
149 err = tplg_save_printf(dst, pfx, "}\n");
150 return err;
151 }
152
153 /* Parse External Control operations. Ops can come from standard names above or
154 * bespoke driver controls with numbers >= 256
155 */
tplg_parse_ext_ops(snd_tplg_t * tplg ATTRIBUTE_UNUSED,snd_config_t * cfg,void * private)156 int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
157 snd_config_t *cfg, void *private)
158 {
159 snd_config_iterator_t i, next;
160 snd_config_t *n;
161 struct snd_soc_tplg_bytes_control *be = private;
162 const char *id, *value;
163 int ival;
164
165 tplg_dbg("\tExt Ops");
166
167 snd_config_for_each(i, next, cfg) {
168
169 n = snd_config_iterator_entry(i);
170
171 /* get id */
172 if (snd_config_get_id(n, &id) < 0)
173 continue;
174
175 /* get value - try strings then ints */
176 if (snd_config_get_type(n) == SND_CONFIG_TYPE_STRING) {
177 if (snd_config_get_string(n, &value) < 0)
178 continue;
179 ival = lookup_ops(value);
180 } else {
181 if (tplg_get_integer(n, &ival, 0))
182 continue;
183 }
184
185 if (strcmp(id, "info") == 0)
186 be->ext_ops.info = ival;
187 else if (strcmp(id, "put") == 0)
188 be->ext_ops.put = ival;
189 else if (strcmp(id, "get") == 0)
190 be->ext_ops.get = ival;
191
192 tplg_dbg("\t\t%s = %s", id, value);
193 }
194
195 return 0;
196 }
197
198 /* save external control operations */
tplg_save_ext_ops(snd_tplg_t * tplg ATTRIBUTE_UNUSED,struct snd_soc_tplg_bytes_control * be,struct tplg_buf * dst,const char * pfx)199 int tplg_save_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
200 struct snd_soc_tplg_bytes_control *be,
201 struct tplg_buf *dst, const char *pfx)
202 {
203 const char *s;
204 int err;
205
206 if (be->ext_ops.info + be->ext_ops.get + be->ext_ops.put == 0)
207 return 0;
208 err = tplg_save_printf(dst, pfx, "extops.0 {\n");
209 if (err >= 0 && be->ext_ops.info > 0) {
210 s = tplg_ops_name(be->ext_ops.info);
211 if (s == NULL)
212 err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
213 be->ext_ops.info);
214 else
215 err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
216 }
217 if (err >= 0 && be->ext_ops.get > 0) {
218 s = tplg_ops_name(be->ext_ops.get);
219 if (s == NULL)
220 err = tplg_save_printf(dst, pfx, "\tget %u\n",
221 be->ext_ops.get);
222 else
223 err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
224 }
225 if (err >= 0 && be->ext_ops.put > 0) {
226 s = tplg_ops_name(be->ext_ops.put);
227 if (s == NULL)
228 err = tplg_save_printf(dst, pfx, "\tput %u\n",
229 be->ext_ops.put);
230 else
231 err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
232 }
233 if (err >= 0)
234 err = tplg_save_printf(dst, pfx, "}\n");
235 return err;
236 }
237