blob: 5f2a7f01a252d53a8d6a43b70fa97262d9a12ae4 [file] [log] [blame]
jasmine cha75fa6f02018-03-30 15:41:33 +08001/*
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 cha75fa6f02018-03-30 15:41:33 +080020#include <audio_hw.h>
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080021#include <cutils/str_parms.h>
22#include <dlfcn.h>
23#include <log/log.h>
jasmine cha75fa6f02018-03-30 15:41:33 +080024#include <math.h>
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080025#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 cha75fa6f02018-03-30 15:41:33 +080032#include "audio_extn.h"
33#include "maxxaudio.h"
34
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080035#define LIB_MA_PARAM "libmaxxaudioqdsp.so"
jasmine cha75fa6f02018-03-30 15:41:33 +080036#define LIB_MA_PATH "vendor/lib/"
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080037#define PRESET_PATH "/vendor/etc"
38#define MPS_BASE_STRING "default"
jasmine cha75fa6f02018-03-30 15:41:33 +080039#define USER_PRESET_PATH ""
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080040#define CONFIG_BASE_STRING "maxx_conf"
jasmine cha75fa6f02018-03-30 15:41:33 +080041#define CAL_PRESIST_STR "cal_persist"
42#define CAL_SAMPLERATE_STR "cal_samplerate"
43
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080044#define MA_QDSP_PARAM_INIT "maxxaudio_qdsp_initialize"
45#define MA_QDSP_PARAM_DEINIT "maxxaudio_qdsp_uninitialize"
46#define MA_QDSP_SET_LR_SWAP "maxxaudio_qdsp_set_lr_swap"
47#define MA_QDSP_SET_MODE "maxxaudio_qdsp_set_sound_mode"
48#define MA_QDSP_SET_VOL "maxxaudio_qdsp_set_volume"
49#define MA_QDSP_SET_VOLT "maxxaudio_qdsp_set_volume_table"
50#define MA_QDSP_SET_PARAM "maxxaudio_qdsp_set_parameter"
jasmine cha75fa6f02018-03-30 15:41:33 +080051
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080052#define SUPPORT_DEV "18d1:5033" // Blackbird usbid
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080053#define SUPPORTED_USB 0x01
jasmine cha75fa6f02018-03-30 15:41:33 +080054
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080055typedef unsigned int effective_scope_flag_t;
56const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0; /* RTC */
57const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1; /* ACDB */
58const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB;
59const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0;
60const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE;
jasmine cha75fa6f02018-03-30 15:41:33 +080061
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080062const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2;
63const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0;
64const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR;
65const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR;
66
67const unsigned int VALUE_AUTO = 0xFFFFFFFF;
68const unsigned int APP_TYPE_AUTO = VALUE_AUTO;
69const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO;
70const unsigned int DEVICE_AUTO = VALUE_AUTO;
71const unsigned int DEVICE_DEFAULT = DEVICE_AUTO;
72
73const unsigned int MAAP_OUTPUT_GAIN = 27;
jasmine cha75fa6f02018-03-30 15:41:33 +080074
75typedef enum MA_STREAM_TYPE {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080076 STREAM_MIN_TYPES = 0,
77 STREAM_VOICE = STREAM_MIN_TYPES,
jasmine cha75fa6f02018-03-30 15:41:33 +080078 STREAM_SYSTEM,
79 STREAM_RING,
80 STREAM_MUSIC,
81 STREAM_ALARM,
82 STREAM_NOTIFICATION ,
83 STREAM_MAX_TYPES,
84} ma_stream_type_t;
85
86typedef enum MA_CMD {
87 MA_CMD_VOL,
88 MA_CMD_SWAP_ENABLE,
89 MA_CMD_SWAP_DISABLE,
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080090 MA_CMD_SOFT_MUTE_ENABLE,
91 MA_CMD_SOFT_MUTE_DISABLE,
jasmine cha75fa6f02018-03-30 15:41:33 +080092} ma_cmd_t;
93
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080094typedef struct ma_audio_cal_version {
95 unsigned int major;
96 unsigned int minor;
97} ma_audio_cal_version_t;
98
99typedef struct ma_audio_cal_common_settings {
100 unsigned int app_type;
101 unsigned int device;
102} ma_audio_cal_common_settings_t;
103
104struct ma_audio_cal_settings {
105 ma_audio_cal_version_t version;
106 ma_audio_cal_common_settings_t common;
107 effective_scope_flag_t effect_scope_flag;
108};
109
110struct ma_state {
111 float vol;
112 bool active;
113};
114
jasmine cha75fa6f02018-03-30 15:41:33 +0800115typedef void *ma_audio_cal_handle_t;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800116typedef int (*set_audio_cal_t)(const char *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800117
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800118typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
119 const char *, const char *, set_audio_cal_t);
jasmine cha75fa6f02018-03-30 15:41:33 +0800120
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800121typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800122
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800123typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
124 const struct ma_audio_cal_settings *, bool);
jasmine cha75fa6f02018-03-30 15:41:33 +0800125
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800126typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
127 const struct ma_audio_cal_settings *,
128 unsigned int);
jasmine cha75fa6f02018-03-30 15:41:33 +0800129
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800130typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
131 const struct ma_audio_cal_settings *, double);
jasmine cha75fa6f02018-03-30 15:41:33 +0800132
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800133typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
134 const struct ma_audio_cal_settings *,
135 size_t, struct ma_state *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800136
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800137typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
138 const struct ma_audio_cal_settings *,
139 unsigned int, double);
140
jasmine cha75fa6f02018-03-30 15:41:33 +0800141struct ma_platform_data {
142 void *waves_handle;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800143 void *platform;
jasmine cha75fa6f02018-03-30 15:41:33 +0800144 pthread_mutex_t lock;
145 ma_param_init_t ma_param_init;
146 ma_param_deinit_t ma_param_deinit;
147 ma_set_lr_swap_t ma_set_lr_swap;
148 ma_set_sound_mode_t ma_set_sound_mode;
149 ma_set_volume_t ma_set_volume;
150 ma_set_volume_table_t ma_set_volume_table;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800151 ma_set_param_t ma_set_param;
152 bool speaker_lr_swap;
jasmine cha75fa6f02018-03-30 15:41:33 +0800153};
154
155ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
156static uint16_t g_supported_dev = 0;
157static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
158static struct ma_platform_data *my_data = NULL;
159
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800160static int set_audio_cal(const char *audio_cal)
jasmine cha75fa6f02018-03-30 15:41:33 +0800161{
162 ALOGV("set_audio_cal: %s", audio_cal);
163
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800164 return platform_set_parameters(my_data->platform,
jasmine cha75fa6f02018-03-30 15:41:33 +0800165 str_parms_create_str(audio_cal));
166}
167
168static bool ma_set_lr_swap_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800169 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha75fa6f02018-03-30 15:41:33 +0800170{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800171 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
172 audio_cal_settings, swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800173}
174
jasmine cha75fa6f02018-03-30 15:41:33 +0800175static bool ma_set_volume_table_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800176 const struct ma_audio_cal_settings *audio_cal_settings,
177 size_t num_streams, struct ma_state *volume_table)
jasmine cha75fa6f02018-03-30 15:41:33 +0800178{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800179 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
180 audio_cal_settings, num_streams,
181 volume_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800182}
183
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800184static bool ma_set_param_l(
185 const struct ma_audio_cal_settings *audio_cal_settings,
186 unsigned int index, double value)
187{
188 return my_data->ma_set_param(g_ma_audio_cal_handle,
189 audio_cal_settings, index, value);
190}
191
jasmine cha75fa6f02018-03-30 15:41:33 +0800192static inline bool valid_usecase(struct audio_usecase *usecase)
193{
194 if ((usecase->type == PCM_PLAYBACK) &&
195 /* supported usecases */
196 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
197 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
198 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
199 /* support devices */
200 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
201 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800202 (audio_is_usb_out_device(usecase->devices) &&
203 audio_extn_ma_supported_usb())))
204 /* TODO: enable A2DP when it is ready */
jasmine cha75fa6f02018-03-30 15:41:33 +0800205
206 return true;
207
208 ALOGV("%s: not support type %d usecase %d device %d",
209 __func__, usecase->type, usecase->id, usecase->devices);
210
211 return false;
212}
213
214// already hold lock
215static inline bool is_active()
216{
217 ma_stream_type_t i = 0;
218
219 for (i = 0; i < STREAM_MAX_TYPES; i++)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800220 if (ma_cur_state_table[i].active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800221 return true;
222
223 return false;
224}
225
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800226static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
227{
228 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
229 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
230 ma_cal->common.app_type = APP_TYPE_DEFAULT;
231 ma_cal->common.device = DEVICE_DEFAULT;
232 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
233}
234
jasmine cha75fa6f02018-03-30 15:41:33 +0800235static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
236{
237 int i = 0;
238 bool ret = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800239 struct listnode *node;
240 struct audio_usecase *usecase;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800241 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800242
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800243 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800244
245 list_for_each(node, &adev->usecase_list) {
246 usecase = node_to_item(node, struct audio_usecase, list);
247 if (valid_usecase(usecase)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800248 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
249 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha75fa6f02018-03-30 15:41:33 +0800250 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800251 __func__, usecase->id, ma_cal.common.app_type,
252 ma_cal.common.device);
jasmine cha75fa6f02018-03-30 15:41:33 +0800253
254 switch (cmd) {
255 case MA_CMD_VOL:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800256 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800257 ma_cur_state_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800258 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800259 ALOGV("ma_set_volume_table_l success");
jasmine cha75fa6f02018-03-30 15:41:33 +0800260 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800261 ALOGE("ma_set_volume_table_l returned with error.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800262
263 ALOGV("%s: send volume table === Start", __func__);
264 for (i = 0; i < STREAM_MAX_TYPES; i++)
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800265 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__,
266 i, ma_cur_state_table[i].vol,
267 ma_cur_state_table[i].active ? "T" : "F");
jasmine cha75fa6f02018-03-30 15:41:33 +0800268 ALOGV("%s: send volume table === End", __func__);
269 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800270
jasmine cha75fa6f02018-03-30 15:41:33 +0800271 case MA_CMD_SWAP_ENABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800272 /* lr swap only enable for speaker path */
273 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
274 ret = ma_set_lr_swap_l(&ma_cal, true);
275 if (ret)
276 ALOGV("ma_set_lr_swap_l enable returned with success.");
277 else
278 ALOGE("ma_set_lr_swap_l enable returned with error.");
279 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800280 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800281
jasmine cha75fa6f02018-03-30 15:41:33 +0800282 case MA_CMD_SWAP_DISABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800283 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha75fa6f02018-03-30 15:41:33 +0800284 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800285 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800286 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800287 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800288 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800289
290 case MA_CMD_SOFT_MUTE_ENABLE:
291 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
292
293 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
294 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, -96);
295 if (!ret)
296 ALOGE("soft mute enable returned with error.");
297 break;
298
299 case MA_CMD_SOFT_MUTE_DISABLE:
300 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
301
302 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
303 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, 0);
304 if (!ret)
305 ALOGE("soft mute disable returned with error.");
306 break;
307
jasmine cha75fa6f02018-03-30 15:41:33 +0800308 default:
309 ALOGE("%s: unsupported cmd %d", __func__, cmd);
310 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800311 }
312 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800313
314 return ret;
315}
316
317static bool find_sup_dev(char *name)
318{
319 char *token;
320 const char s[2] = ",";
321 bool ret = false;
322 char sup_devs[128];
323
324 // the rule of comforming suppored dev's name
325 // 1. Both string len are equal
326 // 2. Both string content are equal
327
328 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
329 token = strtok(sup_devs, s);
330 while (token != NULL) {
331 if (strncmp(token, name, strlen(token)) == 0 &&
332 strlen(token) == strlen(name)) {
333 ALOGD("%s: support dev %s", __func__, token);
334 ret = true;
335 break;
336 }
337 token = strtok(NULL, s);
338 }
339
340 return ret;
341}
342
343static void ma_set_swap_l(struct audio_device *adev, bool enable)
344{
jasmine cha75fa6f02018-03-30 15:41:33 +0800345 if (enable)
346 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
347 else
348 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
349}
350
351static void ma_support_usb(bool enable, int card)
352{
353 char path[128];
354 char id[32];
355 int ret = 0;
356 int32_t fd = -1;
357 char *idd;
358
359 if (enable) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800360 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha75fa6f02018-03-30 15:41:33 +0800361 if (ret < 0) {
362 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800363 __func__, ret, path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800364 goto done;
365 }
366 fd = open(path, O_RDONLY);
367 if (fd < 0) {
368 ALOGE("%s: error failed to open id file %s error: %d\n",
369 __func__, path, errno);
370 goto done;
371 }
372 if (read(fd, id, sizeof(id)) < 0) {
373 ALOGE("%s: file read error", __func__);
374 goto done;
375 }
376 //replace '\n' to '\0'
377 idd = strtok(id, "\n");
378
379 if (find_sup_dev(idd)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800380 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha75fa6f02018-03-30 15:41:33 +0800381 g_supported_dev |= SUPPORTED_USB;
382 } else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800383 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha75fa6f02018-03-30 15:41:33 +0800384 } else {
385 g_supported_dev &= ~SUPPORTED_USB;
386 }
387
388done:
389 if (fd >= 0) close(fd);
390}
391
392// adev_init lock held
393void audio_extn_ma_init(void *platform)
394{
395 ma_stream_type_t i = 0;
396 int ret = 0;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800397 char lib_path[128] = {0};
398 char mps_path[128] = {0};
399 char cnf_path[128] = {0};
400 struct snd_card_split *snd_split_handle = NULL;
401 snd_split_handle = audio_extn_get_snd_card_split();
jasmine cha75fa6f02018-03-30 15:41:33 +0800402
403 if (platform == NULL) {
404 ALOGE("%s: platform is NULL", __func__);
405 goto error;
406 }
407
408 if (my_data) { free(my_data); }
409 my_data = calloc(1, sizeof(struct ma_platform_data));
410 if (my_data == NULL) {
411 ALOGE("%s: ma_cal alloct fail", __func__);
412 goto error;
413 }
414
415 pthread_mutex_init(&my_data->lock, NULL);
416
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800417 my_data->platform = platform;
jasmine cha75fa6f02018-03-30 15:41:33 +0800418 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
419 if (ret < 0) {
420 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
421 goto error;
422 }
423
424 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
425 if (my_data->waves_handle == NULL) {
426 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
427 goto error;
428 } else {
429 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
430
431 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800432 MA_QDSP_PARAM_INIT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800433 if (!my_data->ma_param_init) {
434 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
435 goto error;
436 }
437
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800438 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
439 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800440 if (!my_data->ma_param_deinit) {
441 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
442 goto error;
443 }
444
445 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800446 MA_QDSP_SET_LR_SWAP);
jasmine cha75fa6f02018-03-30 15:41:33 +0800447 if (!my_data->ma_set_lr_swap) {
448 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
449 goto error;
450 }
451
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800452 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
453 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha75fa6f02018-03-30 15:41:33 +0800454 if (!my_data->ma_set_sound_mode) {
455 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
456 goto error;
457 }
458
459 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800460 MA_QDSP_SET_VOL);
jasmine cha75fa6f02018-03-30 15:41:33 +0800461 if (!my_data->ma_set_volume) {
462 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
463 goto error;
464 }
465
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800466 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
467 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800468 if (!my_data->ma_set_volume_table) {
469 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
470 goto error;
471 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800472
473 my_data->ma_set_param = (ma_set_param_t)dlsym(
474 my_data->waves_handle, MA_QDSP_SET_PARAM);
475 if (!my_data->ma_set_param) {
476 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
477 goto error;
478 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800479 }
480
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800481 /* get preset table */
482 if (snd_split_handle == NULL) {
483 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
484 PRESET_PATH, MPS_BASE_STRING);
485 } else {
486 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
487 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha75fa6f02018-03-30 15:41:33 +0800488 }
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800489
490 /* get config files */
491 if (snd_split_handle == NULL) {
492 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
493 PRESET_PATH, CONFIG_BASE_STRING);
494 } else {
495 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
496 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
497 }
498
499 /* check file */
500 if (access(mps_path, R_OK) < 0) {
501 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
502 goto error;
503 } else
504 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
505
jasmine cha75fa6f02018-03-30 15:41:33 +0800506 /* TODO: check user preset table once the feature is enabled
507 if (access(USER_PRESET_PATH, F_OK) < 0 ){
508 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
509 goto error;
510 }
511 */
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800512
513 if (access(cnf_path, R_OK) < 0) {
514 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800515 goto error;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800516 } else
517 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800518
519 /* init ma parameter */
520 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800521 mps_path,
522 USER_PRESET_PATH, /* unused */
523 cnf_path,
524 &set_audio_cal)) {
jasmine cha75fa6f02018-03-30 15:41:33 +0800525 if (!g_ma_audio_cal_handle) {
526 ALOGE("%s: ma parameters initialize failed", __func__);
527 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
528 goto error;
529 }
530 ALOGD("%s: ma parameters initialize successful", __func__);
531 } else {
532 ALOGE("%s: ma parameters initialize failed", __func__);
533 goto error;
534 }
535
536 /* init volume table */
537 for (i = 0; i < STREAM_MAX_TYPES; i++) {
538 ma_cur_state_table[i].vol = 0.0;
539 ma_cur_state_table[i].active = false;
540 }
541
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800542 my_data->speaker_lr_swap = false;
543
jasmine cha75fa6f02018-03-30 15:41:33 +0800544 return;
545
546error:
547 if (my_data) { free(my_data); }
548 my_data = NULL;
549}
550
551//adev_init lock held
552void audio_extn_ma_deinit()
553{
554 if (my_data) {
555 /* deinit ma parameter */
556 if (my_data->ma_param_deinit &&
557 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
558 ALOGD("%s: ma parameters uninitialize successful", __func__);
559 else
560 ALOGD("%s: ma parameters uninitialize failed", __func__);
561
562 pthread_mutex_destroy(&my_data->lock);
563 free(my_data);
564 my_data = NULL;
565 }
566}
567
568// adev_init and adev lock held
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800569bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
570 float vol, bool active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800571{
572 bool ret = false;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800573 bool first_enable = false;
574 struct ma_state pr_mstate;
jasmine cha75fa6f02018-03-30 15:41:33 +0800575
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800576 if (stream_type >= STREAM_MAX_TYPES ||
577 stream_type < STREAM_MIN_TYPES) {
578 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
579 return ret;
580 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800581
582 if (!my_data) {
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800583 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha75fa6f02018-03-30 15:41:33 +0800584 return ret;
585 }
586
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800587 ALOGV("%s: stream[%d] vol[%f] active[%s]",
588 __func__, stream_type, vol, active ? "true" : "false");
jasmine cha75fa6f02018-03-30 15:41:33 +0800589
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800590 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
591 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
592
593 // update condition: vol or active state changes
594 if (pr_mstate.vol != vol || pr_mstate.active != active) {
595
596 pthread_mutex_lock(&my_data->lock);
597 // get active state before updating
598 first_enable = (!is_active()) && active;
599
600 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
601 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
602
603 if (first_enable) //all F -> one of T
604 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_DISABLE);
605 else if (!is_active()) // all F
606 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_ENABLE);
607
608 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha75fa6f02018-03-30 15:41:33 +0800609
610 pthread_mutex_unlock(&my_data->lock);
611 }
612
613 return ret;
614}
615
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800616void audio_extn_ma_set_device(struct audio_usecase *usecase)
jasmine cha75fa6f02018-03-30 15:41:33 +0800617{
618 int i = 0;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800619 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800620
621 if (!my_data) {
622 ALOGV("%s: maxxaudio isn't initialized.", __func__);
623 return;
624 }
625
626 if (!valid_usecase(usecase)) {
627 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
628 return;
629 }
630
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800631 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800632
633 /* update audio_cal and send it */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800634 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
635 ma_cal.common.device = usecase->stream.out->devices;
636 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
637 __func__, usecase->id, ma_cal.common.app_type,
638 ma_cal.common.device);
jasmine cha75fa6f02018-03-30 15:41:33 +0800639
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800640 pthread_mutex_lock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800641
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800642 if (is_active()) {
643 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER)
644 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
645 else
646 ma_set_swap_l(usecase->stream.out->dev, false);
jasmine cha75fa6f02018-03-30 15:41:33 +0800647
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800648 ALOGV("%s: send volume table === Start", __func__);
649 for (i = 0; i < STREAM_MAX_TYPES; i++)
650 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
651 ma_cur_state_table[i].vol,
652 ma_cur_state_table[i].active ? "T" : "F");
653 ALOGV("%s: send volume table === End", __func__);
jasmine cha75fa6f02018-03-30 15:41:33 +0800654
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800655 if (!ma_set_volume_table_l(&ma_cal,
656 STREAM_MAX_TYPES,
657 ma_cur_state_table))
658 ALOGE("ma_set_volume_table_l returned with error.");
659 else
660 ALOGV("ma_set_volume_table_l success");
661
jasmine cha75fa6f02018-03-30 15:41:33 +0800662 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800663 pthread_mutex_unlock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800664}
665
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800666void audio_extn_ma_set_parameters(struct audio_device *adev,
667 struct str_parms *parms)
jasmine cha75fa6f02018-03-30 15:41:33 +0800668{
669 int ret;
jasmine cha75fa6f02018-03-30 15:41:33 +0800670 int val;
671 char value[128];
672
673 // do LR swap and usb recognition
674 ret = str_parms_get_int(parms, "rotation", &val);
675 if (ret >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800676 if (!my_data) {
677 ALOGV("%s: maxxaudio isn't initialized.", __func__);
678 return;
679 }
680
jasmine cha75fa6f02018-03-30 15:41:33 +0800681 switch (val) {
682 case 270:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800683 my_data->speaker_lr_swap = true;
jasmine cha75fa6f02018-03-30 15:41:33 +0800684 break;
685 case 0:
686 case 90:
687 case 180:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800688 my_data->speaker_lr_swap = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800689 break;
690 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800691 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800692 }
693
694 // check connect status
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800695 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
696 sizeof(value));
jasmine cha75fa6f02018-03-30 15:41:33 +0800697 if (ret >= 0) {
698 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
699 if (audio_is_usb_out_device(device)) {
700 ret = str_parms_get_str(parms, "card", value, sizeof(value));
701 if (ret >= 0) {
702 const int card = atoi(value);
703 ma_support_usb(true, card);
704 }
705 }
706 }
707
708 // check disconnect status
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800709 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
710 sizeof(value));
jasmine cha75fa6f02018-03-30 15:41:33 +0800711 if (ret >= 0) {
712 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
713 if (audio_is_usb_out_device(device)) {
714 ret = str_parms_get_str(parms, "card", value, sizeof(value));
715 if (ret >= 0) {
716 const int card = atoi(value);
717 ma_support_usb(false, card /*useless*/);
718 }
719 }
720 }
721}
722
723bool audio_extn_ma_supported_usb()
724{
725 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
726 return (g_supported_dev & SUPPORTED_USB) ? true : false;
727}