Merge remote-tracking branch 'quic/LA.BR.1' into HEAD

* quic/LA.BR.1: (37 commits)
  hal: Performance mode during recording usecase
  audio: Support for compress offload recovery
  audio: Fix for clip skip issue during SSR
  hal: add checks for calibration buffer allocation failures
  mm-audio: aenc-aac: fix integer overflow for encoded buffer timestamp calculation
  hal: Add support for 8909 QRD skue
  hal: Fix for FM mute issue on start of FM record
  hal: Fix array length computation for backend id array.
  hal : set Non SA+ app type for PCM Capture
  hal : Fix for SA+ app type selection for PCM Capture usecase
  hal : Fix for SA+ app type selection for PCM Capture usecase
  hal: use 0 as default return value for out_get_render_position
  audio: hal: Add support for pm8916 on msm8909
  hal: Define a new combo device for AANC and Fluence
  hal: Define a new combo device for AANC and Fluence
  hal: use 0 as default return value for out_get_render_position
  hal: Update DS2 implementation to support ACDB based license mechanism
  hal : Overwrite the APP type for PCM RX and TX path
  audio: HAL to support for peripheral manager
  hal : Set the input device for VOIP calls using audio path
  ...
Conflicts:
	hal/Android.mk
	hal/audio_extn/audio_extn.c
	hal/audio_extn/audio_extn.h
	hal/audio_extn/dolby.c
	hal/audio_extn/utils.c
	hal/audio_hw.c
	hal/msm8916/hw_info.c
	hal/msm8916/platform.c
	hal/msm8974/platform.c
	hal/platform_api.h
Change-Id: Ibfa171e8f3af713dbb2cffbaf2ca2b0df3e8ae73
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 62ab6d5..ff485e5 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -534,6 +534,7 @@
    audio_extn_ds2_set_parameters(adev, parms);
    audio_extn_customstereo_set_parameters(adev, parms);
    audio_extn_hpx_set_parameters(adev, parms);
