1 /*
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 
20 #include <audio_hw.h>
21 #include <cutils/str_parms.h>
22 #include <dlfcn.h>
23 #include <fcntl.h>
24 #include <log/log.h>
25 #include <math.h>
26 #include <platform_api.h>
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <system/audio.h>
31 #include <unistd.h>
32 
33 #include "audio_extn.h"
34 #include "maxxaudio.h"
35 
36 #define LIB_MA_PARAM "libmaxxaudioqdsp.so"
37 #define LIB_MA_PATH "vendor/lib/"
38 #define PRESET_PATH "/vendor/etc"
39 #define MPS_BASE_STRING "default"
40 #define USER_PRESET_PATH ""
41 #define CONFIG_BASE_STRING "maxx_conf"
42 #define CAL_PRESIST_STR "cal_persist"
43 #define CAL_SAMPLERATE_STR "cal_samplerate"
44 
45 #define MA_QDSP_PARAM_INIT      "maxxaudio_qdsp_initialize"
46 #define MA_QDSP_PARAM_DEINIT    "maxxaudio_qdsp_uninitialize"
47 #define MA_QDSP_IS_FEATURE_USED "maxxaudio_qdsp_is_feature_supported"
48 #define MA_QDSP_SET_LR_SWAP     "maxxaudio_qdsp_set_lr_swap"
49 #define MA_QDSP_SET_ORIENTATION "maxxaudio_qdsp_set_orientation"
50 #define MA_QDSP_SET_MODE        "maxxaudio_qdsp_set_sound_mode"
51 #define MA_QDSP_SET_VOL         "maxxaudio_qdsp_set_volume"
52 #define MA_QDSP_SET_VOLT        "maxxaudio_qdsp_set_volume_table"
53 #define MA_QDSP_SET_PARAM       "maxxaudio_qdsp_set_parameter"
54 
55 #define SUPPORT_DEV "18d1:5033" // Blackbird usbid
56 #define SUPPORTED_USB 0x01
57 
58 typedef unsigned int effective_scope_flag_t;
59 const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0;   /* RTC  */
60 const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1;  /* ACDB */
61 const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB;
62 const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0;
63 const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE;
64 
65 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2;
66 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0;
67 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR;
68 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR;
69 
70 const unsigned int VALUE_AUTO = 0xFFFFFFFF;
71 const unsigned int APP_TYPE_AUTO = VALUE_AUTO;
72 const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO;
73 const unsigned int DEVICE_AUTO = VALUE_AUTO;
74 const unsigned int DEVICE_DEFAULT = DEVICE_AUTO;
75 
76 const unsigned int MAAP_OUTPUT_GAIN = 27;
77 
78 typedef enum MA_STREAM_TYPE {
79     STREAM_MIN_TYPES = 0,
80     STREAM_VOICE = STREAM_MIN_TYPES,
81     STREAM_SYSTEM,
82     STREAM_RING,
83     STREAM_MUSIC,
84     STREAM_ALARM,
85     STREAM_NOTIFICATION ,
86     STREAM_MAX_TYPES,
87 } ma_stream_type_t;
88 
89 typedef enum MA_CMD {
90     MA_CMD_VOL,
91     MA_CMD_SWAP_ENABLE,
92     MA_CMD_SWAP_DISABLE,
93     MA_CMD_ROTATE_ENABLE,
94     MA_CMD_ROTATE_DISABLE,
95 } ma_cmd_t;
96 
97 typedef struct ma_audio_cal_version {
98     unsigned int major;
99     unsigned int minor;
100 } ma_audio_cal_version_t;
101 
102 typedef struct ma_audio_cal_common_settings {
103     unsigned int app_type;
104     unsigned int device;
105 } ma_audio_cal_common_settings_t;
106 
107 struct ma_audio_cal_settings {
108     ma_audio_cal_version_t version;
109     ma_audio_cal_common_settings_t common;
110     effective_scope_flag_t effect_scope_flag;
111 };
112 
113 struct ma_state {
114     float vol;
115     bool active;
116 };
117 
118 typedef void *ma_audio_cal_handle_t;
119 typedef int (*set_audio_cal_t)(const char *);
120 
121 typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
122                                 const char *, const char *, set_audio_cal_t);
123 
124 typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
125 
126 typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *);
127 
128 typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
129                                  const struct ma_audio_cal_settings *, bool);
130 
131 typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t,
132                                      const struct ma_audio_cal_settings *, int);
133 
134 typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
135                                     const struct ma_audio_cal_settings *,
136                                     unsigned int);
137 
138 typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
139                                 const struct ma_audio_cal_settings *, double);
140 
141 typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
142                                       const struct ma_audio_cal_settings *,
143                                       size_t, struct ma_state *);
144 
145 typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
146                                const struct ma_audio_cal_settings *,
147                                unsigned int, double);
148 
149 struct ma_platform_data {
150     void *waves_handle;
151     void *platform;
152     pthread_mutex_t lock;
153     ma_param_init_t          ma_param_init;
154     ma_param_deinit_t        ma_param_deinit;
155     ma_is_feature_used_t     ma_is_feature_used;
156     ma_set_lr_swap_t         ma_set_lr_swap;
157     ma_set_orientation_t     ma_set_orientation;
158     ma_set_sound_mode_t      ma_set_sound_mode;
159     ma_set_volume_t          ma_set_volume;
160     ma_set_volume_table_t    ma_set_volume_table;
161     ma_set_param_t           ma_set_param;
162     bool speaker_lr_swap;
163     bool orientation_used;
164     int dispaly_orientation;
165 };
166 
167 ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
168 static uint16_t g_supported_dev = 0;
169 static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
170 static struct ma_platform_data *my_data = NULL;
171 
set_audio_cal(const char * audio_cal)172 static int set_audio_cal(const char *audio_cal)
173 {
174     ALOGV("set_audio_cal: %s", audio_cal);
175 
176     return platform_set_parameters(my_data->platform,
177                                    str_parms_create_str(audio_cal));
178 }
179 
ma_set_lr_swap_l(const struct ma_audio_cal_settings * audio_cal_settings,bool swap)180 static bool ma_set_lr_swap_l(
181     const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
182 {
183     return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
184                                    audio_cal_settings, swap);
185 }
186 
ma_set_orientation_l(const struct ma_audio_cal_settings * audio_cal_settings,int orientation)187 static bool ma_set_orientation_l(
188     const struct ma_audio_cal_settings *audio_cal_settings, int orientation)
189 {
190     return my_data->ma_set_orientation(g_ma_audio_cal_handle,
191                                    audio_cal_settings, orientation);
192 }
193 
ma_set_sound_mode_l(const struct ma_audio_cal_settings * audio_cal_settings,int sound_mode)194 static bool ma_set_sound_mode_l(
195     const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
196 {
197     return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
198                                       audio_cal_settings, sound_mode);
199 }
200 
ma_set_volume_l(const struct ma_audio_cal_settings * audio_cal_settings,double volume)201 static bool ma_set_volume_l(
202     const struct ma_audio_cal_settings *audio_cal_settings, double volume)
203 {
204     return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
205                                   volume);
206 }
207 
ma_set_volume_table_l(const struct ma_audio_cal_settings * audio_cal_settings,size_t num_streams,struct ma_state * volume_table)208 static bool ma_set_volume_table_l(
209     const struct ma_audio_cal_settings *audio_cal_settings,
210     size_t num_streams, struct ma_state *volume_table)
211 {
212     return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
213                                         audio_cal_settings, num_streams,
214                                         volume_table);
215 }
216 
ma_set_param_l(const struct ma_audio_cal_settings * audio_cal_settings,unsigned int index,double value)217 static bool ma_set_param_l(
218     const struct ma_audio_cal_settings *audio_cal_settings,
219     unsigned int index, double value)
220 {
221     return my_data->ma_set_param(g_ma_audio_cal_handle,
222                                  audio_cal_settings, index, value);
223 }
224 
print_state_log()225 static void print_state_log()
226 {
227     ALOGD("%s: send volume table -(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),(%i,%f,%s),"
228           "(%i,%f,%s),(%i,%f,%s)", __func__,
229           STREAM_VOICE, ma_cur_state_table[STREAM_VOICE].vol,
230           ma_cur_state_table[STREAM_VOICE].active ? "T" : "F",
231           STREAM_SYSTEM, ma_cur_state_table[STREAM_SYSTEM].vol,
232           ma_cur_state_table[STREAM_SYSTEM].active ? "T" : "F",
233           STREAM_RING, ma_cur_state_table[STREAM_RING].vol,
234           ma_cur_state_table[STREAM_RING].active ? "T" : "F",
235           STREAM_MUSIC, ma_cur_state_table[STREAM_MUSIC].vol,
236           ma_cur_state_table[STREAM_MUSIC].active ? "T" : "F",
237           STREAM_ALARM, ma_cur_state_table[STREAM_ALARM].vol,
238           ma_cur_state_table[STREAM_ALARM].active ? "T" : "F",
239           STREAM_NOTIFICATION, ma_cur_state_table[STREAM_NOTIFICATION].vol,
240           ma_cur_state_table[STREAM_NOTIFICATION].active ? "T" : "F");
241 
242 }
243 
valid_usecase(struct audio_usecase * usecase)244 static inline bool valid_usecase(struct audio_usecase *usecase)
245 {
246     if ((usecase->type == PCM_PLAYBACK) &&
247         /* supported usecases */
248         ((usecase->id == USECASE_AUDIO_PLAYBACK_DEEP_BUFFER) ||
249          (usecase->id == USECASE_AUDIO_PLAYBACK_LOW_LATENCY) ||
250          (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD)) &&
251         /* support devices */
252         ((usecase->devices & AUDIO_DEVICE_OUT_SPEAKER) ||
253          (usecase->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) ||
254          (audio_is_usb_out_device(usecase->devices) &&
255           audio_extn_ma_supported_usb())))
256         /* TODO: enable A2DP when it is ready */
257 
258         return true;
259 
260     ALOGV("%s: not support type %d usecase %d device %d",
261            __func__, usecase->type, usecase->id, usecase->devices);
262 
263     return false;
264 }
265 
266 // already hold lock
is_active()267 static inline bool is_active()
268 {
269     ma_stream_type_t i = 0;
270 
271     for (i = 0; i < STREAM_MAX_TYPES; i++)
272         if (ma_cur_state_table[i].active)
273             return true;
274 
275     return false;
276 }
277 
ma_cal_init(struct ma_audio_cal_settings * ma_cal)278 static void ma_cal_init(struct ma_audio_cal_settings *ma_cal)
279 {
280     ma_cal->version.major = AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT;
281     ma_cal->version.minor = AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT;
282     ma_cal->common.app_type = APP_TYPE_DEFAULT;
283     ma_cal->common.device = DEVICE_DEFAULT;
284     ma_cal->effect_scope_flag = EFFECTIVE_SCOPE_ALL;
285 }
286 
check_and_send_all_audio_cal(struct audio_device * adev,ma_cmd_t cmd)287 static bool check_and_send_all_audio_cal(struct audio_device *adev, ma_cmd_t cmd)
288 {
289     int i = 0;
290     bool ret = false;
291     float vol = 0;
292     struct listnode *node;
293     struct audio_usecase *usecase;
294     struct ma_audio_cal_settings ma_cal;
295 
296     ma_cal_init(&ma_cal);
297 
298     list_for_each(node, &adev->usecase_list) {
299         usecase = node_to_item(node, struct audio_usecase, list);
300         if (valid_usecase(usecase)) {
301             ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
302             ma_cal.common.device = usecase->stream.out->devices;
303             ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
304                       __func__, usecase->id, ma_cal.common.app_type,
305                       ma_cal.common.device);
306 
307             switch (cmd) {
308                 case MA_CMD_VOL:
309                     ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
310                                                 ma_cur_state_table);
311                     if (ret)
312                         ALOGV("ma_set_volume_table_l success");
313                     else
314                         ALOGE("ma_set_volume_table_l returned with error.");
315                     print_state_log();
316                     break;
317 
318                 case MA_CMD_SWAP_ENABLE:
319                     /* lr swap only enable for speaker path */
320                     if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
321                         ret = ma_set_lr_swap_l(&ma_cal, true);
322                         if (ret)
323                             ALOGV("ma_set_lr_swap_l enable returned with success.");
324                         else
325                             ALOGE("ma_set_lr_swap_l enable returned with error.");
326                     }
327                     break;
328 
329                 case MA_CMD_SWAP_DISABLE:
330                     ret = ma_set_lr_swap_l(&ma_cal, false);
331                     if (ret)
332                         ALOGV("ma_set_lr_swap_l disable returned with success.");
333                     else
334                         ALOGE("ma_set_lr_swap_l disable returned with error.");
335                     break;
336 
337                 case MA_CMD_ROTATE_ENABLE:
338                     if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
339                         ret = ma_set_orientation_l(&ma_cal, my_data->dispaly_orientation);
340                         if (ret)
341                             ALOGV("ma_set_orientation_l %d returned with success.",
342                                   my_data->dispaly_orientation);
343                         else
344                             ALOGE("ma_set_orientation_l %d returned with error.",
345                                   my_data->dispaly_orientation);
346                     }
347                     break;
348 
349                 case MA_CMD_ROTATE_DISABLE:
350                     ret = ma_set_orientation_l(&ma_cal, 0);
351                     if (ret)
352                         ALOGV("ma_set_orientation_l 0 returned with success.");
353                     else
354                         ALOGE("ma_set_orientation_l 0 returned with error.");
355                     break;
356 
357                 default:
358                     ALOGE("%s: unsupported cmd %d", __func__, cmd);
359             }
360         }
361     }
362 
363     return ret;
364 }
365 
find_sup_dev(char * name)366 static bool find_sup_dev(char *name)
367 {
368     char *token;
369     const char s[2] = ",";
370     bool ret = false;
371     char sup_devs[128];
372 
373     // the rule of comforming suppored dev's name
374     // 1. Both string len are equal
375     // 2. Both string content are equal
376 
377     strncpy(sup_devs, SUPPORT_DEV, sizeof(sup_devs));
378     token = strtok(sup_devs, s);
379     while (token != NULL) {
380         if (strncmp(token, name, strlen(token)) == 0 &&
381             strlen(token) == strlen(name)) {
382             ALOGD("%s: support dev %s", __func__, token);
383             ret = true;
384             break;
385         }
386         token = strtok(NULL, s);
387     }
388 
389     return ret;
390 }
391 
ma_set_swap_l(struct audio_device * adev,bool enable)392 static void ma_set_swap_l(struct audio_device *adev, bool enable)
393 {
394     if (enable)
395         check_and_send_all_audio_cal(adev, MA_CMD_SWAP_ENABLE);
396     else
397         check_and_send_all_audio_cal(adev, MA_CMD_SWAP_DISABLE);
398 }
399 
ma_set_rotation_l(struct audio_device * adev,int orientation)400 static void ma_set_rotation_l(struct audio_device *adev, int orientation)
401 {
402     if (orientation != 0)
403         check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_ENABLE);
404     else
405         check_and_send_all_audio_cal(adev, MA_CMD_ROTATE_DISABLE);
406 }
407 
ma_support_usb(bool enable,int card)408 static void ma_support_usb(bool enable, int card)
409 {
410     char path[128];
411     char id[32];
412     int ret = 0;
413     int32_t fd = -1;
414     char *idd;
415 
416     if (enable) {
417         ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
418         if (ret < 0) {
419             ALOGE("%s: failed on snprintf (%d) to path %s\n",
420                   __func__, ret, path);
421             goto done;
422         }
423         fd = open(path, O_RDONLY);
424         if (fd < 0) {
425             ALOGE("%s: error failed to open id file %s error: %d\n",
426                   __func__, path, errno);
427             goto done;
428         }
429         if (read(fd, id, sizeof(id)) < 0) {
430             ALOGE("%s: file read error", __func__);
431             goto done;
432         }
433         //replace '\n' to '\0'
434         idd = strtok(id, "\n");
435 
436         if (find_sup_dev(idd)) {
437             ALOGV("%s: support usbid is %s", __func__, id);
438             g_supported_dev |= SUPPORTED_USB;
439         } else
440             ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
441     } else {
442         g_supported_dev &= ~SUPPORTED_USB;
443     }
444 
445 done:
446     if (fd >= 0) close(fd);
447 }
448 
449 // adev_init lock held
audio_extn_ma_init(void * platform)450 void audio_extn_ma_init(void *platform)
451 {
452     ma_stream_type_t i = 0;
453     int ret = 0;
454     char lib_path[128] = {0};
455     char mps_path[128] = {0};
456     char cnf_path[128] = {0};
457     struct snd_card_split *snd_split_handle = NULL;
458     snd_split_handle = audio_extn_get_snd_card_split();
459 
460     if (platform == NULL) {
461         ALOGE("%s: platform is NULL", __func__);
462         goto error;
463     }
464 
465     if (my_data) { free(my_data); }
466     my_data = calloc(1, sizeof(struct ma_platform_data));
467     if (my_data == NULL) {
468         ALOGE("%s: ma_cal alloct fail", __func__);
469         goto error;
470     }
471 
472     pthread_mutex_init(&my_data->lock, NULL);
473 
474     my_data->platform = platform;
475     ret = snprintf(lib_path, sizeof(lib_path), "%s/%s", LIB_MA_PATH, LIB_MA_PARAM);
476     if (ret < 0) {
477         ALOGE("%s: snprintf failed for lib %s, ret %d", __func__, LIB_MA_PARAM, ret);
478         goto error;
479     }
480 
481     my_data->waves_handle = dlopen(lib_path, RTLD_NOW);
482     if (my_data->waves_handle == NULL) {
483          ALOGE("%s: DLOPEN failed for %s, %s", __func__, LIB_MA_PARAM, dlerror());
484          goto error;
485     } else {
486          ALOGV("%s: DLOPEN successful for %s", __func__, LIB_MA_PARAM);
487 
488          my_data->ma_param_init = (ma_param_init_t)dlsym(my_data->waves_handle,
489                                    MA_QDSP_PARAM_INIT);
490          if (!my_data->ma_param_init) {
491              ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
492              goto error;
493          }
494 
495          my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
496                                      my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
497          if (!my_data->ma_param_deinit) {
498              ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
499              goto error;
500          }
501 
502         my_data->ma_is_feature_used = (ma_is_feature_used_t)dlsym(my_data->waves_handle,
503                                     MA_QDSP_IS_FEATURE_USED);
504         if (!my_data->ma_is_feature_used) {
505             ALOGV("%s: dlsym error %s for ma_is_feature_used", __func__, dlerror());
506         }
507 
508         my_data->ma_set_orientation = (ma_set_orientation_t)dlsym(my_data->waves_handle,
509                                         MA_QDSP_SET_ORIENTATION);
510         if (!my_data->ma_set_orientation) {
511             ALOGV("%s: dlsym error %s for ma_set_orientation", __func__, dlerror());
512         }
513 
514          my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
515                                     MA_QDSP_SET_LR_SWAP);
516          if (!my_data->ma_set_lr_swap) {
517              ALOGE("%s: dlsym error %s for ma_set_lr_swap", __func__, dlerror());
518              goto error;
519          }
520 
521          my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
522                                        my_data->waves_handle, MA_QDSP_SET_MODE);
523          if (!my_data->ma_set_sound_mode) {
524              ALOGE("%s: dlsym error %s for ma_set_sound_mode", __func__, dlerror());
525              goto error;
526          }
527 
528          my_data->ma_set_volume = (ma_set_volume_t)dlsym(my_data->waves_handle,
529                                    MA_QDSP_SET_VOL);
530          if (!my_data->ma_set_volume) {
531              ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
532              goto error;
533          }
534 
535          my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
536                                          my_data->waves_handle, MA_QDSP_SET_VOLT);
537          if (!my_data->ma_set_volume_table) {
538              ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
539              goto error;
540          }
541 
542          my_data->ma_set_param = (ma_set_param_t)dlsym(
543                                   my_data->waves_handle, MA_QDSP_SET_PARAM);
544          if (!my_data->ma_set_param) {
545              ALOGE("%s: dlsym error %s for ma_set_param", __func__, dlerror());
546              goto error;
547          }
548     }
549 
550     /* get preset table */
551     if (snd_split_handle == NULL) {
552         snprintf(mps_path, sizeof(mps_path), "%s/%s.mps",
553                  PRESET_PATH, MPS_BASE_STRING);
554     } else {
555         snprintf(mps_path, sizeof(mps_path), "%s/%s_%s.mps",
556                  PRESET_PATH, MPS_BASE_STRING, snd_split_handle->form_factor);
557     }
558 
559     /* get config files */
560     if (snd_split_handle == NULL) {
561         snprintf(cnf_path, sizeof(cnf_path), "%s/%s.ini",
562                  PRESET_PATH, CONFIG_BASE_STRING);
563     } else {
564         snprintf(cnf_path, sizeof(cnf_path), "%s/%s_%s.ini",
565                  PRESET_PATH, CONFIG_BASE_STRING, snd_split_handle->form_factor);
566     }
567 
568     /* check file */
569     if (access(mps_path, R_OK) < 0) {
570         ALOGW("%s: file %s isn't existed.", __func__, mps_path);
571         goto error;
572     } else
573         ALOGD("%s: Loading mps file: %s", __func__, mps_path);
574 
575     /* TODO: check user preset table once the feature is enabled
576     if (access(USER_PRESET_PATH, F_OK) < 0 ){
577         ALOGW("%s: file %s isn't existed.", __func__, USER_PRESET_PATH);
578         goto error;
579     }
580     */
581 
582     if (access(cnf_path, R_OK) < 0) {
583         ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
584         goto error;
585     } else
586         ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
587 
588     /* init ma parameter */
589     if (my_data->ma_param_init(&g_ma_audio_cal_handle,
590                                mps_path,
591                                USER_PRESET_PATH, /* unused */
592                                cnf_path,
593                                &set_audio_cal)) {
594         if (!g_ma_audio_cal_handle) {
595             ALOGE("%s: ma parameters initialize failed", __func__);
596             my_data->ma_param_deinit(&g_ma_audio_cal_handle);
597             goto error;
598         }
599         ALOGD("%s: ma parameters initialize successful", __func__);
600     } else {
601         ALOGE("%s: ma parameters initialize failed", __func__);
602         goto error;
603     }
604 
605     /* init volume table */
606     for (i = 0; i < STREAM_MAX_TYPES; i++) {
607         ma_cur_state_table[i].vol = 0.0;
608         ma_cur_state_table[i].active = false;
609     }
610 
611     my_data->speaker_lr_swap = false;
612     my_data->orientation_used = false;
613     my_data->dispaly_orientation = 0;
614 
615     if (g_ma_audio_cal_handle && my_data->ma_is_feature_used) {
616         my_data->orientation_used = my_data->ma_is_feature_used(
617                 g_ma_audio_cal_handle, "SET_ORIENTATION");
618     }
619 
620     return;
621 
622 error:
623     if (my_data) { free(my_data); }
624     my_data = NULL;
625 }
626 
627 //adev_init lock held
audio_extn_ma_deinit()628 void audio_extn_ma_deinit()
629 {
630     if (my_data) {
631         /* deinit ma parameter */
632         if (my_data->ma_param_deinit &&
633             my_data->ma_param_deinit(&g_ma_audio_cal_handle))
634             ALOGD("%s: ma parameters uninitialize successful", __func__);
635         else
636             ALOGD("%s: ma parameters uninitialize failed", __func__);
637 
638         pthread_mutex_destroy(&my_data->lock);
639         free(my_data);
640         my_data = NULL;
641     }
642 }
643 
644 // adev_init and adev lock held
audio_extn_ma_set_state(struct audio_device * adev,int stream_type,float vol,bool active)645 bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
646                              float vol, bool active)
647 {
648     bool ret = false;
649     struct ma_state pr_mstate;
650 
651     if (stream_type >= STREAM_MAX_TYPES ||
652         stream_type < STREAM_MIN_TYPES) {
653         ALOGE("%s: stream_type %d out of range.", __func__, stream_type);
654         return ret;
655     }
656 
657     if (!my_data) {
658         ALOGV("%s: maxxaudio isn't initialized.", __func__);
659         return ret;
660     }
661 
662     ALOGV("%s: stream[%d] vol[%f] active[%s]",
663           __func__, stream_type, vol, active ? "true" : "false");
664 
665     pr_mstate.vol = ma_cur_state_table[(ma_stream_type_t)stream_type].vol;
666     pr_mstate.active = ma_cur_state_table[(ma_stream_type_t)stream_type].active;
667 
668     // update condition: vol or active state changes
669     if (pr_mstate.vol != vol || pr_mstate.active != active) {
670 
671         pthread_mutex_lock(&my_data->lock);
672 
673         ma_cur_state_table[(ma_stream_type_t)stream_type].vol = vol;
674         ma_cur_state_table[(ma_stream_type_t)stream_type].active = active;
675 
676         ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
677 
678         pthread_mutex_unlock(&my_data->lock);
679     }
680 
681     return ret;
682 }
683 
audio_extn_ma_set_device(struct audio_usecase * usecase)684 void audio_extn_ma_set_device(struct audio_usecase *usecase)
685 {
686     int i = 0;
687     int u_index = -1;
688     float vol = 0;
689     struct ma_audio_cal_settings ma_cal;
690 
691     if (!my_data) {
692         ALOGV("%s: maxxaudio isn't initialized.", __func__);
693         return;
694     }
695 
696     if (!valid_usecase(usecase)) {
697         ALOGV("%s: %d is not supported usecase", __func__, usecase->id);
698         return;
699     }
700 
701     ma_cal_init(&ma_cal);
702 
703     /* update audio_cal and send it */
704     ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
705     ma_cal.common.device = usecase->stream.out->devices;
706     ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
707               __func__, usecase->id, ma_cal.common.app_type,
708               ma_cal.common.device);
709 
710     pthread_mutex_lock(&my_data->lock);
711 
712     if (is_active()) {
713 
714         if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
715             if (my_data->orientation_used)
716                 ma_set_rotation_l(usecase->stream.out->dev,
717                                   my_data->dispaly_orientation);
718             else
719                 ma_set_swap_l(usecase->stream.out->dev, my_data->speaker_lr_swap);
720         } else {
721             if (my_data->orientation_used)
722                 ma_set_rotation_l(usecase->stream.out->dev, 0);
723             else
724                 ma_set_swap_l(usecase->stream.out->dev, false);
725         }
726 
727         if (!ma_set_volume_table_l(&ma_cal,
728                                    STREAM_MAX_TYPES,
729                                    ma_cur_state_table))
730             ALOGE("ma_set_volume_table_l returned with error.");
731         else
732             ALOGV("ma_set_volume_table_l success");
733         print_state_log();
734 
735     }
736     pthread_mutex_unlock(&my_data->lock);
737 }
738 
audio_extn_ma_set_parameters(struct audio_device * adev,struct str_parms * parms)739 void audio_extn_ma_set_parameters(struct audio_device *adev,
740                                   struct str_parms *parms)
741 {
742     int ret;
743     bool ret_b;
744     int val;
745     char value[128];
746 
747     // do LR swap and usb recognition
748     ret = str_parms_get_int(parms, "rotation", &val);
749     if (ret >= 0) {
750         if (!my_data) {
751             ALOGV("%s: maxxaudio isn't initialized.", __func__);
752             return;
753         }
754 
755         switch (val) {
756         case 270:
757             my_data->speaker_lr_swap = true;
758             break;
759         case 0:
760         case 90:
761         case 180:
762             my_data->speaker_lr_swap = false;
763             break;
764         }
765         my_data->dispaly_orientation = val;
766 
767         if (my_data->orientation_used)
768             ma_set_rotation_l(adev, my_data->dispaly_orientation);
769         else
770             ma_set_swap_l(adev, my_data->speaker_lr_swap);
771     }
772 
773     // check connect status
774     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
775                             sizeof(value));
776     if (ret >= 0) {
777         audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
778         if (audio_is_usb_out_device(device)) {
779             ret = str_parms_get_str(parms, "card", value, sizeof(value));
780             if (ret >= 0) {
781                 const int card = atoi(value);
782                 ma_support_usb(true, card);
783             }
784         }
785     }
786 
787     // check disconnect status
788     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
789                             sizeof(value));
790     if (ret >= 0) {
791         audio_devices_t device = (audio_devices_t)strtoul(value, NULL, 10);
792         if (audio_is_usb_out_device(device)) {
793             ret = str_parms_get_str(parms, "card", value, sizeof(value));
794             if (ret >= 0) {
795                 const int card = atoi(value);
796                 ma_support_usb(false, card /*useless*/);
797             }
798         }
799     }
800 }
801 
audio_extn_ma_supported_usb()802 bool audio_extn_ma_supported_usb()
803 {
804     ALOGV("%s: current support 0x%x", __func__, g_supported_dev);
805     return (g_supported_dev & SUPPORTED_USB) ? true : false;
806 }
807