audio: Updates to comply with standard libhardware_legacy audio hal

Revert back to original libhardware_legacy audio hal headers from AOSP.
This makes corresponding code changes to disable features to match
audio hal implementation with standard libhardware_legacy audio hal.

Change-Id: Ibf1e50d3fffc8280ba417a26172c0f04206474e3
diff --git a/alsa_sound/ALSAControl.cpp b/alsa_sound/ALSAControl.cpp
index 59d550e..2d610a1 100644
--- a/alsa_sound/ALSAControl.cpp
+++ b/alsa_sound/ALSAControl.cpp
@@ -24,10 +24,10 @@
 #include <unistd.h>
 #include <dlfcn.h>
 
-#define LOG_TAG "alsa.msm8960"
+#define LOG_TAG "ALSAControl"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
-
 #include <utils/String8.h>
 
 #include <cutils/properties.h>
diff --git a/alsa_sound/ALSAMixer.cpp b/alsa_sound/ALSAMixer.cpp
index 2c1abcb..d1383cf 100644
--- a/alsa_sound/ALSAMixer.cpp
+++ b/alsa_sound/ALSAMixer.cpp
@@ -25,7 +25,6 @@
 
 #define LOG_TAG "AudioHardwareALSA"
 #include <utils/Log.h>
-
 #include <utils/String8.h>
 
 #include <cutils/properties.h>
diff --git a/alsa_sound/ALSAStreamOps.cpp b/alsa_sound/ALSAStreamOps.cpp
index c37ced7..6735a87 100644
--- a/alsa_sound/ALSAStreamOps.cpp
+++ b/alsa_sound/ALSAStreamOps.cpp
@@ -24,8 +24,9 @@
 #include <unistd.h>
 #include <dlfcn.h>
 
-#define LOG_TAG "audio.primary.msm8960"
+#define LOG_TAG "ALSAStreamOps"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
 #include <utils/String8.h>
 
@@ -114,7 +115,7 @@
             }
         } else {
             switch(mHandle->channels) {
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
                 // For 5.1 recording
                 case 6 :
                     *channels |= AudioSystem::CHANNEL_IN_5POINT1;
@@ -151,7 +152,7 @@
                 break;
             case AudioSystem::AMR_NB:
             case AudioSystem::AMR_WB:
-#if 0
+#ifdef QCOM_QCHAT_ENABLED
             case AudioSystem::EVRC:
             case AudioSystem::EVRCB:
             case AudioSystem::EVRCWB:
@@ -200,7 +201,7 @@
         }
         param.remove(key);
     }
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
     else {
         key = String8(AudioParameter::keyHandleFm);
         if (param.getInt(key, device) == NO_ERROR) {
@@ -227,7 +228,7 @@
         param.addInt(key, (int)mDevices);
     }
     else {
-#if 0
+#ifdef QCOM_VOIP_ENABLED
         key = String8(AudioParameter::keyVoipCheck);
         if (param.get(key, value) == NO_ERROR) {
             if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
@@ -269,7 +270,7 @@
 
         case AudioSystem::AMR_NB:
         case AudioSystem::AMR_WB:
-#if 0
+#ifdef QCOM_QCHAT_ENABLED
         case AudioSystem::EVRC:
         case AudioSystem::EVRCB:
         case AudioSystem::EVRCWB:
@@ -311,7 +312,7 @@
         }
     else
         switch(count) {
-#ifdef SSR_ENABLED            
+#ifdef QCOM_SSR_ENABLED
             // For 5.1 recording
             case 6 :
                 channels |= AudioSystem::CHANNEL_IN_5POINT1;
diff --git a/alsa_sound/Android.mk b/alsa_sound/Android.mk
index cdc2694..38c3a5c 100644
--- a/alsa_sound/Android.mk
+++ b/alsa_sound/Android.mk
@@ -51,7 +51,7 @@
 
 include $(BUILD_SHARED_LIBRARY)
 
-ifeq (1,1) # use default audio policy manager
+ifeq (1,0) # use default audio policy manager
 # This is the ALSA audio policy manager
 
 include $(CLEAR_VARS)
@@ -85,6 +85,15 @@
 include $(BUILD_SHARED_LIBRARY)
 endif
 
+# Load audio_policy.conf to system/etc/
+include $(CLEAR_VARS)
+LOCAL_MODULE       := audio_policy.conf
+LOCAL_MODULE_TAGS  := optional
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_PATH  := $(TARGET_OUT_ETC)/
+LOCAL_SRC_FILES    := audio_policy.conf
+include $(BUILD_PREBUILT)
+
 # This is the ALSA module which behaves closely like the original
 
 include $(CLEAR_VARS)
diff --git a/alsa_sound/AudioHardwareALSA.cpp b/alsa_sound/AudioHardwareALSA.cpp
index 97ff206..d108d22 100644
--- a/alsa_sound/AudioHardwareALSA.cpp
+++ b/alsa_sound/AudioHardwareALSA.cpp
@@ -25,8 +25,9 @@
 #include <dlfcn.h>
 #include <math.h>
 
-#define LOG_TAG "audio.primary.msm8960"
+#define LOG_TAG "AudioHardwareALSA"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
 #include <utils/String8.h>
 #include <sys/prctl.h>
@@ -38,9 +39,17 @@
 #include <hardware_legacy/power.h>
 
 #include "AudioHardwareALSA.h"
+#ifdef QCOM_USBAUDIO_ENABLED
 #include "AudioUsbALSA.h"
+#endif
 
 extern "C" {
+#ifdef QCOM_CSDCLIENT_ENABLED
+#include "csd_client.h"
+#endif
+#ifdef QCOM_ACDB_ENABLED
+#include "acdb-loader.h"
+#endif
 }
 
 extern "C"
@@ -84,12 +93,20 @@
             mVolteCallActive = 0;
             mIsFmActive = 0;
             mDevSettingsFlag = 0;
+#ifdef QCOM_USBAUDIO_ENABLED
             mAudioUsbALSA = new AudioUsbALSA();
+            musbPlaybackState = 0;
+            musbRecordingState = 0;
+#endif
             mDevSettingsFlag |= TTY_OFF;
             mBluetoothVGS = false;
             mFusion3Platform = false;
-            musbPlaybackState = 0;
-            musbRecordingState = 0;
+
+#ifdef QCOM_ACDB_ENABLED
+            if ((acdb_loader_init_ACDB()) < 0) {
+                ALOGE("Failed to initialize ACDB");
+            }
+#endif
 
             if((fp = fopen("/proc/asound/cards","r")) == NULL) {
                 ALOGE("Cannot open /proc/asound/cards file to get sound card info");
@@ -156,7 +173,12 @@
         it->useCase[0] = 0;
         mDeviceList.erase(it);
     }
+#ifdef QCOM_ACDB_ENABLED
+    acdb_loader_deallocate_ACDB();
+#endif
+#ifdef QCOM_USBAUDIO_ENABLED
     delete mAudioUsbALSA;
+#endif
 }
 
 status_t AudioHardwareALSA::initCheck()
@@ -191,9 +213,9 @@
         if(newMode == AudioSystem::MODE_IN_COMMUNICATION) {
             mALSADevice->setVoipVolume(vol);
         } else if (newMode == AudioSystem::MODE_IN_CALL){
-               if (mCSCallActive == AudioSystem::CS_ACTIVE)
+               if (mCSCallActive == CS_ACTIVE)
                    mALSADevice->setVoiceVolume(vol);
-               if (mVolteCallActive == AudioSystem::IMS_ACTIVE)
+               if (mVolteCallActive == IMS_ACTIVE)
                    mALSADevice->setVoLTEVolume(vol);
         }
     }
@@ -201,7 +223,7 @@
     return NO_ERROR;
 }
 
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
 status_t  AudioHardwareALSA::setFmVolume(float value)
 {
     status_t status = NO_ERROR;
@@ -240,7 +262,7 @@
     }
 
     if (mode == AudioSystem::MODE_IN_CALL) {
-        mCallState = AudioSystem::CS_ACTIVE;
+        mCallState = CS_ACTIVE;
     }else if (mode == AudioSystem::MODE_NORMAL) {
         mCallState = 0;
     }
@@ -298,16 +320,20 @@
         doRouting(0);
     }
 
+#ifdef QCOM_CSDCLIENT_ENABLED
     if (mFusion3Platform) {
         key = String8(INCALLMUSIC_KEY);
         if (param.get(key, value) == NO_ERROR) {
             if (value == "true") {
                 ALOGV("Enabling Incall Music setting in the setparameter\n");
+                csd_client_start_playback();
             } else {
                 ALOGV("Disabling Incall Music setting in the setparameter\n");
+                csd_client_stop_playback();
             }
         }
     }
+#endif
 
     key = String8(ANC_KEY);
     if (param.get(key, value) == NO_ERROR) {
@@ -376,7 +402,7 @@
         param.remove(key);
     }
 
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
     key = String8(AudioParameter::keyHandleFm);
     if (param.getInt(key, device) == NO_ERROR) {
         // Ignore if device is 0
@@ -437,7 +463,7 @@
         param.add(key, value);
     }
 
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
     key = String8("Fm-radio");
     if ( param.get(key,value) == NO_ERROR ) {
         if ( mIsFmActive ) {
@@ -456,6 +482,7 @@
     return param.toString();
 }
 
+#ifdef QCOM_USBAUDIO_ENABLED
 void AudioHardwareALSA::closeUSBPlayback()
 {
     ALOGV("closeUSBPlayback, musbPlaybackState: %d", musbPlaybackState);
@@ -500,6 +527,7 @@
         mAudioUsbALSA->startRecording();
     }
 }
+#endif
 
 void AudioHardwareALSA::doRouting(int device)
 {
@@ -507,15 +535,14 @@
     int newMode = mode();
     bool isRouted = false;
 
-    if ((device == AudioSystem::DEVICE_IN_VOICE_CALL) ||
-        (device == AudioSystem::DEVICE_IN_COMMUNICATION) ) {
-
-#if 0
-        ||
-        (device == AudioSystem::DEVICE_IN_FM_RX) ||
-        (device == AudioSystem::DEVICE_OUT_DIRECTOUTPUT) ||
-        (device == AudioSystem::DEVICE_IN_FM_RX_A2DP)) {
+    if ((device == AudioSystem::DEVICE_IN_VOICE_CALL)
+#ifdef QCOM_FM_ENABLED
+        || (device == AudioSystem::DEVICE_IN_FM_RX)
+        || (device == AudioSystem::DEVICE_OUT_DIRECTOUTPUT)
+        || (device == AudioSystem::DEVICE_IN_FM_RX_A2DP)
 #endif
+        || (device == AudioSystem::DEVICE_IN_COMMUNICATION)
+        ) {
         ALOGV("Ignoring routing for FM/INCALL/VOIP recording");
         return;
     }
@@ -529,7 +556,7 @@
     isRouted |= routeVoiceCall(device, newMode);
 
     if(!isRouted) {
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
         if(!(device & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET) &&
             !(device & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET) &&
             !(device & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) &&
@@ -619,6 +646,7 @@
                return MODE_AMR_WB;
          break;
 
+#ifdef QCOM_QCHAT_ENABLED
     case AudioSystem::EVRC:
                return MODE_IS127;
          break;
@@ -629,6 +657,7 @@
     case AudioSystem::EVRCWB:
                return MODE_4GV_WB;
          break;
+#endif
 
     default:
                return MODE_PCM;
@@ -778,7 +807,7 @@
       ALSAHandleList::iterator it = mDeviceList.end();
       it--;
       ALOGD("useCase %s", it->useCase);
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
       if((devices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
          (devices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)){
           ALOGE("Routing to proxy for normal playback in openOutputStream");
@@ -810,6 +839,7 @@
     delete out;
 }
 
+#ifdef QCOM_TUNNEL_LPA_ENABLED
 AudioStreamOut *
 AudioHardwareALSA::openOutputSession(uint32_t devices,
                                      int *format,
@@ -861,7 +891,7 @@
     ALSAHandleList::iterator it = mDeviceList.end();
     it--;
     ALOGD("useCase %s", it->useCase);
-# if 0
+#ifdef QCOM_USBAUDIO_ENABLED
     if((devices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
        (devices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)){
         ALOGE("Routing to proxy for LPA in openOutputSession");
@@ -902,6 +932,7 @@
 {
     delete out;
 }
+#endif
 
 AudioStreamIn *
 AudioHardwareALSA::openInputStream(uint32_t devices,
@@ -933,7 +964,7 @@
             it != mDeviceList.end(); ++it) {
                 if((!strcmp(it->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
                    (!strcmp(it->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
-                    ALOGD("openInput:  it->rxHandle %d it->handle %d",it->rxHandle,it->handle);
+                    ALOGD("openInput:  it->rxHandle %p it->handle %p",it->rxHandle,it->handle);
                     voipstream_active = true;
                     break;
                 }
@@ -979,7 +1010,7 @@
            it = mDeviceList.end();
            it--;
            ALOGE("mCurrDevice: %d", mCurDevice);
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
            if((mCurDevice == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
               (mCurDevice == AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)){
               ALOGE("Routing everything from proxy for voipcall");
@@ -1028,12 +1059,12 @@
             if((0 == strncmp(itDev->useCase, SND_USE_CASE_VERB_HIFI_REC, MAX_UC_LEN))
               ||(0 == strncmp(itDev->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, MAX_UC_LEN))
               ||(0 == strncmp(itDev->useCase, SND_USE_CASE_MOD_CAPTURE_FM, MAX_UC_LEN))
-#if 0
+#ifdef QCOM_FM_ENABLED
               ||(0 == strncmp(itDev->useCase, SND_USE_CASE_VERB_FM_REC, MAX_UC_LEN))
 #endif
               )
             {
-#if 0  
+#ifdef QCOM_FM_ENABLED
                 if(!(devices == AudioSystem::DEVICE_IN_FM_RX_A2DP)){
                     ALOGD("Input stream already exists, new stream not permitted: useCase:%s, devices:0x%x, module:%p",
                         itDev->useCase, itDev->devices, itDev->module);
@@ -1041,7 +1072,7 @@
                 }
 #endif
             }
-#if 0
+#ifdef QCOM_FM_ENABLED
         else if ((0 == strncmp(itDev->useCase, SND_USE_CASE_VERB_FM_A2DP_REC, MAX_UC_LEN))
                 ||(0 == strncmp(itDev->useCase, SND_USE_CASE_MOD_CAPTURE_A2DP_FM, MAX_UC_LEN)))
              {
@@ -1093,7 +1124,7 @@
                                 sizeof(alsa_handle.useCase));
                     }
                 }
-#if 0
+#ifdef QCOM_FM_ENABLED
             } else if((devices == AudioSystem::DEVICE_IN_FM_RX)) {
                 strlcpy(alsa_handle.useCase, SND_USE_CASE_MOD_CAPTURE_FM, sizeof(alsa_handle.useCase));
             } else if(devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
@@ -1127,7 +1158,7 @@
                                sizeof(alsa_handle.useCase));
                     }
                 }
-#if 0
+#ifdef QCOM_FM_ENABLED
             } else if(devices == AudioSystem::DEVICE_IN_FM_RX) {
                 strlcpy(alsa_handle.useCase, SND_USE_CASE_VERB_FM_REC, sizeof(alsa_handle.useCase));
             } else if (devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
@@ -1144,9 +1175,9 @@
         //update channel info before do routing
         if(channels) {
             it->channels = AudioSystem::popCount((*channels) &
-                      (AudioSystem::CHANNEL_IN_STEREO 
-                       | AudioSystem::CHANNEL_IN_MONO 
-#ifdef SSR_ENABLED
+                      (AudioSystem::CHANNEL_IN_STEREO
+                       | AudioSystem::CHANNEL_IN_MONO
+#ifdef QCOM_SSR_ENABLED
                        | AudioSystem::CHANNEL_IN_5POINT1
 #endif
                        ));
@@ -1154,7 +1185,7 @@
         }
         if (devices == AudioSystem::DEVICE_IN_VOICE_CALL){
            /* Add current devices info to devices to do route */
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
             if(mCurDevice == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET ||
                mCurDevice == AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET){
                 ALOGD("Routing everything from proxy for VOIP call");
@@ -1166,7 +1197,7 @@
             }
             mALSADevice->route(&(*it), route_devices, mode());
         } else {
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
             if(devices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET ||
                devices & AudioSystem::DEVICE_IN_PROXY) {
                 devices |= AudioSystem::DEVICE_IN_PROXY;
@@ -1180,7 +1211,7 @@
         }
 
         if(!strcmp(it->useCase, SND_USE_CASE_VERB_HIFI_REC) ||
-#if 0
+#ifdef QCOM_FM_ENABLED
            !strcmp(it->useCase, SND_USE_CASE_VERB_FM_REC) ||
            !strcmp(it->useCase, SND_USE_CASE_VERB_FM_A2DP_REC) ||
 #endif
@@ -1194,7 +1225,7 @@
         if(sampleRate) {
             it->sampleRate = *sampleRate;
         }
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
         if (6 == it->channels) {
             if (!strncmp(it->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
                 || !strncmp(it->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
@@ -1238,9 +1269,9 @@
               mMicMute = state;
               ALOGD("setMicMute: mMicMute %d", mMicMute);
               if(mALSADevice) {
-                 if(mCSCallActive == AudioSystem::CS_ACTIVE)
+                 if(mCSCallActive == CS_ACTIVE)
                     mALSADevice->setMicMute(state);
-                 if(mVolteCallActive == AudioSystem::IMS_ACTIVE)
+                 if(mVolteCallActive == IMS_ACTIVE)
                     mALSADevice->setVoLTEMicMute(state);
               }
         }
@@ -1270,9 +1301,12 @@
     if (format != AudioSystem::PCM_16_BIT
         && format != AudioSystem::AMR_NB
         && format != AudioSystem::AMR_WB
+#ifdef QCOM_QCHAT_ENABLED
         && format != AudioSystem::EVRC
         && format != AudioSystem::EVRCB
-        && format != AudioSystem::EVRCWB) {
+        && format != AudioSystem::EVRCWB
+#endif
+        ) {
          ALOGW("getInputBufferSize bad format: %d", format);
          return 0;
     }
@@ -1286,7 +1320,7 @@
     return bufferSize;
 }
 
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
 void AudioHardwareALSA::handleFm(int device)
 {
 int newMode = mode();
@@ -1375,6 +1409,7 @@
             break;
         }
     }
+#ifdef QCOM_USBAUDIO_ENABLED
    if(musbPlaybackState & USBPLAYBACKBIT_VOICECALL) {
           ALOGE("Voice call ended on USB");
           musbPlaybackState &= ~USBPLAYBACKBIT_VOICECALL;
@@ -1382,6 +1417,7 @@
           closeUsbRecordingIfNothingActive();
           closeUsbPlaybackIfNothingActive();
    }
+#endif
 }
 void AudioHardwareALSA::enableVoiceCall(char* verb, char* modifier, int mode, int device)
 {
@@ -1412,7 +1448,7 @@
     mDeviceList.push_back(alsa_handle);
     ALSAHandleList::iterator it = mDeviceList.end();
     it--;
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
     if((device & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
        (device & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)){
         device |= AudioSystem::DEVICE_OUT_PROXY;
@@ -1426,6 +1462,7 @@
         snd_use_case_set(mUcMgr, "_enamod", modifier);
     }
     mALSADevice->startVoiceCall(&(*it));
+#ifdef QCOM_USBAUDIO_ENABLED
     if((device & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
        (device & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)){
        startUsbRecordingIfNotStarted();
@@ -1433,6 +1470,7 @@
        musbPlaybackState |= USBPLAYBACKBIT_VOICECALL;
        musbRecordingState |= USBRECBIT_VOICECALL;
     }
+#endif
 }
 
 bool AudioHardwareALSA::routeVoiceCall(int device, int newMode)
@@ -1440,23 +1478,23 @@
 int csCallState = mCallState&0xF;
  bool isRouted = false;
  switch (csCallState) {
-    case AudioSystem::CS_INACTIVE:
-        if (mCSCallActive != AudioSystem::CS_INACTIVE) {
+    case CS_INACTIVE:
+        if (mCSCallActive != CS_INACTIVE) {
             ALOGD("doRouting: Disabling voice call");
             disableVoiceCall((char *)SND_USE_CASE_VERB_VOICECALL,
                 (char *)SND_USE_CASE_MOD_PLAY_VOICE, newMode, device);
             isRouted = true;
-            mCSCallActive = AudioSystem::CS_INACTIVE;
+            mCSCallActive = CS_INACTIVE;
         }
     break;
-    case AudioSystem::CS_ACTIVE:
-        if (mCSCallActive == AudioSystem::CS_INACTIVE) {
+    case CS_ACTIVE:
+        if (mCSCallActive == CS_INACTIVE) {
             ALOGD("doRouting: Enabling CS voice call ");
             enableVoiceCall((char *)SND_USE_CASE_VERB_VOICECALL,
                 (char *)SND_USE_CASE_MOD_PLAY_VOICE, newMode, device);
             isRouted = true;
-            mCSCallActive = AudioSystem::CS_ACTIVE;
-        } else if (mCSCallActive == AudioSystem::CS_HOLD) {
+            mCSCallActive = CS_ACTIVE;
+        } else if (mCSCallActive == CS_HOLD) {
              ALOGD("doRouting: Resume voice call from hold state");
              ALSAHandleList::iterator vt_it;
              for(vt_it = mDeviceList.begin();
@@ -1466,7 +1504,7 @@
                      (!strncmp(vt_it->useCase, SND_USE_CASE_MOD_PLAY_VOICE,
                      strlen(SND_USE_CASE_MOD_PLAY_VOICE)))) {
                      alsa_handle_t *handle = (alsa_handle_t *)(&(*vt_it));
-                     mCSCallActive = AudioSystem::CS_ACTIVE;
+                     mCSCallActive = CS_ACTIVE;
                      if(ioctl((int)handle->handle->fd,SNDRV_PCM_IOCTL_PAUSE,0)<0)
                                    ALOGE("VoLTE resume failed");
                      break;
@@ -1474,8 +1512,8 @@
              }
         }
     break;
-    case AudioSystem::CS_HOLD:
-        if (mCSCallActive == AudioSystem::CS_ACTIVE) {
+    case CS_HOLD:
+        if (mCSCallActive == CS_ACTIVE) {
             ALOGD("doRouting: Voice call going to Hold");
              ALSAHandleList::iterator vt_it;
              for(vt_it = mDeviceList.begin();
@@ -1484,7 +1522,7 @@
                      strlen(SND_USE_CASE_VERB_VOICECALL))) ||
                      (!strncmp(vt_it->useCase, SND_USE_CASE_MOD_PLAY_VOICE,
                          strlen(SND_USE_CASE_MOD_PLAY_VOICE)))) {
-                         mCSCallActive = AudioSystem::CS_HOLD;
+                         mCSCallActive = CS_HOLD;
                          alsa_handle_t *handle = (alsa_handle_t *)(&(*vt_it));
                          if(ioctl((int)handle->handle->fd,SNDRV_PCM_IOCTL_PAUSE,1)<0)
                                    ALOGE("Voice pause failed");
@@ -1501,23 +1539,23 @@
 int volteCallState = mCallState&0xF0;
 bool isRouted = false;
 switch (volteCallState) {
-    case AudioSystem::IMS_INACTIVE:
-        if (mVolteCallActive != AudioSystem::IMS_INACTIVE) {
+    case IMS_INACTIVE:
+        if (mVolteCallActive != IMS_INACTIVE) {
             ALOGD("doRouting: Disabling IMS call");
             disableVoiceCall((char *)SND_USE_CASE_VERB_VOLTE,
                 (char *)SND_USE_CASE_MOD_PLAY_VOLTE, newMode, device);
             isRouted = true;
-            mVolteCallActive = AudioSystem::IMS_INACTIVE;
+            mVolteCallActive = IMS_INACTIVE;
         }
     break;
-    case AudioSystem::IMS_ACTIVE:
-        if (mVolteCallActive == AudioSystem::IMS_INACTIVE) {
+    case IMS_ACTIVE:
+        if (mVolteCallActive == IMS_INACTIVE) {
             ALOGD("doRouting: Enabling IMS voice call ");
             enableVoiceCall((char *)SND_USE_CASE_VERB_VOLTE,
                 (char *)SND_USE_CASE_MOD_PLAY_VOLTE, newMode, device);
             isRouted = true;
-            mVolteCallActive = AudioSystem::IMS_ACTIVE;
-        } else if (mVolteCallActive == AudioSystem::IMS_HOLD) {
+            mVolteCallActive = IMS_ACTIVE;
+        } else if (mVolteCallActive == IMS_HOLD) {
              ALOGD("doRouting: Resume IMS call from hold state");
              ALSAHandleList::iterator vt_it;
              for(vt_it = mDeviceList.begin();
@@ -1527,7 +1565,7 @@
                      (!strncmp(vt_it->useCase, SND_USE_CASE_MOD_PLAY_VOLTE,
                      strlen(SND_USE_CASE_MOD_PLAY_VOLTE)))) {
                      alsa_handle_t *handle = (alsa_handle_t *)(&(*vt_it));
-                     mVolteCallActive = AudioSystem::IMS_ACTIVE;
+                     mVolteCallActive = IMS_ACTIVE;
                      if(ioctl((int)handle->handle->fd,SNDRV_PCM_IOCTL_PAUSE,0)<0)
                                    ALOGE("VoLTE resume failed");
                      break;
@@ -1535,8 +1573,8 @@
              }
         }
     break;
-    case AudioSystem::IMS_HOLD:
-        if (mVolteCallActive == AudioSystem::IMS_ACTIVE) {
+    case IMS_HOLD:
+        if (mVolteCallActive == IMS_ACTIVE) {
              ALOGD("doRouting: IMS ACTIVE going to HOLD");
              ALSAHandleList::iterator vt_it;
              for(vt_it = mDeviceList.begin();
@@ -1545,7 +1583,7 @@
                      strlen(SND_USE_CASE_VERB_VOLTE))) ||
                      (!strncmp(vt_it->useCase, SND_USE_CASE_MOD_PLAY_VOLTE,
                          strlen(SND_USE_CASE_MOD_PLAY_VOLTE)))) {
-                          mVolteCallActive = AudioSystem::IMS_HOLD;
+                          mVolteCallActive = IMS_HOLD;
                          alsa_handle_t *handle = (alsa_handle_t *)(&(*vt_it));
                          if(ioctl((int)handle->handle->fd,SNDRV_PCM_IOCTL_PAUSE,1)<0)
                                    ALOGE("VoLTE Pause failed");
diff --git a/alsa_sound/AudioHardwareALSA.h b/alsa_sound/AudioHardwareALSA.h
index ca77f58..9cc22d7 100644
--- a/alsa_sound/AudioHardwareALSA.h
+++ b/alsa_sound/AudioHardwareALSA.h
@@ -27,7 +27,9 @@
 #include <system/audio.h>
 #include <hardware/audio.h>
 #include <utils/threads.h>
+#ifdef QCOM_USBAUDIO_ENABLED
 #include <AudioUsbALSA.h>
+#endif
 
 extern "C" {
    #include <sound/asound.h>
@@ -91,8 +93,7 @@
 #define ANC_FLAG        0x00000001
 #define DMIC_FLAG       0x00000002
 #define QMIC_FLAG       0x00000004
-
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
 #define SSRQMIC_FLAG    0x00000008
 #endif
 
@@ -104,6 +105,7 @@
 
 #define LPA_SESSION_ID 1
 #define TUNNEL_SESSION_ID 2
+#ifdef QCOM_USBAUDIO_ENABLED
 static int USBPLAYBACKBIT_MUSIC = (1 << 0);
 static int USBPLAYBACKBIT_VOICECALL = (1 << 1);
 static int USBPLAYBACKBIT_VOIPCALL = (1 << 2);
@@ -114,12 +116,13 @@
 static int USBRECBIT_VOICECALL = (1 << 1);
 static int USBRECBIT_VOIPCALL = (1 << 2);
 static int USBRECBIT_FM = (1 << 3);
+#endif
 
 #define DEVICE_SPEAKER_HEADSET "Speaker Headset"
 #define DEVICE_HEADSET "Headset"
 #define DEVICE_HEADPHONES "Headphones"
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
 #define COEFF_ARRAY_SIZE          4
 #define FILT_SIZE                 ((512+1)* 6)    /* # ((FFT bins)/2+1)*numOutputs */
 #define SSR_FRAME_SIZE            512
@@ -138,6 +141,16 @@
     INCALL_REC_STEREO,
 };
 
+enum audio_call_mode {
+    CS_INACTIVE   = 0x0,
+    CS_ACTIVE     = 0x1,
+    CS_HOLD       = 0x2,
+    IMS_INACTIVE  = 0x0,
+    IMS_ACTIVE    = 0x10,
+    IMS_HOLD      = 0x20
+};
+
+
 struct alsa_handle_t {
     alsa_device_t *     module;
     uint32_t            devices;
@@ -374,7 +387,7 @@
 
     status_t            open(int mode);
     status_t            close();
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     // Helper function to initialize the Surround Sound library.
     status_t initSurroundSoundLibrary(unsigned long buffersize);
 #endif
@@ -385,7 +398,7 @@
     unsigned int        mFramesLost;
     AudioSystem::audio_in_acoustics mAcoustics;
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     // Function to read coefficients from files.
     status_t            readCoeffsFromFile();
 
@@ -426,7 +439,7 @@
      * the software mixer will emulate this capability.
      */
     virtual status_t    setMasterVolume(float volume);
-#ifdef FM_ENABLED
+#ifdef QCOM_FM_ENABLED
     virtual status_t    setFmVolume(float volume);
 #endif
     /**
@@ -448,6 +461,7 @@
     // parameters is not supported
     virtual size_t    getInputBufferSize(uint32_t sampleRate, int format, int channels);
 
+#ifdef QCOM_TUNNEL_LPA_ENABLED
     /** This method creates and opens the audio hardware output
       *  session for LPA */
     virtual AudioStreamOut* openOutputSession(
@@ -458,6 +472,7 @@
             uint32_t samplingRate=0,
             uint32_t channels=0);
     virtual void closeOutputSession(AudioStreamOut* out);
+#endif
 
     /** This method creates and opens the audio hardware output stream */
     virtual AudioStreamOut* openOutputStream(
@@ -492,15 +507,17 @@
     virtual status_t    dump(int fd, const Vector<String16>& args);
     virtual uint32_t    getVoipMode(int format);
     void                doRouting(int device);
-#ifdef EM_ENABLED
+#ifdef QCOM_FM_ENABLED
     void                handleFm(int device);
 #endif
+#ifdef QCOM_USBAUDIO_ENABLED
     void                closeUSBPlayback();
     void                closeUSBRecording();
     void                closeUsbRecordingIfNothingActive();
     void                closeUsbPlaybackIfNothingActive();
     void                startUsbPlaybackIfNotStarted();
     void                startUsbRecordingIfNotStarted();
+#endif
 
     void                disableVoiceCall(char* verb, char* modifier, int mode, int device);
     void                enableVoiceCall(char* verb, char* modifier, int mode, int device);
@@ -514,7 +531,9 @@
 
     ALSAHandleList      mDeviceList;
 
+#ifdef QCOM_USBAUDIO_ENABLED
     AudioUsbALSA        *mAudioUsbALSA;
+#endif
 
     Mutex                   mLock;
 
@@ -536,9 +555,10 @@
     int mIsFmActive;
     bool mBluetoothVGS;
     bool mFusion3Platform;
-
+#ifdef QCOM_USBAUDIO_ENABLED
     int musbPlaybackState;
     int musbRecordingState;
+#endif
 };
 
 // ----------------------------------------------------------------------------
diff --git a/alsa_sound/AudioPolicyManagerALSA.cpp b/alsa_sound/AudioPolicyManagerALSA.cpp
index a6c2e73..98f8203 100644
--- a/alsa_sound/AudioPolicyManagerALSA.cpp
+++ b/alsa_sound/AudioPolicyManagerALSA.cpp
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "audio_policy.msm8960"
+#define LOG_TAG "AudioPolicyManagerALSA"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
 
 #include "AudioPolicyManagerALSA.h"
@@ -25,758 +26,16 @@
 namespace android_audio_legacy {
 
 // ----------------------------------------------------------------------------
-audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
-{
-    uint32_t device = 0;
-
-    if (fromCache) {
-        ALOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
-        return mDeviceForStrategy[strategy];
-    }
-
-    switch (strategy) {
-    case STRATEGY_DTMF:
-        if (!isInCall()) {
-            // when off call, DTMF strategy follows the same rules as MEDIA strategy
-            device = getDeviceForStrategy(STRATEGY_MEDIA, false);
-            break;
-        }
-        // when in call, DTMF and PHONE strategies follow the same rules
-        // FALL THROUGH
-
-    case STRATEGY_PHONE:
-        // for phone strategy, we first consider the forced use and then the available devices by order
-        // of priority
-        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
-        case AudioSystem::FORCE_BT_SCO:
-            if (!isInCall() || strategy != STRATEGY_DTMF) {
-                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
-                if (device) break;
-            }
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
-            if (device) break;
-            // if SCO device is requested but no SCO device is available, fall back to default case
-            // FALL THROUGH
-
-        default:    // FORCE_NONE
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_PROXY;
-            if (device) break;
-#ifdef WITH_A2DP
-            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
-            if (!isInCall()) {
-                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
-                if (device) break;
-                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
-                if (device) break;
-            }
-#endif
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
-            if (device == 0) {
-                ALOGE("getDeviceForStrategy() earpiece device not found");
-            }
-            break;
-
-        case AudioSystem::FORCE_SPEAKER:
-            if (!isInCall() || strategy != STRATEGY_DTMF) {
-                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
-                if (device) break;
-            }
-#ifdef WITH_A2DP
-            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
-            // A2DP speaker when forcing to speaker output
-             if (!isInCall()) {
-                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
-                if (device) break;
-            }
-#endif
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-            if (device) break;
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
-            if (device == 0) {
-                ALOGE("getDeviceForStrategy() speaker device not found");
-            }
-            break;
-        }
-#ifdef FM_ENABLED
-        if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) {
-            device |= AudioSystem::DEVICE_OUT_FM;
-            if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
-                device &= ~(AudioSystem::DEVICE_OUT_WIRED_HEADSET);
-                device &= ~(AudioSystem::DEVICE_OUT_WIRED_HEADPHONE);
-                device |= AudioSystem::DEVICE_OUT_SPEAKER;
-            }
-        }
-#endif
-    break;
-
-    case STRATEGY_SONIFICATION:
-
-        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
-        // handleIncallSonification().
-        if (isInCall()) {
-            device = getDeviceForStrategy(STRATEGY_PHONE, false);
-            break;
-        }
-        // FALL THROUGH
-
-    case STRATEGY_ENFORCED_AUDIBLE:
-        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
-        // except when in call where it doesn't default to STRATEGY_PHONE behavior
-
-        device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
-        if (device == 0) {
-            ALOGE("getDeviceForStrategy() speaker device not found");
-        }
-        // The second device used for sonification is the same as the device used by media strategy
-        // FALL THROUGH
-
-    case STRATEGY_MEDIA: {
-        //To route FM stream to speaker when headset is connected, a new switch case is added.
-        //case AudioSystem::FORCE_SPEAKER for STRATEGY_MEDIA will come only when we need to route
-        //FM stream to speaker.
-        switch (mForceUse[AudioSystem::FOR_MEDIA]) {
-        default:{
-            uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_PROXY;
-            if(device2 != 0) {
-                // No combo device allowed with proxy device
-                device = 0;
-            }
-    #ifdef WITH_A2DP
-            if (mA2dpOutput != 0) {
-                if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
-                    break;
-                }
-                if (device2 == 0) {
-                    device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
-                }
-                if (device2 == 0) {
-                    device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
-                }
-                if (device2 == 0) {
-                    device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
-                }
-            }
-    #endif
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM_TX;
-            }
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
-            }
-
-            if (device2 == 0) {
-                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
-            }
-
-            // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
-            // STRATEGY_ENFORCED_AUDIBLE, 0 otherwise
-            device |= device2;
-        }
-        break;
-        case AudioSystem::FORCE_SPEAKER:
-            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
-            break;
-        }
-#ifdef FM_ENABLED
-        if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) {
-            device |= AudioSystem::DEVICE_OUT_FM;
-        }
-#endif
-        if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET) {
-            device |= AudioSystem::DEVICE_OUT_ANC_HEADSET;
-        }
-        if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE) {
-            device |= AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
-        }
-        // Do not play media stream if in call and the requested device would change the hardware
-        // output routing
-        if (mPhoneState == AudioSystem::MODE_IN_CALL &&
-            !AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device) &&
-            device != getDeviceForStrategy(STRATEGY_PHONE, true)) {
-            device = getDeviceForStrategy(STRATEGY_PHONE, true);
-            ALOGV("getDeviceForStrategy() incompatible media and phone devices");
-        }
-        } break;
-
-    default:
-        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
-        break;
-    }
-
-    ALOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
-    return (audio_devices_t)device;
-}
-
-
-status_t AudioPolicyManager::setDeviceConnectionState(AudioSystem::audio_devices device,
-                                                      AudioSystem::device_connection_state state,
-                                                      const char *device_address)
-{
-
-    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
-
-    // connect/disconnect only 1 device at a time
-    if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
-
-    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
-        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
-        return BAD_VALUE;
-    }
-
-    // handle output devices
-    if (AudioSystem::isOutputDevice(device)) {
-
-#ifndef WITH_A2DP
-        if (AudioSystem::isA2dpDevice(device)) {
-            ALOGE("setDeviceConnectionState() invalid device: %x", device);
-            return BAD_VALUE;
-        }
-#endif
-
-        switch (state)
-        {
-        // handle output device connection
-        case AudioSystem::DEVICE_STATE_AVAILABLE:
-            if (mAvailableOutputDevices & device) {
-                ALOGW("setDeviceConnectionState() device already connected: %x", device);
-                return INVALID_OPERATION;
-            }
-            ALOGV("setDeviceConnectionState() connecting device %x", device);
-
-            // register new device as available
-            mAvailableOutputDevices = (audio_devices_t)((uint32_t)mAvailableOutputDevices | device);
-
-#ifdef WITH_A2DP
-            // handle A2DP device connection
-            if (AudioSystem::isA2dpDevice(device)) {
-                status_t status = AudioPolicyManagerBase::handleA2dpConnection(device, device_address);
-                if (status != NO_ERROR) {
-                    mAvailableOutputDevices &= ~device;
-                    return status;
-                }
-            } else
-#endif
-            {
-                if (AudioSystem::isBluetoothScoDevice(device)) {
-                    ALOGV("setDeviceConnectionState() BT SCO  device, address %s", device_address);
-                    // keep track of SCO device address
-                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
-                }
-            }
-            break;
-        // handle output device disconnection
-        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
-            if (!(mAvailableOutputDevices & device)) {
-                ALOGW("setDeviceConnectionState() device not connected: %x", device);
-                return INVALID_OPERATION;
-            }
-
-
-            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
-            // remove device from available output devices
-            mAvailableOutputDevices = (audio_devices_t)((uint32_t)mAvailableOutputDevices & ~device);
-
-#ifdef WITH_A2DP
-            // handle A2DP device disconnection
-            if (AudioSystem::isA2dpDevice(device)) {
-                status_t status = AudioPolicyManagerBase::handleA2dpDisconnection(device, device_address);
-                if (status != NO_ERROR) {
-                    mAvailableOutputDevices = (audio_devices_t)((uint32_t)mAvailableOutputDevices | device);
-                    return status;
-                }
-            } else
-#endif
-            {
-                if (AudioSystem::isBluetoothScoDevice(device)) {
-                    mScoDeviceAddress = "";
-                }
-            }
-            } break;
-
-        default:
-            ALOGE("setDeviceConnectionState() invalid state: %x", state);
-            return BAD_VALUE;
-        }
-
-        // request routing change if necessary
-        uint32_t newDevice = AudioPolicyManagerBase::getNewDevice(mPrimaryOutput, false);
-
-        if(newDevice == 0 && mLPADecodeOutput != -1) {
-            newDevice = AudioPolicyManagerBase::getNewDevice(mLPADecodeOutput, false);
-        }
-#ifdef FM_ENABLED
-        if(device == AudioSystem::DEVICE_OUT_FM) {
-            if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
-                mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::FM, 1);
-            }
-            else {
-                mOutputs.valueFor(mPrimaryOutput)->changeRefCount(AudioSystem::FM, -1);
-            }
-            if(newDevice == 0){
-                newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
-            }
-            AudioParameter param = AudioParameter();
-            param.addInt(String8(AudioParameter::keyHandleFm), (int)newDevice);
-            mpClientInterface->setParameters(mPrimaryOutput, param.toString());
-        }
-#endif
-        if(device == AudioSystem::DEVICE_OUT_ANC_HEADPHONE ||
-           device == AudioSystem::DEVICE_OUT_ANC_HEADSET) {
-            if(newDevice == 0){
-                newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
-            }
-        }
-#ifdef WITH_A2DP
-        AudioPolicyManagerBase::checkA2dpSuspend();
-        AudioPolicyManagerBase::checkOutputForAllStrategies();
-        // A2DP outputs must be closed after checkOutputForAllStrategies() is executed
-        if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) {
-            AudioPolicyManagerBase::closeA2dpOutputs();
-        }
-#endif
-        AudioPolicyManagerBase::updateDeviceForStrategy();
-
-        int delayMs = 0;
-        if (AudioSystem::DEVICE_OUT_SPEAKER == newDevice
-            && AudioSystem::DEVICE_OUT_WIRED_HEADSET == device) {
-            //Add some delay(192ms) while switching from wired headset to speaker before pause
-            //music keeps playing from speaker if there's not delay or delay is too small.
-            delayMs = mOutputs.valueFor(mPrimaryOutput)->mLatency*8;
-        }
-
-        if(mLPADecodeOutput != -1)
-            setOutputDevice(mLPADecodeOutput, newDevice, false, delayMs);
-        setOutputDevice(mPrimaryOutput, newDevice, false, delayMs);
-
-        if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
-            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
-        } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
-                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
-                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
-            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
-        } else if(device == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET){
-            device = AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET;
-        } else if(device == AudioSystem::DEVICE_OUT_ANC_HEADSET){
-               device = AudioSystem::DEVICE_IN_ANC_HEADSET; //wait for actual ANC device
-        } else {
-            return NO_ERROR;
-        }
-    }
-    // handle input devices
-    if (AudioSystem::isInputDevice(device)) {
-
-        switch (state)
-        {
-        // handle input device connection
-        case AudioSystem::DEVICE_STATE_AVAILABLE: {
-            if (mAvailableInputDevices & device) {
-                ALOGW("setDeviceConnectionState() device already connected: %d", device);
-                return INVALID_OPERATION;
-            }
-            mAvailableInputDevices = (audio_devices_t)(mAvailableInputDevices | device);
-            }
-            break;
-
-        // handle input device disconnection
-        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
-            if (!(mAvailableInputDevices & device)) {
-                ALOGW("setDeviceConnectionState() device not connected: %d", device);
-                return INVALID_OPERATION;
-            }
-            mAvailableInputDevices = (audio_devices_t)((uint32_t)mAvailableInputDevices & ~device);
-            } break;
-
-        default:
-            ALOGE("setDeviceConnectionState() invalid state: %x", state);
-            return BAD_VALUE;
-        }
-
-        audio_io_handle_t activeInput = AudioPolicyManagerBase::getActiveInput();
-        if (activeInput != 0) {
-            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
-            uint32_t newDevice = AudioPolicyManagerBase::getDeviceForInputSource(inputDesc->mInputSource);
-            if (newDevice != inputDesc->mDevice) {
-                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
-                        inputDesc->mDevice, newDevice, activeInput);
-                inputDesc->mDevice = (audio_devices_t)newDevice;
-                AudioParameter param = AudioParameter();
-                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
-                mpClientInterface->setParameters(activeInput, param.toString());
-            }
-        }
-
-        return NO_ERROR;
-    }
-
-    ALOGW("setDeviceConnectionState() invalid device: %x", device);
-    return BAD_VALUE;
-}
-
-
-void AudioPolicyManager::setPhoneState(int state)
-{
-    ALOGD("setPhoneState() state %d", state);
-    uint32_t newDevice = 0;
-    if (state < 0 || state >= AudioSystem::NUM_MODES) {
-        ALOGW("setPhoneState() invalid state %d", state);
-        return;
-    }
-
-    if (state == mPhoneState ) {
-        ALOGW("setPhoneState() setting same state %d", state);
-        return;
-    }
-
-    // if leaving call state, handle special case of active streams
-    // pertaining to sonification strategy see handleIncallSonification()
-    if (isInCall()) {
-        ALOGV("setPhoneState() in call state management: new state is %d", state);
-        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
-            AudioPolicyManagerBase::handleIncallSonification(stream, false, true);
-        }
-    }
-
-    // store previous phone state for management of sonification strategy below
-    int oldState = mPhoneState;
-    mPhoneState = state;
-    // force routing command to audio hardware when starting call
-    // even if no device change is needed
-    bool force = (mPhoneState == AudioSystem::MODE_IN_CALL);
-
-    // are we entering or starting a call
-    if (!isStateInCall(oldState) && isStateInCall(state)) {
-        ALOGV("  Entering call in setPhoneState()");
-        // force routing command to audio hardware when starting a call
-        // even if no device change is needed
-        force = true;
-    } else if (isStateInCall(oldState) && (state == AudioSystem::MODE_NORMAL)) {
-        ALOGV("  Exiting call in setPhoneState()");
-        // force routing command to audio hardware when exiting a call
-        // even if no device change is needed
-        force = true;
-    } else if (isStateInCall(state) && (state != oldState)) {
-        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
-        // force routing command to audio hardware when switching between telephony and VoIP
-        // even if no device change is needed
-        force = true;
-    }
-
-    // check for device and output changes triggered by new phone state
-    newDevice = AudioPolicyManagerBase::getNewDevice(mPrimaryOutput, false);
-    if (newDevice == 0 && (mLPADecodeOutput != -1 &&
-        mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-        newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
-    }
-
-#ifdef WITH_A2DP
-    AudioPolicyManagerBase::checkA2dpSuspend();
-    AudioPolicyManagerBase::checkOutputForAllStrategies();
-#endif
-    AudioPolicyManagerBase::updateDeviceForStrategy();
-
-    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
-
-    // force routing command to audio hardware when ending call
-    // even if no device change is needed
-    if (isStateInCall(oldState) && newDevice == 0) {
-        newDevice = hwOutputDesc->device();
-        if(state == AudioSystem::MODE_NORMAL) {
-            force = true;
-        }
-    }
-
-    // when changing from ring tone to in call mode, mute the ringing tone
-    // immediately and delay the route change to avoid sending the ring tone
-    // tail into the earpiece or headset.
-    int delayMs = 0;
-    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
-        // delay the device change command by twice the output latency to have some margin
-        // and be sure that audio buffers not yet affected by the mute are out when
-        // we actually apply the route change
-        delayMs = hwOutputDesc->mLatency*2;
-        setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
-    }
-
-    // change routing is necessary
-    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
-
-    // if entering in call state, handle special case of active streams
-    // pertaining to sonification strategy see handleIncallSonification()
-    if (isStateInCall(state)) {
-        ALOGV("setPhoneState() in call state management: new state is %d", state);
-        // unmute the ringing tone after a sufficient delay if it was muted before
-        // setting output device above
-        if (oldState == AudioSystem::MODE_RINGTONE) {
-            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
-        }
-        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
-            AudioPolicyManagerBase::handleIncallSonification(stream, true, true);
-        }
-    }
-
-    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
-    if (state == AudioSystem::MODE_RINGTONE &&
-        (hwOutputDesc->mRefCount[AudioSystem::MUSIC] ||
-        (systemTime() - hwOutputDesc->mStopTime[AudioSystem::MUSIC]) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) {
-  //      (systemTime() - mMusicStopTime) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) {
-        mLimitRingtoneVolume = true;
-    } else {
-        mLimitRingtoneVolume = false;
-    }
-}
-
-#ifdef TUNNEL_LPA_ENABLED
-audio_io_handle_t AudioPolicyManager::getSession(AudioSystem::stream_type stream,
-                                    uint32_t format,
-                                    AudioSystem::output_flags flags,
-                                    int32_t sessionId,
-                                    uint32_t samplingRate,
-                                    uint32_t channels)
-{
-    audio_io_handle_t output = 0;
-    uint32_t latency = 0;
-    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
-    uint32_t device = getDeviceForStrategy(strategy);
-    ALOGV("getSession() stream %d, format %d, sessionId %x, flags %x device %d", stream, format, sessionId, flags, device);
-    AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
-    outputDesc->mDevice = device;
-    outputDesc->mSamplingRate = 0;
-    outputDesc->mFormat = format;
-    outputDesc->mChannels = 2;
-    outputDesc->mLatency = 0;
-    outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
-    outputDesc->mRefCount[stream] = 0;
-    output = mpClientInterface->openSession(&outputDesc->mDevice,
-                                    &outputDesc->mFormat,
-                                    outputDesc->mFlags,
-                                    stream,
-                                    sessionId,
-                                    samplingRate,
-                                    channels);
-
-    // only accept an output with the requeted parameters
-    if ((format != 0 && format != outputDesc->mFormat) || !output) {
-        ALOGE("openSession() failed opening a session: format %d, sessionId %d output %d",
-                format, sessionId, output);
-        if(output) {
-            mpClientInterface->closeSession(output);
-        }
-        delete outputDesc;
-        return 0;
-    }
-
-    //reset it here, it will get updated in startoutput
-    outputDesc->mDevice = 0;
-
-    mOutputs.add(output, outputDesc);
-    mLPADecodeOutput = output;
-    mLPAStreamType   = stream;
-    return output;
-}
-
-void AudioPolicyManager::pauseSession(audio_io_handle_t output, AudioSystem::stream_type stream)
-{
-    ALOGV("pauseSession() Output [%d] and stream is [%d]", output, stream);
-
-    if ( (output == mLPADecodeOutput) &&
-         (stream == mLPAStreamType) ) {
-        AudioPolicyManager::stopOutput(output, mLPAStreamType);
-        mLPAActiveOuput = mLPADecodeOutput;
-        mLPAActiveStreamType = mLPAStreamType;
-        mLPADecodeOutput = -1;
-        mLPAStreamType = AudioSystem::DEFAULT;
-        mLPAMuted = false;
-    }
-}
-
-void AudioPolicyManager::resumeSession(audio_io_handle_t output, AudioSystem::stream_type stream)
-{
-    ALOGV("resumeSession() Output [%d] and stream is [%d]", output, stream);
-
-    if (output == mLPAActiveOuput)
-    {
-        mLPADecodeOutput = mLPAActiveOuput;
-        mLPAStreamType = stream;
-        AudioPolicyManager::startOutput(mLPADecodeOutput, mLPAStreamType);
-
-        // Set Volume if the music stream volume is changed in the Pause state of LPA Jagan
-        mLPAActiveOuput = -1;
-        mLPAActiveStreamType = AudioSystem::DEFAULT;
-    }
-}
-
-void AudioPolicyManager::releaseSession(audio_io_handle_t output)
-{
-    ALOGV("releaseSession() %d", output);
-
-    // This means the Output is put into Pause state
-    if (output == mLPAActiveOuput && mLPADecodeOutput == -1) {
-        mLPADecodeOutput = mLPAActiveOuput;
-        mLPAStreamType = mLPAActiveStreamType;
-    }
-
-    ssize_t index = mOutputs.indexOfKey(output);
-    if (index < 0) {
-        ALOGW("releaseSession() releasing unknown output %d", output);
-        return;
-    }
-
-    AudioPolicyManager::stopOutput(output, mLPAStreamType);
-    delete mOutputs.valueAt(index);
-    mOutputs.removeItem(output);
-    mLPADecodeOutput = -1;
-    mLPAActiveOuput = -1;
-    mLPAStreamType = AudioSystem::DEFAULT;
-    mLPAActiveStreamType = AudioSystem::DEFAULT;
-    mLPAMuted = false;
-}
-#endif
-
-status_t AudioPolicyManager::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session)
-{
-    ALOGV("startOutput() output %d, stream %d", output, stream);
-    ssize_t index = mOutputs.indexOfKey(output);
-    if (index < 0) {
-        ALOGW("startOutput() unknow output %d", output);
-        return BAD_VALUE;
-    }
-
-    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
-    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
-
-#ifdef WITH_A2DP
-    if (mA2dpOutput != 0  && !a2dpUsedForSonification() &&
-            (strategy == STRATEGY_SONIFICATION || strategy == STRATEGY_ENFORCED_AUDIBLE)) {
-        setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
-    }
-#endif
-
-    // incremenent usage count for this stream on the requested output:
-    // NOTE that the usage count is the same for duplicated output and hardware output which is
-    // necassary for a correct control of hardware output routing by startOutput() and stopOutput()
-    outputDesc->changeRefCount(stream, 1);
-#ifdef FM_ENABLED
-    if(stream == AudioSystem::FM && output == mA2dpOutput) {
-        setOutputDevice(output, AudioPolicyManagerBase::getNewDevice(output, true), true);
-    } else
-#endif
-#ifdef TUNNEL_LPA_ENABLED
-    if (output != mLPADecodeOutput) {
-        setOutputDevice(output, AudioPolicyManagerBase::getNewDevice(output, true));
-    } else
-#endif 
-    {
-        setOutputDevice(output, AudioPolicyManagerBase::getNewDevice(output, true), true);
-    }
-
-    // handle special case for sonification while in call
-    if (isInCall()) {
-        AudioPolicyManagerBase::handleIncallSonification(stream, true, false);
-    }
-
-    // apply volume rules for current stream and device if necessary
-    checkAndSetVolume((int)stream, mStreams[stream].mIndexCur.valueFor(outputDesc->device()), output, outputDesc->device());
-
-    return NO_ERROR;
-}
-
-status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session)
-{
-    ALOGV("stopOutput() output %d, stream %d", output, stream);
-    ssize_t index = mOutputs.indexOfKey(output);
-    if (index < 0) {
-        ALOGW("stopOutput() unknow output %d", output);
-        return BAD_VALUE;
-    }
-
-    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
-    routing_strategy strategy = AudioPolicyManagerBase::getStrategy((AudioSystem::stream_type)stream);
-
-    // handle special case for sonification while in call
-    if (isInCall()) {
-        AudioPolicyManagerBase::handleIncallSonification(stream, false, false);
-    }
-
-    if (outputDesc->mRefCount[stream] > 0) {
-        // decrement usage count of this stream on the output
-        outputDesc->changeRefCount(stream, -1);
-        // store time at which the last music track was stopped - see computeVolume()
-        if (stream == AudioSystem::MUSIC) {
-            outputDesc->mStopTime[stream] = systemTime();
-           // mMusicStopTime = systemTime();
-        }
-
-        uint32_t newDevice = AudioPolicyManagerBase::getNewDevice(mPrimaryOutput, false);
-
-        if(newDevice == 0 && mLPADecodeOutput != -1) {
-            newDevice = AudioPolicyManagerBase::getNewDevice(mLPADecodeOutput, false);
-        }
-
-        setOutputDevice(output, newDevice);
-
-#ifdef WITH_A2DP
-        if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
-                (strategy == STRATEGY_SONIFICATION || strategy == STRATEGY_ENFORCED_AUDIBLE)) {
-            setStrategyMute(STRATEGY_MEDIA,
-                            false,
-                            mA2dpOutput,
-                            mOutputs.valueFor(mPrimaryOutput)->mLatency*2);
-        }
-#endif
-        if (output != mPrimaryOutput) {
-            setOutputDevice(mPrimaryOutput, AudioPolicyManagerBase::getNewDevice(mPrimaryOutput, true), true);
-        }
-        return NO_ERROR;
-    } else {
-        ALOGW("stopOutput() refcount is already 0 for output %d", output);
-        return INVALID_OPERATION;
-    }
-}
-// ----------------------------------------------------------------------------
 // AudioPolicyManagerALSA
 // ----------------------------------------------------------------------------
 
+//Compiling error seen (only) on mako-pdk project if AudioParamer doesn't exist in this file
+//No issue is seen on QCOM jb-mailine if remvong this line
+//AudioParameter param;
+
 // ---  class factory
 
+
 extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
 {
     return new AudioPolicyManager(clientInterface);
@@ -787,365 +46,4 @@
     delete interface;
 }
 
-// ---
-
-void AudioPolicyManager::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
-{
-    ALOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
-    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
-
-
-    if (outputDesc->isDuplicated()) {
-        setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
-        setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
-        return;
-    }
-#ifdef WITH_A2DP
-    // filter devices according to output selected
-    if (output == mA2dpOutput) {
-        device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
-    } else {
-        device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
-    }
-#endif
-
-    uint32_t prevDevice = (uint32_t)outputDesc->device();
-    // Do not change the routing if:
-    //  - the requestede device is 0
-    //  - the requested device is the same as current device and force is not specified.
-    // Doing this check here allows the caller to call setOutputDevice() without conditions
-    if ((device == 0 || device == prevDevice) && !force) {
-        ALOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
-        return;
-    }
-
-    outputDesc->mDevice = (audio_devices_t)device;
-    // mute media streams if both speaker and headset are selected
-    if (device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADSET)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADPHONE)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET | AudioSystem::DEVICE_OUT_FM)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE | AudioSystem::DEVICE_OUT_FM)
-        || device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_FM_TX)) {
-        setStrategyMute(STRATEGY_MEDIA, true, output);
-        // Mute LPA output also if it belongs to STRATEGY_MEDIA
-        if(((mLPADecodeOutput != -1) && (mLPADecodeOutput != output) &&
-            mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-            ALOGV("setOutputDevice: Muting mLPADecodeOutput:%d",mLPADecodeOutput);
-            setStrategyMute(STRATEGY_MEDIA, true, mLPADecodeOutput);
-        }
-        // Mute hardware output also if it belongs to STRATEGY_MEDIA
-        if(((mPrimaryOutput != -1) && (mPrimaryOutput != output) &&
-            mOutputs.valueFor(mPrimaryOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-            ALOGV("setOutputDevice: Muting mPrimaryOutput:%d",mPrimaryOutput);
-            setStrategyMute(STRATEGY_MEDIA, true, mPrimaryOutput);
-        }
-        // wait for the PCM output buffers to empty before proceeding with the rest of the command
-        usleep(outputDesc->mLatency*2*1000);
-    }
-
-    // wait for output buffers to be played on the HDMI device before routing to new device
-    if(prevDevice == AudioSystem::DEVICE_OUT_AUX_DIGITAL) {
-        if((mLPADecodeOutput != -1 && output == mLPADecodeOutput &&
-            mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-            checkAndSetVolume(AudioSystem::MUSIC,
-                              mStreams[AudioSystem::MUSIC].mIndexCur.valueFor((audio_devices_t)device),
-                              mLPADecodeOutput, (audio_devices_t)device, delayMs, force);
-            usleep(150*1000);
-        } else {
-            checkAndSetVolume(AudioSystem::MUSIC,
-                              mStreams[AudioSystem::MUSIC].mIndexCur.valueFor((audio_devices_t)device),
-                              output, (audio_devices_t)device, delayMs, force);
-            usleep(outputDesc->mLatency*6*1000);
-        }
-    }
-
-    // do the routing
-    AudioParameter param = AudioParameter();
-    param.addInt(String8(AudioParameter::keyRouting), (int)device);
-    mpClientInterface->setParameters(mPrimaryOutput, param.toString(), delayMs);
-    // update stream volumes according to new device
-    AudioPolicyManagerBase::applyStreamVolumes(output, (audio_devices_t)device, delayMs);
-    if((mLPADecodeOutput != -1 &&
-        mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-        AudioPolicyManagerBase::applyStreamVolumes(mLPADecodeOutput, (audio_devices_t)device, delayMs);
-    }
-
-    // if changing from a combined headset + speaker route, unmute media streams
-    if (prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADSET)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADPHONE)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET | AudioSystem::DEVICE_OUT_FM)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE | AudioSystem::DEVICE_OUT_FM)
-        || prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_FM_TX)) {
-        setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
-        // Unmute LPA output also if it belongs to STRATEGY_MEDIA
-        if(((mLPADecodeOutput != -1) && (mLPADecodeOutput != output) &&
-            mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-            ALOGV("setOutputDevice: Unmuting mLPADecodeOutput:%d delayMs:%d",mLPADecodeOutput,delayMs);
-            setStrategyMute(STRATEGY_MEDIA, false, mLPADecodeOutput, delayMs);
-        }
-        // Unmute hardware output also if it belongs to STRATEGY_MEDIA
-        if(((mPrimaryOutput != -1) && (mPrimaryOutput != output) &&
-            mOutputs.valueFor(mPrimaryOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
-            ALOGV("setOutputDevice: Unmuting mPrimaryOutput:%d delayMs:%d",mPrimaryOutput,delayMs);
-            setStrategyMute(STRATEGY_MEDIA, false, mPrimaryOutput, delayMs);
-        }
-    }
-}
-
-status_t AudioPolicyManager::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
-{
-    // do not change actual stream volume if the stream is muted
-    if ((mOutputs.valueFor(output)->mMuteCount[stream] != 0 && output != mLPADecodeOutput) ||
-        (output == mLPADecodeOutput && stream == mLPAStreamType && mLPAMuted == true)) {
-        ALOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
-        return NO_ERROR;
-    }
-
-    // do not change in call volume if bluetooth is connected and vice versa
-    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
-        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
-        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
-             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
-        return INVALID_OPERATION;
-    }
-
-    float volume = computeVolume(stream, index, output, (audio_devices_t)device);
-    // do not set volume if the float value did not change
-    if ((volume != mOutputs.valueFor(output)->mCurVolume[stream]) || (stream == AudioSystem::VOICE_CALL) ||
-#ifdef FM_ENABLED
-        (stream == AudioSystem::FM) ||
-#endif
-         force) {
-        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
-        ALOGD("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
-        if (stream == AudioSystem::VOICE_CALL ||
-            stream == AudioSystem::DTMF ||
-            stream == AudioSystem::BLUETOOTH_SCO) {
-            float voiceVolume = -1.0;
-            // offset value to reflect actual hardware volume that never reaches 0
-            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
-            volume = 0.01 + 0.99 * volume;
-            if (stream == AudioSystem::VOICE_CALL) {
-                voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
-            } else if (stream == AudioSystem::BLUETOOTH_SCO) {
-                String8 key ("bt_headset_vgs");
-                mpClientInterface->getParameters(output,key);
-                AudioParameter result(mpClientInterface->getParameters(0,key));
-                int value;
-                if(result.getInt(String8("isVGS"),value) == NO_ERROR){
-                   ALOGD("BT-SCO Voice Volume %f",(float)index/(float)mStreams[stream].mIndexMax);
-                   voiceVolume = 1.0;
-                } else {
-                   voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
-                }
-            }
-            if (voiceVolume >= 0 && output == mPrimaryOutput) {
-                mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
-            }
-        } 
-#ifdef FM_ENABLED
-        else if (stream == AudioSystem::FM) {
-            float fmVolume = -1.0;
-            fmVolume = computeVolume(stream, index, output, device);
-            if (fmVolume >= 0) {
-                if(output == mPrimaryOutput)
-                    mpClientInterface->setFmVolume(fmVolume, delayMs);
-                else if(output == mA2dpOutput)
-                    mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
-            }
-            return NO_ERROR;
-        }
-#endif
-        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
-    }
-
-    return NO_ERROR;
-}
-
-void AudioPolicyManager::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
-{
-    StreamDescriptor &streamDesc = mStreams[stream];
-    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
-
-    ALOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
-
-    if (on) {
-        if ((outputDesc->mMuteCount[stream] == 0 && output != mLPADecodeOutput) ||
-            (output == mLPADecodeOutput && stream == mLPAStreamType && false == mLPAMuted)) {
-            if (streamDesc.mCanBeMuted) {
-                checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
-            }
-        }
-        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
-        if(output == mLPADecodeOutput) {
-            if(stream == mLPAStreamType && false == mLPAMuted) {
-                mLPAMuted = true;
-            }
-        } else {
-            outputDesc->mMuteCount[stream]++;
-        }
-    } else {
-        if ((outputDesc->mMuteCount[stream] == 0 && output != mLPADecodeOutput) ||
-            (output == mLPADecodeOutput && stream == mLPAStreamType && false == mLPAMuted)) {
-            ALOGW("setStreamMute() unmuting non muted stream!");
-            return;
-        }
-        if(output == mLPADecodeOutput) {
-            if(stream == mLPAStreamType && true == mLPAMuted) {
-                mLPAMuted = false;
-                checkAndSetVolume(stream, streamDesc.mIndexCur.valueFor(outputDesc->device()), output, outputDesc->device(), delayMs);
-            }
-        } else {
-            if(--outputDesc->mMuteCount[stream] == 0){
-                checkAndSetVolume(stream, streamDesc.mIndexCur.valueFor(outputDesc->device()), output, outputDesc->device(), delayMs);
-            }
-        }
-    }
-}
-
-void AudioPolicyManager::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
-{
-    ALOGD("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
-
-    bool forceVolumeReeval = false;
-    switch(usage) {
-    case AudioSystem::FOR_COMMUNICATION:
-        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
-            config != AudioSystem::FORCE_NONE) {
-            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
-            return;
-        }
-        mForceUse[usage] = config;
-        break;
-    case AudioSystem::FOR_MEDIA:
-        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
-            config != AudioSystem::FORCE_WIRED_ACCESSORY && config != AudioSystem::FORCE_NONE &&
-            config != AudioSystem::FORCE_SPEAKER) {
-            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
-            return;
-        }
-        mForceUse[usage] = config;
-        {
-            uint32_t device = getDeviceForStrategy(STRATEGY_MEDIA, true);
-            setOutputDevice(mPrimaryOutput, device);
-        }
-        break;
-    case AudioSystem::FOR_RECORD:
-        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
-            config != AudioSystem::FORCE_NONE) {
-            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
-            return;
-        }
-        mForceUse[usage] = config;
-        break;
-    case AudioSystem::FOR_DOCK:
-        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
-            config != AudioSystem::FORCE_BT_DESK_DOCK && config != AudioSystem::FORCE_WIRED_ACCESSORY) {
-            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
-        }
-        forceVolumeReeval = true;
-        mForceUse[usage] = config;
-        break;
-    default:
-        ALOGW("setForceUse() invalid usage %d", usage);
-        break;
-    }
-
-    // check for device and output changes triggered by new phone state
-    uint32_t newDevice = getNewDevice(mPrimaryOutput, false);
-#ifdef WITH_A2DP
-    AudioPolicyManagerBase::checkA2dpSuspend();
-    checkOutputForAllStrategies();
-#endif
-    updateDeviceForStrategy();
-    setOutputDevice(mPrimaryOutput, newDevice);
-    if (forceVolumeReeval) {
-        applyStreamVolumes(mPrimaryOutput, (audio_devices_t)newDevice);
-    }
-}
-
-audio_devices_t AudioPolicyManager::getDeviceForInputSource(int inputSource)
-{
-    uint32_t device;
-
-    switch(inputSource) {
-    case AUDIO_SOURCE_DEFAULT:
-    case AUDIO_SOURCE_MIC:
-    case AUDIO_SOURCE_VOICE_RECOGNITION:
-        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
-            mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
-            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
-        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
-            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
-        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_ANC_HEADSET) {
-            device = AudioSystem::DEVICE_IN_ANC_HEADSET;
-        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) {
-            device = AudioSystem::DEVICE_IN_PROXY;
-        } else {
-            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
-        }
-        break;
-    case AUDIO_SOURCE_VOICE_COMMUNICATION:
-        device = AudioSystem::DEVICE_IN_COMMUNICATION;
-        break;
-    case AUDIO_SOURCE_CAMCORDER:
-        if (hasBackMicrophone()) {
-            device = AudioSystem::DEVICE_IN_BACK_MIC;
-        } else {
-            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
-        }
-        break;
-    case AUDIO_SOURCE_VOICE_UPLINK:
-    case AUDIO_SOURCE_VOICE_DOWNLINK:
-    case AUDIO_SOURCE_VOICE_CALL:
-        device = AudioSystem::DEVICE_IN_VOICE_CALL;
-        break;
-#if 0
-   case AUDIO_SOURCE_FM_RX:
-        device = AudioSystem::DEVICE_IN_FM_RX;
-        break;
-    case AUDIO_SOURCE_FM_RX_A2DP:
-        device = AudioSystem::DEVICE_IN_FM_RX_A2DP;
-        break;
-#endif
-    default:
-        ALOGW("getInput() invalid input source %d", inputSource);
-        device = 0;
-        break;
-    }
-    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
-    if (!device)
-        ALOGE("%s: did not select device for input source %d",
-              __FUNCTION__, inputSource);
-    return (audio_devices_t)device;
-}
-
-/*
-Overwriting this function from base class to allow 2 acitve AudioRecord clients in case of FM.
-One for FM A2DP playbck and other for FM recording.
-*/
-status_t AudioPolicyManager::startInput(audio_io_handle_t input)
-{
-    ALOGV("startInput() input %d", input);
-    ssize_t index = mInputs.indexOfKey(input);
-    if (index < 0) {
-        ALOGW("startInput() unknow input %d", input);
-        return BAD_VALUE;
-    }
-    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
-    AudioParameter param = AudioParameter();
-    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
-    // use Voice Recognition mode or not for this input based on input source
-    int vr_enabled = inputDesc->mInputSource == AUDIO_SOURCE_VOICE_RECOGNITION ? 1 : 0;
-    param.addInt(String8("vr_mode"), vr_enabled);
-    ALOGV("AudioPolicyManager::startInput(%d), setting vr_mode to %d", inputDesc->mInputSource, vr_enabled);
-    mpClientInterface->setParameters(input, param.toString());
-    inputDesc->mRefCount = 1;
-    return NO_ERROR;
-}
-
 }; // namespace androidi_audio_legacy
diff --git a/alsa_sound/AudioPolicyManagerALSA.h b/alsa_sound/AudioPolicyManagerALSA.h
index 5f8f63d..2a7dfdb 100644
--- a/alsa_sound/AudioPolicyManagerALSA.h
+++ b/alsa_sound/AudioPolicyManagerALSA.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2009 The Android Open Source Project
- * Copyright (C) 2011, Code Aurora Forum. All rights reserved.
+ * Copyright (C) 2011-2012, Code Aurora Forum. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,81 +28,14 @@
 
 // ----------------------------------------------------------------------------
 
-#define MAX_DEVICE_ADDRESS_LEN 20
-// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
-#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
-// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
-#define SONIFICATION_HEADSET_VOLUME_MIN  0.016
-
 class AudioPolicyManager: public AudioPolicyManagerBase
 {
 
 public:
                 AudioPolicyManager(AudioPolicyClientInterface *clientInterface)
-                : AudioPolicyManagerBase(clientInterface) {
-                    mLPADecodeOutput = -1;
-                    mLPAMuted = false;
-                    mLPAStreamType = AudioSystem::DEFAULT;
-                }
+                : AudioPolicyManagerBase(clientInterface) {}
 
         virtual ~AudioPolicyManager() {}
 
-        // AudioPolicyInterface
-        virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
-                                                          AudioSystem::device_connection_state state,
-                                                          const char *device_address);
-        virtual void setPhoneState(int state);
-
-        // return appropriate device for streams handled by the specified strategy according to current
-        // phone state, connected devices...
-        // if fromCache is true, the device is returned from mDeviceForStrategy[], otherwise it is determined
-        // by current state (device connected, phone state, force use, a2dp output...)
-        // This allows to:
-        //  1 speed up process when the state is stable (when starting or stopping an output)
-        //  2 access to either current device selection (fromCache == true) or
-        // "future" device selection (fromCache == false) when called from a context
-        //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
-        //  before updateDeviceForStrategy() is called.
-        virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
-                                                     bool fromCache);
-
-
-#ifdef TUNNEL_LPA_ENABLED
-        virtual audio_io_handle_t getSession(AudioSystem::stream_type stream,
-                                            uint32_t format,
-                                            AudioSystem::output_flags flags,
-                                            int32_t  sessionId,
-                                            uint32_t samplingRate, uint32_t channels);
-        virtual void pauseSession(audio_io_handle_t output, AudioSystem::stream_type stream);
-        virtual void resumeSession(audio_io_handle_t output, AudioSystem::stream_type stream);
-        virtual void releaseSession(audio_io_handle_t output);
-#endif
-
-        virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session = 0);
-        virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session = 0);
-        virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
-        status_t startInput(audio_io_handle_t input);
-
-protected:
-        // true is current platform implements a back microphone
-        virtual bool hasBackMicrophone() const { return false; }
-#ifdef WITH_A2DP
-        // true is current platform supports suplication of notifications and ringtones over A2DP output
-        virtual bool a2dpUsedForSonification() const { return true; }
-#endif
-        // change the route of the specified output
-        void setOutputDevice(audio_io_handle_t output, uint32_t device, bool force = false, int delayMs = 0);
-        // check that volume change is permitted, compute and send new volume to audio hardware
-        status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false);
-        // select input device corresponding to requested audio source
-        virtual audio_devices_t getDeviceForInputSource(int inputSource);
-        // Mute or unmute the stream on the specified output
-        void setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs = 0);
-        audio_io_handle_t mLPADecodeOutput;           // active output handler
-        audio_io_handle_t mLPAActiveOuput;           // LPA Output Handler during inactive state
-
-        bool    mLPAMuted;
-        AudioSystem::stream_type  mLPAStreamType;
-        AudioSystem::stream_type  mLPAActiveStreamType;
 };
 };
diff --git a/alsa_sound/AudioStreamInALSA.cpp b/alsa_sound/AudioStreamInALSA.cpp
index ba5d5e0..ed05e68 100644
--- a/alsa_sound/AudioStreamInALSA.cpp
+++ b/alsa_sound/AudioStreamInALSA.cpp
@@ -24,8 +24,9 @@
 #include <unistd.h>
 #include <dlfcn.h>
 
-#define LOG_TAG "audio.primary.msm8960"
+#define LOG_TAG "AudioStreamInALSA"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
 #include <utils/String8.h>
 
@@ -36,17 +37,17 @@
 #include "AudioHardwareALSA.h"
 
 extern "C" {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
 #include "csd_client.h"
 #endif
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
 #include "surround_filters_interface.h"
 #endif
 }
 
 namespace android_audio_legacy
 {
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
 #define SURROUND_FILE_1R "/system/etc/surround_sound/filter1r.pcm"
 #define SURROUND_FILE_2R "/system/etc/surround_sound/filter2r.pcm"
 #define SURROUND_FILE_3R "/system/etc/surround_sound/filter3r.pcm"
@@ -68,7 +69,7 @@
     mFramesLost(0),
     mParent(parent),
     mAcoustics(audio_acoustics)
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     , mFp_4ch(NULL),
     mFp_6ch(NULL),
     mRealCoeffs(NULL),
@@ -80,7 +81,7 @@
     mSurroundInputBufferIdx(0)
 #endif
 {
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     char c_multi_ch_dump[128] = {0};
     status_t err = NO_ERROR;
 
@@ -143,7 +144,7 @@
                 ALOGD("read:: mParent->mIncallMode=%d", mParent->mIncallMode);
                 if ((mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_UPLINK) &&
                     (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK)) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
                     if (mParent->mFusion3Platform) {
                         mParent->mALSADevice->setVocRecMode(INCALL_REC_STEREO);
                         strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE,
@@ -156,7 +157,7 @@
                                 sizeof(mHandle->useCase));
                     }
                 } else if (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
                     if (mParent->mFusion3Platform) {
                         mParent->mALSADevice->setVocRecMode(INCALL_REC_MONO);
                         strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE,
@@ -169,7 +170,7 @@
                                 sizeof(mHandle->useCase));
                     }
                 }
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
             } else if(mHandle->devices == AudioSystem::DEVICE_IN_FM_RX) {
                 strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_FM, sizeof(mHandle->useCase));
             } else if (mHandle->devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
@@ -186,7 +187,7 @@
                 ALOGD("read:: ---- mParent->mIncallMode=%d", mParent->mIncallMode);
                 if ((mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_UPLINK) &&
                     (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK)) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
                     if (mParent->mFusion3Platform) {
                         mParent->mALSADevice->setVocRecMode(INCALL_REC_STEREO);
                         strlcpy(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC,
@@ -199,7 +200,7 @@
                                 sizeof(mHandle->useCase));
                     }
                 } else if (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
                    if (mParent->mFusion3Platform) {
                        mParent->mALSADevice->setVocRecMode(INCALL_REC_MONO);
                        strlcpy(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC,
@@ -212,7 +213,7 @@
                                sizeof(mHandle->useCase));
                    }
                 }
-#if 0
+#ifdef QCOM_FM_ENABLED
             } else if(mHandle->devices == AudioSystem::DEVICE_IN_FM_RX) {
                 strlcpy(mHandle->useCase, SND_USE_CASE_VERB_FM_REC, sizeof(mHandle->useCase));
         } else if (mHandle->devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) {
@@ -227,7 +228,7 @@
         free(use_case);
         if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
             (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
             if((mDevices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) ||
                (mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)) {
                 mHandle->module->route(mHandle, (mDevices | AudioSystem::DEVICE_IN_PROXY) , AudioSystem::MODE_IN_COMMUNICATION);
@@ -237,8 +238,7 @@
                 mHandle->module->route(mHandle, mDevices , AudioSystem::MODE_IN_COMMUNICATION);
             }
         } else {
-#if 0
-
+#ifdef QCOM_USBAUDIO_ENABLED
             if((mHandle->devices == AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET)||
                (mHandle->devices == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)){
                 mHandle->module->route(mHandle, AudioSystem::DEVICE_IN_PROXY , mParent->mode());
@@ -272,7 +272,7 @@
 
             return 0;
         }
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
         if((mHandle->devices == AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET)||
            (mHandle->devices == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)){
             if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
@@ -286,7 +286,7 @@
 #endif
         mParent->mLock.unlock();
     }
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
     if(((mDevices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) ||
        (mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)) &&
        (!mParent->musbRecordingState)) {
@@ -307,7 +307,7 @@
     period_size = mHandle->periodSize;
     int read_pending = bytes;
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     if (mSurroundObj) {
         int processed = 0;
         int processed_pending;
@@ -396,7 +396,7 @@
         read = processed * sizeof(Word16);
         buffer = buffer_start;
     } else
-#endif 
+#endif
     {
 
         do {
@@ -461,6 +461,7 @@
     if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
         (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
         if((mParent->mVoipStreamCount)) {
+#ifdef QCOM_USBAUDIO_ENABLED
             ALOGD("musbRecordingState: %d, mVoipStreamCount:%d",mParent->musbRecordingState,
                   mParent->mVoipStreamCount );
             if(mParent->mVoipStreamCount == 1) {
@@ -471,15 +472,18 @@
                 mParent->closeUsbRecordingIfNothingActive();
                 mParent->closeUsbPlaybackIfNothingActive();
             }
+#endif
                return NO_ERROR;
         }
         mParent->mVoipStreamCount = 0;
         mParent->mVoipMicMute = 0;
+#ifdef QCOM_USBAUDIO_ENABLED
     } else {
         ALOGD("Deregistering REC bit, musbRecordingState:%d", mParent->musbRecordingState);
         mParent->musbRecordingState &= ~USBRECBIT_REC;
+#endif
      }
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
     if (mParent->mFusion3Platform) {
        if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC)) ||
            (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE))) {
@@ -488,11 +492,13 @@
     }
 #endif
     ALOGD("close");
+#ifdef QCOM_USBAUDIO_ENABLED
     mParent->closeUsbRecordingIfNothingActive();
+#endif
 
     ALSAStreamOps::close();
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     if (mSurroundObj) {
         surround_filters_release(mSurroundObj);
         if (mSurroundObj)
@@ -547,7 +553,7 @@
          return NO_ERROR;
     }
 
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
     ALOGD("standby");
     if (mParent->mFusion3Platform) {
        if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC)) ||
@@ -559,9 +565,11 @@
 #endif
     mHandle->module->standby(mHandle);
 
+#ifdef QCOM_USBAUDIO_ENABLED
     ALOGD("Checking for musbRecordingState %d", mParent->musbRecordingState);
     mParent->musbRecordingState &= ~USBRECBIT_REC;
     mParent->closeUsbRecordingIfNothingActive();
+#endif
 
     return NO_ERROR;
 }
@@ -587,7 +595,7 @@
     return (status_t)NO_ERROR;
 }
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
 status_t AudioStreamInALSA::initSurroundSoundLibrary(unsigned long buffersize)
 {
     int subwoofer = 0;  // subwoofer channel assignment: default as first microphone input channel
diff --git a/alsa_sound/AudioStreamOutALSA.cpp b/alsa_sound/AudioStreamOutALSA.cpp
index b370791..88ac299 100644
--- a/alsa_sound/AudioStreamOutALSA.cpp
+++ b/alsa_sound/AudioStreamOutALSA.cpp
@@ -25,8 +25,9 @@
 #include <dlfcn.h>
 #include <math.h>
 
-#define LOG_TAG "audio.primary.msm8960"
+#define LOG_TAG "AudioStreamOutALSA"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
 #include <utils/String8.h>
 
@@ -144,7 +145,7 @@
             free(use_case);
             if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
                (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
                 if((mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
                       (mDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)||
                       (mDevices & AudioSystem::DEVICE_OUT_PROXY)) {
@@ -155,7 +156,7 @@
                 {
                   mHandle->module->route(mHandle, mDevices , AudioSystem::MODE_IN_COMMUNICATION);
                 }
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
             } else if((mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)||
                       (mDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)||
                       (mDevices & AudioSystem::DEVICE_OUT_PROXY)) {
@@ -183,7 +184,7 @@
                 mParent->mLock.unlock();
                 return 0;
             }
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
             if((mHandle->devices == AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET)||
                    (mHandle->devices == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)){
                 if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
@@ -199,6 +200,7 @@
         mParent->mLock.unlock();
     }
 
+#ifdef QCOM_USBAUDIO_ENABLED
     if(((mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET) ||
         (mDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET)) &&
         (!mParent->musbPlaybackState)) {
@@ -215,6 +217,7 @@
         }
         mParent->mLock.unlock();
     }
+#endif
 
     period_size = mHandle->periodSize;
     do {
@@ -231,8 +234,8 @@
                       period_size);
         }
         if (n < 0) {
-	    mParent->mLock.lock();
-            ALOGE("pcm_write returned error %d, trying to recover\n", n);
+            mParent->mLock.lock();
+            ALOGE("pcm_write returned error %l, trying to recover\n", n);
             pcm_close(mHandle->handle);
             mHandle->handle = NULL;
             if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) ||
@@ -277,6 +280,7 @@
     if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) ||
         (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) {
          if((mParent->mVoipStreamCount)) {
+#ifdef QCOM_USBAUDIO_ENABLED
              if(mParent->mVoipStreamCount == 1) {
                  ALOGD("Deregistering VOIP Call bit, musbPlaybackState:%d, musbRecordingState: %d",
                        mParent->musbPlaybackState, mParent->musbRecordingState);
@@ -285,11 +289,14 @@
                  mParent->closeUsbPlaybackIfNothingActive();
                  mParent->closeUsbRecordingIfNothingActive();
              }
+#endif
                 return NO_ERROR;
          }
          mParent->mVoipStreamCount = 0;
          mParent->mVoipMicMute = 0;
-    } else if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_HIFI_LOW_POWER)) ||
+    }
+#ifdef QCOM_USBAUDIO_ENABLED
+      else if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_HIFI_LOW_POWER)) ||
               (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_LPA))) {
         mParent->musbPlaybackState &= ~USBPLAYBACKBIT_LPA;
     } else {
@@ -297,6 +304,7 @@
     }
 
     mParent->closeUsbPlaybackIfNothingActive();
+#endif
 
     ALSAStreamOps::close();
 
@@ -314,6 +322,7 @@
         return NO_ERROR;
     }
 
+#ifdef QCOM_USBAUDIO_ENABLED
     if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_HIFI_LOW_POWER)) ||
         (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_LPA))) {
         ALOGD("Deregistering LPA bit");
@@ -322,10 +331,13 @@
         ALOGD("Deregistering MUSIC bit, musbPlaybackState: %d", mParent->musbPlaybackState);
         mParent->musbPlaybackState &= ~USBPLAYBACKBIT_MUSIC;
     }
+#endif
 
     mHandle->module->standby(mHandle);
 
+#ifdef QCOM_USBAUDIO_ENABLED
     mParent->closeUsbPlaybackIfNothingActive();
+#endif
 
     mFrameCount = 0;
 
diff --git a/alsa_sound/AudioUsbALSA.cpp b/alsa_sound/AudioUsbALSA.cpp
index 2d62d34..3316e3e 100644
--- a/alsa_sound/AudioUsbALSA.cpp
+++ b/alsa_sound/AudioUsbALSA.cpp
@@ -26,10 +26,10 @@
 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.primary.msm8960"
-//#define LOG_NDEBUG 0
+#define LOG_TAG "AudioUsbALSA"
+#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
-
 #include <utils/String8.h>
 
 #include <cutils/properties.h>
@@ -52,6 +52,9 @@
 struct pollfd pfdProxyRecording[1];
 struct pollfd pfdUsbRecording[1];
 
+#define USB_PERIOD_SIZE 2048
+#define PROXY_PERIOD_SIZE 3072
+
 namespace android_audio_legacy
 {
 AudioUsbALSA::AudioUsbALSA()
@@ -84,20 +87,21 @@
     return size;
 }
 
-status_t AudioUsbALSA::getPlaybackCap()
+
+status_t AudioUsbALSA::getCap(char * type, int &channels, int &sampleRate)
 {
-    ALOGD("getPlaybackCap");
+    ALOGD("getCap for %s",type);
     long unsigned fileSize;
     FILE *fp;
     char *buffer;
     int err = 1;
     int size = 0;
     int fd, i, lchannelsPlayback;
-    char *read_buf, *playbackstr_start, *channel_start, *ratesStr, *ratesStrForVal,
+    char *read_buf, *str_start, *channel_start, *ratesStr, *ratesStrForVal,
     *ratesStrStart, *chString, *nextSRStr, *test, *nextSRString, *temp_ptr;
     struct stat st;
     memset(&st, 0x0, sizeof(struct stat));
-    msampleRatePlayback = 0;
+    sampleRate = 0;
     fd = open(PATH, O_RDONLY);
     if (fd <0) {
         ALOGE("ERROR: failed to open config file %s error: %d\n", PATH, errno);
@@ -116,15 +120,15 @@
     read_buf = (char *)malloc(BUFFSIZE);
     memset(read_buf, 0x0, BUFFSIZE);
     err = read(fd, read_buf, BUFFSIZE);
-    playbackstr_start = strstr(read_buf, "Playback:");
-    if (playbackstr_start == NULL) {
-        ALOGE("ERROR:Playback section not found in usb config file");
+    str_start = strstr(read_buf, type);
+    if (str_start == NULL) {
+        ALOGE("ERROR:%s section not found in usb config file", type);
         close(fd);
         free(read_buf);
         return UNKNOWN_ERROR;
     }
 
-    channel_start = strstr(playbackstr_start, "Channels:");
+    channel_start = strstr(str_start, "Channels:");
     if (channel_start == NULL) {
         ALOGE("ERROR: Could not find Channels information");
         close(fd);
@@ -141,12 +145,12 @@
 
     lchannelsPlayback = atoi(channel_start);
     if (lchannelsPlayback == 1) {
-        mchannelsPlayback = 1;
+        channels = 1;
     } else {
-        mchannelsPlayback = 2;
+        channels = 2;
     }
     ALOGD("channels supported by device: %d", lchannelsPlayback);
-    ratesStrStart = strstr(playbackstr_start, "Rates:");
+    ratesStrStart = strstr(str_start, "Rates:");
     if (ratesStrStart == NULL) {
         ALOGE("ERROR: Cant find rates information");
         close(fd);
@@ -209,11 +213,11 @@
 
     for (i = 0; i<=size; i++) {
         if (ratesSupported[i] <= 48000) {
-            msampleRatePlayback = ratesSupported[i];
+            sampleRate = ratesSupported[i];
             break;
         }
     }
-    ALOGD("msampleRatePlayback: %d", msampleRatePlayback);
+    ALOGD("sampleRate: %d", sampleRate);
 
     close(fd);
     free(ratesStrForVal);
@@ -225,147 +229,20 @@
     return NO_ERROR;
 }
 
-
-/******************************** Capture ******************************/
-status_t AudioUsbALSA::getCaptureCap(){
-    char *read_buf, *target, *capturestr_start, *channel_startCapture, *ratesStr, *ratesStrForVal,
-    *ratesStrStartCapture, *chString, *nextSRStr, *test, *nextSRString, *temp_ptr;
-
-    int fd, i, lchannelsCapture;
-
-    int err =1, size=0;
-    struct stat st;
-    memset(&st, 0x0, sizeof(struct stat));
-
-    msampleRateCapture = 0;
-    fd = open(PATH, O_RDONLY);
-    if (fd <0) {
-        ALOGE("ERROR: failed to open config file %s error: %d\n", PATH, errno);
-        return UNKNOWN_ERROR;
-    }
-
-    if (fstat(fd, &st) < 0) {
-        ALOGE("ERROR: failed to stat %s error %d\n", PATH, errno);
-        close(fd);
-        return UNKNOWN_ERROR;
-    }
-
-    read_buf = (char *)malloc(BUFFSIZE);
-    memset(read_buf, 0x0, BUFFSIZE);
-    err = read(fd, read_buf, BUFFSIZE);
-    capturestr_start = strstr(read_buf, "Capture:");
-    if (capturestr_start == NULL) {
-        ALOGE("ERROR: Could not find capture section for recording");
-        free(read_buf);
-        close(fd);
-        return NULL;
-    }
-
-    channel_startCapture = strstr(capturestr_start, "Channels: ");
-    if (channel_startCapture == NULL) {
-        ALOGE("ERROR: Could not find Channels info for recording");
-        close(fd);
-        free(read_buf);
-        return UNKNOWN_ERROR;
-    }
-    channel_startCapture = strstr(channel_startCapture, " ");
-    if (channel_startCapture == NULL) {
-        ALOGE("ERROR: Could not find channels information for recording");
-        close(fd);
-        free(read_buf);
-        return UNKNOWN_ERROR;
-    } else {
-        lchannelsCapture = atoi(channel_startCapture);
-        if (lchannelsCapture == 1) {
-            mchannelsCapture = 1;
-        } else {
-            ALOGD("lchannelsCapture: %d", lchannelsCapture);
-            mchannelsCapture = 2;
-        }
-    }
-
-    ratesStrStartCapture = strstr(capturestr_start, "Rates:");
-    if (ratesStrStartCapture == NULL) {
-        ALOGE("ERROR; Could not find rates section in config file for recording");
-        close(fd);
-        free(read_buf);
-        return UNKNOWN_ERROR;
-    }
-    ratesStrStartCapture = strstr(ratesStrStartCapture, " ");
-    if (ratesStrStartCapture == NULL) {
-        ALOGE("ERROR: Could not find rates section in config file for recording");
-        close(fd);
-        free(read_buf);
-        return UNKNOWN_ERROR;
-    }
-
-    //copy to ratesStr, current line.
-    target = strchr(ratesStrStartCapture, '\n');
-    if (target == NULL) {
-        ALOGE("ERROR: end of line not found for rates");
-        close(fd);
-        free(read_buf);
-        return UNKNOWN_ERROR;
-    }
-
-    size = target - ratesStrStartCapture;
-    ratesStr = (char *)malloc(size + 1) ;
-    ratesStrForVal = (char *)malloc(size + 1) ;
-    memcpy(ratesStr, ratesStrStartCapture, size);
-    memcpy(ratesStrForVal, ratesStrStartCapture, size);
-    ratesStr[size] = '\0';
-    ratesStrForVal[size] = '\0';
-
-    size = getnumOfRates(ratesStr);
-    if (!size) {
-        ALOGE("ERROR: Could not get rate size for capture, returning");
-        close(fd);
-        free(read_buf);
-        free(ratesStr);
-        free(ratesStrForVal);
-        return UNKNOWN_ERROR;
-    }
-
-    //populate playback rates array
-    int ratesSupportedCapture[size];
-    nextSRString = strtok_r(ratesStrForVal, " ,", &temp_ptr);
-    if (nextSRString == NULL) {
-        ALOGE("ERROR: Could not find ratesStr for recording");
-        close(fd);
-        free(read_buf);
-        free(ratesStr);
-        free(ratesStrForVal);
-        return UNKNOWN_ERROR;
-    }
-
-    ratesSupportedCapture[0] = atoi(nextSRString);
-    for (i = 1; i<size; i++) {
-        nextSRString = strtok_r(NULL, " ,.-", &temp_ptr);
-        ratesSupportedCapture[i] = atoi(nextSRString);
-    }
-    for (i = 0;i<=size; i++) {
-        if (ratesSupportedCapture[i] <= 48000) {
-            msampleRateCapture = ratesSupportedCapture[i];
-            break;
-        }
-    }
-    ALOGD("msampleRateCapture: %d", msampleRateCapture);
-
-    close(fd);
-    free(read_buf);
-    free(ratesStr);
-    free(ratesStrForVal);
-    read_buf = NULL;
-    ratesStr = NULL;
-    ratesStrForVal = NULL;
-    return NO_ERROR;
-}
-
 void AudioUsbALSA::exitPlaybackThread(uint64_t writeVal)
 {
     ALOGD("exitPlaybackThread, mproxypfdPlayback: %d", mproxypfdPlayback);
     if (writeVal == SIGNAL_EVENT_KILLTHREAD) {
-        closePlaybackDevices();
+        int err;
+
+        err = closeDevice(mproxyPlaybackHandle);
+        if (err) {
+            ALOGE("Info: Could not close proxy %p", mproxyPlaybackHandle);
+        }
+        err = closeDevice(musbPlaybackHandle);
+        if (err) {
+            ALOGE("Info: Could not close USB device %p", musbPlaybackHandle);
+        }
     }
     if ((mproxypfdPlayback != -1) && (musbpfdPlayback != -1)) {
         write(mproxypfdPlayback, &writeVal, sizeof(uint64_t));
@@ -379,43 +256,26 @@
 {
     ALOGD("exitRecordingThread");
     if (writeVal == SIGNAL_EVENT_KILLTHREAD) {
-        closeRecordingDevices();
+        int err;
+
+        err = closeDevice(mproxyRecordingHandle);
+        if (err) {
+            ALOGE("Info: Could not close proxy for recording %p", mproxyRecordingHandle);
+        }
+        err = closeDevice(musbRecordingHandle);
+        if (err) {
+            ALOGE("Info: Could not close USB recording device %p", musbRecordingHandle);
+        }
     }
     mkillRecordingThread = true;
 }
 
-void AudioUsbALSA::closeRecordingDevices(){
-    int err;
-
-    err = closeDevice(mproxyRecordingHandle);
-    if (err) {
-        ALOGE("Info: Could not close proxy for recording %p", mproxyRecordingHandle);
-    }
-    err = closeDevice(musbRecordingHandle);
-    if (err) {
-        ALOGE("Info: Could not close USB recording device %p", musbRecordingHandle);
-    }
-}
-
-void AudioUsbALSA::closePlaybackDevices(){
-    int err;
-
-    err = closeDevice(mproxyPlaybackHandle);
-    if (err) {
-        ALOGE("Info: Could not close proxy %p", mproxyPlaybackHandle);
-    }
-    err = closeDevice(musbPlaybackHandle);
-    if (err) {
-        ALOGE("Info: Could not close USB device %p", musbPlaybackHandle);
-    }
-}
-
 void AudioUsbALSA::setkillUsbRecordingThread(bool val){
     ALOGD("setkillUsbRecordingThread");
     mkillRecordingThread = val;
 }
 
-status_t AudioUsbALSA::setHardwareParams(pcm *txHandle, uint32_t sampleRate, uint32_t channels)
+status_t AudioUsbALSA::setHardwareParams(pcm *txHandle, uint32_t sampleRate, uint32_t channels, int periodBytes)
 {
     ALOGD("setHardwareParams");
     struct snd_pcm_hw_params *params;
@@ -436,8 +296,8 @@
                    SNDRV_PCM_FORMAT_S16_LE);
     param_set_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
                    SNDRV_PCM_SUBFORMAT_STD);
-    ALOGV("Setting period size: 768 samplerate:%d, channels: %d",sampleRate, channels);
-    param_set_min(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 768);
+    ALOGV("Setting period size:%d samplerate:%d, channels: %d",periodBytes,sampleRate, channels);
+    param_set_min(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, periodBytes);
     param_set_int(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 16);
     param_set_int(params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
                   channels - 1 ? 32 : 16);
@@ -464,13 +324,11 @@
     return NO_ERROR;
 }
 
-status_t AudioUsbALSA::setSoftwareParams(pcm *pcm)
+status_t AudioUsbALSA::setSoftwareParams(pcm *pcm, bool playback)
 {
     ALOGD("setSoftwareParams");
     struct snd_pcm_sw_params* params;
 
-    unsigned long periodSize = 1024;
-
     params = (snd_pcm_sw_params*) calloc(1, sizeof(struct snd_pcm_sw_params));
     if (!params) {
         LOG_ALWAYS_FATAL("Failed to allocate ALSA software parameters!");
@@ -482,8 +340,15 @@
 
     params->avail_min = (pcm->flags & PCM_MONO) ? pcm->period_size/2 : pcm->period_size/4;
 
-    params->start_threshold = (pcm->flags & PCM_MONO) ? pcm->period_size/2 : pcm->period_size/4;
+    if (playback) {
+        params->start_threshold = (pcm->flags & PCM_MONO) ? pcm->period_size*8 : pcm->period_size*4;
+        params->xfer_align = (pcm->flags & PCM_MONO) ? pcm->period_size*8 : pcm->period_size*4;
+    } else {
+        params->start_threshold = (pcm->flags & PCM_MONO) ? pcm->period_size/2 : pcm->period_size/4;
+        params->xfer_align = (pcm->flags & PCM_MONO) ? pcm->period_size/2 : pcm->period_size/4;
+    }
     params->stop_threshold = pcm->buffer_size;
+
     params->xfer_align = (pcm->flags & PCM_MONO) ? pcm->period_size/2 : pcm->period_size/4;
     params->silence_size = 0;
     params->silence_threshold = 0;
@@ -529,10 +394,20 @@
     const char *fn = "/data/RecordPcm.pcm";
     filed = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0664);
 
-    err = configureUsbDeviceForRecording();
+    err = getCap((char *)"Capture:", mchannelsCapture, msampleRateCapture);
     if (err) {
+        ALOGE("ERROR: Could not get capture capabilities from usb device");
+        return;
+    }
+    int channelFlag = PCM_MONO;
+    if (mchannelsCapture >= 2) {
+        channelFlag = PCM_STEREO;
+    }
+
+    musbRecordingHandle = configureDevice(PCM_IN|channelFlag|PCM_MMAP, (char *)"hw:1,0",
+                                         msampleRateCapture, mchannelsCapture,768,false);
+    if (!musbRecordingHandle) {
         ALOGE("ERROR: Could not configure USB device for recording");
-        closeDevice(musbRecordingHandle);
         return;
     } else {
         ALOGD("USB device Configured for recording");
@@ -541,10 +416,10 @@
     pfdUsbRecording[0].fd = musbRecordingHandle->fd;                           //DEBUG
     pfdUsbRecording[0].events = POLLIN;
 
-    err = configureProxyDeviceForRecording();
-    if (err) {
+    mproxyRecordingHandle = configureDevice(PCM_OUT|channelFlag|PCM_MMAP, (char *)"hw:0,7",
+                                            msampleRateCapture, mchannelsCapture,768,false);
+    if (!mproxyRecordingHandle) {
         ALOGE("ERROR: Could not configure Proxy for recording");
-        closeDevice(mproxyRecordingHandle);
         closeDevice(musbRecordingHandle);
         return;
     } else {
@@ -712,190 +587,51 @@
     return NULL;
 }
 
-status_t AudioUsbALSA::configureUsbDevice(){
-    unsigned flags = 0;
+struct pcm * AudioUsbALSA::configureDevice(unsigned flags, char* hw, int sampleRate, int channelCount, int periodSize, bool playback){
     int err = NO_ERROR;
-
-    flags = PCM_OUT|PCM_STEREO|PCM_MMAP;
-
-    musbPlaybackHandle = pcm_open(flags, (char *)"hw:1,0");
-    if (!musbPlaybackHandle) {
-        ALOGE("ERROR: pcm_open failed for usb playback case");
-        return UNKNOWN_ERROR;
+    struct pcm * handle = NULL;
+    handle = pcm_open(flags, hw);
+    if (!handle || handle->fd < 0) {
+        ALOGE("ERROR: pcm_open failed");
+        return NULL;
     }
 
-    if (!pcm_ready(musbPlaybackHandle)) {
-        ALOGE("ERROR: pcm_ready failed for usb playback case");
-        return err;
+    if (!pcm_ready(handle)) {
+        ALOGE("ERROR: pcm_ready failed");
+        closeDevice(handle);
+        return NULL;
     }
 
-    err = getPlaybackCap();
+    ALOGD("Setting hardware params: sampleRate:%d, channels: %d",sampleRate, channelCount);
+    err = setHardwareParams(handle, sampleRate, channelCount,periodSize);
+    if (err != NO_ERROR) {
+        ALOGE("ERROR: setHardwareParams failed");
+        closeDevice(handle);
+        return NULL;
+    }
+
+    err = setSoftwareParams(handle, playback);
+    if (err != NO_ERROR) {
+        ALOGE("ERROR: setSoftwareParams failed");
+        closeDevice(handle);
+        return NULL;
+    }
+
+    err = mmap_buffer(handle);
     if (err) {
-        ALOGE("ERROR: Could not get playback capabilities from usb device");
-        return UNKNOWN_ERROR;
+        ALOGE("ERROR: mmap_buffer failed");
+        closeDevice(handle);
+        return NULL;
     }
 
-    ALOGD("Setting hardware params: sampleRate:%d, channels: %d",msampleRatePlayback, mchannelsPlayback);
-    err = setHardwareParams(musbPlaybackHandle, msampleRatePlayback, mchannelsPlayback);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setHardwareParams failed for usb playback case");
-        return err;
-    }
-
-    err = setSoftwareParams(musbPlaybackHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setSoftwareParams failed for usb playback case");
-        return err;
-    }
-
-    err = mmap_buffer(musbPlaybackHandle);
+    err = pcm_prepare(handle);
     if (err) {
-        ALOGE("ERROR: mmap_buffer failed for usb playback case");
-        return err;
+        ALOGE("ERROR: pcm_prepare failed");
+        closeDevice(handle);
+        return NULL;
     }
 
-    err = pcm_prepare(musbPlaybackHandle);
-    if (err) {
-        ALOGE("ERROR: pcm_prepare failed for usb playback case");
-        return err;
-    }
-
-    return err;
-}
-
-status_t AudioUsbALSA::configureUsbDeviceForRecording(){
-    unsigned flags = 0;
-    int err = NO_ERROR;
-
-    flags = PCM_IN|PCM_MONO|PCM_MMAP;
-
-    musbRecordingHandle = pcm_open(flags, (char *)"hw:1,0");
-    if (!musbRecordingHandle) {
-        ALOGE("ERROR: pcm_open failed for usb recording case");
-        return UNKNOWN_ERROR;
-    }
-
-    if (!pcm_ready(musbRecordingHandle)) {
-        ALOGE("ERROR: pcm_ready failed for usb recording case");
-        return err;
-    }
-
-    err = getCaptureCap();
-    if (err) {
-        ALOGE("ERROR: Could not get capture capabilities from usb device");
-        return UNKNOWN_ERROR;
-    }
-
-    ALOGD("Setting hardwareParams for Usb recording msampleRateCapture %d, mchannelsCapture %d", msampleRateCapture, mchannelsCapture);
-    err = setHardwareParams(musbRecordingHandle, msampleRateCapture, mchannelsCapture);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setHardwareParams failed for usb recording case");
-        return err;
-    }
-
-    err = setSoftwareParams(musbRecordingHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setSoftwareParams failed for usb recording case");
-        return err;
-    }
-
-    err = mmap_buffer(musbRecordingHandle);
-    if (err) {
-        ALOGE("ERROR: mmap_buffer failed for usb recording case");
-        return err;
-    }
-
-    err = pcm_prepare(musbRecordingHandle);
-    if (err) {
-        ALOGE("ERROR: pcm_prepare failed for usb recording case");
-        return err;
-    }
-
-    return err;
-}
-
-status_t AudioUsbALSA::configureProxyDeviceForRecording(){
-    unsigned flags = 0;
-    int err = 0;
-    flags = PCM_OUT|PCM_MONO|PCM_MMAP;
-    mproxyRecordingHandle = pcm_open(flags, (char *)"hw:0,7");
-    if (!mproxyRecordingHandle) {
-        ALOGE("ERROR: pcm_open failed for proxy recording case");
-        return UNKNOWN_ERROR;
-    }
-
-    if (!pcm_ready(mproxyRecordingHandle)) {
-        ALOGE("ERROR: pcm_ready failed for proxy recording case");
-        return UNKNOWN_ERROR;
-    }
-
-    err = setHardwareParams(mproxyRecordingHandle, msampleRateCapture, mchannelsCapture);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setHardwareParams failed for proxy recording case");
-        return err;
-    }
-
-    err = setSoftwareParams(mproxyRecordingHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setSoftwareParams failed for proxy recording case");
-        return err;
-    }
-
-    err = mmap_buffer(mproxyRecordingHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: mmap_buffer failed for proxy recording case");
-        return err;
-    }
-
-    err = pcm_prepare(mproxyRecordingHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: pcm_prepare failed for proxy recording case");
-        return err;
-    }
-
-    return err;
-}
-
-status_t AudioUsbALSA::configureProxyDevice(){
-    unsigned flags = 0;
-    int err = 0;
-    flags = PCM_IN|PCM_STEREO|PCM_MMAP;
-    mproxyPlaybackHandle = pcm_open(flags, (char *)"hw:0,8");
-    if (!mproxyPlaybackHandle) {
-        ALOGE("ERROR: pcm_open failed for proxy playback case");
-        return UNKNOWN_ERROR;
-    }
-
-    if (!pcm_ready(mproxyPlaybackHandle)) {
-        ALOGE("ERROR: pcm_ready failed for proxy playback case");
-        return err;
-    }
-
-    err = setHardwareParams(mproxyPlaybackHandle, msampleRatePlayback, mchannelsPlayback);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setHardwareParams failed for proxy playback case");
-        return err;;
-    }
-
-    err = setSoftwareParams(mproxyPlaybackHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: setSoftwareParams failed for proxy playback case");
-        return err;
-    }
-
-    err = mmap_buffer(mproxyPlaybackHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: mmap_buffer failed for proxy playback case");
-        return err;
-    }
-
-    err = pcm_prepare(mproxyPlaybackHandle);
-    if (err != NO_ERROR) {
-        ALOGE("ERROR: pcm_prepare failed for proxy playback case");
-        return err;
-    }
-
-    return err;
+    return handle;
 }
 
 status_t AudioUsbALSA::startDevice(pcm *handle, bool *killThread) {
@@ -1038,6 +774,7 @@
     struct snd_xferi x;
     int bytes_written;
     unsigned avail, xfer, bufsize;
+    unsigned proxyPeriod, usbPeriod;
     uint32_t sampleRate;
     uint32_t channels;
     unsigned int tmp;
@@ -1048,9 +785,21 @@
     mdstUsb_addr = NULL;
     msrcProxy_addr = NULL;
 
-    filed = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0664);
-    err = configureUsbDevice();
+    int proxySizeRemaining = 0;
+    int usbSizeFilled = 0;
+
+    pid_t tid  = gettid();
+    androidSetThreadPriority(tid, ANDROID_PRIORITY_URGENT_AUDIO);
+
+    err = getCap((char *)"Playback:", mchannelsPlayback, msampleRatePlayback);
     if (err) {
+        ALOGE("ERROR: Could not get playback capabilities from usb device");
+        return;
+    }
+
+    musbPlaybackHandle = configureDevice(PCM_OUT|PCM_STEREO|PCM_MMAP, (char *)"hw:1,0",
+                                         msampleRatePlayback, mchannelsPlayback, USB_PERIOD_SIZE, true);
+    if (!musbPlaybackHandle) {
         ALOGE("ERROR: configureUsbDevice failed, returning");
         closeDevice(musbPlaybackHandle);
         return;
@@ -1059,24 +808,25 @@
     }
 
     if (!mkillPlayBackThread) {
-        pfdUsbPlayback[0].fd = musbPlaybackHandle->fd;
-        pfdUsbPlayback[0].events = POLLOUT;
+        pfdUsbPlayback[0].fd = musbPlaybackHandle->timer_fd;
+        pfdUsbPlayback[0].events = POLLIN;
         musbpfdPlayback = eventfd(0,0);
         pfdUsbPlayback[1].fd = musbpfdPlayback;
         pfdUsbPlayback[1].events = (POLLIN | POLLOUT | POLLERR | POLLNVAL | POLLHUP);
     }
 
-    err = configureProxyDevice();
-    if (err) {
+    mproxyPlaybackHandle = configureDevice(PCM_IN|PCM_STEREO|PCM_MMAP, (char *)"hw:0,8",
+                               msampleRatePlayback, mchannelsPlayback, PROXY_PERIOD_SIZE, false);
+    if (!mproxyPlaybackHandle) {
         ALOGE("ERROR: Could not configure Proxy, returning");
         closeDevice(musbPlaybackHandle);
-        closeDevice(mproxyPlaybackHandle);
         return;
     } else {
         ALOGD("Proxy Configured for playback");
     }
 
-    bufsize = mproxyPlaybackHandle->period_size;
+    proxyPeriod = mproxyPlaybackHandle->period_size;
+    usbPeriod = musbPlaybackHandle->period_size;
 
     if (!mkillPlayBackThread) {
         pfdProxyPlayback[0].fd = mproxyPlaybackHandle->fd;
@@ -1086,8 +836,15 @@
         pfdProxyPlayback[1].events = (POLLIN | POLLOUT| POLLERR | POLLNVAL);
     }
 
-    frames = (mproxyPlaybackHandle->flags & PCM_MONO) ? (bufsize / 2) : (bufsize / 4);
-    x.frames = (mproxyPlaybackHandle->flags & PCM_MONO) ? (bufsize / 2) : (bufsize / 4);
+    frames = (mproxyPlaybackHandle->flags & PCM_MONO) ? (proxyPeriod / 2) : (proxyPeriod / 4);
+    x.frames = (mproxyPlaybackHandle->flags & PCM_MONO) ? (proxyPeriod / 2) : (proxyPeriod / 4);
+    int usbframes = (musbPlaybackHandle->flags & PCM_MONO) ? (usbPeriod / 2) : (usbPeriod / 4);
+
+    u_int8_t *proxybuf = ( u_int8_t *) malloc(PROXY_PERIOD_SIZE);
+    u_int8_t *usbbuf = ( u_int8_t *) malloc(USB_PERIOD_SIZE);
+    memset(proxybuf, 0x0, PROXY_PERIOD_SIZE);
+    memset(usbbuf, 0x0, USB_PERIOD_SIZE);
+
 
     /***********************keep reading from proxy and writing to USB******************************************/
     while (mkillPlayBackThread != true) {
@@ -1116,141 +873,171 @@
                 break;
             }
         }
+        if (proxySizeRemaining == 0) {
+            for (;;) {
+                if (!mproxyPlaybackHandle->running) {
+                    if (pcm_prepare(mproxyPlaybackHandle)) {
+                        ALOGE("ERROR: pcm_prepare failed for proxy");
+                        mkillPlayBackThread = true;
+                        break;
+                    }
+                }
+                /* Sync the current Application pointer from the kernel */
+                mproxyPlaybackHandle->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL |
+                                                        SNDRV_PCM_SYNC_PTR_AVAIL_MIN;
 
-        for (;;) {
-            if (!mproxyPlaybackHandle->running) {
-                if (pcm_prepare(mproxyPlaybackHandle)) {
-                    ALOGE("ERROR: pcm_prepare failed for proxy");
-                    mkillPlayBackThread = true;
+                if (mtimeOut == TIMEOUT_INFINITE && !mkillPlayBackThread) {
+                    err = syncPtr(mproxyPlaybackHandle, &mkillPlayBackThread);
+                    if (err == EPIPE) {
+                        continue;
+                    } else if (err != NO_ERROR) {
+                        break;
+                    }
+                    avail = pcm_avail(mproxyPlaybackHandle);
+                }
+                if (avail < mproxyPlaybackHandle->sw_p->avail_min && !mkillPlayBackThread) {
+                    pollForProxyData();
+                    //if polling returned some error
+                    if (!mkillPlayBackThread) {
+                        continue;
+                    } else {
+                        break;
+                    }
+                } else {                                                           //Got some data or mkillPlayBackThread is true
                     break;
                 }
             }
-            /* Sync the current Application pointer from the kernel */
-            mproxyPlaybackHandle->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL |
-                                                    SNDRV_PCM_SYNC_PTR_AVAIL_MIN;
+            if (mkillPlayBackThread) {
+                break;
+            }
 
-            if (mtimeOut == TIMEOUT_INFINITE && !mkillPlayBackThread) {
+            if (x.frames > avail)
+                frames = avail;
+
+            if (!mkillPlayBackThread) {
+                msrcProxy_addr = dst_address(mproxyPlaybackHandle);
+                memcpy(proxybuf, msrcProxy_addr, proxyPeriod );
+
+                x.frames -= frames;
+                mproxyPlaybackHandle->sync_ptr->c.control.appl_ptr += frames;
+                mproxyPlaybackHandle->sync_ptr->flags = 0;
+                proxySizeRemaining = proxyPeriod;
+            }
+
+            if (!mkillPlayBackThread) {
                 err = syncPtr(mproxyPlaybackHandle, &mkillPlayBackThread);
                 if (err == EPIPE) {
                     continue;
                 } else if (err != NO_ERROR) {
                     break;
                 }
-                avail = pcm_avail(mproxyPlaybackHandle);
             }
-            if (avail < mproxyPlaybackHandle->sw_p->avail_min && !mkillPlayBackThread) {
-                pollForProxyData();
-                //if polling returned some error
-                if (!mkillPlayBackThread) {
-                    continue;
+        }
+        //ALOGE("usbSizeFilled %d, proxySizeRemaining %d ",usbSizeFilled,proxySizeRemaining);
+        if (usbPeriod - usbSizeFilled <= proxySizeRemaining) {
+            memcpy(usbbuf + usbSizeFilled, proxybuf + proxyPeriod - proxySizeRemaining, usbPeriod - usbSizeFilled);
+            proxySizeRemaining -= (usbPeriod - usbSizeFilled);
+            usbSizeFilled = usbPeriod;
+        }
+        else {
+            memcpy(usbbuf + usbSizeFilled, proxybuf + proxyPeriod - proxySizeRemaining,proxySizeRemaining);
+            usbSizeFilled += proxySizeRemaining;
+            proxySizeRemaining = 0;
+        }
+
+        if (usbSizeFilled == usbPeriod) {
+            for (;;) {
+                if (!musbPlaybackHandle->running) {
+                    if (pcm_prepare(musbPlaybackHandle)) {
+                        ALOGE("ERROR: pcm_prepare failed for usb");
+                        mkillPlayBackThread = true;
+                        break;
+                    }
+                }
+                /*************USB syncing before write ******************/
+                musbPlaybackHandle->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL |
+                                                      SNDRV_PCM_SYNC_PTR_AVAIL_MIN;
+                if (mtimeOut == TIMEOUT_INFINITE && !mkillPlayBackThread) {
+                    err = syncPtr(musbPlaybackHandle, &mkillPlayBackThread);
+                    if (err == EPIPE) {
+                        continue;
+                    } else if (err != NO_ERROR) {
+                        break;
+                    }
+                    avail = pcm_avail(musbPlaybackHandle);
+                    //ALOGV("Avail USB is: %d", avail);
+                }
+
+                if (avail < musbPlaybackHandle->sw_p->avail_min && !mkillPlayBackThread) {
+                    pollForUsbData();
+                    if (!mkillPlayBackThread) {
+                        continue;
+                    } else {
+                        break;
+                    }
                 } else {
                     break;
                 }
-            } else {                                                           //Got some data or mkillPlayBackThread is true
+            }
+            if (mkillPlayBackThread) {
                 break;
             }
-        }
-        if (mkillPlayBackThread) {
-            break;
-        }
 
-        if (x.frames > avail)
-            frames = avail;
+            if (!mkillPlayBackThread) {
+                mdstUsb_addr = dst_address(musbPlaybackHandle);
 
-        if (!mkillPlayBackThread) {
-            msrcProxy_addr = dst_address(mproxyPlaybackHandle);
-            /**********End Proxy syncing before write**************/
-        }
+                /**************End USB syncing before write *************/
 
-        for (;;) {
-            if (!musbPlaybackHandle->running) {
-                if (pcm_prepare(musbPlaybackHandle)) {
-                    ALOGE("ERROR: pcm_prepare failed for usb");
-                    mkillPlayBackThread = true;
-                    break;
-                }
+                memcpy(mdstUsb_addr, usbbuf, usbPeriod );
+                usbSizeFilled = 0;
+                memset(usbbuf, 0x0, usbPeriod);
             }
-            /*************USB syncing before write ******************/
-            musbPlaybackHandle->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL |
-                                                  SNDRV_PCM_SYNC_PTR_AVAIL_MIN;
-            if (mtimeOut == TIMEOUT_INFINITE && !mkillPlayBackThread) {
+
+            /**************** sync up after write -- USB  ************************/
+            musbPlaybackHandle->sync_ptr->c.control.appl_ptr += usbframes;
+            musbPlaybackHandle->sync_ptr->flags = 0;
+            if (!mkillPlayBackThread) {
                 err = syncPtr(musbPlaybackHandle, &mkillPlayBackThread);
                 if (err == EPIPE) {
                     continue;
                 } else if (err != NO_ERROR) {
                     break;
                 }
-                avail = pcm_avail(musbPlaybackHandle);
-                //ALOGV("Avail USB is: %d", avail);
             }
 
-            if (avail < musbPlaybackHandle->sw_p->avail_min && !mkillPlayBackThread) {
-                pollForUsbData();
+            bytes_written = musbPlaybackHandle->sync_ptr->c.control.appl_ptr - musbPlaybackHandle->sync_ptr->s.status.hw_ptr;
+            ALOGE("Appl ptr %d , hw_ptr %d, difference %d",musbPlaybackHandle->sync_ptr->c.control.appl_ptr, musbPlaybackHandle->sync_ptr->s.status.hw_ptr, bytes_written);
+
+            /*
+                Following is the check to prevent USB from going to bad state.
+                This happens in case of an underrun where there is not enough
+                data from the proxy
+            */
+	    if (bytes_written <= usbPeriod && musbPlaybackHandle->start) {
+                ioctl(musbPlaybackHandle->fd, SNDRV_PCM_IOCTL_PAUSE,1);
+                pcm_prepare(musbPlaybackHandle);
+                musbPlaybackHandle->start = false;
+                continue;
+            }
+            if ((bytes_written >= musbPlaybackHandle->sw_p->start_threshold) && (!musbPlaybackHandle->start)) {
                 if (!mkillPlayBackThread) {
-                    continue;
-                } else {
-                    break;
-                }
-            } else {
-                break;
-            }
-        }
-        if (mkillPlayBackThread) {
-            break;
-        }
-
-        if (!mkillPlayBackThread) {
-            mdstUsb_addr = dst_address(musbPlaybackHandle);
-            memset(mdstUsb_addr, 0x0, bufsize);
-
-            /**************End USB syncing before write *************/
-
-            memcpy(mdstUsb_addr, msrcProxy_addr, bufsize );
-
-            /************* sync up after write -- Proxy  *********************/
-            x.frames -= frames;
-            mproxyPlaybackHandle->sync_ptr->c.control.appl_ptr += frames;
-            mproxyPlaybackHandle->sync_ptr->flags = 0;
-        }
-
-        if (!mkillPlayBackThread) {
-            err = syncPtr(mproxyPlaybackHandle, &mkillPlayBackThread);
-            if (err == EPIPE) {
-                continue;
-            } else if (err != NO_ERROR) {
-                break;
-            }
-        }
-        /************* end sync up after write -- Proxy *********************/
-
-
-        /**************** sync up after write -- USB  ************************/
-        musbPlaybackHandle->sync_ptr->c.control.appl_ptr += frames;
-        musbPlaybackHandle->sync_ptr->flags = 0;
-        if (!mkillPlayBackThread) {
-            err = syncPtr(musbPlaybackHandle, &mkillPlayBackThread);
-            if (err == EPIPE) {
-                continue;
-            } else if (err != NO_ERROR) {
-                break;
-            }
-        }
-
-        bytes_written = musbPlaybackHandle->sync_ptr->c.control.appl_ptr - musbPlaybackHandle->sync_ptr->s.status.hw_ptr;
-        if ((bytes_written >= musbPlaybackHandle->sw_p->start_threshold) && (!musbPlaybackHandle->start)) {
-            if (!mkillPlayBackThread) {
-                err = startDevice(musbPlaybackHandle, &mkillPlayBackThread);
-                if (err == EPIPE) {
-                    continue;
-                } else if (err != NO_ERROR) {
-                    mkillPlayBackThread = true;
-                    break;
+                    err = startDevice(musbPlaybackHandle, &mkillPlayBackThread);
+                    if (err == EPIPE) {
+                        continue;
+                    } else if (err != NO_ERROR) {
+                        mkillPlayBackThread = true;
+                        break;
+                    }
                 }
             }
+            /***************  End sync up after write -- USB *********************/
         }
-        /***************  End sync up after write -- USB *********************/
     }
     if (mkillPlayBackThread) {
+        if (proxybuf)
+            free(proxybuf);
+        if (usbbuf)
+            free(usbbuf);
         mproxypfdPlayback = -1;
         musbpfdPlayback = -1;
         closeDevice(mproxyPlaybackHandle);
diff --git a/alsa_sound/AudioUsbALSA.h b/alsa_sound/AudioUsbALSA.h
index 1b6104d..4204eb0 100644
--- a/alsa_sound/AudioUsbALSA.h
+++ b/alsa_sound/AudioUsbALSA.h
@@ -86,15 +86,8 @@
     pthread_t mRecordingUsb;
     snd_use_case_mgr_t *mUcMgr;
 
-public:
-    AudioUsbALSA();
-    virtual            ~AudioUsbALSA();
-
     //Helper functions
-    status_t configureUsbDevice();
-    status_t configureProxyDevice();
-    status_t configureUsbDeviceForRecording();
-    status_t configureProxyDeviceForRecording();
+    struct pcm * configureDevice(unsigned flags, char* hw, int sampleRate, int channelCount, int periodSize, bool playback);
     status_t syncPtr(struct pcm *handle, bool *killThread);
 
     //playback
@@ -107,6 +100,29 @@
 
     status_t startDevice(pcm *handle, bool *killThread);
 
+    void PlaybackThreadEntry();
+    static void *PlaybackThreadWrapper(void *me);
+
+    void RecordingThreadEntry();
+    static void *RecordingThreadWrapper(void *me);
+
+    status_t setHardwareParams(pcm *local_handle, uint32_t sampleRate, uint32_t channels, int periodSize);
+
+    status_t setSoftwareParams(pcm *pcm, bool playback);
+
+    status_t closeDevice(pcm *handle);
+
+    status_t getCap(char * type, int &channels, int &sampleRate);
+    int         getnumOfRates(char *rateStr);
+    int         mchannelsPlayback;
+    int         msampleRatePlayback;
+    int         mchannelsCapture;
+    int         msampleRateCapture;
+
+public:
+    AudioUsbALSA();
+    virtual            ~AudioUsbALSA();
+
     void exitPlaybackThread(uint64_t writeVal);
     void exitRecordingThread(uint64_t writeVal);
     void setkillUsbRecordingThread(bool val);
@@ -116,32 +132,11 @@
     bool getkillUsbRecordingThread() {
         return mkillRecordingThread;
     }
-    void closePlaybackDevices();
-    void closeRecordingDevices();
     //Playback
     void startPlayback();
-    void PlaybackThreadEntry();
-    static void *PlaybackThreadWrapper(void *me);
 
     //Capture
     void startRecording();
-    void RecordingThreadEntry();
-
-    static void *RecordingThreadWrapper(void *me);
-
-    status_t setHardwareParams(pcm *local_handle, uint32_t sampleRate, uint32_t channels);
-
-    status_t setSoftwareParams(pcm *pcm);
-
-    status_t closeDevice(pcm *handle);
-
-    status_t    getPlaybackCap();
-    status_t    getCaptureCap();
-    int         getnumOfRates(char *rateStr);
-    int         mchannelsPlayback;
-    int         msampleRatePlayback;
-    int         mchannelsCapture;
-    int         msampleRateCapture;
 };
 
 };        // namespace android_audio_legacy
diff --git a/alsa_sound/acoustics_default.cpp b/alsa_sound/acoustics_default.cpp
index c5aecac..aa21190 100644
--- a/alsa_sound/acoustics_default.cpp
+++ b/alsa_sound/acoustics_default.cpp
@@ -86,14 +86,14 @@
 
 static status_t s_cleanup(acoustic_device_t *dev)
 {
-    LOGD("Acoustics close stub called.");
+    ALOGD("Acoustics close stub called.");
     return NO_ERROR;
 }
 
 static status_t s_set_params(acoustic_device_t *dev,
         AudioSystem::audio_in_acoustics acoustics, void *params)
 {
-    LOGD("Acoustics set_params stub called with %d.", (int)acoustics);
+    ALOGD("Acoustics set_params stub called with %d.", (int)acoustics);
     return NO_ERROR;
 }
 }
diff --git a/alsa_sound/alsa_default.cpp b/alsa_sound/alsa_default.cpp
index b876734..9a53672 100644
--- a/alsa_sound/alsa_default.cpp
+++ b/alsa_sound/alsa_default.cpp
@@ -16,15 +16,15 @@
  ** limitations under the License.
  */
 
-#define LOG_TAG "alsa.msm8960"
+#define LOG_TAG "ALSAModule"
 //#define LOG_NDEBUG 0
+#define LOG_NDDEBUG 0
 #include <utils/Log.h>
-
 #include <cutils/properties.h>
 #include <linux/ioctl.h>
 #include "AudioHardwareALSA.h"
 #include <media/AudioRecord.h>
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
 extern "C" {
 #include "csd_client.h"
 }
@@ -217,7 +217,7 @@
     ALOGD("setHardwareParams: reqBuffSize %d channels %d sampleRate %d",
          (int) reqBuffSize, handle->channels, handle->sampleRate);
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     if (channels == 6) {
         if (!strncmp(handle->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
             || !strncmp(handle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
@@ -232,11 +232,14 @@
     param_set_mask(params, SNDRV_PCM_HW_PARAM_ACCESS,
                    SNDRV_PCM_ACCESS_RW_INTERLEAVED);
     if (handle->format != SNDRV_PCM_FORMAT_S16_LE) {
-        if (handle->format == AudioSystem::AMR_NB ||
-            handle->format == AudioSystem::AMR_WB ||
-            handle->format == AudioSystem::EVRC ||
-            handle->format == AudioSystem::EVRCB ||
-            handle->format == AudioSystem::EVRCWB)
+        if (handle->format == AudioSystem::AMR_NB
+            || handle->format == AudioSystem::AMR_WB
+#ifdef QCOM_QCHAT_ENABLED
+            || handle->format == AudioSystem::EVRC
+            || handle->format == AudioSystem::EVRCB
+            || handle->format == AudioSystem::EVRCWB
+#endif
+            )
               format = SNDRV_PCM_FORMAT_SPECIAL;
     }
     param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
@@ -291,7 +294,7 @@
         return NO_INIT;
     }
 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     if (channels == 6) {
         if (!strncmp(handle->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
             || !strncmp(handle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
@@ -356,7 +359,7 @@
                    (devices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
             devices = devices | (AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET |
                       AudioSystem::DEVICE_OUT_BLUETOOTH_SCO);
-#if 0
+#ifdef QCOM_ANC_HEADSET_ENABLED
         } else if ((devices & AudioSystem::DEVICE_OUT_ANC_HEADSET) ||
                    (devices & AudioSystem::DEVICE_IN_ANC_HEADSET)) {
             devices = devices | (AudioSystem::DEVICE_OUT_ANC_HEADSET |
@@ -368,7 +371,7 @@
         } else if (devices & AudioSystem::DEVICE_OUT_AUX_DIGITAL) {
             devices = devices | (AudioSystem::DEVICE_OUT_AUX_DIGITAL |
                       AudioSystem::DEVICE_IN_AUX_DIGITAL);
-#if 0
+#ifdef QCOM_PROXY_DEVICE_ENABLED
         } else if ((devices & AudioSystem::DEVICE_OUT_PROXY) ||
                   (devices & AudioSystem::DEVICE_IN_PROXY)) {
             devices = devices | (AudioSystem::DEVICE_OUT_PROXY |
@@ -376,7 +379,7 @@
 #endif
         }
     }
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     if ((devices & AudioSystem::DEVICE_IN_BUILTIN_MIC) && ( 6 == handle->channels)) {
         if (!strncmp(handle->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC))
             || !strncmp(handle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) {
@@ -467,7 +470,7 @@
        }
     }
     ALOGV("%s,rxDev:%s, txDev:%s, curRxDev:%s, curTxDev:%s\n", __FUNCTION__, rxDevice, txDevice, curRxUCMDevice, curTxUCMDevice);
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
     if (mode == AudioSystem::MODE_IN_CALL && platform_is_Fusion3() && (inCallDevSwitch == true)) {
         err = csd_client_disable_device();
         if (err < 0)
@@ -480,7 +483,7 @@
     if (rxDevice != NULL) {
         snd_use_case_set(handle->ucMgr, "_enadev", rxDevice);
         strlcpy(curRxUCMDevice, rxDevice, sizeof(curRxUCMDevice));
-#if 0
+#ifdef QCOM_FM_ENABLED
         if (devices & AudioSystem::DEVICE_OUT_FM)
             s_set_fm_vol(fmVolume);
 #endif
@@ -523,7 +526,7 @@
             tx_dev_id = DEVICE_SPEAKER_TX_ACDB_ID;
         }
 
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         ALOGV("rx_dev_id=%d, tx_dev_id=%d\n", rx_dev_id, tx_dev_id);
         err = csd_client_enable_device(rx_dev_id, tx_dev_id, mDevSettingsFlag);
         if (err < 0)
@@ -600,7 +603,7 @@
     if (handle->channels == 1) {
         flags |= PCM_MONO;
     } 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
     else if (handle->channels == 4 ) {
         flags |= PCM_QUAD;
     } else if (handle->channels == 6 ) {
@@ -843,7 +846,7 @@
     }
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err = csd_client_start_voice();
         if (err < 0) {
             ALOGE("s_start_voice_call: csd_client error %d\n", err);
@@ -1031,7 +1034,7 @@
         if ((!strcmp(handle->useCase, SND_USE_CASE_VERB_VOICECALL) ||
              !strcmp(handle->useCase, SND_USE_CASE_MOD_PLAY_VOICE)) &&
             platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
             err = csd_client_stop_voice();
             if (err < 0) {
                 ALOGE("s_close: csd_client error %d\n", err);
@@ -1208,7 +1211,7 @@
             (callMode == AudioSystem::MODE_IN_CALL) &&
             ((devices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) ||
              (devices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE))) { 
-#if 0 
+#ifdef QCOM_ANC_HEADSET_ENABLED
              ||
              (devices & AudioSystem::DEVICE_OUT_ANC_HEADSET) ||
              (devices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE))) {
@@ -1231,7 +1234,7 @@
             } else {
                 return strdup(SND_USE_CASE_DEV_SPEAKER_HEADSET); /* COMBO SPEAKER+HEADSET RX */
             }
-#if 0
+#ifdef QCOM_ANC_HEADSET_ENABLED
         } else if ((devices & AudioSystem::DEVICE_OUT_SPEAKER) &&
             ((devices & AudioSystem::DEVICE_OUT_ANC_HEADSET) ||
             (devices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE))) {
@@ -1251,7 +1254,7 @@
             } else {
                 return strdup(SND_USE_CASE_DEV_HEADPHONES); /* HEADSET RX */
             }
-#if 0
+#ifdef QCOM_ANC_HEADSET_ENABLED
         } else if ((devices & AudioSystem::DEVICE_OUT_ANC_HEADSET) ||
                    (devices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE)) {
             return strdup(SND_USE_CASE_DEV_ANC_HEADSET); /* ANC HEADSET RX */
@@ -1265,8 +1268,9 @@
                 return strdup(SND_USE_CASE_DEV_BTSCO_NB_RX); /* BTSCO RX*/
         } else if ((devices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP) ||
                    (devices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES) ||
-//commented JB
-/*                   (devices & AudioSystem::DEVICE_OUT_DIRECTOUTPUT) ||*/
+#ifdef QCOM_VOIP_ENABLED
+                   (devices & AudioSystem::DEVICE_OUT_DIRECTOUTPUT) ||
+#endif
                    (devices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) {
             /* Nothing to be done, use current active device */
             if (strncmp(curRxUCMDevice, "None", 4)) {
@@ -1274,9 +1278,11 @@
             }
         } else if (devices & AudioSystem::DEVICE_OUT_AUX_DIGITAL) {
             return strdup(SND_USE_CASE_DEV_HDMI); /* HDMI RX */
-#if 0
+#ifdef QCOM_PROXY_DEVICE_ENABLED
         } else if (devices & AudioSystem::DEVICE_OUT_PROXY) {
             return strdup(SND_USE_CASE_DEV_PROXY_RX); /* PROXY RX */
+#endif
+#ifdef QCOM_FM_TX_ENABLED
         } else if (devices & AudioSystem::DEVICE_OUT_FM_TX) {
             return strdup(SND_USE_CASE_DEV_FM_TX); /* FM Tx */
 #endif
@@ -1289,7 +1295,7 @@
         if (!(mDevSettingsFlag & TTY_OFF) &&
             (callMode == AudioSystem::MODE_IN_CALL) &&
             ((devices & AudioSystem::DEVICE_IN_WIRED_HEADSET))) { 
-#if 0
+#ifdef QCOM_ANC_HEADSET_ENABLED
             ||(devices & AudioSystem::DEVICE_IN_ANC_HEADSET))) {
 #endif
              if (mDevSettingsFlag & TTY_HCO) {
@@ -1297,15 +1303,11 @@
              } else if (mDevSettingsFlag & TTY_FULL) {
                  return strdup(SND_USE_CASE_DEV_TTY_FULL_TX);
              } else if (mDevSettingsFlag & TTY_VCO) {
-#if 0
                  if (!strncmp(mic_type, "analog", 6)) {
-                     return strdup(SND_USE_CASE_DEV_TTY_HANDSET_ANAALOG_TX);
+                     return strdup(SND_USE_CASE_DEV_TTY_HANDSET_ANALOG_TX);
                  } else {
                      return strdup(SND_USE_CASE_DEV_TTY_HANDSET_TX);
                  }
-#else
-                 return strdup(SND_USE_CASE_DEV_TTY_HANDSET_TX);
-#endif
              }
         } else if (devices & AudioSystem::DEVICE_IN_BUILTIN_MIC) {
             if (!strncmp(mic_type, "analog", 6)) {
@@ -1332,7 +1334,7 @@
                 } else if (mDevSettingsFlag & QMIC_FLAG){
                     return strdup(SND_USE_CASE_DEV_QUAD_MIC);
                 } 
-#ifdef SSR_ENABLED
+#ifdef QCOM_SSR_ENABLED
                 else if (mDevSettingsFlag & SSRQMIC_FLAG){
                     ALOGV("return SSRQMIC_FLAG: 0x%x devices:0x%x",mDevSettingsFlag,devices);
                     // Mapping for quad mic input device.
@@ -1345,7 +1347,7 @@
             }
         } else if (devices & AudioSystem::DEVICE_IN_AUX_DIGITAL) {
             return strdup(SND_USE_CASE_DEV_HDMI_TX); /* HDMI TX */
-#if 0
+#ifdef QCOM_ANC_HEADSET_ENABLED
         } else if ((devices & AudioSystem::DEVICE_IN_WIRED_HEADSET) ||
                    (devices & AudioSystem::DEVICE_IN_ANC_HEADSET)) {
             return strdup(SND_USE_CASE_DEV_HEADSET); /* HEADSET TX */
@@ -1355,7 +1357,7 @@
                  return strdup(SND_USE_CASE_DEV_BTSCO_WB_TX); /* BTSCO TX*/
              else
                  return strdup(SND_USE_CASE_DEV_BTSCO_NB_TX); /* BTSCO TX*/
-#if 0
+#ifdef QCOM_USBAUDIO_ENABLED
         } else if ((devices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) ||
                    (devices & AudioSystem::DEVICE_IN_PROXY)) {
             return strdup(SND_USE_CASE_DEV_PROXY_TX); /* PROXY TX */
@@ -1366,7 +1368,7 @@
             if (strncmp(curTxUCMDevice, "None", 4)) {
                 return strdup(curTxUCMDevice);
             }
-#if 0
+#ifdef QCOM_FM_ENABLED
         } else if ((devices & AudioSystem::DEVICE_IN_FM_RX) ||
                    (devices & AudioSystem::DEVICE_IN_FM_RX_A2DP)) {
             /* Nothing to be done, use current tx device or set dummy device */
@@ -1399,7 +1401,7 @@
     control.set("Voice Rx Volume", vol, 0);
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err = csd_client_volume(vol);
         if (err < 0) {
             ALOGE("s_set_voice_volume: csd_client error %d", err);
@@ -1430,7 +1432,7 @@
     control.set("Voice Tx Mute", state, 0);
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err = csd_client_mic_mute(state);
         if (err < 0) {
             ALOGE("s_set_mic_mute: csd_client error %d", err);
@@ -1502,7 +1504,7 @@
     }
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err == csd_client_wide_voice(flag);
         if (err < 0) {
             ALOGE("s_enable_wide_voice: csd_client error %d", err);
@@ -1531,7 +1533,7 @@
     }
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err = csd_client_fens(flag);
         if (err < 0) {
             ALOGE("s_enable_fens: csd_client error %d", err);
@@ -1553,7 +1555,7 @@
     }
 
     if (platform_is_Fusion3()) {
-#if 0
+#ifdef QCOM_CSDCLIENT_ENABLED
         err = csd_client_slow_talk(flag);
         if (err < 0) {
             ALOGE("s_enable_slow_talk: csd_client error %d", err);
diff --git a/alsa_sound/audio_hw_hal.cpp b/alsa_sound/audio_hw_hal.cpp
index 58fd7d3..98edcf7 100644
--- a/alsa_sound/audio_hw_hal.cpp
+++ b/alsa_sound/audio_hw_hal.cpp
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "audio.primary.msm8960"
+#define LOG_TAG "qcom_audio_hw_hal"
 //#define LOG_NDEBUG 0
 
 #include <stdint.h>
@@ -176,7 +176,9 @@
 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
                                         int64_t *timestamp)
 {
-    return -EINVAL;
+    const struct qcom_stream_out *out =
+        reinterpret_cast<const struct qcom_stream_out *>(stream);
+    return out->qcom_out->getNextWriteTimestamp(timestamp);
 }
 
 /** audio_stream_in implementation **/
@@ -319,14 +321,24 @@
             AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
             AUDIO_DEVICE_OUT_AUX_DIGITAL |
             AUDIO_DEVICE_OUT_ALL_SCO |
-//          AUDIO_DEVICE_OUT_ANC_HEADSET |
+#ifdef QCOM_ANC_HEADSET_ENABLED
+            AUDIO_DEVICE_OUT_ANC_HEADSET |
+            AUDIO_DEVICE_OUT_ANC_HEADPHONE |
+#endif
             AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
             AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
-//          AUDIO_DEVICE_OUT_ANC_HEADPHONE |
-//          AUDIO_DEVICE_OUT_FM |
-//          AUDIO_DEVICE_OUT_FM_TX |
-//          AUDIO_DEVICE_OUT_DIRECTOUTPUT |
-//          AUDIO_DEVICE_OUT_PROXY |
+#ifdef QCOM_FM_ENABLED
+            AUDIO_DEVICE_OUT_FM |
+#endif
+#ifdef QCOM_FM_TX_ENABLED
+            AUDIO_DEVICE_OUT_FM_TX |
+#endif
+#ifdef QCOM_VOIP_ENABLED
+            AUDIO_DEVICE_OUT_DIRECTOUTPUT |
+#endif
+#ifdef QCOM_PROXY_DEVICE_ENABLED
+            AUDIO_DEVICE_OUT_PROXY |
+#endif
             AUDIO_DEVICE_OUT_DEFAULT |
             /* IN */
             AUDIO_DEVICE_IN_VOICE_CALL |
@@ -337,9 +349,13 @@
             AUDIO_DEVICE_IN_AUX_DIGITAL |
             AUDIO_DEVICE_IN_BACK_MIC |
             AUDIO_DEVICE_IN_ALL_SCO |
-//          AUDIO_DEVICE_IN_ANC_HEADSET |
-//          AUDIO_DEVICE_IN_FM_RX |
-//          AUDIO_DEVICE_IN_FM_RX_A2DP |
+#ifdef QCOM_ANC_HEADSET_ENABLED
+            AUDIO_DEVICE_IN_ANC_HEADSET |
+#endif
+#ifdef QCOM_FM_ENABLED
+            AUDIO_DEVICE_IN_FM_RX |
+            AUDIO_DEVICE_IN_FM_RX_A2DP |
+#endif
             AUDIO_DEVICE_IN_DEFAULT);
 }
 
@@ -367,7 +383,8 @@
     struct qcom_audio_device *qadev = to_ladev(dev);
     return qadev->hwif->getMasterVolume(volume);
 }
-#ifdef QUALCOMM_FEATURES_ENABLED
+
+#ifdef QCOM_FM_ENABLED
 static int adev_set_fm_volume(struct audio_hw_device *dev, float volume)
 {
     struct qcom_audio_device *qadev = to_ladev(dev);
@@ -416,7 +433,7 @@
     return qadev->hwif->getInputBufferSize(config->sample_rate, config->format, config->channel_mask);
 }
 
-#ifdef QUALCOMM_FEATURES_ENABLED
+#ifdef QCOM_TUNNEL_LPA_ENABLED
 static int adev_open_output_session(struct audio_hw_device *dev,
                                    uint32_t devices,
                                    int *format,
@@ -626,7 +643,7 @@
     qadev->device.set_voice_volume = adev_set_voice_volume;
     qadev->device.set_master_volume = adev_set_master_volume;
     qadev->device.get_master_volume = adev_get_master_volume;
-#ifdef QUALCOMM_FEATURES_ENABLED
+#ifdef QCOM_FM_ENABLED
     qadev->device.set_fm_volume = adev_set_fm_volume;
 #endif
     qadev->device.set_mode = adev_set_mode;
@@ -636,7 +653,7 @@
     qadev->device.get_parameters = adev_get_parameters;
     qadev->device.get_input_buffer_size = adev_get_input_buffer_size;
     qadev->device.open_output_stream = adev_open_output_stream;
-#ifdef QUALCOMM_FEATURES_ENABLED
+#ifdef QCOM_TUNNEL_LPA_ENABLED
     qadev->device.open_output_session = adev_open_output_session;
 #endif
     qadev->device.close_output_stream = adev_close_output_stream;
diff --git a/alsa_sound/audio_policy.conf b/alsa_sound/audio_policy.conf
new file mode 100644
index 0000000..49e8e79
--- /dev/null
+++ b/alsa_sound/audio_policy.conf
@@ -0,0 +1,46 @@
+#
+# Audio policy configuration for generic device builds (goldfish audio HAL - emulator)
+#
+
+# Global configuration section: lists input and output devices always present on the device
+# as well as the output device selected by default.
+# Devices are designated by a string that corresponds to the enum in audio.h
+
+global_configuration {
+  attached_output_devices AUDIO_DEVICE_OUT_SPEAKER
+  default_output_device AUDIO_DEVICE_OUT_SPEAKER
+  attached_input_devices AUDIO_DEVICE_IN_BUILTIN_MIC
+}
+
+# audio hardware module section: contains descriptors for all audio hw modules present on the
+# device. Each hw module node is named after the corresponding hw module library base name.
+# For instance, "primary" corresponds to audio.primary.<device>.so.
+# The "primary" module is mandatory and must include at least one output with
+# AUDIO_OUTPUT_FLAG_PRIMARY flag.
+# Each module descriptor contains one or more output profile descriptors and zero or more
+# input profile descriptors. Each profile lists all the parameters supported by a given output
+# or input stream category.
+# The "channel_masks", "formats", "devices" and "flags" are specified using strings corresponding
+# to enums in audio.h and audio_policy.h. They are concatenated by use of "|" without space or "\n".
+
+audio_hw_modules {
+  primary {
+    outputs {
+      primary {
+        sampling_rates 44100
+        channel_masks AUDIO_CHANNEL_OUT_STEREO
+        formats AUDIO_FORMAT_PCM_16_BIT
+        devices AUDIO_DEVICE_OUT_SPEAKER|AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_EARPIECE
+        flags AUDIO_OUTPUT_FLAG_PRIMARY
+      }
+    }
+    inputs {
+      primary {
+        sampling_rates 8000|11025|12000|16000|22050|24000|32000|44100|48000
+        channel_masks AUDIO_CHANNEL_IN_MONO|AUDIO_CHANNEL_IN_STEREO
+        formats AUDIO_FORMAT_PCM_16_BIT
+        devices AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET
+      }
+    }
+  }
+}
diff --git a/alsa_sound/audio_policy_hal.cpp b/alsa_sound/audio_policy_hal.cpp
index 7c05fab..d91d81e 100644
--- a/alsa_sound/audio_policy_hal.cpp
+++ b/alsa_sound/audio_policy_hal.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "audio_policy.msm8960"
+#define LOG_TAG "qcom_audio_policy_hal"
 //#define LOG_NDEBUG 0
 
 #include <stdint.h>
@@ -90,16 +90,6 @@
     qap->apm->setPhoneState(state);
 }
 
-    /* indicate a change in ringer mode */
-static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode,
-                               uint32_t mask)
-{
-#if 0
-    struct qcom_audio_policy *qap = to_qap(pol);
-    qap->apm->setRingerMode(mode, mask);
-#endif
-}
-
     /* force using a specific device category for the specified usage */
 static void ap_set_force_use(struct audio_policy *pol,
                           audio_policy_force_use_t usage,
@@ -135,7 +125,7 @@
     return qap->apm->initCheck();
 }
 
-#ifdef TUNNEL_LPA_ENABLED 
+#ifdef QCOM_TUNNEL_LPA_ENABLED
 static audio_io_handle_t ap_get_session(struct audio_policy *pol,
                                        audio_stream_type_t stream,
                                        audio_format_t format,
@@ -369,14 +359,13 @@
     qap->policy.set_device_connection_state = ap_set_device_connection_state;
     qap->policy.get_device_connection_state = ap_get_device_connection_state;
     qap->policy.set_phone_state = ap_set_phone_state;
-    qap->policy.set_ringer_mode = ap_set_ringer_mode;
     qap->policy.set_force_use = ap_set_force_use;
     qap->policy.get_force_use = ap_get_force_use;
     qap->policy.set_can_mute_enforced_audible =
         ap_set_can_mute_enforced_audible;
     qap->policy.init_check = ap_init_check;
     qap->policy.get_output = ap_get_output;
-#ifdef TUNNEL_LPA_ENABLED
+#ifdef QCOM_TUNNEL_LPA_ENABLED
     qap->policy.get_session = ap_get_session;
     qap->policy.pause_session = ap_pause_session;
     qap->policy.resume_session = ap_resume_session;