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