Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 1 | /* |
Saurav Kumar | dba3caf | 2020-05-29 20:53:55 +0530 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved. |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 3 | * 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 Jiang | 2995f66 | 2019-04-17 14:25:12 +0800 | [diff] [blame] | 26 | #include <log/log.h> |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 27 | #include <cutils/list.h> |
Vinay Verma | addfa4a | 2018-04-29 14:03:38 +0530 | [diff] [blame] | 28 | #include <time.h> |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 29 | #include "acdb.h" |
| 30 | #include "platform_api.h" |
Arun Mirpuri | a13495c | 2019-04-11 16:08:12 -0700 | [diff] [blame] | 31 | #include "audio_extn.h" |
| 32 | #include <platform.h> |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 33 | |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 34 | #ifdef INSTANCE_ID_ENABLED |
| 35 | int 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 Mirpuri | a13495c | 2019-04-11 16:08:12 -0700 | [diff] [blame] | 62 | void 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 Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 78 | int acdb_init(int snd_card_num) |
| 79 | { |
| 80 | |
| 81 | int result = -1; |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 82 | struct mixer *mixer = NULL; |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 83 | |
| 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 Managoli | 9fee7c6 | 2018-04-06 16:21:50 +0530 | [diff] [blame] | 93 | return result; |
| 94 | } |
| 95 | result = acdb_init_v2(mixer); |
| 96 | mixer_close(mixer); |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | int acdb_init_v2(struct mixer *mixer) |
| 101 | { |
| 102 | |
| 103 | int result = -1; |
| 104 | char *cvd_version = NULL; |
Saurav Kumar | dba3caf | 2020-05-29 20:53:55 +0530 | [diff] [blame] | 105 | char vendor_config_path[VENDOR_CONFIG_PATH_MAX_LENGTH]; |
| 106 | char platform_info_file[VENDOR_CONFIG_FILE_MAX_LENGTH]; |
Soumya Managoli | 9fee7c6 | 2018-04-06 16:21:50 +0530 | [diff] [blame] | 107 | const char *snd_card_name = NULL; |
| 108 | struct acdb_platform_data *my_data = NULL; |
| 109 | |
| 110 | if (!mixer) { |
| 111 | ALOGE("Invalid mixer handle"); |
| 112 | return result; |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | my_data = calloc(1, sizeof(struct acdb_platform_data)); |
| 116 | if (!my_data) { |
| 117 | ALOGE("failed to allocate acdb platform data"); |
| 118 | goto cleanup; |
| 119 | } |
| 120 | |
| 121 | list_init(&my_data->acdb_meta_key_list); |
Saurav Kumar | dba3caf | 2020-05-29 20:53:55 +0530 | [diff] [blame] | 122 | audio_get_vendor_config_path(vendor_config_path, sizeof(vendor_config_path)); |
| 123 | /* Get path for platorm_info_xml_path_name in vendor */ |
| 124 | snprintf(platform_info_file, sizeof(platform_info_file), |
| 125 | "%s/%s", vendor_config_path, PLATFORM_INFO_XML_PATH_NAME); |
Arun Mirpuri | a13495c | 2019-04-11 16:08:12 -0700 | [diff] [blame] | 126 | get_platform_file_for_device(mixer, platform_info_file); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 127 | /* Extract META KEY LIST INFO */ |
Arun Mirpuri | a13495c | 2019-04-11 16:08:12 -0700 | [diff] [blame] | 128 | platform_info_init(platform_info_file, my_data, ACDB_EXTN); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 129 | |
| 130 | my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW); |
| 131 | if (my_data->acdb_handle == NULL) { |
| 132 | ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER); |
| 133 | goto cleanup; |
| 134 | } |
| 135 | |
| 136 | ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER); |
| 137 | |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 138 | my_data->acdb_init_v4 = (acdb_init_v4_t)dlsym(my_data->acdb_handle, |
| 139 | "acdb_loader_init_v4"); |
| 140 | if (my_data->acdb_init_v4 == NULL) |
| 141 | ALOGE("%s: dlsym error %s for acdb_loader_init_v4", __func__, dlerror()); |
| 142 | |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 143 | my_data->acdb_init_v3 = (acdb_init_v3_t)dlsym(my_data->acdb_handle, |
| 144 | "acdb_loader_init_v3"); |
| 145 | if (my_data->acdb_init_v3 == NULL) |
| 146 | ALOGE("%s: dlsym error %s for acdb_loader_init_v3", __func__, dlerror()); |
| 147 | |
| 148 | my_data->acdb_init_v2 = (acdb_init_v2_t)dlsym(my_data->acdb_handle, |
| 149 | "acdb_loader_init_v2"); |
| 150 | if (my_data->acdb_init_v2 == NULL) |
| 151 | ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror()); |
| 152 | |
| 153 | my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle, |
| 154 | "acdb_loader_init_ACDB"); |
| 155 | if (my_data->acdb_init == NULL && my_data->acdb_init_v2 == NULL |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 156 | && my_data->acdb_init_v3 == NULL && my_data->acdb_init_v4 == NULL) { |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 157 | ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror()); |
| 158 | goto cleanup; |
| 159 | } |
| 160 | |
| 161 | /* Get CVD version */ |
| 162 | cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE); |
| 163 | if (!cvd_version) { |
| 164 | ALOGE("%s: Failed to allocate cvd version", __func__); |
| 165 | goto cleanup; |
| 166 | } else { |
| 167 | struct mixer_ctl *ctl = NULL; |
| 168 | int count = 0; |
| 169 | |
| 170 | ctl = mixer_get_ctl_by_name(mixer, CVD_VERSION_MIXER_CTL); |
| 171 | if (!ctl) { |
| 172 | ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, CVD_VERSION_MIXER_CTL); |
| 173 | goto cleanup; |
| 174 | } |
| 175 | mixer_ctl_update(ctl); |
| 176 | |
| 177 | count = mixer_ctl_get_num_values(ctl); |
| 178 | if (count > MAX_CVD_VERSION_STRING_SIZE) |
| 179 | count = MAX_CVD_VERSION_STRING_SIZE; |
| 180 | |
| 181 | result = mixer_ctl_get_array(ctl, cvd_version, count); |
| 182 | if (result != 0) { |
| 183 | ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__); |
| 184 | goto cleanup; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* Get Sound card name */ |
Aditya Bavanari | 71b6d53 | 2018-01-16 17:48:08 +0530 | [diff] [blame] | 189 | snd_card_name = mixer_get_name(mixer); |
Weiyin Jiang | 301dac6 | 2019-05-08 13:58:00 +0800 | [diff] [blame] | 190 | snd_card_name = platform_get_snd_card_name_for_acdb_loader(snd_card_name); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 191 | if (!snd_card_name) { |
Weiyin Jiang | 301dac6 | 2019-05-08 13:58:00 +0800 | [diff] [blame] | 192 | ALOGE("failed to get snd_card_name"); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 193 | result = -1; |
| 194 | goto cleanup; |
| 195 | } |
| 196 | |
| 197 | int key = 0; |
| 198 | struct listnode *node = NULL; |
| 199 | struct meta_key_list *key_info = NULL; |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 200 | static bool acdb_instance_id_support = false; |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 201 | |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 202 | my_data->acdb_init_data.cvd_version = cvd_version; |
| 203 | my_data->acdb_init_data.snd_card_name = strdup(snd_card_name); |
| 204 | my_data->acdb_init_data.meta_key_list = &my_data->acdb_meta_key_list; |
| 205 | my_data->acdb_init_data.is_instance_id_supported = &acdb_instance_id_support; |
| 206 | |
| 207 | if (my_data->acdb_init_v4) { |
| 208 | result = my_data->acdb_init_v4(&my_data->acdb_init_data, ACDB_LOADER_INIT_V4); |
| 209 | } else if (my_data->acdb_init_v3) { |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 210 | result = my_data->acdb_init_v3(snd_card_name, cvd_version, |
| 211 | &my_data->acdb_meta_key_list); |
| 212 | } else if (my_data->acdb_init_v2) { |
| 213 | node = list_head(&my_data->acdb_meta_key_list); |
| 214 | key_info = node_to_item(node, struct meta_key_list, list); |
| 215 | key = key_info->cal_info.nKey; |
| 216 | result = my_data->acdb_init_v2(snd_card_name, cvd_version, key); |
| 217 | } else { |
| 218 | result = my_data->acdb_init(); |
| 219 | } |
Aditya Bavanari | 29bcea2 | 2017-10-03 20:10:35 +0530 | [diff] [blame] | 220 | ALOGD("%s: ACDB Instance ID support after ACDB init:%d\n", |
| 221 | __func__, acdb_instance_id_support); |
| 222 | check_and_set_instance_id_support(mixer, acdb_instance_id_support); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 223 | |
| 224 | cleanup: |
| 225 | if (NULL != my_data) { |
| 226 | if (my_data->acdb_handle) |
| 227 | dlclose(my_data->acdb_handle); |
| 228 | |
Dhanalakshmi Siddani | 96eb576 | 2017-12-11 11:56:46 +0530 | [diff] [blame] | 229 | struct listnode *node = NULL; |
| 230 | struct meta_key_list *key_info = NULL; |
| 231 | struct listnode *tempnode = NULL; |
| 232 | list_for_each_safe(node, tempnode, &my_data->acdb_meta_key_list) { |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 233 | key_info = node_to_item(node, struct meta_key_list, list); |
Dhanalakshmi Siddani | 96eb576 | 2017-12-11 11:56:46 +0530 | [diff] [blame] | 234 | list_remove(node); |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 235 | free(key_info); |
| 236 | } |
| 237 | free(my_data); |
| 238 | } |
| 239 | |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 240 | if (cvd_version) |
| 241 | free(cvd_version); |
| 242 | |
Vignesh Kulothungan | 5539688 | 2017-04-20 14:37:02 -0700 | [diff] [blame] | 243 | return result; |
| 244 | } |
| 245 | |
| 246 | int acdb_set_metainfo_key(void *platform, char *name, int key) { |
| 247 | |
| 248 | struct meta_key_list *key_info = (struct meta_key_list *) |
| 249 | calloc(1, sizeof(struct meta_key_list)); |
| 250 | struct acdb_platform_data *pdata = (struct acdb_platform_data *)platform; |
| 251 | if (!key_info) { |
| 252 | ALOGE("%s: Could not allocate memory for key %d", __func__, key); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | |
| 256 | key_info->cal_info.nKey = key; |
| 257 | strlcpy(key_info->name, name, sizeof(key_info->name)); |
| 258 | list_add_tail(&pdata->acdb_meta_key_list, &key_info->list); |
| 259 | |
| 260 | ALOGD("%s: successfully added module %s and key %d to the list", __func__, |
| 261 | key_info->name, key_info->cal_info.nKey); |
| 262 | |
| 263 | return 0; |
| 264 | } |