Merge "st-hal: Add change to dynamically load sound trigger platform files"
diff --git a/sound_trigger_platform.c b/sound_trigger_platform.c
index ee7a72c..c09a483 100644
--- a/sound_trigger_platform.c
+++ b/sound_trigger_platform.c
@@ -416,8 +416,43 @@
 
     struct st_lpma_config lpma_cfg;
     struct listnode acdb_meta_key_list;
+
+    char vendor_config_path[MIXER_PATH_MAX_LENGTH];
+    char xml_file_path[MIXER_PATH_MAX_LENGTH];
 };
 
+
+/*
+ * Function to retrieve vendor configs path
+ *
+ * If 'ro.boot.product.vendor.sku' is not set,files would be loaded from
+ * /vendor/etc/. If the property is set, files would be loaded from
+ * /vendor/etc/audio/sku_{ro.boot.product.vendor.sku}.
+ * 'ro.boot.product.vendor.sku' would be set to SoC/SKU at boot up in vendor.
+*/
+static void platform_stdev_get_vendor_config_path(char* config_file_path, int path_size)
+{
+    char vendor_sku[PROPERTY_VALUE_MAX] = {'\0'};
+
+    if (property_get("ro.boot.product.vendor.sku", vendor_sku, "") <= 0) {
+#ifdef LINUX_ENABLED
+        /* Audio configs are stored in /etc */
+        snprintf(config_file_path, path_size, "%s", "/etc");
+#else
+        /* Audio configs are stored in /vendor/etc */
+        snprintf(config_file_path, path_size, "%s", "/vendor/etc");
+#endif
+    } else {
+        /* Audio configs are stored in /vendor/etc/audio/sku_${vendor_sku} */
+        snprintf(config_file_path, path_size,"%s%s", "/vendor/etc/audio/sku_", vendor_sku);
+    }
+}
+
+static void get_xml_file_path(char* path, const char* file_name, const char* vendor_path)
+{
+    snprintf(path, MIXER_PATH_MAX_LENGTH, "%s/%s", vendor_path, file_name);
+}
+
 static int load_soundmodel_lib(sound_trigger_device_t *stdev)
 {
     int status = 0;
@@ -3220,20 +3255,23 @@
     return 0;
 }
 
-static void query_stdev_platform(sound_trigger_device_t *stdev,
+static void query_stdev_platform(struct platform_data *my_data,
                                  const char *snd_card_name,
                                  char *mixer_path_xml)
 {
     if (strstr(snd_card_name, "msm8939-tapan")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_WCD9306,
-                        sizeof(MIXER_PATH_FILE_NAME_WCD9306));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_WCD9306,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     } else if (strstr(snd_card_name, "msm8952-tomtom")||
                  strstr(snd_card_name, "msm8996-tomtom")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_WCD9330,
-                        sizeof(MIXER_PATH_FILE_NAME_WCD9330));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_WCD9330,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     } else if (strstr(snd_card_name, "sdm670-skuw")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_SKUW,
-                        sizeof(MIXER_PATH_FILE_NAME_SKUW));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_SKUW,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     } else if (strstr(snd_card_name, "msm8976-tasha")||
                  strstr(snd_card_name, "msm8952-tasha") ||
                  strstr(snd_card_name, "msm8953-tasha") ||
@@ -3242,22 +3280,27 @@
                  strstr(snd_card_name, "sdm660-tasha") ||
                  strstr(snd_card_name, "sdm670-tasha") ||
                  strstr(snd_card_name, "apq8009-tashalite")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_WCD9335,
-                        sizeof(MIXER_PATH_FILE_NAME_WCD9335));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_WCD9335,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     } else if (strstr(snd_card_name, "tavil")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_WCD9340,
-            sizeof(MIXER_PATH_FILE_NAME_WCD9340));
-        stdev->is_gcs = true;
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_WCD9340,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
+        my_data->stdev->is_gcs = true;
     } else if (strstr(snd_card_name, "bg")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_BG,
-            sizeof(MIXER_PATH_FILE_NAME_BG));
-        stdev->is_gcs = true;
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_BG,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
+        my_data->stdev->is_gcs = true;
     } else if (strstr(snd_card_name, "qcs405-tdm")) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME_TDM,
