/* Copyright(c) 2014-2015 Intel Corporation All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Authors: Mengdong Lin Yao Jin Liam Girdwood */ #include "list.h" #include "tplg_local.h" struct tplg_elem *lookup_pcm_dai_stream(struct list_head *base, const char* id) { struct list_head *pos; struct tplg_elem *elem; struct snd_soc_tplg_pcm *pcm; list_for_each(pos, base) { elem = list_entry(pos, struct tplg_elem, list); if (elem->type != SND_TPLG_TYPE_PCM) return NULL; pcm = elem->pcm; if (pcm && !strcmp(pcm->dai_name, id)) return elem; } return NULL; } /* copy referenced caps to the pcm */ static void copy_pcm_caps(const char *id, struct snd_soc_tplg_stream_caps *caps, struct tplg_elem *ref_elem) { struct snd_soc_tplg_stream_caps *ref_caps = ref_elem->stream_caps; tplg_dbg("Copy pcm caps (%ld bytes) from '%s' to '%s' \n", sizeof(*caps), ref_elem->id, id); *caps = *ref_caps; } /* check referenced config and caps for a pcm */ static int tplg_build_pcm_caps(snd_tplg_t *tplg, struct tplg_elem *elem) { struct tplg_elem *ref_elem = NULL; struct snd_soc_tplg_pcm *pcm; struct snd_soc_tplg_stream_caps *caps; struct snd_soc_tplg_stream *stream; unsigned int i; pcm = elem->pcm; for (i = 0; i < 2; i++) { caps = &pcm->caps[i]; ref_elem = tplg_elem_lookup(&tplg->pcm_caps_list, caps->name, SND_TPLG_TYPE_STREAM_CAPS); if (ref_elem != NULL) copy_pcm_caps(elem->id, caps, ref_elem); } return 0; } /* build FE DAI/PCM configurations */ int tplg_build_pcm(snd_tplg_t *tplg, unsigned int type) { struct list_head *base, *pos; struct tplg_elem *elem; int err = 0; base = &tplg->pcm_list; list_for_each(pos, base) { elem = list_entry(pos, struct tplg_elem, list); if (elem->type != type) { SNDERR("error: invalid elem '%s'\n", elem->id); return -EINVAL; } err = tplg_build_pcm_caps(tplg, elem); if (err < 0) return err; } return 0; } static int tplg_build_stream_cfg(snd_tplg_t *tplg, struct snd_soc_tplg_stream *stream, int num_streams) { struct snd_soc_tplg_stream *strm; struct tplg_elem *ref_elem; int i; for (i = 0; i < num_streams; i++) { strm = stream + i; ref_elem = tplg_elem_lookup(&tplg->pcm_config_list, strm->name, SND_TPLG_TYPE_STREAM_CONFIG); if (ref_elem && ref_elem->stream_cfg) *strm = *ref_elem->stream_cfg; } return 0; } /* build BE/CC DAI link configurations */ int tplg_build_link_cfg(snd_tplg_t *tplg, unsigned int type) { struct list_head *base, *pos; struct tplg_elem *elem; struct snd_soc_tplg_link_config *link; int err = 0; switch (type) { case SND_TPLG_TYPE_BE: base = &tplg->be_list; break; case SND_TPLG_TYPE_CC: base = &tplg->cc_list; break; default: return -EINVAL; } list_for_each(pos, base) { elem = list_entry(pos, struct tplg_elem, list); if (elem->type != type) { SNDERR("error: invalid elem '%s'\n", elem->id); return -EINVAL; } if (type == SND_TPLG_TYPE_BE) link = elem->be; else link = elem->cc; err = tplg_build_stream_cfg(tplg, link->stream, link->num_streams); if (err < 0) return err; } return 0; } static int split_format(struct snd_soc_tplg_stream_caps *caps, char *str) { char *s = NULL; snd_pcm_format_t format; int i = 0; s = strtok(str, ","); while ((s != NULL) && (i < SND_SOC_TPLG_MAX_FORMATS)) { format = snd_pcm_format_value(s); if (format == SND_PCM_FORMAT_UNKNOWN) { SNDERR("error: unsupported stream format %s\n", s); return -EINVAL; } caps->formats[i] = format; s = strtok(NULL, ", "); i++; } return 0; } /* Parse pcm Capabilities */ int tplg_parse_pcm_caps(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED) { struct snd_soc_tplg_stream_caps *sc; struct tplg_elem *elem; snd_config_iterator_t i, next; snd_config_t *n; const char *id, *val; char *s; int err; elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_STREAM_CAPS); if (!elem) return -ENOMEM; sc = elem->stream_caps; sc->size = elem->size; elem_copy_text(sc->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); tplg_dbg(" PCM Capabilities: %s\n", elem->id); snd_config_for_each(i, next, cfg) { n = snd_config_iterator_entry(i); if (snd_config_get_id(n, &id) < 0) continue; /* skip comments */ if (strcmp(id, "comment") == 0) continue; if (id[0] == '#') continue; if (snd_config_get_string(n, &val) < 0) return -EINVAL; if (strcmp(id, "formats") == 0) { s = strdup(val); if (s == NULL) return -ENOMEM; err = split_format(sc, s); free(s); if (err < 0) return err; tplg_dbg("\t\t%s: %s\n", id, val); continue; } if (strcmp(id, "rate_min") == 0) { sc->rate_min = atoi(val); tplg_dbg("\t\t%s: %d\n", id, sc->rate_min); continue; } if (strcmp(id, "rate_max") == 0) { sc->rate_max = atoi(val); tplg_dbg("\t\t%s: %d\n", id, sc->rate_max); continue; } if (strcmp(id, "channels_min") == 0) { sc->channels_min = atoi(val); tplg_dbg("\t\t%s: %d\n", id, sc->channels_min); continue; } if (strcmp(id, "channels_max") == 0) { sc->channels_max = atoi(val); tplg_dbg("\t\t%s: %d\n", id, sc->channels_max); continue; } } return 0; } /* Parse the caps of a pcm stream */ int tplg_parse_stream_caps(snd_tplg_t *tplg, snd_config_t *cfg, void *private) { snd_config_iterator_t i, next; snd_config_t *n; struct tplg_elem *elem = private; struct snd_soc_tplg_pcm *pcm; const char *id, *value; int err, stream; pcm = elem->pcm; snd_config_get_id(cfg, &id); tplg_dbg("\t%s:\n", id); if (strcmp(id, "playback") == 0) { stream = SND_SOC_TPLG_STREAM_PLAYBACK; pcm->playback = 1; } else if (strcmp(id, "capture") == 0) { stream = SND_SOC_TPLG_STREAM_CAPTURE; pcm->capture = 1; } else return -EINVAL; snd_config_for_each(i, next, cfg) { n = snd_config_iterator_entry(i); /* get id */ if (snd_config_get_id(n, &id) < 0) continue; if (strcmp(id, "capabilities") == 0) { if (snd_config_get_string(n, &value) < 0) continue; elem_copy_text(pcm->caps[stream].name, value, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); tplg_dbg("\t\t%s\n\t\t\t%s\n", id, value); continue; } } return 0; } /* Parse pcm */ int tplg_parse_pcm(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED) { struct snd_soc_tplg_pcm *pcm; struct tplg_elem *elem; snd_config_iterator_t i, next; snd_config_t *n; const char *id, *val = NULL; int err; elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_PCM); if (!elem) return -ENOMEM; pcm = elem->pcm; pcm->size = elem->size; elem_copy_text(pcm->dai_name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); tplg_dbg(" PCM: %s\n", elem->id); snd_config_for_each(i, next, cfg) { n = snd_config_iterator_entry(i); if (snd_config_get_id(n, &id) < 0) continue; /* skip comments */ if (strcmp(id, "comment") == 0) continue; if (id[0] == '#') continue; if (strcmp(id, "index") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; elem->index = atoi(val); tplg_dbg("\t%s: %d\n", id, elem->index); continue; } if (strcmp(id, "id") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; pcm->dai_id = atoi(val); tplg_dbg("\t%s: %d\n", id, pcm->dai_id); continue; } if (strcmp(id, "pcm") == 0) { err = tplg_parse_compound(tplg, n, tplg_parse_stream_caps, elem); if (err < 0) return err; continue; } } return 0; } int tplg_parse_be(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED) { struct snd_soc_tplg_link_config *link; struct tplg_elem *elem; snd_config_iterator_t i, next; snd_config_t *n; const char *id, *val = NULL; int err; elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_BE); if (!elem) return -ENOMEM; link = elem->be; link->size = elem->size; tplg_dbg(" BE: %s\n", elem->id); snd_config_for_each(i, next, cfg) { n = snd_config_iterator_entry(i); if (snd_config_get_id(n, &id) < 0) continue; /* skip comments */ if (strcmp(id, "comment") == 0) continue; if (id[0] == '#') continue; if (strcmp(id, "index") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; elem->index = atoi(val); tplg_dbg("\t%s: %d\n", id, elem->index); continue; } if (strcmp(id, "id") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; link->id = atoi(val); tplg_dbg("\t%s: %d\n", id, link->id); continue; } } return 0; } /* Parse cc */ int tplg_parse_cc(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED) { struct snd_soc_tplg_link_config *link; struct tplg_elem *elem; snd_config_iterator_t i, next; snd_config_t *n; const char *id, *val = NULL; int err; elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_CC); if (!elem) return -ENOMEM; link = elem->cc; link->size = elem->size; tplg_dbg(" CC: %s\n", elem->id); snd_config_for_each(i, next, cfg) { n = snd_config_iterator_entry(i); if (snd_config_get_id(n, &id) < 0) continue; /* skip comments */ if (strcmp(id, "comment") == 0) continue; if (id[0] == '#') continue; if (strcmp(id, "index") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; elem->index = atoi(val); tplg_dbg("\t%s: %d\n", id, elem->index); continue; } if (strcmp(id, "id") == 0) { if (snd_config_get_string(n, &val) < 0) return -EINVAL; link->id = atoi(val); tplg_dbg("\t%s: %d\n", id, link->id); continue; } } return 0; }