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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
36 /** The name of the environment variable containing the UCM directory */
37 #define ALSA_CONFIG_UCM_VAR "ALSA_CONFIG_UCM"
39 static int parse_sequence(snd_use_case_mgr_t *uc_mgr,
40 struct list_head *base,
46 int parse_string(snd_config_t *n, char **res)
50 err = snd_config_get_string(n, (const char **)res);
62 int parse_is_name_safe(const char *name)
64 if (strchr(name, '.')) {
65 uc_error("char '.' not allowed in '%s'", name);
71 int parse_get_safe_id(snd_config_t *n, const char **id)
75 err = snd_config_get_id(n, id);
78 if (!parse_is_name_safe((char *)(*id)))
86 static int parse_transition(snd_use_case_mgr_t *uc_mgr,
87 struct list_head *tlist,
90 struct transition_sequence *tseq;
92 snd_config_iterator_t i, next;
96 if (snd_config_get_id(cfg, &id) < 0)
99 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
100 uc_error("compound type expected for %s", id);
104 snd_config_for_each(i, next, cfg) {
105 n = snd_config_iterator_entry(i);
107 if (snd_config_get_id(n, &id) < 0)
110 tseq = calloc(1, sizeof(*tseq));
113 INIT_LIST_HEAD(&tseq->transition_list);
115 tseq->name = strdup(id);
116 if (tseq->name == NULL) {
121 err = parse_sequence(uc_mgr, &tseq->transition_list, n);
123 uc_mgr_free_transition_element(tseq);
127 list_add(&tseq->list, tlist);
135 static int parse_compound(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg,
136 int (*fcn)(snd_use_case_mgr_t *, snd_config_t *, void *, void *),
137 void *data1, void *data2)
140 snd_config_iterator_t i, next;
144 if (snd_config_get_id(cfg, &id) < 0)
147 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
148 uc_error("compound type expected for %s", id);
152 snd_config_for_each(i, next, cfg) {
153 n = snd_config_iterator_entry(i);
155 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
156 uc_error("compound type expected for %s, is %d", id, snd_config_get_type(cfg));
160 err = fcn(uc_mgr, n, data1, data2);
168 static int strip_legacy_dev_index(char *name)
170 char *dot = strchr(name, '.');
173 if (dot[1] != '0' || dot[2] != '\0') {
174 uc_error("device name %s contains a '.',"
175 " and is not legacy foo.0 format", name);
185 static int parse_device_list(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED,
186 struct dev_list *dev_list,
187 enum dev_list_type type,
190 struct dev_list_node *sdev;
192 snd_config_iterator_t i, next;
196 if (dev_list->type != DEVLIST_NONE) {
197 uc_error("error: multiple supported or"
198 " conflicting device lists");
202 if (snd_config_get_id(cfg, &id) < 0)
205 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
206 uc_error("compound type expected for %s", id);
210 snd_config_for_each(i, next, cfg) {
211 n = snd_config_iterator_entry(i);
213 if (snd_config_get_id(n, &id) < 0)
216 sdev = calloc(1, sizeof(struct dev_list_node));
219 err = parse_string(n, &sdev->name);
224 err = strip_legacy_dev_index(sdev->name);
230 list_add(&sdev->list, &dev_list->list);
233 dev_list->type = type;
241 * Sequence controls elements are in the following form:-
244 * cset "element_id_syntax value_syntax"
246 * exec "any unix command with arguments"
249 * cset "name='Master Playback Switch' 0,0"
250 * cset "iface=PCM,name='Disable HDMI',index=1 0"
252 static int parse_sequence(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED,
253 struct list_head *base,
256 struct sequence_element *curr;
257 snd_config_iterator_t i, next;
260 const char *cmd = NULL;
262 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
263 uc_error("error: compound is expected for sequence definition");
267 snd_config_for_each(i, next, cfg) {
270 n = snd_config_iterator_entry(i);
271 err = snd_config_get_id(n, &id);
275 if (snd_config_get_type(n) != SND_CONFIG_TYPE_STRING) {
276 uc_error("error: string type is expected for sequence command");
279 snd_config_get_string(n, &cmd);
283 /* alloc new sequence element */
284 curr = calloc(1, sizeof(struct sequence_element));
287 list_add_tail(&curr->list, base);
289 if (strcmp(cmd, "cdev") == 0) {
290 curr->type = SEQUENCE_ELEMENT_TYPE_CDEV;
291 err = parse_string(n, &curr->data.cdev);
293 uc_error("error: cdev requires a string!");
299 if (strcmp(cmd, "cset") == 0) {
300 curr->type = SEQUENCE_ELEMENT_TYPE_CSET;
301 err = parse_string(n, &curr->data.cset);
303 uc_error("error: cset requires a string!");
309 if (strcmp(cmd, "usleep") == 0) {
310 curr->type = SEQUENCE_ELEMENT_TYPE_SLEEP;
311 err = snd_config_get_integer(n, &curr->data.sleep);
313 uc_error("error: usleep requires integer!");
319 if (strcmp(cmd, "msleep") == 0) {
320 curr->type = SEQUENCE_ELEMENT_TYPE_SLEEP;
321 err = snd_config_get_integer(n, &curr->data.sleep);
323 uc_error("error: msleep requires integer!");
326 curr->data.sleep *= 1000L;
330 if (strcmp(cmd, "exec") == 0) {
331 curr->type = SEQUENCE_ELEMENT_TYPE_EXEC;
332 err = parse_string(n, &curr->data.exec);
334 uc_error("error: exec requires a string!");
340 list_del(&curr->list);
341 uc_mgr_free_sequence_element(curr);
350 * Parse values describing PCM, control/mixer settings and stream parameters.
355 * PlaybackVolume "name='Master Playback Volume',index=2"
356 * PlaybackSwitch "name='Master Playback Switch',index=2"
359 static int parse_value(snd_use_case_mgr_t *uc_mgr ATTRIBUTE_UNUSED,
360 struct list_head *base,
363 struct ucm_value *curr;
364 snd_config_iterator_t i, next;
369 snd_config_type_t type;
372 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
373 uc_error("error: compound is expected for value definition");
376 snd_config_for_each(i, next, cfg) {
378 n = snd_config_iterator_entry(i);
379 err = snd_config_get_id(n, &id);
383 /* alloc new value */
384 curr = calloc(1, sizeof(struct ucm_value));
387 list_add_tail(&curr->list, base);
388 curr->name = strdup(id);
389 if (curr->name == NULL)
391 type = snd_config_get_type(n);
393 case SND_CONFIG_TYPE_INTEGER:
394 curr->data = malloc(16);
395 if (curr->data == NULL)
397 snd_config_get_integer(n, &l);
398 sprintf(curr->data, "%li", l);
400 case SND_CONFIG_TYPE_INTEGER64:
401 curr->data = malloc(32);
402 if (curr->data == NULL)
404 snd_config_get_integer64(n, &ll);
405 sprintf(curr->data, "%lli", ll);
407 case SND_CONFIG_TYPE_REAL:
408 curr->data = malloc(64);
409 if (curr->data == NULL)
411 snd_config_get_real(n, &d);
412 sprintf(curr->data, "%-16g", d);
414 case SND_CONFIG_TYPE_STRING:
415 err = parse_string(n, &curr->data);
417 uc_error("error: unable to parse a string for id '%s'!", id);
422 uc_error("error: invalid type %i in Value compound", type);
431 * Parse Modifier Use cases
433 * # Each modifier is described in new section. N modifiers are allowed
434 * SectionModifier."Capture Voice" {
436 * Comment "Record voice call"
443 * ConflictingDevice [
456 * TransitionSequence."ToModifierName" [
460 * # Optional TQ and ALSA PCMs
464 * PlaybackVolume "name='Master Playback Volume',index=2"
465 * PlaybackSwitch "name='Master Playback Switch',index=2"
470 * SupportedDevice and ConflictingDevice cannot be specified together.
473 static int parse_modifier(snd_use_case_mgr_t *uc_mgr,
478 struct use_case_verb *verb = data1;
479 struct use_case_modifier *modifier;
481 snd_config_iterator_t i, next;
487 if (!parse_is_name_safe(name))
491 if (parse_get_safe_id(cfg, &name) < 0)
495 /* allocate modifier */
496 modifier = calloc(1, sizeof(*modifier));
497 if (modifier == NULL)
499 INIT_LIST_HEAD(&modifier->enable_list);
500 INIT_LIST_HEAD(&modifier->disable_list);
501 INIT_LIST_HEAD(&modifier->transition_list);
502 INIT_LIST_HEAD(&modifier->dev_list.list);
503 INIT_LIST_HEAD(&modifier->value_list);
504 list_add_tail(&modifier->list, &verb->modifier_list);
505 modifier->name = strdup(name);
507 snd_config_for_each(i, next, cfg) {
509 n = snd_config_iterator_entry(i);
510 if (snd_config_get_id(n, &id) < 0)
513 if (strcmp(id, "Comment") == 0) {
514 err = parse_string(n, &modifier->comment);
516 uc_error("error: failed to get modifier comment");
522 if (strcmp(id, "SupportedDevice") == 0) {
523 err = parse_device_list(uc_mgr, &modifier->dev_list,
524 DEVLIST_SUPPORTED, n);
526 uc_error("error: failed to parse supported"
532 if (strcmp(id, "ConflictingDevice") == 0) {
533 err = parse_device_list(uc_mgr, &modifier->dev_list,
534 DEVLIST_CONFLICTING, n);
536 uc_error("error: failed to parse conflicting"
542 if (strcmp(id, "EnableSequence") == 0) {
543 err = parse_sequence(uc_mgr, &modifier->enable_list, n);
545 uc_error("error: failed to parse modifier"
552 if (strcmp(id, "DisableSequence") == 0) {
553 err = parse_sequence(uc_mgr, &modifier->disable_list, n);
555 uc_error("error: failed to parse modifier"
556 " disable sequence");
562 if (strcmp(id, "TransitionSequence") == 0) {
563 err = parse_transition(uc_mgr, &modifier->transition_list, n);
565 uc_error("error: failed to parse transition"
572 if (strcmp(id, "Value") == 0) {
573 err = parse_value(uc_mgr, &modifier->value_list, n);
575 uc_error("error: failed to parse Value");
586 * Parse Device Use Cases
588 *# Each device is described in new section. N devices are allowed
589 *SectionDevice."Headphones" {
590 * Comment "Headphones connected to 3.5mm jack"
597 * ConflictingDevice [
610 * TransitionSequence."ToDevice" [
615 * PlaybackVolume "name='Master Playback Volume',index=2"
616 * PlaybackSwitch "name='Master Playback Switch',index=2"
620 static int parse_device(snd_use_case_mgr_t *uc_mgr,
625 struct use_case_verb *verb = data1;
627 struct use_case_device *device;
628 snd_config_iterator_t i, next;
634 if (!parse_is_name_safe(name))
638 if (parse_get_safe_id(cfg, &name) < 0)
642 device = calloc(1, sizeof(*device));
645 INIT_LIST_HEAD(&device->enable_list);
646 INIT_LIST_HEAD(&device->disable_list);
647 INIT_LIST_HEAD(&device->transition_list);
648 INIT_LIST_HEAD(&device->dev_list.list);
649 INIT_LIST_HEAD(&device->value_list);
650 list_add_tail(&device->list, &verb->device_list);
651 device->name = strdup(name);
653 snd_config_for_each(i, next, cfg) {
655 n = snd_config_iterator_entry(i);
656 if (snd_config_get_id(n, &id) < 0)
659 if (strcmp(id, "Comment") == 0) {
660 err = parse_string(n, &device->comment);
662 uc_error("error: failed to get device comment");
668 if (strcmp(id, "SupportedDevice") == 0) {
669 err = parse_device_list(uc_mgr, &device->dev_list,
670 DEVLIST_SUPPORTED, n);
672 uc_error("error: failed to parse supported"
678 if (strcmp(id, "ConflictingDevice") == 0) {
679 err = parse_device_list(uc_mgr, &device->dev_list,
680 DEVLIST_CONFLICTING, n);
682 uc_error("error: failed to parse conflicting"
688 if (strcmp(id, "EnableSequence") == 0) {
689 uc_dbg("EnableSequence");
690 err = parse_sequence(uc_mgr, &device->enable_list, n);
692 uc_error("error: failed to parse device enable"
699 if (strcmp(id, "DisableSequence") == 0) {
700 uc_dbg("DisableSequence");
701 err = parse_sequence(uc_mgr, &device->disable_list, n);
703 uc_error("error: failed to parse device disable"
710 if (strcmp(id, "TransitionSequence") == 0) {
711 uc_dbg("TransitionSequence");
712 err = parse_transition(uc_mgr, &device->transition_list, n);
714 uc_error("error: failed to parse transition"
721 if (strcmp(id, "Value") == 0) {
722 err = parse_value(uc_mgr, &device->value_list, n);
724 uc_error("error: failed to parse Value");
733 static int parse_compound_check_legacy(snd_use_case_mgr_t *uc_mgr,
735 int (*fcn)(snd_use_case_mgr_t *, snd_config_t *, void *, void *),
738 const char *id, *idchild;
739 int child_ctr = 0, legacy_format = 1;
740 snd_config_iterator_t i, next;
744 err = snd_config_get_id(cfg, &id);
748 snd_config_for_each(i, next, cfg) {
754 child = snd_config_iterator_entry(i);
756 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
761 if (snd_config_get_id(child, &idchild) < 0)
764 if (strcmp(idchild, "0")) {
769 if (child_ctr != 1) {
774 return parse_compound(uc_mgr, cfg, fcn, data1, (void *)id);
776 return fcn(uc_mgr, cfg, data1, NULL);
779 static int parse_device_name(snd_use_case_mgr_t *uc_mgr,
782 void *data2 ATTRIBUTE_UNUSED)
784 return parse_compound_check_legacy(uc_mgr, cfg, parse_device, data1);
787 static int parse_modifier_name(snd_use_case_mgr_t *uc_mgr,
790 void *data2 ATTRIBUTE_UNUSED)
792 return parse_compound_check_legacy(uc_mgr, cfg, parse_modifier, data1);
798 * # Example Use case verb section for Voice call blah
799 * # By Joe Blogs <joe@blogs.com>
802 * # enable and disable sequences are compulsory
804 * cset "name='Master Playback Switch',index=2 0,0"
805 * cset "name='Master Playback Volume',index=2 25,25"
807 * cset "name='Master Playback Switch',index=2 1,1"
808 * cset "name='Master Playback Volume',index=2 50,50"
812 * cset "name='Master Playback Switch',index=2 0,0"
813 * cset "name='Master Playback Volume',index=2 25,25"
815 * cset "name='Master Playback Switch',index=2 1,1"
816 * cset "name='Master Playback Volume',index=2 50,50"
819 * # Optional transition verb
820 * TransitionSequence."ToCaseName" [
824 * # Optional TQ and ALSA PCMs
832 static int parse_verb(snd_use_case_mgr_t *uc_mgr,
833 struct use_case_verb *verb,
836 snd_config_iterator_t i, next;
840 /* parse verb section */
841 snd_config_for_each(i, next, cfg) {
843 n = snd_config_iterator_entry(i);
844 if (snd_config_get_id(n, &id) < 0)
847 if (strcmp(id, "EnableSequence") == 0) {
848 uc_dbg("Parse EnableSequence");
849 err = parse_sequence(uc_mgr, &verb->enable_list, n);
851 uc_error("error: failed to parse verb enable sequence");
857 if (strcmp(id, "DisableSequence") == 0) {
858 uc_dbg("Parse DisableSequence");
859 err = parse_sequence(uc_mgr, &verb->disable_list, n);
861 uc_error("error: failed to parse verb disable sequence");
867 if (strcmp(id, "TransitionSequence") == 0) {
868 uc_dbg("Parse TransitionSequence");
869 err = parse_transition(uc_mgr, &verb->transition_list, n);
871 uc_error("error: failed to parse transition sequence");
877 if (strcmp(id, "Value") == 0) {
878 uc_dbg("Parse Value");
879 err = parse_value(uc_mgr, &verb->value_list, n);
890 * Parse a Use case verb file.
892 * This file contains the following :-
893 * o Verb enable and disable sequences.
894 * o Supported Device enable and disable sequences for verb.
895 * o Supported Modifier enable and disable sequences for verb
896 * o Optional QoS for the verb and modifiers.
897 * o Optional PCM device ID for verb and modifiers
898 * o Alias kcontrols IDs for master and volumes and mutes.
900 static int parse_verb_file(snd_use_case_mgr_t *uc_mgr,
901 const char *use_case_name,
905 snd_config_iterator_t i, next;
907 struct use_case_verb *verb;
909 char filename[MAX_FILE];
910 char *env = getenv(ALSA_CONFIG_UCM_VAR);
914 verb = calloc(1, sizeof(struct use_case_verb));
917 INIT_LIST_HEAD(&verb->enable_list);
918 INIT_LIST_HEAD(&verb->disable_list);
919 INIT_LIST_HEAD(&verb->transition_list);
920 INIT_LIST_HEAD(&verb->device_list);
921 INIT_LIST_HEAD(&verb->modifier_list);
922 INIT_LIST_HEAD(&verb->value_list);
923 list_add_tail(&verb->list, &uc_mgr->verb_list);
924 if (use_case_name == NULL)
926 verb->name = strdup(use_case_name);
927 if (verb->name == NULL)
930 if (comment != NULL) {
931 verb->comment = strdup(comment);
932 if (verb->comment == NULL)
936 /* open Verb file for reading */
937 snprintf(filename, sizeof(filename), "%s/%s/%s",
938 env ? env : ALSA_USE_CASE_DIR,
939 uc_mgr->card_name, file);
940 filename[sizeof(filename)-1] = '\0';
942 err = uc_mgr_config_load(filename, &cfg);
944 uc_error("error: failed to open verb file %s : %d",
949 /* parse master config sections */
950 snd_config_for_each(i, next, cfg) {
952 n = snd_config_iterator_entry(i);
953 if (snd_config_get_id(n, &id) < 0)
956 /* find verb section and parse it */
957 if (strcmp(id, "SectionVerb") == 0) {
958 err = parse_verb(uc_mgr, verb, n);
960 uc_error("error: %s failed to parse verb",
967 /* find device sections and parse them */
968 if (strcmp(id, "SectionDevice") == 0) {
969 err = parse_compound(uc_mgr, n,
970 parse_device_name, verb, NULL);
972 uc_error("error: %s failed to parse device",
979 /* find modifier sections and parse them */
980 if (strcmp(id, "SectionModifier") == 0) {
981 err = parse_compound(uc_mgr, n,
982 parse_modifier_name, verb, NULL);
984 uc_error("error: %s failed to parse modifier",
992 /* use case verb must have at least 1 device */
993 if (list_empty(&verb->device_list)) {
994 uc_error("error: no use case device defined", file);
1001 * Parse master section for "Use Case" and "File" tags.
1003 static int parse_master_section(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg,
1004 void *data1 ATTRIBUTE_UNUSED,
1005 void *data2 ATTRIBUTE_UNUSED)
1007 snd_config_iterator_t i, next;
1009 const char *use_case_name, *file = NULL, *comment = NULL;
1012 if (snd_config_get_id(cfg, &use_case_name) < 0) {
1013 uc_error("unable to get name for use case section");
1017 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1018 uc_error("compound type expected for use case section");
1021 /* parse master config sections */
1022 snd_config_for_each(i, next, cfg) {
1024 n = snd_config_iterator_entry(i);
1025 if (snd_config_get_id(n, &id) < 0)
1028 /* get use case verb file name */
1029 if (strcmp(id, "File") == 0) {
1030 err = snd_config_get_string(n, &file);
1032 uc_error("failed to get File");
1038 /* get optional use case comment */
1039 if (strncmp(id, "Comment", 7) == 0) {
1040 err = snd_config_get_string(n, &comment);
1042 uc_error("error: failed to get Comment");
1048 uc_error("unknown field %s in master section");
1051 uc_dbg("use_case_name %s file '%s'", use_case_name, file);
1053 /* do we have both use case name and file ? */
1055 uc_error("error: use case missing file");
1059 /* parse verb file */
1060 return parse_verb_file(uc_mgr, use_case_name, comment, file);
1066 static int parse_controls(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
1070 if (!list_empty(&uc_mgr->default_list)) {
1071 uc_error("Default list is not empty");
1074 err = parse_sequence(uc_mgr, &uc_mgr->default_list, cfg);
1076 uc_error("Unable to parse SectionDefaults");
1084 * Each sound card has a master sound card file that lists all the supported
1085 * use case verbs for that sound card. i.e.
1087 * #Example master file for blah sound card
1088 * #By Joe Blogs <joe@bloggs.org>
1090 * Comment "Nice Abstracted Soundcard"
1092 * # The file is divided into Use case sections. One section per use case verb.
1094 * SectionUseCase."Voice Call" {
1095 * File "voice_call_blah"
1096 * Comment "Make a voice phone call."
1099 * SectionUseCase."HiFi" {
1101 * Comment "Play and record HiFi quality Music."
1104 * # Define Value defaults
1107 * PlaybackCTL "hw:CARD=0"
1108 * CaptureCTL "hw:CARD=0"
1111 * # This file also stores the default sound card state.
1114 * cset "name='Master Playback Switch',index=2 1,1"
1115 * cset "name='Master Playback Volume',index=2 25,25"
1116 * cset "name='Master Mono Playback',index=1 0"
1117 * cset "name='Master Mono Playback Volume',index=1 0"
1118 * cset "name='PCM Switch',index=2 1,1"
1119 * exec "some binary here"
1124 * # End of example file.
1126 static int parse_master_file(snd_use_case_mgr_t *uc_mgr, snd_config_t *cfg)
1128 snd_config_iterator_t i, next;
1133 if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
1134 uc_error("compound type expected for master file");
1138 /* parse master config sections */
1139 snd_config_for_each(i, next, cfg) {
1141 n = snd_config_iterator_entry(i);
1142 if (snd_config_get_id(n, &id) < 0)
1145 if (strcmp(id, "Comment") == 0) {
1146 err = parse_string(n, &uc_mgr->comment);
1148 uc_error("error: failed to get master comment");
1154 /* find use case section and parse it */
1155 if (strcmp(id, "SectionUseCase") == 0) {
1156 err = parse_compound(uc_mgr, n,
1157 parse_master_section,
1164 /* find default control values section and parse it */
1165 if (strcmp(id, "SectionDefaults") == 0) {
1166 err = parse_controls(uc_mgr, n);
1172 /* get the default values */
1173 if (strcmp(id, "ValueDefaults") == 0) {
1174 err = parse_value(uc_mgr, &uc_mgr->value_list, n);
1176 uc_error("error: failed to parse ValueDefaults");
1182 uc_error("uknown master file field %s", id);
1187 static int load_master_config(const char *card_name, snd_config_t **cfg)
1189 char filename[MAX_FILE];
1190 char *env = getenv(ALSA_CONFIG_UCM_VAR);
1193 snprintf(filename, sizeof(filename)-1,
1194 "%s/%s/%s.conf", env ? env : ALSA_USE_CASE_DIR,
1195 card_name, card_name);
1196 filename[MAX_FILE-1] = '\0';
1198 err = uc_mgr_config_load(filename, cfg);
1200 uc_error("error: could not parse configuration for card %s",
1208 /* load master use case file for sound card */
1209 int uc_mgr_import_master_config(snd_use_case_mgr_t *uc_mgr)
1214 err = load_master_config(uc_mgr->card_name, &cfg);
1217 err = parse_master_file(uc_mgr, cfg);
1218 snd_config_delete(cfg);
1220 uc_mgr_free_verb(uc_mgr);
1225 static int filename_filter(const struct dirent *dirent)
1229 if (dirent->d_type == DT_DIR) {
1230 if (dirent->d_name[0] == '.') {
1231 if (dirent->d_name[1] == '\0')
1233 if (dirent->d_name[1] == '.' &&
1234 dirent->d_name[2] == '\0')
1242 /* scan all cards and comments */
1243 int uc_mgr_scan_master_configs(const char **_list[])
1245 char filename[MAX_FILE], dfl[MAX_FILE];
1246 char *env = getenv(ALSA_CONFIG_UCM_VAR);
1248 snd_config_t *cfg, *c;
1251 struct dirent **namelist;
1253 snprintf(filename, sizeof(filename)-1,
1254 "%s", env ? env : ALSA_USE_CASE_DIR);
1255 filename[MAX_FILE-1] = '\0';
1257 err = scandir(filename, &namelist, filename_filter, versionsort);
1260 uc_error("error: could not scan directory %s: %s",
1261 filename, strerror(-err));
1267 if (strlen(filename) + 8 < sizeof(filename)) {
1268 strcat(filename, "/default");
1269 ss = readlink(filename, dfl, sizeof(dfl)-1);
1272 dfl[sizeof(dfl)-1] = '\0';
1273 if (dfl[0] && dfl[strlen(dfl)-1] == '/')
1274 dfl[strlen(dfl)-1] = '\0';
1280 list = calloc(1, cnt * 2 * sizeof(char *));
1286 for (i = 0; i < cnt; i++) {
1287 err = load_master_config(namelist[i]->d_name, &cfg);
1290 err = snd_config_search(cfg, "Comment", &c);
1292 err = parse_string(c, (char **)&list[i*2+1]);
1294 snd_config_delete(cfg);
1298 snd_config_delete(cfg);
1299 list[i * 2] = strdup(namelist[i]->d_name);
1300 if (list[i * 2] == NULL) {
1304 if (strcmp(dfl, list[i * 2]) == 0) {
1305 /* default to top */
1306 const char *save1 = list[i * 2];
1307 const char *save2 = list[i * 2 + 1];
1308 memmove(list + 2, list, i * 2 * sizeof(char *));
1316 for (i = 0; i < cnt; i++) {
1319 free((void *)list[i * 2]);
1320 free((void *)list[i * 2 + 1]);