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