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.
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.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 * Support for the verb/device/modifier core logic and API,
17 * command line tool and file parser was kindly sponsored by
18 * Texas Instruments Inc.
19 * Support for multiple active modifiers and devices,
20 * transition sequences, multiple client access and user defined use
21 * cases was kindly sponsored by Wolfson Microelectronics PLC.
23 * Copyright (C) 2008-2010 SlimLogic Ltd
24 * Copyright (C) 2010 Wolfson Microelectronics PLC
25 * Copyright (C) 2010 Texas Instruments Inc.
26 * Copyright (C) 2010 Red Hat Inc.
27 * Authors: Liam Girdwood <lrg@slimlogic.co.uk>
28 * Stefan Schmidt <stefan@slimlogic.co.uk>
29 * Justin Xu <justinx@slimlogic.co.uk>
30 * Jaroslav Kysela <perex@perex.cz>
33 #include "ucm_local.h"
40 static int filename_filter(const struct dirent64 *dirent);
42 static int parse_sequence(snd_use_case_mgr_t *uc_mgr,
43 struct list_head *base,
47 * compose the absolute ucm filename
49 static void ucm_filename(char *fn, size_t fn_len, long version,
50 const char *dir, const char *file)
52 const char *env = getenv(version > 1 ? ALSA_CONFIG_UCM2_VAR : ALSA_CONFIG_UCM_VAR);
57 snprintf(fn, fn_len, "%s/%s/%s%s%s",
58 snd_config_topdir(), version > 1 ? "ucm2" : "ucm",
59 dir ?: "", dir ? "/" : "", file);
61 snprintf(fn, fn_len, "%s/%s%s%s",
62 env, dir ?: "", dir ? "/" : "", file);
68 int uc_mgr_config_load_file(snd_use_case_mgr_t *uc_mgr,
69 const char *file, snd_config_t **cfg)
71 char filename[PATH_MAX];
74 ucm_filename(filename, sizeof(filename), uc_mgr->conf_format,
75 file[0] == '/' ? NULL : uc_mgr->conf_dir_name,
77 err = uc_mgr_config_load(uc_mgr->conf_format, filename, cfg);
79 snd_error(UCM, "failed to open file %s: %d", filename, err);
86 * Replace mallocated string
88 static char *replace_string(char **dst, const char *value)
91 *dst = value ? strdup(value) : NULL;
98 static int parse_string(snd_config_t *n, char **res)
102 err = snd_config_get_string(n, (const char **)res);
112 * Parse string and substitute
114 static int parse_string_substitute(snd_use_case_mgr_t *uc_mgr,
115 snd_config_t *n, char **res)
121 err = snd_config_get_string(n, &str);
124 err = uc_mgr_get_substituted_value(uc_mgr, &s, str);
131 * Parse string and substitute
133 static int parse_string_substitute3(snd_use_case_mgr_t *uc_mgr,
134 snd_config_t *n, char **res)
136 if (uc_mgr->conf_format < 3)
137 return parse_string(n, res);
138 return parse_string_substitute(uc_mgr, n, res);
142 * Parse integer with substitution
144 static int parse_integer_substitute(snd_use_case_mgr_t *uc_mgr,
145 snd_config_t *n, long *res)
150 err = snd_config_get_ascii(n, &s1);
153 err = uc_mgr_get_substituted_value(uc_mgr, &s2, s1);
155 err = safe_strtol(s2, res);
162 * Parse integer with substitution
164 static int parse_integer_substitute3(snd_use_case_mgr_t *uc_mgr,
165 snd_config_t *n, long *res)
170 err = snd_config_get_ascii(n, &s1);
173 if (uc_mgr->conf_format < 3)
176 err = uc_mgr_get_substituted_value(uc_mgr, &s2, s1);
178 err = safe_strtol(s2, res);
188 static int parse_is_name_safe(const char *name)
190 if (strchr(name, '.')) {
191 snd_error(UCM, "char '.' not allowed in '%s'", name);
197 static int get_string3(snd_use_case_mgr_t *uc_mgr, const char *s1, char **s)
199 if (uc_mgr->conf_format < 3) {
205 return uc_mgr_get_substituted_value(uc_mgr, s, s1);
208 static int parse_get_safe_name(snd_use_case_mgr_t *uc_mgr, snd_config_t *n,
209 const char *alt, char **name)
217 err = snd_config_get_id(n, &id);
221 err = get_string3(uc_mgr, id, name);
224 if (!parse_is_name_safe(*name)) {
232 * Parse device index from device name
233 * According to use-case.h specification, device names can have numeric suffixes
234 * like HDMI1, HDMI2, or "Line 1", "Line 2".
235 * This function extracts the index and modifies the name to contain only the base.
237 static int parse_device_index(char **name, int *index)
244 if (!name || !*name || !index)
251 /* Start from the end and find where digits begin */
254 /* Skip trailing digits */
255 while (p > *name && isdigit(*p))
258 /* If no digits found at the end, index is 0 (no index) */
259 if (p == *name + len - 1) {
264 /* Move to first digit */
267 /* Check if there's an optional space before the number */
268 if (p > *name && *(p - 1) == ' ')
271 /* Parse the index */
272 num_start = *p == ' ' ? p + 1 : p;
273 err = safe_strtol(num_start, &idx);
275 /* No valid number found */
282 /* Truncate the name to remove the index part */
284 *p = '\0'; /* Remove space and number */
286 *p = '\0'; /* Remove number only */
292 * Handle 'Error' configuration node.
294 static int error_node(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
299 err = parse_string_substitute3(uc_mgr, cfg, &s);
301 snd_error(UCM, "failed to get Error string");
304 if (!uc_mgr->suppress_nodev_errors)
305 snd_error(UCM, "%s", s);
313 static int parse_syntax_field(snd_use_case_mgr_t *uc_mgr,
314 snd_config_t *cfg, const char *filename)
320 err = snd_config_search(cfg, "Syntax", &n);
322 snd_error(UCM, "Syntax field not found in %s", filename);
325 err = snd_config_get_integer(n, &l);
327 snd_error(UCM, "Syntax field is invalid in %s", filename);
330 if (l < 2 || l > SYNTAX_VERSION_MAX) {
331 snd_error(UCM, "Incompatible syntax %ld in %s", l, filename);
334 /* delete this field to optimize strcmp() call in the parsing loop */
335 snd_config_delete(n);
336 uc_mgr->conf_format = l;
341 * Evaluate variable regex definitions (in-place delete)
343 static int evaluate_regex(snd_use_case_mgr_t *uc_mgr,
346 snd_config_iterator_t i, next;
351 err = snd_config_search(cfg, "DefineRegex", &d);
357 if (snd_config_get_type(d) != SND_CONFIG_TYPE_COMPOUND) {
358 snd_error(UCM, "compound type expected for DefineRegex");
362 if (uc_mgr->conf_format < 3) {
363 snd_error(UCM, "DefineRegex is supported in v3+ syntax");
367 snd_config_for_each(i, next, d) {
368 n = snd_config_iterator_entry(i);
369 err = snd_config_get_id(n, &id);
373 snd_error(UCM, "value names starting with '@' are reserved for application variables");
376 err = uc_mgr_define_regex(uc_mgr, id, n);
381 snd_config_delete(d);
386 * Evaluate variable definitions (in-place delete)
388 static int evaluate_define(snd_use_case_mgr_t *uc_mgr,
391 snd_config_iterator_t i, next;
397 err = snd_config_search(cfg, "Define", &d);
399 return evaluate_regex(uc_mgr, cfg);
403 if (snd_config_get_type(d) != SND_CONFIG_TYPE_COMPOUND) {
404 snd_error(UCM, "compound type expected for Define");
408 if (uc_mgr->conf_format < 3) {
409 snd_error(UCM, "Define is supported in v3+ syntax");
413 snd_config_for_each(i, next, d) {
414 n = snd_config_iterator_entry(i);
415 err = snd_config_get_id(n, &id);
418 err = snd_config_get_ascii(n, &var);
421 err = uc_mgr_get_substituted_value(uc_mgr, &s, var);
427 snd_error(UCM, "value names starting with '@' are reserved for application variables");
431 if (uc_mgr->conf_format >= 9) {
432 err = uc_mgr_get_substituted_value(uc_mgr, &sid, id);
438 err = uc_mgr_set_variable(uc_mgr, sid, s);
446 snd_config_delete(d);
448 return evaluate_regex(uc_mgr, cfg);
452 * Evaluate macro definitions (in-place delete)
454 static int evaluate_define_macro(snd_use_case_mgr_t *uc_mgr,
460 err = snd_config_search(cfg, "DefineMacro", &d);
466 if (snd_config_get_type(d) != SND_CONFIG_TYPE_COMPOUND) {
467 snd_error(UCM, "compound type expected for DefineMacro");
471 if (uc_mgr->conf_format < 6) {
472 snd_error(UCM, "DefineMacro is supported in v6+ syntax");
476 err = snd_config_merge(uc_mgr->macros, d, 0);
483 static int evaluate_macro1(snd_use_case_mgr_t *uc_mgr,
487 snd_config_iterator_t i, next;
488 snd_config_t *m, *mc, *a, *n;
489 const char *mid, *id;
490 char name[128], *var, *var2;
494 err = snd_config_get_id(args, &mid);
497 err = snd_config_search(uc_mgr->macros, mid, &m);
499 snd_error(UCM, "Macro '%s' is not defined", mid);
504 if (snd_config_get_type(args) == SND_CONFIG_TYPE_STRING) {
505 err = snd_config_get_string(args, &s);
508 if (uc_mgr->conf_format < 9) {
509 err = snd_config_load_string(&a, s, 0);
511 err = uc_mgr_get_substituted_value(uc_mgr, &var2, s);
513 err = snd_config_load_string(&a, var2, 0);
519 } else if (snd_config_get_type(args) != SND_CONFIG_TYPE_COMPOUND) {
524 snd_config_for_each(i, next, a) {
525 n = snd_config_iterator_entry(i);
526 err = snd_config_get_id(n, &id);
529 snprintf(name, sizeof(name), "__%s", id);
530 if (uc_mgr_get_variable(uc_mgr, name, false)) {
531 snd_error(UCM, "Macro argument '%s' is already defined", name);
534 err = snd_config_get_ascii(n, &var);
537 if (uc_mgr->conf_format < 7) {
538 err = uc_mgr_set_variable(uc_mgr, name, var);
541 err = uc_mgr_get_substituted_value(uc_mgr, &var2, var);
544 err = uc_mgr_set_variable(uc_mgr, name, var2);
552 /* merge + substitute variables */
553 err = snd_config_copy(&mc, m);
556 err = uc_mgr_evaluate_inplace(uc_mgr, mc);
558 snd_config_delete(mc);
561 err = uc_mgr_config_tree_merge(uc_mgr, dst, mc, NULL, NULL);
562 snd_config_delete(mc);
564 /* delete arguments */
565 snd_config_for_each(i, next, a) {
566 n = snd_config_iterator_entry(i);
567 err = snd_config_get_id(n, &id);
570 snprintf(name, sizeof(name), "__%s", id);
571 err = uc_mgr_delete_variable(uc_mgr, name);
578 snd_config_delete(a);
583 * Evaluate macro definitions and instances (in-place delete)
585 static int evaluate_macro(snd_use_case_mgr_t *uc_mgr,
588 snd_config_iterator_t i, i2, next, next2;
589 snd_config_t *d, *n, *n2;
592 ret = evaluate_define_macro(uc_mgr, cfg);
596 err = snd_config_search(cfg, "Macro", &d);
602 if (snd_config_get_type(d) != SND_CONFIG_TYPE_COMPOUND) {
603 snd_error(UCM, "compound type expected for DefineMacro");
607 if (uc_mgr->conf_format < 6) {
608 snd_error(UCM, "Macro is supported in v6+ syntax");
612 snd_config_for_each(i, next, d) {
613 n = snd_config_iterator_entry(i);
614 if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {
616 if (snd_config_get_id(n, &id))
618 snd_error(UCM, "compound type expected for Macro.%s", id);
621 snd_config_for_each(i2, next2, n) {
622 n2 = snd_config_iterator_entry(i2);
623 err = evaluate_macro1(uc_mgr, cfg, n2);
629 snd_config_delete(d);
635 * Evaluate include (in-place)
637 static int evaluate_include(snd_use_case_mgr_t *uc_mgr,
643 err = snd_config_search(cfg, "Include", &n);
649 err = uc_mgr_evaluate_include(uc_mgr, cfg, n);
650 snd_config_delete(n);
655 * Evaluate condition (in-place)
657 static int evaluate_condition(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
662 err = snd_config_search(cfg, "If", &n);
668 err = uc_mgr_evaluate_condition(uc_mgr, cfg, n);
669 snd_config_delete(n);
674 * Evaluate variant (in-place)
676 static int evaluate_variant(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
678 snd_config_iterator_t i, next;
683 err = snd_config_search(cfg, "Variant", &c);
689 if (uc_mgr->conf_format < 6) {
690 snd_error(UCM, "Variant is supported in v6+ syntax");
694 if (uc_mgr->parse_master_section)
697 if (uc_mgr->parse_variant == NULL)
700 snd_config_for_each(i, next, c) {
701 n = snd_config_iterator_entry(i);
703 if (snd_config_get_id(n, &id) < 0)
706 if (strcmp(id, uc_mgr->parse_variant))
709 err = uc_mgr_evaluate_inplace(uc_mgr, n);
713 err = uc_mgr_config_tree_merge(uc_mgr, cfg, n, NULL, NULL);
716 snd_config_delete(c);
721 snd_config_delete(c);
728 int uc_mgr_evaluate_inplace(snd_use_case_mgr_t *uc_mgr,
731 long iterations = 10000;
732 int err1 = 0, err2 = 0, err3 = 0, err4 = 0, err5 = 0, err6 = 0;
734 while (err1 == 0 || err2 == 0 || err3 == 0 || err4 == 0 || err5 == 0 || err6 == 0) {
735 if (iterations == 0) {
736 snd_error(UCM, "Maximal inplace evaluation iterations number reached (recursive references?)");
740 /* variables at first */
741 err1 = evaluate_define(uc_mgr, cfg);
744 /* include at second */
745 err2 = evaluate_include(uc_mgr, cfg);
748 /* include or macro may define another variables */
749 /* conditions may depend on them */
752 err3 = evaluate_variant(uc_mgr, cfg);
757 uc_mgr->macro_hops++;
758 if (uc_mgr->macro_hops > 100) {
759 snd_error(UCM, "Maximal macro hops reached!");
762 err4 = evaluate_macro(uc_mgr, cfg);
763 uc_mgr->macro_hops--;
768 err5 = uc_mgr_evaluate_repeat(uc_mgr, cfg);
771 err6 = evaluate_condition(uc_mgr, cfg);
779 * Parse one item for alsa-lib config
781 static int parse_libconfig1(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
783 snd_config_iterator_t i, next;
784 snd_config_t *n, *config = NULL;
785 const char *id, *file = NULL;
786 bool substfile = false, substconfig = false;
789 if (snd_config_get_id(cfg, &id) < 0)
792 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
793 snd_error(UCM, "compound type expected for %s", id);
797 snd_config_for_each(i, next, cfg) {
798 n = snd_config_iterator_entry(i);
800 if (snd_config_get_id(n, &id) < 0)
803 if (strcmp(id, "File") == 0 ||
804 strcmp(id, "SubstiFile") == 0) {
805 substfile = id[0] == 'S';
806 err = snd_config_get_string(n, &file);
812 if (strcmp(id, "Config") == 0 ||
813 strcmp(id, "SubstiConfig") == 0) {
814 substconfig = id[0] == 'S';
815 if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND)
821 snd_error(UCM, "unknown field %s", id);
828 err = uc_mgr_config_load_file(uc_mgr, file, &cfg);
831 err = uc_mgr_substitute_tree(uc_mgr, cfg);
833 snd_config_delete(cfg);
836 err = snd_config_merge(uc_mgr->local_config, cfg, 0);
838 snd_config_delete(cfg);
842 char filename[PATH_MAX];
844 ucm_filename(filename, sizeof(filename), uc_mgr->conf_format,
845 file[0] == '/' ? NULL : uc_mgr->conf_dir_name,
847 err = uc_mgr_config_load_into(uc_mgr->conf_format, filename, uc_mgr->local_config);
855 err = uc_mgr_substitute_tree(uc_mgr, config);
859 err = snd_config_merge(uc_mgr->local_config, config, 0);
868 * Parse alsa-lib config
870 static int parse_libconfig(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
872 snd_config_iterator_t i, next;
877 if (snd_config_get_id(cfg, &id) < 0)
880 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
881 snd_error(UCM, "compound type expected for %s", id);
885 snd_config_for_each(i, next, cfg) {
886 n = snd_config_iterator_entry(i);
888 err = parse_libconfig1(uc_mgr, n);
899 static int parse_transition(snd_use_case_mgr_t *uc_mgr,
900 struct list_head *tlist,
903 struct transition_sequence *tseq;
905 snd_config_iterator_t i, next;
909 if (snd_config_get_id(cfg, &id) < 0)
912 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
913 snd_error(UCM, "compound type expected for %s", id);
917 snd_config_for_each(i, next, cfg) {
918 n = snd_config_iterator_entry(i);
920 if (snd_config_get_id(n, &id) < 0)
923 tseq = calloc(1, sizeof(*tseq));
926 INIT_LIST_HEAD(&tseq->transition_list);
928 err = get_string3(uc_mgr, id, &tseq->name);
934 err = parse_sequence(uc_mgr, &tseq->transition_list, n);
936 uc_mgr_free_transition_element(tseq);
940 list_add(&tseq->list, tlist);
948 static int parse_compound(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg,
949 int (*fcn)(snd_use_case_mgr_t *, snd_config_t *, void *, void *),
950 void *data1, void *data2)
953 snd_config_iterator_t i, next;
957 if (snd_config_get_id(cfg, &id) < 0)
960 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
961 snd_error(UCM, "compound type expected for %s", id);
965 snd_config_for_each(i, next, cfg) {
966 n = snd_config_iterator_entry(i);
968 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
969 snd_error(UCM, "compound type expected for %s, is %d", id, snd_config_get_type(cfg));
973 err = fcn(uc_mgr, n, data1, data2);
981 static int strip_legacy_dev_index(char *name)
983 char *dot = strchr(name, '.');
986 if (dot[1] != '0' || dot[2] != '\0') {
987 snd_error(UCM, "device name %s contains a '.',"
988 " and is not legacy foo.0 format", name);
999 static int parse_device_list(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED,
1000 struct dev_list *dev_list,
1001 enum dev_list_type type,
1004 struct dev_list_node *sdev;
1006 snd_config_iterator_t i, next;
1010 if (dev_list->type != DEVLIST_NONE) {
1011 snd_error(UCM, "multiple supported or conflicting device lists");
1016 if (snd_config_get_id(cfg, &id) < 0)
1019 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1020 snd_error(UCM, "compound type expected for %s", id);
1024 snd_config_for_each(i, next, cfg) {
1025 n = snd_config_iterator_entry(i);
1027 if (snd_config_get_id(n, &id) < 0)
1030 sdev = calloc(1, sizeof(struct dev_list_node));
1033 err = parse_string_substitute3(uc_mgr, n, &sdev->name);
1038 err = strip_legacy_dev_index(sdev->name);
1044 list_add(&sdev->list, &dev_list->list);
1047 dev_list->type = type;
1052 /* Find a component device by its name, and remove it from machine device
1055 * Component devices are defined by machine components (usually off-soc
1056 * codes or DSP embeded in SoC). Since alsaconf imports their configuration
1057 * files automatically, we don't know which devices are component devices
1058 * until they are referenced by a machine device sequence. So here when we
1059 * find a referenced device, we move it from the machine device list to the
1060 * component device list. Component devices will not be exposed to applications
1061 * by the original API to list devices for backward compatibility. So sound
1062 * servers can only see the machine devices.
1064 struct use_case_device *find_component_dev(snd_use_case_mgr_t *uc_mgr,
1067 struct list_head *pos, *posdev, *_posdev;
1068 struct use_case_verb *verb;
1069 struct use_case_device *dev;
1071 list_for_each(pos, &uc_mgr->verb_list) {
1072 verb = list_entry(pos, struct use_case_verb, list);
1074 /* search in the component device list */
1075 list_for_each(posdev, &verb->cmpt_device_list) {
1076 dev = list_entry(posdev, struct use_case_device, list);
1077 if (!strcmp(dev->name, name))
1081 /* search the machine device list */
1082 list_for_each_safe(posdev, _posdev, &verb->device_list) {
1083 dev = list_entry(posdev, struct use_case_device, list);
1084 if (!strcmp(dev->name, name)) {
1085 /* find the component device, move it from the
1086 * machine device list to the component device
1089 list_del(&dev->list);
1090 list_add_tail(&dev->list,
1091 &verb->cmpt_device_list);
1100 /* parse sequence of a component device
1102 * This function will find the component device and mark if its enable or
1103 * disable sequence is needed by its parenet device.
1105 static int parse_component_seq(snd_use_case_mgr_t *uc_mgr,
1106 snd_config_t *n, int enable,
1107 struct component_sequence *cmpt_seq)
1112 err = parse_string_substitute3(uc_mgr, n, &val);
1116 cmpt_seq->device = find_component_dev(uc_mgr, val);
1117 if (!cmpt_seq->device) {
1118 snd_error(UCM, "Cannot find component device %s", val);
1124 /* Parent needs its enable or disable sequence */
1125 cmpt_seq->enable = enable;
1133 * Sequence controls elements are in the following form:-
1136 * cset "element_id_syntax value_syntax"
1138 * exec "any unix command with arguments"
1139 * enadev "component device name"
1140 * disdev "component device name"
1143 * cset "name='Master Playback Switch' 0,0"
1144 * cset "iface=PCM,name='Disable HDMI',index=1 0"
1145 * enadev "rt286:Headphones"
1146 * disdev "rt286:Speaker"
1148 static int parse_sequence(snd_use_case_mgr_t *uc_mgr,
1149 struct list_head *base,
1152 struct sequence_element *curr;
1153 snd_config_iterator_t i, next;
1156 const char *cmd = NULL;
1158 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1159 snd_error(UCM, "compound is expected for sequence definition");
1163 snd_config_for_each(i, next, cfg) {
1166 n = snd_config_iterator_entry(i);
1167 err = snd_config_get_id(n, &id);
1171 if (snd_config_get_type(n) != SND_CONFIG_TYPE_STRING) {
1172 snd_error(UCM, "string type is expected for sequence command");
1175 err = snd_config_get_string(n, &cmd);
1181 /* alloc new sequence element */
1182 curr = calloc(1, sizeof(struct sequence_element));
1185 list_add_tail(&curr->list, base);
1187 if (strcmp(cmd, "cdev") == 0) {
1188 curr->type = SEQUENCE_ELEMENT_TYPE_CDEV;
1189 err = parse_string_substitute3(uc_mgr, n, &curr->data.cdev);
1191 snd_error(UCM, "cdev requires a string!");
1197 if (strcmp(cmd, "cset") == 0) {
1198 curr->type = SEQUENCE_ELEMENT_TYPE_CSET;
1200 err = parse_string_substitute3(uc_mgr, n, &curr->data.cset);
1202 snd_error(UCM, "%s requires a string!", cmd);
1208 if (strcmp(cmd, "enadev") == 0 ||
1209 strcmp(cmd, "disdev") == 0) {
1210 /* need to enable or disable a component device */
1211 curr->type = SEQUENCE_ELEMENT_TYPE_CMPT_SEQ;
1212 err = parse_component_seq(uc_mgr, n,
1213 strcmp(cmd, "enadev") == 0,
1214 &curr->data.cmpt_seq);
1216 snd_error(UCM, "%s requires a valid device!", cmd);
1222 if (strcmp(cmd, "enadev2") == 0) {
1223 curr->type = SEQUENCE_ELEMENT_TYPE_DEV_ENABLE_SEQ;
1227 if (strcmp(cmd, "disdev2") == 0) {
1228 curr->type = SEQUENCE_ELEMENT_TYPE_DEV_DISABLE_SEQ;
1230 err = parse_string_substitute3(uc_mgr, n, &curr->data.device);
1232 snd_error(UCM, "%s requires a valid device!", cmd);
1238 if (strcmp(cmd, "disdevall") == 0) {
1239 curr->type = SEQUENCE_ELEMENT_TYPE_DEV_DISABLE_ALL;
1243 if (strcmp(cmd, "cset-bin-file") == 0) {
1244 curr->type = SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE;
1248 if (strcmp(cmd, "cset-tlv") == 0) {
1249 curr->type = SEQUENCE_ELEMENT_TYPE_CSET_TLV;
1253 if (strcmp(cmd, "cset-new") == 0) {
1254 curr->type = SEQUENCE_ELEMENT_TYPE_CSET_NEW;
1258 if (strcmp(cmd, "ctl-remove") == 0) {
1259 curr->type = SEQUENCE_ELEMENT_TYPE_CTL_REMOVE;
1263 if (strcmp(cmd, "sysw") == 0) {
1264 curr->type = SEQUENCE_ELEMENT_TYPE_SYSSET;
1265 err = parse_string_substitute3(uc_mgr, n, &curr->data.sysw);
1267 snd_error(UCM, "sysw requires a string!");
1273 if (strcmp(cmd, "usleep") == 0) {
1274 curr->type = SEQUENCE_ELEMENT_TYPE_SLEEP;
1275 err = parse_integer_substitute3(uc_mgr, n, &curr->data.sleep);
1277 snd_error(UCM, "usleep requires integer!");
1283 if (strcmp(cmd, "msleep") == 0) {
1284 curr->type = SEQUENCE_ELEMENT_TYPE_SLEEP;
1285 err = parse_integer_substitute3(uc_mgr, n, &curr->data.sleep);
1287 snd_error(UCM, "msleep requires integer!");
1290 curr->data.sleep *= 1000L;
1294 if (strcmp(cmd, "exec") == 0) {
1295 curr->type = SEQUENCE_ELEMENT_TYPE_EXEC;
1297 err = parse_string_substitute3(uc_mgr, n, &curr->data.exec);
1299 snd_error(UCM, "exec requires a string!");
1305 if (strcmp(cmd, "shell") == 0) {
1306 curr->type = SEQUENCE_ELEMENT_TYPE_SHELL;
1310 if (strcmp(cmd, "cfg-save") == 0) {
1311 curr->type = SEQUENCE_ELEMENT_TYPE_CFGSAVE;
1312 err = parse_string_substitute3(uc_mgr, n, &curr->data.cfgsave);
1314 snd_error(UCM, "sysw requires a string!");
1320 if (strcmp(cmd, "comment") == 0)
1323 snd_error(UCM, "sequence command '%s' is ignored", cmd);
1326 list_del(&curr->list);
1327 uc_mgr_free_sequence_element(curr);
1336 int uc_mgr_add_value(struct list_head *base, const char *key, char *val)
1338 struct ucm_value *curr;
1340 curr = calloc(1, sizeof(struct ucm_value));
1343 curr->name = strdup(key);
1344 if (curr->name == NULL) {
1348 list_add_tail(&curr->list, base);
1356 * Parse values describing PCM, control/mixer settings and stream parameters.
1361 * PlaybackVolume "name='Master Playback Volume',index=2"
1362 * PlaybackSwitch "name='Master Playback Switch',index=2"
1365 static int parse_value(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED,
1366 struct list_head *base,
1369 snd_config_iterator_t i, next;
1372 snd_config_type_t type;
1375 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1376 snd_error(UCM, "compound is expected for value definition");
1380 /* in-place evaluation */
1381 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
1385 snd_config_for_each(i, next, cfg) {
1387 n = snd_config_iterator_entry(i);
1388 err = snd_config_get_id(n, &id);
1392 type = snd_config_get_type(n);
1394 case SND_CONFIG_TYPE_INTEGER:
1395 case SND_CONFIG_TYPE_INTEGER64:
1396 case SND_CONFIG_TYPE_REAL:
1397 err = snd_config_get_ascii(n, &s);
1399 snd_error(UCM, "unable to parse value for id '%s': %s!", id, snd_strerror(err));
1403 case SND_CONFIG_TYPE_STRING:
1404 err = parse_string_substitute(uc_mgr, n, &s);
1406 snd_error(UCM, "unable to parse a string for id '%s'!", id);
1411 snd_error(UCM, "invalid type %i in Value compound '%s'", type, id);
1414 err = uc_mgr_add_value(base, id, s);
1425 * Parse Modifier Use cases
1427 * # Each modifier is described in new section. N modifiers are allowed
1428 * SectionModifier."Capture Voice" {
1430 * Comment "Record voice call"
1437 * ConflictingDevice [
1450 * TransitionSequence."ToModifierName" [
1454 * # Optional TQ and ALSA PCMs
1458 * PlaybackVolume "name='Master Playback Volume',index=2"
1459 * PlaybackSwitch "name='Master Playback Switch',index=2"
1463 * SupportedDevice and ConflictingDevice cannot be specified together.
1464 * Both are optional.
1466 static int parse_modifier(snd_use_case_mgr_t *uc_mgr,
1468 void *data1, void *data2)
1470 struct use_case_verb *verb = data1;
1471 struct use_case_modifier *modifier;
1473 snd_config_iterator_t i, next;
1477 if (parse_get_safe_name(uc_mgr, cfg, data2, &name) < 0)
1480 /* allocate modifier */
1481 modifier = calloc(1, sizeof(*modifier));
1482 if (modifier == NULL) {
1486 INIT_LIST_HEAD(&modifier->enable_list);
1487 INIT_LIST_HEAD(&modifier->disable_list);
1488 INIT_LIST_HEAD(&modifier->transition_list);
1489 INIT_LIST_HEAD(&modifier->dev_list.list);
1490 INIT_LIST_HEAD(&modifier->value_list);
1491 list_add_tail(&modifier->list, &verb->modifier_list);
1492 modifier->name = name;
1494 /* in-place evaluation */
1495 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
1499 snd_config_for_each(i, next, cfg) {
1501 n = snd_config_iterator_entry(i);
1502 if (snd_config_get_id(n, &id) < 0)
1505 if (strcmp(id, "Comment") == 0) {
1506 err = parse_string_substitute3(uc_mgr, n, &modifier->comment);
1508 snd_error(UCM, "failed to get modifier comment");
1514 if (strcmp(id, "SupportedDevice") == 0) {
1515 err = parse_device_list(uc_mgr, &modifier->dev_list,
1516 DEVLIST_SUPPORTED, n);
1518 snd_error(UCM, "failed to parse supported device list");
1523 if (strcmp(id, "ConflictingDevice") == 0) {
1524 err = parse_device_list(uc_mgr, &modifier->dev_list,
1525 DEVLIST_CONFLICTING, n);
1527 snd_error(UCM, "failed to parse conflicting device list");
1532 if (strcmp(id, "EnableSequence") == 0) {
1533 err = parse_sequence(uc_mgr, &modifier->enable_list, n);
1535 snd_error(UCM, "failed to parse modifier enable sequence");
1541 if (strcmp(id, "DisableSequence") == 0) {
1542 err = parse_sequence(uc_mgr, &modifier->disable_list, n);
1544 snd_error(UCM, "failed to parse modifier disable sequence");
1550 if (strcmp(id, "TransitionSequence") == 0) {
1551 err = parse_transition(uc_mgr, &modifier->transition_list, n);
1553 snd_error(UCM, "failed to parse transition modifier");
1559 if (strcmp(id, "Value") == 0) {
1560 err = parse_value(uc_mgr, &modifier->value_list, n);
1562 snd_error(UCM, "failed to parse Value");
1573 * Parse device configuration fields
1575 static int parse_device_fields(snd_use_case_mgr_t *uc_mgr,
1577 struct use_case_device *device)
1579 snd_config_iterator_t i, next;
1583 snd_config_for_each(i, next, cfg) {
1585 n = snd_config_iterator_entry(i);
1586 if (snd_config_get_id(n, &id) < 0)
1589 if (strcmp(id, "Comment") == 0) {
1590 err = parse_string_substitute3(uc_mgr, n, &device->comment);
1592 snd_error(UCM, "failed to get device comment");
1598 if (strcmp(id, "SupportedDevice") == 0) {
1599 err = parse_device_list(uc_mgr, &device->dev_list,
1600 DEVLIST_SUPPORTED, n);
1602 snd_error(UCM, "failed to parse supported device list");
1608 if (strcmp(id, "ConflictingDevice") == 0) {
1609 err = parse_device_list(uc_mgr, &device->dev_list,
1610 DEVLIST_CONFLICTING, n);
1612 snd_error(UCM, "failed to parse conflicting device list");
1618 if (strcmp(id, "EnableSequence") == 0) {
1619 err = parse_sequence(uc_mgr, &device->enable_list, n);
1621 snd_error(UCM, "failed to parse device enable sequence");
1628 if (strcmp(id, "DisableSequence") == 0) {
1629 err = parse_sequence(uc_mgr, &device->disable_list, n);
1631 snd_error(UCM, "failed to parse device disable sequence");
1638 if (strcmp(id, "TransitionSequence") == 0) {
1639 err = parse_transition(uc_mgr, &device->transition_list, n);
1641 snd_error(UCM, "failed to parse transition device");
1648 if (strcmp(id, "Value") == 0) {
1649 err = parse_value(uc_mgr, &device->value_list, n);
1651 snd_error(UCM, "failed to parse Value");
1661 * Helper function to copy, evaluate and optionally merge configuration trees.
1663 static int uc_mgr_config_copy_eval_merge(snd_use_case_mgr_t *uc_mgr,
1666 snd_config_t *merge_from)
1668 snd_config_t *tmp = NULL;
1671 err = snd_config_copy(&tmp, src);
1675 err = uc_mgr_evaluate_inplace(uc_mgr, tmp);
1677 snd_config_delete(tmp);
1682 err = uc_mgr_config_tree_merge(uc_mgr, tmp, merge_from, NULL, NULL);
1684 snd_config_delete(tmp);
1694 * Parse Device Use Cases
1696 * # Each device is described in new section. N devices are allowed
1697 * SectionDevice."Headphones" {
1698 * Comment "Headphones connected to 3.5mm jack"
1705 * ConflictingDevice [
1718 * TransitionSequence."ToDevice" [
1723 * PlaybackVolume "name='Master Playback Volume',index=2"
1724 * PlaybackSwitch "name='Master Playback Switch',index=2"
1729 static int parse_device_by_name(snd_use_case_mgr_t *uc_mgr,
1731 struct use_case_verb *verb,
1733 struct use_case_device **ret_device)
1735 struct use_case_device *device;
1738 device = calloc(1, sizeof(*device));
1742 INIT_LIST_HEAD(&device->enable_list);
1743 INIT_LIST_HEAD(&device->disable_list);
1744 INIT_LIST_HEAD(&device->transition_list);
1745 INIT_LIST_HEAD(&device->dev_list.list);
1746 INIT_LIST_HEAD(&device->value_list);
1747 INIT_LIST_HEAD(&device->variants);
1748 INIT_LIST_HEAD(&device->variant_list);
1749 list_add_tail(&device->list, &verb->device_list);
1750 device->name = strdup(name);
1751 if (device->name == NULL) {
1755 device->orig_name = strdup(name);
1756 if (device->orig_name == NULL)
1759 err = parse_device_fields(uc_mgr, cfg, device);
1764 *ret_device = device;
1769 static int parse_device(snd_use_case_mgr_t *uc_mgr,
1771 void *data1, void *data2)
1773 struct use_case_verb *verb = data1;
1775 const char *variant_label = NULL;
1776 struct use_case_device *device = NULL;
1777 snd_config_t *primary_cfg_copy = NULL;
1778 snd_config_t *device_variant = NULL;
1779 snd_config_t *merged_cfg = NULL;
1780 snd_config_iterator_t i, next;
1784 if (parse_get_safe_name(uc_mgr, cfg, data2, &name) < 0)
1787 if (uc_mgr->conf_format >= 8 && (colon = strchr(name, ':'))) {
1788 variant_label = colon + 1;
1790 err = snd_config_search(cfg, "DeviceVariant", &device_variant);
1792 snd_config_t *variant_cfg = NULL;
1794 /* Save a copy of the primary config for creating variant devices */
1795 err = snd_config_copy(&primary_cfg_copy, cfg);
1801 err = snd_config_search(device_variant, variant_label, &variant_cfg);
1803 err = uc_mgr_config_copy_eval_merge(uc_mgr, &merged_cfg, cfg, variant_cfg);
1813 /* in-place evaluation */
1814 if (cfg != merged_cfg) {
1815 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
1822 err = parse_device_by_name(uc_mgr, cfg, verb, name, &device);
1828 snd_config_delete(merged_cfg);
1832 if (device_variant == NULL)
1835 if (device->dev_list.type == DEVLIST_SUPPORTED) {
1836 snd_error(UCM, "DeviceVariant cannot be used with SupportedDevice");
1841 if (snd_config_get_type(device_variant) != SND_CONFIG_TYPE_COMPOUND) {
1842 snd_error(UCM, "compound type expected for DeviceVariant");
1847 colon = strchr(device->name, ':');
1849 snd_error(UCM, "DeviceVariant requires ':' in device name");
1854 snd_config_for_each(i, next, device_variant) {
1855 const char *variant_name;
1856 char variant_device_name[128];
1857 struct use_case_device *variant = NULL;
1859 n = snd_config_iterator_entry(i);
1861 if (snd_config_get_id(n, &variant_name) < 0)
1864 /* Create variant device name: base:variant_name */
1865 snprintf(variant_device_name, sizeof(variant_device_name),
1866 "%.*s:%s", (int)(colon - device->name),
1867 device->name, variant_name);
1869 err = uc_mgr_config_copy_eval_merge(uc_mgr, &merged_cfg, primary_cfg_copy, n);
1873 err = parse_device_by_name(uc_mgr, merged_cfg, verb,
1874 variant_device_name, &variant);
1875 snd_config_delete(merged_cfg);
1880 /* Link variant to primary device */
1881 list_add(&variant->variant_list, &device->variants);
1883 err = uc_mgr_put_to_dev_list(&device->dev_list, variant->name);
1886 if (device->dev_list.type == DEVLIST_NONE)
1887 device->dev_list.type = DEVLIST_CONFLICTING;
1894 snd_config_delete(merged_cfg);
1895 if (primary_cfg_copy)
1896 snd_config_delete(primary_cfg_copy);
1901 * Parse Device Rename/Delete Command
1903 * # The devices might be renamed to allow the better conditional runtime
1904 * # evaluation. Bellow example renames Speaker1 device to Speaker and
1905 * # removes Speaker2 device.
1906 * RenameDevice."Speaker1" "Speaker"
1907 * RemoveDevice."Speaker2" "Speaker2"
1909 static int parse_dev_name_list(snd_use_case_mgr_t *uc_mgr,
1911 struct list_head *list)
1914 snd_config_iterator_t i, next;
1915 const char *id, *name1;
1916 char *name1s, *name2;
1917 struct ucm_dev_name *dev;
1918 snd_config_iterator_t pos;
1921 if (snd_config_get_id(cfg, &id) < 0)
1924 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1925 snd_error(UCM, "compound type expected for %s", id);
1929 snd_config_for_each(i, next, cfg) {
1930 n = snd_config_iterator_entry(i);
1932 if (snd_config_get_id(n, &name1) < 0)
1935 err = get_string3(uc_mgr, name1, &name1s);
1939 err = parse_string_substitute3(uc_mgr, n, &name2);
1942 snd_error(UCM, "failed to get target device name for '%s'", name1);
1946 /* skip duplicates */
1947 list_for_each(pos, list) {
1948 dev = list_entry(pos, struct ucm_dev_name, list);
1949 if (strcmp(dev->name1, name1s) == 0) {
1958 dev = calloc(1, sizeof(*dev));
1963 dev->name1 = strdup(name1);
1964 if (dev->name1 == NULL) {
1970 list_add_tail(&dev->list, list);
1976 static int parse_compound_check_legacy(snd_use_case_mgr_t *uc_mgr,
1978 int (*fcn)(snd_use_case_mgr_t *, snd_config_t *, void *, void *),
1981 const char *id, *idchild;
1982 int child_ctr = 0, legacy_format = 1;
1983 snd_config_iterator_t i, next;
1984 snd_config_t *child;
1987 err = snd_config_get_id(cfg, &id);
1991 snd_config_for_each(i, next, cfg) {
1993 if (child_ctr > 1) {
1997 child = snd_config_iterator_entry(i);
1999 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
2004 if (snd_config_get_id(child, &idchild) < 0)
2007 if (strcmp(idchild, "0")) {
2012 if (child_ctr != 1) {
2017 return parse_compound(uc_mgr, cfg, fcn, data1, (void *)id);
2019 return fcn(uc_mgr, cfg, data1, NULL);
2022 static int parse_device_name(snd_use_case_mgr_t *uc_mgr,
2025 void *data2 ATTRIBUTE_UNUSED)
2027 return parse_compound_check_legacy(uc_mgr, cfg, parse_device, data1);
2030 static int parse_modifier_name(snd_use_case_mgr_t *uc_mgr,
2033 void *data2 ATTRIBUTE_UNUSED)
2035 return parse_compound_check_legacy(uc_mgr, cfg, parse_modifier, data1);
2038 static int verb_dev_list_add(struct use_case_verb *verb,
2039 enum dev_list_type dst_type,
2043 struct use_case_device *device;
2044 struct list_head *pos;
2046 list_for_each(pos, &verb->device_list) {
2047 device = list_entry(pos, struct use_case_device, list);
2049 if (strcmp(device->name, dst) != 0)
2051 if (device->dev_list.type != dst_type) {
2052 if (list_empty(&device->dev_list.list)) {
2053 device->dev_list.type = dst_type;
2055 snd_error(UCM, "incompatible device list type ('%s', '%s')", device->orig_name, src);
2059 return uc_mgr_put_to_dev_list(&device->dev_list, src);
2061 snd_error(UCM, "unable to find device '%s'", dst);
2065 static int verb_dev_list_check(struct use_case_verb *verb)
2067 struct list_head *pos, *pos2, *pos3;
2068 struct use_case_device *device, *target_dev;
2069 struct dev_list_node *dlist, *dlist2;
2071 int max_iterations = 100; /* safety limit to prevent infinite loops */
2072 bool added_something;
2074 /* First pass: ensure bidirectional relationships */
2075 list_for_each(pos, &verb->device_list) {
2076 device = list_entry(pos, struct use_case_device, list);
2077 list_for_each(pos2, &device->dev_list.list) {
2078 dlist = list_entry(pos2, struct dev_list_node, list);
2079 err = verb_dev_list_add(verb, device->dev_list.type,
2080 dlist->name, device->name);
2086 /* Second pass: complete other relationships for devices in group.
2087 * For n devices, at most n-1 iterations are needed.
2089 for (iteration = 0; iteration < max_iterations; iteration++) {
2090 added_something = false;
2092 list_for_each(pos, &verb->device_list) {
2093 device = list_entry(pos, struct use_case_device, list);
2095 if (device->dev_list.type == DEVLIST_NONE)
2098 list_for_each(pos2, &device->dev_list.list) {
2099 dlist = list_entry(pos2, struct dev_list_node, list);
2102 list_for_each(pos3, &verb->device_list) {
2103 struct use_case_device *tmp_dev;
2104 tmp_dev = list_entry(pos3, struct use_case_device, list);
2105 if (strcmp(tmp_dev->name, dlist->name) == 0) {
2106 target_dev = tmp_dev;
2114 list_for_each(pos3, &device->dev_list.list) {
2115 dlist2 = list_entry(pos3, struct dev_list_node, list);
2117 if (strcmp(dlist2->name, target_dev->name) == 0)
2120 /* verb_dev_list_add returns 1 if device was added, 0 if already exists */
2121 err = verb_dev_list_add(verb, device->dev_list.type,
2122 target_dev->name, dlist2->name);
2126 added_something = true;
2131 /* If nothing was added in this iteration, we're done */
2132 if (!added_something)
2136 if (iteration >= max_iterations) {
2137 snd_error(UCM, "too many device list iterations for verb '%s'", verb->name);
2145 * Check if a device name is already in use
2147 static int is_device_name_used(struct use_case_verb *verb, const char *name, struct use_case_device *current)
2149 struct list_head *pos;
2150 struct use_case_device *device;
2152 list_for_each(pos, &verb->device_list) {
2153 device = list_entry(pos, struct use_case_device, list);
2154 if (device != current && strcmp(device->name, name) == 0)
2161 * Update all references to a device name in modifiers and other devices.
2162 * This helper function is used when renaming devices to ensure all
2163 * dev_list references are updated accordingly.
2165 static int verb_update_device_references(struct use_case_verb *verb,
2166 const char *old_name,
2167 const char *new_name)
2169 struct list_head *pos, *pos2;
2170 struct use_case_device *device;
2171 struct use_case_modifier *modifier;
2172 struct dev_list_node *dlist;
2175 list_for_each(pos, &verb->modifier_list) {
2176 modifier = list_entry(pos, struct use_case_modifier, list);
2177 list_for_each(pos2, &modifier->dev_list.list) {
2178 dlist = list_entry(pos2, struct dev_list_node, list);
2179 if (strcmp(dlist->name, old_name) == 0) {
2180 name_copy = strdup(new_name);
2181 if (name_copy == NULL)
2184 dlist->name = name_copy;
2189 list_for_each(pos, &verb->device_list) {
2190 device = list_entry(pos, struct use_case_device, list);
2191 list_for_each(pos2, &device->dev_list.list) {
2192 dlist = list_entry(pos2, struct dev_list_node, list);
2193 if (strcmp(dlist->name, old_name) == 0) {
2194 name_copy = strdup(new_name);
2195 if (name_copy == NULL)
2198 dlist->name = name_copy;
2207 * Normalize device names according to use-case.h specification.
2208 * Device names like "HDMI 1" or "Line 1" should be normalized to "HDMI1" and "Line1".
2209 * When device name contains ':', add index and remove everything after ':' (including).
2210 * If final name is already used, retry with higher index.
2211 * Also updates dev_list members in modifiers and devices to reference the normalized names.
2213 static int verb_normalize_device_names(snd_use_case_mgr_t *uc_mgr, struct use_case_verb *verb)
2215 struct list_head *pos;
2216 struct use_case_device *device;
2217 char *orig_name, *norm_name, *colon;
2221 list_for_each(pos, &verb->device_list) {
2222 device = list_entry(pos, struct use_case_device, list);
2224 orig_name = strdup(device->name);
2225 if (orig_name == NULL)
2228 norm_name = strdup(device->name);
2229 if (norm_name == NULL) {
2234 if (uc_mgr->conf_format < 8)
2237 colon = strchr(norm_name, ':');
2239 if (colon[1] == '\0' || strchr(colon + 1, ' ')) {
2240 snd_error(UCM, "device descriptor cannot be empty or contain spaces '%s'", orig_name);
2247 snprintf(temp, sizeof(temp), "%s%d", norm_name, index);
2248 if (!is_device_name_used(verb, temp, device))
2251 } while (index < 100); /* Safety limit */
2253 snd_error(UCM, "too many device name conflicts for '%s'", orig_name);
2260 err = parse_device_index(&norm_name, &index);
2262 snd_error(UCM, "cannot parse device name '%s'", orig_name);
2271 snprintf(temp, sizeof(temp), "%s%d", norm_name, index);
2275 device->name = strdup(temp);
2276 if (device->name == NULL) {
2281 /* Update all references to the old device name */
2282 err = verb_update_device_references(verb, orig_name, device->name);
2299 * Strip index from single device names.
2300 * According to use-case.h specification, if there is only one device
2301 * with a given base name (e.g., only "HDMI1" and no "HDMI2"), the index
2302 * should be stripped to produce the final name (e.g., "HDMI").
2304 static int verb_strip_single_device_index(struct use_case_verb *verb)
2306 struct list_head *pos, *pos2;
2307 struct use_case_device *device, *device2;
2308 char *base_name, *test_base;
2310 int count, index, test_index, err;
2312 list_for_each(pos, &verb->device_list) {
2313 device = list_entry(pos, struct use_case_device, list);
2315 base_name = strdup(device->name);
2316 if (base_name == NULL)
2319 err = parse_device_index(&base_name, &index);
2330 /* Count how many devices have the same base name */
2332 list_for_each(pos2, &verb->device_list) {
2333 device2 = list_entry(pos2, struct use_case_device, list);
2334 test_base = strdup(device2->name);
2335 if (test_base == NULL) {
2340 err = parse_device_index(&test_base, &test_index);
2341 if (err >= 0 && strcmp(test_base, base_name) == 0)
2348 orig_name = device->name;
2349 device->name = base_name;
2351 err = verb_update_device_references(verb, orig_name, device->name);
2353 device->name = orig_name;
2368 * Determine priority for a device.
2370 * 1. If 'Priority' value exists, use it as the sort key
2371 * 2. If 'PlaybackPriority' value exists, use it as the sort key
2372 * 3. If 'CapturePriority' value exists, use it as the sort key
2373 * 4. Fallback: LONG_MIN (no priority)
2375 static long verb_device_get_priority(struct use_case_device *device)
2377 struct list_head *pos;
2378 struct ucm_value *val;
2379 const char *priority_str = NULL;
2380 long priority = LONG_MIN;
2383 list_for_each(pos, &device->value_list) {
2384 val = list_entry(pos, struct ucm_value, list);
2385 if (strcmp(val->name, "Priority") == 0) {
2386 priority_str = val->data;
2391 if (!priority_str) {
2392 list_for_each(pos, &device->value_list) {
2393 val = list_entry(pos, struct ucm_value, list);
2394 if (strcmp(val->name, "PlaybackPriority") == 0) {
2395 priority_str = val->data;
2401 if (!priority_str) {
2402 list_for_each(pos, &device->value_list) {
2403 val = list_entry(pos, struct ucm_value, list);
2404 if (strcmp(val->name, "CapturePriority") == 0) {
2405 priority_str = val->data;
2412 err = safe_strtol(priority_str, &priority);
2414 priority = LONG_MIN;
2421 * Sort devices based on priority values.
2423 * 1. If 'Priority' value exists, use it as the sort key
2424 * 2. If 'PlaybackPriority' value exists, use it as the sort key
2425 * 3. If 'CapturePriority' value exists, use it as the sort key
2426 * 4. Fallback: use device->name (original) as the sort key
2427 * Higher priority values are placed first in the list.
2429 static int verb_sort_devices(struct use_case_verb *verb)
2431 struct list_head sorted_list;
2432 struct list_head *pos, *npos;
2433 struct use_case_device *device, *insert_dev;
2435 INIT_LIST_HEAD(&sorted_list);
2437 /* First pass: determine and cache priority for all devices */
2438 list_for_each(pos, &verb->device_list) {
2439 device = list_entry(pos, struct use_case_device, list);
2440 device->sort_priority = verb_device_get_priority(device);
2443 /* Move devices from verb->device_list to sorted_list in sorted order */
2444 list_for_each_safe(pos, npos, &verb->device_list) {
2445 device = list_entry(pos, struct use_case_device, list);
2447 /* Remove device from original list */
2448 list_del(&device->list);
2450 /* Find the insertion point in sorted_list */
2451 /* Devices are sorted in descending order of priority (higher priority first) */
2452 /* If priorities are equal or not defined, use device name as key */
2453 if (list_empty(&sorted_list)) {
2454 list_add_tail(&device->list, &sorted_list);
2456 struct list_head *pos2, *insert_pos = &sorted_list;
2457 list_for_each(pos2, &sorted_list) {
2458 insert_dev = list_entry(pos2, struct use_case_device, list);
2460 if (device->sort_priority > insert_dev->sort_priority) {
2463 } else if (device->sort_priority == insert_dev->sort_priority) {
2464 if (strcmp(device->name, insert_dev->name) < 0) {
2471 list_add_tail(&device->list, insert_pos);
2475 /* Move sorted list back to verb->device_list */
2476 list_splice_init(&sorted_list, &verb->device_list);
2481 static int verb_device_management(snd_use_case_mgr_t *uc_mgr, struct use_case_verb *verb)
2483 struct list_head *pos;
2484 struct ucm_dev_name *dev;
2487 /* rename devices */
2488 list_for_each(pos, &verb->rename_list) {
2489 dev = list_entry(pos, struct ucm_dev_name, list);
2490 err = uc_mgr_rename_device(verb, dev->name1, dev->name2);
2492 snd_error(UCM, "cannot rename device '%s' to '%s'", dev->name1, dev->name2);
2497 /* remove devices */
2498 list_for_each(pos, &verb->remove_list) {
2499 dev = list_entry(pos, struct ucm_dev_name, list);
2500 err = uc_mgr_remove_device(verb, dev->name2);
2502 snd_error(UCM, "cannot remove device '%s'", dev->name2);
2507 /* those lists are no longer used */
2508 uc_mgr_free_dev_name_list(&verb->rename_list);
2509 uc_mgr_free_dev_name_list(&verb->remove_list);
2511 /* strip index from single device names */
2512 if (uc_mgr->conf_format >= 8) {
2513 /* sort devices by priority */
2514 err = verb_sort_devices(verb);
2519 /* normalize device names to remove spaces per use-case.h specification */
2520 err = verb_normalize_device_names(uc_mgr, verb);
2524 /* strip index from single device names */
2525 if (uc_mgr->conf_format >= 8) {
2526 err = verb_strip_single_device_index(verb);
2532 /* handle conflicting/supported lists */
2533 return verb_dev_list_check(verb);
2537 * Parse Verb Section
2539 * # Example Use case verb section for Voice call blah
2540 * # By Joe Blogs <joe@blogs.com>
2543 * # enable and disable sequences are compulsory
2545 * cset "name='Master Playback Switch',index=2 0,0"
2546 * cset "name='Master Playback Volume',index=2 25,25"
2548 * cset "name='Master Playback Switch',index=2 1,1"
2549 * cset "name='Master Playback Volume',index=2 50,50"
2553 * cset "name='Master Playback Switch',index=2 0,0"
2554 * cset "name='Master Playback Volume',index=2 25,25"
2556 * cset "name='Master Playback Switch',index=2 1,1"
2557 * cset "name='Master Playback Volume',index=2 50,50"
2560 * # Optional transition verb
2561 * TransitionSequence."ToCaseName" [
2565 * # Optional TQ and ALSA PCMs
2569 * PlaybackPCM "hw:0"
2573 static int parse_verb(snd_use_case_mgr_t *uc_mgr,
2574 struct use_case_verb *verb,
2577 snd_config_iterator_t i, next;
2581 /* in-place evaluation */
2582 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
2586 /* parse verb section */
2587 snd_config_for_each(i, next, cfg) {
2589 n = snd_config_iterator_entry(i);
2590 if (snd_config_get_id(n, &id) < 0)
2593 if (strcmp(id, "EnableSequence") == 0) {
2594 err = parse_sequence(uc_mgr, &verb->enable_list, n);
2596 snd_error(UCM, "failed to parse verb enable sequence");
2602 if (strcmp(id, "DisableSequence") == 0) {
2603 err = parse_sequence(uc_mgr, &verb->disable_list, n);
2605 snd_error(UCM, "failed to parse verb disable sequence");
2611 if (strcmp(id, "TransitionSequence") == 0) {
2612 snd_debug(UCM, "Parse TransitionSequence");
2613 err = parse_transition(uc_mgr, &verb->transition_list, n);
2615 snd_error(UCM, "failed to parse transition sequence");
2621 if (strcmp(id, "Value") == 0) {
2622 err = parse_value(uc_mgr, &verb->value_list, n);
2633 * Parse a Use case verb configuration.
2635 * This configuration contains the following :-
2636 * o Verb enable and disable sequences.
2637 * o Supported Device enable and disable sequences for verb.
2638 * o Supported Modifier enable and disable sequences for verb
2639 * o Optional QoS for the verb and modifiers.
2640 * o Optional PCM device ID for verb and modifiers
2641 * o Alias kcontrols IDs for master and volumes and mutes.
2643 static int parse_verb_config(snd_use_case_mgr_t *uc_mgr,
2644 const char *use_case_name,
2645 const char *comment,
2649 snd_config_iterator_t i, next;
2651 struct use_case_verb *verb;
2655 verb = calloc(1, sizeof(struct use_case_verb));
2658 INIT_LIST_HEAD(&verb->enable_list);
2659 INIT_LIST_HEAD(&verb->disable_list);
2660 INIT_LIST_HEAD(&verb->transition_list);
2661 INIT_LIST_HEAD(&verb->device_list);
2662 INIT_LIST_HEAD(&verb->cmpt_device_list);
2663 INIT_LIST_HEAD(&verb->modifier_list);
2664 INIT_LIST_HEAD(&verb->value_list);
2665 INIT_LIST_HEAD(&verb->rename_list);
2666 INIT_LIST_HEAD(&verb->remove_list);
2667 list_add_tail(&verb->list, &uc_mgr->verb_list);
2668 if (use_case_name == NULL)
2670 verb->name = strdup(use_case_name);
2671 if (verb->name == NULL)
2674 if (comment != NULL) {
2675 verb->comment = strdup(comment);
2676 if (verb->comment == NULL)
2680 /* in-place evaluation */
2681 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
2685 /* parse master config sections */
2686 snd_config_for_each(i, next, cfg) {
2688 n = snd_config_iterator_entry(i);
2689 if (snd_config_get_id(n, &id) < 0)
2692 /* find verb section and parse it */
2693 if (strcmp(id, "SectionVerb") == 0) {
2694 err = parse_verb(uc_mgr, verb, n);
2696 snd_error(UCM, "%s failed to parse verb", what);
2702 /* find device sections and parse them */
2703 if (strcmp(id, "SectionDevice") == 0) {
2704 err = parse_compound(uc_mgr, n,
2705 parse_device_name, verb, NULL);
2707 snd_error(UCM, "%s failed to parse device", what);
2713 /* find modifier sections and parse them */
2714 if (strcmp(id, "SectionModifier") == 0) {
2715 err = parse_compound(uc_mgr, n,
2716 parse_modifier_name, verb, NULL);
2718 snd_error(UCM, "%s failed to parse modifier", what);
2724 /* device renames */
2725 if (strcmp(id, "RenameDevice") == 0) {
2726 err = parse_dev_name_list(uc_mgr, n, &verb->rename_list);
2728 snd_error(UCM, "%s failed to parse device rename", what);
2735 if (strcmp(id, "RemoveDevice") == 0) {
2736 err = parse_dev_name_list(uc_mgr, n, &verb->remove_list);
2738 snd_error(UCM, "%s failed to parse device remove", what);
2744 /* alsa-lib configuration */
2745 if (uc_mgr->conf_format > 3 && strcmp(id, "LibraryConfig") == 0) {
2746 err = parse_libconfig(uc_mgr, n);
2748 snd_error(UCM, "%s failed to parse LibConfig", what);
2755 /* use case verb must have at least 1 device */
2756 if (list_empty(&verb->device_list)) {
2757 snd_error(UCM, "no use case device defined");
2761 /* do device rename and delete */
2762 err = verb_device_management(uc_mgr, verb);
2764 snd_error(UCM, "device management error in verb '%s'", verb->name);
2772 * Parse variant information
2774 static int parse_variant(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg,
2775 char **_vfile, char **_vcomment)
2777 snd_config_iterator_t i, next;
2779 char *file = NULL, *comment = NULL;
2782 /* parse master config sections */
2783 snd_config_for_each(i, next, cfg) {
2785 n = snd_config_iterator_entry(i);
2786 if (snd_config_get_id(n, &id) < 0)
2789 /* get use case verb file name */
2790 if (strcmp(id, "File") == 0) {
2792 err = parse_string_substitute3(uc_mgr, n, &file);
2794 snd_error(UCM, "failed to get File");
2801 /* get optional use case comment */
2802 if (strncmp(id, "Comment", 7) == 0) {
2804 err = parse_string_substitute3(uc_mgr, n, &comment);
2806 snd_error(UCM, "failed to get Comment");
2813 snd_error(UCM, "unknown field '%s' in Variant section", id);
2821 *_vcomment = comment;
2831 * Parse master section for "Use Case" and "File" tags.
2833 static int parse_master_section(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg,
2834 void *data1 ATTRIBUTE_UNUSED,
2835 void *data2 ATTRIBUTE_UNUSED)
2837 snd_config_iterator_t i, next;
2838 snd_config_t *n, *variant = NULL, *config = NULL;
2839 char *use_case_name, *file = NULL, *comment = NULL;
2840 bool variant_ok = false;
2843 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
2844 snd_error(UCM, "compound type expected for use case section");
2848 err = parse_get_safe_name(uc_mgr, cfg, NULL, &use_case_name);
2850 snd_error(UCM, "unable to get name for use case section");
2854 /* in-place evaluation */
2855 uc_mgr->parse_master_section = 1;
2856 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
2857 uc_mgr->parse_master_section = 0;
2861 /* parse master config sections */
2862 snd_config_for_each(i, next, cfg) {
2864 n = snd_config_iterator_entry(i);
2865 if (snd_config_get_id(n, &id) < 0)
2868 /* get use case verb file name */
2869 if (strcmp(id, "File") == 0) {
2870 err = parse_string_substitute3(uc_mgr, n, &file);
2872 snd_error(UCM, "failed to get File");
2878 /* get use case verb configuration block (syntax version 8+) */
2879 if (strcmp(id, "Config") == 0) {
2880 if (uc_mgr->conf_format < 8) {
2881 snd_error(UCM, "Config is supported in v8+ syntax");
2885 if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {
2886 snd_error(UCM, "compound type expected for Config");
2894 /* get optional use case comment */
2895 if (strncmp(id, "Comment", 7) == 0) {
2896 err = parse_string_substitute3(uc_mgr, n, &comment);
2898 snd_error(UCM, "failed to get Comment");
2904 if (uc_mgr->conf_format >= 6 && strcmp(id, "Variant") == 0) {
2905 snd_config_iterator_t i2, next2;
2907 snd_config_for_each(i2, next2, n) {
2910 n2 = snd_config_iterator_entry(i2);
2911 if (snd_config_get_id(n2, &id2) < 0)
2913 err = uc_mgr_evaluate_inplace(uc_mgr, n2);
2916 if (strcmp(use_case_name, id2) == 0)
2922 snd_error(UCM, "unknown field '%s' in SectionUseCase", id);
2925 if (variant && !variant_ok) {
2926 snd_error(UCM, "undefined variant '%s'", use_case_name);
2931 /* check mutual exclusivity of File and Config */
2932 if (file && config) {
2933 snd_error(UCM, "both File and Config specified in SectionUseCase");
2939 snd_debug(UCM, "use_case_name %s file '%s'", use_case_name, file);
2941 /* do we have both use case name and (file or config) ? */
2942 if (!file && !config) {
2943 snd_error(UCM, "use case missing file or config");
2948 /* parse verb from file or config */
2951 /* load config from file */
2952 err = uc_mgr_config_load_file(uc_mgr, file, &cfg);
2955 /* parse the config */
2956 err = parse_verb_config(uc_mgr, use_case_name, comment, cfg, file);
2957 snd_config_delete(cfg);
2959 /* inline config - parse directly */
2960 err = parse_verb_config(uc_mgr, use_case_name, comment, config,
2961 comment ? comment : use_case_name);
2964 /* parse variants */
2965 struct list_head orig_variable_list;
2966 snd_config_t *orig_macros = NULL;
2967 int first_iteration = 1;
2969 /* save original variable list */
2970 err = uc_mgr_duplicate_variables(&orig_variable_list, &uc_mgr->variable_list);
2974 /* save original macros */
2975 if (uc_mgr->macros) {
2976 err = snd_config_copy(&orig_macros, uc_mgr->macros);
2978 goto __variant_error;
2981 snd_config_for_each(i, next, variant) {
2982 char *vfile, *vcomment;
2985 /* restore variables and macros for second and later iterations */
2986 if (!first_iteration) {
2987 uc_mgr_free_value(&uc_mgr->variable_list);
2989 err = uc_mgr_duplicate_variables(&uc_mgr->variable_list, &orig_variable_list);
2991 goto __variant_error;
2993 if (uc_mgr->macros) {
2994 snd_config_delete(uc_mgr->macros);
2995 uc_mgr->macros = NULL;
2998 err = snd_config_copy(&uc_mgr->macros, orig_macros);
3000 goto __variant_error;
3003 first_iteration = 0;
3005 n = snd_config_iterator_entry(i);
3006 if (snd_config_get_id(n, &id) < 0)
3008 if (!parse_is_name_safe(id)) {
3010 goto __variant_error;
3012 err = parse_variant(uc_mgr, n, &vfile, &vcomment);
3015 uc_mgr->parse_variant = id;
3016 if (vfile || file) {
3018 const char *fname = vfile ? vfile : file;
3019 /* load config from file */
3020 err = uc_mgr_config_load_file(uc_mgr, fname, &cfg);
3022 err = parse_verb_config(uc_mgr, id,
3023 vcomment ? vcomment : comment,
3025 snd_config_delete(cfg);
3028 /* inline config from variant */
3029 err = parse_verb_config(uc_mgr, id,
3030 vcomment ? vcomment : comment,
3032 vcomment ? vcomment : (comment ? comment : id));
3034 uc_mgr->parse_variant = NULL;
3042 uc_mgr_free_value(&orig_variable_list);
3044 snd_config_delete(orig_macros);
3048 free(use_case_name);
3055 * parse controls which should be run only at initial boot (forcefully)
3057 static int parse_controls_fixedboot(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
3061 if (!list_empty(&uc_mgr->fixedboot_list)) {
3062 snd_error(UCM, "FixedBoot list is not empty");
3065 err = parse_sequence(uc_mgr, &uc_mgr->fixedboot_list, cfg);
3067 snd_error(UCM, "Unable to parse FixedBootSequence");
3075 * parse controls which should be run only at initial boot
3077 static int parse_controls_boot(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
3081 if (!list_empty(&uc_mgr->boot_list)) {
3082 snd_error(UCM, "Boot list is not empty");
3085 err = parse_sequence(uc_mgr, &uc_mgr->boot_list, cfg);
3087 snd_error(UCM, "Unable to parse BootSequence");
3097 static int parse_controls(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
3101 if (!list_empty(&uc_mgr->default_list)) {
3102 snd_error(UCM, "Default list is not empty");
3105 err = parse_sequence(uc_mgr, &uc_mgr->default_list, cfg);
3107 snd_error(UCM, "Unable to parse SectionDefaults");
3115 * Each sound card has a master sound card file that lists all the supported
3116 * use case verbs for that sound card. i.e.
3118 * #Example master file for blah sound card
3119 * #By Joe Blogs <joe@bloggs.org>
3121 * Comment "Nice Abstracted Soundcard"
3123 * # The file is divided into Use case sections. One section per use case verb.
3125 * SectionUseCase."Voice Call" {
3126 * File "voice_call_blah"
3127 * Comment "Make a voice phone call."
3130 * SectionUseCase."HiFi" {
3132 * Comment "Play and record HiFi quality Music."
3135 * # Define Value defaults
3138 * PlaybackCTL "hw:CARD=0"
3139 * CaptureCTL "hw:CARD=0"
3142 * # The initial boot (run once) configuration.
3145 * cset "name='Master Playback Switch',index=2 1,1"
3146 * cset "name='Master Playback Volume',index=2 25,25"
3149 * # This file also stores the default sound card state.
3152 * cset "name='Master Mono Playback',index=1 0"
3153 * cset "name='Master Mono Playback Volume',index=1 0"
3154 * cset "name='PCM Switch',index=2 1,1"
3155 * exec "some binary here"
3160 * # End of example file.
3162 static int parse_master_file(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
3164 snd_config_iterator_t i, next;
3169 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
3170 snd_error(UCM, "compound type expected for master file");
3174 if (uc_mgr->conf_format >= 2) {
3175 err = parse_syntax_field(uc_mgr, cfg, uc_mgr->conf_file_name);
3180 /* in-place evaluation */
3181 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
3185 /* parse ValueGlobals first */
3186 err = snd_config_search(cfg, "ValueGlobals", &n);
3188 err = parse_value(uc_mgr, &uc_mgr->global_value_list, n);
3190 snd_error(UCM, "failed to parse ValueGlobals");
3195 err = uc_mgr_check_value(&uc_mgr->global_value_list, "BootCardGroup");
3197 uc_mgr->card_group = true;
3198 /* if we are in boot, skip the main parsing loop */
3199 if (uc_mgr->in_boot)
3203 /* parse master config sections */
3204 snd_config_for_each(i, next, cfg) {
3206 n = snd_config_iterator_entry(i);
3207 if (snd_config_get_id(n, &id) < 0)
3210 if (strcmp(id, "Comment") == 0) {
3211 err = parse_string_substitute3(uc_mgr, n, &uc_mgr->comment);
3213 snd_error(UCM, "failed to get master comment");
3219 /* find use case section and parse it */
3220 if (strcmp(id, "SectionUseCase") == 0) {
3221 err = parse_compound(uc_mgr, n,
3222 parse_master_section,
3229 /* find default control values section (force boot sequence only) */
3230 if (strcmp(id, "FixedBootSequence") == 0) {
3231 err = parse_controls_fixedboot(uc_mgr, n);
3237 /* find default control values section (first boot only) */
3238 if (strcmp(id, "BootSequence") == 0) {
3239 err = parse_controls_boot(uc_mgr, n);
3245 /* find default control values section and parse it */
3246 if (strcmp(id, "SectionDefaults") == 0) {
3247 err = parse_controls(uc_mgr, n);
3253 /* ValueDefaults is now parsed at the top of this function */
3254 if (strcmp(id, "ValueDefaults") == 0) {
3255 err = parse_value(uc_mgr, &uc_mgr->value_list, n);
3257 snd_error(UCM, "failed to parse ValueDefaults");
3263 /* ValueGlobals is parsed at the top of this function */
3264 if (strcmp(id, "ValueGlobals") == 0)
3267 /* alsa-lib configuration */
3268 if (uc_mgr->conf_format > 3 && strcmp(id, "LibraryConfig") == 0) {
3269 err = parse_libconfig(uc_mgr, n);
3271 snd_error(UCM, "failed to parse LibraryConfig");
3278 if (strcmp(id, "Error") == 0)
3279 return error_node(uc_mgr, n);
3281 /* skip further Syntax value updates (Include) */
3282 if (strcmp(id, "Syntax") == 0)
3285 snd_error(UCM, "unknown master file field %s", id);
3290 /* get the card info */
3291 static int get_card_info(snd_use_case_mgr_t *mgr,
3292 const char *ctl_name,
3293 snd_ctl_card_info_t **info)
3295 struct ctl_list *ctl_list;
3298 err = uc_mgr_open_ctl(mgr, &ctl_list, ctl_name, 0);
3303 *info = ctl_list->ctl_info;
3307 /* find the card in the local machine */
3308 static int get_by_card_name(snd_use_case_mgr_t *mgr, const char *card_name)
3311 snd_ctl_card_info_t *info;
3312 const char *_driver, *_name, *_long_name;
3314 snd_ctl_card_info_alloca(&info);
3317 if (snd_card_next(&card) < 0 || card < 0) {
3318 snd_error(UCM, "no soundcards found...");
3325 /* clear the list, keep the only one CTL device */
3326 uc_mgr_free_ctl_list(mgr);
3328 sprintf(name, "hw:%d", card);
3329 err = get_card_info(mgr, name, &info);
3332 _driver = snd_ctl_card_info_get_driver(info);
3333 _name = snd_ctl_card_info_get_name(info);
3334 _long_name = snd_ctl_card_info_get_longname(info);
3335 if (!strcmp(card_name, _driver) ||
3336 !strcmp(card_name, _name) ||
3337 !strcmp(card_name, _long_name))
3341 if (snd_card_next(&card) < 0) {
3342 snd_error(UCM, "snd_card_next");
3347 uc_mgr_free_ctl_list(mgr);
3352 /* set the driver name and long name by the card ctl name */
3353 static inline int get_by_card(snd_use_case_mgr_t *mgr, const char *ctl_name)
3355 return get_card_info(mgr, ctl_name, NULL);
3358 static int parse_toplevel_path(snd_use_case_mgr_t *uc_mgr,
3362 snd_config_iterator_t i, next, i2, next2;
3363 snd_config_t *n, *n2;
3365 char *dir = NULL, *file = NULL, fn[PATH_MAX];
3370 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
3371 snd_error(UCM, "compound type expected for UseCasePath node");
3375 /* parse use case path config sections */
3376 snd_config_for_each(i, next, cfg) {
3377 n = snd_config_iterator_entry(i);
3379 if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {
3380 snd_error(UCM, "compound type expected for UseCasePath.something node");
3384 if (snd_config_get_id(n, &id) < 0)
3389 /* parse use case path config sections */
3390 snd_config_for_each(i2, next2, n) {
3392 n2 = snd_config_iterator_entry(i2);
3393 if (snd_config_get_id(n2, &id) < 0)
3396 if (strcmp(id, "Version") == 0) {
3397 err = parse_integer_substitute(uc_mgr, n2, &version);
3399 snd_error(UCM, "unable to parse UcmDirectory");
3402 if (version < 1 || version > 2) {
3403 snd_error(UCM, "Version must be 1 or 2");
3410 if (strcmp(id, "Directory") == 0) {
3411 err = parse_string_substitute(uc_mgr, n2, &dir);
3413 snd_error(UCM, "unable to parse Directory");
3419 if (strcmp(id, "File") == 0) {
3420 err = parse_string_substitute(uc_mgr, n2, &file);
3422 snd_error(UCM, "unable to parse File");
3428 snd_error(UCM, "unknown UseCasePath field %s", id);
3432 snd_error(UCM, "Directory is not defined in %s!", filename);
3436 snd_error(UCM, "File is not defined in %s!", filename);
3440 ucm_filename(fn, sizeof(fn), version, dir, file);
3441 snd_trace(UCM, "probing configuration file '%s'", fn);
3442 if (access(fn, R_OK) == 0 && lstat64(fn, &st) == 0) {
3443 if (S_ISLNK(st.st_mode)) {
3445 char *link, *dir2, *p;
3447 link = malloc(PATH_MAX);
3450 r = readlink(fn, link, PATH_MAX - 1);
3456 p = strrchr(link, '/');
3459 dir2 = malloc(PATH_MAX);
3464 strncpy(dir2, dir, PATH_MAX - 1);
3465 strncat(dir2, "/", PATH_MAX - 1);
3466 strncat(dir2, link, PATH_MAX - 1);
3467 fn[PATH_MAX - 1] = '\0';
3473 snd_trace(UCM, "using directory '%s' and file '%s'", dir, file);
3474 if (replace_string(&uc_mgr->conf_dir_name, dir) == NULL)
3476 if (replace_string(&uc_mgr->conf_file_name, file) == NULL)
3478 strncpy(filename, fn, PATH_MAX);
3479 filename[PATH_MAX - 1] = '\0';
3480 uc_mgr->conf_format = version;
3508 static int parse_toplevel_config(snd_use_case_mgr_t *uc_mgr,
3512 snd_config_iterator_t i, next;
3517 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
3518 snd_error(UCM, "compound type expected for toplevel file");
3522 err = parse_syntax_field(uc_mgr, cfg, filename);
3526 /* in-place evaluation */
3527 err = uc_mgr_evaluate_inplace(uc_mgr, cfg);
3531 /* parse toplevel config sections */
3532 snd_config_for_each(i, next, cfg) {
3534 n = snd_config_iterator_entry(i);
3535 if (snd_config_get_id(n, &id) < 0)
3538 if (strcmp(id, "UseCasePath") == 0) {
3539 err = parse_toplevel_path(uc_mgr, filename, n);
3545 /* alsa-lib configuration */
3546 if (uc_mgr->conf_format > 3 && strcmp(id, "LibraryConfig") == 0) {
3547 err = parse_libconfig(uc_mgr, n);
3549 snd_error(UCM, "failed to parse LibConfig");
3555 /* skip further Syntax value updates (Include) */
3556 if (strcmp(id, "Syntax") == 0)
3559 snd_error(UCM, "unknown toplevel field %s", id);
3565 static int load_toplevel_config(snd_use_case_mgr_t *uc_mgr,
3568 char filename[PATH_MAX];
3572 ucm_filename(filename, sizeof(filename), 2, NULL, "ucm.conf");
3574 if (access(filename, R_OK) != 0) {
3575 snd_error(UCM, "Unable to find the top-level configuration file '%s'.", filename);
3579 err = uc_mgr_config_load(2, filename, &tcfg);
3583 /* filename is shared for function input and output! */
3584 err = parse_toplevel_config(uc_mgr, filename, tcfg);
3585 snd_config_delete(tcfg);
3589 err = uc_mgr_config_load(uc_mgr->conf_format, filename, cfg);
3591 snd_error(UCM, "could not parse configuration for card %s", uc_mgr->card_name);
3601 /* load master use case file for sound card based on rules in ucm2/ucm.conf
3603 int uc_mgr_import_master_config(snd_use_case_mgr_t *uc_mgr)
3609 err = snd_config_top(&uc_mgr->local_config);
3613 err = snd_config_top(&uc_mgr->macros);
3617 name = uc_mgr->card_name;
3618 if (strncmp(name, "hw:", 3) == 0) {
3619 err = get_by_card(uc_mgr, name);
3621 snd_error(UCM, "card '%s' is not valid", name);
3624 } else if (strncmp(name, "strict:", 7)) {
3625 /* do not handle the error here */
3626 /* we can refer the virtual UCM config */
3627 get_by_card_name(uc_mgr, name);
3630 err = load_toplevel_config(uc_mgr, &cfg);
3634 err = parse_master_file(uc_mgr, cfg);
3635 if (uc_mgr->macros) {
3636 snd_config_delete(uc_mgr->macros);
3637 uc_mgr->macros = NULL;
3639 snd_config_delete(cfg);
3641 uc_mgr_free_ctl_list(uc_mgr);
3642 uc_mgr_free_verb(uc_mgr);
3648 uc_mgr_free_ctl_list(uc_mgr);
3649 replace_string(&uc_mgr->conf_dir_name, NULL);
3653 static int filename_filter(const struct dirent64 *dirent)
3657 if (dirent->d_type == DT_DIR) {
3658 if (dirent->d_name[0] == '.') {
3659 if (dirent->d_name[1] == '\0')
3661 if (dirent->d_name[1] == '.' &&
3662 dirent->d_name[2] == '\0')
3670 /* scan all cards and comments
3672 * Cards are defined by machines. Each card/machine installs its UCM
3673 * configuration files in a subdirectory with the same name as the sound
3674 * card under /usr/share/alsa/ucm2. This function will scan all the card
3675 * directories and skip the component directories defined in the array
3678 int uc_mgr_scan_master_configs(const char **_list[])
3680 char filename[PATH_MAX], dfl[PATH_MAX], fn[FILENAME_MAX];
3681 char *env = getenv(ALSA_CONFIG_UCM2_VAR);
3682 snd_use_case_mgr_t *uc_mgr;
3683 const char **list, *d_name;
3685 snd_config_t *cfg, *c;
3686 int i, j, cnt, err, cards;
3689 struct dirent64 **namelist;
3694 err = snd_card_next(&i);
3701 cards += 4; /* plug-and-play */
3704 snprintf(filename, sizeof(filename), "%s/conf.virt.d", env);
3706 snprintf(filename, sizeof(filename), "%s/ucm2/conf.virt.d",
3707 snd_config_topdir());
3709 #if defined(_GNU_SOURCE) && \
3710 !defined(__NetBSD__) && \
3711 !defined(__FreeBSD__) && \
3712 !defined(__OpenBSD__) && \
3713 !defined(__DragonFly__) && \
3714 !defined(__sun) && \
3715 !defined(__ANDROID__) && \
3717 #define SORTFUNC versionsort64
3719 #define SORTFUNC alphasort64
3721 err = scandir64(filename, &namelist, filename_filter, SORTFUNC);
3724 snd_error(UCM, "could not scan directory %s: %s", filename, strerror(-err));
3730 if (strlen(filename) + 8 < sizeof(filename)) {
3731 strcat(filename, "/default");
3732 ss = readlink(filename, dfl, sizeof(dfl)-1);
3735 dfl[sizeof(dfl)-1] = '\0';
3736 if (dfl[0] && dfl[strlen(dfl)-1] == '/')
3737 dfl[strlen(dfl)-1] = '\0';
3744 list = calloc(1, (cards + cnt) * 2 * sizeof(char *));
3751 while (j / 2 < cards) {
3752 err = snd_card_next(&i);
3757 snprintf(fn, sizeof(fn), "-hw:%d", i);
3758 err = snd_use_case_mgr_open(&uc_mgr, fn);
3759 if (err == -ENOENT || err == -ENXIO)
3762 snd_error(UCM, "Unable to open '%s': %s", fn, snd_strerror(err));
3765 err = snd_use_case_get(uc_mgr, "comment", (const char **)&s);
3767 err = snd_card_get_longname(i, &s);
3771 snd_use_case_mgr_close(uc_mgr);
3772 list[j] = strdup(fn + 1);
3773 if (list[j] == NULL) {
3782 for (i = 0; i < cnt; i++) {
3784 d_name = namelist[i]->d_name;
3786 snprintf(fn, sizeof(fn), "%s.conf", d_name);
3787 ucm_filename(filename, sizeof(filename), 2, d_name, fn);
3789 if (eaccess(filename, R_OK))
3791 if (access(filename, R_OK))
3795 err = uc_mgr_config_load(2, filename, &cfg);
3798 err = snd_config_search(cfg, "Syntax", &c);
3800 snd_error(UCM, "Syntax field not found in %s", d_name);
3801 snd_config_delete(cfg);
3804 err = snd_config_get_integer(c, &l);
3806 snd_error(UCM, "Syntax field is invalid in %s", d_name);
3807 snd_config_delete(cfg);
3810 if (l < 2 || l > SYNTAX_VERSION_MAX) {
3811 snd_error(UCM, "Incompatible syntax %d in %s", l, d_name);
3812 snd_config_delete(cfg);
3815 err = snd_config_search(cfg, "Comment", &c);
3817 err = parse_string(c, (char **)&list[j+1]);
3819 snd_config_delete(cfg);
3823 snd_config_delete(cfg);
3824 list[j] = strdup(d_name);
3825 if (list[j] == NULL) {
3829 if (strcmp(dfl, list[j]) == 0) {
3830 /* default to top */
3831 const char *save1 = list[j];
3832 const char *save2 = list[j + 1];
3833 memmove(list + 2, list, j * sizeof(char *));
3842 for (i = 0; i < cnt; i++)
3846 for (i = 0; i < j; i++) {
3847 free((void *)list[i * 2]);
3848 free((void *)list[i * 2 + 1]);