Merge 105861cb3defb8c64b8cf18ce34fe9526b20331e on remote branch

Change-Id: Ie1c508330be08641120f35afb23a2dd2a1c000b1
diff --git a/sound_trigger_platform.c b/sound_trigger_platform.c
index c09a483..20c4a7e 100644
--- a/sound_trigger_platform.c
+++ b/sound_trigger_platform.c
@@ -5416,7 +5416,7 @@
     return ret;
 }
 
-bool platform_get_module_params_for_lsm_usecase
+static int platform_get_module_params_for_lsm_usecase
 (
     struct st_lsm_params *usecase,
     st_module_type_t sm_version
@@ -5432,18 +5432,18 @@
             memcpy((uint8_t *)usecase->params, (uint8_t *)module_info->params,
                 sizeof(struct st_module_param_info) * MAX_PARAM_IDS);
             usecase->param_tag_tracker = module_info->param_tag_tracker;
-            return true;
+            return 0;
         }
     }
 
-    return false;
+    return -EINVAL;
 }
 
-void platform_get_lsm_usecase
+int platform_get_lsm_usecase
 (
    void* platform,
    struct st_vendor_info* v_info,
-   struct st_lsm_params** lsm_usecase,
+   struct st_lsm_params* lsm_usecase,
    st_exec_mode_t exec_mode,
    bool lpi_enable,
    st_module_type_t sm_version
@@ -5455,19 +5455,18 @@
     audio_devices_t capture_device =
         platform_stdev_get_capture_device(platform);
     sound_trigger_device_t *stdev = my_data->stdev;
-    bool set_module_params = false;
+    int status = 0;
 
     ALOGV("%s: Enter", __func__);
 
     if (!v_info || !lsm_usecase) {
         ALOGE("%s: received null params", __func__);
-        return;
+        return -EINVAL;
     }
 
     if (list_empty(&v_info->lsm_usecase_list)) {
-        *lsm_usecase = NULL;
         ALOGE("%s: lsm usecase not available", __func__);
-        return;
+        return -EINVAL;
     }
 
     list_for_each_safe(lsm_node, node, &v_info->lsm_usecase_list) {
@@ -5500,39 +5499,39 @@
                        stdev->barge_in_mode)) ||
                      (usecase->lpi_enable == ST_PLATFORM_LPI_DISABLE &&
                       !lpi_enable && !stdev->barge_in_mode))) {
-                    *lsm_usecase = usecase;
                     if (my_data->xml_version == PLATFORM_XML_VERSION_0x0106) {
-                        set_module_params =
-                            platform_get_module_params_for_lsm_usecase(usecase,
-                                sm_version);
-                        if (!set_module_params) {
+                        status = platform_get_module_params_for_lsm_usecase(usecase,
+                            sm_version);
+                        if (status) {
                             ALOGE("%s: Error. No matching module info params.",
                                 __func__);
-                            return;
+                            return status;
                         }
                     }
+                    memcpy((uint8_t *)lsm_usecase, (uint8_t *)usecase,
+                        sizeof(struct st_lsm_params));
                     v_info->in_channels = usecase->in_channels;
                     v_info->fluence_type = usecase->fluence_type;
                     v_info->profile_type = usecase->adm_cfg_profile;
                     v_info->shared_buf_fmt =
                         get_shared_buf_fmt(v_info->profile_type);
-                    if (sm_version == ST_MODULE_TYPE_PDK5) {
+                    if (sm_version == ST_MODULE_TYPE_PDK5)
                         v_info->app_type = usecase->pdk5_app_type;
-                    } else {
+                    else
                         v_info->app_type = usecase->app_type;
-                    }
-                    return;
+                    return status;
                 }
             } else {
-              /* Same lsm usecase will be used for multiple sessions */
-              *lsm_usecase = usecase;
-              v_info->in_channels = usecase->in_channels;
-              return;
+                /* Same lsm usecase will be used for multiple sessions */
+                memcpy((uint8_t *)lsm_usecase, (uint8_t *)usecase,
+                    sizeof(struct st_lsm_params));
+                v_info->in_channels = usecase->in_channels;
+                return status;
             }
         }
     }
     ALOGE("%s: No lsm usecase found for exec_mode %d", __func__, exec_mode);
