OSDN Git Service

110eef58851d08c59a70996ea004a13674decca3
[android-x86/external-alsa-lib.git] / src / topology / ops.c
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
35 static int lookup_ops(const char *c)
36 {
37         unsigned int i;
38
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;
42         }
43
44         /* cant find string name in our table so we use its ID number */
45         return strtol(c, NULL, 0);
46 }
47
48 const char *tplg_ops_name(int type)
49 {
50         unsigned int i;
51
52         for (i = 0; i < ARRAY_SIZE(control_map); i++) {
53                 if (control_map[i].id == type)
54                         return control_map[i].name;
55         }
56
57         return NULL;
58 }
59
60 /* Parse Control operations. Ops can come from standard names above or
61  * bespoke driver controls with numbers >= 256
62  */
63 int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, snd_config_t *cfg,
64                    void *private)
65 {
66         snd_config_iterator_t i, next;
67         snd_config_t *n;
68         struct snd_soc_tplg_ctl_hdr *hdr = private;
69         const char *id, *value;
70         int ival;
71
72         tplg_dbg("\tOps");
73         hdr->size = sizeof(*hdr);
74
75         snd_config_for_each(i, next, cfg) {
76
77                 n = snd_config_iterator_entry(i);
78
79                 /* get id */
80                 if (snd_config_get_id(n, &id) < 0)
81                         continue;
82
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)
86                                 continue;
87                         ival = lookup_ops(value);
88                 } else {
89                         if (tplg_get_integer(n, &ival, 0))
90                                 continue;
91                 }
92
93                 if (strcmp(id, "info") == 0)
94                         hdr->ops.info = ival;
95                 else if (strcmp(id, "put") == 0)
96                         hdr->ops.put = ival;
97                 else if (strcmp(id, "get") == 0)
98                         hdr->ops.get = ival;
99
100                 tplg_dbg("\t\t%s = %d", id, ival);
101         }
102
103         return 0;
104 }
105
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,
109                   const char *pfx)
110 {
111         const char *s;
112         int err;
113
114         if (hdr->ops.info + hdr->ops.get + hdr->ops.put == 0)
115                 return 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);
119                 if (s == NULL)
120                         err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
121                                                hdr->ops.info);
122                 else
123                         err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
124         }
125         if (err >= 0 && hdr->ops.get > 0) {
126                 s = tplg_ops_name(hdr->ops.get);
127                 if (s == NULL)
128                         err = tplg_save_printf(dst, pfx, "\tget %u\n",
129                                                hdr->ops.get);
130                 else
131                         err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
132         }
133         if (err >= 0 && hdr->ops.put > 0) {
134                 s = tplg_ops_name(hdr->ops.put);
135                 if (s == NULL)
136                         err = tplg_save_printf(dst, pfx, "\tput %u\n",
137                                                hdr->ops.put);
138                 else
139                         err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
140         }
141         if (err >= 0)
142                 err = tplg_save_printf(dst, pfx, "}\n");
143         return err;
144 }
145
146 /* Parse External Control operations. Ops can come from standard names above or
147  * bespoke driver controls with numbers >= 256
148  */
149 int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
150                        snd_config_t *cfg, void *private)
151 {
152         snd_config_iterator_t i, next;
153         snd_config_t *n;
154         struct snd_soc_tplg_bytes_control *be = private;
155         const char *id, *value;
156         int ival;
157
158         tplg_dbg("\tExt Ops");
159
160         snd_config_for_each(i, next, cfg) {
161
162                 n = snd_config_iterator_entry(i);
163
164                 /* get id */
165                 if (snd_config_get_id(n, &id) < 0)
166                         continue;
167
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)
171                                 continue;
172                         ival = lookup_ops(value);
173                 } else {
174                         if (tplg_get_integer(n, &ival, 0))
175                                 continue;
176                 }
177
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;
184
185                 tplg_dbg("\t\t%s = %s", id, value);
186         }
187
188         return 0;
189 }
190
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)
195 {
196         const char *s;
197         int err;
198
199         if (be->ext_ops.info + be->ext_ops.get + be->ext_ops.put == 0)
200                 return 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);
204                 if (s == NULL)
205                         err = tplg_save_printf(dst, pfx, "\tinfo %u\n",
206                                                be->ext_ops.info);
207                 else
208                         err = tplg_save_printf(dst, pfx, "\tinfo %s\n", s);
209         }
210         if (err >= 0 && be->ext_ops.get > 0) {
211                 s = tplg_ops_name(be->ext_ops.get);
212                 if (s == NULL)
213                         err = tplg_save_printf(dst, pfx, "\tget %u\n",
214                                                be->ext_ops.get);
215                 else
216                         err = tplg_save_printf(dst, pfx, "\tget %s\n", s);
217         }
218         if (err >= 0 && be->ext_ops.put > 0) {
219                 s = tplg_ops_name(be->ext_ops.put);
220                 if (s == NULL)
221                         err = tplg_save_printf(dst, pfx, "\tput %u\n",
222                                                be->ext_ops.put);
223                 else
224                         err = tplg_save_printf(dst, pfx, "\tput %s\n", s);
225         }
226         if (err >= 0)
227                 err = tplg_save_printf(dst, pfx, "}\n");
228         return err;
229 }