audio: porting MAXXAUDIO

  - use effect module to monitor stream's volume
  - the control(maxxaudio) monitors active device and
    changed volume from HAL, and send audio calibration
    to acdb via acdb_loader.
  - support specific USB headset

  Enabled function:
  - LR channels swap
  - load external tuning table for volume preset feature

Bug: 74360112
Test: manual effect test, usb headset test
Change-Id: I9b35436d6abebcfa250beb4857dfa0be9248f2b5
diff --git a/hal/Android.mk b/hal/Android.mk
index d249115..9b1f4e6 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -183,10 +183,16 @@
     LOCAL_SRC_FILES += audio_extn/sndmonitor.c
 endif
 
+
 ifeq ($(strip $(AUDIO_FEATURE_ENABLED_USB_SERVICE_INTERVAL)), true)
     LOCAL_CFLAGS += -DUSB_SERVICE_INTERVAL_ENABLED
 endif
 
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_MAXX_AUDIO)), true)
+    LOCAL_CFLAGS += -DMAXXAUDIO_QDSP_ENABLED
+    LOCAL_SRC_FILES += audio_extn/maxxaudio.c
+endif
+
 LOCAL_SHARED_LIBRARIES += libbase libhidlbase libhwbinder libutils android.hardware.power@1.2 liblog
 
 LOCAL_SRC_FILES += audio_perf.cpp
