jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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_waves" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 20 | #include <audio_hw.h> |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 21 | #include <cutils/str_parms.h> |
| 22 | #include <dlfcn.h> |
| 23 | #include <log/log.h> |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 24 | #include <math.h> |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 25 | #include <platform_api.h> |
| 26 | #include <pthread.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <system/audio.h> |
| 30 | #include <unistd.h> |
| 31 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 32 | #include "audio_extn.h" |
| 33 | #include "maxxaudio.h" |
| 34 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 35 | #define LIB_MA_PARAM "libmaxxaudioqdsp.so" |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 36 | #define LIB_MA_PATH "vendor/lib/" |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 37 | #define PRESET_PATH "/vendor/etc" |
| 38 | #define MPS_BASE_STRING "default" |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 39 | #define USER_PRESET_PATH "" |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 40 | #define CONFIG_BASE_STRING "maxx_conf" |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 41 | #define CAL_PRESIST_STR "cal_persist" |
| 42 | #define CAL_SAMPLERATE_STR "cal_samplerate" |
| 43 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 44 | #define MA_QDSP_PARAM_INIT "maxxaudio_qdsp_initialize" |
| 45 | #define MA_QDSP_PARAM_DEINIT "maxxaudio_qdsp_uninitialize" |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 46 | #define MA_QDSP_IS_FEATURE_USED "maxxaudio_qdsp_is_feature_supported" |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 47 | #define MA_QDSP_SET_LR_SWAP "maxxaudio_qdsp_set_lr_swap" |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 48 | #define MA_QDSP_SET_ORIENTATION "maxxaudio_qdsp_set_orientation" |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 49 | #define MA_QDSP_SET_MODE "maxxaudio_qdsp_set_sound_mode" |
| 50 | #define MA_QDSP_SET_VOL "maxxaudio_qdsp_set_volume" |
| 51 | #define MA_QDSP_SET_VOLT "maxxaudio_qdsp_set_volume_table" |
| 52 | #define MA_QDSP_SET_PARAM "maxxaudio_qdsp_set_parameter" |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 53 | |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 54 | #define SUPPORT_DEV "18d1:5033" // Blackbird usbid |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 55 | #define SUPPORTED_USB 0x01 |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 56 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 57 | typedef unsigned int effective_scope_flag_t; |
| 58 | const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0; /* RTC */ |
| 59 | const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1; /* ACDB */ |
| 60 | const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB; |
| 61 | const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0; |
| 62 | const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 63 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 64 | const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2; |
| 65 | const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0; |
| 66 | const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR; |
| 67 | const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR; |
| 68 | |
| 69 | const unsigned int VALUE_AUTO = 0xFFFFFFFF; |
| 70 | const unsigned int APP_TYPE_AUTO = VALUE_AUTO; |
| 71 | const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO; |
| 72 | const unsigned int DEVICE_AUTO = VALUE_AUTO; |
| 73 | const unsigned int DEVICE_DEFAULT = DEVICE_AUTO; |
| 74 | |
| 75 | const unsigned int MAAP_OUTPUT_GAIN = 27; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 76 | |
| 77 | typedef enum MA_STREAM_TYPE { |
Jasmine Cha | fc63f34 | 2018-06-27 10:55:06 -0700 | [diff] [blame] | 78 | STREAM_MIN_TYPES = 0, |
| 79 | STREAM_VOICE = STREAM_MIN_TYPES, |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 80 | STREAM_SYSTEM, |
| 81 | STREAM_RING, |
| 82 | STREAM_MUSIC, |
| 83 | STREAM_ALARM, |
| 84 | STREAM_NOTIFICATION , |
| 85 | STREAM_MAX_TYPES, |
| 86 | } ma_stream_type_t; |
| 87 | |
| 88 | typedef enum MA_CMD { |
| 89 | MA_CMD_VOL, |
| 90 | MA_CMD_SWAP_ENABLE, |
| 91 | MA_CMD_SWAP_DISABLE, |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 92 | MA_CMD_ROTATE_ENABLE, |
| 93 | MA_CMD_ROTATE_DISABLE, |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 94 | } ma_cmd_t; |
| 95 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 96 | typedef struct ma_audio_cal_version { |
| 97 | unsigned int major; |
| 98 | unsigned int minor; |
| 99 | } ma_audio_cal_version_t; |
| 100 | |
| 101 | typedef struct ma_audio_cal_common_settings { |
| 102 | unsigned int app_type; |
| 103 | unsigned int device; |
| 104 | } ma_audio_cal_common_settings_t; |
| 105 | |
| 106 | struct ma_audio_cal_settings { |
| 107 | ma_audio_cal_version_t version; |
| 108 | ma_audio_cal_common_settings_t common; |
| 109 | effective_scope_flag_t effect_scope_flag; |
| 110 | }; |
| 111 | |
| 112 | struct ma_state { |
| 113 | float vol; |
| 114 | bool active; |
| 115 | }; |
| 116 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 117 | typedef void *ma_audio_cal_handle_t; |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 118 | typedef int (*set_audio_cal_t)(const char *); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 119 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 120 | typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *, |
| 121 | const char *, const char *, set_audio_cal_t); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 122 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 123 | typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 124 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 125 | typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *); |
| 126 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 127 | typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t, |
| 128 | const struct ma_audio_cal_settings *, bool); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 129 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 130 | typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t, |
| 131 | const struct ma_audio_cal_settings *, int); |
| 132 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 133 | typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t, |
| 134 | const struct ma_audio_cal_settings *, |
| 135 | unsigned int); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 136 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 137 | typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t, |
| 138 | const struct ma_audio_cal_settings *, double); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 139 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 140 | typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t, |
| 141 | const struct ma_audio_cal_settings *, |
| 142 | size_t, struct ma_state *); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 143 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 144 | typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t, |
| 145 | const struct ma_audio_cal_settings *, |
| 146 | unsigned int, double); |
| 147 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 148 | struct ma_platform_data { |
| 149 | void *waves_handle; |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 150 | void *platform; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 151 | pthread_mutex_t lock; |
| 152 | ma_param_init_t ma_param_init; |
| 153 | ma_param_deinit_t ma_param_deinit; |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 154 | ma_is_feature_used_t ma_is_feature_used; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 155 | ma_set_lr_swap_t ma_set_lr_swap; |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 156 | ma_set_orientation_t ma_set_orientation; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 157 | ma_set_sound_mode_t ma_set_sound_mode; |
| 158 | ma_set_volume_t ma_set_volume; |
| 159 | ma_set_volume_table_t ma_set_volume_table; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 160 | ma_set_param_t ma_set_param; |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 161 | bool speaker_lr_swap; |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 162 | bool orientation_used; |
| 163 | int dispaly_orientation; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL; |
| 167 | static uint16_t g_supported_dev = 0; |
| 168 | static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES]; |
| 169 | static struct ma_platform_data *my_data = NULL; |
| 170 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 171 | static int set_audio_cal(const char *audio_cal) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 172 | { |
| 173 | ALOGV("set_audio_cal: %s", audio_cal); |
| 174 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 175 | return platform_set_parameters(my_data->platform, |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 176 | str_parms_create_str(audio_cal)); |
| 177 | } |
| 178 | |
| 179 | static bool ma_set_lr_swap_l( |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 180 | const struct ma_audio_cal_settings *audio_cal_settings, bool swap) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 181 | { |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 182 | return my_data->ma_set_lr_swap(g_ma_audio_cal_handle, |
| 183 | audio_cal_settings, swap); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 184 | } |
| 185 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 186 | static bool ma_set_orientation_l( |
| 187 | const struct ma_audio_cal_settings *audio_cal_settings, int orientation) |
| 188 | { |
| 189 | return my_data->ma_set_orientation(g_ma_audio_cal_handle, |
| 190 | audio_cal_settings, orientation); |
| 191 | } |
| 192 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 193 | static bool ma_set_sound_mode_l( |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 194 | const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 195 | { |
| 196 | return my_data->ma_set_sound_mode(g_ma_audio_cal_handle, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 197 | audio_cal_settings, sound_mode); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static bool ma_set_volume_l( |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 201 | const struct ma_audio_cal_settings *audio_cal_settings, double volume) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 202 | { |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 203 | return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings, |
| 204 | volume); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static bool ma_set_volume_table_l( |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 208 | const struct ma_audio_cal_settings *audio_cal_settings, |
| 209 | size_t num_streams, struct ma_state *volume_table) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 210 | { |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 211 | return my_data->ma_set_volume_table(g_ma_audio_cal_handle, |
| 212 | audio_cal_settings, num_streams, |
| 213 | volume_table); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 216 | static bool ma_set_param_l( |
| 217 | const struct ma_audio_cal_settings *audio_cal_settings, |
| 218 | unsigned int index, double value) |
| 219 | { |
| 220 | return my_data->ma_set_param(g_ma_audio_cal_handle, |
| 221 | audio_cal_settings, index, value); |
| 222 | } |
| 223 | |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 224 | static void print_state_log() |
| 225 | { |
| 226 | ALOGD("%s: send volume table -(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),(%i,%f,%s)," |
| 227 | "(%i,%f,%s),(%i,%f,%s)", __func__, |
| 228 | STREAM_VOICE, ma_cur_state_table[STREAM_VOICE].vol, |
| 229 | ma_cur_state_table[STREAM_VOICE].active ? "T" : "F", |
| 230 | STREAM_SYSTEM, ma_cur_state_table[STREAM_SYSTEM].vol, |
| 231 | ma_cur_state_table[STREAM_SYSTEM].active ? "T" : "F", |
| 232 | STREAM_RING, ma_cur_state_table[STREAM_RING].vol, |
| 233 | ma_cur_state_table[STREAM_RING].active ? "T" : "F", |
| 234 | STREAM_MUSIC, ma_cur_state_table[STREAM_MUSIC].vol, |
| 235 | ma_cur_state_table[STREAM_MUSIC].active ? "T" : "F", |
| 236 | STREAM_ALARM, ma_cur_state_table[STREAM_ALARM].vol, |
| 237 | ma_cur_state_table[STREAM_ALARM].active ? "T" : "F", |
| 238 | STREAM_NOTIFICATION, ma_cur_state_table[STREAM_NOTIFICATION].vol, |
| 239 | ma_cur_state_table[STREAM_NOTIFICATION].active ? "T" : "F"); |
| 240 | |
| 241 | } |
| 242 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 243 | static inline bool valid_usecase(struct audio_usecase *usecase) |
| 244 | { |
| 245 | if ((usecase->type == PCM_PLAYBACK) && |
| 246 | /* supported usecases */ |
| 247 | ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) || |
| 248 | (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) || |
| 249 | (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) && |
| 250 | /* support devices */ |
| 251 | ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) || |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 252 | (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) || |
| 253 | (audio_is_usb_out_device(usecase->devices) && |
| 254 | audio_extn_ma_supported_usb()))) |
| 255 | /* TODO: enable A2DP when it is ready */ |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 256 | |
| 257 | return true; |
| 258 | |
| 259 | ALOGV("%s: not support type %d usecase %d device %d", |
| 260 | __func__, usecase->type, usecase->id, usecase->devices); |
| 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | // already hold lock |
| 266 | static inline bool is_active() |
| 267 | { |
| 268 | ma_stream_type_t i = 0; |
| 269 | |
| 270 | for (i = 0; i < STREAM_MAX_TYPES; i++) |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 271 | if (ma_cur_state_table[i].active) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 272 | return true; |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 277 | static void ma_cal_init(struct ma_audio_cal_settings *ma_cal) |
| 278 | { |
| 279 | ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT; |
| 280 | ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT; |
| 281 | ma_cal->common.app_type = APP_TYPE_DEFAULT; |
| 282 | ma_cal->common.device = DEVICE_DEFAULT; |
| 283 | ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL; |
| 284 | } |
| 285 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 286 | static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd) |
| 287 | { |
| 288 | int i = 0; |
| 289 | bool ret = false; |
| 290 | float vol = 0; |
| 291 | struct listnode *node; |
| 292 | struct audio_usecase *usecase; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 293 | struct ma_audio_cal_settings ma_cal; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 294 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 295 | ma_cal_init(&ma_cal); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 296 | |
| 297 | list_for_each(node, &adev->usecase_list) { |
| 298 | usecase = node_to_item(node, struct audio_usecase, list); |
| 299 | if (valid_usecase(usecase)) { |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 300 | ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type; |
| 301 | ma_cal.common.device = usecase->stream.out->devices; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 302 | ALOGV("%s: send usecase(%d) app_type(%d) device(%d)", |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 303 | __func__, usecase->id, ma_cal.common.app_type, |
| 304 | ma_cal.common.device); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 305 | |
| 306 | switch (cmd) { |
| 307 | case MA_CMD_VOL: |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 308 | ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 309 | ma_cur_state_table); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 310 | if (ret) |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 311 | ALOGV("ma_set_volume_table_l success"); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 312 | else |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 313 | ALOGE("ma_set_volume_table_l returned with error."); |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 314 | print_state_log(); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 315 | break; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 316 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 317 | case MA_CMD_SWAP_ENABLE: |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 318 | /* lr swap only enable for speaker path */ |
| 319 | if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 320 | ret = ma_set_lr_swap_l(&ma_cal, true); |
| 321 | if (ret) |
| 322 | ALOGV("ma_set_lr_swap_l enable returned with success."); |
| 323 | else |
| 324 | ALOGE("ma_set_lr_swap_l enable returned with error."); |
| 325 | } |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 326 | break; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 327 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 328 | case MA_CMD_SWAP_DISABLE: |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 329 | ret = ma_set_lr_swap_l(&ma_cal, false); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 330 | if (ret) |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 331 | ALOGV("ma_set_lr_swap_l disable returned with success."); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 332 | else |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 333 | ALOGE("ma_set_lr_swap_l disable returned with error."); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 334 | break; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 335 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 336 | case MA_CMD_ROTATE_ENABLE: |
| 337 | if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 338 | ret = ma_set_orientation_l(&ma_cal, my_data->dispaly_orientation); |
| 339 | if (ret) |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 340 | ALOGV("ma_set_orientation_l %d returned with success.", |
| 341 | my_data->dispaly_orientation); |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 342 | else |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 343 | ALOGE("ma_set_orientation_l %d returned with error.", |
| 344 | my_data->dispaly_orientation); |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 345 | } |
| 346 | break; |
| 347 | |
| 348 | case MA_CMD_ROTATE_DISABLE: |
| 349 | ret = ma_set_orientation_l(&ma_cal, 0); |
| 350 | if (ret) |
| 351 | ALOGV("ma_set_orientation_l 0 returned with success."); |
| 352 | else |
| 353 | ALOGE("ma_set_orientation_l 0 returned with error."); |
| 354 | break; |
| 355 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 356 | default: |
| 357 | ALOGE("%s: unsupported cmd %d", __func__, cmd); |
| 358 | } |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 359 | } |
| 360 | } |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static bool find_sup_dev(char *name) |
| 366 | { |
| 367 | char *token; |
| 368 | const char s[2] = ","; |
| 369 | bool ret = false; |
| 370 | char sup_devs[128]; |
| 371 | |
| 372 | // the rule of comforming suppored dev's name |
| 373 | // 1. Both string len are equal |
| 374 | // 2. Both string content are equal |
| 375 | |
| 376 | strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs)); |
| 377 | token = strtok(sup_devs, s); |
| 378 | while (token != NULL) { |
| 379 | if (strncmp(token, name, strlen(token)) == 0 && |
| 380 | strlen(token) == strlen(name)) { |
| 381 | ALOGD("%s: support dev %s", __func__, token); |
| 382 | ret = true; |
| 383 | break; |
| 384 | } |
| 385 | token = strtok(NULL, s); |
| 386 | } |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static void ma_set_swap_l(struct audio_device *adev, bool enable) |
| 392 | { |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 393 | if (enable) |
| 394 | check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE); |
| 395 | else |
| 396 | check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE); |
| 397 | } |
| 398 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 399 | static void ma_set_rotation_l(struct audio_device *adev, int orientation) |
| 400 | { |
| 401 | if (orientation != 0) |
| 402 | check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_ENABLE); |
| 403 | else |
| 404 | check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_DISABLE); |
| 405 | } |
| 406 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 407 | static void ma_support_usb(bool enable, int card) |
| 408 | { |
| 409 | char path[128]; |
| 410 | char id[32]; |
| 411 | int ret = 0; |
| 412 | int32_t fd = -1; |
| 413 | char *idd; |
| 414 | |
| 415 | if (enable) { |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 416 | ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 417 | if (ret < 0) { |
| 418 | ALOGE("%s: failed on snprintf (%d) to path %s\n", |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 419 | __func__, ret, path); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 420 | goto done; |
| 421 | } |
| 422 | fd = open(path, O_RDONLY); |
| 423 | if (fd < 0) { |
| 424 | ALOGE("%s: error failed to open id file %s error: %d\n", |
| 425 | __func__, path, errno); |
| 426 | goto done; |
| 427 | } |
| 428 | if (read(fd, id, sizeof(id)) < 0) { |
| 429 | ALOGE("%s: file read error", __func__); |
| 430 | goto done; |
| 431 | } |
| 432 | //replace '\n' to '\0' |
| 433 | idd = strtok(id, "\n"); |
| 434 | |
| 435 | if (find_sup_dev(idd)) { |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 436 | ALOGV("%s: support usbid is %s", __func__, id); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 437 | g_supported_dev |= SUPPORTED_USB; |
| 438 | } else |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 439 | ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 440 | } else { |
| 441 | g_supported_dev &= ~SUPPORTED_USB; |
| 442 | } |
| 443 | |
| 444 | done: |
| 445 | if (fd >= 0) close(fd); |
| 446 | } |
| 447 | |
| 448 | // adev_init lock held |
| 449 | void audio_extn_ma_init(void *platform) |
| 450 | { |
| 451 | ma_stream_type_t i = 0; |
| 452 | int ret = 0; |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 453 | char lib_path[128] = {0}; |
| 454 | char mps_path[128] = {0}; |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 455 | char cnf_path[128] = {0}; |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 456 | struct snd_card_split *snd_split_handle = NULL; |
| 457 | snd_split_handle = audio_extn_get_snd_card_split(); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 458 | |
| 459 | if (platform == NULL) { |
| 460 | ALOGE("%s: platform is NULL", __func__); |
| 461 | goto error; |
| 462 | } |
| 463 | |
| 464 | if (my_data) { free(my_data); } |
| 465 | my_data = calloc(1, sizeof(struct ma_platform_data)); |
| 466 | if (my_data == NULL) { |
| 467 | ALOGE("%s: ma_cal alloct fail", __func__); |
| 468 | goto error; |
| 469 | } |
| 470 | |
| 471 | pthread_mutex_init(&my_data->lock, NULL); |
| 472 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 473 | my_data->platform = platform; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 474 | ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM); |
| 475 | if (ret < 0) { |
| 476 | ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret); |
| 477 | goto error; |
| 478 | } |
| 479 | |
| 480 | my_data->waves_handle = dlopen(lib_path, RTLD_NOW); |
| 481 | if (my_data->waves_handle == NULL) { |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 482 | ALOGE("%s: DLOPEN failed for %s, %s", __func__, LIB_MA_PARAM, dlerror()); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 483 | goto error; |
| 484 | } else { |
| 485 | ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM); |
| 486 | |
| 487 | my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 488 | MA_QDSP_PARAM_INIT); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 489 | if (!my_data->ma_param_init) { |
| 490 | ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror()); |
| 491 | goto error; |
| 492 | } |
| 493 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 494 | my_data->ma_param_deinit = (ma_param_deinit_t)dlsym( |
| 495 | my_data->waves_handle, MA_QDSP_PARAM_DEINIT); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 496 | if (!my_data->ma_param_deinit) { |
| 497 | ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror()); |
| 498 | goto error; |
| 499 | } |
| 500 | |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 501 | my_data->ma_is_feature_used = (ma_is_feature_used_t)dlsym(my_data->waves_handle, |
| 502 | MA_QDSP_IS_FEATURE_USED); |
| 503 | if (!my_data->ma_is_feature_used) { |
| 504 | ALOGV("%s: dlsym error %s for ma_is_feature_used", __func__, dlerror()); |
| 505 | } |
| 506 | |
| 507 | my_data->ma_set_orientation = (ma_set_orientation_t)dlsym(my_data->waves_handle, |
| 508 | MA_QDSP_SET_ORIENTATION); |
| 509 | if (!my_data->ma_set_orientation) { |
| 510 | ALOGV("%s: dlsym error %s for ma_set_orientation", __func__, dlerror()); |
| 511 | } |
| 512 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 513 | my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 514 | MA_QDSP_SET_LR_SWAP); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 515 | if (!my_data->ma_set_lr_swap) { |
| 516 | ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror()); |
| 517 | goto error; |
| 518 | } |
| 519 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 520 | my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym( |
| 521 | my_data->waves_handle, MA_QDSP_SET_MODE); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 522 | if (!my_data->ma_set_sound_mode) { |
| 523 | ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror()); |
| 524 | goto error; |
| 525 | } |
| 526 | |
| 527 | my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 528 | MA_QDSP_SET_VOL); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 529 | if (!my_data->ma_set_volume) { |
| 530 | ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror()); |
| 531 | goto error; |
| 532 | } |
| 533 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 534 | my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym( |
| 535 | my_data->waves_handle, MA_QDSP_SET_VOLT); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 536 | if (!my_data->ma_set_volume_table) { |
| 537 | ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror()); |
| 538 | goto error; |
| 539 | } |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 540 | |
| 541 | my_data->ma_set_param = (ma_set_param_t)dlsym( |
| 542 | my_data->waves_handle, MA_QDSP_SET_PARAM); |
| 543 | if (!my_data->ma_set_param) { |
| 544 | ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror()); |
| 545 | goto error; |
| 546 | } |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 547 | } |
| 548 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 549 | /* get preset table */ |
| 550 | if (snd_split_handle == NULL) { |
| 551 | snprintf(mps_path, sizeof(mps_path), "%s/%s.mps", |
| 552 | PRESET_PATH, MPS_BASE_STRING); |
| 553 | } else { |
| 554 | snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps", |
| 555 | PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 556 | } |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 557 | |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 558 | /* get config files */ |
| 559 | if (snd_split_handle == NULL) { |
| 560 | snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini", |
| 561 | PRESET_PATH, CONFIG_BASE_STRING); |
| 562 | } else { |
| 563 | snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini", |
| 564 | PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor); |
| 565 | } |
| 566 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 567 | /* check file */ |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 568 | if (access(mps_path, R_OK) < 0) { |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 569 | ALOGW("%s: file %s isn't existed.", __func__, mps_path); |
| 570 | goto error; |
| 571 | } else |
| 572 | ALOGD("%s: Loading mps file: %s", __func__, mps_path); |
| 573 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 574 | /* TODO: check user preset table once the feature is enabled |
| 575 | if (access(USER_PRESET_PATH, F_OK) < 0 ){ |
| 576 | ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH); |
| 577 | goto error; |
| 578 | } |
| 579 | */ |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 580 | |
| 581 | if (access(cnf_path, R_OK) < 0) { |
| 582 | ALOGW("%s: file %s isn't existed.", __func__, cnf_path); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 583 | goto error; |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 584 | } else |
| 585 | ALOGD("%s: Loading ini file: %s", __func__, cnf_path); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 586 | |
| 587 | /* init ma parameter */ |
| 588 | if (my_data->ma_param_init(&g_ma_audio_cal_handle, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 589 | mps_path, |
| 590 | USER_PRESET_PATH, /* unused */ |
Jasmine Cha | 6d945d2 | 2018-06-19 09:49:04 -0700 | [diff] [blame] | 591 | cnf_path, |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 592 | &set_audio_cal)) { |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 593 | if (!g_ma_audio_cal_handle) { |
| 594 | ALOGE("%s: ma parameters initialize failed", __func__); |
| 595 | my_data->ma_param_deinit(&g_ma_audio_cal_handle); |
| 596 | goto error; |
| 597 | } |
| 598 | ALOGD("%s: ma parameters initialize successful", __func__); |
| 599 | } else { |
| 600 | ALOGE("%s: ma parameters initialize failed", __func__); |
| 601 | goto error; |
| 602 | } |
| 603 | |
| 604 | /* init volume table */ |
| 605 | for (i = 0; i < STREAM_MAX_TYPES; i++) { |
| 606 | ma_cur_state_table[i].vol = 0.0; |
| 607 | ma_cur_state_table[i].active = false; |
| 608 | } |
| 609 | |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 610 | my_data->speaker_lr_swap = false; |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 611 | my_data->orientation_used = false; |
| 612 | my_data->dispaly_orientation = 0; |
| 613 | |
| 614 | if (g_ma_audio_cal_handle && my_data->ma_is_feature_used) { |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 615 | my_data->orientation_used = my_data->ma_is_feature_used( |
| 616 | g_ma_audio_cal_handle, "SET_ORIENTATION"); |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 617 | } |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 618 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 619 | return; |
| 620 | |
| 621 | error: |
| 622 | if (my_data) { free(my_data); } |
| 623 | my_data = NULL; |
| 624 | } |
| 625 | |
| 626 | //adev_init lock held |
| 627 | void audio_extn_ma_deinit() |
| 628 | { |
| 629 | if (my_data) { |
| 630 | /* deinit ma parameter */ |
| 631 | if (my_data->ma_param_deinit && |
| 632 | my_data->ma_param_deinit(&g_ma_audio_cal_handle)) |
| 633 | ALOGD("%s: ma parameters uninitialize successful", __func__); |
| 634 | else |
| 635 | ALOGD("%s: ma parameters uninitialize failed", __func__); |
| 636 | |
| 637 | pthread_mutex_destroy(&my_data->lock); |
| 638 | free(my_data); |
| 639 | my_data = NULL; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | // adev_init and adev lock held |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 644 | bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type, |
| 645 | float vol, bool active) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 646 | { |
| 647 | bool ret = false; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 648 | struct ma_state pr_mstate; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 649 | |
Jasmine Cha | fc63f34 | 2018-06-27 10:55:06 -0700 | [diff] [blame] | 650 | if (stream_type >= STREAM_MAX_TYPES || |
| 651 | stream_type < STREAM_MIN_TYPES) { |
| 652 | ALOGE("%s: stream_type %d out of range.", __func__, stream_type); |
| 653 | return ret; |
| 654 | } |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 655 | |
| 656 | if (!my_data) { |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 657 | ALOGV("%s: maxxaudio isn't initialized.", __func__); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 658 | return ret; |
| 659 | } |
| 660 | |
Jasmine Cha | fc63f34 | 2018-06-27 10:55:06 -0700 | [diff] [blame] | 661 | ALOGV("%s: stream[%d] vol[%f] active[%s]", |
| 662 | __func__, stream_type, vol, active ? "true" : "false"); |
| 663 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 664 | pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol; |
| 665 | pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 666 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 667 | // update condition: vol or active state changes |
| 668 | if (pr_mstate.vol != vol || pr_mstate.active != active) { |
| 669 | |
| 670 | pthread_mutex_lock(&my_data->lock); |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 671 | |
| 672 | ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol; |
| 673 | ma_cur_state_table[(ma_stream_type_t)stream_type].active = active; |
| 674 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 675 | ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 676 | |
| 677 | pthread_mutex_unlock(&my_data->lock); |
| 678 | } |
| 679 | |
| 680 | return ret; |
| 681 | } |
| 682 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 683 | void audio_extn_ma_set_device(struct audio_usecase *usecase) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 684 | { |
| 685 | int i = 0; |
| 686 | int u_index = -1; |
| 687 | float vol = 0; |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 688 | struct ma_audio_cal_settings ma_cal; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 689 | |
| 690 | if (!my_data) { |
| 691 | ALOGV("%s: maxxaudio isn't initialized.", __func__); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | if (!valid_usecase(usecase)) { |
| 696 | ALOGV("%s: %d is not supported usecase", __func__, usecase->id); |
| 697 | return; |
| 698 | } |
| 699 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 700 | ma_cal_init(&ma_cal); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 701 | |
| 702 | /* update audio_cal and send it */ |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 703 | ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type; |
| 704 | ma_cal.common.device = usecase->stream.out->devices; |
| 705 | ALOGV("%s: send usecase(%d) app_type(%d) device(%d)", |
| 706 | __func__, usecase->id, ma_cal.common.app_type, |
| 707 | ma_cal.common.device); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 708 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 709 | pthread_mutex_lock(&my_data->lock); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 710 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 711 | if (is_active()) { |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 712 | |
| 713 | if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 714 | if (my_data->orientation_used) |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 715 | ma_set_rotation_l(usecase->stream.out->dev, |
| 716 | my_data->dispaly_orientation); |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 717 | else |
| 718 | ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap); |
| 719 | } else { |
| 720 | if (my_data->orientation_used) |
| 721 | ma_set_rotation_l(usecase->stream.out->dev, 0); |
| 722 | else |
| 723 | ma_set_swap_l(usecase->stream.out->dev, false); |
| 724 | } |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 725 | |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 726 | if (!ma_set_volume_table_l(&ma_cal, |
| 727 | STREAM_MAX_TYPES, |
| 728 | ma_cur_state_table)) |
| 729 | ALOGE("ma_set_volume_table_l returned with error."); |
| 730 | else |
| 731 | ALOGV("ma_set_volume_table_l success"); |
Jasmine Cha | f5ebe0b | 2019-02-25 20:09:34 +0800 | [diff] [blame] | 732 | print_state_log(); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 733 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 734 | } |
Jasmine Cha | 25cd74d | 2018-10-01 10:06:11 +0800 | [diff] [blame] | 735 | pthread_mutex_unlock(&my_data->lock); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 736 | } |
| 737 | |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 738 | void audio_extn_ma_set_parameters(struct audio_device *adev, |
| 739 | struct str_parms *parms) |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 740 | { |
| 741 | int ret; |
| 742 | bool ret_b; |
| 743 | int val; |
| 744 | char value[128]; |
| 745 | |
| 746 | // do LR swap and usb recognition |
| 747 | ret = str_parms_get_int(parms, "rotation", &val); |
| 748 | if (ret >= 0) { |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 749 | if (!my_data) { |
| 750 | ALOGV("%s: maxxaudio isn't initialized.", __func__); |
| 751 | return; |
| 752 | } |
| 753 | |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 754 | switch (val) { |
| 755 | case 270: |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 756 | my_data->speaker_lr_swap = true; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 757 | break; |
| 758 | case 0: |
| 759 | case 90: |
| 760 | case 180: |
Robert Lee | 837637a | 2018-11-16 16:34:43 +0800 | [diff] [blame] | 761 | my_data->speaker_lr_swap = false; |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 762 | break; |
| 763 | } |
justinweng | 3d9b791 | 2019-01-11 14:38:06 +0800 | [diff] [blame] | 764 | my_data->dispaly_orientation = val; |
| 765 | |
| 766 | if (my_data->orientation_used) |
| 767 | ma_set_rotation_l(adev, my_data->dispaly_orientation); |
| 768 | else |
| 769 | ma_set_swap_l(adev, my_data->speaker_lr_swap); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // check connect status |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 773 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value, |
| 774 | sizeof(value)); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 775 | if (ret >= 0) { |
| 776 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
| 777 | if (audio_is_usb_out_device(device)) { |
| 778 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 779 | if (ret >= 0) { |
| 780 | const int card = atoi(value); |
| 781 | ma_support_usb(true, card); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | // check disconnect status |
Jasmine Cha | 70771b6 | 2018-05-15 15:02:43 +0800 | [diff] [blame] | 787 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value, |
| 788 | sizeof(value)); |
jasmine cha | 270b776 | 2018-03-30 15:41:33 +0800 | [diff] [blame] | 789 | if (ret >= 0) { |
| 790 | audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10); |
| 791 | if (audio_is_usb_out_device(device)) { |
| 792 | ret = str_parms_get_str(parms, "card", value, sizeof(value)); |
| 793 | if (ret >= 0) { |
| 794 | const int card = atoi(value); |
| 795 | ma_support_usb(false, card /*useless*/); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | bool audio_extn_ma_supported_usb() |
| 802 | { |
| 803 | ALOGV("%s: current support 0x%x", __func__, g_supported_dev); |
| 804 | return (g_supported_dev & SUPPORTED_USB) ? true : false; |
| 805 | } |