-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_hw_session_lsm.c
3604 lines (3167 loc) · 131 KB
/
st_hw_session_lsm.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
/* st_hw_session_lsm.c
*
* This file implements the hw session functionality specific to LSM HW
*
* Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. 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.
*/
#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 <sound/asound.h>
#include <stdarg.h>
#include <unistd.h>
#include "st_common_defs.h"
#include "sound_trigger_platform.h"
#include "st_hw_session_lsm.h"
#include "sound_trigger_hw.h"
#include "st_hw_common.h"
#define LSM_LUT_CLEAR_INFO _IOW('U', 0xF0, int)
#define XSTR(x) STR(x)
#define STR(x) #x
#define MAX_DOA_TRACKING_ANGLES 2
#define DOA_POLAR_ACTIVITY_INDICATORS 360
static int ape_reg_sm(st_hw_session_t* p_ses, void *sm_data,
unsigned int sm_size, uint32_t model_id);
static int ape_reg_sm_params(st_hw_session_t* p_ses, unsigned int recognition_mode,
bool capture_requested, struct sound_trigger_recognition_config *rc_config);
static int ape_dereg_sm(st_hw_session_t* p_ses, uint32_t model_id);
static int ape_dereg_sm_params(st_hw_session_t* p_ses);
static int ape_start(st_hw_session_t* p_ses);
static int ape_stop(st_hw_session_t* p_ses);
static int ape_stop_buffering(st_hw_session_t* p_ses);
static int ape_open_session(st_hw_session_t* p_ses);
static void ape_close_session(st_hw_session_t* p_ses);
#ifdef SNDRV_LSM_GET_MODULE_PARAMS
static int ape_get_module_version(st_hw_session_t *p_ses, void *param_info_payload,
size_t size);
#endif
/* Routing layer functions */
static int route_reg_sm_ape(st_hw_session_t *p_ses,
void *sm_data, unsigned int sm_size, uint32_t model_id);
static int route_reg_sm_params_ape(st_hw_session_t* p_ses,
unsigned int recognition_mode, bool capture_requested,
struct sound_trigger_recognition_config *rc_config);
static int route_dereg_sm_ape(st_hw_session_t* p_ses, uint32_t model_id);
static int route_dereg_sm_params_ape(st_hw_session_t* p_ses);
static int route_restart_ape(st_hw_session_t* p_ses,
unsigned int recognition_mode,
struct sound_trigger_recognition_config *rc_config);
static int route_start_ape(st_hw_session_t* p_ses);
static int route_stop_ape(st_hw_session_t* p_ses);
static int route_stop_buffering_ape(st_hw_session_t* p_ses);
static int route_set_device_ape(st_hw_session_t* p_ses,
bool enable);
static int route_read_pcm_ape(st_hw_session_t *p_ses,
unsigned char *buf,
unsigned int bytes);
static void route_audio_capture_ape(st_hw_session_t* p_ses);
static int route_send_custom_chmix_coeff_ape(st_hw_session_t *p_ses, char *str);
static int route_disable_device(st_hw_session_t *p_ses, bool setting_device);
static int route_enable_device(st_hw_session_t *p_ses, bool setting_device);
static void request_exit_callback_thread(st_hw_session_lsm_t *p_lsm_ses);
static int get_param_data(st_hw_session_t* p_ses, const char *param,
void *payload, size_t payload_size, size_t *param_data_size);
static int send_detection_request(st_hw_session_t *p_ses);
typedef struct {
int16_t target_angle_L16[MAX_DOA_TRACKING_ANGLES];
int16_t interf_angle_L16[MAX_DOA_TRACKING_ANGLES];
int8_t polarActivityGUI[DOA_POLAR_ACTIVITY_INDICATORS];
} st_ffv_doa_tracking_monitor_t;
#ifdef ENABLE_SVA_MIXER_CTL
struct st_lsm_cdev_info
{
int fd;
int det_status;
};
#endif
static struct pcm_config stdev_ape_pcm_config = {
.channels = SOUND_TRIGGER_CHANNEL_MODE_MONO,
.rate = SOUND_TRIGGER_SAMPLING_RATE_16000,
.period_size = SOUND_TRIGGER_APE_PERIOD_SIZE,
.period_count = SOUND_TRIGGER_APE_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
};
struct st_session_fptrs ape_fptrs = {
.reg_sm = route_reg_sm_ape ,
.reg_sm_params = route_reg_sm_params_ape,
.dereg_sm = route_dereg_sm_ape ,
.dereg_sm_params = route_dereg_sm_params_ape,
.start = route_start_ape,
.restart = route_restart_ape,
.stop = route_stop_ape,
.stop_buffering = route_stop_buffering_ape,
.set_device = route_set_device_ape,
.read_pcm = route_read_pcm_ape,
.process_lab_capture = route_audio_capture_ape,
.send_custom_chmix_coeff = route_send_custom_chmix_coeff_ape,
.disable_device = route_disable_device,
.enable_device = route_enable_device,
.get_param_data = get_param_data,
.send_detection_request = send_detection_request,
.open_session = ape_open_session,
.close_session = ape_close_session,
#ifdef SNDRV_LSM_GET_MODULE_PARAMS
.get_module_version = ape_get_module_version,
#endif
};
int pcm_ioctl(struct pcm *pcm, int request, ...)
{
va_list ap;
void * arg;
int pcm_fd = *(int*)pcm;
va_start(ap, request);
arg = va_arg(ap, void *);
va_end(ap);
return ioctl(pcm_fd, request, arg);
}
#if (SNDRV_LSM_VERSION >= SNDRV_PROTOCOL_VERSION(0, 3, 1))
typedef struct lsm_params_info_v2 lsm_param_info_t;
typedef struct lsm_param_payload_v2 lsm_param_payload_t;
#ifdef ENABLE_SVA_MIXER_CTL
int lsm_set_session_data(struct mixer * st_mixer, void *ses_data)
{
struct mixer_ctl *ctl = NULL;
const char *mixer_ctl_name = "LSM SESSION_DATA SET";
ctl = mixer_get_ctl_by_name(st_mixer, mixer_ctl_name);
if (!ctl) {
ALOGE("%s: Could not get ctl for mixer cmd - %s",
__func__, mixer_ctl_name);
}
if (mixer_ctl_set_array(ctl, ses_data, sizeof(struct snd_lsm_session_data_v2)) < 0) {
ALOGE("%s: Could not set LSM load mixer control", __func__);
}
return 0;
}
#endif
static int lsm_set_session_data_v2(st_hw_session_t *p_ses)
{
st_hw_session_lsm_t *p_lsm_ses = (st_hw_session_lsm_t*)p_ses;
struct st_vendor_info *v_info = p_ses->vendor_uuid_info;
struct snd_lsm_session_data_v2 ses_data = {0};
uint16_t stage_idx = LSM_STAGE_INDEX_FIRST;
struct listnode *node;
st_lsm_ss_config_t *ss_cfg;
int status = 0;
ses_data.app_id = LSM_VOICE_WAKEUP_APP_ID_V2;
ses_data.num_stages = p_lsm_ses->num_stages;
ses_data.stage_info[stage_idx].app_type =
(v_info->app_type == 0) ?
p_lsm_ses->lsm_usecase.app_type : v_info->app_type;
ses_data.stage_info[stage_idx].lpi_enable = p_ses->lpi_enable;
ALOGD("%s: Sending lpi_enable = %s to LSM", __func__,
p_ses->lpi_enable ? "true" : "false");
list_for_each(node, &p_ses->lsm_ss_cfg_list) {
ss_cfg = node_to_item(node, st_lsm_ss_config_t, list_node);
stage_idx++;
ses_data.stage_info[stage_idx].app_type = ss_cfg->params->app_type;
ses_data.stage_info[stage_idx].lpi_enable = ss_cfg->params->lpi_enable;
ALOGD("%s: Sending lpi_enable = %s to LSM for stage_idx %d", __func__,
ss_cfg->params->lpi_enable ? "true" : "false", stage_idx);
}
ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_set_session_data_v2");
#ifdef ENABLE_SVA_MIXER_CTL
status = lsm_set_session_data(p_ses->stdev->mixer, (void *)(&ses_data));
#else
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_SESSION_DATA_V2, &ses_data);
#endif
ATRACE_END();
if (status)
ALOGE("%s: ERROR. SNDRV_LSM_SET_SESSION_DATA_V2 failed status(%d)",
__func__, status);
return status;
}
#ifdef SNDRV_LSM_GET_MODULE_PARAMS
static void lsm_fill_get_param_info
(
uint32_t param_type,
struct lsm_params_get_info *p_info,
struct st_module_param_info *mparams,
uint16_t stage_idx
)
{
p_info->param_type = param_type;
p_info->module_id = mparams->module_id;
p_info->instance_id = mparams->instance_id;
p_info->param_id = mparams->param_id;
p_info->stage_idx = stage_idx;
}
static int lsm_get_module_params
(
st_hw_session_lsm_t *p_lsm_ses,
struct lsm_params_get_info *lsm_params
)
{
int status = 0;
ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_get_module_params");
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_GET_MODULE_PARAMS, lsm_params);
ATRACE_END();
if (status)
ALOGE("%s: ERROR. SNDRV_LSM_GET_MODULE_PARAMS status(%d)",
__func__, status);
return status;
}
#endif
static void lsm_fill_param_info
(
uint32_t param_type,
lsm_param_info_t *p_info,
struct st_module_param_info *mparams,
uint16_t stage_idx
)
{
p_info->param_type = param_type;
p_info->module_id = mparams->module_id;
p_info->instance_id = mparams->instance_id;
p_info->param_id = mparams->param_id;
p_info->stage_idx = stage_idx;
}
#ifdef ENABLE_SVA_MIXER_CTL
static int lsm_mixer_set_module_params
(
st_hw_session_lsm_t *p_lsm_ses,
struct snd_lsm_module_params *lsm_params
)
{
const char *mixer_ctl_name = "LSM MODULE_PARAMS SET";
struct mixer_ctl *ctl = NULL;
st_hw_session_t p_ses = p_lsm_ses->common;
int ret = 0;
ctl = mixer_get_ctl_by_name(p_ses.stdev->mixer, mixer_ctl_name);
if (!ctl) {
ALOGE("%s: Could not get ctl for mixer cmd - %s",
__func__, mixer_ctl_name);
}
ret = mixer_ctl_set_array(ctl, lsm_params, sizeof(struct snd_lsm_module_params));
if (ret < 0) {
ALOGE("%s: Could not set LSM set module params, ret = %d", __func__, ret);
}
return ret;
}
#endif
static int lsm_set_module_params
(
st_hw_session_lsm_t *p_lsm_ses,
struct snd_lsm_module_params *lsm_params
)
{
int status = 0;
if(!p_lsm_ses->pcm) {
ALOGE("%s: PCM is NULL", __func__);
return -EINVAL;
}
ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_set_module_params_v2");
#ifdef ENABLE_SVA_MIXER_CTL
status = lsm_mixer_set_module_params(p_lsm_ses, lsm_params);
#else
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_MODULE_PARAMS_V2, lsm_params);
#endif
ATRACE_END();
if (status)
ALOGE("%s: ERROR. SNDRV_LSM_SET_MODULE_PARAMS_V2 count(%d), status(%d)",
__func__, lsm_params->num_params, status);
return status;
}
bool st_hw_check_multi_stage_lsm_support()
{
return true;
}
static void lsm_fill_param_header
(
lsm_param_payload_t *custom_conf,
uint32_t payload_size,
struct st_module_param_info *mparams
)
{
custom_conf->module_id = mparams->module_id;
custom_conf->instance_id = mparams->instance_id;
custom_conf->reserved = 0;
custom_conf->param_id = mparams->param_id;
custom_conf->p_size = payload_size;
}
#else
#define LSM_MAX_STAGES_PER_SESSION 1
#define LSM_STAGE_INDEX_FIRST 0
#define LSM_LAB_CONTROL -1
typedef struct lsm_params_info lsm_param_info_t;
typedef struct lsm_param_payload lsm_param_payload_t;
static int lsm_set_session_data_v2(st_hw_session_t *p_ses __unused)
{
ALOGE("%s: ERROR: unexpected call, check support from \"%s\" before using",
__func__, "st_hw_check_multi_stage_lsm_support");
return -ENOSYS;
}
static void lsm_fill_param_info
(
uint32_t param_type,
lsm_param_info_t *p_info,
struct st_module_param_info *mparams,
uint16_t stage_idx __unused
)
{
p_info->param_type = param_type;
p_info->module_id = mparams->module_id;
p_info->param_id = mparams->param_id;
}
static int lsm_set_module_params
(
st_hw_session_lsm_t *p_lsm_ses,
struct snd_lsm_module_params *lsm_params
)
{
int status = 0;
ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_set_module_params");
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_MODULE_PARAMS, lsm_params);
ATRACE_END();
if (status)
ALOGE("%s: ERROR. SNDRV_LSM_SET_MODULE_PARAMS count(%d), status(%d)",
__func__, lsm_params->num_params, status);
return status;
}
bool st_hw_check_multi_stage_lsm_support()
{
return false;
}
static void lsm_fill_param_header
(
lsm_param_payload_t *custom_conf,
uint32_t payload_size,
struct st_module_param_info *mparams
)
{
custom_conf->module_id = mparams->module_id;
custom_conf->param_id = mparams->param_id;
custom_conf->p_size = payload_size;
}
#endif
#ifdef LSM_EVENT_TIMESTAMP_MODE_SUPPORT
#ifdef ENABLE_SVA_MIXER_CTL
int lsm_set_fwk_mode_mixer_ctl(struct mixer * st_mixer, int buf_en)
{
int ret = 0;
struct mixer_ctl *ctl = NULL;
const char *mixer_ctl_name = "LSM FWK_MODE SET";
ctl = mixer_get_ctl_by_name(st_mixer, mixer_ctl_name);
if (!ctl) {
ALOGE("%s: Could not get ctl for mixer cmd - %s",
__func__, mixer_ctl_name);
}
ret = mixer_ctl_set_value(ctl, 0, buf_en);
if (ret < 0) {
ALOGE("%s: Could not set LSM load mixer control", __func__);
}
return ret;
}
#endif
static int set_lsm_fwk_mode(st_hw_session_lsm_t *p_lsm_ses)
{
int status;
struct st_vendor_info *v_info = p_lsm_ses->common.vendor_uuid_info;
unsigned int fwk_mode = LSM_EVENT_NON_TIME_STAMP_MODE;
st_hw_session_t p_ses;
if (v_info && (v_info->fwk_mode == SOUND_TRIGGER_EVENT_TIME_STAMP_MODE))
fwk_mode = LSM_EVENT_TIME_STAMP_MODE;
p_ses = p_lsm_ses->common;
#ifdef ENABLE_SVA_MIXER_CTL
status = lsm_set_fwk_mode_mixer_ctl(p_ses.stdev->mixer, fwk_mode);
#else
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_FWK_MODE_CONFIG,
&fwk_mode);
#endif
if (status)
ALOGE("%s: SNDRV_LSM_SET_FWK_MODE_CONFIG status=%d", __func__, status);
return status;
}
static void update_lsm_event_status_info(st_hw_session_lsm_t *p_lsm_ses,
int *request,
char **st_lsm_event_cmd)
{
#ifdef LSM_DET_EVENT_TYPE_GENERIC
if (p_lsm_ses->common.is_generic_event) {
*request = SNDRV_LSM_GENERIC_DET_EVENT;
*st_lsm_event_cmd = strdup("SNDRV_LSM_GENERIC_DET_EVENT");
}
else
#endif
if (!p_lsm_ses->common.is_generic_event) {
*request = SNDRV_LSM_EVENT_STATUS_V3;
*st_lsm_event_cmd = strdup("SNDRV_LSM_EVENT_STATUS_V3");
}
}
static uint64_t get_event_timestamp(st_lsm_event_status_t *params)
{
uint64_t timestamp;
timestamp = ((uint64_t)params->timestamp_msw << 32) |
params->timestamp_lsw;
return timestamp;
}
#else
static int set_lsm_fwk_mode(st_hw_session_lsm_t *p_lsm_ses __unused)
{
/* set fwk mode not supported */
return 0;
}
static void update_lsm_event_status_info(st_hw_session_lsm_t *p_lsm_ses,
int *request,
char **st_lsm_event_cmd)
{
#ifdef LSM_DET_EVENT_TYPE_GENERIC
if (p_lsm_ses->common.is_generic_event) {
*request = SNDRV_LSM_GENERIC_DET_EVENT;
*st_lsm_event_cmd = strdup("SNDRV_LSM_GENERIC_DET_EVENT");
}
else
#endif
if (!p_lsm_ses->common.is_generic_event) {
*request = SNDRV_LSM_EVENT_STATUS;
*st_lsm_event_cmd = strdup("SNDRV_LSM_EVENT_STATUS");
}
}
static uint64_t get_event_timestamp(st_lsm_event_status_t *params __unused)
{
/* timestamp mode not supported */
return 0;
}
#endif
#ifdef LSM_POLLING_ENABLE_SUPPORT
#ifdef ENABLE_SVA_MIXER_CTL
int lsm_set_port_mixer_ctl(struct mixer * st_mixer)
{
int ret = 0;
struct mixer_ctl *ctl = NULL;
const char *mixer_ctl_name = "LSM PORT SET";
ctl = mixer_get_ctl_by_name(st_mixer, mixer_ctl_name);
if (!ctl) {
ALOGE("%s: Could not get ctl for mixer cmd - %s",
__func__, mixer_ctl_name);
}
ret = mixer_ctl_set_value(ctl, 0, 0);
if (ret < 0) {
ALOGE("%s: Could not set LSM load mixer control", __func__);
}
return ret;
}
#endif
static int lsm_set_port(st_hw_session_lsm_t *p_lsm_ses)
{
int status = 0;
st_hw_session_t p_ses;
if (p_lsm_ses->common.stdev->lpi_enable &&
p_lsm_ses->common.vendor_uuid_info->lab_dam_cfg_payload.token_id) {
status = platform_stdev_set_shared_buf_fmt(
p_lsm_ses->common.stdev->platform, p_lsm_ses->pcm_id,
p_lsm_ses->common.vendor_uuid_info->shared_buf_fmt);
if (status)
return status;
}
p_ses = p_lsm_ses->common;
#ifdef ENABLE_SVA_MIXER_CTL
status = lsm_set_port_mixer_ctl(p_ses.stdev->mixer);
#else
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_PORT);
#endif
if (status)
ALOGE("%s: ERROR. SNDRV_LSM_SET_PORT, status=%d", __func__, status);
return status;
}
static bool fill_lsm_poll_enable_params
(
struct st_vendor_info *v_info,
st_lsm_poll_enable_t *poll_enable,
lsm_param_info_t *poll_en_params,
struct st_module_param_info *mparams,
uint16_t stage_idx
)
{
poll_enable->poll_en = v_info->profile_type == ST_PROFILE_TYPE_NONE;
poll_en_params->param_size = sizeof(*poll_enable);
poll_en_params->param_data = (unsigned char *)poll_enable;
lsm_fill_param_info(LSM_POLLING_ENABLE, poll_en_params,
&mparams[POLLING_ENABLE], stage_idx);
return true;
}
#else
static int lsm_set_port(st_hw_session_lsm_t *p_lsm_ses __unused)
{
return 0;
}
static bool fill_lsm_poll_enable_params
(
struct st_vendor_info *v_info __unused,
st_lsm_poll_enable_t *poll_enable __unused,
lsm_param_info_t *poll_en_params __unused,
struct st_module_param_info *mparams __unused,
uint16_t stage_idx __unused
)
{
return false;
}
#endif
#if (SNDRV_LSM_VERSION >= SNDRV_PROTOCOL_VERSION(0, 3, 0))
#ifdef ENABLE_SVA_MIXER_CTL
int lsm_set_input_hw_params_mixer_ctl(struct mixer * st_mixer, struct snd_lsm_input_hw_params *params)
{
int ret = 0;
struct mixer_ctl *ctl = NULL;
const char *mixer_ctl_name = "LSM INPUT_HW_PARAMS SET";
ctl = mixer_get_ctl_by_name(st_mixer, mixer_ctl_name);
if (!ctl) {
ALOGE("%s: Could not get ctl for mixer cmd - %s",
__func__, mixer_ctl_name);
}
ret = mixer_ctl_set_array(ctl, params, sizeof(struct snd_lsm_input_hw_params));
if (ret < 0) {
ALOGE("%s: Could not set input hw params control", __func__);
}
return ret;
}
#endif
static int send_lsm_input_hw_params(st_hw_session_t *p_ses)
{
struct st_vendor_info *v_info = p_ses->vendor_uuid_info;
st_hw_session_lsm_t *p_lsm_ses = (st_hw_session_lsm_t*)p_ses;
struct snd_lsm_input_hw_params params;
int status = 0;
params.sample_rate = v_info->sample_rate;
params.bit_width = pcm_format_to_bits(v_info->format);
if (platform_get_lpi_mode(p_ses->stdev->platform))
params.num_channels = p_lsm_ses->lsm_usecase.in_channels_lpi;
else
params.num_channels = p_lsm_ses->lsm_usecase.in_channels;
ALOGV("%s: set SNDRV_LSM_SET_INPUT_HW_PARAMS sr=%d bw=%d ch=%d ", __func__,
params.sample_rate, params.bit_width, params.num_channels);
#ifdef ENABLE_SVA_MIXER_CTL
status = lsm_set_input_hw_params_mixer_ctl(p_ses->stdev->mixer, ¶ms);
#else
status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_SET_INPUT_HW_PARAMS,
¶ms);
#endif
if (status) {
ALOGE("%s: SNDRV_LSM_SET_INPUT_HW_PARAMS failed, status [%d] - %s",
__func__, status, strerror(errno));
}
return status;
}
#else
#define send_lsm_input_hw_params(a) (0)
#endif
static void fill_set_params_payload(struct lsm_setparam_payload *custom_conf_setparam,
uint32_t data_payload_size, uint32_t data_payload_addr_lsw,
uint32_t data_payload_addr_msw, uint32_t mem_map_handle)
{
custom_conf_setparam->data_payload_size = data_payload_size;
custom_conf_setparam->data_payload_addr_lsw = data_payload_addr_lsw;
custom_conf_setparam->data_payload_addr_msw = data_payload_addr_msw;
custom_conf_setparam->mem_map_handle = mem_map_handle;
}
#ifdef LSM_DET_EVENT_TYPE_GENERIC
static bool fill_lsm_det_event_type_params
(
st_lsm_det_event_type_t *det_event_type,
lsm_param_info_t *det_event_type_params,
struct st_module_param_info *mparams,
uint16_t stage_idx,
st_module_type_t version,
st_hw_session_lsm_t *p_lsm_ses
)
{
/* fill event type params */
det_event_type->event_type = LSM_DET_EVENT_TYPE_GENERIC;
/* request for confidence level and timestamp */
if (version == ST_MODULE_TYPE_PDK5)
det_event_type->mode = DET_EVENT_MULTI_MODEL_RESULT_INFO_BIT;
else
det_event_type->mode =
DET_EVENT_CONFIDENCE_LEVELS_BIT | DET_EVENT_KEYWORD_INDEX_BIT |
DET_EVENT_TIMESTAMP_INFO_BIT;
if ((version != ST_MODULE_TYPE_PDK5) &&
(platform_is_best_channel_index_supported(p_lsm_ses->common.stdev->platform)))
det_event_type->mode |= DET_EVENT_CHANNEL_INDEX_INFO_BIT;
det_event_type_params->param_size = sizeof(*det_event_type);
det_event_type_params->param_data = (unsigned char *)det_event_type;
lsm_fill_param_info(LSM_DET_EVENT_TYPE, det_event_type_params,
&mparams[DET_EVENT_TYPE], stage_idx);
return true;
}
#else
static bool fill_lsm_det_event_type_params
(
st_lsm_det_event_type_t *det_event_type __unused,
lsm_param_info_t *det_event_type_params __unused,
struct st_module_param_info *mparams __unused,
uint16_t stage_idx __unused,
st_module_type_t version __unused,
st_hw_session_lsm_t *p_lsm_ses __unused
)
{
return false;
}
#endif
static st_profile_type_t get_profile_type(st_hw_session_t *p_ses)
{
st_profile_type_t profile_type;
profile_type = (p_ses->vendor_uuid_info && !p_ses->lpi_enable) ?
p_ses->vendor_uuid_info->profile_type : ST_PROFILE_TYPE_NONE;
return profile_type;
}
static void ape_enable_use_case(bool enable, st_hw_session_t *p_ses)
{
st_hw_session_lsm_t *p_lsm_ses = (st_hw_session_lsm_t *)p_ses;
st_profile_type_t profile_type = get_profile_type(p_ses);
char use_case[USECASE_STRING_SIZE];
audio_devices_t capture_device = 0;
st_device_t st_device = 0;
if (enable) {
strlcpy(use_case,
p_ses->stdev->ape_pcm_use_cases[p_ses->use_case_idx].use_case,
USECASE_STRING_SIZE);
platform_stdev_check_and_append_usecase(p_ses->stdev->platform,
use_case);
ALOGD("%s: enable use case = %s", __func__, use_case);
capture_device = platform_stdev_get_capture_device(p_ses->stdev->platform);
st_device = platform_stdev_get_device_for_cal(p_ses->stdev->platform,
p_ses->vendor_uuid_info, capture_device, p_ses->exec_mode);
platform_stdev_send_stream_app_type_cfg(p_ses->stdev->platform,
p_lsm_ses->pcm_id, st_device,
p_ses->exec_mode, profile_type);
platform_stdev_send_ec_ref_cfg(p_ses->stdev->platform, profile_type, true);
ATRACE_BEGIN("sthal:lsm: audio_route_apply_and_update_path");
audio_route_apply_and_update_path(p_ses->stdev->audio_route, use_case);
ATRACE_END();
if (p_lsm_ses->use_case)
free(p_lsm_ses->use_case);
p_lsm_ses->use_case = strdup(use_case);
} else {
if (!p_lsm_ses->use_case) {
ALOGE("%s: Invalid sound trigger usecase control", __func__);
return;
}
ALOGD("%s: disable use case = %s", __func__, p_lsm_ses->use_case);
ATRACE_BEGIN("sthal:lsm: audio_route_reset_and_update_path");
audio_route_reset_and_update_path(p_ses->stdev->audio_route, p_lsm_ses->use_case);
ATRACE_END();
platform_stdev_send_ec_ref_cfg(p_ses->stdev->platform, profile_type, false);
free(p_lsm_ses->use_case);
p_lsm_ses->use_case = NULL;
}
}
static int ape_enable_port_control(bool enable, st_hw_session_t *p_ses)
{
int ret = 0;
char port_ctrl[USECASE_STRING_SIZE] = {0};
st_hw_session_lsm_t *p_lsm_ses = (st_hw_session_lsm_t *)p_ses;
if (enable) {
strlcpy(port_ctrl,
p_ses->stdev->ape_pcm_use_cases[p_ses->use_case_idx].use_case,
USECASE_STRING_SIZE);
platform_stdev_check_and_append_usecase(p_ses->stdev->platform,
port_ctrl);
strlcat(port_ctrl, " port", USECASE_STRING_SIZE);
ALOGV("%s: enable = %s", __func__, port_ctrl);
ret = audio_route_apply_and_update_path(p_ses->stdev->audio_route, port_ctrl);
if (!ret) {
if (p_lsm_ses->port_ctrl)
free(p_lsm_ses->port_ctrl);
p_lsm_ses->port_ctrl = strdup(port_ctrl);
}
} else {
if (!p_lsm_ses->port_ctrl) {
ALOGW("%s: Invalid sound trigger port control", __func__);
ret = -EINVAL;
} else {
ALOGV("%s: disable = %s", __func__, p_lsm_ses->port_ctrl);
/*
* Do not send reset controls as driver is not maintaining
* state per session since current implementation limits port
* settings to be shared globaly and cannot vary per session.
* After first session resets port value as part of its disable
* sequence, driver is missing info if the next session to be
* disabled was using adm/non-adm path.
* Port update if applicable would be done as part of enable
* sequence of any one of the sessions.
*/
free(p_lsm_ses->port_ctrl);
p_lsm_ses->port_ctrl = NULL;
}
}
return ret;
}
static int read_pcm_data(st_hw_session_lsm_t *p_ses,
unsigned char *buf,
unsigned int bytes)
{
unsigned int read_bytes = 0, move_bytes = 0, delay_bytes = 0;
unsigned int requested_bytes = 0;
struct timespec tspec;
int ret = 0;
pthread_mutex_lock(&p_ses->lock);
ALOGVV("%s: bytes=%d, unread_bytes=%d", __func__, bytes,
p_ses->unread_bytes);
if (p_ses->move_client_ptr) {
/*
* This logic is needed if LAB was enabled due to second stage being enabled,
* but the client did not request LAB. The client read pointer will be shifted
* to the keyword end index in this usecase.
*/
if (p_ses->common.enable_second_stage &&
!p_ses->common.max_hist_buf)
delay_bytes = p_ses->common.kw_end_idx;
else
delay_bytes = convert_ms_to_bytes(
(p_ses->common.max_preroll - p_ses->common.detected_preroll),
&p_ses->common.config);
move_bytes = MIN(delay_bytes, p_ses->unread_bytes);
ALOGD("%s: Moving client ptr by %d bytes", __func__, move_bytes);
st_buffer_flush(p_ses->common.buffer, move_bytes);
p_ses->unread_bytes -= move_bytes;
p_ses->move_client_ptr = false;
}
requested_bytes = bytes;
while (!p_ses->exit_lab_processing && (bytes > 0)) {
if (!p_ses->unread_bytes) {
ALOGVV("%s: waiting on cond, bytes=%d", __func__, bytes);
/* Time out to unblock read thread in case if write thread is
stuck filling the buffers */
GET_WAIT_TIMESPEC(tspec, convert_bytes_to_ms(
(p_ses->lab_drv_buf_size * 4), &p_ses->common.config) *
NSECS_PER_MSEC);
ret = pthread_cond_timedwait(&p_ses->cond, &p_ses->lock, &tspec);
ALOGVV("%s: done waiting on cond", __func__);
if (ret) {
ALOGE("%s: ERROR. read wait timed out, ret %d", __func__, ret);
p_ses->exit_lab_processing = true;
ret = -EIO;
goto exit;
}
if (p_ses->exit_lab_processing) {
ALOGVV("%s: buffering stopped while waiting on cond, exiting",
__func__);
ret = -EIO;
goto exit;
}
}
read_bytes = MIN(bytes, p_ses->unread_bytes);
ret = st_buffer_read(p_ses->common.buffer, buf, read_bytes,
NULL, true);
if (ret) {
ALOGE("%s: st_buffer_read failed, status %d", __func__, ret);
goto exit;
}
pthread_cond_signal(&p_ses->cond);
p_ses->unread_bytes -= read_bytes;
bytes -= read_bytes;
buf += read_bytes;
}
exit:
pthread_mutex_unlock(&p_ses->lock);
return 0;
}
static int write_pcm_data_ape(st_hw_session_lsm_t *p_lsm_ses,
unsigned char *buf,
unsigned int bytes)
{
int status = 0;
struct listnode *node = NULL, *tmp_node = NULL;
st_arm_second_stage_t *st_sec_stage = NULL;
status = st_buffer_write(p_lsm_ses->common.buffer, buf, bytes);
if (status) {
p_lsm_ses->exit_lab_processing = true;
if (p_lsm_ses->common.enable_second_stage) {
list_for_each_safe(node, tmp_node, p_lsm_ses->common.second_stage_list) {
st_sec_stage = node_to_item(node, st_arm_second_stage_t, list_node);
pthread_mutex_lock(&st_sec_stage->ss_session->lock);
ALOGW("%s: Exit 2nd stage processing due to buf overflow",
__func__);
st_sec_stage->ss_session->exit_buffering = true;
pthread_cond_signal(&st_sec_stage->ss_session->cond);
pthread_mutex_unlock(&st_sec_stage->ss_session->lock);
}
}
goto exit;
}
p_lsm_ses->unread_bytes += bytes;
p_lsm_ses->bytes_written += bytes;
if (p_lsm_ses->common.enable_second_stage) {
list_for_each_safe(node, tmp_node, p_lsm_ses->common.second_stage_list) {
st_sec_stage = node_to_item(node, st_arm_second_stage_t, list_node);
/*
* When the current written frame passes the number of bytes indicated
* in buf_start, give the second stage session its read pointer.
*/
pthread_mutex_lock(&st_sec_stage->ss_session->lock);
if ((p_lsm_ses->bytes_written >= st_sec_stage->ss_session->buf_start) &&
!st_sec_stage->ss_session->start_processing) {
st_sec_stage->ss_session->hw_rd_ptr =
st_buffer_get_wr_ptr(p_lsm_ses->common.buffer);
st_sec_stage->ss_session->hw_rd_ptr -= bytes;
st_sec_stage->ss_session->start_processing = true;
}
if (st_sec_stage->ss_session->start_processing) {
st_sec_stage->ss_session->unread_bytes += bytes;
pthread_cond_signal(&st_sec_stage->ss_session->cond);
}
pthread_mutex_unlock(&st_sec_stage->ss_session->lock);
}
}
exit:
ALOGVV("%s: about to signal condition", __func__);
pthread_cond_signal(&p_lsm_ses->cond);
return status;
}
static void adjust_ss_buff_end(st_hw_session_t *p_ses,
uint32_t cnn_append_bytes, uint32_t vop_append_bytes)
{
st_hw_session_lsm_t *p_hw_ses = (st_hw_session_lsm_t *)p_ses;
struct listnode *node = NULL, *tmp_node = NULL;
st_arm_second_stage_t *st_sec_stage = NULL;
list_for_each_safe(node, tmp_node, p_ses->second_stage_list) {
st_sec_stage = node_to_item(node, st_arm_second_stage_t, list_node);
pthread_mutex_lock(&st_sec_stage->ss_session->lock);
if ((st_sec_stage->ss_info->sm_detection_type ==
ST_SM_TYPE_KEYWORD_DETECTION) &&
((p_hw_ses->bytes_written + cnn_append_bytes) <
st_sec_stage->ss_session->buf_end)) {
ALOGV("%s: Adjusting CNN buf_end from %d to %d bytes", __func__,
st_sec_stage->ss_session->buf_end,
p_hw_ses->bytes_written + cnn_append_bytes);
st_sec_stage->ss_session->buf_end = p_hw_ses->bytes_written +
cnn_append_bytes;
} else if ((st_sec_stage->ss_info->sm_detection_type ==
ST_SM_TYPE_USER_VERIFICATION) &&
((p_hw_ses->bytes_written + vop_append_bytes) <
st_sec_stage->ss_session->buff_sz)) {
ALOGV("%s: Adjusting VOP buff_sz from %d to %d bytes", __func__,
st_sec_stage->ss_session->buff_sz,
(p_hw_ses->bytes_written + vop_append_bytes));
st_sec_stage->ss_session->buff_sz =
(p_hw_ses->bytes_written + vop_append_bytes);
}
pthread_mutex_unlock(&st_sec_stage->ss_session->lock);
}
}
static void *buffer_thread_loop(void *context)
{
st_hw_session_lsm_t *p_lsm_ses =
(st_hw_session_lsm_t *)context;
int status = 0;
struct listnode *node = NULL, *tmp_node = NULL;
st_arm_second_stage_t *st_sec_stage = NULL;
unsigned int cnn_prepend_bytes = 0, vop_prepend_bytes = 0;