qcom/audio/hal: Fix the device switch delay issue during voice call

- Device switch from earpiece to speaker during voice call takes
  around 3.5sec which is not acceptable
- Disabling the voice call mixer path consumes 3.5 sec
- Fixed by making sure that device path is deactivated on MDM through
  CSD client before disabling voice call mixer path on APQ.
- Also remove incorrect calls to dlerror() and make the dlsym-error messages
  more consistent.

Change-Id: Ib23c0a3c0341f41904ca06524bf9d2f4214ad92e
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index f4c0e20..49b3634 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -467,7 +467,7 @@
 
     if (out_snd_device == adev->cur_out_snd_device && adev->out_snd_device_active &&
         in_snd_device == adev->cur_in_snd_device && adev->in_snd_device_active) {
-        ALOGV("%s: exit: snd_devices (%d and %d) is already active",
+        ALOGV("%s: exit: snd_devices (%d and %d) are already active",
               __func__, out_snd_device, in_snd_device);
         return 0;
     }
@@ -486,6 +486,20 @@
         in_call_device_switch = true;
     }
 
+    if (in_call_device_switch) {
+        /* This must be called before disabling the mixer controls on APQ side */
+        if (adev->csd_disable_device == NULL) {
+            ALOGE("%s: dlsym error for csd_client_disable_device",
+                  __func__);
+        } else {
+            status = adev->csd_disable_device();
+            if (status < 0) {
+                ALOGE("%s: csd_client_disable_device, failed, error %d",
+                      __func__, status);
+            }
+        }
+    }
+
     if ((out_snd_device != adev->cur_out_snd_device || in_call_device_switch)
             && adev->out_snd_device_active) {
         usecase = &adev->usecase_list;
@@ -518,19 +532,6 @@
         adev->in_snd_device_active = false;
     }
 
-    if (in_call_device_switch) {
-        if (adev->csd_disable_device == NULL) {
-            ALOGE("%s: dlsym error:%s for csd_client_disable_device",
-                  __func__, dlerror());
-        } else {
-            status = adev->csd_disable_device();
-            if (status < 0) {
-                ALOGE("%s: csd_client_disable_device, failed, error %d",
-                      __func__, status);
-            }
-        }
-    }
-
     if (out_snd_device != SND_DEVICE_INVALID && !adev->out_snd_device_active) {
         /* Enable new rx device */
         status = enable_snd_device(adev, out_snd_device);
@@ -556,23 +557,6 @@
     }
     audio_route_update_mixer(adev->audio_route);
 
-    if (in_call_device_switch) {
-        if (adev->csd_enable_device == NULL) {
-            ALOGE("%s: dlsym error: %s for csd_client_enable_device",
-                  __func__, dlerror());
-        } else {
-            acdb_rx_id = get_acdb_device_id(out_snd_device);
-            acdb_tx_id = get_acdb_device_id(in_snd_device);
-
-            /* ToDo: To make sure acdb_settings is updated properly based on TTY mode */
-            status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id, adev->acdb_settings);
-            if (status < 0) {
-                ALOGE("%s: csd_client_enable_device, failed, error %d",
-                      __func__, status);
-            }
-        }
-    }
-
     usecase = &adev->usecase_list;
     while (usecase->next != NULL) {
         usecase = usecase->next;
@@ -587,6 +571,23 @@
     }
     audio_route_update_mixer(adev->audio_route);
 
+    if (in_call_device_switch) {
+        if (adev->csd_enable_device == NULL) {
+            ALOGE("%s: dlsym error for csd_client_enable_device",
+                  __func__);
+        } else {
+            acdb_rx_id = get_acdb_device_id(out_snd_device);
+            acdb_tx_id = get_acdb_device_id(in_snd_device);
+
+            /* ToDo: To make sure acdb_settings is updated properly based on TTY mode */
+            status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id, adev->acdb_settings);
+            if (status < 0) {
+                ALOGE("%s: csd_client_enable_device, failed, error %d",
+                      __func__, status);
+            }
+        }
+    }
+
     ALOGV("%s: exit: status(%d)", __func__, status);
     return status;
 }
@@ -913,7 +914,7 @@
     ALOGV("%s: enter: usecase(%d)", __func__, USECASE_VOICE_CALL);
     if (adev->csd_client) {
         if (adev->csd_stop_voice == NULL) {
-            ALOGE("dlsym:Error:%s Loading csd_client_disable_device", dlerror());
+            ALOGE("dlsym error for csd_client_disable_device");
         } else {
             ret = adev->csd_stop_voice();
             if (ret < 0) {
@@ -1024,7 +1025,7 @@
 
     if (adev->csd_client) {
         if (adev->csd_start_voice == NULL) {
-            ALOGE("dlsym:Error:%s Loading csd_client_start_voice", dlerror());
+            ALOGE("dlsym error for csd_client_start_voice");
         } else {
             ret = adev->csd_start_voice();
             if (ret < 0) {
@@ -1670,7 +1671,7 @@
 
         if (adev->csd_client) {
             if (adev->csd_volume == NULL) {
-                ALOGE("%s:Error:%s Loading csd_client_volume", __func__, dlerror());
+                ALOGE("%s: dlsym error for csd_client_volume", __func__);
             } else {
                 err = adev->csd_volume(vol);
                 if (err < 0) {
@@ -1727,7 +1728,7 @@
     if (adev->mode == AUDIO_MODE_IN_CALL) {
         if (adev->csd_client) {
             if (adev->csd_mic_mute == NULL) {
-                ALOGE("%s: Error:%s Loading csd_mic_mute", __func__, dlerror());
+                ALOGE("%s: dlsym error for csd_mic_mute", __func__);
             } else {
                 err = adev->csd_mic_mute(state);
                 if (err < 0) {
@@ -1853,16 +1854,16 @@
         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
     } else {
         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
-        adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
-                                                    "acdb_loader_init_ACDB");
         adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
                                                     "acdb_loader_deallocate_ACDB");
         adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
                                                     "acdb_loader_send_audio_cal");
         adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
                                                     "acdb_loader_send_voice_cal");
+        adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
+                                                    "acdb_loader_init_ACDB");
         if (adev->acdb_init == NULL)
-            ALOGE("%s: Error:%s Loading acdb_loader_init_ACDB", __func__, dlerror());
+            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
         else
             adev->acdb_init();
     }
@@ -1881,8 +1882,6 @@
 
     if (adev->csd_client) {
         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
-        adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
-                                                    "csd_client_init");
         adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
                                                     "csd_client_deinit");
         adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
@@ -1897,9 +1896,11 @@
                                                     "csd_client_volume");
         adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
                                                     "csd_client_mic_mute");
+        adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
+                                                    "csd_client_init");
 
         if (adev->csd_client_init == NULL) {
-            ALOGE("dlsym: Error:%s Loading csd_client_init", dlerror());
+            ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
         } else {
             adev->csd_client_init();
         }