audio_hw.c revision 778bf121b16f3bd87d4c8b4deaa577f5d2061733
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->in_call && adev->csd_client != NULL) {
603        in_call_device_switch = true;
604        /* This must be called before disabling the mixer controls on APQ side */
605        if (adev->csd_disable_device == NULL) {
606            ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
607        } else {
608            status = adev->csd_disable_device();
609            if (status < 0) {
610                ALOGE("%s: csd_client_disable_device, failed, error %d",
611                      __func__, status);
612            }
613        }
614    }
615
616    if ((out_snd_device != adev->cur_out_snd_device || in_call_device_switch)
617            && adev->out_snd_device_active) {
618        usecase = &adev->usecase_list;
619        while (usecase->next != NULL) {
620            usecase = usecase->next;
621            if (usecase->type == PCM_PLAYBACK || usecase->type == VOICE_CALL) {
622                disable_audio_route(adev->audio_route, usecase->id,
623                                    adev->cur_out_snd_device);
624            }
625        }
626        audio_route_update_mixer(adev->audio_route);
627        /* Disable current rx device */
628        disable_snd_device(adev->audio_route, adev->cur_out_snd_device);
629        adev->out_snd_device_active = false;
630    }
631
632    if ((in_snd_device != adev->cur_in_snd_device || in_call_device_switch)
633            && adev->in_snd_device_active) {
634        usecase = &adev->usecase_list;
635        while (usecase->next != NULL) {
636            usecase = usecase->next;
637            if (usecase->type == PCM_CAPTURE) {
638                disable_audio_route(adev->audio_route, usecase->id,
639                                    adev->cur_in_snd_device);
640            }
641        }
642        audio_route_update_mixer(adev->audio_route);
643        /* Disable current tx device */
644        disable_snd_device(adev->audio_route, adev->cur_in_snd_device);
645        adev->in_snd_device_active = false;
646    }
647
648    if (out_snd_device != SND_DEVICE_INVALID && !adev->out_snd_device_active) {
649        /* Enable new rx device */
650        status = enable_snd_device(adev, out_snd_device);
651        if (status != 0) {
652            ALOGE("%s: Failed to set mixer ctls for snd_device(%d)",
653                  __func__, out_snd_device);
654            return status;
655        }
656        adev->out_snd_device_active = true;
657        adev->cur_out_snd_device = out_snd_device;
658    }
659
660    if (in_snd_device != SND_DEVICE_INVALID && !adev->in_snd_device_active) {
661        /* Enable new tx device */
662        status = enable_snd_device(adev, in_snd_device);
663        if (status != 0) {
664            ALOGE("%s: Failed to set mixer ctls for snd_device(%d)",
665                  __func__, out_snd_device);
666            return status;
667        }
668        adev->in_snd_device_active = true;
669        adev->cur_in_snd_device = in_snd_device;
670    }
671    audio_route_update_mixer(adev->audio_route);
672
673    usecase = &adev->usecase_list;
674    while (usecase->next != NULL) {
675        usecase = usecase->next;
676        if (usecase->type == PCM_PLAYBACK || usecase->type == VOICE_CALL) {
677            usecase->devices = adev->out_device; /* TODO: fix device logic */
678            status = enable_audio_route(adev->audio_route, usecase->id,
679                                        adev->cur_out_snd_device);
680        } else {
681            status = enable_audio_route(adev->audio_route, usecase->id,
682                                        adev->cur_in_snd_device);
683        }
684    }
685    audio_route_update_mixer(adev->audio_route);
686
687    if (adev->mode == AUDIO_MODE_IN_CALL && adev->csd_client) {
688        if (adev->csd_enable_device == NULL) {
689            ALOGE("%s: dlsym error for csd_client_enable_device",
690                  __func__);
691        } else {
692            acdb_rx_id = get_acdb_device_id(out_snd_device);
693            acdb_tx_id = get_acdb_device_id(in_snd_device);
694
695            status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
696                                             adev->acdb_settings);
697            if (status < 0) {
698                ALOGE("%s: csd_client_enable_device, failed, error %d",
699                      __func__, status);
700            }
701        }
702    }
703
704    ALOGV("%s: exit: status(%d)", __func__, status);
705    return status;
706}
707
708static void add_usecase_to_list(struct audio_device *adev,
709                                struct audio_usecase *uc_info)
710{
711    struct audio_usecase *first_entry = adev->usecase_list.next;
712    ALOGV("%s: enter: usecase(%d)", __func__, uc_info->id);
713    /* Insert the new entry on the top of the list */
714    adev->usecase_list.next = uc_info;
715    uc_info->next = first_entry;
716    ALOGV("%s: exit", __func__);
717}
718
719static void remove_usecase_from_list(struct audio_device *adev,
720                                     audio_usecase_t uc_id)
721{
722    struct audio_usecase *uc_to_remove = NULL;
723    struct audio_usecase *list_head = &adev->usecase_list;
724    ALOGV("%s: enter: usecase(%d)", __func__, uc_id);
725    while (list_head->next != NULL) {
726        if (list_head->next->id == uc_id) {
727            uc_to_remove = list_head->next;
728            list_head->next = list_head->next->next;
729            free(uc_to_remove);
730            break;
731        }
732        list_head = list_head->next;
733    }
734    ALOGV("%s: exit", __func__);
735}
736
737static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
738                                                   audio_usecase_t uc_id)
739{
740    struct audio_usecase *uc_info = NULL;
741    struct audio_usecase *list_head = &adev->usecase_list;
742    ALOGV("%s: enter: uc_id(%d)", __func__, uc_id);
743    while (list_head->next != NULL) {
744        list_head = list_head->next;
745        if (list_head->id == uc_id) {
746            uc_info = list_head;
747            break;
748        }
749    }
750    ALOGV("%s: exit: uc_info(%p)", __func__, uc_info);
751    return uc_info;
752}
753
754static int get_num_active_usecases(struct audio_device *adev)
755{
756    int num_uc = 0;
757    struct audio_usecase *list_head = &adev->usecase_list;
758    while (list_head->next != NULL) {
759        num_uc++;
760        list_head = list_head->next;
761    }
762    return num_uc;
763}
764
765static audio_devices_t get_active_out_devices(struct audio_device *adev,
766                                              audio_usecase_t usecase)
767{
768    audio_devices_t devices = 0;
769    struct audio_usecase *list_head = &adev->usecase_list;
770    /* Return the output devices of usecases other than given usecase */
771    while (list_head->next != NULL) {
772        list_head = list_head->next;
773        if (list_head->type == PCM_PLAYBACK && list_head->id != usecase) {
774            devices |= list_head->devices;
775        }
776    }
777    return devices;
778}
779
780static audio_devices_t get_voice_call_out_device(struct audio_device *adev)
781{
782    audio_devices_t devices = 0;
783    struct audio_usecase *list_head = &adev->usecase_list;
784    /* Return the output devices of usecases other than VOICE_CALL usecase */
785    while (list_head->next != NULL) {
786        list_head = list_head->next;
787        if (list_head->id == USECASE_VOICE_CALL) {
788            devices = list_head->devices;
789            break;
790        }
791    }
792    return devices;
793}
794
795static int stop_input_stream(struct stream_in *in)
796{
797    int i, ret = 0;
798    snd_device_t in_snd_device;
799    struct audio_usecase *uc_info;
800    struct audio_device *adev = in->dev;
801
802    adev->active_input = NULL;
803
804    ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
805    uc_info = get_usecase_from_list(adev, in->usecase);
806    if (uc_info == NULL) {
807        ALOGE("%s: Could not find the usecase (%d) in the list",
808              __func__, in->usecase);
809        return -EINVAL;
810    }
811
812    /* 1. Close the PCM device first */
813    if (in->pcm) {
814        pcm_close(in->pcm);
815        in->pcm = NULL;
816    }
817
818    /* 2. Disable stream specific mixer controls */
819    in_snd_device = adev->cur_in_snd_device;
820    disable_audio_route(adev->audio_route, in->usecase, in_snd_device);
821    audio_route_update_mixer(adev->audio_route);
822
823    remove_usecase_from_list(adev, in->usecase);
824
825    /* 3. Disable the tx device */
826    select_devices(adev);
827
828    ALOGV("%s: exit: status(%d)", __func__, ret);
829    return ret;
830}
831
832int start_input_stream(struct stream_in *in)
833{
834    /* 1. Enable output device and stream routing controls */
835    int ret = 0;
836    snd_device_t in_snd_device;
837    struct audio_usecase *uc_info;
838    struct audio_device *adev = in->dev;
839
840    ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
841    adev->active_input = in;
842    in_snd_device = get_input_snd_device(adev);
843    if (in_snd_device == SND_DEVICE_INVALID) {
844        ALOGE("%s: Could not get valid input sound device", __func__);
845        ret = -EINVAL;
846        goto error_config;
847    }
848
849    in->pcm_device_id = get_pcm_device_id(adev->audio_route,
850                                          in->usecase,
851                                          PCM_CAPTURE);
852    if (in->pcm_device_id < 0) {
853        ALOGE("%s: Could not find PCM device id for the usecase(%d)",
854              __func__, in->usecase);
855        ret = -EINVAL;
856        goto error_config;
857    }
858    uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
859    uc_info->id = in->usecase;
860    uc_info->type = PCM_CAPTURE;
861    uc_info->devices = in->device;
862
863    /* 1. Enable the TX device */
864    ret = select_devices(adev);
865    if (ret) {
866        ALOGE("%s: Failed to enable device(0x%x)",
867              __func__, in->device);
868        free(uc_info);
869        goto error_config;
870    }
871    in_snd_device = adev->cur_in_snd_device;
872
873    /* 2. Enable the mixer controls for the audio route */
874    enable_audio_route(adev->audio_route, in->usecase, in_snd_device);
875    audio_route_update_mixer(adev->audio_route);
876
877    /* 3. Add the usecase info to usecase list */
878    add_usecase_to_list(adev, uc_info);
879
880    /* 2. Open the pcm device */
881    ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
882          __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
883    in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
884                           PCM_IN, &in->config);
885    if (in->pcm && !pcm_is_ready(in->pcm)) {
886        ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
887        pcm_close(in->pcm);
888        in->pcm = NULL;
889        ret = -EIO;
890        goto error_open;
891    }
892    ALOGV("%s: exit", __func__);
893    return ret;
894
895error_open:
896    stop_input_stream(in);
897
898error_config:
899    adev->active_input = NULL;
900    ALOGV("%s: exit: status(%d)", __func__, ret);
901
902    return ret;
903}
904
905static int stop_output_stream(struct stream_out *out)
906{
907    int i, ret = 0;
908    snd_device_t out_snd_device;
909    struct audio_usecase *uc_info;
910    struct audio_device *adev = out->dev;
911
912    ALOGV("%s: enter: usecase(%d)", __func__, out->usecase);
913    uc_info = get_usecase_from_list(adev, out->usecase);
914    if (uc_info == NULL) {
915        ALOGE("%s: Could not find the usecase (%d) in the list",
916              __func__, out->usecase);
917        return -EINVAL;
918    }
919
920    /* 1. Close the PCM device first */
921    if (out->pcm) {
922        pcm_close(out->pcm);
923        out->pcm = NULL;
924    }
925
926    /* 2. Get and set stream specific mixer controls */
927    out_snd_device = adev->cur_out_snd_device;
928    disable_audio_route(adev->audio_route, out->usecase, out_snd_device);
929    audio_route_update_mixer(adev->audio_route);
930
931    remove_usecase_from_list(adev, uc_info->id);
932
933    /* 3. Disable the rx device */
934    adev->out_device = get_active_out_devices(adev, out->usecase);
935    adev->out_device |= get_voice_call_out_device(adev);
936    ret = select_devices(adev);
937
938    ALOGV("%s: exit: status(%d)", __func__, ret);
939    return ret;
940}
941
942int start_output_stream(struct stream_out *out)
943{
944    int status;
945    int ret = 0;
946    snd_device_t out_snd_device;
947    struct audio_usecase *uc_info;
948    struct audio_device *adev = out->dev;
949
950    /* 1. Enable output device and stream routing controls */
951    ALOGV("%s: enter: usecase(%d)", __func__, out->usecase);
952    adev->out_device |= out->devices;
953    out_snd_device = get_output_snd_device(adev);
954    if (out_snd_device == SND_DEVICE_INVALID) {
955        ALOGE("%s: Could not get valid output sound device", __func__);
956        /*
957         * TODO: use a single exit point to avoid duplicating code to
958         * reset output device
959         */
960        adev->out_device = get_active_out_devices(adev, out->usecase);
961        adev->out_device |= get_voice_call_out_device(adev);
962        return -EINVAL;
963    }
964
965    out->pcm_device_id = get_pcm_device_id(adev->audio_route,
966                                           out->usecase,
967                                           PCM_PLAYBACK);
968    if (out->pcm_device_id < 0) {
969        ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
970              __func__, out->pcm_device_id, out->usecase);
971        adev->out_device = get_active_out_devices(adev, out->usecase);
972        adev->out_device |= get_voice_call_out_device(adev);
973        return -EINVAL;
974    }
975
976    uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
977    uc_info->id = out->usecase;
978    uc_info->type = PCM_PLAYBACK;
979    uc_info->devices = out->devices;
980
981    ret = select_devices(adev);
982    if (ret) {
983        ALOGE("%s: Failed to enable device(0x%x)",
984              __func__, adev->out_device);
985        adev->out_device = get_active_out_devices(adev, out->usecase);
986        adev->out_device |= get_voice_call_out_device(adev);
987        free(uc_info);
988        return ret;
989    }
990
991    out_snd_device = adev->cur_out_snd_device;
992    enable_audio_route(adev->audio_route, out->usecase, out_snd_device);
993    audio_route_update_mixer(adev->audio_route);
994
995    add_usecase_to_list(adev, uc_info);
996
997    ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
998          __func__, 0, out->pcm_device_id);
999    out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1000                           PCM_OUT, &out->config);
1001    if (out->pcm && !pcm_is_ready(out->pcm)) {
1002        ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1003        pcm_close(out->pcm);
1004        out->pcm = NULL;
1005        status = -EIO;
1006        goto error;
1007    }
1008    ALOGV("%s: exit", __func__);
1009    return 0;
1010error:
1011    stop_output_stream(out);
1012    ALOGE("%s: exit: status(%d)", __func__, status);
1013    return status;
1014}
1015
1016static int stop_voice_call(struct audio_device *adev)
1017{
1018    int i, ret = 0;
1019    snd_device_t out_snd_device;
1020    struct audio_usecase *uc_info;
1021
1022    ALOGV("%s: enter", __func__);
1023    adev->in_call = false;
1024    if (adev->csd_client) {
1025        if (adev->csd_stop_voice == NULL) {
1026            ALOGE("dlsym error for csd_client_disable_device");
1027        } else {
1028            ret = adev->csd_stop_voice();
1029            if (ret < 0) {
1030                ALOGE("%s: csd_client error %d\n", __func__, ret);
1031            }
1032        }
1033    }
1034
1035    /* 1. Close the PCM devices */
1036    if (adev->voice_call_rx) {
1037        pcm_close(adev->voice_call_rx);
1038        adev->voice_call_rx = NULL;
1039    }
1040    if (adev->voice_call_tx) {
1041        pcm_close(adev->voice_call_tx);
1042        adev->voice_call_tx = NULL;
1043    }
1044
1045    uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1046    if (uc_info == NULL) {
1047        ALOGE("%s: Could not find the usecase (%d) in the list",
1048              __func__, USECASE_VOICE_CALL);
1049        return -EINVAL;
1050    }
1051    out_snd_device = adev->cur_out_snd_device;
1052
1053    /* 2. Get and set stream specific mixer controls */
1054    /* ToDo: Status check ?*/
1055    disable_audio_route(adev->audio_route, USECASE_VOICE_CALL, out_snd_device);
1056    audio_route_update_mixer(adev->audio_route);
1057
1058    remove_usecase_from_list(adev, uc_info->id);
1059
1060    /* 3. Disable the rx and tx devices */
1061    ret = select_devices(adev);
1062
1063    ALOGV("%s: exit: status(%d)", __func__, ret);
1064    return ret;
1065}
1066
1067static int start_voice_call(struct audio_device *adev)
1068{
1069    int i, ret = 0;
1070    snd_device_t out_snd_device;
1071    struct audio_usecase *uc_info;
1072    int pcm_dev_rx_id, pcm_dev_tx_id;
1073
1074    ALOGV("%s: enter", __func__);
1075
1076    uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1077    uc_info->id = USECASE_VOICE_CALL;
1078    uc_info->type = VOICE_CALL;
1079    uc_info->devices = adev->out_device;
1080
1081    ret = select_devices(adev);
1082    if (ret) {
1083        free(uc_info);
1084        return ret;
1085    }
1086
1087    out_snd_device = adev->cur_out_snd_device;
1088    enable_audio_route(adev->audio_route, uc_info->id, out_snd_device);
1089    audio_route_update_mixer(adev->audio_route);
1090
1091    add_usecase_to_list(adev, uc_info);
1092
1093    pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1094                                      PCM_PLAYBACK);
1095    pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1096                                      PCM_CAPTURE);
1097
1098    if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1099        ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1100              __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
1101        ret = -EIO;
1102        goto error_start_voice;
1103    }
1104
1105    ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
1106          __func__, SOUND_CARD, pcm_dev_rx_id);
1107    adev->voice_call_rx = pcm_open(SOUND_CARD,
1108                                  pcm_dev_rx_id,
1109                                  PCM_OUT, &pcm_config_voice_call);
1110    if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1111        ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
1112        ret = -EIO;
1113        goto error_start_voice;
1114    }
1115
1116    ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
1117          __func__, SOUND_CARD, pcm_dev_tx_id);
1118    adev->voice_call_tx = pcm_open(SOUND_CARD,
1119                                   pcm_dev_tx_id,
1120                                   PCM_IN, &pcm_config_voice_call);
1121    if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1122        ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
1123        ret = -EIO;
1124        goto error_start_voice;
1125    }
1126    pcm_start(adev->voice_call_rx);
1127    pcm_start(adev->voice_call_tx);
1128
1129    if (adev->csd_client) {
1130        if (adev->csd_start_voice == NULL) {
1131            ALOGE("dlsym error for csd_client_start_voice");
1132            goto error_start_voice;
1133        } else {
1134            ret = adev->csd_start_voice();
1135            if (ret < 0) {
1136                ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1137                goto error_start_voice;
1138            }
1139        }
1140    }
1141
1142    adev->in_call = true;
1143    return 0;
1144
1145error_start_voice:
1146    stop_voice_call(adev);
1147
1148    ALOGV("%s: exit: status(%d)", __func__, ret);
1149    return ret;
1150}
1151
1152static int check_input_parameters(uint32_t sample_rate,
1153                                  audio_format_t format,
1154                                  int channel_count)
1155{
1156    if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1157
1158    if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1159
1160    switch (sample_rate) {
1161    case 8000:
1162    case 11025:
1163    case 12000:
1164    case 16000:
1165    case 22050:
1166    case 24000:
1167    case 32000:
1168    case 44100:
1169    case 48000:
1170        break;
1171    default:
1172        return -EINVAL;
1173    }
1174
1175    return 0;
1176}
1177
1178static size_t get_input_buffer_size(uint32_t sample_rate,
1179                                    audio_format_t format,
1180                                    int channel_count)
1181{
1182    size_t size = 0;
1183
1184    if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1185
1186    if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1187        size = (sample_rate * 20) / 1000;
1188    } else if (sample_rate == 11025 || sample_rate == 12000) {
1189        size = 256;
1190    } else if (sample_rate == 22050 || sample_rate == 24000) {
1191        size = 512;
1192    } else if (sample_rate == 44100 || sample_rate == 48000) {
1193        size = 1024;
1194    }
1195
1196    return size * sizeof(short) * channel_count;
1197}
1198
1199static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1200{
1201    struct stream_out *out = (struct stream_out *)stream;
1202
1203    return out->config.rate;
1204}
1205
1206static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1207{
1208    return -ENOSYS;
1209}
1210
1211static size_t out_get_buffer_size(const struct audio_stream *stream)
1212{
1213    struct stream_out *out = (struct stream_out *)stream;
1214
1215    return out->config.period_size * audio_stream_frame_size(stream);
1216}
1217
1218static uint32_t out_get_channels(const struct audio_stream *stream)
1219{
1220    struct stream_out *out = (struct stream_out *)stream;
1221
1222    return out->channel_mask;
1223}
1224
1225static audio_format_t out_get_format(const struct audio_stream *stream)
1226{
1227    return AUDIO_FORMAT_PCM_16_BIT;
1228}
1229
1230static int out_set_format(struct audio_stream *stream, audio_format_t format)
1231{
1232    return -ENOSYS;
1233}
1234
1235static int out_standby(struct audio_stream *stream)
1236{
1237    struct stream_out *out = (struct stream_out *)stream;
1238    struct audio_device *adev = out->dev;
1239    ALOGV("%s: enter: usecase(%d)", __func__, out->usecase);
1240    pthread_mutex_lock(&out->dev->lock);
1241    pthread_mutex_lock(&out->lock);
1242
1243    if (!out->standby) {
1244        out->standby = true;
1245        stop_output_stream(out);
1246    }
1247    pthread_mutex_unlock(&out->lock);
1248    pthread_mutex_unlock(&out->dev->lock);
1249    ALOGV("%s: exit", __func__);
1250    return 0;
1251}
1252
1253static int out_dump(const struct audio_stream *stream, int fd)
1254{
1255    return 0;
1256}
1257
1258static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1259{
1260    struct stream_out *out = (struct stream_out *)stream;
1261    struct audio_device *adev = out->dev;
1262    struct str_parms *parms;
1263    char value[32];
1264    int ret, val = 0;
1265
1266    ALOGV("%s: enter: usecase(%d) kvpairs: %s",
1267          __func__, out->usecase, kvpairs);
1268    parms = str_parms_create_str(kvpairs);
1269    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1270    if (ret >= 0) {
1271        val = atoi(value);
1272        pthread_mutex_lock(&adev->lock);
1273        pthread_mutex_lock(&out->lock);
1274
1275        if (adev->mode == AUDIO_MODE_IN_CALL && !adev->in_call && (val != 0)) {
1276            adev->out_device = get_active_out_devices(adev, out->usecase) | val;
1277            out->devices = val;
1278            start_voice_call(adev);
1279        } else if (adev->mode != AUDIO_MODE_IN_CALL && adev->in_call) {
1280            if (val != 0) {
1281                adev->out_device = get_active_out_devices(adev, out->usecase) | val;
1282                out->devices = val;
1283            }
1284            stop_voice_call(adev);
1285        } else if ((adev->out_device != (audio_devices_t)val) && (val != 0)) {
1286            if (!out->standby || adev->in_call) {
1287                adev->out_device = get_active_out_devices(adev, out->usecase) | val;
1288                ret = select_devices(adev);
1289            }
1290            out->devices = val;
1291        }
1292
1293        pthread_mutex_unlock(&out->lock);
1294        pthread_mutex_unlock(&adev->lock);
1295    }
1296    str_parms_destroy(parms);
1297    ALOGV("%s: exit: code(%d)", __func__, ret);
1298    return ret;
1299}
1300
1301static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1302{
1303    struct stream_out *out = (struct stream_out *)stream;
1304    struct str_parms *query = str_parms_create_str(keys);
1305    char *str;
1306    char value[256];
1307    struct str_parms *reply = str_parms_create();
1308    size_t i, j;
1309    int ret;
1310    bool first = true;
1311    ALOGV("%s: enter: keys - %s", __func__, keys);
1312    ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1313    if (ret >= 0) {
1314        value[0] = '\0';
1315        i = 0;
1316        while (out->supported_channel_masks[i] != 0) {
1317            for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1318                if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1319                    if (!first) {
1320                        strcat(value, "|");
1321                    }
1322                    strcat(value, out_channels_name_to_enum_table[j].name);
1323                    first = false;
1324                    break;
1325                }
1326            }
1327            i++;
1328        }
1329        str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1330        str = str_parms_to_str(reply);
1331    } else {
1332        str = strdup(keys);
1333    }
1334    str_parms_destroy(query);
1335    str_parms_destroy(reply);
1336    ALOGV("%s: exit: returns - %s", __func__, str);
1337    return str;
1338}
1339
1340static uint32_t out_get_latency(const struct audio_stream_out *stream)
1341{
1342    struct stream_out *out = (struct stream_out *)stream;
1343
1344    return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1345}
1346
1347static int out_set_volume(struct audio_stream_out *stream, float left,
1348                          float right)
1349{
1350    return -ENOSYS;
1351}
1352
1353static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1354                         size_t bytes)
1355{
1356    struct stream_out *out = (struct stream_out *)stream;
1357    struct audio_device *adev = out->dev;
1358    int i, ret = -1;
1359
1360    /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
1361     * on the output stream mutex - e.g. executing select_mode() while holding the hw device
1362     * mutex
1363     */
1364    pthread_mutex_lock(&adev->lock);
1365    pthread_mutex_lock(&out->lock);
1366    if (out->standby) {
1367        ret = start_output_stream(out);
1368        if (ret != 0) {
1369            pthread_mutex_unlock(&adev->lock);
1370            goto exit;
1371        }
1372        out->standby = false;
1373    }
1374    pthread_mutex_unlock(&adev->lock);
1375
1376    if (out->pcm) {
1377        //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1378        ret = pcm_write(out->pcm, (void *)buffer, bytes);
1379    }
1380
1381exit:
1382    pthread_mutex_unlock(&out->lock);
1383
1384    if (ret != 0) {
1385        out_standby(&out->stream.common);
1386        usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1387               out_get_sample_rate(&out->stream.common));
1388    }
1389    return bytes;
1390}
1391
1392static int out_get_render_position(const struct audio_stream_out *stream,
1393                                   uint32_t *dsp_frames)
1394{
1395    return -EINVAL;
1396}
1397
1398static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1399{
1400    return 0;
1401}
1402
1403static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1404{
1405    return 0;
1406}
1407
1408static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1409                                        int64_t *timestamp)
1410{
1411    return -EINVAL;
1412}
1413
1414/** audio_stream_in implementation **/
1415static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1416{
1417    struct stream_in *in = (struct stream_in *)stream;
1418
1419    return in->config.rate;
1420}
1421
1422static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1423{
1424    return -ENOSYS;
1425}
1426
1427static size_t in_get_buffer_size(const struct audio_stream *stream)
1428{
1429    struct stream_in *in = (struct stream_in *)stream;
1430
1431    return in->config.period_size * audio_stream_frame_size(stream);
1432}
1433
1434static uint32_t in_get_channels(const struct audio_stream *stream)
1435{
1436    struct stream_in *in = (struct stream_in *)stream;
1437
1438    return in->channel_mask;
1439}
1440
1441static audio_format_t in_get_format(const struct audio_stream *stream)
1442{
1443    return AUDIO_FORMAT_PCM_16_BIT;
1444}
1445
1446static int in_set_format(struct audio_stream *stream, audio_format_t format)
1447{
1448    return -ENOSYS;
1449}
1450
1451static int in_standby(struct audio_stream *stream)
1452{
1453    struct stream_in *in = (struct stream_in *)stream;
1454    struct audio_device *adev = in->dev;
1455    int status = 0;
1456    ALOGV("%s: enter", __func__);
1457    pthread_mutex_lock(&adev->lock);
1458    pthread_mutex_lock(&in->lock);
1459    if (!in->standby) {
1460        in->standby = true;
1461        status = stop_input_stream(in);
1462    }
1463    pthread_mutex_unlock(&in->lock);
1464    pthread_mutex_unlock(&adev->lock);
1465    ALOGV("%s: exit:  status(%d)", __func__, status);
1466    return status;
1467}
1468
1469static int in_dump(const struct audio_stream *stream, int fd)
1470{
1471    return 0;
1472}
1473
1474static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1475{
1476    struct stream_in *in = (struct stream_in *)stream;
1477    struct audio_device *adev = in->dev;
1478    struct str_parms *parms;
1479    char *str;
1480    char value[32];
1481    int ret, val = 0;
1482
1483    ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
1484    parms = str_parms_create_str(kvpairs);
1485
1486    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1487
1488    pthread_mutex_lock(&adev->lock);
1489    pthread_mutex_lock(&in->lock);
1490    if (ret >= 0) {
1491        val = atoi(value);
1492        /* no audio source uses val == 0 */
1493        if ((in->source != val) && (val != 0)) {
1494            in->source = val;
1495        }
1496    }
1497
1498    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1499    if (ret >= 0) {
1500        val = atoi(value);
1501        if ((in->device != val) && (val != 0)) {
1502            in->device = val;
1503            /* If recording is in progress, change the tx device to new device */
1504            if (!in->standby) {
1505                ret = select_devices(adev);
1506            }
1507        }
1508    }
1509
1510    pthread_mutex_unlock(&in->lock);
1511    pthread_mutex_unlock(&adev->lock);
1512
1513    str_parms_destroy(parms);
1514    ALOGV("%s: exit: status(%d)", __func__, ret);
1515    return ret;
1516}
1517
1518static char* in_get_parameters(const struct audio_stream *stream,
1519                               const char *keys)
1520{
1521    return strdup("");
1522}
1523
1524static int in_set_gain(struct audio_stream_in *stream, float gain)
1525{
1526    return 0;
1527}
1528
1529static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1530                       size_t bytes)
1531{
1532    struct stream_in *in = (struct stream_in *)stream;
1533    struct audio_device *adev = in->dev;
1534    int i, ret = -1;
1535
1536    /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
1537     * on the output stream mutex - e.g. executing select_mode() while holding the hw device
1538     * mutex
1539     */
1540    //ALOGV("%s: buffer(%p) bytes(%d)", __func__, buffer, bytes);
1541    pthread_mutex_lock(&adev->lock);
1542    pthread_mutex_lock(&in->lock);
1543    if (in->standby) {
1544        ret = start_input_stream(in);
1545        if (ret != 0) {
1546            pthread_mutex_unlock(&adev->lock);
1547            goto exit;
1548        }
1549        in->standby = 0;
1550    }
1551    pthread_mutex_unlock(&adev->lock);
1552
1553    if (in->pcm) {
1554        ret = pcm_read(in->pcm, buffer, bytes);
1555    }
1556
1557    /*
1558     * Instead of writing zeroes here, we could trust the hardware
1559     * to always provide zeroes when muted.
1560     */
1561    if (ret == 0 && adev->mic_mute)
1562        memset(buffer, 0, bytes);
1563
1564exit:
1565    pthread_mutex_unlock(&in->lock);
1566
1567    if (ret != 0) {
1568        in_standby(&in->stream.common);
1569        ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1570        usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1571               in_get_sample_rate(&in->stream.common));
1572    }
1573    return bytes;
1574}
1575
1576static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1577{
1578    return 0;
1579}
1580
1581static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1582{
1583    return 0;
1584}
1585
1586static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1587{
1588    return 0;
1589}
1590
1591static int adev_open_output_stream(struct audio_hw_device *dev,
1592                                   audio_io_handle_t handle,
1593                                   audio_devices_t devices,
1594                                   audio_output_flags_t flags,
1595                                   struct audio_config *config,
1596                                   struct audio_stream_out **stream_out)
1597{
1598    struct audio_device *adev = (struct audio_device *)dev;
1599    struct stream_out *out;
1600    int i, ret;
1601
1602    ALOGV("%s: enter: sample_rate(%d) channel_mask(0x%x) devices(0x%x) flags(0x%x)",
1603          __func__, config->sample_rate, config->channel_mask, devices, flags);
1604    *stream_out = NULL;
1605    out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1606
1607    if (devices == AUDIO_DEVICE_NONE)
1608        devices = AUDIO_DEVICE_OUT_SPEAKER;
1609
1610    out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1611    out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1612    out->flags = flags;
1613    out->devices = devices;
1614
1615    /* Init use case and pcm_config */
1616    if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1617        out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1618        out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1619        out->config = pcm_config_hdmi_multi;
1620
1621        pthread_mutex_lock(&adev->lock);
1622        read_hdmi_channel_masks(out);
1623        pthread_mutex_unlock(&adev->lock);
1624
1625        if (config->sample_rate == 0) config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1626        if (config->channel_mask == 0) config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1627        out->channel_mask = config->channel_mask;
1628        out->config.rate = config->sample_rate;
1629        out->config.channels = popcount(out->channel_mask);
1630        out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1631        set_hdmi_channels(adev->mixer, out->config.channels);
1632    } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1633        out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1634        out->config = pcm_config_deep_buffer;
1635    } else {
1636        out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1637        out->config = pcm_config_low_latency;
1638    }
1639
1640    /* Check if this usecase is already existing */
1641    pthread_mutex_lock(&adev->lock);
1642    if (get_usecase_from_list(adev, out->usecase) != NULL) {
1643        ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
1644        free(out);
1645        *stream_out = NULL;
1646        pthread_mutex_unlock(&adev->lock);
1647        return -EEXIST;
1648    }
1649    pthread_mutex_unlock(&adev->lock);
1650
1651    out->stream.common.get_sample_rate = out_get_sample_rate;
1652    out->stream.common.set_sample_rate = out_set_sample_rate;
1653    out->stream.common.get_buffer_size = out_get_buffer_size;
1654    out->stream.common.get_channels = out_get_channels;
1655    out->stream.common.get_format = out_get_format;
1656    out->stream.common.set_format = out_set_format;
1657    out->stream.common.standby = out_standby;
1658    out->stream.common.dump = out_dump;
1659    out->stream.common.set_parameters = out_set_parameters;
1660    out->stream.common.get_parameters = out_get_parameters;
1661    out->stream.common.add_audio_effect = out_add_audio_effect;
1662    out->stream.common.remove_audio_effect = out_remove_audio_effect;
1663    out->stream.get_latency = out_get_latency;
1664    out->stream.set_volume = out_set_volume;
1665    out->stream.write = out_write;
1666    out->stream.get_render_position = out_get_render_position;
1667    out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1668
1669    out->dev = adev;
1670    out->standby = 1;
1671
1672    config->format = out->stream.common.get_format(&out->stream.common);
1673    config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1674    config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1675
1676    *stream_out = &out->stream;
1677    ALOGV("%s: exit", __func__);
1678    return 0;
1679}
1680
1681static void adev_close_output_stream(struct audio_hw_device *dev,
1682                                     struct audio_stream_out *stream)
1683{
1684    ALOGV("%s: enter", __func__);
1685    out_standby(&stream->common);
1686    free(stream);
1687    ALOGV("%s: exit", __func__);
1688}
1689
1690static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1691{
1692    struct audio_device *adev = (struct audio_device *)dev;
1693    struct str_parms *parms;
1694    char *str;
1695    char value[32];
1696    int ret;
1697
1698    ALOGV("%s: enter: %s", __func__, kvpairs);
1699
1700    parms = str_parms_create_str(kvpairs);
1701    ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1702    if (ret >= 0) {
1703        int tty_mode;
1704
1705        if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1706            tty_mode = TTY_MODE_OFF;
1707        else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1708            tty_mode = TTY_MODE_VCO;
1709        else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1710            tty_mode = TTY_MODE_HCO;
1711        else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1712            tty_mode = TTY_MODE_FULL;
1713        else
1714            return -EINVAL;
1715
1716        pthread_mutex_lock(&adev->lock);
1717        if (tty_mode != adev->tty_mode) {
1718            adev->tty_mode = tty_mode;
1719            adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1720            if (adev->in_call)
1721                select_devices(adev);
1722        }
1723        pthread_mutex_unlock(&adev->lock);
1724    }
1725
1726    ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1727    if (ret >= 0) {
1728        /* When set to false, HAL should disable EC and NS
1729         * But it is currently not supported.
1730         */
1731        if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1732            adev->bluetooth_nrec = true;
1733        else
1734            adev->bluetooth_nrec = false;
1735    }
1736
1737    ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1738    if (ret >= 0) {
1739        if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1740            adev->screen_off = false;
1741        else
1742            adev->screen_off = true;
1743    }
1744
1745    str_parms_destroy(parms);
1746    ALOGV("%s: exit with code(%d)", __func__, ret);
1747    return ret;
1748}
1749
1750static char* adev_get_parameters(const struct audio_hw_device *dev,
1751                                 const char *keys)
1752{
1753    /* ToDo: Return requested params */
1754    return strdup("");
1755}
1756
1757static int adev_init_check(const struct audio_hw_device *dev)
1758{
1759    return 0;
1760}
1761
1762static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1763{
1764    struct audio_device *adev = (struct audio_device *)dev;
1765    int vol, err = 0;
1766
1767    pthread_mutex_lock(&adev->lock);
1768    adev->voice_volume = volume;
1769    if (adev->mode == AUDIO_MODE_IN_CALL) {
1770        if (volume < 0.0) {
1771            volume = 0.0;
1772        } else if (volume > 1.0) {
1773            volume = 1.0;
1774        }
1775
1776        vol = lrint(volume * 100.0);
1777
1778        // Voice volume levels from android are mapped to driver volume levels as follows.
1779        // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1780        // So adjust the volume to get the correct volume index in driver
1781        vol = 100 - vol;
1782
1783        if (adev->csd_client) {
1784            if (adev->csd_volume == NULL) {
1785                ALOGE("%s: dlsym error for csd_client_volume", __func__);
1786            } else {
1787                err = adev->csd_volume(vol);
1788                if (err < 0) {
1789                    ALOGE("%s: csd_client error %d", __func__, err);
1790                }
1791            }
1792        } else {
1793            ALOGE("%s: No CSD Client present", __func__);
1794        }
1795    }
1796    pthread_mutex_unlock(&adev->lock);
1797    return err;
1798}
1799
1800static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1801{
1802    return -ENOSYS;
1803}
1804
1805static int adev_get_master_volume(struct audio_hw_device *dev,
1806                                  float *volume)
1807{
1808    return -ENOSYS;
1809}
1810
1811static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1812{
1813    return -ENOSYS;
1814}
1815
1816static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1817{
1818    return -ENOSYS;
1819}
1820
1821static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1822{
1823    struct audio_device *adev = (struct audio_device *)dev;
1824
1825    pthread_mutex_lock(&adev->lock);
1826    if (adev->mode != mode) {
1827        adev->mode = mode;
1828    }
1829    pthread_mutex_unlock(&adev->lock);
1830    return 0;
1831}
1832
1833static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1834{
1835    struct audio_device *adev = (struct audio_device *)dev;
1836    int err = 0;
1837
1838    adev->mic_mute = state;
1839    if (adev->mode == AUDIO_MODE_IN_CALL) {
1840        if (adev->csd_client) {
1841            if (adev->csd_mic_mute == NULL) {
1842                ALOGE("%s: dlsym error for csd_mic_mute", __func__);
1843            } else {
1844                err = adev->csd_mic_mute(state);
1845                if (err < 0) {
1846                    ALOGE("%s: csd_client error %d", __func__, err);
1847                }
1848            }
1849        } else {
1850            ALOGE("%s: No CSD Client present", __func__);
1851        }
1852    }
1853    return err;
1854}
1855
1856static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1857{
1858    struct audio_device *adev = (struct audio_device *)dev;
1859
1860    *state = adev->mic_mute;
1861
1862    return 0;
1863}
1864
1865static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1866                                         const struct audio_config *config)
1867{
1868    size_t size;
1869    int channel_count = popcount(config->channel_mask);
1870
1871    return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1872}
1873
1874static int adev_open_input_stream(struct audio_hw_device *dev,
1875                                  audio_io_handle_t handle,
1876                                  audio_devices_t devices,
1877                                  struct audio_config *config,
1878                                  struct audio_stream_in **stream_in)
1879{
1880    struct audio_device *adev = (struct audio_device *)dev;
1881    struct stream_in *in;
1882    int ret, buffer_size, frame_size;
1883    int channel_count = popcount(config->channel_mask);
1884
1885    ALOGV("%s: enter", __func__);
1886    *stream_in = NULL;
1887    if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1888        return -EINVAL;
1889
1890    in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1891
1892    in->stream.common.get_sample_rate = in_get_sample_rate;
1893    in->stream.common.set_sample_rate = in_set_sample_rate;
1894    in->stream.common.get_buffer_size = in_get_buffer_size;
1895    in->stream.common.get_channels = in_get_channels;
1896    in->stream.common.get_format = in_get_format;
1897    in->stream.common.set_format = in_set_format;
1898    in->stream.common.standby = in_standby;
1899    in->stream.common.dump = in_dump;
1900    in->stream.common.set_parameters = in_set_parameters;
1901    in->stream.common.get_parameters = in_get_parameters;
1902    in->stream.common.add_audio_effect = in_add_audio_effect;
1903    in->stream.common.remove_audio_effect = in_remove_audio_effect;
1904    in->stream.set_gain = in_set_gain;
1905    in->stream.read = in_read;
1906    in->stream.get_input_frames_lost = in_get_input_frames_lost;
1907
1908    in->device = devices;
1909    in->source = AUDIO_SOURCE_DEFAULT;
1910    in->dev = adev;
1911    in->standby = 1;
1912    in->channel_mask = config->channel_mask;
1913
1914    /* Update config params with the requested sample rate and channels */
1915    in->usecase = USECASE_AUDIO_RECORD;
1916    in->config = pcm_config_audio_capture;
1917    in->config.channels = channel_count;
1918    in->config.rate = config->sample_rate;
1919
1920    frame_size = audio_stream_frame_size((struct audio_stream *)in);
1921    buffer_size = get_input_buffer_size(config->sample_rate,
1922                                        config->format,
1923                                        channel_count);
1924    in->config.period_size = buffer_size / frame_size;
1925
1926    *stream_in = &in->stream;
1927    ALOGV("%s: exit", __func__);
1928    return 0;
1929
1930err_open:
1931    free(in);
1932    *stream_in = NULL;
1933    return ret;
1934}
1935
1936static void adev_close_input_stream(struct audio_hw_device *dev,
1937                                    struct audio_stream_in *stream)
1938{
1939    in_standby(&stream->common);
1940    free(stream);
1941
1942    return;
1943}
1944
1945static int adev_dump(const audio_hw_device_t *device, int fd)
1946{
1947    return 0;
1948}
1949
1950static int adev_close(hw_device_t *device)
1951{
1952    struct audio_device *adev = (struct audio_device *)device;
1953    audio_route_free(adev->audio_route);
1954    free(device);
1955    return 0;
1956}
1957
1958static void init_platform_data(struct audio_device *adev)
1959{
1960    char platform[PROPERTY_VALUE_MAX];
1961    char baseband[PROPERTY_VALUE_MAX];
1962    char value[PROPERTY_VALUE_MAX];
1963    int mccmnc;
1964
1965    adev->dualmic_config = DUALMIC_CONFIG_NONE;
1966    adev->fluence_in_voice_call = false;
1967    adev->fluence_in_voice_rec = false;
1968    adev->mic_type_analog = false;
1969
1970    property_get("persist.audio.handset.mic.type",value,"");
1971    if (!strncmp("analog", value, 6))
1972        adev->mic_type_analog = true;
1973
1974    property_get("persist.audio.dualmic.config",value,"");
1975    if (!strncmp("broadside", value, 9)) {
1976        adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
1977        adev->acdb_settings |= DMIC_FLAG;
1978    } else if (!strncmp("endfire", value, 7)) {
1979        adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
1980        adev->acdb_settings |= DMIC_FLAG;
1981    }
1982
1983    if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
1984        property_get("persist.audio.fluence.voicecall",value,"");
1985        if (!strncmp("true", value, 4)) {
1986            adev->fluence_in_voice_call = true;
1987        }
1988
1989        property_get("persist.audio.fluence.voicerec",value,"");
1990        if (!strncmp("true", value, 4)) {
1991            adev->fluence_in_voice_rec = true;
1992        }
1993    }
1994
1995    property_get("gsm.sim.operator.numeric",value,"0");
1996    mccmnc = atoi(value);
1997    ALOGV("%s: tmus mccmnc %d", __func__, mccmnc);
1998    switch(mccmnc) {
1999    /* TMUS MCC(310), MNC(490, 260, 026) */
2000    case 310490:
2001    case 310260:
2002    case 310026:
2003        adev->is_tmus = true;
2004        break;
2005    default:
2006        adev->is_tmus = false;
2007        break;
2008    }
2009
2010    adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2011    if (adev->acdb_handle == NULL) {
2012        ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2013    } else {
2014        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
2015        adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2016                                                    "acdb_loader_deallocate_ACDB");
2017        adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2018                                                    "acdb_loader_send_audio_cal");
2019        adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2020                                                    "acdb_loader_send_voice_cal");
2021        adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2022                                                    "acdb_loader_init_ACDB");
2023        if (adev->acdb_init == NULL)
2024            ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
2025        else
2026            adev->acdb_init();
2027    }
2028
2029    /* If platform is Fusion3, load CSD Client specific symbols
2030     * Voice call is handled by MDM and apps processor talks to
2031     * MDM through CSD Client
2032     */
2033    property_get("ro.board.platform", platform, "");
2034    property_get("ro.baseband", baseband, "");
2035    if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2036        adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2037        if (adev->csd_client == NULL)
2038            ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2039    }
2040
2041    if (adev->csd_client) {
2042        ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
2043        adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2044                                                    "csd_client_deinit");
2045        adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2046                                                    "csd_client_disable_device");
2047        adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2048                                                    "csd_client_enable_device");
2049        adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2050                                                    "csd_client_start_voice");
2051        adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2052                                                    "csd_client_stop_voice");
2053        adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2054                                                    "csd_client_volume");
2055        adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2056                                                    "csd_client_mic_mute");
2057        adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2058                                                    "csd_client_init");
2059
2060        if (adev->csd_client_init == NULL) {
2061            ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
2062        } else {
2063            adev->csd_client_init();
2064        }
2065    }
2066}
2067
2068static int adev_open(const hw_module_t *module, const char *name,
2069                     hw_device_t **device)
2070{
2071    struct audio_device *adev;
2072    int ret;
2073
2074    ALOGV("%s: enter", __func__);
2075    if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2076
2077    adev = calloc(1, sizeof(struct audio_device));
2078
2079    adev->mixer = mixer_open(MIXER_CARD);
2080    if (!adev->mixer) {
2081        ALOGE("Unable to open the mixer, aborting.");
2082        return -ENOSYS;
2083    }
2084
2085    adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2086    if (!adev->audio_route) {
2087        free(adev);
2088        ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2089        *device = NULL;
2090        return -EINVAL;
2091    }
2092
2093    adev->device.common.tag = HARDWARE_DEVICE_TAG;
2094    adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2095    adev->device.common.module = (struct hw_module_t *)module;
2096    adev->device.common.close = adev_close;
2097
2098    adev->device.init_check = adev_init_check;
2099    adev->device.set_voice_volume = adev_set_voice_volume;
2100    adev->device.set_master_volume = adev_set_master_volume;
2101    adev->device.get_master_volume = adev_get_master_volume;
2102    adev->device.set_master_mute = adev_set_master_mute;
2103    adev->device.get_master_mute = adev_get_master_mute;
2104    adev->device.set_mode = adev_set_mode;
2105    adev->device.set_mic_mute = adev_set_mic_mute;
2106    adev->device.get_mic_mute = adev_get_mic_mute;
2107    adev->device.set_parameters = adev_set_parameters;
2108    adev->device.get_parameters = adev_get_parameters;
2109    adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2110    adev->device.open_output_stream = adev_open_output_stream;
2111    adev->device.close_output_stream = adev_close_output_stream;
2112    adev->device.open_input_stream = adev_open_input_stream;
2113    adev->device.close_input_stream = adev_close_input_stream;
2114    adev->device.dump = adev_dump;
2115
2116    /* Set the default route before the PCM stream is opened */
2117    pthread_mutex_lock(&adev->lock);
2118    adev->mode = AUDIO_MODE_NORMAL;
2119    adev->active_input = NULL;
2120    adev->out_device = AUDIO_DEVICE_NONE;
2121    adev->voice_call_rx = NULL;
2122    adev->voice_call_tx = NULL;
2123    adev->voice_volume = 1.0f;
2124    adev->tty_mode = TTY_MODE_OFF;
2125    adev->bluetooth_nrec = true;
2126    adev->cur_out_snd_device = 0;
2127    adev->cur_in_snd_device = 0;
2128    adev->out_snd_device_active = false;
2129    adev->in_snd_device_active = false;
2130    adev->usecase_list.next = NULL;
2131    adev->usecase_list.id = USECASE_INVALID;
2132    adev->in_call = false;
2133    adev->acdb_settings = TTY_MODE_OFF;
2134    pthread_mutex_unlock(&adev->lock);
2135
2136    /* Loads platform specific libraries dynamically */
2137    init_platform_data(adev);
2138
2139    *device = &adev->device.common;
2140
2141    ALOGV("%s: exit", __func__);
2142    return 0;
2143}
2144
2145static struct hw_module_methods_t hal_module_methods = {
2146    .open = adev_open,
2147};
2148
2149struct audio_module HAL_MODULE_INFO_SYM = {
2150    .common = {
2151        .tag = HARDWARE_MODULE_TAG,
2152        .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2153        .hal_api_version = HARDWARE_HAL_API_VERSION,
2154        .id = AUDIO_HARDWARE_MODULE_ID,
2155        .name = "QCOM Audio HAL",
2156        .author = "Code Aurora Forum",
2157        .methods = &hal_module_methods,
2158    },
2159};
2160