Skip to content

Commit aa1bac2

Browse files
committed
topology: add snd_tplg_save()
Signed-off-by: Jaroslav Kysela <[email protected]>
1 parent 4a0efdc commit aa1bac2

14 files changed

Lines changed: 2162 additions & 257 deletions

File tree

‎include/topology.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,21 @@ int snd_tplg_set_manifest_data(snd_tplg_t *tplg, const void *data, int len);
11241124
*/
11251125
int snd_tplg_set_version(snd_tplg_t *tplg, unsigned int version);
11261126

1127+
/*
1128+
* Flags for the snd_tplg_save()
1129+
*/
1130+
#define SND_TPLG_SAVE_SORT (1<<0) /*!< sort identifiers */
1131+
#define SND_TPLG_SAVE_GROUPS (1<<1) /*!< create the structure by group index */
1132+
#define SND_TPLG_SAVE_NOCHECK (1<<16) /*!< unchecked output for debugging */
1133+
1134+
/**
1135+
* \brief Save the topology to the text configuration string.
1136+
* \param tplg Topology instance.
1137+
* \param dst A pointer to string with result (malloc).
1138+
* \return Zero on success, otherwise a negative error code
1139+
*/
1140+
int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags);
1141+
11271142
/* \} */
11281143

11291144
#ifdef __cplusplus

‎src/conf.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,21 @@ static int get_nonwhite(input_t *input)
874874
}
875875
}
876876

