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