-    *lsm_usecase = NULL;
+    return -EINVAL;
 }
 
 int platform_stdev_get_backend_channel_count
diff --git a/sound_trigger_platform.h b/sound_trigger_platform.h
index 79df03d..93654c4 100644
--- a/sound_trigger_platform.h
+++ b/sound_trigger_platform.h
@@ -682,11 +682,11 @@
     listen_model_indicator_enum sm_id
 );
 
-void platform_get_lsm_usecase
+int platform_get_lsm_usecase
 (
    void* platform,
    struct st_vendor_info* v_info,
-   struct st_lsm_params** lsm_usecase,
+   struct st_lsm_params* lsm_usecase,
    st_exec_mode_t exec_mode,
    bool lpi_enable,
    st_module_type_t sm_version
diff --git a/st_hw_session_lsm.c b/st_hw_session_lsm.c
index 7508975..544d48a 100644
--- a/st_hw_session_lsm.c
+++ b/st_hw_session_lsm.c
@@ -167,7 +167,7 @@
     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;
+        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");
@@ -445,9 +445,9 @@
     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;
+        params.num_channels = p_lsm_ses->lsm_usecase.in_channels_lpi;
     else
-        params.num_channels = p_lsm_ses->lsm_usecase->in_channels;
+        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);
@@ -1442,7 +1442,7 @@
     struct snd_lsm_module_params lsm_params = {0};
     struct st_module_param_info *mparams = NULL;
 
-    mparams = p_ses->lsm_usecase->params;
+    mparams = p_ses->lsm_usecase.params;
     sm_header.model_id = model_id;
     sm_header.model_size = sm_size;
     sm_payload = calloc(1, sm_payload_size);
@@ -1496,10 +1496,10 @@
     memset((uint8_t *)param_info, 0, sizeof(lsm_param_info_t) *
         LSM_MAX_STAGES_PER_SESSION);
 