diff --git a/hal/audio_extn/maxxaudio.c b/hal/audio_extn/maxxaudio.c
new file mode 100644
index 0000000..73bd641
--- /dev/null
+++ b/hal/audio_extn/maxxaudio.c
@@ -0,0 +1,627 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "audio_hw_waves"
+/*#define LOG_NDEBUG 0*/
+
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <cutils/str_parms.h>
+#include <log/log.h>
+#include <audio_hw.h>
+#include <system/audio.h>
+#include <platform_api.h>
+#include <string.h>
+#include <math.h>
+#include "audio_extn.h"
+#include "maxxaudio.h"
+
+#define LIB_MA_PARAM "libmaqdspparams.so"
+#define LIB_MA_PATH "vendor/lib/"
+#define PRESET_PATH "/vendor/etc/default.mps"
+#define USER_PRESET_PATH ""
+#define CONFIG_PATH "/vendor/etc/maxx_conf.ini"
+#define CAL_PRESIST_STR "cal_persist"
+#define CAL_SAMPLERATE_STR "cal_samplerate"
+
+#define MA_QDSP_PARAM_INIT "maxxaudio_qdsp_parameters_initialize"
+#define MA_QDSP_PARAM_DEINIT "maxxaudio_qdsp_parameters_uninitialize"
+#define MA_QDSP_SET_LR_SWAP "maxxaudio_qdsp_set_lr_swap"
+#define MA_QDSP_SET_MODE "maxxaudio_qdsp_set_sound_mode"
+#define MA_QDSP_SET_VOL "maxxaudio_qdsp_set_volume"
+#define MA_QDSP_SET_VOLT "maxxaudio_qdsp_set_volume_table"
+
+#define SUPPORT_DEV "Blackbird"
+#define SUPPORTED_USB  0x01
+
+struct ma_audio_cal_settings {
+    void *platform;
+    int app_type;
+    audio_devices_t device;
+};
+
+struct ma_state {
+    float vol;
+    bool active;
+};
+
+typedef enum MA_STREAM_TYPE {
+    STREAM_MIN_STREAM_TYPES,
+    STREAM_VOICE = STREAM_MIN_STREAM_TYPES,
+    STREAM_SYSTEM,
+    STREAM_RING,
+    STREAM_MUSIC,
+    STREAM_ALARM,
+    STREAM_NOTIFICATION ,
+    STREAM_MAX_TYPES,
+} ma_stream_type_t;
+
+typedef enum MA_CMD {
+    MA_CMD_VOL,
+    MA_CMD_SWAP_ENABLE,
+    MA_CMD_SWAP_DISABLE,
+} ma_cmd_t;
+
+typedef void *ma_audio_cal_handle_t;
+typedef int (*set_audio_cal_t)(const struct ma_audio_cal_settings *, const char *);
+
+typedef bool (*ma_param_init_t)(
+                ma_audio_cal_handle_t *,
+                void *, const char *, const char *,
+                const char *, set_audio_cal_t);
+
+typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t);
+
+typedef bool (*ma_set_lr_swap_t)(
+                ma_audio_cal_handle_t,
+                const struct ma_audio_cal_settings *, bool);
+
+typedef bool (*ma_set_sound_mode_t)(
+                ma_audio_cal_handle_t,
+                const struct ma_audio_cal_settings *, unsigned int);
+
+typedef bool (*ma_set_volume_t)(
+                ma_audio_cal_handle_t,
+                const struct ma_audio_cal_settings *, double);
+
+typedef bool (*ma_set_volume_table_t)(
+                ma_audio_cal_handle_t,
+                const struct ma_audio_cal_settings *,
+                size_t, struct ma_state *);
+
+struct ma_platform_data {
+    void *waves_handle;
+    pthread_mutex_t lock;
+    ma_param_init_t          ma_param_init;
+    ma_param_deinit_t        ma_param_deinit;
+    ma_set_lr_swap_t         ma_set_lr_swap;
+    ma_set_sound_mode_t      ma_set_sound_mode;
+    ma_set_volume_t          ma_set_volume;
+    ma_set_volume_table_t    ma_set_volume_table;
+};
+
+ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
+static uint16_t g_supported_dev = 0;
+static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
+static struct ma_platform_data *my_data = NULL;
+
+static int set_audio_cal(
+        const struct ma_audio_cal_settings *audio_cal_settings,
+        const char *audio_cal)
+
+{
+    ALOGV("set_audio_cal: %s", audio_cal);
+
+    return platform_set_parameters(audio_cal_settings->platform,
+                                   str_parms_create_str(audio_cal));
+}
+
+static bool ma_set_lr_swap_l(
+        const struct ma_audio_cal_settings *audio_cal_settings,
+        bool swap)
+{
+    return my_data->ma_set_lr_swap(g_ma_audio_cal_handle, audio_cal_settings, swap);
+}
+
+static bool ma_set_sound_mode_l(
+        const struct ma_audio_cal_settings *audio_cal_settings,
+        int sound_mode)
+{
+    return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
+                                        audio_cal_settings, sound_mode);
+}
+
+static bool ma_set_volume_l(
+        const struct ma_audio_cal_settings *audio_cal_settings,
+        double volume)
+{
+    return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings, volume);
+}
+
+static bool ma_set_volume_table_l(
+        const struct ma_audio_cal_settings *audio_cal_settings,
+        size_t num_streams, struct ma_state *volume_table)
+{
+    return my_data->ma_set_volume_table(
+                     g_ma_audio_cal_handle,
+                     audio_cal_settings,
+                     num_streams,
+                     volume_table);
+
+}
+
+static inline bool valid_usecase(struct audio_usecase *usecase)
+{
+    if ((usecase->type == PCM_PLAYBACK) &&
+        /* supported usecases */
+        ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
+         (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
+         (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
+        /* support devices */
+        ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
+         (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
+         (usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP) ||
+         (usecase->devices & AUDIO_DEVICE_OUT_ALL_USB)))
+
+        return true;
+
+    ALOGV("%s: not support type %d usecase %d device %d",
+           __func__, usecase->type, usecase->id, usecase->devices);
+
+    return false;
+}
+
+// already hold lock
+static inline bool is_active()
+{
+    ma_stream_type_t i = 0;
+
+    for (i = 0; i < STREAM_MAX_TYPES; i++)
+        if (ma_cur_state_table[i].active &&
+                (ma_cur_state_table[i].vol != 0))
+            return true;
+
+    return false;
+}
+
+static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
+{
+    int i = 0;
+    bool ret = false;
+    float vol = 0;
+    struct listnode *node;
+    struct audio_usecase *usecase;
+    struct ma_audio_cal_settings *ma_cal = NULL;
+
+    // alloct
+    ma_cal = (struct ma_audio_cal_settings *)malloc(sizeof(struct ma_audio_cal_settings));
+
+    if (ma_cal == NULL) {
+        ALOGE("%s: ma_cal alloct fail", __func__);
+        return ret;
+    }
+
+    list_for_each(node, &adev->usecase_list) {
+        usecase = node_to_item(node, struct audio_usecase, list);
+        if (valid_usecase(usecase)) {
+            ma_cal->platform = adev->platform;
+            ma_cal->app_type = usecase->stream.out->app_type_cfg.app_type;
+            ma_cal->device = usecase->stream.out->devices;
+            ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
+                      __func__, usecase->id, ma_cal->app_type, ma_cal->device);
+
+            switch (cmd) {
+                case MA_CMD_VOL:
+                    ret = ma_set_volume_table_l(ma_cal, STREAM_MAX_TYPES,
+                                                  ma_cur_state_table);
+                    if (ret)
+                        ALOGV("Waves: ma_set_volume_table_l success");
+                    else
+                        ALOGE("Waves: ma_set_volume_table_l %f returned with error.", vol);
+
+                    ALOGV("%s: send volume table === Start", __func__);
+                    for (i = 0; i < STREAM_MAX_TYPES; i++)
+                        ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
+                            ma_cur_state_table[i].vol,
+                            ma_cur_state_table[i].active ? "T" : "F");
+                    ALOGV("%s: send volume table === End", __func__);
+                    break;
+                case MA_CMD_SWAP_ENABLE:
+                    ret = ma_set_lr_swap_l(ma_cal, true);
+                    if (ret)
+                        ALOGV("Waves: ma_set_lr_swap_l enable returned with success.");
+                    else
+                        ALOGE("Waves: ma_set_lr_swap_l enable returned with error.");
+                    break;
+                case MA_CMD_SWAP_DISABLE:
+                    ret = ma_set_lr_swap_l(ma_cal, false);
+                    if (ret)
+                        ALOGV("Waves: ma_set_lr_swap_l disable returned with success.");
+                    else
+                        ALOGE("Waves: ma_set_lr_swap_l disable returned with error.");
+                    break;
+                default:
+                    ALOGE("%s: unsupported cmd %d", __func__, cmd);
+            }
+
+        }
+    }
+    free(ma_cal);
+
+    return ret;
+}
+
+static bool find_sup_dev(char *name)
+{
+    char *token;
+    const char s[2] = ",";
+    bool ret = false;
+    char sup_devs[128];
+
+    // the rule of comforming suppored dev's name
+    // 1. Both string len are equal
+    // 2. Both string content are equal
+
+    strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
+    token = strtok(sup_devs, s);
+    while (token != NULL) {
+        if (strncmp(token, name, strlen(token)) == 0 &&
+            strlen(token) == strlen(name)) {
+            ALOGD("%s: support dev %s", __func__, token);
+            ret = true;
+            break;
+        }
+        token = strtok(NULL, s);
+    }
+
+    return ret;
+}
+
+static void ma_set_swap_l(struct audio_device *adev, bool enable)
+{
+    if (!my_data) {
+        ALOGE("%s: maxxaudio isn't initialized.", __func__);
+        return;
+    }
+    if (enable)
+        check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
+    else
+        check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
+}
+
+static void ma_support_usb(bool enable, int card)
+{
+    char path[128];
+    char id[32];
+    int ret = 0;
+    int32_t fd = -1;
+    char *idd;
+
+    if (enable) {
+        ret = snprintf(path, sizeof(path), "/proc/asound/card%u/id", card);
+        if (ret < 0) {
+            ALOGE("%s: failed on snprintf (%d) to path %s\n",
+              __func__, ret, path);
+            goto done;
+        }
+        fd = open(path, O_RDONLY);
+        if (fd < 0) {
+            ALOGE("%s: error failed to open id file %s error: %d\n",
+                  __func__, path, errno);
+            goto done;
+        }
+        if (read(fd, id, sizeof(id)) < 0) {
+            ALOGE("%s: file read error", __func__);
+            goto done;
+        }
+        //replace '\n' to '\0'
+        idd = strtok(id, "\n");
+
+        if (find_sup_dev(idd)) {
+            ALOGV("%s: support device name is %s", __func__, id);
+            g_supported_dev |= SUPPORTED_USB;
+        } else
+            ALOGV("%s: device %s isn't found from %s", __func__, id, SUPPORT_DEV);
+    } else {
+        g_supported_dev &= ~SUPPORTED_USB;
+    }
+
+done:
+    if (fd >= 0) close(fd);
+}
+
+// adev_init lock held
+void audio_extn_ma_init(void *platform)
+{
+    ma_stream_type_t i = 0;
+    int ret = 0;
+    char lib_path[256];
+
+    if (platform == NULL) {
+        ALOGE("%s: platform is NULL", __func__);
+        goto error;
+    }
+
+    if (my_data) { free(my_data); }
+    my_data = calloc(1, sizeof(struct ma_platform_data));
+    if (my_data == NULL) {
+        ALOGE("%s: ma_cal alloct fail", __func__);
+        goto error;
+    }
+
+    pthread_mutex_init(&my_data->lock, NULL);
+
+    ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
+    if (ret < 0) {
+        ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
+        goto error;
+    }
+
+    my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
+    if (my_data->waves_handle == NULL) {
+         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
+         goto error;
+    } else {
+         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
+
+         my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
+                                     MA_QDSP_PARAM_INIT);
+         if (!my_data->ma_param_init) {
+             ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
+             goto error;
+         }
+
+         my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(my_data->waves_handle,
+                                       MA_QDSP_PARAM_DEINIT);
+         if (!my_data->ma_param_deinit) {
+             ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
+             goto error;
+         }
+
+         my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
+                                      MA_QDSP_SET_LR_SWAP);
+         if (!my_data->ma_set_lr_swap) {
+             ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
+             goto error;
+         }
+
+         my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(my_data->waves_handle,
+                                         MA_QDSP_SET_MODE);
+         if (!my_data->ma_set_sound_mode) {
+             ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
+             goto error;
+         }
+
+         my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
+                                     MA_QDSP_SET_VOL);
+         if (!my_data->ma_set_volume) {
+             ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
+             goto error;
+         }
+
+         my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(my_data->waves_handle,
+                                           MA_QDSP_SET_VOLT);
+         if (!my_data->ma_set_volume_table) {
+             ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
+             goto error;
+         }
+    }
+
+    /* check file */
+    if (access(PRESET_PATH, F_OK) < 0) {
+        ALOGW("%s: file %s isn't existed.", __func__, PRESET_PATH);
+        goto error;
+    }
+    /* TODO: check user preset table once the feature is enabled
+    if (access(USER_PRESET_PATH, F_OK) < 0 ){
+        ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
+        goto error;
+    }
+    */
+    if (access(CONFIG_PATH, F_OK) < 0) {
+        ALOGW("%s: file %s isn't existed.", __func__, CONFIG_PATH);
+        goto error;
+    }
+
+    /* init ma parameter */
+    if (my_data->ma_param_init(&g_ma_audio_cal_handle,
+                                  platform, /* TODO: remove this on next version*/
+                                  PRESET_PATH,
+                                  USER_PRESET_PATH, /* useless */
+                                  CONFIG_PATH,
+                                  &set_audio_cal)) {
+        if (!g_ma_audio_cal_handle) {
+            ALOGE("%s: ma parameters initialize failed", __func__);
+            my_data->ma_param_deinit(&g_ma_audio_cal_handle);
+            goto error;
+        }
+        ALOGD("%s: ma parameters initialize successful", __func__);
+    } else {
+        ALOGE("%s: ma parameters initialize failed", __func__);
+        goto error;
+    }
+
+    /* init volume table */
+    for (i = 0; i < STREAM_MAX_TYPES; i++) {
+        ma_cur_state_table[i].vol = 0.0;
+        ma_cur_state_table[i].active = false;
+    }
+
+    return;
+
+error:
+    if (my_data) { free(my_data); }
+    my_data = NULL;
+}
+
+//adev_init lock held
+void audio_extn_ma_deinit()
+{
+    if (my_data) {
+        /* deinit ma parameter */
+        if (my_data->ma_param_deinit &&
+            my_data->ma_param_deinit(&g_ma_audio_cal_handle))
+            ALOGD("%s: ma parameters uninitialize successful", __func__);
+        else
+            ALOGD("%s: ma parameters uninitialize failed", __func__);
+
+        pthread_mutex_destroy(&my_data->lock);
+        free(my_data);
+        my_data = NULL;
+    }
+}
+
+// adev_init and adev lock held
+bool audio_extn_ma_set_state(
+        struct audio_device *adev, int stream_type, float vol, bool active)
+{
+    bool ret = false;
+    ma_stream_type_t stype = (ma_stream_type_t)stream_type;
+
+    ALOGV("%s: stream[%d] vol[%f] active[%s]",
+          __func__, stream_type, vol, active ? "true" : "false");
+
+    if (!my_data) {
+        ALOGE("%s: maxxaudio isn't initialized.", __func__);
+        return ret;
+    }
+
+    // update condition
+    // 1. start track: active and volume isn't zero
+    // 2. stop track: no tracks are active
+    if ((active && vol != 0) ||
+         (!active)) {
+        pthread_mutex_lock(&my_data->lock);
+
+        ma_cur_state_table[stype].vol = vol;
+        ma_cur_state_table[stype].active = active;
+        if (is_active())
+            ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
+
+        pthread_mutex_unlock(&my_data->lock);
+    }
+
+    return ret;
+}
+
+void audio_extn_ma_set_device(struct audio_device *adev, struct audio_usecase *usecase)
+{
+    int i = 0;
+    int u_index = -1;
+    float vol = 0;
+    struct ma_audio_cal_settings *ma_cal = NULL;
+
+    if (!my_data) {
+        ALOGV("%s: maxxaudio isn't initialized.", __func__);
+        return;
+    }
+
+    if (!valid_usecase(usecase)) {
+        ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
+        return;
+    }
+
+    ma_cal = (struct ma_audio_cal_settings *)malloc(sizeof(struct ma_audio_cal_settings));
+
+    /* update audio_cal and send it */
+    if (ma_cal != NULL){
+        ma_cal->platform = adev->platform;
+        ma_cal->app_type = usecase->stream.out->app_type_cfg.app_type;
+        ma_cal->device = usecase->stream.out->devices;
+        ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
+                      __func__, usecase->id, ma_cal->app_type, ma_cal->device);
+
+        pthread_mutex_lock(&my_data->lock);
+
+        if (is_active()) {
+            ALOGV("%s: send volume table === Start", __func__);
+            for (i = 0; i < STREAM_MAX_TYPES; i++)
+                ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
+                    ma_cur_state_table[i].vol,
+                    ma_cur_state_table[i].active ? "T" : "F");
+            ALOGV("%s: send volume table === End", __func__);
+
+            if (!ma_set_volume_table_l(ma_cal,
+                                         STREAM_MAX_TYPES,
+                                         ma_cur_state_table))
+                ALOGE("Waves: ma_set_volume_table_l %f returned with error.", vol);
+            else
+                ALOGV("Waves: ma_set_volume_table_l success");
+
+        }
+        pthread_mutex_unlock(&my_data->lock);
+        free(ma_cal);
+    } else {
+        ALOGE("%s: ma_cal alloct fail", __func__);
+    }
+}
+
+void audio_extn_ma_set_parameters(struct audio_device *adev, struct str_parms *parms)
+{
+    int ret;
+    bool ret_b;
+    int val;
+    char value[128];
+
+    // do LR swap and usb recognition
+    ret = str_parms_get_int(parms, "rotation", &val);
+    if (ret >= 0) {
+        switch (val) {
+        case 270:
+            ma_set_swap_l(adev, true);
+            break;
+        case 0:
+        case 90:
+        case 180:
+            ma_set_swap_l(adev, false);
+            break;
+        }
+    }
+
+    // check connect status
+    ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, sizeof(value));
+    if (ret >= 0) {
+        audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
+        if (audio_is_usb_out_device(device)) {
+            ret = str_parms_get_str(parms, "card", value, sizeof(value));
+            if (ret >= 0) {
+                const int card = atoi(value);
+                ma_support_usb(true, card);
+            }
+        }
+    }
+
+    // check disconnect status
+    ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, sizeof(value));
+    if (ret >= 0) {
+        audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
+        if (audio_is_usb_out_device(device)) {
+            ret = str_parms_get_str(parms, "card", value, sizeof(value));
+            if (ret >= 0) {
+                const int card = atoi(value);
+                ma_support_usb(false, card /*useless*/);
+            }
+        }
+    }
+}
+
+bool audio_extn_ma_supported_usb()
+{
+    ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
+    return (g_supported_dev & SUPPORTED_USB) ? true : false;
+}
diff --git a/hal/audio_extn/maxxaudio.h b/hal/audio_extn/maxxaudio.h
new file mode 100644
index 0000000..a2ebaed
--- /dev/null
+++ b/hal/audio_extn/maxxaudio.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MAXXAUDIO_H_
+#define MAXXAUDIO_H_
+
+#ifndef MAXXAUDIO_QDSP_ENABLED
+#define audio_extn_ma_init(platform)                                      (0)
+#define audio_extn_ma_deinit()                                            (0)
+#define audio_extn_ma_set_state(adev, type, vol, active)                  (false)
+#define audio_extn_ma_set_device(adev, usecase)                           (0)
+#define audio_extn_ma_set_parameters(adev, param)                         (0)
+#define audio_extn_ma_supported_usb()                                     (false)
+#else
+void audio_extn_ma_init(void *platform);
+void audio_extn_ma_deinit();
+bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type, float vol, bool active);
+void audio_extn_ma_set_device(struct audio_device *adev, struct audio_usecase *usecase);
+void audio_extn_ma_set_parameters(struct audio_device *adev, struct str_parms *parms);
+bool audio_extn_ma_supported_usb();
+#endif /* MAXXAUDIO_QDSP_ENABLED */
+
+#endif /* MAXXAUDIO_H_ */
+
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index cdd10b3..34cb938 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -58,6 +58,7 @@
 
 #include "sound/compress_params.h"
 #include "audio_extn/tfa_98xx.h"