877+
static inline int get_hexachar(input_t *input)
878+
{
879+
int c, num = 0;
880+
881+
c = get_char(input);
882+
if (c >= '0' && c <= '9') num |= (c - '0') << 4;
883+
else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 4;
884+
else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 4;
885+
c = get_char(input);
886+
if (c >= '0' && c <= '9') num |= (c - '0') << 0;
887+
else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 0;
888+
else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 0;
889+
return c;
890+
}
891+
877892
static int get_quotedchar(input_t *input)
878893
{
879894
int c;
@@ -891,6 +906,8 @@ static int get_quotedchar(input_t *input)
891906
return '\r';
892907
case 'f':
893908
return '\f';
909+
case 'x':
910+
return get_hexachar(input);
894911
case '0': case '1': case '2': case '3':
895912
case '4': case '5': case '6': case '7':
896913
{

‎src/topology/Makefile.am‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ libatopology_la_SOURCES =\
2727
text.c \
2828
channel.c \
2929
ops.c \
30-
elem.c
30+
elem.c \
31+
save.c
3132

3233
noinst_HEADERS = tplg_local.h
3334

‎src/topology/channel.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ static int lookup_channel(const char *c)
7373
return -EINVAL;
7474
}
7575

76+
const char *tplg_channel_name(int type)
77+
{
78+
unsigned int i;
79+
80+
for (i = 0; i < ARRAY_SIZE(channel_map); i++) {
81+
if (channel_map[i].id == type)
82+
return channel_map[i].name;
83+
}
84+
85+
return NULL;
86+
}
87+
7688
/* Parse a channel mapping. */
7789
int tplg_parse_channel(snd_tplg_t *tplg, snd_config_t *cfg,
7890
void *private)
@@ -123,3 +135,36 @@ int tplg_parse_channel(snd_tplg_t *tplg, snd_config_t *cfg,
123135
tplg->channel_idx++;
124136
return 0;
125137
}
138+
139+
int tplg_save_channels(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
140+
struct snd_soc_tplg_channel *channel,
141+
unsigned int count, char **dst, const char *pfx)
142+
{
143+
struct snd_soc_tplg_channel *c;
144+
const char *s;
145+
unsigned int index;
146+
int err;
147+
148+
if (count == 0)
149+
return 0;
150+
err = tplg_save_printf(dst, pfx, "channel {\n");
151+
for (index = 0; err >= 0 && index < count; index++) {
152+
c = channel + index;
153+
s = tplg_channel_name(c->id);
154+
if (s == NULL)
155+
err = tplg_save_printf(dst, pfx, "\t%u", c->id);
156+
else
157+
err = tplg_save_printf(dst, pfx, "\t%s", s);
158+
if (err >= 0)
159+
err = tplg_save_printf(dst, NULL, " {\n");
160+
if (err >= 0)
161+
err = tplg_save_printf(dst, pfx, "\t\treg %d\n", c->reg);
162+
if (err >= 0 && c->shift > 0)
163+
err = tplg_save_printf(dst, pfx, "\t\tshift %u\n", c->shift);
164+
if (err >= 0)
165+
err = tplg_save_printf(dst, pfx, "\t}\n");
166+
}
167+
if (err >= 0)
168+
err = tplg_save_printf(dst, pfx, "}\n");
169+
return err;
170+
}

‎src/topology/ctl.c‎

Lines changed: 196 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ struct ctl_access_elem {
2828
};
2929

3030
/* CTL access strings and codes */
31+
/* place the multi-bit values on top - like read_write - for save */
3132
static const struct ctl_access_elem ctl_access[] = {
33+
{"read_write", SNDRV_CTL_ELEM_ACCESS_READWRITE},
34+
{"tlv_read_write", SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE},
3235
{"read", SNDRV_CTL_ELEM_ACCESS_READ},
3336
{"write", SNDRV_CTL_ELEM_ACCESS_WRITE},
34-
{"read_write", SNDRV_CTL_ELEM_ACCESS_READWRITE},
3537
{"volatile", SNDRV_CTL_ELEM_ACCESS_VOLATILE},
3638
{"timestamp", SNDRV_CTL_ELEM_ACCESS_TIMESTAMP},
3739
{"tlv_read", SNDRV_CTL_ELEM_ACCESS_TLV_READ},
3840
{"tlv_write", SNDRV_CTL_ELEM_ACCESS_TLV_WRITE},
39-
{"tlv_read_write", SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE},
4041
{"tlv_command", SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND},
4142
{"inactive", SNDRV_CTL_ELEM_ACCESS_INACTIVE},
4243
{"lock", SNDRV_CTL_ELEM_ACCESS_LOCK},
@@ -103,6 +104,46 @@ int parse_access(snd_config_t *cfg,
103104
return err;
104105
}
105106

107+
/* Save Access */
108+
static int tplg_save_access(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
109+
struct snd_soc_tplg_ctl_hdr *hdr, char **dst,
110+
const char *pfx)
111+
{
112+
const char *last;
113+
unsigned int j, count, access, cval;
114+
int err;
115+
116+
if (hdr->access == 0)
117+
return 0;
118+
119+
access = hdr->access;
120+
for (j = 0, count = 0, last = NULL; j < ARRAY_SIZE(ctl_access); j++) {
121+
cval = ctl_access[j].value;
122+
if ((access & cval) == cval) {
123+
access &= ~cval;
124+
last = ctl_access[j].name;
125+
count++;
126+
}
127+
}
128+
if (count == 1)
129+
return tplg_save_printf(dst, pfx, "access.0 %s\n", last);
130+
err = tplg_save_printf(dst, pfx, "access [\n");
131+
if (err < 0)
132+
return err;
133+
access = hdr->access;
134+
for (j = 0; j < ARRAY_SIZE(ctl_access); j++) {
135+
cval = ctl_access[j].value;
136+
if ((access & cval) == cval) {
137+
err = tplg_save_printf(dst, pfx, "\t%s\n",
138+
ctl_access[j].name);
139+
if (err < 0)
140+
return err;
141+
access &= ~cval;
142+
}
143+
}
144+
return tplg_save_printf(dst, pfx, "]\n");
145+
}
146+
106147
/* copy referenced TLV to the mixer control */
107148
static int copy_tlv(struct tplg_elem *elem, struct tplg_elem *ref)
108149
{
@@ -358,6 +399,37 @@ int tplg_parse_tlv(snd_tplg_t *tplg, snd_config_t *cfg,
358399
return err;
359400
}
360401

402+
/* save TLV data */
403+
int tplg_save_tlv(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
404+
struct tplg_elem *elem,
405+
char **dst, const char *pfx)
406+
{
407+
struct snd_soc_tplg_ctl_tlv *tlv = elem->tlv;
408+
struct snd_soc_tplg_tlv_dbscale *scale;
409+
int err;
410+
411+
if (tlv->type != SNDRV_CTL_TLVT_DB_SCALE) {
412+
SNDERR("unknown TLV type");
413+
return -EINVAL;
414+
}
415+
416+
scale = &tlv->scale;
417+
err = tplg_save_printf(dst, NULL, "'%s' {\n", elem->id);
418+
if (err >= 0)
419+
err = tplg_save_printf(dst, pfx, "\tscale {\n");
420+
if (err >= 0 && scale->min)
421+
err = tplg_save_printf(dst, pfx, "\t\tmin %i\n", scale->min);
422+
if (err >= 0 && scale->step > 0)
423+
err = tplg_save_printf(dst, pfx, "\t\tstep %i\n", scale->step);
424+
if (err >= 0 && scale->mute > 0)
425+
err = tplg_save_printf(dst, pfx, "\t\tmute %i\n", scale->mute);
426+
if (err >= 0)
427+
err = tplg_save_printf(dst, pfx, "\t}\n");
428+
if (err >= 0)
429+
err = tplg_save_printf(dst, pfx, "}\n");
430+
return err;
431+
}
432+
361433
/* Parse Control Bytes */
362434
int tplg_parse_control_bytes(snd_tplg_t *tplg,
363435
snd_config_t *cfg,
@@ -430,7 +502,7 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
430502
}
431503

432504
if (strcmp(id, "data") == 0) {
433-
err = tplg_parse_data_refs(n, elem);
505+
err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_DATA);
434506
if (err < 0)
435507
return err;
436508
continue;
@@ -485,6 +557,49 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
485557
return 0;
486558
}
487559

560+
/* save control bytes */
561+
int tplg_save_control_bytes(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
562+
struct tplg_elem *elem,
563+
char **dst, const char *pfx)
564+
{
565+
struct snd_soc_tplg_bytes_control *be = elem->bytes_ext;
566+
char pfx2[16];
567+
int err;
568+
569+
if (!be)
570+
return 0;
571+
572+
snprintf(pfx2, sizeof(pfx2), "%s\t", pfx ?: "");
573+
err = tplg_save_printf(dst, NULL, "'%s' {\n", elem->id);
574+
if (err < 0)
575+
return err;
576+
if (err >= 0 && elem->index > 0)
577+
err = tplg_save_printf(dst, pfx, "\tindex %u\n", elem->index);
578+
if (err >= 0 && be->base > 0)
579+
err = tplg_save_printf(dst, pfx, "\tbase %u\n", be->base);
580+
if (err >= 0 && be->num_regs > 0)
581+
err = tplg_save_printf(dst, pfx, "\tnum_regs %u\n", be->num_regs);
582+
if (err >= 0 && be->max > 0)
583+
err = tplg_save_printf(dst, pfx, "\tmax %u\n", be->max);
584+
if (err >= 0 && be->mask > 0)
585+
err = tplg_save_printf(dst, pfx, "\tmask %u\n", be->mask);
586+
if (err >= 0)
587+
err = tplg_save_ops(tplg, &be->hdr, dst, pfx2);
588+
if (err >= 0)
589+
err = tplg_save_ext_ops(tplg, be, dst, pfx2);
590+
if (err >= 0)
591+
err = tplg_save_access(tplg, &be->hdr, dst, pfx2);
592+
if (err >= 0)
593+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_TLV,
594+
"tlv", dst, pfx2);
595+
if (err >= 0)
596+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_DATA,
597+
"data", dst, pfx2);
598+
if (err >= 0)
599+
err = tplg_save_printf(dst, pfx, "}\n");
600+
return err;
601+
}
602+
488603
/* Parse Control Enums. */
489604
int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg,
490605
void *private ATTRIBUTE_UNUSED)
@@ -559,7 +674,7 @@ int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg,
559674
}
560675

