blob: d2b2b94b396939592fd99c9643ad0ece2d83bf89 [file] [log] [blame]
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001/*
Arun Mirpuria13495c2019-04-11 16:08:12 -07002 * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
Vignesh Kulothungan55396882017-04-20 14:37:02 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_acdb"
21//#define LOG_NDEBUG 0
22#define LOG_NDDEBUG 0
23
24#include <stdlib.h>
25#include <dlfcn.h>
Weiyin Jiang2995f662019-04-17 14:25:12 +080026#include <log/log.h>
Vignesh Kulothungan55396882017-04-20 14:37:02 -070027#include <cutils/list.h>
Vinay Vermaaddfa4a2018-04-29 14:03:38 +053028#include <time.h>
Vignesh Kulothungan55396882017-04-20 14:37:02 -070029#include "acdb.h"
30#include "platform_api.h"
Arun Mirpuria13495c2019-04-11 16:08:12 -070031#include "audio_extn.h"
32#include <platform.h>
Vignesh Kulothungan55396882017-04-20 14:37:02 -070033
Aditya Bavanari29bcea22017-10-03 20:10:35 +053034#ifdef INSTANCE_ID_ENABLED
35int check_and_set_instance_id_support(struct mixer* mixer, bool acdb_support)
36{
37 const char *mixer_ctl_name = "Instance ID Support";
38 struct mixer_ctl* ctl;
39
40 ALOGV("%s", __func__);
41
42 /* Check for ACDB and property instance ID support and issue mixer control */
43 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
44 if (!ctl) {
45 ALOGE("%s: Could not get ctl for mixer cmd - %s",
46 __func__, mixer_ctl_name);
47 return -EINVAL;
48 }
49
50 ALOGD("%s: Final Instance ID support:%d\n", __func__, acdb_support);
51 if (mixer_ctl_set_value(ctl, 0, acdb_support) < 0) {
52 ALOGE("%s: Could not set Instance ID support %d", __func__,
53 acdb_support);
54 return -EINVAL;
55 }
56 return 0;
57}
58#else
59#define check_and_set_instance_id_support(x, y) -ENOSYS
60#endif
61
Arun Mirpuria13495c2019-04-11 16:08:12 -070062void get_platform_file_for_device(struct mixer *mixer, char* platform_info_file)
63{
64 const char *snd_card_name = NULL;
65
66 if (mixer != NULL) {
67 /* Get Sound card name */
68 snd_card_name = mixer_get_name(mixer);
69 if (!snd_card_name) {
70 ALOGE("failed to allocate memory for snd_card_name");
71 return;
72 }
73 /* Get platform info file for target */
74 audio_extn_utils_get_platform_info(snd_card_name, platform_info_file);
75 }
76}
77
Vignesh Kulothungan55396882017-04-20 14:37:02 -070078int acdb_init(int snd_card_num)
79{
80
81 int result = -1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -070082 struct mixer *mixer = NULL;
Vignesh Kulothungan55396882017-04-20 14:37:02 -070083
84 if(snd_card_num < 0) {
85 ALOGE("invalid sound card number");
86 return result;
87 }
88
89 mixer = mixer_open(snd_card_num);
90 if (!mixer) {
91 ALOGE("%s: Unable to open the mixer card: %d", __func__,
92 snd_card_num);
Soumya Managoli9fee7c62018-04-06 16:21:50 +053093 return result;
94 }
95 result = acdb_init_v2(mixer);
96 mixer_close(mixer);
97 return result;
98}
99
100int acdb_init_v2(struct mixer *mixer)
101{
102
103 int result = -1;
104 char *cvd_version = NULL;
Arun Mirpuria13495c2019-04-11 16:08:12 -0700105 char platform_info_file[MIXER_PATH_MAX_LENGTH] = PLATFORM_INFO_XML_PATH;
Soumya Managoli9fee7c62018-04-06 16:21:50 +0530106 const char *snd_card_name = NULL;
107 struct acdb_platform_data *my_data = NULL;
108
109 if (!mixer) {
110 ALOGE("Invalid mixer handle");
111 return result;
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700112 }
113
114 my_data = calloc(1, sizeof(struct acdb_platform_data));
115 if (!my_data) {
116 ALOGE("failed to allocate acdb platform data");
117 goto cleanup;
118 }
119
120 list_init(&my_data->acdb_meta_key_list);
Arun Mirpuria13495c2019-04-11 16:08:12 -0700121 get_platform_file_for_device(mixer, platform_info_file);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700122 /* Extract META KEY LIST INFO */
Arun Mirpuria13495c2019-04-11 16:08:12 -0700123 platform_info_init(platform_info_file, my_data, ACDB_EXTN);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700124
125 my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
126 if (my_data->acdb_handle == NULL) {
127 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
128 goto cleanup;
129 }
130
131 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
132
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530133 my_data->acdb_init_v4 = (acdb_init_v4_t)dlsym(my_data->acdb_handle,
134 "acdb_loader_init_v4");
135 if (my_data->acdb_init_v4 == NULL)
136 ALOGE("%s: dlsym error %s for acdb_loader_init_v4", __func__, dlerror());
137
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700138 my_data->acdb_init_v3 = (acdb_init_v3_t)dlsym(my_data->acdb_handle,
139 "acdb_loader_init_v3");
140 if (my_data->acdb_init_v3 == NULL)
141 ALOGE("%s: dlsym error %s for acdb_loader_init_v3", __func__, dlerror());
142
143 my_data->acdb_init_v2 = (acdb_init_v2_t)dlsym(my_data->acdb_handle,
144 "acdb_loader_init_v2");
145 if (my_data->acdb_init_v2 == NULL)
146 ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror());
147
148 my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
149 "acdb_loader_init_ACDB");
150 if (my_data->acdb_init == NULL && my_data->acdb_init_v2 == NULL
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530151 && my_data->acdb_init_v3 == NULL && my_data->acdb_init_v4 == NULL) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700152 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
153 goto cleanup;
154 }
155
156 /* Get CVD version */
157 cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
158 if (!cvd_version) {
159 ALOGE("%s: Failed to allocate cvd version", __func__);
160 goto cleanup;
161 } else {
162 struct mixer_ctl *ctl = NULL;
163 int count = 0;
164
165 ctl = mixer_get_ctl_by_name(mixer, CVD_VERSION_MIXER_CTL);
166 if (!ctl) {
167 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, CVD_VERSION_MIXER_CTL);
168 goto cleanup;
169 }
170 mixer_ctl_update(ctl);
171
172 count = mixer_ctl_get_num_values(ctl);
173 if (count > MAX_CVD_VERSION_STRING_SIZE)
174 count = MAX_CVD_VERSION_STRING_SIZE;
175
176 result = mixer_ctl_get_array(ctl, cvd_version, count);
177 if (result != 0) {
178 ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
179 goto cleanup;
180 }
181 }
182
183 /* Get Sound card name */
Aditya Bavanari71b6d532018-01-16 17:48:08 +0530184 snd_card_name = mixer_get_name(mixer);
Weiyin Jiang301dac62019-05-08 13:58:00 +0800185 snd_card_name = platform_get_snd_card_name_for_acdb_loader(snd_card_name);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700186 if (!snd_card_name) {
Weiyin Jiang301dac62019-05-08 13:58:00 +0800187 ALOGE("failed to get snd_card_name");
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700188 result = -1;
189 goto cleanup;
190 }
191
192 int key = 0;
193 struct listnode *node = NULL;
194 struct meta_key_list *key_info = NULL;
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530195 static bool acdb_instance_id_support = false;
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700196
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530197 my_data->acdb_init_data.cvd_version = cvd_version;
198 my_data->acdb_init_data.snd_card_name = strdup(snd_card_name);
199 my_data->acdb_init_data.meta_key_list = &my_data->acdb_meta_key_list;
200 my_data->acdb_init_data.is_instance_id_supported = &acdb_instance_id_support;
201
202 if (my_data->acdb_init_v4) {
203 result = my_data->acdb_init_v4(&my_data->acdb_init_data, ACDB_LOADER_INIT_V4);
204 } else if (my_data->acdb_init_v3) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700205 result = my_data->acdb_init_v3(snd_card_name, cvd_version,
206 &my_data->acdb_meta_key_list);
207 } else if (my_data->acdb_init_v2) {
208 node = list_head(&my_data->acdb_meta_key_list);
209 key_info = node_to_item(node, struct meta_key_list, list);
210 key = key_info->cal_info.nKey;
211 result = my_data->acdb_init_v2(snd_card_name, cvd_version, key);
212 } else {
213 result = my_data->acdb_init();
214 }
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530215 ALOGD("%s: ACDB Instance ID support after ACDB init:%d\n",
216 __func__, acdb_instance_id_support);
217 check_and_set_instance_id_support(mixer, acdb_instance_id_support);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700218
219cleanup:
220 if (NULL != my_data) {
221 if (my_data->acdb_handle)
222 dlclose(my_data->acdb_handle);
223
Dhanalakshmi Siddani96eb5762017-12-11 11:56:46 +0530224 struct listnode *node = NULL;
225 struct meta_key_list *key_info = NULL;
226 struct listnode *tempnode = NULL;
227 list_for_each_safe(node, tempnode, &my_data->acdb_meta_key_list) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700228 key_info = node_to_item(node, struct meta_key_list, list);
Dhanalakshmi Siddani96eb5762017-12-11 11:56:46 +0530229 list_remove(node);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700230 free(key_info);
231 }
232 free(my_data);
233 }
234
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700235 if (cvd_version)
236 free(cvd_version);
237
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700238 return result;
239}
240
241int acdb_set_metainfo_key(void *platform, char *name, int key) {
242
243 struct meta_key_list *key_info = (struct meta_key_list *)
244 calloc(1, sizeof(struct meta_key_list));
245 struct acdb_platform_data *pdata = (struct acdb_platform_data *)platform;
246 if (!key_info) {
247 ALOGE("%s: Could not allocate memory for key %d", __func__, key);
248 return -ENOMEM;
249 }
250
251 key_info->cal_info.nKey = key;
252 strlcpy(key_info->name, name, sizeof(key_info->name));
253 list_add_tail(&pdata->acdb_meta_key_list, &key_info->list);
254
255 ALOGD("%s: successfully added module %s and key %d to the list", __func__,
256 key_info->name, key_info->cal_info.nKey);
257
258 return 0;
259}