+#include "audio_extn/maxxaudio.h"
 
 /* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB.
  * COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */
@@ -481,6 +482,29 @@
     return ret_val;
 }
 
+#ifdef MAXXAUDIO_QDSP_ENABLED
+bool audio_hw_send_ma_parameter(int stream_type, float vol, bool active)
+{
+    bool ret = false;
+    ALOGV("%s: enter ...", __func__);
+
+    pthread_mutex_lock(&adev_init_lock);
+
+    if (adev != NULL && adev->platform != NULL) {
+        pthread_mutex_lock(&adev->lock);
+        ret = audio_extn_ma_set_state(adev, stream_type, vol, active);
+        pthread_mutex_unlock(&adev->lock);
+    }
+
+    pthread_mutex_unlock(&adev_init_lock);
+
+    ALOGV("%s: exit with ret %d", __func__, ret);
+    return ret;
+}
+#else
+#define audio_hw_send_ma_parameter(stream_type, vol, active) (0)
+#endif
+
 __attribute__ ((visibility ("default")))
 int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
                                     int table_size) {
@@ -1437,6 +1461,8 @@
 
     enable_audio_route(adev, usecase);
 
+    audio_extn_ma_set_device(adev, usecase);
+
     /* Applicable only on the targets that has external modem.
      * Enable device command should be sent to modem only after
      * enabling voice call mixer controls
@@ -4645,6 +4671,7 @@
             adev->screen_off = true;
     }
 
+#ifndef MAXXAUDIO_QDSP_ENABLED
     ret = str_parms_get_int(parms, "rotation", &val);
     if (ret >= 0) {
         bool reverse_speakers = false;
@@ -4670,6 +4697,7 @@
             platform_check_and_set_swap_lr_channels(adev, reverse_speakers);
         }
     }
+#endif
 
     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
     if (ret >= 0) {
@@ -4701,7 +4729,6 @@
             ret = str_parms_get_str(parms, "card", value, sizeof(value));
             if (ret >= 0) {
                 const int card = atoi(value);
-
                 audio_extn_usb_remove_device(device, card);
             }
         } else if (audio_is_usb_in_device(device)) {
@@ -4715,6 +4742,8 @@
 
     audio_extn_hfp_set_parameters(adev, parms);
     audio_extn_a2dp_set_parameters(parms);
+    audio_extn_ma_set_parameters(adev, parms);
+
     // reconfigure should be done only after updating A2DP state in audio extension
     ret = str_parms_get_str(parms,"reconfigA2dp", value, sizeof(value));
     if (ret >= 0) {
@@ -5338,6 +5367,7 @@
     if ((--audio_device_ref_count) == 0) {
         audio_extn_snd_mon_unregister_listener(adev);
         audio_extn_tfa_98xx_deinit();
+        audio_extn_ma_deinit();
         audio_route_free(adev->audio_route);
         free(adev->snd_dev_ref_cnt);
         platform_deinit(adev->platform);
@@ -5645,6 +5675,7 @@
     }
 
     audio_extn_tfa_98xx_init(adev);
+    audio_extn_ma_init(adev->platform);
 
     pthread_mutex_unlock(&adev_init_lock);
 
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index b8755b8..fc9d322 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -34,6 +34,7 @@
 #include <sound/devdep_params.h>
 #endif
 
+#include "maxxaudio.h"
 #include <resolv.h>
 
 #define MIXER_XML_DEFAULT_PATH "mixer_paths.xml"
@@ -266,6 +267,7 @@
     [SND_DEVICE_OUT_USB_HEADSET] = "usb-headset",
     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = "usb-headset",
     [SND_DEVICE_OUT_USB_HEADPHONES] = "usb-headphones",
+    [SND_DEVICE_OUT_USB_HEADSET_SPEC] = "usb-headset",
     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = "usb-headphones",
     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = "speaker-and-usb-headphones",
     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = "speaker-safe-and-usb-headphones",
@@ -382,6 +384,7 @@
     [SND_DEVICE_OUT_USB_HEADSET] = 45,
     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = 45,
     [SND_DEVICE_OUT_USB_HEADPHONES] = 45,
+    [SND_DEVICE_OUT_USB_HEADSET_SPEC] = 45,
     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = 45,
     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 14,
     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = 14,
@@ -511,6 +514,7 @@
     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET)},
     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_PROTECTED)},
     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED)},
+    {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET_SPEC)},
 
     /* in */
     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC)},
