Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved. |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 2 | |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions are |
| 5 | met: |
| 6 | * Redistributions of source code must retain the above copyright |
| 7 | notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above |
| 9 | copyright notice, this list of conditions and the following |
| 10 | disclaimer in the documentation and/or other materials provided |
| 11 | with the distribution. |
| 12 | * Neither the name of The Linux Foundation nor the names of its |
| 13 | contributors may be used to endorse or promote products derived |
| 14 | from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ |
| 27 | |
| 28 | #define LOG_TAG "audio_hw_hfp" |
| 29 | /*#define LOG_NDEBUG 0*/ |
| 30 | #define LOG_NDDEBUG 0 |
| 31 | |
| 32 | #include <errno.h> |
| 33 | #include <math.h> |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 34 | #include <log/log.h> |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 35 | |
| 36 | #include "audio_hw.h" |
| 37 | #include "platform.h" |
| 38 | #include "platform_api.h" |
| 39 | #include <stdlib.h> |
| 40 | #include <cutils/str_parms.h> |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 41 | #include "audio_extn.h" |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 42 | |
Revathi Uddaraju | 1eac8b0 | 2017-05-18 17:13:33 +0530 | [diff] [blame] | 43 | #ifdef DYNAMIC_LOG_ENABLED |
| 44 | #include <log_xml_parser.h> |
| 45 | #define LOG_MASK HAL_MOD_FILE_HFP |
| 46 | #include <log_utils.h> |
| 47 | #endif |
| 48 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 49 | #ifdef HFP_ENABLED |
| 50 | #define AUDIO_PARAMETER_HFP_ENABLE "hfp_enable" |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 51 | #define AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE "hfp_set_sampling_rate" |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 52 | #define AUDIO_PARAMETER_KEY_HFP_VOLUME "hfp_volume" |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 53 | #define AUDIO_PARAMETER_HFP_PCM_DEV_ID "hfp_pcm_dev_id" |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 54 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 55 | #define AUDIO_PARAMETER_KEY_HFP_MIC_VOLUME "hfp_mic_volume" |
| 56 | #define PLAYBACK_VOLUME_MAX 0x2000 |
| 57 | #define CAPTURE_VOLUME_DEFAULT (15.0) |
| 58 | |
Sudheer Papothi | 19e43d0 | 2014-07-16 02:34:41 +0530 | [diff] [blame] | 59 | #ifdef PLATFORM_MSM8994 |
| 60 | #define HFP_RX_VOLUME "SEC AUXPCM LOOPBACK Volume" |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 61 | #elif defined (PLATFORM_MSM8996) || defined (EXTERNAL_BT_SUPPORTED) |
Banajit Goswami | da77c45 | 2015-10-14 20:45:22 -0700 | [diff] [blame] | 62 | #define HFP_RX_VOLUME "PRI AUXPCM LOOPBACK Volume" |
Derek Chen | 3e5b30a | 2018-10-24 01:04:25 -0700 | [diff] [blame] | 63 | #elif defined PLATFORM_AUTO |
| 64 | #define HFP_RX_VOLUME "Playback 36 Volume" |
Meng Wang | ef2f6e1 | 2018-10-08 13:06:05 +0800 | [diff] [blame^] | 65 | #elif defined (PLATFORM_MSM8998) || defined (PLATFORM_MSMFALCON) || \ |
| 66 | defined (PLATFORM_SDM845) || defined (PLATFORM_SDM710) || \ |
| 67 | defined (PLATFORM_QCS605) || defined (PLATFORM_MSMNILE) || \ |
| 68 | defined (PLATFORM_KONA) || defined (PLATFORM_MSMSTEPPE) || \ |
| 69 | defined (PLATFORM_QCS405) |
Banajit Goswami | 4c0dff2 | 2016-03-04 18:31:22 -0800 | [diff] [blame] | 70 | #define HFP_RX_VOLUME "SLIMBUS_7 LOOPBACK Volume" |
Sudheer Papothi | 19e43d0 | 2014-07-16 02:34:41 +0530 | [diff] [blame] | 71 | #else |
| 72 | #define HFP_RX_VOLUME "Internal HFP RX Volume" |
| 73 | #endif |
| 74 | |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 75 | static int32_t start_hfp(struct audio_device *adev, |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 76 | struct str_parms *parms); |
| 77 | |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 78 | static int32_t stop_hfp(struct audio_device *adev); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 79 | |
| 80 | struct hfp_module { |
| 81 | struct pcm *hfp_sco_rx; |
| 82 | struct pcm *hfp_sco_tx; |
| 83 | struct pcm *hfp_pcm_rx; |
| 84 | struct pcm *hfp_pcm_tx; |
| 85 | bool is_hfp_running; |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 86 | float hfp_volume; |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 87 | int32_t hfp_pcm_dev_id; |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 88 | audio_usecase_t ucid; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 89 | float mic_volume; |
| 90 | bool mic_mute; |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct hfp_module hfpmod = { |
| 94 | .hfp_sco_rx = NULL, |
| 95 | .hfp_sco_tx = NULL, |
| 96 | .hfp_pcm_rx = NULL, |
| 97 | .hfp_pcm_tx = NULL, |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 98 | .is_hfp_running = 0, |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 99 | .hfp_volume = 0, |
| 100 | .hfp_pcm_dev_id = HFP_ASM_RX_TX, |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 101 | .ucid = USECASE_AUDIO_HFP_SCO, |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 102 | .mic_volume = CAPTURE_VOLUME_DEFAULT, |
| 103 | .mic_mute = 0, |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 104 | }; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 105 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 106 | static struct pcm_config pcm_config_hfp = { |
| 107 | .channels = 1, |
| 108 | .rate = 8000, |
| 109 | .period_size = 240, |
| 110 | .period_count = 2, |
| 111 | .format = PCM_FORMAT_S16_LE, |
| 112 | .start_threshold = 0, |
| 113 | .stop_threshold = INT_MAX, |
| 114 | .avail_min = 0, |
| 115 | }; |
| 116 | |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 117 | static int32_t hfp_set_volume(struct audio_device *adev, float value) |
| 118 | { |
| 119 | int32_t vol, ret = 0; |
| 120 | struct mixer_ctl *ctl; |
Sudheer Papothi | 19e43d0 | 2014-07-16 02:34:41 +0530 | [diff] [blame] | 121 | const char *mixer_ctl_name = HFP_RX_VOLUME; |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 122 | |
| 123 | ALOGV("%s: entry", __func__); |
| 124 | ALOGD("%s: (%f)\n", __func__, value); |
| 125 | |
Satya Krishna Pindiproli | 3ed6d79 | 2014-09-09 15:32:25 +0530 | [diff] [blame] | 126 | hfpmod.hfp_volume = value; |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 127 | |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 128 | if (value < 0.0) { |
| 129 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 130 | value = 0.0; |
| 131 | } else { |
| 132 | value = ((value > 15.000000) ? 1.0 : (value / 15)); |
| 133 | ALOGW("%s: Volume brought with in range (%f)\n", __func__, value); |
| 134 | } |
| 135 | vol = lrint((value * 0x2000) + 0.5); |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 136 | |
| 137 | if (!hfpmod.is_hfp_running) { |
| 138 | ALOGV("%s: HFP not active, ignoring set_hfp_volume call", __func__); |
| 139 | return -EIO; |
| 140 | } |
| 141 | |
| 142 | ALOGD("%s: Setting HFP volume to %d \n", __func__, vol); |
| 143 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 144 | if (!ctl) { |
| 145 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 146 | __func__, mixer_ctl_name); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | if(mixer_ctl_set_value(ctl, 0, vol) < 0) { |
| 150 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, vol); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | ALOGV("%s: exit", __func__); |
| 155 | return ret; |
| 156 | } |
| 157 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 158 | /*Set mic volume to value. |
| 159 | * |
| 160 | * This interface is used for mic volume control, set mic volume as value(range 0 ~ 15). |
| 161 | */ |
| 162 | static int hfp_set_mic_volume(struct audio_device *adev, float value) |
| 163 | { |
| 164 | int volume, ret = 0; |
| 165 | char mixer_ctl_name[128]; |
| 166 | struct mixer_ctl *ctl; |
| 167 | int pcm_device_id = HFP_ASM_RX_TX; |
| 168 | |
| 169 | if (!hfpmod.is_hfp_running) { |
| 170 | ALOGE("%s: HFP not active, ignoring set_hfp_mic_volume call", __func__); |
| 171 | return -EIO; |
| 172 | } |
| 173 | |
| 174 | if (value < 0.0) { |
| 175 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 176 | value = 0.0; |
| 177 | } else if (value > CAPTURE_VOLUME_DEFAULT) { |
| 178 | value = CAPTURE_VOLUME_DEFAULT; |
| 179 | ALOGW("%s: Volume brought within range (%f)\n", __func__, value); |
| 180 | } |
| 181 | |
| 182 | value = value / CAPTURE_VOLUME_DEFAULT; |
| 183 | memset(mixer_ctl_name, 0, sizeof(mixer_ctl_name)); |
| 184 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 185 | "Playback %d Volume", pcm_device_id); |
| 186 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 187 | if (!ctl) { |
| 188 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 189 | __func__, mixer_ctl_name); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | volume = (int)(value * PLAYBACK_VOLUME_MAX); |
| 193 | |
| 194 | ALOGD("%s: Setting volume to %d (%s)\n", __func__, volume, mixer_ctl_name); |
| 195 | if (mixer_ctl_set_value(ctl, 0, volume) < 0) { |
| 196 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, volume); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static float hfp_get_mic_volume(struct audio_device *adev) |
| 204 | { |
| 205 | int volume; |
| 206 | char mixer_ctl_name[128]; |
| 207 | struct mixer_ctl *ctl; |
| 208 | int pcm_device_id = HFP_ASM_RX_TX; |
| 209 | float value = 0.0; |
| 210 | |
| 211 | if (!hfpmod.is_hfp_running) { |
| 212 | ALOGE("%s: HFP not active, ignoring set_hfp_mic_volume call", __func__); |
| 213 | return -EIO; |
| 214 | } |
| 215 | |
| 216 | memset(mixer_ctl_name, 0, sizeof(mixer_ctl_name)); |
| 217 | snprintf(mixer_ctl_name, sizeof(mixer_ctl_name), |
| 218 | "Playback %d Volume", pcm_device_id); |
| 219 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 220 | if (!ctl) { |
| 221 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 222 | __func__, mixer_ctl_name); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | volume = mixer_ctl_get_value(ctl, 0); |
| 227 | if ( volume < 0) { |
| 228 | ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, volume); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | ALOGD("%s: getting mic volume %d \n", __func__, volume); |
| 232 | |
| 233 | value = (volume / PLAYBACK_VOLUME_MAX) * CAPTURE_VOLUME_DEFAULT; |
| 234 | if (value < 0.0) { |
| 235 | ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value); |
| 236 | value = 0.0; |
| 237 | } else if (value > CAPTURE_VOLUME_DEFAULT) { |
| 238 | value = CAPTURE_VOLUME_DEFAULT; |
| 239 | ALOGW("%s: Volume brought within range (%f)\n", __func__, value); |
| 240 | } |
| 241 | |
| 242 | return value; |
| 243 | } |
| 244 | |
| 245 | /*Set mic mute state. |
| 246 | * |
| 247 | * This interface is used for mic mute state control |
| 248 | */ |
| 249 | int audio_extn_hfp_set_mic_mute(struct audio_device *adev, bool state) |
| 250 | { |
| 251 | int rc = 0; |
| 252 | |
| 253 | if (state == hfpmod.mic_mute) |
| 254 | return rc; |
| 255 | |
| 256 | if (state == true) { |
| 257 | hfpmod.mic_volume = hfp_get_mic_volume(adev); |
| 258 | } |
| 259 | rc = hfp_set_mic_volume(adev, (state == true) ? 0.0 : hfpmod.mic_volume); |
| 260 | adev->voice.mic_mute = state; |
| 261 | hfpmod.mic_mute = state; |
| 262 | ALOGD("%s: Setting mute state %d, rc %d\n", __func__, state, rc); |
| 263 | return rc; |
| 264 | } |
| 265 | |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 266 | static int32_t start_hfp(struct audio_device *adev, |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 267 | struct str_parms *parms __unused) |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 268 | { |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 269 | int32_t ret = 0; |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 270 | struct audio_usecase *uc_info; |
| 271 | int32_t pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, pcm_dev_asm_tx_id; |
| 272 | |
| 273 | ALOGD("%s: enter", __func__); |
| 274 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 275 | if (adev->enable_hfp == true) { |
| 276 | ALOGD("%s: HFP is already active!\n", __func__); |
| 277 | return 0; |
| 278 | } |
| 279 | adev->enable_hfp = true; |
| 280 | platform_set_mic_mute(adev->platform, false); |
| 281 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 282 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
Haynes Mathew George | b51ceb1 | 2014-06-30 13:56:18 -0700 | [diff] [blame] | 283 | |
| 284 | if (!uc_info) |
| 285 | return -ENOMEM; |
| 286 | |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 287 | uc_info->id = hfpmod.ucid; |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 288 | uc_info->type = PCM_HFP_CALL; |
| 289 | uc_info->stream.out = adev->primary_output; |
| 290 | uc_info->devices = adev->primary_output->devices; |
| 291 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 292 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 293 | |
| 294 | list_add_tail(&adev->usecase_list, &uc_info->list); |
| 295 | |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 296 | select_devices(adev, hfpmod.ucid); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 297 | |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 298 | if ((uc_info->out_snd_device != SND_DEVICE_NONE) || |
| 299 | (uc_info->in_snd_device != SND_DEVICE_NONE)) { |
| 300 | if (audio_extn_ext_hw_plugin_usecase_start(adev->ext_hw_plugin, uc_info)) |
| 301 | ALOGE("%s: failed to start ext hw plugin", __func__); |
| 302 | } |
| 303 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 304 | pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); |
| 305 | pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE); |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 306 | pcm_dev_asm_rx_id = hfpmod.hfp_pcm_dev_id; |
| 307 | pcm_dev_asm_tx_id = hfpmod.hfp_pcm_dev_id; |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 308 | if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 || |
| 309 | pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) { |
| 310 | ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)", |
| 311 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id); |
| 312 | ret = -EIO; |
| 313 | goto exit; |
| 314 | } |
| 315 | |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 316 | ALOGD("%s: HFP PCM devices (rx: %d tx: %d pcm dev id: %d) usecase(%d)", |
| 317 | __func__, pcm_dev_rx_id, pcm_dev_tx_id, hfpmod.hfp_pcm_dev_id, uc_info->id); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 318 | |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 319 | hfpmod.hfp_sco_rx = pcm_open(adev->snd_card, |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 320 | pcm_dev_asm_rx_id, |
| 321 | PCM_OUT, &pcm_config_hfp); |
| 322 | if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) { |
| 323 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx)); |
| 324 | ret = -EIO; |
| 325 | goto exit; |
| 326 | } |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 327 | |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 328 | hfpmod.hfp_pcm_rx = pcm_open(adev->snd_card, |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 329 | pcm_dev_rx_id, |
| 330 | PCM_OUT, &pcm_config_hfp); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 331 | if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) { |
| 332 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx)); |
| 333 | ret = -EIO; |
| 334 | goto exit; |
| 335 | } |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 336 | |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 337 | hfpmod.hfp_sco_tx = pcm_open(adev->snd_card, |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 338 | pcm_dev_asm_tx_id, |
| 339 | PCM_IN, &pcm_config_hfp); |
| 340 | if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) { |
| 341 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx)); |
| 342 | ret = -EIO; |
| 343 | goto exit; |
| 344 | } |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 345 | |
Apoorv Raghuvanshi | 84fa2fe | 2013-12-04 11:57:47 -0800 | [diff] [blame] | 346 | hfpmod.hfp_pcm_tx = pcm_open(adev->snd_card, |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 347 | pcm_dev_tx_id, |
| 348 | PCM_IN, &pcm_config_hfp); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 349 | if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) { |
| 350 | ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx)); |
| 351 | ret = -EIO; |
| 352 | goto exit; |
| 353 | } |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 354 | |
Satya Krishna Pindiproli | ce903a0 | 2014-07-28 12:40:56 +0530 | [diff] [blame] | 355 | if (pcm_start(hfpmod.hfp_sco_rx) < 0) { |
| 356 | ALOGE("%s: pcm start for hfp sco rx failed", __func__); |
| 357 | ret = -EINVAL; |
| 358 | goto exit; |
| 359 | } |
| 360 | if (pcm_start(hfpmod.hfp_sco_tx) < 0) { |
| 361 | ALOGE("%s: pcm start for hfp sco tx failed", __func__); |
| 362 | ret = -EINVAL; |
| 363 | goto exit; |
| 364 | } |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 365 | |
Satya Krishna Pindiproli | ce903a0 | 2014-07-28 12:40:56 +0530 | [diff] [blame] | 366 | if (pcm_start(hfpmod.hfp_pcm_rx) < 0) { |
| 367 | ALOGE("%s: pcm start for hfp pcm rx failed", __func__); |
| 368 | ret = -EINVAL; |
| 369 | goto exit; |
| 370 | } |
| 371 | if (pcm_start(hfpmod.hfp_pcm_tx) < 0) { |
| 372 | ALOGE("%s: pcm start for hfp pcm tx failed", __func__); |
| 373 | ret = -EINVAL; |
| 374 | goto exit; |
| 375 | } |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 376 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 377 | hfpmod.is_hfp_running = true; |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 378 | hfp_set_volume(adev, hfpmod.hfp_volume); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 379 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 380 | /* Set mic volume by mute status, we don't provide set mic volume in phone app, only |
| 381 | provide mute and unmute. */ |
| 382 | audio_extn_hfp_set_mic_mute(adev, adev->mic_muted); |
| 383 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 384 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 385 | return 0; |
| 386 | |
| 387 | exit: |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 388 | stop_hfp(adev); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 389 | ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret); |
| 390 | return ret; |
| 391 | } |
| 392 | |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 393 | static int32_t stop_hfp(struct audio_device *adev) |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 394 | { |
Satya Krishna Pindiproli | f1cd92b | 2016-04-14 19:05:23 +0530 | [diff] [blame] | 395 | int32_t ret = 0; |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 396 | struct audio_usecase *uc_info; |
| 397 | |
| 398 | ALOGD("%s: enter", __func__); |
| 399 | hfpmod.is_hfp_running = false; |
| 400 | |
| 401 | /* 1. Close the PCM devices */ |
| 402 | if (hfpmod.hfp_sco_rx) { |
| 403 | pcm_close(hfpmod.hfp_sco_rx); |
| 404 | hfpmod.hfp_sco_rx = NULL; |
| 405 | } |
| 406 | if (hfpmod.hfp_sco_tx) { |
| 407 | pcm_close(hfpmod.hfp_sco_tx); |
| 408 | hfpmod.hfp_sco_tx = NULL; |
| 409 | } |
| 410 | if (hfpmod.hfp_pcm_rx) { |
| 411 | pcm_close(hfpmod.hfp_pcm_rx); |
| 412 | hfpmod.hfp_pcm_rx = NULL; |
| 413 | } |
| 414 | if (hfpmod.hfp_pcm_tx) { |
| 415 | pcm_close(hfpmod.hfp_pcm_tx); |
| 416 | hfpmod.hfp_pcm_tx = NULL; |
| 417 | } |
| 418 | |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 419 | uc_info = get_usecase_from_list(adev, hfpmod.ucid); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 420 | if (uc_info == NULL) { |
| 421 | ALOGE("%s: Could not find the usecase (%d) in the list", |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 422 | __func__, hfpmod.ucid); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | } |
| 425 | |
Derek Chen | d253007 | 2014-11-24 12:39:14 -0800 | [diff] [blame] | 426 | if ((uc_info->out_snd_device != SND_DEVICE_NONE) || |
| 427 | (uc_info->in_snd_device != SND_DEVICE_NONE)) { |
| 428 | if (audio_extn_ext_hw_plugin_usecase_stop(adev->ext_hw_plugin, uc_info)) |
| 429 | ALOGE("%s: failed to stop ext hw plugin", __func__); |
| 430 | } |
| 431 | |
Venkata Narendra Kumar Gutta | 1bbbf54 | 2014-09-04 19:11:25 +0530 | [diff] [blame] | 432 | /* 2. Disable echo reference while stopping hfp */ |
Apoorv Raghuvanshi | 924b302 | 2015-07-06 15:07:14 -0700 | [diff] [blame] | 433 | platform_set_echo_reference(adev, false, uc_info->devices); |
Venkata Narendra Kumar Gutta | 1bbbf54 | 2014-09-04 19:11:25 +0530 | [diff] [blame] | 434 | |
| 435 | /* 3. Get and set stream specific mixer controls */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 436 | disable_audio_route(adev, uc_info); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 437 | |
Venkata Narendra Kumar Gutta | 1bbbf54 | 2014-09-04 19:11:25 +0530 | [diff] [blame] | 438 | /* 4. Disable the rx and tx devices */ |
Haynes Mathew George | 1376ca6 | 2014-04-24 11:55:48 -0700 | [diff] [blame] | 439 | disable_snd_device(adev, uc_info->out_snd_device); |
| 440 | disable_snd_device(adev, uc_info->in_snd_device); |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 441 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 442 | /* Set the unmute Tx mixer control */ |
| 443 | if (voice_get_mic_mute(adev)) { |
| 444 | platform_set_mic_mute(adev->platform, false); |
| 445 | ALOGD("%s: unMute HFP Tx", __func__); |
| 446 | } |
| 447 | adev->enable_hfp = false; |
| 448 | |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 449 | list_remove(&uc_info->list); |
| 450 | free(uc_info); |
| 451 | |
| 452 | ALOGD("%s: exit: status(%d)", __func__, ret); |
| 453 | return ret; |
| 454 | } |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 455 | |
Vimal Puthanveed | 37b4a1c | 2014-01-07 16:47:47 -0800 | [diff] [blame] | 456 | bool audio_extn_hfp_is_active(struct audio_device *adev) |
| 457 | { |
| 458 | struct audio_usecase *hfp_usecase = NULL; |
Vimal Puthanveed | 41fcff2 | 2014-01-23 15:56:53 -0800 | [diff] [blame] | 459 | hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid); |
Vimal Puthanveed | 37b4a1c | 2014-01-07 16:47:47 -0800 | [diff] [blame] | 460 | |
| 461 | if (hfp_usecase != NULL) |
| 462 | return true; |
| 463 | else |
| 464 | return false; |
| 465 | } |
| 466 | |
Dhanalakshmi Siddani | 823dc5a | 2016-09-28 14:47:26 +0530 | [diff] [blame] | 467 | int hfp_set_mic_mute(struct audio_device *adev, bool state) |
| 468 | { |
| 469 | struct mixer_ctl *ctl; |
| 470 | const char *mixer_ctl_name = "HFP TX Mute"; |
Manish Dewangan | 338c50a | 2017-09-12 15:22:03 +0530 | [diff] [blame] | 471 | long set_values[ ] = {0}; |
Dhanalakshmi Siddani | 823dc5a | 2016-09-28 14:47:26 +0530 | [diff] [blame] | 472 | |
| 473 | ALOGI("%s: enter, state=%d", __func__, state); |
| 474 | |
| 475 | set_values[0] = state; |
| 476 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 477 | if (!ctl) { |
| 478 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 479 | __func__, mixer_ctl_name); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | mixer_ctl_set_array(ctl, set_values, ARRAY_SIZE(set_values)); |
| 483 | ALOGV("%s: exit", __func__); |
| 484 | return 0; |
| 485 | } |
| 486 | |
Vimal Puthanveed | 41fcff2 | 2014-01-23 15:56:53 -0800 | [diff] [blame] | 487 | audio_usecase_t audio_extn_hfp_get_usecase() |
| 488 | { |
| 489 | return hfpmod.ucid; |
| 490 | } |
| 491 | |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 492 | void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms) |
| 493 | { |
| 494 | int ret; |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 495 | int rate; |
Vimal Puthanveed | 21e5c76 | 2014-01-08 14:10:09 -0800 | [diff] [blame] | 496 | int val; |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 497 | float vol; |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 498 | char value[32]={0}; |
| 499 | |
| 500 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value, |
| 501 | sizeof(value)); |
| 502 | if (ret >= 0) { |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 503 | if (!strncmp(value, "true", sizeof(value)) && !hfpmod.is_hfp_running) |
| 504 | ret = start_hfp(adev,parms); |
| 505 | else if (!strncmp(value, "false", sizeof(value)) && hfpmod.is_hfp_running) |
| 506 | stop_hfp(adev); |
| 507 | else |
| 508 | ALOGE("hfp_enable=%s is unsupported", value); |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 509 | } |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 510 | memset(value, 0, sizeof(value)); |
| 511 | ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value, |
| 512 | sizeof(value)); |
| 513 | if (ret >= 0) { |
| 514 | rate = atoi(value); |
| 515 | if (rate == 8000){ |
| 516 | hfpmod.ucid = USECASE_AUDIO_HFP_SCO; |
| 517 | pcm_config_hfp.rate = rate; |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 518 | } else if (rate == 16000){ |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 519 | hfpmod.ucid = USECASE_AUDIO_HFP_SCO_WB; |
| 520 | pcm_config_hfp.rate = rate; |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 521 | } else |
Vimal Puthanveed | 47e6485 | 2013-12-20 13:23:39 -0800 | [diff] [blame] | 522 | ALOGE("Unsupported rate.."); |
| 523 | } |
Vimal Puthanveed | 21e5c76 | 2014-01-08 14:10:09 -0800 | [diff] [blame] | 524 | |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 525 | if (hfpmod.is_hfp_running) { |
Vimal Puthanveed | 21e5c76 | 2014-01-08 14:10:09 -0800 | [diff] [blame] | 526 | memset(value, 0, sizeof(value)); |
| 527 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 528 | value, sizeof(value)); |
| 529 | if (ret >= 0) { |
| 530 | val = atoi(value); |
Ravi Kumar Alamanda | bdf1416 | 2014-09-05 16:14:17 -0700 | [diff] [blame] | 531 | if (val > 0) |
Vimal Puthanveed | 21e5c76 | 2014-01-08 14:10:09 -0800 | [diff] [blame] | 532 | select_devices(adev, hfpmod.ucid); |
| 533 | } |
| 534 | } |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 535 | |
| 536 | memset(value, 0, sizeof(value)); |
| 537 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_VOLUME, |
| 538 | value, sizeof(value)); |
| 539 | if (ret >= 0) { |
| 540 | if (sscanf(value, "%f", &vol) != 1){ |
| 541 | ALOGE("%s: error in retrieving hfp volume", __func__); |
| 542 | ret = -EIO; |
| 543 | goto exit; |
| 544 | } |
| 545 | ALOGD("%s: set_hfp_volume usecase, Vol: [%f]", __func__, vol); |
| 546 | hfp_set_volume(adev, vol); |
| 547 | } |
Satya Krishna Pindiproli | c6b0a74 | 2017-02-03 14:37:18 +0530 | [diff] [blame] | 548 | |
| 549 | memset(value, 0, sizeof(value)); |
| 550 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_PCM_DEV_ID, value, sizeof(value)); |
| 551 | if (ret >= 0) { |
| 552 | hfpmod.hfp_pcm_dev_id = atoi(value); |
| 553 | ALOGD("Updating HFP_PCM_DEV_ID as %d from platform XML", hfpmod.hfp_pcm_dev_id); |
| 554 | str_parms_del(parms, AUDIO_PARAMETER_HFP_PCM_DEV_ID); |
| 555 | } |
| 556 | |
Aalique Grahame | 22e4910 | 2018-12-18 14:23:57 -0800 | [diff] [blame] | 557 | memset(value, 0, sizeof(value)); |
| 558 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_MIC_VOLUME, |
| 559 | value, sizeof(value)); |
| 560 | if (ret >= 0) { |
| 561 | if (sscanf(value, "%f", &vol) != 1){ |
| 562 | ALOGE("%s: error in retrieving hfp mic volume", __func__); |
| 563 | ret = -EIO; |
| 564 | goto exit; |
| 565 | } |
| 566 | ALOGD("%s: set_hfp_mic_volume usecase, Vol: [%f]", __func__, vol); |
| 567 | hfp_set_mic_volume(adev, vol); |
| 568 | } |
| 569 | |
Amit Shekhar | 967cab3 | 2014-02-07 17:03:21 -0800 | [diff] [blame] | 570 | exit: |
| 571 | ALOGV("%s Exit",__func__); |
Vimal Puthanveed | 584048b | 2013-12-11 17:00:50 -0800 | [diff] [blame] | 572 | } |
Vimal Puthanveed | 5b4d3f1 | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 573 | #endif /*HFP_ENABLED*/ |