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>
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;
74#ifdef EXTERNAL_BT_SUPPORTED
75    const char *mixer_ctl_name = "PRI AUXPCM LOOPBACK Volume";
76#else
77    const char *mixer_ctl_name = "Internal HFP RX Volume";
78#endif
79
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,
115                         struct str_parms *parms __unused)
116{
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    adev->enable_hfp = true;
123    platform_set_mic_mute(adev->platform, false);
124
125    uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
126    uc_info->id = hfpmod.ucid;
127    uc_info->type = PCM_HFP_CALL;
128    uc_info->stream.out = adev->primary_output;
129    uc_info->devices = adev->primary_output->devices;
130    uc_info->in_snd_device = SND_DEVICE_NONE;
131    uc_info->out_snd_device = SND_DEVICE_NONE;
132
133    list_add_tail(&adev->usecase_list, &uc_info->list);
134
135    select_devices(adev, hfpmod.ucid);
136
137    pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
138    pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
139    pcm_dev_asm_rx_id = HFP_ASM_RX_TX;
140    pcm_dev_asm_tx_id = HFP_ASM_RX_TX;
141    if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 ||
142        pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) {
143        ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)",
144              __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id);
145        ret = -EIO;
146        goto exit;
147    }
148
149    ALOGV("%s: HFP PCM devices (hfp rx tx: %d pcm rx tx: %d) for the usecase(%d)",
150              __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
151
152    ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
153          __func__, adev->snd_card, pcm_dev_rx_id);
154    hfpmod.hfp_sco_rx = pcm_open(adev->snd_card,
155                                  pcm_dev_asm_rx_id,
156                                  PCM_OUT, &pcm_config_hfp);
157    if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) {
158        ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx));
159        ret = -EIO;
160        goto exit;
161    }
162    ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
163          __func__, adev->snd_card, pcm_dev_tx_id);
164    hfpmod.hfp_pcm_rx = pcm_open(adev->snd_card,
165                                   pcm_dev_rx_id,
166                                   PCM_OUT, &pcm_config_hfp);
167    if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) {
168        ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx));
169        ret = -EIO;
170        goto exit;
171    }
172    hfpmod.hfp_sco_tx = pcm_open(adev->snd_card,
173                                  pcm_dev_asm_tx_id,
174                                  PCM_IN, &pcm_config_hfp);
175    if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) {
176        ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx));
177        ret = -EIO;
178        goto exit;
179    }
180    ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
181          __func__, adev->snd_card, pcm_dev_tx_id);
182    hfpmod.hfp_pcm_tx = pcm_open(adev->snd_card,
183                                   pcm_dev_tx_id,
184                                   PCM_IN, &pcm_config_hfp);
185    if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) {
186        ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx));
187        ret = -EIO;
188        goto exit;
189    }
190    pcm_start(hfpmod.hfp_sco_rx);
191    pcm_start(hfpmod.hfp_sco_tx);
192    pcm_start(hfpmod.hfp_pcm_rx);
193    pcm_start(hfpmod.hfp_pcm_tx);
194
195    hfpmod.is_hfp_running = true;
196    hfp_set_volume(adev, hfpmod.hfp_volume);
197
198    ALOGD("%s: exit: status(%d)", __func__, ret);
199    return 0;
200
201exit:
202    stop_hfp(adev);
203    ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret);
204    return ret;
205}
206
207static int32_t stop_hfp(struct audio_device *adev)
208{
209    int32_t i, ret = 0;
210    struct audio_usecase *uc_info;
211
212    ALOGD("%s: enter", __func__);
213    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    /* Disable the echo reference for HFP Tx */
248    platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
249
250    /* Set the unmute Tx mixer control */
251    if (voice_get_mic_mute(adev)) {
252        platform_set_mic_mute(adev->platform, false);
253        ALOGD("%s: unMute HFP Tx", __func__);
254    }
255    adev->enable_hfp = false;
256
257    list_remove(&uc_info->list);
258    free(uc_info);
259
260    ALOGD("%s: exit: status(%d)", __func__, ret);
261    return ret;
262}
263
264bool audio_extn_hfp_is_active(struct audio_device *adev)
265{
266    struct audio_usecase *hfp_usecase = NULL;
267    hfp_usecase = get_usecase_from_list(adev, hfpmod.ucid);
268
269    if (hfp_usecase != NULL)
270        return true;
271    else
272        return false;
273}
274
275audio_usecase_t audio_extn_hfp_get_usecase()
276{
277    return hfpmod.ucid;
278}
279
280void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms)
281{
282    int ret;
283    int rate;
284    int val;
285    float vol;
286    char value[32]={0};
287
288    ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value,
289                            sizeof(value));
290    if (ret >= 0) {
291           if (!strncmp(value,"true",sizeof(value)))
292               ret = start_hfp(adev,parms);
293           else
294               stop_hfp(adev);
295    }
296    memset(value, 0, sizeof(value));
297    ret = str_parms_get_str(parms,AUDIO_PARAMETER_HFP_SET_SAMPLING_RATE, value,
298                            sizeof(value));
299    if (ret >= 0) {
300           rate = atoi(value);
301           if (rate == 8000){
302               hfpmod.ucid = USECASE_AUDIO_HFP_SCO;
303               pcm_config_hfp.rate = rate;
304           } else if (rate == 16000){
305               hfpmod.ucid = USECASE_AUDIO_HFP_SCO_WB;
306               pcm_config_hfp.rate = rate;
307           } else
308               ALOGE("Unsupported rate..");
309    }
310
311    if (hfpmod.is_hfp_running) {
312        memset(value, 0, sizeof(value));
313        ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
314                                value, sizeof(value));
315        if (ret >= 0) {
316            val = atoi(value);
317            if (val > 0)
318                select_devices(adev, hfpmod.ucid);
319        }
320    }
321
322    memset(value, 0, sizeof(value));
323    ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_HFP_VOLUME,
324                            value, sizeof(value));
325    if (ret >= 0) {
326        if (sscanf(value, "%f", &vol) != 1){
327            ALOGE("%s: error in retrieving hfp volume", __func__);
328            ret = -EIO;
329            goto exit;
330        }
331        ALOGD("%s: set_hfp_volume usecase, Vol: [%f]", __func__, vol);
332        hfp_set_volume(adev, vol);
333    }
334exit:
335    ALOGV("%s Exit",__func__);
336}
337