Derek Chen | a30a5f4 | 2019-12-03 11:17:09 -0500 | [diff] [blame^] | 1 | /* icc.c |
| 2 | Copyright (c) 2012-2015, 2016, The Linux Foundation. All rights reserved. |
| 3 | |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions are |
| 6 | met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above |
| 10 | copyright notice, this list of conditions and the following |
| 11 | disclaimer in the documentation and/or other materials provided |
| 12 | with the distribution. |
| 13 | * Neither the name of The Linux Foundation nor the names of its |
| 14 | contributors may be used to endorse or promote products derived |
| 15 | from this software without specific prior written permission. |
| 16 | |
| 17 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ |
| 28 | |
| 29 | #define LOG_TAG "audio_hw_icc" |
| 30 | /*#define LOG_NDEBUG 0*/ |
| 31 | #define LOG_NDDEBUG 0 |
| 32 | |
| 33 | #include <errno.h> |
| 34 | #include <math.h> |
| 35 | #include <cutils/log.h> |
| 36 | |
| 37 | #include "audio_hw.h" |
| 38 | #include "platform.h" |
| 39 | #include "platform_api.h" |
| 40 | #include <stdlib.h> |
| 41 | #include <cutils/str_parms.h> |
| 42 | #include "audio_extn.h" |
| 43 | |
| 44 | #define AUDIO_PARAMETER_ICC_ENABLE "conversation_mode_state" |
| 45 | #define AUDIO_PARAMETER_ICC_SET_SAMPLING_RATE "icc_set_sampling_rate" |
| 46 | #define AUDIO_PARAMETER_KEY_ICC_VOLUME "icc_volume" |
| 47 | |
| 48 | #ifdef PLATFORM_AUTO |
| 49 | #define ICC_RX_VOLUME "Playback 33 Volume" |
| 50 | #else |
| 51 | #define ICC_RX_VOLUME "NULL" |
| 52 | #endif |
| 53 | |
| 54 | static int32_t start_icc(struct audio_device *adev, |
| 55 | struct str_parms *parms); |
| 56 | |
| 57 | static int32_t stop_icc(struct audio_device *adev); |
| 58 | |
| 59 | struct icc_module { |
| 60 | struct pcm *icc_pcm_rx; |
| 61 | struct pcm *icc_pcm_tx; |
| 62 | bool is_icc_running; |
| 63 | float icc_volume; |
| 64 | audio_usecase_t ucid; |
| 65 | }; |
| 66 | |
| 67 | static struct icc_module iccmod = { |
| 68 | .icc_pcm_rx = NULL, |
| 69 | .icc_pcm_tx = NULL, |
| 70 | .icc_volume = 0, |
| 71 | .is_icc_running = 0, |
| 72 | .ucid = USECASE_ICC_CALL, |
| 73 | }; |
| 74 | static struct pcm_config pcm_config_icc = { |
| 75 | .channels = 4, |
| 76 | .rate = 16000, |
| 77 | .period_size = 240, |
| 78 | .period_count = 2, |
| 79 | .format = PCM_FORMAT_S16_LE, |
| 80 | .start_threshold = 0, |
| 81 | .stop_threshold = INT_MAX, |
| 82 | .avail_min = 0, |
| 83 | }; |
| 84 | |
| 85 | static fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id; |
| 86 | static fp_platform_set_echo_reference_t fp_platform_set_echo_reference; |
| 87 | static fp_select_devices_t fp_select_devices; |
| 88 | static fp_audio_extn_ext_hw_plugin_usecase_start_t fp_audio_extn_ext_hw_plugin_usecase_start; |
| 89 | static fp_audio_extn_ext_hw_plugin_usecase_stop_t fp_audio_extn_ext_hw_plugin_usecase_stop; |
| 90 | static fp_get_usecase_from_list_t fp_get_usecase_from_list; |
| 91 | static fp_disable_audio_route_t fp_disable_audio_route; |
| 92 | static fp_disable_snd_device_t fp_disable_snd_device; |
| 93 | |
| 94 | static int32_t icc_set_volume(struct audio_device *adev, float value) |
| 95 | { |
| 96 | int32_t ret = 0, vol = 0; |
| 97 | struct mixer_ctl *ctl; |
| 98 | const char *mixer_ctl_name = ICC_RX_VOLUME; |
| 99 | |
| 100 | ALOGD("%s: enter", __func__); |
| 101 | ALOGD("%s: (%f)", __func__, value); |
| 102 | |
| 103 | iccmod.icc_volume = value; |
| 104 | if (value < 0.0) { |
| 105 | ALOGW("%s: (%f) Under 0.0, assuming 0.0", __func__, value); |
| 106 | value = 0.0; |
| 107 | } else { |
| 108 | value = ((value > 15.000000) ? 1.0 : (value / 15)); |
| 109 | ALOGW("%s: Volume brought with in range (%f)", __func__, value); |
| 110 | } |
| 111 | vol = lrint((value * 0x2000) + 0.5); |
| 112 | |
| 113 | if(!iccmod.is_icc_running) { |
| 114 | ALOGV("%s: ICC not active, ignoring icc_set_volume call", __func__); |
| 115 | return -EIO; |
| 116 | } |
| 117 | |
| 118 | ALOGD("%s: Setting ICC Volume to %d", __func__, vol); |
| 119 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 120 | if(!ctl) { |
| 121 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 122 | __func__, mixer_ctl_name); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | if(mixer_ctl_set_value(ctl, 0, vol) < 0) { |
| 126 | ALOGE("%s: Couldn't set ICC Volume [%d]", __func__, vol); |
| 127 | return -EINVAL; |
| 128 | } |
| 129 | |
| 130 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static int32_t start_icc(struct audio_device *adev, |
| 135 | struct str_parms *parms __unused) |
| 136 | { |
| 137 | int32_t ret = 0; |
| 138 | struct audio_usecase *uc_info; |
| 139 | int32_t pcm_dev_rx_id, pcm_dev_tx_id; |
| 140 | |
| 141 | ALOGD("%s: enter", __func__); |
| 142 | |
| 143 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 144 | |
| 145 | if (!uc_info) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | uc_info->id = iccmod.ucid; |
| 149 | uc_info->type = ICC_CALL; |
| 150 | uc_info->stream.out = adev->primary_output; |
| 151 | list_init(&uc_info->device_list); |
| 152 | assign_devices(&uc_info->device_list, &adev->primary_output->device_list); |
| 153 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 154 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 155 | |
| 156 | list_add_tail(&adev->usecase_list, &uc_info->list); |
| 157 | |
| 158 | fp_select_devices(adev, iccmod.ucid); |
| 159 | |
| 160 | if ((uc_info->out_snd_device != SND_DEVICE_NONE) || |
| 161 | (uc_info->in_snd_device != SND_DEVICE_NONE)) { |
| 162 | if (fp_audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info)) |
| 163 | ALOGE("%s: failed to start ext hw plugin", __func__); |
| 164 | } |
| 165 | |
| 166 | pcm_dev_rx_id = fp_platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); |
| 167 | pcm_dev_tx_id = fp_platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); |
| 168 | if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 ) { |
| 169 | ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", |
| 170 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); |
| 171 | ret = -EIO; |
| 172 | goto exit; |
| 173 | } |
| 174 | |
| 175 | ALOGV("%s: ICC PCM devices (ICC pcm rx: %d pcm tx: %d) for the usecase(%d)", |
| 176 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); |
| 177 | |
| 178 | iccmod.icc_pcm_rx = pcm_open(adev->snd_card, |
| 179 | pcm_dev_rx_id, |
| 180 | PCM_OUT, &pcm_config_icc); |
| 181 | if (iccmod.icc_pcm_rx && !pcm_is_ready(iccmod.icc_pcm_rx)) { |
| 182 | ALOGE("%s: %s", __func__, pcm_get_error(iccmod.icc_pcm_rx)); |
| 183 | ret = -EIO; |
| 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | iccmod.icc_pcm_tx = pcm_open(adev->snd_card, |
| 188 | pcm_dev_tx_id, |
| 189 | PCM_IN, &pcm_config_icc); |
| 190 | if (iccmod.icc_pcm_tx && !pcm_is_ready(iccmod.icc_pcm_tx)) { |
| 191 | ALOGE("%s: %s", __func__, pcm_get_error(iccmod.icc_pcm_tx)); |
| 192 | ret = -EIO; |
| 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | if (pcm_start(iccmod.icc_pcm_rx) < 0) { |
| 197 | ALOGE("%s: pcm start for icc pcm rx failed", __func__); |
| 198 | ret = -EINVAL; |
| 199 | goto exit; |
| 200 | } |
| 201 | if (pcm_start(iccmod.icc_pcm_tx) < 0) { |
| 202 | ALOGE("%s: pcm start for icc pcm tx failed", __func__); |
| 203 | ret = -EINVAL; |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | iccmod.is_icc_running = true; |
| 208 | icc_set_volume(adev, iccmod.icc_volume); |
| 209 | |
| 210 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 211 | return 0; |
| 212 | |
| 213 | exit: |
| 214 | stop_icc(adev); |
| 215 | ALOGE("%s: Problem in ICC start: status(%d)", __func__, ret); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int32_t stop_icc(struct audio_device *adev) |
| 220 | { |
| 221 | int32_t ret = 0; |
| 222 | struct audio_usecase *uc_info; |
| 223 | |
| 224 | ALOGD("%s: enter", __func__); |
| 225 | iccmod.is_icc_running = false; |
| 226 | |
| 227 | /* 1. Close the PCM devices */ |
| 228 | |
| 229 | if (iccmod.icc_pcm_rx) { |
| 230 | pcm_close(iccmod.icc_pcm_rx); |
| 231 | iccmod.icc_pcm_rx = NULL; |
| 232 | } |
| 233 | if (iccmod.icc_pcm_tx) { |
| 234 | pcm_close(iccmod.icc_pcm_tx); |
| 235 | iccmod.icc_pcm_tx = NULL; |
| 236 | } |
| 237 | |
| 238 | uc_info = fp_get_usecase_from_list(adev, iccmod.ucid); |
| 239 | if (uc_info == NULL) { |
| 240 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 241 | __func__, iccmod.ucid); |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | if ((uc_info->out_snd_device != SND_DEVICE_NONE) || |
| 246 | (uc_info->in_snd_device != SND_DEVICE_NONE)) { |
| 247 | if (fp_audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info)) |
| 248 | ALOGE("%s: failed to stop ext hw plugin", __func__); |
| 249 | } |
| 250 | |
| 251 | /* 2. Disable echo reference while stopping icc */ |
| 252 | fp_platform_set_echo_reference(adev, false, &uc_info->device_list); |
| 253 | |
| 254 | /* 3. Get and set stream specific mixer controls */ |
| 255 | fp_disable_audio_route(adev, uc_info); |
| 256 | |
| 257 | /* 4. Disable the rx and tx devices */ |
| 258 | fp_disable_snd_device(adev, uc_info->out_snd_device); |
| 259 | fp_disable_snd_device(adev, uc_info->in_snd_device); |
| 260 | |
| 261 | list_remove(&uc_info->list); |
| 262 | free(uc_info); |
| 263 | |
| 264 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | void icc_init(icc_init_config_t init_config) |
| 269 | { |
| 270 | fp_platform_get_pcm_device_id = init_config.fp_platform_get_pcm_device_id; |
| 271 | fp_platform_set_echo_reference = init_config.fp_platform_set_echo_reference; |
| 272 | fp_select_devices = init_config.fp_select_devices; |
| 273 | fp_audio_extn_ext_hw_plugin_usecase_start = |
| 274 | init_config.fp_audio_extn_ext_hw_plugin_usecase_start; |
| 275 | fp_audio_extn_ext_hw_plugin_usecase_stop = |
| 276 | init_config.fp_audio_extn_ext_hw_plugin_usecase_stop; |
| 277 | fp_get_usecase_from_list = init_config.fp_get_usecase_from_list; |
| 278 | fp_disable_audio_route = init_config.fp_disable_audio_route; |
| 279 | fp_disable_snd_device = init_config.fp_disable_snd_device; |
| 280 | } |
| 281 | |
| 282 | bool icc_is_active(struct audio_device *adev) |
| 283 | { |
| 284 | struct audio_usecase *icc_usecase = NULL; |
| 285 | icc_usecase = fp_get_usecase_from_list(adev, iccmod.ucid); |
| 286 | |
| 287 | if (icc_usecase != NULL) |
| 288 | return true; |
| 289 | else |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | audio_usecase_t icc_get_usecase() |
| 294 | { |
| 295 | return iccmod.ucid; |
| 296 | } |
| 297 | |
| 298 | void icc_set_parameters(struct audio_device *adev, struct str_parms *parms) |
| 299 | { |
| 300 | int ret; |
| 301 | int rate; |
| 302 | int val; |
| 303 | float vol; |
| 304 | char value[32]={0}; |
| 305 | |
| 306 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_ICC_ENABLE, value, |
| 307 | sizeof(value)); |
| 308 | if (ret >= 0) { |
| 309 | if (!strncmp(value,"true",sizeof(value)) && !iccmod.is_icc_running) |
| 310 | ret = start_icc(adev,parms); |
| 311 | else if (!strncmp(value, "false", sizeof(value)) && iccmod.is_icc_running) |
| 312 | stop_icc(adev); |
| 313 | else |
| 314 | ALOGE("%s=%s is unsupported", AUDIO_PARAMETER_ICC_ENABLE, value); |
| 315 | } |
| 316 | memset(value, 0, sizeof(value)); |
| 317 | ret = str_parms_get_str(parms,AUDIO_PARAMETER_ICC_SET_SAMPLING_RATE, value, |
| 318 | sizeof(value)); |
| 319 | if (ret >= 0) { |
| 320 | rate = atoi(value); |
| 321 | if (rate == 16000){ |
| 322 | iccmod.ucid = USECASE_ICC_CALL; |
| 323 | pcm_config_icc.rate = rate; |
| 324 | } else |
| 325 | ALOGE("Unsupported rate.."); |
| 326 | } |
| 327 | |
| 328 | if (iccmod.is_icc_running) { |
| 329 | memset(value, 0, sizeof(value)); |
| 330 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 331 | value, sizeof(value)); |
| 332 | if (ret >= 0) { |
| 333 | val = atoi(value); |
| 334 | if (val > 0) |
| 335 | fp_select_devices(adev, iccmod.ucid); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | memset(value, 0, sizeof(value)); |
| 340 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_ICC_VOLUME, |
| 341 | value, sizeof(value)); |
| 342 | if (ret >= 0) { |
| 343 | if (sscanf(value, "%f", &vol) != 1){ |
| 344 | ALOGE("%s: error in retrieving icc volume", __func__); |
| 345 | ret = -EIO; |
| 346 | goto exit; |
| 347 | } |
| 348 | ALOGD("%s: icc_set_volume usecase, Vol: [%f]", __func__, vol); |
| 349 | icc_set_volume(adev, vol); |
| 350 | } |
| 351 | exit: |
| 352 | ALOGV("%s Exit",__func__); |
| 353 | } |