-            sizeof(MIXER_PATH_FILE_NAME_TDM));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME_TDM,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     } else {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME,
-                         sizeof(MIXER_PATH_FILE_NAME));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     }
     /* create mixer path file name from sound card name
     and attach cdp/qrd if sound card name has cdp/qrd */
@@ -3301,8 +3344,9 @@
     }
     if (!strncmp(snd_card_name, "sm6150-wcd9375qrd-snd-card",
         sizeof("sm6150-wcd9375qrd-snd-card"))) {
-        strlcpy(mixer_path_xml, MIXER_PATH_FILE_NAME,
-                        sizeof(MIXER_PATH_FILE_NAME));
+        get_xml_file_path(my_data->xml_file_path, MIXER_PATH_FILE_NAME,
+            my_data->vendor_config_path);
+        strlcpy(mixer_path_xml, my_data->xml_file_path, MIXER_PATH_MAX_LENGTH);
     }
 
     strlcat(mixer_path_xml, MIXER_FILE_EXT, MIXER_PATH_MAX_LENGTH);
@@ -3336,7 +3380,7 @@
         !strstr(snd_card_name, "sm6150-tavil") &&
         !strstr(snd_card_name, "apq8009-tasha") &&
         !strstr(snd_card_name, "msm8939-tomtom")) {
-        stdev->sw_mad = true;
+        my_data->stdev->sw_mad = true;
     }
 }
 
@@ -3965,8 +4009,12 @@
     list_init(&stdev->adm_cfg_list);
     list_init(&my_data->acdb_meta_key_list);
 
+    platform_stdev_get_vendor_config_path(my_data->vendor_config_path,
+        sizeof(my_data->vendor_config_path));
     platform_stdev_set_default_config(my_data);
-    platform_parse_info(my_data, PLATFORM_PATH_XML);
+    get_xml_file_path(my_data->xml_file_path, PLATFORM_PATH_XML,
+        my_data->vendor_config_path);
+    platform_parse_info(my_data, my_data->xml_file_path);
 
     my_data->hwdep_fd = -1;
     my_data->vad_hwdep_fd = -1;
@@ -4019,7 +4067,7 @@
 
     snd_card_name = mixer_get_name(stdev->mixer);
 
