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 <log/log.h>
24 #include <math.h>
25 #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 
32 #include "audio_extn.h"
33 #include "maxxaudio.h"
34 
35 #define LIB_MA_PARAM "libmaxxaudioqdsp.so"
36 #define LIB_MA_PATH "vendor/lib/"
37 #define PRESET_PATH "/vendor/etc"
38 #define MPS_BASE_STRING "default"
39 #define USER_PRESET_PATH ""
40 #define CONFIG_BASE_STRING "maxx_conf"
41 #define CAL_PRESIST_STR "cal_persist"
42 #define CAL_SAMPLERATE_STR "cal_samplerate"
43 
44 #define MA_QDSP_PARAM_INIT      "maxxaudio_qdsp_initialize"
45 #define MA_QDSP_PARAM_DEINIT    "maxxaudio_qdsp_uninitialize"
46 #define MA_QDSP_IS_FEATURE_USED "maxxaudio_qdsp_is_feature_supported"
47 #define MA_QDSP_SET_LR_SWAP     "maxxaudio_qdsp_set_lr_swap"
48 #define MA_QDSP_SET_ORIENTATION "maxxaudio_qdsp_set_orientation"
49 #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"
53 
54 #define SUPPORT_DEV "18d1:5033" // Blackbird usbid
55 #define SUPPORTED_USB 0x01
56 
57 typedef unsigned int effective_scope_flag_t;
58 const effective_scope_flag_t EFFECTIVE_SCOPE_RTC = 1 << 0;   /* RTC  */
59 const effective_scope_flag_t EFFECTIVE_SCOPE_ACDB = 1 << 1;  /* ACDB */
60 const effective_scope_flag_t EFFECTIVE_SCOPE_ALL = EFFECTIVE_SCOPE_RTC | EFFECTIVE_SCOPE_ACDB;
61 const effective_scope_flag_t EFFECTIVE_SCOPE_NONE = 0;
62 const effective_scope_flag_t EFFECTIVE_SCOPE_DEFAULT = EFFECTIVE_SCOPE_NONE;
63 
64 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR = 2;
65 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR = 0;
66 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MAJOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MAJOR;
67 const unsigned int AUDIO_CAL_SETTINGS_VERSION_MINOR_DEFAULT = AUDIO_CAL_SETTINGS_VERSION_MINOR;
68 
69 const unsigned int VALUE_AUTO = 0xFFFFFFFF;
70 const unsigned int APP_TYPE_AUTO = VALUE_AUTO;
71 const unsigned int APP_TYPE_DEFAULT = APP_TYPE_AUTO;
72 const unsigned int DEVICE_AUTO = VALUE_AUTO;
73 const unsigned int DEVICE_DEFAULT = DEVICE_AUTO;
74 
75 const unsigned int MAAP_OUTPUT_GAIN = 27;
76 
77 typedef enum MA_STREAM_TYPE {
78     STREAM_MIN_TYPES = 0,
79     STREAM_VOICE = STREAM_MIN_TYPES,
80     STREAM_SYSTEM,
81     STREAM_RING,
82     STREAM_MUSIC,
83     STREAM_ALARM,
84     STREAM_NOTIFICATION ,
85     STREAM_MAX_TYPES,
86 } ma_stream_type_t;
87 
88 typedef enum MA_CMD {
89     MA_CMD_VOL,
90     MA_CMD_SWAP_ENABLE,
91     MA_CMD_SWAP_DISABLE,
92     MA_CMD_ROTATE_ENABLE,
93     MA_CMD_ROTATE_DISABLE,
94 } ma_cmd_t;
95 
96 typedef struct ma_audio_cal_version {
97     unsigned int major;
98     unsigned int minor;
99 } ma_audio_cal_version_t;
100 
101 typedef struct ma_audio_cal_common_settings {
102     unsigned int app_type;
103     unsigned int device;
104 } ma_audio_cal_common_settings_t;
105 
106 struct 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 
112 struct ma_state {
113     float vol;
114     bool active;
115 };
116 
117 typedef void *ma_audio_cal_handle_t;
118 typedef int (*set_audio_cal_t)(const char *);
119 
120 typedef bool (*ma_param_init_t)(ma_audio_cal_handle_t *, const char *,
121                                 const char *, const char *, set_audio_cal_t);
122 
123 typedef bool (*ma_param_deinit_t)(ma_audio_cal_handle_t *);
124 
125 typedef bool (*ma_is_feature_used_t)(ma_audio_cal_handle_t, const char *);
126 
127 typedef bool (*ma_set_lr_swap_t)(ma_audio_cal_handle_t,
128                                  const struct ma_audio_cal_settings *, bool);
129 
130 typedef bool (*ma_set_orientation_t)(ma_audio_cal_handle_t,
131                                      const struct ma_audio_cal_settings *, int);
132 
133 typedef bool (*ma_set_sound_mode_t)(ma_audio_cal_handle_t,
134                                     const struct ma_audio_cal_settings *,
135                                     unsigned int);
136 
137 typedef bool (*ma_set_volume_t)(ma_audio_cal_handle_t,
138                                 const struct ma_audio_cal_settings *, double);
139 
140 typedef bool (*ma_set_volume_table_t)(ma_audio_cal_handle_t,
141                                       const struct ma_audio_cal_settings *,
142                                       size_t, struct ma_state *);
143 
144 typedef bool (*ma_set_param_t)(ma_audio_cal_handle_t,
145                                const struct ma_audio_cal_settings *,
146                                unsigned int, double);
147 
148 struct ma_platform_data {
149     void *waves_handle;
150     void *platform;
151     pthread_mutex_t lock;
152     ma_param_init_t          ma_param_init;
153     ma_param_deinit_t        ma_param_deinit;
154     ma_is_feature_used_t     ma_is_feature_used;
155     ma_set_lr_swap_t         ma_set_lr_swap;
156     ma_set_orientation_t     ma_set_orientation;
157     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;
160     ma_set_param_t           ma_set_param;
161     bool speaker_lr_swap;
162     bool orientation_used;
163     int dispaly_orientation;
164 };
165 
166 ma_audio_cal_handle_t g_ma_audio_cal_handle = NULL;
167 static uint16_t g_supported_dev = 0;
168 static struct ma_state ma_cur_state_table[STREAM_MAX_TYPES];
169 static struct ma_platform_data *my_data = NULL;
170 
171 static int set_audio_cal(const char *audio_cal)
172 {
173     ALOGV("set_audio_cal: %s", audio_cal);
174 
175     return platform_set_parameters(my_data->platform,
176                                    str_parms_create_str(audio_cal));
177 }
178 
179 static bool ma_set_lr_swap_l(
180     const struct ma_audio_cal_settings *audio_cal_settings, bool swap)
181 {
182     return my_data->ma_set_lr_swap(g_ma_audio_cal_handle,
183                                    audio_cal_settings, swap);
184 }
185 
186 static 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 
193 static bool ma_set_sound_mode_l(
194     const struct ma_audio_cal_settings *audio_cal_settings, int sound_mode)
195 {
196     return my_data->ma_set_sound_mode(g_ma_audio_cal_handle,
197                                       audio_cal_settings, sound_mode);
198 }
199 
200 static bool ma_set_volume_l(
201     const struct ma_audio_cal_settings *audio_cal_settings, double volume)
202 {
203     return my_data->ma_set_volume(g_ma_audio_cal_handle, audio_cal_settings,
204                                   volume);
205 }
206 
207 static bool ma_set_volume_table_l(
208     const struct ma_audio_cal_settings *audio_cal_settings,
209     size_t num_streams, struct ma_state *volume_table)
210 {
211     return my_data->ma_set_volume_table(g_ma_audio_cal_handle,
212                                         audio_cal_settings, num_streams,
213                                         volume_table);
214 }
215 
216 static 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 
224 static 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 
243 static 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) ||
252          (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 */
256 
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
266 static inline bool is_active()
267 {
268     ma_stream_type_t i = 0;
269 
270     for (i = 0; i < STREAM_MAX_TYPES; i++)
271         if (ma_cur_state_table[i].active)
272             return true;
273 
274     return false;
275 }
276 
277 static 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 
286 static 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;
293     struct ma_audio_cal_settings ma_cal;
294 
295     ma_cal_init(&ma_cal);
296 
297     list_for_each(node, &adev->usecase_list) {
298         usecase = node_to_item(node, struct audio_usecase, list);
299         if (valid_usecase(usecase)) {
300             ma_cal.common.app_type = usecase->stream.out->app_type_cfg.app_type;
301             ma_cal.common.device = usecase->stream.out->devices;
302             ALOGV("%s: send usecase(%d) app_type(%d) device(%d)",
303                       __func__, usecase->id, ma_cal.common.app_type,
304                       ma_cal.common.device);
305 
306             switch (cmd) {
307                 case MA_CMD_VOL:
308                     ret = ma_set_volume_table_l(&ma_cal, STREAM_MAX_TYPES,
309                                                 ma_cur_state_table);
310                     if (ret)
311                         ALOGV("ma_set_volume_table_l success");
312                     else
313                         ALOGE("ma_set_volume_table_l returned with error.");
314                     print_state_log();
315                     break;
316 
317                 case MA_CMD_SWAP_ENABLE:
318                     /* 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                     }
326                     break;
327 
328                 case MA_CMD_SWAP_DISABLE:
329                     ret = ma_set_lr_swap_l(&ma_cal, false);
330                     if (ret)
331                         ALOGV("ma_set_lr_swap_l disable returned with success.");
332                     else
333                         ALOGE("ma_set_lr_swap_l disable returned with error.");
334                     break;
335 
336                 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)
340                             ALOGV("ma_set_orientation_l %d returned with success.",
341                                   my_data->dispaly_orientation);
342                         else
343                             ALOGE("ma_set_orientation_l %d returned with error.",
344                                   my_data->dispaly_orientation);
345                     }
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 
356                 default:
357                     ALOGE("%s: unsupported cmd %d", __func__, cmd);
358             }
359         }
360     }
361 
362     return ret;
363 }
364 
365 static 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 
391 static void ma_set_swap_l(struct audio_device *adev, bool enable)
392 {
393     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 
399 static 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 
407 static 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) {
416         ret = snprintf(path, sizeof(path), "/proc/asound/card%u/usbid", card);
417         if (ret < 0) {
418             ALOGE("%s: failed on snprintf (%d) to path %s\n",
419                   __func__, ret, path);
420             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)) {
436             ALOGV("%s: support usbid is %s", __func__, id);
437             g_supported_dev |= SUPPORTED_USB;
438         } else
439             ALOGV("%s: usbid %s isn't found from %s", __func__, id, SUPPORT_DEV);
440     } else {
441         g_supported_dev &= ~SUPPORTED_USB;
442     }
443 
444 done:
445     if (fd >= 0) close(fd);
446 }
447 
448 // adev_init lock held
449 void audio_extn_ma_init(void *platform)
450 {
451     ma_stream_type_t i = 0;
452     int ret = 0;
453     char lib_path[128] = {0};
454     char mps_path[128] = {0};
455     char cnf_path[128] = {0};
456     struct snd_card_split *snd_split_handle = NULL;
457     snd_split_handle = audio_extn_get_snd_card_split();
458 
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 
473     my_data->platform = platform;
474     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) {
482          ALOGE("%s: DLOPEN failed for %s, %s", __func__, LIB_MA_PARAM, dlerror());
483          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,
488                                    MA_QDSP_PARAM_INIT);
489          if (!my_data->ma_param_init) {
490              ALOGE("%s: dlsym error %s for ma_param_init", __func__, dlerror());
491              goto error;
492          }
493 
494          my_data->ma_param_deinit = (ma_param_deinit_t)dlsym(
495                                      my_data->waves_handle, MA_QDSP_PARAM_DEINIT);
496          if (!my_data->ma_param_deinit) {
497              ALOGE("%s: dlsym error %s for ma_param_deinit", __func__, dlerror());
498              goto error;
499          }
500 
501         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 
513          my_data->ma_set_lr_swap = (ma_set_lr_swap_t)dlsym(my_data->waves_handle,
514                                     MA_QDSP_SET_LR_SWAP);
515          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 
520          my_data->ma_set_sound_mode = (ma_set_sound_mode_t)dlsym(
521                                        my_data->waves_handle, MA_QDSP_SET_MODE);
522          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,
528                                    MA_QDSP_SET_VOL);
529          if (!my_data->ma_set_volume) {
530              ALOGE("%s: dlsym error %s for ma_set_volume", __func__, dlerror());
531              goto error;
532          }
533 
534          my_data->ma_set_volume_table = (ma_set_volume_table_t)dlsym(
535                                          my_data->waves_handle, MA_QDSP_SET_VOLT);
536          if (!my_data->ma_set_volume_table) {
537              ALOGE("%s: dlsym error %s for ma_set_volume_table", __func__, dlerror());
538              goto error;
539          }
540 
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          }
547     }
548 
549     /* 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);
556     }
557 
558     /* 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 
567     /* check file */
568     if (access(mps_path, R_OK) < 0) {
569         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 
574     /* 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     */
580 
581     if (access(cnf_path, R_OK) < 0) {
582         ALOGW("%s: file %s isn't existed.", __func__, cnf_path);
583         goto error;
584     } else
585         ALOGD("%s: Loading ini file: %s", __func__, cnf_path);
586 
587     /* init ma parameter */
588     if (my_data->ma_param_init(&g_ma_audio_cal_handle,
589                                mps_path,
590                                USER_PRESET_PATH, /* unused */
591                                cnf_path,
592                                &set_audio_cal)) {
593         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 
610     my_data->speaker_lr_swap = false;
611     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) {
615         my_data->orientation_used = my_data->ma_is_feature_used(
616                 g_ma_audio_cal_handle, "SET_ORIENTATION");
617     }
618 
619     return;
620 
621 error:
622     if (my_data) { free(my_data); }
623     my_data = NULL;
624 }
625 
626 //adev_init lock held
627 void 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
644 bool audio_extn_ma_set_state(struct audio_device *adev, int stream_type,
645                              float vol, bool active)
646 {
647     bool ret = false;
648     struct ma_state pr_mstate;
649 
650     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     }
655 
656     if (!my_data) {
657         ALOGV("%s: maxxaudio isn't initialized.", __func__);
658         return ret;
659     }
660 
661     ALOGV("%s: stream[%d] vol[%f] active[%s]",
662           __func__, stream_type, vol, active ? "true" : "false");
663 
664     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;
666 
667     // 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);
671 
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 
675         ret = check_and_send_all_audio_cal(adev, MA_CMD_VOL);
676 
677         pthread_mutex_unlock(&my_data->lock);
678     }
679 
680     return ret;
681 }
682 
683 void audio_extn_ma_set_device(struct audio_usecase *usecase)
684 {
685     int i = 0;
686     int u_index = -1;
687     float vol = 0;
688     struct ma_audio_cal_settings ma_cal;
689 
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 
700     ma_cal_init(&ma_cal);
701 
702     /* update audio_cal and send it */
703     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);
708 
709     pthread_mutex_lock(&my_data->lock);
710 
711     if (is_active()) {
712 
713         if (ma_cal.common.device & AUDIO_DEVICE_OUT_SPEAKER) {
714             if (my_data->orientation_used)
715                 ma_set_rotation_l(usecase->stream.out->dev,
716                                   my_data->dispaly_orientation);
717             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         }
725 
726         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");
732         print_state_log();
733 
734     }
735     pthread_mutex_unlock(&my_data->lock);
736 }
737 
738 void audio_extn_ma_set_parameters(struct audio_device *adev,
739                                   struct str_parms *parms)
740 {
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) {
749         if (!my_data) {
750             ALOGV("%s: maxxaudio isn't initialized.", __func__);
751             return;
752         }
753 
754         switch (val) {
755         case 270:
756             my_data->speaker_lr_swap = true;
757             break;
758         case 0:
759         case 90:
760         case 180:
761             my_data->speaker_lr_swap = false;
762             break;
763         }
764         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);
770     }
771 
772     // check connect status
773     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
774                             sizeof(value));
775     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
787     ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
788                             sizeof(value));
789     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 
801 bool 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 }
806