Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 1 | /* st_hw_session_lsm.h |
| 2 | * |
Quinn Male | 9a34552 | 2020-03-12 17:49:25 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016-2020, The Linux Foundation. All rights reserved. |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * * Neither the name of The Linux Foundation nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | #include <pthread.h> |
| 31 | #include <tinyalsa/asoundlib.h> |
| 32 | #include <audio_route/audio_route.h> |
| 33 | #include <sound/lsm_params.h> |
Quinn Male | 5874945 | 2020-03-26 17:14:56 -0700 | [diff] [blame] | 34 | #include <sound/asound.h> |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 35 | |
| 36 | #include "sound_trigger_platform.h" |
| 37 | #include "st_hw_session.h" |
| 38 | #include "st_common_defs.h" |
| 39 | |
| 40 | struct sound_trigger_device; |
| 41 | |
| 42 | #define SOUND_TRIGGER_MAX_EVNT_PAYLOAD_SIZE (256) |
Quinn Male | aba13db | 2019-07-11 15:52:14 -0700 | [diff] [blame] | 43 | |
Quinn Male | 5874945 | 2020-03-26 17:14:56 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Add extra to accomodate multiple LSM_CUSTOM_PARAMS and also |
| 46 | * multiple LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS params for |
| 47 | * multi SM usecase. |
| 48 | */ |
| 49 | #define LSM_SM_PARAMS_INFO_MAX (LSM_PARAMS_MAX * 2) |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 50 | |
| 51 | #define SOUND_TRIGGER_PCM_MAX_RETRY (10) |
| 52 | #define SOUND_TRIGGER_PCM_SLEEP_WAIT (1000) |
| 53 | |
| 54 | #define SOUND_TRIGGER_APE_PERIOD_COUNT (4) |
| 55 | #define SOUND_TRIGGER_APE_PERIOD_SIZE \ |
| 56 | ((((SOUND_TRIGGER_SAMPLING_RATE_16000 * SOUND_TRIGGER_APE_BUFFER_DURATION_MS) \ |
| 57 | /(SOUND_TRIGGER_APE_PERIOD_COUNT * 1000)) + 0x1f) & ~0x1f) |
| 58 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 59 | /* |
Quinn Male | d0814de | 2019-05-29 17:33:22 -0700 | [diff] [blame] | 60 | * The chosen theshold for determining FTRT vs. RT data is one tenth of the |
| 61 | * buffer duration. There can be frames received that are partially FTRT and |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 62 | * partially RT, so the threshold should be less than the full buffer duration |
| 63 | * to account for that usecase. However, if the threshold is too small, then |
| 64 | * some issue in the lower layers could lead to false identification of RT |
| 65 | * data. |
| 66 | */ |
| 67 | #define APE_MAX_LAB_FTRT_FRAME_RD_TIME_NS \ |
| 68 | ((SOUND_TRIGGER_APE_BUFFER_DURATION_MS * NSECS_PER_MSEC)\ |
Quinn Male | d0814de | 2019-05-29 17:33:22 -0700 | [diff] [blame] | 69 | / (10 * SOUND_TRIGGER_APE_PERIOD_COUNT)) |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 70 | |
| 71 | #define LSM_ABORT_RETRY_COUNT (5) |
| 72 | #define LSM_ABORT_WAIT_TIMEOUT_NS (30 * NSECS_PER_MSEC) |
| 73 | |
Quinn Male | 5874945 | 2020-03-26 17:14:56 -0700 | [diff] [blame] | 74 | #define MAX_MULTI_SOUND_MODELS (8) |
| 75 | #define MAX_MULTI_SM_CONF_LEVELS (8) |
| 76 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 77 | #ifdef LSM_EVENT_TIMESTAMP_MODE_SUPPORT |
| 78 | typedef struct snd_lsm_event_status_v3 st_lsm_event_status_t; |
| 79 | #else |
| 80 | typedef struct snd_lsm_event_status st_lsm_event_status_t; |
| 81 | #endif |
| 82 | |
| 83 | #ifdef LSM_POLLING_ENABLE_SUPPORT |
| 84 | typedef struct snd_lsm_poll_enable st_lsm_poll_enable_t; |
| 85 | #else |
| 86 | typedef void* st_lsm_poll_enable_t; |
| 87 | #endif |
| 88 | |
| 89 | #ifdef LSM_DET_EVENT_TYPE_GENERIC |
| 90 | typedef struct snd_lsm_det_event_type st_lsm_det_event_type_t; |
| 91 | #else |
| 92 | typedef void* st_lsm_det_event_type_t; |
| 93 | #endif |
| 94 | |
Quinn Male | 5874945 | 2020-03-26 17:14:56 -0700 | [diff] [blame] | 95 | struct multi_sm_conf_levels { |
| 96 | uint32_t model_id; |
| 97 | uint32_t num_conf_levels; |
| 98 | uint32_t conf_levels[MAX_MULTI_SM_CONF_LEVELS]; |
| 99 | }; |
| 100 | |
| 101 | struct multi_sm_reg_sm { |
| 102 | uint32_t model_id; |
| 103 | uint32_t model_size; |
| 104 | }; |
| 105 | |
| 106 | #if (SNDRV_LSM_VERSION >= SNDRV_PROTOCOL_VERSION(0, 3, 2)) |
| 107 | #define LSM_MULTI_SM_SUPPORT (1) |
| 108 | #else |
| 109 | #define LSM_MULTI_SM_SUPPORT (0) |
| 110 | #endif |
| 111 | |
| 112 | #ifdef LSM_MULTI_SM_SUPPORT |
| 113 | typedef struct multi_sm_reg_sm st_lsm_reg_sm_header_t; |
| 114 | typedef struct multi_sm_conf_levels st_lsm_conf_levels_t; |
| 115 | #else |
| 116 | typedef void* st_lsm_reg_sm_header_t; |
| 117 | typedef void* st_lsm_conf_levels_t; |
| 118 | #endif |
| 119 | |
| 120 | #define DET_EVENT_CONFIDENCE_LEVELS_BIT (1 << 0) |
| 121 | #define DET_EVENT_KEYWORD_INDEX_BIT (1 << 1) |
| 122 | #define DET_EVENT_TIMESTAMP_INFO_BIT (1 << 2) |
| 123 | #define DET_EVENT_MULTI_MODEL_RESULT_INFO_BIT (1 << 4) |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 124 | |
| 125 | struct lsm_param_smm_th_config { |
| 126 | uint32_t minor_version; |
| 127 | uint32_t smm_threshold; |
| 128 | }__packed; |
| 129 | |
| 130 | struct lsm_setparam_payload { |
| 131 | uint32_t data_payload_size; |
| 132 | uint32_t data_payload_addr_lsw; |
| 133 | uint32_t data_payload_addr_msw; |
| 134 | uint32_t mem_map_handle; |
| 135 | }__packed; |
| 136 | |
| 137 | struct lsm_param_payload { |
| 138 | uint32_t module_id; |
| 139 | uint32_t param_id; |
| 140 | uint32_t p_size; |
| 141 | }__packed; |
| 142 | |
| 143 | struct lsm_param_payload_v2 { |
| 144 | uint32_t module_id; |
| 145 | uint16_t instance_id; |
| 146 | uint16_t reserved; |
| 147 | uint32_t param_id; |
| 148 | uint32_t p_size; |
| 149 | }__packed; |
| 150 | |
| 151 | struct lsm_param_custom_config { |
| 152 | struct lsm_setparam_payload set_param; |
| 153 | struct lsm_param_payload common; |
| 154 | }__packed; |
| 155 | |
Harshal Ahire | a766eeb | 2020-07-13 02:38:14 +0530 | [diff] [blame] | 156 | struct version_arch_payload { |
| 157 | unsigned int version; |
| 158 | char arch[64]; |
| 159 | }__packed; |
| 160 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 161 | struct st_hw_session_lsm { |
| 162 | st_hw_session_t common; |
Quinn Male | 8b2cb79 | 2020-07-13 14:41:32 -0700 | [diff] [blame] | 163 | struct st_lsm_params lsm_usecase; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 164 | pthread_t callback_thread; |
| 165 | bool exit_callback_thread; |
| 166 | |
Quinn Male | 48490df | 2020-03-25 10:25:42 -0700 | [diff] [blame] | 167 | pthread_t buffer_thread; |
| 168 | bool exit_buffer_thread; |
| 169 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 170 | int pcm_id; |
| 171 | struct pcm *pcm; |
| 172 | struct pcm_config lab_config; |
| 173 | |
| 174 | bool use_topology_intf; |
| 175 | |
| 176 | struct st_hw_session *transit_ses; |
| 177 | |
| 178 | /* Buffers used in FTRT mode */ |
| 179 | unsigned char *lab_drv_buf; /* small buffer to hold one chunk received from |
| 180 | pcm read */ |
| 181 | unsigned int lab_drv_buf_size; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 182 | unsigned int unread_bytes; |
| 183 | pthread_cond_t cond; |
| 184 | pthread_mutex_t lock; |
| 185 | |
| 186 | bool exit_lab_processing; |
| 187 | bool lab_processing_active; |
| 188 | pthread_cond_t callback_thread_cond; |
| 189 | pthread_mutex_t callback_thread_lock; |
| 190 | |
| 191 | bool lab_buffers_allocated; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 192 | bool lab_on_detection; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 193 | |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 194 | unsigned int bytes_written; |
| 195 | long first_stage_det_event_timestamp; |
| 196 | bool move_client_ptr; |
| 197 | uint16_t num_stages; |
| 198 | |
| 199 | char *use_case; |
| 200 | char *port_ctrl; |
| 201 | }; |
| 202 | |
| 203 | typedef struct st_hw_session_lsm st_hw_session_lsm_t; |
| 204 | |
| 205 | int st_hw_sess_lsm_init(st_hw_session_t *const p_ses, |
| 206 | hw_ses_event_callback_t cb, void *cookie, st_exec_mode_t exec_mode, |
| 207 | struct st_vendor_info *v_info, sound_model_handle_t sm_handle, |
| 208 | struct sound_trigger_device *stdev); |
| 209 | void st_hw_sess_lsm_deinit(st_hw_session_t *const p_ses); |