-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound_trigger_hw.c
3410 lines (3026 loc) · 125 KB
/
sound_trigger_hw.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
/* sound_trigger_hw.c
*
* This file contains the API to load sound models with
* DSP and start/stop detection of associated key phrases.
*
* Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Not a Contribution.
*
* Copyright (C) 2011 The Android Open Source Project
*
* 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.
*
*Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
*/
#define LOG_TAG "sound_trigger_hw"
#define ATRACE_TAG (ATRACE_TAG_HAL)
/* #define LOG_NDEBUG 0 */
#define LOG_NDDEBUG 0
#include <errno.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#include <sys/ioctl.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <cutils/trace.h>
#include <system/thread_defs.h>
#include <hardware/sound_trigger.h>
#include <cutils/str_parms.h>
#include "st_common_defs.h"
#include "sound_trigger_platform.h"
#include "sound_trigger_hw.h"
#include "st_session.h"
#include "st_hw_common.h"
#include "st_hw_extn.h"
#include "st_hw_defs.h"
#define XSTR(x) STR(x)
#define STR(x) #x
/* count of sound trigger hal clients */
static unsigned int stdev_ref_cnt = 0;
static pthread_mutex_t stdev_init_lock;
static struct sound_trigger_device *stdev = NULL;
static struct sound_trigger_properties_extended_1_3 hw_properties_extended;
/* default properties which will later be updated based on platform configuration */
static struct sound_trigger_properties hw_properties = {
"QUALCOMM Technologies, Inc", // implementor
"Sound Trigger HAL", // description
1, // version
{ 0x68ab2d40, 0xe860, 0x11e3, 0x95ef, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid
1, // max_sound_models
1, // max_key_phrases
1, // max_users
RECOGNITION_MODE_VOICE_TRIGGER | RECOGNITION_MODE_GENERIC_TRIGGER, // recognition_modes
true, // capture_transition
0, // max_capture_ms
false, // concurrent_capture
false, //trigger_in_event
0 // power_consumption_mw
};
/* vendor uuid that the client passes for LPMA feature */
static const sound_trigger_uuid_t lpma_uuid =
{ 0x57CADDB1, 0xACDB, 0x4DCE, 0x8CB0, { 0x2E, 0x95, 0xA2, 0x31, 0x3A, 0xEE } };
static void update_available_phrase_info(st_session_t *p_ses,
struct sound_trigger_phrase_sound_model *phrase_sm,
bool add);
static int stdev_reconfig_backend_on_stop(st_session_t *stopped_ses);
static int stop_recognition_l(st_session_t *st_session)
{
int status = 0;
st_session_stop_lab(st_session);
status = st_session_stop(st_session);
pthread_mutex_lock(&st_session->lock);
st_session->callback = NULL;
if (st_session->rc_config) {
free(st_session->rc_config);
st_session->rc_config = NULL;
}
if (st_session->st_conf_levels) {
free(st_session->st_conf_levels);
st_session->st_conf_levels = NULL;
}
pthread_mutex_unlock(&st_session->lock);
stdev_reconfig_backend_on_stop(st_session);
return status;
}
void update_hw_mad_exec_mode(st_exec_mode_t mode, st_profile_type_t profile_type)
{
if (stdev->exec_mode != mode) {
platform_stdev_connect_mad(stdev->platform, mode, profile_type);
stdev->exec_mode = mode;
ALOGV("%s: new exec_mode 0x%x", __func__, mode);
}
}
unsigned int get_num_sessions()
{
struct listnode *ses_node;
st_session_t *p_ses;
unsigned int num_sessions = 0;
if ((list_head(&stdev->sound_model_list) == NULL) ||
(list_tail(&stdev->sound_model_list) == NULL)) {
ALOGE("%s: sound model list is yet to be initialized", __func__);
return num_sessions;
}
list_for_each(ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(ses_node, st_session_t, list_node);
num_sessions++;
}
return num_sessions;
}
unsigned int get_num_sessions_in_exec_mode(st_exec_mode_t mode)
{
struct listnode *ses_node;
st_session_t *p_ses;
unsigned int num_sessions = 0;
if ((list_head(&stdev->sound_model_list) == NULL) ||
(list_tail(&stdev->sound_model_list) == NULL)) {
ALOGE("%s: sound model list is yet to be initialized", __func__);
return num_sessions;
}
list_for_each(ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(ses_node, st_session_t, list_node);
if (p_ses->exec_mode == mode)
num_sessions++;
}
return num_sessions;
}
static bool check_phrases_users_available(struct st_vendor_info *v_info,
unsigned int num_phrases, unsigned int num_users,
st_exec_mode_t exec_mode, bool transit)
{
bool available = false;
if (!v_info) {
ALOGE("%s: NULL vendor info", __func__);
return false;
}
if (!transit) {
if (exec_mode == ST_EXEC_MODE_CPE) {
if ((num_phrases <= v_info->avail_cpe_phrases) &&
(num_users <= v_info->avail_cpe_users))
available = true;
}
if (exec_mode == ST_EXEC_MODE_ADSP) {
if ((num_phrases <= v_info->avail_ape_phrases) &&
(num_users <= v_info->avail_ape_users))
available = true;
}
} else {
if (exec_mode == ST_EXEC_MODE_CPE) {
if ((num_phrases <= v_info->avail_transit_cpe_phrases) &&
(num_users <= v_info->avail_transit_cpe_users))
available = true;
}
if (exec_mode == ST_EXEC_MODE_ADSP) {
if ((num_phrases <= v_info->avail_transit_ape_phrases) &&
(num_users <= v_info->avail_transit_ape_users))
available = true;
}
}
ALOGV("%s: exec mode %d, transit %d, available %d",
__func__, exec_mode, transit, available);
return available;
}
static int parse_exec_mode_config(char *value,
st_exec_mode_t *exec_mode,
sound_model_handle_t *sm_handle)
{
int ret = -EINVAL;
char *id, *test_r;
if (!value || !exec_mode || !sm_handle) {
ALOGE("%s: Invalid params passed", __func__);
return ret;
}
/*
* <exec_mode>,<sound_model_handle>
* sound model handle is optional. Valid set param values are:
* 1. "0" -> exec mode is set and sound model handle to default value 0
* 2. "1,2" -> exec mode and sound model handle are set to passed values
* sound model handle as 0 is treated as a global set parameter.
*/
id = strtok_r(value, ", ", &test_r);
if (!id) {
ALOGE("%s: incorrect exec mode passed", __func__);
return ret;
}
*exec_mode = atoi(id);
id = strtok_r(NULL, ", ", &test_r);
if (!id) {
ALOGD("%s: No session id passed, treat as global set param", __func__);
*sm_handle = 0;
} else {
*sm_handle = atoi(id);
}
ALOGD("%s: mode %d, sm handle %d", __func__, *exec_mode, *sm_handle);
return 0;
}
static bool is_any_session_buffering()
{
struct listnode *p_ses_node;
st_session_t *p_ses;
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
if (st_session_is_buffering(p_ses)) {
ALOGD("%s:[%d] session is buffering", __func__, p_ses->sm_handle);
stdev->is_buffering = true;
return true;
}
}
return false;
}
static bool is_any_session_ssr_state()
{
struct listnode *p_ses_node;
st_session_t *p_ses;
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
if (st_session_is_ssr_state(p_ses)) {
ALOGD("%s:[%d] session is in ssr state", __func__, p_ses->sm_handle);
return true;
}
}
return false;
}
static int check_and_transit_cpe_ses_to_ape(st_session_t *cur_ses)
{
st_session_t *p_ses = NULL;
struct listnode *node = NULL;
unsigned int num_sessions = 0;
struct st_vendor_info *ses_v_info = NULL;
struct listnode transit_sound_model_list;
int ret = -EINVAL;
/* Switch sessions from CPE to ADSP
* 1. If cur_ses is NULL, all existing sessions are switched to ADSP.
* 2. If cur_ses is valid, cur_ses is the session requested to transit.
* In such cases, switch sessions if they share same backend with
* cur_ses.
*/
ALOGD("%s: enter: current ses %p", __func__, cur_ses);
if (stdev->platform_lpi_enable != ST_PLATFORM_LPI_NONE) {
ret = 0;
goto exit;
}
if (0 == get_num_sessions_in_exec_mode(ST_EXEC_MODE_CPE)) {
ret = 0;
goto exit;
}
if (cur_ses && (cur_ses->exec_mode != ST_EXEC_MODE_CPE)) {
ALOGE("%s: Invalid mode set for current session %d",
__func__, cur_ses->exec_mode);
goto exit;
}
/* Initialize transit ape phrases/users for all CPE sessions */
list_for_each(node, &stdev->sound_model_list) {
p_ses = node_to_item(node, st_session_t, list_node);
if (p_ses->exec_mode == ST_EXEC_MODE_CPE) {
ses_v_info = p_ses->vendor_uuid_info;
ses_v_info->avail_transit_ape_phrases = ses_v_info->avail_ape_phrases;
ses_v_info->avail_transit_ape_users = ses_v_info->avail_ape_users;
}
}
list_init(&transit_sound_model_list);
/*
* check if the existing CPE sessions can be moved to ADSP
* Add sessions to be switched to transit list.
*/
list_for_each(node, &stdev->sound_model_list) {
p_ses = node_to_item(node, st_session_t, list_node);
if (p_ses->exec_mode == ST_EXEC_MODE_CPE) {
/* check for sessions to keep on WDSP mode if requested by APP */
if (ST_DET_LOW_POWER_MODE == p_ses->client_req_det_mode) {
ALOGV("%s:[%d] session is requested on WDSP mode, skip",
__func__, p_ses->sm_handle);
continue;
}
/* check for sessions that match backend with current session */
if (cur_ses && (cur_ses != p_ses) &&
!platform_stdev_check_backends_match(stdev->platform,
cur_ses->exec_mode,
cur_ses->hw_proxy_ses->hw_ses_current->st_device,
p_ses->exec_mode,
p_ses->hw_proxy_ses->hw_ses_current->st_device)) {
ALOGV("%s:[%d] session not sharing backend",
__func__, p_ses->sm_handle);
continue;
}
if (++num_sessions > stdev->max_ape_sessions) {
ALOGE("%s: ERROR. sessions exceed max supported", __func__);
goto exit;
}
ses_v_info = p_ses->vendor_uuid_info;
if (ses_v_info->exec_mode_cfg != EXEC_MODE_CFG_DYNAMIC) {
ALOGE("%s:[%d] ERROR. exec mode cfg %d not set to Dynamic",
__func__, p_ses->sm_handle, ses_v_info->exec_mode_cfg);
goto exit;
}
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE) {
if (check_phrases_users_available(ses_v_info, p_ses->num_phrases,
p_ses->num_users, ST_EXEC_MODE_ADSP, true)) {
ses_v_info->avail_transit_ape_phrases -= p_ses->num_phrases;
ses_v_info->avail_transit_ape_users -= p_ses->num_users;
} else {
ALOGE("%s: ERROR. phrases/users exceed max supported", __func__);
goto exit;
}
}
/* Add the session to transit sound models list */
list_add_tail(&transit_sound_model_list,
&p_ses->transit_list_node);
}
}
ALOGV("%s: transit list empty: %s", __func__,
list_empty(&transit_sound_model_list) ? "true" : "false");
/*
* During switching, below order is followed to ensure teardown
* of all sessions happens first and then are started in new exec mode.
* This helps avoid issues during multisession backend switching.
* 1. Teardown the sessions by calling set_exec_mode with NONE.
* 2. Bring up the sessions in ADSP mode.
*/
/* Move sessions present in transit sound model list to NONE */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE)
update_available_phrase_info(p_ses, p_ses->phrase_sm, true);
ALOGD("%s:[%d] switch session to NONE", __func__, p_ses->sm_handle);
ret = st_session_set_exec_mode(p_ses, ST_EXEC_MODE_NONE);
if (ret) {
if (stdev->ssr_offline_received) {
goto ssr_exit;
} else {
/* TODO: Handle error during transitions */
ALOGE("%s:[%d] ERROR. could not set NONE", __func__, p_ses->sm_handle);
goto exit;
}
}
}
/* Move sessions present in transit sound model list to ADSP */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
ALOGD("%s:[%d] switch session to ADSP mode", __func__, p_ses->sm_handle);
ret = st_session_set_exec_mode(p_ses, ST_EXEC_MODE_ADSP);
if (ret) {
if (stdev->ssr_offline_received) {
goto ssr_exit;
} else {
/* TODO: Handle error during transitions */
ALOGE("%s:[%d] ERROR. could not set ADSP mode", __func__, p_ses->sm_handle);
goto exit;
}
}
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE)
update_available_phrase_info(p_ses, p_ses->phrase_sm, false);
}
ssr_exit:
/* Set target exec mode to be used during ssr online handling */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
p_ses->ssr_transit_exec_mode = ST_EXEC_MODE_ADSP;
}
sthw_extn_lpma_notify_event(LPMA_EVENT_TRANSIT_CPE_TO_APE);
exit:
ALOGD("%s: exit: ret %d", __func__, ret);
return ret;
}
static int check_and_transit_ape_ses_to_cpe(st_session_t *cur_ses)
{
st_session_t *p_ses = NULL;
struct listnode *node;
unsigned int num_sessions = 0, max_sessions;
struct st_vendor_info *ses_v_info = NULL;
struct listnode transit_sound_model_list;
int ret = -EINVAL;
/* Switch sessions from ADSP to CPE
* 1. If cur_ses is NULL, all existing sessions are switched to CPE.
* 2. If cur_ses is valid, cur_ses is the session requested to transit.
* In such cases, switch sessions if they share same backend with
* cur_ses.
*/
ALOGD("%s: enter: current ses %p", __func__, cur_ses);
if (stdev->platform_lpi_enable != ST_PLATFORM_LPI_NONE) {
ret = 0;
goto exit;
}
if (0 == get_num_sessions_in_exec_mode(ST_EXEC_MODE_ADSP)) {
ret = 0;
goto exit;
}
if (cur_ses && (cur_ses->exec_mode != ST_EXEC_MODE_ADSP)) {
ALOGE("%s: Invalid mode set for current session %d",
__func__, cur_ses->exec_mode);
goto exit;
}
max_sessions = stdev->max_wdsp_sessions;
/* Initialize transit cpe phrases/users for all ADSP sessions */
list_for_each(node, &stdev->sound_model_list) {
p_ses = node_to_item(node, st_session_t, list_node);
if (p_ses->exec_mode == ST_EXEC_MODE_ADSP) {
ses_v_info = p_ses->vendor_uuid_info;
ses_v_info->avail_transit_cpe_phrases = ses_v_info->avail_cpe_phrases;
ses_v_info->avail_transit_cpe_users = ses_v_info->avail_cpe_users;
}
}
list_init(&transit_sound_model_list);
/*
* check if the existing ADSP sessions can be moved to CPE
* Add sessions to be switched to transit list.
*/
list_for_each(node, &stdev->sound_model_list) {
p_ses = node_to_item(node, st_session_t, list_node);
if (p_ses->exec_mode == ST_EXEC_MODE_ADSP) {
/* check for sessions to keep on ADSP mode if requested by APP */
if (ST_DET_HIGH_PERF_MODE == p_ses->client_req_det_mode) {
ALOGV("%s:[%d] session is requested on ADSP mode, skip",
__func__, p_ses->sm_handle);
continue;
}
/* check for sessions that match backend with current session */
if (cur_ses && (cur_ses != p_ses) &&
!platform_stdev_check_backends_match(stdev->platform,
cur_ses->exec_mode,
cur_ses->hw_proxy_ses->hw_ses_current->st_device,
p_ses->exec_mode,
p_ses->hw_proxy_ses->hw_ses_current->st_device)) {
ALOGV("%s:[%d] session not sharing backend",
__func__, p_ses->sm_handle);
continue;
}
if (++num_sessions > max_sessions) {
ALOGE("%s: ERROR. sessions exceed max supported", __func__);
goto exit;
}
ses_v_info = p_ses->vendor_uuid_info;
if (ses_v_info->exec_mode_cfg != EXEC_MODE_CFG_DYNAMIC) {
ALOGE("%s: [%d] ERROR. exec mode cfg %d not set to Dynamic",
__func__, p_ses->sm_handle, ses_v_info->exec_mode_cfg);
goto exit;
}
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE) {
if (check_phrases_users_available(ses_v_info, p_ses->num_phrases,
p_ses->num_users, ST_EXEC_MODE_CPE, true)) {
ses_v_info->avail_transit_cpe_phrases -= p_ses->num_phrases;
ses_v_info->avail_transit_cpe_users -= p_ses->num_users;
} else {
ALOGE("%s: ERROR. phrases/users exceed max supported", __func__);
goto exit;
}
}
/* Add the session to transit sound models list */
list_add_tail(&transit_sound_model_list,
&p_ses->transit_list_node);
}
}
ALOGV("%s: transit list empty: %s", __func__,
list_empty(&transit_sound_model_list) ? "true" : "false");
/*
* During switching, below order is followed to ensure teardown
* of all sessions happens first and then are started in new exec mode.
* This helps avoid issues during multisession backend switching.
* 1. Teardown the sessions by calling set_exec_mode with NONE.
* 2. Bring up the sessions in CPE mode.
*/
/* Move sessions present in transit sound model list to NONE */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE)
update_available_phrase_info(p_ses, p_ses->phrase_sm, true);
ALOGD("%s:[%d] switch session to NONE", __func__, p_ses->sm_handle);
ret = st_session_set_exec_mode(p_ses, ST_EXEC_MODE_NONE);
if (ret) {
if (stdev->ssr_offline_received) {
goto ssr_exit;
} else {
/* TODO: Handle error during transitions */
ALOGE("%s:[%d] ERROR. could not set NONE", __func__, p_ses->sm_handle);
goto exit;
}
}
}
/* Move sessions present in transit sound model list to CPE */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
ALOGD("%s:[%d] switch session to CPE mode", __func__, p_ses->sm_handle);
ret = st_session_set_exec_mode(p_ses, ST_EXEC_MODE_CPE);
if (ret) {
if (stdev->ssr_offline_received) {
goto ssr_exit;
} else {
/* TODO: Handle error during transitions */
ALOGE("%s:[%d] ERROR. could not set CPE mode", __func__, p_ses->sm_handle);
goto exit;
}
}
if (p_ses->sm_type == SOUND_MODEL_TYPE_KEYPHRASE)
update_available_phrase_info(p_ses, p_ses->phrase_sm, false);
}
ssr_exit:
/* Set target exec mode to be used during ssr online handling */
list_for_each(node, &transit_sound_model_list) {
p_ses = node_to_item(node, st_session_t, transit_list_node);
p_ses->ssr_transit_exec_mode = ST_EXEC_MODE_CPE;
}
sthw_extn_lpma_notify_event(LPMA_EVENT_TRANSIT_APE_TO_CPE);
exit:
ALOGD("%s: exit: ret %d", __func__, ret);
return ret;
}
static void notify_ssr_to_lpma(enum ssr_event_status event)
{
enum sthw_extn_lpma_event_type lpma_event;
switch (event) {
case CPE_STATUS_OFFLINE:
lpma_event = LPMA_EVENT_CPE_STATUS_OFFLINE;
break;
case CPE_STATUS_ONLINE:
lpma_event = LPMA_EVENT_CPE_STATUS_ONLINE;
break;
case SLPI_STATUS_OFFLINE:
lpma_event = LPMA_EVENT_SLPI_STATUS_OFFLINE;
break;
case SLPI_STATUS_ONLINE:
lpma_event = LPMA_EVENT_SLPI_STATUS_ONLINE;
break;
default:
return;
}
sthw_extn_lpma_notify_event(lpma_event);
}
static void handle_ssr_online(enum ssr_event_status event)
{
struct listnode *p_ses_node;
st_session_t *p_ses;
ALOGD("%s: Enter", __func__);
stdev->ssr_offline_received = false;
ATRACE_BEGIN("sthal: handle_ssr_online");
pthread_mutex_lock(&stdev->lock);
/* Events allowed: SND_CARD ONLINE or CPE ONLINE */
/*
* During SSR offline, all the custom topology info is wiped off from
* ADSP. Audio HAL can send these topologies during SSR online, but if
* Audio Hal handles SSR online event later than sthal handles SSR
* online, LSM session open fails with no matching topology found in
* ADSP. Fix it by sending common custom topologies, that includes LSM
* topologies, during SSR online.
*/
if (event == SND_CARD_STATUS_ONLINE) {
ALOGV("%s: sending common custom topology", __func__);
if (platform_stdev_send_common_topology(stdev->platform))
ALOGE("%s: sending common topology failed" ,__func__);
}
/* reload and reactive each previously active session */
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
st_session_ssr_online(p_ses, event);
}
if (sthw_extn_lpma_present())
notify_ssr_to_lpma(event);
pthread_cond_broadcast(&stdev->cond);
pthread_mutex_unlock(&stdev->lock);
ATRACE_END();
ALOGD("%s: Exit event %d", __func__, event);
}
static void handle_ssr_offline(enum ssr_event_status event)
{
struct listnode *p_ses_node;
st_session_t *p_ses;
ALOGD("%s: Enter", __func__);
stdev->ssr_offline_received = true;
ATRACE_BEGIN("sthal: handle_ssr_offline");
pthread_mutex_lock(&stdev->lock);
/* Events allowed: SND_CARD OFFLINE or CPE OFFLINE */
/* teardown each session */
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
st_session_ssr_offline(p_ses, event);
}
if (sthw_extn_lpma_present())
notify_ssr_to_lpma(event);
pthread_mutex_unlock(&stdev->lock);
ATRACE_END();
ALOGD("%s: Exit event %d", __func__, event);
}
static void check_sessions_transition(audio_event_type_t event_type)
{
int status;
if ((AUDIO_EVENT_PLAYBACK_STREAM_ACTIVE == event_type) ||
((AUDIO_EVENT_BATTERY_STATUS_CHANGED == event_type) && stdev->is_charging)) {
/* Transition to ADSP */
if (is_any_session_buffering() ||
is_any_session_ssr_state()) {
stdev->transit_dir = TRANSIT_CPE_TO_APE;
pthread_cond_signal(&stdev->transitions_cond);
} else {
status = check_and_transit_cpe_ses_to_ape(NULL);
if (status)
ALOGE("%s: Transition to ADSP failed, ignore", __func__);
}
} else if ((AUDIO_EVENT_PLAYBACK_STREAM_INACTIVE == event_type) ||
((AUDIO_EVENT_BATTERY_STATUS_CHANGED == event_type) && !stdev->is_charging)) {
stdev->transit_dir = TRANSIT_APE_TO_CPE;
pthread_cond_signal(&stdev->transitions_cond);
}
}
static void handle_audio_ec_ref_enabled(audio_event_info_t* config)
{
if (config == NULL) {
ALOGE("%s: null config event received!", __func__);
return;
}
ALOGD("%s: Enter", __func__);
pthread_mutex_lock(&stdev->lock);
stdev->audio_ec_enabled = config->u.audio_ec_ref_enabled;
pthread_mutex_unlock(&stdev->lock);
ALOGD("%s: Exit audio ec ref=%d", __func__, stdev->audio_ec_enabled);
}
static void handle_audio_concurrency(audio_event_type_t event_type,
audio_event_info_t* config)
{
struct listnode *p_ses_node = NULL, *node = NULL;
st_session_t *p_ses = NULL;
bool conc_allowed = false, lpi_changed = false, barge_in_mode = false;
unsigned int num_sessions = 0;
struct audio_device_info *item = NULL;
if (config == NULL) {
ALOGE("%s: Config is NULL, exiting", __func__);
return;
}
list_for_each (node, &config->device_info.devices) {
item = node_to_item(node, struct audio_device_info, list);
ALOGV("%s: Audio device = 0x%x", __func__, item->type);
}
/*
UC1:
1. start_recognition
2. audio record_active
3. audio_record_inactive
4. stop_recognition
UC1:
1. start_recognition
2. audio record_active
3. stop_recognition
4. audio_record_inactive
UC2:
1. audio_record_active
2. start_recognition
3. stop_recogntion
4. audio_record_inactive
UC3:
1. audio_record_active
2. start_recognition
3. audio_record_inactive
4. stop_recogntion
*/
pthread_mutex_lock(&stdev->lock);
num_sessions = get_num_sessions();
conc_allowed = platform_stdev_check_and_update_concurrency(stdev->platform,
event_type, config, num_sessions);
if (!conc_allowed)
sthw_extn_lpma_notify_event(LPMA_EVENT_AUDIO_CONCURRENCY);
if (!num_sessions) {
stdev->session_allowed = conc_allowed;
/*
* This is needed for the following usecase:
*
* 1. LPI and NLPI have different number of MICs (different devices).
* 2. ST session is stopped from app and unloaded while Tx active.
* 3. Tx stops.
* 4. ST session started again from app on LPI.
*
* The device disablement is missed in step 3 because the st_session was
* deinitialized. Thus, it is handled here.
*/
if (event_type == AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE &&
!platform_stdev_is_dedicated_sva_path(stdev->platform) &&
platform_stdev_backend_reset_allowed(stdev->platform))
if (stdev->tx_concurrency_active == 0)
platform_stdev_disable_stale_devices(stdev->platform);
pthread_mutex_unlock(&stdev->lock);
return;
}
if (stdev->transit_to_adsp_on_playback)
check_sessions_transition(event_type);
if (platform_stdev_is_dedicated_sva_path(stdev->platform)) {
/*
* When SVA has dedicated tx path, ignore capture events when concurrency
* is allowed with this capture event.
* But need to handle capture events to pause VA sessions when concurrency
* is not allowed but previously allowed, and also do resume when concurrency
* is allowed but previously not allowed.
*/
if (event_type == AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE ||
event_type == AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE ||
event_type == AUDIO_EVENT_CAPTURE_STREAM_INACTIVE ||
event_type == AUDIO_EVENT_CAPTURE_STREAM_ACTIVE) {
if (conc_allowed != stdev->session_allowed) {
stdev->session_allowed = conc_allowed;
if (!conc_allowed) {
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
st_session_pause(p_ses);
}
} else {
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
st_session_resume(p_ses);
}
}
} else {
ALOGV("%s: Ignore capture events as sva has dedicated path", __func__);
}
if (!conc_allowed) {
pthread_mutex_unlock(&stdev->lock);
return;
}
}
} else {
/*
* 1. When concurrency is NOT allowed, pause VA sessions.
* 2. When concurrency is allowed on:
* a. audio capture enabling - do nothing as the tx paths can be shared
* for both VA and audio capture.
* b. audio capture disabling - due to tx path was previously disabled
* by audio capture usecase, so need to do session pause->resume
* to resume tx path.
*/
if (!conc_allowed) {
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
st_session_pause(p_ses);
}
} else {
if (event_type == AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE) {
/*
* The reset_backend flag allows the backend device to be disabled. This should
* only be disallowed when in non-dedicated path mode and there is an active
* audio input stream.
*/
stdev->reset_backend = platform_stdev_backend_reset_allowed(stdev->platform);
st_hw_check_and_update_lpi(stdev, p_ses);
stdev->vad_enable = st_hw_check_vad_support(stdev, p_ses, stdev->lpi_enable);
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
ALOGD("%s:[%d] Capture device is disabled, pause SVA session",
__func__, p_ses->sm_handle);
st_session_pause(p_ses);
}
/*
* This is needed when the session goes to loaded state, then
* LPI/NLPI switch happens due to Rx event.
*/
platform_stdev_reset_backend_cfg(stdev->platform);
platform_stdev_disable_stale_devices(stdev->platform);
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
ALOGD("%s:[%d] Capture device is disabled, resume SVA session",
__func__, p_ses->sm_handle);
st_session_resume(p_ses);
}
} else if (event_type == AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE) {
ALOGD("Audio HAL has a capture session ON, don't disable the device");
}
}
stdev->session_allowed = conc_allowed;
if (!conc_allowed) {
pthread_mutex_unlock(&stdev->lock);
return;
}
}
/*
* lpi/vad decision might have changed based on the capture and playback
* usecases becoming active/inactive. So check and update the lpi/vad
* configuration which will be used during device backend setting as part
* of session's start/resume.
*/
barge_in_mode = stdev->barge_in_mode;
st_hw_check_and_update_lpi(stdev, p_ses);
lpi_changed = stdev->lpi_enable != platform_get_lpi_mode(stdev->platform);
stdev->vad_enable = st_hw_check_vad_support(stdev, p_ses, stdev->lpi_enable);
/*
* Usecase 1: Playback enabled without display on/battery charging:
* lpi_changed = true, so transition occurs.
* Usecase 2: Playback enabled with display on/battery charging:
* lpi_changed = false, and barge_in_mode changes from false to
* true. Dynamic EC update or transition will occur depending
* on the flag.
* Usecase 3: Playback disabled without display on/battery charging:
* lpi_changed = true, so transition occurs.
* Usecase 4: Playback disabled with display on/battery charging:
* lpi_changed = false, and barge_in_mode changes from true to
* false. Dynamic EC update or transition will occur depending
* on the flag.
*/
if ((lpi_changed || barge_in_mode != stdev->barge_in_mode) &&
!is_any_session_buffering()) {
if (!lpi_changed && stdev->support_dynamic_ec_update) {
platform_stdev_update_ec_effect(stdev->platform,
stdev->barge_in_mode);
} else {
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
if (p_ses && p_ses->exec_mode == ST_EXEC_MODE_ADSP) {
ALOGD("%s:[%d] LPI: pause SVA session",
__func__, p_ses->sm_handle);
st_session_pause(p_ses);
}
}
platform_stdev_reset_backend_cfg(stdev->platform);
list_for_each(p_ses_node, &stdev->sound_model_list) {
p_ses = node_to_item(p_ses_node, st_session_t, list_node);
if (p_ses && p_ses->exec_mode == ST_EXEC_MODE_ADSP) {
ALOGD("%s:[%d] LPI: resume SVA session",
__func__, p_ses->sm_handle);
st_session_resume(p_ses);
}
}
}
}
/*
* The device can be disabled within this thread upon reception of the device
* active event because audio hal does not enable the device until after returning
* from this callback. After this thread exits, device disablement will be
* disallowed until the device inactive event is received.
*/
if (event_type == AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE &&
!platform_stdev_is_dedicated_sva_path(stdev->platform))
stdev->reset_backend = platform_stdev_backend_reset_allowed(stdev->platform);
pthread_mutex_unlock(&stdev->lock);
ALOGV("%s: Exit", __func__);
}
static void *transitions_thread_loop(void * context __unused)
{
int status = 0;
int in_ssr = 0, ssr_retry = 0;
/*
* This thread is used for audio playback transitions from ADSP
* to WDSP. This is needed for hotword detections on ADSP because
* the app pauses music playback during buffering and resumes when
* buffering stops. An audio playback inactive event sends the
* signal to this thread to begin looping while playback is paused.
* When both buffering and and audio playback are inactive, it is
* assumed to be a stoppage of audio playback NOT due to a hotword
* detection.
* This also handles battery charging use case, when the battery
* is not in charging status, sessions need to be transited back
* to WDSP.
* These two cases will transition to WDSP.
*/
pthread_mutex_lock(&stdev->lock);
if (stdev->transit_wait_time <= 0)
stdev->transit_wait_time = TRANSIT_MIN_SLEEP_TIME_SEC;
while (!stdev->stop_transitions_thread_loop) {
/* Avoid spurious wake ups unncessarily acquiring wakelock later */
while ((stdev->transit_dir == TRANSIT_NONE) &&
!stdev->stop_transitions_thread_loop)
pthread_cond_wait(&stdev->transitions_cond, &stdev->lock);