blob: 85b33ebe3688c736785b7ea9af3127248f1fe2a4 [file] [log] [blame]
Quinn Male2e883752019-03-22 11:28:54 -07001/* sound_trigger_hw.h
2 *
3 * This file contains the API to load sound models with
4 * DSP and start/stop detection of associated key phrases.
5 *
Quinn Maled409e412019-12-09 14:50:48 -08006 * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
Quinn Male2e883752019-03-22 11:28:54 -07007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of The Linux Foundation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef SOUND_TRIGGER_HW_H
35#define SOUND_TRIGGER_HW_H
36
37#include <stdlib.h>
38#include <cutils/list.h>
39#include <pthread.h>
40#include <errno.h>
41#include <hardware/sound_trigger.h>
42#include <tinyalsa/asoundlib.h>
43#include <audio_route/audio_route.h>
44#include <cutils/properties.h>
45
46#include "sound_trigger_prop_intf.h"
47#include "sound_trigger_platform.h"
48#include "st_common_defs.h"
49#include "st_session.h"
50#include "st_second_stage.h"
51#include "sml_model_parser.h"
52
53#define AUDIO_HAL_NAME_PREFIX "audio.primary"
54
55#if LINUX_ENABLED
56 #ifdef __LP64__
57 #define AUDIO_HAL_LIBRARY_PATH1 "/usr/lib64"
58 #define AUDIO_HAL_LIBRARY_PATH2 AUDIO_HAL_LIBRARY_PATH1
59 #else
60 #define AUDIO_HAL_LIBRARY_PATH1 "/usr/lib"
61 #define AUDIO_HAL_LIBRARY_PATH2 AUDIO_HAL_LIBRARY_PATH1
62 #endif
63#else
64 #ifdef __LP64__
65 #define AUDIO_HAL_LIBRARY_PATH1 "/vendor/lib64/hw"
66 #define AUDIO_HAL_LIBRARY_PATH2 "/system/lib64/hw"
67 #else
68 #define AUDIO_HAL_LIBRARY_PATH1 "/vendor/lib/hw"
69 #define AUDIO_HAL_LIBRARY_PATH2 "/system/lib/hw"
70 #endif
71#endif
72
73typedef enum {
74 ST_DEVICE_SSR_APE = 0x1,
75 ST_DEVICE_SSR_CPE = 0x2
76}st_ssr_state_t;
77
78#define TRANSIT_WAKE_LOCK_NAME "sthal_transit_wake_lock"
79#define TRANSIT_SSR_TIMEOUT_SEC (10)
80#define TRANSIT_MIN_SLEEP_TIME_SEC (1)
81
82typedef enum {
83 TRANSIT_NONE,
84 TRANSIT_CPE_TO_APE,
85 TRANSIT_APE_TO_CPE
86}transit_dir_t;
87
88#define USECASE_STRING_SIZE (50)
89#define DEFAULT_NUM_LAB_READS (16)
90
91struct use_case_info {
92 bool active;
93 int pcm_id;
94 int pcm_back_end;
95 char use_case[USECASE_STRING_SIZE];
96};
97
98/* ADPCM decoder APIs */
99typedef int (*g722_init_decoder_t)(void *decoder_inp);
100typedef int (*g722_dec_get_total_byte_size_t)(int *total_byte_size);
101typedef int (*g722_dec_process_t)(short *in_buf, short *out_buf,
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700102 int in_bytes, int *out_samples,
103 void *decoder_inp);
Quinn Male2e883752019-03-22 11:28:54 -0700104
105/* MULAW decoder APIs */
106typedef int (*mulaw_dec_process_t)(short *out_buf, char *in_buf,
107 unsigned int in_bytes);
108
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700109/* Listen Sound Model Library APIs */
110#include "ListenSoundModelLib.h"
111
112typedef listen_status_enum (*smlib_getSoundModelHeader_t)
113(
114 listen_model_type *pSoundModel,
115 listen_sound_model_header *pListenSoundModelHeader
116);
117
118typedef listen_status_enum (*smlib_releaseSoundModelHeader_t)
119(
120 listen_sound_model_header *pListenSoundModelHeader
121);
122
123typedef listen_status_enum (*smlib_getKeywordPhrases_t)
124(
125 listen_model_type *pSoundModel,
126 uint16_t *numKeywords,
127 keywordId_t *keywords
128);
129
130typedef listen_status_enum (*smlib_getUserNames_t)
131(
132 listen_model_type *pSoundModel,
133 uint16_t *numUsers,
134 userId_t *users
135);
136
137typedef listen_status_enum (*smlib_getMergedModelSize_t)
138(
139 uint16_t numModels,
140 listen_model_type *pModels[],
141 uint32_t *nOutputModelSize
142);
143
144typedef listen_status_enum (*smlib_mergeModels_t)
145(
146 uint16_t numModels,
147 listen_model_type *pModels[],
148 listen_model_type *pMergedModel
149);
150
151typedef listen_status_enum (*getSizeAfterDeleting_t)
152(
153 listen_model_type *pInputModel,
154 keywordId_t keywordId,
155 userId_t userId,
156 uint32_t *nOutputModelSize
157);
158
159typedef listen_status_enum (*deleteFromModel_t)
160(
161 listen_model_type *pInputModel,
162 keywordId_t keywordId,
163 userId_t userId,
164 listen_model_type *pResultModel
165);
166
Quinn Male2e883752019-03-22 11:28:54 -0700167struct sound_trigger_device {
168 struct sound_trigger_hw_device device;
169 struct sound_trigger_properties *hw_properties;
170 pthread_mutex_t lock;
171 pthread_cond_t cond;
172 struct listnode sound_model_list;
173 struct mixer *mixer;
174 struct audio_route *audio_route;
175 int snd_card;
176 volatile int session_id;
177 volatile int gcs_token;
178 pthread_mutex_t ref_cnt_lock;
179 int *dev_ref_cnt;
Quinn Maled409e412019-12-09 14:50:48 -0800180 struct listnode available_devices;
Quinn Maledd105082019-11-07 18:36:02 -0800181 int *dev_enable_cnt;
Quinn Male2e883752019-03-22 11:28:54 -0700182 pthread_t transitions_thread;
183 pthread_cond_t transitions_cond;
184
185 unsigned int tx_concurrency_active;
186 unsigned int rx_concurrency_active;
187 bool reset_backend;
188 bool conc_capture_supported;
189 bool conc_voice_call_supported;
190 bool conc_voice_active;
191 bool conc_voip_call_supported;
192 bool conc_voip_active;
193 bool session_allowed;
194 bool rx_concurrency_disabled;
195 bool sw_mad;
196 bool bg_kwd;
197 int num_sessions_configured;
198 unsigned int max_cpe_sessions;
199 unsigned int avail_cpe_phrases;
200 unsigned int avail_cpe_users;
201 unsigned int max_ape_sessions;
202 unsigned int avail_ape_phrases;
203 unsigned int avail_ape_users;
204 unsigned int max_wdsp_sessions;
205 unsigned int max_arm_sessions;
206 bool support_dev_switch;
207 bool transit_to_adsp_on_playback;
208 bool transit_to_adsp_on_battery_charging;
209 bool transit_to_non_lpi_on_battery_charging;
210 bool lpi_enable;
211 bool vad_enable;
212 int transit_wait_time;
213 bool stop_transitions_thread_loop;
214 transit_dir_t transit_dir;
215 bool dedicated_sva_path;
Zhou Songd18c6712019-06-10 11:20:10 +0800216 bool dedicated_headset_path;
Quinn Male2e883752019-03-22 11:28:54 -0700217 bool disable_hwmad;
Quinn Male3d7d9d42019-05-20 13:35:01 -0700218 st_platform_lpi_enable_t platform_lpi_enable;
Quinn Male2e883752019-03-22 11:28:54 -0700219
220 unsigned int rx_conc_max_st_ses;
221 struct use_case_info *ape_pcm_use_cases;
Quinn Male2e883752019-03-22 11:28:54 -0700222 struct use_case_info *arm_pcm_use_cases;
223 bool detect_failure;
224 void *platform;
225 st_hw_type_t hw_type;
226 st_exec_mode_t exec_mode;
227 st_exec_mode_t client_req_exec_mode;
228 int client_req_exec_mode_status;
229
230 bool audio_ec_enabled;
231 void *audio_hal_handle;
232 audio_hw_call_back_t audio_hal_cb;
233 unsigned int sthal_prop_api_version;
234 bool is_gcs;
235
236 struct listnode vendor_uuid_list;
Quinn Male2e883752019-03-22 11:28:54 -0700237
Venkatesh Mangalappalie4f17532019-06-04 16:30:28 -0700238 void *smlib_handle;
239 smlib_getSoundModelHeader_t smlib_getSoundModelHeader;
240 smlib_releaseSoundModelHeader_t smlib_releaseSoundModelHeader;
241 smlib_getKeywordPhrases_t smlib_getKeywordPhrases;
242 smlib_getUserNames_t smlib_getUserNames;
243 smlib_getMergedModelSize_t smlib_getMergedModelSize;
244 smlib_mergeModels_t smlib_mergeModels;
245 getSizeAfterDeleting_t smlib_getSizeAfterDeleting;
246 deleteFromModel_t smlib_deleteFromModel;
Quinn Male2e883752019-03-22 11:28:54 -0700247
Quinn Male2e883752019-03-22 11:28:54 -0700248 void *mulaw_dec_lib_handle;
249 mulaw_dec_process_t mulaw_dec_process;
250
251 struct listnode adm_cfg_list;
Quinn Maled409e412019-12-09 14:50:48 -0800252 struct listnode ec_ref_dev_list;
253 struct listnode active_rx_dev_list;
Quinn Male2e883752019-03-22 11:28:54 -0700254
255 bool ssr_offline_received;
256 int lpma_handle;
257 bool is_charging;
Quinn Maleaba13db2019-07-11 15:52:14 -0700258 bool enable_debug_dumps;
Quinn Malecc1affd2019-07-18 16:13:31 -0700259 bool support_barge_in_mode;
260 bool support_dynamic_ec_update;
261 bool screen_off;
262 bool barge_in_mode;
Zhou Songea5a0b42020-01-06 18:16:29 +0800263 int ec_reset_pending_cnt;
Quinn Male2e883752019-03-22 11:28:54 -0700264};
265
266typedef struct sound_trigger_device sound_trigger_device_t;
267
268st_session_t* get_sound_trigger_session
269(
270 struct sound_trigger_device *stdev,
271 sound_model_handle_t sound_model_handle
272);
273
Quinn Maled409e412019-12-09 14:50:48 -0800274struct audio_device_info {
275 struct listnode list;
276 audio_devices_t type;
277 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
278};
279
Quinn Male2e883752019-03-22 11:28:54 -0700280void update_hw_mad_exec_mode(st_exec_mode_t mode, st_profile_type_t profile_type);
281
Quinn Male2e883752019-03-22 11:28:54 -0700282#define ST_DEBUG_DUMP_LOCATION "/data/vendor/audio"
Quinn Male2e883752019-03-22 11:28:54 -0700283#define ST_DBG_DECLARE(args...) args
284
285#define ST_DBG_FILE_OPEN_WR(fptr, fpath, fname, fextn, fcount) \
286do {\
287 char fptr_fn[100];\
288\
289 snprintf(fptr_fn, sizeof(fptr_fn), fpath"/"fname"_%d."fextn, fcount);\
290 fptr = fopen(fptr_fn, "wb");\
291 if (!fptr) { \
292 ALOGE("%s: File open failed %s: %s", \
293 __func__, fptr_fn, strerror(errno)); \
294 } \
295} while (0)
296
297#define ST_DBG_FILE_CLOSE(fptr) \
298do {\
299 if (fptr) { fclose (fptr); }\
300} while (0)
301
302#define ST_DBG_FILE_WRITE(fptr, buf, buf_size) \
303do {\
304 if (fptr) {\
305 size_t ret_bytes = fwrite(buf, 1, buf_size, fptr);\
306 if (ret_bytes != (size_t)buf_size) {\
307 ALOGE("%s: fwrite %zu < %zu", __func__,\
308 ret_bytes, (size_t)buf_size);\
309 }\
310 fflush(fptr);\
311 }\
312} while (0)
Quinn Male2e883752019-03-22 11:28:54 -0700313
314int dbg_trace_max_lab_reads;
315#define ST_DBG_ATRACE_ASYNC_BEGIN_IF(cond, name, cookie)\
316do {\
317 if (cond) ATRACE_ASYNC_BEGIN(name, cookie);\
318} while(0)
319
320#define ST_DBG_ATRACE_ASYNC_END_IF(cond, name, cookie)\
321do {\
322 if (cond) ATRACE_ASYNC_END(name, cookie);\
323} while(0)
324
325static inline void dbg_trace_parse_max_lab_reads()
326{
327 char value[PROPERTY_VALUE_MAX];
328
329 if (property_get("persist.vendor.sthal.dbg_max_dsp_buf_reads", value, NULL))
330 dbg_trace_max_lab_reads = atoi(value);
331 else
332 dbg_trace_max_lab_reads = DEFAULT_NUM_LAB_READS;
333}
334
335#ifndef LINUX_ENABLED
336#include <hardware_legacy/power.h>
337#else
338#define acquire_wake_lock(a, b) (0)
339#define release_wake_lock(a) (0)
340#define PARTIAL_WAKE_LOCK 1
341#endif
342
343#endif /* SOUND_TRIGGER_HW_H */