vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
| 17 | #define LOG_TAG "audio_hw_acdb" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_NDDEBUG 0 |
| 20 | |
Dan Albert | e4bd825 | 2017-10-16 13:33:44 -0700 | [diff] [blame] | 21 | #include <errno.h> |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <dlfcn.h> |
Haynes Mathew George | e6e2d44 | 2018-02-22 18:51:56 -0800 | [diff] [blame] | 25 | #include <log/log.h> |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 26 | #include <cutils/str_parms.h> |
| 27 | #include <system/audio.h> |
| 28 | #include <tinyalsa/asoundlib.h> |
| 29 | #include "acdb.h" |
| 30 | #include <platform_api.h> |
| 31 | |
| 32 | #define PLATFORM_CONFIG_KEY_SOUNDCARD_NAME "snd_card_name" |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 33 | |
| 34 | int acdb_init(int snd_card_num) |
| 35 | { |
| 36 | |
| 37 | int result = -1; |
| 38 | char *cvd_version = NULL; |
| 39 | |
| 40 | char *snd_card_name = NULL; |
| 41 | struct mixer *mixer = NULL; |
| 42 | struct acdb_platform_data *my_data = NULL; |
| 43 | |
| 44 | if(snd_card_num < 0) { |
| 45 | ALOGE("invalid sound card number"); |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | mixer = mixer_open(snd_card_num); |
| 50 | if (!mixer) { |
| 51 | ALOGE("%s: Unable to open the mixer card: %d", __func__, |
| 52 | snd_card_num); |
| 53 | goto cleanup; |
| 54 | } |
| 55 | |
| 56 | my_data = calloc(1, sizeof(struct acdb_platform_data)); |
| 57 | if (!my_data) { |
| 58 | ALOGE("failed to allocate acdb platform data"); |
| 59 | goto cleanup; |
| 60 | } |
| 61 | |
| 62 | list_init(&my_data->acdb_meta_key_list); |
| 63 | |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 64 | my_data->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW); |
| 65 | if (my_data->acdb_handle == NULL) { |
| 66 | ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER); |
| 67 | goto cleanup; |
| 68 | } |
| 69 | |
| 70 | ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER); |
| 71 | |
| 72 | my_data->acdb_init_v3 = (acdb_init_v3_t)dlsym(my_data->acdb_handle, |
| 73 | "acdb_loader_init_v3"); |
| 74 | if (my_data->acdb_init_v3 == NULL) |
| 75 | ALOGE("%s: dlsym error %s for acdb_loader_init_v3", __func__, dlerror()); |
| 76 | |
| 77 | my_data->acdb_init_v2 = (acdb_init_v2_cvd_t)dlsym(my_data->acdb_handle, |
| 78 | "acdb_loader_init_v2"); |
| 79 | if (my_data->acdb_init_v2 == NULL) |
| 80 | ALOGE("%s: dlsym error %s for acdb_loader_init_v2", __func__, dlerror()); |
| 81 | |
| 82 | my_data->acdb_init = (acdb_init_t)dlsym(my_data->acdb_handle, |
| 83 | "acdb_loader_init_ACDB"); |
| 84 | if (my_data->acdb_init == NULL && my_data->acdb_init_v2 == NULL |
| 85 | && my_data->acdb_init_v3 == NULL) { |
| 86 | ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror()); |
| 87 | goto cleanup; |
| 88 | } |
| 89 | |
| 90 | /* Get CVD version */ |
| 91 | cvd_version = calloc(1, MAX_CVD_VERSION_STRING_SIZE); |
| 92 | if (!cvd_version) { |
| 93 | ALOGE("%s: Failed to allocate cvd version", __func__); |
| 94 | goto cleanup; |
| 95 | } else { |
| 96 | struct mixer_ctl *ctl = NULL; |
| 97 | int count = 0; |
| 98 | |
| 99 | ctl = mixer_get_ctl_by_name(mixer, CVD_VERSION_MIXER_CTL); |
| 100 | if (!ctl) { |
| 101 | ALOGE("%s: Could not get ctl for mixer cmd - %s", __func__, CVD_VERSION_MIXER_CTL); |
D. Andrei Măceș | bd5cde1 | 2018-01-16 21:26:06 +0200 | [diff] [blame^] | 102 | /* CVD is required for ACDB v2/3, so fail fully there. |
| 103 | * For ACDB v1, the CVD version code can be skipped. |
| 104 | */ |
| 105 | if (my_data->acdb_init_v2 || my_data->acdb_init_v3) |
| 106 | goto cleanup; |
| 107 | else |
| 108 | goto card_name; |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 109 | } |
| 110 | mixer_ctl_update(ctl); |
| 111 | |
| 112 | count = mixer_ctl_get_num_values(ctl); |
| 113 | if (count > MAX_CVD_VERSION_STRING_SIZE) |
| 114 | count = MAX_CVD_VERSION_STRING_SIZE; |
| 115 | |
| 116 | result = mixer_ctl_get_array(ctl, cvd_version, count); |
| 117 | if (result != 0) { |
| 118 | ALOGE("%s: ERROR! mixer_ctl_get_array() failed to get CVD Version", __func__); |
| 119 | goto cleanup; |
| 120 | } |
| 121 | } |
| 122 | |
D. Andrei Măceș | bd5cde1 | 2018-01-16 21:26:06 +0200 | [diff] [blame^] | 123 | card_name: |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 124 | /* Get Sound card name */ |
| 125 | snd_card_name = strdup(mixer_get_name(mixer)); |
| 126 | if (!snd_card_name) { |
| 127 | ALOGE("failed to allocate memory for snd_card_name"); |
| 128 | result = -1; |
| 129 | goto cleanup; |
| 130 | } |
| 131 | |
vivek mehta | 1ffb5b2 | 2017-05-25 14:39:26 -0700 | [diff] [blame] | 132 | if (my_data->acdb_init_v3) |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 133 | result = my_data->acdb_init_v3(snd_card_name, cvd_version, |
| 134 | &my_data->acdb_meta_key_list); |
vivek mehta | 1ffb5b2 | 2017-05-25 14:39:26 -0700 | [diff] [blame] | 135 | else if (my_data->acdb_init_v2) |
| 136 | result = my_data->acdb_init_v2(snd_card_name, cvd_version, 0); |
| 137 | else |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 138 | result = my_data->acdb_init(); |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 139 | |
| 140 | cleanup: |
| 141 | if (NULL != my_data) { |
| 142 | if (my_data->acdb_handle) |
| 143 | dlclose(my_data->acdb_handle); |
| 144 | |
| 145 | struct listnode *node; |
| 146 | struct meta_key_list *key_info; |
| 147 | list_for_each(node, &my_data->acdb_meta_key_list) { |
| 148 | key_info = node_to_item(node, struct meta_key_list, list); |
| 149 | free(key_info); |
| 150 | } |
| 151 | free(my_data); |
| 152 | } |
| 153 | |
| 154 | mixer_close(mixer); |
| 155 | free(cvd_version); |
| 156 | free(snd_card_name); |
| 157 | |
| 158 | return result; |
| 159 | } |
| 160 | |
vivek mehta | 0fb1131 | 2017-05-15 19:35:32 -0700 | [diff] [blame] | 161 | int acdb_set_parameters(void *platform, struct str_parms *parms) |
| 162 | { |
| 163 | struct acdb_platform_data *my_data = (struct acdb_platform_data *)platform; |
| 164 | char value[128]; |
| 165 | char *kv_pairs = str_parms_to_str(parms); |
| 166 | int ret = 0; |
| 167 | |
| 168 | if (kv_pairs == NULL) { |
| 169 | ret = -EINVAL; |
| 170 | ALOGE("%s: key-value pair is NULL",__func__); |
| 171 | goto done; |
| 172 | } |
| 173 | |
| 174 | ret = str_parms_get_str(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME, |
| 175 | value, sizeof(value)); |
| 176 | if (ret >= 0) { |
| 177 | str_parms_del(parms, PLATFORM_CONFIG_KEY_SOUNDCARD_NAME); |
| 178 | my_data->snd_card_name = strdup(value); |
| 179 | ALOGV("%s: sound card name %s", __func__, my_data->snd_card_name); |
| 180 | } |
| 181 | |
| 182 | done: |
| 183 | free(kv_pairs); |
| 184 | |
| 185 | return ret; |
| 186 | } |