]> git.alsa-project.org Git - alsa-lib.git/blob - src/topology/parser.c
874ec524c545c68efd4bbc6671afc28f73894834
[alsa-lib.git] / src / topology / parser.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 "tplg_local.h"
21 #include <sys/stat.h>
22
23 /*
24  * Get integer value
25  */
26 int tplg_get_integer(snd_config_t *n, int *val, int base)
27 {
28         const char *str;
29         long lval;
30         int err;
31
32         switch (snd_config_get_type(n)) {
33         case SND_CONFIG_TYPE_INTEGER:
34                 err = snd_config_get_integer(n, &lval);
35                 if (err < 0)
36                         return err;
37                 goto __retval;
38         case SND_CONFIG_TYPE_STRING:
39                 err = snd_config_get_string(n, &str);
40                 if (err < 0)
41                         return err;
42                 err = safe_strtol_base(str, &lval, base);
43                 if (err < 0)
44                         return err;
45                 goto __retval;
46         default:
47                 return -EINVAL;
48         }
49   __retval:
50         if (lval < INT_MIN || lval > INT_MAX)
51                 return -ERANGE;
52         *val = lval;
53         return 0;
54 }
55
56 /*
57  * Get unsigned integer value
58  */
59 int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base)
60 {
61         const char *str;
62         long lval;
63         long long llval;
64         unsigned long uval;
65         int err;
66
67         switch (snd_config_get_type(n)) {
68         case SND_CONFIG_TYPE_INTEGER:
69                 err = snd_config_get_integer(n, &lval);
70                 if (err < 0)
71                         return err;
72                 if (lval < 0 && lval >= INT_MIN)
73                         lval = UINT_MAX + lval + 1;
74                 if (lval < 0 || lval > UINT_MAX)
75                         return -ERANGE;
76                 *val = lval;
77                 return err;
78         case SND_CONFIG_TYPE_INTEGER64:
79                 err = snd_config_get_integer64(n, &llval);
80                 if (err < 0)
81                         return err;
82                 if (llval < 0 && llval >= INT_MIN)
83                         llval = UINT_MAX + llval + 1;
84                 if (llval < 0 || llval > UINT_MAX)
85                         return -ERANGE;
86                 *val = llval;
87                 return err;
88         case SND_CONFIG_TYPE_STRING:
89                 err = snd_config_get_string(n, &str);
90                 if (err < 0)
91                         return err;
92                 errno = 0;
93                 uval = strtoul(str, NULL, base);
94                 if (errno == ERANGE && uval == ULONG_MAX)
95                         return -ERANGE;
96                 if (errno && uval == 0)
97                         return -EINVAL;
98                 if (uval > UINT_MAX)
99                         return -ERANGE;
100                 *val = uval;
101                 return 0;
102         default:
103                 return -EINVAL;
104         }
105 }
106
107 /*
108  * Parse compound
109  */
110 int tplg_parse_compound(snd_tplg_t *tplg, snd_config_t *cfg,
111                         int (*fcn)(snd_tplg_t *, snd_config_t *, void *),
112                         void *private)
113 {
114         const char *id;
115         snd_config_iterator_t i, next;
116         snd_config_t *n;
117         int err = -EINVAL;
118
119         if (snd_config_get_id(cfg, &id) < 0)
120                 return -EINVAL;
121
122         if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
123                 SNDERR("compound type expected for %s", id);
124                 return -EINVAL;
125         }
126
127         /* parse compound */
128         snd_config_for_each(i, next, cfg) {
129                 n = snd_config_iterator_entry(i);
130
131                 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
132                         SNDERR("compound type expected for %s, is %d",
133                                 id, snd_config_get_type(cfg));
134                         return -EINVAL;
135                 }
136
137                 err = fcn(tplg, n, private);
138                 if (err < 0)
139                         return err;
140         }
141
142         return err;
143 }
144
145 static int tplg_parse_config(snd_tplg_t *tplg, snd_config_t *cfg)
146 {
147         int (*parser)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
148         snd_config_iterator_t i, next;
149         snd_config_t *n;
150         const char *id;
151         struct tplg_table *p;
152         unsigned int idx;
153         int err;
154
155         if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
156                 SNDERR("compound type expected at top level");
157                 return -EINVAL;
158         }
159
160         /* parse topology config sections */
161         snd_config_for_each(i, next, cfg) {
162
163                 n = snd_config_iterator_entry(i);
164                 if (snd_config_get_id(n, &id) < 0)
165                         continue;
166
167                 parser = NULL;
168                 for (idx = 0; idx < tplg_table_items; idx++) {
169                         p = &tplg_table[idx];
170                         if (p->id && strcmp(id, p->id) == 0) {
171                                 parser = p->parse;
172                                 break;
173                         }
174                         if (p->id2 && strcmp(id, p->id2) == 0) {
175                                 parser = p->parse;
176                                 break;
177                         }
178                 }
179
180                 if (parser == NULL) {
181                         SNDERR("unknown section %s", id);
182                         continue;
183                 }
184
185                 err = tplg_parse_compound(tplg, n, parser, NULL);
186                 if (err < 0)
187                         return err;
188         }
189         return 0;
190 }
191
192 static int tplg_load_config(snd_tplg_t *tplg, snd_input_t *in)
193 {
194         snd_config_t *top;
195         int ret;
196
197         ret = snd_config_top(&top);
198         if (ret < 0)
199                 return ret;
200
201         ret = snd_config_load(top, in);
202         if (ret < 0) {
203                 SNDERR("could not load configuration");
204                 snd_config_delete(top);
205                 return ret;
206         }
207
208         ret = tplg_parse_config(tplg, top);
209         snd_config_delete(top);
210         if (ret < 0) {
211                 SNDERR("failed to parse topology");
212                 return ret;
213         }
214
215         return 0;
216 }
217
218 static int tplg_build_integ(snd_tplg_t *tplg)
219 {
220         int err;
221
222         err = tplg_build_data(tplg);
223         if (err <  0)
224                 return err;
225
226         err = tplg_build_manifest_data(tplg);
227         if (err <  0)
228                 return err;
229
230         err = tplg_build_controls(tplg);
231         if (err <  0)
232                 return err;
233
234         err = tplg_build_widgets(tplg);
235         if (err <  0)
236                 return err;
237
238         err = tplg_build_pcms(tplg, SND_TPLG_TYPE_PCM);
239         if (err <  0)
240                 return err;
241
242         err = tplg_build_dais(tplg, SND_TPLG_TYPE_DAI);
243         if (err <  0)
244                 return err;
245
246         err = tplg_build_links(tplg, SND_TPLG_TYPE_BE);
247         if (err <  0)
248                 return err;
249
250         err = tplg_build_links(tplg, SND_TPLG_TYPE_CC);
251         if (err <  0)
252                 return err;
253
254         err = tplg_build_routes(tplg);
255         if (err <  0)
256                 return err;
257
258         return err;
259 }
260
261 int snd_tplg_load(snd_tplg_t *tplg, const char *buf, size_t size)
262 {
263         snd_input_t *in;
264         int err;
265
266         err = snd_input_buffer_open(&in, buf, size);
267         if (err < 0) {
268                 SNDERR("could not create input buffer");
269                 return err;
270         }
271
272         err = tplg_load_config(tplg, in);
273         snd_input_close(in);
274         return err;
275 }
276
277 static int tplg_build(snd_tplg_t *tplg)
278 {
279         int err;
280
281         err = tplg_build_integ(tplg);
282         if (err < 0) {
283                 SNDERR("failed to check topology integrity");
284                 return err;
285         }
286
287         err = tplg_write_data(tplg);
288         if (err < 0) {
289                 SNDERR("failed to write data %d", err);
290                 return err;
291         }
292         return 0;
293 }
294
295 int snd_tplg_build_file(snd_tplg_t *tplg,
296                         const char *infile,
297                         const char *outfile)
298 {
299         FILE *fp;
300         snd_input_t *in;
301         int err;
302
303         fp = fopen(infile, "r");
304         if (fp == NULL) {
305                 SNDERR("could not open configuration file %s", infile);
306                 return -errno;
307         }
308
309         err = snd_input_stdio_attach(&in, fp, 1);
310         if (err < 0) {
311                 fclose(fp);
312                 SNDERR("could not attach stdio %s", infile);
313                 return err;
314         }
315
316         err = tplg_load_config(tplg, in);
317         snd_input_close(in);
318         if (err < 0)
319                 return err;
320
321         return snd_tplg_build(tplg, outfile);
322 }
323
324 int snd_tplg_add_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
325 {
326         switch (t->type) {
327         case SND_TPLG_TYPE_MIXER:
328                 return tplg_add_mixer_object(tplg, t);
329         case SND_TPLG_TYPE_ENUM:
330                 return tplg_add_enum_object(tplg, t);
331         case SND_TPLG_TYPE_BYTES:
332                 return tplg_add_bytes_object(tplg, t);
333         case SND_TPLG_TYPE_DAPM_WIDGET:
334                 return tplg_add_widget_object(tplg, t);
335         case SND_TPLG_TYPE_DAPM_GRAPH:
336                 return tplg_add_graph_object(tplg, t);
337         case SND_TPLG_TYPE_PCM:
338                 return tplg_add_pcm_object(tplg, t);
339         case SND_TPLG_TYPE_DAI:
340                 return tplg_add_dai_object(tplg, t);
341         case SND_TPLG_TYPE_LINK:
342         case SND_TPLG_TYPE_BE:
343         case SND_TPLG_TYPE_CC:
344                 return tplg_add_link_object(tplg, t);
345         default:
346                 SNDERR("invalid object type %d", t->type);
347                 return -EINVAL;
348         };
349 }
350
351 int snd_tplg_build(snd_tplg_t *tplg, const char *outfile)
352 {
353         int fd, err;
354         ssize_t r;
355
356         err = tplg_build(tplg);
357         if (err < 0)
358                 return err;
359
360         fd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
361         if (fd < 0) {
362                 SNDERR("failed to open %s err %d", outfile, -errno);
363                 return -errno;
364         }
365         r = write(fd, tplg->bin, tplg->bin_size);
366         close(fd);
367         if (r < 0) {
368                 err = -errno;
369                 SNDERR("write error: %s", strerror(errno));
370                 return err;
371         }
372         if ((size_t)r != tplg->bin_size) {
373                 SNDERR("partial write (%zd != %zd)", r, tplg->bin_size);
374                 return -EIO;
375         }
376         return 0;
377 }
378
379 int snd_tplg_build_bin(snd_tplg_t *tplg,
380                        void **bin, size_t *size)
381 {
382         int err;
383
384         err = tplg_build(tplg);
385         if (err < 0)
386                 return err;
387
388         *bin = tplg->bin;
389         *size = tplg->bin_size;
390         tplg->bin = NULL;
391         tplg->bin_size = tplg->bin_pos = 0;
392         return 0;
393 }
394
395 int snd_tplg_set_manifest_data(snd_tplg_t *tplg, const void *data, int len)
396 {
397         struct tplg_elem *elem;
398
399         elem = tplg_elem_type_lookup(tplg, SND_TPLG_TYPE_MANIFEST);
400         if (elem == NULL) {
401                 elem = tplg_elem_new_common(tplg, NULL, "manifest",
402                                             SND_TPLG_TYPE_MANIFEST);
403                 if (!elem)
404                         return -ENOMEM;
405                 tplg->manifest.size = elem->size;
406         }
407
408         if (len <= 0)
409                 return 0;
410
411         return tplg_add_data_bytes(tplg, elem, NULL, data, len);
412 }
413
414 int snd_tplg_set_version(snd_tplg_t *tplg, unsigned int version)
415 {
416         tplg->version = version;
417
418         return 0;
419 }
420
421 void snd_tplg_verbose(snd_tplg_t *tplg, int verbose)
422 {
423         tplg->verbose = verbose;
424 }
425
426 static bool is_little_endian(void)
427 {
428 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
429         return true;
430 #endif
431         return false;
432 }
433
434 snd_tplg_t *snd_tplg_create(int flags)
435 {
436         snd_tplg_t *tplg;
437
438         if (!is_little_endian()) {
439                 SNDERR("cannot support big-endian machines");
440                 return NULL;
441         }
442
443         tplg = calloc(1, sizeof(snd_tplg_t));
444         if (!tplg)
445                 return NULL;
446
447         tplg->verbose = !!(flags & SND_TPLG_CREATE_VERBOSE);
448         tplg->dapm_sort = (flags & SND_TPLG_CREATE_DAPM_NOSORT) == 0;
449
450         tplg->manifest.size = sizeof(struct snd_soc_tplg_manifest);
451
452         INIT_LIST_HEAD(&tplg->tlv_list);
453         INIT_LIST_HEAD(&tplg->widget_list);
454         INIT_LIST_HEAD(&tplg->pcm_list);
455         INIT_LIST_HEAD(&tplg->dai_list);
456         INIT_LIST_HEAD(&tplg->be_list);
457         INIT_LIST_HEAD(&tplg->cc_list);
458         INIT_LIST_HEAD(&tplg->route_list);
459         INIT_LIST_HEAD(&tplg->pdata_list);
460         INIT_LIST_HEAD(&tplg->manifest_list);
461         INIT_LIST_HEAD(&tplg->text_list);
462         INIT_LIST_HEAD(&tplg->pcm_config_list);
463         INIT_LIST_HEAD(&tplg->pcm_caps_list);
464         INIT_LIST_HEAD(&tplg->mixer_list);
465         INIT_LIST_HEAD(&tplg->enum_list);
466         INIT_LIST_HEAD(&tplg->bytes_ext_list);
467         INIT_LIST_HEAD(&tplg->token_list);
468         INIT_LIST_HEAD(&tplg->tuple_list);
469         INIT_LIST_HEAD(&tplg->hw_cfg_list);
470
471         return tplg;
472 }
473
474 snd_tplg_t *snd_tplg_new(void)
475 {
476         return snd_tplg_create(0);
477 }
478
479 void snd_tplg_free(snd_tplg_t *tplg)
480 {
481         free(tplg->bin);
482         free(tplg->manifest_pdata);
483
484         tplg_elem_free_list(&tplg->tlv_list);
485         tplg_elem_free_list(&tplg->widget_list);
486         tplg_elem_free_list(&tplg->pcm_list);
487         tplg_elem_free_list(&tplg->dai_list);
488         tplg_elem_free_list(&tplg->be_list);
489         tplg_elem_free_list(&tplg->cc_list);
490         tplg_elem_free_list(&tplg->route_list);
491         tplg_elem_free_list(&tplg->pdata_list);
492         tplg_elem_free_list(&tplg->manifest_list);
493         tplg_elem_free_list(&tplg->text_list);
494         tplg_elem_free_list(&tplg->pcm_config_list);
495         tplg_elem_free_list(&tplg->pcm_caps_list);
496         tplg_elem_free_list(&tplg->mixer_list);
497         tplg_elem_free_list(&tplg->enum_list);
498         tplg_elem_free_list(&tplg->bytes_ext_list);
499         tplg_elem_free_list(&tplg->token_list);
500         tplg_elem_free_list(&tplg->tuple_list);
501         tplg_elem_free_list(&tplg->hw_cfg_list);
502
503         free(tplg);
504 }
505
506 const char *snd_tplg_version(void)
507 {
508         return SND_LIB_VERSION_STR;
509 }