2015-07-29 17:45:22 +01:00
|
|
|
/*
|
|
|
|
|
Copyright(c) 2014-2015 Intel Corporation
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
2017-01-24 14:59:08 +00:00
|
|
|
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.
|
2015-07-29 17:45:22 +01:00
|
|
|
|
|
|
|
|
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, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
|
|
if (!tplg->verbose)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2019-12-14 19:13:53 +01:00
|
|
|
fprintf(stdout, "0x%6.6zx/%6.6zd - ", tplg->bin_pos, tplg->bin_pos);
|
2015-07-29 17:45:22 +01:00
|
|
|
vfprintf(stdout, fmt, va);
|
|
|
|
|
va_end(va);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 17:01:03 +01:00
|
|
|
/* write a block, track the position */
|
|
|
|
|
static ssize_t twrite(snd_tplg_t *tplg, void *data, size_t data_size)
|
|
|
|
|
{
|
2019-12-14 19:13:53 +01:00
|
|
|
if (tplg->bin_pos + data_size > tplg->bin_size)
|
2019-12-13 17:01:03 +01:00
|
|
|
return -EIO;
|
2019-12-14 19:13:53 +01:00
|
|
|
memcpy(tplg->bin + tplg->bin_pos, data, data_size);
|
|
|
|
|
tplg->bin_pos += data_size;
|
|
|
|
|
return data_size;
|
2019-12-13 17:01:03 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 17:45:22 +01:00
|
|
|
/* write out block header to output file */
|
2019-12-13 17:01:03 +01:00
|
|
|
static ssize_t 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)
|
2015-07-29 17:45:22 +01:00
|
|
|
{
|
|
|
|
|
struct snd_soc_tplg_hdr hdr;
|
|
|
|
|
|
|
|
|
|
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 */
|
2019-12-14 19:13:53 +01:00
|
|
|
if (tplg->bin_pos != tplg->next_hdr_pos) {
|
2019-12-13 17:01:03 +01:00
|
|
|
SNDERR("error: New header is at offset 0x%zx but file"
|
|
|
|
|
" offset 0x%zx is %s by %ld bytes\n",
|
2019-12-14 19:13:53 +01:00
|
|
|
tplg->next_hdr_pos, tplg->bin_pos,
|
|
|
|
|
tplg->bin_pos > tplg->next_hdr_pos ? "ahead" : "behind",
|
|
|
|
|
labs(tplg->bin_pos - tplg->next_hdr_pos));
|
2019-12-13 17:01:03 +01:00
|
|
|
return -EINVAL;
|
2015-07-29 17:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "header index %d type %d count %d size 0x%lx/%ld vendor %d "
|
2017-06-30 14:14:56 +01:00
|
|
|
"version %d\n", index, type, count,
|
|
|
|
|
(long unsigned int)payload_size, (long int)payload_size,
|
|
|
|
|
vendor_type, version);
|
2015-07-29 17:45:22 +01:00
|
|
|
|
|
|
|
|
tplg->next_hdr_pos += hdr.payload_size + sizeof(hdr);
|
|
|
|
|
|
2019-12-13 17:01:03 +01:00
|
|
|
return twrite(tplg, &hdr, sizeof(hdr));
|
2015-07-29 17:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int write_elem_block(snd_tplg_t *tplg,
|
2019-12-13 17:01:03 +01:00
|
|
|
struct list_head *base, size_t size, int tplg_type, const char *obj_name)
|
2015-07-29 17:45:22 +01:00
|
|
|
{
|
2017-04-13 14:52:47 +08:00
|
|
|
struct list_head *pos, *sub_pos, *sub_base;
|
|
|
|
|
struct tplg_elem *elem, *elem_next;
|
2019-12-13 17:01:03 +01:00
|
|
|
size_t total_size = 0, count = 0, block_size = 0;
|
|
|
|
|
ssize_t ret, wsize;
|
2015-07-29 17:45:22 +01:00
|
|
|
|
2017-04-13 14:52:47 +08:00
|
|
|
sub_base = base;
|
2015-07-29 17:45:22 +01:00
|
|
|
list_for_each(pos, base) {
|
2017-04-13 14:52:47 +08:00
|
|
|
/* find elems with the same index to make a block */
|
2015-07-29 17:45:22 +01:00
|
|
|
elem = list_entry(pos, struct tplg_elem, list);
|
2018-01-02 10:50:01 +05:30
|
|
|
|
|
|
|
|
if (elem->compound_elem)
|
|
|
|
|
continue;
|
|
|
|
|
|
2017-04-13 14:52:47 +08:00
|
|
|
elem_next = list_entry(pos->next, struct tplg_elem, list);
|
|
|
|
|
block_size += elem->size;
|
|
|
|
|
count++;
|
2015-07-29 17:45:22 +01:00
|
|
|
|
2017-04-13 14:52:47 +08:00
|
|
|
if ((pos->next == base) || (elem_next->index != elem->index)) {
|
|
|
|
|
/* write header for the block */
|
|
|
|
|
ret = write_block_header(tplg, tplg_type, elem->vendor_type,
|
|
|
|
|
tplg->version, elem->index, block_size, count);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
SNDERR("error: failed to write %s block %d\n",
|
|
|
|
|
obj_name, ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* write elems for the block */
|
|
|
|
|
list_for_each(sub_pos, sub_base) {
|
|
|
|
|
elem = list_entry(sub_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)
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "%s '%s': write %d bytes\n",
|
2017-04-13 14:52:47 +08:00
|
|
|
obj_name, elem->id, elem->size);
|
|
|
|
|
else
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "%s '%s -> %s -> %s': write %d bytes\n",
|
2017-06-30 14:14:56 +01:00
|
|
|
obj_name, elem->route->source,
|
|
|
|
|
elem->route->control,
|
|
|
|
|
elem->route->sink, elem->size);
|
2017-04-13 14:52:47 +08:00
|
|
|
|
2019-12-13 17:01:03 +01:00
|
|
|
wsize = twrite(tplg, elem->obj, elem->size);
|
|
|
|
|
if (wsize < 0)
|
|
|
|
|
return size;
|
2017-04-13 14:52:47 +08:00
|
|
|
|
|
|
|
|
total_size += wsize;
|
|
|
|
|
/* get to the end of sub list */
|
|
|
|
|
if (sub_pos == pos)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* the last elem of the current sub list as the head of
|
|
|
|
|
next sub list*/
|
|
|
|
|
sub_base = pos;
|
|
|
|
|
count = 0;
|
|
|
|
|
block_size = 0;
|
2015-07-29 17:45:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* make sure we have written the correct size */
|
2017-04-13 14:52:47 +08:00
|
|
|
if (total_size != size) {
|
2019-12-13 17:01:03 +01:00
|
|
|
SNDERR("error: size mismatch. Expected %zu wrote %zu\n",
|
2017-04-13 14:52:47 +08:00
|
|
|
size, total_size);
|
2015-07-29 17:45:22 +01:00
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 19:13:53 +01:00
|
|
|
static size_t calc_manifest_size(snd_tplg_t *tplg)
|
|
|
|
|
{
|
|
|
|
|
return sizeof(struct snd_soc_tplg_hdr) +
|
|
|
|
|
sizeof(tplg->manifest) +
|
|
|
|
|
tplg->manifest.priv.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t calc_real_size(struct list_head *base)
|
|
|
|
|
{
|
|
|
|
|
struct list_head *pos;
|
|
|
|
|
struct tplg_elem *elem, *elem_next;
|
|
|
|
|
size_t 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;
|
|
|
|
|
|
|
|
|
|
if (elem->size <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
size += elem->size;
|
|
|
|
|
|
|
|
|
|
elem_next = list_entry(pos->next, struct tplg_elem, list);
|
|
|
|
|
|
|
|
|
|
if ((pos->next == base) || (elem_next->index != elem->index))
|
|
|
|
|
size += sizeof(struct snd_soc_tplg_hdr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 18:26:20 +01:00
|
|
|
static size_t calc_block_size(struct list_head *base)
|
2015-07-29 17:45:22 +01:00
|
|
|
{
|
|
|
|
|
struct list_head *pos;
|
|
|
|
|
struct tplg_elem *elem;
|
2019-12-13 18:26:20 +01:00
|
|
|
size_t size = 0;
|
2015-07-29 17:45:22 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-04 18:09:46 +01:00
|
|
|
/* write the manifest including its private data */
|
2019-12-13 17:01:03 +01:00
|
|
|
static ssize_t write_manifest_data(snd_tplg_t *tplg)
|
2015-08-04 18:09:46 +01:00
|
|
|
{
|
2019-12-13 17:01:03 +01:00
|
|
|
ssize_t ret;
|
2015-08-04 18:09:46 +01:00
|
|
|
|
|
|
|
|
/* write the header for this block */
|
|
|
|
|
ret = write_block_header(tplg, SND_SOC_TPLG_TYPE_MANIFEST, 0,
|
2015-09-16 17:07:13 +08:00
|
|
|
tplg->version, 0,
|
2015-08-04 18:09:46 +01:00
|
|
|
sizeof(tplg->manifest) + tplg->manifest.priv.size, 1);
|
|
|
|
|
if (ret < 0) {
|
2019-12-13 17:01:03 +01:00
|
|
|
SNDERR("error: failed to write manifest block\n");
|
2015-08-04 18:09:46 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "manifest: write %d bytes\n", sizeof(tplg->manifest));
|
2019-12-13 17:01:03 +01:00
|
|
|
ret = twrite(tplg, &tplg->manifest, sizeof(tplg->manifest));
|
|
|
|
|
if (ret >= 0) {
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "manifest: write %d priv bytes\n", tplg->manifest.priv.size);
|
2019-12-13 17:01:03 +01:00
|
|
|
ret = twrite(tplg, tplg->manifest_pdata, tplg->manifest.priv.size);
|
2015-08-04 18:09:46 +01:00
|
|
|
}
|
2019-12-13 17:01:03 +01:00
|
|
|
return ret;
|
2015-08-04 18:09:46 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 17:45:22 +01:00
|
|
|
int tplg_write_data(snd_tplg_t *tplg)
|
|
|
|
|
{
|
2019-12-13 18:24:55 +01:00
|
|
|
struct wtable {
|
|
|
|
|
const char *name;
|
|
|
|
|
struct list_head *list;
|
|
|
|
|
int type;
|
2019-12-13 18:49:29 +01:00
|
|
|
int tsoc;
|
2019-12-13 18:24:55 +01:00
|
|
|
} *wptr, wtable[] = {
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "control mixer",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->mixer_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_MIXER,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_MIXER,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "control enum",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->enum_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_ENUM,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_ENUM,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "control extended (bytes)",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->bytes_ext_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_BYTES,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_BYTES,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "dapm widget",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->widget_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_DAPM_WIDGET,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_DAPM_WIDGET,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "pcm",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->pcm_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_PCM,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_PCM,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "physical dai",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->dai_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_DAI,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_DAI,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "be",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->be_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_BE,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_BACKEND_LINK,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "cc",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->cc_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_CC,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_CODEC_LINK,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "route (dapm graph)",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->route_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_DAPM_GRAPH,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_DAPM_GRAPH,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
{
|
2019-12-13 18:49:29 +01:00
|
|
|
.name = "private data",
|
2019-12-13 18:24:55 +01:00
|
|
|
.list = &tplg->pdata_list,
|
|
|
|
|
.type = SND_TPLG_TYPE_DATA,
|
2019-12-13 18:49:29 +01:00
|
|
|
.tsoc = SND_SOC_TPLG_TYPE_PDATA,
|
2019-12-13 18:24:55 +01:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-13 17:01:03 +01:00
|
|
|
ssize_t ret;
|
2019-12-14 19:13:53 +01:00
|
|
|
size_t total_size, size;
|
2019-12-13 18:24:55 +01:00
|
|
|
unsigned int index;
|
2015-07-29 17:45:22 +01:00
|
|
|
|
2019-12-14 19:13:53 +01:00
|
|
|
/* calculate total size */
|
|
|
|
|
total_size = calc_manifest_size(tplg);
|
|
|
|
|
for (index = 0; index < ARRAY_SIZE(wtable); index++) {
|
|
|
|
|
wptr = &wtable[index];
|
|
|
|
|
size = calc_real_size(wptr->list);
|
|
|
|
|
total_size += size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* allocate new binary output */
|
|
|
|
|
free(tplg->bin);
|
|
|
|
|
tplg->bin = malloc(total_size);
|
|
|
|
|
tplg->bin_pos = 0;
|
|
|
|
|
tplg->bin_size = total_size;
|
|
|
|
|
if (tplg->bin == NULL) {
|
|
|
|
|
tplg->bin_size = 0;
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 17:45:22 +01:00
|
|
|
/* write manifest */
|
2015-08-04 18:09:46 +01:00
|
|
|
ret = write_manifest_data(tplg);
|
2015-07-29 17:45:22 +01:00
|
|
|
if (ret < 0) {
|
2019-12-13 18:24:55 +01:00
|
|
|
SNDERR("failed to write manifest %d\n", ret);
|
2015-07-29 17:45:22 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-13 18:24:55 +01:00
|
|
|
/* write all blocks */
|
|
|
|
|
for (index = 0; index < ARRAY_SIZE(wtable); index++) {
|
|
|
|
|
wptr = &wtable[index];
|
2019-12-13 18:49:29 +01:00
|
|
|
/* calculate the block size in bytes for all elems in this list */
|
|
|
|
|
size = calc_block_size(wptr->list);
|
|
|
|
|
if (size == 0)
|
|
|
|
|
continue;
|
2019-12-13 18:53:41 +01:00
|
|
|
verbose(tplg, "block size for type %s (%d:%d) is 0x%zx/%zd\n",
|
2019-12-13 18:49:29 +01:00
|
|
|
wptr->name, wptr->type,
|
2019-12-13 18:53:41 +01:00
|
|
|
wptr->tsoc, size, size);
|
2019-12-13 18:49:29 +01:00
|
|
|
ret = write_elem_block(tplg, wptr->list, size,
|
|
|
|
|
wptr->tsoc, wptr->name);
|
2019-12-13 18:24:55 +01:00
|
|
|
if (ret < 0) {
|
2019-12-13 18:49:29 +01:00
|
|
|
SNDERR("failed to write %s elements: %s\n",
|
|
|
|
|
wptr->name, snd_strerror(-ret));
|
2019-12-13 18:24:55 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
2015-07-29 17:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-14 19:13:53 +01:00
|
|
|
verbose(tplg, "total size is 0x%zx/%zd\n", tplg->bin_pos, tplg->bin_pos);
|
|
|
|
|
|
|
|
|
|
if (total_size != tplg->bin_pos) {
|
|
|
|
|
SNDERR("total size mismatch (%zd != %zd)\n",
|
|
|
|
|
total_size, tplg->bin_pos);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2019-12-13 18:53:41 +01:00
|
|
|
|
2015-07-29 17:45:22 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|