summaryrefslogtreecommitdiff
path: root/src/ucm/ucm_confdoc.h
blob: 42c331b6140c7f72efa90bf00d81b8996f8f2199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  Support for the verb/device/modifier core logic and API,
 *  command line tool and file parser was kindly sponsored by
 *  Texas Instruments Inc.
 *  Support for multiple active modifiers and devices,
 *  transition sequences, multiple client access and user defined use
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
 *
 *  Copyright (C) 2021 Red Hat Inc.
 *  Authors: Jaroslav Kysela <perex@perex.cz>
 */

/**
 *  \defgroup ucm_conf Use Case Configuration
 *  The ALSA Use Case Configuration.
 *  See \ref Usecase_conf page for more details.
 *  \{
 */

/*! \page Usecase_conf ALSA Use Case Configuration

The use case configuration files use \ref conf syntax to define the
static configuration tree. This tree is evaluated (modified) at runtime
according the conditions and dynamic variables in the configuration tree.
The result is parsed and exported to the applications using \ref ucm API.

### Configuration directory and main filename lookup

The lookup paths are describen in *ucm.conf* file. The configuration
structure looks like:

~~~{.html}
UseCasePath.path1 {
  Directory "conf.virt.d"
  File "${OpenName}.conf"
}
UseCasePath.path2 {
  Directory "external"
  File "${OpenName}.conf"
}
~~~

### UCM main configuration file

Each sound card has a master sound card file that lists all the supported
use case verbs for that sound card. i.e.:

~~~{.html}
# Example master file for blah sound card
# By Joe Blogs <joe@bloggs.org>

# Use Case name for user interface
Comment "Nice Abstracted Soundcard"

# The file is divided into Use case sections. One section per use case verb.

SectionUseCase."Voice Call" {
  File "voice_call_blah"
  Comment "Make a voice phone call."
}

SectionUseCase."HiFi" {
  File "hifi_blah"
  Comment "Play and record HiFi quality Music."
}

# Define Value defaults

ValueDefaults {
  PlaybackChannels 4
  CaptureChannels 4
}

# Define boot / initialization sequence
# This sequence is skipped, when the soundcard was already configured by system
# (alsactl configuration was already created). The purpose is to not alter
# ALSA card controls which may be modified by user after initial settings.

BootSequence [
  cset "name='My control' on"
]

# Define fixed boot sequence
# This sequence is always executed on boot (hotplug).

FixedBootSequence [
  cset "name='Something to toggle' toggle"
]
~~~

### UCM verb configuration file

The verb configuration file defines devices, modifier and initialization sequences.
It is something like a sound profile.

~~~{.html}
# Example Use case verb section for Voice call blah
# By Joe Blogs <joe@blogs.com>

# verb global section

SectionVerb {

  # enable and disable sequences are compulsory
  EnableSequence [
    cset "name='Master Playback Switch',index=2 0,0"
    cset "name='Master Playback Volume',index=2 25,25"
    msleep 50
    cset "name='Master Playback Switch',index=2 1,1"
    cset "name='Master Playback Volume',index=2 50,50"
  ]

  DisableSequence [
    cset "name='Master Playback Switch',index=2 0,0"
    cset "name='Master Playback Volume',index=2 25,25"
    msleep 50
    cset "name='Master Playback Switch',index=2 1,1"
    cset "name='Master Playback Volume',index=2 50,50"
  ]

  # Optional transition verb
  TransitionSequence."ToCaseName" [
    msleep 1
  ]

  # Optional TQ and device values
  Value {
    TQ HiFi
    PlaybackChannels 6
  }
}

# Each device is described in new section. N devices are allowed

SectionDevice."Headphones" {

  SupportedDevice [
    "x"
    "y"
  ]

  # or (not both)

  ConflictingDevice [
    "x"
    "y"
  ]

  EnableSequence [
    ...
  ]

  DisableSequence [
    ...
  ]

  TransitionSequence."ToDevice" [
    ...
  ]

  Value {
    PlaybackVolume "name='Master Playback Volume',index=2"
    PlaybackSwitch "name='Master Playback Switch',index=2"
    PlaybackPCM "hw:${CardId},4"
  }
}

# Each modifier is described in new section. N modifiers are allowed

SectionModifier."Capture Voice" {
  Comment "Record voice call"

  SupportedDevice [
    "x"
    "y"
  ]

  # or (not both)

  ConflictingDevice [
    "x"
    "y"
  ]

  EnableSequence [
    ...
  ]

  DisableSequence [
    ...
  ]

  TransitionSequence."ToModifierName" [
    ...
  ]

  # Optional TQ and ALSA PCMs
  Value {
    TQ Voice
    CapturePCM "hw:${CardId},11"
    PlaybackVolume "name='Master Playback Volume',index=2"
    PlaybackSwitch "name='Master Playback Switch',index=2"
  }
}
~~~

### Sequence commands

Command name   | Description
---------------|----------------------------------------------
enadev2        | execute device enable sequence
disdev2        | execute device disable sequence
cdev ARG       | ALSA control device name for snd_ctl_open()
cset ARG       | ALSA control set - snd_ctl_ascii_elem_id_parse() + snd_ctl_ascii_value_parse()
cset-new ARG   | Create new ALSA user control element - snd_ctl_ascii_elem_id_parse() + description
ctl-remove ARG | Remove ALSA user control element - snd_ctl_ascii_elem_id_parse()
sysw ARG       | write to sysfs tree
usleep ARG     | sleep for specified amount of microseconds
msleep ARG     | sleep for specified amount of milliseconds
exec ARG       | execute a specific command (without shell - *man execv*)
shell ARG      | execute a specific command (using shell - *man system*)
cfg-save ARG   | save LibraryConfig to a file

~~~{.html}
# Examples
cset "name='PCM Playback Volue',index=2 99"
cset-new "name='Bool2' type=bool,count=2 1,0"
cset-new "name='Enum' type=enum,labels='L1;L2;L3' 'L2'"
ctl-remove "name='Bool2'"
sysw "-/class/sound/ctl-led/speaker/card${CardNumber}/attach:Speaker Channel Switch"
usleep 10
exec "/bin/echo hello"
shell "set"
cfg-save "/tmp/test.conf:+pcm"
~~~

### Naming (devices, verbs)

See the SND_USE_CASE_VERB constains like #SND_USE_CASE_VERB_HIFI for the full list of known verbs.

See the SND_USE_CASE_DEV constants like #SND_USE_CASE_DEV_SPEAKER for the full list of known devices.
If multiple devices with the same name exists, the number suffixes should
be added to these names like HDMI1,HDMI2,HDMI3 etc. No number gaps are
allowed. The names with numbers must be continuous. It is allowed to put
a whitespace between name and index (like 'Line 1') for the better
readability. The device names 'Line 1' and 'Line1' are equal for
this purpose.

If EnableSequence/DisableSequence controls independent paths in the hardware
it is also recommended to split playback and capture UCM devices and use
the number suffixes. Example use case: Use the integrated microphone
in the laptop instead the microphone in headphones.

The preference of the devices is determined by the priority value (higher value = higher priority).

See the SND_USE_CASE_MOD constants like #SND_USE_CASE_MOD_ECHO_REF for the full list of known modifiers.

### Boot (alsactl)

The *FixedBootSequence* is executed at each boot. The *BootSequence* is executed only
if the card's configuration is missing. The purpose is to let the users modify the
configuration like volumes or switches. The alsactl ensures the persistency (store
the state of the controls to the /var tree and loads the previous state in the next
boot).

### Device volume

It is expected that the applications handle the volume settings. It is not recommended
to set the fixed values for the volume settings to the Enable / Disable sequences for
verbs or devices, if the device exports the hardware volume (MixerElem or Volume/Switch
values). The default volume settings should be set in the *BootSequence*.

### Dynamic configuration tree

The evaluation order may look a bit different from the user perspective.
At first, the standard alsa-lib configuration tree is parsed. All other
layers on top are working with this tree. It may shift / move the configuration
blocks from the configuration files as they are placed to the tree internally.

~~~{.html}
Example configuration       | Parsed static tree      | Identical static tree
----------------------------+-------------------------+-------------------------------
If.1 {                      | If {                    | If.1.True.Define.VAR "A"
  True.Define.VAR "A"       |   1.True.Define.VAR "A" | If.2.True.Define.VAR "C"
}                           |   2.True.Define.VAR "C" | Define.VAR "B"
Define.VAR "B"              | }                       |
If.2 {                      | Define.VAR "B"          |
  True.Define.VAR "C"       |                         |
}                           |                         |
~~~

Even if one or both conditions are evaluated as true, the variable _VAR_ will
be evaluated always as **B** because the first _If_ block was before the non-nested
_Define_ . The second _If_ block was appended to the first _If_ block (before
_Define_ in the configuration tree) in the text configuration parser.


### Syntax

Unless described, the syntax version 4 is used.

~~~
Syntax 4
~~~


### Include

There are two ways to include other configuration files.

#### Static include

The static include is inherited from the standard alsa-lib configuration
syntax. It can be placed anywhere in the configuration files. The search
path is composed from the root alsa configuration path (usually
_/usr/share/alsa_) and _ucm2_ directory.

~~~{.html}
<some/path/file.conf>        # include file using the search path
</absolute/path/file.conf>   # include file using the absolute path
~~~

#### Lazy include

The lazy include is evaluated at runtime. The root path is the ucm2
tree. The absolute include appends the ucm2 absolute path to the
specified path. The relative include is relative to the file which
contains the _Include_ configuration block.

~~~{.html}
Include.id1.File "/some/path/file.conf"  # absolute include (in the ucm2 tree)
Include.id2.File "subdir/file.conf"      # relative include to the current configuration directory (UseCasePath)
~~~

### Configuration tree evaluation

The evaluation of the static configuration tree is proceed in
the specific order (see table bellow). When the dynamic configuration
tree changes, the evaluation sequence is restarted to evaluate
all possible changes (new *Define* or *Include* or *If* blocks).

Evaluation order   | Configuration block | Evaluation restart
------------------:|---------------------|--------------------
1                  | Define              | No
2                  | Include             | Yes
3                  | If                  | Yes


### Substitutions

The dynamic tree identifiers and assigned values in the configuration tree are
substituted. The substitutes strings are in the table bellow.

Substituted string   | Value
---------------------|---------------------
${OpenName}          | Original UCM card name (passed to snd_use_case_mgr_open())
${ConfLibDir}        | Library top-level configuration directory (e.g. /usr/share/alsa)
${ConfTopDir}        | Top-level UCM configuration directory (e.g. /usr/share/alsa/ucm2)
${ConfDir}           | Card's UCM configuration directory (e.g. /usr/share/alsa/ucm2/conf.d/USB-Audio)
${ConfName}          | Configuration name (e.g. USB-Audio.conf)
${CardNumber}        | Real ALSA card number (or empty string for the virtual UCM card)
${CardId}            | ALSA card identifier (see snd_ctl_card_info_get_id())
${CardDriver}        | ALSA card driver (see snd_ctl_card_info_get_driver())
${CardName}          | ALSA card name (see snd_ctl_card_info_get_name())
${CardLongName}      | ALSA card long name (see snd_ctl_card_info_get_longname())
${CardComponents}    | ALSA card components (see snd_ctl_card_info_get_components())
${env:<str>}         | Environment variable <str>
${sys:<str>}         | Contents of sysfs file <str>
${var:<str>}         | UCM parser variable (set using a _Define_ block)
${eval:<str>}        | Evaluate expression like *($var+2)/3* [**Syntax 5**]
${find-card:<str>}   | Find a card - see _Find card substitution_ section
${find-device:<str>} | Find a device - see _Find device substitution_ section

#### Special whole string substitution

Substituted string   | Value
---------------------|---------------------
${evali:<str>}       | Evaluate expression like *($var+2)/3* [**Syntax 6**]; target node will be integer; substituted only in the LibraryConfig subtree

#### Find card substitution

This substitutions finds the ALSA card and returns the appropriate identifier or
the card number (see return argument).

Usage example:

~~~{.html}
${find-card:field=name,regex='^acp$',return=number}
~~~

Arguments:

Argument             | Description
---------------------|-----------------------
return               | return value type (id, number), id is the default
field                | field for the lookup (id, driver, name, longname, mixername, components)
regex                | regex string for the field match

#### Find device substitution

Usage example:

~~~{.html}
${find-device:type=pcm,field=name,regex='DMIC'}
~~~

Arguments:

Argument             | Description
---------------------|-----------------------
type                 | device type (pcm)
stream               | stream type (playback, capture), playback is default
field                | field for the lookup (id, name, subname)
regex                | regex string for the field match


### Variable defines

The variables can be defined and altered with the *Define* or *DefineRegex* blocks.
The *Define* block looks like:

~~~{.html}
Define {
  variable1 "a"
  variable2 "b"
}
~~~

The *DefineRegex* allows substring extraction like:

~~~{.html}
DefineRegex.rval {
  Regex "(hello)|(regex)"
  String "hello, it's my regex"
}
~~~

The result will be stored to variables *rval1* as *hello* and *rval2* as *regex* (every matched
substrings are stored to a separate variable with the sequence number postfix.

Variables can be substituted using the `${var:rval1}` reference for example.

### Macros

Macros were added for *Syntax* version *6*. The *DefineMacro* defines new
macro like:

~~~{.html}
DefineMacro.macro1 {
  Define.a "${var:__arg1}"
  Define.b "${var:__other}"
  # Device or any other block may be defined here...
}
~~~

The arguments in the macro are refered as the variables with the double
underscore name prefix (like *__variable*). The configuration block in
the DefineMacro subtree is always evaluated (including arguments and variables)
at the time of the instantiation.

The macros can be instantiated (expanded) using:

~~~{.html}
# short version
Macro.id1.macro1 "arg1='something 1',other='other x'"

# long version
Macro.id1.macro1 {
  arg1 'something 1'
  other 'other x'
}
~~~

The second identifier (in example as *id1*) must be unique, but the contents
is ignored. It just differentiate the items in the subtree (allowing multiple
instances for one macro).

### Conditions

The configuration tree evaluation supports the conditions - *If* blocks. Each *If* blocks
must define a *Condition* block and *True* or *False* blocks or both. The *True* or *False*
blocks will be merged to the parent tree (where the *If* block is defined) when
the *Condition* is evaluated.

Example:

~~~{.html}
If.uniqueid {
  Condition {
    Type String
    Haystack "abcd"
    Needle "a"
  }
  True {
    Define.a a
    define.b b
  }
}
~~~

#### True (Type AlwaysTrue)

Execute only *True* block. It may be used to change the evaluation order as
explained in the *Configuration Tree* paragraph.

#### String is empty (Type String)

Field                | Description
---------------------|-----------------------
Empty                | string

#### Strings are equal (Type String)

Field                | Description
---------------------|-----------------------
String1              | string
String2              | substring in string

#### Substring is present (Type String)

Field                | Description
---------------------|-----------------------
Haystack             | string
Needle               | substring in string

#### Regex match (Type RegexMatch)

Field                | Description
---------------------|-----------------------
String               | string
Regex                | regex expression (extended posix, ignore case)

#### ALSA control element exists (Type ControlExists)

Field                | Description
---------------------|-----------------------
Device               | ALSA control device (see snd_ctl_open())
Control              | control in ASCII (parsed using snd_ctl_ascii_elem_id_parse())
ControlEnum	     | value for the enum control (optional)

Example:

~~~{.html}
If.fmic {
  Condition {
    Type ControlExists
    Control "name='Front Mic Playback Switch'"
  }
  True {
    ...
  }
}
~~~

### Variants

To avoid duplication of the many configuration files for the cases with
minimal configuration changes, there is the variant extension. Variants were
added for *Syntax* version *6*.

The bellow example will create two verbs - "HiFi" and "HiFi 7.1" with
the different playback channels (2 and 8) for the "Speaker" device.

Example (main configuration file):

~~~{.html}
SectionUseCase."HiFi" {
  File "HiFi.conf"
  Variant."HiFi" {
    Comment "HiFi"
  }
  Variant."HiFi 7+1" {
    Comment "HiFi 7.1"
  }
}
~~~

Example (verb configuration file - HiFi.conf):

~~~{.html}
SectionDevice."Speaker" {
  Value {
    PlaybackChannels 2
  }
  Variant."HiFi 7+1".Value {
    PlaybackChannels 8
  }
}
~~~

*/

/**
 \}
 */