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