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