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