File: builder.c

package info (click to toggle)
alsa-lib 1.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,656 kB
  • sloc: ansic: 84,132; sh: 11,192; makefile: 711
file content (341 lines) | stat: -rw-r--r-- 8,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
  Copyright(c) 2014-2015 Intel Corporation
  All rights reserved.

  This library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of
  the License, or (at your option) any later version.

  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 Lesser General Public License for more details.

  Authors: Mengdong Lin <mengdong.lin@intel.com>
           Yao Jin <yao.jin@intel.com>
           Liam Girdwood <liam.r.girdwood@linux.intel.com>
*/

#include "list.h"
#include "tplg_local.h"

/* verbose output detailing each object size and file position */
static void verbose(snd_tplg_t *tplg, const char *fmt, ...)
{
	int offset;
	va_list va;

	if (!tplg->verbose)
		return;

	offset = lseek(tplg->out_fd, 0, SEEK_CUR);

	va_start(va, fmt);
	fprintf(stdout, "0x%6.6x/%6.6d -", offset, offset);
	vfprintf(stdout, fmt, va);
	va_end(va);
}

/* write out block header to output file */
static int write_block_header(snd_tplg_t *tplg, unsigned int type,
	unsigned int vendor_type, unsigned int version, unsigned int index,
	size_t payload_size, int count)
{
	struct snd_soc_tplg_hdr hdr;
	size_t bytes;
	int offset = lseek(tplg->out_fd, 0, SEEK_CUR);

	memset(&hdr, 0, sizeof(hdr));
	hdr.magic = SND_SOC_TPLG_MAGIC;
	hdr.abi = SND_SOC_TPLG_ABI_VERSION;
	hdr.type = type;
	hdr.vendor_type = vendor_type;
	hdr.version = version;
	hdr.payload_size = payload_size;
	hdr.index = index;
	hdr.size = sizeof(hdr);
	hdr.count = count;

	/* make sure file offset is aligned with the calculated HDR offset */
	if ((unsigned int)offset != tplg->next_hdr_pos) {
		SNDERR("error: New header is at offset 0x%x but file"
			" offset 0x%x is %s by %d bytes\n",
			tplg->next_hdr_pos, offset,
			(unsigned int)offset > tplg->next_hdr_pos ? "ahead" : "behind",
			abs(offset - tplg->next_hdr_pos));
		exit(-EINVAL);
	}

	verbose(tplg, " header type %d size 0x%lx/%ld vendor %d "
		"version %d\n", type, (long unsigned int)payload_size,
		(long int)payload_size, vendor_type, version);

	tplg->next_hdr_pos += hdr.payload_size + sizeof(hdr);

	bytes = write(tplg->out_fd, &hdr, sizeof(hdr));
	if (bytes != sizeof(hdr)) {
		SNDERR("error: can't write section header %lu\n",
			(long unsigned int)bytes);
		return bytes;
	}

	return bytes;
}

static int write_elem_block(snd_tplg_t *tplg,
	struct list_head *base, int size, int tplg_type, const char *obj_name)
{
	struct list_head *pos;
	struct tplg_elem *elem;
	int ret, wsize = 0, count = 0, vendor_type;

	/* count number of elements */
	list_for_each(pos, base)
		count++;
	if (!count)
		return 0;

	/* write the header for this block */
	elem = list_entry(base->next, struct tplg_elem, list);
	vendor_type = elem->vendor_type;

	ret = write_block_header(tplg, tplg_type, vendor_type,
		tplg->version, 0, size, count);
	if (ret < 0) {
		SNDERR("error: failed to write %s block %d\n",
			obj_name, ret);
		return ret;
	}

	/* write each elem to block */
	list_for_each(pos, base) {

		elem = list_entry(pos, struct tplg_elem, list);

		/* compound elems have already been copied to other elems */
		if (elem->compound_elem)
			continue;

		if (elem->type != SND_TPLG_TYPE_DAPM_GRAPH)
			verbose(tplg, " %s '%s': write %d bytes\n",
				obj_name, elem->id, elem->size);
		else
			verbose(tplg, " %s '%s': write %d bytes\n",
				obj_name, elem->route->source, elem->size);

		count = write(tplg->out_fd, elem->obj, elem->size);
		if (count < 0) {
			SNDERR("error: failed to write %s %d\n",
				obj_name, ret);
			return ret;
		}

		wsize += count;
	}

	/* make sure we have written the correct size */
	if (wsize != size) {
		SNDERR("error: size mismatch. Expected %d wrote %d\n",
			size, wsize);
		return -EIO;
	}

	return 0;
}

static int calc_block_size(struct list_head *base)
{
	struct list_head *pos;
	struct tplg_elem *elem;
	int size = 0;

	list_for_each(pos, base) {

		elem = list_entry(pos, struct tplg_elem, list);

		/* compound elems have already been copied to other elems */
		if (elem->compound_elem)
			continue;

		size += elem->size;
	}

	return size;
}

