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