1 /*
2 Copyright(c) 2014-2015 Intel Corporation
3 All rights reserved.
4
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 Authors: Mengdong Lin <mengdong.lin@intel.com>
16 Yao Jin <yao.jin@intel.com>
17 Liam Girdwood <liam.r.girdwood@linux.intel.com>
18 */
19
20 #include "tplg_local.h"
21
22 /* mapping of widget text names to types */
23 static const struct map_elem widget_map[] = {
24 {"input", SND_SOC_TPLG_DAPM_INPUT},
25 {"output", SND_SOC_TPLG_DAPM_OUTPUT},
26 {"mux", SND_SOC_TPLG_DAPM_MUX},
27 {"mixer", SND_SOC_TPLG_DAPM_MIXER},
28 {"pga", SND_SOC_TPLG_DAPM_PGA},
29 {"out_drv", SND_SOC_TPLG_DAPM_OUT_DRV},
30 {"adc", SND_SOC_TPLG_DAPM_ADC},
31 {"dac", SND_SOC_TPLG_DAPM_DAC},
32 {"switch", SND_SOC_TPLG_DAPM_SWITCH},
33 {"pre", SND_SOC_TPLG_DAPM_PRE},
34 {"post", SND_SOC_TPLG_DAPM_POST},
35 {"aif_in", SND_SOC_TPLG_DAPM_AIF_IN},
36 {"aif_out", SND_SOC_TPLG_DAPM_AIF_OUT},
37 {"dai_in", SND_SOC_TPLG_DAPM_DAI_IN},
38 {"dai_out", SND_SOC_TPLG_DAPM_DAI_OUT},
39 {"dai_link", SND_SOC_TPLG_DAPM_DAI_LINK},
40 {"buffer", SND_SOC_TPLG_DAPM_BUFFER},
41 {"scheduler", SND_SOC_TPLG_DAPM_SCHEDULER},
42 {"effect", SND_SOC_TPLG_DAPM_EFFECT},
43 {"siggen", SND_SOC_TPLG_DAPM_SIGGEN},
44 {"src", SND_SOC_TPLG_DAPM_SRC},
45 {"asrc", SND_SOC_TPLG_DAPM_ASRC},
46 {"encoder", SND_SOC_TPLG_DAPM_ENCODER},
47 {"decoder", SND_SOC_TPLG_DAPM_DECODER},
48 };
49
lookup_widget(const char *w)50 static int lookup_widget(const char *w)
51 {
52 unsigned int i;
53
54 for (i = 0; i < ARRAY_SIZE(widget_map); i++) {
55 if (strcmp(widget_map[i].name, w) == 0)
56 return widget_map[i].id;
57 }
58
59 return -EINVAL;
60 }
61
get_widget_name(unsigned int type)62 static const char *get_widget_name(unsigned int type)
63 {
64 unsigned int i;
65
66 for (i = 0; i < ARRAY_SIZE(widget_map); i++) {
67 if ((unsigned int)widget_map[i].id == type)
68 return widget_map[i].name;
69 }
70
71 return NULL;
72 }
73
74 /* move referenced controls to the widget */
copy_dapm_control(struct tplg_elem *elem, struct tplg_elem *ref)75 static int copy_dapm_control(struct tplg_elem *elem, struct tplg_elem *ref)
76 {
77 struct snd_soc_tplg_dapm_widget *widget = elem->widget;
78
79 tplg_dbg("Control '%s' used by '%s'", ref->id, elem->id);
80 tplg_dbg("\tparent size: %d + %d -> %d, priv size -> %d",
81 elem->size, ref->size, elem->size + ref->size,
82 widget->priv.size);
83
84 widget = realloc(widget, elem->size + ref->size);
85 if (!widget)
86 return -ENOMEM;
87
88 elem->widget = widget;
89
90 /* append the control to the end of the widget */
91 memcpy((void*)widget + elem->size, ref->obj, ref->size);
92 elem->size += ref->size;
93
94 widget->num_kcontrols++;
95 ref->compound_elem = 1;
96 return 0;
97 }
98
99 /* check referenced controls for a widget */
tplg_build_widget(snd_tplg_t *tplg, struct tplg_elem *elem)100 static int tplg_build_widget(snd_tplg_t *tplg, struct tplg_elem *elem)
101 {
102 struct tplg_ref *ref;
103 struct list_head *base, *pos;
104 int err = 0;
105
106 base = &elem->ref_list;
107
108 /* A widget's private data sits before the embedded controls.
109 * So merge the private data blocks at first
110 */
111 list_for_each(pos, base) {
112 ref = list_entry(pos, struct tplg_ref, list);
113
114 if (ref->type != SND_TPLG_TYPE_DATA)
115 continue;
116
117 err = tplg_copy_data(tplg, elem, ref);
118 if (err < 0)
119 return err;
120 }
121
122 /* Merge the embedded controls */
123 list_for_each(pos, base) {
124
125 ref = list_entry(pos, struct tplg_ref, list);
126
127 switch (ref->type) {
128 case SND_TPLG_TYPE_MIXER:
129 if (!ref->elem)
130 ref->elem = tplg_elem_lookup(&tplg->mixer_list,
131 ref->id, SND_TPLG_TYPE_MIXER, elem->index);
132 if (ref->elem)
133 err = copy_dapm_control(elem, ref->elem);
134 break;
135
136 case SND_TPLG_TYPE_ENUM:
137 if (!ref->elem)
138 ref->elem = tplg_elem_lookup(&tplg->enum_list,
139 ref->id, SND_TPLG_TYPE_ENUM, elem->index);
140 if (ref->elem)
141 err = copy_dapm_control(elem, ref->elem);
142 break;
143
144 case SND_TPLG_TYPE_BYTES:
145 if (!ref->elem)
146 ref->elem = tplg_elem_lookup(&tplg->bytes_ext_list,
147 ref->id, SND_TPLG_TYPE_BYTES, elem->index);
148 if (ref->elem)
149 err = copy_dapm_control(elem, ref->elem);
150 break;
151
152 default:
153 break;
154 }
155
156 if (!ref->elem) {
157 SNDERR("cannot find '%s' referenced by widget '%s'",
158 ref->id, elem->id);
159 return -EINVAL;
160 }
161
162 if (err < 0)
163 return err;
164 }
165
166 return 0;
167 }
168
tplg_build_widgets(snd_tplg_t *tplg)169 int tplg_build_widgets(snd_tplg_t *tplg)
170 {
171
172 struct list_head *base, *pos;
173 struct tplg_elem *elem;
174 int err;
175
176 base = &tplg->widget_list;
177 list_for_each(pos, base) {
178
179 elem = list_entry(pos, struct tplg_elem, list);
180 if (!elem->widget || elem->type != SND_TPLG_TYPE_DAPM_WIDGET) {
181 SNDERR("invalid widget '%s'", elem->id);
182 return -EINVAL;
183 }
184
185 err = tplg_build_widget(tplg, elem);
186 if (err < 0)
187 return err;
188
189 /* add widget to manifest */
190 tplg->manifest.widget_elems++;
191 }
192
193 return 0;
194 }
195
tplg_build_routes(snd_tplg_t *tplg)196 int tplg_build_routes(snd_tplg_t *tplg)
197 {
198 struct list_head *base, *pos;
199 struct tplg_elem *elem;
200 struct snd_soc_tplg_dapm_graph_elem *route;
201
202 base = &tplg->route_list;
203
204 list_for_each(pos, base) {
205 elem = list_entry(pos, struct tplg_elem, list);
206
207 if (!elem->route || elem->type != SND_TPLG_TYPE_DAPM_GRAPH) {
208 SNDERR("invalid route '%s'", elem->id);
209 return -EINVAL;
210 }
211
212 route = elem->route;
213 tplg_dbg("Check route: sink '%s', control '%s', source '%s'",
214 route->sink, route->control, route->source);
215
216 /* validate sink */
217 if (strlen(route->sink) <= 0) {
218 SNDERR("no sink");
219 return -EINVAL;
220
221 }
222 if (!tplg_elem_lookup(&tplg->widget_list, route->sink,
223 SND_TPLG_TYPE_DAPM_WIDGET, SND_TPLG_INDEX_ALL)) {
224 SNDERR("undefined sink widget/stream '%s'", route->sink);
225 }
226
227 /* validate control name */
228 if (strlen(route->control)) {
229 if (!tplg_elem_lookup(&tplg->mixer_list, route->control,
230 SND_TPLG_TYPE_MIXER, elem->index) &&
231 !tplg_elem_lookup(&tplg->enum_list, route->control,
232 SND_TPLG_TYPE_ENUM, elem->index)) {
233 SNDERR("undefined mixer/enum control '%s'",
234 route->control);
235 }
236 }
237
238 /* validate source */
239 if (strlen(route->source) <= 0) {
240 SNDERR("no source");
241 return -EINVAL;
242
243 }
244 if (!tplg_elem_lookup(&tplg->widget_list, route->source,
245 SND_TPLG_TYPE_DAPM_WIDGET, SND_TPLG_INDEX_ALL)) {
246 SNDERR("undefined source widget/stream '%s'",
247 route->source);
248 }
249
250 /* add graph to manifest */
251 tplg->manifest.graph_elems++;
252 }
253
254 return 0;
255 }
256
tplg_elem_new_route(snd_tplg_t *tplg, int index)257 struct tplg_elem *tplg_elem_new_route(snd_tplg_t *tplg, int index)
258 {
259 struct tplg_elem *elem;
260 struct snd_soc_tplg_dapm_graph_elem *line;
261
262 elem = tplg_elem_new();
263 if (!elem)
264 return NULL;
265
266 elem->index = index;
267 if (tplg->dapm_sort)
268 tplg_elem_insert(elem, &tplg->route_list);
269 else
270 list_add_tail(&elem->list, &tplg->route_list);
271 strcpy(elem->id, "line");
272 elem->type = SND_TPLG_TYPE_DAPM_GRAPH;
273 elem->size = sizeof(*line);
274
275 line = calloc(1, sizeof(*line));
276 if (!line) {
277 tplg_elem_free(elem);
278 return NULL;
279 }
280 elem->route = line;
281
282 return elem;
283 }
284
285 #define LINE_SIZE 1024
286
287 /* line is defined as '"sink, control, source"' */
tplg_parse_line(const char *text, struct snd_soc_tplg_dapm_graph_elem *line)288 static int tplg_parse_line(const char *text,
289 struct snd_soc_tplg_dapm_graph_elem *line)
290 {
291 char buf[LINE_SIZE];
292 unsigned int len, i;
293 const char *source = NULL, *sink = NULL, *control = NULL;
294
295 snd_strlcpy(buf, text, LINE_SIZE);
296
297 len = strlen(buf);
298 if (len <= 2) {
299 SNDERR("invalid route \"%s\"", buf);
300 return -EINVAL;
301 }
302
303 /* find first , */
304 for (i = 1; i < len; i++) {
305 if (buf[i] == ',')
306 goto second;
307 }
308 SNDERR("invalid route \"%s\"", buf);
309 return -EINVAL;
310
311 second:
312 /* find second , */
313 sink = buf;
314 control = &buf[i + 2];
315 buf[i] = 0;
316
317 for (; i < len; i++) {
318 if (buf[i] == ',')
319 goto done;
320 }
321
322 SNDERR("invalid route \"%s\"", buf);
323 return -EINVAL;
324
325 done:
326 buf[i] = 0;
327 source = &buf[i + 2];
328
329 strcpy(line->source, source);
330 strcpy(line->control, control);
331 strcpy(line->sink, sink);
332 return 0;
333 }
334
335
tplg_parse_routes(snd_tplg_t *tplg, snd_config_t *cfg, int index)336 static int tplg_parse_routes(snd_tplg_t *tplg, snd_config_t *cfg, int index)
337 {
338 snd_config_iterator_t i, next;
339 snd_config_t *n;
340 struct tplg_elem *elem;
341 struct snd_soc_tplg_dapm_graph_elem *line;
342 int err;
343
344 snd_config_for_each(i, next, cfg) {
345 const char *val;
346
347 n = snd_config_iterator_entry(i);
348 if (snd_config_get_string(n, &val) < 0)
349 continue;
350
351 elem = tplg_elem_new_route(tplg, index);
352 if (!elem)
353 return -ENOMEM;
354 line = elem->route;
355
356 err = tplg_parse_line(val, line);
357 if (err < 0)
358 return err;
359
360 tplg_dbg("route: sink '%s', control '%s', source '%s'",
361 line->sink, line->control, line->source);
362 }
363
364 return 0;
365 }
366
tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED)367 int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg,
368 void *private ATTRIBUTE_UNUSED)
369 {
370 snd_config_iterator_t i, next;
371 snd_config_t *n;
372 int err;
373 const char *graph_id;
374 int index = -1;
375
376 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
377 SNDERR("compound is expected for dapm graph definition");
378 return -EINVAL;
379 }
380
381 snd_config_get_id(cfg, &graph_id);
382
383 snd_config_for_each(i, next, cfg) {
384 const char *id;
385
386 n = snd_config_iterator_entry(i);
387 if (snd_config_get_id(n, &id) < 0) {
388 continue;
389 }
390
391 if (strcmp(id, "index") == 0) {
392 if (tplg_get_integer(n, &index, 0))
393 return -EINVAL;
394 if (index < 0)
395 return -EINVAL;
396 }
397
398 if (strcmp(id, "lines") == 0) {
399 if (index < 0) {
400 SNDERR("failed to parse dapm graph %s, missing index",
401 graph_id);
402 return -EINVAL;
403 }
404 err = tplg_parse_routes(tplg, n, index);
405 if (err < 0) {
406 SNDERR("failed to parse dapm graph %s",
407 graph_id);
408 return err;
409 }
410 continue;
411 }
412 }
413
414 return 0;
415 }
416
417 /* save DAPM graph */
tplg_save_dapm_graph(snd_tplg_t *tplg, int index, struct tplg_buf *dst, const char *pfx)418 int tplg_save_dapm_graph(snd_tplg_t *tplg, int index,
419 struct tplg_buf *dst, const char *pfx)
420 {
421 struct snd_soc_tplg_dapm_graph_elem *route;
422 struct list_head *pos;
423 struct tplg_elem *elem;
424 int err, first, old_index;
425 unsigned block, count;
426 const char *fmt;
427
428 old_index = -1;
429 block = 0;
430 count = 0;
431 list_for_each(pos, &tplg->route_list) {
432 elem = list_entry(pos, struct tplg_elem, list);
433 if (!elem->route || elem->type != SND_TPLG_TYPE_DAPM_GRAPH)
434 continue;
435 if (index >= 0 && elem->index != index)
436 continue;
437 if (old_index != elem->index) {
438 block++;
439 old_index = elem->index;
440 }
441 count++;
442 }
443 if (count == 0)
444 return 0;
445 if (block < 10) {
446 fmt = "\tset%u {\n";
447 } else if (block < 100) {
448 fmt = "\tset%02u {\n";
449 } else if (block < 1000) {
450 fmt = "\tset%03u {\n";
451 } else {
452 return -EINVAL;
453 }
454 old_index = -1;
455 block = -1;
456 first = 1;
457 err = tplg_save_printf(dst, pfx, "SectionGraph {\n");
458 list_for_each(pos, &tplg->route_list) {
459 elem = list_entry(pos, struct tplg_elem, list);
460 if (!elem->route || elem->type != SND_TPLG_TYPE_DAPM_GRAPH)
461 continue;
462 if (index >= 0 && elem->index != index)
463 continue;
464 if (old_index != elem->index) {
465 if (old_index >= 0) {
466 err = tplg_save_printf(dst, pfx, "\t\t]\n");
467 if (err < 0)
468 return err;
469 err = tplg_save_printf(dst, pfx, "\t}\n");
470 if (err < 0)
471 return err;
472 }
473 old_index = elem->index;
474 block++;
475 first = 1;
476 err = tplg_save_printf(dst, pfx, fmt, block);
477 if (err >= 0)
478 err = tplg_save_printf(dst, pfx, "\t\tindex %u\n",
479 elem->index);
480 if (err < 0)
481 return err;
482 }
483 if (first) {
484 first = 0;
485 err = tplg_save_printf(dst, pfx, "\t\tlines [\n");
486 if (err < 0)
487 return err;
488 }
489 route = elem->route;
490 err = tplg_save_printf(dst, pfx, "\t\t\t'%s, %s, %s'\n",
491 route->sink, route->control,
492 route->source);
493 if (err < 0)
494 return err;
495 }
496
497 if (!first) {
498 if (err >= 0)
499 err = tplg_save_printf(dst, pfx, "\t\t]\n");
500 if (err >= 0)
501 err = tplg_save_printf(dst, pfx, "\t}\n");
502 }
503
504 if (err >= 0)
505 err = tplg_save_printf(dst, pfx, "}\n");
506 return err;
507 }
508
509 /* DAPM Widget */
tplg_parse_dapm_widget(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED)510 int tplg_parse_dapm_widget(snd_tplg_t *tplg,
511 snd_config_t *cfg, void *private ATTRIBUTE_UNUSED)
512 {
513 struct snd_soc_tplg_dapm_widget *widget;
514 struct tplg_elem *elem;
515 snd_config_iterator_t i, next;
516 snd_config_t *n;
517 const char *id, *val = NULL;
518 int widget_type, err, ival;
519
520 elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_DAPM_WIDGET);
521 if (!elem)
522 return -ENOMEM;
523
524 tplg_dbg(" Widget: %s", elem->id);
525
526 widget = elem->widget;
527 snd_strlcpy(widget->name, elem->id, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
528 widget->size = elem->size;
529
530 snd_config_for_each(i, next, cfg) {
531
532 n = snd_config_iterator_entry(i);
533 if (snd_config_get_id(n, &id) < 0)
534 continue;
535
536 /* skip comments */
537 if (strcmp(id, "comment") == 0)
538 continue;
539 if (id[0] == '#')
540 continue;
541
542 if (strcmp(id, "type") == 0) {
543 if (snd_config_get_string(n, &val) < 0)
544 return -EINVAL;
545
546 widget_type = lookup_widget(val);
547 if (widget_type < 0){
548 SNDERR("widget '%s': Unsupported widget type %s",
549 elem->id, val);
550 return -EINVAL;
551 }
552
553 widget->id = widget_type;
554 tplg_dbg("\t%s: %s", id, val);
555 continue;
556 }
557
558 if (strcmp(id, "stream_name") == 0) {
559 if (snd_config_get_string(n, &val) < 0)
560 return -EINVAL;
561
562 snd_strlcpy(widget->sname, val,
563 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
564 tplg_dbg("\t%s: %s", id, val);
565 continue;
566 }
567
568 if (strcmp(id, "no_pm") == 0) {
569 ival = snd_config_get_bool(n);
570 if (ival < 0)
571 return -EINVAL;
572
573 widget->reg = ival ? -1 : 0;
574
575 tplg_dbg("\t%s: %s", id, val);
576 continue;
577 }
578
579 if (strcmp(id, "shift") == 0) {
580 if (tplg_get_integer(n, &ival, 0))
581 return -EINVAL;
582
583 widget->shift = ival;
584 tplg_dbg("\t%s: %d", id, widget->shift);
585 continue;
586 }
587
588 if (strcmp(id, "reg") == 0) {
589 if (tplg_get_integer(n, &ival, 0))
590 return -EINVAL;
591
592 widget->reg = ival;
593 tplg_dbg("\t%s: %d", id, widget->reg);
594 continue;
595 }
596
597 if (strcmp(id, "invert") == 0) {
598 ival = snd_config_get_bool(n);
599 if (ival < 0)
600 return -EINVAL;
601
602 widget->invert = ival;
603 tplg_dbg("\t%s: %d", id, widget->invert);
604 continue;
605 }
606
607 if (strcmp(id, "ignore_suspend") == 0) {
608 ival = snd_config_get_bool(n);
609 if (ival < 0)
610 return -EINVAL;
611
612 widget->ignore_suspend = ival;
613
614 tplg_dbg("\t%s: %s", id, val);
615 continue;
616 }
617
618 if (strcmp(id, "subseq") == 0) {
619 if (tplg_get_integer(n, &ival, 0))
620 return -EINVAL;
621
622 widget->subseq = ival;
623 tplg_dbg("\t%s: %d", id, widget->subseq);
624 continue;
625 }
626
627 if (strcmp(id, "event_type") == 0) {
628 if (tplg_get_integer(n, &ival, 0))
629 return -EINVAL;
630
631 widget->event_type = ival;
632 tplg_dbg("\t%s: %d", id, widget->event_type);
633 continue;
634 }
635
636 if (strcmp(id, "event_flags") == 0) {
637 if (tplg_get_integer(n, &ival, 0))
638 return -EINVAL;
639
640 widget->event_flags = ival;
641 tplg_dbg("\t%s: %d", id, widget->event_flags);
642 continue;
643 }
644
645 if (strcmp(id, "enum") == 0) {
646 err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_ENUM);
647 if (err < 0)
648 return err;
649
650 continue;
651 }
652
653 if (strcmp(id, "mixer") == 0) {
654 err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_MIXER);
655 if (err < 0)
656 return err;
657
658 continue;
659 }
660
661 if (strcmp(id, "bytes") == 0) {
662 err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_BYTES);
663 if (err < 0)
664 return err;
665
666 continue;
667 }
668
669 if (strcmp(id, "data") == 0) {
670 err = tplg_parse_refs(n, elem, SND_TPLG_TYPE_DATA);
671 if (err < 0)
672 return err;
673 continue;
674 }
675 }
676
677 return 0;
678 }
679
680 /* save DAPM widget */
tplg_save_dapm_widget(snd_tplg_t *tplg ATTRIBUTE_UNUSED, struct tplg_elem *elem, struct tplg_buf *dst, const char *pfx)681 int tplg_save_dapm_widget(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
682 struct tplg_elem *elem,
683 struct tplg_buf *dst, const char *pfx)
684 {
685 struct snd_soc_tplg_dapm_widget *widget = elem->widget;
686 const char *s;
687 char pfx2[16];
688 int err;
689
690 err = tplg_save_printf(dst, NULL, "'%s' {\n", elem->id);
691 if (err >= 0 && elem->index)
692 err = tplg_save_printf(dst, pfx, "\tindex %u\n",
693 elem->index);
694 if (err >= 0) {
695 s = get_widget_name(widget->id);
696 if (s)
697 err = tplg_save_printf(dst, pfx, "\ttype %s\n", s);
698 else
699 err = tplg_save_printf(dst, pfx, "\ttype %u\n",
700 widget->id);
701 }
702 if (err >= 0 && widget->sname[0])
703 err = tplg_save_printf(dst, pfx, "\tstream_name '%s'\n",
704 widget->sname);
705 if (err >= 0 && widget->reg)
706 err = tplg_save_printf(dst, pfx, "\tno_pm 1\n");
707 if (err >= 0 && widget->shift)
708 err = tplg_save_printf(dst, pfx, "\tshift %u\n",
709 widget->shift);
710 if (err >= 0 && widget->invert)
711 err = tplg_save_printf(dst, pfx, "\tinvert %u\n",
712 widget->invert);
713 if (err >= 0 && widget->ignore_suspend)
714 err = tplg_save_printf(dst, pfx, "\tignore_suspend %u\n",
715 widget->ignore_suspend);
716 if (err >= 0 && widget->subseq)
717 err = tplg_save_printf(dst, pfx, "\tsubseq %u\n",
718 widget->subseq);
719 if (err >= 0 && widget->event_type)
720 err = tplg_save_printf(dst, pfx, "\tevent_type %u\n",
721 widget->event_type);
722 if (err >= 0 && widget->event_flags)
723 err = tplg_save_printf(dst, pfx, "\tevent_flags %u\n",
724 widget->event_flags);
725 snprintf(pfx2, sizeof(pfx2), "%s\t", pfx ?: "");
726 if (err >= 0)
727 err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_ENUM,
728 "enum", dst, pfx2);
729 if (err >= 0)
730 err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_MIXER,
731 "mixer", dst, pfx2);
732 if (err >= 0)
733 err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_BYTES,
734 "bytes", dst, pfx2);
735 if (err >= 0)
736 err = tplg_save_refs(tplg, elem, SND_TPLG_TYPE_DATA,
737 "data", dst, pfx2);
738 if (err >= 0)
739 err = tplg_save_printf(dst, pfx, "}\n");
740 return err;
741 }
742
tplg_add_route(snd_tplg_t *tplg, struct snd_tplg_graph_elem *t, int index)743 int tplg_add_route(snd_tplg_t *tplg, struct snd_tplg_graph_elem *t, int index)
744 {
745 struct tplg_elem *elem;
746 struct snd_soc_tplg_dapm_graph_elem *line;
747
748 if (!t->src || !t->sink)
749 return -EINVAL;
750
751 elem = tplg_elem_new_route(tplg, index);
752 if (!elem)
753 return -ENOMEM;
754
755 line = elem->route;
756 snd_strlcpy(line->source, t->src, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
757 if (t->ctl)
758 snd_strlcpy(line->control, t->ctl,
759 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
760 snd_strlcpy(line->sink, t->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
761
762 return 0;
763 }
764
tplg_add_graph_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)765 int tplg_add_graph_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
766 {
767 struct snd_tplg_graph_template *gt = t->graph;
768 int i, ret;
769
770 for (i = 0; i < gt->count; i++) {
771 ret = tplg_add_route(tplg, gt->elem + i, t->index);
772 if (ret < 0)
773 return ret;
774 }
775
776 return 0;
777 }
778
tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)779 int tplg_add_widget_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t)
780 {
781 struct snd_tplg_widget_template *wt = t->widget;
782 struct snd_soc_tplg_dapm_widget *w;
783 struct tplg_elem *elem;
784 int i, ret = 0;
785
786 tplg_dbg("Widget: %s", wt->name);
787
788 elem = tplg_elem_new_common(tplg, NULL, wt->name,
789 SND_TPLG_TYPE_DAPM_WIDGET);
790 if (!elem)
791 return -ENOMEM;
792
793 /* init new widget */
794 w = elem->widget;
795 w->size = elem->size;
796
797 w->id = wt->id;
798 snd_strlcpy(w->name, wt->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
799 if (wt->sname)
800 snd_strlcpy(w->sname, wt->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
801 w->reg = wt->reg;
802 w->shift = wt->shift;
803 w->mask = wt->mask;
804 w->subseq = wt->subseq;
805 w->invert = wt->invert;
806 w->ignore_suspend = wt->ignore_suspend;
807 w->event_flags = wt->event_flags;
808 w->event_type = wt->event_type;
809
810 /* add private data */
811 if (wt->priv != NULL && wt->priv->size > 0) {
812 ret = tplg_add_data(tplg, elem, wt->priv,
813 sizeof(*wt->priv) + wt->priv->size);
814 if (ret < 0) {
815 tplg_elem_free(elem);
816 return ret;
817 }
818 }
819
820 /* add controls to the widget's reference list */
821 for (i = 0 ; i < wt->num_ctls; i++) {
822 struct snd_tplg_ctl_template *ct = wt->ctl[i];
823 struct tplg_elem *elem_ctl;
824 struct snd_tplg_mixer_template *mt;
825 struct snd_tplg_bytes_template *bt;
826 struct snd_tplg_enum_template *et;
827
828 if (!ct) {
829 tplg_elem_free(elem);
830 return -EINVAL;
831 }
832
833 switch (ct->type) {
834 case SND_SOC_TPLG_TYPE_MIXER:
835 mt = container_of(ct, struct snd_tplg_mixer_template, hdr);
836 ret = tplg_add_mixer(tplg, mt, &elem_ctl);
837 break;
838
839 case SND_SOC_TPLG_TYPE_BYTES:
840 bt = container_of(ct, struct snd_tplg_bytes_template, hdr);
841 ret = tplg_add_bytes(tplg, bt, &elem_ctl);
842 break;
843
844 case SND_SOC_TPLG_TYPE_ENUM:
845 et = container_of(ct, struct snd_tplg_enum_template, hdr);
846 ret = tplg_add_enum(tplg, et, &elem_ctl);
847 break;
848
849 default:
850 SNDERR("widget %s: invalid type %d for ctl %d",
851 wt->name, ct->type, i);
852 ret = -EINVAL;
853 break;
854 }
855
856 if (ret < 0) {
857 tplg_elem_free(elem);
858 return ret;
859 }
860
861 ret = tplg_ref_add_elem(elem, elem_ctl);
862 if (ret < 0) {
863 tplg_elem_free(elem);
864 return ret;
865 }
866 }
867
868 return 0;
869 }
870
871 /* decode dapm widget from the binary input */
tplg_decode_dapm_widget(snd_tplg_t *tplg, size_t pos, struct snd_soc_tplg_hdr *hdr, void *bin, size_t size)872 int tplg_decode_dapm_widget(snd_tplg_t *tplg,
873 size_t pos,
874 struct snd_soc_tplg_hdr *hdr,
875 void *bin, size_t size)
876 {
877 struct list_head heap;
878 struct snd_soc_tplg_dapm_widget *w;
879 snd_tplg_obj_template_t t;
880 struct snd_tplg_widget_template *wt;
881 struct snd_tplg_mixer_template *mt;
882 struct snd_tplg_enum_template *et;
883 struct snd_tplg_bytes_template *bt;
884 struct snd_soc_tplg_ctl_hdr *chdr;
885 struct snd_soc_tplg_mixer_control *mc;
886 struct snd_soc_tplg_enum_control *ec;
887 struct snd_soc_tplg_bytes_control *bc;
888 size_t size2;
889 unsigned int index;
890 int err;
891
892 err = tplg_decode_template(tplg, pos, hdr, &t);
893 if (err < 0)
894 return err;
895
896 next:
897 INIT_LIST_HEAD(&heap);
898 w = bin;
899
900 if (size < sizeof(*w)) {
901 SNDERR("dapm widget: small size %d", size);
902 return -EINVAL;
903 }
904 if (sizeof(*w) != w->size) {
905 SNDERR("dapm widget: unknown element size %d (expected %zd)",
906 w->size, sizeof(*w));
907 return -EINVAL;
908 }
909 if (w->num_kcontrols > 16) {
910 SNDERR("dapm widget: too many kcontrols %d",
911 w->num_kcontrols);
912 return -EINVAL;
913 }
914
915 tplg_log(tplg, 'D', pos, "dapm widget: size %d private size %d kcontrols %d",
916 w->size, w->priv.size, w->num_kcontrols);
917
918 wt = tplg_calloc(&heap, sizeof(*wt) + sizeof(void *) * w->num_kcontrols);
919 if (wt == NULL)
920 return -ENOMEM;
921 wt->id = w->id;
922 wt->name = w->name;
923 wt->sname = w->sname;
924 wt->reg = w->reg;
925 wt->shift = w->shift;
926 wt->mask = w->mask;
927 wt->subseq = w->subseq;
928 wt->invert = w->invert;
929 wt->ignore_suspend = w->ignore_suspend;
930 wt->event_flags = w->event_flags;
931 wt->event_type = w->event_type;
932
933 tplg_log(tplg, 'D', pos, "dapm widget: name '%s' sname '%s'",
934 wt->name, wt->sname);
935
936 if (sizeof(*w) + w->priv.size > size) {
937 SNDERR("dapm widget: wrong private data size %d",
938 w->priv.size);
939 return -EINVAL;
940 }
941
942 tplg_log(tplg, 'D', pos + offsetof(struct snd_soc_tplg_dapm_widget, priv),
943 "dapm widget: private start");
944
945 wt->priv = &w->priv;
946 bin += sizeof(*w) + w->priv.size;
947 size -= sizeof(*w) + w->priv.size;
948 pos += sizeof(*w) + w->priv.size;
949
950 for (index = 0; index < w->num_kcontrols; index++) {
951 chdr = bin;
952 switch (chdr->type) {
953 case SND_SOC_TPLG_TYPE_MIXER:
954 mt = tplg_calloc(&heap, sizeof(*mt));
955 if (mt == NULL) {
956 err = -ENOMEM;
957 goto retval;
958 }
959 wt->ctl[index] = (void *)mt;
960 wt->num_ctls++;
961 mc = bin;
962 size2 = mc->size + mc->priv.size;
963 tplg_log(tplg, 'D', pos, "kcontrol mixer size %zd", size2);
964 if (size2 > size) {
965 SNDERR("dapm widget: small mixer size %d",
966 size2);
967 err = -EINVAL;
968 goto retval;
969 }
970 err = tplg_decode_control_mixer1(tplg, &heap, mt, pos,
971 bin, size2);
972 break;
973 case SND_SOC_TPLG_TYPE_ENUM:
974 et = tplg_calloc(&heap, sizeof(*mt));
975 if (et == NULL) {
976 err = -ENOMEM;
977 goto retval;
978 }
979 wt->ctl[index] = (void *)et;
980 wt->num_ctls++;
981 ec = bin;
982 size2 = ec->size + ec->priv.size;
983 tplg_log(tplg, 'D', pos, "kcontrol enum size %zd", size2);
984 if (size2 > size) {
985 SNDERR("dapm widget: small enum size %d",
986 size2);
987 err = -EINVAL;
988 goto retval;
989 }
990 err = tplg_decode_control_enum1(tplg, &heap, et, pos, ec);
991 break;
992 case SND_SOC_TPLG_TYPE_BYTES:
993 bt = tplg_calloc(&heap, sizeof(*bt));
994 if (bt == NULL) {
995 err = -ENOMEM;
996 goto retval;
997 }
998 wt->ctl[index] = (void *)bt;
999 wt->num_ctls++;
1000 bc = bin;
1001 size2 = bc->size + bc->priv.size;
1002 tplg_log(tplg, 'D', pos, "kcontrol bytes size %zd", size2);
1003 if (size2 > size) {
1004 SNDERR("dapm widget: small bytes size %d",
1005 size2);
1006 err = -EINVAL;
1007 goto retval;
1008 }
1009 err = tplg_decode_control_bytes1(tplg, bt, pos,
1010 bin, size2);
1011 break;
1012 default:
1013 SNDERR("dapm widget: wrong control type %d",
1014 chdr->type);
1015 err = -EINVAL;
1016 goto retval;
1017 }
1018 if (err < 0)
1019 goto retval;
1020 bin += size2;
1021 size -= size2;
1022 pos += size2;
1023 }
1024
1025 t.widget = wt;
1026 err = snd_tplg_add_object(tplg, &t);
1027 tplg_free(&heap);
1028 if (err < 0)
1029 return err;
1030 if (size > 0)
1031 goto next;
1032 return 0;
1033
1034 retval:
1035 tplg_free(&heap);
1036 return err;
1037 }
1038
1039 /* decode dapm link from the binary input */
tplg_decode_dapm_graph(snd_tplg_t *tplg, size_t pos, struct snd_soc_tplg_hdr *hdr, void *bin, size_t size)1040 int tplg_decode_dapm_graph(snd_tplg_t *tplg,
1041 size_t pos,
1042 struct snd_soc_tplg_hdr *hdr,
1043 void *bin, size_t size)
1044 {
1045 struct snd_soc_tplg_dapm_graph_elem *g;
1046 snd_tplg_obj_template_t t;
1047 struct snd_tplg_graph_template *gt;
1048 struct snd_tplg_graph_elem *ge;
1049 size_t asize;
1050 int err;
1051
1052 err = tplg_decode_template(tplg, pos, hdr, &t);
1053 if (err < 0)
1054 return err;
1055
1056 asize = sizeof(*gt) + (size / sizeof(*g)) * sizeof(*ge);
1057 gt = alloca(asize);
1058 memset(gt, 0, asize);
1059 for (ge = gt->elem; size > 0; ge++) {
1060 g = bin;
1061 if (size < sizeof(*g)) {
1062 SNDERR("dapm graph: small size %d", size);
1063 return -EINVAL;
1064 }
1065 ge->src = g->source;
1066 ge->ctl = g->control;
1067 ge->sink = g->sink;
1068 gt->count++;
1069 tplg_log(tplg, 'D', pos, "dapm graph: src='%s' ctl='%s' sink='%s'",
1070 ge->src, ge->ctl, ge->sink);
1071 bin += sizeof(*g);
1072 size -= sizeof(*g);
1073 pos += sizeof(*g);
1074 }
1075
1076 t.graph = gt;
1077 return snd_tplg_add_object(tplg, &t);
1078 }
1079