+   audio_extn_pm_set_parameters(parms);
 }
 
 void audio_extn_get_parameters(const struct audio_device *adev,
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 5aa9f2e..7bfa607 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2013 The Android Open Source Project
@@ -310,6 +310,7 @@
 void audio_extn_dolby_set_endpoint(struct audio_device *adev);
 #endif
 
+
 #if defined(DS1_DOLBY_DDP_ENABLED) || defined(DS2_DOLBY_DAP_ENABLED)
 bool audio_extn_is_dolby_format(audio_format_t format);
 int audio_extn_dolby_get_snd_codec_id(struct audio_device *adev,
@@ -377,6 +378,16 @@
 int audio_extn_dev_arbi_release(snd_device_t snd_device);
 #endif
 
+#ifndef PM_SUPPORT_ENABLED
+#define audio_extn_pm_set_parameters(params) (0)
+#define audio_extn_pm_vote(void) (0)
+#define audio_extn_pm_unvote(void) (0)
+#else
+void audio_extn_pm_set_parameters(struct str_parms *parms);
+int audio_extn_pm_vote (void);
+void audio_extn_pm_unvote(void);
+#endif
+
 void audio_extn_utils_update_streams_output_cfg_list(void *platform,
                                   struct mixer *mixer,
                                   struct listnode *streams_output_cfg_list);
diff --git a/hal/audio_extn/dolby.c b/hal/audio_extn/dolby.c
old mode 100644
new mode 100755
index cdfa2a1..0f53ad7
--- a/hal/audio_extn/dolby.c
+++ b/hal/audio_extn/dolby.c
@@ -20,7 +20,6 @@
 #define LOG_TAG "audio_hw_dolby"
 //#define LOG_NDEBUG 0
 //#define LOG_NDDEBUG 0
-
 #include <errno.h>
 #include <cutils/properties.h>
 #include <stdlib.h>
@@ -749,7 +748,7 @@
 
 void audio_extn_dolby_set_license(struct audio_device *adev)
 {
-    int i_key;
+    int i_key=0;
     char c_key[128] = {0};
     char c_dmid[128] = {0};
     int i_dmid, ret = -EINVAL;
diff --git a/hal/audio_extn/pm.c b/hal/audio_extn/pm.c
new file mode 100644
index 0000000..7b76f60
--- /dev/null
+++ b/hal/audio_extn/pm.c
@@ -0,0 +1,149 @@
+/* Copyright (c) 2014, 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
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#define LOG_TAG "audio_hw_pm"
+/*#define LOG_NDEBUG 0*/
+
+#include "pm.h"
+#include <cutils/log.h>
+
+static s_audio_subsys audio_ss;
+
+int audio_extn_pm_vote(void)
+{
+    int err, intfd, ret;
+    FILE *fd;
+    enum pm_event subsys_state;
+    char halPropVal[PROPERTY_VALUE_MAX];
+    bool prop_unload_image = false;
+    bool pm_reg = false;
+    bool pm_supp = false;
+
+    platform_get_subsys_image_name((char *)&audio_ss.img_name);
+    ALOGD("%s: register with peripheral manager for %s",__func__, audio_ss.img_name);
+    ret = pm_client_register(audio_extn_pm_event_notifier,
+                      &audio_ss,
+                      audio_ss.img_name,
+                      PM_CLIENT_NAME,
+                      &subsys_state,
+                      &audio_ss.pm_handle);
+    if (ret == PM_RET_SUCCESS) {
+        pm_reg = true;
+        pm_supp = true;
+        ALOGV("%s: registered with peripheral manager for %s",
+                  __func__, audio_ss.img_name);
+    } else if (ret == PM_RET_UNSUPPORTED) {
+        pm_reg = true;
+        pm_supp = false;
+        ALOGV("%s: peripheral mgr unsupported for %s",
+              __func__, audio_ss.img_name);
+        return ret;
+    } else {
+       return ret;
+    }
+    if (pm_supp == true &&
+       pm_reg == true) {
+       ALOGD("%s: Voting for subsystem power up", __func__);
+       pm_client_connect(audio_ss.pm_handle);
+
+       if (property_get("sys.audio.init", halPropVal, NULL)) {
+           prop_unload_image = !(strncmp("false", halPropVal, sizeof("false")));
+       }
+       /*
+        * adsp-loader loads modem/adsp image at boot up to play boot tone,
+        * before peripheral manager service is up. Once PM is up, vote to PM
+        * and unload the image to give control to PM to load/unload image
+        */
+       if (prop_unload_image) {
+           intfd = open(BOOT_IMG_SYSFS_PATH, O_WRONLY);
+           if (intfd == -1) {
+               ALOGE("failed to open fd in write mode, %d", errno);
+           } else {
+               ALOGD("%s: write to sysfs to unload image", __func__);
+               err = write(intfd, UNLOAD_IMAGE, 1);
+               close(intfd);
+               property_set("sys.audio.init", "true");
+          }
+       }
+    }
+    return 0;
+}
+
+void audio_extn_pm_unvote(void)
+{
+    ALOGD("%s", __func__);
+    if (audio_ss.pm_handle) {
+        pm_client_disconnect(audio_ss.pm_handle);
+        pm_client_unregister(audio_ss.pm_handle);
+    }
+}
+
+void audio_extn_pm_set_parameters(struct str_parms *parms)
+{
+    int ret;
+    char value[32];
+
+    ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_DEV_SHUTDOWN, value, sizeof(value));
+    if (ret >= 0) {
+        if (strstr(value, "true")) {
+            ALOGD("Device shutdown notification received, unregister with PM");
+            audio_extn_pm_unvote();
+        }
+    }
+}
+
+void audio_extn_pm_event_notifier(void *client_data, enum pm_event event)
+{
+    pm_client_event_acknowledge(audio_ss.pm_handle, event);
+
+    /* Closing and re-opening of session is done based on snd card status given
+     * by AudioDaemon during SS offline/online (legacy code). Just return for now.
+     */
+    switch (event) {
+    case EVENT_PERIPH_GOING_OFFLINE:
+        ALOGV("%s: %s is going offline", __func__, audio_ss.img_name);
+    break;
+
+    case EVENT_PERIPH_IS_OFFLINE:
+        ALOGV("%s: %s is offline", __func__, audio_ss.img_name);
+    break;
+
+    case EVENT_PERIPH_GOING_ONLINE:
+        ALOGV("%s: %s is going online", __func__, audio_ss.img_name);
+    break;
+
+    case EVENT_PERIPH_IS_ONLINE:
+        ALOGV("%s: %s is online", __func__, audio_ss.img_name);
+    break;
+
+    default:
+        ALOGV("%s: invalid event received from PM", __func__);
+    break;
+    }
+}
diff --git a/hal/audio_extn/pm.h b/hal/audio_extn/pm.h
new file mode 100644
index 0000000..daa376e
--- /dev/null
+++ b/hal/audio_extn/pm.h
@@ -0,0 +1,68 @@
+/* Copyright (c) 2014, 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
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of The Linux Foundation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef AUDIO_EXTN_PM_H
+#define AUDIO_EXTN_PM_H
+
+#include <errno.h>
+#include <math.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <pm-service.h>
+#include "audio_hw.h"
+#include <platform.h>
+#include <cutils/properties.h>
+#include <cutils/log.h>
+
+
+/* Client name to be registered with PM */
+#define PM_CLIENT_NAME "audio"
+/* Command to sysfs to unload image */
+#define UNLOAD_IMAGE "0"
+#define MAX_NAME_LEN 32
+#define BOOT_IMG_SYSFS_PATH "/sys/kernel/boot_adsp/boot"
+
+typedef struct {
+    //MAX_NAME_LEN defined in mdm_detect.h
+    char img_name[MAX_NAME_LEN];
+    //this handle is used by peripheral mgr
+    void *pm_handle;
+}s_audio_subsys;
+
+/* Vote to peripheral manager for required subsystem */
+int audio_extn_pm_vote (void);
+
+/* Unvote to peripheral manager */
+void audio_extn_pm_unvote (void);
+
+/* Get subsytem status notification from PM */
+void audio_extn_pm_event_notifier (void *client_data, enum pm_event event);
+
+#endif // AUDIO_EXTN_PM_H
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index 34e6089..96722cc 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -579,23 +579,15 @@
         int snd_device = usecase->out_snd_device;
         snd_device = (snd_device == SND_DEVICE_OUT_SPEAKER) ?
                      audio_extn_get_spkr_prot_snd_device(snd_device) : snd_device;
-        platform_send_audio_calibration(adev->platform, usecase->out_snd_device,
+        platform_send_audio_calibration(adev->platform, usecase,
                                         out->app_type_cfg.app_type,
                                         out->app_type_cfg.sample_rate);
     }
     if ((type == PCM_HFP_CALL) || (type == PCM_CAPTURE)) {
-        if ((type == PCM_CAPTURE) & voice_is_in_call_rec_stream(usecase->stream.in)) {
-            snd_device_t incall_record_snd_device =
-                        voice_get_incall_rec_snd_device(usecase->in_snd_device);
-            platform_send_audio_calibration(adev->platform, incall_record_snd_device,
-                                            platform_get_default_app_type(adev->platform),
-                                            48000);
-        } else {
-            /* when app type is default. the sample rate is not used to send cal */
-            platform_send_audio_calibration(adev->platform, usecase->in_snd_device,
-                                            platform_get_default_app_type(adev->platform),
-                                            48000);
-        }
+        /* when app type is default. the sample rate is not used to send cal */
+        platform_send_audio_calibration(adev->platform, usecase,
+                                        platform_get_default_app_type(adev->platform),
+                                        48000);
     }
 }