-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio_config.c
2154 lines (1786 loc) · 58.7 KB
/
audio_config.c
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012-2013 Wolfson Microelectronics plc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "tiny_hal_config"
/*#define LOG_NDEBUG 0*/
/*#undef NDEBUG*/
#include <stddef.h>
#include <errno.h>
#include <assert.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <cutils/compiler.h>
#include <system/audio.h>
/* Workaround for linker error if audio_effect.h is included in multiple
* source files. Prevent audio.h including it */
#define ANDROID_AUDIO_EFFECT_H
struct effect_interface_s;
typedef struct effect_interface_s **effect_handle_t;
#include <hardware/audio.h>
#include <tinyalsa/asoundlib.h>
#include <expat.h>
#include "audio_config.h"
#define MIXER_CARD_DEFAULT 0
#define PCM_CARD_DEFAULT 0
#define PCM_DEVICE_DEFAULT 0
#define COMPRESS_CARD_DEFAULT 0
#define COMPRESS_DEVICE_DEFAULT 0
/* The dynamic arrays are extended in multiples of this number of objects */
#define DYN_ARRAY_GRANULE 16
/* Largest byte array control we handle */
#define BYTE_ARRAY_MAX_SIZE 512
#define INVALID_CTL_INDEX 0xFFFFFFFFUL
struct config_mgr;
struct stream;
struct path;
struct device;
struct usecase;
struct scase;
/* Dynamically extended array of fixed-size objects */
struct dyn_array {
uint count;
uint16_t max_count; /* current maximum size of allocated array */
uint16_t elem_size; /* size of array elements */
union {
void *data;
struct device *devices;
struct stream *streams;
struct path *paths;
struct usecase *usecases;
struct scase *cases;
struct ctl *ctls;
const char **path_names;
};
};
/* Paths for "on" and "off" are a special case and have fixed ids */
enum {
e_path_id_off = 0,
e_path_id_on = 1,
e_path_id_custom_base = 2
};
struct ctl {
struct mixer_ctl *ctl;
uint32_t index;
uint32_t array_count;
union {
uint32_t uinteger;
const char *name;
const uint8_t *data;
} value;
};
struct path {
int id; /* Integer identifier of this path */
struct dyn_array ctl_array;
};
struct device {
uint32_t type; /* 0 is reserved for the global device */
int use_count; /* counts total streams using this device */
struct dyn_array path_array;
};
struct scase {
const char *name;
struct dyn_array ctl_array;
};
struct usecase {
const char *name;
struct dyn_array case_array;
};
struct stream_control {
struct mixer_ctl *ctl;
uint id;
uint min;
uint max;
};
struct volume_control {
struct stream_control control;
uint min;
uint max;
};
struct stream {
struct hw_stream info; /* must be first member */
struct config_mgr* cm;
const char* name;
int ref_count;
int max_ref_count;
int enable_path; /* id of paths to invoke when enabled */
int disable_path; /* id of paths to invoke when disabled */
uint32_t current_devices; /* devices currently active for this stream */
struct {
struct stream_control volume_left;
struct stream_control volume_right;
} controls;
struct dyn_array usecase_array;
};
struct config_mgr {
pthread_mutex_t lock;
struct mixer *mixer;
uint32_t supported_output_devices;
uint32_t supported_input_devices;
struct dyn_array device_array;
struct dyn_array stream_array;
};
/*********************************************************************
* Structures and enums for XML parser
*********************************************************************/
#define MAX_PARSE_DEPTH 6
/* For faster parsing put more commonly-used elements first */
enum element_index {
e_elem_ctl = 0,
e_elem_path,
e_elem_device,
e_elem_stream,
e_elem_enable,
e_elem_disable,
e_elem_case,
e_elem_usecase,
e_elem_stream_ctl,
e_elem_init,
e_elem_mixer,
e_elem_audiohal,
e_elem_count
};
/* For faster parsing put more commonly-used attribs first */
enum attrib_index {
e_attrib_name = 0,
e_attrib_val,
e_attrib_path,
e_attrib_function,
e_attrib_type,
e_attrib_index,
e_attrib_dir,
e_attrib_card,
e_attrib_device,
e_attrib_instances,
e_attrib_rate,
e_attrib_period_size,
e_attrib_period_count,
e_attrib_min,
e_attrib_max,
e_attrib_count
};
#define BIT(x) (1<<(x))
struct parse_state;
typedef int(*elem_fn)(struct parse_state *state);
struct parse_element {
const char *name;
uint16_t valid_attribs; /* bitflags of valid attribs for this element */
uint16_t required_attribs; /* bitflags of attribs that must be present */
uint16_t valid_subelem; /* bitflags of valid sub-elements */
elem_fn start_fn;
elem_fn end_fn;
};
struct parse_attrib {
const char *name;
};
struct parse_device {
const char *name;
uint32_t device;
};
struct parse_stack_entry {
uint16_t elem_index;
uint16_t valid_subelem;
};
/* Temporary state info for config file parser */
struct parse_state {
struct config_mgr *cm;
FILE *file;
XML_Parser parser;
char read_buf[256];
int parse_error; /* value >0 aborts without error */
int error_line;
int mixer_card_number;
struct {
const char *value[e_attrib_count];
const XML_Char **all;
} attribs;
/* Current parent object. We don't need a stack because
* an object cannot be nested below an object of the same type
* so there can only ever be zero or one object of a given type
* active at any time
*/
struct {
struct device *device;
struct stream *stream;
struct path *path;
struct usecase *usecase;
struct scase *scase;
} current;
/* This array hold a de-duplicated list of all path names encountered */
struct dyn_array path_name_array;
/* This is a temporary path object used to collect the initial
* mixer setup control settings under <mixer><init>
*/
struct path init_path;
struct {
int index;
struct parse_stack_entry entry[MAX_PARSE_DEPTH];
} stack;
};
static const char *debug_device_to_name(uint32_t device);
/*********************************************************************
* Routing control
*********************************************************************/
static void apply_ctls_l( struct ctl *pctl, const int ctl_count )
{
int i;
unsigned int vnum;
unsigned int value_count;
int err = 0;
uint8_t ctl_data[BYTE_ARRAY_MAX_SIZE];
ALOGV("+apply_ctls_l");
for (i = 0; i < ctl_count; ++i, ++pctl) {
switch (mixer_ctl_get_type(pctl->ctl)) {
case MIXER_CTL_TYPE_BOOL:
case MIXER_CTL_TYPE_INT:
value_count = mixer_ctl_get_num_values(pctl->ctl);
ALOGV("apply ctl '%s' = 0x%x (%d values)",
mixer_ctl_get_name(pctl->ctl),
pctl->value.uinteger,
value_count);
if (pctl->index == INVALID_CTL_INDEX) {
for (vnum = 0; vnum < value_count; ++vnum) {
err = mixer_ctl_set_value(pctl->ctl, vnum, pctl->value.uinteger);
if (err < 0) {
break;
}
}
} else {
err = mixer_ctl_set_value(pctl->ctl, pctl->index, pctl->value.uinteger);
}
ALOGE_IF(err < 0, "Failed to set ctl '%s' to %u",
mixer_ctl_get_name(pctl->ctl),
pctl->value.uinteger);
break;
case MIXER_CTL_TYPE_BYTE:
/* byte array */
vnum = mixer_ctl_get_num_values(pctl->ctl);
ALOGV("apply ctl '%s' = byte data (%d bytes)",
mixer_ctl_get_name(pctl->ctl),
vnum);
if ((pctl->index == 0) && (pctl->array_count == vnum)) {
err = mixer_ctl_set_array(pctl->ctl, pctl->value.data, pctl->array_count);
} else {
/* read-modify-write */
err = mixer_ctl_get_array(pctl->ctl, ctl_data, vnum);
if (err >= 0) {
memcpy(&ctl_data[pctl->index], pctl->value.data, pctl->array_count);
err = mixer_ctl_set_array(pctl->ctl, ctl_data, vnum);
}
}
ALOGE_IF(err < 0, "Failed to set ctl '%s'",
mixer_ctl_get_name(pctl->ctl));
break;
case MIXER_CTL_TYPE_ENUM:
ALOGV("apply ctl '%s' to '%s'",
mixer_ctl_get_name(pctl->ctl),
pctl->value.name);
err = mixer_ctl_set_enum_by_string(pctl->ctl, pctl->value.name);
ALOGE_IF(err < 0, "Failed to set ctl '%s' to '%s'",
mixer_ctl_get_name(pctl->ctl),
pctl->value.name);
break;
default:
break;
}
}
ALOGV("-apply_ctls_l");
}
static void apply_path_l(struct path *path)
{
ALOGV("+apply_path_l(%p) id=%u", path, path->id);
apply_ctls_l(path->ctl_array.ctls, path->ctl_array.count);
ALOGV("-apply_path_l(%p)", path);
}
static void apply_device_path_l(struct device *pdev, struct path *path)
{
ALOGV("+apply_device_path_l(%p) id=%u", path, path->id);
/* The on and off paths for a device are reference-counted */
switch (path->id) {
case e_path_id_off:
if (--pdev->use_count > 0) {
ALOGV("Device still in use - not applying 'off' path");
return;
}
break;
case e_path_id_on:
if (++pdev->use_count > 1) {
ALOGV("Device already enabled - not applying 'on' path");
return;
}
break;
default:
break;
}
apply_path_l(path);
ALOGV("-apply_device_path_l(%p)", path);
}
static void apply_paths_by_id_l(struct device *pdev, int first_id,
int second_id)
{
struct path *ppath = pdev->path_array.paths;
struct path *found_paths[2] = {0};
int path_count = pdev->path_array.count;
ALOGV("Applying paths [first=%u second=%u] to device(@%p, mask=0x%x '%s')",
first_id, second_id, ppath, pdev->type, debug_device_to_name(pdev->type));
/* To save time we find both paths in a single walk of the list */
for (; path_count > 0; --path_count, ++ppath) {
if (ppath->id == first_id) {
found_paths[0] = ppath;
if ((found_paths[1] != NULL) || (first_id == second_id)) {
/* We have both paths or there is only one path to find */
break;
}
} else if (ppath->id == second_id) {
found_paths[1] = ppath;
if (found_paths[0] != NULL) {
break;
}
}
}
if (found_paths[0] != NULL) {
apply_device_path_l(pdev, found_paths[0]);
}
if (found_paths[1] != NULL) {
apply_device_path_l(pdev, found_paths[1]);
}
}
static void apply_paths_to_devices_l(struct config_mgr *cm, uint32_t devices,
int first_id, int second_id)
{
struct device *pdev = cm->device_array.devices;
int dev_count = cm->device_array.count;
const uint32_t input_flag = devices & AUDIO_DEVICE_BIT_IN;
/* invoke path path_id on all struct device matching devices */
ALOGV("Apply paths [first=%u second=%u] to devices in 0x%x",
first_id, second_id, devices);
devices &= ~AUDIO_DEVICE_BIT_IN;
while ((dev_count > 0) && (devices != 0)) {
if (((pdev->type & input_flag) == input_flag)
&& ((pdev->type & devices) != 0)) {
devices &= ~pdev->type;
apply_paths_by_id_l(pdev, first_id, second_id);
}
--dev_count;
++pdev;
}
}
static void apply_paths_to_global_l(struct config_mgr *cm,
int first_id, int second_id)
{
struct device *pdev = cm->device_array.devices;
struct device * const pend = pdev + cm->device_array.count;
ALOGV("Apply global paths [first=%u second=%u]", first_id, second_id);
while (pdev < pend) {
if (pdev->type == 0) {
apply_paths_by_id_l(pdev, first_id, second_id);
break;
}
++pdev;
}
}
uint32_t get_current_routes( const struct hw_stream *stream )
{
struct stream *s = (struct stream *)stream;
ALOGV("get_current_routes(%p) 0x%x", stream, s->current_devices);
return s->current_devices;
}
void apply_route( const struct hw_stream *stream, uint32_t devices )
{
struct stream *s = (struct stream *)stream;
struct config_mgr *cm = s->cm;
/* Only apply routes to devices that have changed state on this stream */
uint32_t enabling = devices & ~s->current_devices;
uint32_t disabling = ~devices & s->current_devices;
ALOGV("apply_route(%p) devices=0x%x", stream, devices);
if (stream_is_input(stream)) {
devices &= AUDIO_DEVICE_IN_ALL;
devices |= AUDIO_DEVICE_BIT_IN;
} else {
devices &= AUDIO_DEVICE_OUT_ALL;
}
pthread_mutex_lock(&cm->lock);
apply_paths_to_devices_l(cm, disabling, s->disable_path, e_path_id_off);
apply_paths_to_devices_l(cm, enabling, e_path_id_on, s->enable_path);
/* Save new set of devices for this stream */
s->current_devices = devices;
pthread_mutex_unlock(&cm->lock);
}
uint32_t get_routed_devices( const struct hw_stream *stream )
{
struct stream *s = (struct stream *)stream;
return s->current_devices;
}
void rotate_routes( struct config_mgr *cm, int orientation )
{
/* Route rotation not currently supported */
}
/*********************************************************************
* Stream control
*********************************************************************/
static int set_vol_ctl( const struct stream_control *volctl, uint percent)
{
uint val;
uint range;
switch (percent) {
case 0:
val = volctl->min;
break;
case 100:
val = volctl->max;
break;
default:
range = volctl->max - volctl->min;
val = volctl->min + ((percent * range)/100);
break;
}
ALOGW("set '%s' = %u", mixer_ctl_get_name(volctl->ctl), val);
mixer_ctl_set_value(volctl->ctl, volctl->id, val);
return 0;
}
int set_hw_volume( const struct hw_stream *stream, int left_pc, int right_pc)
{
struct stream *s = (struct stream *)stream;
int ret = -ENOSYS;
if (s->controls.volume_left.ctl) {
if (!s->controls.volume_right.ctl) {
/* Control is mono so average left and right */
left_pc = (left_pc + right_pc) / 2;
}
ret = set_vol_ctl(&s->controls.volume_left, left_pc);
}
if (s->controls.volume_right.ctl) {
ret = set_vol_ctl(&s->controls.volume_right, right_pc);
}
ALOGV_IF(ret == 0, "set_hw_volume: L=%d%% R=%d%%", left_pc, right_pc);
return ret;
}
static struct stream *find_named_stream(struct config_mgr *cm,
const char *name)
{
struct stream *s = cm->stream_array.streams;
int i;
for (i = cm->stream_array.count - 1; i >= 0; --i) {
if (s->name) {
if (strcmp(s->name, name) == 0) {
return s;
}
}
s++;
}
return NULL;
}
static bool open_stream_l(struct config_mgr *cm, struct stream *s)
{
if (s->ref_count < s->max_ref_count) {
++s->ref_count;
if (s->ref_count == 1) {
apply_paths_to_global_l(cm, e_path_id_on, s->enable_path);
}
return true;
} else {
ALOGV("stream at maximum refcount %d", s->ref_count);
return false;
}
}
const struct hw_stream *get_stream(struct config_mgr *cm,
const audio_devices_t devices,
const audio_output_flags_t flags,
const struct audio_config *config )
{
int i;
struct stream *s = cm->stream_array.streams;
const bool pcm = audio_is_linear_pcm(config->format);
enum stream_type type;
ALOGV("+get_stream devices=0x%x flags=0x%x format=0x%x",
devices, flags, config->format );
if (devices & AUDIO_DEVICE_BIT_IN) {
type = pcm ? e_stream_in_pcm : e_stream_in_compress;
} else {
type = pcm ? e_stream_out_pcm : e_stream_out_compress;
}
pthread_mutex_lock(&cm->lock);
for (i = cm->stream_array.count - 1; i >= 0; --i) {
ALOGV("get_stream: require type=%d; try type=%d refcount=%d refmax=%d",
type, s[i].info.type, s[i].ref_count, s[i].max_ref_count );
if (s[i].info.type == type) {
if (open_stream_l(cm, &s[i])) {
break;
}
}
}
pthread_mutex_unlock(&cm->lock);
if (i >= 0) {
ALOGV("-get_stream =%p (refcount=%d)", &s[i].info,
s[i].ref_count );
return &s[i].info;
} else {
ALOGE("-get_stream no suitable stream" );
return NULL;
}
}
const struct hw_stream *get_named_stream(struct config_mgr *cm,
const char *name)
{
int i;
struct stream *s;
ALOGV("+get_named_stream '%s'", name);
/* Streams can't be deleted so don't need to hold the lock during search */
s = find_named_stream(cm, name);
pthread_mutex_lock(&cm->lock);
if (s != NULL) {
if (!open_stream_l(cm, s)) {
s = NULL;
}
}
pthread_mutex_unlock(&cm->lock);
if (s != NULL) {
ALOGV("-get_named_stream =%p (refcount=%d)", &s->info, s->ref_count );
return &s->info;
} else {
ALOGE("-get_named_stream no suitable stream" );
return NULL;
}
}
bool is_named_stream_defined(struct config_mgr *cm, const char *name)
{
struct stream *s;
/* Streams can't be deleted so don't need to hold the lock during search */
s = find_named_stream(cm, name);
ALOGV("is_named_stream_defined '%s' = %d", name, (s != NULL));
return (s != NULL);
}
void release_stream( const struct hw_stream* stream )
{
struct stream *s = (struct stream *)stream;
ALOGV("release_stream %p", stream );
if (s) {
pthread_mutex_lock(&s->cm->lock);
if (--s->ref_count == 0) {
/* Ensure all paths it was using are disabled */
apply_paths_to_devices_l(s->cm, s->current_devices,
e_path_id_off, s->disable_path);
apply_paths_to_global_l(s->cm, s->disable_path, e_path_id_off);
s->current_devices = 0;
}
pthread_mutex_unlock(&s->cm->lock);
}
}
uint32_t get_supported_output_devices( struct config_mgr *cm )
{
const uint32_t d = cm->supported_output_devices;
ALOGV("get_supported_output_devices=0x%x", d);
return d;
}
uint32_t get_supported_input_devices( struct config_mgr *cm )
{
const uint32_t d = cm->supported_input_devices;
ALOGV("get_supported_input_devices=0x%x", d);
return d;
}
/*********************************************************************
* Use-case control
*********************************************************************/
int apply_use_case( const struct hw_stream* stream,
const char *setting,
const char *case_name)
{
struct stream *s = (struct stream *)stream;
struct usecase *puc = s->usecase_array.usecases;
int usecase_count = s->usecase_array.count;
struct scase *pcase;
int case_count;
int ret;
ALOGV("apply_use_case(%p) %s=%s", stream, setting, case_name);
for (; usecase_count > 0; usecase_count--, puc++) {
if (0 == strcmp(puc->name, setting)) {
pcase = puc->case_array.cases;
case_count = puc->case_array.count;
for(; case_count > 0; case_count--, pcase++) {
if (0 == strcmp(pcase->name, case_name)) {
pthread_mutex_lock(&s->cm->lock);
apply_ctls_l(pcase->ctl_array.ctls, pcase->ctl_array.count);
pthread_mutex_unlock(&s->cm->lock);
ret = 0;
goto exit;
}
}
}
}
ret = -ENOSYS; /* use-case not implemented */
exit:
return ret;
}
/*********************************************************************
* Config file parsing
*
* To keep this simple we restrict the order that config file entries
* may appear:
* - The <mixer> section must always appear first
* - Paths must be defined before they can be referred to
*********************************************************************/
static int parse_mixer_start(struct parse_state *state);
static int parse_device_start(struct parse_state *state);
static int parse_device_end(struct parse_state *state);
static int parse_stream_start(struct parse_state *state);
static int parse_stream_end(struct parse_state *state);
static int parse_stream_ctl_start(struct parse_state *state);
static int parse_path_start(struct parse_state *state);
static int parse_path_end(struct parse_state *state);
static int parse_case_start(struct parse_state *state);
static int parse_case_end(struct parse_state *state);
static int parse_usecase_start(struct parse_state *state);
static int parse_usecase_end(struct parse_state *state);
static int parse_enable_start(struct parse_state *state);
static int parse_disable_start(struct parse_state *state);
static int parse_ctl_start(struct parse_state *state);
static int parse_init_start(struct parse_state *state);
static const struct parse_element elem_table[e_elem_count] = {
[e_elem_ctl] = {
.name = "ctl",
.valid_attribs = BIT(e_attrib_name) | BIT(e_attrib_val) | BIT(e_attrib_index),
.required_attribs = BIT(e_attrib_name) | BIT(e_attrib_val),
.valid_subelem = 0,
.start_fn = parse_ctl_start,
.end_fn = NULL
},
[e_elem_path] = {
.name = "path",
.valid_attribs = BIT(e_attrib_name),
.required_attribs = BIT(e_attrib_name),
.valid_subelem = BIT(e_elem_ctl),
.start_fn = parse_path_start,
.end_fn = parse_path_end
},
[e_elem_device] = {
.name = "device",
.valid_attribs = BIT(e_attrib_name),
.required_attribs = BIT(e_attrib_name),
.valid_subelem = BIT(e_elem_path),
.start_fn = parse_device_start,
.end_fn = parse_device_end
},
[e_elem_stream] = {
.name = "stream",
.valid_attribs = BIT(e_attrib_name) | BIT(e_attrib_type)
| BIT(e_attrib_dir) | BIT(e_attrib_card)
| BIT(e_attrib_device) | BIT(e_attrib_instances)
| BIT(e_attrib_rate) | BIT(e_attrib_period_size)
| BIT(e_attrib_period_count),
.required_attribs = BIT(e_attrib_type),
.valid_subelem = BIT(e_elem_stream_ctl)
| BIT(e_elem_enable) | BIT(e_elem_disable)
| BIT(e_elem_usecase),
.start_fn = parse_stream_start,
.end_fn = parse_stream_end
},
[e_elem_enable] = {
.name = "enable",
.valid_attribs = BIT(e_attrib_path),
.required_attribs = BIT(e_attrib_path),
.valid_subelem = 0,
.start_fn = parse_enable_start,
.end_fn = NULL
},
[e_elem_disable] = {
.name = "disable",
.valid_attribs = BIT(e_attrib_path),
.required_attribs = BIT(e_attrib_path),
.valid_subelem = 0,
.start_fn = parse_disable_start,
.end_fn = NULL
},
[e_elem_case] = {
.name = "case",
.valid_attribs = BIT(e_attrib_name),
.required_attribs = BIT(e_attrib_name),
.valid_subelem = BIT(e_elem_ctl),
.start_fn = parse_case_start,
.end_fn = parse_case_end
},
[e_elem_usecase] = {
.name = "usecase",
.valid_attribs = BIT(e_attrib_name),
.required_attribs = BIT(e_attrib_name),
.valid_subelem = BIT(e_elem_case),
.start_fn = parse_usecase_start,
.end_fn = parse_usecase_end
},
[e_elem_stream_ctl] = {
.name = "ctl",
.valid_attribs = BIT(e_attrib_name) | BIT(e_attrib_function)
| BIT(e_attrib_index)
| BIT(e_attrib_min) | BIT(e_attrib_max),
.required_attribs = BIT(e_attrib_name) | BIT(e_attrib_function),
.valid_subelem = 0,
.start_fn = parse_stream_ctl_start,
.end_fn = NULL
},
[e_elem_init] = {
.name = "init",
.valid_attribs = 0,
.required_attribs = 0,
.valid_subelem = BIT(e_elem_ctl),
.start_fn = parse_init_start,
.end_fn = NULL
},
[e_elem_mixer] = {
.name = "mixer",
.valid_attribs = BIT(e_attrib_card),
.required_attribs = 0,
.valid_subelem = BIT(e_elem_init),
.start_fn = parse_mixer_start,
.end_fn = NULL
},
[e_elem_audiohal] = {
.name = "audiohal",
.valid_attribs = 0,
.required_attribs = 0,
.valid_subelem = BIT(e_elem_mixer),
.start_fn = NULL,
.end_fn = NULL
}
};
static const struct parse_attrib attrib_table[e_attrib_count] = {
[e_attrib_name] = {"name"},
[e_attrib_val] = {"val"},
[e_attrib_path] = {"path"},
[e_attrib_function] = {"function"},
[e_attrib_type] = {"type"},
[e_attrib_index] = {"index"},
[e_attrib_dir] = {"dir"},
[e_attrib_card] = {"card"},
[e_attrib_device] = {"device"},
[e_attrib_instances] = {"instances"},
[e_attrib_rate] = {"rate"},
[e_attrib_period_size] = {"period_size"},
[e_attrib_period_count] = {"period_count"},
[e_attrib_min] = {"min"},
[e_attrib_max] = {"max"}
};
static const struct parse_device device_table[] = {
{"global", 0}, /* special dummy device for global settings */
{"speaker", AUDIO_DEVICE_OUT_SPEAKER},
{"earpiece", AUDIO_DEVICE_OUT_EARPIECE},
{"headset", AUDIO_DEVICE_OUT_WIRED_HEADSET},
{"headset_in", AUDIO_DEVICE_IN_WIRED_HEADSET},
{"headphone", AUDIO_DEVICE_OUT_WIRED_HEADPHONE},
{"sco", AUDIO_DEVICE_OUT_ALL_SCO},
{"sco_in", AUDIO_DEVICE_IN_ALL_SCO},
{"a2dp", AUDIO_DEVICE_OUT_ALL_A2DP},
{"usb", AUDIO_DEVICE_OUT_ALL_USB},
{"mic", AUDIO_DEVICE_IN_BUILTIN_MIC},
{"back mic", AUDIO_DEVICE_IN_BACK_MIC},
{"voice", AUDIO_DEVICE_IN_VOICE_CALL},
{"aux", AUDIO_DEVICE_IN_AUX_DIGITAL}
};
static const char *predefined_path_name_table[] = {
[e_path_id_off] = "off",
[e_path_id_on] = "on"
};
static int dyn_array_extend(struct dyn_array *array)
{
const uint elem_size = array->elem_size;
const uint new_count = array->count + 1;
uint max_count = array->max_count;
uint old_size, new_size;
void *p;
uint8_t *pbyte;
if (new_count > max_count) {
if (max_count > 0xFFFF - DYN_ARRAY_GRANULE) {
return -ENOMEM;
}
old_size = max_count * elem_size;
max_count += DYN_ARRAY_GRANULE;
new_size = max_count * elem_size;
p = realloc(array->data, new_size);
if (!p) {
return -ENOMEM;
}
pbyte = p;
memset(pbyte + old_size, 0, new_size - old_size);
array->data = p;
array->max_count = max_count;
}
array->count = new_count;
return 0;
}
static void dyn_array_fix(struct dyn_array *array)
{
/* Fixes the allocated memory to exactly the required length
* This will always be a shrink, discarding granular allocations
* that we don't need */
const uint size = array->count * array->elem_size;
void *p = realloc(array->data, size);
if (p)
{
array->data = p;
array->max_count = array->count;
}
}
static void dyn_array_free(struct dyn_array *array)
{
free(array->data);
}