-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_hw_session_gcs.c
2067 lines (1827 loc) · 78.2 KB
/
st_hw_session_gcs.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_gcs.c
*
* This file implements the hw session functionality specific to HW
* sessions that use GCS.
*
* Copyright (c) 2016-2020, 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.
*/
#define LOG_TAG "sound_trigger_hw"
#define ATRACE_TAG (ATRACE_TAG_HAL)
/* #define LOG_NDEBUG 0 */
#define LOG_NDDEBUG 0
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <cutils/trace.h>
#include <fcntl.h>
#include <stdlib.h>
#include "st_hw_session_gcs.h"
#include "sound_trigger_platform.h"
#include "sound_trigger_hw.h"
#include "st_graphite_api.h"
//#include "gcs_api.h"
#include "st_hw_common.h"
#define GCS_LIB "libgcs.so"
#define GCS_CONCURRENT_READS_CNT 2
#define WDSP_SYSFS_NAME "/dev/wcd_dsp0_control"
static int reg_sm(st_hw_session_t *p_ses,
void *sm_data, unsigned int sm_size,
uint32_t model_id);
static int 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 read_pcm(st_hw_session_t *p_ses,
unsigned char *buf,
unsigned int bytes);
static void process_lab_capture(st_hw_session_t *p_ses);
static int dereg_sm(st_hw_session_t *p_ses, uint32_t model_id __unused);
static int dereg_sm_params(st_hw_session_t *p_ses);
static int start(st_hw_session_t *p_ses);
static int restart(st_hw_session_t *p_ses,
unsigned int recognition_mode,
struct sound_trigger_recognition_config *rc_config __unused);
static int stop(st_hw_session_t *p_ses);
static int stop_buffering(st_hw_session_t *p_ses);
static int set_device(st_hw_session_t *p_ses,
bool enable);
static int disable_device(st_hw_session_t *p_ses, bool setting_device);
static int enable_device(st_hw_session_t *p_ses, bool setting_device);
static int send_detection_request(st_hw_session_t *p_ses);
static int dbg_trace_lab_buf_cnt = -1;
static struct pcm_config stdev_cpe_pcm_config = {
.channels = SOUND_TRIGGER_CHANNEL_MODE_MONO,
.rate = SOUND_TRIGGER_SAMPLING_RATE_16000,
.period_size = ST_GRAPHITE_LAB_PERIOD_SIZE_IN_SAMPLES,
.period_count = ST_GRAPHITE_LAB_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
};
static struct st_session_fptrs fptrs_gcs = {
.reg_sm = reg_sm,
.reg_sm_params = reg_sm_params,
.dereg_sm = dereg_sm,
.dereg_sm_params = dereg_sm_params,
.start = start,
.restart = restart,
.stop = stop,
.stop_buffering = stop_buffering,
.disable_device = disable_device,
.enable_device = enable_device,
.set_device = set_device,
.read_pcm = read_pcm,
.process_lab_capture = process_lab_capture,
.send_detection_request = send_detection_request,
};
/* Line 170: gcs_event_cb */
typedef int32_t(*event_cb_ptr)(uint32_t graph_hdl,
struct gcs_event_rsp *ev, void *private_data);
/* Line 242: gcs_data_cmdrsp_cb */
typedef int32_t(*data_cmd_cb_ptr)(uint32_t graph_handle,
void *rsp,
size_t rsp_size,
void *cookie, int32_t cmd_status);
/* gcs functions loaded from dynamic library */
static int32_t(*gcs_init_fn)(void);
static int32_t(*gcs_open_fn)(uint32_t UID,
uint32_t DID,
uint32_t *graph_handle);
static int32_t(*gcs_enable_fn)(uint32_t graph_handle,
void *non_persist_ucal,
uint32_t size_ucal);
static int32_t(*gcs_disable_fn)(uint32_t graph_handle);
static int32_t(*gcs_close_fn)(uint32_t graph_handle);
static int32_t(*gcs_load_data_fn)(uint32_t graph_handle,
void *data,
uint32_t data_size,
uint32_t *data_handle);
static int32_t(*gcs_unload_data_fn)(uint32_t graph_handle, uint32_t data_handle);
static int32_t(*gcs_deinit_fn)(void);
static int32_t(*gcs_register_for_event_fn)(uint32_t graph_handle,
struct gcs_module_param_info *module_param_info,
event_cb_ptr cb_handler, void *cookie);
static int32_t(*gcs_register_data_cmd_handler_fn)(uint32_t graph_handle,
void *data_cmd_cb_handler,
void *cookie);
static int32_t(*gcs_start_buff_xfer_fn)(uint32_t graph_handle, enum gcs_data_xfer dir);
static int32_t(*gcs_stop_buff_xfer_fn)(uint32_t graph_handle, enum gcs_data_xfer dir);
static int32_t(*gcs_send_data_cmd_fn)(uint32_t graph_handle,
int8_t *payload,
uint32_t payload_size);
static int32_t(*gcs_enable_device_fn)(uint32_t graph_handle,
uint32_t UID, int8_t *payload, uint32_t payload_size);
static int32_t(*gcs_disable_device_fn)(uint32_t graph_handle);
static int32_t (*gcs_set_config_fn)(uint32_t graph_handle, void *payload,
uint32_t payload_size);
/* used to output pcm to file for debugging */
ST_DBG_DECLARE(static int lab_fp_gcs_cnt = 0);
ST_DBG_DECLARE(static int lab_fp_client_cnt = 0);
struct st_hw_gcs_data {
void *lib_handle;
int sysfs_fd;
};
static struct st_hw_gcs_data gcs_data = {.lib_handle = NULL, .sysfs_fd = 0 };
static inline void wdsp_debug_dump(int fd)
{
ssize_t len = (ssize_t)sizeof("DEBUG_DUMP");
ssize_t ret_bytes = write(fd, "DEBUG_DUMP", len);
if (ret_bytes != len) {
//ALOGE("%s: write failed %zd < %zd, %s", __func__, ret_bytes,
// len, strerror(errno));
}
}
static int32_t gcs_event_cb(uint32_t graph_hdl,
struct gcs_event_rsp *ev,
void *private_data)
{
st_hw_session_gcs_t *p_ses = NULL;
int32_t status = 0;
int mutex_ret = 0;
ALOGD("%s: Enter...", __func__);
if (!private_data || !ev) {
ALOGE("%s: received invalid params", __func__);
status = -EINVAL;
goto exit;
}
p_ses = (st_hw_session_gcs_t *)private_data;
if (p_ses->graph_handle != graph_hdl) {
ALOGE("%s: graph_hdl mismatch param has %d but private data has %d",
__func__, graph_hdl, p_ses->graph_handle);
status = -EINVAL;
goto exit;
}
if (ev->payload_size == 0 ||
ev->payload_size > MAX_DETECTION_PAYLOAD_SZ) {
ALOGE("%s: received invalid detection payload size %d",
__func__, ev->payload_size);
status = -EINVAL;
goto exit;
}
if (ev->module_param_info.PID !=
p_ses->gcs_usecase->params[DETECTION_EVENT].param_id) {
ALOGE("%s: Detection event PID does not match the requested PID, exiting",
__func__);
status = -EINVAL;
goto exit;
}
/*
* We use try_lock here, if a detection event is currently being processed, we
* will ignore incoming detections until processing the current one is completed.
* Anyway we are not expected to get any detection notification from FW until the
* previous one is processed and the application restarts recognition
*/
mutex_ret = pthread_mutex_trylock(&p_ses->callback_thread_lock);
if (!mutex_ret) {
/* The duration of this trace log indicates the detection latency. */
ATRACE_ASYNC_BEGIN("sthal: detection success",
p_ses->common.sm_handle);
ATRACE_ASYNC_BEGIN("sthal: detection reject",
p_ses->common.sm_handle);
ALOGV("%s: got mutex lock", __func__);
p_ses->detect_payload_size = ev->payload_size;
memcpy(p_ses->detect_payload, ev->payload, p_ses->detect_payload_size);
p_ses->detection_signaled = true;
pthread_cond_signal(&p_ses->callback_thread_cond);
pthread_mutex_unlock(&p_ses->callback_thread_lock);
} else {
ALOGD("%s: previous event in-progress, ignore event", __func__);
}
exit:
return status;
}
static int32_t gcs_data_cmdrsp_cb(uint32_t graph_handle,
void *rsp,
size_t rsp_size,
void *cookie, int32_t cmd_status)
{
int status = 0;
st_hw_session_gcs_t *p_gcs_ses = NULL;
struct graphite_data_cmdrsp_hdr *hdr = NULL;
struct gcs_cmd_readrsp_payload_t *payload = NULL;
uint32_t buff_sz = 0;
uint8_t *buff = NULL;
struct st_vendor_info *v_info = NULL ;
st_hw_session_t *p_ses = NULL;
sound_trigger_device_t * stdev = NULL;
struct listnode *node = NULL, *tmp_node = NULL;
st_arm_second_stage_t *st_sec_stage = NULL;
p_gcs_ses = (st_hw_session_gcs_t *)cookie;
ST_DBG_ATRACE_ASYNC_END_IF(++dbg_trace_lab_buf_cnt == dbg_trace_max_lab_reads,
"sthal:gcs: gcs_buffer_read",p_gcs_ses->common.sm_handle);
if (!p_gcs_ses) {
ALOGE("%s: received NULL cookie", __func__);
return -EINVAL;
}
if (p_gcs_ses->graph_handle != graph_handle) {
ALOGE("%s: graph_hdl mismatch param has %d but private data has %d",
__func__, graph_handle, p_gcs_ses->graph_handle);
return -EINVAL;
}
if (0 > cmd_status) {
ALOGE("%s: received failed cmdrsp status: %d", __func__, cmd_status);
return status;
}
if (0 == rsp_size) {
ALOGE("%s: received response size of 0", __func__);
return -EINVAL;
}
p_gcs_ses->frame_receive_time = get_current_time_ns();
p_ses = (st_hw_session_t *)p_gcs_ses;
v_info = p_ses->vendor_uuid_info;
stdev = p_ses->stdev;
/* parse CMDRSP message */
hdr = (struct graphite_data_cmdrsp_hdr *)rsp;
if (hdr->module_id != p_gcs_ses->gcs_usecase->params[READ_RSP].module_id ||
hdr->instance_id != p_gcs_ses->gcs_usecase->params[READ_RSP].instance_id ||
hdr->cmd_id != p_gcs_ses->gcs_usecase->params[READ_RSP].param_id) {
ALOGE("%s: received unexpected parameters module_id %d, instance_id %d"
", cmd_id %d expected module_id %d, instance_id %d, cmd_id %d",
__func__, hdr->module_id, hdr->instance_id, hdr->cmd_id,
p_gcs_ses->gcs_usecase->params[READ_RSP].module_id,
p_gcs_ses->gcs_usecase->params[READ_RSP].instance_id,
p_gcs_ses->gcs_usecase->params[READ_RSP].param_id);
return -EINVAL;
}
/* received response for READ CMD */
payload = (struct gcs_cmd_readrsp_payload_t *)((uint8_t *)rsp
+ sizeof(struct graphite_data_cmdrsp_hdr));
if (GCS_READ_CMDRSP_STATUS_SUCCESS != payload->status) {
ALOGE("%s: cmdrsp_status %d", __func__, payload->status);
return -EINVAL;
}
buff_sz = hdr->size_in_bytes - sizeof(struct gcs_cmd_readrsp_payload_t);
buff = (uint8_t *)payload + sizeof(struct gcs_cmd_readrsp_payload_t);
if (p_ses->stdev->enable_debug_dumps)
ST_DBG_FILE_WRITE(p_gcs_ses->lab_fp_gcs, buff, buff_sz);
if (buff_sz > ST_GCS_READ_BUF_SIZE) {
ALOGW("%s: received size %d more than requested %d, truncate",
__func__, buff_sz, ST_GCS_READ_BUF_SIZE);
buff_sz = ST_GCS_READ_BUF_SIZE;
}
pthread_mutex_lock(&p_gcs_ses->lock);
if (!p_gcs_ses->exit_buffering) {
if ((v_info->kw_capture_format & MULAW) &&
stdev->mulaw_dec_process) {
/* Produces 1:2 decoded data for any input size */
ATRACE_BEGIN("sthal:gcs: mulaw_dec_process");
status = stdev->mulaw_dec_process((short *)p_gcs_ses->mulaw_op_buf,
(char *)buff, buff_sz);
ATRACE_END();
if (status) {
ALOGE("%s:[%d] mulaw error %d ", __func__, p_ses->sm_handle,
status);
p_gcs_ses->exit_buffering = true;
goto exit_unlock;
}
/* don't need input buff anymore */
buff = p_gcs_ses->mulaw_op_buf;
buff_sz <<= 1; /* expected decoded output size */
}
status = st_buffer_write(p_ses->buffer, buff, buff_sz);
if (status) {
p_gcs_ses->exit_buffering = true;
if (p_ses->enable_second_stage) {
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);
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_unlock;
}
p_gcs_ses->unread_bytes += buff_sz;
p_gcs_ses->bytes_written += buff_sz;
if (p_ses->enable_second_stage) {
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);
/*
* 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_gcs_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_ses->buffer);
st_sec_stage->ss_session->hw_rd_ptr -= buff_sz;
st_sec_stage->ss_session->start_processing = true;
}
if (st_sec_stage->ss_session->start_processing) {
st_sec_stage->ss_session->unread_bytes += buff_sz;
pthread_cond_signal(&st_sec_stage->ss_session->cond);
}
pthread_mutex_unlock(&st_sec_stage->ss_session->lock);
}
}
/*
* wakeup both reader thread to read and buffering thread to
* request more data
*/
ALOGVV("%s: signaling cond", __func__);
++p_gcs_ses->read_rsp_cnt;
pthread_cond_broadcast(&p_gcs_ses->cond);
}
exit_unlock:
pthread_mutex_unlock(&p_gcs_ses->lock);
return status;
}
static void* callback_thread_loop(void *context)
{
st_hw_session_gcs_t *p_ses = (st_hw_session_gcs_t *)context;
st_hw_sess_event_t hw_ses_event; /* used to report event to st_session */
struct gcs_det_engine_event *p_det = NULL;
struct gcs_det_engine_extended_event *p_det_ext = NULL;
uint8_t *ev_payload = NULL;
size_t ev_payload_size = 0;
uint32_t event_status = 0;
if (!p_ses) {
ALOGE("%s: Received null session ptr", __func__);
goto exit;
}
ALOGV("%s: callback thread started...", __func__);
pthread_mutex_lock(&p_ses->callback_thread_lock);
while (!p_ses->detection_signaled && !p_ses->exit_detection) {
pthread_cond_wait(&p_ses->callback_thread_cond,
&p_ses->callback_thread_lock);
/*
* Currently, DSP does not support the inclusion of detection
* timestamp within the payload. So the timestamp is filled here
* instead.
*/
p_ses->common.first_stage_det_event_time = get_current_time_ns();
ALOGV("%s: came out of cond_wait...", __func__);
if (p_ses->exit_detection)
break;
if (!p_ses->detection_signaled)
continue;
ALOGV("%s: detection signaled", __func__);
p_ses->detection_signaled = false;
p_det = (struct gcs_det_engine_event *)p_ses->detect_payload;
if (p_ses->common.stdev->enable_debug_dumps) {
ST_DBG_DECLARE(FILE *detect_fd = NULL;
static int detect_fd_cnt = 0);
ST_DBG_FILE_OPEN_WR(detect_fd, ST_DEBUG_DUMP_LOCATION,
"gcs_detection_event", "bin", detect_fd_cnt++);
ST_DBG_FILE_WRITE(detect_fd, p_ses->detect_payload,
p_ses->detect_payload_size);
ST_DBG_FILE_CLOSE(detect_fd);
}
if (p_ses->start_engine_cal) {
p_det_ext = (struct gcs_det_engine_extended_event *)p_det;
/* This is a special case of handling multiple VA engines in single
* DSP VA module with HAL providing a customized interface to
* VA module.
* gcs sends one VA engine event to another VA engine session if
* both sessions are registered for same DSP module containing both
* VA engines. Ignore the unwanted event by validating the token in
* the payload
*/
if (p_det_ext->start_params.token !=
p_ses->start_engine_cal->token) {
ALOGV("%s: Ignore unwanted event, gh %d token %d != %d",
__func__, p_ses->graph_handle,
p_det_ext->start_params.token,
p_ses->start_engine_cal->token);
continue;
}
/* Get to actual VA engine specific payload */
ev_payload = p_ses->detect_payload +
sizeof(p_det_ext->start_params);
ev_payload_size = p_det->custom_payload_sz -
sizeof(p_det_ext->start_params);
} else {
unsigned int payload_offset = 0;
if (p_ses->common.vendor_uuid_info->is_qcmd_uuid)
payload_offset = sizeof(struct st_param_header) +
GENERIC_DET_EVENT_HEADER_SIZE;
ev_payload = p_ses->detect_payload + sizeof(*p_det) +
payload_offset;
ev_payload_size = p_det->custom_payload_sz - payload_offset;
}
/* get detection status */
if (p_det->status == GCS_DETECTION_ENGINE_EVENT_DETECTED)
event_status = RECOGNITION_STATUS_SUCCESS;
else
event_status = RECOGNITION_STATUS_FAILURE;
/*
* inform st_session of the event, st_session knows how to parse list
* of conf levels
*/
ALOGV("%s: notifying st_session of the detection event, status = %d",
__func__, event_status);
hw_ses_event.event_id = ST_HW_SESS_EVENT_DETECTED;
hw_ses_event.payload.detected.timestamp = 0;
hw_ses_event.payload.detected.detect_status = event_status;
hw_ses_event.payload.detected.payload_size = ev_payload_size;
hw_ses_event.payload.detected.detect_payload = ev_payload;
p_ses->common.callback_to_st_session(&hw_ses_event,
p_ses->common.cookie);
}
pthread_mutex_unlock(&p_ses->callback_thread_lock);
ALOGV("%s: callback thread ending...", __func__);
exit:
return NULL;
}
static int reg_sm(st_hw_session_t *p_ses, void *sm_data,
unsigned int sm_size, uint32_t model_id __unused)
{
int status = 0;
uint8_t *load_sm_msg = NULL;
struct graphite_cal_header *sm_msg_hdr = NULL;
unsigned int load_sm_msg_sz = 0;
struct st_vendor_info *v_info = p_ses->vendor_uuid_info;
struct gcs_module_param_info gcs_module_info; /* used to register for ev*/
st_hw_session_gcs_t *p_gcs_ses = (st_hw_session_gcs_t *)p_ses;
int st_device = 0, device_acdb_id = 0;
int capture_device;
/* BG target doesn't have wdsp */
if (!p_ses->stdev->bg_kwd) {
status = st_hw_gcs_load_wdsp(true);
if (status)
return status;
}
/* get the acdb device id to use when opening a graph */
capture_device = platform_stdev_get_capture_device(p_ses->stdev->platform);
st_device = platform_stdev_get_device(p_ses->stdev->platform,
v_info, capture_device, p_ses->exec_mode);
if (st_device == ST_DEVICE_NONE) {
ALOGE("%s: Could not find valid device",__func__);
status = -EINVAL;
goto cleanup2;
}
device_acdb_id = platform_stdev_get_acdb_id(st_device,
p_ses->exec_mode);
if (0 > device_acdb_id) {
ALOGE("%s: Could not get device ACDB ID", __func__);
status = -EINVAL;
goto cleanup2;
}
/* allocate a gcs use-case for this session from vendor info */
platform_alloc_gcs_usecase(p_ses->stdev->platform, v_info,
&p_gcs_ses->gcs_usecase, device_acdb_id);
if (!p_gcs_ses->gcs_usecase) {
ALOGE("%s: failed to allocate gcs usecase for the session", __func__);
status = -ENOMEM;
goto cleanup2;
}
p_ses->config = stdev_cpe_pcm_config;
platform_stdev_check_and_update_pcm_config(&p_ses->config, v_info);
ALOGD("%s:[%d] calling gcs_open with uid %d, did %d", __func__,
p_ses->sm_handle, p_gcs_ses->gcs_usecase->uid, device_acdb_id);
ATRACE_BEGIN("sthal:gcs: gcs_open");
status = gcs_open_fn(p_gcs_ses->gcs_usecase->uid, device_acdb_id,
&p_gcs_ses->graph_handle);
ATRACE_END();
if (status) {
ALOGE("%s: gcs_open failed with status %d", __func__, status);
wdsp_debug_dump(gcs_data.sysfs_fd);
goto cleanup1;
}
/* calculate size of load sm msg */
load_sm_msg_sz = sizeof(struct graphite_cal_header); /* param header for sound model param */
load_sm_msg_sz += sm_size; /* SM opaque data size from upper layers */
load_sm_msg_sz = ALIGN(load_sm_msg_sz, 4);
load_sm_msg = calloc(load_sm_msg_sz, sizeof(uint8_t));
if (!load_sm_msg) {
ALOGE("%s: failed to allocate memory for sm msg, size = %u", __func__,
load_sm_msg_sz);
status = -ENOMEM;
goto cleanup1;
}
sm_msg_hdr = (struct graphite_cal_header *)load_sm_msg;
sm_msg_hdr->module_id = p_gcs_ses->gcs_usecase->
params[LOAD_SOUND_MODEL].module_id;
sm_msg_hdr->instance_id = p_gcs_ses->gcs_usecase->
params[LOAD_SOUND_MODEL].instance_id;
sm_msg_hdr->param_id = p_gcs_ses->gcs_usecase->
params[LOAD_SOUND_MODEL].param_id;
sm_msg_hdr->size = sm_size;
ALOGV("%s: sm cal header MID %x, IID %x, PID %x \n sm data %p, sm size %u,"
" alloc size %u", __func__, sm_msg_hdr->module_id,
sm_msg_hdr->instance_id, sm_msg_hdr->param_id, sm_data,
sm_size, load_sm_msg_sz);
memcpy(load_sm_msg + sizeof(struct graphite_cal_header),
(uint8_t *)sm_data, sm_size);
if (p_ses->stdev->enable_debug_dumps) {
ST_DBG_DECLARE(FILE *load_fd = NULL; static int load_fd_cnt = 0);
ST_DBG_FILE_OPEN_WR(load_fd, ST_DEBUG_DUMP_LOCATION, "load_sm", "bin",
load_fd_cnt++);
ST_DBG_FILE_WRITE(load_fd, load_sm_msg, load_sm_msg_sz);
ST_DBG_FILE_CLOSE(load_fd);
}
ALOGD("%s:[%d] calling gcs_load_data with graph_handle %d, load_sm_msg %p, "
"load_sm_msg_sz %d", __func__, p_ses->sm_handle, p_gcs_ses->graph_handle,
load_sm_msg, load_sm_msg_sz);
ATRACE_BEGIN("sthal:gcs: gcs_load_data");
status = gcs_load_data_fn(p_gcs_ses->graph_handle, load_sm_msg,
load_sm_msg_sz, &p_gcs_ses->loaded_sm_handle);
ATRACE_END();
if (status) {
ALOGE("%s: gcs_load_data failed with status %d", __func__, status);
wdsp_debug_dump(gcs_data.sysfs_fd);
goto cleanup1;
}
free(load_sm_msg);
load_sm_msg = NULL;
/* register callback for event handling */
gcs_module_info.module_info.MID =
p_gcs_ses->gcs_usecase->params[DETECTION_EVENT].module_id;
gcs_module_info.module_info.IID =
p_gcs_ses->gcs_usecase->params[DETECTION_EVENT].instance_id;
gcs_module_info.PID =
p_gcs_ses->gcs_usecase->params[DETECTION_EVENT].param_id;
ALOGD("%s:[%d] calling gcs_register_for_event with MID %x, IID %x, PID %x", __func__,
p_ses->sm_handle, gcs_module_info.module_info.MID,
gcs_module_info.module_info.IID, gcs_module_info.PID);
ATRACE_BEGIN("sthal:gcs: gcs_register_for_event");
status = gcs_register_for_event_fn(p_gcs_ses->graph_handle, &gcs_module_info,
gcs_event_cb, p_gcs_ses);
ATRACE_END();
if (status) {
ALOGE("%s: gcs_register_for_event failed with status %d", __func__, status);
wdsp_debug_dump(gcs_data.sysfs_fd);
goto cleanup1;
}
/* register callback for data cmd handling */
ALOGD("%s:[%d] calling gcs_register_data_cmd_handler with handle %d, cb %p, "
"cookie %p", __func__, p_ses->sm_handle,
p_gcs_ses->graph_handle, gcs_data_cmdrsp_cb, p_gcs_ses);
ATRACE_BEGIN("sthal:gcs: gcs_register_data_cmd_handler");
status = gcs_register_data_cmd_handler_fn(p_gcs_ses->graph_handle,
(data_cmd_cb_ptr)gcs_data_cmdrsp_cb, p_gcs_ses);
ATRACE_END();
if (status) {
ALOGE("%s: gcs_register_data_cmd_handler failed with status %d",
__func__, status);
wdsp_debug_dump(gcs_data.sysfs_fd);
goto cleanup1;
}
return status;
cleanup1:
if (load_sm_msg)
free(load_sm_msg);
platform_free_gcs_usecase(v_info, p_gcs_ses->gcs_usecase);
cleanup2:
/* unload WDSP image */
if (gcs_data.sysfs_fd >= 0)
write(gcs_data.sysfs_fd, "0", 2);
return status;
}
static int reg_sm_params(st_hw_session_t *p_ses,
unsigned int recognition_mode,
bool capture_requested,
struct sound_trigger_recognition_config *rc_config)
{
st_hw_session_gcs_t *p_hw_ses = (st_hw_session_gcs_t *)p_ses;
uint8_t *msg_offset = NULL;
uint32_t rt_bytes_one_sec;
unsigned int i;
int status = 0;
size_t det_config_size = 0, custom_config_size = 0, start_custom_config_size = 0;
size_t det_event_type_size = 0;
struct gcs_det_engine_config_param *p_msg = NULL;
struct gcs_det_engine_custom_config_param *cc_msg = NULL;
struct gcs_det_engine_start_custom_config *stcfg_msg = NULL;
struct gcs_det_event_type_custom_config *det_event_msg = NULL;
struct st_vendor_info *v_info = p_ses->vendor_uuid_info;
struct listnode *node = NULL;
struct st_hw_ses_config *sthw_cfg = NULL;
bool disable_custom_config = false;
if (NULL != p_hw_ses->nonpersistent_cal) {
ALOGE("%s: nonpersistent cal data already cached! failing.", __func__);
return -EINVAL;
}
/* The nonpersistant_cal to be sent to WDSP can contain mulitple param
* payloads with corresponding headers. For example
* |Param Header1|
* |Param Payload1|
* |Param Header2|
* |Param Payload2|
* ...
* Each header and its payload size must be 4bytes aligned.
*
* These params can be combination DETECTION_ENGINE_CONFIG,
* DETECTION_ENGINE_CUSTOM_CONFIG and START_ENGINE params.
*
* Set custom_config flag if mID and pID have been set in platform xml
* file. If the flag is set, the opaque data from client will be wrapped
* in a header containing mID, pID, etc. Else, the opaque data is sent as
* it is, and it is assumed that it is formatted from within.
*/
if (!list_empty(&p_ses->sthw_cfg_list)) {
node = list_head(&p_ses->sthw_cfg_list);
sthw_cfg = node_to_item(node, struct st_hw_ses_config,
sthw_cfg_list_node);
} else {
ALOGE("%s: Unexpected, sthw_cfg list is empty", __func__);
return -EINVAL;
}
if ((rc_config->data_size > CUSTOM_CONFIG_OPAQUE_DATA_SIZE) &&
v_info->is_qcva_uuid) {
if (!capture_requested)
disable_custom_config = true;
det_config_size = sizeof(struct gcs_det_engine_config_param) +
sthw_cfg->num_conf_levels;
det_config_size = ALIGN(det_config_size, 4);
p_hw_ses->nonpersistent_cal_size += det_config_size;
if (!disable_custom_config)
p_hw_ses->nonpersistent_cal_size +=
sizeof(struct gcs_det_engine_custom_config_param) +
sizeof(struct st_hist_buffer_info);
} else {
/*
* This logic is for the legacy opaque data or no opaque data at all.
* At most, the custom config detection engine payload will be filled
* from the opaque data.
*/
if (p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].module_id &&
p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].param_id &&
rc_config->data_size) {
custom_config_size =
sizeof(struct gcs_det_engine_custom_config_param) +
rc_config->data_size;
custom_config_size = ALIGN(custom_config_size, 4);
p_hw_ses->nonpersistent_cal_size += custom_config_size;
}
if (v_info->is_qcva_uuid || v_info->is_qcmd_uuid) {
det_config_size = sizeof(struct gcs_det_engine_config_param) +
sthw_cfg->num_conf_levels;
/* If not using custom config param, send opaque data as part of
* DETECTION_ENGINE_CONFIG. Opaque data will be put after confidence
* level payload data.
*/
if (rc_config->data_size && !custom_config_size)
det_config_size += rc_config->data_size;
det_config_size = ALIGN(det_config_size, 4);
p_hw_ses->nonpersistent_cal_size += det_config_size;
}
}
if (p_hw_ses->gcs_usecase->params[DET_EVENT_TYPE].module_id &&
p_hw_ses->gcs_usecase->params[DET_EVENT_TYPE].param_id) {
/* MD module on DSP by default sends generic event.
* So no need to explicitly request the DSP.
* Just set this flag to handle generic detection event coming from DSP
*/
p_ses->is_generic_event = true;
if (v_info->is_qcva_uuid) {
det_event_type_size +=
sizeof(struct gcs_det_event_type_custom_config);
det_event_type_size = ALIGN(det_event_type_size, 4);
p_hw_ses->nonpersistent_cal_size += det_event_type_size;
}
}
if (p_hw_ses->gcs_usecase->params[START_ENGINE].module_id &&
p_hw_ses->gcs_usecase->params[START_ENGINE].param_id) {
start_custom_config_size +=
sizeof(struct gcs_det_engine_start_custom_config);
/* API size is already 4 byte aligned */
p_hw_ses->nonpersistent_cal_size += start_custom_config_size;
}
if (p_hw_ses->nonpersistent_cal_size) {
p_hw_ses->nonpersistent_cal = calloc(1, p_hw_ses->nonpersistent_cal_size);
if (!p_hw_ses->nonpersistent_cal) {
ALOGE("%s: failed to alloc nonpersistent cal ", __func__);
status = -ENOMEM;
goto err_exit;
}
msg_offset = p_hw_ses->nonpersistent_cal;
if (det_config_size) {
p_msg = (struct gcs_det_engine_config_param *)msg_offset;
p_msg->cal_hdr.module_id =
p_hw_ses->gcs_usecase->params[CONFIDENCE_LEVELS].module_id;
p_msg->cal_hdr.instance_id =
p_hw_ses->gcs_usecase->params[CONFIDENCE_LEVELS].instance_id;
p_msg->cal_hdr.param_id =
p_hw_ses->gcs_usecase->params[CONFIDENCE_LEVELS].param_id;
ALOGV("%s: nonpersistent cal header MID %x, IID %x, PID %x",
__func__, p_msg->cal_hdr.module_id,
p_msg->cal_hdr.instance_id, p_msg->cal_hdr.param_id);
p_msg->cal_hdr.size = det_config_size -
sizeof(struct graphite_cal_header);
/*
* SVA doesn't support per keyword recogntion mode.
* use the per soundmodel recognition mode
*/
if (recognition_mode & RECOGNITION_MODE_VOICE_TRIGGER) {
p_msg->mode = GCS_DETECTION_ENGINE_CONFIG_KW_DET_ENABLE;
if (recognition_mode & RECOGNITION_MODE_USER_IDENTIFICATION)
p_msg->mode |= GCS_DETECTION_ENGINE_CONFIG_USER_VER_ENABLE;
if (p_ses->stdev->detect_failure)
p_msg->mode |=
GCS_DETECTION_ENGINE_CONFIG_FAILURE_DET_ENABLE;
} else {
ALOGE("%s: Unknown recognition mode %d", __func__,
recognition_mode);
status = -EINVAL;
goto err_exit;
}
msg_offset += sizeof(struct gcs_det_engine_config_param);
if (sthw_cfg->conf_levels) {
memcpy(msg_offset, (uint8_t *)sthw_cfg->conf_levels,
sthw_cfg->num_conf_levels);
msg_offset += sthw_cfg->num_conf_levels;
p_msg->custom_payload_sz = sthw_cfg->num_conf_levels;
/*
* The detection_engine_config struct has 2 bytes for minor
* version and num_active_models before the confidence levels.
* There is also 1 byte added for each confidence level as an
* enable/disable flag.
*/
for (i = 0; i < (sthw_cfg->num_conf_levels - 2) / 2; i++) {
ALOGD("%s: First stage conf_levels[%d] = %d",
__func__, i, *(sthw_cfg->conf_levels + 2 + i));
}
}
}
/* Start at 4 byte aligned custom config param header */
msg_offset = (uint8_t *)ALIGN((size_t)msg_offset, 4);
if (rc_config->data_size && !disable_custom_config) {
if (p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].module_id &&
p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].param_id) {
cc_msg = (struct gcs_det_engine_custom_config_param *)msg_offset;
cc_msg->cal_hdr.module_id =
p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].module_id;
cc_msg->cal_hdr.instance_id =
p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].instance_id;
cc_msg->cal_hdr.param_id =
p_hw_ses->gcs_usecase->params[CUSTOM_CONFIG].param_id;
ALOGV("%s: custom config header MID %x, IID %x, PID %x", __func__,
cc_msg->cal_hdr.module_id, cc_msg->cal_hdr.instance_id,
cc_msg->cal_hdr.param_id);
if ((rc_config->data_size > CUSTOM_CONFIG_OPAQUE_DATA_SIZE) &&
v_info->is_qcva_uuid) {
/* Custom config for updated opaque data structure for SVA 3.0. */
cc_msg->cal_hdr.size = sizeof(struct st_hist_buffer_info);
msg_offset += sizeof(struct gcs_det_engine_custom_config_param);
st_hw_ses_get_hist_buff_payload(p_ses, (uint8_t *)msg_offset,
sizeof(struct st_hist_buffer_info));
msg_offset += sizeof(struct st_hist_buffer_info);
} else {
/* Custom config for legacy opaque data. */
ALOGV("%s: copying opaque data, size = %d", __func__,
rc_config->data_size);
cc_msg->cal_hdr.size = custom_config_size -
sizeof(struct graphite_cal_header);
msg_offset += sizeof(struct gcs_det_engine_custom_config_param);
memcpy(msg_offset, (unsigned char *)rc_config +
rc_config->data_offset, rc_config->data_size);
msg_offset += rc_config->data_size;
}
} else {
/*
* Copy opaque data as part of det_engine_config if there's no custom
* config IDs.
*/
ALOGV("%s: copying opaque data, size = %d", __func__,
rc_config->data_size);
memcpy(msg_offset, (unsigned char *)rc_config +
rc_config->data_offset, rc_config->data_size);
msg_offset += rc_config->data_size;
}
}
if (start_custom_config_size) {
/* Start at 4 byte aligned start engine param header */
msg_offset = (uint8_t *)ALIGN((size_t)msg_offset, 4);
stcfg_msg = (struct gcs_det_engine_start_custom_config *)msg_offset;
stcfg_msg->cal_hdr.module_id =
p_hw_ses->gcs_usecase->params[START_ENGINE].module_id;
stcfg_msg->cal_hdr.instance_id =
p_hw_ses->gcs_usecase->params[START_ENGINE].instance_id;
stcfg_msg->cal_hdr.param_id =
p_hw_ses->gcs_usecase->params[START_ENGINE].param_id;
stcfg_msg->cal_hdr.size = start_custom_config_size -
sizeof(struct graphite_cal_header);
stcfg_msg->minor_version = 0x01;
stcfg_msg->enable = 1;
stcfg_msg->token = android_atomic_inc(&p_ses->stdev->gcs_token);
p_hw_ses->start_engine_cal = stcfg_msg;
p_hw_ses->start_engine_cal_size = start_custom_config_size;
ALOGV("%s: start VA engine MID 0x%x, IID 0x%x, PID 0x%x,"
"enable %d, token %d", __func__, stcfg_msg->cal_hdr.module_id,
stcfg_msg->cal_hdr.instance_id, stcfg_msg->cal_hdr.param_id,
stcfg_msg->enable, stcfg_msg->token);
msg_offset += sizeof(struct gcs_det_engine_start_custom_config);
}
if (det_event_type_size) {
/* Start at 4 byte aligned start engine param header */
msg_offset = (uint8_t *)ALIGN((size_t)msg_offset, 4);
det_event_msg = (struct gcs_det_event_type_custom_config *)msg_offset;
det_event_msg->cal_hdr.module_id =
p_hw_ses->gcs_usecase->params[DET_EVENT_TYPE].module_id;
det_event_msg->cal_hdr.instance_id =
p_hw_ses->gcs_usecase->params[DET_EVENT_TYPE].instance_id;
det_event_msg->cal_hdr.param_id =
p_hw_ses->gcs_usecase->params[DET_EVENT_TYPE].param_id;
det_event_msg->cal_hdr.size = det_event_type_size -
sizeof(struct graphite_cal_header);
det_event_msg->minor_version = 0x01;
det_event_msg->event_type = 1;
if (v_info->is_qcmd_uuid)
det_event_msg->mode = 1;
else
det_event_msg->mode = 3;
ALOGV("%s: Detection event type MID 0x%x, IID 0x%x, PID 0x%x,"
"event_type %d, mode %d", __func__, det_event_msg->cal_hdr.module_id,
det_event_msg->cal_hdr.instance_id, det_event_msg->cal_hdr.param_id,
det_event_msg->event_type, det_event_msg->mode);
}
}
if (capture_requested) {
int circ_buff_sz = 0;
/* allocate buffers used for LAB transfer*/
rt_bytes_one_sec = (p_ses->config.rate * p_ses->config.channels *
(pcm_format_to_bits(p_ses->config.format) >> 3));
circ_buff_sz = ((v_info->kw_duration +
v_info->client_capture_read_delay) * rt_bytes_one_sec) / 1000;
p_ses->buffer = st_buffer_init(circ_buff_sz);
if (!p_ses->buffer) {
ALOGE("%s: failed to allocate circ buffer", __func__);
status = -ENOMEM;
goto err_exit;
}
if (v_info->kw_capture_format & MULAW) {
/* Allocate 2 times the requested compressed input size */
p_hw_ses->mulaw_op_buf = calloc(1, ST_GCS_READ_BUF_SIZE << 1);
if (!p_hw_ses->mulaw_op_buf) {
ALOGE("%s: failed to allocate mulaw buffer sz %d", __func__,
ST_GCS_READ_BUF_SIZE << 1);
status = -ENOMEM;
goto err_exit;
}
}
}
return status;
err_exit: