Merge "PAL: Add support to vote NLPI for SVA GMM"
diff --git a/configs/crow/mixer_paths_crow_qrd.xml b/configs/crow/mixer_paths_crow_qrd.xml
old mode 100644
new mode 100755
index 44bc7ba..afcf9d3
--- a/configs/crow/mixer_paths_crow_qrd.xml
+++ b/configs/crow/mixer_paths_crow_qrd.xml
@@ -562,7 +562,7 @@
         <ctl name="SpkrLeft COMP Switch" value="1" />
         <ctl name="SpkrLeft VISENSE Switch" value="1" />
         <ctl name="SpkrLeft SWR DAC_Port Switch" value="1" />
-        <ctl name="WSA_RX0 Digital Volume" value="70" />
+        <ctl name="WSA_RX0 Digital Volume" value="60" />
     </path>
 
     <path name="handset-vbat">
diff --git a/configs/kalama/resourcemanager_kalama_cdp.xml b/configs/kalama/resourcemanager_kalama_cdp.xml
index b706057..5ba1983 100644
--- a/configs/kalama/resourcemanager_kalama_cdp.xml
+++ b/configs/kalama/resourcemanager_kalama_cdp.xml
@@ -766,6 +766,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/configs/kalama/resourcemanager_kalama_cdp_apq.xml b/configs/kalama/resourcemanager_kalama_cdp_apq.xml
index 885b661..924b3ee 100644
--- a/configs/kalama/resourcemanager_kalama_cdp_apq.xml
+++ b/configs/kalama/resourcemanager_kalama_cdp_apq.xml
@@ -761,6 +761,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/configs/kalama/resourcemanager_kalama_grd.xml b/configs/kalama/resourcemanager_kalama_grd.xml
index 665934f..becf30f 100644
--- a/configs/kalama/resourcemanager_kalama_grd.xml
+++ b/configs/kalama/resourcemanager_kalama_grd.xml
@@ -764,6 +764,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/configs/kalama/resourcemanager_kalama_mtp.xml b/configs/kalama/resourcemanager_kalama_mtp.xml
index 7dffdc0..bda23c9 100644
--- a/configs/kalama/resourcemanager_kalama_mtp.xml
+++ b/configs/kalama/resourcemanager_kalama_mtp.xml
@@ -781,6 +781,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/configs/kalama/resourcemanager_kalama_mtp_apq.xml b/configs/kalama/resourcemanager_kalama_mtp_apq.xml
index f596bdf..fee3ff1 100644
--- a/configs/kalama/resourcemanager_kalama_mtp_apq.xml
+++ b/configs/kalama/resourcemanager_kalama_mtp_apq.xml
@@ -776,6 +776,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/configs/kalama/resourcemanager_kalama_qrd.xml b/configs/kalama/resourcemanager_kalama_qrd.xml
index d6c6f5f..3818244 100644
--- a/configs/kalama/resourcemanager_kalama_qrd.xml
+++ b/configs/kalama/resourcemanager_kalama_qrd.xml
@@ -782,6 +782,7 @@
               <name>PAL_STREAM_ULTRASOUND</name>
               <priority>4</priority>
               <samplerate>96000</samplerate>
+              <bit_width>16</bit_width>
           </usecase>
         </out-device>
         <out-device>
diff --git a/device/inc/Device.h b/device/inc/Device.h
index 638766d..c14a16a 100644
--- a/device/inc/Device.h
+++ b/device/inc/Device.h
@@ -100,6 +100,7 @@
     //device atrributues per stream are stored by priority in a map
     std::multimap<uint32_t, std::pair<Stream *, struct pal_device *>> mStreamDevAttr;
     uint32_t mSampleRate = 0;
+    uint32_t mBitWidth = 0;
 
     Device(struct pal_device *device, std::shared_ptr<ResourceManager> Rm);
     Device();
@@ -138,6 +139,7 @@
     virtual ~Device();
     void getCurrentSndDevName(char *name);
     void setSampleRate(uint32_t sr){mSampleRate = sr;};
+    void setBitWidth(uint32_t bw) {mBitWidth = bw;};
     void lockDeviceMutex() { mDeviceMutex.lock(); };
     void unlockDeviceMutex() { mDeviceMutex.unlock(); };
     bool compareStreamDevAttr(const struct pal_device *inDevAttr,
diff --git a/device/src/Device.cpp b/device/src/Device.cpp
index 5694e41..5a75d7c 100644
--- a/device/src/Device.cpp
+++ b/device/src/Device.cpp
@@ -934,7 +934,10 @@
     /* update sample rate if it's valid */
     if (mSampleRate)
         deviceAttr->config.sample_rate = mSampleRate;
-
+    if (mBitWidth) {
+        deviceAttr->config.bit_width = mBitWidth;
+        deviceAttr->config.aud_fmt_id = rm->getAudioFmt(mBitWidth);
+    }
 #if DUMP_DEV_ATTR
     pal_stream_attributes dumpstrAttr;
     (*it).second.first->getStreamAttributes(&dumpstrAttr);
diff --git a/device/src/USBAudio.cpp b/device/src/USBAudio.cpp
index 072ebd2..5c40793 100644
--- a/device/src/USBAudio.cpp
+++ b/device/src/USBAudio.cpp
@@ -774,16 +774,12 @@
         channels = MAX_HIFI_CHANNEL_COUNT;
 
     if (is_playback) {
-        // start from 2 channels as framework currently doesn't support mono.
-        if (channels >= 2) {
-            channel[num_masks++] = audio_channel_out_mask_from_count(2);
-        }
-        for (channel_count = 2;
-                channel_count <= channels && num_masks < MAX_SUPPORTED_CHANNEL_MASKS;
-                ++channel_count) {
-            channel[num_masks++] =
-                    audio_channel_mask_for_index_assignment_from_count(channel_count);
-        }
+        channel[num_masks++] = channels <= 2
+                     /* position mask for mono and stereo*/
+                     ? audio_channel_out_mask_from_count(channels)
+                     /* otherwise indexed */
+                     : audio_channel_mask_for_index_assignment_from_count(channels);
+        // TODO: needs to figure out the accurate match of channel mask
     } else {
         // For capture we report all supported channel masks from 1 channel up.
         channel_count = MIN_CHANNEL_COUNT;
diff --git a/resource_manager/src/ResourceManager.cpp b/resource_manager/src/ResourceManager.cpp
index 44a278e..d037720 100644
--- a/resource_manager/src/ResourceManager.cpp
+++ b/resource_manager/src/ResourceManager.cpp
@@ -4447,7 +4447,7 @@
          * 1. sound model loaded but not started by sthal
          * 2. stop recognition called by sthal
          */
-        if (!str->isActive())
+        if (!str->isStarted())
             continue;
 
         cap_prof = str->GetCurrentCaptureProfile();
@@ -8720,82 +8720,29 @@
         case PAL_PARAM_ID_BT_A2DP_RECONFIG_SUPPORTED:
         case PAL_PARAM_ID_BT_A2DP_SUSPENDED:
         case PAL_PARAM_ID_BT_A2DP_ENCODER_LATENCY:
-        {
-            std::shared_ptr<Device> dev = nullptr;
-            struct pal_device dattr;
-            pal_param_bta2dp_t *param_bt_a2dp = nullptr;
-
-            if (isDeviceAvailable(PAL_DEVICE_OUT_BLUETOOTH_A2DP)) {
-                dattr.id = PAL_DEVICE_OUT_BLUETOOTH_A2DP;
-            } else if (isDeviceAvailable(PAL_DEVICE_OUT_BLUETOOTH_BLE)) {
-                dattr.id = PAL_DEVICE_OUT_BLUETOOTH_BLE;
-            } else if (isDeviceAvailable(PAL_DEVICE_OUT_BLUETOOTH_BLE_BROADCAST)) {
-                dattr.id = PAL_DEVICE_OUT_BLUETOOTH_BLE_BROADCAST;
-            } else {
-                goto exit;
-            }
-            dev = Device::getInstance(&dattr , rm);
-            if (dev) {
-                status = dev->getDeviceParameter(param_id, (void **)&param_bt_a2dp);
-                if (status) {
-                    PAL_ERR(LOG_TAG, "get Parameter %d failed\n", param_id);
-                    goto exit;
-                }
-                *param_payload = param_bt_a2dp;
-                *payload_size = sizeof(pal_param_bta2dp_t);
-            }
-            break;
-        }
         case PAL_PARAM_ID_BT_A2DP_CAPTURE_SUSPENDED:
-        {
-            std::shared_ptr<Device> dev = nullptr;
-            struct pal_device dattr;
-            pal_param_bta2dp_t *param_bt_a2dp = nullptr;
-
-            if (isDeviceAvailable(PAL_DEVICE_IN_BLUETOOTH_A2DP)) {
-                dattr.id = PAL_DEVICE_IN_BLUETOOTH_A2DP;
-            } else if (isDeviceAvailable(PAL_DEVICE_IN_BLUETOOTH_BLE)) {
-                dattr.id = PAL_DEVICE_IN_BLUETOOTH_BLE;
-            } else {
-                goto exit;
-            }
-            dev = Device::getInstance(&dattr , rm);
-            if (dev) {
-                status = dev->getDeviceParameter(param_id, (void **)&param_bt_a2dp);
-                if (status) {
-                    PAL_ERR(LOG_TAG, "get Parameter %d failed\n", param_id);
-                    goto exit;
-                }
-                *param_payload = param_bt_a2dp;
-                *payload_size = sizeof(pal_param_bta2dp_t);
-            }
-            break;
-        }
         case PAL_PARAM_ID_BT_A2DP_DECODER_LATENCY:
         {
             std::shared_ptr<Device> dev = nullptr;
             struct pal_device dattr;
             pal_param_bta2dp_t* param_bt_a2dp = nullptr;
 
-            if (isDeviceAvailable(PAL_DEVICE_IN_BLUETOOTH_A2DP)) {
-                dattr.id = PAL_DEVICE_IN_BLUETOOTH_A2DP;
-            } else if (isDeviceAvailable(PAL_DEVICE_IN_BLUETOOTH_BLE)) {
-                dattr.id = PAL_DEVICE_IN_BLUETOOTH_BLE;
+            if (isDeviceAvailable((*(pal_param_bta2dp_t**)param_payload)->dev_id)) {
+                dattr.id = (*(pal_param_bta2dp_t**)param_payload)->dev_id;
             } else {
                 goto exit;
             }
+
             dev = Device::getInstance(&dattr, rm);
-            if (!dev) {
-                PAL_ERR(LOG_TAG, "Failed to get device instance");
-                goto exit;
+            if (dev) {
+                status = dev->getDeviceParameter(param_id, (void**)&param_bt_a2dp);
+                if (status) {
+                    PAL_ERR(LOG_TAG, "get Parameter %d failed\n", param_id);
+                    goto exit;
+                }
+                *param_payload = param_bt_a2dp;
+                *payload_size = sizeof(pal_param_bta2dp_t);
             }
-            status = dev->getDeviceParameter(param_id, (void**)&param_bt_a2dp);
-            if (status) {
-                PAL_ERR(LOG_TAG, "get Parameter %d failed\n", param_id);
-                goto exit;
-            }
-            *param_payload = param_bt_a2dp;
-            *payload_size = sizeof(pal_param_bta2dp_t);
             break;
         }
         case PAL_PARAM_ID_GAIN_LVL_MAP:
diff --git a/stream/inc/StreamSoundTrigger.h b/stream/inc/StreamSoundTrigger.h
index 83f1fbb..9da6d26 100644
--- a/stream/inc/StreamSoundTrigger.h
+++ b/stream/inc/StreamSoundTrigger.h
@@ -147,7 +147,7 @@
                             bool enable __unused) {
         return -ENOSYS;
     }
-
+    bool isStarted();
     void SetDetectedToEngines(bool detected);
     int32_t SetEngineDetectionState(int32_t state);
 
@@ -182,7 +182,6 @@
               (GetCurrentStateId() == ST_STATE_BUFFERING);
     }
     struct st_uuid GetVendorUuid();
-
     void *GetGSLEngine() {
         if (gsl_engine_)
             return (void *)gsl_engine_.get();
diff --git a/stream/src/Stream.cpp b/stream/src/Stream.cpp
index 02ea578..1f8f2c0 100644
--- a/stream/src/Stream.cpp
+++ b/stream/src/Stream.cpp
@@ -1399,11 +1399,12 @@
     char CurrentSndDeviceName[DEVICE_NAME_MAX_SIZE] = {0};
     std::vector <Stream *> streamsToSwitch;
     struct pal_device streamDevAttr;
+    struct pal_device sco_Dattr = {};
     std::vector <Stream*>::iterator sIter;
     bool has_out_device = false, has_in_device = false;
     std::vector <std::shared_ptr<Device>>::iterator dIter;
     struct pal_volume_data *volume = NULL;
-    pal_device_id_t curBtDevId;
+    pal_device_id_t curBtDevId = PAL_DEVICE_NONE;
     pal_device_id_t newBtDevId;
     bool isBtReady = false;
 
@@ -1553,9 +1554,10 @@
             newBtDevId = newDevices[i].id;
             dev = Device::getInstance(&newDevices[i], rm);
             if (!dev) {
-                status = -ENODEV;
                 PAL_ERR(LOG_TAG, "failed to get a2dp/ble device object");
-                goto done;
+                mStreamMutex.unlock();
+                rm->unlockActiveStream();
+                return -ENODEV;
             }
             dev->getDeviceParameter(PAL_PARAM_ID_BT_A2DP_SUSPENDED,
                 (void**)&param_bt_a2dp);
@@ -1599,7 +1601,8 @@
         if (!dev) {
             PAL_ERR(LOG_TAG, "No device instance found");
             mStreamMutex.unlock();
-            return -EINVAL;
+            rm->unlockActiveStream();
+            return -ENODEV;
         }
         dev->insertStreamDeviceAttr(&newDevices[i], streamHandle);
         mPalDevices.push_back(dev);
@@ -1684,7 +1687,6 @@
                 (strAttr.type == PAL_STREAM_VOIP_TX &&
                     newDeviceId == PAL_DEVICE_IN_BLUETOOTH_SCO_HEADSET &&
                     rm->isDeviceActive(PAL_DEVICE_OUT_BLUETOOTH_SCO)))) {
-                struct pal_device sco_Dattr = {};
                 std::shared_ptr<Device> scoDev = nullptr;
                 std::vector <Stream*> activeStreams;
                 if (newDeviceId == PAL_DEVICE_OUT_BLUETOOTH_SCO) {
@@ -1698,7 +1700,7 @@
                     PAL_ERR(LOG_TAG, "getDeviceConfig for bt-sco failed");
                     mStreamMutex.unlock();
                     rm->unlockActiveStream();
-                    return 0;
+                    return status;
                 }
 
                 rm->getActiveStream_l(activeStreams, scoDev);
@@ -1761,7 +1763,7 @@
                             PAL_ERR(LOG_TAG,"getStreamAttributes Failed \n");
                             mStreamMutex.unlock();
                             rm->unlockActiveStream();
-                            goto done;
+                            return status;
                         }
 
                         if (sAttr.type == PAL_STREAM_ULTRASOUND &&
@@ -1848,7 +1850,6 @@
         if (!volume) {
             PAL_ERR(LOG_TAG, "pal_volume_data memory allocation failure");
             mStreamMutex.unlock();
-            rm->unlockActiveStream();
             return -ENOMEM;
         }
         status = streamHandle->getVolumeData(volume);
diff --git a/stream/src/StreamCommon.cpp b/stream/src/StreamCommon.cpp
index bc00847..3c8c7e1 100644
--- a/stream/src/StreamCommon.cpp
+++ b/stream/src/StreamCommon.cpp
@@ -121,6 +121,8 @@
                 continue;
             rm->getDeviceInfo(devAttr.id, sattr->type, "", &inDeviceInfo);
             dev->setSampleRate(inDeviceInfo.samplerate);
+            if (devAttr.id == PAL_DEVICE_OUT_HANDSET)
+                dev->setBitWidth(inDeviceInfo.bit_width);
         }
     }
     for (int i = 0; i < no_of_devices; i++) {
@@ -184,6 +186,7 @@
             if (!dev)
                 continue;
             dev->setSampleRate(0);
+            dev->setBitWidth(0);
         }
     }
 
diff --git a/stream/src/StreamPCM.cpp b/stream/src/StreamPCM.cpp
index e001253..5f2c5a3 100644
--- a/stream/src/StreamPCM.cpp
+++ b/stream/src/StreamPCM.cpp
@@ -1656,8 +1656,10 @@
         rm->lockGraph();
         for (int32_t i=0; i < mDevices.size(); i++) {
             if ((mDevices[i]->getSndDeviceId() == PAL_DEVICE_OUT_BLUETOOTH_A2DP) ||
-                (mDevices[i]->getSndDeviceId() == PAL_DEVICE_OUT_BLUETOOTH_BLE)) {
-                PAL_DBG(LOG_TAG, "start BT A2DP/BLE device as to populate the full GKVs");
+                (mDevices[i]->getSndDeviceId() == PAL_DEVICE_OUT_BLUETOOTH_BLE)  ||
+                (mDevices[i]->getSndDeviceId() == PAL_DEVICE_IN_BLUETOOTH_BLE)   ||
+                (mDevices[i]->getSndDeviceId() == PAL_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
+                PAL_DBG(LOG_TAG, "start BT devices as to populate the full GKVs");
                 status = mDevices[i]->start();
                 if ((0 != status) && mDevices.size() == 1) {
                     PAL_ERR(LOG_TAG, "device start failed: %d", status);
diff --git a/stream/src/StreamSoundTrigger.cpp b/stream/src/StreamSoundTrigger.cpp
index b587ab9..2014c1d 100644
--- a/stream/src/StreamSoundTrigger.cpp
+++ b/stream/src/StreamSoundTrigger.cpp
@@ -1967,7 +1967,7 @@
                     }
 
                     TransitTo(ST_STATE_LOADED);
-                    if (st_stream_.isActive()) {
+                    if (st_stream_.isStarted()) {
                         std::shared_ptr<StEventConfig> ev_cfg1(
                             new StStartRecognitionEventConfig(false));
                         status = st_stream_.ProcessInternalEvent(ev_cfg1);
@@ -2066,7 +2066,7 @@
         }
         case ST_EV_RESUME: {
             st_stream_.paused_ = false;
-            if (!st_stream_.isActive()) {
+            if (!st_stream_.isStarted()) {
                 // Possible if App has stopped recognition during active
                 // concurrency.
                 break;
@@ -2287,7 +2287,7 @@
                 st_stream_.device_opened_ = true;
             }
 
-            if (st_stream_.isActive() && !st_stream_.paused_) {
+            if (st_stream_.isStarted() && !st_stream_.paused_) {
                 status = dev->start();
                 if (0 != status) {
                     PAL_ERR(LOG_TAG, "device %d start failed with status %d",
@@ -2307,7 +2307,7 @@
                 st_stream_.mDevices.pop_back();
                 dev->close();
                 st_stream_.device_opened_ = false;
-            } else if (st_stream_.isActive() && !st_stream_.paused_) {
+            } else if (st_stream_.isStarted() && !st_stream_.paused_) {
                 if (!rm->isDeviceActive_l(dev, &st_stream_))
                     st_stream_.rm->registerDevice(dev, &st_stream_);
                 if (st_stream_.second_stage_processing_) {
@@ -3528,6 +3528,12 @@
     return status;
 }
 
+bool StreamSoundTrigger::isStarted() {
+    return (currentState == STREAM_STARTED ||
+            GetCurrentStateId() == ST_STATE_BUFFERING ||
+            GetCurrentStateId() == ST_STATE_DETECTED);
+}
+
 struct st_uuid StreamSoundTrigger::GetVendorUuid()
 {
     struct st_uuid uuid;
diff --git a/utils/src/ChargerListener.cpp b/utils/src/ChargerListener.cpp
index 685e40a..daefd3d 100644
--- a/utils/src/ChargerListener.cpp
+++ b/utils/src/ChargerListener.cpp
@@ -385,14 +385,14 @@
 int ChargerListenerImpl::getConcurrentState()
 {
     int status_bit = -EINVAL;
-    char state[12];
+    char state[22];
 
     mlock.lock();
     if (0 != readSysfsPath(BOOST_CONCURRENT_PATH, O_RDONLY, 2, state)) {
         ALOGE("%s %d, read Concurrency bit failed %s", __func__, __LINE__,
               strerror(errno));
     } else {
-        sscanf(state, "%d\n", &status_bit);
+        sscanf(state, "%d", &status_bit);
     }
     mlock.unlock();
     return status_bit;