Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (C) 2012 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 
     20 #include <errno.h>
     21 #include <pthread.h>
     22 #include <stdint.h>
     23 #include <stdlib.h>
     24 #include <sys/time.h>
     25 #include <fcntl.h>
     26 
     27 #include <cutils/log.h>
     28 #include <cutils/properties.h>
     29 #include <cutils/str_parms.h>
     30 
     31 #include <hardware/audio.h>
     32 #include <hardware/hardware.h>
     33 
     34 #include <linux/videodev2.h>
     35 #include <videodev2_exynos_media.h>
     36 
     37 #include <system/audio.h>
     38 
     39 #include <tinyalsa/asoundlib.h>
     40 
     41 #include <audio_utils/resampler.h>
     42 #include <audio_route/audio_route.h>
     43 
     44 #include <BubbleLevel.h>
     45 
     46 #include <eS305VoiceProcessing.h>
     47 
     48 #define PCM_CARD 0
     49 #define PCM_CARD_SPDIF 1
     50 #define PCM_TOTAL 2
     51 
     52 #define PCM_DEVICE 0
     53 #define PCM_DEVICE_DEEP 1
     54 #define PCM_DEVICE_VOICE 2
     55 #define PCM_DEVICE_SCO 3
     56 
     57 #define MIXER_CARD 0
     58 
     59 /* duration in ms of volume ramp applied when starting capture to remove plop */
     60 #define CAPTURE_START_RAMP_MS 100
     61 
     62 /* default sampling for HDMI multichannel output */
     63 #define HDMI_MULTI_DEFAULT_SAMPLING_RATE  44100
     64 /* maximum number of channel mask configurations supported. Currently the primary
     65  * output only supports 1 (stereo) and the multi channel HDMI output 2 (5.1 and 7.1) */
     66 #define MAX_SUPPORTED_CHANNEL_MASKS 2
     67 
     68 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     69 
     70 struct pcm_config pcm_config = {
     71     .channels = 2,
     72     .rate = 44100,
     73     .period_size = 256,
     74     .period_count = 2,
     75     .format = PCM_FORMAT_S16_LE,
     76 };
     77 
     78 struct pcm_config pcm_config_in = {
     79     .channels = 2,
     80     .rate = 44100,
     81     .period_size = 1024,
     82     .period_count = 2,
     83     .format = PCM_FORMAT_S16_LE,
     84 };
     85 
     86 struct pcm_config pcm_config_sco = {
     87     .channels = 1,
     88     .rate = 8000,
     89     .period_size = 128,
     90     .period_count = 2,
     91     .format = PCM_FORMAT_S16_LE,
     92 };
     93 
     94 struct pcm_config pcm_config_deep = {
     95     .channels = 2,
     96     .rate = 44100,
     97     /* FIXME This is an arbitrary number, may change.
     98      * Dynamic configuration based on screen on/off is not implemented;
     99      * let's see what power consumption is first to see if necessary.
    100      */
    101     .period_size = 8192,
    102     .period_count = 2,
    103     .format = PCM_FORMAT_S16_LE,
    104 };
    105 
    106 struct pcm_config pcm_config_hdmi_multi = {
    107     .channels = 6, /* changed when the stream is opened */
    108     .rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE,
    109     .period_size = 1024,
    110     .period_count = 4,
    111     .format = PCM_FORMAT_S16_LE,
    112 };
    113 
    114 enum output_type {
    115     OUTPUT_DEEP_BUF,      // deep PCM buffers output stream
    116     OUTPUT_LOW_LATENCY,   // low latency output stream
    117     OUTPUT_HDMI,          // HDMI multi channel
    118     OUTPUT_TOTAL
    119 };
    120 
    121 struct audio_device {
    122     struct audio_hw_device hw_device;
    123 
    124     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    125     audio_devices_t out_device; /* "or" of stream_out.device for all active output streams */
    126     audio_devices_t in_device;
    127     bool mic_mute;
    128     struct audio_route *ar;
    129     audio_source_t input_source;
    130     int cur_route_id;     /* current route ID: combination of input source
    131                            * and output device IDs */
    132     struct pcm *pcm_voice_out;
    133     struct pcm *pcm_sco_out;
    134     struct pcm *pcm_voice_in;
    135     struct pcm *pcm_sco_in;
    136     int es305_preset;
    137     int es305_new_mode;
    138     int es305_mode;
    139     int hdmi_drv_fd;
    140     struct bubble_level *bubble_level;
    141     audio_channel_mask_t in_channel_mask;
    142     unsigned int sco_on_count;
    143 
    144     struct stream_out *outputs[OUTPUT_TOTAL];
    145 };
    146 
    147 struct stream_out {
    148     struct audio_stream_out stream;
    149 
    150     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    151     struct pcm *pcm[PCM_TOTAL];
    152     struct pcm_config config;
    153     unsigned int pcm_device;
    154     bool standby; /* true if all PCMs are inactive */
    155     audio_devices_t device;
    156     /* FIXME: when HDMI multichannel output is active, other outputs must be disabled as
    157      * HDMI and WM1811 share the same I2S. This means that notifications and other sounds are
    158      * silent when watching a 5.1 movie. */
    159     bool disabled;
    160     audio_channel_mask_t channel_mask;
    161     /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */
    162     audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1];
    163     bool muted;
    164     uint64_t written; /* total frames written, not cleared when entering standby */
    165 
    166     struct audio_device *dev;
    167 };
    168 
    169 struct stream_in {
    170     struct audio_stream_in stream;
    171 
    172     pthread_mutex_t lock; /* see note below on mutex acquisition order */
    173     struct pcm *pcm;
    174     bool standby;
    175 
    176     unsigned int requested_rate;
    177     struct resampler_itfe *resampler;
    178     struct resampler_buffer_provider buf_provider;
    179     int16_t *buffer;
    180     size_t frames_in;
    181     int read_status;
    182     audio_source_t input_source;
    183     audio_io_handle_t io_handle;
    184     audio_devices_t device;
    185     uint16_t ramp_vol;
    186     uint16_t ramp_step;
    187     size_t  ramp_frames;
    188     audio_channel_mask_t channel_mask;
    189 
    190     struct audio_device *dev;
    191 };
    192 
    193 #define STRING_TO_ENUM(string) { #string, string }
    194 
    195 struct string_to_enum {
    196     const char *name;
    197     uint32_t value;
    198 };
    199 
    200 const struct string_to_enum out_channels_name_to_enum_table[] = {
    201     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
    202     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
    203     STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
    204 };
    205 
    206 enum {
    207     OUT_DEVICE_SPEAKER,
    208     OUT_DEVICE_HEADSET,
    209     OUT_DEVICE_HEADPHONES,
    210     OUT_DEVICE_BT_SCO,
    211     OUT_DEVICE_SPEAKER_AND_HEADSET,
    212     OUT_DEVICE_TAB_SIZE,           /* number of rows in route_configs[][] */
    213     OUT_DEVICE_NONE,
    214     OUT_DEVICE_CNT
    215 };
    216 
    217 enum {
    218     IN_SOURCE_MIC,
    219     IN_SOURCE_CAMCORDER,
    220     IN_SOURCE_VOICE_RECOGNITION,
    221     IN_SOURCE_VOICE_COMMUNICATION,
    222     IN_SOURCE_TAB_SIZE,            /* number of lines in route_configs[][] */
    223     IN_SOURCE_NONE,
    224     IN_SOURCE_CNT
    225 };
    226 
    227 enum {
    228     ES305_MODE_DEFAULT,
    229     ES305_MODE_LEVEL,
    230     ES305_NUM_MODES,
    231 };
    232 
    233 int get_output_device_id(audio_devices_t device)
    234 {
    235     if (device == AUDIO_DEVICE_NONE)
    236         return OUT_DEVICE_NONE;
    237 
    238     if (popcount(device) == 2) {
    239         if ((device == (AUDIO_DEVICE_OUT_SPEAKER |
    240                        AUDIO_DEVICE_OUT_WIRED_HEADSET)) ||
    241                 (device == (AUDIO_DEVICE_OUT_SPEAKER |
    242                         AUDIO_DEVICE_OUT_WIRED_HEADPHONE)))
    243             return OUT_DEVICE_SPEAKER_AND_HEADSET;
    244         else
    245             return OUT_DEVICE_NONE;
    246     }
    247 
    248     if (popcount(device) != 1)
    249         return OUT_DEVICE_NONE;
    250 
    251     switch (device) {
    252     case AUDIO_DEVICE_OUT_SPEAKER:
    253         return OUT_DEVICE_SPEAKER;
    254     case AUDIO_DEVICE_OUT_WIRED_HEADSET:
    255         return OUT_DEVICE_HEADSET;
    256     case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
    257         return OUT_DEVICE_HEADPHONES;
    258     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
    259     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
    260     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
    261         return OUT_DEVICE_BT_SCO;
    262     default:
    263         return OUT_DEVICE_NONE;
    264     }
    265 }
    266 
    267 int get_input_source_id(audio_source_t source)
    268 {
    269     switch (source) {
    270     case AUDIO_SOURCE_DEFAULT:
    271         return IN_SOURCE_NONE;
    272     case AUDIO_SOURCE_MIC:
    273         return IN_SOURCE_MIC;
    274     case AUDIO_SOURCE_CAMCORDER:
    275         return IN_SOURCE_CAMCORDER;
    276     case AUDIO_SOURCE_VOICE_RECOGNITION:
    277         return IN_SOURCE_VOICE_RECOGNITION;
    278     case AUDIO_SOURCE_VOICE_COMMUNICATION:
    279         return IN_SOURCE_VOICE_COMMUNICATION;
    280     default:
    281         return IN_SOURCE_NONE;
    282     }
    283 }
    284 
    285 struct route_config {
    286     const char * const output_route;
    287     const char * const input_route;
    288     int es305_preset[ES305_NUM_MODES]; // es305 preset for this route.
    289                                        // -1 means es305 bypass
    290 };
    291 
    292 const struct route_config media_speaker = {
    293     "media-speaker",
    294     "media-main-mic",
    295     { ES305_PRESET_OFF,
    296       ES305_PRESET_OFF }
    297 };
    298 
    299 const struct route_config media_headphones = {
    300     "media-headphones",
    301     "media-main-mic",
    302     { ES305_PRESET_OFF,
    303       ES305_PRESET_OFF }
    304 };
    305 
    306 const struct route_config media_headset = {
    307     "media-headphones",
    308     "media-headset-mic",
    309     { ES305_PRESET_OFF,
    310       ES305_PRESET_OFF }
    311 };
    312 
    313 const struct route_config camcorder_speaker = {
    314     "media-speaker",
    315     "media-second-mic",
    316     { ES305_PRESET_CAMCORDER,
    317       ES305_PRESET_CAMCORDER }
    318 };
    319 
    320 const struct route_config camcorder_headphones = {
    321     "media-headphones",
    322     "media-second-mic",
    323     { ES305_PRESET_CAMCORDER,
    324       ES305_PRESET_CAMCORDER }
    325 };
    326 
    327 const struct route_config voice_rec_speaker = {
    328     "voice-rec-speaker",
    329     "voice-rec-main-mic",
    330     { ES305_PRESET_ASRA_HANDHELD,
    331       ES305_PRESET_ASRA_DESKTOP }
    332 };
    333 
    334 const struct route_config voice_rec_headphones = {
    335     "voice-rec-headphones",
    336     "voice-rec-main-mic",
    337     { ES305_PRESET_ASRA_HANDHELD,
    338       ES305_PRESET_ASRA_DESKTOP }
    339 };
    340 
    341 const struct route_config voice_rec_headset = {
    342     "voice-rec-headphones",
    343     "voice-rec-headset-mic",
    344     { ES305_PRESET_ASRA_HEADSET,
    345       ES305_PRESET_ASRA_HEADSET }
    346 };
    347 
    348 const struct route_config communication_speaker = {
    349     "communication-speaker",
    350     "communication-main-mic",
    351     { ES305_PRESET_VOIP_HANDHELD,
    352       ES305_PRESET_VOIP_DESKTOP }
    353 };
    354 
    355 const struct route_config communication_headphones = {
    356     "communication-headphones",
    357     "communication-main-mic",
    358     { ES305_PRESET_VOIP_HEADPHONES,
    359       ES305_PRESET_VOIP_HP_DESKTOP}
    360 };
    361 
    362 const struct route_config communication_headset = {
    363     "communication-headphones",
    364     "communication-headset-mic",
    365     { ES305_PRESET_VOIP_HEADSET,
    366       ES305_PRESET_VOIP_HEADSET }
    367 };
    368 
    369 const struct route_config speaker_and_headphones = {
    370     "speaker-and-headphones",
    371     "main-mic",
    372     { ES305_PRESET_CURRENT,
    373       ES305_PRESET_CURRENT }
    374 };
    375 
    376 const struct route_config bluetooth_sco = {
    377     "bt-sco-headset",
    378     "bt-sco-mic",
    379     { ES305_PRESET_OFF,
    380       ES305_PRESET_OFF }
    381 };
    382 
    383 const struct route_config * const route_configs[IN_SOURCE_TAB_SIZE]
    384                                                [OUT_DEVICE_TAB_SIZE] = {
    385     {   /* IN_SOURCE_MIC */
    386         &media_speaker,             /* OUT_DEVICE_SPEAKER */
    387         &media_headset,             /* OUT_DEVICE_HEADSET */
    388         &media_headphones,          /* OUT_DEVICE_HEADPHONES */
    389         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    390         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    391     },
    392     {   /* IN_SOURCE_CAMCORDER */
    393         &camcorder_speaker,         /* OUT_DEVICE_SPEAKER */
    394         &camcorder_headphones,      /* OUT_DEVICE_HEADSET */
    395         &camcorder_headphones,      /* OUT_DEVICE_HEADPHONES */
    396         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    397         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    398     },
    399     {   /* IN_SOURCE_VOICE_RECOGNITION */
    400         &voice_rec_speaker,         /* OUT_DEVICE_SPEAKER */
    401         &voice_rec_headset,         /* OUT_DEVICE_HEADSET */
    402         &voice_rec_headphones,      /* OUT_DEVICE_HEADPHONES */
    403         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    404         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    405     },
    406     {   /* IN_SOURCE_VOICE_COMMUNICATION */
    407         &communication_speaker,     /* OUT_DEVICE_SPEAKER */
    408         &communication_headset,     /* OUT_DEVICE_HEADSET */
    409         &communication_headphones,  /* OUT_DEVICE_HEADPHONES */
    410         &bluetooth_sco,             /* OUT_DEVICE_BT_SCO */
    411         &speaker_and_headphones     /* OUT_DEVICE_SPEAKER_AND_HEADSET */
    412     }
    413 };
    414 
    415 static int do_out_standby(struct stream_out *out);
    416 
    417 /**
    418  * NOTE: when multiple mutexes have to be acquired, always respect the
    419  * following order: hw device > in stream > out stream
    420  */
    421 
    422 /* Helper functions */
    423 
    424 static int open_hdmi_driver(struct audio_device *adev)
    425 {
    426     if (adev->hdmi_drv_fd < 0) {
    427         adev->hdmi_drv_fd = open("/dev/video16", O_RDWR);
    428         if (adev->hdmi_drv_fd < 0)
    429             ALOGE("%s cannot open video16 (%d)", __func__, adev->hdmi_drv_fd);
    430     }
    431     return adev->hdmi_drv_fd;
    432 }
    433 
    434 /* must be called with hw device mutex locked */
    435 static int enable_hdmi_audio(struct audio_device *adev, int enable)
    436 {
    437     int ret;
    438     struct v4l2_control ctrl;
    439 
    440     ret = open_hdmi_driver(adev);
    441     if (ret < 0)
    442         return ret;
    443 
    444     ctrl.id = V4L2_CID_TV_ENABLE_HDMI_AUDIO;
    445     ctrl.value = !!enable;
    446     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
    447 
    448     if (ret < 0)
    449         ALOGE("V4L2_CID_TV_ENABLE_HDMI_AUDIO ioctl error (%d)", errno);
    450 
    451     return ret;
    452 }
    453 
    454 /* must be called with hw device mutex locked */
    455 static int read_hdmi_channel_masks(struct audio_device *adev, struct stream_out *out) {
    456     int ret;
    457     struct v4l2_control ctrl;
    458 
    459     ret = open_hdmi_driver(adev);
    460     if (ret < 0)
    461         return ret;
    462 
    463     ctrl.id = V4L2_CID_TV_MAX_AUDIO_CHANNELS;
    464     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_G_CTRL, &ctrl);
    465     if (ret < 0) {
    466         ALOGE("V4L2_CID_TV_MAX_AUDIO_CHANNELS ioctl error (%d)", errno);
    467         return ret;
    468     }
    469 
    470     ALOGV("%s ioctl %d got %d max channels", __func__, ret, ctrl.value);
    471 
    472     if (ctrl.value != 6 && ctrl.value != 8)
    473         return -ENOSYS;
    474 
    475     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
    476     if (ctrl.value == 8)
    477         out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
    478 
    479     return ret;
    480 }
    481 
    482 /* must be called with hw device mutex locked */
    483 static int set_hdmi_channels(struct audio_device *adev, int channels) {
    484     int ret;
    485     struct v4l2_control ctrl;
    486 
    487     ret = open_hdmi_driver(adev);
    488     if (ret < 0)
    489         return ret;
    490 
    491     ctrl.id = V4L2_CID_TV_SET_NUM_CHANNELS;
    492     ctrl.value = channels;
    493     ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl);
    494     if (ret < 0)
    495         ALOGE("V4L2_CID_TV_SET_NUM_CHANNELS ioctl error (%d)", errno);
    496 
    497     return ret;
    498 }
    499 
    500 static void select_devices(struct audio_device *adev)
    501 {
    502     int output_device_id = get_output_device_id(adev->out_device);
    503     int input_source_id = get_input_source_id(adev->input_source);
    504     const char *output_route = NULL;
    505     const char *input_route = NULL;
    506     int new_route_id;
    507     int new_es305_preset = -1;
    508 
    509     audio_route_reset(adev->ar);
    510 
    511     enable_hdmi_audio(adev, adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL);
    512 
    513     new_route_id = (1 << (input_source_id + OUT_DEVICE_CNT)) + (1 << output_device_id);
    514     if ((new_route_id == adev->cur_route_id) && (adev->es305_mode == adev->es305_new_mode))
    515         return;
    516     adev->cur_route_id = new_route_id;
    517     adev->es305_mode = adev->es305_new_mode;
    518 
    519     if (input_source_id != IN_SOURCE_NONE) {
    520         if (output_device_id != OUT_DEVICE_NONE) {
    521             input_route =
    522                     route_configs[input_source_id][output_device_id]->input_route;
    523             output_route =
    524                     route_configs[input_source_id][output_device_id]->output_route;
    525             new_es305_preset =
    526                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
    527         } else {
    528             switch (adev->in_device) {
    529             case AUDIO_DEVICE_IN_WIRED_HEADSET & ~AUDIO_DEVICE_BIT_IN:
    530                 output_device_id = OUT_DEVICE_HEADSET;
    531                 break;
    532             case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & ~AUDIO_DEVICE_BIT_IN:
    533                 output_device_id = OUT_DEVICE_BT_SCO;
    534                 break;
    535             default:
    536                 output_device_id = OUT_DEVICE_SPEAKER;
    537                 break;
    538             }
    539             input_route =
    540                     route_configs[input_source_id][output_device_id]->input_route;
    541             new_es305_preset =
    542                 route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode];
    543         }
    544         // disable noise suppression when capturing front and back mic for voice recognition
    545         if ((adev->input_source == AUDIO_SOURCE_VOICE_RECOGNITION) &&
    546                 (adev->in_channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK))
    547             new_es305_preset = -1;
    548     } else {
    549         if (output_device_id != OUT_DEVICE_NONE) {
    550             output_route =
    551                     route_configs[IN_SOURCE_MIC][output_device_id]->output_route;
    552         }
    553     }
    554 
    555     ALOGV("select_devices() devices %#x input src %d output route %s input route %s",
    556           adev->out_device, adev->input_source,
    557           output_route ? output_route : "none",
    558           input_route ? input_route : "none");
    559 
    560     if (output_route)
    561         audio_route_apply_path(adev->ar, output_route);
    562     if (input_route)
    563         audio_route_apply_path(adev->ar, input_route);
    564 
    565     if ((new_es305_preset != ES305_PRESET_CURRENT) &&
    566             (new_es305_preset != adev->es305_preset)) {
    567         ALOGV("  select_devices() changing es305 preset from %d to %d",
    568               adev->es305_preset, new_es305_preset);
    569         if (eS305_UsePreset(new_es305_preset) == 0) {
    570             adev->es305_preset = new_es305_preset;
    571         }
    572     }
    573 
    574     audio_route_update_mixer(adev->ar);
    575 }
    576 
    577 void bubblelevel_callback(bool is_level, void *user_data)
    578 {
    579     struct audio_device *adev = (struct audio_device *)user_data;
    580     int es305_mode;
    581 
    582     if (is_level)
    583         es305_mode = ES305_MODE_LEVEL;
    584     else
    585         es305_mode = ES305_MODE_DEFAULT;
    586 
    587     pthread_mutex_lock(&adev->lock);
    588     if (es305_mode != adev->es305_mode) {
    589         adev->es305_new_mode = es305_mode;
    590         select_devices(adev);
    591         ALOGV("bubblelevel_callback is_level %d es305_mode %d", is_level, es305_mode);
    592     }
    593     pthread_mutex_unlock(&adev->lock);
    594 }
    595 
    596 /* must be called with hw device mutex locked */
    597 bool get_bubblelevel(struct audio_device *adev)
    598 {
    599     if (!adev->bubble_level) {
    600         adev->bubble_level = bubble_level_create();
    601         if (adev->bubble_level)
    602             adev->bubble_level->set_callback(adev->bubble_level, bubblelevel_callback, adev);
    603     }
    604     return (adev->bubble_level != NULL);
    605 }
    606 
    607 static void force_non_hdmi_out_standby(struct audio_device *adev)
    608 {
    609     enum output_type type;
    610     struct stream_out *out;
    611 
    612     for (type = 0; type < OUTPUT_TOTAL; ++type) {
    613         out = adev->outputs[type];
    614         if (type == OUTPUT_HDMI || !out)
    615             continue;
    616         pthread_mutex_lock(&out->lock);
    617         do_out_standby(out);
    618         pthread_mutex_unlock(&out->lock);
    619     }
    620 }
    621 
    622 /* must be called with the hw device mutex locked, OK to hold other mutexes */
    623 static void start_bt_sco(struct audio_device *adev) {
    624     if (adev->sco_on_count++ > 0)
    625         return;
    626 
    627     adev->pcm_voice_out = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_OUT | PCM_MONOTONIC,
    628                               &pcm_config_sco);
    629     if (adev->pcm_voice_out && !pcm_is_ready(adev->pcm_voice_out)) {
    630         ALOGE("pcm_open(VOICE_OUT) failed: %s", pcm_get_error(adev->pcm_voice_out));
    631         goto err_voice_out;
    632     }
    633     adev->pcm_sco_out = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_OUT | PCM_MONOTONIC,
    634                             &pcm_config_sco);
    635     if (adev->pcm_sco_out && !pcm_is_ready(adev->pcm_sco_out)) {
    636         ALOGE("pcm_open(SCO_OUT) failed: %s", pcm_get_error(adev->pcm_sco_out));
    637         goto err_sco_out;
    638     }
    639     adev->pcm_voice_in = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_IN,
    640                                  &pcm_config_sco);
    641     if (adev->pcm_voice_in && !pcm_is_ready(adev->pcm_voice_in)) {
    642         ALOGE("pcm_open(VOICE_IN) failed: %s", pcm_get_error(adev->pcm_voice_in));
    643         goto err_voice_in;
    644     }
    645     adev->pcm_sco_in = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_IN,
    646                                &pcm_config_sco);
    647     if (adev->pcm_sco_in && !pcm_is_ready(adev->pcm_sco_in)) {
    648         ALOGE("pcm_open(SCO_IN) failed: %s", pcm_get_error(adev->pcm_sco_in));
    649         goto err_sco_in;
    650     }
    651 
    652     pcm_start(adev->pcm_voice_out);
    653     pcm_start(adev->pcm_sco_out);
    654     pcm_start(adev->pcm_voice_in);
    655     pcm_start(adev->pcm_sco_in);
    656 
    657     return;
    658 
    659 err_sco_in:
    660     pcm_close(adev->pcm_sco_in);
    661 err_voice_in:
    662     pcm_close(adev->pcm_voice_in);
    663 err_sco_out:
    664     pcm_close(adev->pcm_sco_out);
    665 err_voice_out:
    666     pcm_close(adev->pcm_voice_out);
    667 }
    668 
    669 /* must be called with the hw device mutex locked, OK to hold other mutexes */
    670 static void stop_bt_sco(struct audio_device *adev) {
    671     if (adev->sco_on_count == 0 || --adev->sco_on_count > 0)
    672         return;
    673 
    674     pcm_stop(adev->pcm_voice_out);
    675     pcm_stop(adev->pcm_sco_out);
    676     pcm_stop(adev->pcm_voice_in);
    677     pcm_stop(adev->pcm_sco_in);
    678 
    679     pcm_close(adev->pcm_voice_out);
    680     pcm_close(adev->pcm_sco_out);
    681     pcm_close(adev->pcm_voice_in);
    682     pcm_close(adev->pcm_sco_in);
    683 }
    684 
    685 /* must be called with hw device and output stream mutexes locked */
    686 static int start_output_stream(struct stream_out *out)
    687 {
    688     struct audio_device *adev = out->dev;
    689     int type;
    690 
    691     if (out == adev->outputs[OUTPUT_HDMI]) {
    692         force_non_hdmi_out_standby(adev);
    693     } else if (adev->outputs[OUTPUT_HDMI] && !adev->outputs[OUTPUT_HDMI]->standby) {
    694         out->disabled = true;
    695         return 0;
    696     }
    697 
    698     out->disabled = false;
    699 
    700     if (out->device & (AUDIO_DEVICE_OUT_SPEAKER |
    701                        AUDIO_DEVICE_OUT_WIRED_HEADSET |
    702                        AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
    703                        AUDIO_DEVICE_OUT_AUX_DIGITAL |
    704                        AUDIO_DEVICE_OUT_ALL_SCO)) {
    705 
    706         out->pcm[PCM_CARD] = pcm_open(PCM_CARD, out->pcm_device,
    707                                       PCM_OUT | PCM_MONOTONIC, &out->config);
    708 
    709         if (out->pcm[PCM_CARD] && !pcm_is_ready(out->pcm[PCM_CARD])) {
    710             ALOGE("pcm_open(PCM_CARD) failed: %s",
    711                   pcm_get_error(out->pcm[PCM_CARD]));
    712             pcm_close(out->pcm[PCM_CARD]);
    713             return -ENOMEM;
    714         }
    715     }
    716 
    717     if (out->device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) {
    718         out->pcm[PCM_CARD_SPDIF] = pcm_open(PCM_CARD_SPDIF, out->pcm_device,
    719                                             PCM_OUT | PCM_MONOTONIC, &out->config);
    720 
    721         if (out->pcm[PCM_CARD_SPDIF] &&
    722                 !pcm_is_ready(out->pcm[PCM_CARD_SPDIF])) {
    723             ALOGE("pcm_open(PCM_CARD_SPDIF) failed: %s",
    724                   pcm_get_error(out->pcm[PCM_CARD_SPDIF]));
    725             pcm_close(out->pcm[PCM_CARD_SPDIF]);
    726             return -ENOMEM;
    727         }
    728     }
    729 
    730     adev->out_device |= out->device;
    731     select_devices(adev);
    732 
    733     if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
    734         start_bt_sco(adev);
    735 
    736     if (out->device & AUDIO_DEVICE_OUT_AUX_DIGITAL)
    737         set_hdmi_channels(adev, out->config.channels);
    738 
    739     /* anticipate level measurement in case we start capture later */
    740     if (get_bubblelevel(adev))
    741         adev->bubble_level->poll_once(adev->bubble_level);
    742 
    743     return 0;
    744 }
    745 
    746 /* must be called with hw device and input stream mutexes locked */
    747 static int start_input_stream(struct stream_in *in)
    748 {
    749     struct audio_device *adev = in->dev;
    750 
    751     in->pcm = pcm_open(PCM_CARD, PCM_DEVICE, PCM_IN, &pcm_config_in);
    752 
    753     if (in->pcm && !pcm_is_ready(in->pcm)) {
    754         ALOGE("pcm_open() failed: %s", pcm_get_error(in->pcm));
    755         pcm_close(in->pcm);
    756         return -ENOMEM;
    757     }
    758 
    759     /* if no supported sample rate is available, use the resampler */
    760     if (in->resampler)
    761         in->resampler->reset(in->resampler);
    762 
    763     in->frames_in = 0;
    764     adev->input_source = in->input_source;
    765     adev->in_device = in->device;
    766     adev->in_channel_mask = in->channel_mask;
    767 
    768     eS305_SetActiveIoHandle(in->io_handle);
    769     select_devices(adev);
    770 
    771     if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
    772         start_bt_sco(adev);
    773 
    774     /* initialize volume ramp */
    775     in->ramp_frames = (CAPTURE_START_RAMP_MS * in->requested_rate) / 1000;
    776     in->ramp_step = (uint16_t)(USHRT_MAX / in->ramp_frames);
    777     in->ramp_vol = 0;;
    778 
    779     if (get_bubblelevel(adev)) {
    780         adev->bubble_level->set_poll_interval(adev->bubble_level, BL_POLL_INTERVAL_MIN_SEC);
    781         adev->bubble_level->start_polling(adev->bubble_level);
    782     }
    783 
    784     return 0;
    785 }
    786 
    787 static size_t get_input_buffer_size(unsigned int sample_rate,
    788                                     audio_format_t format,
    789                                     unsigned int channel_count)
    790 {
    791     size_t size;
    792 
    793     /*
    794      * take resampling into account and return the closest majoring
    795      * multiple of 16 frames, as audioflinger expects audio buffers to
    796      * be a multiple of 16 frames
    797      */
    798     size = (pcm_config_in.period_size * sample_rate) / pcm_config_in.rate;
    799     size = ((size + 15) / 16) * 16;
    800 
    801     return size * channel_count * audio_bytes_per_sample(format);
    802 }
    803 
    804 static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
    805                                    struct resampler_buffer* buffer)
    806 {
    807     struct stream_in *in;
    808     size_t i;
    809 
    810     if (buffer_provider == NULL || buffer == NULL)
    811         return -EINVAL;
    812 
    813     in = (struct stream_in *)((char *)buffer_provider -
    814                                    offsetof(struct stream_in, buf_provider));
    815 
    816     if (in->pcm == NULL) {
    817         buffer->raw = NULL;
    818         buffer->frame_count = 0;
    819         in->read_status = -ENODEV;
    820         return -ENODEV;
    821     }
    822 
    823     if (in->frames_in == 0) {
    824         in->read_status = pcm_read(in->pcm,
    825                                    (void*)in->buffer,
    826                                    pcm_frames_to_bytes(in->pcm, pcm_config_in.period_size));
    827         if (in->read_status != 0) {
    828             ALOGE("get_next_buffer() pcm_read error %d", in->read_status);
    829             buffer->raw = NULL;
    830             buffer->frame_count = 0;
    831             return in->read_status;
    832         }
    833 
    834         in->frames_in = pcm_config_in.period_size;
    835 
    836         /* Do stereo to mono conversion in place by discarding right channel */
    837         if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
    838             for (i = 1; i < in->frames_in; i++)
    839                 in->buffer[i] = in->buffer[i * 2];
    840     }
    841 
    842     buffer->frame_count = (buffer->frame_count > in->frames_in) ?
    843                                 in->frames_in : buffer->frame_count;
    844     buffer->i16 = in->buffer +
    845             (pcm_config_in.period_size - in->frames_in) * popcount(in->channel_mask);
    846 
    847     return in->read_status;
    848 
    849 }
    850 
    851 static void release_buffer(struct resampler_buffer_provider *buffer_provider,
    852                                   struct resampler_buffer* buffer)
    853 {
    854     struct stream_in *in;
    855 
    856     if (buffer_provider == NULL || buffer == NULL)
    857         return;
    858 
    859     in = (struct stream_in *)((char *)buffer_provider -
    860                                    offsetof(struct stream_in, buf_provider));
    861 
    862     in->frames_in -= buffer->frame_count;
    863 }
    864 
    865 /* read_frames() reads frames from kernel driver, down samples to capture rate
    866  * if necessary and output the number of frames requested to the buffer specified */
    867 static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames)
    868 {
    869     ssize_t frames_wr = 0;
    870     size_t frame_size = audio_stream_frame_size(&in->stream.common);
    871 
    872     while (frames_wr < frames) {
    873         size_t frames_rd = frames - frames_wr;
    874         if (in->resampler != NULL) {
    875             in->resampler->resample_from_provider(in->resampler,
    876                     (int16_t *)((char *)buffer +
    877                             frames_wr * frame_size),
    878                     &frames_rd);
    879         } else {
    880             struct resampler_buffer buf = {
    881                     { raw : NULL, },
    882                     frame_count : frames_rd,
    883             };
    884             get_next_buffer(&in->buf_provider, &buf);
    885             if (buf.raw != NULL) {
    886                 memcpy((char *)buffer +
    887                            frames_wr * frame_size,
    888                         buf.raw,
    889                         buf.frame_count * frame_size);
    890                 frames_rd = buf.frame_count;
    891             }
    892             release_buffer(&in->buf_provider, &buf);
    893         }
    894         /* in->read_status is updated by getNextBuffer() also called by
    895          * in->resampler->resample_from_provider() */
    896         if (in->read_status != 0)
    897             return in->read_status;
    898 
    899         frames_wr += frames_rd;
    900     }
    901     return frames_wr;
    902 }
    903 
    904 /* API functions */
    905 
    906 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
    907 {
    908     struct stream_out *out = (struct stream_out *)stream;
    909 
    910     return out->config.rate;
    911 }
    912 
    913 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
    914 {
    915     return -ENOSYS;
    916 }
    917 
    918 static size_t out_get_buffer_size(const struct audio_stream *stream)
    919 {
    920     struct stream_out *out = (struct stream_out *)stream;
    921 
    922     return out->config.period_size *
    923             audio_stream_frame_size((struct audio_stream *)stream);
    924 }
    925 
    926 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
    927 {
    928     struct stream_out *out = (struct stream_out *)stream;
    929 
    930     return out->channel_mask;
    931 }
    932 
    933 static audio_format_t out_get_format(const struct audio_stream *stream)
    934 {
    935     return AUDIO_FORMAT_PCM_16_BIT;
    936 }
    937 
    938 static int out_set_format(struct audio_stream *stream, audio_format_t format)
    939 {
    940     return -ENOSYS;
    941 }
    942 
    943 /* Return the set of output devices associated with active streams
    944  * other than out.  Assumes out is non-NULL and out->dev is locked.
    945  */
    946 static audio_devices_t output_devices(struct stream_out *out)
    947 {
    948     struct audio_device *dev = out->dev;
    949     enum output_type type;
    950     audio_devices_t devices = AUDIO_DEVICE_NONE;
    951 
    952     for (type = 0; type < OUTPUT_TOTAL; ++type) {
    953         struct stream_out *other = dev->outputs[type];
    954         if (other && (other != out) && !other->standby) {
    955             /* safe to access other stream without a mutex,
    956              * because we hold the dev lock,
    957              * which prevents the other stream from being closed
    958              */
    959             devices |= other->device;
    960         }
    961     }
    962 
    963     return devices;
    964 }
    965 
    966 static int do_out_standby(struct stream_out *out)
    967 {
    968     struct audio_device *adev = out->dev;
    969     int i;
    970 
    971     if (!out->standby) {
    972         for (i = 0; i < PCM_TOTAL; i++) {
    973             if (out->pcm[i]) {
    974                 pcm_close(out->pcm[i]);
    975                 out->pcm[i] = NULL;
    976             }
    977         }
    978         out->standby = true;
    979 
    980         if (out == adev->outputs[OUTPUT_HDMI]) {
    981             /* force standby on low latency output stream so that it can reuse HDMI driver if
    982              * necessary when restarted */
    983             force_non_hdmi_out_standby(adev);
    984         }
    985 
    986         if (out->device & AUDIO_DEVICE_OUT_ALL_SCO)
    987             stop_bt_sco(adev);
    988 
    989         /* re-calculate the set of active devices from other streams */
    990         adev->out_device = output_devices(out);
    991 
    992         /* Skip resetting the mixer if no output device is active */
    993         if (adev->out_device)
    994             select_devices(adev);
    995     }
    996 
    997     return 0;
    998 }
    999 
   1000 static int out_standby(struct audio_stream *stream)
   1001 {
   1002     struct stream_out *out = (struct stream_out *)stream;
   1003     int ret;
   1004 
   1005     pthread_mutex_lock(&out->dev->lock);
   1006     pthread_mutex_lock(&out->lock);
   1007 
   1008     ret = do_out_standby(out);
   1009 
   1010     pthread_mutex_unlock(&out->lock);
   1011     pthread_mutex_unlock(&out->dev->lock);
   1012 
   1013     return ret;
   1014 }
   1015 
   1016 static int out_dump(const struct audio_stream *stream, int fd)
   1017 {
   1018     return 0;
   1019 }
   1020 
   1021 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1022 {
   1023     struct stream_out *out = (struct stream_out *)stream;
   1024     struct audio_device *adev = out->dev;
   1025     struct str_parms *parms;
   1026     char value[32];
   1027     int ret;
   1028     unsigned int val;
   1029 
   1030     parms = str_parms_create_str(kvpairs);
   1031 
   1032     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
   1033                             value, sizeof(value));
   1034     pthread_mutex_lock(&adev->lock);
   1035     pthread_mutex_lock(&out->lock);
   1036     if (ret >= 0) {
   1037         val = atoi(value);
   1038         if ((out->device != val) && (val != 0)) {
   1039             /* Force standby if moving to/from SPDIF or if the output
   1040              * device changes when in SPDIF mode */
   1041             if (((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^
   1042                  (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) ||
   1043                 (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
   1044                 do_out_standby(out);
   1045             }
   1046 
   1047             /* force output standby to start or stop SCO pcm stream if needed */
   1048             if ((val & AUDIO_DEVICE_OUT_ALL_SCO) ^
   1049                     (out->device & AUDIO_DEVICE_OUT_ALL_SCO)) {
   1050                 do_out_standby(out);
   1051             }
   1052 
   1053             if (!out->standby && (out == adev->outputs[OUTPUT_HDMI] ||
   1054                     !adev->outputs[OUTPUT_HDMI] ||
   1055                     adev->outputs[OUTPUT_HDMI]->standby)) {
   1056                 adev->out_device = output_devices(out) | val;
   1057                 select_devices(adev);
   1058             }
   1059             out->device = val;
   1060         }
   1061     }
   1062     pthread_mutex_unlock(&out->lock);
   1063     pthread_mutex_unlock(&adev->lock);
   1064 
   1065     str_parms_destroy(parms);
   1066     return ret;
   1067 }
   1068 
   1069 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
   1070 {
   1071     struct stream_out *out = (struct stream_out *)stream;
   1072     struct str_parms *query = str_parms_create_str(keys);
   1073     char *str;
   1074     char value[256];
   1075     struct str_parms *reply = str_parms_create();
   1076     size_t i, j;
   1077     int ret;
   1078     bool first = true;
   1079 
   1080     ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
   1081     if (ret >= 0) {
   1082         value[0] = '\0';
   1083         i = 0;
   1084         /* the last entry in supported_channel_masks[] is always 0 */
   1085         while (out->supported_channel_masks[i] != 0) {
   1086             for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
   1087                 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
   1088                     if (!first) {
   1089                         strcat(value, "|");
   1090                     }
   1091                     strcat(value, out_channels_name_to_enum_table[j].name);
   1092                     first = false;
   1093                     break;
   1094                 }
   1095             }
   1096             i++;
   1097         }
   1098         str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
   1099         str = str_parms_to_str(reply);
   1100     } else {
   1101         str = strdup(keys);
   1102     }
   1103 
   1104     str_parms_destroy(query);
   1105     str_parms_destroy(reply);
   1106     return str;
   1107 }
   1108 
   1109 static uint32_t out_get_latency(const struct audio_stream_out *stream)
   1110 {
   1111     struct stream_out *out = (struct stream_out *)stream;
   1112 
   1113     return (out->config.period_size * out->config.period_count * 1000) /
   1114             out->config.rate;
   1115 }
   1116 
   1117 static int out_set_volume(struct audio_stream_out *stream, float left,
   1118                           float right)
   1119 {
   1120     struct stream_out *out = (struct stream_out *)stream;
   1121     struct audio_device *adev = out->dev;
   1122 
   1123     if (out == adev->outputs[OUTPUT_HDMI]) {
   1124         /* only take left channel into account: the API is for stereo anyway */
   1125         out->muted = (left == 0.0f);
   1126         return 0;
   1127     }
   1128     return -ENOSYS;
   1129 }
   1130 
   1131 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
   1132                          size_t bytes)
   1133 {
   1134     int ret = 0;
   1135     struct stream_out *out = (struct stream_out *)stream;
   1136     struct audio_device *adev = out->dev;
   1137     int i;
   1138 
   1139     /*
   1140      * acquiring hw device mutex systematically is useful if a low
   1141      * priority thread is waiting on the output stream mutex - e.g.
   1142      * executing out_set_parameters() while holding the hw device
   1143      * mutex
   1144      */
   1145     pthread_mutex_lock(&adev->lock);
   1146     pthread_mutex_lock(&out->lock);
   1147     if (out->standby) {
   1148         ret = start_output_stream(out);
   1149         if (ret != 0) {
   1150             pthread_mutex_unlock(&adev->lock);
   1151             goto exit;
   1152         }
   1153         out->standby = false;
   1154     }
   1155     pthread_mutex_unlock(&adev->lock);
   1156 
   1157     if (out->disabled) {
   1158         ret = -EPIPE;
   1159         goto exit;
   1160     }
   1161 
   1162     if (out->muted)
   1163         memset((void *)buffer, 0, bytes);
   1164 
   1165     /* Write to all active PCMs */
   1166     for (i = 0; i < PCM_TOTAL; i++)
   1167         if (out->pcm[i]) {
   1168             ret = pcm_write(out->pcm[i], (void *)buffer, bytes);
   1169             if (ret != 0)
   1170                 break;
   1171         }
   1172     if (ret == 0)
   1173         out->written += bytes / (out->config.channels * sizeof(short));
   1174 
   1175 exit:
   1176     pthread_mutex_unlock(&out->lock);
   1177 
   1178     if (ret != 0) {
   1179         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
   1180                out_get_sample_rate(&stream->common));
   1181     }
   1182 
   1183     return bytes;
   1184 }
   1185 
   1186 static int out_get_render_position(const struct audio_stream_out *stream,
   1187                                    uint32_t *dsp_frames)
   1188 {
   1189     return -EINVAL;
   1190 }
   1191 
   1192 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1193 {
   1194     return 0;
   1195 }
   1196 
   1197 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
   1198 {
   1199     return 0;
   1200 }
   1201 
   1202 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
   1203                                         int64_t *timestamp)
   1204 {
   1205     return -EINVAL;
   1206 }
   1207 
   1208 static int out_get_presentation_position(const struct audio_stream_out *stream,
   1209                                    uint64_t *frames, struct timespec *timestamp)
   1210 {
   1211     struct stream_out *out = (struct stream_out *)stream;
   1212     int ret = -1;
   1213 
   1214     pthread_mutex_lock(&out->lock);
   1215 
   1216     int i;
   1217     // There is a question how to implement this correctly when there is more than one PCM stream.
   1218     // We are just interested in the frames pending for playback in the kernel buffer here,
   1219     // not the total played since start.  The current behavior should be safe because the
   1220     // cases where both cards are active are marginal.
   1221     for (i = 0; i < PCM_TOTAL; i++)
   1222         if (out->pcm[i]) {
   1223             size_t avail;
   1224             if (pcm_get_htimestamp(out->pcm[i], &avail, timestamp) == 0) {
   1225                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
   1226                 // FIXME This calculation is incorrect if there is buffering after app processor
   1227                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
   1228                 // It would be unusual for this value to be negative, but check just in case ...
   1229                 if (signed_frames >= 0) {
   1230                     *frames = signed_frames;
   1231                     ret = 0;
   1232                 }
   1233                 break;
   1234             }
   1235         }
   1236 
   1237     pthread_mutex_unlock(&out->lock);
   1238 
   1239     return ret;
   1240 }
   1241 
   1242 /** audio_stream_in implementation **/
   1243 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
   1244 {
   1245     struct stream_in *in = (struct stream_in *)stream;
   1246 
   1247     return in->requested_rate;
   1248 }
   1249 
   1250 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
   1251 {
   1252     return 0;
   1253 }
   1254 
   1255 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
   1256 {
   1257     struct stream_in *in = (struct stream_in *)stream;
   1258 
   1259     return in->channel_mask;
   1260 }
   1261 
   1262 
   1263 static size_t in_get_buffer_size(const struct audio_stream *stream)
   1264 {
   1265     struct stream_in *in = (struct stream_in *)stream;
   1266 
   1267     return get_input_buffer_size(in->requested_rate,
   1268                                  AUDIO_FORMAT_PCM_16_BIT,
   1269                                  popcount(in_get_channels(stream)));
   1270 }
   1271 
   1272 static audio_format_t in_get_format(const struct audio_stream *stream)
   1273 {
   1274     return AUDIO_FORMAT_PCM_16_BIT;
   1275 }
   1276 
   1277 static int in_set_format(struct audio_stream *stream, audio_format_t format)
   1278 {
   1279     return -ENOSYS;
   1280 }
   1281 
   1282 static int do_in_standby(struct stream_in *in)
   1283 {
   1284     struct audio_device *adev = in->dev;
   1285 
   1286     if (!in->standby) {
   1287         pcm_close(in->pcm);
   1288         in->pcm = NULL;
   1289 
   1290         if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)
   1291             stop_bt_sco(adev);
   1292 
   1293         in->dev->input_source = AUDIO_SOURCE_DEFAULT;
   1294         in->dev->in_device = AUDIO_DEVICE_NONE;
   1295         in->dev->in_channel_mask = 0;
   1296         select_devices(adev);
   1297         in->standby = true;
   1298 
   1299         if (get_bubblelevel(adev))
   1300             in->dev->bubble_level->stop_polling(adev->bubble_level);
   1301     }
   1302 
   1303     eS305_SetActiveIoHandle(ES305_IO_HANDLE_NONE);
   1304     return 0;
   1305 }
   1306 
   1307 static int in_standby(struct audio_stream *stream)
   1308 {
   1309     struct stream_in *in = (struct stream_in *)stream;
   1310     int ret;
   1311 
   1312     pthread_mutex_lock(&in->dev->lock);
   1313     pthread_mutex_lock(&in->lock);
   1314 
   1315     ret = do_in_standby(in);
   1316 
   1317     pthread_mutex_unlock(&in->lock);
   1318     pthread_mutex_unlock(&in->dev->lock);
   1319 
   1320     return ret;
   1321 }
   1322 
   1323 static int in_dump(const struct audio_stream *stream, int fd)
   1324 {
   1325     return 0;
   1326 }
   1327 
   1328 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
   1329 {
   1330     struct stream_in *in = (struct stream_in *)stream;
   1331     struct audio_device *adev = in->dev;
   1332     struct str_parms *parms;
   1333     char value[32];
   1334     int ret;
   1335     unsigned int val;
   1336     bool apply_now = false;
   1337 
   1338     parms = str_parms_create_str(kvpairs);
   1339 
   1340     pthread_mutex_lock(&adev->lock);
   1341     pthread_mutex_lock(&in->lock);
   1342     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE,
   1343                             value, sizeof(value));
   1344     if (ret >= 0) {
   1345         val = atoi(value);
   1346         /* no audio source uses val == 0 */
   1347         if ((in->input_source != val) && (val != 0)) {
   1348             in->input_source = val;
   1349             apply_now = !in->standby;
   1350         }
   1351     }
   1352 
   1353     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
   1354                             value, sizeof(value));
   1355     if (ret >= 0) {
   1356         /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
   1357         val = atoi(value) & ~AUDIO_DEVICE_BIT_IN;
   1358         /* no audio device uses val == 0 */
   1359         if ((in->device != val) && (val != 0)) {
   1360             /* force output standby to start or stop SCO pcm stream if needed */
   1361             if ((val & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) ^
   1362                     (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
   1363                 do_in_standby(in);
   1364             }
   1365             in->device = val;
   1366             apply_now = !in->standby;
   1367         }
   1368     }
   1369 
   1370     if (apply_now) {
   1371         adev->input_source = in->input_source;
   1372         adev->in_device = in->device;
   1373         select_devices(adev);
   1374     }
   1375 
   1376     pthread_mutex_unlock(&in->lock);
   1377     pthread_mutex_unlock(&adev->lock);
   1378 
   1379     str_parms_destroy(parms);
   1380     return ret;
   1381 }
   1382 
   1383 static char * in_get_parameters(const struct audio_stream *stream,
   1384                                 const char *keys)
   1385 {
   1386     return strdup("");
   1387 }
   1388 
   1389 static int in_set_gain(struct audio_stream_in *stream, float gain)
   1390 {
   1391     return 0;
   1392 }
   1393 
   1394 static void in_apply_ramp(struct stream_in *in, int16_t *buffer, size_t frames)
   1395 {
   1396     size_t i;
   1397     uint16_t vol = in->ramp_vol;
   1398     uint16_t step = in->ramp_step;
   1399 
   1400     frames = (frames < in->ramp_frames) ? frames : in->ramp_frames;
   1401 
   1402     if (in->channel_mask == AUDIO_CHANNEL_IN_MONO)
   1403         for (i = 0; i < frames; i++)
   1404         {
   1405             buffer[i] = (int16_t)((buffer[i] * vol) >> 16);
   1406             vol += step;
   1407         }
   1408     else
   1409         for (i = 0; i < frames; i++)
   1410         {
   1411             buffer[2*i] = (int16_t)((buffer[2*i] * vol) >> 16);
   1412             buffer[2*i + 1] = (int16_t)((buffer[2*i + 1] * vol) >> 16);
   1413             vol += step;
   1414         }
   1415 
   1416 
   1417     in->ramp_vol = vol;
   1418     in->ramp_frames -= frames;
   1419 }
   1420 
   1421 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
   1422                        size_t bytes)
   1423 {
   1424     int ret = 0;
   1425     struct stream_in *in = (struct stream_in *)stream;
   1426     struct audio_device *adev = in->dev;
   1427     size_t frames_rq = bytes / audio_stream_frame_size(&stream->common);
   1428 
   1429     /*
   1430      * acquiring hw device mutex systematically is useful if a low
   1431      * priority thread is waiting on the input stream mutex - e.g.
   1432      * executing in_set_parameters() while holding the hw device
   1433      * mutex
   1434      */
   1435     pthread_mutex_lock(&adev->lock);
   1436     pthread_mutex_lock(&in->lock);
   1437     if (in->standby) {
   1438         ret = start_input_stream(in);
   1439         if (ret == 0)
   1440             in->standby = 0;
   1441     }
   1442     pthread_mutex_unlock(&adev->lock);
   1443 
   1444     if (ret < 0)
   1445         goto exit;
   1446 
   1447     /*if (in->num_preprocessors != 0)
   1448         ret = process_frames(in, buffer, frames_rq);
   1449       else */
   1450     ret = read_frames(in, buffer, frames_rq);
   1451 
   1452     if (ret > 0)
   1453         ret = 0;
   1454 
   1455     if (in->ramp_frames > 0)
   1456         in_apply_ramp(in, buffer, frames_rq);
   1457 
   1458     /*
   1459      * Instead of writing zeroes here, we could trust the hardware
   1460      * to always provide zeroes when muted.
   1461      */
   1462     if (ret == 0 && adev->mic_mute)
   1463         memset(buffer, 0, bytes);
   1464 
   1465 exit:
   1466     if (ret < 0)
   1467         usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
   1468                in_get_sample_rate(&stream->common));
   1469 
   1470     pthread_mutex_unlock(&in->lock);
   1471     return bytes;
   1472 }
   1473 
   1474 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
   1475 {
   1476     return 0;
   1477 }
   1478 
   1479 static int in_add_audio_effect(const struct audio_stream *stream,
   1480                                effect_handle_t effect)
   1481 {
   1482     struct stream_in *in = (struct stream_in *)stream;
   1483     effect_descriptor_t descr;
   1484     if ((*effect)->get_descriptor(effect, &descr) == 0) {
   1485 
   1486         pthread_mutex_lock(&in->dev->lock);
   1487         pthread_mutex_lock(&in->lock);
   1488 
   1489         eS305_AddEffect(&descr, in->io_handle);
   1490 
   1491         pthread_mutex_unlock(&in->lock);
   1492         pthread_mutex_unlock(&in->dev->lock);
   1493     }
   1494 
   1495     return 0;
   1496 }
   1497 
   1498 static int in_remove_audio_effect(const struct audio_stream *stream,
   1499                                   effect_handle_t effect)
   1500 {
   1501     struct stream_in *in = (struct stream_in *)stream;
   1502     effect_descriptor_t descr;
   1503     if ((*effect)->get_descriptor(effect, &descr) == 0) {
   1504 
   1505         pthread_mutex_lock(&in->dev->lock);
   1506         pthread_mutex_lock(&in->lock);
   1507 
   1508         eS305_RemoveEffect(&descr, in->io_handle);
   1509 
   1510         pthread_mutex_unlock(&in->lock);
   1511         pthread_mutex_unlock(&in->dev->lock);
   1512     }
   1513 
   1514     return 0;
   1515 }
   1516 
   1517 static int adev_open_output_stream(struct audio_hw_device *dev,
   1518                                    audio_io_handle_t handle,
   1519                                    audio_devices_t devices,
   1520                                    audio_output_flags_t flags,
   1521                                    struct audio_config *config,
   1522                                    struct audio_stream_out **stream_out)
   1523 {
   1524     struct audio_device *adev = (struct audio_device *)dev;
   1525     struct stream_out *out;
   1526     int ret;
   1527     enum output_type type;
   1528 
   1529     out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
   1530     if (!out)
   1531         return -ENOMEM;
   1532 
   1533     out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
   1534     out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
   1535     if (devices == AUDIO_DEVICE_NONE)
   1536         devices = AUDIO_DEVICE_OUT_SPEAKER;
   1537     out->device = devices;
   1538 
   1539     if (flags & AUDIO_OUTPUT_FLAG_DIRECT &&
   1540                    devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) {
   1541         pthread_mutex_lock(&adev->lock);
   1542         ret = read_hdmi_channel_masks(adev, out);
   1543         pthread_mutex_unlock(&adev->lock);
   1544         if (ret != 0)
   1545             goto err_open;
   1546         if (config->sample_rate == 0)
   1547             config->sample_rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE;
   1548         if (config->channel_mask == 0)
   1549             config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
   1550         out->channel_mask = config->channel_mask;
   1551         out->config = pcm_config_hdmi_multi;
   1552         out->config.rate = config->sample_rate;
   1553         out->config.channels = popcount(config->channel_mask);
   1554         out->pcm_device = PCM_DEVICE;
   1555         type = OUTPUT_HDMI;
   1556     } else if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
   1557         out->config = pcm_config_deep;
   1558         out->pcm_device = PCM_DEVICE_DEEP;
   1559         type = OUTPUT_DEEP_BUF;
   1560     } else {
   1561         out->config = pcm_config;
   1562         out->pcm_device = PCM_DEVICE;
   1563         type = OUTPUT_LOW_LATENCY;
   1564     }
   1565 
   1566     out->stream.common.get_sample_rate = out_get_sample_rate;
   1567     out->stream.common.set_sample_rate = out_set_sample_rate;
   1568     out->stream.common.get_buffer_size = out_get_buffer_size;
   1569     out->stream.common.get_channels = out_get_channels;
   1570     out->stream.common.get_format = out_get_format;
   1571     out->stream.common.set_format = out_set_format;
   1572     out->stream.common.standby = out_standby;
   1573     out->stream.common.dump = out_dump;
   1574     out->stream.common.set_parameters = out_set_parameters;
   1575     out->stream.common.get_parameters = out_get_parameters;
   1576     out->stream.common.add_audio_effect = out_add_audio_effect;
   1577     out->stream.common.remove_audio_effect = out_remove_audio_effect;
   1578     out->stream.get_latency = out_get_latency;
   1579     out->stream.set_volume = out_set_volume;
   1580     out->stream.write = out_write;
   1581     out->stream.get_render_position = out_get_render_position;
   1582     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
   1583     out->stream.get_presentation_position = out_get_presentation_position;
   1584 
   1585     out->dev = adev;
   1586 
   1587     config->format = out_get_format(&out->stream.common);
   1588     config->channel_mask = out_get_channels(&out->stream.common);
   1589     config->sample_rate = out_get_sample_rate(&out->stream.common);
   1590 
   1591     out->standby = true;
   1592     /* out->muted = false; by calloc() */
   1593     /* out->written = 0; by calloc() */
   1594 
   1595     pthread_mutex_lock(&adev->lock);
   1596     if (adev->outputs[type]) {
   1597         pthread_mutex_unlock(&adev->lock);
   1598         ret = -EBUSY;
   1599         goto err_open;
   1600     }
   1601     adev->outputs[type] = out;
   1602     pthread_mutex_unlock(&adev->lock);
   1603 
   1604     *stream_out = &out->stream;
   1605 
   1606     return 0;
   1607 
   1608 err_open:
   1609     free(out);
   1610     *stream_out = NULL;
   1611     return ret;
   1612 }
   1613 
   1614 static void adev_close_output_stream(struct audio_hw_device *dev,
   1615                                      struct audio_stream_out *stream)
   1616 {
   1617     struct audio_device *adev;
   1618     enum output_type type;
   1619 
   1620     out_standby(&stream->common);
   1621     adev = (struct audio_device *)dev;
   1622     pthread_mutex_lock(&adev->lock);
   1623     for (type = 0; type < OUTPUT_TOTAL; ++type) {
   1624         if (adev->outputs[type] == (struct stream_out *) stream) {
   1625             adev->outputs[type] = NULL;
   1626             break;
   1627         }
   1628     }
   1629     pthread_mutex_unlock(&adev->lock);
   1630     free(stream);
   1631 }
   1632 
   1633 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
   1634 {
   1635     return 0;
   1636 }
   1637 
   1638 static char * adev_get_parameters(const struct audio_hw_device *dev,
   1639                                   const char *keys)
   1640 {
   1641     struct audio_device *adev = (struct audio_device *)dev;
   1642     struct str_parms *parms = str_parms_create_str(keys);
   1643     char value[32];
   1644     int ret = str_parms_get_str(parms, "ec_supported", value, sizeof(value));
   1645     char *str;
   1646 
   1647     str_parms_destroy(parms);
   1648     if (ret >= 0) {
   1649         parms = str_parms_create_str("ec_supported=yes");
   1650         str = str_parms_to_str(parms);
   1651         str_parms_destroy(parms);
   1652         return str;
   1653     }
   1654     return strdup("");
   1655 }
   1656 
   1657 static int adev_init_check(const struct audio_hw_device *dev)
   1658 {
   1659     return 0;
   1660 }
   1661 
   1662 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
   1663 {
   1664     return -ENOSYS;
   1665 }
   1666 
   1667 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
   1668 {
   1669     return -ENOSYS;
   1670 }
   1671 
   1672 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
   1673 {
   1674     return 0;
   1675 }
   1676 
   1677 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
   1678 {
   1679     struct audio_device *adev = (struct audio_device *)dev;
   1680 
   1681     adev->mic_mute = state;
   1682 
   1683     return 0;
   1684 }
   1685 
   1686 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
   1687 {
   1688     struct audio_device *adev = (struct audio_device *)dev;
   1689 
   1690     *state = adev->mic_mute;
   1691 
   1692     return 0;
   1693 }
   1694 
   1695 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
   1696                                          const struct audio_config *config)
   1697 {
   1698 
   1699     return get_input_buffer_size(config->sample_rate, config->format,
   1700                                  popcount(config->channel_mask));
   1701 }
   1702 
   1703 static int adev_open_input_stream(struct audio_hw_device *dev,
   1704                                   audio_io_handle_t handle,
   1705                                   audio_devices_t devices,
   1706                                   struct audio_config *config,
   1707                                   struct audio_stream_in **stream_in)
   1708 {
   1709     struct audio_device *adev = (struct audio_device *)dev;
   1710     struct stream_in *in;
   1711     int ret;
   1712 
   1713     *stream_in = NULL;
   1714 
   1715     /* Respond with a request for mono if a different format is given. */
   1716     if (config->channel_mask != AUDIO_CHANNEL_IN_MONO &&
   1717             config->channel_mask != AUDIO_CHANNEL_IN_FRONT_BACK) {
   1718         config->channel_mask = AUDIO_CHANNEL_IN_MONO;
   1719         return -EINVAL;
   1720     }
   1721 
   1722     in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
   1723     if (!in)
   1724         return -ENOMEM;
   1725 
   1726     in->stream.common.get_sample_rate = in_get_sample_rate;
   1727     in->stream.common.set_sample_rate = in_set_sample_rate;
   1728     in->stream.common.get_buffer_size = in_get_buffer_size;
   1729     in->stream.common.get_channels = in_get_channels;
   1730     in->stream.common.get_format = in_get_format;
   1731     in->stream.common.set_format = in_set_format;
   1732     in->stream.common.standby = in_standby;
   1733     in->stream.common.dump = in_dump;
   1734     in->stream.common.set_parameters = in_set_parameters;
   1735     in->stream.common.get_parameters = in_get_parameters;
   1736     in->stream.common.add_audio_effect = in_add_audio_effect;
   1737     in->stream.common.remove_audio_effect = in_remove_audio_effect;
   1738     in->stream.set_gain = in_set_gain;
   1739     in->stream.read = in_read;
   1740     in->stream.get_input_frames_lost = in_get_input_frames_lost;
   1741 
   1742     in->dev = adev;
   1743     in->standby = true;
   1744     in->requested_rate = config->sample_rate;
   1745     in->input_source = AUDIO_SOURCE_DEFAULT;
   1746     /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */
   1747     in->device = devices & ~AUDIO_DEVICE_BIT_IN;
   1748     in->io_handle = handle;
   1749     in->channel_mask = config->channel_mask;
   1750 
   1751     in->buffer = malloc(pcm_config_in.period_size * pcm_config_in.channels
   1752                                                * audio_stream_frame_size(&in->stream.common));
   1753 
   1754     if (!in->buffer) {
   1755         ret = -ENOMEM;
   1756         goto err_malloc;
   1757     }
   1758 
   1759     if (in->requested_rate != pcm_config_in.rate) {
   1760         in->buf_provider.get_next_buffer = get_next_buffer;
   1761         in->buf_provider.release_buffer = release_buffer;
   1762 
   1763         ret = create_resampler(pcm_config_in.rate,
   1764                                in->requested_rate,
   1765                                popcount(in->channel_mask),
   1766                                RESAMPLER_QUALITY_DEFAULT,
   1767                                &in->buf_provider,
   1768                                &in->resampler);
   1769         if (ret != 0) {
   1770             ret = -EINVAL;
   1771             goto err_resampler;
   1772         }
   1773     }
   1774 
   1775     *stream_in = &in->stream;
   1776     return 0;
   1777 
   1778 err_resampler:
   1779     free(in->buffer);
   1780 err_malloc:
   1781     free(in);
   1782     return ret;
   1783 }
   1784 
   1785 static void adev_close_input_stream(struct audio_hw_device *dev,
   1786                                    struct audio_stream_in *stream)
   1787 {
   1788     struct stream_in *in = (struct stream_in *)stream;
   1789 
   1790     in_standby(&stream->common);
   1791     if (in->resampler) {
   1792         release_resampler(in->resampler);
   1793         in->resampler = NULL;
   1794     }
   1795     free(in->buffer);
   1796     free(stream);
   1797 }
   1798 
   1799 static int adev_dump(const audio_hw_device_t *device, int fd)
   1800 {
   1801     return 0;
   1802 }
   1803 
   1804 static int adev_close(hw_device_t *device)
   1805 {
   1806     struct audio_device *adev = (struct audio_device *)device;
   1807 
   1808     audio_route_free(adev->ar);
   1809 
   1810     eS305_Release();
   1811 
   1812     if (adev->hdmi_drv_fd >= 0)
   1813         close(adev->hdmi_drv_fd);
   1814 
   1815     if (adev->bubble_level)
   1816         bubble_level_release(adev->bubble_level);
   1817 
   1818     free(device);
   1819     return 0;
   1820 }
   1821 
   1822 static int adev_open(const hw_module_t* module, const char* name,
   1823                      hw_device_t** device)
   1824 {
   1825     struct audio_device *adev;
   1826     int ret;
   1827 
   1828     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
   1829         return -EINVAL;
   1830 
   1831     adev = calloc(1, sizeof(struct audio_device));
   1832     if (!adev)
   1833         return -ENOMEM;
   1834 
   1835     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
   1836     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
   1837     adev->hw_device.common.module = (struct hw_module_t *) module;
   1838     adev->hw_device.common.close = adev_close;
   1839 
   1840     adev->hw_device.init_check = adev_init_check;
   1841     adev->hw_device.set_voice_volume = adev_set_voice_volume;
   1842     adev->hw_device.set_master_volume = adev_set_master_volume;
   1843     adev->hw_device.set_mode = adev_set_mode;
   1844     adev->hw_device.set_mic_mute = adev_set_mic_mute;
   1845     adev->hw_device.get_mic_mute = adev_get_mic_mute;
   1846     adev->hw_device.set_parameters = adev_set_parameters;
   1847     adev->hw_device.get_parameters = adev_get_parameters;
   1848     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
   1849     adev->hw_device.open_output_stream = adev_open_output_stream;
   1850     adev->hw_device.close_output_stream = adev_close_output_stream;
   1851     adev->hw_device.open_input_stream = adev_open_input_stream;
   1852     adev->hw_device.close_input_stream = adev_close_input_stream;
   1853     adev->hw_device.dump = adev_dump;
   1854 
   1855     adev->ar = audio_route_init(MIXER_CARD, NULL);
   1856     adev->input_source = AUDIO_SOURCE_DEFAULT;
   1857     /* adev->cur_route_id initial value is 0 and such that first device
   1858      * selection is always applied by select_devices() */
   1859 
   1860     adev->es305_preset = ES305_PRESET_INIT;
   1861     adev->es305_new_mode = ES305_MODE_LEVEL;
   1862     adev->es305_mode = ES305_MODE_LEVEL;
   1863     adev->hdmi_drv_fd = -1;
   1864 
   1865     *device = &adev->hw_device.common;
   1866 
   1867     return 0;
   1868 }
   1869 
   1870 static struct hw_module_methods_t hal_module_methods = {
   1871     .open = adev_open,
   1872 };
   1873 
   1874 struct audio_module HAL_MODULE_INFO_SYM = {
   1875     .common = {
   1876         .tag = HARDWARE_MODULE_TAG,
   1877         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
   1878         .hal_api_version = HARDWARE_HAL_API_VERSION,
   1879         .id = AUDIO_HARDWARE_MODULE_ID,
   1880         .name = "Manta audio HW HAL",
   1881         .author = "The Android Open Source Project",
   1882         .methods = &hal_module_methods,
   1883     },
   1884 };
   1885