-    platform_get_lsm_usecase(p_ses->stdev->platform, v_info,
+    status = platform_get_lsm_usecase(p_ses->stdev->platform, v_info,
         &p_lsm_ses->lsm_usecase, p_ses->exec_mode, p_ses->lpi_enable,
         p_ses->f_stage_version);
-    if (!p_lsm_ses->lsm_usecase) {
+    if (status) {
         ALOGE("%s: couldn't get lsm usecase", __func__);
         status = -EINVAL;
         goto sm_error;
@@ -1538,7 +1538,7 @@
 
     /* Backward compatibility with previous XML version <= 0x0102 */
     int app_type = (v_info->app_type == 0) ?
-        p_lsm_ses->lsm_usecase->app_type : v_info->app_type;
+        p_lsm_ses->lsm_usecase.app_type : v_info->app_type;
 
     capture_device = platform_stdev_get_capture_device(p_ses->stdev->platform);
     status = platform_stdev_send_calibration(p_ses->stdev->platform,
@@ -1645,7 +1645,7 @@
         param_count = 0;
         stage_idx = LSM_STAGE_INDEX_FIRST;
         lsm_params.params = (unsigned char*)&param_info[0];
-        mparams = p_lsm_ses->lsm_usecase->params;
+        mparams = p_lsm_ses->lsm_usecase.params;
         param_info[param_count].param_data = sm_data;
         param_info[param_count].param_size = sm_size;
         lsm_fill_param_info(LSM_REG_SND_MODEL, &param_info[param_count++],
@@ -1676,8 +1676,8 @@
     /* Send detection event type for last stage only, if params set in config */
     mparams = NULL;
     if ((p_lsm_ses->num_stages == 1) &&
-        (p_lsm_ses->lsm_usecase->param_tag_tracker & PARAM_DET_EVENT_TYPE_BIT)) {
-        mparams = p_lsm_ses->lsm_usecase->params;
+        (p_lsm_ses->lsm_usecase.param_tag_tracker & PARAM_DET_EVENT_TYPE_BIT)) {
+        mparams = p_lsm_ses->lsm_usecase.params;
     } else if (p_lsm_ses->num_stages > 1) {
         node = list_tail(&p_ses->lsm_ss_cfg_list);
         ss_cfg = node_to_item(node, st_lsm_ss_config_t, list_node);
@@ -1734,7 +1734,7 @@
     struct snd_lsm_module_params lsm_params = {0};
     struct st_module_param_info *mparams = NULL;
 
-    mparams = p_ses->lsm_usecase->params;
+    mparams = p_ses->lsm_usecase.params;
     param_info.param_size = sizeof(model_id);
     param_info.param_data = (uint8_t *)&model_id;
 #if (SNDRV_LSM_VERSION >= SNDRV_PROTOCOL_VERSION(0, 3, 2))
@@ -1799,11 +1799,11 @@
     stage_idx = LSM_STAGE_INDEX_FIRST;
     param_count = 0;
     lsm_params.params = (unsigned char*)&param_info[0];
-    mparams = p_lsm_ses->lsm_usecase->params;
+    mparams = p_lsm_ses->lsm_usecase.params;
 
     /* reset last stage only if lab capture was set */
     if ((stage_idx == p_lsm_ses->num_stages - 1) && p_ses->lab_enabled &&
-        (p_lsm_ses->lsm_usecase->param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
+        (p_lsm_ses->lsm_usecase.param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
         param_info[param_count].param_size = sizeof(buf_en);
         param_info[param_count].param_data = (unsigned char *)&buf_en;
         lsm_fill_param_info(LSM_LAB_CONTROL, &param_info[param_count++],
@@ -1841,7 +1841,7 @@
          * LAB_CONTROL params is optional only for single stage session.
          */
         if ((p_lsm_ses->num_stages == 1) &&
-            !(p_lsm_ses->lsm_usecase->param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
+            !(p_lsm_ses->lsm_usecase.param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
             ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_lab_control");
             status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_LAB_CONTROL, &buf_en);
             ATRACE_END();
@@ -1868,7 +1868,7 @@
         stage_idx = LSM_STAGE_INDEX_FIRST;
         param_count = 0;
         lsm_params.params = (unsigned char*)&param_info[0];
-        mparams = p_lsm_ses->lsm_usecase->params;
+        mparams = p_lsm_ses->lsm_usecase.params;
         param_info[param_count].param_size = 0;
 
         lsm_fill_param_info(LSM_DEREG_SND_MODEL, &param_info[param_count++],
@@ -2022,8 +2022,8 @@
     stage_idx = LSM_STAGE_INDEX_FIRST;
     param_count = 0;
     lsm_params.params = (unsigned char*)&param_info[0];
-    param_tag_tracker = p_lsm_ses->lsm_usecase->param_tag_tracker;
-    mparams = p_lsm_ses->lsm_usecase->params;
+    param_tag_tracker = p_lsm_ses->lsm_usecase.param_tag_tracker;
+    mparams = p_lsm_ses->lsm_usecase.params;
 
     /*
      * For other than QTI VA, pass only the opaque data as custom params and
@@ -2231,7 +2231,7 @@
      */
     if (capture_requested && p_ses->stdev->lpi_enable &&
         p_ses->vendor_uuid_info->lab_dam_cfg_payload.token_id &&
-        (p_lsm_ses->lsm_usecase->param_tag_tracker &
+        (p_lsm_ses->lsm_usecase.param_tag_tracker &
          PARAM_LAB_DAM_CFG_BIT)) {
 
         lab_dam_cfg_params = &param_info[param_count++];
@@ -2419,7 +2419,7 @@
          * LAB_CONTROL params is optional only for single stage session.
          */
         if (p_lsm_ses->num_stages == 1 &&
-            !(p_lsm_ses->lsm_usecase->param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
+            !(p_lsm_ses->lsm_usecase.param_tag_tracker & PARAM_LAB_CONTROL_BIT)) {
             ATRACE_BEGIN("sthal:lsm: pcm_ioctl sndrv_lsm_lab_control");
             status = pcm_ioctl(p_lsm_ses->pcm, SNDRV_LSM_LAB_CONTROL, &buf_en);
             ATRACE_END();
@@ -2757,11 +2757,11 @@
 
     capture_device = platform_stdev_get_capture_device(p_ses->stdev->platform);
 
-    platform_get_lsm_usecase(p_ses->stdev->platform, v_info,
+    status = platform_get_lsm_usecase(p_ses->stdev->platform, v_info,
         &p_lsm_ses->lsm_usecase, p_ses->exec_mode, p_ses->lpi_enable,
         p_ses->f_stage_version);
-    if (!p_lsm_ses->lsm_usecase) {
-        ALOGE("%s: failed to allocate lsm usecase for the session", __func__);
+    if (status) {
+        ALOGE("%s: failed to get the lsm usecase for the session", __func__);
         return -ENODEV;
     }
 
@@ -2968,13 +2968,13 @@
     uint32_t data_payload_addr_lsw = 0, data_payload_addr_msw = 0;
     uint32_t custom_payload_size = 0, mem_map_handle = 0;
 
-    if (!(p_lsm_ses->lsm_usecase->param_tag_tracker &
+    if (!(p_lsm_ses->lsm_usecase.param_tag_tracker &
           PARAM_REQUEST_DETECTION_BIT)) {
         ALOGE("%s: No request detection IDs set, exiting", __func__);
         return -EINVAL;
     }
 
-    mparams = p_lsm_ses->lsm_usecase->params;
+    mparams = p_lsm_ses->lsm_usecase.params;
     stage_idx = LSM_STAGE_INDEX_FIRST;
     if (platform_stdev_get_xml_version(p_ses->stdev->platform) >
         PLATFORM_XML_VERSION_0x0102) {
diff --git a/st_hw_session_lsm.h b/st_hw_session_lsm.h
index 7fd914a..e713501 100644
--- a/st_hw_session_lsm.h
+++ b/st_hw_session_lsm.h
@@ -155,7 +155,7 @@
 
 struct st_hw_session_lsm {
     st_hw_session_t common;
-    struct st_lsm_params* lsm_usecase;
+    struct st_lsm_params lsm_usecase;
     pthread_t callback_thread;
     bool exit_callback_thread;
 
diff --git a/st_session.c b/st_session.c
index 9533721..a2d9b03 100644
--- a/st_session.c
+++ b/st_session.c
@@ -2603,6 +2603,12 @@
         if (v_info->merge_fs_soundmodels) {
             /* merge_fs_soundmodels is true only for QC SVA UUID */
 
+            if (conf_levels == NULL) {
+                ALOGE("%s: Unexpected, conf_levels pointer is NULL",
+                      __func__);
+                status = -EINVAL;
+                return status;
+            }
              /*
               * Note:
               * For ADSP case, the generated conf levles size must be equal to
@@ -2653,13 +2659,11 @@
              * during only one remaining client model as there won't be a
              * merged model yet.
              */
-
-            /*
-             * User verification confidence is not required
-             * in SVA5 PDK_UV case. As first stage doesn't
-             * support user verification.
-             */
-            num_conf_levels = 1;
+            if (!conf_levels) {
+                ALOGE("%s: ERROR. conf levels alloc failed", __func__);
+                status = -ENOMEM;
+                return status;
+            }
             memcpy(stc_ses->sm_info.cf_levels, conf_levels,
                    stc_ses->sm_info.cf_levels_size);
 
@@ -2713,6 +2717,13 @@
 
 
     } else {
+        if (conf_levels == NULL) {
+                ALOGE("%s: Unexpected, conf_levels pointer is NULL",
+                      __func__);
+                status = -EINVAL;
+                return status;
+        }
+
         if (!st_ses->lab_enabled && enable_lab)
             st_ses->lab_enabled = true;
 
@@ -2725,6 +2736,13 @@
             st_hw_ses->max_preroll = stc_ses->preroll_duration;
 
         /*
+         * User verification confidence is not required
+         * in SVA5 PDK_UV case. As first stage doesn't
+         * support user verification.
+         */
+        num_conf_levels = 1;
+
+        /*
          * Cache it to use when client restarts without
          * config update
          */