Skip to contentSkip to conversation

topology: Add external API for building topology data

PATCH In review Liam Girdwood 2 participants 5 messages Aug 11, 2015 – Aug 12, 2015

lore
Share & tools
lore
#0 lore raw

v3 cover topology: Add external API for building topology data

Currently we can build topology binary data files from topology text files. However it is sometimes necessary for DSP FW vendors to build topology binary data within a toolset and create topology binary data directly from within vendor tools.

This series adds an API to the alsa-lib topology core so that vendor tools can create topology data directly.

Changes since V2:-

o Fixed elem memory leaks on error handling paths
o Fixed indentation.
o Moved container_of and ARRAY_SIZE to local.h
o remove use of unlink() and fixed file permissions.

Changes since V1:-

o Split out refactoring of OBJECT_TYPE to SND_TPLG_TYPE_
o Removed inclusing of type_compat.h from asoc.h

Liam Girdwood (1):
core: add convenience macros to local.h

Mengdong Lin (2):
topology: Add C templates structure for building topology from C
programs
topology: A API calls to directly build topology data from templates

 include/local.h           |   7 ++
 include/topology.h        | 202 ++++++++++++++++++++++++++++++++
 src/topology/ctl.c        | 292 ++++++++++++++++++++++++++++++++++++++++++++++
 src/topology/dapm.c       | 183 ++++++++++++++++++++++++++---
 src/topology/elem.c       |  17 +++
 src/topology/parser.c     |  59 +++++++++-
 src/topology/tplg_local.h |  26 ++---
 7 files changed, 749 insertions(+), 37 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v3 1/3 core: add convenience macros to local.h

Move ARRAY_SIZE() from tplg_local.h to local.h and add container_of() macro to local.h. Both macros are generic but are initially used by topology.

---
 include/local.h           | 7 +++++++
 src/topology/tplg_local.h | 1 -
 2 files changed, 7 insertions(+), 1 deletion(-)
include/local.h +7 -0
index 6600816..b429f5d 100644
@@ -350,4 +350,11 @@ int snd_config_search_alias_hooks(snd_config_t *config,
 
 int _snd_conf_generic_id(const char *id);
 
+/* convenience macros */
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+
+#define container_of(ptr, type, member) ({                      \
+	 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
+	(type *)( (char *)__mptr - offsetof(type,member) );})
+
 #endif
src/topology/tplg_local.h +0 -1
index febc177..3982cc7 100644
@@ -32,7 +32,6 @@
 #define MAX_FILE		256
 #define TPLG_MAX_PRIV_SIZE	(1024 * 128)
 #define ALSA_TPLG_DIR	ALSA_CONFIG_DIR "/topology"
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 
 /** The name of the environment variable containing the tplg directory */
 #define ALSA_CONFIG_TPLG_VAR "ALSA_CONFIG_TPLG"
Signature · 1 line
2.1.4
#2 lore raw

PATCH v3 2/3 topology: Add C templates structure for building topology from C programs

In reply to Liam Girdwood

From: Mengdong Lin <[email protected]>

Define structures that can be used by applications to directly build topology data instead of using text files. The application will build up the topology data by populating the template structures for each object type and then registering the template with the topology core.

---
 include/topology.h        | 176 ++++++++++++++++++++++++++++++++++++++++++++++
 src/topology/tplg_local.h |  18 -----
 2 files changed, 176 insertions(+), 18 deletions(-)
