Home | History | Annotate | Download | only in msm8974
      1 /*
      2  * Copyright (C) 2013-2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #define LOG_TAG "msm8974_platform"
     17 /*#define LOG_NDEBUG 0*/
     18 #define LOG_NDDEBUG 0
     19 
     20 #include <stdlib.h>
     21 #include <dlfcn.h>
     22 #include <pthread.h>
     23 #include <unistd.h>
     24 #include <log/log.h>
     25 #include <cutils/str_parms.h>
     26 #include <cutils/properties.h>
     27 #include <audio_hw.h>
     28 #include <platform_api.h>
     29 #include "acdb.h"
     30 #include "platform.h"
     31 #include "audio_extn.h"
     32 #include <linux/msm_audio.h>
     33 #if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
     34 #include <sound/devdep_params.h>
     35 #endif
     36 
     37 #include "maxxaudio.h"
     38 #include <resolv.h>
     39 
     40 #define MIXER_XML_DEFAULT_PATH "mixer_paths.xml"
     41 #define MIXER_XML_BASE_STRING "mixer_paths"
     42 #define TOMTOM_8226_SND_CARD_NAME "msm8226-tomtom-snd-card"
     43 #define TOMTOM_MIXER_FILE_SUFFIX "wcd9330"
     44 
     45 #define LIB_ACDB_LOADER "libacdbloader.so"
     46 #define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
     47 #define CVD_VERSION_MIXER_CTL "CVD Version"
     48 
     49 #define min(a, b) ((a) < (b) ? (a) : (b))
     50 
     51 /*
     52  * This file will have a maximum of 38 bytes:
     53  *
     54  * 4 bytes: number of audio blocks
     55  * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
     56  * Maximum 10 * 3 bytes: SAD blocks
     57  */
     58 #define MAX_SAD_BLOCKS      10
     59 #define SAD_BLOCK_SIZE      3
     60 
     61 #define MAX_CVD_VERSION_STRING_SIZE    100
     62 
     63 /* EDID format ID for LPCM audio */
     64 #define EDID_FORMAT_LPCM    1
     65 
     66 #define MAX_SND_CARD_NAME_LEN 31
     67 
     68 #define DEFAULT_APP_TYPE_RX_PATH  69936
     69 #define DEFAULT_APP_TYPE_TX_PATH  69938
     70 #define DEFAULT_RX_BACKEND "SLIMBUS_0_RX"
     71 
     72 #define TOSTRING_(x) #x
     73 #define TOSTRING(x) TOSTRING_(x)
     74 
     75 struct audio_block_header
     76 {
     77     int reserved;
     78     int length;
     79 };
     80 
     81 enum {
     82     CAL_MODE_SEND           = 0x1,
     83     CAL_MODE_PERSIST        = 0x2,
     84     CAL_MODE_RTAC           = 0x4
     85 };
     86 
     87 #define PLATFORM_CONFIG_KEY_OPERATOR_INFO "operator_info"
     88 
     89 struct operator_info {
     90     struct listnode list;
     91     char *name;
     92     char *mccmnc;
     93 };
     94 
     95 struct operator_specific_device {
     96     struct listnode list;
     97     char *operator;
     98     char *mixer_path;
     99     int acdb_id;
    100 };
    101 
    102 #define BE_DAI_NAME_MAX_LENGTH 24
    103 struct be_dai_name_struct {
    104     unsigned int be_id;
    105     char be_name[BE_DAI_NAME_MAX_LENGTH];
    106 };
    107 
    108 struct snd_device_to_mic_map {
    109     struct mic_info microphones[AUDIO_MICROPHONE_MAX_COUNT];
    110     size_t mic_count;
    111 };
    112 
    113 static struct listnode operator_info_list;
    114 static struct listnode *operator_specific_device_table[SND_DEVICE_MAX];
    115 
    116 #define AUDIO_PARAMETER_KEY_AUD_CALDATA "cal_data"
    117 
    118 typedef struct acdb_audio_cal_cfg {
    119     uint32_t             persist;
    120     uint32_t             snd_dev_id;
    121     audio_devices_t      dev_id;
    122     int32_t              acdb_dev_id;
    123     uint32_t             app_type;
    124     uint32_t             topo_id;
    125     uint32_t             sampling_rate;
    126     uint32_t             cal_type;
    127     uint32_t             module_id;
    128     uint32_t             param_id;
    129 } acdb_audio_cal_cfg_t;
    130 
    131 /* Audio calibration related functions */
    132 typedef void (*acdb_send_audio_cal_v3_t)(int, int, int, int, int);
    133 
    134 struct platform_data {
    135     struct audio_device *adev;
    136     bool fluence_in_spkr_mode;
    137     bool fluence_in_voice_call;
    138     bool fluence_in_voice_comm;
    139     bool fluence_in_voice_rec;
    140     /* 0 = no fluence, 1 = fluence, 2 = fluence pro */
    141     int  fluence_type;
    142     int  source_mic_type;
    143     bool speaker_lr_swap;
    144 
    145     void *acdb_handle;
    146 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
    147     acdb_init_v2_cvd_t acdb_init;
    148 #elif defined (PLATFORM_MSM8084)
    149     acdb_init_v2_t acdb_init;
    150 #else
    151     acdb_init_t acdb_init;
    152 #endif
    153     acdb_deallocate_t          acdb_deallocate;
    154     acdb_send_audio_cal_t      acdb_send_audio_cal;
    155     acdb_send_audio_cal_v3_t   acdb_send_audio_cal_v3;
    156     acdb_set_audio_cal_t       acdb_set_audio_cal;
    157     acdb_send_voice_cal_t      acdb_send_voice_cal;
    158     acdb_reload_vocvoltable_t  acdb_reload_vocvoltable;
    159     acdb_send_gain_dep_cal_t   acdb_send_gain_dep_cal;
    160     acdb_send_custom_top_t     acdb_send_custom_top;
    161     bool acdb_initialized;
    162 
    163     struct csd_data *csd;
    164     char ec_ref_mixer_path[64];
    165 
    166     codec_backend_cfg_t current_backend_cfg[MAX_CODEC_BACKENDS];
    167     char *snd_card_name;
    168     int max_vol_index;
    169     int max_mic_count;
    170 
    171     void *hw_info;
    172 
    173     uint32_t declared_mic_count;
    174     struct audio_microphone_characteristic_t microphones[AUDIO_MICROPHONE_MAX_COUNT];
    175     struct snd_device_to_mic_map mic_map[SND_DEVICE_MAX];
    176 };
    177 
    178 static int pcm_device_table[AUDIO_USECASE_MAX][2] = {
    179     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
    180                                             DEEP_BUFFER_PCM_DEVICE},
    181     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
    182                                             LOWLATENCY_PCM_DEVICE},
    183     [USECASE_AUDIO_PLAYBACK_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
    184                                          MULTIMEDIA2_PCM_DEVICE},
    185     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {PLAYBACK_OFFLOAD_DEVICE,
    186                                         PLAYBACK_OFFLOAD_DEVICE},
    187     [USECASE_AUDIO_PLAYBACK_TTS] = {MULTIMEDIA2_PCM_DEVICE,
    188                                         MULTIMEDIA2_PCM_DEVICE},
    189     [USECASE_AUDIO_PLAYBACK_ULL] = {MULTIMEDIA3_PCM_DEVICE,
    190                                     MULTIMEDIA3_PCM_DEVICE},
    191     [USECASE_AUDIO_PLAYBACK_MMAP] = {MMAP_PLAYBACK_PCM_DEVICE,
    192             MMAP_PLAYBACK_PCM_DEVICE},
    193 
    194     [USECASE_AUDIO_RECORD] = {AUDIO_RECORD_PCM_DEVICE,
    195                               AUDIO_RECORD_PCM_DEVICE},
    196     [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
    197                                           LOWLATENCY_PCM_DEVICE},
    198 
    199     [USECASE_AUDIO_RECORD_MMAP] = {MMAP_RECORD_PCM_DEVICE,
    200             MMAP_RECORD_PCM_DEVICE},
    201     [USECASE_AUDIO_RECORD_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
    202                                    MULTIMEDIA2_PCM_DEVICE},
    203 
    204     [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE,
    205                             VOICE_CALL_PCM_DEVICE},
    206     [USECASE_VOICE2_CALL] = {VOICE2_CALL_PCM_DEVICE, VOICE2_CALL_PCM_DEVICE},
    207     [USECASE_VOLTE_CALL] = {VOLTE_CALL_PCM_DEVICE, VOLTE_CALL_PCM_DEVICE},
    208     [USECASE_QCHAT_CALL] = {QCHAT_CALL_PCM_DEVICE, QCHAT_CALL_PCM_DEVICE},
    209     [USECASE_VOWLAN_CALL] = {VOWLAN_CALL_PCM_DEVICE, VOWLAN_CALL_PCM_DEVICE},
    210     [USECASE_VOICEMMODE1_CALL] = {VOICEMMODE1_CALL_PCM_DEVICE,
    211                                   VOICEMMODE1_CALL_PCM_DEVICE},
    212     [USECASE_VOICEMMODE2_CALL] = {VOICEMMODE2_CALL_PCM_DEVICE,
    213                                   VOICEMMODE2_CALL_PCM_DEVICE},
    214 
    215     [USECASE_INCALL_REC_UPLINK] = {AUDIO_RECORD_PCM_DEVICE,
    216                                    AUDIO_RECORD_PCM_DEVICE},
    217     [USECASE_INCALL_REC_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
    218                                      AUDIO_RECORD_PCM_DEVICE},
    219     [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
    220                                                 AUDIO_RECORD_PCM_DEVICE},
    221     [USECASE_AUDIO_HFP_SCO] = {HFP_PCM_RX, HFP_SCO_RX},
    222 
    223     [USECASE_AUDIO_SPKR_CALIB_RX] = {SPKR_PROT_CALIB_RX_PCM_DEVICE, -1},
    224     [USECASE_AUDIO_SPKR_CALIB_TX] = {-1, SPKR_PROT_CALIB_TX_PCM_DEVICE},
    225 
    226     [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
    227                                           AFE_PROXY_RECORD_PCM_DEVICE},
    228     [USECASE_AUDIO_RECORD_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
    229                                         AFE_PROXY_RECORD_PCM_DEVICE},
    230     [USECASE_AUDIO_DSM_FEEDBACK] = {QUAT_MI2S_PCM_DEVICE, QUAT_MI2S_PCM_DEVICE},
    231 
    232     [USECASE_AUDIO_PLAYBACK_VOIP] = {AUDIO_PLAYBACK_VOIP_PCM_DEVICE,
    233                                      AUDIO_PLAYBACK_VOIP_PCM_DEVICE},
    234     [USECASE_AUDIO_RECORD_VOIP] = {AUDIO_RECORD_VOIP_PCM_DEVICE,
    235                                    AUDIO_RECORD_VOIP_PCM_DEVICE},
    236 
    237     [USECASE_INCALL_MUSIC_UPLINK] = {INCALL_MUSIC_UPLINK_PCM_DEVICE,
    238                                      INCALL_MUSIC_UPLINK_PCM_DEVICE},
    239 };
    240 
    241 /* Array to store sound devices */
    242 static const char * const device_table[SND_DEVICE_MAX] = {
    243     [SND_DEVICE_NONE] = "none",
    244     /* Playback sound devices */
    245     [SND_DEVICE_OUT_HANDSET] = "handset",
    246     [SND_DEVICE_OUT_SPEAKER] = "speaker",
    247     [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
    248     [SND_DEVICE_OUT_SPEAKER_SAFE] = "speaker-safe",
    249     [SND_DEVICE_OUT_HEADPHONES] = "headphones",
    250     [SND_DEVICE_OUT_LINE] = "line",
    251     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
    252     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = "speaker-safe-and-headphones",
    253     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = "speaker-and-line",
    254     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = "speaker-safe-and-line",
    255     [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
    256     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = "voice-hac-handset",
    257     [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
    258     [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
    259     [SND_DEVICE_OUT_VOICE_LINE] = "voice-line",
    260     [SND_DEVICE_OUT_HDMI] = "hdmi",
    261     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
    262     [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
    263     [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
    264     [SND_DEVICE_OUT_BT_A2DP] = "bt-a2dp",
    265     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = "speaker-and-bt-a2dp",
    266     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = "speaker-safe-and-bt-a2dp",
    267     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
    268     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
    269     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
    270     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
    271     [SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = "voice-tty-full-usb",
    272     [SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = "voice-tty-vco-usb",
    273     [SND_DEVICE_OUT_VOICE_TX] = "voice-tx",
    274     [SND_DEVICE_OUT_USB_HEADSET] = "usb-headset",
    275     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = "usb-headset",
    276     [SND_DEVICE_OUT_USB_HEADPHONES] = "usb-headphones",
    277     [SND_DEVICE_OUT_USB_HEADSET_SPEC] = "usb-headset",
    278     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = "usb-headphones",
    279     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = "speaker-and-usb-headphones",
    280     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = "speaker-safe-and-usb-headphones",
    281     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = "speaker-protected",
    282     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = "voice-speaker-protected",
    283     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = "voice-speaker-hfp",
    284     [SND_DEVICE_OUT_SPEAKER_AND_BT_SCO] = "speaker-and-bt-sco",
    285     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = "speaker-safe-and-bt-sco",
    286     [SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB] = "speaker-and-bt-sco-wb",
    287     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = "speaker-safe-and-bt-sco-wb",
    288 
    289     /* Capture sound devices */
    290     [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
    291     [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
    292     [SND_DEVICE_IN_HANDSET_MIC_NS] = "handset-mic",
    293     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = "handset-mic",
    294     [SND_DEVICE_IN_HANDSET_DMIC] = "dmic-endfire",
    295     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = "dmic-endfire",
    296     [SND_DEVICE_IN_HANDSET_DMIC_NS] = "dmic-endfire",
    297     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = "dmic-endfire",
    298     [SND_DEVICE_IN_HANDSET_DMIC_STEREO] = "dmic-endfire",
    299 
    300     [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
    301     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "speaker-mic",
    302     [SND_DEVICE_IN_SPEAKER_MIC_NS] = "speaker-mic",
    303     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = "speaker-mic",
    304     [SND_DEVICE_IN_SPEAKER_DMIC] = "speaker-dmic-endfire",
    305     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = "speaker-dmic-endfire",
    306     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = "speaker-dmic-endfire",
    307     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = "speaker-dmic-endfire",
    308     [SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = "speaker-dmic-endfire",
    309 
    310     [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
    311     [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
    312 
    313     [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
    314     [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
    315     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = "bt-sco-mic",
    316     [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
    317     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = "bt-sco-mic-wb",
    318     [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
    319 
    320     [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
    321     [SND_DEVICE_IN_VOICE_DMIC_TMUS] = "voice-dmic-ef-tmus",
    322     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
    323     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
    324     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = "voice-speaker-mic-hfp",
    325     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
    326     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
    327     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
    328     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
    329     [SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = "voice-tty-full-usb-mic",
    330     [SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = "voice-tty-hco-usb-mic",
    331 
    332     [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
    333     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = "voice-rec-mic",
    334     [SND_DEVICE_IN_VOICE_REC_MIC_AEC] = "voice-rec-mic",
    335     [SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = "voice-rec-mic",
    336     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = "voice-rec-dmic-ef",
    337     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = "voice-rec-dmic-ef-fluence",
    338     [SND_DEVICE_IN_USB_HEADSET_MIC] = "usb-headset-mic",
    339     [SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] ="usb-headset-mic",
    340     [SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = "usb-headset-mic",
    341     [SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = "usb-headset-mic",
    342     [SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = "usb-headset-mic",
    343     [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "headset-mic",
    344 
    345     [SND_DEVICE_IN_UNPROCESSED_MIC] = "unprocessed-mic",
    346     [SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = "unprocessed-stereo-mic",
    347     [SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = "unprocessed-three-mic",
    348     [SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = "unprocessed-quad-mic",
    349     [SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = "unprocessed-headset-mic",
    350 
    351     [SND_DEVICE_IN_VOICE_RX] = "voice-rx",
    352 
    353     [SND_DEVICE_IN_THREE_MIC] = "three-mic",
    354     [SND_DEVICE_IN_QUAD_MIC] = "quad-mic",
    355     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = "vi-feedback",
    356     [SND_DEVICE_IN_HANDSET_TMIC] = "three-mic",
    357     [SND_DEVICE_IN_HANDSET_QMIC] = "quad-mic",
    358     [SND_DEVICE_IN_HANDSET_TMIC_AEC] = "three-mic",
    359     [SND_DEVICE_IN_HANDSET_QMIC_AEC] = "quad-mic",
    360 };
    361 
    362 /* ACDB IDs (audio DSP path configuration IDs) for each sound device */
    363 static int acdb_device_table[SND_DEVICE_MAX] = {
    364     [SND_DEVICE_NONE] = -1,
    365     [SND_DEVICE_OUT_HANDSET] = 7,
    366     [SND_DEVICE_OUT_SPEAKER] = 15,
    367     [SND_DEVICE_OUT_SPEAKER_REVERSE] = 15,
    368     [SND_DEVICE_OUT_SPEAKER_SAFE] = 15,
    369     [SND_DEVICE_OUT_HEADPHONES] = 10,
    370     [SND_DEVICE_OUT_LINE] = 77,
    371     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
    372     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = 10,
    373     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = 77,
    374     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = 77,
    375     [SND_DEVICE_OUT_VOICE_HANDSET] = ACDB_ID_VOICE_HANDSET,
    376     [SND_DEVICE_OUT_VOICE_SPEAKER] = ACDB_ID_VOICE_SPEAKER,
    377     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = 53,
    378     [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
    379     [SND_DEVICE_OUT_VOICE_LINE] = 77,
    380     [SND_DEVICE_OUT_HDMI] = 18,
    381     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
    382     [SND_DEVICE_OUT_BT_SCO] = 22,
    383     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = 14,
    384     [SND_DEVICE_OUT_BT_SCO_WB] = 39,
    385     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = 14,
    386     [SND_DEVICE_OUT_BT_A2DP] = 20,
    387     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = 14,
    388     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = 14,
    389     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = ACDB_ID_VOICE_HANDSET_TMUS,
    390     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
    391     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
    392     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
    393     [SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = 17,
    394     [SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = 17,
    395     [SND_DEVICE_OUT_VOICE_TX] = 45,
    396     [SND_DEVICE_OUT_USB_HEADSET] = 45,
    397     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = 45,
    398     [SND_DEVICE_OUT_USB_HEADPHONES] = 45,
    399     [SND_DEVICE_OUT_USB_HEADSET_SPEC] = 45,
    400     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = 45,
    401     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 14,
    402     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = 14,
    403     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = 124,
    404     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = 101,
    405     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = ACDB_ID_VOICE_SPEAKER,
    406 
    407     [SND_DEVICE_IN_HANDSET_MIC] = 4,
    408     [SND_DEVICE_IN_HANDSET_MIC_AEC] = 106,
    409     [SND_DEVICE_IN_HANDSET_MIC_NS] = 107,
    410     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = 108,
    411     [SND_DEVICE_IN_HANDSET_DMIC] = 41,
    412     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = 109,
    413     [SND_DEVICE_IN_HANDSET_DMIC_NS] = 110,
    414     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = 111,
    415     [SND_DEVICE_IN_HANDSET_DMIC_STEREO] = 34,
    416 
    417     [SND_DEVICE_IN_SPEAKER_MIC] = 11,
    418     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 112,
    419     [SND_DEVICE_IN_SPEAKER_MIC_NS] = 113,
    420     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = 114,
    421     [SND_DEVICE_IN_SPEAKER_DMIC] = 43,
    422     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = 115,
    423     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = 116,
    424     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = 117,
    425     [SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = 35,
    426 
    427     [SND_DEVICE_IN_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
    428     [SND_DEVICE_IN_HEADSET_MIC_AEC] = ACDB_ID_HEADSET_MIC_AEC,
    429 
    430     [SND_DEVICE_IN_HDMI_MIC] = 4,
    431     [SND_DEVICE_IN_BT_SCO_MIC] = 21,
    432     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = 21,
    433     [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
    434     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = 38,
    435     [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
    436 
    437     [SND_DEVICE_IN_VOICE_DMIC] = 41,
    438     [SND_DEVICE_IN_VOICE_DMIC_TMUS] = ACDB_ID_VOICE_DMIC_EF_TMUS,
    439     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
    440     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = 11,
    441     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
    442     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
    443     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
    444     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
    445     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
    446     [SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = 16,
    447     [SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = 16,
    448 
    449     [SND_DEVICE_IN_VOICE_REC_MIC] = ACDB_ID_VOICE_REC_MIC,
    450     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = 113,
    451     [SND_DEVICE_IN_VOICE_REC_MIC_AEC] = 112,
    452     [SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = 114,
    453     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = 35,
    454     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = 43,
    455     [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
    456 
    457     [SND_DEVICE_IN_UNPROCESSED_MIC] = ACDB_ID_VOICE_REC_MIC,
    458     [SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
    459     [SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = 35,
    460     [SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = 125,
    461     [SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = 125,
    462 
    463     [SND_DEVICE_IN_VOICE_RX] = 44,
    464     [SND_DEVICE_IN_USB_HEADSET_MIC] = 44,
    465     [SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = 44,
    466     [SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = 44,
    467     [SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = 44,
    468     [SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = 44,
    469     [SND_DEVICE_IN_THREE_MIC] = 46,
    470     [SND_DEVICE_IN_QUAD_MIC] = 46,
    471     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = 102,
    472     [SND_DEVICE_IN_HANDSET_TMIC] = 125,
    473     [SND_DEVICE_IN_HANDSET_QMIC] = 125,
    474     [SND_DEVICE_IN_HANDSET_TMIC_AEC] = 125, /* override this for new target to 140 */
    475     [SND_DEVICE_IN_HANDSET_QMIC_AEC] = 125, /* override this for new target to 140 */
    476 };
    477 
    478 // Platform specific backend bit width table
    479 static int backend_bit_width_table[SND_DEVICE_MAX] = {0};
    480 
    481 struct name_to_index {
    482     char name[100];
    483     unsigned int index;
    484 };
    485 
    486 #define TO_NAME_INDEX(X)   #X, X
    487 
    488 /* Used to get index from parsed string */
    489 static const struct name_to_index snd_device_name_index[SND_DEVICE_MAX] = {
    490     /* out */
    491     {TO_NAME_INDEX(SND_DEVICE_OUT_HANDSET)},
    492     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER)},
    493     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_REVERSE)},
    494     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE)},
    495     {TO_NAME_INDEX(SND_DEVICE_OUT_HEADPHONES)},
    496     {TO_NAME_INDEX(SND_DEVICE_OUT_LINE)},
    497     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES)},
    498     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES)},
    499     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_LINE)},
    500     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE)},
    501     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET)},
    502     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER)},
    503     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_HFP)},
    504     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADPHONES)},
    505     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_LINE)},
    506     {TO_NAME_INDEX(SND_DEVICE_OUT_HDMI)},
    507     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HDMI)},
    508     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO)},
    509     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO)},
    510     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO_WB)},
    511     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB)},
    512     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_A2DP)},
    513     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP)},
    514     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)},
    515     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET_TMUS)},
    516     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HAC_HANDSET)},
    517     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES)},
    518     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES)},
    519     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET)},
    520     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_SCO)},
    521     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB)},
    522     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_USB)},
    523     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_USB)},
    524     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET)},
    525     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADSET)},
    526     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADPHONES)},
    527     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADPHONES)},
    528     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET)},
    529     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET)},
    530     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_PROTECTED)},
    531     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED)},
    532     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET_SPEC)},
    533 
    534     /* in */
    535     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC)},
    536     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC)},
    537     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_NS)},
    538     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)},
    539     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC)},
    540     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC)},
    541     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_NS)},
    542     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)},
    543     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_STEREO)},
    544 
    545     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC)},
    546     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC)},
    547     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_NS)},
    548     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)},
    549     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC)},
    550     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC)},
    551     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS)},
    552     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)},
    553     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_STEREO)},
    554 
    555     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC)},
    556     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC_AEC)},
    557 
    558     {TO_NAME_INDEX(SND_DEVICE_IN_HDMI_MIC)},
    559     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC)},
    560     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_NREC)},
    561     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB)},
    562     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB_NREC)},
    563     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_MIC)},
    564 
    565     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC)},
    566     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC_TMUS)},
    567     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC)},
    568     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP)},
    569     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC)},
    570     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_HEADSET_MIC)},
    571     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC)},
    572     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC)},
    573     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC)},
    574     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC)},
    575     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC)},
    576 
    577 
    578     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC)},
    579     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_NS)},
    580     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_AEC)},
    581     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS)},
    582     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_STEREO)},
    583     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE)},
    584     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_HEADSET_MIC)},
    585     {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC)},
    586     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_USB_HEADSET_MIC)},
    587     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC)},
    588     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC)},
    589     {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC_AEC)},
    590 
    591     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_MIC)},
    592     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC)},
    593     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_STEREO_MIC)},
    594     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_THREE_MIC)},
    595     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_QUAD_MIC)},
    596 
    597     {TO_NAME_INDEX(SND_DEVICE_IN_THREE_MIC)},
    598     {TO_NAME_INDEX(SND_DEVICE_IN_QUAD_MIC)},
    599     {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK)},
    600     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_TMIC)},
    601     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC)},
    602     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_TMIC_AEC)},
    603     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC_AEC)},
    604 };
    605 
    606 static char * backend_tag_table[SND_DEVICE_MAX] = {0};
    607 static char * hw_interface_table[SND_DEVICE_MAX] = {0};
    608 
    609 static const struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
    610     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
    611     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
    612     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_HIFI)},
    613     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
    614     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_TTS)},
    615     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
    616     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_MMAP)},
    617     {TO_NAME_INDEX(USECASE_AUDIO_RECORD)},
    618     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_LOW_LATENCY)},
    619     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_MMAP)},
    620     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_HIFI)},
    621     {TO_NAME_INDEX(USECASE_VOICE_CALL)},
    622     {TO_NAME_INDEX(USECASE_VOICE2_CALL)},
    623     {TO_NAME_INDEX(USECASE_VOLTE_CALL)},
    624     {TO_NAME_INDEX(USECASE_QCHAT_CALL)},
    625     {TO_NAME_INDEX(USECASE_VOWLAN_CALL)},
    626     {TO_NAME_INDEX(USECASE_VOICEMMODE1_CALL)},
    627     {TO_NAME_INDEX(USECASE_VOICEMMODE2_CALL)},
    628     {TO_NAME_INDEX(USECASE_INCALL_REC_UPLINK)},
    629     {TO_NAME_INDEX(USECASE_INCALL_REC_DOWNLINK)},
    630     {TO_NAME_INDEX(USECASE_INCALL_REC_UPLINK_AND_DOWNLINK)},
    631     {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO)},
    632     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_RX)},
    633     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_TX)},
    634     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_AFE_PROXY)},
    635     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_AFE_PROXY)},
    636     {TO_NAME_INDEX(USECASE_AUDIO_DSM_FEEDBACK)},
    637     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_VOIP)},
    638     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_VOIP)},
    639     {TO_NAME_INDEX(USECASE_INCALL_MUSIC_UPLINK)},
    640     {TO_NAME_INDEX(USECASE_AUDIO_A2DP_ABR_FEEDBACK)},
    641 };
    642 
    643 static const struct name_to_index usecase_type_index[USECASE_TYPE_MAX] = {
    644     {TO_NAME_INDEX(PCM_PLAYBACK)},
    645     {TO_NAME_INDEX(PCM_CAPTURE)},
    646     {TO_NAME_INDEX(VOICE_CALL)},
    647     {TO_NAME_INDEX(PCM_HFP_CALL)},
    648 };
    649 
    650 struct app_type_entry {
    651     int uc_type;
    652     int bit_width;
    653     int app_type;
    654     int max_rate;
    655     char *mode;
    656     struct listnode node; // membership in app_type_entry_list;
    657 };
    658 
    659 static struct listnode app_type_entry_list;
    660 
    661 #define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
    662 #define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
    663 #define ULL_PLATFORM_DELAY         (3*1000LL)
    664 #define MMAP_PLATFORM_DELAY        (3*1000LL)
    665 
    666 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
    667 static bool is_tmus = false;
    668 
    669 static int init_be_dai_name_table(struct audio_device *adev);
    670 
    671 static void check_operator()
    672 {
    673     char value[PROPERTY_VALUE_MAX];
    674     int mccmnc;
    675     property_get("gsm.sim.operator.numeric",value,"0");
    676     mccmnc = atoi(value);
    677     ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
    678     switch(mccmnc) {
    679     /* TMUS MCC(310), MNC(490, 260, 026) */
    680     case 310490:
    681     case 310260:
    682     case 310026:
    683     /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
    684     case 310800:
    685     case 310660:
    686     case 310580:
    687     case 310310:
    688     case 310270:
    689     case 310250:
    690     case 310240:
    691     case 310230:
    692     case 310220:
    693     case 310210:
    694     case 310200:
    695     case 310160:
    696         is_tmus = true;
    697         break;
    698     }
    699 }
    700 
    701 bool is_operator_tmus()
    702 {
    703     pthread_once(&check_op_once_ctl, check_operator);
    704     return is_tmus;
    705 }
    706 
    707 static char *get_current_operator()
    708 {
    709     struct listnode *node;
    710     struct operator_info *info_item;
    711     char mccmnc[PROPERTY_VALUE_MAX];
    712     char *ret = NULL;
    713 
    714     property_get("gsm.sim.operator.numeric",mccmnc,"00000");
    715 
    716     list_for_each(node, &operator_info_list) {
    717         info_item = node_to_item(node, struct operator_info, list);
    718         if (strstr(info_item->mccmnc, mccmnc) != NULL) {
    719             ret = info_item->name;
    720         }
    721     }
    722 
    723     return ret;
    724 }
    725 
    726 static struct operator_specific_device *get_operator_specific_device(snd_device_t snd_device)
    727 {
    728     struct listnode *node;
    729     struct operator_specific_device *ret = NULL;
    730     struct operator_specific_device *device_item;
    731     char *operator_name;
    732 
    733     operator_name = get_current_operator();
    734     if (operator_name == NULL)
    735         return ret;
    736 
    737     list_for_each(node, operator_specific_device_table[snd_device]) {
    738         device_item = node_to_item(node, struct operator_specific_device, list);
    739         if (strcmp(operator_name, device_item->operator) == 0) {
    740             ret = device_item;
    741         }
    742     }
    743 
    744     return ret;
    745 }
    746 
    747 
    748 static int get_operator_specific_device_acdb_id(snd_device_t snd_device)
    749 {
    750     struct operator_specific_device *device;
    751     int ret = acdb_device_table[snd_device];
    752 
    753     device = get_operator_specific_device(snd_device);
    754     if (device != NULL)
    755         ret = device->acdb_id;
    756 
    757     return ret;
    758 }
    759 
    760 static const char *get_operator_specific_device_mixer_path(snd_device_t snd_device)
    761 {
    762     struct operator_specific_device *device;
    763     const char *ret = device_table[snd_device];
    764 
    765     device = get_operator_specific_device(snd_device);
    766     if (device != NULL)
    767         ret = device->mixer_path;
    768 
    769     return ret;
    770 }
    771 
    772 inline bool platform_supports_app_type_cfg()
    773 {
    774 #if defined (PLATFORM_MSM8998) || (PLATFORM_SDM845)
    775     return true;
    776 #else
    777     return false;
    778 #endif
    779 }
    780 
    781 static int parse_audiocal_cfg(struct str_parms *parms, acdb_audio_cal_cfg_t *cal)
    782 {
    783     int err;
    784     char value[64];
    785     int ret = 0;
    786 
    787     if (parms == NULL || cal == NULL)
    788         return ret;
    789 
    790     err = str_parms_get_str(parms, "cal_persist", value, sizeof(value));
    791     if (err >= 0) {
    792         str_parms_del(parms, "cal_persist");
    793         cal->persist = (uint32_t)strtoul(value, NULL, 0);
    794         ret = ret | 0x1;
    795     }
    796     err = str_parms_get_str(parms, "cal_apptype", value, sizeof(value));
    797     if (err >= 0) {
    798         str_parms_del(parms, "cal_apptype");
    799         cal->app_type = (uint32_t)strtoul(value, NULL, 0);
    800         ret = ret | 0x2;
    801     }
    802     err = str_parms_get_str(parms, "cal_caltype", value, sizeof(value));
    803     if (err >= 0) {
    804         str_parms_del(parms, "cal_caltype");
    805         cal->cal_type = (uint32_t)strtoul(value, NULL, 0);
    806         ret = ret | 0x4;
    807     }
    808     err = str_parms_get_str(parms, "cal_samplerate", value, sizeof(value));
    809     if (err >= 0) {
    810         str_parms_del(parms, "cal_samplerate");
    811         cal->sampling_rate = (uint32_t)strtoul(value, NULL, 0);
    812         ret = ret | 0x8;
    813     }
    814     err = str_parms_get_str(parms, "cal_devid", value, sizeof(value));
    815     if (err >= 0) {
    816         str_parms_del(parms, "cal_devid");
    817         cal->dev_id = (uint32_t)strtoul(value, NULL, 0);
    818         ret = ret | 0x10;
    819     }
    820     err = str_parms_get_str(parms, "cal_snddevid", value, sizeof(value));
    821     if (err >= 0) {
    822         str_parms_del(parms, "cal_snddevid");
    823         cal->snd_dev_id = (uint32_t)strtoul(value, NULL, 0);
    824         ret = ret | 0x20;
    825     }
    826     err = str_parms_get_str(parms, "cal_topoid", value, sizeof(value));
    827     if (err >= 0) {
    828         str_parms_del(parms, "cal_topoid");
    829         cal->topo_id = (uint32_t)strtoul(value, NULL, 0);
    830         ret = ret | 0x40;
    831     }
    832     err = str_parms_get_str(parms, "cal_moduleid", value, sizeof(value));
    833     if (err >= 0) {
    834         str_parms_del(parms, "cal_moduleid");
    835         cal->module_id = (uint32_t)strtoul(value, NULL, 0);
    836         ret = ret | 0x80;
    837     }
    838     err = str_parms_get_str(parms, "cal_paramid", value, sizeof(value));
    839     if (err >= 0) {
    840         str_parms_del(parms, "cal_paramid");
    841         cal->param_id = (uint32_t)strtoul(value, NULL, 0);
    842         ret = ret | 0x100;
    843     }
    844     return ret;
    845 }
    846 
    847 static void set_audiocal(void *platform, struct str_parms *parms, char *value, int len)
    848 {
    849     struct platform_data *my_data = (struct platform_data *)platform;
    850     acdb_audio_cal_cfg_t cal;
    851     uint8_t *dptr = NULL;
    852     int32_t dlen = 0;
    853     int err ,ret;
    854 
    855     if (value == NULL || platform == NULL || parms == NULL) {
    856         ALOGE("[%s] received null pointer, failed", __func__);
    857         goto done_key_audcal;
    858     }
    859 
    860     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
    861     /* parse audio calibration keys */
    862     ret = parse_audiocal_cfg(parms, &cal);
    863 
    864     /* handle audio calibration data now */
    865     err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_AUD_CALDATA, value, len);
    866     if (err >= 0) {
    867         str_parms_del(parms, AUDIO_PARAMETER_KEY_AUD_CALDATA);
    868         dlen = strlen(value);
    869         if (dlen <= 0) {
    870             ALOGE("[%s] null data received", __func__);
    871             goto done_key_audcal;
    872         }
    873         /*
    874            The base64 encoded string is always larger than the binary data,
    875            so b64_pton will always output less data than provided (around 1/3
    876            less than the input data). That's why we can allocate input buffer
    877            length and then get function work.
    878         */
    879         dptr = (uint8_t *)calloc(dlen, sizeof(uint8_t));
    880         if (dptr == NULL) {
    881             ALOGE("[%s] memory allocation failed for %d", __func__, dlen);
    882             goto done_key_audcal;
    883         }
    884         dlen = b64_pton(value, dptr, dlen);
    885         if (dlen <= 0) {
    886             ALOGE("[%s] data decoding failed %d", __func__, dlen);
    887             goto done_key_audcal;
    888         }
    889 
    890         if (cal.dev_id) {
    891             if (audio_is_input_device(cal.dev_id)) {
    892                 cal.snd_dev_id = platform_get_input_snd_device(platform, cal.dev_id);
    893             } else {
    894                 cal.snd_dev_id = platform_get_output_snd_device(platform, cal.dev_id);
    895             }
    896         }
    897         cal.acdb_dev_id = platform_get_snd_device_acdb_id(cal.snd_dev_id);
    898         ALOGD("Setting audio calibration for snd_device(%d) acdb_id(%d)",
    899                 cal.snd_dev_id, cal.acdb_dev_id);
    900         if (cal.acdb_dev_id == -EINVAL) {
    901             ALOGE("[%s] Invalid acdb_device id %d for snd device id %d",
    902                        __func__, cal.acdb_dev_id, cal.snd_dev_id);
    903             goto done_key_audcal;
    904         }
    905         if (my_data->acdb_set_audio_cal) {
    906             ret = my_data->acdb_set_audio_cal((void *)&cal, (void *)dptr, dlen);
    907         }
    908     }
    909 done_key_audcal:
    910     if (dptr != NULL)
    911         free(dptr);
    912 }
    913 
    914 bool platform_send_gain_dep_cal(void *platform, int level)
    915 {
    916     bool ret_val = false;
    917     struct platform_data *my_data = (struct platform_data *)platform;
    918     struct audio_device *adev = my_data->adev;
    919     int acdb_dev_id, app_type;
    920     int acdb_dev_type = MSM_SNDDEV_CAP_RX;
    921     int mode = CAL_MODE_RTAC;
    922     struct listnode *node;
    923     struct audio_usecase *usecase;
    924     bool valid_uc_type;
    925     bool valid_dev;
    926 
    927     if (my_data->acdb_send_gain_dep_cal == NULL) {
    928         ALOGE("%s: dlsym error for acdb_send_gain_dep_cal", __func__);
    929         return ret_val;
    930     }
    931 
    932     if (!voice_is_in_call(adev)) {
    933         ALOGV("%s: Not Voice call usecase, apply new cal for level %d",
    934                __func__, level);
    935 
    936         // find the current active sound device
    937         list_for_each(node, &adev->usecase_list) {
    938             usecase = node_to_item(node, struct audio_usecase, list);
    939             LOG_ALWAYS_FATAL_IF(usecase == NULL,
    940                                 "unxpected NULL usecase in usecase_list");
    941             valid_uc_type = usecase->type == PCM_PLAYBACK;
    942             valid_dev = false;
    943             if (valid_uc_type) {
    944                 audio_devices_t dev = usecase->stream.out->devices;
    945                 valid_dev = (dev == AUDIO_DEVICE_OUT_SPEAKER ||
    946                              dev == AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
    947                              dev == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
    948                              dev == AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
    949             }
    950             if (valid_dev) {
    951                 ALOGV("%s: out device is %d", __func__,  usecase->out_snd_device);
    952                 if (platform_supports_app_type_cfg())
    953                     app_type = usecase->stream.out->app_type_cfg.app_type;
    954                 else
    955                     app_type = DEFAULT_APP_TYPE_RX_PATH;
    956 
    957                 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
    958 
    959                 if (!my_data->acdb_send_gain_dep_cal(acdb_dev_id, app_type,
    960                                                      acdb_dev_type, mode, level)) {
    961                     // set ret_val true if at least one calibration is set successfully
    962                     ret_val = true;
    963                 } else {
    964                     ALOGE("%s: my_data->acdb_send_gain_dep_cal failed ", __func__);
    965                 }
    966             } else {
    967                 ALOGW("%s: Usecase list is empty", __func__);
    968             }
    969         }
    970     } else {
    971         ALOGW("%s: Voice call in progress .. ignore setting new cal",
    972               __func__);
    973     }
    974     return ret_val;
    975 }
    976 
    977 void platform_set_echo_reference(struct audio_device *adev, bool enable, audio_devices_t out_device)
    978 {
    979     struct platform_data *my_data = (struct platform_data *)adev->platform;
    980     snd_device_t snd_device = SND_DEVICE_NONE;
    981 
    982     if (strcmp(my_data->ec_ref_mixer_path, "")) {
    983         ALOGV("%s: diabling %s", __func__, my_data->ec_ref_mixer_path);
    984         audio_route_reset_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
    985     }
    986 
    987     if (enable) {
    988         strcpy(my_data->ec_ref_mixer_path, "echo-reference");
    989         if (out_device != AUDIO_DEVICE_NONE) {
    990             snd_device = platform_get_output_snd_device(adev->platform, out_device);
    991             platform_add_backend_name(adev->platform, my_data->ec_ref_mixer_path, snd_device);
    992         }
    993 
    994         ALOGV("%s: enabling %s", __func__, my_data->ec_ref_mixer_path);
    995         audio_route_apply_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
    996     }
    997 }
    998 
    999 static struct csd_data *open_csd_client(bool i2s_ext_modem)
   1000 {
   1001     struct csd_data *csd = calloc(1, sizeof(struct csd_data));
   1002 
   1003     csd->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
   1004     if (csd->csd_client == NULL) {
   1005         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
   1006         goto error;
   1007     } else {
   1008         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
   1009 
   1010         csd->deinit = (deinit_t)dlsym(csd->csd_client,
   1011                                              "csd_client_deinit");
   1012         if (csd->deinit == NULL) {
   1013             ALOGE("%s: dlsym error %s for csd_client_deinit", __func__,
   1014                   dlerror());
   1015             goto error;
   1016         }
   1017         csd->disable_device = (disable_device_t)dlsym(csd->csd_client,
   1018                                              "csd_client_disable_device");
   1019         if (csd->disable_device == NULL) {
   1020             ALOGE("%s: dlsym error %s for csd_client_disable_device",
   1021                   __func__, dlerror());
   1022             goto error;
   1023         }
   1024         csd->enable_device_config = (enable_device_config_t)dlsym(csd->csd_client,
   1025                                                "csd_client_enable_device_config");
   1026         if (csd->enable_device_config == NULL) {
   1027             ALOGE("%s: dlsym error %s for csd_client_enable_device_config",
   1028                   __func__, dlerror());
   1029             goto error;
   1030         }
   1031         csd->enable_device = (enable_device_t)dlsym(csd->csd_client,
   1032                                              "csd_client_enable_device");
   1033         if (csd->enable_device == NULL) {
   1034             ALOGE("%s: dlsym error %s for csd_client_enable_device",
   1035                   __func__, dlerror());
   1036             goto error;
   1037         }
   1038         csd->start_voice = (start_voice_t)dlsym(csd->csd_client,
   1039                                              "csd_client_start_voice");
   1040         if (csd->start_voice == NULL) {
   1041             ALOGE("%s: dlsym error %s for csd_client_start_voice",
   1042                   __func__, dlerror());
   1043             goto error;
   1044         }
   1045         csd->stop_voice = (stop_voice_t)dlsym(csd->csd_client,
   1046                                              "csd_client_stop_voice");
   1047         if (csd->stop_voice == NULL) {
   1048             ALOGE("%s: dlsym error %s for csd_client_stop_voice",
   1049                   __func__, dlerror());
   1050             goto error;
   1051         }
   1052         csd->volume = (volume_t)dlsym(csd->csd_client,
   1053                                              "csd_client_volume");
   1054         if (csd->volume == NULL) {
   1055             ALOGE("%s: dlsym error %s for csd_client_volume",
   1056                   __func__, dlerror());
   1057             goto error;
   1058         }
   1059         csd->mic_mute = (mic_mute_t)dlsym(csd->csd_client,
   1060                                              "csd_client_mic_mute");
   1061         if (csd->mic_mute == NULL) {
   1062             ALOGE("%s: dlsym error %s for csd_client_mic_mute",
   1063                   __func__, dlerror());
   1064             goto error;
   1065         }
   1066         csd->slow_talk = (slow_talk_t)dlsym(csd->csd_client,
   1067                                              "csd_client_slow_talk");
   1068         if (csd->slow_talk == NULL) {
   1069             ALOGE("%s: dlsym error %s for csd_client_slow_talk",
   1070                   __func__, dlerror());
   1071             goto error;
   1072         }
   1073         csd->start_playback = (start_playback_t)dlsym(csd->csd_client,
   1074                                              "csd_client_start_playback");
   1075         if (csd->start_playback == NULL) {
   1076             ALOGE("%s: dlsym error %s for csd_client_start_playback",
   1077                   __func__, dlerror());
   1078             goto error;
   1079         }
   1080         csd->stop_playback = (stop_playback_t)dlsym(csd->csd_client,
   1081                                              "csd_client_stop_playback");
   1082         if (csd->stop_playback == NULL) {
   1083             ALOGE("%s: dlsym error %s for csd_client_stop_playback",
   1084                   __func__, dlerror());
   1085             goto error;
   1086         }
   1087         csd->start_record = (start_record_t)dlsym(csd->csd_client,
   1088                                              "csd_client_start_record");
   1089         if (csd->start_record == NULL) {
   1090             ALOGE("%s: dlsym error %s for csd_client_start_record",
   1091                   __func__, dlerror());
   1092             goto error;
   1093         }
   1094         csd->stop_record = (stop_record_t)dlsym(csd->csd_client,
   1095                                              "csd_client_stop_record");
   1096         if (csd->stop_record == NULL) {
   1097             ALOGE("%s: dlsym error %s for csd_client_stop_record",
   1098                   __func__, dlerror());
   1099             goto error;
   1100         }
   1101 
   1102         csd->get_sample_rate = (get_sample_rate_t)dlsym(csd->csd_client,
   1103                                              "csd_client_get_sample_rate");
   1104         if (csd->get_sample_rate == NULL) {
   1105             ALOGE("%s: dlsym error %s for csd_client_get_sample_rate",
   1106                   __func__, dlerror());
   1107 
   1108             goto error;
   1109         }
   1110 
   1111         csd->init = (init_t)dlsym(csd->csd_client, "csd_client_init");
   1112 
   1113         if (csd->init == NULL) {
   1114             ALOGE("%s: dlsym error %s for csd_client_init",
   1115                   __func__, dlerror());
   1116             goto error;
   1117         } else {
   1118             csd->init(i2s_ext_modem);
   1119         }
   1120     }
   1121     return csd;
   1122 
   1123 error:
   1124     free(csd);
   1125     csd = NULL;
   1126     return csd;
   1127 }
   1128 
   1129 void close_csd_client(struct csd_data *csd)
   1130 {
   1131     if (csd != NULL) {
   1132         csd->deinit();
   1133         dlclose(csd->csd_client);
   1134         free(csd);
   1135         csd = NULL;
   1136     }
   1137 }
   1138 
   1139 static void platform_csd_init(struct platform_data *my_data)
   1140 {
   1141 #ifdef PLATFORM_MSM8084
   1142     int32_t modems, (*count_modems)(void);
   1143     const char *name = "libdetectmodem.so";
   1144     const char *func = "count_modems";
   1145     const char *error;
   1146 
   1147     my_data->csd = NULL;
   1148 
   1149     void *lib = dlopen(name, RTLD_NOW);
   1150     error = dlerror();
   1151     if (!lib) {
   1152         ALOGE("%s: could not find %s: %s", __func__, name, error);
   1153         return;
   1154     }
   1155 
   1156     count_modems = NULL;
   1157     *(void **)(&count_modems) = dlsym(lib, func);
   1158     error = dlerror();
   1159     if (!count_modems) {
   1160         ALOGE("%s: could not find symbol %s in %s: %s",
   1161               __func__, func, name, error);
   1162         goto done;
   1163     }
   1164 
   1165     modems = count_modems();
   1166     if (modems < 0) {
   1167         ALOGE("%s: count_modems failed\n", __func__);
   1168         goto done;
   1169     }
   1170 
   1171     ALOGD("%s: num_modems %d\n", __func__, modems);
   1172     if (modems > 0)
   1173         my_data->csd = open_csd_client(false /*is_i2s_ext_modem*/);
   1174 
   1175 done:
   1176     dlclose(lib);
   1177 #else
   1178      my_data->csd = NULL;
   1179 #endif
   1180 }
   1181 
   1182 static void set_platform_defaults(struct platform_data * my_data)
   1183 {
   1184     int32_t dev;
   1185     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
   1186         backend_tag_table[dev] = NULL;
   1187         hw_interface_table[dev] = NULL;
   1188         operator_specific_device_table[dev] = NULL;
   1189     }
   1190 
   1191     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
   1192         backend_bit_width_table[dev] = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   1193     }
   1194 
   1195     // To overwrite these go to the audio_platform_info.xml file.
   1196     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("bt-sco");
   1197     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("bt-sco");
   1198     backend_tag_table[SND_DEVICE_OUT_BT_SCO] = strdup("bt-sco");
   1199     backend_tag_table[SND_DEVICE_OUT_HDMI] = strdup("hdmi");
   1200     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("speaker-and-hdmi");
   1201     backend_tag_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("bt-sco-wb");
   1202     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("bt-sco-wb");
   1203     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("bt-sco-wb");
   1204     backend_tag_table[SND_DEVICE_OUT_VOICE_TX] = strdup("afe-proxy");
   1205     backend_tag_table[SND_DEVICE_IN_VOICE_RX] = strdup("afe-proxy");
   1206 
   1207     backend_tag_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("usb-headset");
   1208     backend_tag_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("usb-headset");
   1209     backend_tag_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("usb-headphones");
   1210     backend_tag_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("usb-headphones");
   1211     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] =
   1212         strdup("speaker-and-usb-headphones");
   1213     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] =
   1214         strdup("speaker-safe-and-usb-headphones");
   1215     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] =
   1216         strdup("speaker-safe-and-bt-sco"),
   1217     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] =
   1218         strdup("speaker-safe-and-bt-sco-wb"),
   1219     backend_tag_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("usb-headset-mic");
   1220     backend_tag_table[SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = strdup("usb-headset-mic");
   1221     backend_tag_table[SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = strdup("usb-headset-mic");
   1222     backend_tag_table[SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = strdup("usb-headset-mic");
   1223     backend_tag_table[SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = strdup("usb-headset-mic");
   1224     backend_tag_table[SND_DEVICE_OUT_BT_A2DP] = strdup("bt-a2dp");
   1225     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = strdup("speaker-and-bt-a2dp");
   1226     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = strdup("speaker-safe-and-bt-a2dp");
   1227     backend_tag_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("usb-headset");
   1228 
   1229     hw_interface_table[SND_DEVICE_OUT_HANDSET] = strdup("SLIMBUS_0_RX");
   1230     hw_interface_table[SND_DEVICE_OUT_SPEAKER] = strdup("SLIMBUS_0_RX");
   1231     hw_interface_table[SND_DEVICE_OUT_SPEAKER_REVERSE] = strdup("SLIMBUS_0_RX");
   1232     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE] = strdup("SLIMBUS_0_RX");
   1233     hw_interface_table[SND_DEVICE_OUT_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1234     hw_interface_table[SND_DEVICE_OUT_LINE] = strdup("SLIMBUS_0_RX");
   1235     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1236     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1237     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_LINE] = strdup("SLIMBUS_0_RX");
   1238     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = strdup("SLIMBUS_0_RX");
   1239     hw_interface_table[SND_DEVICE_OUT_VOICE_HANDSET] = strdup("SLIMBUS_0_RX");
   1240     hw_interface_table[SND_DEVICE_OUT_VOICE_HAC_HANDSET] = strdup("SLIMBUS_0_RX");
   1241     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER] = strdup("SLIMBUS_0_RX");
   1242     hw_interface_table[SND_DEVICE_OUT_VOICE_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1243     hw_interface_table[SND_DEVICE_OUT_VOICE_LINE] = strdup("SLIMBUS_0_RX");
   1244     hw_interface_table[SND_DEVICE_OUT_HDMI] = strdup("HDMI_RX");
   1245     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("SLIMBUS_0_RX-and-HDMI_RX");
   1246     hw_interface_table[SND_DEVICE_OUT_BT_SCO] = strdup("SEC_AUX_PCM_RX");
   1247     hw_interface_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("SEC_AUX_PCM_RX");
   1248     hw_interface_table[SND_DEVICE_OUT_BT_A2DP] = strdup("SLIMBUS_7_RX");
   1249     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] =
   1250         strdup("SLIMBUS_0_RX-and-SLIMBUS_7_RX");
   1251     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] =
   1252         strdup("SLIMBUS_0_RX-and-SLIMBUS_7_RX");
   1253     hw_interface_table[SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = strdup("SLIMBUS_0_RX");
   1254     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1255     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = strdup("SLIMBUS_0_RX");
   1256     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = strdup("SLIMBUS_0_RX");
   1257     hw_interface_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("USB_AUDIO_RX");
   1258     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = strdup("USB_AUDIO_RX");
   1259     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = strdup("USB_AUDIO_RX");
   1260     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("USB_AUDIO_RX");
   1261     hw_interface_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
   1262     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
   1263     hw_interface_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("USB_AUDIO_RX");
   1264     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
   1265     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
   1266     hw_interface_table[SND_DEVICE_OUT_VOICE_TX] = strdup("AFE_PCM_RX");
   1267     hw_interface_table[SND_DEVICE_OUT_SPEAKER_PROTECTED] = strdup("SLIMBUS_0_RX");
   1268     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = strdup("SLIMBUS_0_RX");
   1269     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = strdup("SLIMBUS_0_RX");
   1270     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_SCO] = strdup("SLIMBUS_0_RX-and-SEC_AUX_PCM_RX");
   1271     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB] = strdup("SLIMBUS_0_RX-and-SEC_AUX_PCM_RX");
   1272     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = strdup("QUAT_TDM_RX_0-and-SLIMBUS_7_RX"),
   1273     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = strdup("QUAT_TDM_RX_0-and-SLIMBUS_7_RX"),
   1274     hw_interface_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
   1275     hw_interface_table[SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
   1276     hw_interface_table[SND_DEVICE_IN_USB_HEADSET_MIC_AEC] =  strdup("USB_AUDIO_TX");
   1277     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
   1278     hw_interface_table[SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
   1279     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = strdup("USB_AUDIO_TX");
   1280     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = strdup("USB_AUDIO_TX");
   1281     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
   1282     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_AEC] = strdup("SLIMBUS_0_TX");
   1283     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_NS] = strdup("SLIMBUS_0_TX");
   1284     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
   1285     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC] = strdup("SLIMBUS_0_TX");
   1286     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_AEC] = strdup("SLIMBUS_0_TX");
   1287     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_NS] = strdup("SLIMBUS_0_TX");
   1288     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = strdup("SLIMBUS_0_TX");
   1289     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
   1290     hw_interface_table[SND_DEVICE_IN_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1291     hw_interface_table[SND_DEVICE_IN_HEADSET_MIC_AEC] = strdup("SLIMBUS_0_TX");
   1292     hw_interface_table[SND_DEVICE_IN_CAMCORDER_MIC] = strdup("SLIMBUS_0_TX");
   1293     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC] = strdup("SLIMBUS_0_TX");
   1294     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_NS] = strdup("SLIMBUS_0_TX");
   1295     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_AEC] = strdup("SLIMBUS_0_TX");
   1296     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
   1297     hw_interface_table[SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
   1298     hw_interface_table[SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = strdup("SLIMBUS_0_TX");
   1299     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_MIC] = strdup("SLIMBUS_0_TX");
   1300     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1301     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = strdup("SLIMBUS_0_TX");
   1302     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = strdup("SLIMBUS_0_TX");
   1303     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = strdup("SLIMBUS_0_TX");
   1304     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC] = strdup("SLIMBUS_0_TX");
   1305     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_AEC] = strdup("SLIMBUS_0_TX");
   1306     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_NS] = strdup("SLIMBUS_0_TX");
   1307     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
   1308     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC] = strdup("SLIMBUS_0_TX");
   1309     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_AEC] = strdup("SLIMBUS_0_TX");
   1310     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_NS] = strdup("SLIMBUS_0_TX");
   1311     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = strdup("SLIMBUS_0_TX");
   1312     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
   1313     hw_interface_table[SND_DEVICE_IN_VOICE_DMIC] = strdup("SLIMBUS_0_TX");
   1314     hw_interface_table[SND_DEVICE_IN_VOICE_DMIC_TMUS] = strdup("SLIMBUS_0_TX");
   1315     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_MIC] = strdup("SLIMBUS_0_TX");
   1316     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = strdup("SLIMBUS_0_TX");
   1317     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = strdup("SLIMBUS_0_TX");
   1318     hw_interface_table[SND_DEVICE_IN_VOICE_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1319     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1320     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
   1321     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1322     hw_interface_table[SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
   1323     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("SEC_AUX_PCM_TX");
   1324     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("SEC_AUX_PCM_TX");
   1325     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("SEC_AUX_PCM_TX");
   1326     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("SEC_AUX_PCM_TX");
   1327     hw_interface_table[SND_DEVICE_IN_VOICE_RX] = strdup("AFE_PCM_TX");
   1328     hw_interface_table[SND_DEVICE_IN_THREE_MIC] = strdup("SLIMBUS_0_TX");
   1329     hw_interface_table[SND_DEVICE_IN_QUAD_MIC] = strdup("SLIMBUS_0_TX");
   1330     hw_interface_table[SND_DEVICE_IN_HANDSET_TMIC] = strdup("SLIMBUS_0_TX");
   1331     hw_interface_table[SND_DEVICE_IN_HANDSET_QMIC] = strdup("SLIMBUS_0_TX");
   1332     hw_interface_table[SND_DEVICE_IN_HANDSET_TMIC_AEC] = strdup("SLIMBUS_0_TX");
   1333     hw_interface_table[SND_DEVICE_IN_HANDSET_QMIC_AEC] = strdup("SLIMBUS_0_TX");
   1334     my_data->max_mic_count = PLATFORM_DEFAULT_MIC_COUNT;
   1335 }
   1336 
   1337 void get_cvd_version(char *cvd_version, struct audio_device *adev)
   1338 {
   1339     struct mixer_ctl *ctl;
   1340     int count;
   1341     int ret = 0;
   1342 
   1343     ctl = mixer_get_ctl_by_name(adev->mixer, CVD_VERSION_MIXER_CTL);
   1344     if (!ctl) {
   1345         ALOGE("%s: Could not get ctl for mixer cmd - %s",  __func__, CVD_VERSION_MIXER_CTL);
   1346         goto done;
   1347     }
   1348     mixer_ctl_update(ctl);
   1349 
   1350     count = mixer_ctl_get_num_values(ctl);
   1351     if (count > MAX_CVD_VERSION_STRING_SIZE)
   1352         count = MAX_CVD_VERSION_STRING_SIZE - 1;
   1353 
   1354     ret = mixer_ctl_get_array(ctl, cvd_version, count);
   1355     if (ret != 0) {
   1356         ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
   1357         goto done;
   1358     }
   1359 
   1360 done:
   1361     return;
   1362 }
   1363 
   1364 static int platform_acdb_init(void *platform)
   1365 {
   1366     struct platform_data *my_data = (struct platform_data *)platform;
   1367     struct audio_device *adev = my_data->adev;
   1368 
   1369     if (!my_data->acdb_init) {
   1370         ALOGE("%s: no acdb_init fn provided", __func__);
   1371         return -1;
   1372     }
   1373 
   1374     if (my_data->acdb_initialized) {
   1375         ALOGW("acdb is already initialized");
   1376         return 0;
   1377     }
   1378 
   1379 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
   1380     char *cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
   1381     if (!cvd_version)
   1382         ALOGE("failed to allocate cvd_version");
   1383     else {
   1384         get_cvd_version(cvd_version, adev);
   1385         my_data->acdb_init((char *)my_data->snd_card_name, cvd_version, 0);
   1386         free(cvd_version);
   1387     }
   1388 #elif defined (PLATFORM_MSM8084)
   1389     my_data->acdb_init((char *)my_data->snd_card_name);
   1390 #else
   1391     my_data->acdb_init();
   1392 #endif
   1393     my_data->acdb_initialized = true;
   1394     return 0;
   1395 }
   1396 
   1397 static void
   1398 platform_backend_config_init(struct platform_data *pdata)
   1399 {
   1400     int i;
   1401 
   1402     /* initialize backend config */
   1403     for (i = 0; i < MAX_CODEC_BACKENDS; i++) {
   1404         pdata->current_backend_cfg[i].sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   1405         pdata->current_backend_cfg[i].bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   1406         pdata->current_backend_cfg[i].channels = CODEC_BACKEND_DEFAULT_CHANNELS;
   1407 
   1408         if (i > MAX_RX_CODEC_BACKENDS)
   1409             pdata->current_backend_cfg[i].channels = CODEC_BACKEND_DEFAULT_TX_CHANNELS;
   1410 
   1411         pdata->current_backend_cfg[i].bitwidth_mixer_ctl = NULL;
   1412         pdata->current_backend_cfg[i].samplerate_mixer_ctl = NULL;
   1413         pdata->current_backend_cfg[i].channels_mixer_ctl = NULL;
   1414     }
   1415 
   1416     pdata->current_backend_cfg[DEFAULT_CODEC_BACKEND].bitwidth_mixer_ctl =
   1417             strdup("SLIM_0_RX Format");
   1418     pdata->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
   1419             strdup("SLIM_0_RX SampleRate");
   1420 
   1421     pdata->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].bitwidth_mixer_ctl =
   1422             strdup("SLIM_0_TX Format");
   1423     pdata->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].samplerate_mixer_ctl =
   1424             strdup("SLIM_0_TX SampleRate");
   1425 
   1426     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].bitwidth_mixer_ctl =
   1427             strdup("USB_AUDIO_TX Format");
   1428     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].samplerate_mixer_ctl =
   1429             strdup("USB_AUDIO_TX SampleRate");
   1430     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].channels_mixer_ctl =
   1431             strdup("USB_AUDIO_TX Channels");
   1432 
   1433     pdata->current_backend_cfg[HEADPHONE_BACKEND].bitwidth_mixer_ctl =
   1434             strdup("SLIM_6_RX Format");
   1435     pdata->current_backend_cfg[HEADPHONE_BACKEND].samplerate_mixer_ctl =
   1436             strdup("SLIM_6_RX SampleRate");
   1437 
   1438     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].bitwidth_mixer_ctl =
   1439             strdup("USB_AUDIO_RX Format");
   1440     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].samplerate_mixer_ctl =
   1441             strdup("USB_AUDIO_RX SampleRate");
   1442 
   1443     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].channels = 1;
   1444     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].channels_mixer_ctl =
   1445             strdup("USB_AUDIO_RX Channels");
   1446 }
   1447 
   1448 static int
   1449 platform_backend_app_type_cfg_init(struct platform_data *pdata,
   1450                                    struct mixer *mixer)
   1451 {
   1452     size_t app_type_cfg[128] = {0};
   1453     int length, num_app_types = 0;
   1454     struct mixer_ctl *ctl = NULL;
   1455 
   1456     const char *mixer_ctl_name = "App Type Config";
   1457     ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
   1458     if (!ctl) {
   1459         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
   1460         return -1;
   1461     }
   1462 
   1463     length = 1; // reserve index 0 for number of app types
   1464 
   1465     struct listnode *node;
   1466     struct app_type_entry *entry;
   1467     list_for_each(node, &app_type_entry_list) {
   1468         entry = node_to_item(node, struct app_type_entry, node);
   1469         app_type_cfg[length++] = entry->app_type;
   1470         app_type_cfg[length++] = entry->max_rate;
   1471         app_type_cfg[length++] = entry->bit_width;
   1472         ALOGI("%s add entry %d %d", __func__, entry->app_type, entry->bit_width);
   1473         num_app_types += 1;
   1474     }
   1475 
   1476     // default for capture
   1477     int t;
   1478     platform_get_default_app_type_v2(pdata,
   1479                                      PCM_CAPTURE,
   1480                                      &t);
   1481     app_type_cfg[length++] = t;
   1482     app_type_cfg[length++] = 48000;
   1483     app_type_cfg[length++] = 16;
   1484     num_app_types += 1;
   1485 
   1486     if (num_app_types) {
   1487         app_type_cfg[0] = num_app_types;
   1488         if (mixer_ctl_set_array(ctl, app_type_cfg, length) < 0) {
   1489             ALOGE("Failed to set app type cfg");
   1490         }
   1491     }
   1492     return 0;
   1493 }
   1494 
   1495 static void configure_flicker_sensor_input(struct mixer *mixer)
   1496 {
   1497     struct mixer_ctl *ctl;
   1498     const char* ctl1 = "AIF3_CAP Mixer SLIM TX2";
   1499     int setting1 = 1;
   1500     const char* ctl2 = "CDC_IF TX2 MUX";
   1501     const char* setting2 = "DEC2";
   1502     const char* ctl3 = "SLIM_1_TX Channels";
   1503     const char* setting3 = "One";
   1504     const char* ctl4 = "ADC MUX2";
   1505     const char* setting4 = "AMIC";
   1506     const char* ctl5 = "AMIC MUX2";
   1507     const char* setting5 = "ADC1";
   1508     const char* ctl6 = "DEC2 Volume";
   1509     int setting6 = 84;
   1510     const char* ctl7 = "MultiMedia9 Mixer SLIM_1_TX";
   1511     int setting7 = 1;
   1512     const char* ctl8 = "SLIM_1_TX SampleRate";
   1513     const char* setting8 = "KHZ_8";
   1514 
   1515     ctl = mixer_get_ctl_by_name(mixer, ctl1);
   1516     mixer_ctl_set_value(ctl, 0, setting1);
   1517     ctl = mixer_get_ctl_by_name(mixer, ctl2);
   1518     mixer_ctl_set_enum_by_string(ctl, setting2);
   1519     ctl = mixer_get_ctl_by_name(mixer, ctl3);
   1520     mixer_ctl_set_enum_by_string(ctl, setting3);
   1521     ctl = mixer_get_ctl_by_name(mixer, ctl4);
   1522     mixer_ctl_set_enum_by_string(ctl, setting4);
   1523     ctl = mixer_get_ctl_by_name(mixer, ctl5);
   1524     mixer_ctl_set_enum_by_string(ctl, setting5);
   1525     ctl = mixer_get_ctl_by_name(mixer, ctl6);
   1526     mixer_ctl_set_value(ctl, 0, setting6);
   1527     ctl = mixer_get_ctl_by_name(mixer, ctl7);
   1528     mixer_ctl_set_value(ctl, 0, setting7);
   1529     ctl = mixer_get_ctl_by_name(mixer, ctl8);
   1530     mixer_ctl_set_enum_by_string(ctl, setting8);
   1531 }
   1532 
   1533 void *platform_init(struct audio_device *adev)
   1534 {
   1535     char value[PROPERTY_VALUE_MAX];
   1536     struct platform_data *my_data = NULL;
   1537     int retry_num = 0, snd_card_num = 0, key = 0, ret = 0;
   1538     bool dual_mic_config = false, use_default_mixer_path = true;
   1539     const char *snd_card_name;
   1540     char *cvd_version = NULL;
   1541     char *snd_internal_name = NULL;
   1542     char *tmp = NULL;
   1543     char mixer_xml_file[MIXER_PATH_MAX_LENGTH]= {0};
   1544     char platform_info_file[MIXER_PATH_MAX_LENGTH]= {0};
   1545     struct snd_card_split *snd_split_handle = NULL;
   1546     my_data = calloc(1, sizeof(struct platform_data));
   1547 
   1548     my_data->adev = adev;
   1549 
   1550     list_init(&operator_info_list);
   1551     list_init(&app_type_entry_list);
   1552 
   1553     set_platform_defaults(my_data);
   1554 
   1555     // audio_extn_utils_get_snd_card_num does
   1556     // - open mixer and get snd card name
   1557     // - parse platform info xml file and check for valid snd card name
   1558     // - on failure loop through all the active snd card
   1559 
   1560     snd_card_num = audio_extn_utils_get_snd_card_num();
   1561     if (-1 == snd_card_num) {
   1562         ALOGE("%s: invalid sound card number (-1), bailing out ", __func__);
   1563         goto init_failed;
   1564     }
   1565 
   1566     adev->mixer = mixer_open(snd_card_num);
   1567     snd_card_name = mixer_get_name(adev->mixer);
   1568     my_data->hw_info = hw_info_init(snd_card_name);
   1569 
   1570     audio_extn_set_snd_card_split(snd_card_name);
   1571     snd_split_handle = audio_extn_get_snd_card_split();
   1572 
   1573     /* Get the codec internal name from the sound card and/or form factor
   1574      * name and form the mixer paths and platfor info file name dynamically.
   1575      * This is generic way of picking any codec and forma factor name based
   1576      * mixer and platform info files in future with no code change.
   1577 
   1578      * current code extends and looks for any of the exteneded mixer path and
   1579      * platform info file present based on codec and form factor.
   1580 
   1581      * order of picking appropriate file is
   1582      * <i>   mixer_paths_<codec_name>_<form_factor>.xml, if file not present
   1583      * <ii>  mixer_paths_<codec_name>.xml, if file not present
   1584      * <iii> mixer_paths.xml
   1585 
   1586      * same order is followed for audio_platform_info.xml as well
   1587      */
   1588 
   1589     // need to carryforward old file name
   1590     if (!strncmp(snd_card_name, TOMTOM_8226_SND_CARD_NAME,
   1591                  min(strlen(TOMTOM_8226_SND_CARD_NAME), strlen(snd_card_name)))) {
   1592         snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s.xml",
   1593                          MIXER_XML_BASE_STRING, TOMTOM_MIXER_FILE_SUFFIX );
   1594     } else {
   1595 
   1596         snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s_%s.xml",
   1597                          MIXER_XML_BASE_STRING, snd_split_handle->snd_card,
   1598                          snd_split_handle->form_factor);
   1599         if (!audio_extn_utils_resolve_config_file(mixer_xml_file)) {
   1600             memset(mixer_xml_file, 0, sizeof(mixer_xml_file));
   1601             snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s.xml",
   1602                          MIXER_XML_BASE_STRING, snd_split_handle->snd_card);
   1603 
   1604             if (!audio_extn_utils_resolve_config_file(mixer_xml_file)) {
   1605                 memset(mixer_xml_file, 0, sizeof(mixer_xml_file));
   1606                 strlcpy(mixer_xml_file, MIXER_XML_DEFAULT_PATH, MIXER_PATH_MAX_LENGTH);
   1607                 audio_extn_utils_resolve_config_file(mixer_xml_file);
   1608             }
   1609         }
   1610     }
   1611 
   1612     audio_extn_utils_get_platform_info(snd_card_name, platform_info_file);
   1613 
   1614     my_data->declared_mic_count = 0;
   1615     /* Initialize platform specific ids and/or backends*/
   1616     platform_info_init(platform_info_file, my_data);
   1617 
   1618     ALOGD("%s: Loading mixer file: %s", __func__, mixer_xml_file);
   1619     adev->audio_route = audio_route_init(snd_card_num, mixer_xml_file);
   1620 
   1621     if (!adev->audio_route) {
   1622         ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
   1623         mixer_close(adev->mixer);
   1624         adev->mixer = NULL;
   1625         hw_info_deinit(my_data->hw_info);
   1626         my_data->hw_info = NULL;
   1627         goto init_failed;
   1628     }
   1629     adev->snd_card = snd_card_num;
   1630     ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
   1631 
   1632     //set max volume step for voice call
   1633     property_get("ro.config.vc_call_vol_steps", value, TOSTRING(MAX_VOL_INDEX));
   1634     my_data->max_vol_index = atoi(value);
   1635 
   1636     property_get("persist.audio.dualmic.config",value,"");
   1637     if (!strcmp("endfire", value)) {
   1638         dual_mic_config = true;
   1639     }
   1640 
   1641     my_data->source_mic_type = 0;
   1642 
   1643     my_data->fluence_in_spkr_mode = false;
   1644     my_data->fluence_in_voice_call = false;
   1645     my_data->fluence_in_voice_comm = false;
   1646     my_data->fluence_in_voice_rec = false;
   1647 
   1648     property_get("ro.qc.sdk.audio.fluencetype", value, "none");
   1649     if (!strcmp("fluencepro", value)) {
   1650         my_data->fluence_type = FLUENCE_PRO_ENABLE;
   1651     } else if (!strcmp("fluence", value) || (dual_mic_config)) {
   1652         my_data->fluence_type = FLUENCE_ENABLE;
   1653     } else if (!strcmp("none", value)) {
   1654         my_data->fluence_type = FLUENCE_DISABLE;
   1655     }
   1656 
   1657     if (my_data->fluence_type != FLUENCE_DISABLE) {
   1658         property_get("persist.audio.fluence.voicecall",value,"");
   1659         if (!strcmp("true", value)) {
   1660             my_data->fluence_in_voice_call = true;
   1661         }
   1662 
   1663         property_get("persist.audio.fluence.voicecomm",value,"");
   1664         if (!strcmp("true", value)) {
   1665             my_data->fluence_in_voice_comm = true;
   1666         }
   1667 
   1668         property_get("persist.audio.fluence.voicerec",value,"");
   1669         if (!strcmp("true", value)) {
   1670             my_data->fluence_in_voice_rec = true;
   1671         }
   1672 
   1673         property_get("persist.audio.fluence.speaker",value,"");
   1674         if (!strcmp("true", value)) {
   1675             my_data->fluence_in_spkr_mode = true;
   1676         }
   1677     }
   1678 
   1679     // support max to mono, example if max count is 3, usecase supports Three, dual and mono mic
   1680     switch (my_data->max_mic_count) {
   1681         case 4:
   1682             my_data->source_mic_type |= SOURCE_QUAD_MIC;
   1683         case 3:
   1684             my_data->source_mic_type |= SOURCE_THREE_MIC;
   1685         case 2:
   1686             my_data->source_mic_type |= SOURCE_DUAL_MIC;
   1687         case 1:
   1688             my_data->source_mic_type |= SOURCE_MONO_MIC;
   1689             break;
   1690         default:
   1691             ALOGE("%s: max_mic_count (%d), is not supported, setting to default",
   1692                    __func__, my_data->max_mic_count);
   1693             my_data->source_mic_type = SOURCE_MONO_MIC|SOURCE_DUAL_MIC;
   1694             break;
   1695         }
   1696 
   1697     ALOGV("%s: Fluence_Type(%d) max_mic_count(%d) mic_type(0x%x) fluence_in_voice_call(%d)"
   1698           " fluence_in_voice_comm(%d) fluence_in_voice_rec(%d) fluence_in_spkr_mode(%d) ",
   1699           __func__, my_data->fluence_type, my_data->max_mic_count, my_data->source_mic_type,
   1700           my_data->fluence_in_voice_call, my_data->fluence_in_voice_comm,
   1701           my_data->fluence_in_voice_rec, my_data->fluence_in_spkr_mode);
   1702 
   1703     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
   1704     if (my_data->acdb_handle == NULL) {
   1705         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
   1706     } else {
   1707         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
   1708         my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
   1709                                                     "acdb_loader_deallocate_ACDB");
   1710         if (!my_data->acdb_deallocate)
   1711             ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
   1712                   __func__, LIB_ACDB_LOADER);
   1713 
   1714         my_data->acdb_send_audio_cal_v3 = (acdb_send_audio_cal_v3_t)dlsym(my_data->acdb_handle,
   1715                                                     "acdb_loader_send_audio_cal_v3");
   1716         if (!my_data->acdb_send_audio_cal_v3)
   1717             ALOGE("%s: Could not find the symbol acdb_send_audio_cal_v3 from %s",
   1718                   __func__, LIB_ACDB_LOADER);
   1719 
   1720         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
   1721                                                     "acdb_loader_send_audio_cal");
   1722         if (!my_data->acdb_send_audio_cal)
   1723             ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
   1724                   __func__, LIB_ACDB_LOADER);
   1725 
   1726         my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
   1727                                                     "acdb_loader_send_voice_cal");
   1728         if (!my_data->acdb_send_voice_cal)
   1729             ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
   1730                   __func__, LIB_ACDB_LOADER);
   1731 
   1732         my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
   1733                                                     "acdb_loader_reload_vocvoltable");
   1734         if (!my_data->acdb_reload_vocvoltable)
   1735             ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
   1736                   __func__, LIB_ACDB_LOADER);
   1737 
   1738         my_data->acdb_send_gain_dep_cal = (acdb_send_gain_dep_cal_t)dlsym(my_data->acdb_handle,
   1739                                                     "acdb_loader_send_gain_dep_cal");
   1740         if (!my_data->acdb_send_gain_dep_cal)
   1741             ALOGV("%s: Could not find the symbol acdb_loader_send_gain_dep_cal from %s",
   1742                   __func__, LIB_ACDB_LOADER);
   1743 
   1744 #if defined (FLICKER_SENSOR_INPUT)
   1745         configure_flicker_sensor_input(adev->mixer);
   1746 #endif
   1747 
   1748 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
   1749         acdb_init_v2_cvd_t acdb_init_local;
   1750         acdb_init_local = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
   1751                                               "acdb_loader_init_v2");
   1752         if (acdb_init_local == NULL)
   1753             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
   1754                   dlerror());
   1755 
   1756 #elif defined (PLATFORM_MSM8084)
   1757         acdb_init_v2_t acdb_init_local;
   1758         acdb_init_local = (acdb_init_v2_t)dlsym(my_data->acdb_handle,
   1759                                           "acdb_loader_init_v2");
   1760         if (acdb_init_local == NULL)
   1761             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
   1762                   dlerror());
   1763 
   1764 #else
   1765         acdb_init_t acdb_init_local;
   1766         acdb_init_local = (acdb_init_t)dlsym(my_data->acdb_handle,
   1767                                                     "acdb_loader_init_ACDB");
   1768         if (acdb_init_local == NULL)
   1769             ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__,
   1770                   dlerror());
   1771 #endif
   1772         my_data->acdb_init = acdb_init_local;
   1773 
   1774         my_data->acdb_send_custom_top = (acdb_send_custom_top_t)
   1775                                         dlsym(my_data->acdb_handle,
   1776                                               "acdb_loader_send_common_custom_topology");
   1777 
   1778         if (!my_data->acdb_send_custom_top)
   1779             ALOGE("%s: Could not find the symbol acdb_get_default_app_type from %s",
   1780                   __func__, LIB_ACDB_LOADER);
   1781 
   1782         my_data->acdb_set_audio_cal = (acdb_set_audio_cal_t)dlsym(my_data->acdb_handle,
   1783                                                     "acdb_loader_set_audio_cal_v2");
   1784         if (!my_data->acdb_set_audio_cal)
   1785             ALOGE("%s: Could not find the symbol acdb_set_audio_cal_v2 from %s",
   1786                   __func__, LIB_ACDB_LOADER);
   1787 
   1788         int result = acdb_init(adev->snd_card);
   1789         if (!result) {
   1790             my_data->acdb_initialized = true;
   1791             ALOGD("ACDB initialized");
   1792         } else {
   1793             my_data->acdb_initialized = false;
   1794             ALOGD("ACDB initialization failed");
   1795         }
   1796     }
   1797 
   1798     /* init usb */
   1799     audio_extn_usb_init(adev);
   1800 
   1801     /* init a2dp */
   1802     audio_extn_a2dp_init(adev);
   1803 
   1804     audio_extn_spkr_prot_init(adev);
   1805 
   1806     audio_extn_hwdep_cal_send(adev->snd_card, my_data->acdb_handle);
   1807 
   1808     /* load csd client */
   1809     platform_csd_init(my_data);
   1810 
   1811     platform_backend_config_init(my_data);
   1812 
   1813     init_be_dai_name_table(adev);
   1814 
   1815     if (platform_supports_app_type_cfg())
   1816         platform_backend_app_type_cfg_init(my_data, adev->mixer);
   1817 
   1818     return my_data;
   1819 
   1820 init_failed:
   1821     if (my_data)
   1822         free(my_data);
   1823     return NULL;
   1824 }
   1825 
   1826 void platform_deinit(void *platform)
   1827 {
   1828     int32_t dev;
   1829     struct operator_info *info_item;
   1830     struct operator_specific_device *device_item;
   1831     struct app_type_entry *ap;
   1832     struct listnode *node;
   1833 
   1834     struct platform_data *my_data = (struct platform_data *)platform;
   1835     close_csd_client(my_data->csd);
   1836 
   1837     hw_info_deinit(my_data->hw_info);
   1838 
   1839     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
   1840         if (backend_tag_table[dev])
   1841             free(backend_tag_table[dev]);
   1842         if (hw_interface_table[dev])
   1843             free(hw_interface_table[dev]);
   1844         if (operator_specific_device_table[dev]) {
   1845             while (!list_empty(operator_specific_device_table[dev])) {
   1846                 node = list_head(operator_specific_device_table[dev]);
   1847                 list_remove(node);
   1848                 device_item = node_to_item(node, struct operator_specific_device, list);
   1849                 free(device_item->operator);
   1850                 free(device_item->mixer_path);
   1851                 free(device_item);
   1852             }
   1853             free(operator_specific_device_table[dev]);
   1854         }
   1855     }
   1856 
   1857     if (my_data->snd_card_name)
   1858         free(my_data->snd_card_name);
   1859 
   1860     while (!list_empty(&operator_info_list)) {
   1861         node = list_head(&operator_info_list);
   1862         list_remove(node);
   1863         info_item = node_to_item(node, struct operator_info, list);
   1864         free(info_item->name);
   1865         free(info_item->mccmnc);
   1866         free(info_item);
   1867     }
   1868 
   1869     while (!list_empty(&app_type_entry_list)) {
   1870         node = list_head(&app_type_entry_list);
   1871         list_remove(node);
   1872         ap = node_to_item(node, struct app_type_entry, node);
   1873         if (ap->mode) free(ap->mode);
   1874         free(ap);
   1875     }
   1876 
   1877     mixer_close(my_data->adev->mixer);
   1878     free(platform);
   1879 
   1880     /* deinit usb */
   1881     audio_extn_usb_deinit();
   1882 }
   1883 
   1884 const char *platform_get_snd_device_name(snd_device_t snd_device)
   1885 {
   1886     if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
   1887         if (operator_specific_device_table[snd_device] != NULL) {
   1888             return get_operator_specific_device_mixer_path(snd_device);
   1889         }
   1890         return device_table[snd_device];
   1891     } else
   1892         return "none";
   1893 }
   1894 
   1895 int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
   1896                                       char *device_name)
   1897 {
   1898     struct platform_data *my_data = (struct platform_data *)platform;
   1899 
   1900     if (platform == NULL) {
   1901         ALOGW("%s: something wrong, use legacy get_snd_device name", __func__);
   1902         strlcpy(device_name, platform_get_snd_device_name(snd_device),
   1903                 DEVICE_NAME_MAX_SIZE);
   1904     } else if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
   1905         if (operator_specific_device_table[snd_device] != NULL) {
   1906             strlcpy(device_name, get_operator_specific_device_mixer_path(snd_device),
   1907                     DEVICE_NAME_MAX_SIZE);
   1908         } else {
   1909             strlcpy(device_name, device_table[snd_device], DEVICE_NAME_MAX_SIZE);
   1910         }
   1911         hw_info_append_hw_type(my_data->hw_info, snd_device, device_name);
   1912     } else {
   1913         strlcpy(device_name, "none", DEVICE_NAME_MAX_SIZE);
   1914         return -EINVAL;
   1915     }
   1916 
   1917     return 0;
   1918 }
   1919 
   1920 void platform_add_backend_name(void *platform, char *mixer_path,
   1921                                snd_device_t snd_device)
   1922 {
   1923     struct platform_data *my_data = (struct platform_data *)platform;
   1924 
   1925     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
   1926         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
   1927         return;
   1928     }
   1929 
   1930     const char * suffix = backend_tag_table[snd_device];
   1931 
   1932     if (suffix != NULL) {
   1933         strcat(mixer_path, " ");
   1934         strcat(mixer_path, suffix);
   1935     }
   1936 }
   1937 
   1938 bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
   1939 {
   1940     ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
   1941                 platform_get_snd_device_name(snd_device1),
   1942                 platform_get_snd_device_name(snd_device2));
   1943 
   1944     if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
   1945         ALOGE("%s: Invalid snd_device = %s", __func__,
   1946                 platform_get_snd_device_name(snd_device1));
   1947         return false;
   1948     }
   1949     if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
   1950         ALOGE("%s: Invalid snd_device = %s", __func__,
   1951                 platform_get_snd_device_name(snd_device2));
   1952         return false;
   1953     }
   1954 
   1955     const char * be_itf1 = hw_interface_table[snd_device1];
   1956     const char * be_itf2 = hw_interface_table[snd_device2];
   1957     /*
   1958       hw_interface_table has overrides for a snd_device.
   1959       if there is no entry for a device, assume DEFAULT_RX_BACKEND
   1960     */
   1961     if (be_itf1 == NULL) {
   1962         be_itf1 = DEFAULT_RX_BACKEND;
   1963     }
   1964     if (be_itf2 == NULL) {
   1965         be_itf2 = DEFAULT_RX_BACKEND;
   1966     }
   1967     ALOGV("%s: be_itf1 = %s, be_itf2 = %s", __func__, be_itf1, be_itf2);
   1968     /*
   1969       this takes care of finding a device within a combo device pair as well
   1970      */
   1971     return strstr(be_itf1, be_itf2) != NULL || strstr(be_itf2, be_itf1) != NULL;
   1972 }
   1973 
   1974 int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
   1975 {
   1976     int device_id;
   1977     if (device_type == PCM_PLAYBACK)
   1978         device_id = pcm_device_table[usecase][0];
   1979     else
   1980         device_id = pcm_device_table[usecase][1];
   1981     return device_id;
   1982 }
   1983 
   1984 static int find_index(const struct name_to_index * table, int32_t len,
   1985                       const char * name)
   1986 {
   1987     int ret = 0;
   1988     int32_t i;
   1989 
   1990     if (table == NULL) {
   1991         ALOGE("%s: table is NULL", __func__);
   1992         ret = -ENODEV;
   1993         goto done;
   1994     }
   1995 
   1996     if (name == NULL) {
   1997         ALOGE("null key");
   1998         ret = -ENODEV;
   1999         goto done;
   2000     }
   2001 
   2002     for (i=0; i < len; i++) {
   2003         if (!strcmp(table[i].name, name)) {
   2004             ret = table[i].index;
   2005             goto done;
   2006         }
   2007     }
   2008     ALOGE("%s: Could not find index for name = %s",
   2009             __func__, name);
   2010     ret = -ENODEV;
   2011 done:
   2012     return ret;
   2013 }
   2014 
   2015 int platform_get_snd_device_index(char *device_name)
   2016 {
   2017     return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
   2018 }
   2019 
   2020 int platform_get_usecase_index(const char *usecase_name)
   2021 {
   2022     return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
   2023 }
   2024 
   2025 void platform_add_operator_specific_device(snd_device_t snd_device,
   2026                                            const char *operator,
   2027                                            const char *mixer_path,
   2028                                            unsigned int acdb_id)
   2029 {
   2030     struct operator_specific_device *device;
   2031 
   2032     if (operator_specific_device_table[snd_device] == NULL) {
   2033         operator_specific_device_table[snd_device] =
   2034             (struct listnode *)calloc(1, sizeof(struct listnode));
   2035         list_init(operator_specific_device_table[snd_device]);
   2036     }
   2037 
   2038     device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
   2039 
   2040     device->operator = strdup(operator);
   2041     device->mixer_path = strdup(mixer_path);
   2042     device->acdb_id = acdb_id;
   2043 
   2044     list_add_tail(operator_specific_device_table[snd_device], &device->list);
   2045 
   2046     ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
   2047             platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
   2048 
   2049 }
   2050 
   2051 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
   2052 {
   2053     int ret = 0;
   2054 
   2055     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
   2056         ALOGE("%s: Invalid snd_device = %d",
   2057             __func__, snd_device);
   2058         ret = -EINVAL;
   2059         goto done;
   2060     }
   2061 
   2062     ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
   2063           platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
   2064     acdb_device_table[snd_device] = acdb_id;
   2065 done:
   2066     return ret;
   2067 }
   2068 
   2069 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
   2070 {
   2071     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
   2072         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
   2073         return -EINVAL;
   2074     }
   2075 
   2076     /*
   2077      * If speaker protection is enabled, function returns supported
   2078      * sound device for speaker. Else same sound device is returned.
   2079      */
   2080     snd_device = audio_extn_get_spkr_prot_snd_device(snd_device);
   2081 
   2082     if (operator_specific_device_table[snd_device] != NULL)
   2083         return get_operator_specific_device_acdb_id(snd_device);
   2084     else
   2085         return acdb_device_table[snd_device];
   2086 }
   2087 
   2088 static int platform_get_backend_index(snd_device_t snd_device)
   2089 {
   2090     int32_t port = DEFAULT_CODEC_BACKEND;
   2091 
   2092     if (snd_device >= SND_DEVICE_OUT_BEGIN && snd_device < SND_DEVICE_OUT_END) {
   2093         if (backend_tag_table[snd_device] != NULL) {
   2094                 if (strncmp(backend_tag_table[snd_device], "headphones",
   2095                             sizeof("headphones")) == 0)
   2096                         port = HEADPHONE_BACKEND;
   2097                 else if (strcmp(backend_tag_table[snd_device], "hdmi") == 0)
   2098                         port = HDMI_RX_BACKEND;
   2099                 else if ((strcmp(backend_tag_table[snd_device], "usb-headphones") == 0) ||
   2100                            (strcmp(backend_tag_table[snd_device], "usb-headset") == 0))
   2101                         port = USB_AUDIO_RX_BACKEND;
   2102         }
   2103     } else if (snd_device >= SND_DEVICE_IN_BEGIN && snd_device < SND_DEVICE_IN_END) {
   2104         port = DEFAULT_CODEC_TX_BACKEND;
   2105         if (backend_tag_table[snd_device] != NULL) {
   2106                 if (strcmp(backend_tag_table[snd_device], "usb-headset-mic") == 0)
   2107                         port = USB_AUDIO_TX_BACKEND;
   2108                 else if (strstr(backend_tag_table[snd_device], "bt-sco") != NULL)
   2109                         port = BT_SCO_TX_BACKEND;
   2110         }
   2111     } else {
   2112         ALOGW("%s:napb: Invalid device - %d ", __func__, snd_device);
   2113     }
   2114 
   2115     ALOGV("%s:napb: backend port - %d device - %d ", __func__, port, snd_device);
   2116 
   2117     return port;
   2118 }
   2119 
   2120 int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
   2121 {
   2122     struct platform_data *my_data = (struct platform_data *)platform;
   2123     int acdb_dev_id, acdb_dev_type;
   2124 
   2125     if (platform_supports_app_type_cfg()) // use v2 instead
   2126         return -ENOSYS;
   2127 
   2128     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
   2129     if (acdb_dev_id < 0) {
   2130         ALOGE("%s: Could not find acdb id for device(%d)",
   2131               __func__, snd_device);
   2132         return -EINVAL;
   2133     }
   2134     if (my_data->acdb_send_audio_cal) {
   2135         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
   2136               __func__, snd_device, acdb_dev_id);
   2137         if (snd_device >= SND_DEVICE_OUT_BEGIN &&
   2138                 snd_device < SND_DEVICE_OUT_END)
   2139             acdb_dev_type = ACDB_DEV_TYPE_OUT;
   2140         else
   2141             acdb_dev_type = ACDB_DEV_TYPE_IN;
   2142         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
   2143     }
   2144     return 0;
   2145 }
   2146 
   2147 int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
   2148                                        int app_type, int sample_rate)
   2149 {
   2150     struct platform_data *my_data = (struct platform_data *)platform;
   2151     int acdb_dev_id, acdb_dev_type;
   2152     int snd_device = usecase->out_snd_device;
   2153     int new_snd_device[SND_DEVICE_OUT_END] = {0};
   2154     int i, num_devices = 1;
   2155 
   2156     if (!platform_supports_app_type_cfg()) // use v1 instead
   2157         return -ENOSYS;
   2158 
   2159     if ((usecase->type == PCM_HFP_CALL) || (usecase->type == PCM_CAPTURE))
   2160         snd_device = usecase->in_snd_device;
   2161 
   2162     // skipped over get_spkr_prot_device
   2163     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
   2164     if (acdb_dev_id < 0) {
   2165         ALOGE("%s: Could not find acdb id for device(%d)",
   2166               __func__, snd_device);
   2167         return -EINVAL;
   2168     }
   2169 
   2170     if (platform_can_split_snd_device(snd_device,
   2171                                       &num_devices, new_snd_device) < 0) {
   2172         new_snd_device[0] = snd_device;
   2173     }
   2174 
   2175     for (i = 0; i < num_devices; i++) {
   2176         acdb_dev_id = platform_get_snd_device_acdb_id(new_snd_device[i]);
   2177         if (acdb_dev_id < 0) {
   2178             ALOGE("%s: Could not find acdb id for device(%d)",
   2179                   __func__, new_snd_device[i]);
   2180             return -EINVAL;
   2181         }
   2182         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
   2183               __func__, new_snd_device[i], acdb_dev_id);
   2184         if (new_snd_device[i] >= SND_DEVICE_OUT_BEGIN &&
   2185                 new_snd_device[i] < SND_DEVICE_OUT_END)
   2186             acdb_dev_type = ACDB_DEV_TYPE_OUT;
   2187         else
   2188             acdb_dev_type = ACDB_DEV_TYPE_IN;
   2189 
   2190         if (my_data->acdb_send_audio_cal_v3) {
   2191             my_data->acdb_send_audio_cal_v3(acdb_dev_id, acdb_dev_type,
   2192                                             app_type, sample_rate, i);
   2193         } else if (my_data->acdb_send_audio_cal) {
   2194             my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type); // this version differs from internal
   2195         }
   2196     }
   2197 
   2198     return 0;
   2199 }
   2200 
   2201 
   2202 int platform_switch_voice_call_device_pre(void *platform)
   2203 {
   2204     struct platform_data *my_data = (struct platform_data *)platform;
   2205     int ret = 0;
   2206 
   2207     if (my_data->csd != NULL &&
   2208         voice_is_in_call(my_data->adev)) {
   2209         /* This must be called before disabling mixer controls on APQ side */
   2210         ret = my_data->csd->disable_device();
   2211         if (ret < 0) {
   2212             ALOGE("%s: csd_client_disable_device, failed, error %d",
   2213                   __func__, ret);
   2214         }
   2215     }
   2216     return ret;
   2217 }
   2218 
   2219 int platform_switch_voice_call_enable_device_config(void *platform,
   2220                                                     snd_device_t out_snd_device,
   2221                                                     snd_device_t in_snd_device)
   2222 {
   2223     struct platform_data *my_data = (struct platform_data *)platform;
   2224     int acdb_rx_id, acdb_tx_id;
   2225     int ret = 0;
   2226 
   2227     if (my_data->csd == NULL)
   2228         return ret;
   2229 
   2230     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
   2231     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
   2232 
   2233     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
   2234         ret = my_data->csd->enable_device_config(acdb_rx_id, acdb_tx_id);
   2235         if (ret < 0) {
   2236             ALOGE("%s: csd_enable_device_config, failed, error %d",
   2237                   __func__, ret);
   2238         }
   2239     } else {
   2240         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
   2241               acdb_rx_id, acdb_tx_id);
   2242     }
   2243 
   2244     return ret;
   2245 }
   2246 
   2247 int platform_switch_voice_call_device_post(void *platform,
   2248                                            snd_device_t out_snd_device,
   2249                                            snd_device_t in_snd_device)
   2250 {
   2251     struct platform_data *my_data = (struct platform_data *)platform;
   2252     int acdb_rx_id, acdb_tx_id;
   2253 
   2254     if (my_data->acdb_send_voice_cal == NULL) {
   2255         ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
   2256     } else {
   2257         acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
   2258         acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
   2259 
   2260         if (acdb_rx_id > 0 && acdb_tx_id > 0)
   2261             my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
   2262         else
   2263             ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
   2264                   acdb_rx_id, acdb_tx_id);
   2265     }
   2266 
   2267     return 0;
   2268 }
   2269 
   2270 int platform_switch_voice_call_usecase_route_post(void *platform,
   2271                                                   snd_device_t out_snd_device,
   2272                                                   snd_device_t in_snd_device)
   2273 {
   2274     struct platform_data *my_data = (struct platform_data *)platform;
   2275     int acdb_rx_id, acdb_tx_id;
   2276     int ret = 0;
   2277 
   2278     if (my_data->csd == NULL)
   2279         return ret;
   2280 
   2281     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
   2282     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
   2283 
   2284     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
   2285         ret = my_data->csd->enable_device(acdb_rx_id, acdb_tx_id,
   2286                                           my_data->adev->acdb_settings);
   2287         if (ret < 0) {
   2288             ALOGE("%s: csd_enable_device, failed, error %d", __func__, ret);
   2289         }
   2290     } else {
   2291         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
   2292               acdb_rx_id, acdb_tx_id);
   2293     }
   2294 
   2295     return ret;
   2296 }
   2297 
   2298 int platform_start_voice_call(void *platform, uint32_t vsid)
   2299 {
   2300     struct platform_data *my_data = (struct platform_data *)platform;
   2301     int ret = 0;
   2302 
   2303     if (my_data->csd != NULL) {
   2304         ret = my_data->csd->start_voice(vsid);
   2305         if (ret < 0) {
   2306             ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
   2307         }
   2308     }
   2309     return ret;
   2310 }
   2311 
   2312 int platform_stop_voice_call(void *platform, uint32_t vsid)
   2313 {
   2314     struct platform_data *my_data = (struct platform_data *)platform;
   2315     int ret = 0;
   2316 
   2317     if (my_data->csd != NULL) {
   2318         ret = my_data->csd->stop_voice(vsid);
   2319         if (ret < 0) {
   2320             ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
   2321         }
   2322     }
   2323     return ret;
   2324 }
   2325 
   2326 int platform_set_mic_break_det(void *platform, bool enable)
   2327 {
   2328     int ret = 0;
   2329     struct platform_data *my_data = (struct platform_data *)platform;
   2330     struct audio_device *adev = my_data->adev;
   2331     const char *mixer_ctl_name = "Voice Mic Break Enable";
   2332     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   2333     if (!ctl) {
   2334         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   2335               __func__, mixer_ctl_name);
   2336         return -EINVAL;
   2337     }
   2338 
   2339     ret = mixer_ctl_set_value(ctl, 0, enable);
   2340     if(ret)
   2341         ALOGE("%s: Failed to set mixer ctl: %s", __func__, mixer_ctl_name);
   2342 
   2343     return ret;
   2344 }
   2345 
   2346 int platform_get_sample_rate(void *platform, uint32_t *rate)
   2347 {
   2348     struct platform_data *my_data = (struct platform_data *)platform;
   2349     int ret = 0;
   2350 
   2351     if (my_data->csd != NULL) {
   2352         ret = my_data->csd->get_sample_rate(rate);
   2353         if (ret < 0) {
   2354             ALOGE("%s: csd_get_sample_rate error %d\n", __func__, ret);
   2355         }
   2356     }
   2357     return ret;
   2358 }
   2359 
   2360 void platform_set_speaker_gain_in_combo(struct audio_device *adev,
   2361                                         snd_device_t snd_device,
   2362                                         bool enable)
   2363 {
   2364     const char* name;
   2365     switch (snd_device) {
   2366         case SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES:
   2367             if (enable)
   2368                 name = "spkr-gain-in-headphone-combo";
   2369             else
   2370                 name = "speaker-gain-default";
   2371             break;
   2372         case SND_DEVICE_OUT_SPEAKER_AND_LINE:
   2373             if (enable)
   2374                 name = "spkr-gain-in-line-combo";
   2375             else
   2376                 name = "speaker-gain-default";
   2377             break;
   2378         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES:
   2379             if (enable)
   2380                 name = "spkr-safe-gain-in-headphone-combo";
   2381             else
   2382                 name = "speaker-safe-gain-default";
   2383             break;
   2384         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE:
   2385             if (enable)
   2386                 name = "spkr-safe-gain-in-line-combo";
   2387             else
   2388                 name = "speaker-safe-gain-default";
   2389             break;
   2390         default:
   2391             return;
   2392     }
   2393 
   2394     audio_route_apply_and_update_path(adev->audio_route, name);
   2395 }
   2396 
   2397 int platform_set_voice_volume(void *platform, int volume)
   2398 {
   2399     struct platform_data *my_data = (struct platform_data *)platform;
   2400     struct audio_device *adev = my_data->adev;
   2401     struct mixer_ctl *ctl;
   2402     const char *mixer_ctl_name = "Voice Rx Gain";
   2403     const char *mute_mixer_ctl_name = "Voice Rx Device Mute";
   2404     int vol_index = 0, ret = 0;
   2405     uint32_t set_values[ ] = {0,
   2406                               ALL_SESSION_VSID,
   2407                               DEFAULT_VOLUME_RAMP_DURATION_MS};
   2408 
   2409     // Voice volume levels are mapped to adsp volume levels as follows.
   2410     // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1  0 -> 0
   2411     // But this values don't changed in kernel. So, below change is need.
   2412     vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, my_data->max_vol_index);
   2413     set_values[0] = vol_index;
   2414 
   2415     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   2416     if (!ctl) {
   2417         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   2418               __func__, mixer_ctl_name);
   2419         return -EINVAL;
   2420     }
   2421     ALOGV("Setting voice volume index: %d", set_values[0]);
   2422     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
   2423 
   2424     // Send mute command in case volume index is max since indexes are inverted
   2425     // for mixer controls.
   2426     if (vol_index == my_data->max_vol_index) {
   2427         set_values[0] = 1;
   2428     }
   2429     else {
   2430         set_values[0] = 0;
   2431     }
   2432 
   2433     ctl = mixer_get_ctl_by_name(adev->mixer, mute_mixer_ctl_name);
   2434     if (!ctl) {
   2435         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   2436               __func__, mute_mixer_ctl_name);
   2437         return -EINVAL;
   2438     }
   2439     ALOGV("%s: Setting RX Device Mute to: %d", __func__, set_values[0]);
   2440     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
   2441 
   2442     if (my_data->csd != NULL) {
   2443         ret = my_data->csd->volume(ALL_SESSION_VSID, volume,
   2444                                    DEFAULT_VOLUME_RAMP_DURATION_MS);
   2445         if (ret < 0) {
   2446             ALOGE("%s: csd_volume error %d", __func__, ret);
   2447         }
   2448     }
   2449     return ret;
   2450 }
   2451 
   2452 int platform_set_mic_mute(void *platform, bool state)
   2453 {
   2454     struct platform_data *my_data = (struct platform_data *)platform;
   2455     struct audio_device *adev = my_data->adev;
   2456     struct mixer_ctl *ctl;
   2457     const char *mixer_ctl_name = "Voice Tx Mute";
   2458     int ret = 0;
   2459     uint32_t set_values[ ] = {0,
   2460                               ALL_SESSION_VSID,
   2461                               DEFAULT_MUTE_RAMP_DURATION_MS};
   2462 
   2463     if (adev->mode != AUDIO_MODE_IN_CALL &&
   2464         adev->mode != AUDIO_MODE_IN_COMMUNICATION)
   2465         return 0;
   2466 
   2467     if (adev->enable_hfp)
   2468         mixer_ctl_name = "HFP Tx Mute";
   2469 
   2470     set_values[0] = state;
   2471     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   2472     if (!ctl) {
   2473         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   2474               __func__, mixer_ctl_name);
   2475         return -EINVAL;
   2476     }
   2477     ALOGV("Setting voice mute state: %d", state);
   2478     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
   2479 
   2480     if (my_data->csd != NULL) {
   2481         ret = my_data->csd->mic_mute(ALL_SESSION_VSID, state,
   2482                                      DEFAULT_MUTE_RAMP_DURATION_MS);
   2483         if (ret < 0) {
   2484             ALOGE("%s: csd_mic_mute error %d", __func__, ret);
   2485         }
   2486     }
   2487     return ret;
   2488 }
   2489 
   2490 int platform_set_device_mute(void *platform, bool state, char *dir)
   2491 {
   2492     struct platform_data *my_data = (struct platform_data *)platform;
   2493     struct audio_device *adev = my_data->adev;
   2494     struct mixer_ctl *ctl;
   2495     char *mixer_ctl_name = NULL;
   2496     int ret = 0;
   2497     uint32_t set_values[ ] = {0,
   2498                               ALL_SESSION_VSID,
   2499                               0};
   2500     if(dir == NULL) {
   2501         ALOGE("%s: Invalid direction:%s", __func__, dir);
   2502         return -EINVAL;
   2503     }
   2504 
   2505     if (!strncmp("rx", dir, sizeof("rx"))) {
   2506         mixer_ctl_name = "Voice Rx Device Mute";
   2507     } else if (!strncmp("tx", dir, sizeof("tx"))) {
   2508         mixer_ctl_name = "Voice Tx Device Mute";
   2509     } else {
   2510         return -EINVAL;
   2511     }
   2512 
   2513     set_values[0] = state;
   2514     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   2515     if (!ctl) {
   2516         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   2517               __func__, mixer_ctl_name);
   2518         return -EINVAL;
   2519     }
   2520 
   2521     ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
   2522           __func__,state, mixer_ctl_name);
   2523     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
   2524 
   2525     return ret;
   2526 }
   2527 
   2528 int platform_can_split_snd_device(snd_device_t snd_device,
   2529                                   int *num_devices,
   2530                                   snd_device_t *new_snd_devices)
   2531 {
   2532     int ret = -EINVAL;
   2533     if (NULL == num_devices || NULL == new_snd_devices) {
   2534         ALOGE("%s: NULL pointer ..", __func__);
   2535         return -EINVAL;
   2536     }
   2537 
   2538     /*
   2539      * If wired headset/headphones/line devices share the same backend
   2540      * with speaker/earpiece this routine returns -EINVAL.
   2541      */
   2542     if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
   2543         !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
   2544         *num_devices = 2;
   2545         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2546         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
   2547         ret = 0;
   2548     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
   2549                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
   2550         *num_devices = 2;
   2551         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2552         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
   2553         ret = 0;
   2554     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES &&
   2555                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_HEADPHONES)) {
   2556         *num_devices = 2;
   2557         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2558         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
   2559         ret = 0;
   2560     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE &&
   2561                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_LINE)) {
   2562         *num_devices = 2;
   2563         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2564         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
   2565         ret = 0;
   2566     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO &&
   2567                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
   2568                                               SND_DEVICE_OUT_BT_SCO)) {
   2569         *num_devices = 2;
   2570         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2571         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
   2572         ret = 0;
   2573     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO &&
   2574                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
   2575                                               SND_DEVICE_OUT_BT_SCO)) {
   2576         *num_devices = 2;
   2577         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2578         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
   2579         ret = 0;
   2580     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB &&
   2581                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
   2582                                               SND_DEVICE_OUT_BT_SCO_WB)) {
   2583         *num_devices = 2;
   2584         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2585         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
   2586         ret = 0;
   2587     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB &&
   2588                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
   2589                                               SND_DEVICE_OUT_BT_SCO_WB)) {
   2590         *num_devices = 2;
   2591         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2592         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
   2593         ret = 0;
   2594     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET &&
   2595                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_USB_HEADSET)) {
   2596         *num_devices = 2;
   2597         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2598         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
   2599         ret = 0;
   2600     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET &&
   2601                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_USB_HEADSET)) {
   2602         *num_devices = 2;
   2603         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2604         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
   2605         ret = 0;
   2606     } else if (SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device &&
   2607                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
   2608                                               SND_DEVICE_OUT_BT_A2DP)) {
   2609         *num_devices = 2;
   2610         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
   2611         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
   2612         ret = 0;
   2613     } else if (SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device &&
   2614                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
   2615                                               SND_DEVICE_OUT_BT_A2DP)) {
   2616         *num_devices = 2;
   2617         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
   2618         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
   2619         ret = 0;
   2620     }
   2621 
   2622     return ret;
   2623 }
   2624 
   2625 snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
   2626 {
   2627     struct platform_data *my_data = (struct platform_data *)platform;
   2628     struct audio_device *adev = my_data->adev;
   2629     audio_mode_t mode = adev->mode;
   2630     snd_device_t snd_device = SND_DEVICE_NONE;
   2631 
   2632     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
   2633     if (devices == AUDIO_DEVICE_NONE ||
   2634         devices & AUDIO_DEVICE_BIT_IN) {
   2635         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
   2636         goto exit;
   2637     }
   2638 
   2639     if (popcount(devices) == 2) {
   2640         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
   2641                         AUDIO_DEVICE_OUT_SPEAKER) ||
   2642                 devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
   2643                             AUDIO_DEVICE_OUT_SPEAKER)) {
   2644             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
   2645         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
   2646                                AUDIO_DEVICE_OUT_SPEAKER)) {
   2647             snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
   2648         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
   2649                                AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
   2650                    devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
   2651                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
   2652             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES;
   2653         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
   2654                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
   2655             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE;
   2656         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
   2657                                AUDIO_DEVICE_OUT_SPEAKER)) {
   2658             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
   2659         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
   2660                    ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER)) {
   2661             snd_device = adev->bt_wb_speech_enabled ?
   2662                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB :
   2663                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO;
   2664         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
   2665                          ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
   2666             snd_device = adev->bt_wb_speech_enabled ?
   2667                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB :
   2668                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO;
   2669         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
   2670                                AUDIO_DEVICE_OUT_SPEAKER)) ||
   2671                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
   2672                                                AUDIO_DEVICE_OUT_SPEAKER))) {
   2673             snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
   2674         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
   2675                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) ||
   2676                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
   2677                                                AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
   2678             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET;
   2679         } else if ((devices & AUDIO_DEVICE_OUT_SPEAKER) &&
   2680                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
   2681             snd_device = SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP;
   2682         }  else if ((devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
   2683                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
   2684             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP;
   2685         } else {
   2686             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
   2687             goto exit;
   2688         }
   2689         if (snd_device != SND_DEVICE_NONE) {
   2690             goto exit;
   2691         }
   2692     }
   2693 
   2694     if (popcount(devices) != 1) {
   2695         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
   2696         goto exit;
   2697     }
   2698 
   2699     if (voice_is_in_call(adev) || adev->enable_voicerx || audio_extn_hfp_is_active(adev)) {
   2700         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   2701             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
   2702             devices & AUDIO_DEVICE_OUT_LINE) {
   2703             if (voice_is_in_call(adev) &&
   2704                 (adev->voice.tty_mode == TTY_MODE_FULL))
   2705                 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
   2706             else if (voice_is_in_call(adev) &&
   2707                 (adev->voice.tty_mode == TTY_MODE_VCO))
   2708                 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
   2709             else if (voice_is_in_call(adev) &&
   2710                 (adev->voice.tty_mode == TTY_MODE_HCO))
   2711                 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
   2712             else {
   2713                 if (devices & AUDIO_DEVICE_OUT_LINE)
   2714                     snd_device = SND_DEVICE_OUT_VOICE_LINE;
   2715                 else
   2716                     snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
   2717                 }
   2718         } else if (audio_is_usb_out_device(devices)) {
   2719             if (voice_is_in_call(adev)) {
   2720                 switch (adev->voice.tty_mode) {
   2721                     case TTY_MODE_FULL:
   2722                         snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_USB;
   2723                         break;
   2724                     case TTY_MODE_VCO:
   2725                         snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_USB;
   2726                         break;
   2727                     case TTY_MODE_HCO:
   2728                         // since Hearing will be on handset\speaker, use existing device
   2729                         snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
   2730                         break;
   2731                     case TTY_MODE_OFF:
   2732                         break;
   2733                     default:
   2734                         ALOGE("%s: Invalid TTY mode (%#x)",
   2735                               __func__, adev->voice.tty_mode);
   2736                 }
   2737             }
   2738             if (snd_device == SND_DEVICE_NONE) {
   2739                     snd_device = audio_extn_usb_is_capture_supported() ?
   2740                                  SND_DEVICE_OUT_VOICE_USB_HEADSET :
   2741                                  SND_DEVICE_OUT_VOICE_USB_HEADPHONES;
   2742             }
   2743         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
   2744             if (adev->bt_wb_speech_enabled) {
   2745                 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
   2746             } else {
   2747                 snd_device = SND_DEVICE_OUT_BT_SCO;
   2748             }
   2749         } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
   2750             snd_device = SND_DEVICE_OUT_BT_A2DP;
   2751         } else if (devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
   2752             if (!adev->enable_hfp) {
   2753                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
   2754             } else {
   2755                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_HFP;
   2756             }
   2757         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
   2758             if(adev->voice.hac)
   2759                 snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
   2760             else if (is_operator_tmus())
   2761                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
   2762             else
   2763                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
   2764         } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX)
   2765             snd_device = SND_DEVICE_OUT_VOICE_TX;
   2766 
   2767         if (snd_device != SND_DEVICE_NONE) {
   2768             goto exit;
   2769         }
   2770     }
   2771 
   2772     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   2773         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
   2774         snd_device = SND_DEVICE_OUT_HEADPHONES;
   2775     } else if (devices & AUDIO_DEVICE_OUT_LINE) {
   2776         snd_device = SND_DEVICE_OUT_LINE;
   2777     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
   2778         snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
   2779     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
   2780         /*
   2781          * Perform device switch only if acdb tuning is different between SPEAKER & SPEAKER_REVERSE,
   2782          * Or there will be a small pause while performing device switch.
   2783          */
   2784         if (my_data->speaker_lr_swap &&
   2785             (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
   2786             acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]))
   2787             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
   2788         else
   2789             snd_device = SND_DEVICE_OUT_SPEAKER;
   2790     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
   2791         if (adev->bt_wb_speech_enabled) {
   2792             snd_device = SND_DEVICE_OUT_BT_SCO_WB;
   2793         } else {
   2794             snd_device = SND_DEVICE_OUT_BT_SCO;
   2795         }
   2796     } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
   2797         snd_device = SND_DEVICE_OUT_BT_A2DP;
   2798     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   2799         snd_device = SND_DEVICE_OUT_HDMI ;
   2800     } else if (audio_is_usb_out_device(devices)) {
   2801         if (audio_extn_ma_supported_usb())
   2802             snd_device = SND_DEVICE_OUT_USB_HEADSET_SPEC;
   2803         else if (audio_extn_usb_is_capture_supported())
   2804             snd_device = SND_DEVICE_OUT_USB_HEADSET;
   2805         else
   2806             snd_device = SND_DEVICE_OUT_USB_HEADPHONES;
   2807     }else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
   2808         /*HAC support for voice-ish audio (eg visual voicemail)*/
   2809         if(adev->voice.hac)
   2810             snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
   2811         else
   2812             snd_device = SND_DEVICE_OUT_HANDSET;
   2813     } else {
   2814         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
   2815     }
   2816 exit:
   2817     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
   2818     return snd_device;
   2819 }
   2820 
   2821 snd_device_t platform_get_input_snd_device(void *platform, audio_devices_t out_device)
   2822 {
   2823     struct platform_data *my_data = (struct platform_data *)platform;
   2824     struct audio_device *adev = my_data->adev;
   2825     audio_source_t  source = (adev->active_input == NULL) ?
   2826                                 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
   2827 
   2828     audio_mode_t    mode   = adev->mode;
   2829     audio_devices_t in_device = ((adev->active_input == NULL) ?
   2830                                     AUDIO_DEVICE_NONE : adev->active_input->device)
   2831                                 & ~AUDIO_DEVICE_BIT_IN;
   2832     audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
   2833                                 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
   2834     snd_device_t snd_device = SND_DEVICE_NONE;
   2835     int channel_count = popcount(channel_mask);
   2836 
   2837     ALOGV("%s: enter: out_device(%#x) in_device(%#x) channel_count (%d) channel_mask (0x%x)",
   2838           __func__, out_device, in_device, channel_count, channel_mask);
   2839     if ((out_device != AUDIO_DEVICE_NONE) && (voice_is_in_call(adev) ||
   2840         audio_extn_hfp_is_active(adev))) {
   2841         if (adev->voice.tty_mode != TTY_MODE_OFF) {
   2842             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   2843                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
   2844                 out_device & AUDIO_DEVICE_OUT_LINE) {
   2845                 switch (adev->voice.tty_mode) {
   2846                     case TTY_MODE_FULL:
   2847                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
   2848                         break;
   2849                     case TTY_MODE_VCO:
   2850                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
   2851                         break;
   2852                     case TTY_MODE_HCO:
   2853                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
   2854                         break;
   2855                     default:
   2856                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
   2857                 }
   2858                 goto exit;
   2859             } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
   2860                 switch (adev->voice.tty_mode) {
   2861                     case TTY_MODE_FULL:
   2862                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC;
   2863                         break;
   2864                     case TTY_MODE_VCO:
   2865                         // since voice will be captured from handset mic, use existing device
   2866                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
   2867                         break;
   2868                     case TTY_MODE_HCO:
   2869                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC;
   2870                         break;
   2871                     default:
   2872                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
   2873                 }
   2874                 goto exit;
   2875             }
   2876         }
   2877         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
   2878             if (my_data->fluence_in_voice_call == false) {
   2879                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
   2880             } else {
   2881                 if (is_operator_tmus())
   2882                     snd_device = SND_DEVICE_IN_VOICE_DMIC_TMUS;
   2883                 else
   2884                     snd_device = SND_DEVICE_IN_VOICE_DMIC;
   2885             }
   2886         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
   2887             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
   2888         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
   2889             if (adev->bt_wb_speech_enabled) {
   2890                 if (adev->bluetooth_nrec)
   2891                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
   2892                 else
   2893                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
   2894             } else {
   2895                 if (adev->bluetooth_nrec)
   2896                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
   2897                 else
   2898                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
   2899             }
   2900         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
   2901                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
   2902                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   2903                    out_device & AUDIO_DEVICE_OUT_LINE) {
   2904             if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
   2905                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
   2906                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
   2907                 } else {
   2908                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
   2909                 }
   2910             }
   2911 
   2912             //select default
   2913             if (snd_device == SND_DEVICE_NONE) {
   2914                 if (!adev->enable_hfp) {
   2915                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
   2916                 } else {
   2917                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP;
   2918                     platform_set_echo_reference(adev, true, out_device);
   2919                 }
   2920             }
   2921         } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX) {
   2922             snd_device = SND_DEVICE_IN_VOICE_RX;
   2923         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
   2924             if (audio_extn_usb_is_capture_supported()) {
   2925               snd_device = SND_DEVICE_IN_VOICE_USB_HEADSET_MIC;
   2926             } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
   2927                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
   2928                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
   2929                 } else {
   2930                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
   2931                 }
   2932             }
   2933         }
   2934     } else if (source == AUDIO_SOURCE_CAMCORDER) {
   2935         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
   2936             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
   2937             snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
   2938         }
   2939     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
   2940         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   2941             if (my_data->fluence_in_voice_rec && channel_count == 1) {
   2942                 if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
   2943                     (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
   2944                     if (adev->active_input->enable_aec)
   2945                         snd_device = SND_DEVICE_IN_HANDSET_QMIC_AEC;
   2946                     else
   2947                         snd_device = SND_DEVICE_IN_HANDSET_QMIC;
   2948                 } else if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
   2949                     (my_data->source_mic_type & SOURCE_THREE_MIC)) {
   2950                     if (adev->active_input->enable_aec)
   2951                         snd_device = SND_DEVICE_IN_HANDSET_TMIC_AEC;
   2952                     else
   2953                         snd_device = SND_DEVICE_IN_HANDSET_TMIC;
   2954                 } else if (((my_data->fluence_type == FLUENCE_PRO_ENABLE) ||
   2955                     (my_data->fluence_type == FLUENCE_ENABLE)) &&
   2956                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   2957                     if (adev->active_input->enable_aec)
   2958                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
   2959                     else
   2960                         snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
   2961                 }
   2962                 platform_set_echo_reference(adev, true, out_device);
   2963             } else if ((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) &&
   2964                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   2965                 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
   2966             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
   2967                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
   2968                 snd_device = SND_DEVICE_IN_THREE_MIC;
   2969             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
   2970                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
   2971                 snd_device = SND_DEVICE_IN_QUAD_MIC;
   2972             }
   2973             if (snd_device == SND_DEVICE_NONE) {
   2974                 if (adev->active_input->enable_aec) {
   2975                     if (adev->active_input->enable_ns) {
   2976                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS;
   2977                     } else {
   2978                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC;
   2979                     }
   2980                     platform_set_echo_reference(adev, true, out_device);
   2981                 } else if (adev->active_input->enable_ns) {
   2982                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
   2983                 } else {
   2984                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
   2985                 }
   2986             }
   2987         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
   2988             snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC;
   2989         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
   2990             snd_device = SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC;
   2991         }
   2992     } else if (source == AUDIO_SOURCE_UNPROCESSED) {
   2993         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   2994             if (((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) ||
   2995                  (channel_mask == AUDIO_CHANNEL_IN_STEREO)) &&
   2996                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   2997                 snd_device = SND_DEVICE_IN_UNPROCESSED_STEREO_MIC;
   2998             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
   2999                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
   3000                 snd_device = SND_DEVICE_IN_UNPROCESSED_THREE_MIC;
   3001             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
   3002                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
   3003                 snd_device = SND_DEVICE_IN_UNPROCESSED_QUAD_MIC;
   3004             } else {
   3005                 snd_device = SND_DEVICE_IN_UNPROCESSED_MIC;
   3006             }
   3007         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
   3008             snd_device = SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC;
   3009         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
   3010             snd_device = SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC;
   3011         }
   3012     } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
   3013                mode == AUDIO_MODE_IN_COMMUNICATION) {
   3014         if (out_device & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
   3015             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   3016             (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE | AUDIO_DEVICE_OUT_USB_HEADSET) &&
   3017                 !audio_extn_usb_is_capture_supported())) {
   3018             in_device = AUDIO_DEVICE_IN_BACK_MIC;
   3019         }
   3020         if (adev->active_input) {
   3021             if (adev->active_input->enable_aec &&
   3022                     adev->active_input->enable_ns) {
   3023                 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
   3024                     if (my_data->fluence_in_spkr_mode &&
   3025                             my_data->fluence_in_voice_comm &&
   3026                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3027                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
   3028                     } else {
   3029                         snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
   3030                     }
   3031                 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   3032                     if (my_data->fluence_in_voice_comm &&
   3033                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3034                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
   3035                     } else {
   3036                         snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
   3037                     }
   3038                 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
   3039                     snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
   3040                 } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
   3041                     snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
   3042                 }
   3043                 platform_set_echo_reference(adev, true, out_device);
   3044             } else if (adev->active_input->enable_aec) {
   3045                 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
   3046                     if (my_data->fluence_in_spkr_mode &&
   3047                             my_data->fluence_in_voice_comm &&
   3048                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3049                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
   3050                     } else {
   3051                         snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
   3052                     }
   3053                 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   3054                     if (my_data->fluence_in_voice_comm &&
   3055                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3056                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
   3057                     } else {
   3058                         snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
   3059                     }
   3060                } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
   3061                    snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
   3062                } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
   3063                    snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
   3064                }
   3065                platform_set_echo_reference(adev, true, out_device);
   3066             } else if (adev->active_input->enable_ns) {
   3067                 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
   3068                     if (my_data->fluence_in_spkr_mode &&
   3069                             my_data->fluence_in_voice_comm &&
   3070                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3071                         snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
   3072                     } else {
   3073                         snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
   3074                     }
   3075                 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   3076                     if (my_data->fluence_in_voice_comm &&
   3077                             (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
   3078                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
   3079                     } else {
   3080                         snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
   3081                     }
   3082                 }
   3083             }
   3084         }
   3085     } else if (source == AUDIO_SOURCE_DEFAULT) {
   3086         goto exit;
   3087     }
   3088 
   3089 
   3090     if (snd_device != SND_DEVICE_NONE) {
   3091         goto exit;
   3092     }
   3093 
   3094     if (in_device != AUDIO_DEVICE_NONE &&
   3095             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
   3096             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
   3097         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
   3098             if ((my_data->source_mic_type & SOURCE_QUAD_MIC) &&
   3099                 channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) {
   3100                 snd_device = SND_DEVICE_IN_QUAD_MIC;
   3101             } else if ((my_data->source_mic_type & SOURCE_THREE_MIC) &&
   3102                        channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) {
   3103                 snd_device = SND_DEVICE_IN_THREE_MIC;
   3104             } else if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
   3105                        channel_count == 2) {
   3106                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_STEREO;
   3107             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
   3108                        channel_count == 1) {
   3109                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
   3110             } else {
   3111                 ALOGE("%s: something wrong (1): source type (%d) channel_count (%d) .."
   3112                       " channel mask (0x%x) no combination found .. setting to mono", __func__,
   3113                        my_data->source_mic_type, channel_count, channel_mask);
   3114                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
   3115             }
   3116         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
   3117             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
   3118                     channel_count == 2) {
   3119                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
   3120             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
   3121                     channel_count == 1) {
   3122                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
   3123             } else {
   3124                 ALOGE("%s: something wrong (2): source type (%d) channel_count (%d) .."
   3125                       " no combination found .. setting to mono", __func__,
   3126                        my_data->source_mic_type, channel_count);
   3127                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
   3128             }
   3129         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
   3130             snd_device = SND_DEVICE_IN_HEADSET_MIC;
   3131         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
   3132             if (adev->bt_wb_speech_enabled) {
   3133                 if (adev->bluetooth_nrec)
   3134                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
   3135                 else
   3136                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
   3137             } else {
   3138                 if (adev->bluetooth_nrec)
   3139                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
   3140                 else
   3141                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
   3142             }
   3143         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
   3144             snd_device = SND_DEVICE_IN_HDMI_MIC;
   3145         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
   3146             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
   3147         } else {
   3148             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
   3149             ALOGW("%s: Using default handset-mic", __func__);
   3150             snd_device = SND_DEVICE_IN_HANDSET_MIC;
   3151         }
   3152     } else {
   3153         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
   3154             snd_device = SND_DEVICE_IN_HANDSET_MIC;
   3155         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
   3156             snd_device = SND_DEVICE_IN_HEADSET_MIC;
   3157         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
   3158                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
   3159                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
   3160                    out_device & AUDIO_DEVICE_OUT_LINE) {
   3161             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
   3162                     channel_count == 2) {
   3163                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
   3164             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
   3165                           channel_count == 1) {
   3166                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
   3167             } else {
   3168                 ALOGE("%s: something wrong (3): source type (%d) channel_count (%d) .."
   3169                       " no combination found .. setting to mono", __func__,
   3170                        my_data->source_mic_type, channel_count);
   3171                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
   3172             }
   3173         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
   3174             if (adev->bt_wb_speech_enabled) {
   3175                 if (adev->bluetooth_nrec)
   3176                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
   3177                 else
   3178                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
   3179             } else {
   3180                 if (adev->bluetooth_nrec)
   3181                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
   3182                 else
   3183                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
   3184             }
   3185         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   3186             snd_device = SND_DEVICE_IN_HDMI_MIC;
   3187         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
   3188             if (audio_extn_usb_is_capture_supported())
   3189               snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
   3190             else
   3191               snd_device = SND_DEVICE_IN_SPEAKER_MIC;
   3192         } else {
   3193             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
   3194             ALOGW("%s: Using default handset-mic", __func__);
   3195             snd_device = SND_DEVICE_IN_HANDSET_MIC;
   3196         }
   3197     }
   3198 exit:
   3199     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
   3200     return snd_device;
   3201 }
   3202 
   3203 int platform_set_hdmi_channels(void *platform,  int channel_count)
   3204 {
   3205     struct platform_data *my_data = (struct platform_data *)platform;
   3206     struct audio_device *adev = my_data->adev;
   3207     struct mixer_ctl *ctl;
   3208     const char *channel_cnt_str = NULL;
   3209     const char *mixer_ctl_name = "HDMI_RX Channels";
   3210     switch (channel_count) {
   3211     case 8:
   3212         channel_cnt_str = "Eight"; break;
   3213     case 7:
   3214         channel_cnt_str = "Seven"; break;
   3215     case 6:
   3216         channel_cnt_str = "Six"; break;
   3217     case 5:
   3218         channel_cnt_str = "Five"; break;
   3219     case 4:
   3220         channel_cnt_str = "Four"; break;
   3221     case 3:
   3222         channel_cnt_str = "Three"; break;
   3223     default:
   3224         channel_cnt_str = "Two"; break;
   3225     }
   3226     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   3227     if (!ctl) {
   3228         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   3229               __func__, mixer_ctl_name);
   3230         return -EINVAL;
   3231     }
   3232     ALOGV("HDMI channel count: %s", channel_cnt_str);
   3233     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
   3234     return 0;
   3235 }
   3236 
   3237 int platform_edid_get_max_channels(void *platform)
   3238 {
   3239     struct platform_data *my_data = (struct platform_data *)platform;
   3240     struct audio_device *adev = my_data->adev;
   3241     char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
   3242     char *sad = block;
   3243     int num_audio_blocks;
   3244     int channel_count;
   3245     int max_channels = 0;
   3246     int i, ret, count;
   3247 
   3248     struct mixer_ctl *ctl;
   3249 
   3250     ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
   3251     if (!ctl) {
   3252         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   3253               __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
   3254         return 0;
   3255     }
   3256 
   3257     mixer_ctl_update(ctl);
   3258 
   3259     count = mixer_ctl_get_num_values(ctl);
   3260 
   3261     /* Read SAD blocks, clamping the maximum size for safety */
   3262     if (count > (int)sizeof(block))
   3263         count = (int)sizeof(block);
   3264 
   3265     ret = mixer_ctl_get_array(ctl, block, count);
   3266     if (ret != 0) {
   3267         ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
   3268         return 0;
   3269     }
   3270 
   3271     /* Calculate the number of SAD blocks */
   3272     num_audio_blocks = count / SAD_BLOCK_SIZE;
   3273 
   3274     for (i = 0; i < num_audio_blocks; i++) {
   3275         /* Only consider LPCM blocks */
   3276         if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
   3277             sad += 3;
   3278             continue;
   3279         }
   3280 
   3281         channel_count = (sad[0] & 0x7) + 1;
   3282         if (channel_count > max_channels)
   3283             max_channels = channel_count;
   3284 
   3285         /* Advance to next block */
   3286         sad += 3;
   3287     }
   3288 
   3289     return max_channels;
   3290 }
   3291 
   3292 int platform_set_incall_recording_session_id(void *platform,
   3293                                              uint32_t session_id, int rec_mode)
   3294 {
   3295     int ret = 0;
   3296     struct platform_data *my_data = (struct platform_data *)platform;
   3297     struct audio_device *adev = my_data->adev;
   3298     struct mixer_ctl *ctl;
   3299     const char *mixer_ctl_name = "Voc VSID";
   3300     int num_ctl_values;
   3301     int i;
   3302 
   3303     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   3304     if (!ctl) {
   3305         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   3306               __func__, mixer_ctl_name);
   3307         ret = -EINVAL;
   3308     } else {
   3309         num_ctl_values = mixer_ctl_get_num_values(ctl);
   3310         for (i = 0; i < num_ctl_values; i++) {
   3311             if (mixer_ctl_set_value(ctl, i, session_id)) {
   3312                 ALOGV("Error: invalid session_id: %x", session_id);
   3313                 ret = -EINVAL;
   3314                 break;
   3315             }
   3316         }
   3317     }
   3318 
   3319     if (my_data->csd != NULL) {
   3320         ret = my_data->csd->start_record(ALL_SESSION_VSID, rec_mode);
   3321         if (ret < 0) {
   3322             ALOGE("%s: csd_client_start_record failed, error %d",
   3323                   __func__, ret);
   3324         }
   3325     }
   3326 
   3327     return ret;
   3328 }
   3329 
   3330 int platform_set_incall_recording_session_channels(void *platform,
   3331                                              uint32_t channel_count)
   3332 {
   3333     int ret = 0;
   3334     struct platform_data *my_data = (struct platform_data *)platform;
   3335     struct audio_device *adev = my_data->adev;
   3336     const char *mixer_ctl_name = "Voc Rec Config";
   3337     int num_ctl_values;
   3338     int i;
   3339     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   3340 
   3341     if (!ctl) {
   3342         ALOGE("%s: Could not get ctl for mixer cmd - %s",
   3343               __func__, mixer_ctl_name);
   3344         ret = -EINVAL;
   3345     } else {
   3346         num_ctl_values = mixer_ctl_get_num_values(ctl);
   3347         for (i = 0; i < num_ctl_values; i++) {
   3348             if (mixer_ctl_set_value(ctl, i, channel_count)) {
   3349                 ALOGE("Error: invalid channel count: %x", channel_count);
   3350                 ret = -EINVAL;
   3351                 break;
   3352             }
   3353         }
   3354     }
   3355 
   3356     return ret;
   3357 }
   3358 
   3359 int platform_stop_incall_recording_usecase(void *platform)
   3360 {
   3361     int ret = 0;
   3362     struct platform_data *my_data = (struct platform_data *)platform;
   3363 
   3364     if (my_data->csd != NULL) {
   3365         ret = my_data->csd->stop_record(ALL_SESSION_VSID);
   3366         if (ret < 0) {
   3367             ALOGE("%s: csd_client_stop_record failed, error %d",
   3368                   __func__, ret);
   3369         }
   3370     }
   3371 
   3372     return ret;
   3373 }
   3374 
   3375 int platform_start_incall_music_usecase(void *platform)
   3376 {
   3377     int ret = 0;
   3378     struct platform_data *my_data = (struct platform_data *)platform;
   3379 
   3380     if (my_data->csd != NULL) {
   3381         ret = my_data->csd->start_playback(ALL_SESSION_VSID);
   3382         if (ret < 0) {
   3383             ALOGE("%s: csd_client_start_playback failed, error %d",
   3384                   __func__, ret);
   3385         }
   3386     }
   3387 
   3388     return ret;
   3389 }
   3390 
   3391 int platform_stop_incall_music_usecase(void *platform)
   3392 {
   3393     int ret = 0;
   3394     struct platform_data *my_data = (struct platform_data *)platform;
   3395 
   3396     if (my_data->csd != NULL) {
   3397         ret = my_data->csd->stop_playback(ALL_SESSION_VSID);
   3398         if (ret < 0) {
   3399             ALOGE("%s: csd_client_stop_playback failed, error %d",
   3400                   __func__, ret);
   3401         }
   3402     }
   3403 
   3404     return ret;
   3405 }
   3406 
   3407 int platform_set_parameters(void *platform, struct str_parms *parms)
   3408 {
   3409     struct platform_data *my_data = (struct platform_data *)platform;
   3410     char *value = NULL;
   3411     char *kv_pairs = str_parms_to_str(parms);
   3412     int len;
   3413     int ret = 0, err;
   3414 
   3415     if (kv_pairs == NULL) {
   3416         ret = -EINVAL;
   3417         ALOGE("%s: key-value pair is NULL", __func__);
   3418         goto done;
   3419     }
   3420 
   3421     ALOGV("%s: enter: %s", __func__, kv_pairs);
   3422 
   3423     len = strlen(kv_pairs);
   3424     value = (char*)calloc(len + 1, sizeof(char));
   3425     if (value == NULL) {
   3426         ret = -ENOMEM;
   3427         ALOGE("[%s] failed to allocate memory", __func__);
   3428         goto done;
   3429     }
   3430 
   3431     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME,
   3432                             value, len);
   3433     if (err >= 0) {
   3434         str_parms_del(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME);
   3435         my_data->snd_card_name = strdup(value);
   3436         ALOGV("%s: sound card name %s", __func__, my_data->snd_card_name);
   3437     }
   3438 
   3439     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
   3440                             value, len);
   3441     if (err >= 0) {
   3442         struct operator_info *info;
   3443         char *str = value;
   3444         char *name;
   3445 
   3446         str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
   3447         info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
   3448         name = strtok(str, ";");
   3449         info->name = strdup(name);
   3450         info->mccmnc = strdup(str + strlen(name) + 1);
   3451 
   3452         list_add_tail(&operator_info_list, &info->list);
   3453         ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
   3454     }
   3455 
   3456     memset(value, 0, len + 1);
   3457     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT,
   3458                             value, len);
   3459     if (err >= 0) {
   3460         str_parms_del(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT);
   3461         my_data->max_mic_count = atoi(value);
   3462         ALOGV("%s: max_mic_count %s/%d", __func__, value, my_data->max_mic_count);
   3463     }
   3464 
   3465     /* handle audio calibration parameters */
   3466     set_audiocal(platform, parms, value, len);
   3467 
   3468     // to-do: disable setting sidetone gain, will revist this later
   3469     // audio_extn_usb_set_sidetone_gain(parms, value, len);
   3470 done:
   3471     ALOGV("%s: exit with code(%d)", __func__, ret);
   3472     if (kv_pairs != NULL)
   3473         free(kv_pairs);
   3474     if (value != NULL)
   3475         free(value);
   3476 
   3477     return ret;
   3478 }
   3479 
   3480 /* Delay in Us */
   3481 int64_t platform_render_latency(audio_usecase_t usecase)
   3482 {
   3483     switch (usecase) {
   3484         case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
   3485             return DEEP_BUFFER_PLATFORM_DELAY;
   3486         case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
   3487             return LOW_LATENCY_PLATFORM_DELAY;
   3488         case USECASE_AUDIO_PLAYBACK_ULL:
   3489             return ULL_PLATFORM_DELAY;
   3490         case USECASE_AUDIO_PLAYBACK_MMAP:
   3491             return MMAP_PLATFORM_DELAY;
   3492         default:
   3493             return 0;
   3494     }
   3495 }
   3496 
   3497 int platform_set_snd_device_backend(snd_device_t device, const char *backend_tag,
   3498                                     const char * hw_interface)
   3499 {
   3500     int ret = 0;
   3501 
   3502     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
   3503         ALOGE("%s: Invalid snd_device = %d",
   3504             __func__, device);
   3505         ret = -EINVAL;
   3506         goto done;
   3507     }
   3508 
   3509     ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
   3510           platform_get_snd_device_name(device),
   3511           backend_tag_table[device] != NULL ? backend_tag_table[device]: "null", backend_tag);
   3512     if (backend_tag_table[device]) {
   3513         free(backend_tag_table[device]);
   3514     }
   3515     backend_tag_table[device] = strdup(backend_tag);
   3516 
   3517     if (hw_interface != NULL) {
   3518         if (hw_interface_table[device])
   3519             free(hw_interface_table[device]);
   3520         ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
   3521         hw_interface_table[device] = strdup(hw_interface);
   3522     }
   3523 done:
   3524     return ret;
   3525 }
   3526 
   3527 int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
   3528 {
   3529     int ret = 0;
   3530     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
   3531         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
   3532         ret = -EINVAL;
   3533         goto done;
   3534     }
   3535 
   3536     if ((type != 0) && (type != 1)) {
   3537         ALOGE("%s: invalid usecase type", __func__);
   3538         ret = -EINVAL;
   3539     }
   3540     ALOGV("%s: pcm_device_table[%d %s][%d] = %d", __func__, usecase,
   3541           use_case_table[usecase],
   3542           type, pcm_id);
   3543     pcm_device_table[usecase][type] = pcm_id;
   3544 done:
   3545     return ret;
   3546 }
   3547 
   3548 #define DEFAULT_NOMINAL_SPEAKER_GAIN 20
   3549 int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
   3550     // backup_gain: gain to try to set in case of an error during ramp
   3551     int start_gain, end_gain, step, backup_gain, i;
   3552     bool error = false;
   3553     const struct mixer_ctl *ctl;
   3554     const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
   3555     const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
   3556     struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
   3557     struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
   3558     if (!ctl_left || !ctl_right) {
   3559         ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
   3560                       __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
   3561         return -EINVAL;
   3562     } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
   3563             || (mixer_ctl_get_num_values(ctl_right) != 1)) {
   3564         ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
   3565                               __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
   3566         return -EINVAL;
   3567     }
   3568     if (ramp_up) {
   3569         start_gain = 0;
   3570         end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
   3571         step = +1;
   3572         backup_gain = end_gain;
   3573     } else {
   3574         // using same gain on left and right
   3575         const int left_gain = mixer_ctl_get_value(ctl_left, 0);
   3576         start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
   3577         end_gain = 0;
   3578         step = -1;
   3579         backup_gain = start_gain;
   3580     }
   3581     for (i = start_gain ; i != (end_gain + step) ; i += step) {
   3582         //ALOGV("setting speaker gain to %d", i);
   3583         if (mixer_ctl_set_value(ctl_left, 0, i)) {
   3584             ALOGE("%s: error setting %s to %d during gain ramp",
   3585                     __func__, mixer_ctl_name_gain_left, i);
   3586             error = true;
   3587             break;
   3588         }
   3589         if (mixer_ctl_set_value(ctl_right, 0, i)) {
   3590             ALOGE("%s: error setting %s to %d during gain ramp",
   3591                     __func__, mixer_ctl_name_gain_right, i);
   3592             error = true;
   3593             break;
   3594         }
   3595         usleep(1000);
   3596     }
   3597     if (error) {
   3598         // an error occured during the ramp, let's still try to go back to a safe volume
   3599         if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
   3600             ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
   3601         }
   3602         if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
   3603             ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
   3604         }
   3605     }
   3606     return start_gain;
   3607 }
   3608 
   3609 int platform_set_swap_mixer(struct audio_device *adev, bool swap_channels)
   3610 {
   3611     const char *mixer_ctl_name = "Swap channel";
   3612     struct mixer_ctl *ctl;
   3613     const char *mixer_path;
   3614     struct platform_data *my_data = (struct platform_data *)adev->platform;
   3615 
   3616     // forced to set to swap, but device not rotated ... ignore set
   3617     if (swap_channels && !my_data->speaker_lr_swap)
   3618         return 0;
   3619 
   3620     ALOGV("%s:", __func__);
   3621 
   3622     if (swap_channels) {
   3623         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
   3624         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
   3625     } else {
   3626         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
   3627         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
   3628     }
   3629 
   3630     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   3631     if (!ctl) {
   3632         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
   3633         return -EINVAL;
   3634     }
   3635 
   3636     if (mixer_ctl_set_value(ctl, 0, swap_channels) < 0) {
   3637         ALOGE("%s: Could not set reverse cotrol %d",__func__, swap_channels);
   3638         return -EINVAL;
   3639     }
   3640 
   3641     ALOGV("platfor_force_swap_channel :: Channel orientation ( %s ) ",
   3642            swap_channels?"R --> L":"L --> R");
   3643 
   3644     return 0;
   3645 }
   3646 
   3647 int platform_check_and_set_swap_lr_channels(struct audio_device *adev, bool swap_channels)
   3648 {
   3649     // only update if there is active pcm playback on speaker
   3650     struct audio_usecase *usecase;
   3651     struct listnode *node;
   3652     struct platform_data *my_data = (struct platform_data *)adev->platform;
   3653 
   3654     my_data->speaker_lr_swap = swap_channels;
   3655 
   3656     return platform_set_swap_channels(adev, swap_channels);
   3657 }
   3658 
   3659 int platform_set_swap_channels(struct audio_device *adev, bool swap_channels)
   3660 {
   3661     // only update if there is active pcm playback on speaker
   3662     struct audio_usecase *usecase;
   3663     struct listnode *node;
   3664     struct platform_data *my_data = (struct platform_data *)adev->platform;
   3665 
   3666     // do not swap channels in audio modes with concurrent capture and playback
   3667     // as this may break the echo reference
   3668     if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || (adev->mode == AUDIO_MODE_IN_CALL)) {
   3669         ALOGV("%s: will not swap due to audio mode %d", __func__, adev->mode);
   3670         return 0;
   3671     }
   3672 
   3673     list_for_each(node, &adev->usecase_list) {
   3674         usecase = node_to_item(node, struct audio_usecase, list);
   3675         if (usecase->type == PCM_PLAYBACK &&
   3676                 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
   3677             /*
   3678              * If acdb tuning is different for SPEAKER_REVERSE, it is must
   3679              * to perform device switch to disable the current backend to
   3680              * enable it with new acdb data.
   3681              */
   3682             if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
   3683                 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
   3684                 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
   3685                 select_devices(adev, usecase->id);
   3686                 if (initial_skpr_gain != -EINVAL)
   3687                     ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
   3688 
   3689             } else {
   3690                 platform_set_swap_mixer(adev, swap_channels);
   3691             }
   3692             break;
   3693         }
   3694     }
   3695 
   3696     return 0;
   3697 }
   3698 
   3699 static struct amp_db_and_gain_table tbl_mapping[MAX_VOLUME_CAL_STEPS];
   3700 static int num_gain_tbl_entry = 0;
   3701 
   3702 bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry) {
   3703 
   3704     ALOGV("%s: enter .. add %f %f %d", __func__, tbl_entry->amp, tbl_entry->db, tbl_entry->level);
   3705     if (num_gain_tbl_entry == -1) {
   3706         ALOGE("%s: num entry beyond valid step levels or corrupted..rejecting custom mapping",
   3707                __func__);
   3708         return false;
   3709     }
   3710 
   3711     if (num_gain_tbl_entry >= MAX_VOLUME_CAL_STEPS) {
   3712         ALOGE("%s: max entry reached max[%d] current index[%d]  .. rejecting", __func__,
   3713                MAX_VOLUME_CAL_STEPS, num_gain_tbl_entry);
   3714         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
   3715         return false;
   3716     }
   3717 
   3718     if (num_gain_tbl_entry > 0 && tbl_mapping[num_gain_tbl_entry - 1].amp >= tbl_entry->amp) {
   3719         ALOGE("%s: value not in ascending order .. rejecting custom mapping", __func__);
   3720         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
   3721         return false;
   3722     }
   3723 
   3724     tbl_mapping[num_gain_tbl_entry] = *tbl_entry;
   3725     ++num_gain_tbl_entry;
   3726 
   3727     return true;
   3728 }
   3729 
   3730 int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
   3731                                     int table_size) {
   3732     int itt = 0;
   3733     ALOGV("platform_get_gain_level_mapping called ");
   3734 
   3735     if (num_gain_tbl_entry <= 0 || num_gain_tbl_entry > MAX_VOLUME_CAL_STEPS) {
   3736         ALOGD("%s: empty or currupted gain_mapping_table", __func__);
   3737         return 0;
   3738     }
   3739 
   3740     for (; itt < num_gain_tbl_entry && itt <= table_size; itt++) {
   3741         mapping_tbl[itt] = tbl_mapping[itt];
   3742         ALOGV("%s: added amp[%f] db[%f] level[%d]", __func__,
   3743                mapping_tbl[itt].amp, mapping_tbl[itt].db, mapping_tbl[itt].level);
   3744     }
   3745 
   3746     return num_gain_tbl_entry;
   3747 }
   3748 
   3749 int platform_snd_card_update(void *platform, card_status_t status)
   3750 {
   3751     struct platform_data *my_data = (struct platform_data *)platform;
   3752     struct audio_device *adev = my_data->adev;
   3753 
   3754     if (status == CARD_STATUS_ONLINE) {
   3755         if (my_data->acdb_send_custom_top)
   3756             my_data->acdb_send_custom_top();
   3757     }
   3758     return 0;
   3759 }
   3760 
   3761 /*
   3762  * configures afe with bit width and Sample Rate
   3763  */
   3764 int platform_set_backend_cfg(const struct audio_device* adev,
   3765                              snd_device_t snd_device,
   3766                              const struct audio_backend_cfg *backend_cfg)
   3767 {
   3768 
   3769     int ret = 0;
   3770     const int backend_idx = platform_get_backend_index(snd_device);
   3771     struct platform_data *my_data = (struct platform_data *)adev->platform;
   3772     const unsigned int bit_width = backend_cfg->bit_width;
   3773     const unsigned int sample_rate = backend_cfg->sample_rate;
   3774     const unsigned int channels = backend_cfg->channels;
   3775     const audio_format_t format = backend_cfg->format;
   3776     const bool passthrough_enabled = backend_cfg->passthrough_enabled;
   3777 
   3778 
   3779     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
   3780           ", backend_idx %d device (%s)", __func__,  bit_width,
   3781           sample_rate, channels, backend_idx,
   3782           platform_get_snd_device_name(snd_device));
   3783 
   3784     if ((my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl) &&
   3785         (bit_width != my_data->current_backend_cfg[backend_idx].bit_width)) {
   3786 
   3787         struct  mixer_ctl *ctl = NULL;
   3788         ctl = mixer_get_ctl_by_name(adev->mixer,
   3789                                     my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
   3790         if (!ctl) {
   3791             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
   3792                   __func__,
   3793                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
   3794             return -EINVAL;
   3795         }
   3796 
   3797         if (bit_width == 24) {
   3798             if (format == AUDIO_FORMAT_PCM_24_BIT_PACKED)
   3799                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_3LE");
   3800             else
   3801                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_LE");
   3802         } else if (bit_width == 32) {
   3803             ret = mixer_ctl_set_enum_by_string(ctl, "S32_LE");
   3804         } else {
   3805             ret = mixer_ctl_set_enum_by_string(ctl, "S16_LE");
   3806         }
   3807         if ( ret < 0) {
   3808             ALOGE("%s:becf: afe: fail for %s mixer set to %d bit for %x format", __func__,
   3809                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
   3810         } else {
   3811             my_data->current_backend_cfg[backend_idx].bit_width = bit_width;
   3812             ALOGD("%s:becf: afe: %s mixer set to %d bit for %x format", __func__,
   3813                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
   3814         }
   3815         /* set the ret as 0 and not pass back to upper layer */
   3816         ret = 0;
   3817     }
   3818 
   3819     if (passthrough_enabled || ((my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl) &&
   3820                                 (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate))) {
   3821         char *rate_str = NULL;
   3822         struct  mixer_ctl *ctl = NULL;
   3823 
   3824         switch (sample_rate) {
   3825             case 32000:
   3826                 if (passthrough_enabled) {
   3827                     rate_str = "KHZ_32";
   3828                     break;
   3829                 }
   3830             case 8000:
   3831             case 11025:
   3832             case 16000:
   3833             case 22050:
   3834             case 48000:
   3835                 rate_str = "KHZ_48";
   3836                 break;
   3837             case 44100:
   3838                 rate_str = "KHZ_44P1";
   3839                 break;
   3840             case 64000:
   3841             case 96000:
   3842                 rate_str = "KHZ_96";
   3843                 break;
   3844             case 88200:
   3845                 rate_str = "KHZ_88P2";
   3846                 break;
   3847             case 176400:
   3848                 rate_str = "KHZ_176P4";
   3849                 break;
   3850             case 192000:
   3851                 rate_str = "KHZ_192";
   3852                 break;
   3853             case 352800:
   3854                 rate_str = "KHZ_352P8";
   3855                 break;
   3856             case 384000:
   3857                 rate_str = "KHZ_384";
   3858                 break;
   3859             case 144000:
   3860                 if (passthrough_enabled) {
   3861                     rate_str = "KHZ_144";
   3862                     break;
   3863                 }
   3864             default:
   3865                 rate_str = "KHZ_48";
   3866                 break;
   3867         }
   3868 
   3869         ctl = mixer_get_ctl_by_name(adev->mixer,
   3870                                     my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
   3871         if(!ctl) {
   3872             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
   3873                   __func__,
   3874                   my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
   3875             return -EINVAL;
   3876         }
   3877 
   3878         ALOGD("%s:becf: afe: %s set to %s", __func__,
   3879               my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl, rate_str);
   3880         mixer_ctl_set_enum_by_string(ctl, rate_str);
   3881         my_data->current_backend_cfg[backend_idx].sample_rate = sample_rate;
   3882     }
   3883     if ((my_data->current_backend_cfg[backend_idx].channels_mixer_ctl) &&
   3884         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
   3885         struct  mixer_ctl *ctl = NULL;
   3886         char *channel_cnt_str = NULL;
   3887 
   3888         switch (channels) {
   3889             case 8:
   3890                 channel_cnt_str = "Eight"; break;
   3891             case 7:
   3892                 channel_cnt_str = "Seven"; break;
   3893             case 6:
   3894                 channel_cnt_str = "Six"; break;
   3895             case 5:
   3896                 channel_cnt_str = "Five"; break;
   3897             case 4:
   3898                 channel_cnt_str = "Four"; break;
   3899             case 3:
   3900                 channel_cnt_str = "Three"; break;
   3901             case 1:
   3902                 channel_cnt_str = "One"; break;
   3903             case 2:
   3904             default:
   3905                 channel_cnt_str = "Two"; break;
   3906         }
   3907 
   3908         ctl = mixer_get_ctl_by_name(adev->mixer,
   3909                                     my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
   3910         if (!ctl) {
   3911             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
   3912                   __func__,
   3913                   my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
   3914             return -EINVAL;
   3915         }
   3916         mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
   3917         my_data->current_backend_cfg[backend_idx].channels = channels;
   3918 
   3919         // skip EDID configuration for HDMI backend
   3920 
   3921         ALOGD("%s:becf: afe: %s set to %s", __func__,
   3922               my_data->current_backend_cfg[backend_idx].channels_mixer_ctl,
   3923               channel_cnt_str);
   3924     }
   3925 
   3926     // skip set ext_display format mixer control
   3927     return ret;
   3928 }
   3929 
   3930 static int platform_get_snd_device_bit_width(snd_device_t snd_device)
   3931 {
   3932     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
   3933         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
   3934         return CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   3935     }
   3936 
   3937     return backend_bit_width_table[snd_device];
   3938 }
   3939 
   3940 /*
   3941  * return backend_idx on which voice call is active
   3942  */
   3943 static int platform_get_voice_call_backend(struct audio_device* adev)
   3944 {
   3945     struct audio_usecase *uc = NULL;
   3946     struct listnode *node;
   3947     snd_device_t out_snd_device = SND_DEVICE_NONE;
   3948 
   3949     int backend_idx = -1;
   3950 
   3951     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
   3952         list_for_each(node, &adev->usecase_list) {
   3953             uc =  node_to_item(node, struct audio_usecase, list);
   3954             if (uc && uc->type == VOICE_CALL && uc->stream.out) {
   3955                 out_snd_device = platform_get_output_snd_device(adev->platform,
   3956                                                         uc->stream.out->devices);
   3957                 backend_idx = platform_get_backend_index(out_snd_device);
   3958                 break;
   3959             }
   3960         }
   3961     }
   3962     return backend_idx;
   3963 }
   3964 
   3965 /*
   3966  * goes through all the current usecases and picks the highest
   3967  * bitwidth & samplerate
   3968  */
   3969 static bool platform_check_capture_backend_cfg(struct audio_device* adev,
   3970                                    int backend_idx,
   3971                                    struct audio_backend_cfg *backend_cfg)
   3972 {
   3973     bool backend_change = false;
   3974     unsigned int bit_width;
   3975     unsigned int sample_rate;
   3976     unsigned int channels;
   3977     struct platform_data *my_data = (struct platform_data *)adev->platform;
   3978 
   3979     bit_width = backend_cfg->bit_width;
   3980     sample_rate = backend_cfg->sample_rate;
   3981     channels = backend_cfg->channels;
   3982 
   3983     ALOGV("%s:txbecf: afe: Codec selected backend: %d current bit width: %d and "
   3984           "sample rate: %d, channels %d",__func__,backend_idx, bit_width,
   3985           sample_rate, channels);
   3986 
   3987     // For voice calls use default configuration i.e. 16b/48K, only applicable to
   3988     // default backend
   3989     // force routing is not required here, caller will do it anyway
   3990     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
   3991         ALOGW("%s:txbecf: afe: Use default bw and sr for voice/voip calls and "
   3992               "for unprocessed/camera source", __func__);
   3993         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   3994         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   3995     }
   3996 
   3997     if (backend_idx == USB_AUDIO_TX_BACKEND) {
   3998         audio_extn_usb_is_config_supported(&bit_width, &sample_rate, &channels, false);
   3999         ALOGV("%s:txbecf: afe: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
   4000               __func__, bit_width, sample_rate, channels);
   4001     }
   4002 
   4003     ALOGV("%s:txbecf: afe: Codec selected backend: %d updated bit width: %d and "
   4004           "sample rate: %d", __func__, backend_idx, bit_width, sample_rate);
   4005 
   4006     // Force routing if the expected bitwdith or samplerate
   4007     // is not same as current backend comfiguration
   4008     if ((bit_width != my_data->current_backend_cfg[backend_idx].bit_width) ||
   4009         (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate) ||
   4010         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
   4011         backend_cfg->bit_width = bit_width;
   4012         backend_cfg->sample_rate= sample_rate;
   4013         backend_cfg->channels = channels;
   4014         backend_change = true;
   4015         ALOGI("%s:txbecf: afe: Codec backend needs to be updated. new bit width: %d "
   4016               "new sample rate: %d new channel: %d",
   4017               __func__, backend_cfg->bit_width,
   4018               backend_cfg->sample_rate, backend_cfg->channels);
   4019     }
   4020 
   4021     return backend_change;
   4022 }
   4023 
   4024 static void pick_playback_cfg_for_uc(struct audio_device *adev,
   4025                                      struct audio_usecase *usecase,
   4026                                      snd_device_t snd_device,
   4027                                      unsigned int *bit_width,
   4028                                      unsigned int *sample_rate,
   4029                                      unsigned int *channels)
   4030 {
   4031     int i =0;
   4032     struct listnode *node;
   4033     list_for_each(node, &adev->usecase_list) {
   4034         struct audio_usecase *uc;
   4035         uc = node_to_item(node, struct audio_usecase, list);
   4036         struct stream_out *out = (struct stream_out*) uc->stream.out;
   4037         if (uc->type == PCM_PLAYBACK && out && usecase != uc) {
   4038             unsigned int out_channels = audio_channel_count_from_out_mask(out->channel_mask);
   4039             ALOGV("%s:napb: (%d) - (%s)id (%d) sr %d bw "
   4040                   "(%d) ch (%d) device %s", __func__, i++, use_case_table[uc->id],
   4041                   uc->id, out->sample_rate,
   4042                   pcm_format_to_bits(out->config.format), out_channels,
   4043                   platform_get_snd_device_name(uc->out_snd_device));
   4044 
   4045             if (platform_check_backends_match(snd_device, uc->out_snd_device)) {
   4046                 if (*bit_width < pcm_format_to_bits(out->config.format))
   4047                     *bit_width = pcm_format_to_bits(out->config.format);
   4048                 if (*sample_rate < out->sample_rate)
   4049                     *sample_rate = out->sample_rate;
   4050                 if (out->sample_rate < OUTPUT_SAMPLING_RATE_44100)
   4051                     *sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   4052                 if (*channels < out_channels)
   4053                     *channels = out_channels;
   4054             }
   4055         }
   4056     }
   4057     return;
   4058 }
   4059 
   4060 static void headset_is_config_supported(unsigned int *bit_width,
   4061                                         unsigned int *sample_rate,
   4062                                         unsigned int *channels) {
   4063     switch (*bit_width) {
   4064         case 16:
   4065         case 24:
   4066             break;
   4067         default:
   4068             *bit_width = 16;
   4069             break;
   4070     }
   4071 
   4072     if (*sample_rate > 192000) {
   4073         *sample_rate = 192000;
   4074     }
   4075 
   4076     if (*channels > 2) {
   4077         *channels = 2;
   4078     }
   4079 }
   4080 
   4081 static bool platform_check_playback_backend_cfg(struct audio_device* adev,
   4082                                              struct audio_usecase* usecase,
   4083                                              snd_device_t snd_device,
   4084                                              struct audio_backend_cfg *backend_cfg)
   4085 {
   4086     bool backend_change = false;
   4087     unsigned int bit_width;
   4088     unsigned int sample_rate;
   4089     unsigned int channels;
   4090     int backend_idx = DEFAULT_CODEC_BACKEND;
   4091     unsigned long service_interval = 0; // 0 is invalid
   4092     struct platform_data *my_data = (struct platform_data *)adev->platform;
   4093 
   4094     if (snd_device == SND_DEVICE_OUT_BT_SCO ||
   4095         snd_device == SND_DEVICE_OUT_BT_SCO_WB) {
   4096         backend_change = false;
   4097         return backend_change;
   4098     }
   4099 
   4100     backend_idx = platform_get_backend_index(snd_device);
   4101     bit_width = backend_cfg->bit_width;
   4102     sample_rate = backend_cfg->sample_rate;
   4103     channels = backend_cfg->channels;
   4104 
   4105     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
   4106           ", backend_idx %d usecase = %d device (%s)", __func__, bit_width,
   4107           sample_rate, channels, backend_idx, usecase->id,
   4108           platform_get_snd_device_name(snd_device));
   4109 
   4110     if (backend_idx == platform_get_voice_call_backend(adev)) {
   4111         ALOGW("%s:becf: afe:Use default bw and sr for voice/voip calls ",
   4112               __func__);
   4113         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   4114         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   4115         channels = CODEC_BACKEND_DEFAULT_CHANNELS;
   4116     } else {
   4117         /*
   4118          * The backend should be configured at highest bit width and/or
   4119          * sample rate amongst all playback usecases.
   4120          * If the selected sample rate and/or bit width differ with
   4121          * current backend sample rate and/or bit width, then, we set the
   4122          * backend re-configuration flag.
   4123          *
   4124          * Exception: 16 bit playbacks is allowed through 16 bit/48/44.1 khz backend only
   4125          */
   4126         pick_playback_cfg_for_uc(adev, usecase, snd_device,
   4127                                  &bit_width,
   4128                                  &sample_rate,
   4129                                  &channels);
   4130     }
   4131 
   4132     switch (backend_idx) {
   4133         case USB_AUDIO_RX_BACKEND:
   4134             audio_extn_usb_is_config_supported(&bit_width,
   4135                                                &sample_rate, &channels, true);
   4136             if (platform_get_usb_service_interval(adev->platform, true,
   4137                                                   &service_interval) == 0) {
   4138                 /* overwrite with best altset for this service interval */
   4139                 int ret =
   4140                         audio_extn_usb_altset_for_service_interval(true /*playback*/,
   4141                                                                    service_interval,
   4142                                                                    &bit_width,
   4143                                                                    &sample_rate,
   4144                                                                    &channels);
   4145                 if (ret < 0) {
   4146                     ALOGE("Failed to find altset for service interval %lu, skip reconfig",
   4147                           service_interval);
   4148                     return false;
   4149                 }
   4150             }
   4151             ALOGV("%s: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
   4152                   __func__, bit_width, sample_rate, channels);
   4153             break;
   4154         case HEADPHONE_BACKEND:
   4155             headset_is_config_supported(&bit_width, &sample_rate, &channels);
   4156             break;
   4157         case DEFAULT_CODEC_BACKEND:
   4158         default:
   4159             bit_width = platform_get_snd_device_bit_width(snd_device);
   4160             sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   4161             channels = CODEC_BACKEND_DEFAULT_CHANNELS;
   4162             break;
   4163     }
   4164 
   4165     ALOGV("%s:becf: afe: Codec selected backend: %d updated bit width: %d and"
   4166           "sample rate: %d",
   4167           __func__, backend_idx , bit_width, sample_rate);
   4168 
   4169     // Force routing if the expected bitwdith or samplerate
   4170     // is not same as current backend comfiguration
   4171     if (bit_width != my_data->current_backend_cfg[backend_idx].bit_width ||
   4172         sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate ||
   4173         channels != my_data->current_backend_cfg[backend_idx].channels) {
   4174         backend_cfg->bit_width = bit_width;
   4175         backend_cfg->sample_rate = sample_rate;
   4176         backend_cfg->channels = channels;
   4177         backend_cfg->passthrough_enabled = false;
   4178         backend_change = true;
   4179         ALOGV("%s:becf: afe: Codec backend needs to be updated. new bit width: %d"
   4180               "new sample rate: %d new channels: %d",
   4181               __func__, backend_cfg->bit_width, backend_cfg->sample_rate, backend_cfg->channels);
   4182     }
   4183 
   4184     return backend_change;
   4185 }
   4186 
   4187 bool platform_check_and_set_playback_backend_cfg(struct audio_device* adev,
   4188     struct audio_usecase *usecase, snd_device_t snd_device)
   4189 {
   4190     int backend_idx = DEFAULT_CODEC_BACKEND;
   4191     int new_snd_devices[SND_DEVICE_OUT_END];
   4192     int i, num_devices = 1;
   4193     bool ret = false;
   4194     struct platform_data *my_data = (struct platform_data *)adev->platform;
   4195     struct audio_backend_cfg backend_cfg;
   4196 
   4197     backend_idx = platform_get_backend_index(snd_device);
   4198 
   4199     backend_cfg.bit_width = pcm_format_to_bits(usecase->stream.out->config.format);
   4200     backend_cfg.sample_rate = usecase->stream.out->sample_rate;
   4201     backend_cfg.format = usecase->stream.out->format;
   4202     backend_cfg.channels = audio_channel_count_from_out_mask(usecase->stream.out->channel_mask);
   4203     /*this is populated by check_codec_backend_cfg hence set default value to false*/
   4204     backend_cfg.passthrough_enabled = false;
   4205 
   4206     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
   4207           ", backend_idx %d usecase = %d device (%s)", __func__, backend_cfg.bit_width,
   4208           backend_cfg.sample_rate, backend_cfg.channels, backend_idx, usecase->id,
   4209           platform_get_snd_device_name(snd_device));
   4210 
   4211     if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices) < 0)
   4212         new_snd_devices[0] = snd_device;
   4213 
   4214     for (i = 0; i < num_devices; i++) {
   4215         ALOGV("%s: new_snd_devices[%d] is %d", __func__, i, new_snd_devices[i]);
   4216         if ((platform_check_playback_backend_cfg(adev, usecase, new_snd_devices[i],
   4217                                                  &backend_cfg))) {
   4218             platform_set_backend_cfg(adev, new_snd_devices[i],
   4219                                      &backend_cfg);
   4220             ret = true;
   4221         }
   4222     }
   4223     return ret;
   4224 }
   4225 
   4226 bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev,
   4227     struct audio_usecase *usecase, snd_device_t snd_device)
   4228 {
   4229     int backend_idx = platform_get_backend_index(snd_device);
   4230     int ret = 0;
   4231     struct audio_backend_cfg backend_cfg;
   4232     memset(&backend_cfg, 0, sizeof(struct audio_backend_cfg));
   4233 
   4234     if (usecase->type == PCM_CAPTURE) {
   4235         backend_cfg.format = usecase->stream.in->format;
   4236         backend_cfg.channels = audio_channel_count_from_in_mask(usecase->stream.in->channel_mask);
   4237     } else {
   4238         backend_cfg.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
   4239         backend_cfg.sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
   4240         backend_cfg.format = AUDIO_FORMAT_PCM_16_BIT;
   4241         backend_cfg.channels = 1;
   4242     }
   4243 
   4244     ALOGV("%s:txbecf: afe: bitwidth %d, samplerate %d, channel %d"
   4245           ", backend_idx %d usecase = %d device (%s)", __func__,
   4246           backend_cfg.bit_width,
   4247           backend_cfg.sample_rate,
   4248           backend_cfg.channels,
   4249           backend_idx, usecase->id,
   4250           platform_get_snd_device_name(snd_device));
   4251 
   4252     if (platform_check_capture_backend_cfg(adev, backend_idx, &backend_cfg)) {
   4253         ret = platform_set_backend_cfg(adev, snd_device,
   4254                                        &backend_cfg);
   4255         if(!ret)
   4256             return true;
   4257     }
   4258 
   4259     return false;
   4260 }
   4261 
   4262 static int max_be_dai_names = 0;
   4263 static const struct be_dai_name_struct *be_dai_name_table;
   4264 
   4265 /*
   4266  * Retrieves the be_dai_name_table from kernel to enable a mapping
   4267  * between sound device hw interfaces and backend IDs. This allows HAL to
   4268  * specify the backend a specific calibration is needed for.
   4269  */
   4270 static int init_be_dai_name_table(struct audio_device *adev)
   4271 {
   4272     const char *mixer_ctl_name = "Backend DAI Name Table";
   4273     struct mixer_ctl *ctl;
   4274     int i, j, ret, size;
   4275     bool valid_hw_interface;
   4276 
   4277     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
   4278     if (!ctl) {
   4279         ALOGE("%s: Could not get ctl for mixer name %s\n",
   4280                __func__, mixer_ctl_name);
   4281         ret = -EINVAL;
   4282         goto done;
   4283     }
   4284 
   4285     mixer_ctl_update(ctl);
   4286 
   4287     size = mixer_ctl_get_num_values(ctl);
   4288     if (size <= 0){
   4289         ALOGE("%s: Failed to get %s size %d\n",
   4290                __func__, mixer_ctl_name, size);
   4291         ret = -EFAULT;
   4292         goto done;
   4293     }
   4294 
   4295     be_dai_name_table =
   4296             (const struct be_dai_name_struct *)calloc(1, size);
   4297     if (be_dai_name_table == NULL) {
   4298         ALOGE("%s: Failed to allocate memory for %s\n",
   4299                __func__, mixer_ctl_name);
   4300         ret = -ENOMEM;
   4301         goto freeMem;
   4302     }
   4303 
   4304     ret = mixer_ctl_get_array(ctl, (void *)be_dai_name_table, size);
   4305     if (ret) {
   4306         ALOGE("%s: Failed to get %s, ret %d\n",
   4307                __func__, mixer_ctl_name, ret);
   4308         ret = -EFAULT;
   4309         goto freeMem;
   4310     }
   4311 
   4312     if (be_dai_name_table != NULL) {
   4313         max_be_dai_names = size / sizeof(struct be_dai_name_struct);
   4314         ALOGV("%s: Successfully got %s, number of be dais is %d\n",
   4315               __func__, mixer_ctl_name, max_be_dai_names);
   4316         ret = 0;
   4317     } else {
   4318         ALOGE("%s: Failed to get %s\n", __func__, mixer_ctl_name);
   4319         ret = -EFAULT;
   4320         goto freeMem;
   4321     }
   4322 
   4323     /*
   4324      * Validate all sound devices have a valid backend set to catch
   4325      * errors for uncommon sound devices
   4326      */
   4327     for (i = 0; i < SND_DEVICE_MAX; i++) {
   4328         valid_hw_interface = false;
   4329 
   4330         if (hw_interface_table[i] == NULL) {
   4331             ALOGW("%s: sound device %s has no hw interface set\n",
   4332                   __func__, platform_get_snd_device_name(i));
   4333             continue;
   4334         }
   4335 
   4336         for (j = 0; j < max_be_dai_names; j++) {
   4337             if (strcmp(hw_interface_table[i], be_dai_name_table[j].be_name)
   4338                 == 0) {
   4339                 valid_hw_interface = true;
   4340                 break;
   4341             }
   4342         }
   4343         if (!valid_hw_interface)
   4344             ALOGD("%s: sound device %s does not have a valid hw interface set "
   4345                   "(disregard for combo devices) %s\n",
   4346                   __func__, platform_get_snd_device_name(i),
   4347                   hw_interface_table[i]);
   4348     }
   4349 
   4350     goto done;
   4351 
   4352 freeMem:
   4353     if (be_dai_name_table) {
   4354         free((void *)be_dai_name_table);
   4355         be_dai_name_table = NULL;
   4356     }
   4357 
   4358 done:
   4359     return ret;
   4360 }
   4361 
   4362 int platform_get_snd_device_backend_index(snd_device_t device)
   4363 {
   4364     int i, be_dai_id;
   4365     const char * hw_interface_name = NULL;
   4366 
   4367     ALOGV("%s: enter with device %d\n", __func__, device);
   4368 
   4369     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
   4370         ALOGE("%s: Invalid snd_device = %d",
   4371               __func__, device);
   4372         be_dai_id = -EINVAL;
   4373         goto done;
   4374     }
   4375 
   4376     /* Get string value of necessary backend for device */
   4377     hw_interface_name = hw_interface_table[device];
   4378     if (hw_interface_name == NULL) {
   4379         ALOGE("%s: no hw_interface set for device %d\n", __func__, device);
   4380         be_dai_id = -EINVAL;
   4381         goto done;
   4382     }
   4383 
   4384     /* Check if be dai name table was retrieved successfully */
   4385     if (be_dai_name_table == NULL) {
   4386         ALOGE("%s: BE DAI Name Table is not present\n", __func__);
   4387         be_dai_id = -EFAULT;
   4388         goto done;
   4389     }
   4390 
   4391     /* Get backend ID for device specified */
   4392     for (i = 0; i < max_be_dai_names; i++) {
   4393         if (strcmp(hw_interface_name, be_dai_name_table[i].be_name) == 0) {
   4394             be_dai_id = be_dai_name_table[i].be_id;
   4395             goto done;
   4396         }
   4397     }
   4398     ALOGE("%s: no interface matching name %s\n", __func__, hw_interface_name);
   4399     be_dai_id = -EINVAL;
   4400     goto done;
   4401 
   4402 done:
   4403     return be_dai_id;
   4404 }
   4405 
   4406 void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
   4407                                                 unsigned int stream_sr, int* sample_rate)
   4408 {
   4409     struct platform_data* my_data = (struct platform_data *)platform;
   4410     int backend_idx = platform_get_backend_index(snd_device);
   4411     int device_sr = my_data->current_backend_cfg[backend_idx].sample_rate;
   4412     /*
   4413      *Check if device SR is multiple of 8K or 11.025 Khz
   4414      *check if the stream SR is multiple of same base, if yes
   4415      *then have copp SR equal to stream SR, this ensures that
   4416      *post processing happens at stream SR, else have
   4417      *copp SR equal to device SR.
   4418      */
   4419     if (!(((sample_rate_multiple(device_sr, SAMPLE_RATE_8000)) &&
   4420            (sample_rate_multiple(stream_sr, SAMPLE_RATE_8000))) ||
   4421           ((sample_rate_multiple(device_sr, SAMPLE_RATE_11025)) &&
   4422            (sample_rate_multiple(stream_sr, SAMPLE_RATE_11025))))) {
   4423         *sample_rate = device_sr;
   4424     } else
   4425         *sample_rate = stream_sr;
   4426 
   4427     ALOGI("sn_device %d device sr %d stream sr %d copp sr %d", snd_device, device_sr, stream_sr
   4428           , *sample_rate);
   4429 
   4430 }
   4431 
   4432 // called from info parser
   4433 void platform_add_app_type(const char *uc_type,
   4434                            const char *mode,
   4435                            int bw,
   4436                            int app_type, int max_rate) {
   4437     struct app_type_entry *ap =
   4438             (struct app_type_entry *)calloc(1, sizeof(struct app_type_entry));
   4439 
   4440     if (!ap) {
   4441         ALOGE("%s failed to allocate mem for app type", __func__);
   4442         return;
   4443     }
   4444 
   4445     ap->uc_type = -1;
   4446     for (int i=0; i<USECASE_TYPE_MAX; i++) {
   4447         if (!strcmp(uc_type, usecase_type_index[i].name)) {
   4448             ap->uc_type = usecase_type_index[i].index;
   4449             break;
   4450         }
   4451     }
   4452 
   4453     if (ap->uc_type == -1) {
   4454         free(ap);
   4455         return;
   4456     }
   4457 
   4458     ALOGI("%s uc %s mode %s bw %d app_type %d max_rate %d",
   4459           __func__, uc_type, mode, bw, app_type, max_rate);
   4460     ap->bit_width = bw;
   4461     ap->app_type = app_type;
   4462     ap->max_rate = max_rate;
   4463     ap->mode = strdup(mode);
   4464     list_add_tail(&app_type_entry_list, &ap->node);
   4465 }
   4466 
   4467 
   4468 int platform_get_default_app_type_v2(void *platform __unused,
   4469                                      usecase_type_t type,
   4470                                      int *app_type )
   4471 {
   4472     if (type == PCM_PLAYBACK)
   4473         *app_type = DEFAULT_APP_TYPE_RX_PATH;
   4474     else
   4475         *app_type = DEFAULT_APP_TYPE_TX_PATH;
   4476     return 0;
   4477 }
   4478 
   4479 int platform_get_app_type_v2(void *platform,
   4480                              usecase_type_t uc_type,
   4481                              const char *mode,
   4482                              int bw, int sr __unused,
   4483                              int *app_type)
   4484 {
   4485     struct listnode *node;
   4486     struct app_type_entry *entry;
   4487     *app_type = -1;
   4488 
   4489     ALOGV("%s find match for uc %d mode %s bw %d rate %d",
   4490           __func__, uc_type, mode, bw, sr);
   4491     list_for_each(node, &app_type_entry_list) {
   4492         entry = node_to_item(node, struct app_type_entry, node);
   4493         ALOGV("%s uc %d mode %s bw %d app_type %d max_rate %d",
   4494               __func__, entry->uc_type, entry->mode, entry->bit_width,
   4495               entry->app_type, entry->max_rate);
   4496         if (entry->bit_width == bw &&
   4497             entry->uc_type == uc_type &&
   4498             sr <= entry->max_rate &&
   4499             entry->mode && !strcmp(mode, entry->mode)) {
   4500             ALOGV("%s found match %d", __func__, entry->app_type);
   4501             *app_type = entry->app_type;
   4502             break;
   4503         }
   4504     }
   4505 
   4506     if (*app_type == -1) {
   4507         ALOGV("%s no match found, return default", __func__);
   4508         return platform_get_default_app_type_v2(platform, uc_type, app_type);
   4509     }
   4510     return 0;
   4511 }
   4512 
   4513 int platform_set_sidetone(struct audio_device *adev,
   4514                           snd_device_t out_snd_device,
   4515                           bool enable, char *str)
   4516 {
   4517     int ret;
   4518     if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
   4519         out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
   4520             ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
   4521             if (ret)
   4522                 ALOGI("%s: usb device %d does not support device sidetone\n",
   4523                   __func__, out_snd_device);
   4524     } else {
   4525         ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
   4526               __func__, out_snd_device, str);
   4527         if (enable)
   4528             audio_route_apply_and_update_path(adev->audio_route, str);
   4529         else
   4530             audio_route_reset_and_update_path(adev->audio_route, str);
   4531     }
   4532     return 0;
   4533 }
   4534 
   4535 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
   4536                               int *fd __unused, uint32_t *size __unused)
   4537 {
   4538 #if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845)
   4539     struct platform_data *my_data = (struct platform_data *)platform;
   4540     struct audio_device *adev = my_data->adev;
   4541     int hw_fd = -1;
   4542     char dev_name[128];
   4543     struct snd_pcm_mmap_fd mmap_fd;
   4544     memset(&mmap_fd, 0, sizeof(mmap_fd));
   4545     mmap_fd.dir = dir;
   4546     snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
   4547              adev->snd_card, HWDEP_FE_BASE+fe_dev);
   4548     hw_fd = open(dev_name, O_RDONLY);
   4549     if (hw_fd < 0) {
   4550         ALOGE("fe hw dep node open %d/%d failed", adev->snd_card, fe_dev);
   4551         return -1;
   4552     }
   4553     if (ioctl(hw_fd, SNDRV_PCM_IOCTL_MMAP_DATA_FD, &mmap_fd) < 0) {
   4554         ALOGE("fe hw dep node ioctl failed");
   4555         close(hw_fd);
   4556         return -1;
   4557     }
   4558     *fd = mmap_fd.fd;
   4559     *size = mmap_fd.size;
   4560     close(hw_fd); // mmap_fd should still be valid
   4561     return 0;
   4562 #else
   4563     return -1;
   4564 #endif
   4565 }
   4566 
   4567 bool platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id)
   4568 {
   4569     bool needs_event = false;
   4570 
   4571     switch (uc_id) {
   4572     /* concurrent capture usecases which needs event */
   4573     case USECASE_AUDIO_RECORD:
   4574     case USECASE_AUDIO_RECORD_LOW_LATENCY:
   4575     case USECASE_AUDIO_RECORD_MMAP:
   4576     case USECASE_AUDIO_RECORD_HIFI:
   4577     case USECASE_AUDIO_RECORD_VOIP:
   4578     case USECASE_VOICEMMODE1_CALL:
   4579     case USECASE_VOICEMMODE2_CALL:
   4580     /* concurrent playback usecases that needs event */
   4581     case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
   4582     case USECASE_AUDIO_PLAYBACK_OFFLOAD:
   4583         needs_event = true;
   4584         break;
   4585     default:
   4586         ALOGV("%s:usecase_id[%d] no need to raise event.", __func__, uc_id);
   4587     }
   4588     return needs_event;
   4589 }
   4590 
   4591 bool platform_snd_device_has_speaker(snd_device_t dev) {
   4592     int num_devs = 2;
   4593     snd_device_t split_devs[2] = {SND_DEVICE_NONE, SND_DEVICE_NONE};
   4594     if (platform_can_split_snd_device(dev, &num_devs, split_devs) == 0) {
   4595         return platform_snd_device_has_speaker(split_devs[0]) ||
   4596                 platform_snd_device_has_speaker(split_devs[1]);
   4597     }
   4598 
   4599     switch (dev) {
   4600         case SND_DEVICE_OUT_SPEAKER:
   4601         case SND_DEVICE_OUT_SPEAKER_SAFE:
   4602         case SND_DEVICE_OUT_SPEAKER_REVERSE:
   4603         case SND_DEVICE_OUT_SPEAKER_PROTECTED:
   4604         case SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED:
   4605         case SND_DEVICE_OUT_VOICE_SPEAKER_HFP:
   4606             return true;
   4607         default:
   4608             break;
   4609     }
   4610     return false;
   4611 }
   4612 
   4613 bool platform_set_microphone_characteristic(void *platform,
   4614                                             struct audio_microphone_characteristic_t mic) {
   4615     struct platform_data *my_data = (struct platform_data *)platform;
   4616     if (my_data->declared_mic_count >= AUDIO_MICROPHONE_MAX_COUNT) {
   4617         ALOGE("mic number is more than maximum number");
   4618         return false;
   4619     }
   4620     for (size_t ch = 0; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
   4621         mic.channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
   4622     }
   4623     my_data->microphones[my_data->declared_mic_count++] = mic;
   4624     return true;
   4625 }
   4626 
   4627 int platform_get_microphones(void *platform,
   4628                              struct audio_microphone_characteristic_t *mic_array,
   4629                              size_t *mic_count) {
   4630     struct platform_data *my_data = (struct platform_data *)platform;
   4631     if (mic_count == NULL) {
   4632         return -EINVAL;
   4633     }
   4634     if (mic_array == NULL) {
   4635         return -EINVAL;
   4636     }
   4637 
   4638     if (*mic_count == 0) {
   4639         *mic_count = my_data->declared_mic_count;
   4640         return 0;
   4641     }
   4642 
   4643     size_t max_mic_count = *mic_count;
   4644     size_t actual_mic_count = 0;
   4645     for (size_t i = 0; i < max_mic_count && i < my_data->declared_mic_count; i++) {
   4646         mic_array[i] = my_data->microphones[i];
   4647         actual_mic_count++;
   4648     }
   4649     *mic_count = actual_mic_count;
   4650     return 0;
   4651 }
   4652 
   4653 bool platform_set_microphone_map(void *platform, snd_device_t in_snd_device,
   4654                                  const struct mic_info *info) {
   4655     struct platform_data *my_data = (struct platform_data *)platform;
   4656     if (in_snd_device < SND_DEVICE_IN_BEGIN || in_snd_device >= SND_DEVICE_IN_END) {
   4657         ALOGE("%s: Sound device not valid", __func__);
   4658         return false;
   4659     }
   4660     size_t m_count = my_data->mic_map[in_snd_device].mic_count++;
   4661     if (m_count >= AUDIO_MICROPHONE_MAX_COUNT) {
   4662         ALOGE("%s: Microphone count is greater than max allowed value", __func__);
   4663         my_data->mic_map[in_snd_device].mic_count--;
   4664         return false;
   4665     }
   4666     my_data->mic_map[in_snd_device].microphones[m_count] = *info;
   4667     return true;
   4668 }
   4669 
   4670 int platform_get_active_microphones(void *platform, unsigned int channels,
   4671                                     audio_usecase_t uc_id,
   4672                                     struct audio_microphone_characteristic_t *mic_array,
   4673                                     size_t *mic_count) {
   4674     struct platform_data *my_data = (struct platform_data *)platform;
   4675     struct audio_usecase *usecase = get_usecase_from_list(my_data->adev, uc_id);
   4676     if (mic_count == NULL || mic_array == NULL || usecase == NULL) {
   4677         return -EINVAL;
   4678     }
   4679     size_t max_mic_count = my_data->declared_mic_count;
   4680     size_t actual_mic_count = 0;
   4681 
   4682     snd_device_t active_input_snd_device =
   4683             platform_get_input_snd_device(platform, usecase->stream.in->device);
   4684     if (active_input_snd_device == SND_DEVICE_NONE) {
   4685         ALOGI("%s: No active microphones found", __func__);
   4686         goto end;
   4687     }
   4688 
   4689     size_t  active_mic_count = my_data->mic_map[active_input_snd_device].mic_count;
   4690     struct mic_info *m_info = my_data->mic_map[active_input_snd_device].microphones;
   4691 
   4692     for (size_t i = 0; i < active_mic_count; i++) {
   4693         unsigned int channels_for_active_mic = channels;
   4694         if (channels_for_active_mic > m_info[i].channel_count) {
   4695             channels_for_active_mic = m_info[i].channel_count;
   4696         }
   4697         for (size_t j = 0; j < max_mic_count; j++) {
   4698             if (strcmp(my_data->microphones[j].device_id,
   4699                        m_info[i].device_id) == 0) {
   4700                 mic_array[actual_mic_count] = my_data->microphones[j];
   4701                 for (size_t ch = 0; ch < channels_for_active_mic; ch++) {
   4702                      mic_array[actual_mic_count].channel_mapping[ch] =
   4703                              m_info[i].channel_mapping[ch];
   4704                 }
   4705                 actual_mic_count++;
   4706                 break;
   4707             }
   4708         }
   4709     }
   4710 end:
   4711     *mic_count = actual_mic_count;
   4712     return 0;
   4713 }
   4714 
   4715 int platform_set_usb_service_interval(void *platform,
   4716                                       bool playback,
   4717                                       unsigned long service_interval,
   4718                                       bool *reconfig)
   4719 {
   4720 #if defined (USB_SERVICE_INTERVAL_ENABLED)
   4721     struct platform_data *_platform = (struct platform_data *)platform;
   4722     *reconfig = false;
   4723     if (!playback) {
   4724         ALOGE("%s not valid for capture", __func__);
   4725         return -1;
   4726     }
   4727     const char *ctl_name = "USB_AUDIO_RX service_interval";
   4728     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
   4729                                                   ctl_name);
   4730     if (!ctl) {
   4731         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
   4732         return -1;
   4733     }
   4734     if (mixer_ctl_get_value(ctl, 0) != (int)service_interval) {
   4735         mixer_ctl_set_value(ctl, 0, service_interval);
   4736         *reconfig = true;
   4737     }
   4738     return 0;
   4739 #else
   4740     *reconfig = false;
   4741     (void)platform;
   4742     (void)playback;
   4743     (void)service_interval;
   4744     return -1;
   4745 #endif
   4746 }
   4747 
   4748 int platform_get_usb_service_interval(void *platform,
   4749                                       bool playback,
   4750                                       unsigned long *service_interval)
   4751 {
   4752 #if defined (USB_SERVICE_INTERVAL_ENABLED)
   4753     struct platform_data *_platform = (struct platform_data *)platform;
   4754     if (!playback) {
   4755         ALOGE("%s not valid for capture", __func__);
   4756         return -1;
   4757     }
   4758     const char *ctl_name = "USB_AUDIO_RX service_interval";
   4759     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
   4760                                                   ctl_name);
   4761     if (!ctl) {
   4762         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
   4763         return -1;
   4764     }
   4765     *service_interval = mixer_ctl_get_value(ctl, 0);
   4766     return 0;
   4767 #else
   4768     (void)platform;
   4769     (void)playback;
   4770     (void)service_interval;
   4771     return -1;
   4772 #endif
   4773 }
   4774 
   4775 int platform_set_acdb_metainfo_key(void *platform __unused,
   4776                                    char *name __unused,
   4777                                    int key __unused)
   4778 {
   4779     return -ENOSYS;
   4780 }
   4781