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