include/topology.h +176 -0
index 0cb2d79..aee43de 100644
@@ -456,9 +456,30 @@ extern "C" {
  *
  */
 
+/** Maximum number of channels supported in one control */
+#define SND_TPLG_MAX_CHAN		8
+
 /** Topology context */
 typedef struct snd_tplg snd_tplg_t;
 
+/** Topology object types */
+enum snd_tplg_type {
+	SND_TPLG_TYPE_TLV = 0,		/*!< TLV Data */
+	SND_TPLG_TYPE_MIXER,		/*!< Mixer control*/
+	SND_TPLG_TYPE_ENUM,		/*!< Enumerated control */
+	SND_TPLG_TYPE_TEXT,		/*!< Text data */
+	SND_TPLG_TYPE_DATA,		/*!< Private data */
+	SND_TPLG_TYPE_BYTES,		/*!< Byte control */
+	SND_TPLG_TYPE_STREAM_CONFIG,	/*!< PCM Stream configuration */
+	SND_TPLG_TYPE_STREAM_CAPS,	/*!< PCM Stream capabilities */
+	SND_TPLG_TYPE_PCM,		/*!< PCM stream device */
+	SND_TPLG_TYPE_DAPM_WIDGET,	/*!< DAPM widget */
+	SND_TPLG_TYPE_DAPM_GRAPH,	/*!< DAPM graph elements */
+	SND_TPLG_TYPE_BE,		/*!< BE DAI link */
+	SND_TPLG_TYPE_CC,		/*!< Hostless codec <-> codec link */
+	SND_TPLG_TYPE_MANIFEST,		/*!< Topology manifest */
+};
+
 /**
  * \brief Create a new topology parser instance.
  * \return New topology parser instance
@@ -488,6 +509,161 @@ int snd_tplg_build_file(snd_tplg_t *tplg, const char *infile,
  */
 void snd_tplg_verbose(snd_tplg_t *tplg, int verbose);
 
+/** \struct snd_tplg_tlv_template
+ * \brief Template type for all TLV objects.
+ */
+struct snd_tplg_tlv_template {
+	int type;	 /*!< TLV type SNDRV_CTL_TLVT_ */
+};
+
+/** \struct snd_tplg_tlv_dbscale_template
+ * \brief Template type for TLV Scale objects.
+ */
+struct snd_tplg_tlv_dbscale_template {
+	struct snd_tplg_tlv_template hdr;	/*!< TLV type header */
+	int min;			/*!< dB minimum value in 0.1dB */
+	int step;			/*!< dB step size in 0.1dB */
+	int mute;			/*!< is min dB value mute ? */
+};
+
+/** \struct snd_tplg_channel_template
+ * \brief Template type for single channel mapping.
+ */
+struct snd_tplg_channel_elem {
+	int size;	/*!< size in bytes of this structure */
+	int reg;	/*!< channel control register */
+	int shift;	/*!< channel shift for control bits */
+	int id;		/*!< ID maps to Left, Right, LFE etc */
+};
+
+/** \struct snd_tplg_channel_map_template
+ * \brief Template type for channel mapping.
+ */
+struct snd_tplg_channel_map_template {
+	int num_channels;	/*!< number of channel mappings */
+	struct snd_tplg_channel_elem channel[SND_TPLG_MAX_CHAN];	/*!< mapping */
+};
+
+/** \struct snd_tplg_pdata_template
+ * \brief Template type for private data objects.
+ */
+struct snd_tplg_pdata_template {
+	unsigned int length;	/*!< data length */
+	const void *data;	/*!< data */
+};
+
+/** \struct snd_tplg_io_ops_template
+ * \brief Template type for object operations mapping.
+ */
+struct snd_tplg_io_ops_template {
+	int get;	/*!< get callback ID */
+	int put;	/*!< put callback ID */
+	int info;	/*!< info callback ID */
+};
+
+/** \struct snd_tplg_ctl_template
+ * \brief Template type for control objects.
+ */
+struct snd_tplg_ctl_template {
+	int type;		/*!< Control type */
+	const char *name;	/*!< Control name */
+	int access;		/*!< Control access */
+	struct snd_tplg_io_ops_template ops;	/*!< operations */
+	struct snd_tplg_tlv_template *tlv; /*!< non NULL means we have TLV data */
+};
+
+/** \struct snd_tplg_mixer_template
+ * \brief Template type for mixer control objects.
+ */
+struct snd_tplg_mixer_template {
+	struct snd_tplg_ctl_template hdr;	/*!< control type header */
+	struct snd_tplg_channel_map_template *map;	/*!< channel map */
+	int min;	/*!< min value for mixer */
+	int max;	/*!< max value for mixer */
+	int platform_max;	/*!< max value for platform control */
+	int invert;	/*!< whether controls bits are inverted */
+	struct snd_soc_tplg_private *priv;	/*!< control private data */
+};
+
+/** \struct snd_tplg_enum_template
+ * \brief Template type for enumerated control objects.
+ */
+struct snd_tplg_enum_template {
+	struct snd_tplg_ctl_template hdr;	/*!< control type header */
+	struct snd_tplg_channel_map_template *map;	/*!< channel map */
+	int items;	/*!< number of enumerated items in control */
+	int mask;	/*!< register mask size */
+	const char **texts;	/*!< control text items */
+	const int **values;	/*!< control value items */
+	struct snd_soc_tplg_private *priv;	/*!< control private data */
+};
+
+/** \struct snd_tplg_bytes_template
+ * \brief Template type for TLV Scale objects.
+ */
+struct snd_tplg_bytes_template {
+	struct snd_tplg_ctl_template hdr;	/*!< control type header */
+	int max;		/*!< max byte control value */
+	int mask;		/*!< byte control mask */
+	int base;		/*!< base register */
+	int num_regs;		/*!< number of registers */
+	struct snd_tplg_io_ops_template ext_ops;	/*!< ops mapping */
+	struct snd_soc_tplg_private *priv;	/*!< control private data */
+};
+
+/** \struct snd_tplg_graph_elem
+ * \brief Template type for single DAPM graph element.
+ */
+struct snd_tplg_graph_elem {
+	const char *src;	/*!< source widget name */
+	const char *ctl;	/*!< control name or NULL if no control */
+	const char *sink;	/*!< sink widget name */
+};
+
+/** \struct snd_tplg_graph_template
+ * \brief Template type for array of DAPM graph elements.
+ */
+struct snd_tplg_graph_template {
+	int count;		/*!< Number of graph elements */
+	struct snd_tplg_graph_elem elem[0];	/*!< graph elements */
+};
+
+/** \struct snd_tplg_widget_template
+ * \brief Template type for DAPM widget objects.
+ */
+struct snd_tplg_widget_template {
+	int id;			/*!< SND_SOC_DAPM_CTL */
+	const char *name;	/*!< widget name */
+	const char *sname;	/*!< stream name (certain widgets only) */
+	int reg;		/*!< negative reg = no direct dapm */
+	int shift;		/*!< bits to shift */
+	int mask;		/*!< non-shifted mask */
+	int subseq;		/*!< sort within widget type */
+	unsigned int invert;		/*!< invert the power bit */
+	unsigned int ignore_suspend;	/*!< kept enabled over suspend */
+	unsigned short event_flags;	/*!< PM event sequence flags */
+	unsigned short event_type;	/*!< PM event sequence type */
+	struct snd_soc_tplg_private *priv;	/*!< widget private data */
+	int num_ctls;			/*!< Number of controls used by widget */
+	struct snd_tplg_ctl_template *ctl[0];	/*!< array of widget controls */
+};
+
+/** \struct snd_tplg_obj_template
+ * \brief Generic Template Object
+ */
+typedef struct snd_tplg_obj_template {
+	enum snd_tplg_type type;	/*!< template object type */
+	int index;		/*!< group index for object */
+	int version;		/*!< optional vendor specific version details */
+	int vendor_type;	/*!< optional vendor specific type info */
+	union {
+		struct snd_tplg_widget_template *widget;	/*!< DAPM widget */
+		struct snd_tplg_mixer_template *mixer;		/*!< Mixer control */
+		struct snd_tplg_bytes_template *bytes_ctl;	/*!< Bytes control */
+		struct snd_tplg_enum_template *enum_ctl;	/*!< Enum control */
+		struct snd_tplg_graph_template *graph;		/*!< Graph elements */
+	};
+} snd_tplg_obj_template_t;
 /* \} */
 
 #ifdef __cplusplus
src/topology/tplg_local.h +0 -18
index 3982cc7..ec63045 100644
@@ -39,24 +39,6 @@
 struct tplg_ref;
 struct tplg_elem;
 
-/** Topology object types */
-enum snd_tplg_type {
-	SND_TPLG_TYPE_TLV = 0,		/*!< TLV Data */
-	SND_TPLG_TYPE_MIXER,		/*!< Mixer control*/
-	SND_TPLG_TYPE_ENUM,		/*!< Enumerated control */
-	SND_TPLG_TYPE_TEXT,		/*!< Text data */
-	SND_TPLG_TYPE_DATA,		/*!< Private data */
-	SND_TPLG_TYPE_BYTES,		/*!< Byte control */
-	SND_TPLG_TYPE_STREAM_CONFIG,	/*!< PCM Stream configuration */
-	SND_TPLG_TYPE_STREAM_CAPS,	/*!< PCM Stream capabilities */
-	SND_TPLG_TYPE_PCM,		/*!< PCM stream device */
-	SND_TPLG_TYPE_DAPM_WIDGET,	/*!< DAPM widget */
-	SND_TPLG_TYPE_DAPM_GRAPH,	/*!< DAPM graph elements */
-	SND_TPLG_TYPE_BE,		/*!< BE DAI link */
-	SND_TPLG_TYPE_CC,		/*!< Hostless codec <-> codec link */
-	SND_TPLG_TYPE_MANIFEST,		/*!< Topology manifest */
-};
-
 struct snd_tplg {
 
 	/* opaque vendor data */
Signature · 1 line
2.1.4
#3 lore raw

PATCH v3 3/3 topology: A API calls to directly build topology data from templates

In reply to Liam Girdwood

From: Mengdong Lin <[email protected]>

Add some new API calls so that applications can directly build topology data using template structures.

---
 include/topology.h        |  26 +++++
 src/topology/ctl.c        | 292 ++++++++++++++++++++++++++++++++++++++++++++++
 src/topology/dapm.c       | 183 ++++++++++++++++++++++++++---
 src/topology/elem.c       |  17 +++
 src/topology/parser.c     |  59 +++++++++-
 src/topology/tplg_local.h |   7 ++
 6 files changed, 566 insertions(+), 18 deletions(-)
include/topology.h +26 -0
index aee43de..6ff8c5f 100644
@@ -664,6 +664,32 @@ typedef struct snd_tplg_obj_template {
 		struct snd_tplg_graph_template *graph;		/*!< Graph elements */
 	};
 } snd_tplg_obj_template_t;
+
+/**
+ * \brief Register topology template object.
+ * \param tplg Topology instance.
+ * \param t Template object.
+ * \return Zero on success, otherwise a negative error code
+ */
+int snd_tplg_add_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
+
+/**
+ * \brief Build all registered topology data into binary file.
+ * \param tplg Topology instance.
+ * \param outfile Binary topology output file.
+ * \return Zero on success, otherwise a negative error code
+ */
+int snd_tplg_build(snd_tplg_t *tplg, const char *outfile);
+
+/**
+ * \brief Attach private data to topology manifest.
+ * \param tplg Topology instance.
+ * \param data Private data.
+ * \param len Length of data in bytes.
+ * \return Zero on success, otherwise a negative error code
+ */
+int snd_tplg_set_manifest_data(snd_tplg_t *tplg, const void *data, int len);
+
 /* \} */
 
 #ifdef __cplusplus
src/topology/ctl.c +292 -0

301 lines not on this page · raw message

src/topology/dapm.c +169 -14

219 lines not on this page · raw message

src/topology/elem.c +17 -0
index daabe75..d784236 100644
@@ -35,6 +35,23 @@ int tplg_ref_add(struct tplg_elem *elem, int type, const char* id)
 	return 0;
 }
 
