hal: add audio device arbitration extension

The audio device arbitration extension enables audio HAL to manage
shared resources by notifying clients when devices are required
by audio HAL and when they can be used by the clients.

This change is implemented as an audio extension. It dynamically
loads a library which implements the arbitration and IPC
mechanism

Change-Id: I13ade59caecfd69639f5bf8892dba917502ae357
diff --git a/hal/Android.mk b/hal/Android.mk
index d6f07fa..1046b55 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -141,6 +141,11 @@
     LOCAL_CFLAGS += -DQTI_FLAC_DECODER
 endif
 
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_DEV_ARBI)),true)
+    LOCAL_CFLAGS += -DDEV_ARBI_ENABLED
+    LOCAL_SRC_FILES += audio_extn/dev_arbi.c
+endif
+
 LOCAL_SHARED_LIBRARIES := \
 	liblog \
 	libcutils \
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 7ce1c3a..f68d3a8 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -260,6 +260,18 @@
 audio_usecase_t audio_extn_hfp_get_usecase();
 #endif
 
+#ifndef DEV_ARBI_ENABLED
+#define audio_extn_dev_arbi_init()                  (0)
+#define audio_extn_dev_arbi_deinit()                (0)
+#define audio_extn_dev_arbi_acquire(snd_device)     (0)
+#define audio_extn_dev_arbi_release(snd_device)     (0)
+#else
+int audio_extn_dev_arbi_init();
+int audio_extn_dev_arbi_deinit();
+int audio_extn_dev_arbi_acquire(snd_device_t snd_device);
+int audio_extn_dev_arbi_release(snd_device_t snd_device);
+#endif
+
 void audio_extn_utils_update_streams_output_cfg_list(void *platform,
                                   struct mixer *mixer,
                                   struct listnode *streams_output_cfg_list);
