• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright(c) 2021 Intel Corporation
3   All rights reserved.
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17   The full GNU General Public License is included in this distribution
18   in the file called LICENSE.GPL.
19 */
20 #include <errno.h>
21 #include <stdio.h>
22 #include <alsa/asoundlib.h>
23 #include "topology.h"
24 #include "pre-processor.h"
25 
tplg_build_hw_cfg_object(struct tplg_pre_processor * tplg_pp,snd_config_t * obj_cfg,snd_config_t * parent)26 int tplg_build_hw_cfg_object(struct tplg_pre_processor *tplg_pp,
27 			       snd_config_t *obj_cfg, snd_config_t *parent)
28 {
29 	snd_config_t *hw_cfg, *obj;
30 	const char *name;
31 	int ret;
32 
33 	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);
34 
35 	name = tplg_object_get_name(tplg_pp, obj);
36 	if (!name)
37 		return -EINVAL;
38 
39 	ret = tplg_build_object_from_template(tplg_pp, obj_cfg, &hw_cfg, NULL, false);
40 	if (ret < 0)
41 		return ret;
42 
43 	return tplg_parent_update(tplg_pp, parent, "hw_configs", name);
44 }
45 
tplg_build_fe_dai_object(struct tplg_pre_processor * tplg_pp,snd_config_t * obj_cfg,snd_config_t * parent)46 int tplg_build_fe_dai_object(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
47 			      snd_config_t *parent)
48 {
49 	return tplg_build_base_object(tplg_pp, obj_cfg, parent, false);
50 }
51 
tplg_update_pcm_object(struct tplg_pre_processor * tplg_pp,snd_config_t * obj_cfg,snd_config_t * parent)52 static int tplg_update_pcm_object(struct tplg_pre_processor *tplg_pp,
53 			       snd_config_t *obj_cfg, snd_config_t *parent)
54 {
55 	snd_config_t *top, *parent_obj, *obj, *dest, *cfg, *pcm, *child;
56 	const char *parent_name, *item_name, *direction;
57 	int ret;
58 
59 	/* get object name */
60 	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);
61 	item_name = tplg_object_get_name(tplg_pp, obj);
62 	if (!item_name)
63 		return -EINVAL;
64 
65 	/* get direction */
66 	ret = snd_config_search(obj, "direction", &cfg);
67 	if (ret < 0) {
68 		SNDERR("no direction attribute in %s\n", item_name);
69 		return ret;
70 	}
71 
72 	ret = snd_config_get_string(cfg, &direction);
73 	if (ret < 0) {
74 		SNDERR("Invalid direction attribute in %s\n", item_name);
75 		return ret;
76 	}
77 
78 	/* add to parent section */
79 	top = tplg_object_get_section(tplg_pp, parent);
80 	if (!top) {
81 		SNDERR("Cannot find parent for %s\n", item_name);
82 		return -EINVAL;
83 	}
84 
85 	parent_obj = tplg_object_get_instance_config(tplg_pp, parent);
86 
87 	/* get parent name. if parent has no name, skip adding config */
88 	parent_name = tplg_object_get_name(tplg_pp, parent_obj);
89 	if (!parent_name)
90 		return 0;
91 
92 	/* find parent config with name */
93 	dest = tplg_find_config(top, parent_name);
94 	if (!dest) {
95 		SNDERR("Cannot find parent section %s\n", parent_name);
96 		return -EINVAL;
97 	}
98 
99 	ret = snd_config_search(dest, "pcm", &pcm);
100 	if (ret < 0) {
101 		ret = tplg_config_make_add(&pcm, "pcm", SND_CONFIG_TYPE_COMPOUND, dest);
102 		if (ret < 0) {
103 			SNDERR("Error creating pcm config in %s\n", parent_name);
104 			return ret;
105 		}
106 	}
107 
108 	ret = snd_config_search(pcm, direction, &cfg);
109 	if (ret >= 0) {
110 		SNDERR("pcm.%s exists already in %s\n", direction, parent_name);
111 		return -EEXIST;
112 	}
113 
114 	ret = tplg_config_make_add(&cfg, direction, SND_CONFIG_TYPE_COMPOUND, pcm);
115 
116 	if (ret >= 0)
117 	ret = tplg_config_make_add(&child, "capabilities", SND_CONFIG_TYPE_STRING, cfg);
118 
119 	if (ret >= 0)
120 	ret = snd_config_set_string(child, item_name);
121 
122 	return ret;
123 }
124 
tplg_build_pcm_caps_object(struct tplg_pre_processor * tplg_pp,snd_config_t * obj_cfg,snd_config_t * parent)125 int tplg_build_pcm_caps_object(struct tplg_pre_processor *tplg_pp,
126 			       snd_config_t *obj_cfg, snd_config_t *parent)
127 {
128 	snd_config_t *caps;
129 	int ret;
130 
131 	ret = tplg_build_object_from_template(tplg_pp, obj_cfg, &caps, NULL, false);
132 	if (ret < 0)
133 		return ret;
134 
135 	/* add pcm capabilities to parent */
136 	return tplg_update_pcm_object(tplg_pp, obj_cfg, parent);
137 }
138