blob: 7394906b742d1a5b4a2fd5c65cab50dc58776c60 [file] [log] [blame]
Vignesh Kulothungan55396882017-04-20 14:37:02 -07001/*
Aditya Bavanari29bcea22017-10-03 20:10:35 +05302 * Copyright (c) 2013-2018, 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"
31
Aditya Bavanari29bcea22017-10-03 20:10:35 +053032#ifdef INSTANCE_ID_ENABLED
33int check_and_set_instance_id_support(struct mixer* mixer, bool acdb_support)
34{
35 const char *mixer_ctl_name = "Instance ID Support";
36 struct mixer_ctl* ctl;
37
38 ALOGV("%s", __func__);
39
40 /* Check for ACDB and property instance ID support and issue mixer control */
41 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
42 if (!ctl) {
43 ALOGE("%s: Could not get ctl for mixer cmd - %s",
44 __func__, mixer_ctl_name);
45 return -EINVAL;
46 }
47
48 ALOGD("%s: Final Instance ID support:%d\n", __func__, acdb_support);
49 if (mixer_ctl_set_value(ctl, 0, acdb_support) < 0) {
50 ALOGE("%s: Could not set Instance ID support %d", __func__,
51 acdb_support);
52 return -EINVAL;
53 }
54 return 0;
55}
56#else
57#define check_and_set_instance_id_support(x, y) -ENOSYS
58#endif
59
Vignesh Kulothungan55396882017-04-20 14:37:02 -070060int acdb_init(int snd_card_num)
61{
62
63 int result = -1;
Vignesh Kulothungan55396882017-04-20 14:37:02 -070064 struct mixer *mixer = NULL;
Vignesh Kulothungan55396882017-04-20 14:37:02 -070065
66 if(snd_card_num < 0) {
67 ALOGE("invalid sound card number");
68 return result;
69 }
70
71 mixer = mixer_open(snd_card_num);
72 if (!mixer) {
73 ALOGE("%s: Unable to open the mixer card: %d", __func__,
74 snd_card_num);
Soumya Managoli9fee7c62018-04-06 16:21:50 +053075 return result;
76 }
77 result = acdb_init_v2(mixer);
78 mixer_close(mixer);
79 return result;
80}
81
82int acdb_init_v2(struct mixer *mixer)
83{
84
85 int result = -1;
86 char *cvd_version = NULL;
87
88 const char *snd_card_name = NULL;
89 struct acdb_platform_data *my_data = NULL;
90
91 if (!mixer) {
92 ALOGE("Invalid mixer handle");
93 return result;
Vignesh Kulothungan55396882017-04-20 14:37:02 -070094 }
95
96 my_data = calloc(1, sizeof(struct acdb_platform_data));
97 if (!my_data) {
98 ALOGE("failed to allocate acdb platform data");
99 goto cleanup;
100 }
101
102 list_init(&my_data->acdb_meta_key_list);
103
104 /* Extract META KEY LIST INFO */
105 platform_info_init(PLATFORM_INFO_XML_PATH, my_data, ACDB_EXTN);
106
107 my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
108 if (my_data->acdb_handle == NULL) {
109 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
110 goto cleanup;
111 }
112
113 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
114
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530115 my_data->acdb_init_v4 = (acdb_init_v4_t)dlsym(my_data->acdb_handle,
116 "acdb_loader_init_v4");
117 if (my_data->acdb_init_v4 == NULL)
118 ALOGE("%s: dlsym error %s for acdb_loader_init_v4", __func__, dlerror());
119
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700120 my_data->acdb_init_v3 = (acdb_init_v3_t)dlsym(my_data->acdb_handle,
121 "acdb_loader_init_v3");
122 if (my_data->acdb_init_v3 == NULL)
123 ALOGE("%s: dlsym error %s for acdb_loader_init_v3", __func__, dlerror());
124
125 my_data->acdb_init_v2 = (acdb_init_v2_t)dlsym(my_data->acdb_handle,
126 "acdb_loader_init_v2");
127 if (my_data->acdb_init_v2 == NULL)
128 ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror());
129
130 my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle,
131 "acdb_loader_init_ACDB");
132 if (my_data->acdb_init == NULL && my_data->acdb_init_v2 == NULL
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530133 && my_data->acdb_init_v3 == NULL && my_data->acdb_init_v4 == NULL) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700134 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
135 goto cleanup;
136 }
137
138 /* Get CVD version */
139 cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE);
140 if (!cvd_version) {
141 ALOGE("%s: Failed to allocate cvd version", __func__);
142 goto cleanup;
143 } else {
144 struct mixer_ctl *ctl = NULL;
145 int count = 0;
146
147 ctl = mixer_get_ctl_by_name(mixer, CVD_VERSION_MIXER_CTL);
148 if (!ctl) {
149 ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, CVD_VERSION_MIXER_CTL);
150 goto cleanup;
151 }
152 mixer_ctl_update(ctl);
153
154 count = mixer_ctl_get_num_values(ctl);
155 if (count > MAX_CVD_VERSION_STRING_SIZE)
156 count = MAX_CVD_VERSION_STRING_SIZE;
157
158 result = mixer_ctl_get_array(ctl, cvd_version, count);
159 if (result != 0) {
160 ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__);
161 goto cleanup;
162 }
163 }
164
165 /* Get Sound card name */
Aditya Bavanari71b6d532018-01-16 17:48:08 +0530166 snd_card_name = mixer_get_name(mixer);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700167 if (!snd_card_name) {
168 ALOGE("failed to allocate memory for snd_card_name");
169 result = -1;
170 goto cleanup;
171 }
172
Aditya Bavanari71b6d532018-01-16 17:48:08 +0530173 snd_card_name = platform_get_snd_card_name_for_acdb_loader(snd_card_name);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700174 int key = 0;
175 struct listnode *node = NULL;
176 struct meta_key_list *key_info = NULL;
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530177 static bool acdb_instance_id_support = false;
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700178
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530179 my_data->acdb_init_data.cvd_version = cvd_version;
180 my_data->acdb_init_data.snd_card_name = strdup(snd_card_name);
181 my_data->acdb_init_data.meta_key_list = &my_data->acdb_meta_key_list;
182 my_data->acdb_init_data.is_instance_id_supported = &acdb_instance_id_support;
183
184 if (my_data->acdb_init_v4) {
185 result = my_data->acdb_init_v4(&my_data->acdb_init_data, ACDB_LOADER_INIT_V4);
186 } else if (my_data->acdb_init_v3) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700187 result = my_data->acdb_init_v3(snd_card_name, cvd_version,
188 &my_data->acdb_meta_key_list);
189 } else if (my_data->acdb_init_v2) {
190 node = list_head(&my_data->acdb_meta_key_list);
191 key_info = node_to_item(node, struct meta_key_list, list);
192 key = key_info->cal_info.nKey;
193 result = my_data->acdb_init_v2(snd_card_name, cvd_version, key);
194 } else {
195 result = my_data->acdb_init();
196 }
Aditya Bavanari29bcea22017-10-03 20:10:35 +0530197 ALOGD("%s: ACDB Instance ID support after ACDB init:%d\n",
198 __func__, acdb_instance_id_support);
199 check_and_set_instance_id_support(mixer, acdb_instance_id_support);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700200
201cleanup:
202 if (NULL != my_data) {
203 if (my_data->acdb_handle)
204 dlclose(my_data->acdb_handle);
205
Dhanalakshmi Siddani96eb5762017-12-11 11:56:46 +0530206 struct listnode *node = NULL;
207 struct meta_key_list *key_info = NULL;
208 struct listnode *tempnode = NULL;
209 list_for_each_safe(node, tempnode, &my_data->acdb_meta_key_list) {
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700210 key_info = node_to_item(node, struct meta_key_list, list);
Dhanalakshmi Siddani96eb5762017-12-11 11:56:46 +0530211 list_remove(node);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700212 free(key_info);
213 }
214 free(my_data);
215 }
216
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700217 if (cvd_version)
218 free(cvd_version);
219
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700220 return result;
221}
222
223int acdb_set_metainfo_key(void *platform, char *name, int key) {
224
225 struct meta_key_list *key_info = (struct meta_key_list *)
226 calloc(1, sizeof(struct meta_key_list));
227 struct acdb_platform_data *pdata = (struct acdb_platform_data *)platform;
228 if (!key_info) {
229 ALOGE("%s: Could not allocate memory for key %d", __func__, key);
230 return -ENOMEM;
231 }
232
233 key_info->cal_info.nKey = key;
234 strlcpy(key_info->name, name, sizeof(key_info->name));
235 list_add_tail(&pdata->acdb_meta_key_list, &key_info->list);
236
237 ALOGD("%s: successfully added module %s and key %d to the list", __func__,
238 key_info->name, key_info->cal_info.nKey);
239
240 return 0;
241}