static int write_block(snd_tplg_t *tplg, struct list_head *base,
	int type)
{
	int size;

	/* calculate the block size in bytes for all elems in this list */
	size = calc_block_size(base);
	if (size <= 0)
		return size;

	verbose(tplg, " block size for type %d is %d\n", type, size);

	/* write each elem for this block */
	switch (type) {
	case SND_TPLG_TYPE_MIXER:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_MIXER, "mixer");
	case SND_TPLG_TYPE_BYTES:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_BYTES, "bytes");
	case SND_TPLG_TYPE_ENUM:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_ENUM, "enum");
	case SND_TPLG_TYPE_DAPM_GRAPH:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_DAPM_GRAPH, "route");
	case SND_TPLG_TYPE_DAPM_WIDGET:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_DAPM_WIDGET, "widget");
	case SND_TPLG_TYPE_PCM:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_PCM, "pcm");
	case SND_TPLG_TYPE_BE:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_BACKEND_LINK, "be");
	case SND_TPLG_TYPE_CC:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_CODEC_LINK, "cc");
	case SND_TPLG_TYPE_DATA:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_PDATA, "data");
	case SND_TPLG_TYPE_DAI:
		return write_elem_block(tplg, base, size,
			SND_SOC_TPLG_TYPE_DAI, "dai");
	default:
		return -EINVAL;
	}

	return 0;
}

/* write the manifest including its private data */
static int write_manifest_data(snd_tplg_t *tplg)
{
	int ret;

	/* write the header for this block */
	ret = write_block_header(tplg, SND_SOC_TPLG_TYPE_MANIFEST, 0,
		tplg->version, 0,
		sizeof(tplg->manifest) + tplg->manifest.priv.size, 1);
	if (ret < 0) {
		SNDERR("error: failed to write manifest block %d\n", ret);
		return ret;
	}

	verbose(tplg, "manifest : write %d bytes\n", sizeof(tplg->manifest));
	ret = write(tplg->out_fd, &tplg->manifest, sizeof(tplg->manifest));
	if (ret < 0) {
		SNDERR("error: failed to write manifest %d\n", ret);
		return ret;
	}

	verbose(tplg, "manifest : write %d priv bytes\n", tplg->manifest.priv.size);
	ret = write(tplg->out_fd, tplg->manifest_pdata, tplg->manifest.priv.size);
	if (ret < 0) {
		SNDERR("error: failed to write manifest priv data %d\n", ret);
		return ret;
	}

	return 0;
}

int tplg_write_data(snd_tplg_t *tplg)
{
	int ret;

	/* write manifest */
	ret = write_manifest_data(tplg);
	if (ret < 0) {
		SNDERR("failed to write manifest %d\n", ret);
		return ret;
	}

	/* write mixer elems. */
	ret = write_block(tplg, &tplg->mixer_list,
		SND_TPLG_TYPE_MIXER);
	if (ret < 0) {
		SNDERR("failed to write control elems %d\n", ret);
		return ret;
	}

	/* write enum control elems. */
	ret = write_block(tplg, &tplg->enum_list,
		SND_TPLG_TYPE_ENUM);
	if (ret < 0) {
		SNDERR("failed to write control elems %d\n", ret);
		return ret;
	}

	/* write bytes extended control elems. */
	ret = write_block(tplg, &tplg->bytes_ext_list,
		SND_TPLG_TYPE_BYTES);
	if (ret < 0) {
		SNDERR("failed to write control elems %d\n", ret);
		return ret;
	}

	/* write widget elems */
	ret = write_block(tplg, &tplg->widget_list,
		SND_TPLG_TYPE_DAPM_WIDGET);
	if (ret < 0) {
		SNDERR("failed to write widget elems %d\n", ret);
		return ret;
	}

	/* write pcm elems */
	ret = write_block(tplg, &tplg->pcm_list,
		SND_TPLG_TYPE_PCM);
	if (ret < 0) {
		SNDERR("failed to write pcm elems %d\n", ret);
		return ret;
	}

	/* write physical dai elems */
	ret = write_block(tplg, &tplg->dai_list,
		SND_TPLG_TYPE_DAI);
	if (ret < 0) {
		SNDERR("failed to write physical dai elems %d\n", ret);
		return ret;
	}

	/* write be elems */
	ret = write_block(tplg, &tplg->be_list,
		SND_TPLG_TYPE_BE);
	if (ret < 0) {
		SNDERR("failed to write be elems %d\n", ret);
		return ret;
	}

	/* write cc elems */
	ret = write_block(tplg, &tplg->cc_list,
		SND_TPLG_TYPE_CC);
	if (ret < 0) {
		SNDERR("failed to write cc elems %d\n", ret);
		return ret;
	}

	/* write route elems */
	ret = write_block(tplg, &tplg->route_list,
		SND_TPLG_TYPE_DAPM_GRAPH);
	if (ret < 0) {
		SNDERR("failed to write graph elems %d\n", ret);
		return ret;
	}

	/* write private data */
	ret = write_block(tplg, &tplg->pdata_list,
		SND_TPLG_TYPE_DATA);
	if (ret < 0) {
		SNDERR("failed to write private data %d\n", ret);
		return ret;
	}

	return 0;
}