blob: f1393f6cf703953a36f86b2181a87ace34a88244 [file] [log] [blame]
jasmine cha75fa6f02018-03-30 15:41:33 +08001/*
Vatsal Bucha7fe95a82019-05-06 15:25:58 +05302 * Copyright (C) 2018 The Android Open Source Project
jasmine cha75fa6f02018-03-30 15:41:33 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_waves"
18/*#define LOG_NDEBUG 0*/
19
jasmine cha75fa6f02018-03-30 15:41:33 +080020#include <audio_hw.h>
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080021#include <cutils/str_parms.h>
22#include <dlfcn.h>
23#include <log/log.h>
jasmine cha75fa6f02018-03-30 15:41:33 +080024#include <math.h>
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080025#include <platform_api.h>
26#include <pthread.h>
27#include <stdlib.h>
28#include <string.h>
29#include <system/audio.h>
30#include <unistd.h>
31
jasmine cha75fa6f02018-03-30 15:41:33 +080032#include "audio_extn.h"
33#include "maxxaudio.h"
34
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080035#define LIB_MA_PARAM "libmaxxaudioqdsp.so"
jasmine cha75fa6f02018-03-30 15:41:33 +080036#define LIB_MA_PATH "vendor/lib/"
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080037#define PRESET_PATH "/vendor/etc"
38#define MPS_BASE_STRING "default"
jasmine cha75fa6f02018-03-30 15:41:33 +080039#define USER_PRESET_PATH ""
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080040#define CONFIG_BASE_STRING "maxx_conf"
jasmine cha75fa6f02018-03-30 15:41:33 +080041#define CAL_PRESIST_STR "cal_persist"
42#define CAL_SAMPLERATE_STR "cal_samplerate"
43
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080044#define MA_QDSP_PARAM_INIT "maxxaudio_qdsp_initialize"
45#define MA_QDSP_PARAM_DEINIT "maxxaudio_qdsp_uninitialize"
justinwengbbffa622019-01-11 14:38:06 +080046#define MA_QDSP_IS_FEATURE_USED "maxxaudio_qdsp_is_feature_supported"
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080047#define MA_QDSP_SET_LR_SWAP "maxxaudio_qdsp_set_lr_swap"
justinwengbbffa622019-01-11 14:38:06 +080048#define MA_QDSP_SET_ORIENTATION "maxxaudio_qdsp_set_orientation"
Arun Mirpurib1bec9c2019-01-29 16:42:45 -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"
justinwengd5395152019-11-04 12:23:09 +080053#define MA_QDSP_SET_COMMAND "maxxaudio_qdsp_set_command"
54#define MA_QDSP_GET_COMMAND "maxxaudio_qdsp_get_command"
jasmine cha75fa6f02018-03-30 15:41:33 +080055
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080056#define SUPPORT_DEV "18d1:5033" // Blackbird usbid
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -080057#define SUPPORTED_USB 0x01
jasmine cha75fa6f02018-03-30 15:41:33 +080058
justinwengd5395152019-11-04 12:23:09 +080059#define WAVES_COMMAND_SIZE 10240
60
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080061typedef unsigned int effective_scope_flag_t;
62const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0; /* RTC */
63const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1; /* ACDB */
64const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB;
65const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0;
66const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE;
jasmine cha75fa6f02018-03-30 15:41:33 +080067
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080068const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2;
69const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0;
70const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR;
71const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR;
72
73const unsigned int VALUE_AUTO = 0xFFFFFFFF;
74const unsigned int APP_TYPE_AUTO = VALUE_AUTO;
75const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO;
76const unsigned int DEVICE_AUTO = VALUE_AUTO;
77const unsigned int DEVICE_DEFAULT = DEVICE_AUTO;
78
79const unsigned int MAAP_OUTPUT_GAIN = 27;
jasmine cha75fa6f02018-03-30 15:41:33 +080080
81typedef enum MA_STREAM_TYPE {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080082 STREAM_MIN_TYPES = 0,
83 STREAM_VOICE = STREAM_MIN_TYPES,
jasmine cha75fa6f02018-03-30 15:41:33 +080084 STREAM_SYSTEM,
85 STREAM_RING,
86 STREAM_MUSIC,
87 STREAM_ALARM,
88 STREAM_NOTIFICATION ,
89 STREAM_MAX_TYPES,
90} ma_stream_type_t;
91
92typedef enum MA_CMD {
93 MA_CMD_VOL,
94 MA_CMD_SWAP_ENABLE,
95 MA_CMD_SWAP_DISABLE,
justinwengbbffa622019-01-11 14:38:06 +080096 MA_CMD_ROTATE_ENABLE,
97 MA_CMD_ROTATE_DISABLE,
jasmine cha75fa6f02018-03-30 15:41:33 +080098} ma_cmd_t;
99
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800100typedef struct ma_audio_cal_version {
101 unsigned int major;
102 unsigned int minor;
103} ma_audio_cal_version_t;
104
105typedef struct ma_audio_cal_common_settings {
106 unsigned int app_type;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800107 struct listnode devices;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800108} ma_audio_cal_common_settings_t;
109
110struct ma_audio_cal_settings {
111 ma_audio_cal_version_t version;
112 ma_audio_cal_common_settings_t common;
113 effective_scope_flag_t effect_scope_flag;
114};
115
116struct ma_state {
117 float vol;
118 bool active;
119};
120
jasmine cha75fa6f02018-03-30 15:41:33 +0800121typedef void *ma_audio_cal_handle_t;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800122typedef int (*set_audio_cal_t)(const char *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800123
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800124typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
125 const char *, const char *, set_audio_cal_t);
jasmine cha75fa6f02018-03-30 15:41:33 +0800126
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800127typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800128
justinwengbbffa622019-01-11 14:38:06 +0800129typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *);
130
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800131typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
132 const struct ma_audio_cal_settings *, bool);
jasmine cha75fa6f02018-03-30 15:41:33 +0800133
justinwengbbffa622019-01-11 14:38:06 +0800134typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t,
135 const struct ma_audio_cal_settings *, int);
136
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800137typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
138 const struct ma_audio_cal_settings *,
139 unsigned int);
jasmine cha75fa6f02018-03-30 15:41:33 +0800140
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800141typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
142 const struct ma_audio_cal_settings *, double);
jasmine cha75fa6f02018-03-30 15:41:33 +0800143
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800144typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
145 const struct ma_audio_cal_settings *,
146 size_t, struct ma_state *);
jasmine cha75fa6f02018-03-30 15:41:33 +0800147
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800148typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
149 const struct ma_audio_cal_settings *,
150 unsigned int, double);
151
justinwengd5395152019-11-04 12:23:09 +0800152typedef bool (*ma_set_cmd_t)(ma_audio_cal_handle_t handle,
153 const struct ma_audio_cal_settings *,
154 const char*);
155
156typedef bool (*ma_get_cmd_t)(ma_audio_cal_handle_t handle,
157 const struct ma_audio_cal_settings *,
158 const char *,
159 char *,
160 uint32_t);
161
jasmine cha75fa6f02018-03-30 15:41:33 +0800162struct ma_platform_data {
163 void *waves_handle;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800164 void *platform;
jasmine cha75fa6f02018-03-30 15:41:33 +0800165 pthread_mutex_t lock;
166 ma_param_init_t ma_param_init;
167 ma_param_deinit_t ma_param_deinit;
justinwengbbffa622019-01-11 14:38:06 +0800168 ma_is_feature_used_t ma_is_feature_used;
jasmine cha75fa6f02018-03-30 15:41:33 +0800169 ma_set_lr_swap_t ma_set_lr_swap;
justinwengbbffa622019-01-11 14:38:06 +0800170 ma_set_orientation_t ma_set_orientation;
jasmine cha75fa6f02018-03-30 15:41:33 +0800171 ma_set_sound_mode_t ma_set_sound_mode;
172 ma_set_volume_t ma_set_volume;
173 ma_set_volume_table_t ma_set_volume_table;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800174 ma_set_param_t ma_set_param;
justinwengd5395152019-11-04 12:23:09 +0800175 ma_set_cmd_t ma_set_cmd;
176 ma_get_cmd_t ma_get_cmd;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800177 bool speaker_lr_swap;
justinwengbbffa622019-01-11 14:38:06 +0800178 bool orientation_used;
179 int dispaly_orientation;
jasmine cha75fa6f02018-03-30 15:41:33 +0800180};
181
182ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
183static uint16_t g_supported_dev = 0;
184static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
185static struct ma_platform_data *my_data = NULL;
justinwengd5395152019-11-04 12:23:09 +0800186static char ma_command_data[WAVES_COMMAND_SIZE];
187static char ma_reply_data[WAVES_COMMAND_SIZE];
188
Arun Mirpurid750ac52019-04-12 18:33:55 -0700189// --- external function dependency ---
190fp_platform_set_parameters_t fp_platform_set_parameters;
191fp_audio_extn_get_snd_card_split_t fp_audio_extn_get_snd_card_split;
jasmine cha75fa6f02018-03-30 15:41:33 +0800192
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800193static int set_audio_cal(const char *audio_cal)
jasmine cha75fa6f02018-03-30 15:41:33 +0800194{
195 ALOGV("set_audio_cal: %s", audio_cal);
196
Arun Mirpurid750ac52019-04-12 18:33:55 -0700197 return fp_platform_set_parameters(my_data->platform,
jasmine cha75fa6f02018-03-30 15:41:33 +0800198 str_parms_create_str(audio_cal));
199}
200
201static bool ma_set_lr_swap_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800202 const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
jasmine cha75fa6f02018-03-30 15:41:33 +0800203{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800204 return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
205 audio_cal_settings, swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800206}
207
justinwengbbffa622019-01-11 14:38:06 +0800208static bool ma_set_orientation_l(
209 const struct ma_audio_cal_settings *audio_cal_settings, int orientation)
210{
211 return my_data->ma_set_orientation(g_ma_audio_cal_handle,
212 audio_cal_settings, orientation);
213}
214
jasmine cha75fa6f02018-03-30 15:41:33 +0800215static bool ma_set_volume_table_l(
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800216 const struct ma_audio_cal_settings *audio_cal_settings,
217 size_t num_streams, struct ma_state *volume_table)
jasmine cha75fa6f02018-03-30 15:41:33 +0800218{
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800219 return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
220 audio_cal_settings, num_streams,
221 volume_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800222}
223
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800224static bool ma_set_param_l(
225 const struct ma_audio_cal_settings *audio_cal_settings,
226 unsigned int index, double value)
227{
228 return my_data->ma_set_param(g_ma_audio_cal_handle,
229 audio_cal_settings, index, value);
230}
231
Jasmine Cha0ad09772019-02-25 20:09:34 +0800232static void print_state_log()
233{
234 ALOGD("%s: send volume table -(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),"
235 "(%i,%f,%s),(%i,%f,%s)", __func__,
236 STREAM_VOICE, ma_cur_state_table[STREAM_VOICE].vol,
237 ma_cur_state_table[STREAM_VOICE].active ? "T" : "F",
238 STREAM_SYSTEM, ma_cur_state_table[STREAM_SYSTEM].vol,
239 ma_cur_state_table[STREAM_SYSTEM].active ? "T" : "F",
240 STREAM_RING, ma_cur_state_table[STREAM_RING].vol,
241 ma_cur_state_table[STREAM_RING].active ? "T" : "F",
242 STREAM_MUSIC, ma_cur_state_table[STREAM_MUSIC].vol,
243 ma_cur_state_table[STREAM_MUSIC].active ? "T" : "F",
244 STREAM_ALARM, ma_cur_state_table[STREAM_ALARM].vol,
245 ma_cur_state_table[STREAM_ALARM].active ? "T" : "F",
246 STREAM_NOTIFICATION, ma_cur_state_table[STREAM_NOTIFICATION].vol,
247 ma_cur_state_table[STREAM_NOTIFICATION].active ? "T" : "F");
248
249}
250
jasmine cha75fa6f02018-03-30 15:41:33 +0800251static inline bool valid_usecase(struct audio_usecase *usecase)
252{
253 if ((usecase->type == PCM_PLAYBACK) &&
254 /* supported usecases */
255 ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
256 (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
257 (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
258 /* support devices */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800259 (compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_SPEAKER) ||
260 compare_device_type(&usecase->device_list, AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
261 (is_usb_out_device_type(&usecase->device_list) &&
Arun Mirpurid750ac52019-04-12 18:33:55 -0700262 ma_supported_usb())))
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800263 /* TODO: enable A2DP when it is ready */
jasmine cha75fa6f02018-03-30 15:41:33 +0800264
265 return true;
266
267 ALOGV("%s: not support type %d usecase %d device %d",
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800268 __func__, usecase->type, usecase->id, get_device_types(&usecase->device_list));
jasmine cha75fa6f02018-03-30 15:41:33 +0800269
270 return false;
271}
272
273// already hold lock
274static inline bool is_active()
275{
276 ma_stream_type_t i = 0;
277
278 for (i = 0; i < STREAM_MAX_TYPES; i++)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800279 if (ma_cur_state_table[i].active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800280 return true;
281
282 return false;
283}
284
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800285static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
286{
287 ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
288 ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
289 ma_cal->common.app_type = APP_TYPE_DEFAULT;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800290 list_init(&ma_cal->common.devices);
291 update_device_list(&ma_cal->common.devices, DEVICE_DEFAULT,
292 "", true);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800293 ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
294}
295
jasmine cha75fa6f02018-03-30 15:41:33 +0800296static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
297{
jasmine cha75fa6f02018-03-30 15:41:33 +0800298 bool ret = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800299 struct listnode *node;
300 struct audio_usecase *usecase;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800301 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800302
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800303 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800304
305 list_for_each(node, &adev->usecase_list) {
306 usecase = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +0530307 if (usecase->stream.out && valid_usecase(usecase)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800308 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800309 assign_devices(&ma_cal.common.devices, &usecase->stream.out->device_list);
jasmine cha75fa6f02018-03-30 15:41:33 +0800310 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800311 __func__, usecase->id, ma_cal.common.app_type,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800312 get_device_types(&ma_cal.common.devices));
jasmine cha75fa6f02018-03-30 15:41:33 +0800313
314 switch (cmd) {
315 case MA_CMD_VOL:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800316 ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800317 ma_cur_state_table);
jasmine cha75fa6f02018-03-30 15:41:33 +0800318 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800319 ALOGV("ma_set_volume_table_l success");
jasmine cha75fa6f02018-03-30 15:41:33 +0800320 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800321 ALOGE("ma_set_volume_table_l returned with error.");
Jasmine Cha0ad09772019-02-25 20:09:34 +0800322 print_state_log();
jasmine cha75fa6f02018-03-30 15:41:33 +0800323 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800324
jasmine cha75fa6f02018-03-30 15:41:33 +0800325 case MA_CMD_SWAP_ENABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800326 /* lr swap only enable for speaker path */
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800327 if (compare_device_type(&ma_cal.common.devices,
328 AUDIO_DEVICE_OUT_SPEAKER)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800329 ret = ma_set_lr_swap_l(&ma_cal, true);
330 if (ret)
331 ALOGV("ma_set_lr_swap_l enable returned with success.");
332 else
333 ALOGE("ma_set_lr_swap_l enable returned with error.");
334 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800335 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800336
jasmine cha75fa6f02018-03-30 15:41:33 +0800337 case MA_CMD_SWAP_DISABLE:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800338 ret = ma_set_lr_swap_l(&ma_cal, false);
jasmine cha75fa6f02018-03-30 15:41:33 +0800339 if (ret)
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800340 ALOGV("ma_set_lr_swap_l disable returned with success.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800341 else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800342 ALOGE("ma_set_lr_swap_l disable returned with error.");
jasmine cha75fa6f02018-03-30 15:41:33 +0800343 break;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800344
justinwengbbffa622019-01-11 14:38:06 +0800345 case MA_CMD_ROTATE_ENABLE:
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800346 if (compare_device_type(&ma_cal.common.devices,
347 AUDIO_DEVICE_OUT_SPEAKER)) {
justinwengbbffa622019-01-11 14:38:06 +0800348 ret = ma_set_orientation_l(&ma_cal, my_data->dispaly_orientation);
349 if (ret)
Jasmine Cha0ad09772019-02-25 20:09:34 +0800350 ALOGV("ma_set_orientation_l %d returned with success.",
351 my_data->dispaly_orientation);
justinwengbbffa622019-01-11 14:38:06 +0800352 else
Jasmine Cha0ad09772019-02-25 20:09:34 +0800353 ALOGE("ma_set_orientation_l %d returned with error.",
354 my_data->dispaly_orientation);
justinwengbbffa622019-01-11 14:38:06 +0800355 }
356 break;
357
358 case MA_CMD_ROTATE_DISABLE:
359 ret = ma_set_orientation_l(&ma_cal, 0);
360 if (ret)
361 ALOGV("ma_set_orientation_l 0 returned with success.");
362 else
363 ALOGE("ma_set_orientation_l 0 returned with error.");
364 break;
365
jasmine cha75fa6f02018-03-30 15:41:33 +0800366 default:
367 ALOGE("%s: unsupported cmd %d", __func__, cmd);
368 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800369 }
370 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800371
372 return ret;
373}
374
375static bool find_sup_dev(char *name)
376{
Weiyin Jiang0d985872019-06-13 15:47:21 +0800377 char *token, *saveptr = NULL;
jasmine cha75fa6f02018-03-30 15:41:33 +0800378 const char s[2] = ",";
379 bool ret = false;
380 char sup_devs[128];
381
382 // the rule of comforming suppored dev's name
383 // 1. Both string len are equal
384 // 2. Both string content are equal
385
386 strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
Weiyin Jiang0d985872019-06-13 15:47:21 +0800387 token = strtok_r(sup_devs, s, &saveptr);
jasmine cha75fa6f02018-03-30 15:41:33 +0800388 while (token != NULL) {
389 if (strncmp(token, name, strlen(token)) == 0 &&
390 strlen(token) == strlen(name)) {
391 ALOGD("%s: support dev %s", __func__, token);
392 ret = true;
393 break;
394 }
Weiyin Jiang0d985872019-06-13 15:47:21 +0800395 token = strtok_r(NULL, s, &saveptr);
jasmine cha75fa6f02018-03-30 15:41:33 +0800396 }
397
398 return ret;
399}
400
401static void ma_set_swap_l(struct audio_device *adev, bool enable)
402{
jasmine cha75fa6f02018-03-30 15:41:33 +0800403 if (enable)
404 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
405 else
406 check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
407}
408
justinwengbbffa622019-01-11 14:38:06 +0800409static void ma_set_rotation_l(struct audio_device *adev, int orientation)
410{
411 if (orientation != 0)
412 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_ENABLE);
413 else
414 check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_DISABLE);
415}
416
jasmine cha75fa6f02018-03-30 15:41:33 +0800417static void ma_support_usb(bool enable, int card)
418{
419 char path[128];
420 char id[32];
421 int ret = 0;
422 int32_t fd = -1;
423 char *idd;
Weiyin Jiang0d985872019-06-13 15:47:21 +0800424 char *saveptr = NULL;
jasmine cha75fa6f02018-03-30 15:41:33 +0800425
426 if (enable) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800427 ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
jasmine cha75fa6f02018-03-30 15:41:33 +0800428 if (ret < 0) {
429 ALOGE("%s: failed on snprintf (%d) to path %s\n",
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800430 __func__, ret, path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800431 goto done;
432 }
433 fd = open(path, O_RDONLY);
434 if (fd < 0) {
435 ALOGE("%s: error failed to open id file %s error: %d\n",
436 __func__, path, errno);
437 goto done;
438 }
439 if (read(fd, id, sizeof(id)) < 0) {
440 ALOGE("%s: file read error", __func__);
441 goto done;
442 }
443 //replace '\n' to '\0'
Weiyin Jiang0d985872019-06-13 15:47:21 +0800444 idd = strtok_r(id, "\n", &saveptr);
jasmine cha75fa6f02018-03-30 15:41:33 +0800445
446 if (find_sup_dev(idd)) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800447 ALOGV("%s: support usbid is %s", __func__, id);
jasmine cha75fa6f02018-03-30 15:41:33 +0800448 g_supported_dev |= SUPPORTED_USB;
449 } else
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800450 ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
jasmine cha75fa6f02018-03-30 15:41:33 +0800451 } else {
452 g_supported_dev &= ~SUPPORTED_USB;
453 }
454
455done:
456 if (fd >= 0) close(fd);
457}
458
459// adev_init lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700460void ma_init(void *platform, maxx_audio_init_config_t init_config)
jasmine cha75fa6f02018-03-30 15:41:33 +0800461{
462 ma_stream_type_t i = 0;
463 int ret = 0;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800464 char lib_path[128] = {0};
465 char mps_path[128] = {0};
466 char cnf_path[128] = {0};
467 struct snd_card_split *snd_split_handle = NULL;
Arun Mirpurid750ac52019-04-12 18:33:55 -0700468
469 fp_platform_set_parameters = init_config.fp_platform_set_parameters;
470 fp_audio_extn_get_snd_card_split = init_config.fp_audio_extn_get_snd_card_split;
471
472 snd_split_handle = fp_audio_extn_get_snd_card_split();
jasmine cha75fa6f02018-03-30 15:41:33 +0800473
474 if (platform == NULL) {
475 ALOGE("%s: platform is NULL", __func__);
476 goto error;
477 }
478
479 if (my_data) { free(my_data); }
480 my_data = calloc(1, sizeof(struct ma_platform_data));
481 if (my_data == NULL) {
482 ALOGE("%s: ma_cal alloct fail", __func__);
483 goto error;
484 }
485
486 pthread_mutex_init(&my_data->lock, NULL);
487
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800488 my_data->platform = platform;
jasmine cha75fa6f02018-03-30 15:41:33 +0800489 ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
490 if (ret < 0) {
491 ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
492 goto error;
493 }
494
495 my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
496 if (my_data->waves_handle == NULL) {
justinwengd5395152019-11-04 12:23:09 +0800497 ALOGE("%s: DLOPEN failed for %s, %s", __func__, LIB_MA_PARAM, dlerror());
498 goto error;
jasmine cha75fa6f02018-03-30 15:41:33 +0800499 } else {
justinwengd5395152019-11-04 12:23:09 +0800500 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
jasmine cha75fa6f02018-03-30 15:41:33 +0800501
justinwengd5395152019-11-04 12:23:09 +0800502 my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle, MA_QDSP_PARAM_INIT);
503 if (!my_data->ma_param_init) {
504 ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
505 goto error;
506 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800507
justinwengd5395152019-11-04 12:23:09 +0800508 my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(my_data->waves_handle,
509 MA_QDSP_PARAM_DEINIT);
510 if (!my_data->ma_param_deinit) {
511 ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
512 goto error;
513 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800514
justinwengbbffa622019-01-11 14:38:06 +0800515 my_data->ma_is_feature_used = (ma_is_feature_used_t)dlsym(my_data->waves_handle,
justinwengd5395152019-11-04 12:23:09 +0800516 MA_QDSP_IS_FEATURE_USED);
justinwengbbffa622019-01-11 14:38:06 +0800517 if (!my_data->ma_is_feature_used) {
518 ALOGV("%s: dlsym error %s for ma_is_feature_used", __func__, dlerror());
519 }
520
521 my_data->ma_set_orientation = (ma_set_orientation_t)dlsym(my_data->waves_handle,
justinwengd5395152019-11-04 12:23:09 +0800522 MA_QDSP_SET_ORIENTATION);
justinwengbbffa622019-01-11 14:38:06 +0800523 if (!my_data->ma_set_orientation) {
524 ALOGV("%s: dlsym error %s for ma_set_orientation", __func__, dlerror());
525 }
526
justinwengd5395152019-11-04 12:23:09 +0800527 my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
528 MA_QDSP_SET_LR_SWAP);
529 if (!my_data->ma_set_lr_swap) {
530 ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
531 goto error;
532 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800533
justinwengd5395152019-11-04 12:23:09 +0800534 my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(my_data->waves_handle,
535 MA_QDSP_SET_MODE);
536 if (!my_data->ma_set_sound_mode) {
537 ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
538 goto error;
539 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800540
justinwengd5395152019-11-04 12:23:09 +0800541 my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle, MA_QDSP_SET_VOL);
542 if (!my_data->ma_set_volume) {
543 ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
544 goto error;
545 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800546
justinwengd5395152019-11-04 12:23:09 +0800547 my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(my_data->waves_handle,
548 MA_QDSP_SET_VOLT);
549 if (!my_data->ma_set_volume_table) {
550 ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
551 goto error;
552 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800553
justinwengd5395152019-11-04 12:23:09 +0800554 my_data->ma_set_param = (ma_set_param_t)dlsym(my_data->waves_handle, MA_QDSP_SET_PARAM);
555 if (!my_data->ma_set_param) {
556 ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
557 goto error;
558 }
559
560 my_data->ma_set_cmd = (ma_set_cmd_t)dlsym(my_data->waves_handle, MA_QDSP_SET_COMMAND);
561 if (!my_data->ma_set_cmd) {
562 ALOGE("%s: dlsym error %s for ma_set_cmd", __func__, dlerror());
563 }
564
565 my_data->ma_get_cmd = (ma_get_cmd_t)dlsym(my_data->waves_handle, MA_QDSP_GET_COMMAND);
566 if (!my_data->ma_get_cmd) {
567 ALOGE("%s: dlsym error %s for ma_get_cmd", __func__, dlerror());
568 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800569 }
570
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800571 /* get preset table */
572 if (snd_split_handle == NULL) {
573 snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
574 PRESET_PATH, MPS_BASE_STRING);
575 } else {
576 snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
577 PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
jasmine cha75fa6f02018-03-30 15:41:33 +0800578 }
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800579
580 /* get config files */
581 if (snd_split_handle == NULL) {
582 snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
583 PRESET_PATH, CONFIG_BASE_STRING);
584 } else {
585 snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
586 PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
587 }
588
589 /* check file */
590 if (access(mps_path, R_OK) < 0) {
591 ALOGW("%s: file %s isn't existed.", __func__, mps_path);
592 goto error;
593 } else
594 ALOGD("%s: Loading mps file: %s", __func__, mps_path);
595
jasmine cha75fa6f02018-03-30 15:41:33 +0800596 /* TODO: check user preset table once the feature is enabled
597 if (access(USER_PRESET_PATH, F_OK) < 0 ){
598 ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
599 goto error;
600 }
601 */
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800602
603 if (access(cnf_path, R_OK) < 0) {
604 ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800605 goto error;
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800606 } else
607 ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
jasmine cha75fa6f02018-03-30 15:41:33 +0800608
609 /* init ma parameter */
610 if (my_data->ma_param_init(&g_ma_audio_cal_handle,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800611 mps_path,
612 USER_PRESET_PATH, /* unused */
613 cnf_path,
614 &set_audio_cal)) {
jasmine cha75fa6f02018-03-30 15:41:33 +0800615 if (!g_ma_audio_cal_handle) {
616 ALOGE("%s: ma parameters initialize failed", __func__);
617 my_data->ma_param_deinit(&g_ma_audio_cal_handle);
618 goto error;
619 }
620 ALOGD("%s: ma parameters initialize successful", __func__);
621 } else {
622 ALOGE("%s: ma parameters initialize failed", __func__);
623 goto error;
624 }
625
626 /* init volume table */
627 for (i = 0; i < STREAM_MAX_TYPES; i++) {
628 ma_cur_state_table[i].vol = 0.0;
629 ma_cur_state_table[i].active = false;
630 }
631
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800632 my_data->speaker_lr_swap = false;
justinwengbbffa622019-01-11 14:38:06 +0800633 my_data->orientation_used = false;
634 my_data->dispaly_orientation = 0;
635
636 if (g_ma_audio_cal_handle && my_data->ma_is_feature_used) {
Jasmine Cha0ad09772019-02-25 20:09:34 +0800637 my_data->orientation_used = my_data->ma_is_feature_used(
638 g_ma_audio_cal_handle, "SET_ORIENTATION");
justinwengbbffa622019-01-11 14:38:06 +0800639 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800640
jasmine cha75fa6f02018-03-30 15:41:33 +0800641 return;
642
643error:
644 if (my_data) { free(my_data); }
645 my_data = NULL;
646}
647
648//adev_init lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700649void ma_deinit()
jasmine cha75fa6f02018-03-30 15:41:33 +0800650{
651 if (my_data) {
652 /* deinit ma parameter */
653 if (my_data->ma_param_deinit &&
654 my_data->ma_param_deinit(&g_ma_audio_cal_handle))
655 ALOGD("%s: ma parameters uninitialize successful", __func__);
656 else
657 ALOGD("%s: ma parameters uninitialize failed", __func__);
658
659 pthread_mutex_destroy(&my_data->lock);
660 free(my_data);
661 my_data = NULL;
662 }
663}
664
665// adev_init and adev lock held
Arun Mirpurid750ac52019-04-12 18:33:55 -0700666bool ma_set_state(struct audio_device *adev, int stream_type,
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800667 float vol, bool active)
jasmine cha75fa6f02018-03-30 15:41:33 +0800668{
669 bool ret = false;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800670 struct ma_state pr_mstate;
jasmine cha75fa6f02018-03-30 15:41:33 +0800671
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800672 if (stream_type >= STREAM_MAX_TYPES ||
673 stream_type < STREAM_MIN_TYPES) {
674 ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
675 return ret;
676 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800677
678 if (!my_data) {
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800679 ALOGV("%s: maxxaudio isn't initialized.", __func__);
jasmine cha75fa6f02018-03-30 15:41:33 +0800680 return ret;
681 }
682
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800683 ALOGV("%s: stream[%d] vol[%f] active[%s]",
684 __func__, stream_type, vol, active ? "true" : "false");
jasmine cha75fa6f02018-03-30 15:41:33 +0800685
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800686 pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
687 pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
688
689 // update condition: vol or active state changes
690 if (pr_mstate.vol != vol || pr_mstate.active != active) {
691
692 pthread_mutex_lock(&my_data->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800693
694 ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
695 ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
696
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800697 ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
jasmine cha75fa6f02018-03-30 15:41:33 +0800698
699 pthread_mutex_unlock(&my_data->lock);
700 }
701
702 return ret;
703}
704
Arun Mirpurid750ac52019-04-12 18:33:55 -0700705void ma_set_device(struct audio_usecase *usecase)
jasmine cha75fa6f02018-03-30 15:41:33 +0800706{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800707 struct ma_audio_cal_settings ma_cal;
jasmine cha75fa6f02018-03-30 15:41:33 +0800708
709 if (!my_data) {
710 ALOGV("%s: maxxaudio isn't initialized.", __func__);
711 return;
712 }
713
714 if (!valid_usecase(usecase)) {
715 ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
716 return;
717 }
718
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800719 ma_cal_init(&ma_cal);
jasmine cha75fa6f02018-03-30 15:41:33 +0800720
721 /* update audio_cal and send it */
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800722 ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800723 assign_devices(&ma_cal.common.devices, &usecase->stream.out->device_list);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800724 ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
725 __func__, usecase->id, ma_cal.common.app_type,
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800726 get_device_types(&ma_cal.common.devices));
jasmine cha75fa6f02018-03-30 15:41:33 +0800727
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800728 pthread_mutex_lock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800729
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800730 if (is_active()) {
justinwengbbffa622019-01-11 14:38:06 +0800731
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -0800732 if (compare_device_type(&ma_cal.common.devices,
733 AUDIO_DEVICE_OUT_SPEAKER)) {
justinwengbbffa622019-01-11 14:38:06 +0800734 if (my_data->orientation_used)
Jasmine Cha0ad09772019-02-25 20:09:34 +0800735 ma_set_rotation_l(usecase->stream.out->dev,
736 my_data->dispaly_orientation);
justinwengbbffa622019-01-11 14:38:06 +0800737 else
738 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
739 } else {
740 if (my_data->orientation_used)
741 ma_set_rotation_l(usecase->stream.out->dev, 0);
742 else
743 ma_set_swap_l(usecase->stream.out->dev, false);
744 }
jasmine cha75fa6f02018-03-30 15:41:33 +0800745
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800746 if (!ma_set_volume_table_l(&ma_cal,
747 STREAM_MAX_TYPES,
748 ma_cur_state_table))
749 ALOGE("ma_set_volume_table_l returned with error.");
750 else
751 ALOGV("ma_set_volume_table_l success");
Jasmine Cha0ad09772019-02-25 20:09:34 +0800752 print_state_log();
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800753
jasmine cha75fa6f02018-03-30 15:41:33 +0800754 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800755 pthread_mutex_unlock(&my_data->lock);
jasmine cha75fa6f02018-03-30 15:41:33 +0800756}
757
justinwengd5395152019-11-04 12:23:09 +0800758static bool ma_set_command(struct ma_audio_cal_settings *audio_cal_settings, char *cmd_data)
759{
760 if (my_data->ma_set_cmd)
761 return my_data->ma_set_cmd(g_ma_audio_cal_handle, audio_cal_settings, cmd_data);
762 return false;
763}
764
765static bool ma_get_command(struct ma_audio_cal_settings *audio_cal_settings, char *cmd_data,
766 char *reply_data, uint32_t reply_size)
767{
768 if (my_data->ma_get_cmd)
769 return my_data->ma_get_cmd(g_ma_audio_cal_handle, audio_cal_settings, cmd_data, reply_data,
770 reply_size);
771 return false;
772}
773
774static bool ma_fill_apptype_and_device_from_params(struct str_parms *parms, uint32_t *app_type,
775 struct listnode *devices)
776{
777 int ret;
778 char value[128];
779
780 ret = str_parms_get_str(parms, "cal_apptype", value, sizeof(value));
781
782 if (ret >= 0) {
783 *app_type = (uint32_t)atoi(value);
784 ret = str_parms_get_str(parms, "cal_devid", value, sizeof(value));
785 if (ret >= 0) {
786 update_device_list(devices, (uint32_t)atoi(value), "", true);
787 return true;
788 }
789 }
790 return false;
791}
792
793static bool ma_add_apptype_and_device_to_params(struct str_parms *parms, uint32_t app_type,
794 struct listnode *devices)
795{
796 if (0 <= str_parms_add_int(parms, "cal_apptype", app_type)) {
797 if (0 <= str_parms_add_int(parms, "cal_devid", get_device_types(devices))) {
798 return true;
799 }
800 }
801 return false;
802}
803
804static bool ma_get_command_parameters(struct str_parms *query, struct str_parms *reply)
805{
806 struct ma_audio_cal_settings ma_cal;
807 int ret;
808
809 ret = str_parms_get_str(query, "waves_data", ma_command_data, sizeof(ma_command_data));
810 if (ret >= 0) {
811 ma_cal_init(&ma_cal);
812 if (ma_fill_apptype_and_device_from_params(query, &ma_cal.common.app_type,
813 &ma_cal.common.devices)) {
814 ma_add_apptype_and_device_to_params(reply, ma_cal.common.app_type,
815 &ma_cal.common.devices);
816 ALOGV("%s: before - command=%s", __func__, (char *)ma_command_data);
817 if (ma_get_command(&ma_cal, ma_command_data, ma_reply_data, sizeof(ma_reply_data))) {
818 str_parms_add_str(reply, "waves_data", ma_reply_data);
819 ALOGV("%s: after - command=%s", __func__, (char *)ma_reply_data);
820 return true;
821 } else {
822 str_parms_add_str(reply, "waves_data", "");
823 }
824 }
825 }
826 return false;
827}
828
829static bool ma_set_command_parameters(struct str_parms *parms)
830{
831 struct ma_audio_cal_settings ma_cal;
832 int ret;
833
834 ret = str_parms_get_str(parms, "waves_data", ma_command_data, sizeof(ma_command_data));
835 if (ret >= 0) {
836 ma_cal_init(&ma_cal);
837 if (ma_fill_apptype_and_device_from_params(parms, &ma_cal.common.app_type,
838 &ma_cal.common.devices)) {
839 return ma_set_command(&ma_cal, ma_command_data);
840 }
841 }
842 return false;
843}
844
845void ma_get_parameters(struct audio_device *adev, struct str_parms *query,
846 struct str_parms *reply)
847{
848 (void)adev;
849 ma_get_command_parameters(query, reply);
850}
851
852void ma_set_parameters(struct audio_device *adev, struct str_parms *parms)
jasmine cha75fa6f02018-03-30 15:41:33 +0800853{
854 int ret;
jasmine cha75fa6f02018-03-30 15:41:33 +0800855 int val;
856 char value[128];
857
858 // do LR swap and usb recognition
859 ret = str_parms_get_int(parms, "rotation", &val);
860 if (ret >= 0) {
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800861 if (!my_data) {
862 ALOGV("%s: maxxaudio isn't initialized.", __func__);
863 return;
864 }
865
jasmine cha75fa6f02018-03-30 15:41:33 +0800866 switch (val) {
867 case 270:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800868 my_data->speaker_lr_swap = true;
jasmine cha75fa6f02018-03-30 15:41:33 +0800869 break;
870 case 0:
871 case 90:
872 case 180:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800873 my_data->speaker_lr_swap = false;
jasmine cha75fa6f02018-03-30 15:41:33 +0800874 break;
875 }
justinwengbbffa622019-01-11 14:38:06 +0800876 my_data->dispaly_orientation = val;
877
878 if (my_data->orientation_used)
879 ma_set_rotation_l(adev, my_data->dispaly_orientation);
880 else
881 ma_set_swap_l(adev, my_data->speaker_lr_swap);
jasmine cha75fa6f02018-03-30 15:41:33 +0800882 }
883
884 // check connect status
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800885 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
886 sizeof(value));
jasmine cha75fa6f02018-03-30 15:41:33 +0800887 if (ret >= 0) {
888 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
889 if (audio_is_usb_out_device(device)) {
890 ret = str_parms_get_str(parms, "card", value, sizeof(value));
891 if (ret >= 0) {
892 const int card = atoi(value);
893 ma_support_usb(true, card);
894 }
895 }
896 }
897
898 // check disconnect status
Aalique Grahame5ce7fbe2019-01-28 12:08:13 -0800899 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
900 sizeof(value));
jasmine cha75fa6f02018-03-30 15:41:33 +0800901 if (ret >= 0) {
902 audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
903 if (audio_is_usb_out_device(device)) {
904 ret = str_parms_get_str(parms, "card", value, sizeof(value));
905 if (ret >= 0) {
906 const int card = atoi(value);
907 ma_support_usb(false, card /*useless*/);
908 }
909 }
910 }
justinwengd5395152019-11-04 12:23:09 +0800911
912 ma_set_command_parameters(parms);
jasmine cha75fa6f02018-03-30 15:41:33 +0800913}
914
Arun Mirpurid750ac52019-04-12 18:33:55 -0700915bool ma_supported_usb()
jasmine cha75fa6f02018-03-30 15:41:33 +0800916{
917 ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
918 return (g_supported_dev & SUPPORTED_USB) ? true : false;
919}