blob: 73824bdffa8c1729692a588f49211e7571ae3201 [file] [log] [blame]
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -08001/* hfp.c
2Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
28
29#define LOG_TAG "audio_hw_hfp"
30/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32
33#include <errno.h>
34#include <math.h>
35#include <cutils/log.h>
36
37#include "audio_hw.h"
38#include "platform.h"
39#include "platform_api.h"
40#include <stdlib.h>
41#include <cutils/str_parms.h>
42
43#ifdef HFP_ENABLED
44#define AUDIO_PARAMETER_HFP_ENABLE "hfp_enable"
45
46static int32_t audio_extn_start_hfp(struct audio_device *adev,
47 struct str_parms *parms);
48
49static int32_t audio_extn_stop_hfp(struct audio_device *adev);
50
51struct hfp_module {
52 struct pcm *hfp_sco_rx;
53 struct pcm *hfp_sco_tx;
54 struct pcm *hfp_pcm_rx;
55 struct pcm *hfp_pcm_tx;
56 bool is_hfp_running;
57 int hfp_volume;
58};
59
60static 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 .is_hfp_running = 0,
67};
68static struct pcm_config pcm_config_hfp = {
69 .channels = 1,
70 .rate = 8000,
71 .period_size = 240,
72 .period_count = 2,
73 .format = PCM_FORMAT_S16_LE,
74 .start_threshold = 0,
75 .stop_threshold = INT_MAX,
76 .avail_min = 0,
77};
78
79void audio_extn_hfp_set_parameters(struct audio_device *adev, struct str_parms *parms)
80{
81 int ret;
82 char value[32]={0};
83
84 ret = str_parms_get_str(parms, AUDIO_PARAMETER_HFP_ENABLE, value,
85 sizeof(value));
86 if (ret >= 0) {
87 if(!strncmp(value,"true",sizeof(value)))
88 ret = audio_extn_start_hfp(adev,parms);
89 else
90 audio_extn_stop_hfp(adev);
91 }
92}
93
94static int32_t audio_extn_start_hfp(struct audio_device *adev,
95 struct str_parms *parms)
96{
97 int32_t i, ret = 0;
98 struct audio_usecase *uc_info;
99 int32_t pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, pcm_dev_asm_tx_id;
100
101 ALOGD("%s: enter", __func__);
102
103 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
104 uc_info->id = USECASE_AUDIO_HFP_SCO;
105 uc_info->type = PCM_HFP_CALL;
106 uc_info->stream.out = adev->primary_output;
107 uc_info->devices = adev->primary_output->devices;
108 uc_info->in_snd_device = SND_DEVICE_NONE;
109 uc_info->out_snd_device = SND_DEVICE_NONE;
110
111 list_add_tail(&adev->usecase_list, &uc_info->list);
112
113 select_devices(adev, USECASE_AUDIO_HFP_SCO);
114
115 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
116 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
117 pcm_dev_asm_rx_id = HFP_ASM_RX_TX;
118 pcm_dev_asm_tx_id = HFP_ASM_RX_TX;
119 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0 ||
120 pcm_dev_asm_rx_id < 0 || pcm_dev_asm_tx_id < 0 ) {
121 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d asm: rx tx %d) for the usecase(%d)",
122 __func__, pcm_dev_rx_id, pcm_dev_tx_id, pcm_dev_asm_rx_id, uc_info->id);
123 ret = -EIO;
124 goto exit;
125 }
126
127 ALOGV("%s: HFP PCM devices (hfp rx tx: %d pcm rx tx: %d) for the usecase(%d)",
128 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
129
130 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
131 __func__, SOUND_CARD, pcm_dev_rx_id);
132 hfpmod.hfp_sco_rx = pcm_open(SOUND_CARD,
133 pcm_dev_asm_rx_id,
134 PCM_OUT, &pcm_config_hfp);
135 if (hfpmod.hfp_sco_rx && !pcm_is_ready(hfpmod.hfp_sco_rx)) {
136 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_rx));
137 ret = -EIO;
138 goto exit;
139 }
140 ALOGD("%s: Opening PCM capture device card_id(%d) device_id(%d)",
141 __func__, SOUND_CARD, pcm_dev_tx_id);
142 hfpmod.hfp_pcm_rx = pcm_open(SOUND_CARD,
143 pcm_dev_rx_id,
144 PCM_OUT, &pcm_config_hfp);
145 if (hfpmod.hfp_pcm_rx && !pcm_is_ready(hfpmod.hfp_pcm_rx)) {
146 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_rx));
147 ret = -EIO;
148 goto exit;
149 }
150 hfpmod.hfp_sco_tx = pcm_open(SOUND_CARD,
151 pcm_dev_asm_tx_id,
152 PCM_IN, &pcm_config_hfp);
153 if (hfpmod.hfp_sco_tx && !pcm_is_ready(hfpmod.hfp_sco_tx)) {
154 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_sco_tx));
155 ret = -EIO;
156 goto exit;
157 }
158 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
159 __func__, SOUND_CARD, pcm_dev_tx_id);
160 hfpmod.hfp_pcm_tx = pcm_open(SOUND_CARD,
161 pcm_dev_tx_id,
162 PCM_IN, &pcm_config_hfp);
163 if (hfpmod.hfp_pcm_tx && !pcm_is_ready(hfpmod.hfp_pcm_tx)) {
164 ALOGE("%s: %s", __func__, pcm_get_error(hfpmod.hfp_pcm_tx));
165 ret = -EIO;
166 goto exit;
167 }
168 pcm_start(hfpmod.hfp_sco_rx);
169 pcm_start(hfpmod.hfp_sco_tx);
170 pcm_start(hfpmod.hfp_pcm_rx);
171 pcm_start(hfpmod.hfp_pcm_tx);
172
173
174 hfpmod.is_hfp_running = true;
175
176 ALOGD("%s: exit: status(%d)", __func__, ret);
177 return 0;
178
179exit:
180 audio_extn_stop_hfp(adev);
181 ALOGE("%s: Problem in HFP start: status(%d)", __func__, ret);
182 return ret;
183}
184
185static int32_t audio_extn_stop_hfp(struct audio_device *adev)
186{
187 int32_t i, ret = 0;
188 struct audio_usecase *uc_info;
189
190 ALOGD("%s: enter", __func__);
191 hfpmod.is_hfp_running = false;
192
193 /* 1. Close the PCM devices */
194 if (hfpmod.hfp_sco_rx) {
195 pcm_close(hfpmod.hfp_sco_rx);
196 hfpmod.hfp_sco_rx = NULL;
197 }
198 if (hfpmod.hfp_sco_tx) {
199 pcm_close(hfpmod.hfp_sco_tx);
200 hfpmod.hfp_sco_tx = NULL;
201 }
202 if (hfpmod.hfp_pcm_rx) {
203 pcm_close(hfpmod.hfp_pcm_rx);
204 hfpmod.hfp_pcm_rx = NULL;
205 }
206 if (hfpmod.hfp_pcm_tx) {
207 pcm_close(hfpmod.hfp_pcm_tx);
208 hfpmod.hfp_pcm_tx = NULL;
209 }
210
211 uc_info = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
212 if (uc_info == NULL) {
213 ALOGE("%s: Could not find the usecase (%d) in the list",
214 __func__, USECASE_AUDIO_HFP_SCO);
215 return -EINVAL;
216 }
217
218 /* 2. Get and set stream specific mixer controls */
219 disable_audio_route(adev, uc_info, true);
220
221 /* 3. Disable the rx and tx devices */
222 disable_snd_device(adev, uc_info->out_snd_device, false);
223 disable_snd_device(adev, uc_info->in_snd_device, true);
224
225 list_remove(&uc_info->list);
226 free(uc_info);
227
228 ALOGD("%s: exit: status(%d)", __func__, ret);
229 return ret;
230}
231#endif /*HFP_ENABLED*/