2 Copyright(c) 2014-2015 Intel Corporation
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.
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.
15 Authors: Mengdong Lin <mengdong.lin@intel.com>
16 Yao Jin <yao.jin@intel.com>
17 Liam Girdwood <liam.r.girdwood@linux.intel.com>
20 #include "tplg_local.h"
26 int tplg_get_integer(snd_config_t *n, int *val, int base)
32 switch (snd_config_get_type(n)) {
33 case SND_CONFIG_TYPE_INTEGER:
34 err = snd_config_get_integer(n, &lval);
38 case SND_CONFIG_TYPE_STRING:
39 err = snd_config_get_string(n, &str);
42 err = safe_strtol_base(str, &lval, base);
50 if (lval < INT_MIN || lval > INT_MAX)
57 * Get unsigned integer value
59 int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base)
67 switch (snd_config_get_type(n)) {
68 case SND_CONFIG_TYPE_INTEGER:
69 err = snd_config_get_integer(n, &lval);
72 if (lval < 0 && lval >= INT_MIN)
73 lval = UINT_MAX + lval + 1;
74 if (lval < 0 || lval > UINT_MAX)
78 case SND_CONFIG_TYPE_INTEGER64:
79 err = snd_config_get_integer64(n, &llval);
82 if (llval < 0 && llval >= INT_MIN)
83 llval = UINT_MAX + llval + 1;
84 if (llval < 0 || llval > UINT_MAX)
88 case SND_CONFIG_TYPE_STRING:
89 err = snd_config_get_string(n, &str);
93 uval = strtoul(str, NULL, base);
94 if (errno == ERANGE && uval == ULONG_MAX)
96 if (errno && uval == 0)
110 int tplg_parse_compound(snd_tplg_t *tplg, snd_config_t *cfg,
111 int (*fcn)(snd_tplg_t *, snd_config_t *, void *),
115 snd_config_iterator_t i, next;
119 if (snd_config_get_id(cfg, &id) < 0)
122 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
123 snd_error(TOPOLOGY, "compound type expected for %s", id);
128 snd_config_for_each(i, next, cfg) {
129 n = snd_config_iterator_entry(i);
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));
138 err = fcn(tplg, n, private);
146 static int tplg_parse_config(snd_tplg_t *tplg, snd_config_t *cfg)
148 int (*parser)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
149 snd_config_iterator_t i, next;
152 struct tplg_table *p;
156 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
157 snd_error(TOPOLOGY, "compound type expected at top level");
161 /* parse topology config sections */
162 snd_config_for_each(i, next, cfg) {
164 n = snd_config_iterator_entry(i);
165 if (snd_config_get_id(n, &id) < 0)
169 for (idx = 0; idx < tplg_table_items; idx++) {
170 p = &tplg_table[idx];
171 if (p->id && strcmp(id, p->id) == 0) {
175 if (p->id2 && strcmp(id, p->id2) == 0) {
181 if (parser == NULL) {
182 snd_error(TOPOLOGY, "unknown section %s", id);
186 err = tplg_parse_compound(tplg, n, parser, NULL);
193 static int tplg_load_config(snd_tplg_t *tplg, snd_input_t *in)
198 ret = snd_config_top(&top);
202 ret = snd_config_load(top, in);
204 snd_error(TOPOLOGY, "could not load configuration");
205 snd_config_delete(top);
209 ret = tplg_parse_config(tplg, top);
210 snd_config_delete(top);
212 snd_error(TOPOLOGY, "failed to parse topology");
219 static int tplg_build_integ(snd_tplg_t *tplg)
223 err = tplg_build_data(tplg);
227 err = tplg_build_manifest_data(tplg);
231 err = tplg_build_controls(tplg);
235 err = tplg_build_widgets(tplg);
239 err = tplg_build_pcms(tplg, SND_TPLG_TYPE_PCM);
243 err = tplg_build_dais(tplg, SND_TPLG_TYPE_DAI);
247 err = tplg_build_links(tplg, SND_TPLG_TYPE_BE);
251 err = tplg_build_links(tplg, SND_TPLG_TYPE_CC);
255 err = tplg_build_routes(tplg);
262 int snd_tplg_load(snd_tplg_t *tplg, const char *buf, size_t size)
267 err = snd_input_buffer_open(&in, buf, size);
269 snd_error(TOPOLOGY, "could not create input buffer");
273 err = tplg_load_config(tplg, in);
278 static int tplg_build(snd_tplg_t *tplg)
282 err = tplg_build_integ(tplg);
284 snd_error(TOPOLOGY, "failed to check topology integrity");
288 err = tplg_write_data(tplg);
290 snd_error(TOPOLOGY, "failed to write data %d", err);
296 int snd_tplg_build_file(snd_tplg_t *tplg,
304 fp = fopen(infile, "r");
306 snd_error(TOPOLOGY, "could not open configuration file %s", infile);
310 err = snd_input_stdio_attach(&in, fp, 1);
313 snd_error(TOPOLOGY, "could not attach stdio %s", infile);
317 err = tplg_load_config(tplg, in);
322 return snd_tplg_build(tplg, outfile);
325 int snd_tplg_add_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
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);
347 snd_error(TOPOLOGY, "invalid object type %d", t->type);
352 int snd_tplg_build(snd_tplg_t *tplg, const char *outfile)
357 err = tplg_build(tplg);
361 fd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
363 snd_error(TOPOLOGY, "failed to open %s err %d", outfile, -errno);
366 r = write(fd, tplg->bin, tplg->bin_size);
370 snd_error(TOPOLOGY, "write error: %s", strerror(errno));
373 if ((size_t)r != tplg->bin_size) {
374 snd_error(TOPOLOGY, "partial write (%zd != %zd)", r, tplg->bin_size);
380 int snd_tplg_build_bin(snd_tplg_t *tplg,
381 void **bin, size_t *size)
385 err = tplg_build(tplg);
390 *size = tplg->bin_size;
392 tplg->bin_size = tplg->bin_pos = 0;
396 int snd_tplg_set_manifest_data(snd_tplg_t *tplg, const void *data, int len)
398 struct tplg_elem *elem;
400 elem = tplg_elem_type_lookup(tplg, SND_TPLG_TYPE_MANIFEST);
402 elem = tplg_elem_new_common(tplg, NULL, "manifest",
403 SND_TPLG_TYPE_MANIFEST);
406 tplg->manifest.size = elem->size;
412 return tplg_add_data_bytes(tplg, elem, NULL, data, len);
415 int snd_tplg_set_version(snd_tplg_t *tplg, unsigned int version)
417 tplg->version = version;
422 void snd_tplg_verbose(snd_tplg_t *tplg, int verbose)
424 tplg->verbose = verbose;
427 static bool is_little_endian(void)
429 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
435 snd_tplg_t *snd_tplg_create(int flags)
439 if (!is_little_endian()) {
440 snd_error(TOPOLOGY, "cannot support big-endian machines");
444 tplg = calloc(1, sizeof(snd_tplg_t));
448 tplg->verbose = !!(flags & SND_TPLG_CREATE_VERBOSE);
449 tplg->dapm_sort = (flags & SND_TPLG_CREATE_DAPM_NOSORT) == 0;
451 tplg->manifest.size = sizeof(struct snd_soc_tplg_manifest);
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);
475 snd_tplg_t *snd_tplg_new(void)
477 return snd_tplg_create(0);
480 void snd_tplg_free(snd_tplg_t *tplg)
483 free(tplg->manifest_pdata);
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);
507 const char *snd_tplg_version(void)
509 return SND_LIB_VERSION_STR;