+/* directly add a reference elem */
+int tplg_ref_add_elem(struct tplg_elem *elem, struct tplg_elem *elem_ref)
+{
+	struct tplg_ref *ref;
+
+	ref = calloc(1, sizeof(*ref));
+	if (!ref)
+		return -ENOMEM;
+
+	ref->type = elem_ref->type;
+	ref->elem = elem_ref;
+	elem_copy_text(ref->id,  elem_ref->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
+
+	list_add_tail(&ref->list, &elem->ref_list);
+	return 0;
+}
+
 void tplg_ref_free_list(struct list_head *base)
 {
 	struct list_head *pos, *npos;
src/topology/parser.c +55 -4

72 lines not on this page · raw message

src/topology/tplg_local.h +7 -0
index ec63045..063ff9f 100644
@@ -190,6 +190,7 @@ int tplg_build_pcm_dai(snd_tplg_t *tplg, unsigned int type);
 int tplg_copy_data(struct tplg_elem *elem, struct tplg_elem *ref);
 
 int tplg_ref_add(struct tplg_elem *elem, int type, const char* id);
+int tplg_ref_add_elem(struct tplg_elem *elem, struct tplg_elem *elem_ref);
 
 struct tplg_elem *tplg_elem_new(void);
 void tplg_elem_free(struct tplg_elem *elem);
@@ -214,3 +215,9 @@ int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
 
 struct tplg_elem *lookup_pcm_dai_stream(struct list_head *base,
 	const char* id);
+
+int tplg_add_mixer_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
+int tplg_add_enum_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
+int tplg_add_bytes_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
+int tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
+int tplg_add_graph_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
Signature · 1 line
2.1.4
#4 lore raw
In reply to Liam Girdwood
Quoted 16 lines · Liam Girdwood

Currently we can build topology binary data files from topology text files. However it is sometimes necessary for DSP FW vendors to build topology binary data within a toolset and create topology binary data directly from within vendor tools.

This series adds an API to the alsa-lib topology core so that vendor tools can create topology data directly.

Changes since V2:-

o Fixed elem memory leaks on error handling paths
o Fixed indentation.
o Moved container_of and ARRAY_SIZE to local.h
o remove use of unlink() and fixed file permissions.

Quoted 4 lines

Changes since V1:-

o Split out refactoring of OBJECT_TYPE to SND_TPLG_TYPE_
o Removed inclusing of type_compat.h from asoc.h

Applied all three patches now. The third patch missed the declaration of tplg_add_mixer() & co, which leaded to compile warnings, so I fixed them manually.

thanks,

Takashi

Quoted 21 lines

Liam Girdwood (1):
core: add convenience macros to local.h

Mengdong Lin (2):
topology: Add C templates structure for building topology from C
programs
topology: A API calls to directly build topology data from templates

 include/local.h           |   7 ++
 include/topology.h        | 202 ++++++++++++++++++++++++++++++++
 src/topology/ctl.c        | 292 ++++++++++++++++++++++++++++++++++++++++++++++
 src/topology/dapm.c       | 183 ++++++++++++++++++++++++++---
 src/topology/elem.c       |  17 +++
 src/topology/parser.c     |  59 +++++++++-
 src/topology/tplg_local.h |  26 ++---
 7 files changed, 749 insertions(+), 37 deletions(-)
Signature · 1 line
2.1.4