1 /*
2  * Copyright (C) 2015 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 "hardware_cal"
18 /*#define LOG_NDEBUG 0*/
19 #define LOG_NDDEBUG 0
20 
21 #ifdef HWDEP_CAL_ENABLED
22 
23 #include <stdlib.h>
24 #include <dlfcn.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <cutils/log.h>
28 #include <audio_hw.h>
29 #include "audio_extn.h"
30 #include "sound/msmcal-hwdep.h"
31 
32 #define SOUND_TRIGGER_DEVICE_HANDSET_MONO_LOW_POWER_ACDB_ID (100)
33 #define MAX_CAL_NAME 20
34 
35 typedef struct acdb_audio_cal_cfg {
36     uint32_t             persist;
37     uint32_t             snd_dev_id;
38     audio_devices_t      dev_id;
39     int32_t              acdb_dev_id;
40     uint32_t             app_type;
41     uint32_t             topo_id;
42     uint32_t             sampling_rate;
43     uint32_t             cal_type;
44     uint32_t             module_id;
45     uint32_t             param_id;
46 } acdb_audio_cal_cfg_t;
47 
48 struct param_data {
49     int    use_case;
50     int    acdb_id;
51     int    get_size;
52     int    buff_size;
53     int    data_size;
54     void   *buff;
55 };
56 
57 char cal_name_info[WCD9XXX_MAX_CAL][MAX_CAL_NAME] = {
58         [WCD9XXX_ANC_CAL] = "anc_cal",
59         [WCD9XXX_MBHC_CAL] = "mbhc_cal",
60         [WCD9XXX_MAD_CAL] = "mad_cal",
61 };
62 
63 typedef int (*acdb_get_calibration_t)(char *attr, int size, void *data);
64 acdb_get_calibration_t     acdb_get_calibration;
65 
hw_util_open(int card_no)66 static int hw_util_open(int card_no)
67 {
68     int fd = -1;
69     char dev_name[256];
70 
71     snprintf(dev_name, sizeof(dev_name), "/dev/snd/hwC%uD%u",
72                                card_no, WCD9XXX_CODEC_HWDEP_NODE);
73     ALOGD("%s: Opening device %s\n", __func__, dev_name);
74     fd = open(dev_name, O_WRONLY);
75     if (fd < 0) {
76         ALOGE("%s: cannot open device '%s'\n", __func__, dev_name);
77         return fd;
78     }
79     ALOGD("%s: success", __func__);
80     return fd;
81 }
82 
send_codec_cal(acdb_get_calibration_t acdb_loader_get_calibration,int fd)83 static int send_codec_cal(acdb_get_calibration_t acdb_loader_get_calibration, int fd)
84 {
85     int ret = 0, type;
86 
87     for (type = WCD9XXX_ANC_CAL; type < WCD9XXX_MAX_CAL; type++) {
88         struct wcdcal_ioctl_buffer codec_buffer;
89         struct param_data calib;
90 
91         if (!strcmp(cal_name_info[type], "mad_cal"))
92             calib.acdb_id = SOUND_TRIGGER_DEVICE_HANDSET_MONO_LOW_POWER_ACDB_ID;
93         calib.get_size = 1;
94         ret = acdb_loader_get_calibration(cal_name_info[type], sizeof(struct param_data),
95                                                                  &calib);
96         if (ret < 0) {
97             ALOGE("%s get_calibration failed\n", __func__);
98             return ret;
99         }
100         calib.get_size = 0;
101         calib.buff = malloc(calib.buff_size);
102         ret = acdb_loader_get_calibration(cal_name_info[type],
103                               sizeof(struct param_data), &calib);
104         if (ret < 0) {
105             ALOGE("%s get_calibration failed\n", __func__);
106             free(calib.buff);
107             return ret;
108         }
109         codec_buffer.buffer = calib.buff;
110         codec_buffer.size = calib.data_size;
111         codec_buffer.cal_type = type;
112         if (ioctl(fd, SNDRV_CTL_IOCTL_HWDEP_CAL_TYPE, &codec_buffer) < 0)
113             ALOGE("Failed to call ioctl  for %s err=%d",
114                                   cal_name_info[type], errno);
115         ALOGD("%s cal sent for %s", __func__, cal_name_info[type]);
116         free(calib.buff);
117     }
118     return ret;
119 }
120 
121 
audio_extn_hwdep_cal_send(int snd_card,void * acdb_handle)122 void audio_extn_hwdep_cal_send(int snd_card, void *acdb_handle)
123 {
124     int fd;
125 
126     fd = hw_util_open(snd_card);
127     if (fd == -1) {
128         ALOGE("%s error open\n", __func__);
129         return;
130     }
131 
132     acdb_get_calibration = (acdb_get_calibration_t)
133           dlsym(acdb_handle, "acdb_loader_get_calibration");
134 
135     if (acdb_get_calibration == NULL) {
136         ALOGE("%s: ERROR. dlsym Error:%s acdb_loader_get_calibration", __func__,
137            dlerror());
138         return;
139     }
140     if (send_codec_cal(acdb_get_calibration, fd) < 0)
141         ALOGE("%s: Could not send anc cal", __FUNCTION__);
142 
143     close(fd);
144 }
145 
146 #endif
147