blob: cc5070c0f28679584fa55a3eb03f37d8f6fd08c7 [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
Robert Lee837637a2018-11-16 16:34:43 +080052#define SUPPORT_DEV "18d1:5033" // Blackbird usbid
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;
Robert Lee837637a2018-11-16 16:34:43 +0800152 bool speaker_lr_swap;
jasmine cha270b7762018-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
Jasmine Cha70771b62018-05-15 15:02:43 +0800160static int set_audio_cal(const char *audio_cal)
jasmine cha270b7762018-03-30 15:41:33 +0800161{
162 ALOGV("set_audio_cal: %s", audio_cal);
163
Jasmine Cha70771b62018-05-15 15:02:43 +0800164 return platform_set_parameters(my_data->platform,
jasmine cha270b7762018-03-30 15:41:33 +0800165 str_parms_create_str(audio_cal));
166}
167
168static bool ma_set_lr_swap_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800169 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha270b7762018-03-30 15:41:33 +0800170{
Jasmine Cha70771b62018-05-15 15:02:43 +0800171 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
172 audio_cal_settings, swap);
jasmine cha270b7762018-03-30 15:41:33 +0800173}
174
175static bool ma_set_sound_mode_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800176 const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
jasmine cha270b7762018-03-30 15:41:33 +0800177{
178 return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800179 audio_cal_settings, sound_mode);
jasmine cha270b7762018-03-30 15:41:33 +0800180}
181
182static bool ma_set_volume_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800183 const struct ma_audio_cal_settings *audio_cal_settings, double volume)
jasmine cha270b7762018-03-30 15:41:33 +0800184{
Jasmine Cha70771b62018-05-15 15:02:43 +0800185 return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
186 volume);
jasmine cha270b7762018-03-30 15:41:33 +0800187}
188
189static bool ma_set_volume_table_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800190 const struct ma_audio_cal_settings *audio_cal_settings,
191 size_t num_streams, struct ma_state *volume_table)
jasmine cha270b7762018-03-30 15:41:33 +0800192{
Jasmine Cha70771b62018-05-15 15:02:43 +0800193 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
194 audio_cal_settings, num_streams,
195 volume_table);
jasmine cha270b7762018-03-30 15:41:33 +0800196}
197
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800198static bool ma_set_param_l(
199 const struct ma_audio_cal_settings *audio_cal_settings,
200 unsigned int index, double value)
201{
202 return my_data->ma_set_param(g_ma_audio_cal_handle,
203 audio_cal_settings, index, value);
204}
205
jasmine cha270b7762018-03-30 15:41:33 +0800206static inline bool valid_usecase(struct audio_usecase *usecase)
207{
208 if ((usecase->type == PCM_PLAYBACK) &&
209 /* supported usecases */
210 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
211 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
212 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
213 /* support devices */
214 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
Robert Lee837637a2018-11-16 16:34:43 +0800215 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
216 (audio_is_usb_out_device(usecase->devices) &&
217 audio_extn_ma_supported_usb())))
218 /* TODO: enable A2DP when it is ready */
jasmine cha270b7762018-03-30 15:41:33 +0800219
220 return true;
221
222 ALOGV("%s: not support type %d usecase %d device %d",
223 __func__, usecase->type, usecase->id, usecase->devices);
224
225 return false;
226}
227
228// already hold lock
229static inline bool is_active()
230{
231 ma_stream_type_t i = 0;
232
233 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800234 if (ma_cur_state_table[i].active)
jasmine cha270b7762018-03-30 15:41:33 +0800235 return true;
236
237 return false;
238}
239
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800240static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
241{
242 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
243 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
244 ma_cal->common.app_type = APP_TYPE_DEFAULT;
245 ma_cal->common.device = DEVICE_DEFAULT;
246 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
247}
248
jasmine cha270b7762018-03-30 15:41:33 +0800249static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
250{
251 int i = 0;
252 bool ret = false;
253 float vol = 0;
254 struct listnode *node;
255 struct audio_usecase *usecase;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800256 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800257
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800258 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800259
260 list_for_each(node, &adev->usecase_list) {
261 usecase = node_to_item(node, struct audio_usecase, list);
262 if (valid_usecase(usecase)) {
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800263 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
264 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha270b7762018-03-30 15:41:33 +0800265 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800266 __func__, usecase->id, ma_cal.common.app_type,
267 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800268
269 switch (cmd) {
270 case MA_CMD_VOL:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800271 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Jasmine Cha70771b62018-05-15 15:02:43 +0800272 ma_cur_state_table);
jasmine cha270b7762018-03-30 15:41:33 +0800273 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800274 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800275 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800276 ALOGE("ma_set_volume_table_l returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800277
278 ALOGV("%s: send volume table === Start", __func__);
279 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha70771b62018-05-15 15:02:43 +0800280 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__,
281 i, ma_cur_state_table[i].vol,
282 ma_cur_state_table[i].active ? "T" : "F");
jasmine cha270b7762018-03-30 15:41:33 +0800283 ALOGV("%s: send volume table === End", __func__);
284 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800285
jasmine cha270b7762018-03-30 15:41:33 +0800286 case MA_CMD_SWAP_ENABLE:
Robert Lee837637a2018-11-16 16:34:43 +0800287 /* lr swap only enable for speaker path */
288 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
289 ret = ma_set_lr_swap_l(&ma_cal, true);
290 if (ret)
291 ALOGV("ma_set_lr_swap_l enable returned with success.");
292 else
293 ALOGE("ma_set_lr_swap_l enable returned with error.");
294 }
jasmine cha270b7762018-03-30 15:41:33 +0800295 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800296
jasmine cha270b7762018-03-30 15:41:33 +0800297 case MA_CMD_SWAP_DISABLE:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800298 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha270b7762018-03-30 15:41:33 +0800299 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800300 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha270b7762018-03-30 15:41:33 +0800301 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800302 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800303 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800304
305 case MA_CMD_SOFT_MUTE_ENABLE:
306 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
307
308 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
309 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, -96);
310 if (!ret)
311 ALOGE("soft mute enable returned with error.");
312 break;
313
314 case MA_CMD_SOFT_MUTE_DISABLE:
315 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
316
317 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
318 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, 0);
319 if (!ret)
320 ALOGE("soft mute disable returned with error.");
321 break;
322
jasmine cha270b7762018-03-30 15:41:33 +0800323 default:
324 ALOGE("%s: unsupported cmd %d", __func__, cmd);
325 }
jasmine cha270b7762018-03-30 15:41:33 +0800326 }
327 }
jasmine cha270b7762018-03-30 15:41:33 +0800328
329 return ret;
330}
331
332static bool find_sup_dev(char *name)
333{
334 char *token;
335 const char s[2] = ",";
336 bool ret = false;
337 char sup_devs[128];
338
339 // the rule of comforming suppored dev's name
340 // 1. Both string len are equal
341 // 2. Both string content are equal
342
343 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
344 token = strtok(sup_devs, s);
345 while (token != NULL) {
346 if (strncmp(token, name, strlen(token)) == 0 &&
347 strlen(token) == strlen(name)) {
348 ALOGD("%s: support dev %s", __func__, token);
349 ret = true;
350 break;
351 }
352 token = strtok(NULL, s);
353 }
354
355 return ret;
356}
357
358static void ma_set_swap_l(struct audio_device *adev, bool enable)
359{
jasmine cha270b7762018-03-30 15:41:33 +0800360 if (enable)
361 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
362 else
363 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
364}
365
366static void ma_support_usb(bool enable, int card)
367{
368 char path[128];
369 char id[32];
370 int ret = 0;
371 int32_t fd = -1;
372 char *idd;
373
374 if (enable) {
Robert Lee837637a2018-11-16 16:34:43 +0800375 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha270b7762018-03-30 15:41:33 +0800376 if (ret < 0) {
377 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Jasmine Cha70771b62018-05-15 15:02:43 +0800378 __func__, ret, path);
jasmine cha270b7762018-03-30 15:41:33 +0800379 goto done;
380 }
381 fd = open(path, O_RDONLY);
382 if (fd < 0) {
383 ALOGE("%s: error failed to open id file %s error: %d\n",
384 __func__, path, errno);
385 goto done;
386 }
387 if (read(fd, id, sizeof(id)) < 0) {
388 ALOGE("%s: file read error", __func__);
389 goto done;
390 }
391 //replace '\n' to '\0'
392 idd = strtok(id, "\n");
393
394 if (find_sup_dev(idd)) {
Robert Lee837637a2018-11-16 16:34:43 +0800395 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha270b7762018-03-30 15:41:33 +0800396 g_supported_dev |= SUPPORTED_USB;
397 } else
Robert Lee837637a2018-11-16 16:34:43 +0800398 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha270b7762018-03-30 15:41:33 +0800399 } else {
400 g_supported_dev &= ~SUPPORTED_USB;
401 }
402
403done:
404 if (fd >= 0) close(fd);
405}
406
407// adev_init lock held
408void audio_extn_ma_init(void *platform)
409{
410 ma_stream_type_t i = 0;
411 int ret = 0;
Jasmine Cha70771b62018-05-15 15:02:43 +0800412 char lib_path[128] = {0};
413 char mps_path[128] = {0};
Jasmine Cha6d945d22018-06-19 09:49:04 -0700414 char cnf_path[128] = {0};
Jasmine Cha70771b62018-05-15 15:02:43 +0800415 struct snd_card_split *snd_split_handle = NULL;
416 snd_split_handle = audio_extn_get_snd_card_split();
jasmine cha270b7762018-03-30 15:41:33 +0800417
418 if (platform == NULL) {
419 ALOGE("%s: platform is NULL", __func__);
420 goto error;
421 }
422
423 if (my_data) { free(my_data); }
424 my_data = calloc(1, sizeof(struct ma_platform_data));
425 if (my_data == NULL) {
426 ALOGE("%s: ma_cal alloct fail", __func__);
427 goto error;
428 }
429
430 pthread_mutex_init(&my_data->lock, NULL);
431
Jasmine Cha70771b62018-05-15 15:02:43 +0800432 my_data->platform = platform;
jasmine cha270b7762018-03-30 15:41:33 +0800433 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
434 if (ret < 0) {
435 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
436 goto error;
437 }
438
439 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
440 if (my_data->waves_handle == NULL) {
441 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
442 goto error;
443 } else {
444 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
445
446 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800447 MA_QDSP_PARAM_INIT);
jasmine cha270b7762018-03-30 15:41:33 +0800448 if (!my_data->ma_param_init) {
449 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
450 goto error;
451 }
452
Jasmine Cha70771b62018-05-15 15:02:43 +0800453 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
454 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha270b7762018-03-30 15:41:33 +0800455 if (!my_data->ma_param_deinit) {
456 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
457 goto error;
458 }
459
460 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800461 MA_QDSP_SET_LR_SWAP);
jasmine cha270b7762018-03-30 15:41:33 +0800462 if (!my_data->ma_set_lr_swap) {
463 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
464 goto error;
465 }
466
Jasmine Cha70771b62018-05-15 15:02:43 +0800467 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
468 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha270b7762018-03-30 15:41:33 +0800469 if (!my_data->ma_set_sound_mode) {
470 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
471 goto error;
472 }
473
474 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800475 MA_QDSP_SET_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800476 if (!my_data->ma_set_volume) {
477 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
478 goto error;
479 }
480
Jasmine Cha70771b62018-05-15 15:02:43 +0800481 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
482 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha270b7762018-03-30 15:41:33 +0800483 if (!my_data->ma_set_volume_table) {
484 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
485 goto error;
486 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800487
488 my_data->ma_set_param = (ma_set_param_t)dlsym(
489 my_data->waves_handle, MA_QDSP_SET_PARAM);
490 if (!my_data->ma_set_param) {
491 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
492 goto error;
493 }
jasmine cha270b7762018-03-30 15:41:33 +0800494 }
495
Jasmine Cha70771b62018-05-15 15:02:43 +0800496 /* get preset table */
497 if (snd_split_handle == NULL) {
498 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
499 PRESET_PATH, MPS_BASE_STRING);
500 } else {
501 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
502 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha270b7762018-03-30 15:41:33 +0800503 }
Jasmine Cha70771b62018-05-15 15:02:43 +0800504
Jasmine Cha6d945d22018-06-19 09:49:04 -0700505 /* get config files */
506 if (snd_split_handle == NULL) {
507 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
508 PRESET_PATH, CONFIG_BASE_STRING);
509 } else {
510 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
511 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
512 }
513
Jasmine Cha70771b62018-05-15 15:02:43 +0800514 /* check file */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700515 if (access(mps_path, R_OK) < 0) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800516 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
517 goto error;
518 } else
519 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
520
jasmine cha270b7762018-03-30 15:41:33 +0800521 /* TODO: check user preset table once the feature is enabled
522 if (access(USER_PRESET_PATH, F_OK) < 0 ){
523 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
524 goto error;
525 }
526 */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700527
528 if (access(cnf_path, R_OK) < 0) {
529 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800530 goto error;
Jasmine Cha6d945d22018-06-19 09:49:04 -0700531 } else
532 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800533
534 /* init ma parameter */
535 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800536 mps_path,
537 USER_PRESET_PATH, /* unused */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700538 cnf_path,
Jasmine Cha70771b62018-05-15 15:02:43 +0800539 &set_audio_cal)) {
jasmine cha270b7762018-03-30 15:41:33 +0800540 if (!g_ma_audio_cal_handle) {
541 ALOGE("%s: ma parameters initialize failed", __func__);
542 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
543 goto error;
544 }
545 ALOGD("%s: ma parameters initialize successful", __func__);
546 } else {
547 ALOGE("%s: ma parameters initialize failed", __func__);
548 goto error;
549 }
550
551 /* init volume table */
552 for (i = 0; i < STREAM_MAX_TYPES; i++) {
553 ma_cur_state_table[i].vol = 0.0;
554 ma_cur_state_table[i].active = false;
555 }
556
Robert Lee837637a2018-11-16 16:34:43 +0800557 my_data->speaker_lr_swap = false;
558
jasmine cha270b7762018-03-30 15:41:33 +0800559 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()) {
Robert Lee837637a2018-11-16 16:34:43 +0800660 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER)
661 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
662 else
663 ma_set_swap_l(usecase->stream.out->dev, false);
664
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800665 ALOGV("%s: send volume table === Start", __func__);
666 for (i = 0; i < STREAM_MAX_TYPES; i++)
667 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
668 ma_cur_state_table[i].vol,
669 ma_cur_state_table[i].active ? "T" : "F");
670 ALOGV("%s: send volume table === End", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800671
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800672 if (!ma_set_volume_table_l(&ma_cal,
673 STREAM_MAX_TYPES,
674 ma_cur_state_table))
675 ALOGE("ma_set_volume_table_l returned with error.");
676 else
677 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800678
jasmine cha270b7762018-03-30 15:41:33 +0800679 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800680 pthread_mutex_unlock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800681}
682
Jasmine Cha70771b62018-05-15 15:02:43 +0800683void audio_extn_ma_set_parameters(struct audio_device *adev,
684 struct str_parms *parms)
jasmine cha270b7762018-03-30 15:41:33 +0800685{
686 int ret;
687 bool ret_b;
688 int val;
689 char value[128];
690
691 // do LR swap and usb recognition
692 ret = str_parms_get_int(parms, "rotation", &val);
693 if (ret >= 0) {
Robert Lee837637a2018-11-16 16:34:43 +0800694 if (!my_data) {
695 ALOGV("%s: maxxaudio isn't initialized.", __func__);
696 return;
697 }
698
jasmine cha270b7762018-03-30 15:41:33 +0800699 switch (val) {
700 case 270:
Robert Lee837637a2018-11-16 16:34:43 +0800701 my_data->speaker_lr_swap = true;
jasmine cha270b7762018-03-30 15:41:33 +0800702 break;
703 case 0:
704 case 90:
705 case 180:
Robert Lee837637a2018-11-16 16:34:43 +0800706 my_data->speaker_lr_swap = false;
jasmine cha270b7762018-03-30 15:41:33 +0800707 break;
708 }
Robert Lee837637a2018-11-16 16:34:43 +0800709 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha270b7762018-03-30 15:41:33 +0800710 }
711
712 // check connect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800713 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
714 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800715 if (ret >= 0) {
716 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
717 if (audio_is_usb_out_device(device)) {
718 ret = str_parms_get_str(parms, "card", value, sizeof(value));
719 if (ret >= 0) {
720 const int card = atoi(value);
721 ma_support_usb(true, card);
722 }
723 }
724 }
725
726 // check disconnect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800727 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
728 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800729 if (ret >= 0) {
730 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
731 if (audio_is_usb_out_device(device)) {
732 ret = str_parms_get_str(parms, "card", value, sizeof(value));
733 if (ret >= 0) {
734 const int card = atoi(value);
735 ma_support_usb(false, card /*useless*/);
736 }
737 }
738 }
739}
740
741bool audio_extn_ma_supported_usb()
742{
743 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
744 return (g_supported_dev & SUPPORTED_USB) ? true : false;
745}