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,
 
     /*