561676
if (strcmp(id, "data") == 0) {
562-
err = tplg_parse_data_refs(n, elem);
677+
err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_DATA);
563678
if (err < 0)
564679
return err;
565680
continue;
@@ -582,6 +697,42 @@ int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg,
582697
return 0;
583698
}
584699

700+
/* save control eunm */
701+
int tplg_save_control_enum(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
702+
struct tplg_elem *elem,
703+
char **dst, const char *pfx)
704+
{
705+
struct snd_soc_tplg_enum_control *ec = elem->enum_ctrl;
706+
char pfx2[16];
707+
int err;
708+
709+
if (!ec)
710+
return 0;
711+
712+
snprintf(pfx2, sizeof(pfx2), "%s\t", pfx ?: "");
713+
err = tplg_save_printf(dst, NULL, "'%s' {\n", elem->id);
714+
if (err < 0)
715+
return err;
716+
if (err >= 0 && elem->index > 0)
717+
err = tplg_save_printf(dst, pfx, "\tindex %u\n", elem->index);
718+
if (err >= 0)
719+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_TEXT,
720+
"texts", dst, pfx2);
721+
if (err >= 0)
722+
err = tplg_save_channels(tplg, ec->channel, ec->num_channels,
723+
dst, pfx2);
724+
if (err >= 0)
725+
err = tplg_save_ops(tplg, &ec->hdr, dst, pfx2);
726+
if (err >= 0)
727+
err = tplg_save_access(tplg, &ec->hdr, dst, pfx2);
728+
if (err >= 0)
729+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_DATA,
730+
"data", dst, pfx2);
731+
if (err >= 0)
732+
err = tplg_save_printf(dst, pfx, "}\n");
733+
return err;
734+
}
735+
585736
/* Parse Controls.
586737
*
587738
* Mixer control. Supports multiple channels.
@@ -683,7 +834,7 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
683834
}
684835

685836
if (strcmp(id, "data") == 0) {
686-
err = tplg_parse_data_refs(n, elem);
837+
err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_DATA);
687838
if (err < 0)
688839
return err;
689840
continue;
@@ -709,6 +860,46 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
709860
return 0;
710861
}
711862

863+
int tplg_save_control_mixer(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
864+
struct tplg_elem *elem, char **dst,
865+
const char *pfx)
866+
{
867+
struct snd_soc_tplg_mixer_control *mc = elem->mixer_ctrl;
868+
char pfx2[16];
869+
int err;
870+
871+
if (!mc)
872+
return 0;
873+
err = tplg_save_printf(dst, NULL, "'%s' {\n", elem->id);
874+
if (err < 0)
875+
return err;
876+
snprintf(pfx2, sizeof(pfx2), "%s\t", pfx ?: "");
877+
if (err >= 0 && elem->index > 0)
878+
err = tplg_save_printf(dst, pfx, "\tindex %u\n", elem->index);
879+
if (err >= 0)
880+
err = tplg_save_channels(tplg, mc->channel, mc->num_channels,
881+
dst, pfx2);
882+
if (err >= 0 && mc->max > 0)
883+
err = tplg_save_printf(dst, pfx, "\tmax %u\n", mc->max);
884+
if (err >= 0 && mc->invert > 0)
885+
err = tplg_save_printf(dst, pfx, "\tinvert 1\n", mc->max);
886+
if (err >= 0 && mc->invert > 0)
887+
err = tplg_save_printf(dst, pfx, "\tinvert 1\n", mc->max);
888+
if (err >= 0)
889+
err = tplg_save_ops(tplg, &mc->hdr, dst, pfx2);
890+
if (err >= 0)
891+
err = tplg_save_access(tplg, &mc->hdr, dst, pfx2);
892+
if (err >= 0)
893+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_TLV,
894+
"tlv", dst, pfx2);
895+
if (err >= 0)
896+
err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_DATA,
897+
"data", dst, pfx2);
898+
if (err >= 0)
899+
err = tplg_save_printf(dst, pfx, "}\n");
900+
return err;
901+
}
902+
712903
static int init_ctl_hdr(struct snd_soc_tplg_ctl_hdr *hdr,
713904
struct snd_tplg_ctl_template *t)
714905
{

0 commit comments

Comments
 (0)