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