blob: 883048656a1020bc89e79dab0cfe4935f2297567 [file] [log] [blame]
jasmine cha75fa6f02018-03-30 15:41:33 +08001/*
Arun Mirpurid750ac52019-04-12 18:33:55 -07002 * Copyright (C) 2018-2019 The Android Open Source Project
jasmine cha75fa6f02018-03-30 15:41:33 +08003 *
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;
Arun Mirpurid750ac52019-04-12 18:33:55 -0700159// --- external function dependency ---
160fp_platform_set_parameters_t fp_platform_set_parameters;
161fp_audio_extn_get_snd_card_split_t fp_audio_extn_get_snd_card_split;
jasmine cha75fa6f02018-03-30 15:41:33 +0800162
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800163static int set_audio_cal(const char *audio_cal)
jasmine cha75fa6f02018-03-30 15:41:33 +0800164{
165 ALOGV("set_audio_cal: %s", audio_cal);
166
Arun Mirpurid750ac52019-04-12 18:33:55 -0700167 return fp_platform_set_parameters(my_data->platform,
jasmine cha75fa6f02018-03-30 15:41:33 +0800168 str_parms_create_str(audio_cal));
169}
170
171static bool ma_set_lr_swap_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800172 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha75fa6f02018-03-30 15:41:33 +0800173{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800174 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
175 audio_cal_settings, swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800176}
177
jasmine cha75fa6f02018-03-30 15:41:33 +0800178static bool ma_set_volume_table_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800179 const struct ma_audio_cal_settings *audio_cal_settings,
180 size_t num_streams, struct ma_state *volume_table)
jasmine cha75fa6f02018-03-30 15:41:33 +0800181{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800182 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
183 audio_cal_settings, num_streams,
184 volume_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800185}
186
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800187static bool ma_set_param_l(
188 const struct ma_audio_cal_settings *audio_cal_settings,
189 unsigned int index, double value)
190{
191 return my_data->ma_set_param(g_ma_audio_cal_handle,
192 audio_cal_settings, index, value);
193}
194
jasmine cha75fa6f02018-03-30 15:41:33 +0800195static inline bool valid_usecase(struct audio_usecase *usecase)
196{
197 if ((usecase->type == PCM_PLAYBACK) &&
198 /* supported usecases */
199 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
200 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
201 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
202 /* support devices */
203 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
204 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800205 (audio_is_usb_out_device(usecase->devices) &&
Arun Mirpurid750ac52019-04-12 18:33:55 -0700206 ma_supported_usb())))
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800207 /* TODO: enable A2DP when it is ready */
jasmine cha75fa6f02018-03-30 15:41:33 +0800208
209 return true;
210
211 ALOGV("%s: not support type %d usecase %d device %d",
212 __func__, usecase->type, usecase->id, usecase->devices);
213
214 return false;
215}
216
217// already hold lock
218static inline bool is_active()
219{
220 ma_stream_type_t i = 0;
221
222 for (i = 0; i < STREAM_MAX_TYPES; i++)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800223 if (ma_cur_state_table[i].active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800224 return true;
225
226 return false;
227}
228
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800229static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
230{
231 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
232 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
233 ma_cal->common.app_type = APP_TYPE_DEFAULT;
234 ma_cal->common.device = DEVICE_DEFAULT;
235 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
236}
237
jasmine cha75fa6f02018-03-30 15:41:33 +0800238static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
239{
240 int i = 0;
241 bool ret = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800242 struct listnode *node;
243 struct audio_usecase *usecase;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800244 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800245
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800246 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800247
248 list_for_each(node, &adev->usecase_list) {
249 usecase = node_to_item(node, struct audio_usecase, list);
250 if (valid_usecase(usecase)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800251 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
252 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha75fa6f02018-03-30 15:41:33 +0800253 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800254 __func__, usecase->id, ma_cal.common.app_type,
255 ma_cal.common.device);
jasmine cha75fa6f02018-03-30 15:41:33 +0800256
257 switch (cmd) {
258 case MA_CMD_VOL:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800259 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800260 ma_cur_state_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800261 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800262 ALOGV("ma_set_volume_table_l success");
jasmine cha75fa6f02018-03-30 15:41:33 +0800263 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800264 ALOGE("ma_set_volume_table_l returned with error.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800265
266 ALOGV("%s: send volume table === Start", __func__);
267 for (i = 0; i < STREAM_MAX_TYPES; i++)
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800268 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__,
269 i, ma_cur_state_table[i].vol,
270 ma_cur_state_table[i].active ? "T" : "F");
jasmine cha75fa6f02018-03-30 15:41:33 +0800271 ALOGV("%s: send volume table === End", __func__);
272 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800273
jasmine cha75fa6f02018-03-30 15:41:33 +0800274 case MA_CMD_SWAP_ENABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800275 /* lr swap only enable for speaker path */
276 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
277 ret = ma_set_lr_swap_l(&ma_cal, true);
278 if (ret)
279 ALOGV("ma_set_lr_swap_l enable returned with success.");
280 else
281 ALOGE("ma_set_lr_swap_l enable returned with error.");
282 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800283 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800284
jasmine cha75fa6f02018-03-30 15:41:33 +0800285 case MA_CMD_SWAP_DISABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800286 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha75fa6f02018-03-30 15:41:33 +0800287 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800288 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800289 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800290 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800291 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800292
293 case MA_CMD_SOFT_MUTE_ENABLE:
294 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
295
296 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
297 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, -96);
298 if (!ret)
299 ALOGE("soft mute enable returned with error.");
300 break;
301
302 case MA_CMD_SOFT_MUTE_DISABLE:
303 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
304
305 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
306 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, 0);
307 if (!ret)
308 ALOGE("soft mute disable returned with error.");
309 break;
310
jasmine cha75fa6f02018-03-30 15:41:33 +0800311 default:
312 ALOGE("%s: unsupported cmd %d", __func__, cmd);
313 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800314 }
315 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800316
317 return ret;
318}
319
320static bool find_sup_dev(char *name)
321{
322 char *token;
323 const char s[2] = ",";
324 bool ret = false;
325 char sup_devs[128];
326
327 // the rule of comforming suppored dev's name
328 // 1. Both string len are equal
329 // 2. Both string content are equal
330
331 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
332 token = strtok(sup_devs, s);
333 while (token != NULL) {
334 if (strncmp(token, name, strlen(token)) == 0 &&
335 strlen(token) == strlen(name)) {
336 ALOGD("%s: support dev %s", __func__, token);
337 ret = true;
338 break;
339 }
340 token = strtok(NULL, s);
341 }
342
343 return ret;
344}
345
346static void ma_set_swap_l(struct audio_device *adev, bool enable)
347{
jasmine cha75fa6f02018-03-30 15:41:33 +0800348 if (enable)
349 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
350 else
351 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
352}
353
354static void ma_support_usb(bool enable, int card)
355{
356 char path[128];
357 char id[32];
358 int ret = 0;
359 int32_t fd = -1;
360 char *idd;
361
362 if (enable) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800363 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha75fa6f02018-03-30 15:41:33 +0800364 if (ret < 0) {
365 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800366 __func__, ret, path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800367 goto done;
368 }
369 fd = open(path, O_RDONLY);
370 if (fd < 0) {
371 ALOGE("%s: error failed to open id file %s error: %d\n",
372 __func__, path, errno);
373 goto done;
374 }
375 if (read(fd, id, sizeof(id)) < 0) {
376 ALOGE("%s: file read error", __func__);
377 goto done;
378 }
379 //replace '\n' to '\0'
380 idd = strtok(id, "\n");
381
382 if (find_sup_dev(idd)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800383 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha75fa6f02018-03-30 15:41:33 +0800384 g_supported_dev |= SUPPORTED_USB;
385 } else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800386 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha75fa6f02018-03-30 15:41:33 +0800387 } else {
388 g_supported_dev &= ~SUPPORTED_USB;
389 }
390
391done:
392 if (fd >= 0) close(fd);
393}
394
395// adev_init lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700396void ma_init(void *platform, maxx_audio_init_config_t init_config)
jasmine cha75fa6f02018-03-30 15:41:33 +0800397{
398 ma_stream_type_t i = 0;
399 int ret = 0;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800400 char lib_path[128] = {0};
401 char mps_path[128] = {0};
402 char cnf_path[128] = {0};
403 struct snd_card_split *snd_split_handle = NULL;
Arun Mirpurid750ac52019-04-12 18:33:55 -0700404
405 fp_platform_set_parameters = init_config.fp_platform_set_parameters;
406 fp_audio_extn_get_snd_card_split = init_config.fp_audio_extn_get_snd_card_split;
407
408 snd_split_handle = fp_audio_extn_get_snd_card_split();
jasmine cha75fa6f02018-03-30 15:41:33 +0800409
410 if (platform == NULL) {
411 ALOGE("%s: platform is NULL", __func__);
412 goto error;
413 }
414
415 if (my_data) { free(my_data); }
416 my_data = calloc(1, sizeof(struct ma_platform_data));
417 if (my_data == NULL) {
418 ALOGE("%s: ma_cal alloct fail", __func__);
419 goto error;
420 }
421
422 pthread_mutex_init(&my_data->lock, NULL);
423
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800424 my_data->platform = platform;
jasmine cha75fa6f02018-03-30 15:41:33 +0800425 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
426 if (ret < 0) {
427 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
428 goto error;
429 }
430
431 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
432 if (my_data->waves_handle == NULL) {
433 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
434 goto error;
435 } else {
436 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
437
438 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800439 MA_QDSP_PARAM_INIT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800440 if (!my_data->ma_param_init) {
441 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
442 goto error;
443 }
444
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800445 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
446 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800447 if (!my_data->ma_param_deinit) {
448 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
449 goto error;
450 }
451
452 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800453 MA_QDSP_SET_LR_SWAP);
jasmine cha75fa6f02018-03-30 15:41:33 +0800454 if (!my_data->ma_set_lr_swap) {
455 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
456 goto error;
457 }
458
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800459 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
460 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha75fa6f02018-03-30 15:41:33 +0800461 if (!my_data->ma_set_sound_mode) {
462 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
463 goto error;
464 }
465
466 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800467 MA_QDSP_SET_VOL);
jasmine cha75fa6f02018-03-30 15:41:33 +0800468 if (!my_data->ma_set_volume) {
469 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
470 goto error;
471 }
472
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800473 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
474 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha75fa6f02018-03-30 15:41:33 +0800475 if (!my_data->ma_set_volume_table) {
476 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
477 goto error;
478 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800479
480 my_data->ma_set_param = (ma_set_param_t)dlsym(
481 my_data->waves_handle, MA_QDSP_SET_PARAM);
482 if (!my_data->ma_set_param) {
483 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
484 goto error;
485 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800486 }
487
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800488 /* get preset table */
489 if (snd_split_handle == NULL) {
490 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
491 PRESET_PATH, MPS_BASE_STRING);
492 } else {
493 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
494 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha75fa6f02018-03-30 15:41:33 +0800495 }
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800496
497 /* get config files */
498 if (snd_split_handle == NULL) {
499 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
500 PRESET_PATH, CONFIG_BASE_STRING);
501 } else {
502 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
503 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
504 }
505
506 /* check file */
507 if (access(mps_path, R_OK) < 0) {
508 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
509 goto error;
510 } else
511 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
512
jasmine cha75fa6f02018-03-30 15:41:33 +0800513 /* TODO: check user preset table once the feature is enabled
514 if (access(USER_PRESET_PATH, F_OK) < 0 ){
515 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
516 goto error;
517 }
518 */
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800519
520 if (access(cnf_path, R_OK) < 0) {
521 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800522 goto error;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800523 } else
524 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800525
526 /* init ma parameter */
527 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800528 mps_path,
529 USER_PRESET_PATH, /* unused */
530 cnf_path,
531 &set_audio_cal)) {
jasmine cha75fa6f02018-03-30 15:41:33 +0800532 if (!g_ma_audio_cal_handle) {
533 ALOGE("%s: ma parameters initialize failed", __func__);
534 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
535 goto error;
536 }
537 ALOGD("%s: ma parameters initialize successful", __func__);
538 } else {
539 ALOGE("%s: ma parameters initialize failed", __func__);
540 goto error;
541 }
542
543 /* init volume table */
544 for (i = 0; i < STREAM_MAX_TYPES; i++) {
545 ma_cur_state_table[i].vol = 0.0;
546 ma_cur_state_table[i].active = false;
547 }
548
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800549 my_data->speaker_lr_swap = false;
550
jasmine cha75fa6f02018-03-30 15:41:33 +0800551 return;
552
553error:
554 if (my_data) { free(my_data); }
555 my_data = NULL;
556}
557
558//adev_init lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700559void ma_deinit()
jasmine cha75fa6f02018-03-30 15:41:33 +0800560{
561 if (my_data) {
562 /* deinit ma parameter */
563 if (my_data->ma_param_deinit &&
564 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
565 ALOGD("%s: ma parameters uninitialize successful", __func__);
566 else
567 ALOGD("%s: ma parameters uninitialize failed", __func__);
568
569 pthread_mutex_destroy(&my_data->lock);
570 free(my_data);
571 my_data = NULL;
572 }
573}
574
575// adev_init and adev lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700576bool ma_set_state(struct audio_device *adev, int stream_type,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800577 float vol, bool active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800578{
579 bool ret = false;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800580 bool first_enable = false;
581 struct ma_state pr_mstate;
jasmine cha75fa6f02018-03-30 15:41:33 +0800582
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800583 if (stream_type >= STREAM_MAX_TYPES ||
584 stream_type < STREAM_MIN_TYPES) {
585 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
586 return ret;
587 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800588
589 if (!my_data) {
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800590 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha75fa6f02018-03-30 15:41:33 +0800591 return ret;
592 }
593
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800594 ALOGV("%s: stream[%d] vol[%f] active[%s]",
595 __func__, stream_type, vol, active ? "true" : "false");
jasmine cha75fa6f02018-03-30 15:41:33 +0800596
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800597 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
598 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
599
600 // update condition: vol or active state changes
601 if (pr_mstate.vol != vol || pr_mstate.active != active) {
602
603 pthread_mutex_lock(&my_data->lock);
604 // get active state before updating
605 first_enable = (!is_active()) && active;
606
607 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
608 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
609
610 if (first_enable) //all F -> one of T
611 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_DISABLE);
612 else if (!is_active()) // all F
613 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_ENABLE);
614
615 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha75fa6f02018-03-30 15:41:33 +0800616
617 pthread_mutex_unlock(&my_data->lock);
618 }
619
620 return ret;
621}
622
Arun Mirpurid750ac52019-04-12 18:33:55 -0700623void ma_set_device(struct audio_usecase *usecase)
jasmine cha75fa6f02018-03-30 15:41:33 +0800624{
625 int i = 0;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800626 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800627
628 if (!my_data) {
629 ALOGV("%s: maxxaudio isn't initialized.", __func__);
630 return;
631 }
632
633 if (!valid_usecase(usecase)) {
634 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
635 return;
636 }
637
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800638 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800639
640 /* update audio_cal and send it */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800641 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
642 ma_cal.common.device = usecase->stream.out->devices;
643 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
644 __func__, usecase->id, ma_cal.common.app_type,
645 ma_cal.common.device);
jasmine cha75fa6f02018-03-30 15:41:33 +0800646
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800647 pthread_mutex_lock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800648
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800649 if (is_active()) {
650 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER)
651 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
652 else
653 ma_set_swap_l(usecase->stream.out->dev, false);
jasmine cha75fa6f02018-03-30 15:41:33 +0800654
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800655 ALOGV("%s: send volume table === Start", __func__);
656 for (i = 0; i < STREAM_MAX_TYPES; i++)
657 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
658 ma_cur_state_table[i].vol,
659 ma_cur_state_table[i].active ? "T" : "F");
660 ALOGV("%s: send volume table === End", __func__);
jasmine cha75fa6f02018-03-30 15:41:33 +0800661
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800662 if (!ma_set_volume_table_l(&ma_cal,
663 STREAM_MAX_TYPES,
664 ma_cur_state_table))
665 ALOGE("ma_set_volume_table_l returned with error.");
666 else
667 ALOGV("ma_set_volume_table_l success");
668
jasmine cha75fa6f02018-03-30 15:41:33 +0800669 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800670 pthread_mutex_unlock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800671}
672
Arun Mirpurid750ac52019-04-12 18:33:55 -0700673void ma_set_parameters(struct audio_device *adev,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800674 struct str_parms *parms)
jasmine cha75fa6f02018-03-30 15:41:33 +0800675{
676 int ret;
jasmine cha75fa6f02018-03-30 15:41:33 +0800677 int val;
678 char value[128];
679
680 // do LR swap and usb recognition
681 ret = str_parms_get_int(parms, "rotation", &val);
682 if (ret >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800683 if (!my_data) {
684 ALOGV("%s: maxxaudio isn't initialized.", __func__);
685 return;
686 }
687
jasmine cha75fa6f02018-03-30 15:41:33 +0800688 switch (val) {
689 case 270:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800690 my_data->speaker_lr_swap = true;
jasmine cha75fa6f02018-03-30 15:41:33 +0800691 break;
692 case 0:
693 case 90:
694 case 180:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800695 my_data->speaker_lr_swap = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800696 break;
697 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800698 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800699 }
700
701 // check connect status
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800702 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
703 sizeof(value));
jasmine cha75fa6f02018-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
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800716 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
717 sizeof(value));
jasmine cha75fa6f02018-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
Arun Mirpurid750ac52019-04-12 18:33:55 -0700730bool ma_supported_usb()
jasmine cha75fa6f02018-03-30 15:41:33 +0800731{
732 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
733 return (g_supported_dev & SUPPORTED_USB) ? true : false;
734}