Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_hfp" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | #define LOG_NDDEBUG 0 |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <math.h> |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include "audio_hw.h" |
| 26 | #include "platform.h" |
| 27 | #include "platform_api.h" |
| 28 | #include <stdlib.h> |
| 29 | #include <cutils/str_parms.h> |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 30 | #include "audio_extn/tfa_98xx.h" |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 31 | |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 32 | #define AUDIO_PARAMETER_HFP_ENABLE "hfp_enable" |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 33 | #define AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE "hfp_set_sampling_rate" |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 34 | #define AUDIO_PARAMETER_KEY_HFP_VOLUME "hfp_volume" |
| 35 | #define AUDIO_PARAMETER_HFP_VOL_MIXER_CTL "hfp_vol_mixer_ctl" |
| 36 | #define AUDIO_PARAMATER_HFP_VALUE_MAX 128 |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 37 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 38 | #define AUDIO_PARAMETER_KEY_HFP_MIC_VOLUME "hfp_mic_volume" |
| 39 | #define PLAYBACK_VOLUME_MAX 0x2000 |
| 40 | #define CAPTURE_VOLUME_DEFAULT (15.0) |
| 41 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 42 | static int32_t start_hfp(struct audio_device *adev, |
| 43 | struct str_parms *parms); |
| 44 | |
| 45 | static int32_t stop_hfp(struct audio_device *adev); |
| 46 | |
| 47 | struct hfp_module { |
| 48 | struct pcm *hfp_sco_rx; |
| 49 | struct pcm *hfp_sco_tx; |
| 50 | struct pcm *hfp_pcm_rx; |
| 51 | struct pcm *hfp_pcm_tx; |
| 52 | float hfp_volume; |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 53 | float mic_volume; |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 54 | char hfp_vol_mixer_ctl[AUDIO_PARAMATER_HFP_VALUE_MAX]; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 55 | bool is_hfp_running; |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 56 | bool mic_mute; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 57 | audio_usecase_t ucid; |
| 58 | }; |
| 59 | |
| 60 | static struct hfp_module hfpmod = { |
| 61 | .hfp_sco_rx = NULL, |
| 62 | .hfp_sco_tx = NULL, |
| 63 | .hfp_pcm_rx = NULL, |
| 64 | .hfp_pcm_tx = NULL, |
| 65 | .hfp_volume = 0, |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 66 | .mic_volume = CAPTURE_VOLUME_DEFAULT, |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 67 | .hfp_vol_mixer_ctl = {0, }, |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 68 | .is_hfp_running = 0, |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 69 | .mic_mute = 0, |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 70 | .ucid = USECASE_AUDIO_HFP_SCO, |
| 71 | }; |
| 72 | static struct pcm_config pcm_config_hfp = { |
| 73 | .channels = 1, |
| 74 | .rate = 8000, |
| 75 | .period_size = 240, |
| 76 | .period_count = 2, |
| 77 | .format = PCM_FORMAT_S16_LE, |
| 78 | .start_threshold = 0, |
| 79 | .stop_threshold = INT_MAX, |
| 80 | .avail_min = 0, |
| 81 | }; |
| 82 | |
| 83 | static int32_t hfp_set_volume(struct audio_device *adev, float value) |
| 84 | { |
| 85 | int32_t vol, ret = 0; |
| 86 | struct mixer_ctl *ctl; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 87 | |
| 88 | ALOGV("%s: entry", __func__); |
| 89 | ALOGD("%s: (%f)\n", __func__, value); |
| 90 | |
Tom Cherry | 122b241 | 2016-11-17 17:07:54 -0800 | [diff] [blame] | 91 | hfpmod.hfp_volume = value; |
| 92 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 93 | if (value < 0.0) { |
| 94 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 95 | value = 0.0; |
| 96 | } else { |
| 97 | value = ((value > 15.000000) ? 1.0 : (value / 15)); |
| 98 | ALOGW("%s: Volume brought with in range (%f)\n", __func__, value); |
| 99 | } |
| 100 | vol = lrint((value * 0x2000) + 0.5); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 101 | |
| 102 | if (!hfpmod.is_hfp_running) { |
| 103 | ALOGV("%s: HFP not active, ignoring set_hfp_volume call", __func__); |
| 104 | return -EIO; |
| 105 | } |
| 106 | |
| 107 | ALOGD("%s: Setting HFP volume to %d \n", __func__, vol); |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 108 | if (0 == hfpmod.hfp_vol_mixer_ctl[0]) { |
| 109 | #ifdef EXTERNAL_BT_SUPPORTED |
| 110 | strcpy(hfpmod.hfp_vol_mixer_ctl, "PRI AUXPCM LOOPBACK Volume"); |
| 111 | #else |
| 112 | strcpy(hfpmod.hfp_vol_mixer_ctl, "Internal HFP RX Volume"); |
| 113 | #endif |
| 114 | ALOGW("%s: Defaulting hfp mixer control to: %s", |
| 115 | __func__, hfpmod.hfp_vol_mixer_ctl); |
| 116 | } |
| 117 | ctl = mixer_get_ctl_by_name(adev->mixer, hfpmod.hfp_vol_mixer_ctl); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 118 | if (!ctl) { |
| 119 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 120 | __func__, hfpmod.hfp_vol_mixer_ctl); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 121 | return -EINVAL; |
| 122 | } |
| 123 | if(mixer_ctl_set_value(ctl, 0, vol) < 0) { |
| 124 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, vol); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | ALOGV("%s: exit", __func__); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 132 | |
| 133 | /*Set mic volume to value. |
| 134 | * |
| 135 | * This interface is used for mic volume control, set mic volume as value(range 0 ~ 15). |
| 136 | */ |
| 137 | static int hfp_set_mic_volume(struct audio_device *adev, float value) |
| 138 | { |
| 139 | int volume, ret = 0; |
| 140 | char mixer_ctl_name[128]; |
| 141 | struct mixer_ctl *ctl; |
| 142 | int pcm_device_id = HFP_ASM_RX_TX; |
| 143 | |
| 144 | if (!hfpmod.is_hfp_running) { |
| 145 | ALOGE("%s: HFP not active, ignoring set_hfp_mic_volume call", __func__); |
| 146 | return -EIO; |
| 147 | } |
| 148 | |
| 149 | if (value < 0.0) { |
| 150 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 151 | value = 0.0; |
| 152 | } else if (value > CAPTURE_VOLUME_DEFAULT) { |
| 153 | value = CAPTURE_VOLUME_DEFAULT; |
| 154 | ALOGW("%s: Volume brought within range (%f)\n", __func__, value); |
| 155 | } |
| 156 | |
| 157 | value = value / CAPTURE_VOLUME_DEFAULT; |
| 158 | memset(mixer_ctl_name, 0, sizeof(mixer_ctl_name)); |
| 159 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 160 | "Playback %d Volume", pcm_device_id); |
| 161 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 162 | if (!ctl) { |
| 163 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 164 | __func__, mixer_ctl_name); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | volume = (int)(value * PLAYBACK_VOLUME_MAX); |
| 168 | |
| 169 | ALOGD("%s: Setting volume to %d (%s)\n", __func__, volume, mixer_ctl_name); |
| 170 | if (mixer_ctl_set_value(ctl, 0, volume) < 0) { |
| 171 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, volume); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static float hfp_get_mic_volume(struct audio_device *adev) |
| 179 | { |
| 180 | int volume, ret = 0; |
| 181 | char mixer_ctl_name[128]; |
| 182 | struct mixer_ctl *ctl; |
| 183 | int pcm_device_id = HFP_ASM_RX_TX; |
| 184 | float value = 0.0; |
| 185 | |
| 186 | if (!hfpmod.is_hfp_running) { |
| 187 | ALOGE("%s: HFP not active, ignoring set_hfp_mic_volume call", __func__); |
| 188 | return -EIO; |
| 189 | } |
| 190 | |
| 191 | memset(mixer_ctl_name, 0, sizeof(mixer_ctl_name)); |
| 192 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 193 | "Playback %d Volume", pcm_device_id); |
| 194 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 195 | if (!ctl) { |
| 196 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 197 | __func__, mixer_ctl_name); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | volume = mixer_ctl_get_value(ctl, 0); |
| 202 | if ( volume < 0) { |
| 203 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, volume); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | ALOGD("%s: getting mic volume %d \n", __func__, volume); |
| 207 | |
| 208 | value = (volume / PLAYBACK_VOLUME_MAX) * CAPTURE_VOLUME_DEFAULT; |
| 209 | if (value < 0.0) { |
| 210 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 211 | value = 0.0; |
| 212 | } else if (value > CAPTURE_VOLUME_DEFAULT) { |
| 213 | value = CAPTURE_VOLUME_DEFAULT; |
| 214 | ALOGW("%s: Volume brought within range (%f)\n", __func__, value); |
| 215 | } |
| 216 | |
| 217 | return value; |
| 218 | } |
| 219 | |
| 220 | /*Set mic mute state. |
| 221 | * |
| 222 | * This interface is used for mic mute state control |
| 223 | */ |
| 224 | int audio_extn_hfp_set_mic_mute(struct audio_device *adev, bool state) |
| 225 | { |
| 226 | int rc = 0; |
| 227 | |
| 228 | if (state == hfpmod.mic_mute) |
| 229 | return rc; |
| 230 | |
| 231 | if (state == true) { |
| 232 | hfpmod.mic_volume = hfp_get_mic_volume(adev); |
| 233 | } |
| 234 | rc = hfp_set_mic_volume(adev, (state == true) ? 0.0 : hfpmod.mic_volume); |
| 235 | adev->voice.mic_mute = state; |
| 236 | hfpmod.mic_mute = state; |
| 237 | ALOGD("%s: Setting mute state %d, rc %d\n", __func__, state, rc); |
| 238 | return rc; |
| 239 | } |
| 240 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 241 | static int32_t start_hfp(struct audio_device *adev, |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 242 | struct str_parms *parms __unused) |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 243 | { |
| 244 | int32_t i, ret = 0; |
| 245 | struct audio_usecase *uc_info; |
| 246 | int32_t pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, pcm_dev_asm_tx_id; |
| 247 | |
| 248 | ALOGD("%s: enter", __func__); |
Uday Kishore Pasupuleti | 7629719 | 2015-09-18 08:39:43 -0700 | [diff] [blame] | 249 | adev->enable_hfp = true; |
Uday Kishore Pasupuleti | a1f4805 | 2015-09-08 22:49:18 +0900 | [diff] [blame] | 250 | platform_set_mic_mute(adev->platform, false); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 251 | |
| 252 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 253 | uc_info->id = hfpmod.ucid; |
| 254 | uc_info->type = PCM_HFP_CALL; |
| 255 | uc_info->stream.out = adev->primary_output; |
| 256 | uc_info->devices = adev->primary_output->devices; |
| 257 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 258 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 259 | |
| 260 | list_add_tail(&adev->usecase_list, &uc_info->list); |
| 261 | |
| 262 | select_devices(adev, hfpmod.ucid); |
| 263 | |
| 264 | pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); |
| 265 | pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); |
| 266 | pcm_dev_asm_rx_id = HFP_ASM_RX_TX; |
| 267 | pcm_dev_asm_tx_id = HFP_ASM_RX_TX; |
| 268 | if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 || |
| 269 | pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) { |
| 270 | ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)", |
| 271 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id); |
| 272 | ret = -EIO; |
| 273 | goto exit; |
| 274 | } |
| 275 | |
| 276 | ALOGV("%s: HFP PCM devices (hfp rx tx: %d pcm rx tx: %d) for the usecase(%d)", |
| 277 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id); |
| 278 | |
| 279 | ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 280 | __func__, adev->snd_card, pcm_dev_rx_id); |
| 281 | hfpmod.hfp_sco_rx = pcm_open(adev->snd_card, |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 282 | pcm_dev_asm_rx_id, |
| 283 | PCM_OUT, &pcm_config_hfp); |
| 284 | if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) { |
| 285 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx)); |
| 286 | ret = -EIO; |
| 287 | goto exit; |
| 288 | } |
| 289 | ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 290 | __func__, adev->snd_card, pcm_dev_tx_id); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 291 | |
| 292 | if (audio_extn_tfa_98xx_is_supported() == false) { |
| 293 | hfpmod.hfp_pcm_rx = pcm_open(adev->snd_card, |
| 294 | pcm_dev_rx_id, |
| 295 | PCM_OUT, &pcm_config_hfp); |
| 296 | if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) { |
| 297 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx)); |
| 298 | ret = -EIO; |
| 299 | goto exit; |
| 300 | } |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 301 | } |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 302 | hfpmod.hfp_sco_tx = pcm_open(adev->snd_card, |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 303 | pcm_dev_asm_tx_id, |
| 304 | PCM_IN, &pcm_config_hfp); |
| 305 | if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) { |
| 306 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx)); |
| 307 | ret = -EIO; |
| 308 | goto exit; |
| 309 | } |
| 310 | ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 311 | __func__, adev->snd_card, pcm_dev_tx_id); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 312 | |
| 313 | if (audio_extn_tfa_98xx_is_supported() == false) { |
| 314 | hfpmod.hfp_pcm_tx = pcm_open(adev->snd_card, |
| 315 | pcm_dev_tx_id, |
| 316 | PCM_IN, &pcm_config_hfp); |
| 317 | if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) { |
| 318 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx)); |
| 319 | ret = -EIO; |
| 320 | goto exit; |
| 321 | } |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 322 | } |
| 323 | pcm_start(hfpmod.hfp_sco_rx); |
| 324 | pcm_start(hfpmod.hfp_sco_tx); |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 325 | if (audio_extn_tfa_98xx_is_supported() == false) { |
| 326 | pcm_start(hfpmod.hfp_pcm_rx); |
| 327 | pcm_start(hfpmod.hfp_pcm_tx); |
| 328 | } |
| 329 | |
| 330 | audio_extn_tfa_98xx_enable_speaker(); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 331 | |
| 332 | hfpmod.is_hfp_running = true; |
| 333 | hfp_set_volume(adev, hfpmod.hfp_volume); |
| 334 | |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 335 | /* Set mic volume by mute status, we don't provide set mic volume in phone app, only |
| 336 | provide mute and unmute. */ |
| 337 | audio_extn_hfp_set_mic_mute(adev, adev->mic_muted); |
| 338 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 339 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 340 | return 0; |
| 341 | |
| 342 | exit: |
| 343 | stop_hfp(adev); |
| 344 | ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret); |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | static int32_t stop_hfp(struct audio_device *adev) |
| 349 | { |
| 350 | int32_t i, ret = 0; |
| 351 | struct audio_usecase *uc_info; |
| 352 | |
| 353 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 354 | hfpmod.is_hfp_running = false; |
| 355 | |
| 356 | /* 1. Close the PCM devices */ |
| 357 | if (hfpmod.hfp_sco_rx) { |
| 358 | pcm_close(hfpmod.hfp_sco_rx); |
| 359 | hfpmod.hfp_sco_rx = NULL; |
| 360 | } |
| 361 | if (hfpmod.hfp_sco_tx) { |
| 362 | pcm_close(hfpmod.hfp_sco_tx); |
| 363 | hfpmod.hfp_sco_tx = NULL; |
| 364 | } |
| 365 | if (hfpmod.hfp_pcm_rx) { |
| 366 | pcm_close(hfpmod.hfp_pcm_rx); |
| 367 | hfpmod.hfp_pcm_rx = NULL; |
| 368 | } |
| 369 | if (hfpmod.hfp_pcm_tx) { |
| 370 | pcm_close(hfpmod.hfp_pcm_tx); |
| 371 | hfpmod.hfp_pcm_tx = NULL; |
| 372 | } |
| 373 | |
| 374 | uc_info = get_usecase_from_list(adev, hfpmod.ucid); |
| 375 | if (uc_info == NULL) { |
| 376 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 377 | __func__, hfpmod.ucid); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | /* 2. Get and set stream specific mixer controls */ |
| 382 | disable_audio_route(adev, uc_info); |
| 383 | |
| 384 | /* 3. Disable the rx and tx devices */ |
| 385 | disable_snd_device(adev, uc_info->out_snd_device); |
| 386 | disable_snd_device(adev, uc_info->in_snd_device); |
| 387 | |
Uday Kishore Pasupuleti | a1f4805 | 2015-09-08 22:49:18 +0900 | [diff] [blame] | 388 | /* Disable the echo reference for HFP Tx */ |
| 389 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); |
| 390 | |
| 391 | /* Set the unmute Tx mixer control */ |
| 392 | if (voice_get_mic_mute(adev)) { |
| 393 | platform_set_mic_mute(adev->platform, false); |
| 394 | ALOGD("%s: unMute HFP Tx", __func__); |
| 395 | } |
| 396 | adev->enable_hfp = false; |
| 397 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 398 | list_remove(&uc_info->list); |
| 399 | free(uc_info); |
| 400 | |
| 401 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | bool audio_extn_hfp_is_active(struct audio_device *adev) |
| 406 | { |
| 407 | struct audio_usecase *hfp_usecase = NULL; |
| 408 | hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid); |
| 409 | |
| 410 | if (hfp_usecase != NULL) |
| 411 | return true; |
| 412 | else |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | audio_usecase_t audio_extn_hfp_get_usecase() |
| 417 | { |
| 418 | return hfpmod.ucid; |
| 419 | } |
| 420 | |
| 421 | void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms) |
| 422 | { |
| 423 | int ret; |
| 424 | int rate; |
| 425 | int val; |
| 426 | float vol; |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 427 | char value[AUDIO_PARAMATER_HFP_VALUE_MAX] = {0, }; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 428 | |
| 429 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value, |
| 430 | sizeof(value)); |
| 431 | if (ret >= 0) { |
| 432 | if (!strncmp(value,"true",sizeof(value))) |
| 433 | ret = start_hfp(adev,parms); |
| 434 | else |
| 435 | stop_hfp(adev); |
| 436 | } |
| 437 | memset(value, 0, sizeof(value)); |
| 438 | ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value, |
| 439 | sizeof(value)); |
| 440 | if (ret >= 0) { |
| 441 | rate = atoi(value); |
| 442 | if (rate == 8000){ |
| 443 | hfpmod.ucid = USECASE_AUDIO_HFP_SCO; |
| 444 | pcm_config_hfp.rate = rate; |
| 445 | } else if (rate == 16000){ |
| 446 | hfpmod.ucid = USECASE_AUDIO_HFP_SCO_WB; |
| 447 | pcm_config_hfp.rate = rate; |
| 448 | } else |
| 449 | ALOGE("Unsupported rate.."); |
| 450 | } |
| 451 | |
| 452 | if (hfpmod.is_hfp_running) { |
| 453 | memset(value, 0, sizeof(value)); |
| 454 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 455 | value, sizeof(value)); |
| 456 | if (ret >= 0) { |
| 457 | val = atoi(value); |
| 458 | if (val > 0) |
| 459 | select_devices(adev, hfpmod.ucid); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | memset(value, 0, sizeof(value)); |
Surendar karka | 56631b5 | 2016-07-13 15:08:39 -0700 | [diff] [blame] | 464 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_VOL_MIXER_CTL, |
| 465 | value, sizeof(value)); |
| 466 | if (ret >= 0) { |
| 467 | ALOGD("%s: mixer ctl name: %s", __func__, value); |
| 468 | strcpy(hfpmod.hfp_vol_mixer_ctl, value); |
| 469 | str_parms_del(parms, AUDIO_PARAMETER_HFP_VOL_MIXER_CTL); |
| 470 | } |
| 471 | |
| 472 | memset(value, 0, sizeof(value)); |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 473 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_VOLUME, |
| 474 | value, sizeof(value)); |
| 475 | if (ret >= 0) { |
| 476 | if (sscanf(value, "%f", &vol) != 1){ |
| 477 | ALOGE("%s: error in retrieving hfp volume", __func__); |
| 478 | ret = -EIO; |
| 479 | goto exit; |
| 480 | } |
| 481 | ALOGD("%s: set_hfp_volume usecase, Vol: [%f]", __func__, vol); |
| 482 | hfp_set_volume(adev, vol); |
| 483 | } |
Alain Vongsouvanh | 13f26e8 | 2016-11-18 14:39:11 -0800 | [diff] [blame^] | 484 | |
| 485 | memset(value, 0, sizeof(value)); |
| 486 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_MIC_VOLUME, |
| 487 | value, sizeof(value)); |
| 488 | if (ret >= 0) { |
| 489 | if (sscanf(value, "%f", &vol) != 1){ |
| 490 | ALOGE("%s: error in retrieving hfp mic volume", __func__); |
| 491 | ret = -EIO; |
| 492 | goto exit; |
| 493 | } |
| 494 | ALOGD("%s: set_hfp_mic_volume usecase, Vol: [%f]", __func__, vol); |
| 495 | hfp_set_mic_volume(adev, vol); |
| 496 | } |
| 497 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 498 | exit: |
| 499 | ALOGV("%s Exit",__func__); |
| 500 | } |