-    query_stdev_platform(stdev, snd_card_name, mixer_path_xml);
+    query_stdev_platform(my_data, snd_card_name, mixer_path_xml);
     stdev->audio_route = audio_route_init(snd_card_num, mixer_path_xml);
     if (!stdev->audio_route) {
         ALOGE("%s: ERROR. Failed to init audio route controls, aborting.",
diff --git a/sound_trigger_platform.h b/sound_trigger_platform.h
index ffe3af8..79df03d 100644
--- a/sound_trigger_platform.h
+++ b/sound_trigger_platform.h
@@ -42,27 +42,17 @@
 
 #define PCM_DEVICE_FILE "/proc/asound/pcm"
 
-#define MIXER_PATH_MAX_LENGTH 100
+#define MIXER_PATH_MAX_LENGTH 128
 #define MIXER_FILE_EXT ".xml"
-#define MIXER_PATH_FILE_NAME_WCD9306 "/vendor/etc/sound_trigger_mixer_paths_wcd9306"
-#define MIXER_PATH_FILE_NAME_WCD9330 "/vendor/etc/sound_trigger_mixer_paths_wcd9330"
-
-#ifdef LINUX_ENABLED
-#define MIXER_PATH_FILE_NAME "/etc/sound_trigger_mixer_paths"
-#define MIXER_PATH_FILE_NAME_WCD9335 "/etc/sound_trigger_mixer_paths_wcd9335"
-#define MIXER_PATH_FILE_NAME_SKUW "/etc/sound_trigger_mixer_paths_skuw"
-#define MIXER_PATH_FILE_NAME_TDM "/etc/sound_trigger_mixer_paths_tdm"
-#define PLATFORM_PATH_XML "/etc/sound_trigger_platform_info.xml"
-#else
-#define MIXER_PATH_FILE_NAME "/vendor/etc/sound_trigger_mixer_paths"
-#define MIXER_PATH_FILE_NAME_WCD9335 "/vendor/etc/sound_trigger_mixer_paths_wcd9335"
-#define MIXER_PATH_FILE_NAME_SKUW "/vendor/etc/sound_trigger_mixer_paths_skuw"
-#define MIXER_PATH_FILE_NAME_TDM "/vendor/etc/sound_trigger_mixer_paths_tdm"
-#define PLATFORM_PATH_XML "/vendor/etc/sound_trigger_platform_info.xml"
-#endif
-#define MIXER_PATH_FILE_NAME_WCD9340 "/vendor/etc/sound_trigger_mixer_paths_wcd9340"
-#define MIXER_PATH_FILE_NAME_BG "/vendor/etc/sound_trigger_mixer_paths_bg"
-
+#define MIXER_PATH_FILE_NAME_WCD9306 "sound_trigger_mixer_paths_wcd9306"
+#define MIXER_PATH_FILE_NAME_WCD9330 "sound_trigger_mixer_paths_wcd9330"
+#define MIXER_PATH_FILE_NAME "sound_trigger_mixer_paths"
+#define MIXER_PATH_FILE_NAME_WCD9335 "sound_trigger_mixer_paths_wcd9335"
+#define MIXER_PATH_FILE_NAME_SKUW "sound_trigger_mixer_paths_skuw"
+#define MIXER_PATH_FILE_NAME_TDM "sound_trigger_mixer_paths_tdm"
+#define PLATFORM_PATH_XML "sound_trigger_platform_info.xml"
+#define MIXER_PATH_FILE_NAME_WCD9340 "sound_trigger_mixer_paths_wcd9340"
+#define MIXER_PATH_FILE_NAME_BG "sound_trigger_mixer_paths_bg"
 #define LIB_ACDB_LOADER "libacdbloader.so"
 #define LIB_MULAW_DECODER "libmulawdec.so"
 #define LIB_SVA_SOUNDMODEL "liblistensoundmodel2.so"
diff --git a/st_hw_session_pcm.c b/st_hw_session_pcm.c
index 284b647..3b4e6e0 100644
--- a/st_hw_session_pcm.c
+++ b/st_hw_session_pcm.c
@@ -218,6 +218,27 @@
 }
 #endif
 
+static void get_lib_path(char * lib_path, int path_size)
+{
+#ifdef LINUX_ENABLED
+#ifdef __LP64__
+    /* libs are stored in /usr/lib64 */
+    snprintf(lib_path, path_size, "%s", "/usr/lib64");
+#else
+    /* libs are stored in /usr/lib */
+    snprintf(lib_path, path_size, "%s", "/usr/lib");
+#endif
+#else
+#ifdef __LP64__
+    /* libs are stored in /vendor/lib64 */
+    snprintf(lib_path, path_size, "%s", "/vendor/lib64");
+#else
+    /* libs are stored in /vendor/lib */
+    snprintf(lib_path, path_size, "%s", "/vendor/lib");
+#endif
+#endif
+}
+
 static size_t get_ffv_read_buffer_len(st_hw_session_pcm_t *p_ses)
 {
     st_get_param_payload_t *params_buffer_ptr;
@@ -1244,7 +1265,9 @@
     int num_tx_in_ch, num_out_ch, num_ec_ref_ch;
     int frame_len;
     int sample_rate;
-    const char *config_file_path = ST_FFV_CONFIG_FILE_PATH;
+    char vendor_config_path[MIXER_PATH_MAX_LENGTH];
+    char st_ffv_config_file[MIXER_PATH_MAX_LENGTH];
+    const char *config_file_path;
     FfvStatusType status_type;
     EspStatusType esp_status_type;
     int total_mem_size;
@@ -1273,6 +1296,12 @@
                                      p_pcm_ses->capture_config.rate,
                                      SOUND_TRIGGER_PCM_PERIOD_COUNT, 32);
 
+    platform_stdev_get_vendor_config_path(vendor_config_path,
+        sizeof(vendor_config_path));
+    /* Get path for st_ffv_config_file_name in vendor */
+    snprintf(st_ffv_config_file, sizeof(st_ffv_config_file),
+            "%s/%s", vendor_config_path, ST_FFV_CONFIG_FILE_NAME);
+    config_file_path = st_ffv_config_file;
     ALOGD("%s: opening pcm device=%d ", __func__, p_pcm_ses->pcm_id);
     ALOGV("%s: config: channels=%d rate=%d, period_size=%d, period_cnt=%d, format=%d",
           __func__, p_pcm_ses->capture_config.channels, p_pcm_ses->capture_config.rate,
@@ -2103,11 +2132,19 @@
 int st_hw_pcm_init()
 {
     int status = 0;
+    char lib_path[MIXER_PATH_MAX_LENGTH];
+    char ffv_lib_file[MIXER_PATH_MAX_LENGTH];
+    char esp_lib_file[MIXER_PATH_MAX_LENGTH];
 
+    /* Get path for libs */
+    get_lib_path(lib_path, sizeof(lib_path));
+
+    /* Get path for ffv_lib_file */
+    snprintf(ffv_lib_file, sizeof(ffv_lib_file), "%s/%s", lib_path, FFV_LIB_NAME);
     /* load FFV + SVA library */
-    pcm_data.ffv_lib_handle = dlopen(FFV_LIB, RTLD_NOW);
+    pcm_data.ffv_lib_handle = dlopen(ffv_lib_file, RTLD_NOW);
     if (!pcm_data.ffv_lib_handle) {
-        ALOGE("%s: Unable to open %s, error %s", __func__, FFV_LIB,
+        ALOGE("%s: Unable to open %s, error %s", __func__, ffv_lib_file,
             dlerror());
         status = -ENOENT;
         goto exit;
@@ -2139,10 +2176,13 @@
     if (status)
         goto exit;
 
+    /* Get path for esp_lib_file */
+    snprintf(esp_lib_file, sizeof(esp_lib_file), "%s/%s", lib_path, ESP_LIB_NAME);
+
     /* load ESP library */
-    pcm_data.esp_lib_handle = dlopen(ESP_LIB, RTLD_NOW);
+    pcm_data.esp_lib_handle = dlopen(esp_lib_file, RTLD_NOW);
     if (!pcm_data.esp_lib_handle) {
-        ALOGE("%s: Unable to open %s, error %s", __func__, ESP_LIB,
+        ALOGE("%s: Unable to open %s, error %s", __func__, esp_lib_file,
             dlerror());
         status = -ENOENT;
         goto exit_1;
diff --git a/st_hw_session_pcm.h b/st_hw_session_pcm.h
index 9347867..0b072f4 100644
--- a/st_hw_session_pcm.h
+++ b/st_hw_session_pcm.h
@@ -1,6 +1,6 @@
 /* st_hw_session_pcm.h
  *
- * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2017-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
@@ -37,25 +37,10 @@
 
 struct sound_trigger_device;
 
-#if LINUX_ENABLED
-#define ST_FFV_CONFIG_FILE_PATH "/etc/BF_1out.cfg"
-#ifdef __LP64__
-#define FFV_LIB "/usr/lib64/libffv.so"
-#define ESP_LIB "/usr/lib64/libesp.so"
-#else
-#define FFV_LIB "/usr/lib/libffv.so"
-#define ESP_LIB "/usr/lib/libesp.so"
-#endif
-#else
-#define ST_FFV_CONFIG_FILE_PATH "/vendor/etc/BF_1out.cfg"
-#ifdef __LP64__
-#define FFV_LIB "/vendor/lib64/libffv.so"
-#define ESP_LIB "/vendor/lib64/libesp.so"
-#else
-#define FFV_LIB "/vendor/lib/libffv.so"
-#define ESP_LIB "/vendor/lib/libesp.so"
-#endif
-#endif
+
+#define ST_FFV_CONFIG_FILE_NAME "BF_1out.cfg"
+#define FFV_LIB_NAME "libffv.so"
+#define ESP_LIB_NAME "libesp.so"
 
 #define SOUND_TRIGGER_MAX_EVNT_PAYLOAD_SIZE (256)