OSDN Git Service

0c7be2001a637bd1d2ca30d7936cdc559aace83d
[android-x86/external-alsa-lib.git] / src / topology / tplg_local.h
1 /*
2  *  This library is free software; you can redistribute it and/or
3  *  modify it under the terms of the GNU Lesser General Public
4  *  License as published by the Free Software Foundation; either
5  *  version 2 of the License, or (at your option) any later version.
6  *
7  *  This library is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  Lesser General Public License for more details.
11  */
12
13 #include <limits.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16
17 #include "local.h"
18 #include "list.h"
19 #include "topology.h"
20
21 #include <sound/type_compat.h>
22 #include <sound/asound.h>
23 #include <sound/asoc.h>
24 #include <sound/tlv.h>
25
26 #ifdef TPLG_DEBUG
27 #define tplg_dbg SNDERR
28 #else
29 #define tplg_dbg(fmt, arg...) do { } while (0)
30 #endif
31
32 #define TPLG_MAX_PRIV_SIZE      (1024 * 128)
33
34 /** The name of the environment variable containing the tplg directory */
35 #define ALSA_CONFIG_TPLG_VAR "ALSA_CONFIG_TPLG"
36
37 struct tplg_ref;
38 struct tplg_elem;
39 struct tplg_table;
40
41 typedef enum _snd_pcm_rates {
42         SND_PCM_RATE_UNKNOWN = -1,
43         SND_PCM_RATE_5512 = 0,
44         SND_PCM_RATE_8000,
45         SND_PCM_RATE_11025,
46         SND_PCM_RATE_16000,
47         SND_PCM_RATE_22050,
48         SND_PCM_RATE_32000,
49         SND_PCM_RATE_44100,
50         SND_PCM_RATE_48000,
51         SND_PCM_RATE_64000,
52         SND_PCM_RATE_88200,
53         SND_PCM_RATE_96000,
54         SND_PCM_RATE_176400,
55         SND_PCM_RATE_192000,
56         SND_PCM_RATE_CONTINUOUS = 30,
57         SND_PCM_RATE_KNOT = 31,
58         SND_PCM_RATE_LAST = SND_PCM_RATE_KNOT,
59 } snd_pcm_rates_t;
60
61 struct snd_tplg {
62         /* out file */
63         unsigned char *bin;
64         size_t bin_pos;
65         size_t bin_size;
66
67         int verbose;
68         unsigned int dapm_sort: 1;
69         unsigned int version;
70
71         /* runtime state */
72         size_t next_hdr_pos;
73         int index;
74         int channel_idx;
75
76         /* manifest */
77         struct snd_soc_tplg_manifest manifest;
78         void *manifest_pdata;   /* copied by builder at file write */
79
80         /* list of each element type */
81         struct list_head tlv_list;
82         struct list_head widget_list;
83         struct list_head pcm_list;
84         struct list_head dai_list;
85         struct list_head be_list;
86         struct list_head cc_list;
87         struct list_head route_list;
88         struct list_head text_list;
89         struct list_head pdata_list;
90         struct list_head token_list;
91         struct list_head tuple_list;
92         struct list_head manifest_list;
93         struct list_head pcm_config_list;
94         struct list_head pcm_caps_list;
95         struct list_head hw_cfg_list;
96
97         /* type-specific control lists */
98         struct list_head mixer_list;
99         struct list_head enum_list;
100         struct list_head bytes_ext_list;
101 };
102
103 /* object text references */
104 struct tplg_ref {
105         unsigned int type;
106         struct tplg_elem *elem;
107         char id[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
108         struct list_head list;
109 };
110
111 struct tplg_texts {
112         unsigned int num_items;
113         char items[SND_SOC_TPLG_NUM_TEXTS][SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
114 };
115
116 /* element for vendor tokens */
117 struct tplg_token {
118         char id[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
119         unsigned int value;
120 };
121
122 struct tplg_vendor_tokens {
123         unsigned int num_tokens;
124         struct tplg_token token[0];
125 };
126
127 /* element for vendor tuples */
128 struct tplg_tuple {
129         char token[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
130         union {
131                 char string[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
132                 unsigned char uuid[16];
133                 unsigned int value;
134         };
135 };
136
137 struct tplg_tuple_set {
138         unsigned int  type; /* uuid, bool, byte, short, word, string*/
139         unsigned int  num_tuples;
140         struct tplg_tuple tuple[0];
141 };
142
143 struct tplg_vendor_tuples {
144         unsigned int num_sets;
145         unsigned int alloc_sets;
146         struct tplg_tuple_set **set;
147 };
148
149 /* topology element */
150 struct tplg_elem {
151
152         struct tplg_table *table;
153
154         char id[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
155
156         int index;
157         enum snd_tplg_type type;
158
159         int size; /* total size of this object inc pdata and ref objects */
160         int compound_elem; /* dont write this element as individual elem */
161         int vendor_type; /* vendor type for private data */
162
163         /* UAPI object for this elem */
164         union {
165                 void *obj;
166                 struct snd_soc_tplg_mixer_control *mixer_ctrl;
167                 struct snd_soc_tplg_enum_control *enum_ctrl;
168                 struct snd_soc_tplg_bytes_control *bytes_ext;
169                 struct snd_soc_tplg_dapm_widget *widget;
170                 struct snd_soc_tplg_pcm *pcm;
171                 struct snd_soc_tplg_dai *dai;
172                 struct snd_soc_tplg_link_config *link;/* physical link */
173                 struct snd_soc_tplg_dapm_graph_elem *route;
174                 struct snd_soc_tplg_stream *stream_cfg;
175                 struct snd_soc_tplg_stream_caps *stream_caps;
176                 struct snd_soc_tplg_hw_config *hw_cfg;
177
178                 /* these do not map to UAPI structs but are internal only */
179                 struct snd_soc_tplg_ctl_tlv *tlv;
180                 struct tplg_texts *texts;
181                 struct snd_soc_tplg_private *data;
182                 struct tplg_vendor_tokens *tokens;
183                 struct tplg_vendor_tuples *tuples;
184                 struct snd_soc_tplg_manifest *manifest;
185         };
186
187         /* an element may refer to other elements:
188          * a mixer control may refer to a tlv,
189          * a widget may refer to a mixer control array,
190          * a graph may refer to some widgets.
191          */
192         struct list_head ref_list;
193         struct list_head list; /* list of all elements with same type */
194
195         void (*free)(void *obj);
196 };
197
198 struct map_elem {
199         const char *name;
200         int id;
201 };
202
203 /* mapping table */
204 struct tplg_table {
205         const char *name;
206         const char *id;
207         const char *id2;
208         off_t loff;
209         size_t size;
210         int type;
211         int tsoc;
212         unsigned build: 1;
213         unsigned enew: 1;
214         void (*free)(void *);
215         int (*parse)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
216         int (*save)(snd_tplg_t *tplg, struct tplg_elem *elem,
217                     char **dst, const char *prefix);
218         int (*gsave)(snd_tplg_t *tplg, int index,
219                      char **dst, const char *prefix);
220         int (*decod)(snd_tplg_t *tplg, size_t pos,
221                      struct snd_soc_tplg_hdr *hdr,
222                      void *bin, size_t size);
223 };
224
225 extern struct tplg_table tplg_table[];
226 extern unsigned int tplg_table_items;
227
228 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
229 static inline unsigned int unaligned_get32(void *src)
230 {
231         unsigned int ret;
232         memcpy(&ret, src, sizeof(ret));
233         return ret;
234 }
235 static inline void unaligned_put32(void *dst, unsigned int val)
236 {
237         memcpy(dst, &val, sizeof(val));
238 }
239 #endif
240
241 #define tplg_log(tplg, type, pos, fmt, args...) do { \
242         if ((tplg)->verbose) \
243                 tplg_log_((tplg), (type), (pos), (fmt), ##args); \
244 } while (0)
245
246 void tplg_log_(snd_tplg_t *tplg, char type, size_t pos, const char *fmt, ...);
247
248 void *tplg_calloc(struct list_head *heap, size_t size);
249 void tplg_free(struct list_head *heap);
250
251 int tplg_get_type(int asoc_type);
252
253 int tplg_parse_compound(snd_tplg_t *tplg, snd_config_t *cfg,
254         int (*fcn)(snd_tplg_t *, snd_config_t *, void *),
255         void *private);
256
257 int tplg_write_data(snd_tplg_t *tplg);
258
259 int tplg_parse_tlv(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
260 int tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
261 int tplg_parse_data(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
262 int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
263 int tplg_parse_tuples(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
264 int tplg_parse_manifest_data(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
265 int tplg_parse_control_bytes(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
266 int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
267 int tplg_parse_control_mixer(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
268 int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
269 int tplg_parse_dapm_widget(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
270 int tplg_parse_stream_caps(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
271 int tplg_parse_pcm(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
272 int tplg_parse_dai(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
273 int tplg_parse_link(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
274 int tplg_parse_cc(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
275 int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
276
277 unsigned int tplg_get_tuple_size(int type);
278 void tplg_free_tuples(void *obj);
279
280 int tplg_build_data(snd_tplg_t *tplg);
281 int tplg_build_manifest_data(snd_tplg_t *tplg);
282 int tplg_build_controls(snd_tplg_t *tplg);
283 int tplg_build_widgets(snd_tplg_t *tplg);
284 int tplg_build_routes(snd_tplg_t *tplg);
285 int tplg_build_pcm_dai(snd_tplg_t *tplg, unsigned int type);
286
287 int tplg_copy_data(snd_tplg_t *tplg, struct tplg_elem *elem,
288                    struct tplg_ref *ref);
289
290 int tplg_parse_refs(snd_config_t *cfg, struct tplg_elem *elem,
291                     unsigned int type);
292
293 int tplg_ref_add(struct tplg_elem *elem, int type, const char* id);
294 int tplg_ref_add_elem(struct tplg_elem *elem, struct tplg_elem *elem_ref);
295
296 struct tplg_elem *tplg_elem_new(void);
297 void tplg_elem_free(struct tplg_elem *elem);
298 void tplg_elem_free_list(struct list_head *base);
299 void tplg_elem_insert(struct tplg_elem *elem_p, struct list_head *list);
300 struct tplg_elem *tplg_elem_lookup(struct list_head *base,
301                                 const char* id,
302                                 unsigned int type,
303                                 int index);
304 struct tplg_elem *tplg_elem_type_lookup(snd_tplg_t *tplg,
305                                         enum snd_tplg_type type);
306 struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
307         snd_config_t *cfg, const char *name, enum snd_tplg_type type);
308
309 int tplg_get_integer(snd_config_t *n, int *val, int base);
310 int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base);
311
312 const char *tplg_channel_name(int type);
313 int tplg_parse_channel(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
314         snd_config_t *cfg, void *private);
315
316 const char *tplg_ops_name(int type);
317 int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
318         snd_config_t *cfg, void *private);
319 int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
320         snd_config_t *cfg, void *private);
321
322 struct tplg_elem *lookup_pcm_dai_stream(struct list_head *base,
323         const char* id);
324
325 int tplg_add_data(snd_tplg_t *tplg, struct tplg_elem *parent,
326                   const void *bin, size_t size);
327 int tplg_add_data_bytes(snd_tplg_t *tplg, struct tplg_elem *parent,
328                         const char *suffix, const void *bin, size_t size);
329 int tplg_add_mixer_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
330 int tplg_add_enum_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
331 int tplg_add_bytes_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
332 int tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
333 int tplg_add_graph_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
334
335 int tplg_add_mixer(snd_tplg_t *tplg, struct snd_tplg_mixer_template *mixer,
336                    struct tplg_elem **e);
337 int tplg_add_enum(snd_tplg_t *tplg, struct snd_tplg_enum_template *enum_ctl,
338                   struct tplg_elem **e);
339 int tplg_add_bytes(snd_tplg_t *tplg, struct snd_tplg_bytes_template *bytes_ctl,
340                    struct tplg_elem **e);
341
342 int tplg_build_pcms(snd_tplg_t *tplg, unsigned int type);
343 int tplg_build_dais(snd_tplg_t *tplg, unsigned int type);
344 int tplg_build_links(snd_tplg_t *tplg, unsigned int type);
345 int tplg_add_link_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
346 int tplg_add_pcm_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
347 int tplg_add_dai_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
348
349 int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value);
350
351 int tplg_save_printf(char **dst, const char *prefix, const char *fmt, ...);
352 int tplg_save_refs(snd_tplg_t *tplg, struct tplg_elem *elem, unsigned int type,
353                    const char *id, char **dst, const char *pfx);
354 int tplg_save_channels(snd_tplg_t *tplg, struct snd_soc_tplg_channel *channel,
355                        unsigned int channel_count, char **dst, const char *pfx);
356 int tplg_save_ops(snd_tplg_t *tplg, struct snd_soc_tplg_ctl_hdr *hdr,
357                   char **dst, const char *pfx);
358 int tplg_save_ext_ops(snd_tplg_t *tplg, struct snd_soc_tplg_bytes_control *be,
359                       char **dst, const char *pfx);
360 int tplg_save_manifest_data(snd_tplg_t *tplg, struct tplg_elem *elem,
361                             char **dst, const char *pfx);
362 int tplg_save_control_mixer(snd_tplg_t *tplg, struct tplg_elem *elem,
363                             char **dst, const char *pfx);
364 int tplg_save_control_enum(snd_tplg_t *tplg, struct tplg_elem *elem,
365                            char **dst, const char *pfx);
366 int tplg_save_control_bytes(snd_tplg_t *tplg, struct tplg_elem *elem,
367                             char **dst, const char *pfx);
368 int tplg_save_tlv(snd_tplg_t *tplg, struct tplg_elem *elem,
369                   char **dst, const char *pfx);
370 int tplg_save_data(snd_tplg_t *tplg, struct tplg_elem *elem,
371                    char **dst, const char *pfx);
372 int tplg_save_text(snd_tplg_t *tplg, struct tplg_elem *elem,
373                    char **dst, const char *pfx);
374 int tplg_save_tokens(snd_tplg_t *tplg, struct tplg_elem *elem,
375                      char **dst, const char *pfx);
376 int tplg_save_tuples(snd_tplg_t *tplg, struct tplg_elem *elem,
377                      char **dst, const char *pfx);
378 int tplg_save_dapm_graph(snd_tplg_t *tplg, int index,
379                          char **dst, const char *pfx);
380 int tplg_save_dapm_widget(snd_tplg_t *tplg, struct tplg_elem *elem,
381                           char **dst, const char *pfx);
382 int tplg_save_link(snd_tplg_t *tplg, struct tplg_elem *elem,
383                    char **dst, const char *pfx);
384 int tplg_save_cc(snd_tplg_t *tplg, struct tplg_elem *elem,
385                  char **dst, const char *pfx);
386 int tplg_save_pcm(snd_tplg_t *tplg, struct tplg_elem *elem,
387                   char **dst, const char *pfx);
388 int tplg_save_hw_config(snd_tplg_t *tplg, struct tplg_elem *elem,
389                         char **dst, const char *pfx);
390 int tplg_save_stream_caps(snd_tplg_t *tplg, struct tplg_elem *elem,
391                           char **dst, const char *pfx);
392 int tplg_save_dai(snd_tplg_t *tplg, struct tplg_elem *elem,
393                   char **dst, const char *pfx);
394
395 int tplg_decode_template(snd_tplg_t *tplg,
396                          size_t pos,
397                          struct snd_soc_tplg_hdr *hdr,
398                          snd_tplg_obj_template_t *t);
399 int tplg_decode_manifest_data(snd_tplg_t *tplg, size_t pos,
400                               struct snd_soc_tplg_hdr *hdr,
401                               void *bin, size_t size);
402 int tplg_decode_control_mixer1(snd_tplg_t *tplg,
403                                struct list_head *heap,
404                                struct snd_tplg_mixer_template *mt,
405                                size_t pos,
406                                void *bin, size_t size);
407 int tplg_decode_control_mixer(snd_tplg_t *tplg, size_t pos,
408                               struct snd_soc_tplg_hdr *hdr,
409                               void *bin, size_t size);
410 int tplg_decode_control_enum1(snd_tplg_t *tplg,
411                               struct list_head *heap,
412                               struct snd_tplg_enum_template *et,
413                               size_t pos,
414                               struct snd_soc_tplg_enum_control *ec);
415 int tplg_decode_control_enum(snd_tplg_t *tplg, size_t pos,
416                              struct snd_soc_tplg_hdr *hdr,
417                              void *bin, size_t size);
418 int tplg_decode_control_bytes1(snd_tplg_t *tplg,
419                                struct snd_tplg_bytes_template *bt,
420                                size_t pos,
421                                void *bin, size_t size);
422 int tplg_decode_control_bytes(snd_tplg_t *tplg, size_t pos,
423                               struct snd_soc_tplg_hdr *hdr,
424                               void *bin, size_t size);
425 int tplg_decode_data(snd_tplg_t *tplg, size_t pos,
426                      struct snd_soc_tplg_hdr *hdr,
427                      void *bin, size_t size);
428 int tplg_decode_dapm_graph(snd_tplg_t *tplg, size_t pos,
429                            struct snd_soc_tplg_hdr *hdr,
430                            void *bin, size_t size);
431 int tplg_decode_dapm_widget(snd_tplg_t *tplg, size_t pos,
432                             struct snd_soc_tplg_hdr *hdr,
433                             void *bin, size_t size);
434 int tplg_decode_link(snd_tplg_t *tplg, size_t pos,
435                      struct snd_soc_tplg_hdr *hdr,
436                      void *bin, size_t size);
437 int tplg_decode_cc(snd_tplg_t *tplg, size_t pos,
438                    struct snd_soc_tplg_hdr *hdr,
439                    void *bin, size_t size);
440 int tplg_decode_pcm(snd_tplg_t *tplg, size_t pos,
441                     struct snd_soc_tplg_hdr *hdr,
442                     void *bin, size_t size);
443 int tplg_decode_dai(snd_tplg_t *tplg, size_t pos,
444                     struct snd_soc_tplg_hdr *hdr,
445                     void *bin, size_t size);