blob: 69c46066f623e9efedfd82ee0868e223d4f5de23 [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"
justinweng3d9b7912019-01-11 14:38:06 +080046#define MA_QDSP_IS_FEATURE_USED "maxxaudio_qdsp_is_feature_supported"
Jasmine Cha25cd74d2018-10-01 10:06:11 +080047#define MA_QDSP_SET_LR_SWAP "maxxaudio_qdsp_set_lr_swap"
justinweng3d9b7912019-01-11 14:38:06 +080048#define MA_QDSP_SET_ORIENTATION "maxxaudio_qdsp_set_orientation"
Jasmine Cha25cd74d2018-10-01 10:06:11 +080049#define MA_QDSP_SET_MODE "maxxaudio_qdsp_set_sound_mode"
50#define MA_QDSP_SET_VOL "maxxaudio_qdsp_set_volume"
51#define MA_QDSP_SET_VOLT "maxxaudio_qdsp_set_volume_table"
52#define MA_QDSP_SET_PARAM "maxxaudio_qdsp_set_parameter"
jasmine cha270b7762018-03-30 15:41:33 +080053
Robert Lee837637a2018-11-16 16:34:43 +080054#define SUPPORT_DEV "18d1:5033" // Blackbird usbid
Jasmine Cha70771b62018-05-15 15:02:43 +080055#define SUPPORTED_USB 0x01
jasmine cha270b7762018-03-30 15:41:33 +080056
Jasmine Cha25cd74d2018-10-01 10:06:11 +080057typedef unsigned int effective_scope_flag_t;
58const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0; /* RTC */
59const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1; /* ACDB */
60const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB;
61const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0;
62const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE;
jasmine cha270b7762018-03-30 15:41:33 +080063
Jasmine Cha25cd74d2018-10-01 10:06:11 +080064const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2;
65const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0;
66const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR;
67const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR;
68
69const unsigned int VALUE_AUTO = 0xFFFFFFFF;
70const unsigned int APP_TYPE_AUTO = VALUE_AUTO;
71const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO;
72const unsigned int DEVICE_AUTO = VALUE_AUTO;
73const unsigned int DEVICE_DEFAULT = DEVICE_AUTO;
74
75const unsigned int MAAP_OUTPUT_GAIN = 27;
jasmine cha270b7762018-03-30 15:41:33 +080076
77typedef enum MA_STREAM_TYPE {
Jasmine Chafc63f342018-06-27 10:55:06 -070078 STREAM_MIN_TYPES = 0,
79 STREAM_VOICE = STREAM_MIN_TYPES,
jasmine cha270b7762018-03-30 15:41:33 +080080 STREAM_SYSTEM,
81 STREAM_RING,
82 STREAM_MUSIC,
83 STREAM_ALARM,
84 STREAM_NOTIFICATION ,
85 STREAM_MAX_TYPES,
86} ma_stream_type_t;
87
88typedef enum MA_CMD {
89 MA_CMD_VOL,
90 MA_CMD_SWAP_ENABLE,
91 MA_CMD_SWAP_DISABLE,
justinweng3d9b7912019-01-11 14:38:06 +080092 MA_CMD_ROTATE_ENABLE,
93 MA_CMD_ROTATE_DISABLE,
Jasmine Cha25cd74d2018-10-01 10:06:11 +080094 MA_CMD_SOFT_MUTE_ENABLE,
95 MA_CMD_SOFT_MUTE_DISABLE,
jasmine cha270b7762018-03-30 15:41:33 +080096} ma_cmd_t;
97
Jasmine Cha25cd74d2018-10-01 10:06:11 +080098typedef struct ma_audio_cal_version {
99 unsigned int major;
100 unsigned int minor;
101} ma_audio_cal_version_t;
102
103typedef struct ma_audio_cal_common_settings {
104 unsigned int app_type;
105 unsigned int device;
106} ma_audio_cal_common_settings_t;
107
108struct ma_audio_cal_settings {
109 ma_audio_cal_version_t version;
110 ma_audio_cal_common_settings_t common;
111 effective_scope_flag_t effect_scope_flag;
112};
113
114struct ma_state {
115 float vol;
116 bool active;
117};
118
jasmine cha270b7762018-03-30 15:41:33 +0800119typedef void *ma_audio_cal_handle_t;
Jasmine Cha70771b62018-05-15 15:02:43 +0800120typedef int (*set_audio_cal_t)(const char *);
jasmine cha270b7762018-03-30 15:41:33 +0800121
Jasmine Cha70771b62018-05-15 15:02:43 +0800122typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
123 const char *, const char *, set_audio_cal_t);
jasmine cha270b7762018-03-30 15:41:33 +0800124
Jasmine Cha70771b62018-05-15 15:02:43 +0800125typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
jasmine cha270b7762018-03-30 15:41:33 +0800126
justinweng3d9b7912019-01-11 14:38:06 +0800127typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *);
128
Jasmine Cha70771b62018-05-15 15:02:43 +0800129typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
130 const struct ma_audio_cal_settings *, bool);
jasmine cha270b7762018-03-30 15:41:33 +0800131
justinweng3d9b7912019-01-11 14:38:06 +0800132typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t,
133 const struct ma_audio_cal_settings *, int);
134
Jasmine Cha70771b62018-05-15 15:02:43 +0800135typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
136 const struct ma_audio_cal_settings *,
137 unsigned int);
jasmine cha270b7762018-03-30 15:41:33 +0800138
Jasmine Cha70771b62018-05-15 15:02:43 +0800139typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
140 const struct ma_audio_cal_settings *, double);
jasmine cha270b7762018-03-30 15:41:33 +0800141
Jasmine Cha70771b62018-05-15 15:02:43 +0800142typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
143 const struct ma_audio_cal_settings *,
144 size_t, struct ma_state *);
jasmine cha270b7762018-03-30 15:41:33 +0800145
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800146typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
147 const struct ma_audio_cal_settings *,
148 unsigned int, double);
149
jasmine cha270b7762018-03-30 15:41:33 +0800150struct ma_platform_data {
151 void *waves_handle;
Jasmine Cha70771b62018-05-15 15:02:43 +0800152 void *platform;
jasmine cha270b7762018-03-30 15:41:33 +0800153 pthread_mutex_t lock;
154 ma_param_init_t ma_param_init;
155 ma_param_deinit_t ma_param_deinit;
justinweng3d9b7912019-01-11 14:38:06 +0800156 ma_is_feature_used_t ma_is_feature_used;
jasmine cha270b7762018-03-30 15:41:33 +0800157 ma_set_lr_swap_t ma_set_lr_swap;
justinweng3d9b7912019-01-11 14:38:06 +0800158 ma_set_orientation_t ma_set_orientation;
jasmine cha270b7762018-03-30 15:41:33 +0800159 ma_set_sound_mode_t ma_set_sound_mode;
160 ma_set_volume_t ma_set_volume;
161 ma_set_volume_table_t ma_set_volume_table;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800162 ma_set_param_t ma_set_param;
Robert Lee837637a2018-11-16 16:34:43 +0800163 bool speaker_lr_swap;
justinweng3d9b7912019-01-11 14:38:06 +0800164 bool orientation_used;
165 int dispaly_orientation;
jasmine cha270b7762018-03-30 15:41:33 +0800166};
167
168ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
169static uint16_t g_supported_dev = 0;
170static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
171static struct ma_platform_data *my_data = NULL;
172
Jasmine Cha70771b62018-05-15 15:02:43 +0800173static int set_audio_cal(const char *audio_cal)
jasmine cha270b7762018-03-30 15:41:33 +0800174{
175 ALOGV("set_audio_cal: %s", audio_cal);
176
Jasmine Cha70771b62018-05-15 15:02:43 +0800177 return platform_set_parameters(my_data->platform,
jasmine cha270b7762018-03-30 15:41:33 +0800178 str_parms_create_str(audio_cal));
179}
180
181static bool ma_set_lr_swap_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800182 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha270b7762018-03-30 15:41:33 +0800183{
Jasmine Cha70771b62018-05-15 15:02:43 +0800184 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
185 audio_cal_settings, swap);
jasmine cha270b7762018-03-30 15:41:33 +0800186}
187
justinweng3d9b7912019-01-11 14:38:06 +0800188static bool ma_set_orientation_l(
189 const struct ma_audio_cal_settings *audio_cal_settings, int orientation)
190{
191 return my_data->ma_set_orientation(g_ma_audio_cal_handle,
192 audio_cal_settings, orientation);
193}
194
jasmine cha270b7762018-03-30 15:41:33 +0800195static bool ma_set_sound_mode_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800196 const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
jasmine cha270b7762018-03-30 15:41:33 +0800197{
198 return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800199 audio_cal_settings, sound_mode);
jasmine cha270b7762018-03-30 15:41:33 +0800200}
201
202static bool ma_set_volume_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800203 const struct ma_audio_cal_settings *audio_cal_settings, double volume)
jasmine cha270b7762018-03-30 15:41:33 +0800204{
Jasmine Cha70771b62018-05-15 15:02:43 +0800205 return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
206 volume);
jasmine cha270b7762018-03-30 15:41:33 +0800207}
208
209static bool ma_set_volume_table_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800210 const struct ma_audio_cal_settings *audio_cal_settings,
211 size_t num_streams, struct ma_state *volume_table)
jasmine cha270b7762018-03-30 15:41:33 +0800212{
Jasmine Cha70771b62018-05-15 15:02:43 +0800213 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
214 audio_cal_settings, num_streams,
215 volume_table);
jasmine cha270b7762018-03-30 15:41:33 +0800216}
217
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800218static bool ma_set_param_l(
219 const struct ma_audio_cal_settings *audio_cal_settings,
220 unsigned int index, double value)
221{
222 return my_data->ma_set_param(g_ma_audio_cal_handle,
223 audio_cal_settings, index, value);
224}
225
jasmine cha270b7762018-03-30 15:41:33 +0800226static inline bool valid_usecase(struct audio_usecase *usecase)
227{
228 if ((usecase->type == PCM_PLAYBACK) &&
229 /* supported usecases */
230 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
231 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
232 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
233 /* support devices */
234 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
Robert Lee837637a2018-11-16 16:34:43 +0800235 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
236 (audio_is_usb_out_device(usecase->devices) &&
237 audio_extn_ma_supported_usb())))
238 /* TODO: enable A2DP when it is ready */
jasmine cha270b7762018-03-30 15:41:33 +0800239
240 return true;
241
242 ALOGV("%s: not support type %d usecase %d device %d",
243 __func__, usecase->type, usecase->id, usecase->devices);
244
245 return false;
246}
247
248// already hold lock
249static inline bool is_active()
250{
251 ma_stream_type_t i = 0;
252
253 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800254 if (ma_cur_state_table[i].active)
jasmine cha270b7762018-03-30 15:41:33 +0800255 return true;
256
257 return false;
258}
259
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800260static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
261{
262 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
263 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
264 ma_cal->common.app_type = APP_TYPE_DEFAULT;
265 ma_cal->common.device = DEVICE_DEFAULT;
266 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
267}
268
jasmine cha270b7762018-03-30 15:41:33 +0800269static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
270{
271 int i = 0;
272 bool ret = false;
273 float vol = 0;
274 struct listnode *node;
275 struct audio_usecase *usecase;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800276 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800277
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800278 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800279
280 list_for_each(node, &adev->usecase_list) {
281 usecase = node_to_item(node, struct audio_usecase, list);
282 if (valid_usecase(usecase)) {
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800283 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
284 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha270b7762018-03-30 15:41:33 +0800285 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800286 __func__, usecase->id, ma_cal.common.app_type,
287 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800288
289 switch (cmd) {
290 case MA_CMD_VOL:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800291 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Jasmine Cha70771b62018-05-15 15:02:43 +0800292 ma_cur_state_table);
jasmine cha270b7762018-03-30 15:41:33 +0800293 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800294 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800295 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800296 ALOGE("ma_set_volume_table_l returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800297
298 ALOGV("%s: send volume table === Start", __func__);
299 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha70771b62018-05-15 15:02:43 +0800300 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__,
301 i, ma_cur_state_table[i].vol,
302 ma_cur_state_table[i].active ? "T" : "F");
jasmine cha270b7762018-03-30 15:41:33 +0800303 ALOGV("%s: send volume table === End", __func__);
304 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800305
jasmine cha270b7762018-03-30 15:41:33 +0800306 case MA_CMD_SWAP_ENABLE:
Robert Lee837637a2018-11-16 16:34:43 +0800307 /* lr swap only enable for speaker path */
308 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
309 ret = ma_set_lr_swap_l(&ma_cal, true);
310 if (ret)
311 ALOGV("ma_set_lr_swap_l enable returned with success.");
312 else
313 ALOGE("ma_set_lr_swap_l enable returned with error.");
314 }
jasmine cha270b7762018-03-30 15:41:33 +0800315 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800316
jasmine cha270b7762018-03-30 15:41:33 +0800317 case MA_CMD_SWAP_DISABLE:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800318 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha270b7762018-03-30 15:41:33 +0800319 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800320 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha270b7762018-03-30 15:41:33 +0800321 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800322 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800323 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800324
justinweng3d9b7912019-01-11 14:38:06 +0800325 case MA_CMD_ROTATE_ENABLE:
326 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
327 ret = ma_set_orientation_l(&ma_cal, my_data->dispaly_orientation);
328 if (ret)
329 ALOGV("ma_set_orientation_l %d returned with success.", my_data->dispaly_orientation);
330 else
331 ALOGE("ma_set_orientation_l %d returned with error.", my_data->dispaly_orientation);
332 }
333 break;
334
335 case MA_CMD_ROTATE_DISABLE:
336 ret = ma_set_orientation_l(&ma_cal, 0);
337 if (ret)
338 ALOGV("ma_set_orientation_l 0 returned with success.");
339 else
340 ALOGE("ma_set_orientation_l 0 returned with error.");
341 break;
342
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800343 case MA_CMD_SOFT_MUTE_ENABLE:
344 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
345
346 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
347 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, -96);
348 if (!ret)
349 ALOGE("soft mute enable returned with error.");
350 break;
351
352 case MA_CMD_SOFT_MUTE_DISABLE:
353 if (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) break;
354
355 ma_cal.effect_scope_flag = EFFECTIVE_SCOPE_RTC;
356 ret = ma_set_param_l(&ma_cal, MAAP_OUTPUT_GAIN, 0);
357 if (!ret)
358 ALOGE("soft mute disable returned with error.");
359 break;
360
jasmine cha270b7762018-03-30 15:41:33 +0800361 default:
362 ALOGE("%s: unsupported cmd %d", __func__, cmd);
363 }
jasmine cha270b7762018-03-30 15:41:33 +0800364 }
365 }
jasmine cha270b7762018-03-30 15:41:33 +0800366
367 return ret;
368}
369
370static bool find_sup_dev(char *name)
371{
372 char *token;
373 const char s[2] = ",";
374 bool ret = false;
375 char sup_devs[128];
376
377 // the rule of comforming suppored dev's name
378 // 1. Both string len are equal
379 // 2. Both string content are equal
380
381 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
382 token = strtok(sup_devs, s);
383 while (token != NULL) {
384 if (strncmp(token, name, strlen(token)) == 0 &&
385 strlen(token) == strlen(name)) {
386 ALOGD("%s: support dev %s", __func__, token);
387 ret = true;
388 break;
389 }
390 token = strtok(NULL, s);
391 }
392
393 return ret;
394}
395
396static void ma_set_swap_l(struct audio_device *adev, bool enable)
397{
jasmine cha270b7762018-03-30 15:41:33 +0800398 if (enable)
399 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
400 else
401 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
402}
403
justinweng3d9b7912019-01-11 14:38:06 +0800404static void ma_set_rotation_l(struct audio_device *adev, int orientation)
405{
406 if (orientation != 0)
407 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_ENABLE);
408 else
409 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_DISABLE);
410}
411
jasmine cha270b7762018-03-30 15:41:33 +0800412static void ma_support_usb(bool enable, int card)
413{
414 char path[128];
415 char id[32];
416 int ret = 0;
417 int32_t fd = -1;
418 char *idd;
419
420 if (enable) {
Robert Lee837637a2018-11-16 16:34:43 +0800421 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha270b7762018-03-30 15:41:33 +0800422 if (ret < 0) {
423 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Jasmine Cha70771b62018-05-15 15:02:43 +0800424 __func__, ret, path);
jasmine cha270b7762018-03-30 15:41:33 +0800425 goto done;
426 }
427 fd = open(path, O_RDONLY);
428 if (fd < 0) {
429 ALOGE("%s: error failed to open id file %s error: %d\n",
430 __func__, path, errno);
431 goto done;
432 }
433 if (read(fd, id, sizeof(id)) < 0) {
434 ALOGE("%s: file read error", __func__);
435 goto done;
436 }
437 //replace '\n' to '\0'
438 idd = strtok(id, "\n");
439
440 if (find_sup_dev(idd)) {
Robert Lee837637a2018-11-16 16:34:43 +0800441 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha270b7762018-03-30 15:41:33 +0800442 g_supported_dev |= SUPPORTED_USB;
443 } else
Robert Lee837637a2018-11-16 16:34:43 +0800444 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha270b7762018-03-30 15:41:33 +0800445 } else {
446 g_supported_dev &= ~SUPPORTED_USB;
447 }
448
449done:
450 if (fd >= 0) close(fd);
451}
452
453// adev_init lock held
454void audio_extn_ma_init(void *platform)
455{
456 ma_stream_type_t i = 0;
457 int ret = 0;
Jasmine Cha70771b62018-05-15 15:02:43 +0800458 char lib_path[128] = {0};
459 char mps_path[128] = {0};
Jasmine Cha6d945d22018-06-19 09:49:04 -0700460 char cnf_path[128] = {0};
Jasmine Cha70771b62018-05-15 15:02:43 +0800461 struct snd_card_split *snd_split_handle = NULL;
462 snd_split_handle = audio_extn_get_snd_card_split();
jasmine cha270b7762018-03-30 15:41:33 +0800463
464 if (platform == NULL) {
465 ALOGE("%s: platform is NULL", __func__);
466 goto error;
467 }
468
469 if (my_data) { free(my_data); }
470 my_data = calloc(1, sizeof(struct ma_platform_data));
471 if (my_data == NULL) {
472 ALOGE("%s: ma_cal alloct fail", __func__);
473 goto error;
474 }
475
476 pthread_mutex_init(&my_data->lock, NULL);
477
Jasmine Cha70771b62018-05-15 15:02:43 +0800478 my_data->platform = platform;
jasmine cha270b7762018-03-30 15:41:33 +0800479 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
480 if (ret < 0) {
481 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
482 goto error;
483 }
484
485 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
486 if (my_data->waves_handle == NULL) {
487 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_MA_PARAM);
488 goto error;
489 } else {
490 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
491
492 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800493 MA_QDSP_PARAM_INIT);
jasmine cha270b7762018-03-30 15:41:33 +0800494 if (!my_data->ma_param_init) {
495 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
496 goto error;
497 }
498
Jasmine Cha70771b62018-05-15 15:02:43 +0800499 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
500 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha270b7762018-03-30 15:41:33 +0800501 if (!my_data->ma_param_deinit) {
502 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
503 goto error;
504 }
505
justinweng3d9b7912019-01-11 14:38:06 +0800506 my_data->ma_is_feature_used = (ma_is_feature_used_t)dlsym(my_data->waves_handle,
507 MA_QDSP_IS_FEATURE_USED);
508 if (!my_data->ma_is_feature_used) {
509 ALOGV("%s: dlsym error %s for ma_is_feature_used", __func__, dlerror());
510 }
511
512 my_data->ma_set_orientation = (ma_set_orientation_t)dlsym(my_data->waves_handle,
513 MA_QDSP_SET_ORIENTATION);
514 if (!my_data->ma_set_orientation) {
515 ALOGV("%s: dlsym error %s for ma_set_orientation", __func__, dlerror());
516 }
517
jasmine cha270b7762018-03-30 15:41:33 +0800518 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800519 MA_QDSP_SET_LR_SWAP);
jasmine cha270b7762018-03-30 15:41:33 +0800520 if (!my_data->ma_set_lr_swap) {
521 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
522 goto error;
523 }
524
Jasmine Cha70771b62018-05-15 15:02:43 +0800525 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
526 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha270b7762018-03-30 15:41:33 +0800527 if (!my_data->ma_set_sound_mode) {
528 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
529 goto error;
530 }
531
532 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800533 MA_QDSP_SET_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800534 if (!my_data->ma_set_volume) {
535 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
536 goto error;
537 }
538
Jasmine Cha70771b62018-05-15 15:02:43 +0800539 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
540 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha270b7762018-03-30 15:41:33 +0800541 if (!my_data->ma_set_volume_table) {
542 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
543 goto error;
544 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800545
546 my_data->ma_set_param = (ma_set_param_t)dlsym(
547 my_data->waves_handle, MA_QDSP_SET_PARAM);
548 if (!my_data->ma_set_param) {
549 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
550 goto error;
551 }
jasmine cha270b7762018-03-30 15:41:33 +0800552 }
553
Jasmine Cha70771b62018-05-15 15:02:43 +0800554 /* get preset table */
555 if (snd_split_handle == NULL) {
556 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
557 PRESET_PATH, MPS_BASE_STRING);
558 } else {
559 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
560 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha270b7762018-03-30 15:41:33 +0800561 }
Jasmine Cha70771b62018-05-15 15:02:43 +0800562
Jasmine Cha6d945d22018-06-19 09:49:04 -0700563 /* get config files */
564 if (snd_split_handle == NULL) {
565 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
566 PRESET_PATH, CONFIG_BASE_STRING);
567 } else {
568 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
569 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
570 }
571
Jasmine Cha70771b62018-05-15 15:02:43 +0800572 /* check file */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700573 if (access(mps_path, R_OK) < 0) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800574 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
575 goto error;
576 } else
577 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
578
jasmine cha270b7762018-03-30 15:41:33 +0800579 /* TODO: check user preset table once the feature is enabled
580 if (access(USER_PRESET_PATH, F_OK) < 0 ){
581 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
582 goto error;
583 }
584 */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700585
586 if (access(cnf_path, R_OK) < 0) {
587 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800588 goto error;
Jasmine Cha6d945d22018-06-19 09:49:04 -0700589 } else
590 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800591
592 /* init ma parameter */
593 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800594 mps_path,
595 USER_PRESET_PATH, /* unused */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700596 cnf_path,
Jasmine Cha70771b62018-05-15 15:02:43 +0800597 &set_audio_cal)) {
jasmine cha270b7762018-03-30 15:41:33 +0800598 if (!g_ma_audio_cal_handle) {
599 ALOGE("%s: ma parameters initialize failed", __func__);
600 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
601 goto error;
602 }
603 ALOGD("%s: ma parameters initialize successful", __func__);
604 } else {
605 ALOGE("%s: ma parameters initialize failed", __func__);
606 goto error;
607 }
608
609 /* init volume table */
610 for (i = 0; i < STREAM_MAX_TYPES; i++) {
611 ma_cur_state_table[i].vol = 0.0;
612 ma_cur_state_table[i].active = false;
613 }
614
Robert Lee837637a2018-11-16 16:34:43 +0800615 my_data->speaker_lr_swap = false;
justinweng3d9b7912019-01-11 14:38:06 +0800616 my_data->orientation_used = false;
617 my_data->dispaly_orientation = 0;
618
619 if (g_ma_audio_cal_handle && my_data->ma_is_feature_used) {
620 my_data->orientation_used = my_data->ma_is_feature_used(g_ma_audio_cal_handle, "SET_ORIENTATION");
621 }
Robert Lee837637a2018-11-16 16:34:43 +0800622
jasmine cha270b7762018-03-30 15:41:33 +0800623 return;
624
625error:
626 if (my_data) { free(my_data); }
627 my_data = NULL;
628}
629
630//adev_init lock held
631void audio_extn_ma_deinit()
632{
633 if (my_data) {
634 /* deinit ma parameter */
635 if (my_data->ma_param_deinit &&
636 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
637 ALOGD("%s: ma parameters uninitialize successful", __func__);
638 else
639 ALOGD("%s: ma parameters uninitialize failed", __func__);
640
641 pthread_mutex_destroy(&my_data->lock);
642 free(my_data);
643 my_data = NULL;
644 }
645}
646
647// adev_init and adev lock held
Jasmine Cha70771b62018-05-15 15:02:43 +0800648bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
649 float vol, bool active)
jasmine cha270b7762018-03-30 15:41:33 +0800650{
651 bool ret = false;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800652 bool first_enable = false;
653 struct ma_state pr_mstate;
jasmine cha270b7762018-03-30 15:41:33 +0800654
Jasmine Chafc63f342018-06-27 10:55:06 -0700655 if (stream_type >= STREAM_MAX_TYPES ||
656 stream_type < STREAM_MIN_TYPES) {
657 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
658 return ret;
659 }
jasmine cha270b7762018-03-30 15:41:33 +0800660
661 if (!my_data) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800662 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800663 return ret;
664 }
665
Jasmine Chafc63f342018-06-27 10:55:06 -0700666 ALOGV("%s: stream[%d] vol[%f] active[%s]",
667 __func__, stream_type, vol, active ? "true" : "false");
668
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800669 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
670 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
jasmine cha270b7762018-03-30 15:41:33 +0800671
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800672 // update condition: vol or active state changes
673 if (pr_mstate.vol != vol || pr_mstate.active != active) {
674
675 pthread_mutex_lock(&my_data->lock);
676 // get active state before updating
677 first_enable = (!is_active()) && active;
678
679 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
680 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
681
682 if (first_enable) //all F -> one of T
683 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_DISABLE);
684 else if (!is_active()) // all F
685 ret = check_and_send_all_audio_cal(adev, MA_CMD_SOFT_MUTE_ENABLE);
686
687 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800688
689 pthread_mutex_unlock(&my_data->lock);
690 }
691
692 return ret;
693}
694
Jasmine Cha70771b62018-05-15 15:02:43 +0800695void audio_extn_ma_set_device(struct audio_usecase *usecase)
jasmine cha270b7762018-03-30 15:41:33 +0800696{
697 int i = 0;
698 int u_index = -1;
699 float vol = 0;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800700 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800701
702 if (!my_data) {
703 ALOGV("%s: maxxaudio isn't initialized.", __func__);
704 return;
705 }
706
707 if (!valid_usecase(usecase)) {
708 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
709 return;
710 }
711
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800712 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800713
714 /* update audio_cal and send it */
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800715 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
716 ma_cal.common.device = usecase->stream.out->devices;
717 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
718 __func__, usecase->id, ma_cal.common.app_type,
719 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800720
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800721 pthread_mutex_lock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800722
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800723 if (is_active()) {
justinweng3d9b7912019-01-11 14:38:06 +0800724
725 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
726 if (my_data->orientation_used)
727 ma_set_rotation_l(usecase->stream.out->dev, my_data->dispaly_orientation);
728 else
729 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
730 } else {
731 if (my_data->orientation_used)
732 ma_set_rotation_l(usecase->stream.out->dev, 0);
733 else
734 ma_set_swap_l(usecase->stream.out->dev, false);
735 }
Robert Lee837637a2018-11-16 16:34:43 +0800736
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800737 ALOGV("%s: send volume table === Start", __func__);
738 for (i = 0; i < STREAM_MAX_TYPES; i++)
739 ALOGV("%s: stream(%d) volume(%f) active(%s)", __func__, i,
740 ma_cur_state_table[i].vol,
741 ma_cur_state_table[i].active ? "T" : "F");
742 ALOGV("%s: send volume table === End", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800743
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800744 if (!ma_set_volume_table_l(&ma_cal,
745 STREAM_MAX_TYPES,
746 ma_cur_state_table))
747 ALOGE("ma_set_volume_table_l returned with error.");
748 else
749 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800750
jasmine cha270b7762018-03-30 15:41:33 +0800751 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800752 pthread_mutex_unlock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800753}
754
Jasmine Cha70771b62018-05-15 15:02:43 +0800755void audio_extn_ma_set_parameters(struct audio_device *adev,
756 struct str_parms *parms)
jasmine cha270b7762018-03-30 15:41:33 +0800757{
758 int ret;
759 bool ret_b;
760 int val;
761 char value[128];
762
763 // do LR swap and usb recognition
764 ret = str_parms_get_int(parms, "rotation", &val);
765 if (ret >= 0) {
Robert Lee837637a2018-11-16 16:34:43 +0800766 if (!my_data) {
767 ALOGV("%s: maxxaudio isn't initialized.", __func__);
768 return;
769 }
770
jasmine cha270b7762018-03-30 15:41:33 +0800771 switch (val) {
772 case 270:
Robert Lee837637a2018-11-16 16:34:43 +0800773 my_data->speaker_lr_swap = true;
jasmine cha270b7762018-03-30 15:41:33 +0800774 break;
775 case 0:
776 case 90:
777 case 180:
Robert Lee837637a2018-11-16 16:34:43 +0800778 my_data->speaker_lr_swap = false;
jasmine cha270b7762018-03-30 15:41:33 +0800779 break;
780 }
justinweng3d9b7912019-01-11 14:38:06 +0800781 my_data->dispaly_orientation = val;
782
783 if (my_data->orientation_used)
784 ma_set_rotation_l(adev, my_data->dispaly_orientation);
785 else
786 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha270b7762018-03-30 15:41:33 +0800787 }
788
789 // check connect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800790 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
791 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800792 if (ret >= 0) {
793 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
794 if (audio_is_usb_out_device(device)) {
795 ret = str_parms_get_str(parms, "card", value, sizeof(value));
796 if (ret >= 0) {
797 const int card = atoi(value);
798 ma_support_usb(true, card);
799 }
800 }
801 }
802
803 // check disconnect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800804 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
805 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800806 if (ret >= 0) {
807 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
808 if (audio_is_usb_out_device(device)) {
809 ret = str_parms_get_str(parms, "card", value, sizeof(value));
810 if (ret >= 0) {
811 const int card = atoi(value);
812 ma_support_usb(false, card /*useless*/);
813 }
814 }
815 }
816}
817
818bool audio_extn_ma_supported_usb()
819{
820 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
821 return (g_supported_dev & SUPPORTED_USB) ? true : false;
822}