blob: 66d1f9b0b2f5bacc112342f379dfa8bbcb2937bc [file] [log] [blame]
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08001/*
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>
30
31#define AUDIO_PARAMETER_HFP_ENABLE "hfp_enable"
32#define AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE "hfp_set_sampling_rate"
33#define AUDIO_PARAMETER_KEY_HFP_VOLUME "hfp_volume"
34
35static int32_t start_hfp(struct audio_device *adev,
36 struct str_parms *parms);
37
38static int32_t stop_hfp(struct audio_device *adev);
39
40struct hfp_module {
41 struct pcm *hfp_sco_rx;
42 struct pcm *hfp_sco_tx;
43 struct pcm *hfp_pcm_rx;
44 struct pcm *hfp_pcm_tx;
45 float hfp_volume;
46 bool is_hfp_running;
47 audio_usecase_t ucid;
48};
49
50static struct hfp_module hfpmod = {
51 .hfp_sco_rx = NULL,
52 .hfp_sco_tx = NULL,
53 .hfp_pcm_rx = NULL,
54 .hfp_pcm_tx = NULL,
55 .hfp_volume = 0,
56 .is_hfp_running = 0,
57 .ucid = USECASE_AUDIO_HFP_SCO,
58};
59static struct pcm_config pcm_config_hfp = {
60 .channels = 1,
61 .rate = 8000,
62 .period_size = 240,
63 .period_count = 2,
64 .format = PCM_FORMAT_S16_LE,
65 .start_threshold = 0,
66 .stop_threshold = INT_MAX,
67 .avail_min = 0,
68};
69
70static int32_t hfp_set_volume(struct audio_device *adev, float value)
71{
72 int32_t vol, ret = 0;
73 struct mixer_ctl *ctl;
Uday Kishore Pasupuleti28ddc6f2015-08-21 11:51:52 -070074#ifdef EXTERNAL_BT_SUPPORTED
75 const char *mixer_ctl_name = "PRI AUXPCM LOOPBACK Volume";
76#else
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080077 const char *mixer_ctl_name = "Internal HFP RX Volume";
Uday Kishore Pasupuleti28ddc6f2015-08-21 11:51:52 -070078#endif
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080079
80 ALOGV("%s: entry", __func__);
81 ALOGD("%s: (%f)\n", __func__, value);
82
83 if (value < 0.0) {
84 ALOGW("%s: (%f) Under 0.0, assuming 0.0\n", __func__, value);
85 value = 0.0;
86 } else {
87 value = ((value > 15.000000) ? 1.0 : (value / 15));
88 ALOGW("%s: Volume brought with in range (%f)\n", __func__, value);
89 }
90 vol = lrint((value * 0x2000) + 0.5);
91 hfpmod.hfp_volume = value;
92
93 if (!hfpmod.is_hfp_running) {
94 ALOGV("%s: HFP not active, ignoring set_hfp_volume call", __func__);
95 return -EIO;
96 }
97
98 ALOGD("%s: Setting HFP volume to %d \n", __func__, vol);
99 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
100 if (!ctl) {
101 ALOGE("%s: Could not get ctl for mixer cmd - %s",
102 __func__, mixer_ctl_name);
103 return -EINVAL;
104 }
105 if(mixer_ctl_set_value(ctl, 0, vol) < 0) {
106 ALOGE("%s: Couldn't set HFP Volume: [%d]", __func__, vol);
107 return -EINVAL;
108 }
109
110 ALOGV("%s: exit", __func__);
111 return ret;
112}
113
114static int32_t start_hfp(struct audio_device *adev,
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -0700115 struct str_parms *parms __unused)
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800116{
117 int32_t i, ret = 0;
118 struct audio_usecase *uc_info;
119 int32_t pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, pcm_dev_asm_tx_id;
120
121 ALOGD("%s: enter", __func__);
Uday Kishore Pasupuleti76297192015-09-18 08:39:43 -0700122 adev->enable_hfp = true;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800123
124 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
125 uc_info->id = hfpmod.ucid;
126 uc_info->type = PCM_HFP_CALL;
127 uc_info->stream.out = adev->primary_output;
128 uc_info->devices = adev->primary_output->devices;
129 uc_info->in_snd_device = SND_DEVICE_NONE;
130 uc_info->out_snd_device = SND_DEVICE_NONE;
131
132 list_add_tail(&adev->usecase_list, &uc_info->list);
133
134 select_devices(adev, hfpmod.ucid);
135
136 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
137 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
138 pcm_dev_asm_rx_id = HFP_ASM_RX_TX;
139 pcm_dev_asm_tx_id = HFP_ASM_RX_TX;
140 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 ||
141 pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) {
142 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)",
143 __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id);
144 ret = -EIO;
145 goto exit;
146 }
147
148 ALOGV("%s: HFP PCM devices (hfp rx tx: %d pcm rx tx: %d) for the usecase(%d)",
149 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
150
151 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700152 __func__, adev->snd_card, pcm_dev_rx_id);
153 hfpmod.hfp_sco_rx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800154 pcm_dev_asm_rx_id,
155 PCM_OUT, &pcm_config_hfp);
156 if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) {
157 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx));
158 ret = -EIO;
159 goto exit;
160 }
161 ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700162 __func__, adev->snd_card, pcm_dev_tx_id);
163 hfpmod.hfp_pcm_rx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800164 pcm_dev_rx_id,
165 PCM_OUT, &pcm_config_hfp);
166 if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) {
167 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx));
168 ret = -EIO;
169 goto exit;
170 }
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700171 hfpmod.hfp_sco_tx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800172 pcm_dev_asm_tx_id,
173 PCM_IN, &pcm_config_hfp);
174 if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) {
175 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx));
176 ret = -EIO;
177 goto exit;
178 }
179 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700180 __func__, adev->snd_card, pcm_dev_tx_id);
181 hfpmod.hfp_pcm_tx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800182 pcm_dev_tx_id,
183 PCM_IN, &pcm_config_hfp);
184 if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) {
185 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx));
186 ret = -EIO;
187 goto exit;
188 }
189 pcm_start(hfpmod.hfp_sco_rx);
190 pcm_start(hfpmod.hfp_sco_tx);
191 pcm_start(hfpmod.hfp_pcm_rx);
192 pcm_start(hfpmod.hfp_pcm_tx);
193
194 hfpmod.is_hfp_running = true;
195 hfp_set_volume(adev, hfpmod.hfp_volume);
196
197 ALOGD("%s: exit: status(%d)", __func__, ret);
198 return 0;
199
200exit:
201 stop_hfp(adev);
202 ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret);
203 return ret;
204}
205
206static int32_t stop_hfp(struct audio_device *adev)
207{
208 int32_t i, ret = 0;
209 struct audio_usecase *uc_info;
210
211 ALOGD("%s: enter", __func__);
Uday Kishore Pasupuleti76297192015-09-18 08:39:43 -0700212 adev->enable_hfp = false;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800213 hfpmod.is_hfp_running = false;
214
215 /* 1. Close the PCM devices */
216 if (hfpmod.hfp_sco_rx) {
217 pcm_close(hfpmod.hfp_sco_rx);
218 hfpmod.hfp_sco_rx = NULL;
219 }
220 if (hfpmod.hfp_sco_tx) {
221 pcm_close(hfpmod.hfp_sco_tx);
222 hfpmod.hfp_sco_tx = NULL;
223 }
224 if (hfpmod.hfp_pcm_rx) {
225 pcm_close(hfpmod.hfp_pcm_rx);
226 hfpmod.hfp_pcm_rx = NULL;
227 }
228 if (hfpmod.hfp_pcm_tx) {
229 pcm_close(hfpmod.hfp_pcm_tx);
230 hfpmod.hfp_pcm_tx = NULL;
231 }
232
233 uc_info = get_usecase_from_list(adev, hfpmod.ucid);
234 if (uc_info == NULL) {
235 ALOGE("%s: Could not find the usecase (%d) in the list",
236 __func__, hfpmod.ucid);
237 return -EINVAL;
238 }
239
240 /* 2. Get and set stream specific mixer controls */
241 disable_audio_route(adev, uc_info);
242
243 /* 3. Disable the rx and tx devices */
244 disable_snd_device(adev, uc_info->out_snd_device);
245 disable_snd_device(adev, uc_info->in_snd_device);
246
247 list_remove(&uc_info->list);
248 free(uc_info);
249
250 ALOGD("%s: exit: status(%d)", __func__, ret);
251 return ret;
252}
253
254bool audio_extn_hfp_is_active(struct audio_device *adev)
255{
256 struct audio_usecase *hfp_usecase = NULL;
257 hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid);
258
259 if (hfp_usecase != NULL)
260 return true;
261 else
262 return false;
263}
264
265audio_usecase_t audio_extn_hfp_get_usecase()
266{
267 return hfpmod.ucid;
268}
269
270void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms)
271{
272 int ret;
273 int rate;
274 int val;
275 float vol;
276 char value[32]={0};
277
278 ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value,
279 sizeof(value));
280 if (ret >= 0) {
281 if (!strncmp(value,"true",sizeof(value)))
282 ret = start_hfp(adev,parms);
283 else
284 stop_hfp(adev);
285 }
286 memset(value, 0, sizeof(value));
287 ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value,
288 sizeof(value));
289 if (ret >= 0) {
290 rate = atoi(value);
291 if (rate == 8000){
292 hfpmod.ucid = USECASE_AUDIO_HFP_SCO;
293 pcm_config_hfp.rate = rate;
294 } else if (rate == 16000){
295 hfpmod.ucid = USECASE_AUDIO_HFP_SCO_WB;
296 pcm_config_hfp.rate = rate;
297 } else
298 ALOGE("Unsupported rate..");
299 }
300
301 if (hfpmod.is_hfp_running) {
302 memset(value, 0, sizeof(value));
303 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
304 value, sizeof(value));
305 if (ret >= 0) {
306 val = atoi(value);
307 if (val > 0)
308 select_devices(adev, hfpmod.ucid);
309 }
310 }
311
312 memset(value, 0, sizeof(value));
313 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_VOLUME,
314 value, sizeof(value));
315 if (ret >= 0) {
316 if (sscanf(value, "%f", &vol) != 1){
317 ALOGE("%s: error in retrieving hfp volume", __func__);
318 ret = -EIO;
319 goto exit;
320 }
321 ALOGD("%s: set_hfp_volume usecase, Vol: [%f]", __func__, vol);
322 hfp_set_volume(adev, vol);
323 }
324exit:
325 ALOGV("%s Exit",__func__);
326}