@@ -1200,6 +1204,7 @@
     backend_tag_table[SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = strdup("usb-headset-mic");
     backend_tag_table[SND_DEVICE_OUT_BT_A2DP] = strdup("bt-a2dp");
     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = strdup("speaker-and-bt-a2dp");
+    backend_tag_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("usb-headset");
 
     hw_interface_table[SND_DEVICE_OUT_HANDSET] = strdup("SLIMBUS_0_RX");
     hw_interface_table[SND_DEVICE_OUT_SPEAKER] = strdup("SLIMBUS_0_RX");
@@ -1233,6 +1238,7 @@
     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("USB_AUDIO_RX");
     hw_interface_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
+    hw_interface_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("USB_AUDIO_RX");
     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
     hw_interface_table[SND_DEVICE_OUT_VOICE_TX] = strdup("AFE_PCM_RX");
@@ -2718,7 +2724,9 @@
     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
         snd_device = SND_DEVICE_OUT_HDMI ;
     } else if (audio_is_usb_out_device(devices)) {
-        if (audio_extn_usb_is_capture_supported())
+        if (audio_extn_ma_supported_usb())
+            snd_device = SND_DEVICE_OUT_USB_HEADSET_SPEC;
+        else if (audio_extn_usb_is_capture_supported())
             snd_device = SND_DEVICE_OUT_USB_HEADSET;
         else
             snd_device = SND_DEVICE_OUT_USB_HEADPHONES;
diff --git a/hal/msm8974/platform.h b/hal/msm8974/platform.h
index 8f16802..f512de3 100644
--- a/hal/msm8974/platform.h
+++ b/hal/msm8974/platform.h
@@ -99,6 +99,8 @@
     SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET,
     SND_DEVICE_OUT_VOICE_USB_HEADPHONES,
     SND_DEVICE_OUT_VOICE_USB_HEADSET,
+    /* Specific snd_devices */
+    SND_DEVICE_OUT_USB_HEADSET_SPEC,
     SND_DEVICE_OUT_END,
 
     /*
diff --git a/post_proc/Android.mk b/post_proc/Android.mk
index c31b769..dc0e260 100644
--- a/post_proc/Android.mk
+++ b/post_proc/Android.mk
@@ -73,3 +73,36 @@
 include $(BUILD_SHARED_LIBRARY)
 
 endif
+
+################################################################################
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_MAXX_AUDIO)), true)
+
+include $(CLEAR_VARS)
+
+LOCAL_CFLAGS := -D HAL_LIB_NAME=\"audio.primary."$(TARGET_BOARD_PLATFORM)".so\"
+
+LOCAL_SRC_FILES:= \
+	ma_listener.c
+
+LOCAL_CFLAGS += $(qcom_post_proc_common_cflags)
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+	liblog \
+	libdl
+
+LOCAL_MODULE_RELATIVE_PATH := soundfx
+LOCAL_MODULE:= libmalistener
+LOCAL_MODULE_OWNER := google
+LOCAL_PROPRIETARY_MODULE := true
+
+LOCAL_C_INCLUDES := \
+	hardware/qcom/audio/hal \
+	system/media/audio/include/system \
+	$(call include-path-for, audio-effects)
+
+LOCAL_HEADER_LIBRARIES += libhardware_headers
+LOCAL_HEADER_LIBRARIES += libsystem_headers
+include $(BUILD_SHARED_LIBRARY)
+
+endif
diff --git a/post_proc/ma_listener.c b/post_proc/ma_listener.c
new file mode 100644
index 0000000..8e57db1
--- /dev/null
+++ b/post_proc/ma_listener.c
@@ -0,0 +1,627 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "ma_listener"
+/*#define LOG_NDEBUG 0*/
+
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <math.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <cutils/list.h>
+#include <cutils/log.h>
+#include <hardware/audio_effect.h>
+#include <audio-base.h>
+
+#define MA_FLAG ( EFFECT_FLAG_TYPE_INSERT | \
+                   EFFECT_FLAG_VOLUME_IND | \
+                   EFFECT_FLAG_DEVICE_IND | \
+                   EFFECT_FLAG_OFFLOAD_SUPPORTED | \
+                   EFFECT_FLAG_NO_PROCESS)
+
+#define PRINT_STREAM_TYPE(i) ALOGV("descriptor found and is of stream type %s ",\
+                                                            i == AUDIO_STREAM_MUSIC ? "MUSIC": \
+                                                            i == AUDIO_STREAM_RING ? "RING": \
+                                                            i == AUDIO_STREAM_ALARM ? "ALARM": \
+                                                            i == AUDIO_STREAM_VOICE_CALL ? "Voice_call": \
+                                                            i == AUDIO_STREAM_SYSTEM ? "SYSTEM":       \
+                                                            i == AUDIO_STREAM_NOTIFICATION ? "Notification":\
+                                                            "--INVALID--"); \
+
+#define MA_SET_STATE "audio_hw_send_ma_parameter"
+#define HAL_VENDOR_PATH "/vendor/lib/hw"
+
+enum {
+    MA_LISTENER_STATE_UNINITIALIZED,
+    MA_LISTENER_STATE_INITIALIZED,
+    MA_LISTENER_STATE_ACTIVE,
+};
+
+typedef struct ma_listener_context_s ma_listener_context_t;
+static const struct effect_interface_s effect_interface;
+
+struct ma_state {
+    float vol;
+    bool active;
+};
+
+static const audio_stream_type_t MIN_STREAM_TYPES = AUDIO_STREAM_VOICE_CALL;
+static const audio_stream_type_t MAX_STREAM_TYPES = AUDIO_STREAM_NOTIFICATION;
+static struct ma_state g_cur_state[MAX_STREAM_TYPES];
+
+struct ma_listener_context_s {
+    const struct effect_interface_s *itfe;
+    struct listnode effect_list_node;
+    effect_config_t config;
+    const effect_descriptor_t *desc;
+    uint32_t stream_type;
+    uint32_t session_id;
+    uint32_t state;
+    uint32_t dev_id;
+    float left_vol;
+    float right_vol;
+};
+
+/* voice UUID: 4ece09c2-3728-11e8-a9f9-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_voice_descriptor = {
+    { 0x46f924ed, 0x25c3, 0x4272, 0x85b1, { 0x41, 0x78, 0x3f, 0x0d, 0xc6, 0x38 } },  // type
+    { 0x4ece09c2, 0x3728, 0x11e8, 0xa9f9, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for Voice",
+    "The Android Open Source Project",
+};
+
+/* system UUID: 4f705ff6-3728-11e8-a0c6-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_system_descriptor = {
+    { 0x8bd0f979, 0x5266, 0x4791, 0x9213, { 0x11, 0x3a, 0xbc, 0xf7, 0xd3, 0xdc } },  // type
+    { 0x4f705ff6, 0x3728, 0x11e8, 0xa0c6, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for System",
+    "The Android Open Source Project",
+};
+
+/* ring UUID: 4fd6e5c8-3728-11e8-8303-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_ring_descriptor = {
+    { 0x5380692a, 0x872e, 0x4697, 0x8e38, { 0xcd, 0xd1, 0x09, 0xf6, 0xcb, 0x87 } },  // type
+    { 0x4fd6e5c8, 0x3728, 0x11e8, 0x8303, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for Ring",
+    "The Android Open Source Project",
+};
+/* music UUID: 5036194e-3728-11e8-8db9-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_music_descriptor = {
+    { 0x3a3a19b2, 0x62b1, 0x4785, 0xb55e, { 0xb2, 0x8f, 0xd4, 0x1b, 0x83, 0x58 } },  // type
+    { 0x5036194e, 0x3728, 0x11e8, 0x8db9, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for Music",
+    "The Android Open Source Project",
+};
+/* alarm UUID: 50b9f084-3728-11e8-9225-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_alarm_descriptor = {
+    { 0x8d08d30f, 0xb4c3, 0x4600, 0x8f99, { 0xfc, 0xbb, 0x5d, 0x05, 0x8b, 0x60 } },  // type
+    { 0x50b9f084, 0x3728, 0x11e8, 0x9225, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for Alarm",
+    "The Android Open Source Project",
+};
+/* notification UUID: 50fe4d56-3728-11e8-ac73-fc4dd4486b6d */
+const effect_descriptor_t ma_listener_notification_descriptor = {
+    { 0x513d09f5, 0xae7f, 0x483d, 0x922a, { 0x5c, 0x72, 0xc5, 0xe5, 0x68, 0x4c } },  // type
+    { 0x50fe4d56, 0x3728, 0x11e8, 0xac73, { 0xfc, 0x4d, 0xd4, 0x48, 0x6b, 0x6d } },  // uuid
+    EFFECT_CONTROL_API_VERSION,
+    MA_FLAG,
+    0, /* TODO */
+    1,
+    "MAXXAUDIO listener for Notification",
+    "The Android Open Source Project",
+};
+
+static const effect_descriptor_t *descriptors[] = {
+    &ma_listener_voice_descriptor,
+    &ma_listener_system_descriptor,
+    &ma_listener_ring_descriptor,
+    &ma_listener_music_descriptor,
+    &ma_listener_alarm_descriptor,
+    &ma_listener_notification_descriptor,
+    NULL,
+};
+
+/* function of sending state to HAL */
+static bool (*send_ma_parameter)(int, float, bool);
+
+static int init_state = -1;
+pthread_once_t once = PTHREAD_ONCE_INIT;
+
+struct listnode ma_effect_list;
+pthread_mutex_t ma_listner_init_lock;
+
+/*
+ *  Local functions
+ */
+static inline bool valid_dev_in_context(struct ma_listener_context_s *context)
+{
+    /* check device */
+    if ((context->dev_id & AUDIO_DEVICE_OUT_SPEAKER) ||
+        (context->dev_id & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
+        /* TODO: it should be dynamic if hybird split A2SP is implemented. */
+        (context->dev_id & AUDIO_DEVICE_OUT_ALL_A2DP) ||
+        (context->dev_id & AUDIO_DEVICE_OUT_ALL_USB)) {
+        return true;
+    }
+
+    return false;
+}
+
+static void check_and_set_ma_parameter(uint32_t stream_type)
+{
+    bool active = false;
+    float temp_vol = 0.0;
+    float max_vol = 0.0;
+    struct listnode *node = NULL;
+    ma_listener_context_t *context = NULL;
+
+    ALOGV("%s .. called ..", __func__);
+    // get maximum volume for the active session with same strem type
+    list_for_each(node, &ma_effect_list) {
+        context = node_to_item(node, struct ma_listener_context_s, effect_list_node);
+        if (context->stream_type == stream_type &&
+            valid_dev_in_context(context)) {
+            if (context->state == MA_LISTENER_STATE_ACTIVE) {
+                active = true;
+                temp_vol = fmax(context->left_vol, context->right_vol);
+                if (max_vol < temp_vol)
+                    max_vol = temp_vol;
+                ALOGV("%s: check session(%d) volume(%f) for stream(%d)",
+                       __func__, context->session_id, temp_vol, stream_type);
+            }
+        }
+    }
+
+    // check volume
+    if (max_vol < 0.0) max_vol = 0;
+    else if (max_vol > 1.0) max_vol = 1.0;
+
+    if (send_ma_parameter != NULL &&
+        (g_cur_state[stream_type].vol != max_vol ||
+         g_cur_state[stream_type].active != active)) {
+
+        ALOGV("%s: set stream(%d) active(%s->%s) volume(%f->%f)",
+              __func__, stream_type,
+              g_cur_state[stream_type].active ? "T" : "F", active ? "T" : "F",
+              g_cur_state[stream_type].vol, max_vol);
+
+        // update changes to hal
+        send_ma_parameter(stream_type, max_vol, active);
+        g_cur_state[stream_type].vol = max_vol;
+        g_cur_state[stream_type].active = active;
+    }
+
+    return;
+}
+
+/*
+ * Effect Control Interface Implementation
+ */
+static int ma_effect_command(effect_handle_t self,
+                              uint32_t cmd_code, uint32_t cmd_size,
+                              void *p_cmd_data, uint32_t *reply_size,
+                              void *p_reply_data)
+{
+    ma_listener_context_t *context = (ma_listener_context_t *)self;
+    int status = 0;
+
+    ALOGV("%s .. called ..", __func__);
+    pthread_mutex_lock(&ma_listner_init_lock);
+
+    if (context == NULL || context->state == MA_LISTENER_STATE_UNINITIALIZED) {
+        ALOGE("%s: %s is NULL", __func__, (context == NULL) ?
+              "context" : "context->state");
+        status = -EINVAL;
+        goto exit;
+    }
+
+    switch (cmd_code) {
+    case EFFECT_CMD_INIT:
+        ALOGV("%s :: cmd called EFFECT_CMD_INIT", __func__);
+        if (p_reply_data == NULL || *reply_size != sizeof(int)) {
+            ALOGE("%s: EFFECT_CMD_INIT: %s, sending -EINVAL", __func__,
+                  (p_reply_data == NULL) ? "p_reply_data is NULL" :
+                  "*reply_size != sizeof(int)");
+            status = -EINVAL;
+            goto exit;
+        }
+        *(int *)p_reply_data = 0;
+        break;
+
+    case EFFECT_CMD_SET_CONFIG:
+        ALOGV("%s :: cmd called EFFECT_CMD_SET_CONFIG", __func__);
+        if (p_cmd_data == NULL || cmd_size != sizeof(effect_config_t)
+                || p_reply_data == NULL || reply_size == NULL || *reply_size != sizeof(int)) {
+            status = -EINVAL;
+            goto exit;
+        }
+        context->config = *(effect_config_t *)p_cmd_data;
+        *(int *)p_reply_data = 0;
+        break;
+
+    case EFFECT_CMD_GET_CONFIG:
+        ALOGV("%s :: cmd called EFFECT_CMD_GET_CONFIG", __func__);
+        break;
+
+    case EFFECT_CMD_RESET:
+        ALOGV("%s :: cmd called EFFECT_CMD_RESET", __func__);
+        break;
+
+    case EFFECT_CMD_SET_AUDIO_MODE:
+        ALOGV("%s :: cmd called EFFECT_CMD_SET_AUDIO_MODE", __func__);
+        break;
+
+    case EFFECT_CMD_OFFLOAD:
+        ALOGV("%s :: cmd called EFFECT_CMD_OFFLOAD", __func__);
+        if (p_reply_data == NULL || *reply_size != sizeof(int)) {
+            ALOGE("%s: EFFECT_CMD_OFFLOAD: %s, sending -EINVAL", __func__,
+                  (p_reply_data == NULL) ? "p_reply_data is NULL" :
+                  "*reply_size != sizeof(int)");
+            status = -EINVAL;
+            goto exit;
+        }
+        *(int *)p_reply_data = 0;
+        break;
+
+    case EFFECT_CMD_ENABLE:
+        ALOGV("%s :: cmd called EFFECT_CMD_ENABLE", __func__);
+        if (p_reply_data == NULL || *reply_size != sizeof(int)) {
+            ALOGE("%s: EFFECT_CMD_ENABLE: %s, sending -EINVAL", __func__,
+                   (p_reply_data == NULL) ? "p_reply_data is NULL" :
+                   "*reply_size != sizeof(int)");
+            status = -EINVAL;
+            goto exit;
+        }
+
+        if (context->state != MA_LISTENER_STATE_INITIALIZED) {
+            ALOGE("%s: EFFECT_CMD_ENABLE : state not INITIALIZED", __func__);
+            status = -ENOSYS;
+            goto exit;
+        }
+
+        context->state = MA_LISTENER_STATE_ACTIVE;
+        *(int *)p_reply_data = 0;
+
+        // After changing the state and if device is valid
+        // check and send state
+        if (valid_dev_in_context(context)) {
+            check_and_set_ma_parameter(context->stream_type);
+        }
+
+        break;
+
+    case EFFECT_CMD_DISABLE:
+        ALOGV("%s :: cmd called EFFECT_CMD_DISABLE", __func__);
+        if (p_reply_data == NULL || *reply_size != sizeof(int)) {
+            ALOGE("%s: EFFECT_CMD_DISABLE: %s, sending -EINVAL", __func__,
+                  (p_reply_data == NULL) ? "p_reply_data is NULL" :
+                  "*reply_size != sizeof(int)");
+            status = -EINVAL;
+            goto exit;
+        }
+
+        if (context->state != MA_LISTENER_STATE_ACTIVE) {
+            ALOGE("%s: EFFECT_CMD_ENABLE : state not ACTIVE", __func__);
+            status = -ENOSYS;
+            goto exit;
+        }
+
+        context->state = MA_LISTENER_STATE_INITIALIZED;
+        *(int *)p_reply_data = 0;
+
+        // After changing the state and if device is valid
+        // check and send state
+        if (valid_dev_in_context(context)) {
+            check_and_set_ma_parameter(context->stream_type);
+        }
+
+        break;
+
+    case EFFECT_CMD_GET_PARAM:
+        ALOGV("%s :: cmd called EFFECT_CMD_GET_PARAM", __func__);
+        break;
+
+    case EFFECT_CMD_SET_PARAM:
+        ALOGV("%s :: cmd called EFFECT_CMD_SET_PARAM", __func__);
+        break;
+
+    case EFFECT_CMD_SET_DEVICE:
+    {
+        uint32_t new_device;
+
+        if (p_cmd_data == NULL) {
+            ALOGE("%s: EFFECT_CMD_SET_DEVICE: cmd data NULL", __func__);
+            status = -EINVAL;
+            goto exit;
+        }
+
+        new_device = *(uint32_t *)p_cmd_data;
+        ALOGV("%s :: EFFECT_CMD_SET_DEVICE: (current/new) device (0x%x / 0x%x)",
+               __func__, context->dev_id, new_device);
+
+        context->dev_id = new_device;
+        // After changing the state and if device is valid
+        // check and send parameter
+        if (valid_dev_in_context(context)) {
+            check_and_set_ma_parameter(context->stream_type);
+        }
+    }
+    break;
+
+    case EFFECT_CMD_SET_VOLUME:
+    {
+        float left_vol = 0, right_vol = 0;
+
+        ALOGV("cmd called EFFECT_CMD_SET_VOLUME");
+        if (p_cmd_data == NULL || cmd_size != 2 * sizeof(uint32_t)) {
+            ALOGE("%s: EFFECT_CMD_SET_VOLUME: %s", __func__, (p_cmd_data == NULL) ?
+                  "p_cmd_data is NULL" : "cmd_size issue");
+            status = -EINVAL;
+            goto exit;
+        }
+
+        left_vol = (float)(*(uint32_t *)p_cmd_data) / (1 << 24);
+        right_vol = (float)(*((uint32_t *)p_cmd_data + 1)) / (1 << 24);
+        ALOGV("Current Volume (%f / %f ) new Volume (%f / %f)", context->left_vol,
+              context->right_vol, left_vol, right_vol);
+
+        context->left_vol = left_vol;
+        context->right_vol = right_vol;
+
+        // After changing the state and if device is valid
+        // check and send volume
+        if (valid_dev_in_context(context)) {
+            check_and_set_ma_parameter(context->stream_type);
+        }
+    }
+    break;
+
+    default:
+        ALOGW("%s: unknow command %d", __func__, cmd_code);
+        status = -ENOSYS;
+        break;
+    }
+
+exit:
+    pthread_mutex_unlock(&ma_listner_init_lock);
+    return status;
+}
+
+/* Effect Control Interface Implementation: get_descriptor */
+static int ma_effect_get_descriptor(effect_handle_t   self,
+                                     effect_descriptor_t *descriptor)
+{
+    ma_listener_context_t *context = (ma_listener_context_t *)self;
+    ALOGV("%s .. called ..", __func__);
+
+    if (descriptor == NULL) {
+        ALOGE("%s: descriptor is NULL", __func__);
+        return -EINVAL;
+    }
+
+    *descriptor = *context->desc;
+    return 0;
+}
+
+static void init_once()
+{
+    int ret = 0;
+    void *handle = NULL;
+    char lib_path[PATH_MAX] = {0};
+
+    if (init_state == 0) {
+        ALOGD("%s : already init ... do nothing", __func__);
+        return;
+    }
+
+    ALOGV("%s .. called ..", __func__);
+
+    send_ma_parameter = NULL;
+
+    ret = snprintf(lib_path, PATH_MAX, "%s/%s", HAL_VENDOR_PATH, HAL_LIB_NAME);
+    if (ret < 0) {
+        ALOGE("%s: snprintf failed for lib %s ret %d", __func__, HAL_LIB_NAME, ret);
+        return;
+    }
+
+    handle = dlopen(lib_path, RTLD_NOW);
+    if (handle == NULL) {
+        ALOGE("%s: DLOPEN failed for %s", __func__, HAL_LIB_NAME);
+        return;
+    } else {
+        ALOGV("%s: DLOPEN successful for %s", __func__, HAL_LIB_NAME);
+        send_ma_parameter = (bool (*)(int, float, bool))dlsym(handle, MA_SET_STATE);
+
+        if (!send_ma_parameter) {
+            ALOGE("%s: dlsym error %s for send_ma_parameter", __func__, dlerror());
+            return;
+        }
+    }
+
+    pthread_mutex_init(&ma_listner_init_lock, NULL);
+    list_init(&ma_effect_list);
+    init_state = 0;
+
+    ALOGD("%s: exit ret %d", __func__, init_state);
+}
+
+static bool lib_init()
+{
+    pthread_once(&once, init_once);
+    return init_state;
+}
+
+static int ma_prc_lib_create(const effect_uuid_t *uuid,
+                              int32_t session_id,
+                              int32_t io_id __unused,
+                              effect_handle_t *p_handle)
+{
+    int itt = 0;
+    ma_listener_context_t *context = NULL;
+
+    ALOGV("%s .. called ..", __func__);
+
+    if (lib_init() != 0) {
+        return init_state;
+    }
+
+    if (p_handle == NULL || uuid == NULL) {
+        ALOGE("%s: %s is NULL", __func__, (p_handle == NULL) ? "p_handle" : "uuid");
+        return -EINVAL;
+    }
+
+    context = (ma_listener_context_t *)calloc(1, sizeof(ma_listener_context_t));
+
+    if (context == NULL) {
+        ALOGE("%s: failed to allocate for context .. oops !!", __func__);
+        return -EINVAL;
+    }
+
+    // check if UUID is supported
+    for (itt = 0; descriptors[itt] != NULL; itt++) {
+        if (memcmp(uuid, &descriptors[itt]->uuid, sizeof(effect_uuid_t)) == 0) {
+            context->desc = descriptors[itt];
+            context->stream_type = itt;
+            PRINT_STREAM_TYPE(itt)
+            break;
+        }
+    }
+
+    if (descriptors[itt] == NULL) {
+        ALOGE("%s .. couldnt find passed uuid, something wrong", __func__);
+        free(context);
+        return -EINVAL;
+    }
+
+    ALOGV("%s CREATED_CONTEXT %p", __func__, context);
+
+    context->itfe = &effect_interface;
+    context->state = MA_LISTENER_STATE_INITIALIZED;
+    context->dev_id = AUDIO_DEVICE_NONE;
+    context->session_id = session_id;
+
+    // Add this to master list
+    pthread_mutex_lock(&ma_listner_init_lock);
+    list_add_tail(&ma_effect_list, &context->effect_list_node);
+
+    pthread_mutex_unlock(&ma_listner_init_lock);
+
+    *p_handle = (effect_handle_t)context;
+    return 0;
+}
+
+static int ma_prc_lib_release(effect_handle_t handle)
+{
+    struct listnode *node, *temp_node_next;
+    ma_listener_context_t *context = NULL;
+    ma_listener_context_t *recv_contex = (ma_listener_context_t *)handle;
+    int status = -EINVAL;
+
+    ALOGV("%s: context %p", __func__, handle);
+
+    if (recv_contex == NULL) {
+        return status;
+    }
+
+    pthread_mutex_lock(&ma_listner_init_lock);
+    // check if the handle/context provided is valid
+    list_for_each_safe(node, temp_node_next, &ma_effect_list) {
+        context = node_to_item(node, struct ma_listener_context_s, effect_list_node);
+        if (context == recv_contex) {
+            ALOGV("--- Found something to remove ---");
+            list_remove(node);
+            PRINT_STREAM_TYPE(context->stream_type);
+            free(context);
+            status = 0;
+        }
+    }
+
+    if (status != 0) {
+        ALOGE("%s: nothing to remove, ret %d", __func__, status);
+        pthread_mutex_unlock(&ma_listner_init_lock);
+        return status;
+    }
+
+    pthread_mutex_unlock(&ma_listner_init_lock);
+    return status;
+}
+
+static int ma_prc_lib_get_descriptor(const effect_uuid_t *uuid,
+                                      effect_descriptor_t *descriptor)
+{
+    int i = 0;
+
+    ALOGV("%s .. called ..", __func__);
+    if (lib_init() != 0) {
+        return init_state;
+    }
+
+    if (descriptor == NULL || uuid == NULL) {
+        ALOGE("%s: %s is NULL", __func__, (descriptor == NULL) ? "descriptor" : "uuid");
+        return -EINVAL;
+    }
+
+    for (i = 0; descriptors[i] != NULL; i++) {
+        if (memcmp(uuid, &descriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
+            *descriptor = *descriptors[i];
+            return 0;
+        }
+    }
+
+    ALOGE("%s: couldnt found uuid passed, oops", __func__);
+    return -EINVAL;
+}
+
+
+/* effect_handle_t interface implementation for volume listener effect */
+static const struct effect_interface_s effect_interface = {
+    NULL,
+    ma_effect_command,
+    ma_effect_get_descriptor,
+    NULL,
+};
+
+__attribute__((visibility("default")))
+audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
+    .tag = AUDIO_EFFECT_LIBRARY_TAG,
+    .version = EFFECT_LIBRARY_API_VERSION,
+    .name = "MAXXAUDIO Listener Effect Library",
+    .implementor = "The Android Open Source Project",
+    .create_effect = ma_prc_lib_create,
+    .release_effect = ma_prc_lib_release,
+    .get_descriptor = ma_prc_lib_get_descriptor,
+};