blob: 50792333df93a0c2afa6ecbe196bddf490a8196b [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 cha270b7762018-03-30 15:41:33 +080094} ma_cmd_t;
95
Jasmine Cha25cd74d2018-10-01 10:06:11 +080096typedef struct ma_audio_cal_version {
97 unsigned int major;
98 unsigned int minor;
99} ma_audio_cal_version_t;
100
101typedef struct ma_audio_cal_common_settings {
102 unsigned int app_type;
103 unsigned int device;
104} ma_audio_cal_common_settings_t;
105
106struct ma_audio_cal_settings {
107 ma_audio_cal_version_t version;
108 ma_audio_cal_common_settings_t common;
109 effective_scope_flag_t effect_scope_flag;
110};
111
112struct ma_state {
113 float vol;
114 bool active;
115};
116
jasmine cha270b7762018-03-30 15:41:33 +0800117typedef void *ma_audio_cal_handle_t;
Jasmine Cha70771b62018-05-15 15:02:43 +0800118typedef int (*set_audio_cal_t)(const char *);
jasmine cha270b7762018-03-30 15:41:33 +0800119
Jasmine Cha70771b62018-05-15 15:02:43 +0800120typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
121 const char *, const char *, set_audio_cal_t);
jasmine cha270b7762018-03-30 15:41:33 +0800122
Jasmine Cha70771b62018-05-15 15:02:43 +0800123typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
jasmine cha270b7762018-03-30 15:41:33 +0800124
justinweng3d9b7912019-01-11 14:38:06 +0800125typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *);
126
Jasmine Cha70771b62018-05-15 15:02:43 +0800127typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
128 const struct ma_audio_cal_settings *, bool);
jasmine cha270b7762018-03-30 15:41:33 +0800129
justinweng3d9b7912019-01-11 14:38:06 +0800130typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t,
131 const struct ma_audio_cal_settings *, int);
132
Jasmine Cha70771b62018-05-15 15:02:43 +0800133typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
134 const struct ma_audio_cal_settings *,
135 unsigned int);
jasmine cha270b7762018-03-30 15:41:33 +0800136
Jasmine Cha70771b62018-05-15 15:02:43 +0800137typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
138 const struct ma_audio_cal_settings *, double);
jasmine cha270b7762018-03-30 15:41:33 +0800139
Jasmine Cha70771b62018-05-15 15:02:43 +0800140typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
141 const struct ma_audio_cal_settings *,
142 size_t, struct ma_state *);
jasmine cha270b7762018-03-30 15:41:33 +0800143
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800144typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
145 const struct ma_audio_cal_settings *,
146 unsigned int, double);
147
jasmine cha270b7762018-03-30 15:41:33 +0800148struct ma_platform_data {
149 void *waves_handle;
Jasmine Cha70771b62018-05-15 15:02:43 +0800150 void *platform;
jasmine cha270b7762018-03-30 15:41:33 +0800151 pthread_mutex_t lock;
152 ma_param_init_t ma_param_init;
153 ma_param_deinit_t ma_param_deinit;
justinweng3d9b7912019-01-11 14:38:06 +0800154 ma_is_feature_used_t ma_is_feature_used;
jasmine cha270b7762018-03-30 15:41:33 +0800155 ma_set_lr_swap_t ma_set_lr_swap;
justinweng3d9b7912019-01-11 14:38:06 +0800156 ma_set_orientation_t ma_set_orientation;
jasmine cha270b7762018-03-30 15:41:33 +0800157 ma_set_sound_mode_t ma_set_sound_mode;
158 ma_set_volume_t ma_set_volume;
159 ma_set_volume_table_t ma_set_volume_table;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800160 ma_set_param_t ma_set_param;
Robert Lee837637a2018-11-16 16:34:43 +0800161 bool speaker_lr_swap;
justinweng3d9b7912019-01-11 14:38:06 +0800162 bool orientation_used;
163 int dispaly_orientation;
jasmine cha270b7762018-03-30 15:41:33 +0800164};
165
166ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
167static uint16_t g_supported_dev = 0;
168static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
169static struct ma_platform_data *my_data = NULL;
170
Jasmine Cha70771b62018-05-15 15:02:43 +0800171static int set_audio_cal(const char *audio_cal)
jasmine cha270b7762018-03-30 15:41:33 +0800172{
173 ALOGV("set_audio_cal: %s", audio_cal);
174
Jasmine Cha70771b62018-05-15 15:02:43 +0800175 return platform_set_parameters(my_data->platform,
jasmine cha270b7762018-03-30 15:41:33 +0800176 str_parms_create_str(audio_cal));
177}
178
179static bool ma_set_lr_swap_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800180 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha270b7762018-03-30 15:41:33 +0800181{
Jasmine Cha70771b62018-05-15 15:02:43 +0800182 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
183 audio_cal_settings, swap);
jasmine cha270b7762018-03-30 15:41:33 +0800184}
185
justinweng3d9b7912019-01-11 14:38:06 +0800186static bool ma_set_orientation_l(
187 const struct ma_audio_cal_settings *audio_cal_settings, int orientation)
188{
189 return my_data->ma_set_orientation(g_ma_audio_cal_handle,
190 audio_cal_settings, orientation);
191}
192
jasmine cha270b7762018-03-30 15:41:33 +0800193static bool ma_set_sound_mode_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800194 const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
jasmine cha270b7762018-03-30 15:41:33 +0800195{
196 return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800197 audio_cal_settings, sound_mode);
jasmine cha270b7762018-03-30 15:41:33 +0800198}
199
200static bool ma_set_volume_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800201 const struct ma_audio_cal_settings *audio_cal_settings, double volume)
jasmine cha270b7762018-03-30 15:41:33 +0800202{
Jasmine Cha70771b62018-05-15 15:02:43 +0800203 return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
204 volume);
jasmine cha270b7762018-03-30 15:41:33 +0800205}
206
207static bool ma_set_volume_table_l(
Jasmine Cha70771b62018-05-15 15:02:43 +0800208 const struct ma_audio_cal_settings *audio_cal_settings,
209 size_t num_streams, struct ma_state *volume_table)
jasmine cha270b7762018-03-30 15:41:33 +0800210{
Jasmine Cha70771b62018-05-15 15:02:43 +0800211 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
212 audio_cal_settings, num_streams,
213 volume_table);
jasmine cha270b7762018-03-30 15:41:33 +0800214}
215
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800216static bool ma_set_param_l(
217 const struct ma_audio_cal_settings *audio_cal_settings,
218 unsigned int index, double value)
219{
220 return my_data->ma_set_param(g_ma_audio_cal_handle,
221 audio_cal_settings, index, value);
222}
223
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800224static void print_state_log()
225{
226 ALOGD("%s: send volume table -(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),"
227 "(%i,%f,%s),(%i,%f,%s)", __func__,
228 STREAM_VOICE, ma_cur_state_table[STREAM_VOICE].vol,
229 ma_cur_state_table[STREAM_VOICE].active ? "T" : "F",
230 STREAM_SYSTEM, ma_cur_state_table[STREAM_SYSTEM].vol,
231 ma_cur_state_table[STREAM_SYSTEM].active ? "T" : "F",
232 STREAM_RING, ma_cur_state_table[STREAM_RING].vol,
233 ma_cur_state_table[STREAM_RING].active ? "T" : "F",
234 STREAM_MUSIC, ma_cur_state_table[STREAM_MUSIC].vol,
235 ma_cur_state_table[STREAM_MUSIC].active ? "T" : "F",
236 STREAM_ALARM, ma_cur_state_table[STREAM_ALARM].vol,
237 ma_cur_state_table[STREAM_ALARM].active ? "T" : "F",
238 STREAM_NOTIFICATION, ma_cur_state_table[STREAM_NOTIFICATION].vol,
239 ma_cur_state_table[STREAM_NOTIFICATION].active ? "T" : "F");
240
241}
242
jasmine cha270b7762018-03-30 15:41:33 +0800243static inline bool valid_usecase(struct audio_usecase *usecase)
244{
245 if ((usecase->type == PCM_PLAYBACK) &&
246 /* supported usecases */
247 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
248 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
249 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
250 /* support devices */
251 ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
Robert Lee837637a2018-11-16 16:34:43 +0800252 (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
253 (audio_is_usb_out_device(usecase->devices) &&
254 audio_extn_ma_supported_usb())))
255 /* TODO: enable A2DP when it is ready */
jasmine cha270b7762018-03-30 15:41:33 +0800256
257 return true;
258
259 ALOGV("%s: not support type %d usecase %d device %d",
260 __func__, usecase->type, usecase->id, usecase->devices);
261
262 return false;
263}
264
265// already hold lock
266static inline bool is_active()
267{
268 ma_stream_type_t i = 0;
269
270 for (i = 0; i < STREAM_MAX_TYPES; i++)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800271 if (ma_cur_state_table[i].active)
jasmine cha270b7762018-03-30 15:41:33 +0800272 return true;
273
274 return false;
275}
276
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800277static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
278{
279 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
280 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
281 ma_cal->common.app_type = APP_TYPE_DEFAULT;
282 ma_cal->common.device = DEVICE_DEFAULT;
283 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
284}
285
jasmine cha270b7762018-03-30 15:41:33 +0800286static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
287{
288 int i = 0;
289 bool ret = false;
290 float vol = 0;
291 struct listnode *node;
292 struct audio_usecase *usecase;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800293 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800294
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800295 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800296
297 list_for_each(node, &adev->usecase_list) {
298 usecase = node_to_item(node, struct audio_usecase, list);
299 if (valid_usecase(usecase)) {
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800300 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
301 ma_cal.common.device = usecase->stream.out->devices;
jasmine cha270b7762018-03-30 15:41:33 +0800302 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800303 __func__, usecase->id, ma_cal.common.app_type,
304 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800305
306 switch (cmd) {
307 case MA_CMD_VOL:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800308 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Jasmine Cha70771b62018-05-15 15:02:43 +0800309 ma_cur_state_table);
jasmine cha270b7762018-03-30 15:41:33 +0800310 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800311 ALOGV("ma_set_volume_table_l success");
jasmine cha270b7762018-03-30 15:41:33 +0800312 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800313 ALOGE("ma_set_volume_table_l returned with error.");
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800314 print_state_log();
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_ENABLE:
Robert Lee837637a2018-11-16 16:34:43 +0800318 /* lr swap only enable for speaker path */
319 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
320 ret = ma_set_lr_swap_l(&ma_cal, true);
321 if (ret)
322 ALOGV("ma_set_lr_swap_l enable returned with success.");
323 else
324 ALOGE("ma_set_lr_swap_l enable returned with error.");
325 }
jasmine cha270b7762018-03-30 15:41:33 +0800326 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800327
jasmine cha270b7762018-03-30 15:41:33 +0800328 case MA_CMD_SWAP_DISABLE:
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800329 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha270b7762018-03-30 15:41:33 +0800330 if (ret)
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800331 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha270b7762018-03-30 15:41:33 +0800332 else
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800333 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha270b7762018-03-30 15:41:33 +0800334 break;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800335
justinweng3d9b7912019-01-11 14:38:06 +0800336 case MA_CMD_ROTATE_ENABLE:
337 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
338 ret = ma_set_orientation_l(&ma_cal, my_data->dispaly_orientation);
339 if (ret)
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800340 ALOGV("ma_set_orientation_l %d returned with success.",
341 my_data->dispaly_orientation);
justinweng3d9b7912019-01-11 14:38:06 +0800342 else
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800343 ALOGE("ma_set_orientation_l %d returned with error.",
344 my_data->dispaly_orientation);
justinweng3d9b7912019-01-11 14:38:06 +0800345 }
346 break;
347
348 case MA_CMD_ROTATE_DISABLE:
349 ret = ma_set_orientation_l(&ma_cal, 0);
350 if (ret)
351 ALOGV("ma_set_orientation_l 0 returned with success.");
352 else
353 ALOGE("ma_set_orientation_l 0 returned with error.");
354 break;
355
jasmine cha270b7762018-03-30 15:41:33 +0800356 default:
357 ALOGE("%s: unsupported cmd %d", __func__, cmd);
358 }
jasmine cha270b7762018-03-30 15:41:33 +0800359 }
360 }
jasmine cha270b7762018-03-30 15:41:33 +0800361
362 return ret;
363}
364
365static bool find_sup_dev(char *name)
366{
367 char *token;
368 const char s[2] = ",";
369 bool ret = false;
370 char sup_devs[128];
371
372 // the rule of comforming suppored dev's name
373 // 1. Both string len are equal
374 // 2. Both string content are equal
375
376 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
377 token = strtok(sup_devs, s);
378 while (token != NULL) {
379 if (strncmp(token, name, strlen(token)) == 0 &&
380 strlen(token) == strlen(name)) {
381 ALOGD("%s: support dev %s", __func__, token);
382 ret = true;
383 break;
384 }
385 token = strtok(NULL, s);
386 }
387
388 return ret;
389}
390
391static void ma_set_swap_l(struct audio_device *adev, bool enable)
392{
jasmine cha270b7762018-03-30 15:41:33 +0800393 if (enable)
394 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
395 else
396 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
397}
398
justinweng3d9b7912019-01-11 14:38:06 +0800399static void ma_set_rotation_l(struct audio_device *adev, int orientation)
400{
401 if (orientation != 0)
402 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_ENABLE);
403 else
404 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_DISABLE);
405}
406
jasmine cha270b7762018-03-30 15:41:33 +0800407static void ma_support_usb(bool enable, int card)
408{
409 char path[128];
410 char id[32];
411 int ret = 0;
412 int32_t fd = -1;
413 char *idd;
414
415 if (enable) {
Robert Lee837637a2018-11-16 16:34:43 +0800416 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha270b7762018-03-30 15:41:33 +0800417 if (ret < 0) {
418 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Jasmine Cha70771b62018-05-15 15:02:43 +0800419 __func__, ret, path);
jasmine cha270b7762018-03-30 15:41:33 +0800420 goto done;
421 }
422 fd = open(path, O_RDONLY);
423 if (fd < 0) {
424 ALOGE("%s: error failed to open id file %s error: %d\n",
425 __func__, path, errno);
426 goto done;
427 }
428 if (read(fd, id, sizeof(id)) < 0) {
429 ALOGE("%s: file read error", __func__);
430 goto done;
431 }
432 //replace '\n' to '\0'
433 idd = strtok(id, "\n");
434
435 if (find_sup_dev(idd)) {
Robert Lee837637a2018-11-16 16:34:43 +0800436 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha270b7762018-03-30 15:41:33 +0800437 g_supported_dev |= SUPPORTED_USB;
438 } else
Robert Lee837637a2018-11-16 16:34:43 +0800439 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha270b7762018-03-30 15:41:33 +0800440 } else {
441 g_supported_dev &= ~SUPPORTED_USB;
442 }
443
444done:
445 if (fd >= 0) close(fd);
446}
447
448// adev_init lock held
449void audio_extn_ma_init(void *platform)
450{
451 ma_stream_type_t i = 0;
452 int ret = 0;
Jasmine Cha70771b62018-05-15 15:02:43 +0800453 char lib_path[128] = {0};
454 char mps_path[128] = {0};
Jasmine Cha6d945d22018-06-19 09:49:04 -0700455 char cnf_path[128] = {0};
Jasmine Cha70771b62018-05-15 15:02:43 +0800456 struct snd_card_split *snd_split_handle = NULL;
457 snd_split_handle = audio_extn_get_snd_card_split();
jasmine cha270b7762018-03-30 15:41:33 +0800458
459 if (platform == NULL) {
460 ALOGE("%s: platform is NULL", __func__);
461 goto error;
462 }
463
464 if (my_data) { free(my_data); }
465 my_data = calloc(1, sizeof(struct ma_platform_data));
466 if (my_data == NULL) {
467 ALOGE("%s: ma_cal alloct fail", __func__);
468 goto error;
469 }
470
471 pthread_mutex_init(&my_data->lock, NULL);
472
Jasmine Cha70771b62018-05-15 15:02:43 +0800473 my_data->platform = platform;
jasmine cha270b7762018-03-30 15:41:33 +0800474 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
475 if (ret < 0) {
476 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
477 goto error;
478 }
479
480 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
481 if (my_data->waves_handle == NULL) {
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800482 ALOGE("%s: DLOPEN failed for %s, %s", __func__, LIB_MA_PARAM, dlerror());
jasmine cha270b7762018-03-30 15:41:33 +0800483 goto error;
484 } else {
485 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
486
487 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800488 MA_QDSP_PARAM_INIT);
jasmine cha270b7762018-03-30 15:41:33 +0800489 if (!my_data->ma_param_init) {
490 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
491 goto error;
492 }
493
Jasmine Cha70771b62018-05-15 15:02:43 +0800494 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
495 my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
jasmine cha270b7762018-03-30 15:41:33 +0800496 if (!my_data->ma_param_deinit) {
497 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
498 goto error;
499 }
500
justinweng3d9b7912019-01-11 14:38:06 +0800501 my_data->ma_is_feature_used = (ma_is_feature_used_t)dlsym(my_data->waves_handle,
502 MA_QDSP_IS_FEATURE_USED);
503 if (!my_data->ma_is_feature_used) {
504 ALOGV("%s: dlsym error %s for ma_is_feature_used", __func__, dlerror());
505 }
506
507 my_data->ma_set_orientation = (ma_set_orientation_t)dlsym(my_data->waves_handle,
508 MA_QDSP_SET_ORIENTATION);
509 if (!my_data->ma_set_orientation) {
510 ALOGV("%s: dlsym error %s for ma_set_orientation", __func__, dlerror());
511 }
512
jasmine cha270b7762018-03-30 15:41:33 +0800513 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800514 MA_QDSP_SET_LR_SWAP);
jasmine cha270b7762018-03-30 15:41:33 +0800515 if (!my_data->ma_set_lr_swap) {
516 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
517 goto error;
518 }
519
Jasmine Cha70771b62018-05-15 15:02:43 +0800520 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
521 my_data->waves_handle, MA_QDSP_SET_MODE);
jasmine cha270b7762018-03-30 15:41:33 +0800522 if (!my_data->ma_set_sound_mode) {
523 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
524 goto error;
525 }
526
527 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800528 MA_QDSP_SET_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800529 if (!my_data->ma_set_volume) {
530 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
531 goto error;
532 }
533
Jasmine Cha70771b62018-05-15 15:02:43 +0800534 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
535 my_data->waves_handle, MA_QDSP_SET_VOLT);
jasmine cha270b7762018-03-30 15:41:33 +0800536 if (!my_data->ma_set_volume_table) {
537 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
538 goto error;
539 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800540
541 my_data->ma_set_param = (ma_set_param_t)dlsym(
542 my_data->waves_handle, MA_QDSP_SET_PARAM);
543 if (!my_data->ma_set_param) {
544 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
545 goto error;
546 }
jasmine cha270b7762018-03-30 15:41:33 +0800547 }
548
Jasmine Cha70771b62018-05-15 15:02:43 +0800549 /* get preset table */
550 if (snd_split_handle == NULL) {
551 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
552 PRESET_PATH, MPS_BASE_STRING);
553 } else {
554 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
555 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha270b7762018-03-30 15:41:33 +0800556 }
Jasmine Cha70771b62018-05-15 15:02:43 +0800557
Jasmine Cha6d945d22018-06-19 09:49:04 -0700558 /* get config files */
559 if (snd_split_handle == NULL) {
560 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
561 PRESET_PATH, CONFIG_BASE_STRING);
562 } else {
563 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
564 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
565 }
566
Jasmine Cha70771b62018-05-15 15:02:43 +0800567 /* check file */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700568 if (access(mps_path, R_OK) < 0) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800569 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
570 goto error;
571 } else
572 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
573
jasmine cha270b7762018-03-30 15:41:33 +0800574 /* TODO: check user preset table once the feature is enabled
575 if (access(USER_PRESET_PATH, F_OK) < 0 ){
576 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
577 goto error;
578 }
579 */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700580
581 if (access(cnf_path, R_OK) < 0) {
582 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800583 goto error;
Jasmine Cha6d945d22018-06-19 09:49:04 -0700584 } else
585 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha270b7762018-03-30 15:41:33 +0800586
587 /* init ma parameter */
588 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Jasmine Cha70771b62018-05-15 15:02:43 +0800589 mps_path,
590 USER_PRESET_PATH, /* unused */
Jasmine Cha6d945d22018-06-19 09:49:04 -0700591 cnf_path,
Jasmine Cha70771b62018-05-15 15:02:43 +0800592 &set_audio_cal)) {
jasmine cha270b7762018-03-30 15:41:33 +0800593 if (!g_ma_audio_cal_handle) {
594 ALOGE("%s: ma parameters initialize failed", __func__);
595 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
596 goto error;
597 }
598 ALOGD("%s: ma parameters initialize successful", __func__);
599 } else {
600 ALOGE("%s: ma parameters initialize failed", __func__);
601 goto error;
602 }
603
604 /* init volume table */
605 for (i = 0; i < STREAM_MAX_TYPES; i++) {
606 ma_cur_state_table[i].vol = 0.0;
607 ma_cur_state_table[i].active = false;
608 }
609
Robert Lee837637a2018-11-16 16:34:43 +0800610 my_data->speaker_lr_swap = false;
justinweng3d9b7912019-01-11 14:38:06 +0800611 my_data->orientation_used = false;
612 my_data->dispaly_orientation = 0;
613
614 if (g_ma_audio_cal_handle && my_data->ma_is_feature_used) {
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800615 my_data->orientation_used = my_data->ma_is_feature_used(
616 g_ma_audio_cal_handle, "SET_ORIENTATION");
justinweng3d9b7912019-01-11 14:38:06 +0800617 }
Robert Lee837637a2018-11-16 16:34:43 +0800618
jasmine cha270b7762018-03-30 15:41:33 +0800619 return;
620
621error:
622 if (my_data) { free(my_data); }
623 my_data = NULL;
624}
625
626//adev_init lock held
627void audio_extn_ma_deinit()
628{
629 if (my_data) {
630 /* deinit ma parameter */
631 if (my_data->ma_param_deinit &&
632 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
633 ALOGD("%s: ma parameters uninitialize successful", __func__);
634 else
635 ALOGD("%s: ma parameters uninitialize failed", __func__);
636
637 pthread_mutex_destroy(&my_data->lock);
638 free(my_data);
639 my_data = NULL;
640 }
641}
642
643// adev_init and adev lock held
Jasmine Cha70771b62018-05-15 15:02:43 +0800644bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
645 float vol, bool active)
jasmine cha270b7762018-03-30 15:41:33 +0800646{
647 bool ret = false;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800648 struct ma_state pr_mstate;
jasmine cha270b7762018-03-30 15:41:33 +0800649
Jasmine Chafc63f342018-06-27 10:55:06 -0700650 if (stream_type >= STREAM_MAX_TYPES ||
651 stream_type < STREAM_MIN_TYPES) {
652 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
653 return ret;
654 }
jasmine cha270b7762018-03-30 15:41:33 +0800655
656 if (!my_data) {
Jasmine Cha70771b62018-05-15 15:02:43 +0800657 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha270b7762018-03-30 15:41:33 +0800658 return ret;
659 }
660
Jasmine Chafc63f342018-06-27 10:55:06 -0700661 ALOGV("%s: stream[%d] vol[%f] active[%s]",
662 __func__, stream_type, vol, active ? "true" : "false");
663
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800664 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
665 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
jasmine cha270b7762018-03-30 15:41:33 +0800666
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800667 // update condition: vol or active state changes
668 if (pr_mstate.vol != vol || pr_mstate.active != active) {
669
670 pthread_mutex_lock(&my_data->lock);
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800671
672 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
673 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
674
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800675 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha270b7762018-03-30 15:41:33 +0800676
677 pthread_mutex_unlock(&my_data->lock);
678 }
679
680 return ret;
681}
682
Jasmine Cha70771b62018-05-15 15:02:43 +0800683void audio_extn_ma_set_device(struct audio_usecase *usecase)
jasmine cha270b7762018-03-30 15:41:33 +0800684{
685 int i = 0;
686 int u_index = -1;
687 float vol = 0;
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800688 struct ma_audio_cal_settings ma_cal;
jasmine cha270b7762018-03-30 15:41:33 +0800689
690 if (!my_data) {
691 ALOGV("%s: maxxaudio isn't initialized.", __func__);
692 return;
693 }
694
695 if (!valid_usecase(usecase)) {
696 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
697 return;
698 }
699
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800700 ma_cal_init(&ma_cal);
jasmine cha270b7762018-03-30 15:41:33 +0800701
702 /* update audio_cal and send it */
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800703 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
704 ma_cal.common.device = usecase->stream.out->devices;
705 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
706 __func__, usecase->id, ma_cal.common.app_type,
707 ma_cal.common.device);
jasmine cha270b7762018-03-30 15:41:33 +0800708
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800709 pthread_mutex_lock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800710
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800711 if (is_active()) {
justinweng3d9b7912019-01-11 14:38:06 +0800712
713 if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
714 if (my_data->orientation_used)
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800715 ma_set_rotation_l(usecase->stream.out->dev,
716 my_data->dispaly_orientation);
justinweng3d9b7912019-01-11 14:38:06 +0800717 else
718 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
719 } else {
720 if (my_data->orientation_used)
721 ma_set_rotation_l(usecase->stream.out->dev, 0);
722 else
723 ma_set_swap_l(usecase->stream.out->dev, false);
724 }
Robert Lee837637a2018-11-16 16:34:43 +0800725
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800726 if (!ma_set_volume_table_l(&ma_cal,
727 STREAM_MAX_TYPES,
728 ma_cur_state_table))
729 ALOGE("ma_set_volume_table_l returned with error.");
730 else
731 ALOGV("ma_set_volume_table_l success");
Jasmine Chaf5ebe0b2019-02-25 20:09:34 +0800732 print_state_log();
jasmine cha270b7762018-03-30 15:41:33 +0800733
jasmine cha270b7762018-03-30 15:41:33 +0800734 }
Jasmine Cha25cd74d2018-10-01 10:06:11 +0800735 pthread_mutex_unlock(&my_data->lock);
jasmine cha270b7762018-03-30 15:41:33 +0800736}
737
Jasmine Cha70771b62018-05-15 15:02:43 +0800738void audio_extn_ma_set_parameters(struct audio_device *adev,
739 struct str_parms *parms)
jasmine cha270b7762018-03-30 15:41:33 +0800740{
741 int ret;
742 bool ret_b;
743 int val;
744 char value[128];
745
746 // do LR swap and usb recognition
747 ret = str_parms_get_int(parms, "rotation", &val);
748 if (ret >= 0) {
Robert Lee837637a2018-11-16 16:34:43 +0800749 if (!my_data) {
750 ALOGV("%s: maxxaudio isn't initialized.", __func__);
751 return;
752 }
753
jasmine cha270b7762018-03-30 15:41:33 +0800754 switch (val) {
755 case 270:
Robert Lee837637a2018-11-16 16:34:43 +0800756 my_data->speaker_lr_swap = true;
jasmine cha270b7762018-03-30 15:41:33 +0800757 break;
758 case 0:
759 case 90:
760 case 180:
Robert Lee837637a2018-11-16 16:34:43 +0800761 my_data->speaker_lr_swap = false;
jasmine cha270b7762018-03-30 15:41:33 +0800762 break;
763 }
justinweng3d9b7912019-01-11 14:38:06 +0800764 my_data->dispaly_orientation = val;
765
766 if (my_data->orientation_used)
767 ma_set_rotation_l(adev, my_data->dispaly_orientation);
768 else
769 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha270b7762018-03-30 15:41:33 +0800770 }
771
772 // check connect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800773 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
774 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800775 if (ret >= 0) {
776 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
777 if (audio_is_usb_out_device(device)) {
778 ret = str_parms_get_str(parms, "card", value, sizeof(value));
779 if (ret >= 0) {
780 const int card = atoi(value);
781 ma_support_usb(true, card);
782 }
783 }
784 }
785
786 // check disconnect status
Jasmine Cha70771b62018-05-15 15:02:43 +0800787 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
788 sizeof(value));
jasmine cha270b7762018-03-30 15:41:33 +0800789 if (ret >= 0) {
790 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
791 if (audio_is_usb_out_device(device)) {
792 ret = str_parms_get_str(parms, "card", value, sizeof(value));
793 if (ret >= 0) {
794 const int card = atoi(value);
795 ma_support_usb(false, card /*useless*/);
796 }
797 }
798 }
799}
800
801bool audio_extn_ma_supported_usb()
802{
803 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
804 return (g_supported_dev & SUPPORTED_USB) ? true : false;
805}