Home | History | Annotate | Download | only in hal
      1 /*
      2  * Copyright (C) 2013 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 
     17 #define LOG_TAG "audio_hw_primary"
     18 /*#define LOG_NDEBUG 0*/
     19 #define LOG_NDDEBUG 0
     20 
     21 #include <errno.h>
     22 #include <pthread.h>
     23 #include <stdint.h>
     24 #include <sys/time.h>
     25 #include <stdlib.h>
     26 #include <dlfcn.h>
     27 #include <math.h>
     28 
     29 #include <cutils/log.h>
     30 #include <cutils/str_parms.h>
     31 #include <cutils/properties.h>
     32 #include <cutils/list.h>
     33 
     34 #include "audio_hw.h"
     35 
     36 #define LIB_ACDB_LOADER "/system/lib/libacdbloader.so"
     37 #define LIB_CSD_CLIENT "/system/lib/libcsd-client.so"
     38 #define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
     39 #define MIXER_CARD 0
     40 
     41 #define STRING_TO_ENUM(string) { #string, string }
     42 
     43 /* Flags used to initialize acdb_settings variable that goes to ACDB library */
     44 #define DMIC_FLAG       0x00000002
     45 #define TTY_MODE_OFF    0x00000010
     46 #define TTY_MODE_FULL   0x00000020
     47 #define TTY_MODE_VCO    0x00000040
     48 #define TTY_MODE_HCO    0x00000080
     49 #define TTY_MODE_CLEAR  0xFFFFFF0F
     50 
     51 struct string_to_enum {
     52     const char *name;
     53     uint32_t value;
     54 };
     55 
     56 static const struct string_to_enum out_channels_name_to_enum_table[] = {
     57     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
     58     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
     59     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
     60 };
     61 
     62 static const char * const use_case_table[AUDIO_USECASE_MAX] = {
     63     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
     64     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
     65     [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
     66     [USECASE_AUDIO_RECORD] = "audio-record",
     67     [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
     68     [USECASE_VOICE_CALL] = "voice-call",
     69 };
     70 
     71 static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
     72     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
     73 #ifdef MSM8974
     74     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
     75 #else
     76     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
     77 #endif
     78     [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
     79     [USECASE_AUDIO_RECORD] = {0, 0},
     80     [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
     81     [USECASE_VOICE_CALL] = {12, 12},
     82 };
     83 
     84 /* Array to store sound devices */
     85 static const char * const device_table[SND_DEVICE_MAX] = {
     86     [SND_DEVICE_NONE] = "none",
     87     /* Playback sound devices */
     88     [SND_DEVICE_OUT_HANDSET] = "handset",
     89     [SND_DEVICE_OUT_SPEAKER] = "speaker",
     90     [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
     91     [SND_DEVICE_OUT_HEADPHONES] = "headphones",
     92     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
     93     [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
     94     [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
     95     [SND_DEVICE_OUT_HDMI] = "hdmi",
     96     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
     97     [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
     98     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
     99     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
    100     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
    101     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
    102 
    103     /* Capture sound devices */
    104     [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
    105     [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
    106     [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
    107     [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
    108     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
    109     [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
    110     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
    111     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
    112     [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
    113     [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
    114     [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
    115     [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
    116     [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
    117     [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
    118     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
    119     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
    120     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
    121     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
    122     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
    123     [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
    124     [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
    125     [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
    126     [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
    127     [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
    128 };
    129 
    130 /* ACDB IDs (audio DSP path configuration IDs) for each sound device */
    131 static const int acdb_device_table[SND_DEVICE_MAX] = {
    132     [SND_DEVICE_NONE] = -1,
    133     [SND_DEVICE_OUT_HANDSET] = 7,
    134     [SND_DEVICE_OUT_SPEAKER] = 14,
    135     [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
    136     [SND_DEVICE_OUT_HEADPHONES] = 10,
    137     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
    138     [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
    139     [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
    140     [SND_DEVICE_OUT_HDMI] = 18,
    141     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
    142     [SND_DEVICE_OUT_BT_SCO] = 22,
    143     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
    144     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
    145     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
    146     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
    147 
    148     [SND_DEVICE_IN_HANDSET_MIC] = 4,
    149     [SND_DEVICE_IN_SPEAKER_MIC] = 4,
    150     [SND_DEVICE_IN_HEADSET_MIC] = 8,
    151     [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
    152     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
    153     [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
    154     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
    155     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
    156     [SND_DEVICE_IN_HDMI_MIC] = 4,
    157     [SND_DEVICE_IN_BT_SCO_MIC] = 21,
    158     [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
    159     [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
    160     [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
    161     [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
    162     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
    163     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
    164     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
    165     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
    166     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
    167     [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
    168     /* TODO: Update with proper acdb ids */
    169     [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
    170     [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
    171     [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
    172     [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
    173 };
    174 
    175 int edid_get_max_channels(void);
    176 
    177 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
    178 static bool is_tmus = false;
    179 
    180 static void check_operator()
    181 {
    182     char value[PROPERTY_VALUE_MAX];
    183     int mccmnc;
    184     property_get("gsm.sim.operator.numeric",value,"0");
    185     mccmnc = atoi(value);
    186     ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
    187     switch(mccmnc) {
    188     /* TMUS MCC(310), MNC(490, 260, 026) */
    189     case 310490:
    190     case 310260:
    191     case 310026:
    192         is_tmus = true;
    193         break;
    194     }
    195 }
    196 
    197 static bool is_operator_tmus()
    198 {
    199     pthread_once(&check_op_once_ctl, check_operator);
    200     return is_tmus;
    201 }
    202 
    203 static int get_pcm_device_id(struct audio_route *ar,
    204                              audio_usecase_t usecase,
    205                              int device_type)
    206 {
    207     int device_id;
    208     if (device_type == PCM_PLAYBACK)
    209         device_id = pcm_device_table[usecase][0];
    210     else
    211         device_id = pcm_device_table[usecase][1];
    212     return device_id;
    213 }
    214 
    215 static int get_acdb_device_id(snd_device_t snd_device)
    216 {
    217     return acdb_device_table[snd_device];
    218 }
    219 
    220 static void add_backend_name(char *mixer_path,
    221                              snd_device_t snd_device)
    222 {
    223     if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
    224         strcat(mixer_path, " bt-sco");
    225     else if(snd_device == SND_DEVICE_OUT_BT_SCO)
    226         strcat(mixer_path, " bt-sco");
    227     else if (snd_device == SND_DEVICE_OUT_HDMI)
    228         strcat(mixer_path, " hdmi");
    229     else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
    230         strcat(mixer_path, " speaker-and-hdmi");
    231 }
    232 
    233 static int enable_audio_route(struct audio_device *adev,
    234                               struct audio_usecase *usecase,
    235                               bool update_mixer)
    236 {
    237     snd_device_t snd_device;
    238     char mixer_path[50];
    239 
    240     if (usecase == NULL)
    241         return -EINVAL;
    242 
    243     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
    244 
    245     if (usecase->type == PCM_CAPTURE)
    246         snd_device = usecase->in_snd_device;
    247     else
    248         snd_device = usecase->out_snd_device;
    249 
    250     strcpy(mixer_path, use_case_table[usecase->id]);
    251     add_backend_name(mixer_path, snd_device);
    252     ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
    253     audio_route_apply_path(adev->audio_route, mixer_path);
    254     if (update_mixer)
    255         audio_route_update_mixer(adev->audio_route);
    256 
    257     ALOGV("%s: exit", __func__);
    258     return 0;
    259 }
    260 
    261 static int disable_audio_route(struct audio_device *adev,
    262                                struct audio_usecase *usecase,
    263                                bool update_mixer)
    264 {
    265     snd_device_t snd_device;
    266     char mixer_path[50];
    267 
    268     if (usecase == NULL)
    269         return -EINVAL;
    270 
    271     ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
    272     if (usecase->type == PCM_CAPTURE)
    273         snd_device = usecase->in_snd_device;
    274     else
    275         snd_device = usecase->out_snd_device;
    276     strcpy(mixer_path, use_case_table[usecase->id]);
    277     add_backend_name(mixer_path, snd_device);
    278     ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
    279     audio_route_reset_path(adev->audio_route, mixer_path);
    280     if (update_mixer)
    281         audio_route_update_mixer(adev->audio_route);
    282 
    283     ALOGV("%s: exit", __func__);
    284     return 0;
    285 }
    286 
    287 static int enable_snd_device(struct audio_device *adev,
    288                              snd_device_t snd_device,
    289                              bool update_mixer)
    290 {
    291     int acdb_dev_id, acdb_dev_type;
    292 
    293     if (snd_device < SND_DEVICE_MIN ||
    294         snd_device >= SND_DEVICE_MAX) {
    295         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
    296         return -EINVAL;
    297     }
    298 
    299     adev->snd_dev_ref_cnt[snd_device]++;
    300     if (adev->snd_dev_ref_cnt[snd_device] > 1) {
    301         ALOGD("%s: snd_device(%d: %s) is already active",
    302               __func__, snd_device, device_table[snd_device]);
    303         return 0;
    304     }
    305 
    306     acdb_dev_id = get_acdb_device_id(snd_device);
    307     if (acdb_dev_id < 0) {
    308         ALOGE("%s: Could not find acdb id for device(%d)",
    309               __func__, snd_device);
    310         adev->snd_dev_ref_cnt[snd_device]--;
    311         return -EINVAL;
    312     }
    313     if (adev->acdb_send_audio_cal) {
    314         ALOGD("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
    315               __func__, snd_device, acdb_dev_id);
    316         if (snd_device >= SND_DEVICE_OUT_BEGIN &&
    317                 snd_device < SND_DEVICE_OUT_END)
    318             acdb_dev_type = ACDB_DEV_TYPE_OUT;
    319         else
    320             acdb_dev_type = ACDB_DEV_TYPE_IN;
    321         adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
    322     } else {
    323         ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
    324               __func__, LIB_ACDB_LOADER);
    325     }
    326 
    327     ALOGD("%s: snd_device(%d: %s)", __func__,
    328           snd_device, device_table[snd_device]);
    329     audio_route_apply_path(adev->audio_route, device_table[snd_device]);
    330     if (update_mixer)
    331         audio_route_update_mixer(adev->audio_route);
    332 
    333     return 0;
    334 }
    335 
    336 static int disable_snd_device(struct audio_device *adev,
    337                               snd_device_t snd_device,
    338                               bool update_mixer)
    339 {
    340     if (snd_device < SND_DEVICE_MIN ||
    341         snd_device >= SND_DEVICE_MAX) {
    342         ALOGE("%s: Invalid sound device %d", __func__, snd_device);
    343         return -EINVAL;
    344     }
    345     if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
    346         ALOGE("%s: device ref cnt is already 0", __func__);
    347         return -EINVAL;
    348     }
    349     adev->snd_dev_ref_cnt[snd_device]--;
    350     if (adev->snd_dev_ref_cnt[snd_device] == 0) {
    351         ALOGD("%s: snd_device(%d: %s)", __func__,
    352               snd_device, device_table[snd_device]);
    353         audio_route_reset_path(adev->audio_route, device_table[snd_device]);
    354         if (update_mixer)
    355             audio_route_update_mixer(adev->audio_route);
    356     }
    357     return 0;
    358 }
    359 
    360 static void check_usecases_codec_backend(struct audio_device *adev,
    361                                           struct audio_usecase *uc_info,
    362                                           snd_device_t snd_device)
    363 {
    364     struct listnode *node;
    365     struct audio_usecase *usecase;
    366     bool switch_device[AUDIO_USECASE_MAX];
    367     int i, num_uc_to_switch = 0;
    368 
    369     /*
    370      * This function is to make sure that all the usecases that are active on
    371      * the hardware codec backend are always routed to any one device that is
    372      * handled by the hardware codec.
    373      * For example, if low-latency and deep-buffer usecases are currently active
    374      * on speaker and out_set_parameters(headset) is received on low-latency
    375      * output, then we have to make sure deep-buffer is also switched to headset,
    376      * because of the limitation that both the devices cannot be enabled
    377      * at the same time as they share the same backend.
    378      */
    379     /* Disable all the usecases on the shared backend other than the
    380        specified usecase */
    381     for (i = 0; i < AUDIO_USECASE_MAX; i++)
    382         switch_device[i] = false;
    383 
    384     list_for_each(node, &adev->usecase_list) {
    385         usecase = node_to_item(node, struct audio_usecase, list);
    386         if (usecase->type != PCM_CAPTURE &&
    387                 usecase != uc_info &&
    388                 usecase->out_snd_device != snd_device &&
    389                 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
    390             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
    391                   __func__, use_case_table[usecase->id],
    392                   device_table[usecase->out_snd_device]);
    393             disable_audio_route(adev, usecase, false);
    394             switch_device[usecase->id] = true;
    395             num_uc_to_switch++;
    396         }
    397     }
    398 
    399     if (num_uc_to_switch) {
    400         /* Make sure all the streams are de-routed before disabling the device */
    401         audio_route_update_mixer(adev->audio_route);
    402 
    403         list_for_each(node, &adev->usecase_list) {
    404             usecase = node_to_item(node, struct audio_usecase, list);
    405             if (switch_device[usecase->id]) {
    406                 disable_snd_device(adev, usecase->out_snd_device, false);
    407                 enable_snd_device(adev, snd_device, false);
    408             }
    409         }
    410 
    411         /* Make sure new snd device is enabled before re-routing the streams */
    412         audio_route_update_mixer(adev->audio_route);
    413 
    414         /* Re-route all the usecases on the shared backend other than the
    415            specified usecase to new snd devices */
    416         list_for_each(node, &adev->usecase_list) {
    417             usecase = node_to_item(node, struct audio_usecase, list);
    418             /* Update the out_snd_device only before enabling the audio route */
    419             if (switch_device[usecase->id] ) {
    420                 usecase->out_snd_device = snd_device;
    421                 enable_audio_route(adev, usecase, false);
    422             }
    423         }
    424 
    425         audio_route_update_mixer(adev->audio_route);
    426     }
    427 }
    428 
    429 static void check_and_route_capture_usecases(struct audio_device *adev,
    430                                              struct audio_usecase *uc_info,
    431                                              snd_device_t snd_device)
    432 {
    433     struct listnode *node;
    434     struct audio_usecase *usecase;
    435     bool switch_device[AUDIO_USECASE_MAX];
    436     int i, num_uc_to_switch = 0;
    437 
    438     /*
    439      * This function is to make sure that all the active capture usecases
    440      * are always routed to the same input sound device.
    441      * For example, if audio-record and voice-call usecases are currently
    442      * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
    443      * is received for voice call then we have to make sure that audio-record
    444      * usecase is also switched to earpiece i.e. voice-dmic-ef,
    445      * because of the limitation that two devices cannot be enabled
    446      * at the same time if they share the same backend.
    447      */
    448     for (i = 0; i < AUDIO_USECASE_MAX; i++)
    449         switch_device[i] = false;
    450 
    451     list_for_each(node, &adev->usecase_list) {
    452         usecase = node_to_item(node, struct audio_usecase, list);
    453         if (usecase->type != PCM_PLAYBACK &&
    454                 usecase != uc_info &&
    455                 usecase->in_snd_device != snd_device) {
    456             ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
    457                   __func__, use_case_table[usecase->id],
    458                   device_table[usecase->in_snd_device]);
    459             disable_audio_route(adev, usecase, false);
    460             switch_device[usecase->id] = true;
    461             num_uc_to_switch++;
    462         }
    463     }
    464 
    465     if (num_uc_to_switch) {
    466         /* Make sure all the streams are de-routed before disabling the device */
    467         audio_route_update_mixer(adev->audio_route);
    468 
    469         list_for_each(node, &adev->usecase_list) {
    470             usecase = node_to_item(node, struct audio_usecase, list);
    471             if (switch_device[usecase->id]) {
    472                 disable_snd_device(adev, usecase->in_snd_device, false);
    473                 enable_snd_device(adev, snd_device, false);
    474             }
    475         }
    476 
    477         /* Make sure new snd device is enabled before re-routing the streams */
    478         audio_route_update_mixer(adev->audio_route);
    479 
    480         /* Re-route all the usecases on the shared backend other than the
    481            specified usecase to new snd devices */
    482         list_for_each(node, &adev->usecase_list) {
    483             usecase = node_to_item(node, struct audio_usecase, list);
    484             /* Update the in_snd_device only before enabling the audio route */
    485             if (switch_device[usecase->id] ) {
    486                 usecase->in_snd_device = snd_device;
    487                 enable_audio_route(adev, usecase, false);
    488             }
    489         }
    490 
    491         audio_route_update_mixer(adev->audio_route);
    492     }
    493 }
    494 
    495 static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
    496 {
    497     struct mixer_ctl *ctl;
    498     const char *mixer_ctl_name = "EC_REF_RX";
    499 
    500     ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
    501     if (!ctl) {
    502         ALOGE("%s: Could not get ctl for mixer cmd - %s",
    503               __func__, mixer_ctl_name);
    504         return -EINVAL;
    505     }
    506     ALOGV("Setting EC Reference: %s", ec_ref);
    507     mixer_ctl_set_enum_by_string(ctl, ec_ref);
    508     return 0;
    509 }
    510 
    511 static int set_hdmi_channels(struct mixer *mixer,
    512                              int channel_count)
    513 {
    514     struct mixer_ctl *ctl;
    515     const char *channel_cnt_str = NULL;
    516     const char *mixer_ctl_name = "HDMI_RX Channels";
    517     switch (channel_count) {
    518     case 8:
    519         channel_cnt_str = "Eight"; break;
    520     case 7:
    521         channel_cnt_str = "Seven"; break;
    522     case 6:
    523         channel_cnt_str = "Six"; break;
    524     case 5:
    525         channel_cnt_str = "Five"; break;
    526     case 4:
    527         channel_cnt_str = "Four"; break;
    528     case 3:
    529         channel_cnt_str = "Three"; break;
    530     default:
    531         channel_cnt_str = "Two"; break;
    532     }
    533     ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
    534     if (!ctl) {
    535         ALOGE("%s: Could not get ctl for mixer cmd - %s",
    536               __func__, mixer_ctl_name);
    537         return -EINVAL;
    538     }
    539     ALOGV("HDMI channel count: %s", channel_cnt_str);
    540     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
    541     return 0;
    542 }
    543 
    544 /* must be called with hw device mutex locked */
    545 static int read_hdmi_channel_masks(struct stream_out *out)
    546 {
    547     int ret = 0;
    548     int channels = edid_get_max_channels();
    549 
    550     switch (channels) {
    551         /*
    552          * Do not handle stereo output in Multi-channel cases
    553          * Stereo case is handled in normal playback path
    554          */
    555     case 6:
    556         ALOGV("%s: HDMI supports 5.1", __func__);
    557         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    558         break;
    559     case 8:
    560         ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
    561         out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    562         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
    563         break;
    564     default:
    565         ALOGE("HDMI does not support multi channel playback");
    566         ret = -ENOSYS;
    567         break;
    568     }
    569     return ret;
    570 }
    571 
    572 static snd_device_t get_output_snd_device(struct audio_device *adev,
    573                                           audio_devices_t devices)
    574 {
    575     audio_mode_t mode = adev->mode;
    576     snd_device_t snd_device = SND_DEVICE_NONE;
    577 
    578     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
    579     if (devices == AUDIO_DEVICE_NONE ||
    580         devices & AUDIO_DEVICE_BIT_IN) {
    581         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
    582         goto exit;
    583     }
    584 
    585     if (mode == AUDIO_MODE_IN_CALL) {
    586         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
    587             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
    588             if (adev->tty_mode == TTY_MODE_FULL)
    589                 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
    590             else if (adev->tty_mode == TTY_MODE_VCO)
    591                 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
    592             else if (adev->tty_mode == TTY_MODE_HCO)
    593                 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
    594             else
    595                 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
    596         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
    597             snd_device = SND_DEVICE_OUT_BT_SCO;
    598         } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
    599             snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
    600         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
    601             if (is_operator_tmus())
    602                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
    603             else
    604                 snd_device = SND_DEVICE_OUT_HANDSET;
    605         }
    606         if (snd_device != SND_DEVICE_NONE) {
    607             goto exit;
    608         }
    609     }
    610 
    611     if (popcount(devices) == 2) {
    612         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
    613                         AUDIO_DEVICE_OUT_SPEAKER)) {
    614             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
    615         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
    616                                AUDIO_DEVICE_OUT_SPEAKER)) {
    617             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
    618         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
    619                                AUDIO_DEVICE_OUT_SPEAKER)) {
    620             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
    621         } else {
    622             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
    623             goto exit;
    624         }
    625         if (snd_device != SND_DEVICE_NONE) {
    626             goto exit;
    627         }
    628     }
    629 
    630     if (popcount(devices) != 1) {
    631         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
    632         goto exit;
    633     }
    634 
    635     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
    636         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
    637         snd_device = SND_DEVICE_OUT_HEADPHONES;
    638     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
    639         if (adev->speaker_lr_swap) {
    640             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
    641         } else {
    642             snd_device = SND_DEVICE_OUT_SPEAKER;
    643         }
    644     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
    645         snd_device = SND_DEVICE_OUT_BT_SCO;
    646     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
    647         snd_device = SND_DEVICE_OUT_HDMI ;
    648     } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
    649         snd_device = SND_DEVICE_OUT_HANDSET;
    650     } else {
    651         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
    652     }
    653 exit:
    654     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
    655     return snd_device;
    656 }
    657 
    658 static snd_device_t get_input_snd_device(struct audio_device *adev,
    659                                          audio_devices_t out_device)
    660 {
    661     audio_source_t  source = (adev->active_input == NULL) ?
    662                                 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
    663 
    664     audio_mode_t    mode   = adev->mode;
    665     audio_devices_t in_device = ((adev->active_input == NULL) ?
    666                                     AUDIO_DEVICE_NONE : adev->active_input->device)
    667                                 & ~AUDIO_DEVICE_BIT_IN;
    668     audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
    669                                 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
    670     snd_device_t snd_device = SND_DEVICE_NONE;
    671 
    672     ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
    673           __func__, out_device, in_device);
    674     if (mode == AUDIO_MODE_IN_CALL) {
    675         if (out_device == AUDIO_DEVICE_NONE) {
    676             ALOGE("%s: No output device set for voice call", __func__);
    677             goto exit;
    678         }
    679         if (adev->tty_mode != TTY_MODE_OFF) {
    680             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
    681                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
    682                 switch (adev->tty_mode) {
    683                 case TTY_MODE_FULL:
    684                     snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
    685                     break;
    686                 case TTY_MODE_VCO:
    687                     snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
    688                     break;
    689                 case TTY_MODE_HCO:
    690                     snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
    691                     break;
    692                 default:
    693                     ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
    694                 }
    695                 goto exit;
    696             }
    697         }
    698         if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
    699             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
    700             if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
    701                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
    702             } else {
    703                 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
    704                     if (is_operator_tmus())
    705                         snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
    706                     else
    707                         snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
    708                 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
    709                     snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
    710                 else
    711                     snd_device = SND_DEVICE_IN_HANDSET_MIC;
    712             }
    713         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
    714             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
    715         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
    716             snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
    717         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
    718             if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
    719                     adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
    720                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
    721             } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
    722                        adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
    723                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
    724             } else {
    725                 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
    726             }
    727         }
    728     } else if (source == AUDIO_SOURCE_CAMCORDER) {
    729         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
    730             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
    731             snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
    732         }
    733     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
    734         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
    735             if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
    736                 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
    737                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
    738                 else if (adev->fluence_in_voice_rec)
    739                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
    740             } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
    741                 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
    742                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
    743                 else if (adev->fluence_in_voice_rec)
    744                     snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
    745             }
    746 
    747             if (snd_device == SND_DEVICE_NONE) {
    748                 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
    749             }
    750         }
    751     } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
    752         if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
    753             in_device = AUDIO_DEVICE_IN_BACK_MIC;
    754         if (adev->active_input) {
    755             if (adev->active_input->enable_aec) {
    756                 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
    757                     if (adev->mic_type_analog)
    758                         snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
    759                     else
    760                         snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
    761                 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
    762                     snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
    763                 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
    764                     snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
    765                 }
    766                 set_echo_reference(adev->mixer, "SLIM_RX");
    767             } else
    768                 set_echo_reference(adev->mixer, "NONE");
    769         }
    770     } else if (source == AUDIO_SOURCE_DEFAULT) {
    771         goto exit;
    772     }
    773 
    774 
    775     if (snd_device != SND_DEVICE_NONE) {
    776         goto exit;
    777     }
    778 
    779     if (in_device != AUDIO_DEVICE_NONE &&
    780             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
    781             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
    782         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
    783             snd_device = SND_DEVICE_IN_HANDSET_MIC;
    784         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
    785             if (adev->mic_type_analog)
    786                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
    787             else
    788                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
    789         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
    790             snd_device = SND_DEVICE_IN_HEADSET_MIC;
    791         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
    792             snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
    793         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
    794             snd_device = SND_DEVICE_IN_HDMI_MIC;
    795         } else {
    796             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
    797             ALOGW("%s: Using default handset-mic", __func__);
    798             snd_device = SND_DEVICE_IN_HANDSET_MIC;
    799         }
    800     } else {
    801         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
    802             snd_device = SND_DEVICE_IN_HANDSET_MIC;
    803         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
    804             snd_device = SND_DEVICE_IN_HEADSET_MIC;
    805         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
    806             snd_device = SND_DEVICE_IN_SPEAKER_MIC;
    807         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
    808             snd_device = SND_DEVICE_IN_HANDSET_MIC;
    809         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
    810             snd_device = SND_DEVICE_IN_BT_SCO_MIC;
    811         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
    812             snd_device = SND_DEVICE_IN_HDMI_MIC;
    813         } else {
    814             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
    815             ALOGW("%s: Using default handset-mic", __func__);
    816             snd_device = SND_DEVICE_IN_HANDSET_MIC;
    817         }
    818     }
    819 exit:
    820     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
    821     return snd_device;
    822 }
    823 
    824 static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
    825                                                    audio_usecase_t uc_id)
    826 {
    827     struct audio_usecase *usecase;
    828     struct listnode *node;
    829 
    830     list_for_each(node, &adev->usecase_list) {
    831         usecase = node_to_item(node, struct audio_usecase, list);
    832         if (usecase->id == uc_id)
    833             return usecase;
    834     }
    835     return NULL;
    836 }
    837 
    838 static int select_devices(struct audio_device *adev,
    839                           audio_usecase_t uc_id)
    840 {
    841     snd_device_t out_snd_device = SND_DEVICE_NONE;
    842     snd_device_t in_snd_device = SND_DEVICE_NONE;
    843     struct audio_usecase *usecase = NULL;
    844     struct audio_usecase *vc_usecase = NULL;
    845     struct listnode *node;
    846     int acdb_rx_id, acdb_tx_id;
    847     int status = 0;
    848 
    849     usecase = get_usecase_from_list(adev, uc_id);
    850     if (usecase == NULL) {
    851         ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
    852         return -EINVAL;
    853     }
    854 
    855     if (usecase->type == VOICE_CALL) {
    856         out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
    857         in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
    858         usecase->devices = usecase->stream.out->devices;
    859     } else {
    860         /*
    861          * If the voice call is active, use the sound devices of voice call usecase
    862          * so that it would not result any device switch. All the usecases will
    863          * be switched to new device when select_devices() is called for voice call
    864          * usecase. This is to avoid switching devices for voice call when
    865          * check_usecases_codec_backend() is called below.
    866          */
    867         if (adev->in_call) {
    868             vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
    869             if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
    870                 in_snd_device = vc_usecase->in_snd_device;
    871                 out_snd_device = vc_usecase->out_snd_device;
    872             }
    873         }
    874         if (usecase->type == PCM_PLAYBACK) {
    875             usecase->devices = usecase->stream.out->devices;
    876             in_snd_device = SND_DEVICE_NONE;
    877             if (out_snd_device == SND_DEVICE_NONE) {
    878                 out_snd_device = get_output_snd_device(adev,
    879                                             usecase->stream.out->devices);
    880                 if (usecase->stream.out == adev->primary_output &&
    881                         adev->active_input &&
    882                         adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
    883                     select_devices(adev, adev->active_input->usecase);
    884                 }
    885             }
    886         } else if (usecase->type == PCM_CAPTURE) {
    887             usecase->devices = usecase->stream.in->device;
    888             out_snd_device = SND_DEVICE_NONE;
    889             if (in_snd_device == SND_DEVICE_NONE) {
    890                 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
    891                         adev->primary_output && !adev->primary_output->standby) {
    892                     in_snd_device = get_input_snd_device(adev,
    893                                         adev->primary_output->devices);
    894                 } else {
    895                     in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
    896                 }
    897             }
    898         }
    899     }
    900 
    901     if (out_snd_device == usecase->out_snd_device &&
    902         in_snd_device == usecase->in_snd_device) {
    903         return 0;
    904     }
    905 
    906     ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
    907           out_snd_device, device_table[out_snd_device],
    908           in_snd_device,  device_table[in_snd_device]);
    909 
    910     /*
    911      * Limitation: While in call, to do a device switch we need to disable
    912      * and enable both RX and TX devices though one of them is same as current
    913      * device.
    914      */
    915     if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
    916             usecase->in_snd_device != SND_DEVICE_NONE &&
    917             usecase->out_snd_device != SND_DEVICE_NONE) {
    918         /* This must be called before disabling the mixer controls on APQ side */
    919         if (adev->csd_disable_device == NULL) {
    920             ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
    921         } else {
    922             status = adev->csd_disable_device();
    923             if (status < 0) {
    924                 ALOGE("%s: csd_client_disable_device, failed, error %d",
    925                       __func__, status);
    926             }
    927         }
    928     }
    929 
    930     /* Disable current sound devices */
    931     if (usecase->out_snd_device != SND_DEVICE_NONE) {
    932         disable_audio_route(adev, usecase, true);
    933         disable_snd_device(adev, usecase->out_snd_device, false);
    934     }
    935 
    936     if (usecase->in_snd_device != SND_DEVICE_NONE) {
    937         disable_audio_route(adev, usecase, true);
    938         disable_snd_device(adev, usecase->in_snd_device, false);
    939     }
    940 
    941     /* Enable new sound devices */
    942     if (out_snd_device != SND_DEVICE_NONE) {
    943         if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
    944             check_usecases_codec_backend(adev, usecase, out_snd_device);
    945         enable_snd_device(adev, out_snd_device, false);
    946     }
    947 
    948     if (in_snd_device != SND_DEVICE_NONE) {
    949         check_and_route_capture_usecases(adev, usecase, in_snd_device);
    950         enable_snd_device(adev, in_snd_device, false);
    951     }
    952 
    953     audio_route_update_mixer(adev->audio_route);
    954 
    955     usecase->in_snd_device = in_snd_device;
    956     usecase->out_snd_device = out_snd_device;
    957 
    958     enable_audio_route(adev, usecase, true);
    959 
    960     if (usecase->type == VOICE_CALL && adev->csd_client) {
    961         if (adev->csd_enable_device == NULL) {
    962             ALOGE("%s: dlsym error for csd_client_enable_device",
    963                   __func__);
    964         } else {
    965             acdb_rx_id = get_acdb_device_id(out_snd_device);
    966             acdb_tx_id = get_acdb_device_id(in_snd_device);
    967 
    968             if (acdb_rx_id > 0 || acdb_tx_id > 0) {
    969                 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
    970                                                  adev->acdb_settings);
    971                 if (status < 0) {
    972                     ALOGE("%s: csd_client_enable_device, failed, error %d",
    973                           __func__, status);
    974                 }
    975             } else {
    976                 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
    977                       acdb_rx_id, acdb_tx_id);
    978             }
    979         }
    980     }
    981 
    982     return status;
    983 }
    984 
    985 static int stop_input_stream(struct stream_in *in)
    986 {
    987     int i, ret = 0;
    988     struct audio_usecase *uc_info;
    989     struct audio_device *adev = in->dev;
    990 
    991     adev->active_input = NULL;
    992 
    993     ALOGD("%s: enter: usecase(%d: %s)", __func__,
    994           in->usecase, use_case_table[in->usecase]);
    995     uc_info = get_usecase_from_list(adev, in->usecase);
    996     if (uc_info == NULL) {
    997         ALOGE("%s: Could not find the usecase (%d) in the list",
    998               __func__, in->usecase);
    999         return -EINVAL;
   1000     }
   1001 
   1002     /* 1. Disable stream specific mixer controls */
   1003     disable_audio_route(adev, uc_info, true);
   1004 
   1005     /* 2. Disable the tx device */
   1006     disable_snd_device(adev, uc_info->in_snd_device, true);
   1007 
   1008     list_remove(&uc_info->list);
   1009     free(uc_info);
   1010 
   1011     ALOGD("%s: exit: status(%d)", __func__, ret);
   1012     return ret;
   1013 }
   1014 
   1015 int start_input_stream(struct stream_in *in)
   1016 {
   1017     /* 1. Enable output device and stream routing controls */
   1018     int ret = 0;
   1019     struct audio_usecase *uc_info;
   1020     struct audio_device *adev = in->dev;
   1021 
   1022     ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
   1023     in->pcm_device_id = get_pcm_device_id(adev->audio_route,
   1024                                           in->usecase,
   1025                                           PCM_CAPTURE);
   1026     if (in->pcm_device_id < 0) {
   1027         ALOGE("%s: Could not find PCM device id for the usecase(%d)",
   1028               __func__, in->usecase);
   1029         ret = -EINVAL;
   1030         goto error_config;
   1031     }
   1032 
   1033     adev->active_input = in;
   1034     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
   1035     uc_info->id = in->usecase;
   1036     uc_info->type = PCM_CAPTURE;
   1037     uc_info->stream.in = in;
   1038     uc_info->devices = in->device;
   1039     uc_info->in_snd_device = SND_DEVICE_NONE;
   1040     uc_info->out_snd_device = SND_DEVICE_NONE;
   1041 
   1042     list_add_tail(&adev->usecase_list, &uc_info->list);
   1043     select_devices(adev, in->usecase);
   1044 
   1045     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
   1046           __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
   1047     in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
   1048                            PCM_IN, &in->config);
   1049     if (in->pcm && !pcm_is_ready(in->pcm)) {
   1050         ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
   1051         pcm_close(in->pcm);
   1052         in->pcm = NULL;
   1053         ret = -EIO;
   1054         goto error_open;
   1055     }
   1056     ALOGD("%s: exit", __func__);
   1057     return ret;
   1058 
   1059 error_open:
   1060     stop_input_stream(in);
   1061 
   1062 error_config:
   1063     adev->active_input = NULL;
   1064     ALOGD("%s: exit: status(%d)", __func__, ret);
   1065 
   1066     return ret;
   1067 }
   1068 
   1069 static int stop_output_stream(struct stream_out *out)
   1070 {
   1071     int i, ret = 0;
   1072     struct audio_usecase *uc_info;
   1073     struct audio_device *adev = out->dev;
   1074 
   1075     ALOGD("%s: enter: usecase(%d: %s)", __func__,
   1076           out->usecase, use_case_table[out->usecase]);
   1077     uc_info = get_usecase_from_list(adev, out->usecase);
   1078     if (uc_info == NULL) {
   1079         ALOGE("%s: Could not find the usecase (%d) in the list",
   1080               __func__, out->usecase);
   1081         return -EINVAL;
   1082     }
   1083 
   1084     /* 1. Get and set stream specific mixer controls */
   1085     disable_audio_route(adev, uc_info, true);
   1086 
   1087     /* 2. Disable the rx device */
   1088     disable_snd_device(adev, uc_info->out_snd_device, true);
   1089 
   1090     list_remove(&uc_info->list);
   1091     free(uc_info);
   1092 
   1093     ALOGD("%s: exit: status(%d)", __func__, ret);
   1094     return ret;
   1095 }
   1096 
   1097 int start_output_stream(struct stream_out *out)
   1098 {
   1099     int ret = 0;
   1100     struct audio_usecase *uc_info;
   1101     struct audio_device *adev = out->dev;
   1102 
   1103     ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
   1104           __func__, out->usecase, use_case_table[out->usecase], out->devices);
   1105     out->pcm_device_id = get_pcm_device_id(adev->audio_route,
   1106                                            out->usecase,
   1107                                            PCM_PLAYBACK);
   1108     if (out->pcm_device_id < 0) {
   1109         ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
   1110               __func__, out->pcm_device_id, out->usecase);
   1111         ret = -EINVAL;
   1112         goto error_config;
   1113     }
   1114 
   1115     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
   1116     uc_info->id = out->usecase;
   1117     uc_info->type = PCM_PLAYBACK;
   1118     uc_info->stream.out = out;
   1119     uc_info->devices = out->devices;
   1120     uc_info->in_snd_device = SND_DEVICE_NONE;
   1121     uc_info->out_snd_device = SND_DEVICE_NONE;
   1122 
   1123     list_add_tail(&adev->usecase_list, &uc_info->list);
   1124 
   1125     select_devices(adev, out->usecase);
   1126 
   1127     ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
   1128           __func__, 0, out->pcm_device_id);
   1129     out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
   1130                            PCM_OUT, &out->config);
   1131     if (out->pcm && !pcm_is_ready(out->pcm)) {
   1132         ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
   1133         pcm_close(out->pcm);
   1134         out->pcm = NULL;
   1135         ret = -EIO;
   1136         goto error_pcm_open;
   1137     }
   1138     ALOGD("%s: exit", __func__);
   1139     return 0;
   1140 error_pcm_open:
   1141     stop_output_stream(out);
   1142 error_config:
   1143     return ret;
   1144 }
   1145 
   1146 static int stop_voice_call(struct audio_device *adev)
   1147 {
   1148     int i, ret = 0;
   1149     struct audio_usecase *uc_info;
   1150 
   1151     ALOGD("%s: enter", __func__);
   1152     adev->in_call = false;
   1153     if (adev->csd_client) {
   1154         if (adev->csd_stop_voice == NULL) {
   1155             ALOGE("dlsym error for csd_client_disable_device");
   1156         } else {
   1157             ret = adev->csd_stop_voice();
   1158             if (ret < 0) {
   1159                 ALOGE("%s: csd_client error %d\n", __func__, ret);
   1160             }
   1161         }
   1162     }
   1163 
   1164     /* 1. Close the PCM devices */
   1165     if (adev->voice_call_rx) {
   1166         pcm_close(adev->voice_call_rx);
   1167         adev->voice_call_rx = NULL;
   1168     }
   1169     if (adev->voice_call_tx) {
   1170         pcm_close(adev->voice_call_tx);
   1171         adev->voice_call_tx = NULL;
   1172     }
   1173 
   1174     uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
   1175     if (uc_info == NULL) {
   1176         ALOGE("%s: Could not find the usecase (%d) in the list",
   1177               __func__, USECASE_VOICE_CALL);
   1178         return -EINVAL;
   1179     }
   1180 
   1181     /* 2. Get and set stream specific mixer controls */
   1182     disable_audio_route(adev, uc_info, true);
   1183 
   1184     /* 3. Disable the rx and tx devices */
   1185     disable_snd_device(adev, uc_info->out_snd_device, false);
   1186     disable_snd_device(adev, uc_info->in_snd_device, true);
   1187 
   1188     list_remove(&uc_info->list);
   1189     free(uc_info);
   1190 
   1191     ALOGD("%s: exit: status(%d)", __func__, ret);
   1192     return ret;
   1193 }
   1194 
   1195 static int start_voice_call(struct audio_device *adev)
   1196 {
   1197     int i, ret = 0;
   1198     struct audio_usecase *uc_info;
   1199     int pcm_dev_rx_id, pcm_dev_tx_id;
   1200 
   1201     ALOGD("%s: enter", __func__);
   1202 
   1203     uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
   1204     uc_info->id = USECASE_VOICE_CALL;
   1205     uc_info->type = VOICE_CALL;
   1206     uc_info->stream.out = adev->primary_output;
   1207     uc_info->devices = adev->primary_output->devices;
   1208     uc_info->in_snd_device = SND_DEVICE_NONE;
   1209     uc_info->out_snd_device = SND_DEVICE_NONE;
   1210 
   1211     list_add_tail(&adev->usecase_list, &uc_info->list);
   1212 
   1213     select_devices(adev, USECASE_VOICE_CALL);
   1214 
   1215     pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
   1216                                       PCM_PLAYBACK);
   1217     pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
   1218                                       PCM_CAPTURE);
   1219 
   1220     if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
   1221         ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
   1222               __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
   1223         ret = -EIO;
   1224         goto error_start_voice;
   1225     }
   1226 
   1227     ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
   1228           __func__, SOUND_CARD, pcm_dev_rx_id);
   1229     adev->voice_call_rx = pcm_open(SOUND_CARD,
   1230                                   pcm_dev_rx_id,
   1231                                   PCM_OUT, &pcm_config_voice_call);
   1232     if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
   1233         ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
   1234         ret = -EIO;
   1235         goto error_start_voice;
   1236     }
   1237 
   1238     ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
   1239           __func__, SOUND_CARD, pcm_dev_tx_id);
   1240     adev->voice_call_tx = pcm_open(SOUND_CARD,
   1241                                    pcm_dev_tx_id,
   1242                                    PCM_IN, &pcm_config_voice_call);
   1243     if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
   1244         ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
   1245         ret = -EIO;
   1246         goto error_start_voice;
   1247     }
   1248     pcm_start(adev->voice_call_rx);
   1249     pcm_start(adev->voice_call_tx);
   1250 
   1251     if (adev->csd_client) {
   1252         if (adev->csd_start_voice == NULL) {
   1253             ALOGE("dlsym error for csd_client_start_voice");
   1254             goto error_start_voice;
   1255         } else {
   1256             ret = adev->csd_start_voice();
   1257             if (ret < 0) {
   1258                 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
   1259                 goto error_start_voice;
   1260             }
   1261         }
   1262     }
   1263 
   1264     adev->in_call = true;
   1265     return 0;
   1266 
   1267 error_start_voice:
   1268     stop_voice_call(adev);
   1269 
   1270     ALOGD("%s: exit: status(%d)", __func__, ret);
   1271     return ret;
   1272 }
   1273 
   1274 static int check_input_parameters(uint32_t sample_rate,
   1275                                   audio_format_t format,
   1276                                   int channel_count)
   1277 {
   1278     if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
   1279 
   1280     if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
   1281 
   1282     switch (sample_rate) {
   1283     case 8000:
   1284     case 11025:
   1285     case 12000:
   1286     case 16000:
   1287     case 22050:
   1288     case 24000:
   1289     case 32000:
   1290     case 44100:
   1291     case 48000:
   1292         break;
   1293     default:
   1294         return -EINVAL;
   1295     }
   1296 
   1297     return 0;
   1298 }
   1299 
   1300 static size_t get_input_buffer_size(uint32_t sample_rate,
   1301                                     audio_format_t format,
   1302                                     int channel_count)
   1303 {
   1304     size_t size = 0;
   1305 
   1306     if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
   1307 
   1308     if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
   1309         size = (sample_rate * 20) / 1000;
   1310     } else if (sample_rate == 11025 || sample_rate == 12000) {
   1311         size = 256;
   1312     } else if (sample_rate == 22050 || sample_rate == 24000) {
   1313         size = 512;
   1314     } else if (sample_rate == 44100 || sample_rate == 48000) {
   1315         size = 1024;
   1316     }
   1317 
   1318     return size * sizeof(short) * channel_count;
   1319 }
   1320 
   1321 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
   1322 {
   1323     struct stream_out *out = (struct stream_out *)stream;
   1324 
   1325     return out->config.rate;
   1326 }
   1327 
   1328 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
   1329 {
   1330     return -ENOSYS;
   1331 }
   1332 
   1333 static size_t out_get_buffer_size(const struct audio_stream *stream)
   1334 {
   1335     struct stream_out *out = (struct stream_out *)stream;
   1336 
   1337     return out->config.period_size * audio_stream_frame_size(stream);
   1338 }
   1339 
   1340 static uint32_t out_get_channels(const struct audio_stream *stream)
   1341 {
   1342     struct stream_out *out = (struct stream_out *)stream;
   1343 
   1344     return out->channel_mask;
   1345 }
   1346 
   1347 static audio_format_t out_get_format(const struct audio_stream *stream)
   1348 {
   1349     return AUDIO_FORMAT_PCM_16_BIT;
   1350 }
   1351 
   1352 static int out_set_format(struct audio_stream *stream, audio_format_t format)
   1353 {
   1354     return -ENOSYS;
   1355 }
   1356 
   1357 static int out_standby(struct audio_stream *stream)
   1358 {
   1359     struct stream_out *out = (struct stream_out *)stream;
   1360     struct audio_device *adev = out->dev;
   1361     ALOGD("%s: enter: usecase(%d: %s)", __func__,
   1362           out->usecase, use_case_table[out->usecase]);
   1363     pthread_mutex_lock(&out->lock);
   1364 
   1365     if (!out->standby) {
   1366         out->standby = true;
   1367         if (out->pcm) {
   1368             pcm_close(out->pcm);
   1369             out->pcm = NULL;
   1370         }
   1371         pthread_mutex_lock(&adev->lock);
   1372         stop_output_stream(out);
   1373         pthread_mutex_unlock(&adev->lock);
   1374     }
   1375     pthread_mutex_unlock(&out->lock);
   1376     ALOGD("%s: exit", __func__);
   1377     return 0;
   1378 }
   1379 
   1380 static int out_dump(const struct audio_stream *stream, int fd)
   1381 {
   1382     return 0;
   1383 }
   1384 
   1385 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1386 {
   1387     struct stream_out *out = (struct stream_out *)stream;
   1388     struct audio_device *adev = out->dev;
   1389     struct audio_usecase *usecase;
   1390     struct listnode *node;
   1391     struct str_parms *parms;
   1392     char value[32];
   1393     int ret, val = 0;
   1394     bool select_new_device = false;
   1395 
   1396     ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
   1397           __func__, out->usecase, use_case_table[out->usecase], kvpairs);
   1398     parms = str_parms_create_str(kvpairs);
   1399     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
   1400     if (ret >= 0) {
   1401         val = atoi(value);
   1402         pthread_mutex_lock(&out->lock);
   1403         pthread_mutex_lock(&adev->lock);
   1404 
   1405         /*
   1406          * When HDMI cable is unplugged the music playback is paused and
   1407          * the policy manager sends routing=0. But the audioflinger
   1408          * continues to write data until standby time (3sec).
   1409          * As the HDMI core is turned off, the write gets blocked.
   1410          * Avoid this by routing audio to speaker until standby.
   1411          */
   1412         if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
   1413                 val == AUDIO_DEVICE_NONE) {
   1414             val = AUDIO_DEVICE_OUT_SPEAKER;
   1415         }
   1416 
   1417         /*
   1418          * select_devices() call below switches all the usecases on the same
   1419          * backend to the new device. Refer to check_usecases_codec_backend() in
   1420          * the select_devices(). But how do we undo this?
   1421          *
   1422          * For example, music playback is active on headset (deep-buffer usecase)
   1423          * and if we go to ringtones and select a ringtone, low-latency usecase
   1424          * will be started on headset+speaker. As we can't enable headset+speaker
   1425          * and headset devices at the same time, select_devices() switches the music
   1426          * playback to headset+speaker while starting low-lateny usecase for ringtone.
   1427          * So when the ringtone playback is completed, how do we undo the same?
   1428          *
   1429          * We are relying on the out_set_parameters() call on deep-buffer output,
   1430          * once the ringtone playback is ended.
   1431          * NOTE: We should not check if the current devices are same as new devices.
   1432          *       Because select_devices() must be called to switch back the music
   1433          *       playback to headset.
   1434          */
   1435         if (val != 0) {
   1436             out->devices = val;
   1437 
   1438             if (!out->standby)
   1439                 select_devices(adev, out->usecase);
   1440 
   1441             if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
   1442                     (out == adev->primary_output)) {
   1443                 start_voice_call(adev);
   1444             } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
   1445                        (out == adev->primary_output)) {
   1446                 select_devices(adev, USECASE_VOICE_CALL);
   1447             }
   1448         }
   1449 
   1450         if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
   1451                 (out == adev->primary_output)) {
   1452             stop_voice_call(adev);
   1453         }
   1454 
   1455         pthread_mutex_unlock(&adev->lock);
   1456         pthread_mutex_unlock(&out->lock);
   1457     }
   1458     str_parms_destroy(parms);
   1459     ALOGD("%s: exit: code(%d)", __func__, ret);
   1460     return ret;
   1461 }
   1462 
   1463 static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
   1464 {
   1465     struct stream_out *out = (struct stream_out *)stream;
   1466     struct str_parms *query = str_parms_create_str(keys);
   1467     char *str;
   1468     char value[256];
   1469     struct str_parms *reply = str_parms_create();
   1470     size_t i, j;
   1471     int ret;
   1472     bool first = true;
   1473     ALOGD("%s: enter: keys - %s", __func__, keys);
   1474     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
   1475     if (ret >= 0) {
   1476         value[0] = '\0';
   1477         i = 0;
   1478         while (out->supported_channel_masks[i] != 0) {
   1479             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
   1480                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
   1481                     if (!first) {
   1482                         strcat(value, "|");
   1483                     }
   1484                     strcat(value, out_channels_name_to_enum_table[j].name);
   1485                     first = false;
   1486                     break;
   1487                 }
   1488             }
   1489             i++;
   1490         }
   1491         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
   1492         str = str_parms_to_str(reply);
   1493     } else {
   1494         str = strdup(keys);
   1495     }
   1496     str_parms_destroy(query);
   1497     str_parms_destroy(reply);
   1498     ALOGD("%s: exit: returns - %s", __func__, str);
   1499     return str;
   1500 }
   1501 
   1502 static uint32_t out_get_latency(const struct audio_stream_out *stream)
   1503 {
   1504     struct stream_out *out = (struct stream_out *)stream;
   1505 
   1506     return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
   1507 }
   1508 
   1509 static int out_set_volume(struct audio_stream_out *stream, float left,
   1510                           float right)
   1511 {
   1512     struct stream_out *out = (struct stream_out *)stream;
   1513     if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
   1514         /* only take left channel into account: the API is for stereo anyway */
   1515         out->muted = (left == 0.0f);
   1516         return 0;
   1517     }
   1518     return -ENOSYS;
   1519 }
   1520 
   1521 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
   1522                          size_t bytes)
   1523 {
   1524     struct stream_out *out = (struct stream_out *)stream;
   1525     struct audio_device *adev = out->dev;
   1526     int i, ret = -1;
   1527 
   1528     pthread_mutex_lock(&out->lock);
   1529     if (out->standby) {
   1530         out->standby = false;
   1531         pthread_mutex_lock(&adev->lock);
   1532         ret = start_output_stream(out);
   1533         pthread_mutex_unlock(&adev->lock);
   1534         if (ret != 0) {
   1535             out->standby = true;
   1536             goto exit;
   1537         }
   1538     }
   1539 
   1540     if (out->pcm) {
   1541         if (out->muted)
   1542             memset((void *)buffer, 0, bytes);
   1543         //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
   1544         ret = pcm_write(out->pcm, (void *)buffer, bytes);
   1545     }
   1546 
   1547 exit:
   1548     pthread_mutex_unlock(&out->lock);
   1549 
   1550     if (ret != 0) {
   1551         if (out->pcm)
   1552             ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
   1553         out_standby(&out->stream.common);
   1554         usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
   1555                out_get_sample_rate(&out->stream.common));
   1556     }
   1557     return bytes;
   1558 }
   1559 
   1560 static int out_get_render_position(const struct audio_stream_out *stream,
   1561                                    uint32_t *dsp_frames)
   1562 {
   1563     return -EINVAL;
   1564 }
   1565 
   1566 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1567 {
   1568     return 0;
   1569 }
   1570 
   1571 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1572 {
   1573     return 0;
   1574 }
   1575 
   1576 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
   1577                                         int64_t *timestamp)
   1578 {
   1579     return -EINVAL;
   1580 }
   1581 
   1582 /** audio_stream_in implementation **/
   1583 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
   1584 {
   1585     struct stream_in *in = (struct stream_in *)stream;
   1586 
   1587     return in->config.rate;
   1588 }
   1589 
   1590 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
   1591 {
   1592     return -ENOSYS;
   1593 }
   1594 
   1595 static size_t in_get_buffer_size(const struct audio_stream *stream)
   1596 {
   1597     struct stream_in *in = (struct stream_in *)stream;
   1598 
   1599     return in->config.period_size * audio_stream_frame_size(stream);
   1600 }
   1601 
   1602 static uint32_t in_get_channels(const struct audio_stream *stream)
   1603 {
   1604     struct stream_in *in = (struct stream_in *)stream;
   1605 
   1606     return in->channel_mask;
   1607 }
   1608 
   1609 static audio_format_t in_get_format(const struct audio_stream *stream)
   1610 {
   1611     return AUDIO_FORMAT_PCM_16_BIT;
   1612 }
   1613 
   1614 static int in_set_format(struct audio_stream *stream, audio_format_t format)
   1615 {
   1616     return -ENOSYS;
   1617 }
   1618 
   1619 static int in_standby(struct audio_stream *stream)
   1620 {
   1621     struct stream_in *in = (struct stream_in *)stream;
   1622     struct audio_device *adev = in->dev;
   1623     int status = 0;
   1624     ALOGD("%s: enter", __func__);
   1625     pthread_mutex_lock(&in->lock);
   1626     if (!in->standby) {
   1627         in->standby = true;
   1628         if (in->pcm) {
   1629             pcm_close(in->pcm);
   1630             in->pcm = NULL;
   1631         }
   1632         pthread_mutex_lock(&adev->lock);
   1633         status = stop_input_stream(in);
   1634         pthread_mutex_unlock(&adev->lock);
   1635     }
   1636     pthread_mutex_unlock(&in->lock);
   1637     ALOGD("%s: exit:  status(%d)", __func__, status);
   1638     return status;
   1639 }
   1640 
   1641 static int in_dump(const struct audio_stream *stream, int fd)
   1642 {
   1643     return 0;
   1644 }
   1645 
   1646 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1647 {
   1648     struct stream_in *in = (struct stream_in *)stream;
   1649     struct audio_device *adev = in->dev;
   1650     struct str_parms *parms;
   1651     char *str;
   1652     char value[32];
   1653     int ret, val = 0;
   1654 
   1655     ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
   1656     parms = str_parms_create_str(kvpairs);
   1657 
   1658     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
   1659 
   1660     pthread_mutex_lock(&in->lock);
   1661     pthread_mutex_lock(&adev->lock);
   1662     if (ret >= 0) {
   1663         val = atoi(value);
   1664         /* no audio source uses val == 0 */
   1665         if ((in->source != val) && (val != 0)) {
   1666             in->source = val;
   1667         }
   1668     }
   1669 
   1670     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
   1671     if (ret >= 0) {
   1672         val = atoi(value);
   1673         if ((in->device != val) && (val != 0)) {
   1674             in->device = val;
   1675             /* If recording is in progress, change the tx device to new device */
   1676             if (!in->standby)
   1677                 ret = select_devices(adev, in->usecase);
   1678         }
   1679     }
   1680 
   1681     pthread_mutex_unlock(&adev->lock);
   1682     pthread_mutex_unlock(&in->lock);
   1683 
   1684     str_parms_destroy(parms);
   1685     ALOGD("%s: exit: status(%d)", __func__, ret);
   1686     return ret;
   1687 }
   1688 
   1689 static char* in_get_parameters(const struct audio_stream *stream,
   1690                                const char *keys)
   1691 {
   1692     return strdup("");
   1693 }
   1694 
   1695 static int in_set_gain(struct audio_stream_in *stream, float gain)
   1696 {
   1697     return 0;
   1698 }
   1699 
   1700 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
   1701                        size_t bytes)
   1702 {
   1703     struct stream_in *in = (struct stream_in *)stream;
   1704     struct audio_device *adev = in->dev;
   1705     int i, ret = -1;
   1706 
   1707     pthread_mutex_lock(&in->lock);
   1708     if (in->standby) {
   1709         pthread_mutex_lock(&adev->lock);
   1710         ret = start_input_stream(in);
   1711         pthread_mutex_unlock(&adev->lock);
   1712         if (ret != 0) {
   1713             goto exit;
   1714         }
   1715         in->standby = 0;
   1716     }
   1717 
   1718     if (in->pcm) {
   1719         ret = pcm_read(in->pcm, buffer, bytes);
   1720     }
   1721 
   1722     /*
   1723      * Instead of writing zeroes here, we could trust the hardware
   1724      * to always provide zeroes when muted.
   1725      */
   1726     if (ret == 0 && adev->mic_mute)
   1727         memset(buffer, 0, bytes);
   1728 
   1729 exit:
   1730     pthread_mutex_unlock(&in->lock);
   1731 
   1732     if (ret != 0) {
   1733         in_standby(&in->stream.common);
   1734         ALOGV("%s: read failed - sleeping for buffer duration", __func__);
   1735         usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
   1736                in_get_sample_rate(&in->stream.common));
   1737     }
   1738     return bytes;
   1739 }
   1740 
   1741 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
   1742 {
   1743     return 0;
   1744 }
   1745 
   1746 static int add_remove_audio_effect(const struct audio_stream *stream,
   1747                                    effect_handle_t effect,
   1748                                    bool enable)
   1749 {
   1750     struct stream_in *in = (struct stream_in *)stream;
   1751     int status = 0;
   1752     effect_descriptor_t desc;
   1753 
   1754     status = (*effect)->get_descriptor(effect, &desc);
   1755     if (status != 0)
   1756         return status;
   1757 
   1758     pthread_mutex_lock(&in->lock);
   1759     pthread_mutex_lock(&in->dev->lock);
   1760     if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
   1761             in->enable_aec != enable &&
   1762             (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
   1763         in->enable_aec = enable;
   1764         if (!in->standby)
   1765             select_devices(in->dev, in->usecase);
   1766     }
   1767     pthread_mutex_unlock(&in->dev->lock);
   1768     pthread_mutex_unlock(&in->lock);
   1769 
   1770     return 0;
   1771 }
   1772 
   1773 static int in_add_audio_effect(const struct audio_stream *stream,
   1774                                effect_handle_t effect)
   1775 {
   1776     ALOGD("%s: effect %p", __func__, effect);
   1777     return add_remove_audio_effect(stream, effect, true);
   1778 }
   1779 
   1780 static int in_remove_audio_effect(const struct audio_stream *stream,
   1781                                   effect_handle_t effect)
   1782 {
   1783     ALOGD("%s: effect %p", __func__, effect);
   1784     return add_remove_audio_effect(stream, effect, false);
   1785 }
   1786 
   1787 static int adev_open_output_stream(struct audio_hw_device *dev,
   1788                                    audio_io_handle_t handle,
   1789                                    audio_devices_t devices,
   1790                                    audio_output_flags_t flags,
   1791                                    struct audio_config *config,
   1792                                    struct audio_stream_out **stream_out)
   1793 {
   1794     struct audio_device *adev = (struct audio_device *)dev;
   1795     struct stream_out *out;
   1796     int i, ret;
   1797 
   1798     ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
   1799           __func__, config->sample_rate, config->channel_mask, devices, flags);
   1800     *stream_out = NULL;
   1801     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
   1802 
   1803     if (devices == AUDIO_DEVICE_NONE)
   1804         devices = AUDIO_DEVICE_OUT_SPEAKER;
   1805 
   1806     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
   1807     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
   1808     out->flags = flags;
   1809     out->devices = devices;
   1810 
   1811     /* Init use case and pcm_config */
   1812     if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
   1813         out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   1814         pthread_mutex_lock(&adev->lock);
   1815         ret = read_hdmi_channel_masks(out);
   1816         pthread_mutex_unlock(&adev->lock);
   1817         if (ret != 0) {
   1818             /* If HDMI does not support multi channel playback, set the default */
   1819             out->config.channels = popcount(out->channel_mask);
   1820             set_hdmi_channels(adev->mixer, out->config.channels);
   1821             goto error_open;
   1822         }
   1823 
   1824         if (config->sample_rate == 0)
   1825             config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
   1826         if (config->channel_mask == 0)
   1827             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
   1828 
   1829         out->channel_mask = config->channel_mask;
   1830         out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
   1831         out->config = pcm_config_hdmi_multi;
   1832         out->config.rate = config->sample_rate;
   1833         out->config.channels = popcount(out->channel_mask);
   1834         out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
   1835         set_hdmi_channels(adev->mixer, out->config.channels);
   1836     } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
   1837         out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
   1838         out->config = pcm_config_deep_buffer;
   1839     } else {
   1840         out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
   1841         out->config = pcm_config_low_latency;
   1842     }
   1843 
   1844     if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
   1845         if(adev->primary_output == NULL)
   1846             adev->primary_output = out;
   1847         else {
   1848             ALOGE("%s: Primary output is already opened", __func__);
   1849             ret = -EEXIST;
   1850             goto error_open;
   1851         }
   1852     }
   1853 
   1854     /* Check if this usecase is already existing */
   1855     pthread_mutex_lock(&adev->lock);
   1856     if (get_usecase_from_list(adev, out->usecase) != NULL) {
   1857         ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
   1858         pthread_mutex_unlock(&adev->lock);
   1859         ret = -EEXIST;
   1860         goto error_open;
   1861     }
   1862     pthread_mutex_unlock(&adev->lock);
   1863 
   1864     out->stream.common.get_sample_rate = out_get_sample_rate;
   1865     out->stream.common.set_sample_rate = out_set_sample_rate;
   1866     out->stream.common.get_buffer_size = out_get_buffer_size;
   1867     out->stream.common.get_channels = out_get_channels;
   1868     out->stream.common.get_format = out_get_format;
   1869     out->stream.common.set_format = out_set_format;
   1870     out->stream.common.standby = out_standby;
   1871     out->stream.common.dump = out_dump;
   1872     out->stream.common.set_parameters = out_set_parameters;
   1873     out->stream.common.get_parameters = out_get_parameters;
   1874     out->stream.common.add_audio_effect = out_add_audio_effect;
   1875     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   1876     out->stream.get_latency = out_get_latency;
   1877     out->stream.set_volume = out_set_volume;
   1878     out->stream.write = out_write;
   1879     out->stream.get_render_position = out_get_render_position;
   1880     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
   1881 
   1882     out->dev = adev;
   1883     out->standby = 1;
   1884     /* out->muted = false; by calloc() */
   1885 
   1886     config->format = out->stream.common.get_format(&out->stream.common);
   1887     config->channel_mask = out->stream.common.get_channels(&out->stream.common);
   1888     config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
   1889 
   1890     *stream_out = &out->stream;
   1891     ALOGD("%s: exit", __func__);
   1892     return 0;
   1893 
   1894 error_open:
   1895     free(out);
   1896     *stream_out = NULL;
   1897     ALOGD("%s: exit: ret %d", __func__, ret);
   1898     return ret;
   1899 }
   1900 
   1901 static void adev_close_output_stream(struct audio_hw_device *dev,
   1902                                      struct audio_stream_out *stream)
   1903 {
   1904     ALOGD("%s: enter", __func__);
   1905     out_standby(&stream->common);
   1906     free(stream);
   1907     ALOGD("%s: exit", __func__);
   1908 }
   1909 
   1910 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   1911 {
   1912     struct audio_device *adev = (struct audio_device *)dev;
   1913     struct str_parms *parms;
   1914     char *str;
   1915     char value[32];
   1916     int val;
   1917     int ret;
   1918 
   1919     ALOGD("%s: enter: %s", __func__, kvpairs);
   1920 
   1921     parms = str_parms_create_str(kvpairs);
   1922     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
   1923     if (ret >= 0) {
   1924         int tty_mode;
   1925 
   1926         if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
   1927             tty_mode = TTY_MODE_OFF;
   1928         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
   1929             tty_mode = TTY_MODE_VCO;
   1930         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
   1931             tty_mode = TTY_MODE_HCO;
   1932         else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
   1933             tty_mode = TTY_MODE_FULL;
   1934         else
   1935             return -EINVAL;
   1936 
   1937         pthread_mutex_lock(&adev->lock);
   1938         if (tty_mode != adev->tty_mode) {
   1939             adev->tty_mode = tty_mode;
   1940             adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
   1941             if (adev->in_call)
   1942                 select_devices(adev, USECASE_VOICE_CALL);
   1943         }
   1944         pthread_mutex_unlock(&adev->lock);
   1945     }
   1946 
   1947     ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
   1948     if (ret >= 0) {
   1949         /* When set to false, HAL should disable EC and NS
   1950          * But it is currently not supported.
   1951          */
   1952         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
   1953             adev->bluetooth_nrec = true;
   1954         else
   1955             adev->bluetooth_nrec = false;
   1956     }
   1957 
   1958     ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
   1959     if (ret >= 0) {
   1960         if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
   1961             adev->screen_off = false;
   1962         else
   1963             adev->screen_off = true;
   1964     }
   1965 
   1966     ret = str_parms_get_int(parms, "rotation", &val);
   1967     if (ret >= 0) {
   1968         bool reverse_speakers = false;
   1969         switch(val) {
   1970         // FIXME: note that the code below assumes that the speakers are in the correct placement
   1971         //   relative to the user when the device is rotated 90deg from its default rotation. This
   1972         //   assumption is device-specific, not platform-specific like this code.
   1973         case 270:
   1974             reverse_speakers = true;
   1975             break;
   1976         case 0:
   1977         case 90:
   1978         case 180:
   1979             break;
   1980         default:
   1981             ALOGE("%s: unexpected rotation of %d", __func__, val);
   1982         }
   1983         pthread_mutex_lock(&adev->lock);
   1984         if (adev->speaker_lr_swap != reverse_speakers) {
   1985             adev->speaker_lr_swap = reverse_speakers;
   1986             // only update the selected device if there is active pcm playback
   1987             struct audio_usecase *usecase;
   1988             struct listnode *node;
   1989             list_for_each(node, &adev->usecase_list) {
   1990                 usecase = node_to_item(node, struct audio_usecase, list);
   1991                 if (usecase->type == PCM_PLAYBACK) {
   1992                     select_devices(adev, usecase->id);
   1993                     break;
   1994                 }
   1995             }
   1996         }
   1997         pthread_mutex_unlock(&adev->lock);
   1998     }
   1999 
   2000     str_parms_destroy(parms);
   2001     ALOGD("%s: exit with code(%d)", __func__, ret);
   2002     return ret;
   2003 }
   2004 
   2005 static char* adev_get_parameters(const struct audio_hw_device *dev,
   2006                                  const char *keys)
   2007 {
   2008     return strdup("");
   2009 }
   2010 
   2011 static int adev_init_check(const struct audio_hw_device *dev)
   2012 {
   2013     return 0;
   2014 }
   2015 
   2016 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   2017 {
   2018     struct audio_device *adev = (struct audio_device *)dev;
   2019     int vol, err = 0;
   2020 
   2021     pthread_mutex_lock(&adev->lock);
   2022     adev->voice_volume = volume;
   2023     if (adev->mode == AUDIO_MODE_IN_CALL) {
   2024         if (volume < 0.0) {
   2025             volume = 0.0;
   2026         } else if (volume > 1.0) {
   2027             volume = 1.0;
   2028         }
   2029 
   2030         vol = lrint(volume * 100.0);
   2031 
   2032         // Voice volume levels from android are mapped to driver volume levels as follows.
   2033         // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
   2034         // So adjust the volume to get the correct volume index in driver
   2035         vol = 100 - vol;
   2036 
   2037         if (adev->csd_client) {
   2038             if (adev->csd_volume == NULL) {
   2039                 ALOGE("%s: dlsym error for csd_client_volume", __func__);
   2040             } else {
   2041                 err = adev->csd_volume(vol);
   2042                 if (err < 0) {
   2043                     ALOGE("%s: csd_client error %d", __func__, err);
   2044                 }
   2045             }
   2046         } else {
   2047             ALOGE("%s: No CSD Client present", __func__);
   2048         }
   2049     }
   2050     pthread_mutex_unlock(&adev->lock);
   2051     return err;
   2052 }
   2053 
   2054 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
   2055 {
   2056     return -ENOSYS;
   2057 }
   2058 
   2059 static int adev_get_master_volume(struct audio_hw_device *dev,
   2060                                   float *volume)
   2061 {
   2062     return -ENOSYS;
   2063 }
   2064 
   2065 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
   2066 {
   2067     return -ENOSYS;
   2068 }
   2069 
   2070 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
   2071 {
   2072     return -ENOSYS;
   2073 }
   2074 
   2075 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
   2076 {
   2077     struct audio_device *adev = (struct audio_device *)dev;
   2078 
   2079     pthread_mutex_lock(&adev->lock);
   2080     if (adev->mode != mode) {
   2081         adev->mode = mode;
   2082     }
   2083     pthread_mutex_unlock(&adev->lock);
   2084     return 0;
   2085 }
   2086 
   2087 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   2088 {
   2089     struct audio_device *adev = (struct audio_device *)dev;
   2090     int err = 0;
   2091 
   2092     pthread_mutex_lock(&adev->lock);
   2093     adev->mic_mute = state;
   2094     if (adev->mode == AUDIO_MODE_IN_CALL) {
   2095         if (adev->csd_client) {
   2096             if (adev->csd_mic_mute == NULL) {
   2097                 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
   2098             } else {
   2099                 err = adev->csd_mic_mute(state);
   2100                 if (err < 0) {
   2101                     ALOGE("%s: csd_client error %d", __func__, err);
   2102                 }
   2103             }
   2104         } else {
   2105             ALOGE("%s: No CSD Client present", __func__);
   2106         }
   2107     }
   2108     pthread_mutex_unlock(&adev->lock);
   2109     return err;
   2110 }
   2111 
   2112 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   2113 {
   2114     struct audio_device *adev = (struct audio_device *)dev;
   2115 
   2116     *state = adev->mic_mute;
   2117 
   2118     return 0;
   2119 }
   2120 
   2121 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
   2122                                          const struct audio_config *config)
   2123 {
   2124     int channel_count = popcount(config->channel_mask);
   2125 
   2126     return get_input_buffer_size(config->sample_rate, config->format, channel_count);
   2127 }
   2128 
   2129 static int adev_open_input_stream(struct audio_hw_device *dev,
   2130                                   audio_io_handle_t handle,
   2131                                   audio_devices_t devices,
   2132                                   struct audio_config *config,
   2133                                   struct audio_stream_in **stream_in)
   2134 {
   2135     struct audio_device *adev = (struct audio_device *)dev;
   2136     struct stream_in *in;
   2137     int ret, buffer_size, frame_size;
   2138     int channel_count = popcount(config->channel_mask);
   2139 
   2140     ALOGD("%s: enter", __func__);
   2141     *stream_in = NULL;
   2142     if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
   2143         return -EINVAL;
   2144 
   2145     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
   2146 
   2147     in->stream.common.get_sample_rate = in_get_sample_rate;
   2148     in->stream.common.set_sample_rate = in_set_sample_rate;
   2149     in->stream.common.get_buffer_size = in_get_buffer_size;
   2150     in->stream.common.get_channels = in_get_channels;
   2151     in->stream.common.get_format = in_get_format;
   2152     in->stream.common.set_format = in_set_format;
   2153     in->stream.common.standby = in_standby;
   2154     in->stream.common.dump = in_dump;
   2155     in->stream.common.set_parameters = in_set_parameters;
   2156     in->stream.common.get_parameters = in_get_parameters;
   2157     in->stream.common.add_audio_effect = in_add_audio_effect;
   2158     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   2159     in->stream.set_gain = in_set_gain;
   2160     in->stream.read = in_read;
   2161     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   2162 
   2163     in->device = devices;
   2164     in->source = AUDIO_SOURCE_DEFAULT;
   2165     in->dev = adev;
   2166     in->standby = 1;
   2167     in->channel_mask = config->channel_mask;
   2168 
   2169     /* Update config params with the requested sample rate and channels */
   2170     in->usecase = USECASE_AUDIO_RECORD;
   2171     in->config = pcm_config_audio_capture;
   2172     in->config.channels = channel_count;
   2173     in->config.rate = config->sample_rate;
   2174 
   2175     frame_size = audio_stream_frame_size((struct audio_stream *)in);
   2176     buffer_size = get_input_buffer_size(config->sample_rate,
   2177                                         config->format,
   2178                                         channel_count);
   2179     in->config.period_size = buffer_size / frame_size;
   2180 
   2181     *stream_in = &in->stream;
   2182     ALOGD("%s: exit", __func__);
   2183     return 0;
   2184 
   2185 err_open:
   2186     free(in);
   2187     *stream_in = NULL;
   2188     return ret;
   2189 }
   2190 
   2191 static void adev_close_input_stream(struct audio_hw_device *dev,
   2192                                     struct audio_stream_in *stream)
   2193 {
   2194     ALOGD("%s", __func__);
   2195 
   2196     in_standby(&stream->common);
   2197     free(stream);
   2198 
   2199     return;
   2200 }
   2201 
   2202 static int adev_dump(const audio_hw_device_t *device, int fd)
   2203 {
   2204     return 0;
   2205 }
   2206 
   2207 static int adev_close(hw_device_t *device)
   2208 {
   2209     struct audio_device *adev = (struct audio_device *)device;
   2210     audio_route_free(adev->audio_route);
   2211     free(device);
   2212     return 0;
   2213 }
   2214 
   2215 static void init_platform_data(struct audio_device *adev)
   2216 {
   2217     char platform[PROPERTY_VALUE_MAX];
   2218     char baseband[PROPERTY_VALUE_MAX];
   2219     char value[PROPERTY_VALUE_MAX];
   2220 
   2221     adev->dualmic_config = DUALMIC_CONFIG_NONE;
   2222     adev->fluence_in_spkr_mode = false;
   2223     adev->fluence_in_voice_call = false;
   2224     adev->fluence_in_voice_rec = false;
   2225     adev->mic_type_analog = false;
   2226 
   2227     property_get("persist.audio.handset.mic.type",value,"");
   2228     if (!strcmp("analog", value))
   2229         adev->mic_type_analog = true;
   2230 
   2231     property_get("persist.audio.dualmic.config",value,"");
   2232     if (!strcmp("broadside", value)) {
   2233         adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
   2234         adev->acdb_settings |= DMIC_FLAG;
   2235     } else if (!strcmp("endfire", value)) {
   2236         adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
   2237         adev->acdb_settings |= DMIC_FLAG;
   2238     }
   2239 
   2240     if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
   2241         property_get("persist.audio.fluence.voicecall",value,"");
   2242         if (!strcmp("true", value)) {
   2243             adev->fluence_in_voice_call = true;
   2244         }
   2245 
   2246         property_get("persist.audio.fluence.voicerec",value,"");
   2247         if (!strcmp("true", value)) {
   2248             adev->fluence_in_voice_rec = true;
   2249         }
   2250 
   2251         property_get("persist.audio.fluence.speaker",value,"");
   2252         if (!strcmp("true", value)) {
   2253             adev->fluence_in_spkr_mode = true;
   2254         }
   2255     }
   2256 
   2257     adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
   2258     if (adev->acdb_handle == NULL) {
   2259         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
   2260     } else {
   2261         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
   2262         adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
   2263                                                     "acdb_loader_deallocate_ACDB");
   2264         adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
   2265                                                     "acdb_loader_send_audio_cal");
   2266         adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
   2267                                                     "acdb_loader_send_voice_cal");
   2268         adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
   2269                                                     "acdb_loader_init_ACDB");
   2270         if (adev->acdb_init == NULL)
   2271             ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
   2272         else
   2273             adev->acdb_init();
   2274     }
   2275 
   2276     /* If platform is Fusion3, load CSD Client specific symbols
   2277      * Voice call is handled by MDM and apps processor talks to
   2278      * MDM through CSD Client
   2279      */
   2280     property_get("ro.board.platform", platform, "");
   2281     property_get("ro.baseband", baseband, "");
   2282     if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
   2283         adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
   2284         if (adev->csd_client == NULL)
   2285             ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
   2286     }
   2287 
   2288     if (adev->csd_client) {
   2289         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
   2290         adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
   2291                                                     "csd_client_deinit");
   2292         adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
   2293                                                     "csd_client_disable_device");
   2294         adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
   2295                                                     "csd_client_enable_device");
   2296         adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
   2297                                                     "csd_client_start_voice");
   2298         adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
   2299                                                     "csd_client_stop_voice");
   2300         adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
   2301                                                     "csd_client_volume");
   2302         adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
   2303                                                     "csd_client_mic_mute");
   2304         adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
   2305                                                     "csd_client_init");
   2306 
   2307         if (adev->csd_client_init == NULL) {
   2308             ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
   2309         } else {
   2310             adev->csd_client_init();
   2311         }
   2312     }
   2313 }
   2314 
   2315 static int adev_open(const hw_module_t *module, const char *name,
   2316                      hw_device_t **device)
   2317 {
   2318     struct audio_device *adev;
   2319     int i, ret;
   2320 
   2321     ALOGD("%s: enter", __func__);
   2322     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
   2323 
   2324     adev = calloc(1, sizeof(struct audio_device));
   2325 
   2326     adev->mixer = mixer_open(MIXER_CARD);
   2327     if (!adev->mixer) {
   2328         ALOGE("Unable to open the mixer, aborting.");
   2329         return -ENOSYS;
   2330     }
   2331 
   2332     adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
   2333     if (!adev->audio_route) {
   2334         free(adev);
   2335         ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
   2336         *device = NULL;
   2337         return -EINVAL;
   2338     }
   2339 
   2340     adev->device.common.tag = HARDWARE_DEVICE_TAG;
   2341     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   2342     adev->device.common.module = (struct hw_module_t *)module;
   2343     adev->device.common.close = adev_close;
   2344 
   2345     adev->device.init_check = adev_init_check;
   2346     adev->device.set_voice_volume = adev_set_voice_volume;
   2347     adev->device.set_master_volume = adev_set_master_volume;
   2348     adev->device.get_master_volume = adev_get_master_volume;
   2349     adev->device.set_master_mute = adev_set_master_mute;
   2350     adev->device.get_master_mute = adev_get_master_mute;
   2351     adev->device.set_mode = adev_set_mode;
   2352     adev->device.set_mic_mute = adev_set_mic_mute;
   2353     adev->device.get_mic_mute = adev_get_mic_mute;
   2354     adev->device.set_parameters = adev_set_parameters;
   2355     adev->device.get_parameters = adev_get_parameters;
   2356     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
   2357     adev->device.open_output_stream = adev_open_output_stream;
   2358     adev->device.close_output_stream = adev_close_output_stream;
   2359     adev->device.open_input_stream = adev_open_input_stream;
   2360     adev->device.close_input_stream = adev_close_input_stream;
   2361     adev->device.dump = adev_dump;
   2362 
   2363     /* Set the default route before the PCM stream is opened */
   2364     pthread_mutex_lock(&adev->lock);
   2365     adev->mode = AUDIO_MODE_NORMAL;
   2366     adev->active_input = NULL;
   2367     adev->primary_output = NULL;
   2368     adev->out_device = AUDIO_DEVICE_NONE;
   2369     adev->voice_call_rx = NULL;
   2370     adev->voice_call_tx = NULL;
   2371     adev->voice_volume = 1.0f;
   2372     adev->tty_mode = TTY_MODE_OFF;
   2373     adev->bluetooth_nrec = true;
   2374     adev->in_call = false;
   2375     adev->acdb_settings = TTY_MODE_OFF;
   2376     adev->speaker_lr_swap = false;
   2377     for (i = 0; i < SND_DEVICE_MAX; i++) {
   2378         adev->snd_dev_ref_cnt[i] = 0;
   2379     }
   2380     list_init(&adev->usecase_list);
   2381     pthread_mutex_unlock(&adev->lock);
   2382 
   2383     /* Loads platform specific libraries dynamically */
   2384     init_platform_data(adev);
   2385 
   2386     *device = &adev->device.common;
   2387 
   2388     ALOGD("%s: exit", __func__);
   2389     return 0;
   2390 }
   2391 
   2392 static struct hw_module_methods_t hal_module_methods = {
   2393     .open = adev_open,
   2394 };
   2395 
   2396 struct audio_module HAL_MODULE_INFO_SYM = {
   2397     .common = {
   2398         .tag = HARDWARE_MODULE_TAG,
   2399         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   2400         .hal_api_version = HARDWARE_HAL_API_VERSION,
   2401         .id = AUDIO_HARDWARE_MODULE_ID,
   2402         .name = "QCOM Audio HAL",
   2403         .author = "Code Aurora Forum",
   2404         .methods = &hal_module_methods,
   2405     },
   2406 };
   2407