Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 1 | /* 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 Male | d409e41 | 2019-12-09 14:50:48 -0800 | [diff] [blame] | 6 | * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved. |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 7 | * |
| 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 | |
| 73 | typedef 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 | |
| 82 | typedef 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 | |
| 91 | struct 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 */ |
| 99 | typedef int (*g722_init_decoder_t)(void *decoder_inp); |
| 100 | typedef int (*g722_dec_get_total_byte_size_t)(int *total_byte_size); |
| 101 | typedef int (*g722_dec_process_t)(short *in_buf, short *out_buf, |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 102 | int in_bytes, int *out_samples, |
| 103 | void *decoder_inp); |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 104 | |
| 105 | /* MULAW decoder APIs */ |
| 106 | typedef int (*mulaw_dec_process_t)(short *out_buf, char *in_buf, |
| 107 | unsigned int in_bytes); |
| 108 | |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 109 | /* Listen Sound Model Library APIs */ |
| 110 | #include "ListenSoundModelLib.h" |
| 111 | |
| 112 | typedef listen_status_enum (*smlib_getSoundModelHeader_t) |
| 113 | ( |
| 114 | listen_model_type *pSoundModel, |
| 115 | listen_sound_model_header *pListenSoundModelHeader |
| 116 | ); |
| 117 | |
| 118 | typedef listen_status_enum (*smlib_releaseSoundModelHeader_t) |
| 119 | ( |
| 120 | listen_sound_model_header *pListenSoundModelHeader |
| 121 | ); |
| 122 | |
| 123 | typedef listen_status_enum (*smlib_getKeywordPhrases_t) |
| 124 | ( |
| 125 | listen_model_type *pSoundModel, |
| 126 | uint16_t *numKeywords, |
| 127 | keywordId_t *keywords |
| 128 | ); |
| 129 | |
| 130 | typedef listen_status_enum (*smlib_getUserNames_t) |
| 131 | ( |
| 132 | listen_model_type *pSoundModel, |
| 133 | uint16_t *numUsers, |
| 134 | userId_t *users |
| 135 | ); |
| 136 | |
| 137 | typedef listen_status_enum (*smlib_getMergedModelSize_t) |
| 138 | ( |
| 139 | uint16_t numModels, |
| 140 | listen_model_type *pModels[], |
| 141 | uint32_t *nOutputModelSize |
| 142 | ); |
| 143 | |
| 144 | typedef listen_status_enum (*smlib_mergeModels_t) |
| 145 | ( |
| 146 | uint16_t numModels, |
| 147 | listen_model_type *pModels[], |
| 148 | listen_model_type *pMergedModel |
| 149 | ); |
| 150 | |
| 151 | typedef 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 | |
| 159 | typedef 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 167 | struct 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 Male | d409e41 | 2019-12-09 14:50:48 -0800 | [diff] [blame] | 180 | struct listnode available_devices; |
Quinn Male | dd10508 | 2019-11-07 18:36:02 -0800 | [diff] [blame] | 181 | int *dev_enable_cnt; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 182 | 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 Song | d18c671 | 2019-06-10 11:20:10 +0800 | [diff] [blame] | 216 | bool dedicated_headset_path; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 217 | bool disable_hwmad; |
Quinn Male | 3d7d9d4 | 2019-05-20 13:35:01 -0700 | [diff] [blame] | 218 | st_platform_lpi_enable_t platform_lpi_enable; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 219 | |
| 220 | unsigned int rx_conc_max_st_ses; |
| 221 | struct use_case_info *ape_pcm_use_cases; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 222 | 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 237 | |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 238 | 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 247 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 248 | void *mulaw_dec_lib_handle; |
| 249 | mulaw_dec_process_t mulaw_dec_process; |
| 250 | |
| 251 | struct listnode adm_cfg_list; |
Quinn Male | d409e41 | 2019-12-09 14:50:48 -0800 | [diff] [blame] | 252 | struct listnode ec_ref_dev_list; |
| 253 | struct listnode active_rx_dev_list; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 254 | |
| 255 | bool ssr_offline_received; |
| 256 | int lpma_handle; |
| 257 | bool is_charging; |
Quinn Male | aba13db | 2019-07-11 15:52:14 -0700 | [diff] [blame] | 258 | bool enable_debug_dumps; |
Quinn Male | cc1affd | 2019-07-18 16:13:31 -0700 | [diff] [blame] | 259 | bool support_barge_in_mode; |
| 260 | bool support_dynamic_ec_update; |
| 261 | bool screen_off; |
| 262 | bool barge_in_mode; |
Zhou Song | ea5a0b4 | 2020-01-06 18:16:29 +0800 | [diff] [blame] | 263 | int ec_reset_pending_cnt; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | typedef struct sound_trigger_device sound_trigger_device_t; |
| 267 | |
| 268 | st_session_t* get_sound_trigger_session |
| 269 | ( |
| 270 | struct sound_trigger_device *stdev, |
| 271 | sound_model_handle_t sound_model_handle |
| 272 | ); |
| 273 | |
Quinn Male | d409e41 | 2019-12-09 14:50:48 -0800 | [diff] [blame] | 274 | struct audio_device_info { |
| 275 | struct listnode list; |
| 276 | audio_devices_t type; |
| 277 | char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; |
| 278 | }; |
| 279 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 280 | void update_hw_mad_exec_mode(st_exec_mode_t mode, st_profile_type_t profile_type); |
| 281 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 282 | #define ST_DEBUG_DUMP_LOCATION "/data/vendor/audio" |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 283 | #define ST_DBG_DECLARE(args...) args |
| 284 | |
| 285 | #define ST_DBG_FILE_OPEN_WR(fptr, fpath, fname, fextn, fcount) \ |
| 286 | do {\ |
| 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) \ |
| 298 | do {\ |
| 299 | if (fptr) { fclose (fptr); }\ |
| 300 | } while (0) |
| 301 | |
| 302 | #define ST_DBG_FILE_WRITE(fptr, buf, buf_size) \ |
| 303 | do {\ |
| 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 Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 313 | |
| 314 | int dbg_trace_max_lab_reads; |
| 315 | #define ST_DBG_ATRACE_ASYNC_BEGIN_IF(cond, name, cookie)\ |
| 316 | do {\ |
| 317 | if (cond) ATRACE_ASYNC_BEGIN(name, cookie);\ |
| 318 | } while(0) |
| 319 | |
| 320 | #define ST_DBG_ATRACE_ASYNC_END_IF(cond, name, cookie)\ |
| 321 | do {\ |
| 322 | if (cond) ATRACE_ASYNC_END(name, cookie);\ |
| 323 | } while(0) |
| 324 | |
| 325 | static 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 */ |