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