• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     property_get("ro.qc.sdk.audio.fluencetype", value, "none");
1809     if (!strcmp("fluencepro", value)) {
1810         my_data->fluence_type = FLUENCE_PRO_ENABLE;
1811     } else if (!strcmp("fluence", value) || (dual_mic_config)) {
1812         my_data->fluence_type = FLUENCE_ENABLE;
1813     } else if (!strcmp("none", value)) {
1814         my_data->fluence_type = FLUENCE_DISABLE;
1815     }
1816 
1817     if (my_data->fluence_type != FLUENCE_DISABLE) {
1818         property_get("persist.audio.fluence.voicecall",value,"");
1819         if (!strcmp("true", value)) {
1820             my_data->fluence_in_voice_call = true;
1821         }
1822 
1823         property_get("persist.audio.fluence.voicecomm",value,"");
1824         if (!strcmp("true", value)) {
1825             my_data->fluence_in_voice_comm = true;
1826         }
1827 
1828         property_get("persist.audio.fluence.voicerec",value,"");
1829         if (!strcmp("true", value)) {
1830             my_data->fluence_in_voice_rec = true;
1831         }
1832 
1833         property_get("persist.audio.fluence.speaker",value,"");
1834         if (!strcmp("true", value)) {
1835             my_data->fluence_in_spkr_mode = true;
1836         }
1837     }
1838 
1839     // support max to mono, example if max count is 3, usecase supports Three, dual and mono mic
1840     switch (my_data->max_mic_count) {
1841         case 4:
1842             my_data->source_mic_type |= SOURCE_QUAD_MIC;
1843         case 3:
1844             my_data->source_mic_type |= SOURCE_THREE_MIC;
1845         case 2:
1846             my_data->source_mic_type |= SOURCE_DUAL_MIC;
1847         case 1:
1848             my_data->source_mic_type |= SOURCE_MONO_MIC;
1849             break;
1850         default:
1851             ALOGE("%s: max_mic_count (%d), is not supported, setting to default",
1852                    __func__, my_data->max_mic_count);
1853             my_data->source_mic_type = SOURCE_MONO_MIC|SOURCE_DUAL_MIC;
1854             break;
1855         }
1856 
1857     ALOGV("%s: Fluence_Type(%d) max_mic_count(%d) mic_type(0x%x) fluence_in_voice_call(%d)"
1858           " fluence_in_voice_comm(%d) fluence_in_voice_rec(%d) fluence_in_spkr_mode(%d) ",
1859           __func__, my_data->fluence_type, my_data->max_mic_count, my_data->source_mic_type,
1860           my_data->fluence_in_voice_call, my_data->fluence_in_voice_comm,
1861           my_data->fluence_in_voice_rec, my_data->fluence_in_spkr_mode);
1862 
1863     my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
1864     if (my_data->acdb_handle == NULL) {
1865         ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
1866     } else {
1867         ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
1868         my_data->acdb_deallocate = (acdb_deallocate_t)dlsym(my_data->acdb_handle,
1869                                                     "acdb_loader_deallocate_ACDB");
1870         if (!my_data->acdb_deallocate)
1871             ALOGE("%s: Could not find the symbol acdb_loader_deallocate_ACDB from %s",
1872                   __func__, LIB_ACDB_LOADER);
1873 
1874         my_data->acdb_send_audio_cal_v3 = (acdb_send_audio_cal_v3_t)dlsym(my_data->acdb_handle,
1875                                                     "acdb_loader_send_audio_cal_v3");
1876         if (!my_data->acdb_send_audio_cal_v3)
1877             ALOGE("%s: Could not find the symbol acdb_send_audio_cal_v3 from %s",
1878                   __func__, LIB_ACDB_LOADER);
1879 
1880         my_data->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(my_data->acdb_handle,
1881                                                     "acdb_loader_send_audio_cal");
1882         if (!my_data->acdb_send_audio_cal)
1883             ALOGE("%s: Could not find the symbol acdb_send_audio_cal from %s",
1884                   __func__, LIB_ACDB_LOADER);
1885 
1886         my_data->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(my_data->acdb_handle,
1887                                                     "acdb_loader_send_voice_cal");
1888         if (!my_data->acdb_send_voice_cal)
1889             ALOGE("%s: Could not find the symbol acdb_loader_send_voice_cal from %s",
1890                   __func__, LIB_ACDB_LOADER);
1891 
1892         my_data->acdb_reload_vocvoltable = (acdb_reload_vocvoltable_t)dlsym(my_data->acdb_handle,
1893                                                     "acdb_loader_reload_vocvoltable");
1894         if (!my_data->acdb_reload_vocvoltable)
1895             ALOGE("%s: Could not find the symbol acdb_loader_reload_vocvoltable from %s",
1896                   __func__, LIB_ACDB_LOADER);
1897 
1898         my_data->acdb_send_gain_dep_cal = (acdb_send_gain_dep_cal_t)dlsym(my_data->acdb_handle,
1899                                                     "acdb_loader_send_gain_dep_cal");
1900         if (!my_data->acdb_send_gain_dep_cal)
1901             ALOGV("%s: Could not find the symbol acdb_loader_send_gain_dep_cal from %s",
1902                   __func__, LIB_ACDB_LOADER);
1903 
1904 #if defined (FLICKER_SENSOR_INPUT)
1905         configure_flicker_sensor_input(adev->mixer);
1906 #endif
1907 
1908 #if defined (PLATFORM_MSM8994) || (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
1909         acdb_init_v2_cvd_t acdb_init_local;
1910         acdb_init_local = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle,
1911                                               "acdb_loader_init_v2");
1912         if (acdb_init_local == NULL)
1913             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
1914                   dlerror());
1915 
1916 #elif defined (PLATFORM_MSM8084)
1917         acdb_init_v2_t acdb_init_local;
1918         acdb_init_local = (acdb_init_v2_t)dlsym(my_data->acdb_handle,
1919                                           "acdb_loader_init_v2");
1920         if (acdb_init_local == NULL)
1921             ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__,
1922                   dlerror());
1923 
1924 #else
1925         acdb_init_t acdb_init_local;
1926         acdb_init_local = (acdb_init_t)dlsym(my_data->acdb_handle,
1927                                                     "acdb_loader_init_ACDB");
1928         if (acdb_init_local == NULL)
1929             ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__,
1930                   dlerror());
1931 #endif
1932         my_data->acdb_init = acdb_init_local;
1933 
1934         my_data->acdb_send_custom_top = (acdb_send_custom_top_t)
1935                                         dlsym(my_data->acdb_handle,
1936                                               "acdb_loader_send_common_custom_topology");
1937 
1938         if (!my_data->acdb_send_custom_top)
1939             ALOGE("%s: Could not find the symbol acdb_get_default_app_type from %s",
1940                   __func__, LIB_ACDB_LOADER);
1941 
1942         my_data->acdb_set_audio_cal = (acdb_set_audio_cal_t)dlsym(my_data->acdb_handle,
1943                                                     "acdb_loader_set_audio_cal_v2");
1944         if (!my_data->acdb_set_audio_cal)
1945             ALOGE("%s: Could not find the symbol acdb_set_audio_cal_v2 from %s",
1946                   __func__, LIB_ACDB_LOADER);
1947 
1948         int result = acdb_init(adev->snd_card);
1949         if (!result) {
1950             my_data->acdb_initialized = true;
1951             ALOGD("ACDB initialized");
1952         } else {
1953             my_data->acdb_initialized = false;
1954             ALOGD("ACDB initialization failed");
1955         }
1956     }
1957 
1958     /* init usb */
1959     audio_extn_usb_init(adev);
1960 
1961     /* init a2dp */
1962     audio_extn_a2dp_init(adev);
1963 
1964     audio_extn_spkr_prot_init(adev);
1965 
1966     audio_extn_hwdep_cal_send(adev->snd_card, my_data->acdb_handle);
1967 
1968     /* load csd client */
1969     platform_csd_init(my_data);
1970 
1971     platform_backend_config_init(my_data);
1972 
1973     init_be_dai_name_table(adev);
1974 
1975     if (platform_supports_app_type_cfg())
1976         platform_backend_app_type_cfg_init(my_data, adev->mixer);
1977 
1978     return my_data;
1979 
1980 init_failed:
1981     if (my_data)
1982         free(my_data);
1983     return NULL;
1984 }
1985 
platform_deinit(void * platform)1986 void platform_deinit(void *platform)
1987 {
1988     int32_t dev;
1989     struct operator_info *info_item;
1990     struct operator_specific_device *device_item;
1991     struct external_specific_device *ext_dev;
1992     struct app_type_entry *ap;
1993     struct listnode *node;
1994 
1995     struct platform_data *my_data = (struct platform_data *)platform;
1996     close_csd_client(my_data->csd);
1997 
1998     audio_extn_spkr_prot_deinit(my_data->adev);
1999 
2000     hw_info_deinit(my_data->hw_info);
2001 
2002     for (dev = 0; dev < SND_DEVICE_MAX; dev++) {
2003         if (backend_tag_table[dev])
2004             free(backend_tag_table[dev]);
2005         if (hw_interface_table[dev])
2006             free(hw_interface_table[dev]);
2007         if (operator_specific_device_table[dev]) {
2008             while (!list_empty(operator_specific_device_table[dev])) {
2009                 node = list_head(operator_specific_device_table[dev]);
2010                 list_remove(node);
2011                 device_item = node_to_item(node, struct operator_specific_device, list);
2012                 free(device_item->operator);
2013                 free(device_item->mixer_path);
2014                 free(device_item);
2015             }
2016             free(operator_specific_device_table[dev]);
2017         }
2018 
2019         if (external_specific_device_table[dev]) {
2020             while (!list_empty(external_specific_device_table[dev])) {
2021                 node = list_head(external_specific_device_table[dev]);
2022                 list_remove(node);
2023                 ext_dev = node_to_item(node, struct external_specific_device, list);
2024                 free(ext_dev->usbid);
2025                 free(ext_dev);
2026             }
2027             free(external_specific_device_table[dev]);
2028         }
2029     }
2030 
2031     if (my_data->snd_card_name)
2032         free(my_data->snd_card_name);
2033 
2034     while (!list_empty(&operator_info_list)) {
2035         node = list_head(&operator_info_list);
2036         list_remove(node);
2037         info_item = node_to_item(node, struct operator_info, list);
2038         free(info_item->name);
2039         free(info_item->mccmnc);
2040         free(info_item);
2041     }
2042 
2043     while (!list_empty(&app_type_entry_list)) {
2044         node = list_head(&app_type_entry_list);
2045         list_remove(node);
2046         ap = node_to_item(node, struct app_type_entry, node);
2047         if (ap->mode) free(ap->mode);
2048         free(ap);
2049     }
2050 
2051     mixer_close(my_data->adev->mixer);
2052     free(platform);
2053 
2054     /* deinit usb */
2055     audio_extn_usb_deinit();
2056 }
2057 
platform_get_snd_device_name(snd_device_t snd_device)2058 const char *platform_get_snd_device_name(snd_device_t snd_device)
2059 {
2060     if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
2061         if (operator_specific_device_table[snd_device] != NULL) {
2062             return get_operator_specific_device_mixer_path(snd_device);
2063         }
2064         return device_table[snd_device];
2065     } else
2066         return "none";
2067 }
2068 
platform_get_snd_device_name_extn(void * platform,snd_device_t snd_device,char * device_name)2069 int platform_get_snd_device_name_extn(void *platform, snd_device_t snd_device,
2070                                       char *device_name)
2071 {
2072     struct platform_data *my_data = (struct platform_data *)platform;
2073 
2074     if (platform == NULL) {
2075         ALOGW("%s: something wrong, use legacy get_snd_device name", __func__);
2076         strlcpy(device_name, platform_get_snd_device_name(snd_device),
2077                 DEVICE_NAME_MAX_SIZE);
2078     } else if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) {
2079         if (operator_specific_device_table[snd_device] != NULL) {
2080             strlcpy(device_name, get_operator_specific_device_mixer_path(snd_device),
2081                     DEVICE_NAME_MAX_SIZE);
2082         } else {
2083             strlcpy(device_name, device_table[snd_device], DEVICE_NAME_MAX_SIZE);
2084         }
2085         hw_info_append_hw_type(my_data->hw_info, snd_device, device_name);
2086     } else {
2087         strlcpy(device_name, "none", DEVICE_NAME_MAX_SIZE);
2088         return -EINVAL;
2089     }
2090 
2091     return 0;
2092 }
2093 
platform_add_backend_name(void * platform,char * mixer_path,snd_device_t snd_device)2094 void platform_add_backend_name(void *platform, char *mixer_path,
2095                                snd_device_t snd_device)
2096 {
2097     struct platform_data *my_data = (struct platform_data *)platform;
2098 
2099     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2100         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
2101         return;
2102     }
2103 
2104     const char * suffix = backend_tag_table[snd_device];
2105 
2106     if (suffix != NULL) {
2107         strcat(mixer_path, " ");
2108         strcat(mixer_path, suffix);
2109     }
2110 }
2111 
platform_check_backends_match(snd_device_t snd_device1,snd_device_t snd_device2)2112 bool platform_check_backends_match(snd_device_t snd_device1, snd_device_t snd_device2)
2113 {
2114     ALOGV("%s: snd_device1 = %s, snd_device2 = %s", __func__,
2115                 platform_get_snd_device_name(snd_device1),
2116                 platform_get_snd_device_name(snd_device2));
2117 
2118     if ((snd_device1 < SND_DEVICE_MIN) || (snd_device1 >= SND_DEVICE_MAX)) {
2119         ALOGE("%s: Invalid snd_device = %s", __func__,
2120                 platform_get_snd_device_name(snd_device1));
2121         return false;
2122     }
2123     if ((snd_device2 < SND_DEVICE_MIN) || (snd_device2 >= SND_DEVICE_MAX)) {
2124         ALOGE("%s: Invalid snd_device = %s", __func__,
2125                 platform_get_snd_device_name(snd_device2));
2126         return false;
2127     }
2128 
2129     const char * be_itf1 = hw_interface_table[snd_device1];
2130     const char * be_itf2 = hw_interface_table[snd_device2];
2131     /*
2132       hw_interface_table has overrides for a snd_device.
2133       if there is no entry for a device, assume DEFAULT_RX_BACKEND
2134     */
2135     if (be_itf1 == NULL) {
2136         be_itf1 = DEFAULT_RX_BACKEND;
2137     }
2138     if (be_itf2 == NULL) {
2139         be_itf2 = DEFAULT_RX_BACKEND;
2140     }
2141     ALOGV("%s: be_itf1 = %s, be_itf2 = %s", __func__, be_itf1, be_itf2);
2142     /*
2143       this takes care of finding a device within a combo device pair as well
2144      */
2145     return strstr(be_itf1, be_itf2) != NULL || strstr(be_itf2, be_itf1) != NULL;
2146 }
2147 
platform_get_pcm_device_id(audio_usecase_t usecase,int device_type)2148 int platform_get_pcm_device_id(audio_usecase_t usecase, int device_type)
2149 {
2150     int device_id;
2151     if (device_type == PCM_PLAYBACK)
2152         device_id = pcm_device_table[usecase][0];
2153     else
2154         device_id = pcm_device_table[usecase][1];
2155     return device_id;
2156 }
2157 
platform_get_haptics_pcm_device_id()2158 int platform_get_haptics_pcm_device_id()
2159 {
2160     return HAPTICS_PCM_DEVICE;
2161 }
2162 
find_index(const struct name_to_index * table,int32_t len,const char * name)2163 static int find_index(const struct name_to_index * table, int32_t len,
2164                       const char * name)
2165 {
2166     int ret = 0;
2167     int32_t i;
2168 
2169     if (table == NULL) {
2170         ALOGE("%s: table is NULL", __func__);
2171         ret = -ENODEV;
2172         goto done;
2173     }
2174 
2175     if (name == NULL) {
2176         ALOGE("null key");
2177         ret = -ENODEV;
2178         goto done;
2179     }
2180 
2181     for (i=0; i < len; i++) {
2182         if (!strcmp(table[i].name, name)) {
2183             ret = table[i].index;
2184             goto done;
2185         }
2186     }
2187     ALOGE("%s: Could not find index for name = %s",
2188             __func__, name);
2189     ret = -ENODEV;
2190 done:
2191     return ret;
2192 }
2193 
platform_get_snd_device_index(char * device_name)2194 int platform_get_snd_device_index(char *device_name)
2195 {
2196     return find_index(snd_device_name_index, SND_DEVICE_MAX, device_name);
2197 }
2198 
platform_get_usecase_index(const char * usecase_name)2199 int platform_get_usecase_index(const char *usecase_name)
2200 {
2201     return find_index(usecase_name_index, AUDIO_USECASE_MAX, usecase_name);
2202 }
2203 
platform_get_effect_config_data(snd_device_t snd_device,struct audio_effect_config * effect_config,effect_type_t effect_type)2204 int platform_get_effect_config_data(snd_device_t snd_device,
2205                                       struct audio_effect_config *effect_config,
2206                                       effect_type_t effect_type)
2207 {
2208     int ret = 0;
2209 
2210     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
2211         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
2212         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
2213             __func__, snd_device, effect_type);
2214         ret = -EINVAL;
2215         goto done;
2216     }
2217 
2218     if (effect_config == NULL) {
2219         ALOGE("%s: Invalid effect_config", __func__);
2220         ret = -EINVAL;
2221         goto done;
2222     }
2223 
2224     ALOGV("%s: snd_device = %d module_id = %d",
2225             __func__, snd_device, effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type].module_id);
2226     *effect_config = effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type];
2227 
2228 done:
2229     return ret;
2230 }
2231 
platform_set_effect_config_data(snd_device_t snd_device,struct audio_effect_config effect_config,effect_type_t effect_type)2232 int platform_set_effect_config_data(snd_device_t snd_device,
2233                                       struct audio_effect_config effect_config,
2234                                       effect_type_t effect_type)
2235 {
2236     int ret = 0;
2237 
2238     if ((snd_device < SND_DEVICE_IN_BEGIN) || (snd_device >= SND_DEVICE_MAX) ||
2239         (effect_type <= EFFECT_NONE) || (effect_type >= EFFECT_COUNT)) {
2240         ALOGE("%s: Invalid snd_device = %d or effect_type = %d",
2241             __func__, snd_device, effect_type);
2242         ret = -EINVAL;
2243         goto done;
2244     }
2245 
2246     ALOGV("%s 0x%x 0x%x 0x%x 0x%x", __func__, effect_config.module_id,
2247            effect_config.instance_id, effect_config.param_id,
2248            effect_config.param_value);
2249     effect_config_table[GET_IN_DEVICE_INDEX(snd_device)][effect_type] = effect_config;
2250 
2251 done:
2252     return ret;
2253 }
2254 
platform_add_operator_specific_device(snd_device_t snd_device,const char * operator,const char * mixer_path,unsigned int acdb_id)2255 void platform_add_operator_specific_device(snd_device_t snd_device,
2256                                            const char *operator,
2257                                            const char *mixer_path,
2258                                            unsigned int acdb_id)
2259 {
2260     struct operator_specific_device *device;
2261 
2262     if (operator_specific_device_table[snd_device] == NULL) {
2263         operator_specific_device_table[snd_device] =
2264             (struct listnode *)calloc(1, sizeof(struct listnode));
2265         list_init(operator_specific_device_table[snd_device]);
2266     }
2267 
2268     device = (struct operator_specific_device *)calloc(1, sizeof(struct operator_specific_device));
2269 
2270     device->operator = strdup(operator);
2271     device->mixer_path = strdup(mixer_path);
2272     device->acdb_id = acdb_id;
2273 
2274     list_add_tail(operator_specific_device_table[snd_device], &device->list);
2275 
2276     ALOGD("%s: device[%s] -> operator[%s] mixer_path[%s] acdb_id[%d]", __func__,
2277             platform_get_snd_device_name(snd_device), operator, mixer_path, acdb_id);
2278 
2279 }
2280 
platform_add_external_specific_device(snd_device_t snd_device,const char * usbid,unsigned int acdb_id)2281 void platform_add_external_specific_device(snd_device_t snd_device,
2282                                            const char *usbid,
2283                                            unsigned int acdb_id)
2284 {
2285     struct external_specific_device *device;
2286 
2287     if (external_specific_device_table[snd_device] == NULL) {
2288         external_specific_device_table[snd_device] =
2289             (struct listnode *)calloc(1, sizeof(struct listnode));
2290         list_init(external_specific_device_table[snd_device]);
2291     }
2292 
2293     device = (struct external_specific_device *)calloc(1, sizeof(struct external_specific_device));
2294 
2295     device->usbid = strdup(usbid);
2296     device->acdb_id = acdb_id;
2297 
2298     list_add_tail(external_specific_device_table[snd_device], &device->list);
2299 
2300     ALOGD("%s: device[%s] usbid[%s] -> acdb_id[%d]", __func__,
2301             platform_get_snd_device_name(snd_device), usbid, acdb_id);
2302 }
2303 
2304 
platform_set_snd_device_acdb_id(snd_device_t snd_device,unsigned int acdb_id)2305 int platform_set_snd_device_acdb_id(snd_device_t snd_device, unsigned int acdb_id)
2306 {
2307     int ret = 0;
2308 
2309     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2310         ALOGE("%s: Invalid snd_device = %d",
2311             __func__, snd_device);
2312         ret = -EINVAL;
2313         goto done;
2314     }
2315 
2316     ALOGV("%s: acdb_device_table[%s]: old = %d new = %d", __func__,
2317           platform_get_snd_device_name(snd_device), acdb_device_table[snd_device], acdb_id);
2318     acdb_device_table[snd_device] = acdb_id;
2319 done:
2320     return ret;
2321 }
2322 
platform_get_snd_device_acdb_id(snd_device_t snd_device)2323 int platform_get_snd_device_acdb_id(snd_device_t snd_device)
2324 {
2325     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
2326         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
2327         return -EINVAL;
2328     }
2329 
2330     /*
2331      * If speaker protection is enabled, function returns supported
2332      * sound device for speaker. Else same sound device is returned.
2333      */
2334     snd_device = audio_extn_get_spkr_prot_snd_device(snd_device);
2335 
2336     if (operator_specific_device_table[snd_device] != NULL)
2337         return get_operator_specific_device_acdb_id(snd_device);
2338     else if (external_specific_device_table[snd_device] != NULL)
2339         return get_external_specific_device_acdb_id(snd_device);
2340     else
2341         return acdb_device_table[snd_device];
2342 }
2343 
platform_get_backend_index(snd_device_t snd_device)2344 static int platform_get_backend_index(snd_device_t snd_device)
2345 {
2346     int32_t port = DEFAULT_CODEC_BACKEND;
2347 
2348     if (snd_device >= SND_DEVICE_OUT_BEGIN && snd_device < SND_DEVICE_OUT_END) {
2349         if (backend_tag_table[snd_device] != NULL) {
2350                 if (strncmp(backend_tag_table[snd_device], "headphones",
2351                             sizeof("headphones")) == 0)
2352                         port = HEADPHONE_BACKEND;
2353                 else if (strcmp(backend_tag_table[snd_device], "hdmi") == 0)
2354                         port = HDMI_RX_BACKEND;
2355                 else if ((strcmp(backend_tag_table[snd_device], "usb-headphones") == 0) ||
2356                            (strcmp(backend_tag_table[snd_device], "usb-headset") == 0))
2357                         port = USB_AUDIO_RX_BACKEND;
2358         }
2359     } else if (snd_device >= SND_DEVICE_IN_BEGIN && snd_device < SND_DEVICE_IN_END) {
2360         port = DEFAULT_CODEC_TX_BACKEND;
2361         if (backend_tag_table[snd_device] != NULL) {
2362                 if (strcmp(backend_tag_table[snd_device], "usb-headset-mic") == 0)
2363                         port = USB_AUDIO_TX_BACKEND;
2364                 else if (strstr(backend_tag_table[snd_device], "bt-sco") != NULL)
2365                         port = BT_SCO_TX_BACKEND;
2366         }
2367     } else {
2368         ALOGW("%s:napb: Invalid device - %d ", __func__, snd_device);
2369     }
2370 
2371     ALOGV("%s:napb: backend port - %d device - %d ", __func__, port, snd_device);
2372 
2373     return port;
2374 }
2375 
platform_send_audio_calibration(void * platform,snd_device_t snd_device)2376 int platform_send_audio_calibration(void *platform, snd_device_t snd_device)
2377 {
2378     struct platform_data *my_data = (struct platform_data *)platform;
2379     int acdb_dev_id, acdb_dev_type;
2380 
2381     if (platform_supports_app_type_cfg()) // use v2 instead
2382         return -ENOSYS;
2383 
2384     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
2385     if (acdb_dev_id < 0) {
2386         ALOGE("%s: Could not find acdb id for device(%d)",
2387               __func__, snd_device);
2388         return -EINVAL;
2389     }
2390     if (my_data->acdb_send_audio_cal) {
2391         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
2392               __func__, snd_device, acdb_dev_id);
2393         if (snd_device >= SND_DEVICE_OUT_BEGIN &&
2394                 snd_device < SND_DEVICE_OUT_END)
2395             acdb_dev_type = ACDB_DEV_TYPE_OUT;
2396         else
2397             acdb_dev_type = ACDB_DEV_TYPE_IN;
2398         my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
2399     }
2400     return 0;
2401 }
2402 
platform_send_audio_calibration_v2(void * platform,struct audio_usecase * usecase,int app_type,int sample_rate)2403 int platform_send_audio_calibration_v2(void *platform, struct audio_usecase *usecase,
2404                                        int app_type, int sample_rate)
2405 {
2406     struct platform_data *my_data = (struct platform_data *)platform;
2407     int acdb_dev_id, acdb_dev_type;
2408     int snd_device = usecase->out_snd_device;
2409     int new_snd_device[SND_DEVICE_OUT_END] = {0};
2410     int i, num_devices = 1;
2411 
2412     if (!platform_supports_app_type_cfg()) // use v1 instead
2413         return -ENOSYS;
2414 
2415     if ((usecase->type == PCM_HFP_CALL) || (usecase->type == PCM_CAPTURE))
2416         snd_device = usecase->in_snd_device;
2417 
2418     // skipped over get_spkr_prot_device
2419     acdb_dev_id = platform_get_snd_device_acdb_id(snd_device);
2420     if (acdb_dev_id < 0) {
2421         ALOGE("%s: Could not find acdb id for device(%d)",
2422               __func__, snd_device);
2423         return -EINVAL;
2424     }
2425 
2426     if (platform_can_split_snd_device(snd_device,
2427                                       &num_devices, new_snd_device) < 0) {
2428         new_snd_device[0] = snd_device;
2429     }
2430 
2431     for (i = 0; i < num_devices; i++) {
2432         acdb_dev_id = platform_get_snd_device_acdb_id(new_snd_device[i]);
2433         if (acdb_dev_id < 0) {
2434             ALOGE("%s: Could not find acdb id for device(%d)",
2435                   __func__, new_snd_device[i]);
2436             return -EINVAL;
2437         }
2438         ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
2439               __func__, new_snd_device[i], acdb_dev_id);
2440         if (new_snd_device[i] >= SND_DEVICE_OUT_BEGIN &&
2441                 new_snd_device[i] < SND_DEVICE_OUT_END)
2442             acdb_dev_type = ACDB_DEV_TYPE_OUT;
2443         else
2444             acdb_dev_type = ACDB_DEV_TYPE_IN;
2445 
2446         if (my_data->acdb_send_audio_cal_v3) {
2447             my_data->acdb_send_audio_cal_v3(acdb_dev_id, acdb_dev_type,
2448                                             app_type, sample_rate, i);
2449         } else if (my_data->acdb_send_audio_cal) {
2450             my_data->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type); // this version differs from internal
2451         }
2452     }
2453 
2454     return 0;
2455 }
2456 
2457 
platform_switch_voice_call_device_pre(void * platform)2458 int platform_switch_voice_call_device_pre(void *platform)
2459 {
2460     struct platform_data *my_data = (struct platform_data *)platform;
2461     int ret = 0;
2462 
2463     if (my_data->csd != NULL &&
2464         voice_is_in_call(my_data->adev)) {
2465         /* This must be called before disabling mixer controls on APQ side */
2466         ret = my_data->csd->disable_device();
2467         if (ret < 0) {
2468             ALOGE("%s: csd_client_disable_device, failed, error %d",
2469                   __func__, ret);
2470         }
2471     }
2472     return ret;
2473 }
2474 
platform_switch_voice_call_enable_device_config(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2475 int platform_switch_voice_call_enable_device_config(void *platform,
2476                                                     snd_device_t out_snd_device,
2477                                                     snd_device_t in_snd_device)
2478 {
2479     struct platform_data *my_data = (struct platform_data *)platform;
2480     int acdb_rx_id, acdb_tx_id;
2481     int ret = 0;
2482 
2483     if (my_data->csd == NULL)
2484         return ret;
2485 
2486     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2487     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2488 
2489     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
2490         ret = my_data->csd->enable_device_config(acdb_rx_id, acdb_tx_id);
2491         if (ret < 0) {
2492             ALOGE("%s: csd_enable_device_config, failed, error %d",
2493                   __func__, ret);
2494         }
2495     } else {
2496         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2497               acdb_rx_id, acdb_tx_id);
2498     }
2499 
2500     return ret;
2501 }
2502 
platform_switch_voice_call_device_post(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2503 int platform_switch_voice_call_device_post(void *platform,
2504                                            snd_device_t out_snd_device,
2505                                            snd_device_t in_snd_device)
2506 {
2507     struct platform_data *my_data = (struct platform_data *)platform;
2508     int acdb_rx_id, acdb_tx_id;
2509 
2510     if (my_data->acdb_send_voice_cal == NULL) {
2511         ALOGE("%s: dlsym error for acdb_send_voice_call", __func__);
2512     } else {
2513         acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2514         acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2515 
2516         if (acdb_rx_id > 0 && acdb_tx_id > 0)
2517             my_data->acdb_send_voice_cal(acdb_rx_id, acdb_tx_id);
2518         else
2519             ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2520                   acdb_rx_id, acdb_tx_id);
2521     }
2522 
2523     return 0;
2524 }
2525 
platform_switch_voice_call_usecase_route_post(void * platform,snd_device_t out_snd_device,snd_device_t in_snd_device)2526 int platform_switch_voice_call_usecase_route_post(void *platform,
2527                                                   snd_device_t out_snd_device,
2528                                                   snd_device_t in_snd_device)
2529 {
2530     struct platform_data *my_data = (struct platform_data *)platform;
2531     int acdb_rx_id, acdb_tx_id;
2532     int ret = 0;
2533 
2534     if (my_data->csd == NULL)
2535         return ret;
2536 
2537     acdb_rx_id = platform_get_snd_device_acdb_id(out_snd_device);
2538     acdb_tx_id = platform_get_snd_device_acdb_id(in_snd_device);
2539 
2540     if (acdb_rx_id > 0 && acdb_tx_id > 0) {
2541         ret = my_data->csd->enable_device(acdb_rx_id, acdb_tx_id,
2542                                           my_data->adev->acdb_settings);
2543         if (ret < 0) {
2544             ALOGE("%s: csd_enable_device, failed, error %d", __func__, ret);
2545         }
2546     } else {
2547         ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
2548               acdb_rx_id, acdb_tx_id);
2549     }
2550 
2551     return ret;
2552 }
2553 
platform_start_voice_call(void * platform,uint32_t vsid)2554 int platform_start_voice_call(void *platform, uint32_t vsid)
2555 {
2556     struct platform_data *my_data = (struct platform_data *)platform;
2557     int ret = 0;
2558 
2559     if (my_data->csd != NULL) {
2560         ret = my_data->csd->start_voice(vsid);
2561         if (ret < 0) {
2562             ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
2563         }
2564     }
2565     return ret;
2566 }
2567 
platform_stop_voice_call(void * platform,uint32_t vsid)2568 int platform_stop_voice_call(void *platform, uint32_t vsid)
2569 {
2570     struct platform_data *my_data = (struct platform_data *)platform;
2571     int ret = 0;
2572 
2573     if (my_data->csd != NULL) {
2574         ret = my_data->csd->stop_voice(vsid);
2575         if (ret < 0) {
2576             ALOGE("%s: csd_stop_voice error %d\n", __func__, ret);
2577         }
2578     }
2579     return ret;
2580 }
2581 
platform_set_mic_break_det(void * platform,bool enable)2582 int platform_set_mic_break_det(void *platform, bool enable)
2583 {
2584     int ret = 0;
2585     struct platform_data *my_data = (struct platform_data *)platform;
2586     struct audio_device *adev = my_data->adev;
2587     const char *mixer_ctl_name = "Voice Mic Break Enable";
2588     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2589     if (!ctl) {
2590         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2591               __func__, mixer_ctl_name);
2592         return -EINVAL;
2593     }
2594 
2595     ret = mixer_ctl_set_value(ctl, 0, enable);
2596     if(ret)
2597         ALOGE("%s: Failed to set mixer ctl: %s", __func__, mixer_ctl_name);
2598 
2599     return ret;
2600 }
2601 
platform_get_sample_rate(void * platform,uint32_t * rate)2602 int platform_get_sample_rate(void *platform, uint32_t *rate)
2603 {
2604     struct platform_data *my_data = (struct platform_data *)platform;
2605     int ret = 0;
2606 
2607     if (my_data->csd != NULL) {
2608         ret = my_data->csd->get_sample_rate(rate);
2609         if (ret < 0) {
2610             ALOGE("%s: csd_get_sample_rate error %d\n", __func__, ret);
2611         }
2612     }
2613     return ret;
2614 }
2615 
platform_set_speaker_gain_in_combo(struct audio_device * adev,snd_device_t snd_device,bool enable)2616 void platform_set_speaker_gain_in_combo(struct audio_device *adev,
2617                                         snd_device_t snd_device,
2618                                         bool enable)
2619 {
2620     const char* name;
2621     switch (snd_device) {
2622         case SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES:
2623             if (enable)
2624                 name = "spkr-gain-in-headphone-combo";
2625             else
2626                 name = "speaker-gain-default";
2627             break;
2628         case SND_DEVICE_OUT_SPEAKER_AND_LINE:
2629             if (enable)
2630                 name = "spkr-gain-in-line-combo";
2631             else
2632                 name = "speaker-gain-default";
2633             break;
2634         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES:
2635             if (enable)
2636                 name = "spkr-safe-gain-in-headphone-combo";
2637             else
2638                 name = "speaker-safe-gain-default";
2639             break;
2640         case SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE:
2641             if (enable)
2642                 name = "spkr-safe-gain-in-line-combo";
2643             else
2644                 name = "speaker-safe-gain-default";
2645             break;
2646         default:
2647             return;
2648     }
2649 
2650     audio_route_apply_and_update_path(adev->audio_route, name);
2651 }
2652 
platform_set_voice_volume(void * platform,int volume)2653 int platform_set_voice_volume(void *platform, int volume)
2654 {
2655     struct platform_data *my_data = (struct platform_data *)platform;
2656     struct audio_device *adev = my_data->adev;
2657     struct mixer_ctl *ctl;
2658     const char *mixer_ctl_name = "Voice Rx Gain";
2659     const char *mute_mixer_ctl_name = "Voice Rx Device Mute";
2660     int vol_index = 0, ret = 0;
2661     uint32_t set_values[ ] = {0,
2662                               ALL_SESSION_VSID,
2663                               DEFAULT_VOLUME_RAMP_DURATION_MS};
2664 
2665     // Voice volume levels are mapped to adsp volume levels as follows.
2666     // 100 -> 5, 80 -> 4, 60 -> 3, 40 -> 2, 20 -> 1  0 -> 0
2667     // But this values don't changed in kernel. So, below change is need.
2668     vol_index = (int)percent_to_index(volume, MIN_VOL_INDEX, my_data->max_vol_index);
2669     set_values[0] = vol_index;
2670 
2671     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2672     if (!ctl) {
2673         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2674               __func__, mixer_ctl_name);
2675         return -EINVAL;
2676     }
2677     ALOGV("Setting voice volume index: %d", set_values[0]);
2678     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2679 
2680     // Send mute command in case volume index is max since indexes are inverted
2681     // for mixer controls.
2682     if (vol_index == my_data->max_vol_index) {
2683         set_values[0] = 1;
2684     }
2685     else {
2686         set_values[0] = 0;
2687     }
2688 
2689     ctl = mixer_get_ctl_by_name(adev->mixer, mute_mixer_ctl_name);
2690     if (!ctl) {
2691         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2692               __func__, mute_mixer_ctl_name);
2693         return -EINVAL;
2694     }
2695     ALOGV("%s: Setting RX Device Mute to: %d", __func__, set_values[0]);
2696     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2697 
2698     if (my_data->csd != NULL) {
2699         ret = my_data->csd->volume(ALL_SESSION_VSID, volume,
2700                                    DEFAULT_VOLUME_RAMP_DURATION_MS);
2701         if (ret < 0) {
2702             ALOGE("%s: csd_volume error %d", __func__, ret);
2703         }
2704     }
2705     return ret;
2706 }
2707 
platform_set_mic_mute(void * platform,bool state)2708 int platform_set_mic_mute(void *platform, bool state)
2709 {
2710     struct platform_data *my_data = (struct platform_data *)platform;
2711     struct audio_device *adev = my_data->adev;
2712     struct mixer_ctl *ctl;
2713     const char *mixer_ctl_name = "Voice Tx Mute";
2714     int ret = 0;
2715     uint32_t set_values[ ] = {0,
2716                               ALL_SESSION_VSID,
2717                               DEFAULT_MUTE_RAMP_DURATION_MS};
2718 
2719     if (adev->mode != AUDIO_MODE_IN_CALL &&
2720         adev->mode != AUDIO_MODE_IN_COMMUNICATION)
2721         return 0;
2722 
2723     if (adev->enable_hfp)
2724         mixer_ctl_name = "HFP Tx Mute";
2725 
2726     set_values[0] = state;
2727     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2728     if (!ctl) {
2729         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2730               __func__, mixer_ctl_name);
2731         return -EINVAL;
2732     }
2733     ALOGV("%s: Setting voice mute state: %d", __func__, state);
2734     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2735 
2736     if (my_data->csd != NULL) {
2737         ret = my_data->csd->mic_mute(ALL_SESSION_VSID, state,
2738                                      DEFAULT_MUTE_RAMP_DURATION_MS);
2739         if (ret < 0) {
2740             ALOGE("%s: csd_mic_mute error %d", __func__, ret);
2741         }
2742     }
2743     return ret;
2744 }
2745 
platform_set_device_mute(void * platform,bool state,char * dir)2746 int platform_set_device_mute(void *platform, bool state, char *dir)
2747 {
2748     struct platform_data *my_data = (struct platform_data *)platform;
2749     struct audio_device *adev = my_data->adev;
2750     struct mixer_ctl *ctl;
2751     char *mixer_ctl_name = NULL;
2752     int ret = 0;
2753     uint32_t set_values[ ] = {0,
2754                               ALL_SESSION_VSID,
2755                               0};
2756     if(dir == NULL) {
2757         ALOGE("%s: Invalid direction:%s", __func__, dir);
2758         return -EINVAL;
2759     }
2760 
2761     if (!strncmp("rx", dir, sizeof("rx"))) {
2762         mixer_ctl_name = "Voice Rx Device Mute";
2763     } else if (!strncmp("tx", dir, sizeof("tx"))) {
2764         mixer_ctl_name = "Voice Tx Device Mute";
2765     } else {
2766         return -EINVAL;
2767     }
2768 
2769     set_values[0] = state;
2770     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2771     if (!ctl) {
2772         ALOGE("%s: Could not get ctl for mixer cmd - %s",
2773               __func__, mixer_ctl_name);
2774         return -EINVAL;
2775     }
2776 
2777     ALOGV("%s: Setting device mute state: %d, mixer ctrl:%s",
2778           __func__,state, mixer_ctl_name);
2779     mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values));
2780 
2781     return ret;
2782 }
2783 
platform_can_split_snd_device(snd_device_t snd_device,int * num_devices,snd_device_t * new_snd_devices)2784 int platform_can_split_snd_device(snd_device_t snd_device,
2785                                   int *num_devices,
2786                                   snd_device_t *new_snd_devices)
2787 {
2788     int ret = -EINVAL;
2789     if (NULL == num_devices || NULL == new_snd_devices) {
2790         ALOGE("%s: NULL pointer ..", __func__);
2791         return -EINVAL;
2792     }
2793 
2794     /*
2795      * If wired headset/headphones/line devices share the same backend
2796      * with speaker/earpiece this routine returns -EINVAL.
2797      */
2798     if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES &&
2799         !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_HEADPHONES)) {
2800         *num_devices = 2;
2801         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2802         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
2803         ret = 0;
2804     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_LINE &&
2805                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_LINE)) {
2806         *num_devices = 2;
2807         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2808         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
2809         ret = 0;
2810     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES &&
2811                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_HEADPHONES)) {
2812         *num_devices = 2;
2813         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2814         new_snd_devices[1] = SND_DEVICE_OUT_HEADPHONES;
2815         ret = 0;
2816     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE &&
2817                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_LINE)) {
2818         *num_devices = 2;
2819         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2820         new_snd_devices[1] = SND_DEVICE_OUT_LINE;
2821         ret = 0;
2822     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO &&
2823                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2824                                               SND_DEVICE_OUT_BT_SCO)) {
2825         *num_devices = 2;
2826         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2827         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
2828         ret = 0;
2829     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO &&
2830                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2831                                               SND_DEVICE_OUT_BT_SCO)) {
2832         *num_devices = 2;
2833         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2834         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO;
2835         ret = 0;
2836     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB &&
2837                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2838                                               SND_DEVICE_OUT_BT_SCO_WB)) {
2839         *num_devices = 2;
2840         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2841         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
2842         ret = 0;
2843     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB &&
2844                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2845                                               SND_DEVICE_OUT_BT_SCO_WB)) {
2846         *num_devices = 2;
2847         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2848         new_snd_devices[1] = SND_DEVICE_OUT_BT_SCO_WB;
2849         ret = 0;
2850     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET &&
2851                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER, SND_DEVICE_OUT_USB_HEADSET)) {
2852         *num_devices = 2;
2853         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2854         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
2855         ret = 0;
2856     } else if (snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET &&
2857                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE, SND_DEVICE_OUT_USB_HEADSET)) {
2858         *num_devices = 2;
2859         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2860         new_snd_devices[1] = SND_DEVICE_OUT_USB_HEADSET;
2861         ret = 0;
2862     } else if (SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device &&
2863                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER,
2864                                               SND_DEVICE_OUT_BT_A2DP)) {
2865         *num_devices = 2;
2866         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER;
2867         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
2868         ret = 0;
2869     } else if (SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device &&
2870                !platform_check_backends_match(SND_DEVICE_OUT_SPEAKER_SAFE,
2871                                               SND_DEVICE_OUT_BT_A2DP)) {
2872         *num_devices = 2;
2873         new_snd_devices[0] = SND_DEVICE_OUT_SPEAKER_SAFE;
2874         new_snd_devices[1] = SND_DEVICE_OUT_BT_A2DP;
2875         ret = 0;
2876     }
2877 
2878     return ret;
2879 }
2880 
platform_get_output_snd_device(void * platform,audio_devices_t devices)2881 snd_device_t platform_get_output_snd_device(void *platform, audio_devices_t devices)
2882 {
2883     struct platform_data *my_data = (struct platform_data *)platform;
2884     struct audio_device *adev = my_data->adev;
2885     audio_mode_t mode = adev->mode;
2886     snd_device_t snd_device = SND_DEVICE_NONE;
2887 
2888     ALOGV("%s: enter: output devices(%#x)", __func__, devices);
2889     if (devices == AUDIO_DEVICE_NONE ||
2890         devices & AUDIO_DEVICE_BIT_IN) {
2891         ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
2892         goto exit;
2893     }
2894 
2895     if (popcount(devices) == 2) {
2896         if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
2897                         AUDIO_DEVICE_OUT_SPEAKER) ||
2898                 devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
2899                             AUDIO_DEVICE_OUT_SPEAKER)) {
2900             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
2901         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
2902                                AUDIO_DEVICE_OUT_SPEAKER)) {
2903             snd_device = SND_DEVICE_OUT_SPEAKER_AND_LINE;
2904         } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
2905                                AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
2906                    devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
2907                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2908             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_HEADPHONES;
2909         } else if (devices == (AUDIO_DEVICE_OUT_LINE |
2910                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2911             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_LINE;
2912         } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
2913                                AUDIO_DEVICE_OUT_SPEAKER)) {
2914             snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
2915         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
2916                    ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER)) {
2917             snd_device = adev->bt_wb_speech_enabled ?
2918                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO_WB :
2919                     SND_DEVICE_OUT_SPEAKER_AND_BT_SCO;
2920         } else if ((devices & AUDIO_DEVICE_OUT_ALL_SCO) &&
2921                          ((devices & ~AUDIO_DEVICE_OUT_ALL_SCO) == AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
2922             snd_device = adev->bt_wb_speech_enabled ?
2923                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO_WB :
2924                     SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_SCO;
2925         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
2926                                AUDIO_DEVICE_OUT_SPEAKER)) ||
2927                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
2928                                                AUDIO_DEVICE_OUT_SPEAKER))) {
2929             snd_device = SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET;
2930         } else if ((devices == (AUDIO_DEVICE_OUT_USB_DEVICE |
2931                                AUDIO_DEVICE_OUT_SPEAKER_SAFE)) ||
2932                 (devices == (AUDIO_DEVICE_OUT_USB_HEADSET |
2933                                                AUDIO_DEVICE_OUT_SPEAKER_SAFE))) {
2934             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_USB_HEADSET;
2935         } else if ((devices & AUDIO_DEVICE_OUT_SPEAKER) &&
2936                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
2937             snd_device = SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP;
2938         }  else if ((devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) &&
2939                    (devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
2940             snd_device = SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP;
2941         } else {
2942             ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
2943             goto exit;
2944         }
2945         if (snd_device != SND_DEVICE_NONE) {
2946             goto exit;
2947         }
2948     }
2949 
2950     if (popcount(devices) != 1) {
2951         ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
2952         goto exit;
2953     }
2954 
2955     if (voice_is_in_call(adev) || adev->enable_voicerx || audio_extn_hfp_is_active(adev)) {
2956         if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
2957             devices & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
2958             devices & AUDIO_DEVICE_OUT_LINE) {
2959             if (voice_is_in_call(adev) &&
2960                 (adev->voice.tty_mode == TTY_MODE_FULL))
2961                 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
2962             else if (voice_is_in_call(adev) &&
2963                 (adev->voice.tty_mode == TTY_MODE_VCO))
2964                 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
2965             else if (voice_is_in_call(adev) &&
2966                 (adev->voice.tty_mode == TTY_MODE_HCO))
2967                 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
2968             else {
2969                 if (devices & AUDIO_DEVICE_OUT_LINE)
2970                     snd_device = SND_DEVICE_OUT_VOICE_LINE;
2971                 else if (devices & AUDIO_DEVICE_OUT_WIRED_HEADSET)
2972                     snd_device = SND_DEVICE_OUT_VOICE_HEADSET;
2973                 else
2974                     snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
2975                 }
2976         } else if (audio_is_usb_out_device(devices)) {
2977             if (voice_is_in_call(adev)) {
2978                 switch (adev->voice.tty_mode) {
2979                     case TTY_MODE_FULL:
2980                         snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_USB;
2981                         break;
2982                     case TTY_MODE_VCO:
2983                         snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_USB;
2984                         break;
2985                     case TTY_MODE_HCO:
2986                         // since Hearing will be on handset\speaker, use existing device
2987                         snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
2988                         break;
2989                     case TTY_MODE_OFF:
2990                         break;
2991                     default:
2992                         ALOGE("%s: Invalid TTY mode (%#x)",
2993                               __func__, adev->voice.tty_mode);
2994                 }
2995             }
2996             if (snd_device == SND_DEVICE_NONE) {
2997                     snd_device = audio_extn_usb_is_capture_supported() ?
2998                                  SND_DEVICE_OUT_VOICE_USB_HEADSET :
2999                                  SND_DEVICE_OUT_VOICE_USB_HEADPHONES;
3000             }
3001         } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3002             if (adev->bt_wb_speech_enabled) {
3003                 snd_device = SND_DEVICE_OUT_BT_SCO_WB;
3004             } else {
3005                 snd_device = SND_DEVICE_OUT_BT_SCO;
3006             }
3007         } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3008             snd_device = SND_DEVICE_OUT_BT_A2DP;
3009         } else if (devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
3010             if (!adev->enable_hfp) {
3011                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
3012             } else {
3013                 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER_HFP;
3014             }
3015         } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
3016             if(adev->voice.hac)
3017                 snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
3018             else if (is_operator_tmus())
3019                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
3020             else
3021                 snd_device = SND_DEVICE_OUT_VOICE_HANDSET;
3022         } else if (devices & AUDIO_DEVICE_OUT_TELEPHONY_TX) {
3023             snd_device = SND_DEVICE_OUT_VOICE_TX;
3024         } else if (devices & AUDIO_DEVICE_OUT_HEARING_AID) {
3025             snd_device = SND_DEVICE_OUT_VOICE_HEARING_AID;
3026         }
3027 
3028         if (snd_device != SND_DEVICE_NONE) {
3029             goto exit;
3030         }
3031     }
3032 
3033     if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3034         devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3035         snd_device = SND_DEVICE_OUT_HEADPHONES;
3036     } else if (devices & AUDIO_DEVICE_OUT_LINE) {
3037         snd_device = SND_DEVICE_OUT_LINE;
3038     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
3039         snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
3040     } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
3041         /*
3042          * Perform device switch only if acdb tuning is different between SPEAKER & SPEAKER_REVERSE,
3043          * Or there will be a small pause while performing device switch.
3044          */
3045         if (my_data->speaker_lr_swap &&
3046             (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
3047             acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]))
3048             snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
3049         else
3050             snd_device = SND_DEVICE_OUT_SPEAKER;
3051     } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
3052         if (adev->bt_wb_speech_enabled) {
3053             snd_device = SND_DEVICE_OUT_BT_SCO_WB;
3054         } else {
3055             snd_device = SND_DEVICE_OUT_BT_SCO;
3056         }
3057     } else if (devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
3058         snd_device = SND_DEVICE_OUT_BT_A2DP;
3059     } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
3060         snd_device = SND_DEVICE_OUT_HDMI ;
3061     } else if (audio_is_usb_out_device(devices)) {
3062         if (audio_extn_ma_supported_usb())
3063             snd_device = SND_DEVICE_OUT_USB_HEADSET_SPEC;
3064         else if (audio_extn_usb_is_capture_supported())
3065             snd_device = SND_DEVICE_OUT_USB_HEADSET;
3066         else
3067             snd_device = SND_DEVICE_OUT_USB_HEADPHONES;
3068     }else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
3069         /*HAC support for voice-ish audio (eg visual voicemail)*/
3070         if(adev->voice.hac)
3071             snd_device = SND_DEVICE_OUT_VOICE_HAC_HANDSET;
3072         else
3073             snd_device = SND_DEVICE_OUT_HANDSET;
3074     } else {
3075         ALOGE("%s: Unknown device(s) %#x", __func__, devices);
3076     }
3077 exit:
3078     ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
3079     return snd_device;
3080 }
3081 
3082 #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)3083 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
3084                                                   struct stream_in *in __unused,
3085                                                   audio_devices_t out_device __unused,
3086                                                   audio_devices_t in_device)
3087 {
3088     struct audio_device *adev = my_data->adev;
3089     snd_device_t snd_device = SND_DEVICE_NONE;
3090 
3091     if (my_data->fluence_type != FLUENCE_DISABLE) {
3092         switch(AUDIO_DEVICE_BIT_IN | in_device) {
3093             case AUDIO_DEVICE_IN_BACK_MIC:
3094                 if (my_data->fluence_in_spkr_mode) {
3095                     if ((my_data->fluence_type & FLUENCE_PRO_ENABLE) &&
3096                         (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3097                         snd_device = SND_DEVICE_IN_SPEAKER_QMIC_AEC_NS;
3098                     } else if (((my_data->fluence_type & FLUENCE_PRO_ENABLE) ||
3099                                (my_data->fluence_type & FLUENCE_ENABLE)) &&
3100                                (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3101                             snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
3102                     }
3103                     adev->acdb_settings |= DMIC_FLAG;
3104                 } else
3105                     snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
3106                 break;
3107             case AUDIO_DEVICE_IN_BUILTIN_MIC:
3108                 if (((my_data->fluence_type & FLUENCE_PRO_ENABLE) ||
3109                     (my_data->fluence_type & FLUENCE_ENABLE)) &&
3110                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3111                     snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
3112                     adev->acdb_settings |= DMIC_FLAG;
3113                 } else
3114                     snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
3115                 break;
3116             default:
3117                 ALOGE("%s: Unsupported in_device %#x", __func__, in_device);
3118                 break;
3119         }
3120         in->enable_ec_port = true;
3121     }
3122 
3123     return snd_device;
3124 }
3125 #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)3126 static snd_device_t get_snd_device_for_voice_comm(struct platform_data *my_data,
3127                                                   struct stream_in *in,
3128                                                   audio_devices_t out_device __unused,
3129                                                   audio_devices_t in_device)
3130 {
3131     struct audio_device *adev = my_data->adev;
3132     snd_device_t snd_device = SND_DEVICE_NONE;
3133 
3134     if (in->enable_aec &&
3135             in->enable_ns) {
3136         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3137             if (my_data->fluence_in_spkr_mode &&
3138                     my_data->fluence_in_voice_comm &&
3139                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3140                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC_NS;
3141             } else {
3142                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC_NS;
3143             }
3144         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3145             if (my_data->fluence_in_voice_comm &&
3146                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3147                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC_NS;
3148             } else {
3149                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC_NS;
3150             }
3151         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3152             snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
3153         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3154             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
3155         }
3156     } else if (in->enable_aec) {
3157         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3158             if (my_data->fluence_in_spkr_mode &&
3159                     my_data->fluence_in_voice_comm &&
3160                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3161                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_AEC;
3162             } else {
3163                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
3164             }
3165         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3166             if (my_data->fluence_in_voice_comm &&
3167                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3168                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
3169             } else {
3170                 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
3171             }
3172        } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3173            snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
3174        } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3175            snd_device = SND_DEVICE_IN_USB_HEADSET_MIC_AEC;
3176        }
3177     } else if (in->enable_ns) {
3178         if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3179             if (my_data->fluence_in_spkr_mode &&
3180                     my_data->fluence_in_voice_comm &&
3181                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3182                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_NS;
3183             } else {
3184                 snd_device = SND_DEVICE_IN_SPEAKER_MIC_NS;
3185             }
3186         } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3187             if (my_data->fluence_in_voice_comm &&
3188                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3189                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_NS;
3190             } else {
3191                 snd_device = SND_DEVICE_IN_HANDSET_MIC_NS;
3192             }
3193         }
3194     }
3195 
3196     return snd_device;
3197 }
3198 #endif //DYNAMIC_ECNS_ENABLED
3199 
platform_get_input_snd_device(void * platform,struct stream_in * in,audio_devices_t out_device)3200 snd_device_t platform_get_input_snd_device(void *platform,
3201                                            struct stream_in *in,
3202                                            audio_devices_t out_device)
3203 {
3204     struct platform_data *my_data = (struct platform_data *)platform;
3205     struct audio_device *adev = my_data->adev;
3206     audio_mode_t mode = adev->mode;
3207     snd_device_t snd_device = SND_DEVICE_NONE;
3208 
3209     if (in == NULL) {
3210         in = adev_get_active_input(adev);
3211     }
3212 
3213     audio_source_t source = (in == NULL) ? AUDIO_SOURCE_DEFAULT : in->source;
3214     audio_devices_t in_device =
3215         ((in == NULL) ? AUDIO_DEVICE_NONE : in->device) & ~AUDIO_DEVICE_BIT_IN;
3216     audio_channel_mask_t channel_mask = (in == NULL) ? AUDIO_CHANNEL_IN_MONO : in->channel_mask;
3217     int channel_count = audio_channel_count_from_in_mask(channel_mask);
3218 
3219     ALOGV("%s: enter: out_device(%#x) in_device(%#x) channel_count (%d) channel_mask (0x%x)",
3220           __func__, out_device, in_device, channel_count, channel_mask);
3221 
3222     if ((out_device != AUDIO_DEVICE_NONE) && (voice_is_in_call(adev) ||
3223         audio_extn_hfp_is_active(adev))) {
3224         if (adev->voice.tty_mode != TTY_MODE_OFF) {
3225             if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3226                 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET ||
3227                 out_device & AUDIO_DEVICE_OUT_LINE) {
3228                 switch (adev->voice.tty_mode) {
3229                     case TTY_MODE_FULL:
3230                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
3231                         break;
3232                     case TTY_MODE_VCO:
3233                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
3234                         break;
3235                     case TTY_MODE_HCO:
3236                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
3237                         break;
3238                     default:
3239                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
3240                 }
3241                 goto exit;
3242             } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3243                 switch (adev->voice.tty_mode) {
3244                     case TTY_MODE_FULL:
3245                         snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_USB_MIC;
3246                         break;
3247                     case TTY_MODE_VCO:
3248                         // since voice will be captured from handset mic, use existing device
3249                         snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
3250                         break;
3251                     case TTY_MODE_HCO:
3252                         snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_USB_MIC;
3253                         break;
3254                     default:
3255                         ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->voice.tty_mode);
3256                 }
3257                 goto exit;
3258             }
3259         }
3260         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
3261             if (my_data->fluence_in_voice_call == false) {
3262                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3263             } else {
3264                 if (is_operator_tmus())
3265                     snd_device = SND_DEVICE_IN_VOICE_DMIC_TMUS;
3266                 else
3267                     snd_device = SND_DEVICE_IN_VOICE_DMIC;
3268             }
3269         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3270             snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
3271         } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
3272             if (adev->bt_wb_speech_enabled) {
3273                 if (adev->bluetooth_nrec)
3274                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3275                 else
3276                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3277             } else {
3278                 if (adev->bluetooth_nrec)
3279                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3280                 else
3281                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3282             }
3283         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
3284                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
3285                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3286                    out_device & AUDIO_DEVICE_OUT_LINE) {
3287             if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
3288                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
3289                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
3290                 } else {
3291                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3292                 }
3293             }
3294 
3295             //select default
3296             if (snd_device == SND_DEVICE_NONE) {
3297                 if (!adev->enable_hfp) {
3298                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3299                 } else {
3300                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC_HFP;
3301                     platform_set_echo_reference(adev, true, out_device);
3302                 }
3303             }
3304         } else if (out_device & AUDIO_DEVICE_OUT_TELEPHONY_TX) {
3305             snd_device = SND_DEVICE_IN_VOICE_RX;
3306         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3307             if (audio_extn_usb_is_capture_supported()) {
3308               snd_device = SND_DEVICE_IN_VOICE_USB_HEADSET_MIC;
3309             } else if (my_data->fluence_in_voice_call && my_data->fluence_in_spkr_mode) {
3310                 if (my_data->source_mic_type & SOURCE_DUAL_MIC) {
3311                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC;
3312                 } else {
3313                     snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
3314                 }
3315             }
3316         } else if (out_device & AUDIO_DEVICE_OUT_HEARING_AID) {
3317             snd_device = SND_DEVICE_IN_VOICE_HEARING_AID;
3318         }
3319     } else if (source == AUDIO_SOURCE_CAMCORDER) {
3320         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
3321             in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3322             switch (adev->camera_orientation) {
3323             case CAMERA_BACK_LANDSCAPE:
3324                 snd_device = SND_DEVICE_IN_CAMCORDER_LANDSCAPE;
3325                 break;
3326             case CAMERA_BACK_INVERT_LANDSCAPE:
3327                 snd_device = SND_DEVICE_IN_CAMCORDER_INVERT_LANDSCAPE;
3328                 break;
3329             case CAMERA_BACK_PORTRAIT:
3330                 snd_device = SND_DEVICE_IN_CAMCORDER_PORTRAIT;
3331                 break;
3332             case CAMERA_FRONT_LANDSCAPE:
3333                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_LANDSCAPE;
3334                 break;
3335             case CAMERA_FRONT_INVERT_LANDSCAPE:
3336                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_INVERT_LANDSCAPE;
3337                 break;
3338             case CAMERA_FRONT_PORTRAIT:
3339                 snd_device = SND_DEVICE_IN_CAMCORDER_SELFIE_PORTRAIT;
3340                 break;
3341             default:
3342                 ALOGW("%s: invalid camera orientation %08x", __func__, adev->camera_orientation);
3343                 snd_device = SND_DEVICE_IN_CAMCORDER_LANDSCAPE;
3344                 break;
3345             }
3346         }
3347     } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
3348         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3349             if (my_data->fluence_in_voice_rec && channel_count == 1) {
3350                 if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
3351                     (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3352                     if (in->enable_aec)
3353                         snd_device = SND_DEVICE_IN_HANDSET_QMIC_AEC;
3354                     else
3355                         snd_device = SND_DEVICE_IN_HANDSET_QMIC;
3356                 } else if ((my_data->fluence_type == FLUENCE_PRO_ENABLE) &&
3357                     (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3358                     if (in->enable_aec)
3359                         snd_device = SND_DEVICE_IN_HANDSET_TMIC_AEC;
3360                     else
3361                         snd_device = SND_DEVICE_IN_HANDSET_TMIC;
3362                 } else if (((my_data->fluence_type == FLUENCE_PRO_ENABLE) ||
3363                     (my_data->fluence_type == FLUENCE_ENABLE)) &&
3364                     (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3365                     if (in->enable_aec)
3366                         snd_device = SND_DEVICE_IN_HANDSET_DMIC_AEC;
3367                     else
3368                         snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_FLUENCE;
3369                 }
3370                 in->enable_ec_port = true;
3371             } else if ((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) &&
3372                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3373                 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_STEREO;
3374             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
3375                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3376                 snd_device = SND_DEVICE_IN_THREE_MIC;
3377             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
3378                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3379                 snd_device = SND_DEVICE_IN_QUAD_MIC;
3380             }
3381             if (snd_device == SND_DEVICE_NONE) {
3382                 if (in->enable_aec) {
3383                     if (in->enable_ns) {
3384                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC_NS;
3385                     } else {
3386                         snd_device = SND_DEVICE_IN_VOICE_REC_MIC_AEC;
3387                     }
3388                 } else if (in->enable_ns) {
3389                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC_NS;
3390                 } else {
3391                     snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
3392                 }
3393             }
3394         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3395             snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC;
3396         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3397             snd_device = SND_DEVICE_IN_VOICE_RECOG_USB_HEADSET_MIC;
3398         }
3399     } else if (source == AUDIO_SOURCE_UNPROCESSED) {
3400         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3401             if (((channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) ||
3402                  (channel_mask == AUDIO_CHANNEL_IN_STEREO)) &&
3403                        (my_data->source_mic_type & SOURCE_DUAL_MIC)) {
3404                 snd_device = SND_DEVICE_IN_UNPROCESSED_STEREO_MIC;
3405             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) &&
3406                        (my_data->source_mic_type & SOURCE_THREE_MIC)) {
3407                 snd_device = SND_DEVICE_IN_UNPROCESSED_THREE_MIC;
3408             } else if ((channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) &&
3409                        (my_data->source_mic_type & SOURCE_QUAD_MIC)) {
3410                 snd_device = SND_DEVICE_IN_UNPROCESSED_QUAD_MIC;
3411             } else {
3412                 snd_device = SND_DEVICE_IN_UNPROCESSED_MIC;
3413             }
3414         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3415             snd_device = SND_DEVICE_IN_UNPROCESSED_HEADSET_MIC;
3416         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3417             snd_device = SND_DEVICE_IN_UNPROCESSED_USB_HEADSET_MIC;
3418         }
3419     } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
3420                mode == AUDIO_MODE_IN_COMMUNICATION) {
3421         if (out_device & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
3422             out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3423             (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE | AUDIO_DEVICE_OUT_USB_HEADSET) &&
3424                 !audio_extn_usb_is_capture_supported())) {
3425             in_device = AUDIO_DEVICE_IN_BACK_MIC;
3426         }
3427 
3428         if (in) {
3429             snd_device = get_snd_device_for_voice_comm(my_data, in, out_device, in_device);
3430         }
3431     } else if (source == AUDIO_SOURCE_DEFAULT) {
3432         goto exit;
3433     }
3434 
3435 
3436     if (snd_device != SND_DEVICE_NONE) {
3437         goto exit;
3438     }
3439 
3440     if (in_device != AUDIO_DEVICE_NONE &&
3441             !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
3442             !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
3443         if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
3444             if ((my_data->source_mic_type & SOURCE_QUAD_MIC) &&
3445                 channel_mask == AUDIO_CHANNEL_INDEX_MASK_4) {
3446                 snd_device = SND_DEVICE_IN_QUAD_MIC;
3447             } else if ((my_data->source_mic_type & SOURCE_THREE_MIC) &&
3448                        channel_mask == AUDIO_CHANNEL_INDEX_MASK_3) {
3449                 snd_device = SND_DEVICE_IN_THREE_MIC;
3450             } else if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3451                        channel_count == 2) {
3452                 snd_device = SND_DEVICE_IN_HANDSET_DMIC_STEREO;
3453             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3454                        channel_count == 1) {
3455                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3456             } else {
3457                 ALOGE("%s: something wrong (1): source type (%d) channel_count (%d) .."
3458                       " channel mask (0x%x) no combination found .. setting to mono", __func__,
3459                        my_data->source_mic_type, channel_count, channel_mask);
3460                 snd_device = SND_DEVICE_IN_HANDSET_MIC;
3461             }
3462         } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
3463             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3464                     channel_count == 2) {
3465                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
3466             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3467                     channel_count == 1) {
3468                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3469             } else {
3470                 ALOGE("%s: something wrong (2): source type (%d) channel_count (%d) .."
3471                       " no combination found .. setting to mono", __func__,
3472                        my_data->source_mic_type, channel_count);
3473                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3474             }
3475         } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
3476             snd_device = SND_DEVICE_IN_HEADSET_MIC;
3477         } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
3478             if (adev->bt_wb_speech_enabled) {
3479                 if (adev->bluetooth_nrec)
3480                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3481                 else
3482                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3483             } else {
3484                 if (adev->bluetooth_nrec)
3485                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3486                 else
3487                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3488             }
3489         } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
3490             snd_device = SND_DEVICE_IN_HDMI_MIC;
3491         } else if (audio_is_usb_in_device(in_device | AUDIO_DEVICE_BIT_IN)) {
3492             snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
3493         } else {
3494             ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
3495             ALOGW("%s: Using default handset-mic", __func__);
3496             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3497         }
3498     } else {
3499         if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
3500             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3501         } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
3502             snd_device = SND_DEVICE_IN_HEADSET_MIC;
3503         } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER ||
3504                    out_device & AUDIO_DEVICE_OUT_SPEAKER_SAFE ||
3505                    out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
3506                    out_device & AUDIO_DEVICE_OUT_LINE) {
3507             if ((my_data->source_mic_type & SOURCE_DUAL_MIC) &&
3508                     channel_count == 2) {
3509                 snd_device = SND_DEVICE_IN_SPEAKER_DMIC_STEREO;
3510             } else if ((my_data->source_mic_type & SOURCE_MONO_MIC) &&
3511                           channel_count == 1) {
3512                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3513             } else {
3514                 ALOGE("%s: something wrong (3): source type (%d) channel_count (%d) .."
3515                       " no combination found .. setting to mono", __func__,
3516                        my_data->source_mic_type, channel_count);
3517                 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3518             }
3519         } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
3520             if (adev->bt_wb_speech_enabled) {
3521                 if (adev->bluetooth_nrec)
3522                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB_NREC;
3523                 else
3524                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_WB;
3525             } else {
3526                 if (adev->bluetooth_nrec)
3527                     snd_device = SND_DEVICE_IN_BT_SCO_MIC_NREC;
3528                 else
3529                     snd_device = SND_DEVICE_IN_BT_SCO_MIC;
3530             }
3531         } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
3532             snd_device = SND_DEVICE_IN_HDMI_MIC;
3533         } else if (out_device & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) {
3534             if (audio_extn_usb_is_capture_supported())
3535               snd_device = SND_DEVICE_IN_USB_HEADSET_MIC;
3536             else
3537               snd_device = SND_DEVICE_IN_SPEAKER_MIC;
3538         } else {
3539             ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
3540             ALOGW("%s: Using default handset-mic", __func__);
3541             snd_device = SND_DEVICE_IN_HANDSET_MIC;
3542         }
3543     }
3544 exit:
3545     ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
3546     return snd_device;
3547 }
3548 
platform_set_hdmi_channels(void * platform,int channel_count)3549 int platform_set_hdmi_channels(void *platform,  int channel_count)
3550 {
3551     struct platform_data *my_data = (struct platform_data *)platform;
3552     struct audio_device *adev = my_data->adev;
3553     struct mixer_ctl *ctl;
3554     const char *channel_cnt_str = NULL;
3555     const char *mixer_ctl_name = "HDMI_RX Channels";
3556     switch (channel_count) {
3557     case 8:
3558         channel_cnt_str = "Eight"; break;
3559     case 7:
3560         channel_cnt_str = "Seven"; break;
3561     case 6:
3562         channel_cnt_str = "Six"; break;
3563     case 5:
3564         channel_cnt_str = "Five"; break;
3565     case 4:
3566         channel_cnt_str = "Four"; break;
3567     case 3:
3568         channel_cnt_str = "Three"; break;
3569     default:
3570         channel_cnt_str = "Two"; break;
3571     }
3572     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3573     if (!ctl) {
3574         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3575               __func__, mixer_ctl_name);
3576         return -EINVAL;
3577     }
3578     ALOGV("HDMI channel count: %s", channel_cnt_str);
3579     mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
3580     return 0;
3581 }
3582 
platform_edid_get_max_channels(void * platform)3583 int platform_edid_get_max_channels(void *platform)
3584 {
3585     struct platform_data *my_data = (struct platform_data *)platform;
3586     struct audio_device *adev = my_data->adev;
3587     char block[MAX_SAD_BLOCKS * SAD_BLOCK_SIZE];
3588     char *sad = block;
3589     int num_audio_blocks;
3590     int channel_count;
3591     int max_channels = 0;
3592     int i, ret, count;
3593 
3594     struct mixer_ctl *ctl;
3595 
3596     ctl = mixer_get_ctl_by_name(adev->mixer, AUDIO_DATA_BLOCK_MIXER_CTL);
3597     if (!ctl) {
3598         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3599               __func__, AUDIO_DATA_BLOCK_MIXER_CTL);
3600         return 0;
3601     }
3602 
3603     mixer_ctl_update(ctl);
3604 
3605     count = mixer_ctl_get_num_values(ctl);
3606 
3607     /* Read SAD blocks, clamping the maximum size for safety */
3608     if (count > (int)sizeof(block))
3609         count = (int)sizeof(block);
3610 
3611     ret = mixer_ctl_get_array(ctl, block, count);
3612     if (ret != 0) {
3613         ALOGE("%s: mixer_ctl_get_array() failed to get EDID info", __func__);
3614         return 0;
3615     }
3616 
3617     /* Calculate the number of SAD blocks */
3618     num_audio_blocks = count / SAD_BLOCK_SIZE;
3619 
3620     for (i = 0; i < num_audio_blocks; i++) {
3621         /* Only consider LPCM blocks */
3622         if ((sad[0] >> 3) != EDID_FORMAT_LPCM) {
3623             sad += 3;
3624             continue;
3625         }
3626 
3627         channel_count = (sad[0] & 0x7) + 1;
3628         if (channel_count > max_channels)
3629             max_channels = channel_count;
3630 
3631         /* Advance to next block */
3632         sad += 3;
3633     }
3634 
3635     return max_channels;
3636 }
3637 
platform_set_incall_recording_session_id(void * platform,uint32_t session_id,int rec_mode)3638 int platform_set_incall_recording_session_id(void *platform,
3639                                              uint32_t session_id, int rec_mode)
3640 {
3641     int ret = 0;
3642     struct platform_data *my_data = (struct platform_data *)platform;
3643     struct audio_device *adev = my_data->adev;
3644     struct mixer_ctl *ctl;
3645     const char *mixer_ctl_name = "Voc VSID";
3646     int num_ctl_values;
3647     int i;
3648 
3649     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3650     if (!ctl) {
3651         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3652               __func__, mixer_ctl_name);
3653         ret = -EINVAL;
3654     } else {
3655         num_ctl_values = mixer_ctl_get_num_values(ctl);
3656         for (i = 0; i < num_ctl_values; i++) {
3657             if (mixer_ctl_set_value(ctl, i, session_id)) {
3658                 ALOGV("Error: invalid session_id: %x", session_id);
3659                 ret = -EINVAL;
3660                 break;
3661             }
3662         }
3663     }
3664 
3665     if (my_data->csd != NULL) {
3666         ret = my_data->csd->start_record(ALL_SESSION_VSID, rec_mode);
3667         if (ret < 0) {
3668             ALOGE("%s: csd_client_start_record failed, error %d",
3669                   __func__, ret);
3670         }
3671     }
3672 
3673     return ret;
3674 }
3675 
platform_set_incall_recording_session_channels(void * platform,uint32_t channel_count)3676 int platform_set_incall_recording_session_channels(void *platform,
3677                                              uint32_t channel_count)
3678 {
3679     int ret = 0;
3680     struct platform_data *my_data = (struct platform_data *)platform;
3681     struct audio_device *adev = my_data->adev;
3682     const char *mixer_ctl_name = "Voc Rec Config";
3683     int num_ctl_values;
3684     int i;
3685     struct mixer_ctl *ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3686 
3687     if (!ctl) {
3688         ALOGE("%s: Could not get ctl for mixer cmd - %s",
3689               __func__, mixer_ctl_name);
3690         ret = -EINVAL;
3691     } else {
3692         num_ctl_values = mixer_ctl_get_num_values(ctl);
3693         for (i = 0; i < num_ctl_values; i++) {
3694             if (mixer_ctl_set_value(ctl, i, channel_count)) {
3695                 ALOGE("Error: invalid channel count: %x", channel_count);
3696                 ret = -EINVAL;
3697                 break;
3698             }
3699         }
3700     }
3701 
3702     return ret;
3703 }
3704 
platform_stop_incall_recording_usecase(void * platform)3705 int platform_stop_incall_recording_usecase(void *platform)
3706 {
3707     int ret = 0;
3708     struct platform_data *my_data = (struct platform_data *)platform;
3709 
3710     if (my_data->csd != NULL) {
3711         ret = my_data->csd->stop_record(ALL_SESSION_VSID);
3712         if (ret < 0) {
3713             ALOGE("%s: csd_client_stop_record failed, error %d",
3714                   __func__, ret);
3715         }
3716     }
3717 
3718     return ret;
3719 }
3720 
platform_start_incall_music_usecase(void * platform)3721 int platform_start_incall_music_usecase(void *platform)
3722 {
3723     int ret = 0;
3724     struct platform_data *my_data = (struct platform_data *)platform;
3725 
3726     if (my_data->csd != NULL) {
3727         ret = my_data->csd->start_playback(ALL_SESSION_VSID);
3728         if (ret < 0) {
3729             ALOGE("%s: csd_client_start_playback failed, error %d",
3730                   __func__, ret);
3731         }
3732     }
3733 
3734     return ret;
3735 }
3736 
platform_stop_incall_music_usecase(void * platform)3737 int platform_stop_incall_music_usecase(void *platform)
3738 {
3739     int ret = 0;
3740     struct platform_data *my_data = (struct platform_data *)platform;
3741 
3742     if (my_data->csd != NULL) {
3743         ret = my_data->csd->stop_playback(ALL_SESSION_VSID);
3744         if (ret < 0) {
3745             ALOGE("%s: csd_client_stop_playback failed, error %d",
3746                   __func__, ret);
3747         }
3748     }
3749 
3750     return ret;
3751 }
3752 
platform_set_parameters(void * platform,struct str_parms * parms)3753 int platform_set_parameters(void *platform, struct str_parms *parms)
3754 {
3755     struct platform_data *my_data = (struct platform_data *)platform;
3756     char *value = NULL;
3757     char *kv_pairs = str_parms_to_str(parms);
3758     int len;
3759     int ret = 0, err;
3760 
3761     if (kv_pairs == NULL) {
3762         ret = -EINVAL;
3763         ALOGE("%s: key-value pair is NULL", __func__);
3764         goto done;
3765     }
3766 
3767     ALOGV("%s: enter: %s", __func__, kv_pairs);
3768 
3769     len = strlen(kv_pairs);
3770     value = (char*)calloc(len + 1, sizeof(char));
3771     if (value == NULL) {
3772         ret = -ENOMEM;
3773         ALOGE("[%s] failed to allocate memory", __func__);
3774         goto done;
3775     }
3776 
3777     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME,
3778                             value, len);
3779     if (err >= 0) {
3780         str_parms_del(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME);
3781         my_data->snd_card_name = strdup(value);
3782         ALOGV("%s: sound card name %s", __func__, my_data->snd_card_name);
3783     }
3784 
3785     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO,
3786                             value, len);
3787     if (err >= 0) {
3788         struct operator_info *info;
3789         char *str = value;
3790         char *name;
3791 
3792         str_parms_del(parms, PLATFORM_CONFIG_KEY_OPERATOR_INFO);
3793         info = (struct operator_info *)calloc(1, sizeof(struct operator_info));
3794         name = strtok(str, ";");
3795         info->name = strdup(name);
3796         info->mccmnc = strdup(str + strlen(name) + 1);
3797 
3798         list_add_tail(&operator_info_list, &info->list);
3799         ALOGV("%s: add operator[%s] mccmnc[%s]", __func__, info->name, info->mccmnc);
3800     }
3801 
3802     memset(value, 0, len + 1);
3803     err = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT,
3804                             value, len);
3805     if (err >= 0) {
3806         str_parms_del(parms, PLATFORM_CONFIG_KEY_MAX_MIC_COUNT);
3807         my_data->max_mic_count = atoi(value);
3808         ALOGV("%s: max_mic_count %s/%d", __func__, value, my_data->max_mic_count);
3809     }
3810 
3811     /* handle audio calibration parameters */
3812     set_audiocal(platform, parms, value, len);
3813 
3814     // to-do: disable setting sidetone gain, will revist this later
3815     // audio_extn_usb_set_sidetone_gain(parms, value, len);
3816 done:
3817     ALOGV("%s: exit with code(%d)", __func__, ret);
3818     if (kv_pairs != NULL)
3819         free(kv_pairs);
3820     if (value != NULL)
3821         free(value);
3822 
3823     return ret;
3824 }
3825 
3826 /* Delay in Us */
platform_render_latency(audio_usecase_t usecase)3827 int64_t platform_render_latency(audio_usecase_t usecase)
3828 {
3829     switch (usecase) {
3830         case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
3831             return DEEP_BUFFER_PLATFORM_DELAY;
3832         case USECASE_AUDIO_PLAYBACK_LOW_LATENCY:
3833         case USECASE_AUDIO_PLAYBACK_WITH_HAPTICS:
3834             return LOW_LATENCY_PLATFORM_DELAY;
3835         case USECASE_AUDIO_PLAYBACK_ULL:
3836             return ULL_PLATFORM_DELAY;
3837         case USECASE_AUDIO_PLAYBACK_MMAP:
3838             return MMAP_PLATFORM_DELAY;
3839         default:
3840             return 0;
3841     }
3842 }
3843 
platform_set_snd_device_backend(snd_device_t device,const char * backend_tag,const char * hw_interface)3844 int platform_set_snd_device_backend(snd_device_t device, const char *backend_tag,
3845                                     const char * hw_interface)
3846 {
3847     int ret = 0;
3848 
3849     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
3850         ALOGE("%s: Invalid snd_device = %d",
3851             __func__, device);
3852         ret = -EINVAL;
3853         goto done;
3854     }
3855 
3856     ALOGV("%s: backend_tag_table[%s]: old = %s new = %s", __func__,
3857           platform_get_snd_device_name(device),
3858           backend_tag_table[device] != NULL ? backend_tag_table[device]: "null", backend_tag);
3859     if (backend_tag_table[device]) {
3860         free(backend_tag_table[device]);
3861     }
3862     backend_tag_table[device] = strdup(backend_tag);
3863 
3864     if (hw_interface != NULL) {
3865         if (hw_interface_table[device])
3866             free(hw_interface_table[device]);
3867         ALOGV("%s: hw_interface_table[%d] = %s", __func__, device, hw_interface);
3868         hw_interface_table[device] = strdup(hw_interface);
3869     }
3870 done:
3871     return ret;
3872 }
3873 
platform_set_usecase_pcm_id(audio_usecase_t usecase,int32_t type,int32_t pcm_id)3874 int platform_set_usecase_pcm_id(audio_usecase_t usecase, int32_t type, int32_t pcm_id)
3875 {
3876     int ret = 0;
3877     if ((usecase <= USECASE_INVALID) || (usecase >= AUDIO_USECASE_MAX)) {
3878         ALOGE("%s: invalid usecase case idx %d", __func__, usecase);
3879         ret = -EINVAL;
3880         goto done;
3881     }
3882 
3883     if ((type != 0) && (type != 1)) {
3884         ALOGE("%s: invalid usecase type", __func__);
3885         ret = -EINVAL;
3886     }
3887     ALOGV("%s: pcm_device_table[%d %s][%d] = %d", __func__, usecase,
3888           use_case_table[usecase],
3889           type, pcm_id);
3890     pcm_device_table[usecase][type] = pcm_id;
3891 done:
3892     return ret;
3893 }
3894 
3895 #define DEFAULT_NOMINAL_SPEAKER_GAIN 20
ramp_speaker_gain(struct audio_device * adev,bool ramp_up,int target_ramp_up_gain)3896 int ramp_speaker_gain(struct audio_device *adev, bool ramp_up, int target_ramp_up_gain) {
3897     // backup_gain: gain to try to set in case of an error during ramp
3898     int start_gain, end_gain, step, backup_gain, i;
3899     bool error = false;
3900     const struct mixer_ctl *ctl;
3901     const char *mixer_ctl_name_gain_left = "Left Speaker Gain";
3902     const char *mixer_ctl_name_gain_right = "Right Speaker Gain";
3903     struct mixer_ctl *ctl_left = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_left);
3904     struct mixer_ctl *ctl_right = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name_gain_right);
3905     if (!ctl_left || !ctl_right) {
3906         ALOGE("%s: Could not get ctl for mixer cmd - %s or %s, not applying speaker gain ramp",
3907                       __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
3908         return -EINVAL;
3909     } else if ((mixer_ctl_get_num_values(ctl_left) != 1)
3910             || (mixer_ctl_get_num_values(ctl_right) != 1)) {
3911         ALOGE("%s: Unexpected num values for mixer cmd - %s or %s, not applying speaker gain ramp",
3912                               __func__, mixer_ctl_name_gain_left, mixer_ctl_name_gain_right);
3913         return -EINVAL;
3914     }
3915     if (ramp_up) {
3916         start_gain = 0;
3917         end_gain = target_ramp_up_gain > 0 ? target_ramp_up_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
3918         step = +1;
3919         backup_gain = end_gain;
3920     } else {
3921         // using same gain on left and right
3922         const int left_gain = mixer_ctl_get_value(ctl_left, 0);
3923         start_gain = left_gain > 0 ? left_gain : DEFAULT_NOMINAL_SPEAKER_GAIN;
3924         end_gain = 0;
3925         step = -1;
3926         backup_gain = start_gain;
3927     }
3928     for (i = start_gain ; i != (end_gain + step) ; i += step) {
3929         //ALOGV("setting speaker gain to %d", i);
3930         if (mixer_ctl_set_value(ctl_left, 0, i)) {
3931             ALOGE("%s: error setting %s to %d during gain ramp",
3932                     __func__, mixer_ctl_name_gain_left, i);
3933             error = true;
3934             break;
3935         }
3936         if (mixer_ctl_set_value(ctl_right, 0, i)) {
3937             ALOGE("%s: error setting %s to %d during gain ramp",
3938                     __func__, mixer_ctl_name_gain_right, i);
3939             error = true;
3940             break;
3941         }
3942         usleep(1000);
3943     }
3944     if (error) {
3945         // an error occured during the ramp, let's still try to go back to a safe volume
3946         if (mixer_ctl_set_value(ctl_left, 0, backup_gain)) {
3947             ALOGE("%s: error restoring left gain to %d", __func__, backup_gain);
3948         }
3949         if (mixer_ctl_set_value(ctl_right, 0, backup_gain)) {
3950             ALOGE("%s: error restoring right gain to %d", __func__, backup_gain);
3951         }
3952     }
3953     return start_gain;
3954 }
3955 
platform_set_swap_mixer(struct audio_device * adev,bool swap_channels)3956 int platform_set_swap_mixer(struct audio_device *adev, bool swap_channels)
3957 {
3958     const char *mixer_ctl_name = "Swap channel";
3959     struct mixer_ctl *ctl;
3960     const char *mixer_path;
3961     struct platform_data *my_data = (struct platform_data *)adev->platform;
3962 
3963     // forced to set to swap, but device not rotated ... ignore set
3964     if (swap_channels && !my_data->speaker_lr_swap)
3965         return 0;
3966 
3967     ALOGV("%s:", __func__);
3968 
3969     if (swap_channels) {
3970         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER_REVERSE);
3971         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
3972     } else {
3973         mixer_path = platform_get_snd_device_name(SND_DEVICE_OUT_SPEAKER);
3974         audio_route_apply_and_update_path(adev->audio_route, mixer_path);
3975     }
3976 
3977     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
3978     if (!ctl) {
3979         ALOGE("%s: Could not get ctl for mixer cmd - %s",__func__, mixer_ctl_name);
3980         return -EINVAL;
3981     }
3982 
3983     if (mixer_ctl_set_value(ctl, 0, swap_channels) < 0) {
3984         ALOGE("%s: Could not set reverse cotrol %d",__func__, swap_channels);
3985         return -EINVAL;
3986     }
3987 
3988     ALOGV("platfor_force_swap_channel :: Channel orientation ( %s ) ",
3989            swap_channels?"R --> L":"L --> R");
3990 
3991     return 0;
3992 }
3993 
platform_check_and_set_swap_lr_channels(struct audio_device * adev,bool swap_channels)3994 int platform_check_and_set_swap_lr_channels(struct audio_device *adev, bool swap_channels)
3995 {
3996     // only update if there is active pcm playback on speaker
3997     struct audio_usecase *usecase;
3998     struct listnode *node;
3999     struct platform_data *my_data = (struct platform_data *)adev->platform;
4000 
4001     my_data->speaker_lr_swap = swap_channels;
4002 
4003     return platform_set_swap_channels(adev, swap_channels);
4004 }
4005 
platform_set_swap_channels(struct audio_device * adev,bool swap_channels)4006 int platform_set_swap_channels(struct audio_device *adev, bool swap_channels)
4007 {
4008     // only update if there is active pcm playback on speaker
4009     struct audio_usecase *usecase;
4010     struct listnode *node;
4011     struct platform_data *my_data = (struct platform_data *)adev->platform;
4012 
4013     // do not swap channels in audio modes with concurrent capture and playback
4014     // as this may break the echo reference
4015     if ((adev->mode == AUDIO_MODE_IN_COMMUNICATION) || (adev->mode == AUDIO_MODE_IN_CALL)) {
4016         ALOGV("%s: will not swap due to audio mode %d", __func__, adev->mode);
4017         return 0;
4018     }
4019 
4020     list_for_each(node, &adev->usecase_list) {
4021         usecase = node_to_item(node, struct audio_usecase, list);
4022         if (usecase->type == PCM_PLAYBACK &&
4023                 usecase->stream.out->devices & AUDIO_DEVICE_OUT_SPEAKER) {
4024             /*
4025              * If acdb tuning is different for SPEAKER_REVERSE, it is must
4026              * to perform device switch to disable the current backend to
4027              * enable it with new acdb data.
4028              */
4029             if (acdb_device_table[SND_DEVICE_OUT_SPEAKER] !=
4030                 acdb_device_table[SND_DEVICE_OUT_SPEAKER_REVERSE]) {
4031                 const int initial_skpr_gain = ramp_speaker_gain(adev, false /*ramp_up*/, -1);
4032                 select_devices(adev, usecase->id);
4033                 if (initial_skpr_gain != -EINVAL)
4034                     ramp_speaker_gain(adev, true /*ramp_up*/, initial_skpr_gain);
4035 
4036             } else {
4037                 platform_set_swap_mixer(adev, swap_channels);
4038             }
4039             break;
4040         }
4041     }
4042 
4043     return 0;
4044 }
4045 
4046 static struct amp_db_and_gain_table tbl_mapping[MAX_VOLUME_CAL_STEPS];
4047 static int num_gain_tbl_entry = 0;
4048 
platform_add_gain_level_mapping(struct amp_db_and_gain_table * tbl_entry)4049 bool platform_add_gain_level_mapping(struct amp_db_and_gain_table *tbl_entry) {
4050 
4051     ALOGV("%s: enter .. add %f %f %d", __func__, tbl_entry->amp, tbl_entry->db, tbl_entry->level);
4052     if (num_gain_tbl_entry == -1) {
4053         ALOGE("%s: num entry beyond valid step levels or corrupted..rejecting custom mapping",
4054                __func__);
4055         return false;
4056     }
4057 
4058     if (num_gain_tbl_entry >= MAX_VOLUME_CAL_STEPS) {
4059         ALOGE("%s: max entry reached max[%d] current index[%d]  .. rejecting", __func__,
4060                MAX_VOLUME_CAL_STEPS, num_gain_tbl_entry);
4061         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
4062         return false;
4063     }
4064 
4065     if (num_gain_tbl_entry > 0 && tbl_mapping[num_gain_tbl_entry - 1].amp >= tbl_entry->amp) {
4066         ALOGE("%s: value not in ascending order .. rejecting custom mapping", __func__);
4067         num_gain_tbl_entry  = -1; // indicates error and no more info will be cached
4068         return false;
4069     }
4070 
4071     tbl_mapping[num_gain_tbl_entry] = *tbl_entry;
4072     ++num_gain_tbl_entry;
4073 
4074     return true;
4075 }
4076 
platform_get_gain_level_mapping(struct amp_db_and_gain_table * mapping_tbl,int table_size)4077 int platform_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
4078                                     int table_size) {
4079     int itt = 0;
4080     ALOGV("platform_get_gain_level_mapping called ");
4081 
4082     if (num_gain_tbl_entry <= 0 || num_gain_tbl_entry > MAX_VOLUME_CAL_STEPS) {
4083         ALOGD("%s: empty or currupted gain_mapping_table", __func__);
4084         return 0;
4085     }
4086 
4087     for (; itt < num_gain_tbl_entry && itt <= table_size; itt++) {
4088         mapping_tbl[itt] = tbl_mapping[itt];
4089         ALOGV("%s: added amp[%f] db[%f] level[%d]", __func__,
4090                mapping_tbl[itt].amp, mapping_tbl[itt].db, mapping_tbl[itt].level);
4091     }
4092 
4093     return num_gain_tbl_entry;
4094 }
4095 
platform_snd_card_update(void * platform,card_status_t status)4096 int platform_snd_card_update(void *platform, card_status_t status)
4097 {
4098     struct platform_data *my_data = (struct platform_data *)platform;
4099     struct audio_device *adev = my_data->adev;
4100 
4101     if (status == CARD_STATUS_ONLINE) {
4102         if (my_data->acdb_send_custom_top)
4103             my_data->acdb_send_custom_top();
4104     }
4105     return 0;
4106 }
4107 
4108 /*
4109  * configures afe with bit width and Sample Rate
4110  */
platform_set_backend_cfg(const struct audio_device * adev,snd_device_t snd_device,const struct audio_backend_cfg * backend_cfg)4111 int platform_set_backend_cfg(const struct audio_device* adev,
4112                              snd_device_t snd_device,
4113                              const struct audio_backend_cfg *backend_cfg)
4114 {
4115 
4116     int ret = 0;
4117     const int backend_idx = platform_get_backend_index(snd_device);
4118     struct platform_data *my_data = (struct platform_data *)adev->platform;
4119     const unsigned int bit_width = backend_cfg->bit_width;
4120     const unsigned int sample_rate = backend_cfg->sample_rate;
4121     const unsigned int channels = backend_cfg->channels;
4122     const audio_format_t format = backend_cfg->format;
4123     const bool passthrough_enabled = backend_cfg->passthrough_enabled;
4124 
4125 
4126     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4127           ", backend_idx %d device (%s)", __func__,  bit_width,
4128           sample_rate, channels, backend_idx,
4129           platform_get_snd_device_name(snd_device));
4130 
4131     if ((my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl) &&
4132         (bit_width != my_data->current_backend_cfg[backend_idx].bit_width)) {
4133 
4134         struct  mixer_ctl *ctl = NULL;
4135         ctl = mixer_get_ctl_by_name(adev->mixer,
4136                                     my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
4137         if (!ctl) {
4138             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4139                   __func__,
4140                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl);
4141             return -EINVAL;
4142         }
4143 
4144         if (bit_width == 24) {
4145             if (format == AUDIO_FORMAT_PCM_24_BIT_PACKED)
4146                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_3LE");
4147             else
4148                 ret = mixer_ctl_set_enum_by_string(ctl, "S24_LE");
4149         } else if (bit_width == 32) {
4150             ret = mixer_ctl_set_enum_by_string(ctl, "S32_LE");
4151         } else {
4152             ret = mixer_ctl_set_enum_by_string(ctl, "S16_LE");
4153         }
4154         if ( ret < 0) {
4155             ALOGE("%s:becf: afe: fail for %s mixer set to %d bit for %x format", __func__,
4156                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
4157         } else {
4158             my_data->current_backend_cfg[backend_idx].bit_width = bit_width;
4159             ALOGD("%s:becf: afe: %s mixer set to %d bit for %x format", __func__,
4160                   my_data->current_backend_cfg[backend_idx].bitwidth_mixer_ctl, bit_width, format);
4161         }
4162         /* set the ret as 0 and not pass back to upper layer */
4163         ret = 0;
4164     }
4165 
4166     if (passthrough_enabled || ((my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl) &&
4167                                 (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate))) {
4168         char *rate_str = NULL;
4169         struct  mixer_ctl *ctl = NULL;
4170 
4171         switch (sample_rate) {
4172             case 32000:
4173                 if (passthrough_enabled) {
4174                     rate_str = "KHZ_32";
4175                     break;
4176                 }
4177             case 8000:
4178             case 11025:
4179             case 16000:
4180             case 22050:
4181             case 48000:
4182                 rate_str = "KHZ_48";
4183                 break;
4184             case 44100:
4185                 rate_str = "KHZ_44P1";
4186                 break;
4187             case 64000:
4188             case 96000:
4189                 rate_str = "KHZ_96";
4190                 break;
4191             case 88200:
4192                 rate_str = "KHZ_88P2";
4193                 break;
4194             case 176400:
4195                 rate_str = "KHZ_176P4";
4196                 break;
4197             case 192000:
4198                 rate_str = "KHZ_192";
4199                 break;
4200             case 352800:
4201                 rate_str = "KHZ_352P8";
4202                 break;
4203             case 384000:
4204                 rate_str = "KHZ_384";
4205                 break;
4206             case 144000:
4207                 if (passthrough_enabled) {
4208                     rate_str = "KHZ_144";
4209                     break;
4210                 }
4211             default:
4212                 rate_str = "KHZ_48";
4213                 break;
4214         }
4215 
4216         ctl = mixer_get_ctl_by_name(adev->mixer,
4217                                     my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
4218         if(!ctl) {
4219             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4220                   __func__,
4221                   my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl);
4222             return -EINVAL;
4223         }
4224 
4225         ALOGD("%s:becf: afe: %s set to %s", __func__,
4226               my_data->current_backend_cfg[backend_idx].samplerate_mixer_ctl, rate_str);
4227         mixer_ctl_set_enum_by_string(ctl, rate_str);
4228         my_data->current_backend_cfg[backend_idx].sample_rate = sample_rate;
4229     }
4230     if ((my_data->current_backend_cfg[backend_idx].channels_mixer_ctl) &&
4231         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
4232         struct  mixer_ctl *ctl = NULL;
4233         char *channel_cnt_str = NULL;
4234 
4235         switch (channels) {
4236             case 8:
4237                 channel_cnt_str = "Eight"; break;
4238             case 7:
4239                 channel_cnt_str = "Seven"; break;
4240             case 6:
4241                 channel_cnt_str = "Six"; break;
4242             case 5:
4243                 channel_cnt_str = "Five"; break;
4244             case 4:
4245                 channel_cnt_str = "Four"; break;
4246             case 3:
4247                 channel_cnt_str = "Three"; break;
4248             case 1:
4249                 channel_cnt_str = "One"; break;
4250             case 2:
4251             default:
4252                 channel_cnt_str = "Two"; break;
4253         }
4254 
4255         ctl = mixer_get_ctl_by_name(adev->mixer,
4256                                     my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
4257         if (!ctl) {
4258             ALOGE("%s:becf: afe: Could not get ctl for mixer command - %s",
4259                   __func__,
4260                   my_data->current_backend_cfg[backend_idx].channels_mixer_ctl);
4261             return -EINVAL;
4262         }
4263         mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
4264         my_data->current_backend_cfg[backend_idx].channels = channels;
4265 
4266         // skip EDID configuration for HDMI backend
4267 
4268         ALOGD("%s:becf: afe: %s set to %s", __func__,
4269               my_data->current_backend_cfg[backend_idx].channels_mixer_ctl,
4270               channel_cnt_str);
4271     }
4272 
4273     // skip set ext_display format mixer control
4274     return ret;
4275 }
4276 
platform_get_snd_device_bit_width(snd_device_t snd_device)4277 static int platform_get_snd_device_bit_width(snd_device_t snd_device)
4278 {
4279     if ((snd_device < SND_DEVICE_MIN) || (snd_device >= SND_DEVICE_MAX)) {
4280         ALOGE("%s: Invalid snd_device = %d", __func__, snd_device);
4281         return CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4282     }
4283 
4284     return backend_bit_width_table[snd_device];
4285 }
4286 
4287 /*
4288  * return backend_idx on which voice call is active
4289  */
platform_get_voice_call_backend(struct audio_device * adev)4290 static int platform_get_voice_call_backend(struct audio_device* adev)
4291 {
4292     struct audio_usecase *uc = NULL;
4293     struct listnode *node;
4294     snd_device_t out_snd_device = SND_DEVICE_NONE;
4295 
4296     int backend_idx = -1;
4297 
4298     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
4299         list_for_each(node, &adev->usecase_list) {
4300             uc =  node_to_item(node, struct audio_usecase, list);
4301             if (uc && uc->type == VOICE_CALL && uc->stream.out) {
4302                 out_snd_device = platform_get_output_snd_device(adev->platform,
4303                                                         uc->stream.out->devices);
4304                 backend_idx = platform_get_backend_index(out_snd_device);
4305                 break;
4306             }
4307         }
4308     }
4309     return backend_idx;
4310 }
4311 
4312 /*
4313  * goes through all the current usecases and picks the highest
4314  * bitwidth & samplerate
4315  */
platform_check_capture_backend_cfg(struct audio_device * adev,int backend_idx,struct audio_backend_cfg * backend_cfg)4316 static bool platform_check_capture_backend_cfg(struct audio_device* adev,
4317                                    int backend_idx,
4318                                    struct audio_backend_cfg *backend_cfg)
4319 {
4320     bool backend_change = false;
4321     unsigned int bit_width;
4322     unsigned int sample_rate;
4323     unsigned int channels;
4324     struct platform_data *my_data = (struct platform_data *)adev->platform;
4325 
4326     bit_width = backend_cfg->bit_width;
4327     sample_rate = backend_cfg->sample_rate;
4328     channels = backend_cfg->channels;
4329 
4330     ALOGV("%s:txbecf: afe: Codec selected backend: %d current bit width: %d and "
4331           "sample rate: %d, channels %d",__func__,backend_idx, bit_width,
4332           sample_rate, channels);
4333 
4334     // For voice calls use default configuration i.e. 16b/48K, only applicable to
4335     // default backend
4336     // force routing is not required here, caller will do it anyway
4337     if (voice_is_in_call(adev) || adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
4338         ALOGW("%s:txbecf: afe: Use default bw and sr for voice/voip calls and "
4339               "for unprocessed/camera source", __func__);
4340         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4341         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4342     }
4343 
4344     if (backend_idx == USB_AUDIO_TX_BACKEND) {
4345         audio_extn_usb_is_config_supported(&bit_width, &sample_rate, &channels, false);
4346         ALOGV("%s:txbecf: afe: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
4347               __func__, bit_width, sample_rate, channels);
4348     }
4349 
4350     ALOGV("%s:txbecf: afe: Codec selected backend: %d updated bit width: %d and "
4351           "sample rate: %d", __func__, backend_idx, bit_width, sample_rate);
4352 
4353     // Force routing if the expected bitwdith or samplerate
4354     // is not same as current backend comfiguration
4355     if ((bit_width != my_data->current_backend_cfg[backend_idx].bit_width) ||
4356         (sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate) ||
4357         (channels != my_data->current_backend_cfg[backend_idx].channels)) {
4358         backend_cfg->bit_width = bit_width;
4359         backend_cfg->sample_rate= sample_rate;
4360         backend_cfg->channels = channels;
4361         backend_change = true;
4362         ALOGI("%s:txbecf: afe: Codec backend needs to be updated. new bit width: %d "
4363               "new sample rate: %d new channel: %d",
4364               __func__, backend_cfg->bit_width,
4365               backend_cfg->sample_rate, backend_cfg->channels);
4366     }
4367 
4368     return backend_change;
4369 }
4370 
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)4371 static void pick_playback_cfg_for_uc(struct audio_device *adev,
4372                                      struct audio_usecase *usecase,
4373                                      snd_device_t snd_device,
4374                                      unsigned int *bit_width,
4375                                      unsigned int *sample_rate,
4376                                      unsigned int *channels)
4377 {
4378     int i =0;
4379     struct listnode *node;
4380     list_for_each(node, &adev->usecase_list) {
4381         struct audio_usecase *uc;
4382         uc = node_to_item(node, struct audio_usecase, list);
4383         struct stream_out *out = (struct stream_out*) uc->stream.out;
4384         if (uc->type == PCM_PLAYBACK && out && usecase != uc) {
4385             unsigned int out_channels = audio_channel_count_from_out_mask(out->channel_mask);
4386             ALOGV("%s:napb: (%d) - (%s)id (%d) sr %d bw "
4387                   "(%d) ch (%d) device %s", __func__, i++, use_case_table[uc->id],
4388                   uc->id, out->sample_rate,
4389                   pcm_format_to_bits(out->config.format), out_channels,
4390                   platform_get_snd_device_name(uc->out_snd_device));
4391 
4392             if (platform_check_backends_match(snd_device, uc->out_snd_device)) {
4393                 if (*bit_width < pcm_format_to_bits(out->config.format))
4394                     *bit_width = pcm_format_to_bits(out->config.format);
4395                 if (*sample_rate < out->sample_rate)
4396                     *sample_rate = out->sample_rate;
4397                 if (out->sample_rate < OUTPUT_SAMPLING_RATE_44100)
4398                     *sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4399                 if (*channels < out_channels)
4400                     *channels = out_channels;
4401             }
4402         }
4403     }
4404     return;
4405 }
4406 
headset_is_config_supported(unsigned int * bit_width,unsigned int * sample_rate,unsigned int * channels)4407 static void headset_is_config_supported(unsigned int *bit_width,
4408                                         unsigned int *sample_rate,
4409                                         unsigned int *channels) {
4410     switch (*bit_width) {
4411         case 16:
4412         case 24:
4413             break;
4414         default:
4415             *bit_width = 16;
4416             break;
4417     }
4418 
4419     if (*sample_rate > 192000) {
4420         *sample_rate = 192000;
4421     }
4422 
4423     if (*channels > 2) {
4424         *channels = 2;
4425     }
4426 }
4427 
platform_check_playback_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device,struct audio_backend_cfg * backend_cfg)4428 static bool platform_check_playback_backend_cfg(struct audio_device* adev,
4429                                              struct audio_usecase* usecase,
4430                                              snd_device_t snd_device,
4431                                              struct audio_backend_cfg *backend_cfg)
4432 {
4433     bool backend_change = false;
4434     unsigned int bit_width;
4435     unsigned int sample_rate;
4436     unsigned int channels;
4437     int backend_idx = DEFAULT_CODEC_BACKEND;
4438     unsigned long service_interval = 0; // 0 is invalid
4439     struct platform_data *my_data = (struct platform_data *)adev->platform;
4440 
4441     if (snd_device == SND_DEVICE_OUT_BT_SCO ||
4442         snd_device == SND_DEVICE_OUT_BT_SCO_WB) {
4443         backend_change = false;
4444         return backend_change;
4445     }
4446 
4447     backend_idx = platform_get_backend_index(snd_device);
4448     bit_width = backend_cfg->bit_width;
4449     sample_rate = backend_cfg->sample_rate;
4450     channels = backend_cfg->channels;
4451 
4452     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4453           ", backend_idx %d usecase = %d device (%s)", __func__, bit_width,
4454           sample_rate, channels, backend_idx, usecase->id,
4455           platform_get_snd_device_name(snd_device));
4456 
4457     if (backend_idx == platform_get_voice_call_backend(adev)) {
4458         ALOGW("%s:becf: afe:Use default bw and sr for voice/voip calls ",
4459               __func__);
4460         bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4461         sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4462         channels = CODEC_BACKEND_DEFAULT_CHANNELS;
4463     } else {
4464         /*
4465          * The backend should be configured at highest bit width and/or
4466          * sample rate amongst all playback usecases.
4467          * If the selected sample rate and/or bit width differ with
4468          * current backend sample rate and/or bit width, then, we set the
4469          * backend re-configuration flag.
4470          *
4471          * Exception: 16 bit playbacks is allowed through 16 bit/48/44.1 khz backend only
4472          */
4473         pick_playback_cfg_for_uc(adev, usecase, snd_device,
4474                                  &bit_width,
4475                                  &sample_rate,
4476                                  &channels);
4477     }
4478 
4479     switch (backend_idx) {
4480         case USB_AUDIO_RX_BACKEND:
4481             audio_extn_usb_is_config_supported(&bit_width,
4482                                                &sample_rate, &channels, true);
4483             if (platform_get_usb_service_interval(adev->platform, true,
4484                                                   &service_interval) == 0) {
4485                 /* overwrite with best altset for this service interval */
4486                 int ret =
4487                         audio_extn_usb_altset_for_service_interval(true /*playback*/,
4488                                                                    service_interval,
4489                                                                    &bit_width,
4490                                                                    &sample_rate,
4491                                                                    &channels);
4492                 if (ret < 0) {
4493                     ALOGE("Failed to find altset for service interval %lu, skip reconfig",
4494                           service_interval);
4495                     return false;
4496                 }
4497             }
4498             ALOGV("%s: USB BE configured as bit_width(%d)sample_rate(%d)channels(%d)",
4499                   __func__, bit_width, sample_rate, channels);
4500             break;
4501         case HEADPHONE_BACKEND:
4502             headset_is_config_supported(&bit_width, &sample_rate, &channels);
4503             break;
4504         case DEFAULT_CODEC_BACKEND:
4505         default:
4506             bit_width = platform_get_snd_device_bit_width(snd_device);
4507             sample_rate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4508             channels = CODEC_BACKEND_DEFAULT_CHANNELS;
4509             break;
4510     }
4511 
4512     ALOGV("%s:becf: afe: Codec selected backend: %d updated bit width: %d and"
4513           "sample rate: %d",
4514           __func__, backend_idx , bit_width, sample_rate);
4515 
4516     // Force routing if the expected bitwdith or samplerate
4517     // is not same as current backend comfiguration
4518     if (bit_width != my_data->current_backend_cfg[backend_idx].bit_width ||
4519         sample_rate != my_data->current_backend_cfg[backend_idx].sample_rate ||
4520         channels != my_data->current_backend_cfg[backend_idx].channels) {
4521         backend_cfg->bit_width = bit_width;
4522         backend_cfg->sample_rate = sample_rate;
4523         backend_cfg->channels = channels;
4524         backend_cfg->passthrough_enabled = false;
4525         backend_change = true;
4526         ALOGV("%s:becf: afe: Codec backend needs to be updated. new bit width: %d"
4527               "new sample rate: %d new channels: %d",
4528               __func__, backend_cfg->bit_width, backend_cfg->sample_rate, backend_cfg->channels);
4529     }
4530 
4531     return backend_change;
4532 }
4533 
platform_check_and_set_playback_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device)4534 bool platform_check_and_set_playback_backend_cfg(struct audio_device* adev,
4535     struct audio_usecase *usecase, snd_device_t snd_device)
4536 {
4537     int backend_idx = DEFAULT_CODEC_BACKEND;
4538     int new_snd_devices[SND_DEVICE_OUT_END];
4539     int i, num_devices = 1;
4540     bool ret = false;
4541     struct platform_data *my_data = (struct platform_data *)adev->platform;
4542     struct audio_backend_cfg backend_cfg;
4543 
4544     backend_idx = platform_get_backend_index(snd_device);
4545 
4546     backend_cfg.bit_width = pcm_format_to_bits(usecase->stream.out->config.format);
4547     backend_cfg.sample_rate = usecase->stream.out->sample_rate;
4548     backend_cfg.format = usecase->stream.out->format;
4549     backend_cfg.channels = audio_channel_count_from_out_mask(usecase->stream.out->channel_mask);
4550     /*this is populated by check_codec_backend_cfg hence set default value to false*/
4551     backend_cfg.passthrough_enabled = false;
4552 
4553     ALOGV("%s:becf: afe: bitwidth %d, samplerate %d channels %d"
4554           ", backend_idx %d usecase = %d device (%s)", __func__, backend_cfg.bit_width,
4555           backend_cfg.sample_rate, backend_cfg.channels, backend_idx, usecase->id,
4556           platform_get_snd_device_name(snd_device));
4557 
4558     if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices) < 0)
4559         new_snd_devices[0] = snd_device;
4560 
4561     for (i = 0; i < num_devices; i++) {
4562         ALOGV("%s: new_snd_devices[%d] is %d", __func__, i, new_snd_devices[i]);
4563         if ((platform_check_playback_backend_cfg(adev, usecase, new_snd_devices[i],
4564                                                  &backend_cfg))) {
4565             platform_set_backend_cfg(adev, new_snd_devices[i],
4566                                      &backend_cfg);
4567             ret = true;
4568         }
4569     }
4570     return ret;
4571 }
4572 
platform_check_and_set_capture_backend_cfg(struct audio_device * adev,struct audio_usecase * usecase,snd_device_t snd_device)4573 bool platform_check_and_set_capture_backend_cfg(struct audio_device* adev,
4574     struct audio_usecase *usecase, snd_device_t snd_device)
4575 {
4576     int backend_idx = platform_get_backend_index(snd_device);
4577     int ret = 0;
4578     struct audio_backend_cfg backend_cfg;
4579     memset(&backend_cfg, 0, sizeof(struct audio_backend_cfg));
4580 
4581     if (usecase->type == PCM_CAPTURE) {
4582         backend_cfg.format = usecase->stream.in->format;
4583         backend_cfg.channels = audio_channel_count_from_in_mask(usecase->stream.in->channel_mask);
4584     } else {
4585         backend_cfg.bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
4586         backend_cfg.sample_rate =  CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
4587         backend_cfg.format = AUDIO_FORMAT_PCM_16_BIT;
4588         backend_cfg.channels = 1;
4589     }
4590 
4591     ALOGV("%s:txbecf: afe: bitwidth %d, samplerate %d, channel %d"
4592           ", backend_idx %d usecase = %d device (%s)", __func__,
4593           backend_cfg.bit_width,
4594           backend_cfg.sample_rate,
4595           backend_cfg.channels,
4596           backend_idx, usecase->id,
4597           platform_get_snd_device_name(snd_device));
4598 
4599     if (platform_check_capture_backend_cfg(adev, backend_idx, &backend_cfg)) {
4600         ret = platform_set_backend_cfg(adev, snd_device,
4601                                        &backend_cfg);
4602         if(!ret)
4603             return true;
4604     }
4605 
4606     return false;
4607 }
4608 
4609 static int max_be_dai_names = 0;
4610 static const struct be_dai_name_struct *be_dai_name_table;
4611 
4612 /*
4613  * Retrieves the be_dai_name_table from kernel to enable a mapping
4614  * between sound device hw interfaces and backend IDs. This allows HAL to
4615  * specify the backend a specific calibration is needed for.
4616  */
init_be_dai_name_table(struct audio_device * adev)4617 static int init_be_dai_name_table(struct audio_device *adev)
4618 {
4619     const char *mixer_ctl_name = "Backend DAI Name Table";
4620     struct mixer_ctl *ctl;
4621     int i, j, ret, size;
4622     bool valid_hw_interface;
4623 
4624     ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
4625     if (!ctl) {
4626         ALOGE("%s: Could not get ctl for mixer name %s\n",
4627                __func__, mixer_ctl_name);
4628         ret = -EINVAL;
4629         goto done;
4630     }
4631 
4632     mixer_ctl_update(ctl);
4633 
4634     size = mixer_ctl_get_num_values(ctl);
4635     if (size <= 0){
4636         ALOGE("%s: Failed to get %s size %d\n",
4637                __func__, mixer_ctl_name, size);
4638         ret = -EFAULT;
4639         goto done;
4640     }
4641 
4642     be_dai_name_table =
4643             (const struct be_dai_name_struct *)calloc(1, size);
4644     if (be_dai_name_table == NULL) {
4645         ALOGE("%s: Failed to allocate memory for %s\n",
4646                __func__, mixer_ctl_name);
4647         ret = -ENOMEM;
4648         goto freeMem;
4649     }
4650 
4651     ret = mixer_ctl_get_array(ctl, (void *)be_dai_name_table, size);
4652     if (ret) {
4653         ALOGE("%s: Failed to get %s, ret %d\n",
4654                __func__, mixer_ctl_name, ret);
4655         ret = -EFAULT;
4656         goto freeMem;
4657     }
4658 
4659     if (be_dai_name_table != NULL) {
4660         max_be_dai_names = size / sizeof(struct be_dai_name_struct);
4661         ALOGV("%s: Successfully got %s, number of be dais is %d\n",
4662               __func__, mixer_ctl_name, max_be_dai_names);
4663         ret = 0;
4664     } else {
4665         ALOGE("%s: Failed to get %s\n", __func__, mixer_ctl_name);
4666         ret = -EFAULT;
4667         goto freeMem;
4668     }
4669 
4670     /*
4671      * Validate all sound devices have a valid backend set to catch
4672      * errors for uncommon sound devices
4673      */
4674     for (i = 0; i < SND_DEVICE_MAX; i++) {
4675         valid_hw_interface = false;
4676 
4677         if (hw_interface_table[i] == NULL) {
4678             ALOGW("%s: sound device %s has no hw interface set\n",
4679                   __func__, platform_get_snd_device_name(i));
4680             continue;
4681         }
4682 
4683         for (j = 0; j < max_be_dai_names; j++) {
4684             if (strcmp(hw_interface_table[i], be_dai_name_table[j].be_name)
4685                 == 0) {
4686                 valid_hw_interface = true;
4687                 break;
4688             }
4689         }
4690         if (!valid_hw_interface)
4691             ALOGD("%s: sound device %s does not have a valid hw interface set "
4692                   "(disregard for combo devices) %s\n",
4693                   __func__, platform_get_snd_device_name(i),
4694                   hw_interface_table[i]);
4695     }
4696 
4697     goto done;
4698 
4699 freeMem:
4700     if (be_dai_name_table) {
4701         free((void *)be_dai_name_table);
4702         be_dai_name_table = NULL;
4703     }
4704 
4705 done:
4706     return ret;
4707 }
4708 
platform_get_snd_device_backend_index(snd_device_t device)4709 int platform_get_snd_device_backend_index(snd_device_t device)
4710 {
4711     int i, be_dai_id;
4712     const char * hw_interface_name = NULL;
4713 
4714     ALOGV("%s: enter with device %d\n", __func__, device);
4715 
4716     if ((device < SND_DEVICE_MIN) || (device >= SND_DEVICE_MAX)) {
4717         ALOGE("%s: Invalid snd_device = %d",
4718               __func__, device);
4719         be_dai_id = -EINVAL;
4720         goto done;
4721     }
4722 
4723     /* Get string value of necessary backend for device */
4724     hw_interface_name = hw_interface_table[device];
4725     if (hw_interface_name == NULL) {
4726         ALOGE("%s: no hw_interface set for device %d\n", __func__, device);
4727         be_dai_id = -EINVAL;
4728         goto done;
4729     }
4730 
4731     /* Check if be dai name table was retrieved successfully */
4732     if (be_dai_name_table == NULL) {
4733         ALOGE("%s: BE DAI Name Table is not present\n", __func__);
4734         be_dai_id = -EFAULT;
4735         goto done;
4736     }
4737 
4738     /* Get backend ID for device specified */
4739     for (i = 0; i < max_be_dai_names; i++) {
4740         if (strcmp(hw_interface_name, be_dai_name_table[i].be_name) == 0) {
4741             be_dai_id = be_dai_name_table[i].be_id;
4742             goto done;
4743         }
4744     }
4745     ALOGE("%s: no interface matching name %s\n", __func__, hw_interface_name);
4746     be_dai_id = -EINVAL;
4747     goto done;
4748 
4749 done:
4750     return be_dai_id;
4751 }
4752 
platform_check_and_update_copp_sample_rate(void * platform,snd_device_t snd_device,unsigned int stream_sr,int * sample_rate)4753 void platform_check_and_update_copp_sample_rate(void* platform, snd_device_t snd_device,
4754                                                 unsigned int stream_sr, int* sample_rate)
4755 {
4756     struct platform_data* my_data = (struct platform_data *)platform;
4757     int backend_idx = platform_get_backend_index(snd_device);
4758     int device_sr = my_data->current_backend_cfg[backend_idx].sample_rate;
4759     /*
4760      *Check if device SR is multiple of 8K or 11.025 Khz
4761      *check if the stream SR is multiple of same base, if yes
4762      *then have copp SR equal to stream SR, this ensures that
4763      *post processing happens at stream SR, else have
4764      *copp SR equal to device SR.
4765      */
4766     if (!(((sample_rate_multiple(device_sr, SAMPLE_RATE_8000)) &&
4767            (sample_rate_multiple(stream_sr, SAMPLE_RATE_8000))) ||
4768           ((sample_rate_multiple(device_sr, SAMPLE_RATE_11025)) &&
4769            (sample_rate_multiple(stream_sr, SAMPLE_RATE_11025))))) {
4770         *sample_rate = device_sr;
4771     } else
4772         *sample_rate = stream_sr;
4773 
4774     ALOGI("sn_device %d device sr %d stream sr %d copp sr %d", snd_device, device_sr, stream_sr
4775           , *sample_rate);
4776 
4777 }
4778 
4779 // called from info parser
platform_add_app_type(const char * uc_type,const char * mode,int bw,int app_type,int max_rate)4780 void platform_add_app_type(const char *uc_type,
4781                            const char *mode,
4782                            int bw,
4783                            int app_type, int max_rate) {
4784     struct app_type_entry *ap =
4785             (struct app_type_entry *)calloc(1, sizeof(struct app_type_entry));
4786 
4787     if (!ap) {
4788         ALOGE("%s failed to allocate mem for app type", __func__);
4789         return;
4790     }
4791 
4792     ap->uc_type = -1;
4793     for (int i=0; i<USECASE_TYPE_MAX; i++) {
4794         if (!strcmp(uc_type, usecase_type_index[i].name)) {
4795             ap->uc_type = usecase_type_index[i].index;
4796             break;
4797         }
4798     }
4799 
4800     if (ap->uc_type == -1) {
4801         free(ap);
4802         return;
4803     }
4804 
4805     ALOGI("%s uc %s mode %s bw %d app_type %d max_rate %d",
4806           __func__, uc_type, mode, bw, app_type, max_rate);
4807     ap->bit_width = bw;
4808     ap->app_type = app_type;
4809     ap->max_rate = max_rate;
4810     ap->mode = strdup(mode);
4811     list_add_tail(&app_type_entry_list, &ap->node);
4812 }
4813 
4814 
platform_get_default_app_type_v2(void * platform __unused,usecase_type_t type,int * app_type)4815 int platform_get_default_app_type_v2(void *platform __unused,
4816                                      usecase_type_t type,
4817                                      int *app_type )
4818 {
4819     if (type == PCM_PLAYBACK)
4820         *app_type = DEFAULT_APP_TYPE_RX_PATH;
4821     else
4822         *app_type = DEFAULT_APP_TYPE_TX_PATH;
4823     return 0;
4824 }
4825 
platform_get_app_type_v2(void * platform,usecase_type_t uc_type,const char * mode,int bw,int sr __unused,int * app_type)4826 int platform_get_app_type_v2(void *platform,
4827                              usecase_type_t uc_type,
4828                              const char *mode,
4829                              int bw, int sr __unused,
4830                              int *app_type)
4831 {
4832     struct listnode *node;
4833     struct app_type_entry *entry;
4834     *app_type = -1;
4835 
4836     ALOGV("%s find match for uc %d mode %s bw %d rate %d",
4837           __func__, uc_type, mode, bw, sr);
4838     list_for_each(node, &app_type_entry_list) {
4839         entry = node_to_item(node, struct app_type_entry, node);
4840         ALOGV("%s uc %d mode %s bw %d app_type %d max_rate %d",
4841               __func__, entry->uc_type, entry->mode, entry->bit_width,
4842               entry->app_type, entry->max_rate);
4843         if (entry->bit_width == bw &&
4844             entry->uc_type == uc_type &&
4845             sr <= entry->max_rate &&
4846             entry->mode && !strcmp(mode, entry->mode)) {
4847             ALOGV("%s found match %d", __func__, entry->app_type);
4848             *app_type = entry->app_type;
4849             break;
4850         }
4851     }
4852 
4853     if (*app_type == -1) {
4854         ALOGV("%s no match found, return default", __func__);
4855         return platform_get_default_app_type_v2(platform, uc_type, app_type);
4856     }
4857     return 0;
4858 }
4859 
platform_set_sidetone(struct audio_device * adev,snd_device_t out_snd_device,bool enable,char * str)4860 int platform_set_sidetone(struct audio_device *adev,
4861                           snd_device_t out_snd_device,
4862                           bool enable, char *str)
4863 {
4864     int ret;
4865     if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
4866         out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
4867             ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
4868             if (ret)
4869                 ALOGI("%s: usb device %d does not support device sidetone\n",
4870                   __func__, out_snd_device);
4871     } else {
4872         ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
4873               __func__, out_snd_device, str);
4874         if (enable)
4875             audio_route_apply_and_update_path(adev->audio_route, str);
4876         else
4877             audio_route_reset_and_update_path(adev->audio_route, str);
4878     }
4879     return 0;
4880 }
4881 
platform_get_mmap_data_fd(void * platform __unused,int fe_dev __unused,int dir __unused,int * fd __unused,uint32_t * size __unused)4882 int platform_get_mmap_data_fd(void *platform __unused, int fe_dev __unused, int dir __unused,
4883                               int *fd __unused, uint32_t *size __unused)
4884 {
4885 #if defined (PLATFORM_MSM8996) || (PLATFORM_MSM8998) || (PLATFORM_SDM845) || (PLATFORM_SDM710) || (PLATFORM_SM8150)
4886     struct platform_data *my_data = (struct platform_data *)platform;
4887     struct audio_device *adev = my_data->adev;
4888     int hw_fd = -1;
4889     char dev_name[128];
4890     struct snd_pcm_mmap_fd mmap_fd;
4891     memset(&mmap_fd, 0, sizeof(mmap_fd));
4892     mmap_fd.dir = dir;
4893     snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
4894              adev->snd_card, HWDEP_FE_BASE+fe_dev);
4895     hw_fd = open(dev_name, O_RDONLY);
4896     if (hw_fd < 0) {
4897         ALOGE("fe hw dep node open %d/%d failed", adev->snd_card, fe_dev);
4898         return -1;
4899     }
4900     if (ioctl(hw_fd, SNDRV_PCM_IOCTL_MMAP_DATA_FD, &mmap_fd) < 0) {
4901         ALOGE("fe hw dep node ioctl failed");
4902         close(hw_fd);
4903         return -1;
4904     }
4905     *fd = mmap_fd.fd;
4906     *size = mmap_fd.size;
4907     close(hw_fd); // mmap_fd should still be valid
4908     return 0;
4909 #else
4910     return -1;
4911 #endif
4912 }
4913 
platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id)4914 bool platform_sound_trigger_usecase_needs_event(audio_usecase_t uc_id)
4915 {
4916     bool needs_event = false;
4917 
4918     switch (uc_id) {
4919     /* concurrent capture usecases which needs event */
4920     case USECASE_AUDIO_RECORD:
4921     case USECASE_AUDIO_RECORD_LOW_LATENCY:
4922     case USECASE_AUDIO_RECORD_MMAP:
4923     case USECASE_AUDIO_RECORD_HIFI:
4924     case USECASE_AUDIO_RECORD_VOIP:
4925     case USECASE_VOICEMMODE1_CALL:
4926     case USECASE_VOICEMMODE2_CALL:
4927     /* concurrent playback usecases that needs event */
4928     case USECASE_AUDIO_PLAYBACK_DEEP_BUFFER:
4929     case USECASE_AUDIO_PLAYBACK_OFFLOAD:
4930         needs_event = true;
4931         break;
4932     default:
4933         ALOGV("%s:usecase_id[%d] no need to raise event.", __func__, uc_id);
4934     }
4935     return needs_event;
4936 }
4937 
platform_snd_device_has_speaker(snd_device_t dev)4938 bool platform_snd_device_has_speaker(snd_device_t dev) {
4939     int num_devs = 2;
4940     snd_device_t split_devs[2] = {SND_DEVICE_NONE, SND_DEVICE_NONE};
4941     if (platform_can_split_snd_device(dev, &num_devs, split_devs) == 0) {
4942         return platform_snd_device_has_speaker(split_devs[0]) ||
4943                 platform_snd_device_has_speaker(split_devs[1]);
4944     }
4945 
4946     switch (dev) {
4947         case SND_DEVICE_OUT_SPEAKER:
4948         case SND_DEVICE_OUT_SPEAKER_SAFE:
4949         case SND_DEVICE_OUT_SPEAKER_REVERSE:
4950         case SND_DEVICE_OUT_SPEAKER_PROTECTED:
4951         case SND_DEVICE_OUT_VOICE_SPEAKER_PROTECTED:
4952         case SND_DEVICE_OUT_VOICE_SPEAKER_HFP:
4953             return true;
4954         default:
4955             break;
4956     }
4957     return false;
4958 }
4959 
platform_set_microphone_characteristic(void * platform,struct audio_microphone_characteristic_t mic)4960 bool platform_set_microphone_characteristic(void *platform,
4961                                             struct audio_microphone_characteristic_t mic) {
4962     struct platform_data *my_data = (struct platform_data *)platform;
4963     if (my_data->declared_mic_count >= AUDIO_MICROPHONE_MAX_COUNT) {
4964         ALOGE("mic number is more than maximum number");
4965         return false;
4966     }
4967     for (size_t ch = 0; ch < AUDIO_CHANNEL_COUNT_MAX; ch++) {
4968         mic.channel_mapping[ch] = AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED;
4969     }
4970     my_data->microphones[my_data->declared_mic_count++] = mic;
4971     return true;
4972 }
4973 
platform_get_microphones(void * platform,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)4974 int platform_get_microphones(void *platform,
4975                              struct audio_microphone_characteristic_t *mic_array,
4976                              size_t *mic_count) {
4977     struct platform_data *my_data = (struct platform_data *)platform;
4978     if (mic_count == NULL) {
4979         return -EINVAL;
4980     }
4981     if (mic_array == NULL) {
4982         return -EINVAL;
4983     }
4984 
4985     if (*mic_count == 0) {
4986         *mic_count = my_data->declared_mic_count;
4987         return 0;
4988     }
4989 
4990     size_t max_mic_count = *mic_count;
4991     size_t actual_mic_count = 0;
4992     for (size_t i = 0; i < max_mic_count && i < my_data->declared_mic_count; i++) {
4993         mic_array[i] = my_data->microphones[i];
4994         actual_mic_count++;
4995     }
4996     *mic_count = actual_mic_count;
4997     return 0;
4998 }
4999 
platform_set_microphone_map(void * platform,snd_device_t in_snd_device,const struct mic_info * info)5000 bool platform_set_microphone_map(void *platform, snd_device_t in_snd_device,
5001                                  const struct mic_info *info) {
5002     struct platform_data *my_data = (struct platform_data *)platform;
5003     if (in_snd_device < SND_DEVICE_IN_BEGIN || in_snd_device >= SND_DEVICE_IN_END) {
5004         ALOGE("%s: Sound device not valid", __func__);
5005         return false;
5006     }
5007     size_t m_count = my_data->mic_map[in_snd_device].mic_count++;
5008     if (m_count >= AUDIO_MICROPHONE_MAX_COUNT) {
5009         ALOGE("%s: Microphone count is greater than max allowed value", __func__);
5010         my_data->mic_map[in_snd_device].mic_count--;
5011         return false;
5012     }
5013     my_data->mic_map[in_snd_device].microphones[m_count] = *info;
5014     return true;
5015 }
5016 
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)5017 int platform_get_active_microphones(void *platform, unsigned int channels,
5018                                     audio_usecase_t uc_id,
5019                                     struct audio_microphone_characteristic_t *mic_array,
5020                                     size_t *mic_count) {
5021     struct platform_data *my_data = (struct platform_data *)platform;
5022     struct audio_usecase *usecase = get_usecase_from_list(my_data->adev, uc_id);
5023     if (mic_count == NULL || mic_array == NULL || usecase == NULL) {
5024         return -EINVAL;
5025     }
5026     size_t max_mic_count = my_data->declared_mic_count;
5027     size_t actual_mic_count = 0;
5028 
5029     snd_device_t active_input_snd_device =
5030             platform_get_input_snd_device(platform, usecase->stream.in, AUDIO_DEVICE_NONE);
5031     if (active_input_snd_device == SND_DEVICE_NONE) {
5032         ALOGI("%s: No active microphones found", __func__);
5033         goto end;
5034     }
5035 
5036     size_t  active_mic_count = my_data->mic_map[active_input_snd_device].mic_count;
5037     struct mic_info *m_info = my_data->mic_map[active_input_snd_device].microphones;
5038 
5039     for (size_t i = 0; i < active_mic_count; i++) {
5040         unsigned int channels_for_active_mic = channels;
5041         if (channels_for_active_mic > m_info[i].channel_count) {
5042             channels_for_active_mic = m_info[i].channel_count;
5043         }
5044         for (size_t j = 0; j < max_mic_count; j++) {
5045             if (strcmp(my_data->microphones[j].device_id,
5046                        m_info[i].device_id) == 0) {
5047                 mic_array[actual_mic_count] = my_data->microphones[j];
5048                 for (size_t ch = 0; ch < channels_for_active_mic; ch++) {
5049                      mic_array[actual_mic_count].channel_mapping[ch] =
5050                              m_info[i].channel_mapping[ch];
5051                 }
5052                 actual_mic_count++;
5053                 break;
5054             }
5055         }
5056     }
5057 end:
5058     *mic_count = actual_mic_count;
5059     return 0;
5060 }
5061 
platform_set_usb_service_interval(void * platform,bool playback,unsigned long service_interval,bool * reconfig)5062 int platform_set_usb_service_interval(void *platform,
5063                                       bool playback,
5064                                       unsigned long service_interval,
5065                                       bool *reconfig)
5066 {
5067 #if defined (USB_SERVICE_INTERVAL_ENABLED)
5068     struct platform_data *_platform = (struct platform_data *)platform;
5069     *reconfig = false;
5070     if (!playback) {
5071         ALOGE("%s not valid for capture", __func__);
5072         return -1;
5073     }
5074     const char *ctl_name = "USB_AUDIO_RX service_interval";
5075     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
5076                                                   ctl_name);
5077     if (!ctl) {
5078         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
5079         return -1;
5080     }
5081     if (mixer_ctl_get_value(ctl, 0) != (int)service_interval) {
5082         mixer_ctl_set_value(ctl, 0, service_interval);
5083         *reconfig = true;
5084     }
5085     return 0;
5086 #else
5087     *reconfig = false;
5088     (void)platform;
5089     (void)playback;
5090     (void)service_interval;
5091     return -1;
5092 #endif
5093 }
5094 
platform_get_usb_service_interval(void * platform,bool playback,unsigned long * service_interval)5095 int platform_get_usb_service_interval(void *platform,
5096                                       bool playback,
5097                                       unsigned long *service_interval)
5098 {
5099 #if defined (USB_SERVICE_INTERVAL_ENABLED)
5100     struct platform_data *_platform = (struct platform_data *)platform;
5101     if (!playback) {
5102         ALOGE("%s not valid for capture", __func__);
5103         return -1;
5104     }
5105     const char *ctl_name = "USB_AUDIO_RX service_interval";
5106     struct mixer_ctl *ctl = mixer_get_ctl_by_name(_platform->adev->mixer,
5107                                                   ctl_name);
5108     if (!ctl) {
5109         ALOGV("%s: could not get mixer %s", __func__, ctl_name);
5110         return -1;
5111     }
5112     *service_interval = mixer_ctl_get_value(ctl, 0);
5113     return 0;
5114 #else
5115     (void)platform;
5116     (void)playback;
5117     (void)service_interval;
5118     return -1;
5119 #endif
5120 }
5121 
platform_set_acdb_metainfo_key(void * platform __unused,char * name __unused,int key __unused)5122 int platform_set_acdb_metainfo_key(void *platform __unused,
5123                                    char *name __unused,
5124                                    int key __unused)
5125 {
5126     return -ENOSYS;
5127 }
5128