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