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