blob: 2fd188d63334d0bf9f9217ac2225b51037890ce8 [file] [log] [blame]
jasmine cha270b7762018-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 cha270b7762018-03-30 15:41:33 +080020#include <audio_hw.h>
Jasmine Cha70771b62018-05-15 15:02:43 +080021#include <cutils/str_parms.h>
22#include <dlfcn.h>
23#include <log/log.h>
jasmine cha270b7762018-03-30 15:41:33 +080024#include <math.h>
Jasmine Cha70771b62018-05-15 15:02:43 +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 cha270b7762018-03-30 15:41:33 +080032#include "audio_extn.h"
33#include "maxxaudio.h"
34
Jasmine Cha70771b62018-05-15 15:02:43 +080035#define LIB_MA_PARAM "libmaxxaudioqdsp.so"
jasmine cha270b7762018-03-30 15:41:33 +080036#define LIB_MA_PATH "vendor/lib/"
Jasmine Cha70771b62018-05-15 15:02:43 +080037#define PRESET_PATH "/vendor/etc"
38#define MPS_BASE_STRING "default"
jasmine cha270b7762018-03-30 15:41:33 +080039#define USER_PRESET_PATH ""
Jasmine Cha6d945d22018-06-19 09:49:04 -070040#define CONFIG_BASE_STRING "maxx_conf"
jasmine cha270b7762018-03-30 15:41:33 +080041#define CAL_PRESIST_STR "cal_persist"
42#define CAL_SAMPLERATE_STR "cal_samplerate"
43
Jasmine Cha25cd74d2018-10-01 10:06:11 +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 cha270b7762018-03-30 15:41:33 +080051
52#define SUPPORT_DEV "Blackbird"
Jasmine Cha70771b62018-05-15 15:02:43 +080053#define SUPPORTED_USB 0x01
jasmine cha270b7762018-03-30 15:41:33 +080054
Jasmine Cha25cd74d2018-10-01 10:06:11 +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 cha270b7762018-03-30 15:41:33 +080061
Jasmine Cha25cd74d2018-10-01 10:06:11 +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 cha270b7762018-03-30 15:41:33 +080074
75typedef enum MA_STREAM_TYPE {
Jasmine Chafc63f342018-06-27 10:55:06 -070076 STREAM_MIN_TYPES = 0,
77 STREAM_VOICE = STREAM_MIN_TYPES,
jasmine cha270b7762018-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,
Jasmine Cha25cd74d2018-10-01 10:06:11 +080090 MA_CMD_SOFT_MUTE_ENABLE,
91 MA_CMD_SOFT_MUTE_DISABLE,
jasmine cha270b7762018-03-30 15:41:33 +080092} ma_cmd_t;
93
Jasmine Cha25cd74d2018-10-01 10:06:11 +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 cha270b7762018-03-30 15:41:33 +0800115typedef void *ma_audio_cal_handle_t;
Jasmine Cha70771b62018-05-15 15:02:43 +0800116typedef int (*set_audio_cal_t)(const char *);
jasmine cha270b7762018-03-30 15:41:33 +0800117
Jasmine Cha70771b62018-05-15 15:02:43 +0800118typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
119 const char *, const char *, set_audio_cal_t);
jasmine cha270b7762018-03-30 15:41:33 +0800120
Jasmine Cha70771b62018-05-15 15:02:43 +0800121typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
jasmine cha270b7762018-03-30 15:41:33 +0800122
Jasmine Cha70771b62018-05-15 15:02:43 +0800123typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
124 const struct ma_audio_cal_settings *, bool);
jasmine cha270b7762018-03-30 15:41:33 +0800125
Jasmine Cha70771b62018-05-15 15:02:43 +0800126typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
127 const struct ma_audio_cal_settings *,
128 unsigned int);
jasmine cha270b7762018-03-30 15:41:33 +0800129
Jasmine Cha70771b62018-05-15 15:02:43 +0800130typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
131 const struct ma_audio_cal_settings *, double);
jasmine cha270b7762018-03-30 15:41:33 +0800132
Jasmine Cha70771b62018-05-15 15:02:43 +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 cha270b7762018-03-30 15:41:33 +0800136
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800137typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
138 const struct ma_audio_cal_settings *,
139 unsigned int, double);
140
jasmine cha270b7762018-03-30 15:41:33 +0800141struct ma_platform_data {
142 void *waves_handle;
Jasmine Cha70771b62018-05-15 15:02:43 +0800143 void *platform;
jasmine cha270b7762018-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;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800151 ma_set_param_t ma_set_param;
jasmine cha270b7762018-03-30 15:41:33 +0800152};
153
154ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
155static uint16_t g_supported_dev = 0;
156static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
157static struct ma_platform_data *my_data = NULL;
158
Jasmine Cha70771b62018-05-15 15:02:43 +0800159static int set_audio_cal(const char *audio_cal)
jasmine cha270b7762018-03-30 15:41:33 +0800160{
161 ALOGV("set_audio_cal: %s", audio_cal);
162
Jasmine Cha70771b62018-05-15 15:02:43 +0800163 return platform_set_parameters(my_data->platform,
jasmine cha270b7762018-03-30 15:41:33 +0800164 str_parms_create_str(audio_cal));
165}
166
167static bool ma_set_lr_swap_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800168 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha270b7762018-03-30 15:41:33 +0800169{
Jasmine Cha70771b62018-05-15 15:02:43 +0800170 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
171 audio_cal_settings, swap);
jasmine cha270b7762018-03-30 15:41:33 +0800172}
173
174static bool ma_set_sound_mode_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800175 const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
jasmine cha270b7762018-03-30 15:41:33 +0800176{
177 return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800178 audio_cal_settings, sound_mode);
jasmine cha270b7762018-03-30 15:41:33 +0800179}
180
181static bool ma_set_volume_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800182 const struct ma_audio_cal_settings *audio_cal_settings, double volume)
jasmine cha270b7762018-03-30 15:41:33 +0800183{
Jasmine Cha70771b62018-05-15 15:02:43 +0800184 return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
185 volume);
jasmine cha270b7762018-03-30 15:41:33 +0800186}
187
188static bool ma_set_volume_table_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800189 const struct ma_audio_cal_settings *audio_cal_settings,
190 size_t num_streams, struct ma_state *volume_table)
jasmine cha270b7762018-03-30 15:41:33 +0800191{
Jasmine Cha70771b62018-05-15 15:02:43 +0800192 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
193 audio_cal_settings, num_streams,
194 volume_table);
jasmine cha270b7762018-03-30 15:41:33 +0800195}
196
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800197static bool ma_set_param_l(
198 const struct ma_audio_cal_settings *audio_cal_settings,
199 unsigned int index, double value)
200{
201 return my_data->ma_set_param(g_ma_audio_cal_handle,
202 audio_cal_settings, index, value);
203}
204
jasmine cha270b7762018-03-30 15:41:33 +0800205static inline bool valid_usecase(struct audio_usecase *usecase)
206{
207 if ((usecase->type == PCM_PLAYBACK) &&
208 /* supported usecases */
209 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
210 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
211 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
212 /* support devices */
213 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800214 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE)))
215 /* TODO: enable A2DP/USB when it is ready */
jasmine cha270b7762018-03-30 15:41:33 +0800216
217 return true;
218
219 ALOGV("%s: not support type %d usecase %d device %d",
220 __func__, usecase->type, usecase->id, usecase->devices);
221
222 return false;
223}
224
225// already hold lock
226static inline bool is_active()
227{
228 ma_stream_type_t i = 0;
229
230 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800231 if (ma_cur_state_table[i].active)
jasmine cha270b7762018-03-30 15:41:33 +0800232 return true;
233
234 return false;
235}
236
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800237static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
238{
239 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
240 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
241 ma_cal->common.app_type = APP_TYPE_DEFAULT;
242 ma_cal->common.device = DEVICE_DEFAULT;
243 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
244}
245
jasmine cha270b7762018-03-30 15:41:33 +0800246static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
247{
248 int i = 0;
249 bool ret = false;
250 float vol = 0;
251 struct listnode *node;
252 struct audio_usecase *usecase;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800253 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800254
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800255 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800256
257 list_for_each(node, &adev->usecase_list) {
258 usecase = node_to_item(node, struct audio_usecase, list);
259 if (valid_usecase(usecase)) {
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800260 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
261 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha270b7762018-03-30 15:41:33 +0800262 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800263 __func__, usecase->id, ma_cal.common.app_type,
264 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800265
266 switch (cmd) {
267 case MA_CMD_VOL:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800268 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Jasmine Cha70771b62018-05-15 15:02:43 +0800269 ma_cur_state_table);
jasmine cha270b7762018-03-30 15:41:33 +0800270 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800271 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800272 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800273 ALOGE("ma_set_volume_table_l returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800274
275 ALOGV("%s: send volume table === Start", __func__);
276 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha70771b62018-05-15 15:02:43 +0800277 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__,
278 i, ma_cur_state_table[i].vol,
279 ma_cur_state_table[i].active ? "T" : "F");
jasmine cha270b7762018-03-30 15:41:33 +0800280 ALOGV("%s: send volume table === End", __func__);
281 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800282
jasmine cha270b7762018-03-30 15:41:33 +0800283 case MA_CMD_SWAP_ENABLE:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800284 ret = ma_set_lr_swap_l(&ma_cal, true);
jasmine cha270b7762018-03-30 15:41:33 +0800285 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800286 ALOGV("ma_set_lr_swap_l enable returned with success.");
jasmine cha270b7762018-03-30 15:41:33 +0800287 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800288 ALOGE("ma_set_lr_swap_l enable returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800289 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800290
jasmine cha270b7762018-03-30 15:41:33 +0800291 case MA_CMD_SWAP_DISABLE:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800292 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha270b7762018-03-30 15:41:33 +0800293 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800294 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha270b7762018-03-30 15:41:33 +0800295 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800296 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800297 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800298
299 case MA_CMD_SOFT_MUTE_ENABLE:
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, -96);
304 if (!ret)
305 ALOGE("soft mute enable returned with error.");
306 break;
307
308 case MA_CMD_SOFT_MUTE_DISABLE:
309 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
310
311 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
312 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, 0);
313 if (!ret)
314 ALOGE("soft mute disable returned with error.");
315 break;
316
jasmine cha270b7762018-03-30 15:41:33 +0800317 default:
318 ALOGE("%s: unsupported cmd %d", __func__, cmd);
319 }
jasmine cha270b7762018-03-30 15:41:33 +0800320 }
321 }
jasmine cha270b7762018-03-30 15:41:33 +0800322
323 return ret;
324}
325
326static bool find_sup_dev(char *name)
327{
328 char *token;
329 const char s[2] = ",";
330 bool ret = false;
331 char sup_devs[128];
332
333 // the rule of comforming suppored dev's name
334 // 1. Both string len are equal
335 // 2. Both string content are equal
336
337 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
338 token = strtok(sup_devs, s);
339 while (token != NULL) {
340 if (strncmp(token, name, strlen(token)) == 0 &&
341 strlen(token) == strlen(name)) {
342 ALOGD("%s: support dev %s", __func__, token);
343 ret = true;
344 break;
345 }
346 token = strtok(NULL, s);
347 }
348
349 return ret;
350}
351
352static void ma_set_swap_l(struct audio_device *adev, bool enable)
353{
Jasmine Cha70771b62018-05-15 15:02:43 +0800354 // do platform LR swap if it enables on Waves effect
355 // but there is no Waves implementation
jasmine cha270b7762018-03-30 15:41:33 +0800356 if (!my_data) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800357 platform_check_and_set_swap_lr_channels(adev, enable);
358 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800359 return;
360 }
Jasmine Cha70771b62018-05-15 15:02:43 +0800361
jasmine cha270b7762018-03-30 15:41:33 +0800362 if (enable)
363 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
364 else
365 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
366}
367
368static void ma_support_usb(bool enable, int card)
369{
370 char path[128];
371 char id[32];
372 int ret = 0;
373 int32_t fd = -1;
374 char *idd;
375
376 if (enable) {
377 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/id", card);
378 if (ret < 0) {
379 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Jasmine Cha70771b62018-05-15 15:02:43 +0800380 __func__, ret, path);
jasmine cha270b7762018-03-30 15:41:33 +0800381 goto done;
382 }
383 fd = open(path, O_RDONLY);
384 if (fd < 0) {
385 ALOGE("%s: error failed to open id file %s error: %d\n",
386 __func__, path, errno);
387 goto done;
388 }
389 if (read(fd, id, sizeof(id)) < 0) {
390 ALOGE("%s: file read error", __func__);
391 goto done;
392 }
393 //replace '\n' to '\0'
394 idd = strtok(id, "\n");
395
396 if (find_sup_dev(idd)) {
397 ALOGV("%s: support device name is %s", __func__, id);
398 g_supported_dev |= SUPPORTED_USB;
399 } else
400 ALOGV("%s: device %s isn't found from %s", __func__, id, SUPPORT_DEV);
401 } else {
402 g_supported_dev &= ~SUPPORTED_USB;
403 }
404
405done:
406 if (fd >= 0) close(fd);
407}
408
409// adev_init lock held
410void audio_extn_ma_init(void *platform)
411{
412 ma_stream_type_t i = 0;
413 int ret = 0;
Jasmine Cha70771b62018-05-15 15:02:43 +0800414 char lib_path[128] = {0};
415 char mps_path[128] = {0};
Jasmine Cha6d945d22018-06-19 09:49:04 -0700416 char cnf_path[128] = {0};
Jasmine Cha70771b62018-05-15 15:02:43 +0800417 struct snd_card_split *snd_split_handle = NULL;
418 snd_split_handle = audio_extn_get_snd_card_split();
jasmine cha270b7762018-03-30 15:41:33 +0800419
420 if (platform == NULL) {
421 ALOGE("%s: platform is NULL", __func__);
422 goto error;
423 }
424
425 if (my_data) { free(my_data); }
426 my_data = calloc(1, sizeof(struct ma_platform_data));
427 if (my_data == NULL) {
428 ALOGE("%s: ma_cal alloct fail", __func__);
429 goto error;
430 }
431
432 pthread_mutex_init(&my_data->lock, NULL);
433
Jasmine Cha70771b62018-05-15 15:02:43 +0800434 my_data->platform = platform;
jasmine cha270b7762018-03-30 15:41:33 +0800435 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
436 if (ret < 0) {
437 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
438 goto error;
439 }
440
441 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
442 if (my_data->waves_handle == NULL) {
443 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
444 goto error;
445 } else {
446 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
447
448 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800449 MA_QDSP_PARAM_INIT);
jasmine cha270b7762018-03-30 15:41:33 +0800450 if (!my_data->ma_param_init) {
451 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
452 goto error;
453 }
454
Jasmine Cha70771b62018-05-15 15:02:43 +0800455 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
456 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha270b7762018-03-30 15:41:33 +0800457 if (!my_data->ma_param_deinit) {
458 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
459 goto error;
460 }
461
462 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800463 MA_QDSP_SET_LR_SWAP);
jasmine cha270b7762018-03-30 15:41:33 +0800464 if (!my_data->ma_set_lr_swap) {
465 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
466 goto error;
467 }
468
Jasmine Cha70771b62018-05-15 15:02:43 +0800469 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
470 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha270b7762018-03-30 15:41:33 +0800471 if (!my_data->ma_set_sound_mode) {
472 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
473 goto error;
474 }
475
476 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800477 MA_QDSP_SET_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800478 if (!my_data->ma_set_volume) {
479 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
480 goto error;
481 }
482
Jasmine Cha70771b62018-05-15 15:02:43 +0800483 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
484 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha270b7762018-03-30 15:41:33 +0800485 if (!my_data->ma_set_volume_table) {
486 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
487 goto error;
488 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800489
490 my_data->ma_set_param = (ma_set_param_t)dlsym(
491 my_data->waves_handle, MA_QDSP_SET_PARAM);
492 if (!my_data->ma_set_param) {
493 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
494 goto error;
495 }
jasmine cha270b7762018-03-30 15:41:33 +0800496 }
497
Jasmine Cha70771b62018-05-15 15:02:43 +0800498 /* get preset table */
499 if (snd_split_handle == NULL) {
500 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
501 PRESET_PATH, MPS_BASE_STRING);
502 } else {
503 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
504 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha270b7762018-03-30 15:41:33 +0800505 }
Jasmine Cha70771b62018-05-15 15:02:43 +0800506
Jasmine Cha6d945d22018-06-19 09:49:04 -0700507 /* get config files */
508 if (snd_split_handle == NULL) {
509 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
510 PRESET_PATH, CONFIG_BASE_STRING);
511 } else {
512 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
513 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
514 }
515
Jasmine Cha70771b62018-05-15 15:02:43 +0800516 /* check file */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700517 if (access(mps_path, R_OK) < 0) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800518 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
519 goto error;
520 } else
521 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
522
jasmine cha270b7762018-03-30 15:41:33 +0800523 /* TODO: check user preset table once the feature is enabled
524 if (access(USER_PRESET_PATH, F_OK) < 0 ){
525 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
526 goto error;
527 }
528 */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700529
530 if (access(cnf_path, R_OK) < 0) {
531 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800532 goto error;
Jasmine Cha6d945d22018-06-19 09:49:04 -0700533 } else
534 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800535
536 /* init ma parameter */
537 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800538 mps_path,
539 USER_PRESET_PATH, /* unused */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700540 cnf_path,
Jasmine Cha70771b62018-05-15 15:02:43 +0800541 &set_audio_cal)) {
jasmine cha270b7762018-03-30 15:41:33 +0800542 if (!g_ma_audio_cal_handle) {
543 ALOGE("%s: ma parameters initialize failed", __func__);
544 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
545 goto error;
546 }
547 ALOGD("%s: ma parameters initialize successful", __func__);
548 } else {
549 ALOGE("%s: ma parameters initialize failed", __func__);
550 goto error;
551 }
552
553 /* init volume table */
554 for (i = 0; i < STREAM_MAX_TYPES; i++) {
555 ma_cur_state_table[i].vol = 0.0;
556 ma_cur_state_table[i].active = false;
557 }
558
559 return;
560
561error:
562 if (my_data) { free(my_data); }
563 my_data = NULL;
564}
565
566//adev_init lock held
567void audio_extn_ma_deinit()
568{
569 if (my_data) {
570 /* deinit ma parameter */
571 if (my_data->ma_param_deinit &&
572 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
573 ALOGD("%s: ma parameters uninitialize successful", __func__);
574 else
575 ALOGD("%s: ma parameters uninitialize failed", __func__);
576
577 pthread_mutex_destroy(&my_data->lock);
578 free(my_data);
579 my_data = NULL;
580 }
581}
582
583// adev_init and adev lock held
Jasmine Cha70771b62018-05-15 15:02:43 +0800584bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
585 float vol, bool active)
jasmine cha270b7762018-03-30 15:41:33 +0800586{
587 bool ret = false;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800588 bool first_enable = false;
589 struct ma_state pr_mstate;
jasmine cha270b7762018-03-30 15:41:33 +0800590
Jasmine Chafc63f342018-06-27 10:55:06 -0700591 if (stream_type >= STREAM_MAX_TYPES ||
592 stream_type < STREAM_MIN_TYPES) {
593 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
594 return ret;
595 }
jasmine cha270b7762018-03-30 15:41:33 +0800596
597 if (!my_data) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800598 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800599 return ret;
600 }
601
Jasmine Chafc63f342018-06-27 10:55:06 -0700602 ALOGV("%s: stream[%d] vol[%f] active[%s]",
603 __func__, stream_type, vol, active ? "true" : "false");
604
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800605 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
606 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
jasmine cha270b7762018-03-30 15:41:33 +0800607
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800608 // update condition: vol or active state changes
609 if (pr_mstate.vol != vol || pr_mstate.active != active) {
610
611 pthread_mutex_lock(&my_data->lock);
612 // get active state before updating
613 first_enable = (!is_active()) && active;
614
615 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
616 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
617
618 if (first_enable) //all F -> one of T
619 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_DISABLE);
620 else if (!is_active()) // all F
621 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_ENABLE);
622
623 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800624
625 pthread_mutex_unlock(&my_data->lock);
626 }
627
628 return ret;
629}
630
Jasmine Cha70771b62018-05-15 15:02:43 +0800631void audio_extn_ma_set_device(struct audio_usecase *usecase)
jasmine cha270b7762018-03-30 15:41:33 +0800632{
633 int i = 0;
634 int u_index = -1;
635 float vol = 0;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800636 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800637
638 if (!my_data) {
639 ALOGV("%s: maxxaudio isn't initialized.", __func__);
640 return;
641 }
642
643 if (!valid_usecase(usecase)) {
644 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
645 return;
646 }
647
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800648 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800649
650 /* update audio_cal and send it */
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800651 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
652 ma_cal.common.device = usecase->stream.out->devices;
653 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
654 __func__, usecase->id, ma_cal.common.app_type,
655 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800656
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800657 pthread_mutex_lock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800658
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800659 if (is_active()) {
660 ALOGV("%s: send volume table === Start", __func__);
661 for (i = 0; i < STREAM_MAX_TYPES; i++)
662 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
663 ma_cur_state_table[i].vol,
664 ma_cur_state_table[i].active ? "T" : "F");
665 ALOGV("%s: send volume table === End", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800666
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800667 if (!ma_set_volume_table_l(&ma_cal,
668 STREAM_MAX_TYPES,
669 ma_cur_state_table))
670 ALOGE("ma_set_volume_table_l returned with error.");
671 else
672 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800673
jasmine cha270b7762018-03-30 15:41:33 +0800674 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800675 pthread_mutex_unlock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800676}
677
Jasmine Cha70771b62018-05-15 15:02:43 +0800678void audio_extn_ma_set_parameters(struct audio_device *adev,
679 struct str_parms *parms)
jasmine cha270b7762018-03-30 15:41:33 +0800680{
681 int ret;
682 bool ret_b;
683 int val;
684 char value[128];
685
686 // do LR swap and usb recognition
687 ret = str_parms_get_int(parms, "rotation", &val);
688 if (ret >= 0) {
689 switch (val) {
690 case 270:
691 ma_set_swap_l(adev, true);
692 break;
693 case 0:
694 case 90:
695 case 180:
696 ma_set_swap_l(adev, false);
697 break;
698 }
699 }
700
701 // check connect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800702 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
703 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800704 if (ret >= 0) {
705 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
706 if (audio_is_usb_out_device(device)) {
707 ret = str_parms_get_str(parms, "card", value, sizeof(value));
708 if (ret >= 0) {
709 const int card = atoi(value);
710 ma_support_usb(true, card);
711 }
712 }
713 }
714
715 // check disconnect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800716 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
717 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800718 if (ret >= 0) {
719 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
720 if (audio_is_usb_out_device(device)) {
721 ret = str_parms_get_str(parms, "card", value, sizeof(value));
722 if (ret >= 0) {
723 const int card = atoi(value);
724 ma_support_usb(false, card /*useless*/);
725 }
726 }
727 }
728}
729
730bool audio_extn_ma_supported_usb()
731{
732 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
733 return (g_supported_dev & SUPPORTED_USB) ? true : false;
734}