diff --git a/hal/audio_extn/dev_arbi.c b/hal/audio_extn/dev_arbi.c
new file mode 100644
index 0000000..d3c01c5
--- /dev/null
+++ b/hal/audio_extn/dev_arbi.c
@@ -0,0 +1,169 @@
+/*
+ * Copyright (c) 2014, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define LOG_TAG "audio_hw_dev_arbi"
+/*#define LOG_NDEBUG 0*/
+#define LOG_NDDEBUG 0
+
+#include <errno.h>
+#include <cutils/log.h>
+#include <fcntl.h>
+#include "audio_hw.h"
+#include "platform.h"
+#include "platform_api.h"
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <cutils/properties.h>
+#include "audio_extn.h"
+
+#ifdef DEV_ARBI_ENABLED
+
+typedef int (init_fn_t)();
+typedef int (deinit_fn_t)();
+typedef int (acquire_fn_t)(audio_devices_t aud_dev);
+typedef int (release_fn_t)(audio_devices_t aud_dev);
+
+typedef struct {
+    snd_device_t snd_device;
+    audio_devices_t aud_device;
+} snd_aud_dev_mapping_t;
+
+static void* lib_handle = NULL;
+
+static init_fn_t *init_fp = NULL;
+static deinit_fn_t *deinit_fp = NULL;
+static acquire_fn_t *acquire_fp = NULL;
+static release_fn_t *release_fp = NULL;
+
+static int load_dev_arbi_lib()
+{
+    int rc = -EINVAL;
+
+    if (lib_handle != NULL) {
+        ALOGE("%s: library already loaded", __func__);
+        return rc;
+    }
+
+    lib_handle = dlopen("libaudiodevarb.so", RTLD_NOW);
+    if (lib_handle != NULL) {
+        init_fp = (init_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_init");
+        deinit_fp = (deinit_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_deinit");
+        acquire_fp = (acquire_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_acquire");
+        release_fp = (release_fn_t*)dlsym(lib_handle, "aud_dev_arbi_server_release");
+
+        if ((init_fp == NULL) ||
+            (deinit_fp == NULL) ||
+            (acquire_fp == NULL) ||
+            (release_fp == NULL)) {
+
+            ALOGE("%s: error loading symbols from library", __func__);
+
+            init_fp = NULL;
+            deinit_fp = NULL;
+            acquire_fp = NULL;
+            release_fp = NULL;
+        } else
+            return 0;
+    }
+
+    return rc;
+}
+
+int audio_extn_dev_arbi_init()
+{
+    int rc = load_dev_arbi_lib();
+    if (!rc)
+        rc = init_fp();
+
+    return rc;
+}
+
+int audio_extn_dev_arbi_deinit()
+{
+    int rc = -EINVAL;
+
+    if(deinit_fp != NULL) {
+        rc = deinit_fp();
+
+        init_fp = NULL;
+        deinit_fp = NULL;
+        acquire_fp = NULL;
+        release_fp = NULL;
+
+        dlclose(lib_handle);
+        lib_handle = NULL;
+    }
+
+    return rc;
+}
+
+static audio_devices_t get_audio_device(snd_device_t snd_device)
+{
+    static snd_aud_dev_mapping_t snd_aud_dev_map[] = {
+        {SND_DEVICE_OUT_HANDSET, AUDIO_DEVICE_OUT_EARPIECE},
+        {SND_DEVICE_OUT_VOICE_HANDSET, AUDIO_DEVICE_OUT_EARPIECE}
+    };
+
+    audio_devices_t aud_device = AUDIO_DEVICE_NONE;
+    uint32_t ind = 0;
+
+    for (ind = 0; ind < ARRAY_SIZE(snd_aud_dev_map); ++ind) {
+        if (snd_device == snd_aud_dev_map[ind].snd_device) {
+            aud_device = snd_aud_dev_map[ind].aud_device;
+            break;
+        }
+    }
+
+    return aud_device;
+}
+
+int audio_extn_dev_arbi_acquire(snd_device_t snd_device)
+{
+    int rc = -EINVAL;
+    audio_devices_t audio_device = get_audio_device(snd_device);
+
+    if ((acquire_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
+        rc = acquire_fp(audio_device);
+
+    return rc;
+}
+
+int audio_extn_dev_arbi_release(snd_device_t snd_device)
+{
+    int rc = -EINVAL;
+    audio_devices_t audio_device = get_audio_device(snd_device);
+
+    if ((release_fp != NULL) && (audio_device != AUDIO_DEVICE_NONE))
+        rc = release_fp(audio_device);
+
+    return rc;
+}
+
+#endif /*DEV_ARBI_ENABLED*/
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 87ea0d3..3a3d709 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -379,6 +379,7 @@
                                         LISTEN_EVENT_SND_DEVICE_FREE);
             return -EINVAL;
         }
+        audio_extn_dev_arbi_acquire(snd_device);
         audio_route_apply_and_update_path(adev->audio_route, device_name);
     }
     return 0;
@@ -422,9 +423,10 @@
             snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
             audio_extn_spkr_prot_is_enabled()) {
             audio_extn_spkr_prot_stop_processing();
-        } else
+        } else {
             audio_route_reset_and_update_path(adev->audio_route, device_name);
-
+            audio_extn_dev_arbi_release(snd_device);
+        }
         audio_extn_listen_update_device_status(snd_device,
                                         LISTEN_EVENT_SND_DEVICE_FREE);
     }
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 3f62164..78c364a 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -843,6 +843,9 @@
 
     audio_extn_dolby_set_license(adev);
 
+    /* init audio device arbitration */
+    audio_extn_dev_arbi_init();
+
     return my_data;
 }
 
@@ -853,6 +856,9 @@
     hw_info_deinit(my_data->hw_info);
     close_csd_client(my_data->csd);
 
+    /* deinit audio device arbitration */
+    audio_extn_dev_arbi_deinit();
+
     free(platform);
     /* deinit usb */
     audio_extn_usb_deinit();