OSDN Git Service

topology: improve the printf buffer management
[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 /* output buffer */
204 struct tplg_buf {
205         char *dst;
206         size_t dst_len;
207         char *printf_buf;
208         size_t printf_buf_size;
209 };
210
211 /* mapping table */
212 struct tplg_table {
213         const char *name;
214         const char *id;
215         const char *id2;
216         off_t loff;
217         size_t size;
218         int type;
219         int tsoc;
220         unsigned build: 1;
221         unsigned enew: 1;
222         void (*free)(void *);
223         int (*parse)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
224         int (*save)(snd_tplg_t *tplg, struct tplg_elem *elem,
225                     struct tplg_buf *dst, const char *prefix);
226         int (*gsave)(snd_tplg_t *tplg, int index,
227                      struct tplg_buf *dst, const char *prefix);
228         int (*decod)(snd_tplg_t *tplg, size_t pos,
229                      struct snd_soc_tplg_hdr *hdr,
230                      void *bin, size_t size);
231 };
232
233 extern struct tplg_table tplg_table[];
234 extern unsigned int tplg_table_items;
235
236 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
237 static inline unsigned int unaligned_get32(void *src)
238 {
239         unsigned int ret;
240         memcpy(&ret, src, sizeof(ret));
241         return ret;
242 }
243 static inline void unaligned_put32(void *dst, unsigned int val)
244 {
245         memcpy(dst, &val, sizeof(val));
246 }
247 #endif
248
249 #define tplg_log(tplg, type, pos, fmt, args...) do { \
250         if ((tplg)->verbose) \
251                 tplg_log_((tplg), (type), (pos), (fmt), ##args); \
252 } while (0)
253
254 void tplg_log_(snd_tplg_t *tplg, char type, size_t pos, const char *fmt, ...);
255
256 void *tplg_calloc(struct list_head *heap, size_t size);
257 void tplg_free(struct list_head *heap);
258
259 int tplg_get_type(int asoc_type);
260
261 int tplg_parse_compound(snd_tplg_t *tplg, snd_config_t *cfg,
262         int (*fcn)(snd_tplg_t *, snd_config_t *, void *),
263         void *private);
264
265 int tplg_write_data(snd_tplg_t *tplg);
266
267 int tplg_parse_tlv(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
268 int tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
269 int tplg_parse_data(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
270 int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
271 int tplg_parse_tuples(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
272 int tplg_parse_manifest_data(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
273 int tplg_parse_control_bytes(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
274 int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
275 int tplg_parse_control_mixer(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
276 int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
277 int tplg_parse_dapm_widget(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
278 int tplg_parse_stream_caps(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
279 int tplg_parse_pcm(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
280 int tplg_parse_dai(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
281 int tplg_parse_link(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
282 int tplg_parse_cc(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
283 int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
284
285 unsigned int tplg_get_tuple_size(int type);
286 void tplg_free_tuples(void *obj);
287
288 int tplg_build_data(snd_tplg_t *tplg);
289 int tplg_build_manifest_data(snd_tplg_t *tplg);
290 int tplg_build_controls(snd_tplg_t *tplg);
291 int tplg_build_widgets(snd_tplg_t *tplg);
292 int tplg_build_routes(snd_tplg_t *tplg);
293 int tplg_build_pcm_dai(snd_tplg_t *tplg, unsigned int type);
294
295 int tplg_copy_data(snd_tplg_t *tplg, struct tplg_elem *elem,
296                    struct tplg_ref *ref);
297
298 int tplg_parse_refs(snd_config_t *cfg, struct tplg_elem *elem,
299                     unsigned int type);
300
301 int tplg_ref_add(struct tplg_elem *elem, int type, const char* id);
302 int tplg_ref_add_elem(struct tplg_elem *elem, struct tplg_elem *elem_ref);
303
304 struct tplg_elem *tplg_elem_new(void);
305 void tplg_elem_free(struct tplg_elem *elem);
306 void tplg_elem_free_list(struct list_head *base);
307 void tplg_elem_insert(struct tplg_elem *elem_p, struct list_head *list);
308 struct tplg_elem *tplg_elem_lookup(struct list_head *base,
309                                 const char* id,
310                                 unsigned int type,
311                                 int index);
312 struct tplg_elem *tplg_elem_type_lookup(snd_tplg_t *tplg,
313                                         enum snd_tplg_type type);
314 struct tplg_elem* tplg_elem_new_common(snd_tplg_t *tplg,
315         snd_config_t *cfg, const char *name, enum snd_tplg_type type);
316
317 int tplg_get_integer(snd_config_t *n, int *val, int base);
318 int tplg_get_unsigned(snd_config_t *n, unsigned *val, int base);
319
320 const char *tplg_channel_name(int type);
321 int tplg_parse_channel(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
322         snd_config_t *cfg, void *private);
323
324 const char *tplg_ops_name(int type);
325 int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
326         snd_config_t *cfg, void *private);
327 int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
328         snd_config_t *cfg, void *private);
329
330 struct tplg_elem *lookup_pcm_dai_stream(struct list_head *base,
331         const char* id);
332
333 int tplg_add_data(snd_tplg_t *tplg, struct tplg_elem *parent,
334                   const void *bin, size_t size);
335 int tplg_add_data_bytes(snd_tplg_t *tplg, struct tplg_elem *parent,
336                         const char *suffix, const void *bin, size_t size);
337 int tplg_add_mixer_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
338 int tplg_add_enum_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
339 int tplg_add_bytes_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
340 int tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
341 int tplg_add_graph_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
342
343 int tplg_add_mixer(snd_tplg_t *tplg, struct snd_tplg_mixer_template *mixer,
344                    struct tplg_elem **e);
345 int tplg_add_enum(snd_tplg_t *tplg, struct snd_tplg_enum_template *enum_ctl,
346                   struct tplg_elem **e);
347 int tplg_add_bytes(snd_tplg_t *tplg, struct snd_tplg_bytes_template *bytes_ctl,
348                    struct tplg_elem **e);
349
350 int tplg_build_pcms(snd_tplg_t *tplg, unsigned int type);
351 int tplg_build_dais(snd_tplg_t *tplg, unsigned int type);
352 int tplg_build_links(snd_tplg_t *tplg, unsigned int type);
353 int tplg_add_link_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
354 int tplg_add_pcm_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
355 int tplg_add_dai_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
356
357 int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value);
358
359 int tplg_save_printf(struct tplg_buf *dst, const char *prefix, const char *fmt, ...);
360 int tplg_save_refs(snd_tplg_t *tplg, struct tplg_elem *elem, unsigned int type,
361                    const char *id, struct tplg_buf *dst, const char *pfx);
362 int tplg_save_channels(snd_tplg_t *tplg, struct snd_soc_tplg_channel *channel,
363                        unsigned int channel_count, struct tplg_buf *dst, const char *pfx);
364 int tplg_save_ops(snd_tplg_t *tplg, struct snd_soc_tplg_ctl_hdr *hdr,
365                   struct tplg_buf *dst, const char *pfx);
366 int tplg_save_ext_ops(snd_tplg_t *tplg, struct snd_soc_tplg_bytes_control *be,
367                       struct tplg_buf *dst, const char *pfx);
368 int tplg_save_manifest_data(snd_tplg_t *tplg, struct tplg_elem *elem,
369                             struct tplg_buf *dst, const char *pfx);
370 int tplg_save_control_mixer(snd_tplg_t *tplg, struct tplg_elem *elem,
371                             struct tplg_buf *dst, const char *pfx);
372 int tplg_save_control_enum(snd_tplg_t *tplg, struct tplg_elem *elem,
373                            struct tplg_buf *dst, const char *pfx);
374 int tplg_save_control_bytes(snd_tplg_t *tplg, struct tplg_elem *elem,
375                             struct tplg_buf *dst, const char *pfx);
376 int tplg_save_tlv(snd_tplg_t *tplg, struct tplg_elem *elem,
377                   struct tplg_buf *dst, const char *pfx);
378 int tplg_save_data(snd_tplg_t *tplg, struct tplg_elem *elem,
379                    struct tplg_buf *dst, const char *pfx);
380 int tplg_save_text(snd_tplg_t *tplg, struct tplg_elem *elem,
381                    struct tplg_buf *dst, const char *pfx);
382 int tplg_save_tokens(snd_tplg_t *tplg, struct tplg_elem *elem,
383                      struct tplg_buf *dst, const char *pfx);
384 int tplg_save_tuples(snd_tplg_t *tplg, struct tplg_elem *elem,
385                      struct tplg_buf *dst, const char *pfx);
386 int tplg_save_dapm_graph(snd_tplg_t *tplg, int index,
387                          struct tplg_buf *dst, const char *pfx);
388 int tplg_save_dapm_widget(snd_tplg_t *tplg, struct tplg_elem *elem,
389                           struct tplg_buf *dst, const char *pfx);
390 int tplg_save_link(snd_tplg_t *tplg, struct tplg_elem *elem,
391                    struct tplg_buf *dst, const char *pfx);
392 int tplg_save_cc(snd_tplg_t *tplg, struct tplg_elem *elem,
393                  struct tplg_buf *dst, const char *pfx);
394 int tplg_save_pcm(snd_tplg_t *tplg, struct tplg_elem *elem,
395                   struct tplg_buf *dst, const char *pfx);
396 int tplg_save_hw_config(snd_tplg_t *tplg, struct tplg_elem *elem,
397                         struct tplg_buf *dst, const char *pfx);
398 int tplg_save_stream_caps(snd_tplg_t *tplg, struct tplg_elem *elem,
399                           struct tplg_buf *dst, const char *pfx);
400 int tplg_save_dai(snd_tplg_t *tplg, struct tplg_elem *elem,
401                   struct tplg_buf *dst, const char *pfx);
402
403 int tplg_decode_template(snd_tplg_t *tplg,
404                          size_t pos,
405                          struct snd_soc_tplg_hdr *hdr,
406                          snd_tplg_obj_template_t *t);
407 int tplg_decode_manifest_data(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_mixer1(snd_tplg_t *tplg,
411                                struct list_head *heap,
412                                struct snd_tplg_mixer_template *mt,
413                                size_t pos,
414                                void *bin, size_t size);
415 int tplg_decode_control_mixer(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_enum1(snd_tplg_t *tplg,
419                               struct list_head *heap,
420                               struct snd_tplg_enum_template *et,
421                               size_t pos,
422                               struct snd_soc_tplg_enum_control *ec);
423 int tplg_decode_control_enum(snd_tplg_t *tplg, size_t pos,
424                              struct snd_soc_tplg_hdr *hdr,
425                              void *bin, size_t size);
426 int tplg_decode_control_bytes1(snd_tplg_t *tplg,
427                                struct snd_tplg_bytes_template *bt,
428                                size_t pos,
429                                void *bin, size_t size);
430 int tplg_decode_control_bytes(snd_tplg_t *tplg, size_t pos,
431                               struct snd_soc_tplg_hdr *hdr,
432                               void *bin, size_t size);
433 int tplg_decode_data(snd_tplg_t *tplg, size_t pos,
434                      struct snd_soc_tplg_hdr *hdr,
435                      void *bin, size_t size);
436 int tplg_decode_dapm_graph(snd_tplg_t *tplg, size_t pos,
437                            struct snd_soc_tplg_hdr *hdr,
438                            void *bin, size_t size);
439 int tplg_decode_dapm_widget(snd_tplg_t *tplg, size_t pos,
440                             struct snd_soc_tplg_hdr *hdr,
441                             void *bin, size_t size);
442 int tplg_decode_link(snd_tplg_t *tplg, size_t pos,
443                      struct snd_soc_tplg_hdr *hdr,
444                      void *bin, size_t size);
445 int tplg_decode_cc(snd_tplg_t *tplg, size_t pos,
446                    struct snd_soc_tplg_hdr *hdr,
447                    void *bin, size_t size);
448 int tplg_decode_pcm(snd_tplg_t *tplg, size_t pos,
449                     struct snd_soc_tplg_hdr *hdr,
450                     void *bin, size_t size);
451 int tplg_decode_dai(snd_tplg_t *tplg, size_t pos,
452                     struct snd_soc_tplg_hdr *hdr,
453                     void *bin, size_t size);