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)
31 err = snd_config_get_llong(n, &llval, base);
34 if (llval < INT_MIN || llval > INT_MAX)
41 * Get unsigned integer value
43 int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base)
48 err = snd_config_get_llong(n, &llval, base);
51 if (llval < 0 && llval >= INT_MIN)
52 llval = UINT_MAX + llval + 1;
53 if (llval < 0 || llval > UINT_MAX)
62 int tplg_parse_compound(snd_tplg_t *tplg, snd_config_t *cfg,
63 int (*fcn)(snd_tplg_t *, snd_config_t *, void *),
67 snd_config_iterator_t i, next;
71 if (snd_config_get_id(cfg, &id) < 0)
74 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
75 snd_error(TOPOLOGY, "compound type expected for %s", id);
80 snd_config_for_each(i, next, cfg) {
81 n = snd_config_iterator_entry(i);
83 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
84 snd_error(TOPOLOGY, "compound type expected for %s, is %d",
85 id, snd_config_get_type(cfg));
90 err = fcn(tplg, n, private);
98 static int tplg_parse_config(snd_tplg_t *tplg, snd_config_t *cfg)
100 int (*parser)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
101 snd_config_iterator_t i, next;
104 struct tplg_table *p;
108 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
109 snd_error(TOPOLOGY, "compound type expected at top level");
113 /* parse topology config sections */
114 snd_config_for_each(i, next, cfg) {
116 n = snd_config_iterator_entry(i);
117 if (snd_config_get_id(n, &id) < 0)
121 for (idx = 0; idx < tplg_table_items; idx++) {
122 p = &tplg_table[idx];
123 if (p->id && strcmp(id, p->id) == 0) {
127 if (p->id2 && strcmp(id, p->id2) == 0) {
133 if (parser == NULL) {
134 snd_error(TOPOLOGY, "unknown section %s", id);
138 err = tplg_parse_compound(tplg, n, parser, NULL);
145 static int tplg_load_config(snd_tplg_t *tplg, snd_input_t *in)
150 ret = snd_config_top(&top);
154 ret = snd_config_load(top, in);
156 snd_error(TOPOLOGY, "could not load configuration");
157 snd_config_delete(top);
161 ret = tplg_parse_config(tplg, top);
162 snd_config_delete(top);
164 snd_error(TOPOLOGY, "failed to parse topology");
171 static int tplg_build_integ(snd_tplg_t *tplg)
175 err = tplg_build_data(tplg);
179 err = tplg_build_manifest_data(tplg);
183 err = tplg_build_controls(tplg);
187 err = tplg_build_widgets(tplg);
191 err = tplg_build_pcms(tplg, SND_TPLG_TYPE_PCM);
195 err = tplg_build_dais(tplg, SND_TPLG_TYPE_DAI);
199 err = tplg_build_links(tplg, SND_TPLG_TYPE_BE);
203 err = tplg_build_links(tplg, SND_TPLG_TYPE_CC);
207 err = tplg_build_routes(tplg);
214 int snd_tplg_load(snd_tplg_t *tplg, const char *buf, size_t size)
219 err = snd_input_buffer_open(&in, buf, size);
221 snd_error(TOPOLOGY, "could not create input buffer");
225 err = tplg_load_config(tplg, in);
230 static int tplg_build(snd_tplg_t *tplg)
234 err = tplg_build_integ(tplg);
236 snd_error(TOPOLOGY, "failed to check topology integrity");
240 err = tplg_write_data(tplg);
242 snd_error(TOPOLOGY, "failed to write data %d", err);
248 int snd_tplg_build_file(snd_tplg_t *tplg,
256 fp = fopen(infile, "r");
258 snd_error(TOPOLOGY, "could not open configuration file %s", infile);
262 err = snd_input_stdio_attach(&in, fp, 1);
265 snd_error(TOPOLOGY, "could not attach stdio %s", infile);
269 err = tplg_load_config(tplg, in);
274 return snd_tplg_build(tplg, outfile);
277 int snd_tplg_add_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
280 case SND_TPLG_TYPE_MIXER:
281 return tplg_add_mixer_object(tplg, t);
282 case SND_TPLG_TYPE_ENUM:
283 return tplg_add_enum_object(tplg, t);
284 case SND_TPLG_TYPE_BYTES:
285 return tplg_add_bytes_object(tplg, t);
286 case SND_TPLG_TYPE_DAPM_WIDGET:
287 return tplg_add_widget_object(tplg, t);
288 case SND_TPLG_TYPE_DAPM_GRAPH:
289 return tplg_add_graph_object(tplg, t);
290 case SND_TPLG_TYPE_PCM:
291 return tplg_add_pcm_object(tplg, t);
292 case SND_TPLG_TYPE_DAI:
293 return tplg_add_dai_object(tplg, t);
294 case SND_TPLG_TYPE_LINK:
295 case SND_TPLG_TYPE_BE:
296 case SND_TPLG_TYPE_CC:
297 return tplg_add_link_object(tplg, t);
299 snd_error(TOPOLOGY, "invalid object type %d", t->type);
304 int snd_tplg_build(snd_tplg_t *tplg, const char *outfile)
309 err = tplg_build(tplg);
313 fd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
315 snd_error(TOPOLOGY, "failed to open %s err %d", outfile, -errno);
318 r = write(fd, tplg->bin, tplg->bin_size);
322 snd_error(TOPOLOGY, "write error: %s", strerror(errno));
325 if ((size_t)r != tplg->bin_size) {
326 snd_error(TOPOLOGY, "partial write (%zd != %zd)", r, tplg->bin_size);
332 int snd_tplg_build_bin(snd_tplg_t *tplg,
333 void **bin, size_t *size)
337 err = tplg_build(tplg);
342 *size = tplg->bin_size;
344 tplg->bin_size = tplg->bin_pos = 0;
348 int snd_tplg_set_manifest_data(snd_tplg_t *tplg, const void *data, int len)
350 struct tplg_elem *elem;
352 elem = tplg_elem_type_lookup(tplg, SND_TPLG_TYPE_MANIFEST);
354 elem = tplg_elem_new_common(tplg, NULL, "manifest",
355 SND_TPLG_TYPE_MANIFEST);
358 tplg->manifest.size = elem->size;
364 return tplg_add_data_bytes(tplg, elem, NULL, data, len);
367 int snd_tplg_set_version(snd_tplg_t *tplg, unsigned int version)
369 tplg->version = version;
374 void snd_tplg_verbose(snd_tplg_t *tplg, int verbose)
376 tplg->verbose = verbose;
379 static bool is_little_endian(void)
381 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
387 snd_tplg_t *snd_tplg_create(int flags)
391 if (!is_little_endian()) {
392 snd_error(TOPOLOGY, "cannot support big-endian machines");
396 tplg = calloc(1, sizeof(snd_tplg_t));
400 tplg->verbose = !!(flags & SND_TPLG_CREATE_VERBOSE);
401 tplg->dapm_sort = (flags & SND_TPLG_CREATE_DAPM_NOSORT) == 0;
403 tplg->manifest.size = sizeof(struct snd_soc_tplg_manifest);
405 INIT_LIST_HEAD(&tplg->tlv_list);
406 INIT_LIST_HEAD(&tplg->widget_list);
407 INIT_LIST_HEAD(&tplg->pcm_list);
408 INIT_LIST_HEAD(&tplg->dai_list);
409 INIT_LIST_HEAD(&tplg->be_list);
410 INIT_LIST_HEAD(&tplg->cc_list);
411 INIT_LIST_HEAD(&tplg->route_list);
412 INIT_LIST_HEAD(&tplg->pdata_list);
413 INIT_LIST_HEAD(&tplg->manifest_list);
414 INIT_LIST_HEAD(&tplg->text_list);
415 INIT_LIST_HEAD(&tplg->pcm_config_list);
416 INIT_LIST_HEAD(&tplg->pcm_caps_list);
417 INIT_LIST_HEAD(&tplg->mixer_list);
418 INIT_LIST_HEAD(&tplg->enum_list);
419 INIT_LIST_HEAD(&tplg->bytes_ext_list);
420 INIT_LIST_HEAD(&tplg->token_list);
421 INIT_LIST_HEAD(&tplg->tuple_list);
422 INIT_LIST_HEAD(&tplg->hw_cfg_list);
427 snd_tplg_t *snd_tplg_new(void)
429 return snd_tplg_create(0);
432 void snd_tplg_free(snd_tplg_t *tplg)
435 free(tplg->manifest_pdata);
437 tplg_elem_free_list(&tplg->tlv_list);
438 tplg_elem_free_list(&tplg->widget_list);
439 tplg_elem_free_list(&tplg->pcm_list);
440 tplg_elem_free_list(&tplg->dai_list);
441 tplg_elem_free_list(&tplg->be_list);
442 tplg_elem_free_list(&tplg->cc_list);
443 tplg_elem_free_list(&tplg->route_list);
444 tplg_elem_free_list(&tplg->pdata_list);
445 tplg_elem_free_list(&tplg->manifest_list);
446 tplg_elem_free_list(&tplg->text_list);
447 tplg_elem_free_list(&tplg->pcm_config_list);
448 tplg_elem_free_list(&tplg->pcm_caps_list);
449 tplg_elem_free_list(&tplg->mixer_list);
450 tplg_elem_free_list(&tplg->enum_list);
451 tplg_elem_free_list(&tplg->bytes_ext_list);
452 tplg_elem_free_list(&tplg->token_list);
453 tplg_elem_free_list(&tplg->tuple_list);
454 tplg_elem_free_list(&tplg->hw_cfg_list);
459 const char *snd_tplg_version(void)
461 return SND_LIB_VERSION_STR;