Fix SVACE defects
[platform/adaptation/spreadtrum/audio-hal-sc7727.git] / tizen-audio-impl-ucm.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #ifdef ALSA_UCM_DEBUG_TIME
28 #include <sys/time.h>
29 #include <time.h>
30 #endif
31
32 #include "tizen-audio-internal.h"
33
34 #ifdef ALSA_UCM_DEBUG_TIME
35 #define SND_USE_CASE_SET __set_use_case_with_time
36 #else
37 #define SND_USE_CASE_SET snd_use_case_set
38 #endif
39
40 #define UCM_PREFIX_CURRENT   ">>> UCM current"
41 #define UCM_PREFIX_REQUESTED "> UCM requested"
42 #define UCM_PREFIX_CHANGED   "<<< UCM changed"
43
44 #define DUMP_LEN 512
45
46 static void __dump_use_case(const char* prefix, const char *verb, const char *devices[], int dev_count, const char *modifiers[], int mod_count)
47 {
48     int i;
49     dump_data_t* dump = NULL;
50
51     if (!(dump = _audio_dump_new(DUMP_LEN))) {
52         AUDIO_LOG_ERROR("Failed to create dump string...");
53         return;
54     }
55
56     /* Verb */
57     _audio_dump_add_str(dump, "Verb [ %s ] Devices [ ", verb ? verb : AUDIO_USE_CASE_VERB_INACTIVE);
58
59     /* Devices */
60     if (devices) {
61         for (i = 0; i < dev_count; i++) {
62             _audio_dump_add_str(dump, (i != dev_count - 1) ? "%s, " : "%s", devices[i]);
63         }
64     }
65     _audio_dump_add_str(dump, " ] Modifier [ ");
66
67     /* Modifiers */
68     if (modifiers) {
69         for (i = 0; i < mod_count; i++) {
70             _audio_dump_add_str(dump, (i != mod_count - 1) ? "%s, " : "%s", modifiers[i]);
71         }
72     }
73     _audio_dump_add_str(dump, " ]");
74
75     AUDIO_LOG_INFO("TEST %s : %s", prefix, _audio_dump_get_str(dump));
76
77     _audio_dump_free(dump);
78 }
79
80 #ifdef ALSA_UCM_DEBUG_TIME
81 static inline int __set_use_case_with_time(snd_use_case_mgr_t *uc_mgr, const char *identifier, const char *value)
82 {
83     int ret = 0;
84     struct timeval t_start, t_stop;
85     unsigned long long t_diff = 0;
86
87     gettimeofday(&t_start, NULL);
88     ret = snd_use_case_set(uc_mgr, identifier, value);
89     gettimeofday(&t_stop, NULL);
90     if (t_start.tv_sec < t_stop.tv_sec)
91         t_diff = (t_stop.tv_sec - t_start.tv_sec) * 1000000;
92     t_diff += (t_stop.tv_usec - t_start.tv_usec);
93     AUDIO_LOG_DEBUG("identifier %s value %s takes %lluusec", identifier, value, t_diff);
94
95     return ret;
96 }
97 #endif
98
99 audio_return_t _ucm_init(audio_hal_t *ah)
100 {
101     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
102
103     snd_use_case_mgr_open(&ah->ucm.uc_mgr, ALSA_DEFAULT_CARD);
104
105     if (!ah->ucm.uc_mgr) {
106         AUDIO_LOG_ERROR("uc_mgr open failed");
107         return AUDIO_ERR_RESOURCE;
108     }
109     return AUDIO_RET_OK;
110 }
111
112 audio_return_t _ucm_deinit(audio_hal_t *ah)
113 {
114     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
115     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
116
117     if (ah->ucm.uc_mgr) {
118         snd_use_case_mgr_close(ah->ucm.uc_mgr);
119         ah->ucm.uc_mgr = NULL;
120     }
121
122     return AUDIO_RET_OK;
123 }
124
125 audio_return_t _ucm_get_device_name(audio_hal_t *ah, const char *use_case, audio_direction_t direction, const char **value)
126 {
127     char identifier[70] = { 0, };
128
129     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
130     AUDIO_RETURN_VAL_IF_FAIL(use_case, AUDIO_ERR_PARAMETER);
131     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
132
133     snprintf(identifier, sizeof(identifier), "%sPCM//%s",
134              (direction == AUDIO_DIRECTION_IN) ? "Capture" : "Playback", use_case);
135
136     snd_use_case_get(ah->ucm.uc_mgr, identifier, value);
137
138     AUDIO_LOG_INFO("get device name : [%s]", *value);
139
140     return AUDIO_RET_OK;
141 }
142
143 /* UCM sequence
144     1) If verb is null or verb is not changed
145     1-1) If device is changed
146          (If there is request for same device, it will be ignored)
147          -> Set "Inactive" verb, disable modifiers & devices, set current verb again, enable devices & modifiers
148             (playback/capture device will be enabled again if there is no request for playback/capture device)
149     1-2) If device is not changed
150      1-2-1) If modifier is changed
151             (If there is request for same modifier, it will be ignored)
152             -> Disable modifiers, enable modifiers
153    2) If verb is changed
154       -> Reset, set new verb, enable devices & modifiers
155  */
156 audio_return_t _ucm_set_use_case(audio_hal_t *ah, const char *verb, const char *devices[], const char *modifiers[])
157 {
158     audio_return_t audio_ret = AUDIO_RET_OK;
159     int is_verb_changed = 0, is_dev_changed = 0, is_mod_changed = 0;
160     const char *old_verb = NULL, **old_dev_list = NULL, **old_mod_list = NULL;
161     int old_dev_count = 0, dev_count = 0;
162     int old_mod_count = 0, mod_count = 0;
163     const char **dis_dev_list = NULL, **ena_dev_list = NULL;
164     const char **dis_mod_list = NULL, **ena_mod_list = NULL;
165     int dis_dev_count = 0, ena_dev_count = 0;
166     int dis_mod_count = 0, ena_mod_count = 0;
167     int i = 0, j = 0;
168
169     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
170     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
171     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
172
173     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
174     old_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &old_dev_list);
175     old_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &old_mod_list);
176     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, old_dev_list, old_dev_count, old_mod_list, old_mod_count);
177
178     if (devices) {
179         for (dev_count = 0; devices[dev_count]; dev_count++);
180     }
181     if (modifiers) {
182         for (mod_count = 0; modifiers[mod_count]; mod_count++);
183     }
184
185     __dump_use_case(UCM_PREFIX_REQUESTED, verb, devices, dev_count, modifiers, mod_count);
186
187     if (old_verb && streq(verb, old_verb)) {
188         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
189
190         if (old_dev_count > 0) {
191             dis_dev_list = (const char **)malloc(sizeof(const char *) * old_dev_count);
192             for (i = 0; i < old_dev_count; i++) {
193                 dis_dev_list[i] = NULL;
194             }
195         }
196         if (dev_count > 0) {
197             ena_dev_list = (const char **)malloc(sizeof(const char *) * dev_count);
198             for (i = 0; i < dev_count; i++) {
199                 ena_dev_list[i] = NULL;
200             }
201         }
202         if (old_mod_count > 0) {
203             dis_mod_list = (const char **)malloc(sizeof(const char *) * old_mod_count);
204             for (i = 0; i < old_mod_count; i++) {
205                 dis_mod_list[i] = NULL;
206             }
207         }
208         if (mod_count > 0) {
209             ena_mod_list = (const char **)malloc(sizeof(const char *) * mod_count);
210             for (i = 0; i < mod_count; i++) {
211                 ena_mod_list[i] = NULL;
212             }
213         }
214
215         /* update disable modifiers list which are not present in new modifier list */
216         for (i = 0; i < old_mod_count; i++) {
217             int need_disable_mod = 1;
218
219             for (j = 0; j < mod_count; j++) {
220                 if (streq(old_mod_list[i], modifiers[j])) {
221                     need_disable_mod = 0;
222                     break;
223                 }
224             }
225             if (need_disable_mod) {
226                 if (is_mod_changed == 0)
227                     is_mod_changed = 1;
228                 dis_mod_list[dis_mod_count++] = old_mod_list[i];
229             }
230         }
231
232         /* update disable devices list which are not present in new device list */
233         for (i = 0; i < old_dev_count; i++) {
234             int need_disable_dev = 1;
235
236             for (j = 0; j < dev_count; j++) {
237                 if (streq(old_dev_list[i], devices[j])) {
238                     need_disable_dev = 0;
239                     break;
240                 }
241             }
242             if (need_disable_dev) {
243                 if (is_dev_changed == 0)
244                     is_dev_changed = 1;
245                 dis_dev_list[dis_dev_count++] = old_dev_list[i];
246             }
247         }
248
249         /* update enable devices list which are not present in old device list */
250         for (i = 0; i < dev_count; i++) {
251             int need_enable_dev = 1;
252
253             for (j = 0; j < old_dev_count; j++) {
254                 if (streq(devices[i], old_dev_list[j])) {
255                     need_enable_dev = 0;
256                     break;
257                 }
258             }
259             if (need_enable_dev) {
260                 if (is_dev_changed == 0)
261                     is_dev_changed = 1;
262                 ena_dev_list[ena_dev_count++] = devices[i];
263             }
264         }
265
266         /* update enable modifiers list which are not present in old modifier list */
267         for (i = 0; i < mod_count; i++) {
268             int need_enable_mod = 1;
269
270             for (j = 0; j < old_mod_count; j++) {
271                 if (streq(modifiers[i], old_mod_list[j])) {
272                     need_enable_mod = 0;
273                     break;
274                 }
275             }
276             if (need_enable_mod) {
277                 if (is_mod_changed == 0)
278                     is_mod_changed = 1;
279                 ena_mod_list[ena_mod_count++] = modifiers[i];
280             }
281         }
282
283         /* disable modifiers */
284         for (i = 0; i < dis_mod_count; i++) {
285             AUDIO_LOG_INFO("Disable modifier : %s", dis_mod_list[i]);
286             if (snd_use_case_set(ah->ucm.uc_mgr, "_dismod", dis_mod_list[i]) < 0)
287                 AUDIO_LOG_ERROR("disable %s modifier failed", dis_mod_list[i]);
288         }
289
290         /* disable devices */
291         for (i = 0; i < dis_dev_count; i++) {
292             AUDIO_LOG_INFO("Disable device : %s", dis_dev_list[i]);
293             if (snd_use_case_set(ah->ucm.uc_mgr, "_disdev", dis_dev_list[i]) < 0)
294                 AUDIO_LOG_ERROR("disable %s device failed", dis_dev_list[i]);
295         }
296
297         /* enable devices */
298         for (i = 0; i < ena_dev_count; i++) {
299             AUDIO_LOG_INFO("Enable device : %s", ena_dev_list[i]);
300             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", ena_dev_list[i]) < 0)
301                 AUDIO_LOG_ERROR("enable %s device failed", ena_dev_list[i]);
302         }
303
304         /* enable modifiers */
305         for (i = 0; i < ena_mod_count; i++) {
306             AUDIO_LOG_INFO("Enable modifier : %s", ena_mod_list[i]);
307             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", ena_mod_list[i]) < 0)
308                 AUDIO_LOG_ERROR("enable %s modifier failed", ena_mod_list[i]);
309         }
310     } else {
311         is_verb_changed = 1;
312
313         AUDIO_LOG_DEBUG("Setting new verb: %s", verb);
314         /* set new verb */
315         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
316             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
317             audio_ret = AUDIO_ERR_UNDEFINED;
318             goto exit;
319         }
320         /* enable devices */
321         for (i = 0; i < dev_count; i++) {
322             AUDIO_LOG_DEBUG("Enable device : %s", devices[i]);
323             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", devices[i]) < 0)
324                 AUDIO_LOG_ERROR("Enable %s device failed", devices[i]);
325         }
326         /* enable modifiers */
327         for (i = 0; i < mod_count; i++) {
328             AUDIO_LOG_DEBUG("Enable modifier : %s", modifiers[i]);
329             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", modifiers[i]) < 0)
330                 AUDIO_LOG_ERROR("Enable %s modifier failed", modifiers[i]);
331         }
332     }
333
334 exit:
335     if (old_verb)
336         free((void *)old_verb);
337     if (old_dev_list)
338         snd_use_case_free_list(old_dev_list, old_dev_count);
339     if (old_mod_list)
340         snd_use_case_free_list(old_mod_list, old_mod_count);
341     if (dis_dev_list)
342         free((void *)dis_dev_list);
343     if (ena_dev_list)
344         free((void *)ena_dev_list);
345     if (dis_mod_list)
346         free((void *)dis_mod_list);
347     if (ena_mod_list)
348         free((void *)ena_mod_list);
349
350     if (is_verb_changed == 1 || is_dev_changed == 1 || is_mod_changed == 1) {
351         const char *new_verb = NULL, **new_dev_list = NULL, **new_mod_list = NULL;
352         int new_dev_count = 0, new_mod_count = 0;
353
354         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
355         new_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &new_dev_list);
356         new_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &new_mod_list);
357         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, new_dev_list, new_dev_count, new_mod_list, new_mod_count);
358
359         if (new_verb)
360             free((void *)new_verb);
361         if (new_dev_list)
362             snd_use_case_free_list(new_dev_list, new_dev_count);
363         if (new_mod_list)
364             snd_use_case_free_list(new_mod_list, new_mod_count);
365     }
366
367     return audio_ret;
368 }
369
370 audio_return_t _ucm_set_devices(audio_hal_t *ah, const char *verb, const char *devices[])
371 {
372     audio_return_t audio_ret = AUDIO_RET_OK;
373     int is_verb_changed = 0, is_dev_changed = 0;
374     const char *old_verb = NULL, **old_dev_list = NULL;
375     int old_dev_count = 0, dev_count = 0;
376     const char **dis_dev_list = NULL, **ena_dev_list = NULL;
377     int dis_dev_count = 0, ena_dev_count = 0;
378     int i = 0, j = 0;
379
380     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
381     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
382     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
383     AUDIO_RETURN_VAL_IF_FAIL(devices, AUDIO_ERR_PARAMETER);
384
385     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
386     old_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &old_dev_list);
387     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, old_dev_list, old_dev_count, NULL, 0);
388
389     if (devices) {
390         for (dev_count = 0; devices[dev_count]; dev_count++);
391     }
392
393     __dump_use_case(UCM_PREFIX_REQUESTED, verb, devices, dev_count, NULL, 0);
394
395     if (old_verb && streq(verb, old_verb)) {
396         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
397
398         if (old_dev_count > 0) {
399             dis_dev_list = (const char **)malloc(sizeof(const char *) * old_dev_count);
400             for (i = 0; i < old_dev_count; i++) {
401                 dis_dev_list[i] = NULL;
402             }
403         }
404         if (dev_count > 0) {
405             ena_dev_list = (const char **)malloc(sizeof(const char *) * dev_count);
406             for (i = 0; i < dev_count; i++) {
407                 ena_dev_list[i] = NULL;
408             }
409         }
410
411         /* update disable devices list which are not present in new device list */
412         for (i = 0; i < old_dev_count; i++) {
413             int need_disable_dev = 1;
414
415             for (j = 0; j < dev_count; j++) {
416                 if (streq(old_dev_list[i], devices[j])) {
417                     need_disable_dev = 0;
418                     break;
419                 }
420             }
421             if (need_disable_dev) {
422                 if (is_dev_changed == 0)
423                     is_dev_changed = 1;
424                 dis_dev_list[dis_dev_count++] = old_dev_list[i];
425             }
426         }
427
428         /* update enable devices list which are not present in old device list */
429         for (i = 0; i < dev_count; i++) {
430             int need_enable_dev = 1;
431
432             for (j = 0; j < old_dev_count; j++) {
433                 if (streq(devices[i], old_dev_list[j])) {
434                     need_enable_dev = 0;
435                     break;
436                 }
437             }
438             if (need_enable_dev) {
439                 if (is_dev_changed == 0)
440                     is_dev_changed = 1;
441                 ena_dev_list[ena_dev_count++] = devices[i];
442             }
443         }
444
445         /* disable devices */
446         for (i = 0; i < dis_dev_count; i++) {
447             AUDIO_LOG_INFO("Disable device : %s", dis_dev_list[i]);
448             if (snd_use_case_set(ah->ucm.uc_mgr, "_disdev", dis_dev_list[i]) < 0)
449                 AUDIO_LOG_ERROR("disable %s device failed", dis_dev_list[i]);
450         }
451
452         /* enable devices */
453         for (i = 0; i < ena_dev_count; i++) {
454             AUDIO_LOG_INFO("Enable device : %s", ena_dev_list[i]);
455             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", ena_dev_list[i]) < 0)
456                 AUDIO_LOG_ERROR("enable %s device failed", ena_dev_list[i]);
457         }
458
459     } else {
460         is_verb_changed = 1;
461
462         AUDIO_LOG_INFO("Setting new verb: %s", verb);
463         /* set new verb */
464         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
465             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
466             audio_ret = AUDIO_ERR_UNDEFINED;
467             goto exit;
468         }
469         /* enable devices */
470         for (i = 0; i < dev_count; i++) {
471             AUDIO_LOG_INFO("Enable device : %s", devices[i]);
472             if (snd_use_case_set(ah->ucm.uc_mgr, "_enadev", devices[i]) < 0)
473                 AUDIO_LOG_ERROR("Enable %s device failed", devices[i]);
474         }
475     }
476
477 exit:
478     if (old_verb)
479         free((void *)old_verb);
480     if (old_dev_list)
481         snd_use_case_free_list(old_dev_list, old_dev_count);
482     if (dis_dev_list)
483         free((void *)dis_dev_list);
484     if (ena_dev_list)
485         free((void *)ena_dev_list);
486
487     if (is_verb_changed == 1 || is_dev_changed == 1) {
488         const char *new_verb = NULL, **new_dev_list = NULL;
489         int new_dev_count = 0;
490
491         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
492         new_dev_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enadevs", &new_dev_list);
493         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, new_dev_list, new_dev_count, NULL, 0);
494
495         if (new_verb)
496             free((void *)new_verb);
497         if (new_dev_list)
498             snd_use_case_free_list(new_dev_list, new_dev_count);
499     }
500
501     return audio_ret;
502
503 }
504
505 audio_return_t _ucm_set_modifiers(audio_hal_t *ah, const char *verb, const char *modifiers[])
506 {
507     audio_return_t audio_ret = AUDIO_RET_OK;
508     int is_verb_changed = 0, is_mod_changed = 0;
509     const char *old_verb = NULL, **old_mod_list = NULL;
510     int old_mod_count = 0, mod_count = 0;
511     const char **dis_mod_list = NULL, **ena_mod_list = NULL;
512     int dis_mod_count = 0, ena_mod_count = 0;
513     int i = 0, j = 0;
514
515     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
516     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
517     AUDIO_RETURN_VAL_IF_FAIL(verb, AUDIO_ERR_PARAMETER);
518     AUDIO_RETURN_VAL_IF_FAIL(modifiers, AUDIO_ERR_PARAMETER);
519
520     snd_use_case_get(ah->ucm.uc_mgr, "_verb", &old_verb);
521     old_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &old_mod_list);
522     __dump_use_case(UCM_PREFIX_CURRENT, old_verb, NULL, 0, old_mod_list, old_mod_count);
523
524     if (modifiers) {
525         for (mod_count = 0; modifiers[mod_count]; mod_count++);
526     }
527
528     __dump_use_case(UCM_PREFIX_REQUESTED, verb, NULL, 0, modifiers, mod_count);
529
530     if (old_verb && streq(verb, old_verb)) {
531         AUDIO_LOG_DEBUG("current verb and new verb is same. No need to change verb, disable devices explicitely");
532
533         if (old_mod_count > 0) {
534             dis_mod_list = (const char **)malloc(sizeof(const char *) * old_mod_count);
535             for (i = 0; i < old_mod_count; i++) {
536                 dis_mod_list[i] = NULL;
537             }
538         }
539         if (mod_count > 0) {
540             ena_mod_list = (const char **)malloc(sizeof(const char *) * mod_count);
541             for (i = 0; i < mod_count; i++) {
542                 ena_mod_list[i] = NULL;
543             }
544         }
545
546         /* update disable modifiers list which are not present in new modifier list */
547         for (i = 0; i < old_mod_count; i++) {
548             int need_disable_mod = 1;
549
550             for (j = 0; j < mod_count; j++) {
551                 if (streq(old_mod_list[i], modifiers[j])) {
552                     need_disable_mod = 0;
553                     break;
554                 }
555             }
556             if (need_disable_mod) {
557                 if (is_mod_changed == 0)
558                     is_mod_changed = 1;
559                 dis_mod_list[dis_mod_count++] = old_mod_list[i];
560             }
561         }
562
563         /* update enable modifiers list which are not present in old modifier list */
564         for (i = 0; i < mod_count; i++) {
565             int need_enable_mod = 1;
566
567             for (j = 0; j < old_mod_count; j++) {
568                 if (streq(modifiers[i], old_mod_list[j])) {
569                     need_enable_mod = 0;
570                     break;
571                 }
572             }
573             if (need_enable_mod) {
574                 if (is_mod_changed == 0)
575                     is_mod_changed = 1;
576                 ena_mod_list[ena_mod_count++] = modifiers[i];
577             }
578         }
579
580         /* disable modifiers */
581         for (i = 0; i < dis_mod_count; i++) {
582             AUDIO_LOG_INFO("Disable modifier : %s", dis_mod_list[i]);
583             if (snd_use_case_set(ah->ucm.uc_mgr, "_dismod", dis_mod_list[i]) < 0)
584                 AUDIO_LOG_ERROR("disable %s modifier failed", dis_mod_list[i]);
585         }
586
587         /* enable modifiers */
588         for (i = 0; i < ena_mod_count; i++) {
589             AUDIO_LOG_INFO("Enable modifier : %s", ena_mod_list[i]);
590             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", ena_mod_list[i]) < 0)
591                 AUDIO_LOG_ERROR("enable %s modifier failed", ena_mod_list[i]);
592         }
593     } else {
594         is_verb_changed = 1;
595
596         AUDIO_LOG_DEBUG("Setting new verb: %s", verb);
597         /* set new verb */
598         if (snd_use_case_set(ah->ucm.uc_mgr, "_verb", verb) < 0) {
599             AUDIO_LOG_ERROR("Setting verb %s failed", verb);
600             audio_ret = AUDIO_ERR_UNDEFINED;
601             goto exit;
602         }
603         /* enable modifiers */
604         for (i = 0; i < mod_count; i++) {
605             AUDIO_LOG_DEBUG("Enable modifier : %s", modifiers[i]);
606             if (snd_use_case_set(ah->ucm.uc_mgr, "_enamod", modifiers[i]) < 0)
607                 AUDIO_LOG_ERROR("Enable %s modifier failed", modifiers[i]);
608         }
609     }
610
611 exit:
612     if (old_verb)
613         free((void *)old_verb);
614     if (old_mod_list)
615         snd_use_case_free_list(old_mod_list, old_mod_count);
616     if (dis_mod_list)
617         free((void *)dis_mod_list);
618     if (ena_mod_list)
619         free((void *)ena_mod_list);
620
621     if (is_verb_changed == 1 || is_mod_changed == 1) {
622         const char *new_verb = NULL, **new_mod_list = NULL;
623         int new_mod_count = 0;
624
625         snd_use_case_get(ah->ucm.uc_mgr, "_verb", &new_verb);
626         new_mod_count = snd_use_case_get_list(ah->ucm.uc_mgr, "_enamods", &new_mod_list);
627         __dump_use_case(UCM_PREFIX_CHANGED, new_verb, NULL, 0, new_mod_list, new_mod_count);
628
629         if (new_verb)
630             free((void *)new_verb);
631         if (new_mod_list)
632             snd_use_case_free_list(new_mod_list, new_mod_count);
633     }
634
635     return audio_ret;
636 }
637
638 audio_return_t _ucm_get_verb(audio_hal_t *ah, const char **value)
639 {
640     audio_return_t ret = AUDIO_RET_OK;
641
642     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
643     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
644     AUDIO_RETURN_VAL_IF_FAIL(value, AUDIO_ERR_PARAMETER);
645
646     if ((ret = snd_use_case_get(ah->ucm.uc_mgr, "_verb", value)) < 0) {
647         AUDIO_LOG_ERROR("Getting current verb failed: Reason %d", ret);
648         ret = AUDIO_ERR_UNDEFINED;
649     }
650
651     return ret;
652 }
653
654 audio_return_t _ucm_reset_use_case(audio_hal_t *ah)
655 {
656     audio_return_t ret = AUDIO_RET_OK;
657
658     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
659     AUDIO_RETURN_VAL_IF_FAIL(ah->ucm.uc_mgr, AUDIO_ERR_PARAMETER);
660
661     AUDIO_LOG_INFO(">>> UCM reset Verb [ %s ]", AUDIO_USE_CASE_VERB_INACTIVE);
662
663     if ((ret = snd_use_case_set(ah->ucm.uc_mgr, "_verb", AUDIO_USE_CASE_VERB_INACTIVE)) < 0) {
664         AUDIO_LOG_ERROR("Reset use case failed: Reason %d", ret);
665         ret = AUDIO_ERR_UNDEFINED;
666     }
667
668     return ret;
669 }