audio: introduce dedicated adev->cal_lock for gef interfaces

Audio device lock is not suitable for GEF operations, so add a dedicated
calibration lock for GEF.

CRs-Fixed: 2413132
Change-Id: Ic1e840c02e9d7f1bce5d3cb12702366af1c07f01
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 578ed9e..4c597c9 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -842,7 +842,7 @@
 #ifndef AUDIO_GENERIC_EFFECT_FRAMEWORK_ENABLED
 
 #define audio_extn_gef_init(adev) (0)
-#define audio_extn_gef_deinit() (0)
+#define audio_extn_gef_deinit(adev) (0)
 #define audio_extn_gef_notify_device_config(devices, cmask, sample_rate, acdb_id) (0)
 
 #ifndef INSTANCE_ID_ENABLED
@@ -872,7 +872,7 @@
 #else
 
 void audio_extn_gef_init(struct audio_device *adev);
-void audio_extn_gef_deinit();
+void audio_extn_gef_deinit(struct audio_device *adev);
 
 void audio_extn_gef_notify_device_config(audio_devices_t audio_device,
     audio_channel_mask_t channel_mask, int sample_rate, int acdb_id);
diff --git a/hal/audio_extn/gef.c b/hal/audio_extn/gef.c
index 7f82a8a..cfed6b3 100644
--- a/hal/audio_extn/gef.c
+++ b/hal/audio_extn/gef.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -113,6 +113,7 @@
 
     ALOGV("%s: Enter with error", __func__);
 
+    pthread_mutex_init(&adev->cal_lock, (const pthread_mutexattr_t *) NULL);
     memset(&gef_hal_handle, 0, sizeof(gef_data));
 
     //: check error for dlopen
@@ -186,8 +187,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -196,7 +197,7 @@
 
     ret = platform_send_audio_cal(adev->platform, &cal, data, length, persist);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -218,8 +219,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -228,7 +229,7 @@
 
     ret = platform_get_audio_cal(adev->platform, &cal, data, length, persist);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -250,8 +251,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -260,7 +261,7 @@
 
     ret = platform_store_audio_cal(adev->platform, &cal, data, length);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -281,8 +282,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -291,7 +292,7 @@
 
     ret = platform_retrieve_audio_cal(adev->platform, &cal, data, length);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -313,8 +314,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -323,7 +324,7 @@
 
     ret = platform_send_audio_cal(adev->platform, &cal, data, length, persist);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -345,8 +346,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -355,7 +356,7 @@
 
     ret = platform_get_audio_cal(adev->platform, &cal, data, length, persist);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -376,8 +377,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -386,7 +387,7 @@
 
     ret = platform_store_audio_cal(adev->platform, &cal, data, length);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -407,8 +408,8 @@
     ALOGV("%s: Enter", __func__);
     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
 
-    //lock adev
-    pthread_mutex_lock(&adev->lock);
+    //lock adev->cal_lock
+    pthread_mutex_lock(&adev->cal_lock);
 
     //pack cal
     platform_make_cal_cfg(&cal, acdb_dev_id,
@@ -417,7 +418,7 @@
 
     ret = platform_retrieve_audio_cal(adev->platform, &cal, data, length);
 
-    pthread_mutex_unlock(&adev->lock);
+    pthread_mutex_unlock(&adev->cal_lock);
 
     ALOGV("%s: Exit with error %d", __func__, ret);
 
@@ -442,7 +443,7 @@
     return;
 }
 
-void audio_extn_gef_deinit()
+void audio_extn_gef_deinit(struct audio_device *adev)
 {
     ALOGV("%s: Enter", __func__);
 
@@ -452,6 +453,7 @@
         dlclose(gef_hal_handle.handle);
     }
 
+    pthread_mutex_destroy(&adev->cal_lock);
     memset(&gef_hal_handle, 0, sizeof(gef_data));
 
     ALOGV("%s: Exit", __func__);
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 8da1c08..5eb4e1e 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2103,10 +2103,6 @@
     struct stream_out stream_out;
     audio_usecase_t hfp_ucid;
     int status = 0;
-    audio_devices_t audio_device;
-    audio_channel_mask_t channel_mask;
-    int sample_rate;
-    int acdb_id;
 
     ALOGD("%s for use case (%s)", __func__, use_case_table[uc_id]);
 
@@ -2357,11 +2353,12 @@
             usecase->stream.out->app_type_cfg.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
         }
 
-        /* Cache stream information to be notified to gef clients */
-        audio_device = usecase->stream.out->devices;
-        channel_mask = usecase->stream.out->channel_mask;
-        sample_rate = usecase->stream.out->app_type_cfg.sample_rate;
-        acdb_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
+        /* Notify device change info to effect clients registered */
+        audio_extn_gef_notify_device_config(
+                usecase->stream.out->devices,
+                usecase->stream.out->channel_mask,
+                usecase->stream.out->app_type_cfg.sample_rate,
+                platform_get_snd_device_acdb_id(usecase->out_snd_device));
     }
     enable_audio_route(adev, usecase);
 
@@ -2419,16 +2416,6 @@
          }
     }
 
-    /* Notify device change info to effect clients registered
-     * NOTE: device lock has to be unlock temporarily here.
-     * To the worst case, we notify stale info to clients.
-     */
-    if (usecase->type == PCM_PLAYBACK) {
-        pthread_mutex_unlock(&adev->lock);
-        audio_extn_gef_notify_device_config(audio_device, channel_mask, sample_rate, acdb_id);
-        pthread_mutex_lock(&adev->lock);
-    }
-
     ALOGD("%s: done",__func__);
 
     return status;
@@ -7726,7 +7713,7 @@
         if (audio_extn_qaf_is_enabled())
             audio_extn_qaf_deinit();
         audio_route_free(adev->audio_route);
-        audio_extn_gef_deinit();
+        audio_extn_gef_deinit(adev);
         free(adev->snd_dev_ref_cnt);
         platform_deinit(adev->platform);
         if (adev->adm_deinit)
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 9e2b6fc..e367909 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -465,6 +465,7 @@
 struct audio_device {
     struct audio_hw_device device;
     pthread_mutex_t lock; /* see note below on mutex acquisition order */
+    pthread_mutex_t cal_lock;
     struct mixer *mixer;
     audio_mode_t mode;
     audio_devices_t out_device;