1 /*
2  * Copyright (C) 2013-2017 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 #define LOG_TAG "msm8974_platform"
17 /*#define LOG_NDEBUG 0*/
18 #define LOG_NDDEBUG 0
19 
20 #include <stdlib.h>
21 #include <dlfcn.h>
22 #include <pthread.h>
23 #include <unistd.h>
24 #include <log/log.h>
25 #include <cutils/str_parms.h>
26 #include <cutils/properties.h>
27 #include <audio_hw.h>
28 #include <platform_api.h>
29 #include "acdb.h"
30 #include "platform.h"
31 #include "audio_extn.h"
32 #include <linux/msm_audio.h>
33 #if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
34 #include <sound/devdep_params.h>
35 #endif
36 
37 #include "maxxaudio.h"
38 #include <resolv.h>
39 
40 #define MIXER_XML_DEFAULT_PATH "mixer_paths.xml"
41 #define MIXER_XML_BASE_STRING "mixer_paths"
42 #define TOMTOM_8226_SND_CARD_NAME "msm8226-tomtom-snd-card"
43 #define TOMTOM_MIXER_FILE_SUFFIX "wcd9330"
44 
45 #define LIB_ACDB_LOADER "libacdbloader.so"
46 #define AUDIO_DATA_BLOCK_MIXER_CTL "HDMI EDID"
47 #define CVD_VERSION_MIXER_CTL "CVD Version"
48 
49 #define min(a, b) ((a) < (b) ? (a) : (b))
50 
51 /*
52  * This file will have a maximum of 38 bytes:
53  *
54  * 4 bytes: number of audio blocks
55  * 4 bytes: total length of Short Audio Descriptor (SAD) blocks
56  * Maximum 10 * 3 bytes: SAD blocks
57  */
58 #define MAX_SAD_BLOCKS      10
59 #define SAD_BLOCK_SIZE      3
60 
61 #define MAX_CVD_VERSION_STRING_SIZE    100
62 
63 /* EDID format ID for LPCM audio */
64 #define EDID_FORMAT_LPCM    1
65 
66 #define MAX_SND_CARD_NAME_LEN 31
67 
68 #define DEFAULT_APP_TYPE_RX_PATH  69936
69 #define DEFAULT_APP_TYPE_TX_PATH  69938
70 #define DEFAULT_RX_BACKEND "SLIMBUS_0_RX"
71 
72 #define TOSTRING_(x) #x
73 #define TOSTRING(x) TOSTRING_(x)
74 
75 #define GET_IN_DEVICE_INDEX(SND_DEVICE) ((SND_DEVICE) - (SND_DEVICE_IN_BEGIN))
76 
77 struct audio_block_header
78 {
79     int reserved;
80     int length;
81 };
82 
83 enum {
84     CAL_MODE_SEND           = 0x1,
85     CAL_MODE_PERSIST        = 0x2,
86     CAL_MODE_RTAC           = 0x4
87 };
88 
89 #define PLATFORM_CONFIG_KEY_OPERATOR_INFO "operator_info"
90 
91 struct operator_info {
92     struct listnode list;
93     char *name;
94     char *mccmnc;
95 };
96 
97 struct operator_specific_device {
98     struct listnode list;
99     char *operator;
100     char *mixer_path;
101     int acdb_id;
102 };
103 
104 struct external_specific_device {
105     struct listnode list;
106     char *usbid;
107     int acdb_id;
108 };
109 
110 #define BE_DAI_NAME_MAX_LENGTH 24
111 struct be_dai_name_struct {
112     unsigned int be_id;
113     char be_name[BE_DAI_NAME_MAX_LENGTH];
114 };
115 
116 struct snd_device_to_mic_map {
117     struct mic_info microphones[AUDIO_MICROPHONE_MAX_COUNT];
118     size_t mic_count;
119 };
120 
121 static struct listnode operator_info_list;
122 static struct listnode *operator_specific_device_table[SND_DEVICE_MAX];
123 static struct listnode *external_specific_device_table[SND_DEVICE_MAX];
124 
125 #define AUDIO_PARAMETER_KEY_AUD_CALDATA "cal_data"
126 
127 typedef struct acdb_audio_cal_cfg {
128     uint32_t             persist;
129     uint32_t             snd_dev_id;
130     audio_devices_t      dev_id;
131     int32_t              acdb_dev_id;
132     uint32_t             app_type;
133     uint32_t             topo_id;
134     uint32_t             sampling_rate;
135     uint32_t             cal_type;
136     uint32_t             module_id;
137 #ifdef PLATFORM_SM8150
138     uint16_t             instance_id;
139     uint16_t             reserved;
140 #endif
141     uint32_t             param_id;
142 } acdb_audio_cal_cfg_t;
143 
144 /* Audio calibration related functions */
145 typedef void (*acdb_send_audio_cal_v3_t)(int, int, int, int, int);
146 
147 struct platform_data {
148     struct audio_device *adev;
149     bool fluence_in_spkr_mode;
150     bool fluence_in_voice_call;
151     bool fluence_in_voice_comm;
152     bool fluence_in_voice_rec;
153     /* 0 = no fluence, 1 = fluence, 2 = fluence pro */
154     int  fluence_type;
155     int  source_mic_type;
156     bool speaker_lr_swap;
157 
158     void *acdb_handle;
159 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
160     acdb_init_v2_cvd_t acdb_init;
161 #elif defined (PLATFORM_MSM8084)
162     acdb_init_v2_t acdb_init;
163 #else
164     acdb_init_t acdb_init;
165 #endif
166     acdb_deallocate_t          acdb_deallocate;
167     acdb_send_audio_cal_t      acdb_send_audio_cal;
168     acdb_send_audio_cal_v3_t   acdb_send_audio_cal_v3;
169     acdb_set_audio_cal_t       acdb_set_audio_cal;
170     acdb_send_voice_cal_t      acdb_send_voice_cal;
171     acdb_reload_vocvoltable_t  acdb_reload_vocvoltable;
172     acdb_send_gain_dep_cal_t   acdb_send_gain_dep_cal;
173     acdb_send_custom_top_t     acdb_send_custom_top;
174     bool acdb_initialized;
175 
176     struct csd_data *csd;
177     char ec_ref_mixer_path[64];
178 
179     codec_backend_cfg_t current_backend_cfg[MAX_CODEC_BACKENDS];
180     char *snd_card_name;
181     int max_vol_index;
182     int max_mic_count;
183 
184     void *hw_info;
185 
186     uint32_t declared_mic_count;
187     struct audio_microphone_characteristic_t microphones[AUDIO_MICROPHONE_MAX_COUNT];
188     struct snd_device_to_mic_map mic_map[SND_DEVICE_MAX];
189 };
190 
191 static int pcm_device_table[AUDIO_USECASE_MAX][2] = {
192     [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {DEEP_BUFFER_PCM_DEVICE,
193                                             DEEP_BUFFER_PCM_DEVICE},
194     [USECASE_AUDIO_PLAYBACK_WITH_HAPTICS] = {AUDIO_HAPTICS_PCM_DEVICE,
195                                              AUDIO_HAPTICS_PCM_DEVICE},
196     [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
197                                             LOWLATENCY_PCM_DEVICE},
198     [USECASE_AUDIO_PLAYBACK_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
199                                          MULTIMEDIA2_PCM_DEVICE},
200     [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {PLAYBACK_OFFLOAD_DEVICE,
201                                         PLAYBACK_OFFLOAD_DEVICE},
202     [USECASE_AUDIO_PLAYBACK_TTS] = {MULTIMEDIA2_PCM_DEVICE,
203                                         MULTIMEDIA2_PCM_DEVICE},
204     [USECASE_AUDIO_PLAYBACK_ULL] = {MULTIMEDIA3_PCM_DEVICE,
205                                     MULTIMEDIA3_PCM_DEVICE},
206     [USECASE_AUDIO_PLAYBACK_MMAP] = {MMAP_PLAYBACK_PCM_DEVICE,
207             MMAP_PLAYBACK_PCM_DEVICE},
208 
209     [USECASE_AUDIO_RECORD] = {AUDIO_RECORD_PCM_DEVICE,
210                               AUDIO_RECORD_PCM_DEVICE},
211     [USECASE_AUDIO_RECORD_LOW_LATENCY] = {LOWLATENCY_PCM_DEVICE,
212                                           LOWLATENCY_PCM_DEVICE},
213 
214     [USECASE_AUDIO_RECORD_MMAP] = {MMAP_RECORD_PCM_DEVICE,
215             MMAP_RECORD_PCM_DEVICE},
216     [USECASE_AUDIO_RECORD_HIFI] = {MULTIMEDIA2_PCM_DEVICE,
217                                    MULTIMEDIA2_PCM_DEVICE},
218 
219     [USECASE_VOICE_CALL] = {VOICE_CALL_PCM_DEVICE,
220                             VOICE_CALL_PCM_DEVICE},
221     [USECASE_VOICE2_CALL] = {VOICE2_CALL_PCM_DEVICE, VOICE2_CALL_PCM_DEVICE},
222     [USECASE_VOLTE_CALL] = {VOLTE_CALL_PCM_DEVICE, VOLTE_CALL_PCM_DEVICE},
223     [USECASE_QCHAT_CALL] = {QCHAT_CALL_PCM_DEVICE, QCHAT_CALL_PCM_DEVICE},
224     [USECASE_VOWLAN_CALL] = {VOWLAN_CALL_PCM_DEVICE, VOWLAN_CALL_PCM_DEVICE},
225     [USECASE_VOICEMMODE1_CALL] = {VOICEMMODE1_CALL_PCM_DEVICE,
226                                   VOICEMMODE1_CALL_PCM_DEVICE},
227     [USECASE_VOICEMMODE2_CALL] = {VOICEMMODE2_CALL_PCM_DEVICE,
228                                   VOICEMMODE2_CALL_PCM_DEVICE},
229 
230     [USECASE_INCALL_REC_UPLINK] = {AUDIO_RECORD_PCM_DEVICE,
231                                    AUDIO_RECORD_PCM_DEVICE},
232     [USECASE_INCALL_REC_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
233                                      AUDIO_RECORD_PCM_DEVICE},
234     [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = {AUDIO_RECORD_PCM_DEVICE,
235                                                 AUDIO_RECORD_PCM_DEVICE},
236     [USECASE_AUDIO_HFP_SCO] = {HFP_PCM_RX, HFP_SCO_RX},
237 
238     [USECASE_AUDIO_SPKR_CALIB_RX] = {SPKR_PROT_CALIB_RX_PCM_DEVICE, -1},
239     [USECASE_AUDIO_SPKR_CALIB_TX] = {-1, SPKR_PROT_CALIB_TX_PCM_DEVICE},
240 
241     [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
242                                           AFE_PROXY_RECORD_PCM_DEVICE},
243     [USECASE_AUDIO_RECORD_AFE_PROXY] = {AFE_PROXY_PLAYBACK_PCM_DEVICE,
244                                         AFE_PROXY_RECORD_PCM_DEVICE},
245     [USECASE_AUDIO_DSM_FEEDBACK] = {QUAT_MI2S_PCM_DEVICE, QUAT_MI2S_PCM_DEVICE},
246 
247     [USECASE_AUDIO_PLAYBACK_VOIP] = {AUDIO_PLAYBACK_VOIP_PCM_DEVICE,
248                                      AUDIO_PLAYBACK_VOIP_PCM_DEVICE},
249     [USECASE_AUDIO_RECORD_VOIP] = {AUDIO_RECORD_VOIP_PCM_DEVICE,
250                                    AUDIO_RECORD_VOIP_PCM_DEVICE},
251 
252     [USECASE_INCALL_MUSIC_UPLINK] = {INCALL_MUSIC_UPLINK_PCM_DEVICE,
253                                      INCALL_MUSIC_UPLINK_PCM_DEVICE},
254     [USECASE_INCALL_MUSIC_UPLINK2] = {INCALL_MUSIC_UPLINK2_PCM_DEVICE,
255                                      INCALL_MUSIC_UPLINK2_PCM_DEVICE},
256 };
257 
258 /* Array to store sound devices */
259 static const char * const device_table[SND_DEVICE_MAX] = {
260     [SND_DEVICE_NONE] = "none",
261     /* Playback sound devices */
262     [SND_DEVICE_OUT_HANDSET] = "handset",
263     [SND_DEVICE_OUT_SPEAKER] = "speaker",
264     [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
265     [SND_DEVICE_OUT_SPEAKER_SAFE] = "speaker-safe",
266     [SND_DEVICE_OUT_HEADPHONES] = "headphones",
267     [SND_DEVICE_OUT_LINE] = "line",
268     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
269     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = "speaker-safe-and-headphones",
270     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = "speaker-and-line",
271     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = "speaker-safe-and-line",
272     [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset",
273     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = "voice-hac-handset",
274     [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
275     [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
276     [SND_DEVICE_OUT_VOICE_HEADSET] = "voice-headphones",
277     [SND_DEVICE_OUT_VOICE_LINE] = "voice-line",
278     [SND_DEVICE_OUT_HDMI] = "hdmi",
279     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
280     [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
281     [SND_DEVICE_OUT_BT_SCO_WB] = "bt-sco-headset-wb",
282     [SND_DEVICE_OUT_BT_A2DP] = "bt-a2dp",
283     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = "speaker-and-bt-a2dp",
284     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = "speaker-safe-and-bt-a2dp",
285     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
286     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
287     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
288     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
289     [SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = "voice-tty-full-usb",
290     [SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = "voice-tty-vco-usb",
291     [SND_DEVICE_OUT_VOICE_TX] = "voice-tx",
292     [SND_DEVICE_OUT_VOICE_MUSIC_TX] = "voice-music-tx",
293     [SND_DEVICE_OUT_USB_HEADSET] = "usb-headset",
294     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = "usb-headset",
295     [SND_DEVICE_OUT_USB_HEADPHONES] = "usb-headphones",
296     [SND_DEVICE_OUT_USB_HEADSET_SPEC] = "usb-headset",
297     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = "usb-headphones",
298     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = "speaker-and-usb-headphones",
299     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = "speaker-safe-and-usb-headphones",
300     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = "speaker-protected",
301     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = "voice-speaker-protected",
302     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = "voice-speaker-hfp",
303     [SND_DEVICE_OUT_SPEAKER_AND_BT_SCO] = "speaker-and-bt-sco",
304     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = "speaker-safe-and-bt-sco",
305     [SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB] = "speaker-and-bt-sco-wb",
306     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = "speaker-safe-and-bt-sco-wb",
307     [SND_DEVICE_OUT_VOICE_HEARING_AID] = "hearing-aid",
308 
309     /* Capture sound devices */
310     [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
311     [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
312     [SND_DEVICE_IN_HANDSET_MIC_NS] = "handset-mic",
313     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = "handset-mic",
314     [SND_DEVICE_IN_HANDSET_DMIC] = "dmic-endfire",
315     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = "dmic-endfire",
316     [SND_DEVICE_IN_HANDSET_DMIC_NS] = "dmic-endfire",
317     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = "dmic-endfire",
318     [SND_DEVICE_IN_HANDSET_DMIC_STEREO] = "dmic-endfire",
319 
320     [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
321     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "speaker-mic",
322     [SND_DEVICE_IN_SPEAKER_MIC_NS] = "speaker-mic",
323     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = "speaker-mic",
324     [SND_DEVICE_IN_SPEAKER_DMIC] = "speaker-dmic-endfire",
325     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = "speaker-dmic-endfire",
326     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = "speaker-dmic-endfire",
327     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = "speaker-dmic-endfire",
328     [SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = "speaker-dmic-endfire",
329 
330     [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
331     [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
332 
333     [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
334     [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
335     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = "bt-sco-mic",
336     [SND_DEVICE_IN_BT_SCO_MIC_WB] = "bt-sco-mic-wb",
337     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = "bt-sco-mic-wb",
338     [SND_DEVICE_IN_CAMCORDER_LANDSCAPE] = "camcorder-mic",
339 
340     [SND_DEVICE_IN_VOICE_DMIC] = "voice-dmic-ef",
341     [SND_DEVICE_IN_VOICE_DMIC_TMUS] = "voice-dmic-ef-tmus",
342     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
343     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = "voice-speaker-dmic-ef",
344     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = "voice-speaker-mic-hfp",
345     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
346     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
347     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
348     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
349     [SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = "voice-tty-full-usb-mic",
350     [SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = "voice-tty-hco-usb-mic",
351 
352     [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
353     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = "voice-rec-mic",
354     [SND_DEVICE_IN_VOICE_REC_MIC_AEC] = "voice-rec-mic",
355     [SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = "voice-rec-mic",
356     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = "voice-rec-dmic-ef",
357     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = "voice-rec-dmic-ef-fluence",
358     [SND_DEVICE_IN_USB_HEADSET_MIC] = "usb-headset-mic",
359     [SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] ="usb-headset-mic",
360     [SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = "usb-headset-mic",
361     [SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = "usb-headset-mic",
362     [SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = "usb-headset-mic",
363     [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "headset-mic",
364 
365     [SND_DEVICE_IN_UNPROCESSED_MIC] = "unprocessed-mic",
366     [SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = "unprocessed-stereo-mic",
367     [SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = "unprocessed-three-mic",
368     [SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = "unprocessed-quad-mic",
369     [SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = "unprocessed-headset-mic",
370 
371     [SND_DEVICE_IN_VOICE_RX] = "voice-rx",
372 
373     [SND_DEVICE_IN_THREE_MIC] = "three-mic",
374     [SND_DEVICE_IN_QUAD_MIC] = "quad-mic",
375     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = "vi-feedback",
376     [SND_DEVICE_IN_HANDSET_TMIC] = "three-mic",
377     [SND_DEVICE_IN_HANDSET_QMIC] = "quad-mic",
378     [SND_DEVICE_IN_HANDSET_TMIC_AEC] = "three-mic",
379     [SND_DEVICE_IN_HANDSET_QMIC_AEC] = "quad-mic",
380     [SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE] = "camcorder-mic",
381     [SND_DEVICE_IN_CAMCORDER_PORTRAIT] = "camcorder-mic",
382     [SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE] = "camcorder-mic",
383     [SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE] = "camcorder-mic",
384     [SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT] = "camcorder-mic",
385     [SND_DEVICE_IN_SPEAKER_QMIC_NS] = "quad-mic",
386     [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = "quad-mic",
387     [SND_DEVICE_IN_VOICE_HEARING_AID] = "hearing-aid-mic",
388 };
389 
390 static struct audio_effect_config \
391                   effect_config_table[GET_IN_DEVICE_INDEX(SND_DEVICE_MAX)][EFFECT_COUNT] = {
392     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)][EFFECT_AEC] = \
393                                       {TX_VOICE_FLUENCE_PROV2, 0x0, 0x10EAF, 0x01},
394     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)][EFFECT_NS] = \
395                                       {TX_VOICE_FLUENCE_PROV2,  0x0, 0x10EAF, 0x02},
396     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)][EFFECT_AEC] = \
397                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x01},
398     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)][EFFECT_NS] = \
399                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x02},
400     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)][EFFECT_AEC] = \
401                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x01},
402     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)][EFFECT_NS] = \
403                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x02},
404     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)][EFFECT_AEC] = \
405                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x01},
406     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)][EFFECT_NS] = \
407                                       {TX_VOICE_FV5ECNS_DM, 0x0, 0x10EAF, 0x02},
408     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)][EFFECT_AEC] = \
409                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x01},
410     [GET_IN_DEVICE_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)][EFFECT_NS] = \
411                                       {TX_VOICE_FV5ECNS_SM, 0x0, 0x10EAF, 0x02},
412 };
413 
414 /* ACDB IDs (audio DSP path configuration IDs) for each sound device */
415 static int acdb_device_table[SND_DEVICE_MAX] = {
416     [SND_DEVICE_NONE] = -1,
417     [SND_DEVICE_OUT_HANDSET] = 7,
418     [SND_DEVICE_OUT_SPEAKER] = 15,
419     [SND_DEVICE_OUT_SPEAKER_REVERSE] = 15,
420     [SND_DEVICE_OUT_SPEAKER_SAFE] = 15,
421     [SND_DEVICE_OUT_HEADPHONES] = 10,
422     [SND_DEVICE_OUT_LINE] = 77,
423     [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
424     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = 10,
425     [SND_DEVICE_OUT_SPEAKER_AND_LINE] = 77,
426     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = 77,
427     [SND_DEVICE_OUT_VOICE_HANDSET] = ACDB_ID_VOICE_HANDSET,
428     [SND_DEVICE_OUT_VOICE_SPEAKER] = ACDB_ID_VOICE_SPEAKER,
429     [SND_DEVICE_OUT_VOICE_HAC_HANDSET] = 53,
430     [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
431     [SND_DEVICE_OUT_VOICE_HEADSET] = 10,
432     [SND_DEVICE_OUT_VOICE_LINE] = 77,
433     [SND_DEVICE_OUT_HDMI] = 18,
434     [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 15,
435     [SND_DEVICE_OUT_BT_SCO] = 22,
436     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = 14,
437     [SND_DEVICE_OUT_BT_SCO_WB] = 39,
438     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = 14,
439     [SND_DEVICE_OUT_BT_A2DP] = 20,
440     [SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = 14,
441     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = 14,
442     [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = ACDB_ID_VOICE_HANDSET_TMUS,
443     [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
444     [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
445     [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
446     [SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = 17,
447     [SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = 17,
448     [SND_DEVICE_OUT_VOICE_TX] = 45,
449     [SND_DEVICE_OUT_VOICE_MUSIC_TX] = 3,
450     [SND_DEVICE_OUT_USB_HEADSET] = 45,
451     [SND_DEVICE_OUT_VOICE_USB_HEADSET] = 45,
452     [SND_DEVICE_OUT_USB_HEADPHONES] = 45,
453     [SND_DEVICE_OUT_USB_HEADSET_SPEC] = 45,
454     [SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = 45,
455     [SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = 14,
456     [SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = 14,
457     [SND_DEVICE_OUT_SPEAKER_PROTECTED] = 124,
458     [SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = 101,
459     [SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = ACDB_ID_VOICE_SPEAKER,
460     [SND_DEVICE_OUT_VOICE_HEARING_AID] = 45,
461 
462     [SND_DEVICE_IN_HANDSET_MIC] = 4,
463     [SND_DEVICE_IN_HANDSET_MIC_AEC] = 106,
464     [SND_DEVICE_IN_HANDSET_MIC_NS] = 107,
465     [SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = 108,
466     [SND_DEVICE_IN_HANDSET_DMIC] = 41,
467     [SND_DEVICE_IN_HANDSET_DMIC_AEC] = 109,
468     [SND_DEVICE_IN_HANDSET_DMIC_NS] = 110,
469     [SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = 111,
470     [SND_DEVICE_IN_HANDSET_DMIC_STEREO] = 34,
471 
472     [SND_DEVICE_IN_SPEAKER_MIC] = 11,
473     [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 112,
474     [SND_DEVICE_IN_SPEAKER_MIC_NS] = 113,
475     [SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = 114,
476     [SND_DEVICE_IN_SPEAKER_DMIC] = 43,
477     [SND_DEVICE_IN_SPEAKER_DMIC_AEC] = 115,
478     [SND_DEVICE_IN_SPEAKER_DMIC_NS] = 116,
479     [SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = 117,
480     [SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = 35,
481 
482     [SND_DEVICE_IN_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
483     [SND_DEVICE_IN_HEADSET_MIC_AEC] = ACDB_ID_HEADSET_MIC_AEC,
484 
485     [SND_DEVICE_IN_HDMI_MIC] = 4,
486     [SND_DEVICE_IN_BT_SCO_MIC] = 21,
487     [SND_DEVICE_IN_BT_SCO_MIC_NREC] = 21,
488     [SND_DEVICE_IN_BT_SCO_MIC_WB] = 38,
489     [SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = 38,
490     [SND_DEVICE_IN_CAMCORDER_LANDSCAPE] = 61,
491 
492     [SND_DEVICE_IN_VOICE_DMIC] = 41,
493     [SND_DEVICE_IN_VOICE_DMIC_TMUS] = ACDB_ID_VOICE_DMIC_EF_TMUS,
494     [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
495     [SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = 11,
496     [SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = 43,
497     [SND_DEVICE_IN_VOICE_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
498     [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
499     [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
500     [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
501     [SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = 16,
502     [SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = 16,
503 
504     [SND_DEVICE_IN_VOICE_REC_MIC] = ACDB_ID_VOICE_REC_MIC,
505     [SND_DEVICE_IN_VOICE_REC_MIC_NS] = 113,
506     [SND_DEVICE_IN_VOICE_REC_MIC_AEC] = 112,
507     [SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = 114,
508     [SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = 35,
509     [SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = 43,
510     [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
511 
512     [SND_DEVICE_IN_UNPROCESSED_MIC] = ACDB_ID_VOICE_REC_MIC,
513     [SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = ACDB_ID_HEADSET_MIC_AEC,
514     [SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = 35,
515     [SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = 125,
516     [SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = 125,
517 
518     [SND_DEVICE_IN_VOICE_RX] = 44,
519     [SND_DEVICE_IN_USB_HEADSET_MIC] = 44,
520     [SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = 44,
521     [SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = 44,
522     [SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = 44,
523     [SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = 44,
524     [SND_DEVICE_IN_THREE_MIC] = 46,
525     [SND_DEVICE_IN_QUAD_MIC] = 46,
526     [SND_DEVICE_IN_CAPTURE_VI_FEEDBACK] = 102,
527     [SND_DEVICE_IN_HANDSET_TMIC] = 125,
528     [SND_DEVICE_IN_HANDSET_QMIC] = 125,
529     [SND_DEVICE_IN_HANDSET_TMIC_AEC] = 125, /* override this for new target to 140 */
530     [SND_DEVICE_IN_HANDSET_QMIC_AEC] = 125, /* override this for new target to 140 */
531     [SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE] = 61,
532     [SND_DEVICE_IN_CAMCORDER_PORTRAIT] = 61,
533     [SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE] = 61,
534     [SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE] = 61,
535     [SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT] = 61,
536     [SND_DEVICE_IN_SPEAKER_QMIC_NS] = 129,
537     [SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = 129,
538     [SND_DEVICE_IN_VOICE_HEARING_AID] = 44,
539 };
540 
541 // Platform specific backend bit width table
542 static int backend_bit_width_table[SND_DEVICE_MAX] = {0};
543 
544 struct name_to_index {
545     char name[100];
546     unsigned int index;
547 };
548 
549 #define TO_NAME_INDEX(X)   #X, X
550 
551 /* Used to get index from parsed string */
552 static const struct name_to_index snd_device_name_index[SND_DEVICE_MAX] = {
553     /* out */
554     {TO_NAME_INDEX(SND_DEVICE_OUT_HANDSET)},
555     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER)},
556     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_REVERSE)},
557     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE)},
558     {TO_NAME_INDEX(SND_DEVICE_OUT_HEADPHONES)},
559     {TO_NAME_INDEX(SND_DEVICE_OUT_LINE)},
560     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES)},
561     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES)},
562     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_LINE)},
563     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE)},
564     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET)},
565     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER)},
566     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_HFP)},
567     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADPHONES)},
568     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEADSET)},
569     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_LINE)},
570     {TO_NAME_INDEX(SND_DEVICE_OUT_HDMI)},
571     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_HDMI)},
572     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO)},
573     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO)},
574     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_SCO_WB)},
575     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB)},
576     {TO_NAME_INDEX(SND_DEVICE_OUT_BT_A2DP)},
577     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP)},
578     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)},
579     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HANDSET_TMUS)},
580     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HAC_HANDSET)},
581     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES)},
582     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES)},
583     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET)},
584     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_SCO)},
585     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB)},
586     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_FULL_USB)},
587     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_TTY_VCO_USB)},
588     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET)},
589     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADSET)},
590     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADPHONES)},
591     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_USB_HEADPHONES)},
592     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET)},
593     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET)},
594     {TO_NAME_INDEX(SND_DEVICE_OUT_SPEAKER_PROTECTED)},
595     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED)},
596     {TO_NAME_INDEX(SND_DEVICE_OUT_USB_HEADSET_SPEC)},
597     {TO_NAME_INDEX(SND_DEVICE_OUT_VOICE_HEARING_AID)},
598 
599     /* in */
600     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC)},
601     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC)},
602     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_NS)},
603     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_MIC_AEC_NS)},
604     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC)},
605     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC)},
606     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_NS)},
607     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_AEC_NS)},
608     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_DMIC_STEREO)},
609 
610     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC)},
611     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC)},
612     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_NS)},
613     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_MIC_AEC_NS)},
614     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC)},
615     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC)},
616     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_NS)},
617     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS)},
618     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_DMIC_STEREO)},
619 
620     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC)},
621     {TO_NAME_INDEX(SND_DEVICE_IN_HEADSET_MIC_AEC)},
622 
623     {TO_NAME_INDEX(SND_DEVICE_IN_HDMI_MIC)},
624     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC)},
625     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_NREC)},
626     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB)},
627     {TO_NAME_INDEX(SND_DEVICE_IN_BT_SCO_MIC_WB_NREC)},
628     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_LANDSCAPE)},
629 
630     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC)},
631     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_DMIC_TMUS)},
632     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC)},
633     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP)},
634     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_SPEAKER_DMIC)},
635     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_HEADSET_MIC)},
636     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC)},
637     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC)},
638     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC)},
639     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC)},
640     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC)},
641 
642 
643     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC)},
644     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_NS)},
645     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_AEC)},
646     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS)},
647     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_STEREO)},
648     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE)},
649     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_REC_HEADSET_MIC)},
650     {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC)},
651     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_USB_HEADSET_MIC)},
652     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC)},
653     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC)},
654     {TO_NAME_INDEX(SND_DEVICE_IN_USB_HEADSET_MIC_AEC)},
655 
656     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_MIC)},
657     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC)},
658     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_STEREO_MIC)},
659     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_THREE_MIC)},
660     {TO_NAME_INDEX(SND_DEVICE_IN_UNPROCESSED_QUAD_MIC)},
661 
662     {TO_NAME_INDEX(SND_DEVICE_IN_THREE_MIC)},
663     {TO_NAME_INDEX(SND_DEVICE_IN_QUAD_MIC)},
664     {TO_NAME_INDEX(SND_DEVICE_IN_CAPTURE_VI_FEEDBACK)},
665     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_TMIC)},
666     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC)},
667     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_TMIC_AEC)},
668     {TO_NAME_INDEX(SND_DEVICE_IN_HANDSET_QMIC_AEC)},
669     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE)},
670     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_PORTRAIT)},
671     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE)},
672     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE)},
673     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT)},
674     {TO_NAME_INDEX(SND_DEVICE_IN_VOICE_HEARING_AID)},
675 
676     /* For legacy xml file parsing */
677     {TO_NAME_INDEX(SND_DEVICE_IN_CAMCORDER_MIC)},
678     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_NS)},
679     {TO_NAME_INDEX(SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS)},
680 };
681 
682 static char * backend_tag_table[SND_DEVICE_MAX] = {0};
683 static char * hw_interface_table[SND_DEVICE_MAX] = {0};
684 
685 static const struct name_to_index usecase_name_index[AUDIO_USECASE_MAX] = {
686     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_DEEP_BUFFER)},
687     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_LOW_LATENCY)},
688     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_WITH_HAPTICS)},
689     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_HIFI)},
690     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_OFFLOAD)},
691     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_TTS)},
692     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_ULL)},
693     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_MMAP)},
694     {TO_NAME_INDEX(USECASE_AUDIO_RECORD)},
695     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_LOW_LATENCY)},
696     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_MMAP)},
697     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_HIFI)},
698     {TO_NAME_INDEX(USECASE_VOICE_CALL)},
699     {TO_NAME_INDEX(USECASE_VOICE2_CALL)},
700     {TO_NAME_INDEX(USECASE_VOLTE_CALL)},
701     {TO_NAME_INDEX(USECASE_QCHAT_CALL)},
702     {TO_NAME_INDEX(USECASE_VOWLAN_CALL)},
703     {TO_NAME_INDEX(USECASE_VOICEMMODE1_CALL)},
704     {TO_NAME_INDEX(USECASE_VOICEMMODE2_CALL)},
705     {TO_NAME_INDEX(USECASE_INCALL_REC_UPLINK)},
706     {TO_NAME_INDEX(USECASE_INCALL_REC_DOWNLINK)},
707     {TO_NAME_INDEX(USECASE_INCALL_REC_UPLINK_AND_DOWNLINK)},
708     {TO_NAME_INDEX(USECASE_AUDIO_HFP_SCO)},
709     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_RX)},
710     {TO_NAME_INDEX(USECASE_AUDIO_SPKR_CALIB_TX)},
711     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_AFE_PROXY)},
712     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_AFE_PROXY)},
713     {TO_NAME_INDEX(USECASE_AUDIO_DSM_FEEDBACK)},
714     {TO_NAME_INDEX(USECASE_AUDIO_PLAYBACK_VOIP)},
715     {TO_NAME_INDEX(USECASE_AUDIO_RECORD_VOIP)},
716     {TO_NAME_INDEX(USECASE_INCALL_MUSIC_UPLINK)},
717     {TO_NAME_INDEX(USECASE_INCALL_MUSIC_UPLINK2)},
718     {TO_NAME_INDEX(USECASE_AUDIO_A2DP_ABR_FEEDBACK)},
719 };
720 
721 static const struct name_to_index usecase_type_index[USECASE_TYPE_MAX] = {
722     {TO_NAME_INDEX(PCM_PLAYBACK)},
723     {TO_NAME_INDEX(PCM_CAPTURE)},
724     {TO_NAME_INDEX(VOICE_CALL)},
725     {TO_NAME_INDEX(PCM_HFP_CALL)},
726 };
727 
728 struct app_type_entry {
729     int uc_type;
730     int bit_width;
731     int app_type;
732     int max_rate;
733     char *mode;
734     struct listnode node; // membership in app_type_entry_list;
735 };
736 
737 static struct listnode app_type_entry_list;
738 
739 #define DEEP_BUFFER_PLATFORM_DELAY (29*1000LL)
740 #define LOW_LATENCY_PLATFORM_DELAY (13*1000LL)
741 #define ULL_PLATFORM_DELAY         (3*1000LL)
742 #define MMAP_PLATFORM_DELAY        (3*1000LL)
743 
744 static int audio_usecase_delay_ms[AUDIO_USECASE_MAX] = {0};
745 
746 static int audio_source_delay_ms[AUDIO_SOURCE_CNT] = {0};
747 
748 static struct name_to_index audio_source_index[AUDIO_SOURCE_CNT] = {
749     {TO_NAME_INDEX(AUDIO_SOURCE_DEFAULT)},
750     {TO_NAME_INDEX(AUDIO_SOURCE_MIC)},
751     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_UPLINK)},
752     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_DOWNLINK)},
753     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_CALL)},
754     {TO_NAME_INDEX(AUDIO_SOURCE_CAMCORDER)},
755     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_RECOGNITION)},
756     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_COMMUNICATION)},
757     {TO_NAME_INDEX(AUDIO_SOURCE_REMOTE_SUBMIX)},
758     {TO_NAME_INDEX(AUDIO_SOURCE_UNPROCESSED)},
759     {TO_NAME_INDEX(AUDIO_SOURCE_VOICE_PERFORMANCE)},
760 };
761 
762 static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
763 static bool is_tmus = false;
764 
765 static int init_be_dai_name_table(struct audio_device *adev);
766 
is_usb_snd_dev(snd_device_t snd_device)767 static bool is_usb_snd_dev(snd_device_t snd_device)
768 {
769     if (snd_device < SND_DEVICE_IN_BEGIN) {
770         if (snd_device == SND_DEVICE_OUT_USB_HEADSET ||\
771             snd_device == SND_DEVICE_OUT_USB_HEADPHONES ||\
772             snd_device == SND_DEVICE_OUT_VOICE_USB_HEADPHONES ||\
773             snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET ||\
774             snd_device == SND_DEVICE_OUT_VOICE_TTY_FULL_USB ||\
775             snd_device == SND_DEVICE_OUT_VOICE_TTY_VCO_USB)
776             return true;
777     } else {
778         if (snd_device == SND_DEVICE_IN_USB_HEADSET_MIC ||\
779             snd_device == SND_DEVICE_IN_USB_HEADSET_MIC_AEC ||\
780             snd_device == SND_DEVICE_IN_VOICE_USB_HEADSET_MIC ||\
781             snd_device == SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC ||\
782             snd_device == SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC)
783             return true;
784     }
785     return false;
786 }
787 
check_operator()788 static void check_operator()
789 {
790     char value[PROPERTY_VALUE_MAX];
791     int mccmnc;
792     property_get("gsm.sim.operator.numeric",value,"0");
793     mccmnc = atoi(value);
794     ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
795     switch(mccmnc) {
796     /* TMUS MCC(310), MNC(490, 260, 026) */
797     case 310490:
798     case 310260:
799     case 310026:
800     /* Add new TMUS MNC(800, 660, 580, 310, 270, 250, 240, 230, 220, 210, 200, 160) */
801     case 310800:
802     case 310660:
803     case 310580:
804     case 310310:
805     case 310270:
806     case 310250:
807     case 310240:
808     case 310230:
809     case 310220:
810     case 310210:
811     case 310200:
812     case 310160:
813         is_tmus = true;
814         break;
815     }
816 }
817 
is_operator_tmus()818 bool is_operator_tmus()
819 {
820     pthread_once(&check_op_once_ctl, check_operator);
821     return is_tmus;
822 }
823 
get_current_operator()824 static char *get_current_operator()
825 {
826     struct listnode *node;
827     struct operator_info *info_item;
828     char mccmnc[PROPERTY_VALUE_MAX];
829     char *ret = NULL;
830 
831     property_get("gsm.sim.operator.numeric",mccmnc,"00000");
832 
833     list_for_each(node, &operator_info_list) {
834         info_item = node_to_item(node, struct operator_info, list);
835         if (strstr(info_item->mccmnc, mccmnc) != NULL) {
836             ret = info_item->name;
837         }
838     }
839 
840     return ret;
841 }
842 
get_operator_specific_device(snd_device_t snd_device)843 static struct operator_specific_device *get_operator_specific_device(snd_device_t snd_device)
844 {
845     struct listnode *node;
846     struct operator_specific_device *ret = NULL;
847     struct operator_specific_device *device_item;
848     char *operator_name;
849 
850     operator_name = get_current_operator();
851     if (operator_name == NULL)
852         return ret;
853 
854     list_for_each(node, operator_specific_device_table[snd_device]) {
855         device_item = node_to_item(node, struct operator_specific_device, list);
856         if (strcmp(operator_name, device_item->operator) == 0) {
857             ret = device_item;
858         }
859     }
860 
861     return ret;
862 }
863 
864 
get_operator_specific_device_acdb_id(snd_device_t snd_device)865 static int get_operator_specific_device_acdb_id(snd_device_t snd_device)
866 {
867     struct operator_specific_device *device;
868     int ret = acdb_device_table[snd_device];
869 
870     device = get_operator_specific_device(snd_device);
871     if (device != NULL)
872         ret = device->acdb_id;
873 
874     return ret;
875 }
876 
get_external_specific_device_acdb_id(snd_device_t snd_device)877 static int get_external_specific_device_acdb_id(snd_device_t snd_device)
878 {
879     struct external_specific_device *ext_dev;
880     int ret = acdb_device_table[snd_device];
881     char *usbid = NULL;
882     struct listnode *node;
883 
884     if (is_usb_snd_dev(snd_device))
885         usbid = audio_extn_usb_usbid();
886 
887     if (usbid) {
888         list_for_each(node, external_specific_device_table[snd_device]) {
889             ext_dev = node_to_item(node, struct external_specific_device, list);
890             if (ext_dev->usbid && !strcmp(usbid, ext_dev->usbid)) {
891                 ret = ext_dev->acdb_id;
892                 break;
893             }
894         }
895 
896         free(usbid);
897     }
898     return ret;
899 }
900 
get_operator_specific_device_mixer_path(snd_device_t snd_device)901 static const char *get_operator_specific_device_mixer_path(snd_device_t snd_device)
902 {
903     struct operator_specific_device *device;
904     const char *ret = device_table[snd_device];
905 
906     device = get_operator_specific_device(snd_device);
907     if (device != NULL)
908         ret = device->mixer_path;
909 
910     return ret;
911 }
912 
platform_supports_app_type_cfg()913 inline bool platform_supports_app_type_cfg()
914 {
915 #if defined (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
916     return true;
917 #else
918     return false;
919 #endif
920 }
921 
parse_audiocal_cfg(struct str_parms * parms,acdb_audio_cal_cfg_t * cal)922 static int parse_audiocal_cfg(struct str_parms *parms, acdb_audio_cal_cfg_t *cal)
923 {
924     int err;
925     char value[64];
926     int ret = 0;
927 
928     if (parms == NULL || cal == NULL)
929         return ret;
930 
931     err = str_parms_get_str(parms, "cal_persist", value, sizeof(value));
932     if (err >= 0) {
933         str_parms_del(parms, "cal_persist");
934         cal->persist = (uint32_t)strtoul(value, NULL, 0);
935         ret = ret | 0x1;
936     }
937     err = str_parms_get_str(parms, "cal_apptype", value, sizeof(value));
938     if (err >= 0) {
939         str_parms_del(parms, "cal_apptype");
940         cal->app_type = (uint32_t)strtoul(value, NULL, 0);
941         ret = ret | 0x2;
942     }
943     err = str_parms_get_str(parms, "cal_caltype", value, sizeof(value));
944     if (err >= 0) {
945         str_parms_del(parms, "cal_caltype");
946         cal->cal_type = (uint32_t)strtoul(value, NULL, 0);
947         ret = ret | 0x4;
948     }
949     err = str_parms_get_str(parms, "cal_samplerate", value, sizeof(value));
950     if (err >= 0) {
951         str_parms_del(parms, "cal_samplerate");
952         cal->sampling_rate = (uint32_t)strtoul(value, NULL, 0);
953         ret = ret | 0x8;
954     }
955     err = str_parms_get_str(parms, "cal_devid", value, sizeof(value));
956     if (err >= 0) {
957         str_parms_del(parms, "cal_devid");
958         cal->dev_id = (uint32_t)strtoul(value, NULL, 0);
959         ret = ret | 0x10;
960     }
961     err = str_parms_get_str(parms, "cal_snddevid", value, sizeof(value));
962     if (err >= 0) {
963         str_parms_del(parms, "cal_snddevid");
964         cal->snd_dev_id = (uint32_t)strtoul(value, NULL, 0);
965         ret = ret | 0x20;
966     }
967     err = str_parms_get_str(parms, "cal_topoid", value, sizeof(value));
968     if (err >= 0) {
969         str_parms_del(parms, "cal_topoid");
970         cal->topo_id = (uint32_t)strtoul(value, NULL, 0);
971         ret = ret | 0x40;
972     }
973     err = str_parms_get_str(parms, "cal_moduleid", value, sizeof(value));
974     if (err >= 0) {
975         str_parms_del(parms, "cal_moduleid");
976         cal->module_id = (uint32_t)strtoul(value, NULL, 0);
977         ret = ret | 0x80;
978     }
979     err = str_parms_get_str(parms, "cal_paramid", value, sizeof(value));
980     if (err >= 0) {
981         str_parms_del(parms, "cal_paramid");
982         cal->param_id = (uint32_t)strtoul(value, NULL, 0);
983         ret = ret | 0x100;
984     }
985 #ifdef PLATFORM_SM8150
986     err = str_parms_get_str(parms, "cal_instanceid", value, sizeof(value));
987     if (err >= 0) {
988         str_parms_del(parms, "cal_instanceid");
989         cal->instance_id = (uint32_t)strtoul(value, NULL, 0);
990         ret = ret | 0x200;
991     }
992 #endif
993 
994     return ret;
995 }
996 
set_audiocal(void * platform,struct str_parms * parms,char * value,int len)997 static void set_audiocal(void *platform, struct str_parms *parms, char *value, int len)
998 {
999     struct platform_data *my_data = (struct platform_data *)platform;
1000     acdb_audio_cal_cfg_t cal;
1001     uint8_t *dptr = NULL;
1002     int32_t dlen = 0;
1003     int err ,ret;
1004 
1005     if (value == NULL || platform == NULL || parms == NULL) {
1006         ALOGE("[%s] received null pointer, failed", __func__);
1007         goto done_key_audcal;
1008     }
1009 
1010     memset(&cal, 0, sizeof(acdb_audio_cal_cfg_t));
1011     /* parse audio calibration keys */
1012     ret = parse_audiocal_cfg(parms, &cal);
1013 
1014     /* handle audio calibration data now */
1015     err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_AUD_CALDATA, value, len);
1016     if (err >= 0) {
1017         str_parms_del(parms, AUDIO_PARAMETER_KEY_AUD_CALDATA);
1018         dlen = strlen(value);
1019         if (dlen <= 0) {
1020             ALOGE("[%s] null data received", __func__);
1021             goto done_key_audcal;
1022         }
1023         /*
1024            The base64 encoded string is always larger than the binary data,
1025            so b64_pton will always output less data than provided (around 1/3
1026            less than the input data). That's why we can allocate input buffer
1027            length and then get function work.
1028         */
1029         dptr = (uint8_t *)calloc(dlen, sizeof(uint8_t));
1030         if (dptr == NULL) {
1031             ALOGE("[%s] memory allocation failed for %d", __func__, dlen);
1032             goto done_key_audcal;
1033         }
1034         dlen = b64_pton(value, dptr, dlen);
1035         if (dlen <= 0) {
1036             ALOGE("[%s] data decoding failed %d", __func__, dlen);
1037             goto done_key_audcal;
1038         }
1039 
1040         if (cal.dev_id) {
1041             if (audio_is_input_device(cal.dev_id)) {
1042                 // FIXME: why pass an input device whereas
1043                 // platform_get_input_snd_device() expects as an output device?
1044                 cal.snd_dev_id = platform_get_input_snd_device(platform, NULL, cal.dev_id);
1045             } else {
1046                 cal.snd_dev_id = platform_get_output_snd_device(platform, cal.dev_id);
1047             }
1048         }
1049         cal.acdb_dev_id = platform_get_snd_device_acdb_id(cal.snd_dev_id);
1050         ALOGD("Setting audio calibration for snd_device(%d) acdb_id(%d)",
1051                 cal.snd_dev_id, cal.acdb_dev_id);
1052         if (cal.acdb_dev_id == -EINVAL) {
1053             ALOGE("[%s] Invalid acdb_device id %d for snd device id %d",
1054                        __func__, cal.acdb_dev_id, cal.snd_dev_id);
1055             goto done_key_audcal;
1056         }
1057         if (my_data->acdb_set_audio_cal) {
1058             ret = my_data->acdb_set_audio_cal((void *)&cal, (void *)dptr, dlen);
1059         }
1060     }
1061 done_key_audcal:
1062     if (dptr != NULL)
1063         free(dptr);
1064 }
1065 
platform_send_gain_dep_cal(void * platform,int level)1066 bool platform_send_gain_dep_cal(void *platform, int level)
1067 {
1068     bool ret_val = false;
1069     struct platform_data *my_data = (struct platform_data *)platform;
1070     struct audio_device *adev = my_data->adev;
1071     int acdb_dev_id, app_type;
1072     int acdb_dev_type = MSM_SNDDEV_CAP_RX;
1073     int mode = CAL_MODE_RTAC;
1074     struct listnode *node;
1075     struct audio_usecase *usecase;
1076     bool valid_uc_type;
1077     bool valid_dev;
1078 
1079     if (my_data->acdb_send_gain_dep_cal == NULL) {
1080         ALOGE("%s: dlsym error for acdb_send_gain_dep_cal", __func__);
1081         return ret_val;
1082     }
1083 
1084     if (!voice_is_in_call(adev)) {
1085         ALOGV("%s: Not Voice call usecase, apply new cal for level %d",
1086                __func__, level);
1087 
1088         // find the current active sound device
1089         list_for_each(node, &adev->usecase_list) {
1090             usecase = node_to_item(node, struct audio_usecase, list);
1091             LOG_ALWAYS_FATAL_IF(usecase == NULL,
1092                                 "unxpected NULL usecase in usecase_list");
1093             valid_uc_type = usecase->type == PCM_PLAYBACK;
1094             valid_dev = false;
1095             if (valid_uc_type) {
1096                 audio_devices_t dev = usecase->stream.out->devices;
1097                 valid_dev = (dev == AUDIO_DEVICE_OUT_SPEAKER ||
1098                              dev == AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
1099                              dev == AUDIO_DEVICE_OUT_WIRED_HEADSET ||
1100                              dev == AUDIO_DEVICE_OUT_WIRED_HEADPHONE);
1101             }
1102             if (valid_dev) {
1103                 ALOGV("%s: out device is %d", __func__,  usecase->out_snd_device);
1104                 if (platform_supports_app_type_cfg())
1105                     app_type = usecase->stream.out->app_type_cfg.app_type;
1106                 else
1107                     app_type = DEFAULT_APP_TYPE_RX_PATH;
1108 
1109                 acdb_dev_id = platform_get_snd_device_acdb_id(usecase->out_snd_device);
1110 
1111                 if (!my_data->acdb_send_gain_dep_cal(acdb_dev_id, app_type,
1112                                                      acdb_dev_type, mode, level)) {
1113                     // set ret_val true if at least one calibration is set successfully
1114                     ret_val = true;
1115                 } else {
1116                     ALOGE("%s: my_data->acdb_send_gain_dep_cal failed ", __func__);
1117                 }
1118             } else {
1119                 ALOGW("%s: Usecase list is empty", __func__);
1120             }
1121         }
1122     } else {
1123         ALOGW("%s: Voice call in progress .. ignore setting new cal",
1124               __func__);
1125     }
1126     return ret_val;
1127 }
1128 
platform_set_echo_reference(struct audio_device * adev,bool enable,audio_devices_t out_device)1129 void platform_set_echo_reference(struct audio_device *adev, bool enable, audio_devices_t out_device)
1130 {
1131     struct platform_data *my_data = (struct platform_data *)adev->platform;
1132     snd_device_t snd_device = SND_DEVICE_NONE;
1133 
1134     if (strcmp(my_data->ec_ref_mixer_path, "")) {
1135         ALOGV("%s: diabling %s", __func__, my_data->ec_ref_mixer_path);
1136         audio_route_reset_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
1137     }
1138 
1139     if (enable) {
1140         strcpy(my_data->ec_ref_mixer_path, "echo-reference");
1141         if (out_device != AUDIO_DEVICE_NONE) {
1142             snd_device = platform_get_output_snd_device(adev->platform, out_device);
1143             platform_add_backend_name(adev->platform, my_data->ec_ref_mixer_path, snd_device);
1144         }
1145 
1146         ALOGV("%s: enabling %s", __func__, my_data->ec_ref_mixer_path);
1147         audio_route_apply_and_update_path(adev->audio_route, my_data->ec_ref_mixer_path);
1148     }
1149 }
1150 
open_csd_client(bool i2s_ext_modem)1151 static struct csd_data *open_csd_client(bool i2s_ext_modem)
1152 {
1153     struct csd_data *csd = calloc(1, sizeof(struct csd_data));
1154 
1155     csd->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
1156     if (csd->csd_client == NULL) {
1157         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
1158         goto error;
1159     } else {
1160         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
1161 
1162         csd->deinit = (deinit_t)dlsym(csd->csd_client,
1163                                              "csd_client_deinit");
1164         if (csd->deinit == NULL) {
1165             ALOGE("%s: dlsym error %s for csd_client_deinit", __func__,
1166                   dlerror());
1167             goto error;
1168         }
1169         csd->disable_device = (disable_device_t)dlsym(csd->csd_client,
1170                                              "csd_client_disable_device");
1171         if (csd->disable_device == NULL) {
1172             ALOGE("%s: dlsym error %s for csd_client_disable_device",
1173                   __func__, dlerror());
1174             goto error;
1175         }
1176         csd->enable_device_config = (enable_device_config_t)dlsym(csd->csd_client,
1177                                                "csd_client_enable_device_config");
1178         if (csd->enable_device_config == NULL) {
1179             ALOGE("%s: dlsym error %s for csd_client_enable_device_config",
1180                   __func__, dlerror());
1181             goto error;
1182         }
1183         csd->enable_device = (enable_device_t)dlsym(csd->csd_client,
1184                                              "csd_client_enable_device");
1185         if (csd->enable_device == NULL) {
1186             ALOGE("%s: dlsym error %s for csd_client_enable_device",
1187                   __func__, dlerror());
1188             goto error;
1189         }
1190         csd->start_voice = (start_voice_t)dlsym(csd->csd_client,
1191                                              "csd_client_start_voice");
1192         if (csd->start_voice == NULL) {
1193             ALOGE("%s: dlsym error %s for csd_client_start_voice",
1194                   __func__, dlerror());
1195             goto error;
1196         }
1197         csd->stop_voice = (stop_voice_t)dlsym(csd->csd_client,
1198                                              "csd_client_stop_voice");
1199         if (csd->stop_voice == NULL) {
1200             ALOGE("%s: dlsym error %s for csd_client_stop_voice",
1201                   __func__, dlerror());
1202             goto error;
1203         }
1204         csd->volume = (volume_t)dlsym(csd->csd_client,
1205                                              "csd_client_volume");
1206         if (csd->volume == NULL) {
1207             ALOGE("%s: dlsym error %s for csd_client_volume",
1208                   __func__, dlerror());
1209             goto error;
1210         }
1211         csd->mic_mute = (mic_mute_t)dlsym(csd->csd_client,
1212                                              "csd_client_mic_mute");
1213         if (csd->mic_mute == NULL) {
1214             ALOGE("%s: dlsym error %s for csd_client_mic_mute",
1215                   __func__, dlerror());
1216             goto error;
1217         }
1218         csd->slow_talk = (slow_talk_t)dlsym(csd->csd_client,
1219                                              "csd_client_slow_talk");
1220         if (csd->slow_talk == NULL) {
1221             ALOGE("%s: dlsym error %s for csd_client_slow_talk",
1222                   __func__, dlerror());
1223             goto error;
1224         }
1225         csd->start_playback = (start_playback_t)dlsym(csd->csd_client,
1226                                              "csd_client_start_playback");
1227         if (csd->start_playback == NULL) {
1228             ALOGE("%s: dlsym error %s for csd_client_start_playback",
1229                   __func__, dlerror());
1230             goto error;
1231         }
1232         csd->stop_playback = (stop_playback_t)dlsym(csd->csd_client,
1233                                              "csd_client_stop_playback");
1234         if (csd->stop_playback == NULL) {
1235             ALOGE("%s: dlsym error %s for csd_client_stop_playback",
1236                   __func__, dlerror());
1237             goto error;
1238         }
1239         csd->start_record = (start_record_t)dlsym(csd->csd_client,
1240                                              "csd_client_start_record");
1241         if (csd->start_record == NULL) {
1242             ALOGE("%s: dlsym error %s for csd_client_start_record",
1243                   __func__, dlerror());
1244             goto error;
1245         }
1246         csd->stop_record = (stop_record_t)dlsym(csd->csd_client,
1247                                              "csd_client_stop_record");
1248         if (csd->stop_record == NULL) {
1249             ALOGE("%s: dlsym error %s for csd_client_stop_record",
1250                   __func__, dlerror());
1251             goto error;
1252         }
1253 
1254         csd->get_sample_rate = (get_sample_rate_t)dlsym(csd->csd_client,
1255                                              "csd_client_get_sample_rate");
1256         if (csd->get_sample_rate == NULL) {
1257             ALOGE("%s: dlsym error %s for csd_client_get_sample_rate",
1258                   __func__, dlerror());
1259 
1260             goto error;
1261         }
1262 
1263         csd->init = (init_t)dlsym(csd->csd_client, "csd_client_init");
1264 
1265         if (csd->init == NULL) {
1266             ALOGE("%s: dlsym error %s for csd_client_init",
1267                   __func__, dlerror());
1268             goto error;
1269         } else {
1270             csd->init(i2s_ext_modem);
1271         }
1272     }
1273     return csd;
1274 
1275 error:
1276     free(csd);
1277     csd = NULL;
1278     return csd;
1279 }
1280 
close_csd_client(struct csd_data * csd)1281 void close_csd_client(struct csd_data *csd)
1282 {
1283     if (csd != NULL) {
1284         csd->deinit();
1285         dlclose(csd->csd_client);
1286         free(csd);
1287         csd = NULL;
1288     }
1289 }
1290 
platform_csd_init(struct platform_data * my_data)1291 static void platform_csd_init(struct platform_data *my_data)
1292 {
1293 #ifdef PLATFORM_MSM8084
1294     int32_t modems, (*count_modems)(void);
1295     const char *name = "libdetectmodem.so";
1296     const char *func = "count_modems";
1297     const char *error;
1298 
1299     my_data->csd = NULL;
1300 
1301     void *lib = dlopen(name, RTLD_NOW);
1302     error = dlerror();
1303     if (!lib) {
1304         ALOGE("%s: could not find %s: %s", __func__, name, error);
1305         return;
1306     }
1307 
1308     count_modems = NULL;
1309     *(void **)(&count_modems) = dlsym(lib, func);
1310     error = dlerror();
1311     if (!count_modems) {
1312         ALOGE("%s: could not find symbol %s in %s: %s",
1313               __func__, func, name, error);
1314         goto done;
1315     }
1316 
1317     modems = count_modems();
1318     if (modems < 0) {
1319         ALOGE("%s: count_modems failed\n", __func__);
1320         goto done;
1321     }
1322 
1323     ALOGD("%s: num_modems %d\n", __func__, modems);
1324     if (modems > 0)
1325         my_data->csd = open_csd_client(false /*is_i2s_ext_modem*/);
1326 
1327 done:
1328     dlclose(lib);
1329 #else
1330      my_data->csd = NULL;
1331 #endif
1332 }
1333 
set_platform_defaults(struct platform_data * my_data)1334 static void set_platform_defaults(struct platform_data * my_data)
1335 {
1336     int32_t dev;
1337     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
1338         backend_tag_table[dev] = NULL;
1339         hw_interface_table[dev] = NULL;
1340         operator_specific_device_table[dev] = NULL;
1341         external_specific_device_table[dev] = NULL;
1342     }
1343 
1344     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
1345         backend_bit_width_table[dev] = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1346     }
1347 
1348     // To overwrite these go to the audio_platform_info.xml file.
1349     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("bt-sco");
1350     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("bt-sco");
1351     backend_tag_table[SND_DEVICE_OUT_BT_SCO] = strdup("bt-sco");
1352     backend_tag_table[SND_DEVICE_OUT_HDMI] = strdup("hdmi");
1353     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("speaker-and-hdmi");
1354     backend_tag_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("bt-sco-wb");
1355     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("bt-sco-wb");
1356     backend_tag_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("bt-sco-wb");
1357     backend_tag_table[SND_DEVICE_OUT_VOICE_TX] = strdup("afe-proxy");
1358     backend_tag_table[SND_DEVICE_IN_VOICE_RX] = strdup("afe-proxy");
1359 
1360     backend_tag_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("usb-headset");
1361     backend_tag_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("usb-headset");
1362     backend_tag_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("usb-headphones");
1363     backend_tag_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("usb-headphones");
1364     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] =
1365         strdup("speaker-and-usb-headphones");
1366     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] =
1367         strdup("speaker-safe-and-usb-headphones");
1368     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] =
1369         strdup("speaker-safe-and-bt-sco"),
1370     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] =
1371         strdup("speaker-safe-and-bt-sco-wb"),
1372     backend_tag_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("usb-headset-mic");
1373     backend_tag_table[SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = strdup("usb-headset-mic");
1374     backend_tag_table[SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = strdup("usb-headset-mic");
1375     backend_tag_table[SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = strdup("usb-headset-mic");
1376     backend_tag_table[SND_DEVICE_IN_USB_HEADSET_MIC_AEC] = strdup("usb-headset-mic");
1377     backend_tag_table[SND_DEVICE_OUT_BT_A2DP] = strdup("bt-a2dp");
1378     backend_tag_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] = strdup("speaker-and-bt-a2dp");
1379     backend_tag_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] = strdup("speaker-safe-and-bt-a2dp");
1380     backend_tag_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("usb-headset");
1381     backend_tag_table[SND_DEVICE_OUT_VOICE_HEARING_AID] = strdup("hearing-aid");
1382 
1383     hw_interface_table[SND_DEVICE_OUT_HANDSET] = strdup("SLIMBUS_0_RX");
1384     hw_interface_table[SND_DEVICE_OUT_SPEAKER] = strdup("SLIMBUS_0_RX");
1385     hw_interface_table[SND_DEVICE_OUT_SPEAKER_REVERSE] = strdup("SLIMBUS_0_RX");
1386     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE] = strdup("SLIMBUS_0_RX");
1387     hw_interface_table[SND_DEVICE_OUT_HEADPHONES] = strdup("SLIMBUS_0_RX");
1388     hw_interface_table[SND_DEVICE_OUT_LINE] = strdup("SLIMBUS_0_RX");
1389     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = strdup("SLIMBUS_0_RX");
1390     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES] = strdup("SLIMBUS_0_RX");
1391     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_LINE] = strdup("SLIMBUS_0_RX");
1392     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE] = strdup("SLIMBUS_0_RX");
1393     hw_interface_table[SND_DEVICE_OUT_VOICE_HANDSET] = strdup("SLIMBUS_0_RX");
1394     hw_interface_table[SND_DEVICE_OUT_VOICE_HAC_HANDSET] = strdup("SLIMBUS_0_RX");
1395     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER] = strdup("SLIMBUS_0_RX");
1396     hw_interface_table[SND_DEVICE_OUT_VOICE_HEADPHONES] = strdup("SLIMBUS_0_RX");
1397     hw_interface_table[SND_DEVICE_OUT_VOICE_HEADSET] = strdup("SLIMBUS_0_RX");
1398     hw_interface_table[SND_DEVICE_OUT_VOICE_MUSIC_TX] = strdup("VOICE_PLAYBACK_TX");
1399     hw_interface_table[SND_DEVICE_OUT_VOICE_LINE] = strdup("SLIMBUS_0_RX");
1400     hw_interface_table[SND_DEVICE_OUT_HDMI] = strdup("HDMI_RX");
1401     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_HDMI] = strdup("SLIMBUS_0_RX-and-HDMI_RX");
1402     hw_interface_table[SND_DEVICE_OUT_BT_SCO] = strdup("SEC_AUX_PCM_RX");
1403     hw_interface_table[SND_DEVICE_OUT_BT_SCO_WB] = strdup("SEC_AUX_PCM_RX");
1404     hw_interface_table[SND_DEVICE_OUT_BT_A2DP] = strdup("SLIMBUS_7_RX");
1405     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP] =
1406         strdup("SLIMBUS_0_RX-and-SLIMBUS_7_RX");
1407     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP] =
1408         strdup("SLIMBUS_0_RX-and-SLIMBUS_7_RX");
1409     hw_interface_table[SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = strdup("SLIMBUS_0_RX");
1410     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = strdup("SLIMBUS_0_RX");
1411     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = strdup("SLIMBUS_0_RX");
1412     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = strdup("SLIMBUS_0_RX");
1413     hw_interface_table[SND_DEVICE_OUT_USB_HEADSET] = strdup("USB_AUDIO_RX");
1414     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_FULL_USB] = strdup("USB_AUDIO_RX");
1415     hw_interface_table[SND_DEVICE_OUT_VOICE_TTY_VCO_USB] = strdup("USB_AUDIO_RX");
1416     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADSET] = strdup("USB_AUDIO_RX");
1417     hw_interface_table[SND_DEVICE_OUT_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
1418     hw_interface_table[SND_DEVICE_OUT_VOICE_USB_HEADPHONES] = strdup("USB_AUDIO_RX");
1419     hw_interface_table[SND_DEVICE_OUT_USB_HEADSET_SPEC] = strdup("USB_AUDIO_RX");
1420     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
1421     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET] = strdup("SLIMBUS_0_RX-and-USB_AUDIO_RX");
1422     hw_interface_table[SND_DEVICE_OUT_VOICE_TX] = strdup("AFE_PCM_RX");
1423     hw_interface_table[SND_DEVICE_OUT_SPEAKER_PROTECTED] = strdup("SLIMBUS_0_RX");
1424     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED] = strdup("SLIMBUS_0_RX");
1425     hw_interface_table[SND_DEVICE_OUT_VOICE_SPEAKER_HFP] = strdup("SLIMBUS_0_RX");
1426     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_SCO] = strdup("SLIMBUS_0_RX-and-SEC_AUX_PCM_RX");
1427     hw_interface_table[SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB] = strdup("SLIMBUS_0_RX-and-SEC_AUX_PCM_RX");
1428     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO] = strdup("QUAT_TDM_RX_0-and-SLIMBUS_7_RX"),
1429     hw_interface_table[SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB] = strdup("QUAT_TDM_RX_0-and-SLIMBUS_7_RX"),
1430     /* So far, primary hal doesn't support hearing aid device.
1431        Need snd_device to route voice call and use specific acdb tuning.
1432        Also, BT_RX is a virtual port to indicate bluetooth hearing aid. */
1433     hw_interface_table[SND_DEVICE_OUT_VOICE_HEARING_AID] = strdup("BT_RX"),
1434 
1435     hw_interface_table[SND_DEVICE_IN_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
1436     hw_interface_table[SND_DEVICE_IN_VOICE_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
1437     hw_interface_table[SND_DEVICE_IN_USB_HEADSET_MIC_AEC] =  strdup("USB_AUDIO_TX");
1438     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
1439     hw_interface_table[SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC] = strdup("USB_AUDIO_TX");
1440     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC] = strdup("USB_AUDIO_TX");
1441     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC] = strdup("USB_AUDIO_TX");
1442     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
1443     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_AEC] = strdup("SLIMBUS_0_TX");
1444     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_NS] = strdup("SLIMBUS_0_TX");
1445     hw_interface_table[SND_DEVICE_IN_HANDSET_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1446     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC] = strdup("SLIMBUS_0_TX");
1447     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_AEC] = strdup("SLIMBUS_0_TX");
1448     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_NS] = strdup("SLIMBUS_0_TX");
1449     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1450     hw_interface_table[SND_DEVICE_IN_HANDSET_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
1451     hw_interface_table[SND_DEVICE_IN_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1452     hw_interface_table[SND_DEVICE_IN_HEADSET_MIC_AEC] = strdup("SLIMBUS_0_TX");
1453     hw_interface_table[SND_DEVICE_IN_CAMCORDER_LANDSCAPE] = strdup("SLIMBUS_0_TX");
1454     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC] = strdup("SLIMBUS_0_TX");
1455     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_NS] = strdup("SLIMBUS_0_TX");
1456     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_AEC] = strdup("SLIMBUS_0_TX");
1457     hw_interface_table[SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1458     hw_interface_table[SND_DEVICE_IN_VOICE_REC_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
1459     hw_interface_table[SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE] = strdup("SLIMBUS_0_TX");
1460     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_MIC] = strdup("SLIMBUS_0_TX");
1461     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1462     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_STEREO_MIC] = strdup("SLIMBUS_0_TX");
1463     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_THREE_MIC] = strdup("SLIMBUS_0_TX");
1464     hw_interface_table[SND_DEVICE_IN_UNPROCESSED_QUAD_MIC] = strdup("SLIMBUS_0_TX");
1465     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC] = strdup("SLIMBUS_0_TX");
1466     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_AEC] = strdup("SLIMBUS_0_TX");
1467     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_NS] = strdup("SLIMBUS_0_TX");
1468     hw_interface_table[SND_DEVICE_IN_SPEAKER_MIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1469     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC] = strdup("SLIMBUS_0_TX");
1470     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_AEC] = strdup("SLIMBUS_0_TX");
1471     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_NS] = strdup("SLIMBUS_0_TX");
1472     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1473     hw_interface_table[SND_DEVICE_IN_SPEAKER_DMIC_STEREO] = strdup("SLIMBUS_0_TX");
1474     hw_interface_table[SND_DEVICE_IN_VOICE_DMIC] = strdup("SLIMBUS_0_TX");
1475     hw_interface_table[SND_DEVICE_IN_VOICE_DMIC_TMUS] = strdup("SLIMBUS_0_TX");
1476     hw_interface_table[SND_DEVICE_IN_SPEAKER_QMIC_NS] = strdup("SLIMBUS_0_TX");
1477     hw_interface_table[SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS] = strdup("SLIMBUS_0_TX");
1478     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_MIC] = strdup("SLIMBUS_0_TX");
1479     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_DMIC] = strdup("SLIMBUS_0_TX");
1480     hw_interface_table[SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP] = strdup("SLIMBUS_0_TX");
1481     hw_interface_table[SND_DEVICE_IN_VOICE_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1482     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1483     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = strdup("SLIMBUS_0_TX");
1484     hw_interface_table[SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1485     hw_interface_table[SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = strdup("SLIMBUS_0_TX");
1486     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC] = strdup("SEC_AUX_PCM_TX");
1487     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_NREC] = strdup("SEC_AUX_PCM_TX");
1488     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB] = strdup("SEC_AUX_PCM_TX");
1489     hw_interface_table[SND_DEVICE_IN_BT_SCO_MIC_WB_NREC] = strdup("SEC_AUX_PCM_TX");
1490     hw_interface_table[SND_DEVICE_IN_VOICE_RX] = strdup("AFE_PCM_TX");
1491     hw_interface_table[SND_DEVICE_IN_THREE_MIC] = strdup("SLIMBUS_0_TX");
1492     hw_interface_table[SND_DEVICE_IN_QUAD_MIC] = strdup("SLIMBUS_0_TX");
1493     hw_interface_table[SND_DEVICE_IN_HANDSET_TMIC] = strdup("SLIMBUS_0_TX");
1494     hw_interface_table[SND_DEVICE_IN_HANDSET_QMIC] = strdup("SLIMBUS_0_TX");
1495     hw_interface_table[SND_DEVICE_IN_HANDSET_TMIC_AEC] = strdup("SLIMBUS_0_TX");
1496     hw_interface_table[SND_DEVICE_IN_HANDSET_QMIC_AEC] = strdup("SLIMBUS_0_TX");
1497     hw_interface_table[SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE] = strdup("SLIMBUS_0_TX");
1498     hw_interface_table[SND_DEVICE_IN_CAMCORDER_PORTRAIT] = strdup("SLIMBUS_0_TX");
1499     hw_interface_table[SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE] = strdup("SLIMBUS_0_TX");
1500     hw_interface_table[SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE] = strdup("SLIMBUS_0_TX");
1501     hw_interface_table[SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT] = strdup("SLIMBUS_0_TX");
1502     hw_interface_table[SND_DEVICE_IN_VOICE_HEARING_AID] = strdup("SLIMBUS_0_TX");
1503 
1504     my_data->max_mic_count = PLATFORM_DEFAULT_MIC_COUNT;
1505 }
1506 
get_cvd_version(char * cvd_version,struct audio_device * adev)1507 void get_cvd_version(char *cvd_version, struct audio_device *adev)
1508 {
1509     struct mixer_ctl *ctl;
1510     int count;
1511     int ret = 0;
1512 
1513     ctl = mixer_get_ctl_by_name(adev->mixer, CVD_VERSION_MIXER_CTL);
1514     if (!ctl) {
1515         ALOGE("%s: Could not get ctl for mixer cmd - %s",  __func__, CVD_VERSION_MIXER_CTL);
1516         goto done;
1517     }
1518     mixer_ctl_update(ctl);
1519 
1520     count = mixer_ctl_get_num_values(ctl);
1521     if (count > MAX_CVD_VERSION_STRING_SIZE)
1522         count = MAX_CVD_VERSION_STRING_SIZE - 1;
1523 
1524     ret = mixer_ctl_get_array(ctl, cvd_version, count);
1525     if (ret != 0) {
1526         ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
1527         goto done;
1528     }
1529 
1530 done:
1531     return;
1532 }
1533 
platform_acdb_init(void * platform)1534 static int platform_acdb_init(void *platform)
1535 {
1536     struct platform_data *my_data = (struct platform_data *)platform;
1537     struct audio_device *adev = my_data->adev;
1538 
1539     if (!my_data->acdb_init) {
1540         ALOGE("%s: no acdb_init fn provided", __func__);
1541         return -1;
1542     }
1543 
1544     if (my_data->acdb_initialized) {
1545         ALOGW("acdb is already initialized");
1546         return 0;
1547     }
1548 
1549 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
1550     char *cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
1551     if (!cvd_version)
1552         ALOGE("failed to allocate cvd_version");
1553     else {
1554         get_cvd_version(cvd_version, adev);
1555         my_data->acdb_init((char *)my_data->snd_card_name, cvd_version, 0);
1556         free(cvd_version);
1557     }
1558 #elif defined (PLATFORM_MSM8084)
1559     my_data->acdb_init((char *)my_data->snd_card_name);
1560 #else
1561     my_data->acdb_init();
1562 #endif
1563     my_data->acdb_initialized = true;
1564     return 0;
1565 }
1566 
1567 static void
platform_backend_config_init(struct platform_data * pdata)1568 platform_backend_config_init(struct platform_data *pdata)
1569 {
1570     int i;
1571 
1572     /* initialize backend config */
1573     for (i = 0; i < MAX_CODEC_BACKENDS; i++) {
1574         pdata->current_backend_cfg[i].sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
1575         pdata->current_backend_cfg[i].bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
1576         pdata->current_backend_cfg[i].channels = CODEC_BACKEND_DEFAULT_CHANNELS;
1577 
1578         if (i > MAX_RX_CODEC_BACKENDS)
1579             pdata->current_backend_cfg[i].channels = CODEC_BACKEND_DEFAULT_TX_CHANNELS;
1580 
1581         pdata->current_backend_cfg[i].bitwidth_mixer_ctl = NULL;
1582         pdata->current_backend_cfg[i].samplerate_mixer_ctl = NULL;
1583         pdata->current_backend_cfg[i].channels_mixer_ctl = NULL;
1584     }
1585 
1586     pdata->current_backend_cfg[DEFAULT_CODEC_BACKEND].bitwidth_mixer_ctl =
1587             strdup("SLIM_0_RX Format");
1588     pdata->current_backend_cfg[DEFAULT_CODEC_BACKEND].samplerate_mixer_ctl =
1589             strdup("SLIM_0_RX SampleRate");
1590 
1591     pdata->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].bitwidth_mixer_ctl =
1592             strdup("SLIM_0_TX Format");
1593     pdata->current_backend_cfg[DEFAULT_CODEC_TX_BACKEND].samplerate_mixer_ctl =
1594             strdup("SLIM_0_TX SampleRate");
1595 
1596     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].bitwidth_mixer_ctl =
1597             strdup("USB_AUDIO_TX Format");
1598     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].samplerate_mixer_ctl =
1599             strdup("USB_AUDIO_TX SampleRate");
1600     pdata->current_backend_cfg[USB_AUDIO_TX_BACKEND].channels_mixer_ctl =
1601             strdup("USB_AUDIO_TX Channels");
1602 
1603     if (strstr(pdata->snd_card_name, "intcodec")) {
1604         pdata->current_backend_cfg[HEADPHONE_BACKEND].bitwidth_mixer_ctl =
1605                 strdup("INT0_MI2S_RX Format");
1606         pdata->current_backend_cfg[HEADPHONE_BACKEND].samplerate_mixer_ctl =
1607                 strdup("INT0_MI2S_RX SampleRate");
1608     } else {
1609         pdata->current_backend_cfg[HEADPHONE_BACKEND].bitwidth_mixer_ctl =
1610                 strdup("SLIM_6_RX Format");
1611         pdata->current_backend_cfg[HEADPHONE_BACKEND].samplerate_mixer_ctl =
1612                 strdup("SLIM_6_RX SampleRate");
1613     }
1614 
1615     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].bitwidth_mixer_ctl =
1616             strdup("USB_AUDIO_RX Format");
1617     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].samplerate_mixer_ctl =
1618             strdup("USB_AUDIO_RX SampleRate");
1619 
1620     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].channels = 1;
1621     pdata->current_backend_cfg[USB_AUDIO_RX_BACKEND].channels_mixer_ctl =
1622             strdup("USB_AUDIO_RX Channels");
1623 }
1624 
1625 static int
platform_backend_app_type_cfg_init(struct platform_data * pdata,struct mixer * mixer)1626 platform_backend_app_type_cfg_init(struct platform_data *pdata,
1627                                    struct mixer *mixer)
1628 {
1629     size_t app_type_cfg[128] = {0};
1630     int length, num_app_types = 0;
1631     struct mixer_ctl *ctl = NULL;
1632 
1633     const char *mixer_ctl_name = "App Type Config";
1634     ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
1635     if (!ctl) {
1636         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
1637         return -1;
1638     }
1639 
1640     length = 1; // reserve index 0 for number of app types
1641 
1642     struct listnode *node;
1643     struct app_type_entry *entry;
1644     list_for_each(node, &app_type_entry_list) {
1645         entry = node_to_item(node, struct app_type_entry, node);
1646         app_type_cfg[length++] = entry->app_type;
1647         app_type_cfg[length++] = entry->max_rate;
1648         app_type_cfg[length++] = entry->bit_width;
1649         ALOGI("%s add entry %d %d", __func__, entry->app_type, entry->bit_width);
1650         num_app_types += 1;
1651     }
1652 
1653     // default for capture
1654     int t;
1655     platform_get_default_app_type_v2(pdata,
1656                                      PCM_CAPTURE,
1657                                      &t);
1658     app_type_cfg[length++] = t;
1659     app_type_cfg[length++] = 48000;
1660     app_type_cfg[length++] = 16;
1661     num_app_types += 1;
1662 
1663     if (num_app_types) {
1664         app_type_cfg[0] = num_app_types;
1665         if (mixer_ctl_set_array(ctl, app_type_cfg, length) < 0) {
1666             ALOGE("Failed to set app type cfg");
1667         }
1668     }
1669     return 0;
1670 }
1671 
configure_flicker_sensor_input(struct mixer * mixer)1672 static void configure_flicker_sensor_input(struct mixer *mixer)
1673 {
1674     struct mixer_ctl *ctl;
1675     const char* ctl1 = "AIF3_CAP Mixer SLIM TX2";
1676     int setting1 = 1;
1677     const char* ctl2 = "CDC_IF TX2 MUX";
1678     const char* setting2 = "DEC2";
1679     const char* ctl3 = "SLIM_1_TX Channels";
1680     const char* setting3 = "One";
1681     const char* ctl4 = "ADC MUX2";
1682     const char* setting4 = "AMIC";
1683     const char* ctl5 = "AMIC MUX2";
1684     const char* setting5 = "ADC1";
1685     const char* ctl6 = "DEC2 Volume";
1686     int setting6 = 84;
1687     const char* ctl7 = "MultiMedia9 Mixer SLIM_1_TX";
1688     int setting7 = 1;
1689     const char* ctl8 = "SLIM_1_TX SampleRate";
1690     const char* setting8 = "KHZ_8";
1691 
1692     ctl = mixer_get_ctl_by_name(mixer, ctl1);
1693     mixer_ctl_set_value(ctl, 0, setting1);
1694     ctl = mixer_get_ctl_by_name(mixer, ctl2);
1695     mixer_ctl_set_enum_by_string(ctl, setting2);
1696     ctl = mixer_get_ctl_by_name(mixer, ctl3);
1697     mixer_ctl_set_enum_by_string(ctl, setting3);
1698     ctl = mixer_get_ctl_by_name(mixer, ctl4);
1699     mixer_ctl_set_enum_by_string(ctl, setting4);
1700     ctl = mixer_get_ctl_by_name(mixer, ctl5);
1701     mixer_ctl_set_enum_by_string(ctl, setting5);
1702     ctl = mixer_get_ctl_by_name(mixer, ctl6);
1703     mixer_ctl_set_value(ctl, 0, setting6);
1704     ctl = mixer_get_ctl_by_name(mixer, ctl7);
1705     mixer_ctl_set_value(ctl, 0, setting7);
1706     ctl = mixer_get_ctl_by_name(mixer, ctl8);
1707     mixer_ctl_set_enum_by_string(ctl, setting8);
1708 }
1709 
platform_init(struct audio_device * adev)1710 void *platform_init(struct audio_device *adev)
1711 {
1712     char value[PROPERTY_VALUE_MAX];
1713     struct platform_data *my_data = NULL;
1714     int retry_num = 0, snd_card_num = 0, key = 0, ret = 0;
1715     bool dual_mic_config = false, use_default_mixer_path = true;
1716     const char *snd_card_name;
1717     char *cvd_version = NULL;
1718     char *snd_internal_name = NULL;
1719     char *tmp = NULL;
1720     char mixer_xml_file[MIXER_PATH_MAX_LENGTH]= {0};
1721     char platform_info_file[MIXER_PATH_MAX_LENGTH]= {0};
1722     struct snd_card_split *snd_split_handle = NULL;
1723     my_data = calloc(1, sizeof(struct platform_data));
1724 
1725     my_data->adev = adev;
1726 
1727     list_init(&operator_info_list);
1728     list_init(&app_type_entry_list);
1729 
1730     set_platform_defaults(my_data);
1731 
1732     // audio_extn_utils_get_snd_card_num does
1733     // - open mixer and get snd card name
1734     // - parse platform info xml file and check for valid snd card name
1735     // - on failure loop through all the active snd card
1736 
1737     snd_card_num = audio_extn_utils_get_snd_card_num();
1738     if (-1 == snd_card_num) {
1739         ALOGE("%s: invalid sound card number (-1), bailing out ", __func__);
1740         goto init_failed;
1741     }
1742 
1743     adev->mixer = mixer_open(snd_card_num);
1744     snd_card_name = mixer_get_name(adev->mixer);
1745     my_data->hw_info = hw_info_init(snd_card_name);
1746 
1747     audio_extn_set_snd_card_split(snd_card_name);
1748     snd_split_handle = audio_extn_get_snd_card_split();
1749 
1750     /* Get the codec internal name from the sound card and/or form factor
1751      * name and form the mixer paths and platfor info file name dynamically.
1752      * This is generic way of picking any codec and forma factor name based
1753      * mixer and platform info files in future with no code change.
1754 
1755      * current code extends and looks for any of the exteneded mixer path and
1756      * platform info file present based on codec and form factor.
1757 
1758      * order of picking appropriate file is
1759      * <i>   mixer_paths_<codec_name>_<form_factor>.xml, if file not present
1760      * <ii>  mixer_paths_<codec_name>.xml, if file not present
1761      * <iii> mixer_paths.xml
1762 
1763      * same order is followed for audio_platform_info.xml as well
1764      */
1765 
1766     // need to carryforward old file name
1767     if (!strncmp(snd_card_name, TOMTOM_8226_SND_CARD_NAME,
1768                  min(strlen(TOMTOM_8226_SND_CARD_NAME), strlen(snd_card_name)))) {
1769         snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s.xml",
1770                          MIXER_XML_BASE_STRING, TOMTOM_MIXER_FILE_SUFFIX );
1771     } else {
1772 
1773         snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s_%s.xml",
1774                          MIXER_XML_BASE_STRING, snd_split_handle->snd_card,
1775                          snd_split_handle->form_factor);
1776         if (!audio_extn_utils_resolve_config_file(mixer_xml_file)) {
1777             memset(mixer_xml_file, 0, sizeof(mixer_xml_file));
1778             snprintf(mixer_xml_file, sizeof(mixer_xml_file), "%s_%s.xml",
1779                          MIXER_XML_BASE_STRING, snd_split_handle->snd_card);
1780 
1781             if (!audio_extn_utils_resolve_config_file(mixer_xml_file)) {
1782                 memset(mixer_xml_file, 0, sizeof(mixer_xml_file));
1783                 strlcpy(mixer_xml_file, MIXER_XML_DEFAULT_PATH, MIXER_PATH_MAX_LENGTH);
1784                 audio_extn_utils_resolve_config_file(mixer_xml_file);
1785             }
1786         }
1787     }
1788 
1789     audio_extn_utils_get_platform_info(snd_card_name, platform_info_file);
1790 
1791     my_data->declared_mic_count = 0;
1792     /* Initialize platform specific ids and/or backends*/
1793     platform_info_init(platform_info_file, my_data,
1794                        true, &platform_set_parameters);
1795 
1796     ALOGD("%s: Loading mixer file: %s", __func__, mixer_xml_file);
1797     adev->audio_route = audio_route_init(snd_card_num, mixer_xml_file);
1798 
1799     if (!adev->audio_route) {
1800         ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
1801         mixer_close(adev->mixer);
1802         adev->mixer = NULL;
1803         hw_info_deinit(my_data->hw_info);
1804         my_data->hw_info = NULL;
1805         goto init_failed;
1806     }
1807     adev->snd_card = snd_card_num;
1808     ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
1809 
1810     //set max volume step for voice call
1811     property_get("ro.config.vc_call_vol_steps", value, TOSTRING(MAX_VOL_INDEX));
1812     my_data->max_vol_index = atoi(value);
1813 
1814     property_get("persist.audio.dualmic.config",value,"");
1815     if (!strcmp("endfire", value)) {
1816         dual_mic_config = true;
1817     }
1818 
1819     my_data->source_mic_type = 0;
1820 
1821     my_data->fluence_in_spkr_mode = false;
1822     my_data->fluence_in_voice_call = false;
1823     my_data->fluence_in_voice_comm = false;
1824     my_data->fluence_in_voice_rec = false;
1825 
1826     if (property_get("ro.vendor.audio.sdk.fluencetype", value, NULL) == 0) {
1827         property_get("ro.qc.sdk.audio.fluencetype", value, "none");
1828     }
1829     if (!strcmp("fluencepro", value)) {
1830         my_data->fluence_type = FLUENCE_PRO_ENABLE;
1831     } else if (!strcmp("fluence", value) || (dual_mic_config)) {
1832         my_data->fluence_type = FLUENCE_ENABLE;
1833     } else if (!strcmp("none", value)) {
1834         my_data->fluence_type = FLUENCE_DISABLE;
1835     }
1836 
1837     if (my_data->fluence_type != FLUENCE_DISABLE) {
1838         property_get("persist.audio.fluence.voicecall",value,"");
1839         if (!strcmp("true", value)) {
1840             my_data->fluence_in_voice_call = true;
1841         }
1842 
1843         property_get("persist.audio.fluence.voicecomm",value,"");
1844         if (!strcmp("true", value)) {
1845             my_data->fluence_in_voice_comm = true;
1846         }
1847 
1848         property_get("persist.audio.fluence.voicerec",value,"");
1849         if (!strcmp("true", value)) {
1850             my_data->fluence_in_voice_rec = true;
1851         }
1852 
1853         property_get("persist.audio.fluence.speaker",value,"");
1854         if (!strcmp("true", value)) {
1855             my_data->fluence_in_spkr_mode = true;
1856         }
1857     }
1858 
1859     // support max to mono, example if max count is 3, usecase supports Three, dual and mono mic
1860     switch (my_data->max_mic_count) {
1861         case 4:
1862             my_data->source_mic_type |= SOURCE_QUAD_MIC;
1863         case 3:
1864             my_data->source_mic_type |= SOURCE_THREE_MIC;
1865         case 2:
1866             my_data->source_mic_type |= SOURCE_DUAL_MIC;
1867         case 1:
1868             my_data->source_mic_type |= SOURCE_MONO_MIC;
1869             break;
1870         default:
1871             ALOGE("%s: max_mic_count (%d), is not supported, setting to default",
1872                    __func__, my_data->max_mic_count);
1873             my_data->source_mic_type = SOURCE_MONO_MIC|SOURCE_DUAL_MIC;
1874             break;
1875         }
1876 
1877     ALOGV("%s: Fluence_Type(%d) max_mic_count(%d) mic_type(0x%x) fluence_in_voice_call(%d)"
1878           " fluence_in_voice_comm(%d) fluence_in_voice_rec(%d) fluence_in_spkr_mode(%d) ",
1879           __func__, my_data->fluence_type, my_data->max_mic_count, my_data->source_mic_type,
1880           my_data->fluence_in_voice_call, my_data->fluence_in_voice_comm,
1881           my_data->fluence_in_voice_rec, my_data->fluence_in_spkr_mode);
1882 
1883     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
1884     if (my_data->acdb_handle == NULL) {
1885         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
1886     } else {
1887         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
1888         my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
1889                                                     "acdb_loader_deallocate_ACDB");
1890         if (!my_data->acdb_deallocate)
1891             ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
1892                   __func__, LIB_ACDB_LOADER);
1893 
1894         my_data->acdb_send_audio_cal_v3 = (acdb_send_audio_cal_v3_t)dlsym(my_data->acdb_handle,
1895                                                     "acdb_loader_send_audio_cal_v3");
1896         if (!my_data->acdb_send_audio_cal_v3)
1897             ALOGE("%s: Could not find the symbol acdb_send_audio_cal_v3 from %s",
1898                   __func__, LIB_ACDB_LOADER);
1899 
1900         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
1901                                                     "acdb_loader_send_audio_cal");
1902         if (!my_data->acdb_send_audio_cal)
1903             ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
1904                   __func__, LIB_ACDB_LOADER);
1905 
1906         my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
1907                                                     "acdb_loader_send_voice_cal");
1908         if (!my_data->acdb_send_voice_cal)
1909             ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
1910                   __func__, LIB_ACDB_LOADER);
1911 
1912         my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
1913                                                     "acdb_loader_reload_vocvoltable");
1914         if (!my_data->acdb_reload_vocvoltable)
1915             ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
1916                   __func__, LIB_ACDB_LOADER);
1917 
1918         my_data->acdb_send_gain_dep_cal = (acdb_send_gain_dep_cal_t)dlsym(my_data->acdb_handle,
1919                                                     "acdb_loader_send_gain_dep_cal");
1920         if (!my_data->acdb_send_gain_dep_cal)
1921             ALOGV("%s: Could not find the symbol acdb_loader_send_gain_dep_cal from %s",
1922                   __func__, LIB_ACDB_LOADER);
1923 
1924 #if defined (FLICKER_SENSOR_INPUT)
1925         configure_flicker_sensor_input(adev->mixer);
1926 #endif
1927 
1928 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
1929         acdb_init_v2_cvd_t acdb_init_local;
1930         acdb_init_local = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
1931                                               "acdb_loader_init_v2");
1932         if (acdb_init_local == NULL)
1933             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
1934                   dlerror());
1935 
1936 #elif defined (PLATFORM_MSM8084)
1937         acdb_init_v2_t acdb_init_local;
1938         acdb_init_local = (acdb_init_v2_t)dlsym(my_data->acdb_handle,
1939                                           "acdb_loader_init_v2");
1940         if (acdb_init_local == NULL)
1941             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
1942                   dlerror());
1943 
1944 #else
1945         acdb_init_t acdb_init_local;
1946         acdb_init_local = (acdb_init_t)dlsym(my_data->acdb_handle,
1947                                                     "acdb_loader_init_ACDB");
1948         if (acdb_init_local == NULL)
1949             ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__,
1950                   dlerror());
1951 #endif
1952         my_data->acdb_init = acdb_init_local;
1953 
1954         my_data->acdb_send_custom_top = (acdb_send_custom_top_t)
1955                                         dlsym(my_data->acdb_handle,
1956                                               "acdb_loader_send_common_custom_topology");
1957 
1958         if (!my_data->acdb_send_custom_top)
1959             ALOGE("%s: Could not find the symbol acdb_get_default_app_type from %s",
1960                   __func__, LIB_ACDB_LOADER);
1961 
1962         my_data->acdb_set_audio_cal = (acdb_set_audio_cal_t)dlsym(my_data->acdb_handle,
1963                                                     "acdb_loader_set_audio_cal_v2");
1964         if (!my_data->acdb_set_audio_cal)
1965             ALOGE("%s: Could not find the symbol acdb_set_audio_cal_v2 from %s",
1966                   __func__, LIB_ACDB_LOADER);
1967 
1968         int result = acdb_init(adev->snd_card);
1969         if (!result) {
1970             my_data->acdb_initialized = true;
1971             ALOGD("ACDB initialized");
1972         } else {
1973             my_data->acdb_initialized = false;
1974             ALOGD("ACDB initialization failed");
1975         }
1976     }
1977 
1978     /* init usb */
1979     audio_extn_usb_init(adev);
1980 
1981     /* init a2dp */
1982     audio_extn_a2dp_init(adev);
1983 
1984     audio_extn_spkr_prot_init(adev);
1985 
1986     audio_extn_hwdep_cal_send(adev->snd_card, my_data->acdb_handle);
1987 
1988     /* load csd client */
1989     platform_csd_init(my_data);
1990 
1991     platform_backend_config_init(my_data);
1992 
1993     init_be_dai_name_table(adev);
1994 
1995     if (platform_supports_app_type_cfg())
1996         platform_backend_app_type_cfg_init(my_data, adev->mixer);
1997 
1998     return my_data;
1999 
2000 init_failed:
2001     if (my_data)
2002         free(my_data);
2003     return NULL;
2004 }
2005 
platform_deinit(void * platform)2006 void platform_deinit(void *platform)
2007 {
2008     int32_t dev;
2009     struct operator_info *info_item;
2010     struct operator_specific_device *device_item;
2011     struct external_specific_device *ext_dev;
2012     struct app_type_entry *ap;
2013     struct listnode *node;
2014 
2015     struct platform_data *my_data = (struct platform_data *)platform;
2016     close_csd_client(my_data->csd);
2017 
2018     audio_extn_spkr_prot_deinit(my_data->adev);
2019 
2020     hw_info_deinit(my_data->hw_info);
2021 
2022     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
2023         if (backend_tag_table[dev])
2024             free(backend_tag_table[dev]);
2025         if (hw_interface_table[dev])
2026             free(hw_interface_table[dev]);
2027         if (operator_specific_device_table[dev]) {
2028             while (!list_empty(operator_specific_device_table[dev])) {
2029                 node = list_head(operator_specific_device_table[dev]);
2030                 list_remove(node);
2031                 device_item = node_to_item(node, struct operator_specific_device, list);
2032                 free(device_item->operator);
2033                 free(device_item->mixer_path);
2034                 free(device_item);
2035             }
2036             free(operator_specific_device_table[dev]);
2037         }
2038 
2039         if (external_specific_device_table[dev]) {
2040             while (!list_empty(external_specific_device_table[dev])) {
2041                 node = list_head(external_specific_device_table[dev]);
2042                 list_remove(node);
2043                 ext_dev = node_to_item(node, struct external_specific_device, list);
2044                 free(ext_dev->usbid);
2045                 free(ext_dev);
2046             }
2047             free(external_specific_device_table[dev]);
2048         }
2049     }
2050 
2051     if (my_data->snd_card_name)
2052         free(my_data->snd_card_name);
2053 
2054     while (!list_empty(&operator_info_list)) {
2055         node = list_head(&operator_info_list);
2056         list_remove(node);
2057         info_item = node_to_item(node, struct operator_info, list);
2058         free(info_item->name);
2059         free(info_item->mccmnc);
2060         free(info_item);
2061     }
2062 
2063     while (!list_empty(&app_type_entry_list)) {
2064         node = list_head(&app_type_entry_list);
2065         list_remove(node);
2066         ap = node_to_item(node, struct app_type_entry, node);
2067         if (ap->mode) free(ap->mode);
2068         free(ap);
2069     }
2070 
2071     mixer_close(my_data->adev->mixer);
2072     free(platform);
2073 
2074     /* deinit usb */
2075     audio_extn_usb_deinit();
2076 }
2077 
platform_get_snd_device_name(snd_device_t snd_device)2078 const char *platform_get_snd_device_name(snd_device_t snd_device)
2079 {
2080     if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
2081         if (operator_specific_device_table[snd_device] != NULL) {
2082             return get_operator_specific_device_mixer_path(snd_device);
2083         }
2084         return device_table[snd_device];
2085     } else
2086         return "none";
2087 }
2088 
platform_get_snd_device_name_extn(void * platform,snd_device_t snd_device,char * device_name)2089 int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
2090                                       char *device_name)
2091 {
2092     struct platform_data *my_data = (struct platform_data *)platform;
2093 
2094     if (platform == NULL) {
2095         ALOGW("%s: something wrong, use legacy get_snd_device name", __func__);
2096         strlcpy(device_name, platform_get_snd_device_name(snd_device),
2097                 DEVICE_NAME_MAX_SIZE);
2098     } else if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
2099         if (operator_specific_device_table[snd_device] != NULL) {
2100             strlcpy(device_name, get_operator_specific_device_mixer_path(snd_device),
2101                     DEVICE_NAME_MAX_SIZE);
2102         } else {
2103             strlcpy(device_name, device_table[snd_device], DEVICE_NAME_MAX_SIZE);
2104         }
2105         hw_info_append_hw_type(my_data->hw_info, snd_device, device_name);
2106     } else {
2107         strlcpy(device_name, "none", DEVICE_NAME_MAX_SIZE);
2108         return -EINVAL;
2109     }
2110 
2111     return 0;
2112 }
2113 
platform_add_backend_name(void * platform,char * mixer_path,snd_device_t snd_device)2114 void platform_add_backend_name(void *platform, char *mixer_path,
2115                                snd_device_t snd_device)
2116 {
2117     struct platform_data *my_data = (struct platform_data *)platform;
2118 
2119     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2120         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
2121         return;
2122     }
2123 
2124     const char * suffix = backend_tag_table[snd_device];
2125 
2126     if (suffix != NULL) {
2127         strcat(mixer_path, " ");
2128         strcat(mixer_path, suffix);
2129     }
2130 }
2131 
platform_check_backends_match(snd_device_t snd_device1,snd_device_t snd_device2)2132 bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
2133 {
2134     ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
2135                 platform_get_snd_device_name(snd_device1),
2136                 platform_get_snd_device_name(snd_device2));
2137 
2138     if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
2139         ALOGE("%s: Invalid snd_device = %s", __func__,
2140                 platform_get_snd_device_name(snd_device1));
2141         return false;
2142     }
2143     if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
2144         ALOGE("%s: Invalid snd_device = %s", __func__,
2145                 platform_get_snd_device_name(snd_device2));
2146         return false;
2147     }
2148 
2149     const char * be_itf1 = hw_interface_table[snd_device1];
2150     const char * be_itf2 = hw_interface_table[snd_device2];
2151     /*
2152       hw_interface_table has overrides for a snd_device.
2153       if there is no entry for a device, assume DEFAULT_RX_BACKEND
2154     */
2155     if (be_itf1 == NULL) {
2156         be_itf1 = DEFAULT_RX_BACKEND;
2157     }
2158     if (be_itf2 == NULL) {
2159         be_itf2 = DEFAULT_RX_BACKEND;
2160     }
2161     ALOGV("%s: be_itf1 = %s, be_itf2 = %s", __func__, be_itf1, be_itf2);
2162     /*
2163       this takes care of finding a device within a combo device pair as well
2164      */
2165     return strstr(be_itf1, be_itf2) != NULL || strstr(be_itf2, be_itf1) != NULL;
2166 }
2167 
platform_get_pcm_device_id(audio_usecase_t usecase,int device_type)2168 int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
2169 {
2170     int device_id;
2171     if (device_type == PCM_PLAYBACK)
2172         device_id = pcm_device_table[usecase][0];
2173     else
2174         device_id = pcm_device_table[usecase][1];
2175     return device_id;
2176 }
2177 
platform_get_haptics_pcm_device_id()2178 int platform_get_haptics_pcm_device_id()
2179 {
2180     return HAPTICS_PCM_DEVICE;
2181 }
2182 
find_index(const struct name_to_index * table,int32_t len,const char * name)2183 static int find_index(const struct name_to_index * table, int32_t len,
2184                       const char * name)
2185 {
2186     int ret = 0;
2187     int32_t i;
2188 
2189     if (table == NULL) {
2190         ALOGE("%s: table is NULL", __func__);
2191         ret = -ENODEV;
2192         goto done;
2193     }
2194 
2195     if (name == NULL) {
2196         ALOGE("null key");
2197         ret = -ENODEV;
2198         goto done;
2199     }
2200 
2201     for (i=0; i < len; i++) {
2202         if (!strcmp(table[i].name, name)) {
2203             ret = table[i].index;
2204             goto done;
2205         }
2206     }
2207     ALOGE("%s: Could not find index for name = %s",
2208             __func__, name);
2209     ret = -ENODEV;
2210 done:
2211     return ret;
2212 }
2213 
platform_get_snd_device_index(char * device_name)2214 int platform_get_snd_device_index(char *device_name)
2215 {
2216     return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
2217 }
2218 
platform_get_usecase_index(const char * usecase_name)2219 int platform_get_usecase_index(const char *usecase_name)
2220 {
2221     return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
2222 }
2223 
platform_get_effect_config_data(snd_device_t snd_device,struct audio_effect_config * effect_config,effect_type_t effect_type)2224 int platform_get_effect_config_data(snd_device_t snd_device,
2225                                       struct audio_effect_config *effect_config,
2226                                       effect_type_t effect_type)
2227 {
2228     int ret = 0;
2229 
2230     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
2231         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
2232         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
2233             __func__, snd_device, effect_type);
2234         ret = -EINVAL;
2235         goto done;
2236     }
2237 
2238     if (effect_config == NULL) {
2239         ALOGE("%s: Invalid effect_config", __func__);
2240         ret = -EINVAL;
2241         goto done;
2242     }
2243 
2244     ALOGV("%s: snd_device = %d module_id = %d",
2245             __func__, snd_device, effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type].module_id);
2246     *effect_config = effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type];
2247 
2248 done:
2249     return ret;
2250 }
2251 
platform_set_effect_config_data(snd_device_t snd_device,struct audio_effect_config effect_config,effect_type_t effect_type)2252 int platform_set_effect_config_data(snd_device_t snd_device,
2253                                       struct audio_effect_config effect_config,
2254                                       effect_type_t effect_type)
2255 {
2256     int ret = 0;
2257 
2258     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
2259         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
2260         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
2261             __func__, snd_device, effect_type);
2262         ret = -EINVAL;
2263         goto done;
2264     }
2265 
2266     ALOGV("%s 0x%x 0x%x 0x%x 0x%x", __func__, effect_config.module_id,
2267            effect_config.instance_id, effect_config.param_id,
2268            effect_config.param_value);
2269     effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type] = effect_config;
2270 
2271 done:
2272     return ret;
2273 }
2274 
platform_get_audio_source_index(const char * audio_source_name)2275 int platform_get_audio_source_index(const char *audio_source_name)
2276 {
2277     return find_index(audio_source_index, AUDIO_SOURCE_CNT, audio_source_name);
2278 }
2279 
platform_add_operator_specific_device(snd_device_t snd_device,const char * operator,const char * mixer_path,unsigned int acdb_id)2280 void platform_add_operator_specific_device(snd_device_t snd_device,
2281                                            const char *operator,
2282                                            const char *mixer_path,
2283                                            unsigned int acdb_id)
2284 {
2285     struct operator_specific_device *device;
2286 
2287     if (operator_specific_device_table[snd_device] == NULL) {
2288         operator_specific_device_table[snd_device] =
2289             (struct listnode *)calloc(1, sizeof(struct listnode));
2290         list_init(operator_specific_device_table[snd_device]);
2291     }
2292 
2293     device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
2294 
2295     device->operator = strdup(operator);
2296     device->mixer_path = strdup(mixer_path);
2297     device->acdb_id = acdb_id;
2298 
2299     list_add_tail(operator_specific_device_table[snd_device], &device->list);
2300 
2301     ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
2302             platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
2303 
2304 }
2305 
platform_add_external_specific_device(snd_device_t snd_device,const char * usbid,unsigned int acdb_id)2306 void platform_add_external_specific_device(snd_device_t snd_device,
2307                                            const char *usbid,
2308                                            unsigned int acdb_id)
2309 {
2310     struct external_specific_device *device;
2311 
2312     if (external_specific_device_table[snd_device] == NULL) {
2313         external_specific_device_table[snd_device] =
2314             (struct listnode *)calloc(1, sizeof(struct listnode));
2315         list_init(external_specific_device_table[snd_device]);
2316     }
2317 
2318     device = (struct external_specific_device *)calloc(1, sizeof(struct external_specific_device));
2319 
2320     device->usbid = strdup(usbid);
2321     device->acdb_id = acdb_id;
2322 
2323     list_add_tail(external_specific_device_table[snd_device], &device->list);
2324 
2325     ALOGD("%s: device[%s] usbid[%s] -> acdb_id[%d]", __func__,
2326             platform_get_snd_device_name(snd_device), usbid, acdb_id);
2327 }
2328 
2329 
platform_set_snd_device_acdb_id(snd_device_t snd_device,unsigned int acdb_id)2330 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
2331 {
2332     int ret = 0;
2333 
2334     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2335         ALOGE("%s: Invalid snd_device = %d",
2336             __func__, snd_device);
2337         ret = -EINVAL;
2338         goto done;
2339     }
2340 
2341     ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
2342           platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
2343     acdb_device_table[snd_device] = acdb_id;
2344 done:
2345     return ret;
2346 }
2347 
platform_get_snd_device_acdb_id(snd_device_t snd_device)2348 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
2349 {
2350     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2351         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
2352         return -EINVAL;
2353     }
2354 
2355     /*
2356      * If speaker protection is enabled, function returns supported
2357      * sound device for speaker. Else same sound device is returned.
2358      */
2359     snd_device = audio_extn_get_spkr_prot_snd_device(snd_device);
2360 
2361     if (operator_specific_device_table[snd_device] != NULL)
2362         return get_operator_specific_device_acdb_id(snd_device);
2363     else if (external_specific_device_table[snd_device] != NULL)
2364         return get_external_specific_device_acdb_id(snd_device);
2365     else
2366         return acdb_device_table[snd_device];
2367 }
2368 
platform_get_backend_index(snd_device_t snd_device)2369 static int platform_get_backend_index(snd_device_t snd_device)
2370 {
2371     int32_t port = DEFAULT_CODEC_BACKEND;
2372 
2373     if (snd_device >= SND_DEVICE_OUT_BEGIN && snd_device < SND_DEVICE_OUT_END) {
2374         if (backend_tag_table[snd_device] != NULL) {
2375                 if (strncmp(backend_tag_table[snd_device], "headphones",
2376                             sizeof("headphones")) == 0)
2377                         port = HEADPHONE_BACKEND;
2378                 else if (strcmp(backend_tag_table[snd_device], "hdmi") == 0)
2379                         port = HDMI_RX_BACKEND;
2380                 else if ((strcmp(backend_tag_table[snd_device], "usb-headphones") == 0) ||
2381                            (strcmp(backend_tag_table[snd_device], "usb-headset") == 0))
2382                         port = USB_AUDIO_RX_BACKEND;
2383         }
2384     } else if (snd_device >= SND_DEVICE_IN_BEGIN && snd_device < SND_DEVICE_IN_END) {
2385         port = DEFAULT_CODEC_TX_BACKEND;
2386         if (backend_tag_table[snd_device] != NULL) {
2387                 if (strcmp(backend_tag_table[snd_device], "usb-headset-mic") == 0)
2388                         port = USB_AUDIO_TX_BACKEND;
2389                 else if (strstr(backend_tag_table[snd_device], "bt-sco") != NULL)
2390                         port = BT_SCO_TX_BACKEND;
2391         }
2392     } else {
2393         ALOGW("%s:napb: Invalid device - %d ", __func__, snd_device);
2394     }
2395 
2396     ALOGV("%s:napb: backend port - %d device - %d ", __func__, port, snd_device);
2397 
2398     return port;
2399 }
2400 
platform_send_audio_calibration(void * platform,snd_device_t snd_device)2401 int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
2402 {
2403     struct platform_data *my_data = (struct platform_data *)platform;
2404     int acdb_dev_id, acdb_dev_type;
2405 
2406     if (platform_supports_app_type_cfg()) // use v2 instead
2407         return -ENOSYS;
2408 
2409     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
2410     if (acdb_dev_id < 0) {
2411         ALOGE("%s: Could not find acdb id for device(%d)",
2412               __func__, snd_device);
2413         return -EINVAL;
2414     }
2415     if (my_data->acdb_send_audio_cal) {
2416         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
2417               __func__, snd_device, acdb_dev_id);
2418         if (snd_device >= SND_DEVICE_OUT_BEGIN &&
2419                 snd_device < SND_DEVICE_OUT_END)
2420             acdb_dev_type = ACDB_DEV_TYPE_OUT;
2421         else
2422             acdb_dev_type = ACDB_DEV_TYPE_IN;
2423         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
2424     }
2425     return 0;
2426 }
2427 
platform_send_audio_calibration_v2(void * platform,struct audio_usecase * usecase,int app_type,int sample_rate)2428 int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
2429                                        int app_type, int sample_rate)
2430 {
2431     struct platform_data *my_data = (struct platform_data *)platform;
2432     int acdb_dev_id, acdb_dev_type;
2433     int snd_device = usecase->out_snd_device;
2434     int new_snd_device[SND_DEVICE_OUT_END] = {0};
2435     int i, num_devices = 1;
2436 
2437     if (!platform_supports_app_type_cfg()) // use v1 instead
2438         return -ENOSYS;
2439 
2440     if ((usecase->type == PCM_HFP_CALL) || (usecase->type == PCM_CAPTURE))
2441         snd_device = usecase->in_snd_device;
2442 
2443     // skipped over get_spkr_prot_device
2444     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
2445     if (acdb_dev_id < 0) {
2446         ALOGE("%s: Could not find acdb id for device(%d)",
2447               __func__, snd_device);
2448         return -EINVAL;
2449     }
2450 
2451     if (platform_can_split_snd_device(snd_device,
2452                                       &num_devices, new_snd_device) < 0) {
2453         new_snd_device[0] = snd_device;
2454     }
2455 
2456     for (i = 0; i < num_devices; i++) {
2457         acdb_dev_id = platform_get_snd_device_acdb_id(new_snd_device[i]);
2458         if (acdb_dev_id < 0) {
2459             ALOGE("%s: Could not find acdb id for device(%d)",
2460                   __func__, new_snd_device[i]);
2461             return -EINVAL;
2462         }
2463         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
2464               __func__, new_snd_device[i], acdb_dev_id);
2465         if (new_snd_device[i] >= SND_DEVICE_OUT_BEGIN &&
2466                 new_snd_device[i] < SND_DEVICE_OUT_END)
2467             acdb_dev_type = ACDB_DEV_TYPE_OUT;
2468         else
2469             acdb_dev_type = ACDB_DEV_TYPE_IN;
2470 
2471         if (my_data->acdb_send_audio_cal_v3) {
2472             my_data->acdb_send_audio_cal_v3(acdb_dev_id, acdb_dev_type,
2473                                             app_type, sample_rate, i);
2474         } else if (my_data->acdb_send_audio_cal) {
2475             my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type); // this version differs from internal
2476         }
2477     }
2478 
2479     return 0;
2480 }
2481 
2482 
platform_switch_voice_call_device_pre(void * platform)2483 int platform_switch_voice_call_device_pre(void *platform)
2484 {
2485     struct platform_data *my_data = (struct platform_data *)platform;
2486     int ret = 0;
2487 
2488     if (my_data->csd != NULL &&
2489         voice_is_in_call(my_data->adev)) {
2490         /* This must be called before disabling mixer controls on APQ side */
2491         ret = my_data->csd->disable_device();
2492         if (ret < 0) {
2493             ALOGE("%s: csd_client_disable_device, failed, error %d",
2494                   __func__, ret);
2495         }
2496     }
2497     return ret;
2498 }
2499 
platform_switch_voice_call_enable_device_config(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2500 int platform_switch_voice_call_enable_device_config(void *platform,
2501                                                     snd_device_t out_snd_device,
2502                                                     snd_device_t in_snd_device)
2503 {
2504     struct platform_data *my_data = (struct platform_data *)platform;
2505     int acdb_rx_id, acdb_tx_id;
2506     int ret = 0;
2507 
2508     if (my_data->csd == NULL)
2509         return ret;
2510 
2511     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2512     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2513 
2514     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
2515         ret = my_data->csd->enable_device_config(acdb_rx_id, acdb_tx_id);
2516         if (ret < 0) {
2517             ALOGE("%s: csd_enable_device_config, failed, error %d",
2518                   __func__, ret);
2519         }
2520     } else {
2521         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2522               acdb_rx_id, acdb_tx_id);
2523     }
2524 
2525     return ret;
2526 }
2527 
platform_switch_voice_call_device_post(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2528 int platform_switch_voice_call_device_post(void *platform,
2529                                            snd_device_t out_snd_device,
2530                                            snd_device_t in_snd_device)
2531 {
2532     struct platform_data *my_data = (struct platform_data *)platform;
2533     int acdb_rx_id, acdb_tx_id;
2534 
2535     if (my_data->acdb_send_voice_cal == NULL) {
2536         ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
2537     } else {
2538         acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2539         acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2540 
2541         if (acdb_rx_id > 0 && acdb_tx_id > 0)
2542             my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
2543         else
2544             ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2545                   acdb_rx_id, acdb_tx_id);
2546     }
2547 
2548     return 0;
2549 }
2550 
platform_switch_voice_call_usecase_route_post(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2551 int platform_switch_voice_call_usecase_route_post(void *platform,
2552                                                   snd_device_t out_snd_device,
2553                                                   snd_device_t in_snd_device)
2554 {
2555     struct platform_data *my_data = (struct platform_data *)platform;
2556     int acdb_rx_id, acdb_tx_id;
2557     int ret = 0;
2558 
2559     if (my_data->csd == NULL)
2560         return ret;
2561 
2562     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2563     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2564 
2565     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
2566         ret = my_data->csd->enable_device(acdb_rx_id, acdb_tx_id,
2567                                           my_data->adev->acdb_settings);
2568         if (ret < 0) {
2569             ALOGE("%s: csd_enable_device, failed, error %d", __func__, ret);
2570         }
2571     } else {
2572         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2573               acdb_rx_id, acdb_tx_id);
2574     }
2575 
2576     return ret;
2577 }
2578 
platform_start_voice_call(void * platform,uint32_t vsid)2579 int platform_start_voice_call(void *platform, uint32_t vsid)
2580 {
2581     struct platform_data *my_data = (struct platform_data *)platform;
2582     int ret = 0;
2583 
2584     if (my_data->csd != NULL) {
2585         ret = my_data->csd->start_voice(vsid);
2586         if (ret < 0) {
2587             ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
2588         }
2589     }
2590     return ret;
2591 }
2592 
platform_stop_voice_call(void * platform,uint32_t vsid)2593 int platform_stop_voice_call(void *platform, uint32_t vsid)
2594 {
2595     struct platform_data *my_data = (struct platform_data *)platform;
2596     int ret = 0;
2597 
2598     if (my_data->csd != NULL) {
2599         ret = my_data->csd->stop_voice(vsid);
2600         if (ret < 0) {
2601             ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
2602         }
2603     }
2604     return ret;
2605 }
2606 
platform_set_mic_break_det(void * platform,bool enable)2607 int platform_set_mic_break_det(void *platform, bool enable)
2608 {
2609     int ret = 0;
2610     struct platform_data *my_data = (struct platform_data *)platform;
2611     struct audio_device *adev = my_data->adev;
2612     const char *mixer_ctl_name = "Voice Mic Break Enable";
2613     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2614     if (!ctl) {
2615         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2616               __func__, mixer_ctl_name);
2617         return -EINVAL;
2618     }
2619 
2620     ret = mixer_ctl_set_value(ctl, 0, enable);
2621     if(ret)
2622         ALOGE("%s: Failed to set mixer ctl: %s", __func__, mixer_ctl_name);
2623 
2624     return ret;
2625 }
2626 
platform_get_sample_rate(void * platform,uint32_t * rate)2627 int platform_get_sample_rate(void *platform, uint32_t *rate)
2628 {
2629     struct platform_data *my_data = (struct platform_data *)platform;
2630     int ret = 0;
2631 
2632     if (my_data->csd != NULL) {
2633         ret = my_data->csd->get_sample_rate(rate);
2634         if (ret < 0) {
2635             ALOGE("%s: csd_get_sample_rate error %d\n", __func__, ret);
2636         }
2637     }
2638     return ret;
2639 }
2640 
platform_set_speaker_gain_in_combo(struct audio_device * adev,snd_device_t snd_device,bool enable)2641 void platform_set_speaker_gain_in_combo(struct audio_device *adev,
2642                                         snd_device_t snd_device,
2643                                         bool enable)
2644 {
2645     const char* name;
2646     switch (snd_device) {
2647         case SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES:
2648             if (enable)
2649                 name = "spkr-gain-in-headphone-combo";
2650             else
2651                 name = "speaker-gain-default";
2652             break;
2653         case SND_DEVICE_OUT_SPEAKER_AND_LINE:
2654             if (enable)
2655                 name = "spkr-gain-in-line-combo";
2656             else
2657                 name = "speaker-gain-default";
2658             break;
2659         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES:
2660             if (enable)
2661                 name = "spkr-safe-gain-in-headphone-combo";
2662             else
2663                 name = "speaker-safe-gain-default";
2664             break;
2665         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE:
2666             if (enable)
2667                 name = "spkr-safe-gain-in-line-combo";
2668             else
2669                 name = "speaker-safe-gain-default";
2670             break;
2671         default:
2672             return;
2673     }
2674 
2675     audio_route_apply_and_update_path(adev->audio_route, name);
2676 }
2677 
platform_set_voice_volume(void * platform,int volume)2678 int platform_set_voice_volume(void *platform, int volume)
2679 {
2680     struct platform_data *my_data = (struct platform_data *)platform;
2681     struct audio_device *adev = my_data->adev;
2682     struct mixer_ctl *ctl;
2683     const char *mixer_ctl_name = "Voice Rx Gain";
2684     const char *mute_mixer_ctl_name = "Voice Rx Device Mute";
2685     int vol_index = 0, ret = 0;
2686     uint32_t set_values[ ] = {0,
2687                               ALL_SESSION_VSID,
2688                               DEFAULT_VOLUME_RAMP_DURATION_MS};
2689 
2690     // Voice volume levels are mapped to adsp volume levels as follows.
2691     // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1  0 -> 0
2692     // But this values don't changed in kernel. So, below change is need.
2693     vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, my_data->max_vol_index);
2694     set_values[0] = vol_index;
2695 
2696     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2697     if (!ctl) {
2698         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2699               __func__, mixer_ctl_name);
2700         return -EINVAL;
2701     }
2702     ALOGV("Setting voice volume index: %d", set_values[0]);
2703     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2704 
2705     // Send mute command in case volume index is max since indexes are inverted
2706     // for mixer controls.
2707     if (vol_index == my_data->max_vol_index) {
2708         set_values[0] = 1;
2709     }
2710     else {
2711         set_values[0] = 0;
2712     }
2713 
2714     ctl = mixer_get_ctl_by_name(adev->mixer, mute_mixer_ctl_name);
2715     if (!ctl) {
2716         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2717               __func__, mute_mixer_ctl_name);
2718         return -EINVAL;
2719     }
2720     ALOGV("%s: Setting RX Device Mute to: %d", __func__, set_values[0]);
2721     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2722 
2723     if (my_data->csd != NULL) {
2724         ret = my_data->csd->volume(ALL_SESSION_VSID, volume,
2725                                    DEFAULT_VOLUME_RAMP_DURATION_MS);
2726         if (ret < 0) {
2727             ALOGE("%s: csd_volume error %d", __func__, ret);
2728         }
2729     }
2730     return ret;
2731 }
2732 
platform_set_mic_mute(void * platform,bool state)2733 int platform_set_mic_mute(void *platform, bool state)
2734 {
2735     struct platform_data *my_data = (struct platform_data *)platform;
2736     struct audio_device *adev = my_data->adev;
2737     struct mixer_ctl *ctl;
2738     const char *mixer_ctl_name = "Voice Tx Mute";
2739     int ret = 0;
2740     uint32_t set_values[ ] = {0,
2741                               ALL_SESSION_VSID,
2742                               DEFAULT_MUTE_RAMP_DURATION_MS};
2743 
2744     if (adev->mode != AUDIO_MODE_IN_CALL &&
2745         adev->mode != AUDIO_MODE_IN_COMMUNICATION)
2746         return 0;
2747 
2748     if (adev->enable_hfp)
2749         mixer_ctl_name = "HFP Tx Mute";
2750 
2751     set_values[0] = state;
2752     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2753     if (!ctl) {
2754         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2755               __func__, mixer_ctl_name);
2756         return -EINVAL;
2757     }
2758     ALOGV("%s: Setting voice mute state: %d", __func__, state);
2759     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2760 
2761     if (my_data->csd != NULL) {
2762         ret = my_data->csd->mic_mute(ALL_SESSION_VSID, state,
2763                                      DEFAULT_MUTE_RAMP_DURATION_MS);
2764         if (ret < 0) {
2765             ALOGE("%s: csd_mic_mute error %d", __func__, ret);
2766         }
2767     }
2768     return ret;
2769 }
2770 
platform_set_device_mute(void * platform,bool state,char * dir)2771 int platform_set_device_mute(void *platform, bool state, char *dir)
2772 {
2773     struct platform_data *my_data = (struct platform_data *)platform;
2774     struct audio_device *adev = my_data->adev;
2775     struct mixer_ctl *ctl;
2776     char *mixer_ctl_name = NULL;
2777     int ret = 0;
2778     uint32_t set_values[ ] = {0,
2779                               ALL_SESSION_VSID,
2780                               0};
2781     if(dir == NULL) {
2782         ALOGE("%s: Invalid direction:%s", __func__, dir);
2783         return -EINVAL;
2784     }
2785 
2786     if (!strncmp("rx", dir, sizeof("rx"))) {
2787         mixer_ctl_name = "Voice Rx Device Mute";
2788     } else if (!strncmp("tx", dir, sizeof("tx"))) {
2789         mixer_ctl_name = "Voice Tx Device Mute";
2790     } else {
2791         return -EINVAL;
2792     }
2793 
2794     set_values[0] = state;
2795     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2796     if (!ctl) {
2797         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2798               __func__, mixer_ctl_name);
2799         return -EINVAL;
2800     }
2801 
2802     ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
2803           __func__,state, mixer_ctl_name);
2804     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2805 
2806     return ret;
2807 }
2808 
platform_can_split_snd_device(snd_device_t snd_device,int * num_devices,snd_device_t * new_snd_devices)2809 int platform_can_split_snd_device(snd_device_t snd_device,
2810                                   int *num_devices,
2811                                   snd_device_t *new_snd_devices)
2812 {
2813     int ret = -EINVAL;
2814     if (NULL == num_devices || NULL == new_snd_devices) {
2815         ALOGE("%s: NULL pointer ..", __func__);
2816         return -EINVAL;
2817     }
2818 
2819     /*
2820      * If wired headset/headphones/line devices share the same backend
2821      * with speaker/earpiece this routine returns -EINVAL.
2822      */
2823     if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
2824         !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
2825         *num_devices = 2;
2826         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2827         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
2828         ret = 0;
2829     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
2830                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
2831         *num_devices = 2;
2832         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2833         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
2834         ret = 0;
2835     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES &&
2836                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_HEADPHONES)) {
2837         *num_devices = 2;
2838         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2839         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
2840         ret = 0;
2841     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE &&
2842                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_LINE)) {
2843         *num_devices = 2;
2844         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2845         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
2846         ret = 0;
2847     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO &&
2848                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2849                                               SND_DEVICE_OUT_BT_SCO)) {
2850         *num_devices = 2;
2851         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2852         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
2853         ret = 0;
2854     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO &&
2855                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2856                                               SND_DEVICE_OUT_BT_SCO)) {
2857         *num_devices = 2;
2858         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2859         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
2860         ret = 0;
2861     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB &&
2862                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2863                                               SND_DEVICE_OUT_BT_SCO_WB)) {
2864         *num_devices = 2;
2865         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2866         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
2867         ret = 0;
2868     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB &&
2869                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2870                                               SND_DEVICE_OUT_BT_SCO_WB)) {
2871         *num_devices = 2;
2872         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2873         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
2874         ret = 0;
2875     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET &&
2876                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_USB_HEADSET)) {
2877         *num_devices = 2;
2878         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2879         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
2880         ret = 0;
2881     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET &&
2882                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_USB_HEADSET)) {
2883         *num_devices = 2;
2884         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2885         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
2886         ret = 0;
2887     } else if (SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device &&
2888                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2889                                               SND_DEVICE_OUT_BT_A2DP)) {
2890         *num_devices = 2;
2891         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2892         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
2893         ret = 0;
2894     } else if (SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device &&
2895                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2896                                               SND_DEVICE_OUT_BT_A2DP)) {
2897         *num_devices = 2;
2898         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2899         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
2900         ret = 0;
2901     }
2902 
2903     return ret;
2904 }
2905 
platform_get_output_snd_device(void * platform,audio_devices_t devices)2906 snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
2907 {
2908     struct platform_data *my_data = (struct platform_data *)platform;
2909     struct audio_device *adev = my_data->adev;
2910     audio_mode_t mode = adev->mode;
2911     snd_device_t snd_device = SND_DEVICE_NONE;
2912 
2913     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
2914     if (devices == AUDIO_DEVICE_NONE ||
2915         devices & AUDIO_DEVICE_BIT_IN) {
2916         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
2917         goto exit;
2918     }
2919 
2920     if (popcount(devices) == 2) {
2921         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
2922                         AUDIO_DEVICE_OUT_SPEAKER) ||
2923                 devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
2924                             AUDIO_DEVICE_OUT_SPEAKER)) {
2925             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
2926         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
2927                                AUDIO_DEVICE_OUT_SPEAKER)) {
2928             snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
2929         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
2930                                AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
2931                    devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
2932                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2933             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES;
2934         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
2935                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2936             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE;
2937         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
2938                                AUDIO_DEVICE_OUT_SPEAKER)) {
2939             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
2940         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
2941                    ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER)) {
2942             snd_device = adev->bt_wb_speech_enabled ?
2943                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB :
2944                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO;
2945         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
2946                          ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2947             snd_device = adev->bt_wb_speech_enabled ?
2948                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB :
2949                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO;
2950         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
2951                                AUDIO_DEVICE_OUT_SPEAKER)) ||
2952                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
2953                                                AUDIO_DEVICE_OUT_SPEAKER))) {
2954             snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
2955         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
2956                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) ||
2957                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
2958                                                AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
2959             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET;
2960         } else if ((devices & AUDIO_DEVICE_OUT_SPEAKER) &&
2961                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
2962             snd_device = SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP;
2963         }  else if ((devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
2964                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
2965             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP;
2966         } else {
2967             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
2968             goto exit;
2969         }
2970         if (snd_device != SND_DEVICE_NONE) {
2971             goto exit;
2972         }
2973     }
2974 
2975     if (popcount(devices) != 1) {
2976         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
2977         goto exit;
2978     }
2979 
2980     if (voice_is_in_call(adev) || adev->enable_voicerx || audio_extn_hfp_is_active(adev)) {
2981         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2982             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2983             devices & AUDIO_DEVICE_OUT_LINE) {
2984             if (voice_is_in_call(adev) &&
2985                 (adev->voice.tty_mode == TTY_MODE_FULL))
2986                 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
2987             else if (voice_is_in_call(adev) &&
2988                 (adev->voice.tty_mode == TTY_MODE_VCO))
2989                 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
2990             else if (voice_is_in_call(adev) &&
2991                 (adev->voice.tty_mode == TTY_MODE_HCO))
2992                 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
2993             else {
2994                 if (devices & AUDIO_DEVICE_OUT_LINE)
2995                     snd_device = SND_DEVICE_OUT_VOICE_LINE;
2996                 else if (devices & AUDIO_DEVICE_OUT_WIRED_HEADSET)
2997                     snd_device = SND_DEVICE_OUT_VOICE_HEADSET;
2998                 else
2999                     snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
3000                 }
3001         } else if (audio_is_usb_out_device(devices)) {
3002             if (voice_is_in_call(adev)) {
3003                 switch (adev->voice.tty_mode) {
3004                     case TTY_MODE_FULL:
3005                         snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_USB;
3006                         break;
3007                     case TTY_MODE_VCO:
3008                         snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_USB;
3009                         break;
3010                     case TTY_MODE_HCO:
3011                         // since Hearing will be on handset\speaker, use existing device
3012                         snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
3013                         break;
3014                     case TTY_MODE_OFF:
3015                         break;
3016                     default:
3017                         ALOGE("%s: Invalid TTY mode (%#x)",
3018                               __func__, adev->voice.tty_mode);
3019                 }
3020             }
3021             if (snd_device == SND_DEVICE_NONE) {
3022                     snd_device = audio_extn_usb_is_capture_supported() ?
3023                                  SND_DEVICE_OUT_VOICE_USB_HEADSET :
3024                                  SND_DEVICE_OUT_VOICE_USB_HEADPHONES;
3025             }
3026         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3027             if (adev->bt_wb_speech_enabled) {
3028                 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
3029             } else {
3030                 snd_device = SND_DEVICE_OUT_BT_SCO;
3031             }
3032         } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3033             snd_device = SND_DEVICE_OUT_BT_A2DP;
3034         } else if (devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
3035             if (!adev->enable_hfp) {
3036                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
3037             } else {
3038                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_HFP;
3039             }
3040         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
3041             if(adev->voice.hac)
3042                 snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
3043             else if (is_operator_tmus())
3044                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
3045             else
3046                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
3047         } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX) {
3048             snd_device = SND_DEVICE_OUT_VOICE_TX;
3049         } else if (devices & AUDIO_DEVICE_OUT_HEARING_AID) {
3050             snd_device = SND_DEVICE_OUT_VOICE_HEARING_AID;
3051         }
3052 
3053         if (snd_device != SND_DEVICE_NONE) {
3054             goto exit;
3055         }
3056     }
3057 
3058     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3059         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3060         snd_device = SND_DEVICE_OUT_HEADPHONES;
3061     } else if (devices & AUDIO_DEVICE_OUT_LINE) {
3062         snd_device = SND_DEVICE_OUT_LINE;
3063     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3064         snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3065     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
3066         /*
3067          * Perform device switch only if acdb tuning is different between SPEAKER & SPEAKER_REVERSE,
3068          * Or there will be a small pause while performing device switch.
3069          */
3070         if (my_data->speaker_lr_swap &&
3071             (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
3072             acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]))
3073             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
3074         else
3075             snd_device = SND_DEVICE_OUT_SPEAKER;
3076     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3077         if (adev->bt_wb_speech_enabled) {
3078             snd_device = SND_DEVICE_OUT_BT_SCO_WB;
3079         } else {
3080             snd_device = SND_DEVICE_OUT_BT_SCO;
3081         }
3082     } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3083         snd_device = SND_DEVICE_OUT_BT_A2DP;
3084     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
3085         snd_device = SND_DEVICE_OUT_HDMI ;
3086     } else if (audio_is_usb_out_device(devices)) {
3087         if (audio_extn_ma_supported_usb())
3088             snd_device = SND_DEVICE_OUT_USB_HEADSET_SPEC;
3089         else if (audio_extn_usb_is_capture_supported())
3090             snd_device = SND_DEVICE_OUT_USB_HEADSET;
3091         else
3092             snd_device = SND_DEVICE_OUT_USB_HEADPHONES;
3093     }else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
3094         /*HAC support for voice-ish audio (eg visual voicemail)*/
3095         if(adev->voice.hac)
3096             snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
3097         else
3098             snd_device = SND_DEVICE_OUT_HANDSET;
3099     } else {
3100         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
3101     }
3102 exit:
3103     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
3104     return snd_device;
3105 }
3106 
3107 #ifdef DYNAMIC_ECNS_ENABLED
get_snd_device_for_voice_comm(struct platform_data * my_data,struct stream_in * in __unused,audio_devices_t out_device __unused,audio_devices_t in_device)3108 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
3109                                                   struct stream_in *in __unused,
3110                                                   audio_devices_t out_device __unused,
3111                                                   audio_devices_t in_device)
3112 {
3113     struct audio_device *adev = my_data->adev;
3114     snd_device_t snd_device = SND_DEVICE_NONE;
3115 
3116     if (my_data->fluence_type != FLUENCE_DISABLE) {
3117         switch(AUDIO_DEVICE_BIT_IN | in_device) {
3118             case AUDIO_DEVICE_IN_BACK_MIC:
3119                 if (my_data->fluence_in_spkr_mode) {
3120                     if ((my_data->fluence_type & FLUENCE_PRO_ENABLE) &&
3121                         (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3122                         snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
3123                     } else if (((my_data->fluence_type & FLUENCE_PRO_ENABLE) ||
3124                                (my_data->fluence_type & FLUENCE_ENABLE)) &&
3125                                (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3126                             snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
3127                     }
3128                     adev->acdb_settings |= DMIC_FLAG;
3129                 } else
3130                     snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
3131                 break;
3132             case AUDIO_DEVICE_IN_BUILTIN_MIC:
3133                 if (((my_data->fluence_type & FLUENCE_PRO_ENABLE) ||
3134                     (my_data->fluence_type & FLUENCE_ENABLE)) &&
3135                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3136                     snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
3137                     adev->acdb_settings |= DMIC_FLAG;
3138                 } else
3139                     snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
3140                 break;
3141             default:
3142                 ALOGE("%s: Unsupported in_device %#x", __func__, in_device);
3143                 break;
3144         }
3145         in->enable_ec_port = true;
3146     }
3147 
3148     return snd_device;
3149 }
3150 #else
get_snd_device_for_voice_comm(struct platform_data * my_data,struct stream_in * in,audio_devices_t out_device __unused,audio_devices_t in_device)3151 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
3152                                                   struct stream_in *in,
3153                                                   audio_devices_t out_device __unused,
3154                                                   audio_devices_t in_device)
3155 {
3156     struct audio_device *adev = my_data->adev;
3157     snd_device_t snd_device = SND_DEVICE_NONE;
3158 
3159     if (in->enable_aec &&
3160             in->enable_ns) {
3161         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3162             if (my_data->fluence_in_spkr_mode &&
3163                     my_data->fluence_in_voice_comm &&
3164                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3165                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
3166             } else {
3167                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
3168             }
3169         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3170             if (my_data->fluence_in_voice_comm &&
3171                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3172                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
3173             } else {
3174                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
3175             }
3176         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3177             snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
3178         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3179             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
3180         }
3181     } else if (in->enable_aec) {
3182         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3183             if (my_data->fluence_in_spkr_mode &&
3184                     my_data->fluence_in_voice_comm &&
3185                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3186                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
3187             } else {
3188                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
3189             }
3190         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3191             if (my_data->fluence_in_voice_comm &&
3192                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3193                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
3194             } else {
3195                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
3196             }
3197        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3198            snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
3199        } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3200            snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
3201        }
3202     } else if (in->enable_ns) {
3203         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3204             if (my_data->fluence_in_spkr_mode &&
3205                     my_data->fluence_in_voice_comm &&
3206                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3207                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
3208             } else {
3209                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
3210             }
3211         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3212             if (my_data->fluence_in_voice_comm &&
3213                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3214                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
3215             } else {
3216                 snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
3217             }
3218         }
3219     }
3220 
3221     return snd_device;
3222 }
3223 #endif //DYNAMIC_ECNS_ENABLED
3224 
platform_get_input_snd_device(void * platform,struct stream_in * in,audio_devices_t out_device)3225 snd_device_t platform_get_input_snd_device(void *platform,
3226                                            struct stream_in *in,
3227                                            audio_devices_t out_device)
3228 {
3229     struct platform_data *my_data = (struct platform_data *)platform;
3230     struct audio_device *adev = my_data->adev;
3231     audio_mode_t mode = adev->mode;
3232     snd_device_t snd_device = SND_DEVICE_NONE;
3233 
3234     if (in == NULL) {
3235         in = adev_get_active_input(adev);
3236     }
3237 
3238     audio_source_t source = (in == NULL) ? AUDIO_SOURCE_DEFAULT : in->source;
3239     audio_devices_t in_device =
3240         ((in == NULL) ? AUDIO_DEVICE_NONE : in->device) & ~AUDIO_DEVICE_BIT_IN;
3241     audio_channel_mask_t channel_mask = (in == NULL) ? AUDIO_CHANNEL_IN_MONO : in->channel_mask;
3242     int channel_count = audio_channel_count_from_in_mask(channel_mask);
3243 
3244     ALOGV("%s: enter: out_device(%#x) in_device(%#x) channel_count (%d) channel_mask (0x%x)",
3245           __func__, out_device, in_device, channel_count, channel_mask);
3246 
3247     if ((out_device != AUDIO_DEVICE_NONE) && (voice_is_in_call(adev) ||
3248         audio_extn_hfp_is_active(adev))) {
3249         if (adev->voice.tty_mode != TTY_MODE_OFF) {
3250             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3251                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
3252                 out_device & AUDIO_DEVICE_OUT_LINE) {
3253                 switch (adev->voice.tty_mode) {
3254                     case TTY_MODE_FULL:
3255                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
3256                         break;
3257                     case TTY_MODE_VCO:
3258                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
3259                         break;
3260                     case TTY_MODE_HCO:
3261                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
3262                         break;
3263                     default:
3264                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
3265                 }
3266                 goto exit;
3267             } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3268                 switch (adev->voice.tty_mode) {
3269                     case TTY_MODE_FULL:
3270                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC;
3271                         break;
3272                     case TTY_MODE_VCO:
3273                         // since voice will be captured from handset mic, use existing device
3274                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
3275                         break;
3276                     case TTY_MODE_HCO:
3277                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC;
3278                         break;
3279                     default:
3280                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
3281                 }
3282                 goto exit;
3283             }
3284         }
3285         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
3286             if (my_data->fluence_in_voice_call == false) {
3287                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3288             } else {
3289                 if (is_operator_tmus())
3290                     snd_device = SND_DEVICE_IN_VOICE_DMIC_TMUS;
3291                 else
3292                     snd_device = SND_DEVICE_IN_VOICE_DMIC;
3293             }
3294         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3295             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
3296         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
3297             if (adev->bt_wb_speech_enabled) {
3298                 if (adev->bluetooth_nrec)
3299                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3300                 else
3301                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3302             } else {
3303                 if (adev->bluetooth_nrec)
3304                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3305                 else
3306                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3307             }
3308         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
3309                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
3310                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3311                    out_device & AUDIO_DEVICE_OUT_LINE) {
3312             if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
3313                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
3314                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
3315                 } else {
3316                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3317                 }
3318             }
3319 
3320             //select default
3321             if (snd_device == SND_DEVICE_NONE) {
3322                 if (!adev->enable_hfp) {
3323                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3324                 } else {
3325                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP;
3326                     platform_set_echo_reference(adev, true, out_device);
3327                 }
3328             }
3329         } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX) {
3330             snd_device = SND_DEVICE_IN_VOICE_RX;
3331         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3332             if (audio_extn_usb_is_capture_supported()) {
3333               snd_device = SND_DEVICE_IN_VOICE_USB_HEADSET_MIC;
3334             } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
3335                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
3336                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
3337                 } else {
3338                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3339                 }
3340             }
3341         } else if (out_device & AUDIO_DEVICE_OUT_HEARING_AID) {
3342             snd_device = SND_DEVICE_IN_VOICE_HEARING_AID;
3343         }
3344     } else if (source == AUDIO_SOURCE_CAMCORDER) {
3345         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
3346             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3347             switch (adev->camera_orientation) {
3348             case CAMERA_BACK_LANDSCAPE:
3349                 snd_device = SND_DEVICE_IN_CAMCORDER_LANDSCAPE;
3350                 break;
3351             case CAMERA_BACK_INVERT_LANDSCAPE:
3352                 snd_device = SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE;
3353                 break;
3354             case CAMERA_BACK_PORTRAIT:
3355                 snd_device = SND_DEVICE_IN_CAMCORDER_PORTRAIT;
3356                 break;
3357             case CAMERA_FRONT_LANDSCAPE:
3358                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE;
3359                 break;
3360             case CAMERA_FRONT_INVERT_LANDSCAPE:
3361                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE;
3362                 break;
3363             case CAMERA_FRONT_PORTRAIT:
3364                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT;
3365                 break;
3366             default:
3367                 ALOGW("%s: invalid camera orientation %08x", __func__, adev->camera_orientation);
3368                 snd_device = SND_DEVICE_IN_CAMCORDER_LANDSCAPE;
3369                 break;
3370             }
3371         }
3372     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
3373         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3374             if (my_data->fluence_in_voice_rec && channel_count == 1) {
3375                 if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
3376                     (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3377                     if (in->enable_aec)
3378                         snd_device = SND_DEVICE_IN_HANDSET_QMIC_AEC;
3379                     else
3380                         snd_device = SND_DEVICE_IN_HANDSET_QMIC;
3381                 } else if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
3382                     (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3383                     if (in->enable_aec)
3384                         snd_device = SND_DEVICE_IN_HANDSET_TMIC_AEC;
3385                     else
3386                         snd_device = SND_DEVICE_IN_HANDSET_TMIC;
3387                 } else if (((my_data->fluence_type == FLUENCE_PRO_ENABLE) ||
3388                     (my_data->fluence_type == FLUENCE_ENABLE)) &&
3389                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3390                     if (in->enable_aec)
3391                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
3392                     else
3393                         snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
3394                 }
3395                 in->enable_ec_port = true;
3396             } else if (((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) ||
3397                        (channel_mask == AUDIO_CHANNEL_IN_STEREO) ||
3398                        (channel_mask == AUDIO_CHANNEL_INDEX_MASK_2)) &&
3399                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3400                 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
3401             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
3402                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3403                 snd_device = SND_DEVICE_IN_THREE_MIC;
3404             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
3405                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3406                 snd_device = SND_DEVICE_IN_QUAD_MIC;
3407             }
3408             if (snd_device == SND_DEVICE_NONE) {
3409                 if (in->enable_aec) {
3410                     if (in->enable_ns) {
3411                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS;
3412                     } else {
3413                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC;
3414                     }
3415                 } else if (in->enable_ns) {
3416                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
3417                 } else {
3418                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
3419                 }
3420             }
3421         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3422             snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC;
3423         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3424             snd_device = SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC;
3425         }
3426     } else if (source == AUDIO_SOURCE_UNPROCESSED) {
3427         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3428             if (((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) ||
3429                  (channel_mask == AUDIO_CHANNEL_IN_STEREO) ||
3430                  (channel_mask == AUDIO_CHANNEL_INDEX_MASK_2)) &&
3431                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3432                 snd_device = SND_DEVICE_IN_UNPROCESSED_STEREO_MIC;
3433             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
3434                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3435                 snd_device = SND_DEVICE_IN_UNPROCESSED_THREE_MIC;
3436             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
3437                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3438                 snd_device = SND_DEVICE_IN_UNPROCESSED_QUAD_MIC;
3439             } else {
3440                 snd_device = SND_DEVICE_IN_UNPROCESSED_MIC;
3441             }
3442         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3443             snd_device = SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC;
3444         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3445             snd_device = SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC;
3446         }
3447     } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
3448                mode == AUDIO_MODE_IN_COMMUNICATION) {
3449         if (out_device & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
3450             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3451             (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE | AUDIO_DEVICE_OUT_USB_HEADSET) &&
3452                 !audio_extn_usb_is_capture_supported())) {
3453             in_device = AUDIO_DEVICE_IN_BACK_MIC;
3454         }
3455 
3456         if (in) {
3457             snd_device = get_snd_device_for_voice_comm(my_data, in, out_device, in_device);
3458         }
3459     } else if (source == AUDIO_SOURCE_DEFAULT) {
3460         goto exit;
3461     }
3462 
3463 
3464     if (snd_device != SND_DEVICE_NONE) {
3465         goto exit;
3466     }
3467 
3468     if (in_device != AUDIO_DEVICE_NONE &&
3469             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
3470             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
3471         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3472             if ((my_data->source_mic_type & SOURCE_QUAD_MIC) &&
3473                 channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) {
3474                 snd_device = SND_DEVICE_IN_QUAD_MIC;
3475             } else if ((my_data->source_mic_type & SOURCE_THREE_MIC) &&
3476                        channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) {
3477                 snd_device = SND_DEVICE_IN_THREE_MIC;
3478             } else if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3479                        channel_count == 2) {
3480                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_STEREO;
3481             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3482                        channel_count == 1) {
3483                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3484             } else {
3485                 ALOGE("%s: something wrong (1): source type (%d) channel_count (%d) .."
3486                       " channel mask (0x%x) no combination found .. setting to mono", __func__,
3487                        my_data->source_mic_type, channel_count, channel_mask);
3488                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3489             }
3490         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3491             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3492                     channel_count == 2) {
3493                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
3494             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3495                     channel_count == 1) {
3496                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3497             } else {
3498                 ALOGE("%s: something wrong (2): source type (%d) channel_count (%d) .."
3499                       " no combination found .. setting to mono", __func__,
3500                        my_data->source_mic_type, channel_count);
3501                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3502             }
3503         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3504             snd_device = SND_DEVICE_IN_HEADSET_MIC;
3505         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
3506             if (adev->bt_wb_speech_enabled) {
3507                 if (adev->bluetooth_nrec)
3508                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3509                 else
3510                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3511             } else {
3512                 if (adev->bluetooth_nrec)
3513                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3514                 else
3515                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3516             }
3517         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
3518             snd_device = SND_DEVICE_IN_HDMI_MIC;
3519         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3520             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
3521         } else {
3522             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
3523             ALOGW("%s: Using default handset-mic", __func__);
3524             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3525         }
3526     } else {
3527         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
3528             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3529         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3530             snd_device = SND_DEVICE_IN_HEADSET_MIC;
3531         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
3532                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
3533                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3534                    out_device & AUDIO_DEVICE_OUT_LINE) {
3535             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3536                     channel_count == 2) {
3537                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
3538             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3539                           channel_count == 1) {
3540                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3541             } else {
3542                 ALOGE("%s: something wrong (3): source type (%d) channel_count (%d) .."
3543                       " no combination found .. setting to mono", __func__,
3544                        my_data->source_mic_type, channel_count);
3545                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3546             }
3547         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
3548             if (adev->bt_wb_speech_enabled) {
3549                 if (adev->bluetooth_nrec)
3550                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3551                 else
3552                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3553             } else {
3554                 if (adev->bluetooth_nrec)
3555                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3556                 else
3557                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3558             }
3559         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
3560             snd_device = SND_DEVICE_IN_HDMI_MIC;
3561         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3562             if (audio_extn_usb_is_capture_supported())
3563               snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
3564             else
3565               snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3566         } else {
3567             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
3568             ALOGW("%s: Using default handset-mic", __func__);
3569             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3570         }
3571     }
3572 exit:
3573     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
3574     return snd_device;
3575 }
3576 
platform_set_hdmi_channels(void * platform,int channel_count)3577 int platform_set_hdmi_channels(void *platform,  int channel_count)
3578 {
3579     struct platform_data *my_data = (struct platform_data *)platform;
3580     struct audio_device *adev = my_data->adev;
3581     struct mixer_ctl *ctl;
3582     const char *channel_cnt_str = NULL;
3583     const char *mixer_ctl_name = "HDMI_RX Channels";
3584     switch (channel_count) {
3585     case 8:
3586         channel_cnt_str = "Eight"; break;
3587     case 7:
3588         channel_cnt_str = "Seven"; break;
3589     case 6:
3590         channel_cnt_str = "Six"; break;
3591     case 5:
3592         channel_cnt_str = "Five"; break;
3593     case 4:
3594         channel_cnt_str = "Four"; break;
3595     case 3:
3596         channel_cnt_str = "Three"; break;
3597     default:
3598         channel_cnt_str = "Two"; break;
3599     }
3600     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3601     if (!ctl) {
3602         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3603               __func__, mixer_ctl_name);
3604         return -EINVAL;
3605     }
3606     ALOGV("HDMI channel count: %s", channel_cnt_str);
3607     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
3608     return 0;
3609 }
3610 
platform_edid_get_max_channels(void * platform)3611 int platform_edid_get_max_channels(void *platform)
3612 {
3613     struct platform_data *my_data = (struct platform_data *)platform;
3614     struct audio_device *adev = my_data->adev;
3615     char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
3616     char *sad = block;
3617     int num_audio_blocks;
3618     int channel_count;
3619     int max_channels = 0;
3620     int i, ret, count;
3621 
3622     struct mixer_ctl *ctl;
3623 
3624     ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
3625     if (!ctl) {
3626         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3627               __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
3628         return 0;
3629     }
3630 
3631     mixer_ctl_update(ctl);
3632 
3633     count = mixer_ctl_get_num_values(ctl);
3634 
3635     /* Read SAD blocks, clamping the maximum size for safety */
3636     if (count > (int)sizeof(block))
3637         count = (int)sizeof(block);
3638 
3639     ret = mixer_ctl_get_array(ctl, block, count);
3640     if (ret != 0) {
3641         ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
3642         return 0;
3643     }
3644 
3645     /* Calculate the number of SAD blocks */
3646     num_audio_blocks = count / SAD_BLOCK_SIZE;
3647 
3648     for (i = 0; i < num_audio_blocks; i++) {
3649         /* Only consider LPCM blocks */
3650         if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
3651             sad += 3;
3652             continue;
3653         }
3654 
3655         channel_count = (sad[0] & 0x7) + 1;
3656         if (channel_count > max_channels)
3657             max_channels = channel_count;
3658 
3659         /* Advance to next block */
3660         sad += 3;
3661     }
3662 
3663     return max_channels;
3664 }
3665 
platform_set_incall_recording_session_id(void * platform,uint32_t session_id,int rec_mode)3666 int platform_set_incall_recording_session_id(void *platform,
3667                                              uint32_t session_id, int rec_mode)
3668 {
3669     int ret = 0;
3670     struct platform_data *my_data = (struct platform_data *)platform;
3671     struct audio_device *adev = my_data->adev;
3672     struct mixer_ctl *ctl;
3673     const char *mixer_ctl_name = "Voc VSID";
3674     int num_ctl_values;
3675     int i;
3676 
3677     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3678     if (!ctl) {
3679         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3680               __func__, mixer_ctl_name);
3681         ret = -EINVAL;
3682     } else {
3683         num_ctl_values = mixer_ctl_get_num_values(ctl);
3684         for (i = 0; i < num_ctl_values; i++) {
3685             if (mixer_ctl_set_value(ctl, i, session_id)) {
3686                 ALOGV("Error: invalid session_id: %x", session_id);
3687                 ret = -EINVAL;
3688                 break;
3689             }
3690         }
3691     }
3692 
3693     if (my_data->csd != NULL) {
3694         ret = my_data->csd->start_record(ALL_SESSION_VSID, rec_mode);
3695         if (ret < 0) {
3696             ALOGE("%s: csd_client_start_record failed, error %d",
3697                   __func__, ret);
3698         }
3699     }
3700 
3701     return ret;
3702 }
3703 
platform_set_incall_recording_session_channels(void * platform,uint32_t channel_count)3704 int platform_set_incall_recording_session_channels(void *platform,
3705                                              uint32_t channel_count)
3706 {
3707     int ret = 0;
3708     struct platform_data *my_data = (struct platform_data *)platform;
3709     struct audio_device *adev = my_data->adev;
3710     const char *mixer_ctl_name = "Voc Rec Config";
3711     int num_ctl_values;
3712     int i;
3713     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3714 
3715     if (!ctl) {
3716         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3717               __func__, mixer_ctl_name);
3718         ret = -EINVAL;
3719     } else {
3720         num_ctl_values = mixer_ctl_get_num_values(ctl);
3721         for (i = 0; i < num_ctl_values; i++) {
3722             if (mixer_ctl_set_value(ctl, i, channel_count)) {
3723                 ALOGE("Error: invalid channel count: %x", channel_count);
3724                 ret = -EINVAL;
3725                 break;
3726             }
3727         }
3728     }
3729 
3730     return ret;
3731 }
3732 
platform_stop_incall_recording_usecase(void * platform)3733 int platform_stop_incall_recording_usecase(void *platform)
3734 {
3735     int ret = 0;
3736     struct platform_data *my_data = (struct platform_data *)platform;
3737 
3738     if (my_data->csd != NULL) {
3739         ret = my_data->csd->stop_record(ALL_SESSION_VSID);
3740         if (ret < 0) {
3741             ALOGE("%s: csd_client_stop_record failed, error %d",
3742                   __func__, ret);
3743         }
3744     }
3745 
3746     return ret;
3747 }
3748 
platform_start_incall_music_usecase(void * platform)3749 int platform_start_incall_music_usecase(void *platform)
3750 {
3751     int ret = 0;
3752     struct platform_data *my_data = (struct platform_data *)platform;
3753 
3754     if (my_data->csd != NULL) {
3755         ret = my_data->csd->start_playback(ALL_SESSION_VSID);
3756         if (ret < 0) {
3757             ALOGE("%s: csd_client_start_playback failed, error %d",
3758                   __func__, ret);
3759         }
3760     }
3761 
3762     return ret;
3763 }
3764 
platform_stop_incall_music_usecase(void * platform)3765 int platform_stop_incall_music_usecase(void *platform)
3766 {
3767     int ret = 0;
3768     struct platform_data *my_data = (struct platform_data *)platform;
3769 
3770     if (my_data->csd != NULL) {
3771         ret = my_data->csd->stop_playback(ALL_SESSION_VSID);
3772         if (ret < 0) {
3773             ALOGE("%s: csd_client_stop_playback failed, error %d",
3774                   __func__, ret);
3775         }
3776     }
3777 
3778     return ret;
3779 }
3780 
platform_set_parameters(void * platform,struct str_parms * parms)3781 int platform_set_parameters(void *platform, struct str_parms *parms)
3782 {
3783     struct platform_data *my_data = (struct platform_data *)platform;
3784     char *value = NULL;
3785     char *kv_pairs = str_parms_to_str(parms);
3786     int len;
3787     int ret = 0, err;
3788 
3789     if (kv_pairs == NULL) {
3790         ret = -EINVAL;
3791         ALOGE("%s: key-value pair is NULL", __func__);
3792         goto done;
3793     }
3794 
3795     ALOGV("%s: enter: %s", __func__, kv_pairs);
3796 
3797     len = strlen(kv_pairs);
3798     value = (char*)calloc(len + 1, sizeof(char));
3799     if (value == NULL) {
3800         ret = -ENOMEM;
3801         ALOGE("[%s] failed to allocate memory", __func__);
3802         goto done;
3803     }
3804 
3805     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME,
3806                             value, len);
3807     if (err >= 0) {
3808         str_parms_del(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME);
3809         my_data->snd_card_name = strdup(value);
3810         ALOGV("%s: sound card name %s", __func__, my_data->snd_card_name);
3811     }
3812 
3813     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
3814                             value, len);
3815     if (err >= 0) {
3816         struct operator_info *info;
3817         char *str = value;
3818         char *name;
3819 
3820         str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
3821         info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
3822         name = strtok(str, ";");
3823         info->name = strdup(name);
3824         info->mccmnc = strdup(str + strlen(name) + 1);
3825 
3826         list_add_tail(&operator_info_list, &info->list);
3827         ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
3828     }
3829 
3830     memset(value, 0, len + 1);
3831     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT,
3832                             value, len);
3833     if (err >= 0) {
3834         str_parms_del(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT);
3835         my_data->max_mic_count = atoi(value);
3836         ALOGV("%s: max_mic_count %s/%d", __func__, value, my_data->max_mic_count);
3837     }
3838 
3839     /* handle audio calibration parameters */
3840     set_audiocal(platform, parms, value, len);
3841 
3842     // to-do: disable setting sidetone gain, will revist this later
3843     // audio_extn_usb_set_sidetone_gain(parms, value, len);
3844 done:
3845     ALOGV("%s: exit with code(%d)", __func__, ret);
3846     if (kv_pairs != NULL)
3847         free(kv_pairs);
3848     if (value != NULL)
3849         free(value);
3850 
3851     return ret;
3852 }
3853 
platform_set_audio_source_delay(audio_source_t audio_source,int delay_ms)3854 void platform_set_audio_source_delay(audio_source_t audio_source, int delay_ms)
3855 {
3856     if ((audio_source < AUDIO_SOURCE_DEFAULT) ||
3857            (audio_source > AUDIO_SOURCE_MAX)) {
3858         ALOGE("%s: Invalid audio_source = %d", __func__, audio_source);
3859         return;
3860     }
3861 
3862     audio_source_delay_ms[audio_source] = delay_ms;
3863 }
3864 
3865 /* Delay in Us */
platform_get_audio_source_delay(audio_source_t audio_source)3866 int64_t platform_get_audio_source_delay(audio_source_t audio_source)
3867 {
3868     if ((audio_source < AUDIO_SOURCE_DEFAULT) ||
3869             (audio_source > AUDIO_SOURCE_MAX)) {
3870         ALOGE("%s: Invalid audio_source = %d", __func__, audio_source);
3871         return 0;
3872     }
3873 
3874     return 1000LL * audio_source_delay_ms[audio_source];
3875 }
3876 
platform_set_audio_usecase_delay(audio_usecase_t usecase,int delay_ms)3877 void platform_set_audio_usecase_delay(audio_usecase_t usecase, int delay_ms)
3878 {
3879     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
3880         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
3881         return;
3882     }
3883 
3884     audio_usecase_delay_ms[usecase] = delay_ms;
3885 }
3886 
3887 /* Delay in Us */
platform_get_audio_usecase_delay(audio_usecase_t usecase)3888 int64_t platform_get_audio_usecase_delay(audio_usecase_t usecase)
3889 {
3890     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
3891         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
3892         return 0;
3893     }
3894 
3895     return 1000LL *  audio_usecase_delay_ms[usecase] ;
3896 }
3897 
3898 /* Delay in Us */
platform_render_latency(struct stream_out * out)3899 int64_t platform_render_latency(struct stream_out *out)
3900 {
3901     int64_t delay = 0LL;
3902 
3903     if (!out)
3904         return delay;
3905 
3906     switch (out->usecase) {
3907         case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
3908             delay = DEEP_BUFFER_PLATFORM_DELAY;
3909             break;
3910         case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
3911         case USECASE_AUDIO_PLAYBACK_WITH_HAPTICS:
3912             delay = LOW_LATENCY_PLATFORM_DELAY;
3913             break;
3914         case USECASE_AUDIO_PLAYBACK_ULL:
3915             delay = ULL_PLATFORM_DELAY;
3916             break;
3917         case USECASE_AUDIO_PLAYBACK_MMAP:
3918             delay = MMAP_PLATFORM_DELAY;
3919             break;
3920         default:
3921             break;
3922     }
3923 
3924 /* out->usecase could be used to add delay time if it's necessary */
3925     delay += platform_get_audio_usecase_delay(out->usecase);
3926     return delay;
3927 }
3928 
platform_capture_latency(struct stream_in * in)3929 int64_t platform_capture_latency(struct stream_in *in)
3930 {
3931     int64_t delay = 0LL;
3932 
3933     if (!in)
3934         return delay;
3935 
3936     delay = platform_get_audio_source_delay(in->source);
3937 
3938 /* in->device could be used to add delay time if it's necessary */
3939     return delay;
3940 }
3941 
platform_set_snd_device_backend(snd_device_t device,const char * backend_tag,const char * hw_interface)3942 int platform_set_snd_device_backend(snd_device_t device, const char *backend_tag,
3943                                     const char * hw_interface)
3944 {
3945     int ret = 0;
3946 
3947     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
3948         ALOGE("%s: Invalid snd_device = %d",
3949             __func__, device);
3950         ret = -EINVAL;
3951         goto done;
3952     }
3953 
3954     ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
3955           platform_get_snd_device_name(device),
3956           backend_tag_table[device] != NULL ? backend_tag_table[device]: "null", backend_tag);
3957     if (backend_tag_table[device]) {
3958         free(backend_tag_table[device]);
3959     }
3960     backend_tag_table[device] = strdup(backend_tag);
3961 
3962     if (hw_interface != NULL) {
3963         if (hw_interface_table[device])
3964             free(hw_interface_table[device]);
3965         ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
3966         hw_interface_table[device] = strdup(hw_interface);
3967     }
3968 done:
3969     return ret;
3970 }
3971 
platform_set_usecase_pcm_id(audio_usecase_t usecase,int32_t type,int32_t pcm_id)3972 int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
3973 {
3974     int ret = 0;
3975     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
3976         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
3977         ret = -EINVAL;
3978         goto done;
3979     }
3980 
3981     if ((type != 0) && (type != 1)) {
3982         ALOGE("%s: invalid usecase type", __func__);
3983         ret = -EINVAL;
3984     }
3985     ALOGV("%s: pcm_device_table[%d %s][%d] = %d", __func__, usecase,
3986           use_case_table[usecase],
3987           type, pcm_id);
3988     pcm_device_table[usecase][type] = pcm_id;
3989 done:
3990     return ret;
3991 }
3992 
3993 #define DEFAULT_NOMINAL_SPEAKER_GAIN 20
ramp_speaker_gain(struct audio_device * adev,bool ramp_up,int target_ramp_up_gain)3994 int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
3995     // backup_gain: gain to try to set in case of an error during ramp
3996     int start_gain, end_gain, step, backup_gain, i;
3997     bool error = false;
3998     const struct mixer_ctl *ctl;
3999     const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
4000     const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
4001     struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
4002     struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
4003     if (!ctl_left || !ctl_right) {
4004         ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
4005                       __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
4006         return -EINVAL;
4007     } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
4008             || (mixer_ctl_get_num_values(ctl_right) != 1)) {
4009         ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
4010                               __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
4011         return -EINVAL;
4012     }
4013     if (ramp_up) {
4014         start_gain = 0;
4015         end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
4016         step = +1;
4017         backup_gain = end_gain;
4018     } else {
4019         // using same gain on left and right
4020         const int left_gain = mixer_ctl_get_value(ctl_left, 0);
4021         start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
4022         end_gain = 0;
4023         step = -1;
4024         backup_gain = start_gain;
4025     }
4026     for (i = start_gain ; i != (end_gain + step) ; i += step) {
4027         //ALOGV("setting speaker gain to %d", i);
4028         if (mixer_ctl_set_value(ctl_left, 0, i)) {
4029             ALOGE("%s: error setting %s to %d during gain ramp",
4030                     __func__, mixer_ctl_name_gain_left, i);
4031             error = true;
4032             break;
4033         }
4034         if (mixer_ctl_set_value(ctl_right, 0, i)) {
4035             ALOGE("%s: error setting %s to %d during gain ramp",
4036                     __func__, mixer_ctl_name_gain_right, i);
4037             error = true;
4038             break;
4039         }
4040         usleep(1000);
4041     }
4042     if (error) {
4043         // an error occured during the ramp, let's still try to go back to a safe volume
4044         if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
4045             ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
4046         }
4047         if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
4048             ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
4049         }
4050     }
4051     return start_gain;
4052 }
4053 
platform_set_swap_mixer(struct audio_device * adev,bool swap_channels)4054 int platform_set_swap_mixer(struct audio_device *adev, bool swap_channels)
4055 {
4056     const char *mixer_ctl_name = "Swap channel";
4057     struct mixer_ctl *ctl;
4058     const char *mixer_path;
4059     struct platform_data *my_data = (struct platform_data *)adev->platform;
4060 
4061     // forced to set to swap, but device not rotated ... ignore set
4062     if (swap_channels && !my_data->speaker_lr_swap)
4063         return 0;
4064 
4065     ALOGV("%s:", __func__);
4066 
4067     if (swap_channels) {
4068         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
4069         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
4070     } else {
4071         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
4072         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
4073     }
4074 
4075     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4076     if (!ctl) {
4077         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
4078         return -EINVAL;
4079     }
4080 
4081     if (mixer_ctl_set_value(ctl, 0, swap_channels) < 0) {
4082         ALOGE("%s: Could not set reverse cotrol %d",__func__, swap_channels);
4083         return -EINVAL;
4084     }
4085 
4086     ALOGV("platfor_force_swap_channel :: Channel orientation ( %s ) ",
4087            swap_channels?"R --> L":"L --> R");
4088 
4089     return 0;
4090 }
4091 
platform_check_and_set_swap_lr_channels(struct audio_device * adev,bool swap_channels)4092 int platform_check_and_set_swap_lr_channels(struct audio_device *adev, bool swap_channels)
4093 {
4094     // only update if there is active pcm playback on speaker
4095     struct audio_usecase *usecase;
4096     struct listnode *node;
4097     struct platform_data *my_data = (struct platform_data *)adev->platform;
4098 
4099     my_data->speaker_lr_swap = swap_channels;
4100 
4101     return platform_set_swap_channels(adev, swap_channels);
4102 }
4103 
platform_set_swap_channels(struct audio_device * adev,bool swap_channels)4104 int platform_set_swap_channels(struct audio_device *adev, bool swap_channels)
4105 {
4106     // only update if there is active pcm playback on speaker
4107     struct audio_usecase *usecase;
4108     struct listnode *node;
4109     struct platform_data *my_data = (struct platform_data *)adev->platform;
4110 
4111     // do not swap channels in audio modes with concurrent capture and playback
4112     // as this may break the echo reference
4113     if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || (adev->mode == AUDIO_MODE_IN_CALL)) {
4114         ALOGV("%s: will not swap due to audio mode %d", __func__, adev->mode);
4115         return 0;
4116     }
4117 
4118     list_for_each(node, &adev->usecase_list) {
4119         usecase = node_to_item(node, struct audio_usecase, list);
4120         if (usecase->type == PCM_PLAYBACK &&
4121                 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
4122             /*
4123              * If acdb tuning is different for SPEAKER_REVERSE, it is must
4124              * to perform device switch to disable the current backend to
4125              * enable it with new acdb data.
4126              */
4127             if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
4128                 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
4129                 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
4130                 select_devices(adev, usecase->id);
4131                 if (initial_skpr_gain != -EINVAL)
4132                     ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
4133 
4134             } else {
4135                 platform_set_swap_mixer(adev, swap_channels);
4136             }
4137             break;
4138         }
4139     }
4140 
4141     return 0;
4142 }
4143 
4144 static struct amp_db_and_gain_table tbl_mapping[MAX_VOLUME_CAL_STEPS];
4145 static int num_gain_tbl_entry = 0;
4146 
platform_add_gain_level_mapping(struct amp_db_and_gain_table * tbl_entry)4147 bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry) {
4148 
4149     ALOGV("%s: enter .. add %f %f %d", __func__, tbl_entry->amp, tbl_entry->db, tbl_entry->level);
4150     if (num_gain_tbl_entry == -1) {
4151         ALOGE("%s: num entry beyond valid step levels or corrupted..rejecting custom mapping",
4152                __func__);
4153         return false;
4154     }
4155 
4156     if (num_gain_tbl_entry >= MAX_VOLUME_CAL_STEPS) {
4157         ALOGE("%s: max entry reached max[%d] current index[%d]  .. rejecting", __func__,
4158                MAX_VOLUME_CAL_STEPS, num_gain_tbl_entry);
4159         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
4160         return false;
4161     }
4162 
4163     if (num_gain_tbl_entry > 0 && tbl_mapping[num_gain_tbl_entry - 1].amp >= tbl_entry->amp) {
4164         ALOGE("%s: value not in ascending order .. rejecting custom mapping", __func__);
4165         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
4166         return false;
4167     }
4168 
4169     tbl_mapping[num_gain_tbl_entry] = *tbl_entry;
4170     ++num_gain_tbl_entry;
4171 
4172     return true;
4173 }
4174 
platform_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl,int table_size)4175 int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
4176                                     int table_size) {
4177     int itt = 0;
4178     ALOGV("platform_get_gain_level_mapping called ");
4179 
4180     if (num_gain_tbl_entry <= 0 || num_gain_tbl_entry > MAX_VOLUME_CAL_STEPS) {
4181         ALOGD("%s: empty or currupted gain_mapping_table", __func__);
4182         return 0;
4183     }
4184 
4185     for (; itt < num_gain_tbl_entry && itt <= table_size; itt++) {
4186         mapping_tbl[itt] = tbl_mapping[itt];
4187         ALOGV("%s: added amp[%f] db[%f] level[%d]", __func__,
4188                mapping_tbl[itt].amp, mapping_tbl[itt].db, mapping_tbl[itt].level);
4189     }
4190 
4191     return num_gain_tbl_entry;
4192 }
4193 
platform_snd_card_update(void * platform,card_status_t status)4194 int platform_snd_card_update(void *platform, card_status_t status)
4195 {
4196     struct platform_data *my_data = (struct platform_data *)platform;
4197     struct audio_device *adev = my_data->adev;
4198 
4199     if (status == CARD_STATUS_ONLINE) {
4200         if (my_data->acdb_send_custom_top)
4201             my_data->acdb_send_custom_top();
4202     }
4203     return 0;
4204 }
4205 
4206 /*
4207  * configures afe with bit width and Sample Rate
4208  */
platform_set_backend_cfg(const struct audio_device * adev,snd_device_t snd_device,const struct audio_backend_cfg * backend_cfg)4209 int platform_set_backend_cfg(const struct audio_device* adev,
4210                              snd_device_t snd_device,
4211                              const struct audio_backend_cfg *backend_cfg)
4212 {
4213 
4214     int ret = 0;
4215     const int backend_idx = platform_get_backend_index(snd_device);
4216     struct platform_data *my_data = (struct platform_data *)adev->platform;
4217     const unsigned int bit_width = backend_cfg->bit_width;
4218     const unsigned int sample_rate = backend_cfg->sample_rate;
4219     const unsigned int channels = backend_cfg->channels;
4220     const audio_format_t format = backend_cfg->format;
4221     const bool passthrough_enabled = backend_cfg->passthrough_enabled;
4222 
4223 
4224     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4225           ", backend_idx %d device (%s)", __func__,  bit_width,
4226           sample_rate, channels, backend_idx,
4227           platform_get_snd_device_name(snd_device));
4228 
4229     if ((my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl) &&
4230         (bit_width != my_data->current_backend_cfg[backend_idx].bit_width)) {
4231 
4232         struct  mixer_ctl *ctl = NULL;
4233         ctl = mixer_get_ctl_by_name(adev->mixer,
4234                                     my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
4235         if (!ctl) {
4236             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4237                   __func__,
4238                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
4239             return -EINVAL;
4240         }
4241 
4242         if (bit_width == 24) {
4243             if (format == AUDIO_FORMAT_PCM_24_BIT_PACKED)
4244                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_3LE");
4245             else
4246                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_LE");
4247         } else if (bit_width == 32) {
4248             ret = mixer_ctl_set_enum_by_string(ctl, "S32_LE");
4249         } else {
4250             ret = mixer_ctl_set_enum_by_string(ctl, "S16_LE");
4251         }
4252         if ( ret < 0) {
4253             ALOGE("%s:becf: afe: fail for %s mixer set to %d bit for %x format", __func__,
4254                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
4255         } else {
4256             my_data->current_backend_cfg[backend_idx].bit_width = bit_width;
4257             ALOGD("%s:becf: afe: %s mixer set to %d bit for %x format", __func__,
4258                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
4259         }
4260         /* set the ret as 0 and not pass back to upper layer */
4261         ret = 0;
4262     }
4263 
4264     if (passthrough_enabled || ((my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl) &&
4265                                 (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate))) {
4266         char *rate_str = NULL;
4267         struct  mixer_ctl *ctl = NULL;
4268 
4269         switch (sample_rate) {
4270             case 32000:
4271                 if (passthrough_enabled) {
4272                     rate_str = "KHZ_32";
4273                     break;
4274                 }
4275             case 8000:
4276             case 11025:
4277             case 16000:
4278             case 22050:
4279             case 48000:
4280                 rate_str = "KHZ_48";
4281                 break;
4282             case 44100:
4283                 rate_str = "KHZ_44P1";
4284                 break;
4285             case 64000:
4286             case 96000:
4287                 rate_str = "KHZ_96";
4288                 break;
4289             case 88200:
4290                 rate_str = "KHZ_88P2";
4291                 break;
4292             case 176400:
4293                 rate_str = "KHZ_176P4";
4294                 break;
4295             case 192000:
4296                 rate_str = "KHZ_192";
4297                 break;
4298             case 352800:
4299                 rate_str = "KHZ_352P8";
4300                 break;
4301             case 384000:
4302                 rate_str = "KHZ_384";
4303                 break;
4304             case 144000:
4305                 if (passthrough_enabled) {
4306                     rate_str = "KHZ_144";
4307                     break;
4308                 }
4309             default:
4310                 rate_str = "KHZ_48";
4311                 break;
4312         }
4313 
4314         ctl = mixer_get_ctl_by_name(adev->mixer,
4315                                     my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
4316         if(!ctl) {
4317             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4318                   __func__,
4319                   my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
4320             return -EINVAL;
4321         }
4322 
4323         ALOGD("%s:becf: afe: %s set to %s", __func__,
4324               my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl, rate_str);
4325         mixer_ctl_set_enum_by_string(ctl, rate_str);
4326         my_data->current_backend_cfg[backend_idx].sample_rate = sample_rate;
4327     }
4328     if ((my_data->current_backend_cfg[backend_idx].channels_mixer_ctl) &&
4329         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
4330         struct  mixer_ctl *ctl = NULL;
4331         char *channel_cnt_str = NULL;
4332 
4333         switch (channels) {
4334             case 8:
4335                 channel_cnt_str = "Eight"; break;
4336             case 7:
4337                 channel_cnt_str = "Seven"; break;
4338             case 6:
4339                 channel_cnt_str = "Six"; break;
4340             case 5:
4341                 channel_cnt_str = "Five"; break;
4342             case 4:
4343                 channel_cnt_str = "Four"; break;
4344             case 3:
4345                 channel_cnt_str = "Three"; break;
4346             case 1:
4347                 channel_cnt_str = "One"; break;
4348             case 2:
4349             default:
4350                 channel_cnt_str = "Two"; break;
4351         }
4352 
4353         ctl = mixer_get_ctl_by_name(adev->mixer,
4354                                     my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
4355         if (!ctl) {
4356             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4357                   __func__,
4358                   my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
4359             return -EINVAL;
4360         }
4361         mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
4362         my_data->current_backend_cfg[backend_idx].channels = channels;
4363 
4364         // skip EDID configuration for HDMI backend
4365 
4366         ALOGD("%s:becf: afe: %s set to %s", __func__,
4367               my_data->current_backend_cfg[backend_idx].channels_mixer_ctl,
4368               channel_cnt_str);
4369     }
4370 
4371     // skip set ext_display format mixer control
4372     return ret;
4373 }
4374 
platform_get_snd_device_bit_width(snd_device_t snd_device)4375 static int platform_get_snd_device_bit_width(snd_device_t snd_device)
4376 {
4377     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
4378         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
4379         return CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4380     }
4381 
4382     return backend_bit_width_table[snd_device];
4383 }
4384 
4385 /*
4386  * return backend_idx on which voice call is active
4387  */
platform_get_voice_call_backend(struct audio_device * adev)4388 static int platform_get_voice_call_backend(struct audio_device* adev)
4389 {
4390     struct audio_usecase *uc = NULL;
4391     struct listnode *node;
4392     snd_device_t out_snd_device = SND_DEVICE_NONE;
4393 
4394     int backend_idx = -1;
4395 
4396     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
4397         list_for_each(node, &adev->usecase_list) {
4398             uc =  node_to_item(node, struct audio_usecase, list);
4399             if (uc && uc->type == VOICE_CALL && uc->stream.out) {
4400                 out_snd_device = platform_get_output_snd_device(adev->platform,
4401                                                         uc->stream.out->devices);
4402                 backend_idx = platform_get_backend_index(out_snd_device);
4403                 break;
4404             }
4405         }
4406     }
4407     return backend_idx;
4408 }
4409 
4410 /*
4411  * goes through all the current usecases and picks the highest
4412  * bitwidth & samplerate
4413  */
platform_check_capture_backend_cfg(struct audio_device * adev,int backend_idx,struct audio_backend_cfg * backend_cfg)4414 static bool platform_check_capture_backend_cfg(struct audio_device* adev,
4415                                    int backend_idx,
4416                                    struct audio_backend_cfg *backend_cfg)
4417 {
4418     bool backend_change = false;
4419     unsigned int bit_width;
4420     unsigned int sample_rate;
4421     unsigned int channels;
4422     struct platform_data *my_data = (struct platform_data *)adev->platform;
4423 
4424     bit_width = backend_cfg->bit_width;
4425     sample_rate = backend_cfg->sample_rate;
4426     channels = backend_cfg->channels;
4427 
4428     ALOGV("%s:txbecf: afe: Codec selected backend: %d current bit width: %d and "
4429           "sample rate: %d, channels %d",__func__,backend_idx, bit_width,
4430           sample_rate, channels);
4431 
4432     // For voice calls use default configuration i.e. 16b/48K, only applicable to
4433     // default backend
4434     // force routing is not required here, caller will do it anyway
4435     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
4436         ALOGW("%s:txbecf: afe: Use default bw and sr for voice/voip calls and "
4437               "for unprocessed/camera source", __func__);
4438         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4439         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4440     }
4441 
4442     if (backend_idx == USB_AUDIO_TX_BACKEND) {
4443         audio_extn_usb_is_config_supported(&bit_width, &sample_rate, &channels, false);
4444         ALOGV("%s:txbecf: afe: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
4445               __func__, bit_width, sample_rate, channels);
4446     }
4447 
4448     ALOGV("%s:txbecf: afe: Codec selected backend: %d updated bit width: %d and "
4449           "sample rate: %d", __func__, backend_idx, bit_width, sample_rate);
4450 
4451     // Force routing if the expected bitwdith or samplerate
4452     // is not same as current backend comfiguration
4453     if ((bit_width != my_data->current_backend_cfg[backend_idx].bit_width) ||
4454         (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate) ||
4455         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
4456         backend_cfg->bit_width = bit_width;
4457         backend_cfg->sample_rate= sample_rate;
4458         backend_cfg->channels = channels;
4459         backend_change = true;
4460         ALOGI("%s:txbecf: afe: Codec backend needs to be updated. new bit width: %d "
4461               "new sample rate: %d new channel: %d",
4462               __func__, backend_cfg->bit_width,
4463               backend_cfg->sample_rate, backend_cfg->channels);
4464     }
4465 
4466     return backend_change;
4467 }
4468 
pick_playback_cfg_for_uc(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device,unsigned int * bit_width,unsigned int * sample_rate,unsigned int * channels)4469 static void pick_playback_cfg_for_uc(struct audio_device *adev,
4470                                      struct audio_usecase *usecase,
4471                                      snd_device_t snd_device,
4472                                      unsigned int *bit_width,
4473                                      unsigned int *sample_rate,
4474                                      unsigned int *channels)
4475 {
4476     int i =0;
4477     struct listnode *node;
4478     list_for_each(node, &adev->usecase_list) {
4479         struct audio_usecase *uc;
4480         uc = node_to_item(node, struct audio_usecase, list);
4481         struct stream_out *out = (struct stream_out*) uc->stream.out;
4482         if (uc->type == PCM_PLAYBACK && out && usecase != uc) {
4483             unsigned int out_channels = audio_channel_count_from_out_mask(out->channel_mask);
4484             ALOGV("%s:napb: (%d) - (%s)id (%d) sr %d bw "
4485                   "(%d) ch (%d) device %s", __func__, i++, use_case_table[uc->id],
4486                   uc->id, out->sample_rate,
4487                   pcm_format_to_bits(out->config.format), out_channels,
4488                   platform_get_snd_device_name(uc->out_snd_device));
4489 
4490             if (platform_check_backends_match(snd_device, uc->out_snd_device)) {
4491                 if (*bit_width < pcm_format_to_bits(out->config.format))
4492                     *bit_width = pcm_format_to_bits(out->config.format);
4493                 if (*sample_rate < out->sample_rate)
4494                     *sample_rate = out->sample_rate;
4495                 if (out->sample_rate < OUTPUT_SAMPLING_RATE_44100)
4496                     *sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4497                 if (*channels < out_channels)
4498                     *channels = out_channels;
4499             }
4500         }
4501     }
4502     return;
4503 }
4504 
headset_is_config_supported(unsigned int * bit_width,unsigned int * sample_rate,unsigned int * channels)4505 static void headset_is_config_supported(unsigned int *bit_width,
4506                                         unsigned int *sample_rate,
4507                                         unsigned int *channels) {
4508     switch (*bit_width) {
4509         case 16:
4510         case 24:
4511             break;
4512         default:
4513             *bit_width = 16;
4514             break;
4515     }
4516 
4517     if (*sample_rate > 192000) {
4518         *sample_rate = 192000;
4519     }
4520 
4521     if (*channels > 2) {
4522         *channels = 2;
4523     }
4524 }
4525 
platform_check_playback_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device,struct audio_backend_cfg * backend_cfg)4526 static bool platform_check_playback_backend_cfg(struct audio_device* adev,
4527                                              struct audio_usecase* usecase,
4528                                              snd_device_t snd_device,
4529                                              struct audio_backend_cfg *backend_cfg)
4530 {
4531     bool backend_change = false;
4532     unsigned int bit_width;
4533     unsigned int sample_rate;
4534     unsigned int channels;
4535     int backend_idx = DEFAULT_CODEC_BACKEND;
4536     unsigned long service_interval = 0; // 0 is invalid
4537     struct platform_data *my_data = (struct platform_data *)adev->platform;
4538 
4539     if (snd_device == SND_DEVICE_OUT_BT_SCO ||
4540         snd_device == SND_DEVICE_OUT_BT_SCO_WB) {
4541         backend_change = false;
4542         return backend_change;
4543     }
4544 
4545     backend_idx = platform_get_backend_index(snd_device);
4546     bit_width = backend_cfg->bit_width;
4547     sample_rate = backend_cfg->sample_rate;
4548     channels = backend_cfg->channels;
4549 
4550     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4551           ", backend_idx %d usecase = %d device (%s)", __func__, bit_width,
4552           sample_rate, channels, backend_idx, usecase->id,
4553           platform_get_snd_device_name(snd_device));
4554 
4555     if (backend_idx == platform_get_voice_call_backend(adev)) {
4556         ALOGW("%s:becf: afe:Use default bw and sr for voice/voip calls ",
4557               __func__);
4558         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4559         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4560         channels = CODEC_BACKEND_DEFAULT_CHANNELS;
4561     } else {
4562         /*
4563          * The backend should be configured at highest bit width and/or
4564          * sample rate amongst all playback usecases.
4565          * If the selected sample rate and/or bit width differ with
4566          * current backend sample rate and/or bit width, then, we set the
4567          * backend re-configuration flag.
4568          *
4569          * Exception: 16 bit playbacks is allowed through 16 bit/48/44.1 khz backend only
4570          */
4571         pick_playback_cfg_for_uc(adev, usecase, snd_device,
4572                                  &bit_width,
4573                                  &sample_rate,
4574                                  &channels);
4575     }
4576 
4577     switch (backend_idx) {
4578         case USB_AUDIO_RX_BACKEND:
4579             audio_extn_usb_is_config_supported(&bit_width,
4580                                                &sample_rate, &channels, true);
4581             if (platform_get_usb_service_interval(adev->platform, true,
4582                                                   &service_interval) == 0) {
4583                 /* overwrite with best altset for this service interval */
4584                 int ret =
4585                         audio_extn_usb_altset_for_service_interval(true /*playback*/,
4586                                                                    service_interval,
4587                                                                    &bit_width,
4588                                                                    &sample_rate,
4589                                                                    &channels);
4590                 if (ret < 0) {
4591                     ALOGE("Failed to find altset for service interval %lu, skip reconfig",
4592                           service_interval);
4593                     return false;
4594                 }
4595             }
4596             ALOGV("%s: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
4597                   __func__, bit_width, sample_rate, channels);
4598             break;
4599         case HEADPHONE_BACKEND:
4600             headset_is_config_supported(&bit_width, &sample_rate, &channels);
4601             break;
4602         case DEFAULT_CODEC_BACKEND:
4603         default:
4604             bit_width = platform_get_snd_device_bit_width(snd_device);
4605             sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4606             channels = CODEC_BACKEND_DEFAULT_CHANNELS;
4607             break;
4608     }
4609 
4610     ALOGV("%s:becf: afe: Codec selected backend: %d updated bit width: %d and"
4611           "sample rate: %d",
4612           __func__, backend_idx , bit_width, sample_rate);
4613 
4614     // Force routing if the expected bitwdith or samplerate
4615     // is not same as current backend comfiguration
4616     if (bit_width != my_data->current_backend_cfg[backend_idx].bit_width ||
4617         sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate ||
4618         channels != my_data->current_backend_cfg[backend_idx].channels) {
4619         backend_cfg->bit_width = bit_width;
4620         backend_cfg->sample_rate = sample_rate;
4621         backend_cfg->channels = channels;
4622         backend_cfg->passthrough_enabled = false;
4623         backend_change = true;
4624         ALOGV("%s:becf: afe: Codec backend needs to be updated. new bit width: %d"
4625               "new sample rate: %d new channels: %d",
4626               __func__, backend_cfg->bit_width, backend_cfg->sample_rate, backend_cfg->channels);
4627     }
4628 
4629     return backend_change;
4630 }
4631 
platform_check_and_set_playback_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device)4632 bool platform_check_and_set_playback_backend_cfg(struct audio_device* adev,
4633     struct audio_usecase *usecase, snd_device_t snd_device)
4634 {
4635     int backend_idx = DEFAULT_CODEC_BACKEND;
4636     int new_snd_devices[SND_DEVICE_OUT_END];
4637     int i, num_devices = 1;
4638     bool ret = false;
4639     struct platform_data *my_data = (struct platform_data *)adev->platform;
4640     struct audio_backend_cfg backend_cfg;
4641 
4642     backend_idx = platform_get_backend_index(snd_device);
4643 
4644     backend_cfg.bit_width = pcm_format_to_bits(usecase->stream.out->config.format);
4645     backend_cfg.sample_rate = usecase->stream.out->sample_rate;
4646     backend_cfg.format = usecase->stream.out->format;
4647     backend_cfg.channels = audio_channel_count_from_out_mask(usecase->stream.out->channel_mask);
4648     /*this is populated by check_codec_backend_cfg hence set default value to false*/
4649     backend_cfg.passthrough_enabled = false;
4650 
4651     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4652           ", backend_idx %d usecase = %d device (%s)", __func__, backend_cfg.bit_width,
4653           backend_cfg.sample_rate, backend_cfg.channels, backend_idx, usecase->id,
4654           platform_get_snd_device_name(snd_device));
4655 
4656     if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices) < 0)
4657         new_snd_devices[0] = snd_device;
4658 
4659     for (i = 0; i < num_devices; i++) {
4660         ALOGV("%s: new_snd_devices[%d] is %d", __func__, i, new_snd_devices[i]);
4661         if ((platform_check_playback_backend_cfg(adev, usecase, new_snd_devices[i],
4662                                                  &backend_cfg))) {
4663             platform_set_backend_cfg(adev, new_snd_devices[i],
4664                                      &backend_cfg);
4665             ret = true;
4666         }
4667     }
4668     return ret;
4669 }
4670 
platform_check_and_set_capture_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device)4671 bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev,
4672     struct audio_usecase *usecase, snd_device_t snd_device)
4673 {
4674     int backend_idx = platform_get_backend_index(snd_device);
4675     int ret = 0;
4676     struct audio_backend_cfg backend_cfg;
4677     memset(&backend_cfg, 0, sizeof(struct audio_backend_cfg));
4678 
4679     if (usecase->type == PCM_CAPTURE) {
4680         backend_cfg.format = usecase->stream.in->format;
4681         backend_cfg.channels = audio_channel_count_from_in_mask(usecase->stream.in->channel_mask);
4682     } else {
4683         backend_cfg.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4684         backend_cfg.sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4685         backend_cfg.format = AUDIO_FORMAT_PCM_16_BIT;
4686         backend_cfg.channels = 1;
4687     }
4688 
4689     ALOGV("%s:txbecf: afe: bitwidth %d, samplerate %d, channel %d"
4690           ", backend_idx %d usecase = %d device (%s)", __func__,
4691           backend_cfg.bit_width,
4692           backend_cfg.sample_rate,
4693           backend_cfg.channels,
4694           backend_idx, usecase->id,
4695           platform_get_snd_device_name(snd_device));
4696 
4697     if (platform_check_capture_backend_cfg(adev, backend_idx, &backend_cfg)) {
4698         ret = platform_set_backend_cfg(adev, snd_device,
4699                                        &backend_cfg);
4700         if(!ret)
4701             return true;
4702     }
4703 
4704     return false;
4705 }
4706 
4707 static int max_be_dai_names = 0;
4708 static const struct be_dai_name_struct *be_dai_name_table;
4709 
4710 /*
4711  * Retrieves the be_dai_name_table from kernel to enable a mapping
4712  * between sound device hw interfaces and backend IDs. This allows HAL to
4713  * specify the backend a specific calibration is needed for.
4714  */
init_be_dai_name_table(struct audio_device * adev)4715 static int init_be_dai_name_table(struct audio_device *adev)
4716 {
4717     const char *mixer_ctl_name = "Backend DAI Name Table";
4718     struct mixer_ctl *ctl;
4719     int i, j, ret, size;
4720     bool valid_hw_interface;
4721 
4722     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4723     if (!ctl) {
4724         ALOGE("%s: Could not get ctl for mixer name %s\n",
4725                __func__, mixer_ctl_name);
4726         ret = -EINVAL;
4727         goto done;
4728     }
4729 
4730     mixer_ctl_update(ctl);
4731 
4732     size = mixer_ctl_get_num_values(ctl);
4733     if (size <= 0){
4734         ALOGE("%s: Failed to get %s size %d\n",
4735                __func__, mixer_ctl_name, size);
4736         ret = -EFAULT;
4737         goto done;
4738     }
4739 
4740     be_dai_name_table =
4741             (const struct be_dai_name_struct *)calloc(1, size);
4742     if (be_dai_name_table == NULL) {
4743         ALOGE("%s: Failed to allocate memory for %s\n",
4744                __func__, mixer_ctl_name);
4745         ret = -ENOMEM;
4746         goto freeMem;
4747     }
4748 
4749     ret = mixer_ctl_get_array(ctl, (void *)be_dai_name_table, size);
4750     if (ret) {
4751         ALOGE("%s: Failed to get %s, ret %d\n",
4752                __func__, mixer_ctl_name, ret);
4753         ret = -EFAULT;
4754         goto freeMem;
4755     }
4756 
4757     if (be_dai_name_table != NULL) {
4758         max_be_dai_names = size / sizeof(struct be_dai_name_struct);
4759         ALOGV("%s: Successfully got %s, number of be dais is %d\n",
4760               __func__, mixer_ctl_name, max_be_dai_names);
4761         ret = 0;
4762     } else {
4763         ALOGE("%s: Failed to get %s\n", __func__, mixer_ctl_name);
4764         ret = -EFAULT;
4765         goto freeMem;
4766     }
4767 
4768     /*
4769      * Validate all sound devices have a valid backend set to catch
4770      * errors for uncommon sound devices
4771      */
4772     for (i = 0; i < SND_DEVICE_MAX; i++) {
4773         valid_hw_interface = false;
4774 
4775         if (hw_interface_table[i] == NULL) {
4776             ALOGW("%s: sound device %s has no hw interface set\n",
4777                   __func__, platform_get_snd_device_name(i));
4778             continue;
4779         }
4780 
4781         for (j = 0; j < max_be_dai_names; j++) {
4782             if (strcmp(hw_interface_table[i], be_dai_name_table[j].be_name)
4783                 == 0) {
4784                 valid_hw_interface = true;
4785                 break;
4786             }
4787         }
4788         if (!valid_hw_interface)
4789             ALOGD("%s: sound device %s does not have a valid hw interface set "
4790                   "(disregard for combo devices) %s\n",
4791                   __func__, platform_get_snd_device_name(i),
4792                   hw_interface_table[i]);
4793     }
4794 
4795     goto done;
4796 
4797 freeMem:
4798     if (be_dai_name_table) {
4799         free((void *)be_dai_name_table);
4800         be_dai_name_table = NULL;
4801     }
4802 
4803 done:
4804     return ret;
4805 }
4806 
platform_get_snd_device_backend_index(snd_device_t device)4807 int platform_get_snd_device_backend_index(snd_device_t device)
4808 {
4809     int i, be_dai_id;
4810     const char * hw_interface_name = NULL;
4811 
4812     ALOGV("%s: enter with device %d\n", __func__, device);
4813 
4814     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
4815         ALOGE("%s: Invalid snd_device = %d",
4816               __func__, device);
4817         be_dai_id = -EINVAL;
4818         goto done;
4819     }
4820 
4821     /* Get string value of necessary backend for device */
4822     hw_interface_name = hw_interface_table[device];
4823     if (hw_interface_name == NULL) {
4824         ALOGE("%s: no hw_interface set for device %d\n", __func__, device);
4825         be_dai_id = -EINVAL;
4826         goto done;
4827     }
4828 
4829     /* Check if be dai name table was retrieved successfully */
4830     if (be_dai_name_table == NULL) {
4831         ALOGE("%s: BE DAI Name Table is not present\n", __func__);
4832         be_dai_id = -EFAULT;
4833         goto done;
4834     }
4835 
4836     /* Get backend ID for device specified */
4837     for (i = 0; i < max_be_dai_names; i++) {
4838         if (strcmp(hw_interface_name, be_dai_name_table[i].be_name) == 0) {
4839             be_dai_id = be_dai_name_table[i].be_id;
4840             goto done;
4841         }
4842     }
4843     ALOGE("%s: no interface matching name %s\n", __func__, hw_interface_name);
4844     be_dai_id = -EINVAL;
4845     goto done;
4846 
4847 done:
4848     return be_dai_id;
4849 }
4850 
platform_check_and_update_copp_sample_rate(void * platform,snd_device_t snd_device,unsigned int stream_sr,int * sample_rate)4851 void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
4852                                                 unsigned int stream_sr, int* sample_rate)
4853 {
4854     struct platform_data* my_data = (struct platform_data *)platform;
4855     int backend_idx = platform_get_backend_index(snd_device);
4856     int device_sr = my_data->current_backend_cfg[backend_idx].sample_rate;
4857     /*
4858      *Check if device SR is multiple of 8K or 11.025 Khz
4859      *check if the stream SR is multiple of same base, if yes
4860      *then have copp SR equal to stream SR, this ensures that
4861      *post processing happens at stream SR, else have
4862      *copp SR equal to device SR.
4863      */
4864     if (!(((sample_rate_multiple(device_sr, SAMPLE_RATE_8000)) &&
4865            (sample_rate_multiple(stream_sr, SAMPLE_RATE_8000))) ||
4866           ((sample_rate_multiple(device_sr, SAMPLE_RATE_11025)) &&
4867            (sample_rate_multiple(stream_sr, SAMPLE_RATE_11025))))) {
4868         *sample_rate = device_sr;
4869     } else
4870         *sample_rate = stream_sr;
4871 
4872     ALOGI("sn_device %d device sr %d stream sr %d copp sr %d", snd_device, device_sr, stream_sr
4873           , *sample_rate);
4874 
4875 }
4876 
4877 // called from info parser
platform_add_app_type(const char * uc_type,const char * mode,int bw,int app_type,int max_rate)4878 void platform_add_app_type(const char *uc_type,
4879                            const char *mode,
4880                            int bw,
4881                            int app_type, int max_rate) {
4882     struct app_type_entry *ap =
4883             (struct app_type_entry *)calloc(1, sizeof(struct app_type_entry));
4884 
4885     if (!ap) {
4886         ALOGE("%s failed to allocate mem for app type", __func__);
4887         return;
4888     }
4889 
4890     ap->uc_type = -1;
4891     for (int i=0; i<USECASE_TYPE_MAX; i++) {
4892         if (!strcmp(uc_type, usecase_type_index[i].name)) {
4893             ap->uc_type = usecase_type_index[i].index;
4894             break;
4895         }
4896     }
4897 
4898     if (ap->uc_type == -1) {
4899         free(ap);
4900         return;
4901     }
4902 
4903     ALOGI("%s uc %s mode %s bw %d app_type %d max_rate %d",
4904           __func__, uc_type, mode, bw, app_type, max_rate);
4905     ap->bit_width = bw;
4906     ap->app_type = app_type;
4907     ap->max_rate = max_rate;
4908     ap->mode = strdup(mode);
4909     list_add_tail(&app_type_entry_list, &ap->node);
4910 }
4911 
4912 
platform_get_default_app_type_v2(void * platform __unused,usecase_type_t type,int * app_type)4913 int platform_get_default_app_type_v2(void *platform __unused,
4914                                      usecase_type_t type,
4915                                      int *app_type )
4916 {
4917     if (type == PCM_PLAYBACK)
4918         *app_type = DEFAULT_APP_TYPE_RX_PATH;
4919     else
4920         *app_type = DEFAULT_APP_TYPE_TX_PATH;
4921     return 0;
4922 }
4923 
platform_get_app_type_v2(void * platform,usecase_type_t uc_type,const char * mode,int bw,int sr __unused,int * app_type)4924 int platform_get_app_type_v2(void *platform,
4925                              usecase_type_t uc_type,
4926                              const char *mode,
4927                              int bw, int sr __unused,
4928                              int *app_type)
4929 {
4930     struct listnode *node;
4931     struct app_type_entry *entry;
4932     *app_type = -1;
4933 
4934     ALOGV("%s find match for uc %d mode %s bw %d rate %d",
4935           __func__, uc_type, mode, bw, sr);
4936     list_for_each(node, &app_type_entry_list) {
4937         entry = node_to_item(node, struct app_type_entry, node);
4938         ALOGV("%s uc %d mode %s bw %d app_type %d max_rate %d",
4939               __func__, entry->uc_type, entry->mode, entry->bit_width,
4940               entry->app_type, entry->max_rate);
4941         if (entry->bit_width == bw &&
4942             entry->uc_type == uc_type &&
4943             sr <= entry->max_rate &&
4944             entry->mode && !strcmp(mode, entry->mode)) {
4945             ALOGV("%s found match %d", __func__, entry->app_type);
4946             *app_type = entry->app_type;
4947             break;
4948         }
4949     }
4950 
4951     if (*app_type == -1) {
4952         ALOGV("%s no match found, return default", __func__);
4953         return platform_get_default_app_type_v2(platform, uc_type, app_type);
4954     }
4955     return 0;
4956 }
4957 
platform_set_sidetone(struct audio_device * adev,snd_device_t out_snd_device,bool enable,char * str)4958 int platform_set_sidetone(struct audio_device *adev,
4959                           snd_device_t out_snd_device,
4960                           bool enable, char *str)
4961 {
4962     int ret;
4963     if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
4964         out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
4965             ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
4966             if (ret)
4967                 ALOGI("%s: usb device %d does not support device sidetone\n",
4968                   __func__, out_snd_device);
4969     } else {
4970         ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
4971               __func__, out_snd_device, str);
4972         if (enable)
4973             audio_route_apply_and_update_path(adev->audio_route, str);
4974         else
4975             audio_route_reset_and_update_path(adev->audio_route, str);
4976     }
4977     return 0;
4978 }
4979 
platform_get_mmap_data_fd(void * platform __unused,int fe_dev __unused,int dir __unused,int * fd __unused,uint32_t * size __unused)4980 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
4981                               int *fd __unused, uint32_t *size __unused)
4982 {
4983 #if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
4984     struct platform_data *my_data = (struct platform_data *)platform;
4985     struct audio_device *adev = my_data->adev;
4986     int hw_fd = -1;
4987     char dev_name[128];
4988     struct snd_pcm_mmap_fd mmap_fd;
4989     memset(&mmap_fd, 0, sizeof(mmap_fd));
4990     mmap_fd.dir = dir;
4991     snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
4992              adev->snd_card, HWDEP_FE_BASE+fe_dev);
4993     hw_fd = open(dev_name, O_RDONLY);
4994     if (hw_fd < 0) {
4995         ALOGE("fe hw dep node open %d/%d failed", adev->snd_card, fe_dev);
4996         return -1;
4997     }
4998     if (ioctl(hw_fd, SNDRV_PCM_IOCTL_MMAP_DATA_FD, &mmap_fd) < 0) {
4999         ALOGE("fe hw dep node ioctl failed");
5000         close(hw_fd);
5001         return -1;
5002     }
5003     *fd = mmap_fd.fd;
5004     *size = mmap_fd.size;
5005     close(hw_fd); // mmap_fd should still be valid
5006     return 0;
5007 #else
5008     return -1;
5009 #endif
5010 }
5011 
platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id)5012 bool platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id)
5013 {
5014     bool needs_event = false;
5015 
5016     switch (uc_id) {
5017     /* concurrent capture usecases which needs event */
5018     case USECASE_AUDIO_RECORD:
5019     case USECASE_AUDIO_RECORD_LOW_LATENCY:
5020     case USECASE_AUDIO_RECORD_MMAP:
5021     case USECASE_AUDIO_RECORD_HIFI:
5022     case USECASE_AUDIO_RECORD_VOIP:
5023     case USECASE_VOICEMMODE1_CALL:
5024     case USECASE_VOICEMMODE2_CALL:
5025     /* concurrent playback usecases that needs event */
5026     case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
5027     case USECASE_AUDIO_PLAYBACK_OFFLOAD:
5028         needs_event = true;
5029         break;
5030     default:
5031         ALOGV("%s:usecase_id[%d] no need to raise event.", __func__, uc_id);
5032     }
5033     return needs_event;
5034 }
5035 
platform_snd_device_has_speaker(snd_device_t dev)5036 bool platform_snd_device_has_speaker(snd_device_t dev) {
5037     int num_devs = 2;
5038     snd_device_t split_devs[2] = {SND_DEVICE_NONE, SND_DEVICE_NONE};
5039     if (platform_can_split_snd_device(dev, &num_devs, split_devs) == 0) {
5040         return platform_snd_device_has_speaker(split_devs[0]) ||
5041                 platform_snd_device_has_speaker(split_devs[1]);
5042     }
5043 
5044     switch (dev) {
5045         case SND_DEVICE_OUT_SPEAKER:
5046         case SND_DEVICE_OUT_SPEAKER_SAFE:
5047         case SND_DEVICE_OUT_SPEAKER_REVERSE:
5048         case SND_DEVICE_OUT_SPEAKER_PROTECTED:
5049         case SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED:
5050         case SND_DEVICE_OUT_VOICE_SPEAKER_HFP:
5051             return true;
5052         default:
5053             break;
5054     }
5055     return false;
5056 }
5057 
platform_set_microphone_characteristic(void * platform,struct audio_microphone_characteristic_t mic)5058 bool platform_set_microphone_characteristic(void *platform,
5059                                             struct audio_microphone_characteristic_t mic) {
5060     struct platform_data *my_data = (struct platform_data *)platform;
5061     if (my_data->declared_mic_count >= AUDIO_MICROPHONE_MAX_COUNT) {
5062         ALOGE("mic number is more than maximum number");
5063         return false;
5064     }
5065     for (size_t ch = 0; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
5066         mic.channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
5067     }
5068     my_data->microphones[my_data->declared_mic_count++] = mic;
5069     return true;
5070 }
5071 
platform_get_microphones(void * platform,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)5072 int platform_get_microphones(void *platform,
5073                              struct audio_microphone_characteristic_t *mic_array,
5074                              size_t *mic_count) {
5075     struct platform_data *my_data = (struct platform_data *)platform;
5076     if (mic_count == NULL) {
5077         return -EINVAL;
5078     }
5079     if (mic_array == NULL) {
5080         return -EINVAL;
5081     }
5082 
5083     if (*mic_count == 0) {
5084         *mic_count = my_data->declared_mic_count;
5085         return 0;
5086     }
5087 
5088     size_t max_mic_count = *mic_count;
5089     size_t actual_mic_count = 0;
5090     for (size_t i = 0; i < max_mic_count && i < my_data->declared_mic_count; i++) {
5091         mic_array[i] = my_data->microphones[i];
5092         actual_mic_count++;
5093     }
5094     *mic_count = actual_mic_count;
5095     return 0;
5096 }
5097 
platform_set_microphone_map(void * platform,snd_device_t in_snd_device,const struct mic_info * info)5098 bool platform_set_microphone_map(void *platform, snd_device_t in_snd_device,
5099                                  const struct mic_info *info) {
5100     struct platform_data *my_data = (struct platform_data *)platform;
5101     if (in_snd_device < SND_DEVICE_IN_BEGIN || in_snd_device >= SND_DEVICE_IN_END) {
5102         ALOGE("%s: Sound device not valid", __func__);
5103         return false;
5104     }
5105     size_t m_count = my_data->mic_map[in_snd_device].mic_count++;
5106     if (m_count >= AUDIO_MICROPHONE_MAX_COUNT) {
5107         ALOGE("%s: Microphone count is greater than max allowed value", __func__);
5108         my_data->mic_map[in_snd_device].mic_count--;
5109         return false;
5110     }
5111     my_data->mic_map[in_snd_device].microphones[m_count] = *info;
5112     return true;
5113 }
5114 
platform_get_active_microphones(void * platform,unsigned int channels,audio_usecase_t uc_id,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)5115 int platform_get_active_microphones(void *platform, unsigned int channels,
5116                                     audio_usecase_t uc_id,
5117                                     struct audio_microphone_characteristic_t *mic_array,
5118                                     size_t *mic_count) {
5119     struct platform_data *my_data = (struct platform_data *)platform;
5120     struct audio_usecase *usecase = get_usecase_from_list(my_data->adev, uc_id);
5121     if (mic_count == NULL || mic_array == NULL || usecase == NULL) {
5122         return -EINVAL;
5123     }
5124     size_t max_mic_count = my_data->declared_mic_count;
5125     size_t actual_mic_count = 0;
5126 
5127     snd_device_t active_input_snd_device =
5128             platform_get_input_snd_device(platform, usecase->stream.in, AUDIO_DEVICE_NONE);
5129     if (active_input_snd_device == SND_DEVICE_NONE) {
5130         ALOGI("%s: No active microphones found", __func__);
5131         goto end;
5132     }
5133 
5134     size_t  active_mic_count = my_data->mic_map[active_input_snd_device].mic_count;
5135     struct mic_info *m_info = my_data->mic_map[active_input_snd_device].microphones;
5136 
5137     for (size_t i = 0; i < active_mic_count; i++) {
5138         unsigned int channels_for_active_mic = channels;
5139         if (channels_for_active_mic > m_info[i].channel_count) {
5140             channels_for_active_mic = m_info[i].channel_count;
5141         }
5142         for (size_t j = 0; j < max_mic_count; j++) {
5143             if (strcmp(my_data->microphones[j].device_id,
5144                        m_info[i].device_id) == 0) {
5145                 mic_array[actual_mic_count] = my_data->microphones[j];
5146                 for (size_t ch = 0; ch < channels_for_active_mic; ch++) {
5147                      mic_array[actual_mic_count].channel_mapping[ch] =
5148                              m_info[i].channel_mapping[ch];
5149                 }
5150                 actual_mic_count++;
5151                 break;
5152             }
5153         }
5154     }
5155 end:
5156     *mic_count = actual_mic_count;
5157     return 0;
5158 }
5159 
platform_set_usb_service_interval(void * platform,bool playback,unsigned long service_interval,bool * reconfig)5160 int platform_set_usb_service_interval(void *platform,
5161                                       bool playback,
5162                                       unsigned long service_interval,
5163                                       bool *reconfig)
5164 {
5165 #if defined (USB_SERVICE_INTERVAL_ENABLED)
5166     struct platform_data *_platform = (struct platform_data *)platform;
5167     *reconfig = false;
5168     if (!playback) {
5169         ALOGE("%s not valid for capture", __func__);
5170         return -1;
5171     }
5172     const char *ctl_name = "USB_AUDIO_RX service_interval";
5173     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
5174                                                   ctl_name);
5175     if (!ctl) {
5176         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
5177         return -1;
5178     }
5179     if (mixer_ctl_get_value(ctl, 0) != (int)service_interval) {
5180         mixer_ctl_set_value(ctl, 0, service_interval);
5181         *reconfig = true;
5182     }
5183     return 0;
5184 #else
5185     *reconfig = false;
5186     (void)platform;
5187     (void)playback;
5188     (void)service_interval;
5189     return -1;
5190 #endif
5191 }
5192 
platform_get_usb_service_interval(void * platform,bool playback,unsigned long * service_interval)5193 int platform_get_usb_service_interval(void *platform,
5194                                       bool playback,
5195                                       unsigned long *service_interval)
5196 {
5197 #if defined (USB_SERVICE_INTERVAL_ENABLED)
5198     struct platform_data *_platform = (struct platform_data *)platform;
5199     if (!playback) {
5200         ALOGE("%s not valid for capture", __func__);
5201         return -1;
5202     }
5203     const char *ctl_name = "USB_AUDIO_RX service_interval";
5204     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
5205                                                   ctl_name);
5206     if (!ctl) {
5207         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
5208         return -1;
5209     }
5210     *service_interval = mixer_ctl_get_value(ctl, 0);
5211     return 0;
5212 #else
5213     (void)platform;
5214     (void)playback;
5215     (void)service_interval;
5216     return -1;
5217 #endif
5218 }
5219 
platform_set_acdb_metainfo_key(void * platform __unused,char * name __unused,int key __unused)5220 int platform_set_acdb_metainfo_key(void *platform __unused,
5221                                    char *name __unused,
5222                                    int key __unused)
5223 {
5224     return -ENOSYS;
5225 }
5226