blob: bfeb6e74cb40e6666173233af7c60478f6899aa6 [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__);
122
123 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
124 uc_info->id = hfpmod.ucid;
125 uc_info->type = PCM_HFP_CALL;
126 uc_info->stream.out = adev->primary_output;
127 uc_info->devices = adev->primary_output->devices;
128 uc_info->in_snd_device = SND_DEVICE_NONE;
129 uc_info->out_snd_device = SND_DEVICE_NONE;
130
131 list_add_tail(&adev->usecase_list, &uc_info->list);
132
133 select_devices(adev, hfpmod.ucid);
134
135 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
136 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
137 pcm_dev_asm_rx_id = HFP_ASM_RX_TX;
138 pcm_dev_asm_tx_id = HFP_ASM_RX_TX;
139 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 ||
140 pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) {
141 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)",
142 __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id);
143 ret = -EIO;
144 goto exit;
145 }
146
147 ALOGV("%s: HFP PCM devices (hfp rx tx: %d pcm rx tx: %d) for the usecase(%d)",
148 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
149
150 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700151 __func__, adev->snd_card, pcm_dev_rx_id);
152 hfpmod.hfp_sco_rx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800153 pcm_dev_asm_rx_id,
154 PCM_OUT, &pcm_config_hfp);
155 if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) {
156 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx));
157 ret = -EIO;
158 goto exit;
159 }
160 ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700161 __func__, adev->snd_card, pcm_dev_tx_id);
162 hfpmod.hfp_pcm_rx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800163 pcm_dev_rx_id,
164 PCM_OUT, &pcm_config_hfp);
165 if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) {
166 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx));
167 ret = -EIO;
168 goto exit;
169 }
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700170 hfpmod.hfp_sco_tx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800171 pcm_dev_asm_tx_id,
172 PCM_IN, &pcm_config_hfp);
173 if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) {
174 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx));
175 ret = -EIO;
176 goto exit;
177 }
178 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700179 __func__, adev->snd_card, pcm_dev_tx_id);
180 hfpmod.hfp_pcm_tx = pcm_open(adev->snd_card,
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800181 pcm_dev_tx_id,
182 PCM_IN, &pcm_config_hfp);
183 if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) {
184 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx));
185 ret = -EIO;
186 goto exit;
187 }
188 pcm_start(hfpmod.hfp_sco_rx);
189 pcm_start(hfpmod.hfp_sco_tx);
190 pcm_start(hfpmod.hfp_pcm_rx);
191 pcm_start(hfpmod.hfp_pcm_tx);
192
193 hfpmod.is_hfp_running = true;
194 hfp_set_volume(adev, hfpmod.hfp_volume);
195
196 ALOGD("%s: exit: status(%d)", __func__, ret);
197 return 0;
198
199exit:
200 stop_hfp(adev);
201 ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret);
202 return ret;
203}
204
205static int32_t stop_hfp(struct audio_device *adev)
206{
207 int32_t i, ret = 0;
208 struct audio_usecase *uc_info;
209
210 ALOGD("%s: enter", __func__);
211 hfpmod.is_hfp_running = false;
212
213 /* 1. Close the PCM devices */
214 if (hfpmod.hfp_sco_rx) {
215 pcm_close(hfpmod.hfp_sco_rx);
216 hfpmod.hfp_sco_rx = NULL;
217 }
218 if (hfpmod.hfp_sco_tx) {
219 pcm_close(hfpmod.hfp_sco_tx);
220 hfpmod.hfp_sco_tx = NULL;
221 }
222 if (hfpmod.hfp_pcm_rx) {
223 pcm_close(hfpmod.hfp_pcm_rx);
224 hfpmod.hfp_pcm_rx = NULL;
225 }
226 if (hfpmod.hfp_pcm_tx) {
227 pcm_close(hfpmod.hfp_pcm_tx);
228 hfpmod.hfp_pcm_tx = NULL;
229 }
230
231 uc_info = get_usecase_from_list(adev, hfpmod.ucid);
232 if (uc_info == NULL) {
233 ALOGE("%s: Could not find the usecase (%d) in the list",
234 __func__, hfpmod.ucid);
235 return -EINVAL;
236 }
237
238 /* 2. Get and set stream specific mixer controls */
239 disable_audio_route(adev, uc_info);
240
241 /* 3. Disable the rx and tx devices */
242 disable_snd_device(adev, uc_info->out_snd_device);
243 disable_snd_device(adev, uc_info->in_snd_device);
244
245 list_remove(&uc_info->list);
246 free(uc_info);
247
248 ALOGD("%s: exit: status(%d)", __func__, ret);
249 return ret;
250}
251
252bool audio_extn_hfp_is_active(struct audio_device *adev)
253{
254 struct audio_usecase *hfp_usecase = NULL;
255 hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid);
256
257 if (hfp_usecase != NULL)
258 return true;
259 else
260 return false;
261}
262
263audio_usecase_t audio_extn_hfp_get_usecase()
264{
265 return hfpmod.ucid;
266}
267
268void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms)
269{
270 int ret;
271 int rate;
272 int val;
273 float vol;
274 char value[32]={0};
275
276 ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value,
277 sizeof(value));
278 if (ret >= 0) {
279 if (!strncmp(value,"true",sizeof(value)))
280 ret = start_hfp(adev,parms);
281 else
282 stop_hfp(adev);
283 }
284 memset(value, 0, sizeof(value));
285 ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value,
286 sizeof(value));
287 if (ret >= 0) {
288 rate = atoi(value);
289 if (rate == 8000){
290 hfpmod.ucid = USECASE_AUDIO_HFP_SCO;
291 pcm_config_hfp.rate = rate;
292 } else if (rate == 16000){
293 hfpmod.ucid = USECASE_AUDIO_HFP_SCO_WB;
294 pcm_config_hfp.rate = rate;
295 } else
296 ALOGE("Unsupported rate..");
297 }
298
299 if (hfpmod.is_hfp_running) {
300 memset(value, 0, sizeof(value));
301 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
302 value, sizeof(value));
303 if (ret >= 0) {
304 val = atoi(value);
305 if (val > 0)
306 select_devices(adev, hfpmod.ucid);
307 }
308 }
309
310 memset(value, 0, sizeof(value));
311 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_VOLUME,
312 value, sizeof(value));
313 if (ret >= 0) {
314 if (sscanf(value, "%f", &vol) != 1){
315 ALOGE("%s: error in retrieving hfp volume", __func__);
316 ret = -EIO;
317 goto exit;
318 }
319 ALOGD("%s: set_hfp_volume usecase, Vol: [%f]", __func__, vol);
320 hfp_set_volume(adev, vol);
321 }
322exit:
323 ALOGV("%s Exit",__func__);
324}