blob: c874aa8221266f86e7ce3b01146538cc1d90e89d [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 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_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080019#define LOG_NDDEBUG 0
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080020
21#include <errno.h>
22#include <pthread.h>
23#include <stdint.h>
24#include <sys/time.h>
25#include <stdlib.h>
26#include <dlfcn.h>
27#include <math.h>
28
29#include <cutils/log.h>
30#include <cutils/str_parms.h>
31#include <cutils/properties.h>
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080032#include <cutils/list.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080033
34#include "audio_hw.h"
35
36#define LIB_ACDB_LOADER "/system/lib/libacdbloader.so"
37#define LIB_CSD_CLIENT "/system/lib/libcsd-client.so"
38#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
39#define MIXER_CARD 0
40
41#define STRING_TO_ENUM(string) { #string, string }
42
43/* Flags used to initialize acdb_settings variable that goes to ACDB library */
44#define DMIC_FLAG 0x00000002
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080045#define TTY_MODE_OFF 0x00000010
46#define TTY_MODE_FULL 0x00000020
47#define TTY_MODE_VCO 0x00000040
48#define TTY_MODE_HCO 0x00000080
49#define TTY_MODE_CLEAR 0xFFFFFF0F
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050
51struct string_to_enum {
52 const char *name;
53 uint32_t value;
54};
55
56static const struct string_to_enum out_channels_name_to_enum_table[] = {
57 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
58 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
59 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
60};
61
62static const char * const use_case_table[AUDIO_USECASE_MAX] = {
63 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
64 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
65 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
66 [USECASE_AUDIO_RECORD] = "audio-record",
67 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
68 [USECASE_VOICE_CALL] = "voice-call",
69};
70
71static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
72 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070073#ifdef MSM8974
74 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
75#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080076 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070077#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
79 [USECASE_AUDIO_RECORD] = {0, 0},
80 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
81 [USECASE_VOICE_CALL] = {12, 12},
82};
83
84/* Array to store sound devices */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080085static const char * const device_table[SND_DEVICE_MAX] = {
86 [SND_DEVICE_NONE] = "none",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080087 /* Playback sound devices */
88 [SND_DEVICE_OUT_HANDSET] = "handset",
89 [SND_DEVICE_OUT_SPEAKER] = "speaker",
90 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
91 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
92 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
93 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080094 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080095 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
96 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080097 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080098 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
99 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
100 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800101
102 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800103 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800104 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
105 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700106 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
107 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
108 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800109 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
110 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
111 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800112 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800113 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800114 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
115 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
116 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
117 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
118 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800119 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
120 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
121 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800122 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800123 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
124 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800125 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
126 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127};
128
Eric Laurentc8400632013-02-14 19:04:54 -0800129/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800130static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700131 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800132 [SND_DEVICE_OUT_HANDSET] = 7,
133 [SND_DEVICE_OUT_SPEAKER] = 14,
134 [SND_DEVICE_OUT_HEADPHONES] = 10,
135 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
136 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
137 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800138 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800139 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
140 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800141 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800142 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
143 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
144 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800145
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800146 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800147 [SND_DEVICE_IN_SPEAKER_MIC] = 4,
148 [SND_DEVICE_IN_HEADSET_MIC] = 8,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700149 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
150 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
151 [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800152 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
153 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
154 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800155 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800156 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800157 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
158 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
159 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
160 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
161 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800162 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
163 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
164 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800165 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700166 /* TODO: Update with proper acdb ids */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800167 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
168 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700169 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
170 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171};
172
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800173int edid_get_max_channels(void);
174
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800175static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
176static bool is_tmus = false;
177
178static void check_operator()
179{
180 char value[PROPERTY_VALUE_MAX];
181 int mccmnc;
182 property_get("gsm.sim.operator.numeric",value,"0");
183 mccmnc = atoi(value);
184 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
185 switch(mccmnc) {
186 /* TMUS MCC(310), MNC(490, 260, 026) */
187 case 310490:
188 case 310260:
189 case 310026:
190 is_tmus = true;
191 break;
192 }
193}
194
195static bool is_operator_tmus()
196{
197 pthread_once(&check_op_once_ctl, check_operator);
198 return is_tmus;
199}
200
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800201static int get_pcm_device_id(struct audio_route *ar,
202 audio_usecase_t usecase,
203 int device_type)
204{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800205 int device_id;
206 if (device_type == PCM_PLAYBACK)
207 device_id = pcm_device_table[usecase][0];
208 else
209 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800210 return device_id;
211}
212
213static int get_acdb_device_id(snd_device_t snd_device)
214{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700215 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800216}
217
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800218static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800220{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
222 strcat(mixer_path, " bt-sco");
223 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
224 strcat(mixer_path, " bt-sco");
225 else if (snd_device == SND_DEVICE_OUT_HDMI)
226 strcat(mixer_path, " hdmi");
227 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
228 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800229}
230
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700231static int enable_audio_route(struct audio_device *adev,
232 struct audio_usecase *usecase,
233 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800234{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700235 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800236 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800237
238 if (usecase == NULL)
239 return -EINVAL;
240
241 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
242
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800243 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800245 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800247
248 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800250 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700251 audio_route_apply_path(adev->audio_route, mixer_path);
252 if (update_mixer)
253 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800254
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800255 ALOGV("%s: exit", __func__);
256 return 0;
257}
258
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700259static int disable_audio_route(struct audio_device *adev,
260 struct audio_usecase *usecase,
261 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800262{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700263 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800265
266 if (usecase == NULL)
267 return -EINVAL;
268
269 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700270 if (usecase->type == PCM_CAPTURE)
271 snd_device = usecase->in_snd_device;
272 else
273 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800274 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700275 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800276 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700277 audio_route_reset_path(adev->audio_route, mixer_path);
278 if (update_mixer)
279 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800280
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800281 ALOGV("%s: exit", __func__);
282 return 0;
283}
284
285static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700286 snd_device_t snd_device,
287 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800288{
289 int acdb_dev_id, acdb_dev_type;
290
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800291 if (snd_device < SND_DEVICE_MIN ||
292 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800293 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800294 return -EINVAL;
295 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700296
297 adev->snd_dev_ref_cnt[snd_device]++;
298 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
299 ALOGD("%s: snd_device(%d: %s) is already active",
300 __func__, snd_device, device_table[snd_device]);
301 return 0;
302 }
303
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800304 acdb_dev_id = get_acdb_device_id(snd_device);
305 if (acdb_dev_id < 0) {
306 ALOGE("%s: Could not find acdb id for device(%d)",
307 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 return -EINVAL;
310 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800311 if (adev->acdb_send_audio_cal) {
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700312 ALOGD("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800313 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700314 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
315 snd_device < SND_DEVICE_OUT_END)
316 acdb_dev_type = ACDB_DEV_TYPE_OUT;
317 else
318 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800319 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
320 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700321 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800322 __func__, LIB_ACDB_LOADER);
323 }
324
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700325 ALOGD("%s: snd_device(%d: %s)", __func__,
326 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800327 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700328 if (update_mixer)
329 audio_route_update_mixer(adev->audio_route);
330
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800331 return 0;
332}
333
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700334static int disable_snd_device(struct audio_device *adev,
335 snd_device_t snd_device,
336 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800337{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800338 if (snd_device < SND_DEVICE_MIN ||
339 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800340 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800341 return -EINVAL;
342 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700343 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
344 ALOGE("%s: device ref cnt is already 0", __func__);
345 return -EINVAL;
346 }
347 adev->snd_dev_ref_cnt[snd_device]--;
348 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
349 ALOGD("%s: snd_device(%d: %s)", __func__,
350 snd_device, device_table[snd_device]);
351 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
352 if (update_mixer)
353 audio_route_update_mixer(adev->audio_route);
354 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800355 return 0;
356}
357
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700358static void check_usecases_codec_backend(struct audio_device *adev,
359 struct audio_usecase *uc_info,
360 snd_device_t snd_device)
361{
362 struct listnode *node;
363 struct audio_usecase *usecase;
364 bool switch_device[AUDIO_USECASE_MAX];
365 int i, num_uc_to_switch = 0;
366
367 /*
368 * This function is to make sure that all the usecases that are active on
369 * the hardware codec backend are always routed to any one device that is
370 * handled by the hardware codec.
371 * For example, if low-latency and deep-buffer usecases are currently active
372 * on speaker and out_set_parameters(headset) is received on low-latency
373 * output, then we have to make sure deep-buffer is also switched to headset,
374 * because of the limitation that both the devices cannot be enabled
375 * at the same time as they share the same backend.
376 */
377 /* Disable all the usecases on the shared backend other than the
378 specified usecase */
379 for (i = 0; i < AUDIO_USECASE_MAX; i++)
380 switch_device[i] = false;
381
382 list_for_each(node, &adev->usecase_list) {
383 usecase = node_to_item(node, struct audio_usecase, list);
384 if (usecase->type != PCM_CAPTURE &&
385 usecase != uc_info &&
386 usecase->out_snd_device != snd_device &&
387 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
388 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
389 __func__, use_case_table[usecase->id],
390 device_table[usecase->out_snd_device]);
391 disable_audio_route(adev, usecase, false);
392 switch_device[usecase->id] = true;
393 num_uc_to_switch++;
394 }
395 }
396
397 if (num_uc_to_switch) {
398 /* Make sure all the streams are de-routed before disabling the device */
399 audio_route_update_mixer(adev->audio_route);
400
401 list_for_each(node, &adev->usecase_list) {
402 usecase = node_to_item(node, struct audio_usecase, list);
403 if (switch_device[usecase->id]) {
404 disable_snd_device(adev, usecase->out_snd_device, false);
405 enable_snd_device(adev, snd_device, false);
406 }
407 }
408
409 /* Make sure new snd device is enabled before re-routing the streams */
410 audio_route_update_mixer(adev->audio_route);
411
412 /* Re-route all the usecases on the shared backend other than the
413 specified usecase to new snd devices */
414 list_for_each(node, &adev->usecase_list) {
415 usecase = node_to_item(node, struct audio_usecase, list);
416 /* Update the out_snd_device only before enabling the audio route */
417 if (switch_device[usecase->id] ) {
418 usecase->out_snd_device = snd_device;
419 enable_audio_route(adev, usecase, false);
420 }
421 }
422
423 audio_route_update_mixer(adev->audio_route);
424 }
425}
426
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700427static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
428{
429 struct mixer_ctl *ctl;
430 const char *mixer_ctl_name = "EC_REF_RX";
431
432 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
433 if (!ctl) {
434 ALOGE("%s: Could not get ctl for mixer cmd - %s",
435 __func__, mixer_ctl_name);
436 return -EINVAL;
437 }
438 ALOGV("Setting EC Reference: %s", ec_ref);
439 mixer_ctl_set_enum_by_string(ctl, ec_ref);
440 return 0;
441}
442
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800443static int set_hdmi_channels(struct mixer *mixer,
444 int channel_count)
445{
446 struct mixer_ctl *ctl;
447 const char *channel_cnt_str = NULL;
448 const char *mixer_ctl_name = "HDMI_RX Channels";
449 switch (channel_count) {
450 case 8:
451 channel_cnt_str = "Eight"; break;
452 case 7:
453 channel_cnt_str = "Seven"; break;
454 case 6:
455 channel_cnt_str = "Six"; break;
456 case 5:
457 channel_cnt_str = "Five"; break;
458 case 4:
459 channel_cnt_str = "Four"; break;
460 case 3:
461 channel_cnt_str = "Three"; break;
462 default:
463 channel_cnt_str = "Two"; break;
464 }
465 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
466 if (!ctl) {
467 ALOGE("%s: Could not get ctl for mixer cmd - %s",
468 __func__, mixer_ctl_name);
469 return -EINVAL;
470 }
471 ALOGV("HDMI channel count: %s", channel_cnt_str);
472 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
473 return 0;
474}
475
476/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700477static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800478{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700479 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800481
482 switch (channels) {
483 /*
484 * Do not handle stereo output in Multi-channel cases
485 * Stereo case is handled in normal playback path
486 */
487 case 6:
488 ALOGV("%s: HDMI supports 5.1", __func__);
489 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
490 break;
491 case 8:
492 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
493 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
494 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
495 break;
496 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700497 ALOGE("HDMI does not support multi channel playback");
498 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800499 break;
500 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700501 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800502}
503
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700504static snd_device_t get_output_snd_device(struct audio_device *adev,
505 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800506{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700507 audio_mode_t mode = adev->mode;
508 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800509
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800510 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511 if (devices == AUDIO_DEVICE_NONE ||
512 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800513 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800514 goto exit;
515 }
516
517 if (mode == AUDIO_MODE_IN_CALL) {
518 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
519 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800520 if (adev->tty_mode == TTY_MODE_FULL)
521 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
522 else if (adev->tty_mode == TTY_MODE_VCO)
523 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
524 else if (adev->tty_mode == TTY_MODE_HCO)
525 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
526 else
527 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800528 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
529 snd_device = SND_DEVICE_OUT_BT_SCO;
530 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
531 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
532 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800533 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800534 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
535 else
536 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800538 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539 goto exit;
540 }
541 }
542
543 if (popcount(devices) == 2) {
544 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
545 AUDIO_DEVICE_OUT_SPEAKER)) {
546 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
547 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
548 AUDIO_DEVICE_OUT_SPEAKER)) {
549 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
550 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
551 AUDIO_DEVICE_OUT_SPEAKER)) {
552 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
553 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800554 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555 goto exit;
556 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800557 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800558 goto exit;
559 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800560 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800561
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800563 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564 goto exit;
565 }
566
567 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
568 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
569 snd_device = SND_DEVICE_OUT_HEADPHONES;
570 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
571 snd_device = SND_DEVICE_OUT_SPEAKER;
572 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
573 snd_device = SND_DEVICE_OUT_BT_SCO;
574 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
575 snd_device = SND_DEVICE_OUT_HDMI ;
576 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
577 snd_device = SND_DEVICE_OUT_HANDSET;
578 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800579 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800580 }
581exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800583 return snd_device;
584}
585
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700586static snd_device_t get_input_snd_device(struct audio_device *adev,
587 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588{
Eric Laurentc8400632013-02-14 19:04:54 -0800589 audio_source_t source = (adev->active_input == NULL) ?
590 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
591
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800593 audio_devices_t in_device = ((adev->active_input == NULL) ?
594 AUDIO_DEVICE_NONE : adev->active_input->device)
595 & ~AUDIO_DEVICE_BIT_IN;
596 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
597 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800598 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800599
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800600 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800601 __func__, out_device, in_device);
602 if (mode == AUDIO_MODE_IN_CALL) {
603 if (out_device == AUDIO_DEVICE_NONE) {
604 ALOGE("%s: No output device set for voice call", __func__);
605 goto exit;
606 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800607 if (adev->tty_mode != TTY_MODE_OFF) {
608 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
609 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
610 switch (adev->tty_mode) {
611 case TTY_MODE_FULL:
612 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
613 break;
614 case TTY_MODE_VCO:
615 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
616 break;
617 case TTY_MODE_HCO:
618 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
619 break;
620 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800621 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800622 }
623 goto exit;
624 }
625 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800626 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
627 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800628 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
629 snd_device = SND_DEVICE_IN_HANDSET_MIC;
630 } else {
631 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800632 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800633 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
634 else
635 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
636 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
637 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
638 else
639 snd_device = SND_DEVICE_IN_HANDSET_MIC;
640 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
642 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
643 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
644 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
645 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800646 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800647 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
648 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800649 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800650 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
651 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
652 } else {
653 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
654 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 }
656 } else if (source == AUDIO_SOURCE_CAMCORDER) {
657 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
658 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
659 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
660 }
661 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
662 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800663 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
664 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
665 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
666 else if (adev->fluence_in_voice_rec)
667 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
Eric Laurentc8400632013-02-14 19:04:54 -0800668 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
669 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
670 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
671 else if (adev->fluence_in_voice_rec)
672 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700673 }
674
675 if (snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800676 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700677 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800678 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800679 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
680 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
681 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700682 if (adev->active_input) {
683 if (adev->active_input->enable_aec) {
684 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
685 if (adev->mic_type_analog)
686 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
687 else
688 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
689 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
690 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
691 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
692 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
693 }
694 set_echo_reference(adev->mixer, "SLIM_RX");
695 } else
696 set_echo_reference(adev->mixer, "NONE");
697 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698 } else if (source == AUDIO_SOURCE_DEFAULT) {
699 goto exit;
700 }
701
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700702
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800703 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 goto exit;
705 }
706
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800707 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800708 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
709 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800711 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800713 if (adev->mic_type_analog)
714 snd_device = SND_DEVICE_IN_HANDSET_MIC;
715 else
716 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
718 snd_device = SND_DEVICE_IN_HEADSET_MIC;
719 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
720 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
721 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
722 snd_device = SND_DEVICE_IN_HDMI_MIC;
723 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800724 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800725 ALOGW("%s: Using default handset-mic", __func__);
726 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727 }
728 } else {
729 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800730 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800731 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
732 snd_device = SND_DEVICE_IN_HEADSET_MIC;
733 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
734 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
735 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800736 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800738 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
740 snd_device = SND_DEVICE_IN_HDMI_MIC;
741 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800742 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800743 ALOGW("%s: Using default handset-mic", __func__);
744 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 }
746 }
747exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800748 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749 return snd_device;
750}
751
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700752static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
753 audio_usecase_t uc_id)
754{
755 struct audio_usecase *usecase;
756 struct listnode *node;
757
758 list_for_each(node, &adev->usecase_list) {
759 usecase = node_to_item(node, struct audio_usecase, list);
760 if (usecase->id == uc_id)
761 return usecase;
762 }
763 return NULL;
764}
765
766static int select_devices(struct audio_device *adev,
767 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800768{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800769 snd_device_t out_snd_device = SND_DEVICE_NONE;
770 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700771 struct audio_usecase *usecase = NULL;
772 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800773 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700775 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700777 usecase = get_usecase_from_list(adev, uc_id);
778 if (usecase == NULL) {
779 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
780 return -EINVAL;
781 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700783 if (usecase->type == VOICE_CALL) {
784 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
785 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
786 usecase->devices = usecase->stream.out->devices;
787 } else {
788 /*
789 * If the voice call is active, use the sound devices of voice call usecase
790 * so that it would not result any device switch. All the usecases will
791 * be switched to new device when select_devices() is called for voice call
792 * usecase. This is to avoid switching devices for voice call when
793 * check_usecases_codec_backend() is called below.
794 */
795 if (adev->in_call) {
796 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
797 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
798 in_snd_device = vc_usecase->in_snd_device;
799 out_snd_device = vc_usecase->out_snd_device;
800 }
801 }
802 if (usecase->type == PCM_PLAYBACK) {
803 usecase->devices = usecase->stream.out->devices;
804 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700805 if (out_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700806 out_snd_device = get_output_snd_device(adev,
807 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700808 if (usecase->stream.out == adev->primary_output &&
809 adev->active_input &&
810 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
811 select_devices(adev, adev->active_input->usecase);
812 }
813 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700814 } else if (usecase->type == PCM_CAPTURE) {
815 usecase->devices = usecase->stream.in->device;
816 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700817 if (in_snd_device == SND_DEVICE_NONE) {
818 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
819 adev->primary_output && !adev->primary_output->standby) {
820 in_snd_device = get_input_snd_device(adev,
821 adev->primary_output->devices);
822 } else {
823 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
824 }
825 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700826 }
827 }
828
829 if (out_snd_device == usecase->out_snd_device &&
830 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800831 return 0;
832 }
833
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800834 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
835 out_snd_device, device_table[out_snd_device],
836 in_snd_device, device_table[in_snd_device]);
837
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800838 /*
839 * Limitation: While in call, to do a device switch we need to disable
840 * and enable both RX and TX devices though one of them is same as current
841 * device.
842 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700843 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
844 usecase->in_snd_device != SND_DEVICE_NONE &&
845 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800846 /* This must be called before disabling the mixer controls on APQ side */
847 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800848 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800849 } else {
850 status = adev->csd_disable_device();
851 if (status < 0) {
852 ALOGE("%s: csd_client_disable_device, failed, error %d",
853 __func__, status);
854 }
855 }
856 }
857
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700858 /* Disable current sound devices */
859 if (usecase->out_snd_device != SND_DEVICE_NONE) {
860 disable_audio_route(adev, usecase, true);
861 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800862 }
863
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700864 if (usecase->in_snd_device != SND_DEVICE_NONE) {
865 disable_audio_route(adev, usecase, true);
866 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867 }
868
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700869 /* Enable new sound devices */
870 if (out_snd_device != SND_DEVICE_NONE) {
871 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
872 check_usecases_codec_backend(adev, usecase, out_snd_device);
873 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800874 }
875
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700876 if (in_snd_device != SND_DEVICE_NONE)
877 enable_snd_device(adev, in_snd_device, false);
878
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800879 audio_route_update_mixer(adev->audio_route);
880
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700881 usecase->in_snd_device = in_snd_device;
882 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700884 enable_audio_route(adev, usecase, true);
885
886 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800887 if (adev->csd_enable_device == NULL) {
888 ALOGE("%s: dlsym error for csd_client_enable_device",
889 __func__);
890 } else {
891 acdb_rx_id = get_acdb_device_id(out_snd_device);
892 acdb_tx_id = get_acdb_device_id(in_snd_device);
893
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700894 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
895 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
896 adev->acdb_settings);
897 if (status < 0) {
898 ALOGE("%s: csd_client_enable_device, failed, error %d",
899 __func__, status);
900 }
901 } else {
902 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
903 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800904 }
905 }
906 }
907
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 return status;
909}
910
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800911static int stop_input_stream(struct stream_in *in)
912{
913 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914 struct audio_usecase *uc_info;
915 struct audio_device *adev = in->dev;
916
Eric Laurentc8400632013-02-14 19:04:54 -0800917 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800918
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700919 ALOGD("%s: enter: usecase(%d: %s)", __func__,
920 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921 uc_info = get_usecase_from_list(adev, in->usecase);
922 if (uc_info == NULL) {
923 ALOGE("%s: Could not find the usecase (%d) in the list",
924 __func__, in->usecase);
925 return -EINVAL;
926 }
927
Eric Laurent150dbfe2013-02-27 14:31:02 -0800928 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700929 disable_audio_route(adev, uc_info, true);
930
931 /* 2. Disable the tx device */
932 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800933
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800934 list_remove(&uc_info->list);
935 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800937 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800938 return ret;
939}
940
941int start_input_stream(struct stream_in *in)
942{
943 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800944 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800945 struct audio_usecase *uc_info;
946 struct audio_device *adev = in->dev;
947
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800948 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800949 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
950 in->usecase,
951 PCM_CAPTURE);
952 if (in->pcm_device_id < 0) {
953 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
954 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800955 ret = -EINVAL;
956 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800957 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700958
959 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
961 uc_info->id = in->usecase;
962 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800963 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700964 uc_info->devices = in->device;
965 uc_info->in_snd_device = SND_DEVICE_NONE;
966 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800967
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800968 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700969 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800970
Eric Laurentc8400632013-02-14 19:04:54 -0800971 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
972 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
974 PCM_IN, &in->config);
975 if (in->pcm && !pcm_is_ready(in->pcm)) {
976 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
977 pcm_close(in->pcm);
978 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800979 ret = -EIO;
980 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800981 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800982 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800983 return ret;
984
985error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800986 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800987
988error_config:
989 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700990 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800991
992 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800993}
994
995static int stop_output_stream(struct stream_out *out)
996{
997 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800998 struct audio_usecase *uc_info;
999 struct audio_device *adev = out->dev;
1000
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001001 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1002 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003 uc_info = get_usecase_from_list(adev, out->usecase);
1004 if (uc_info == NULL) {
1005 ALOGE("%s: Could not find the usecase (%d) in the list",
1006 __func__, out->usecase);
1007 return -EINVAL;
1008 }
1009
Eric Laurent150dbfe2013-02-27 14:31:02 -08001010 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001011 disable_audio_route(adev, uc_info, true);
1012
1013 /* 2. Disable the rx device */
1014 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001016 list_remove(&uc_info->list);
1017 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001019 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 return ret;
1021}
1022
1023int start_output_stream(struct stream_out *out)
1024{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001025 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 struct audio_usecase *uc_info;
1027 struct audio_device *adev = out->dev;
1028
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001029 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1030 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
1032 out->usecase,
1033 PCM_PLAYBACK);
1034 if (out->pcm_device_id < 0) {
1035 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1036 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001037 ret = -EINVAL;
1038 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039 }
1040
1041 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1042 uc_info->id = out->usecase;
1043 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001044 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001045 uc_info->devices = out->devices;
1046 uc_info->in_snd_device = SND_DEVICE_NONE;
1047 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001049 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001050
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001051 select_devices(adev, out->usecase);
1052
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1054 __func__, 0, out->pcm_device_id);
1055 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1056 PCM_OUT, &out->config);
1057 if (out->pcm && !pcm_is_ready(out->pcm)) {
1058 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1059 pcm_close(out->pcm);
1060 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001061 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001062 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001064 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001066error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001068error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001069 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070}
1071
1072static int stop_voice_call(struct audio_device *adev)
1073{
1074 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075 struct audio_usecase *uc_info;
1076
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001077 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001078 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 if (adev->csd_client) {
1080 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001081 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082 } else {
1083 ret = adev->csd_stop_voice();
1084 if (ret < 0) {
1085 ALOGE("%s: csd_client error %d\n", __func__, ret);
1086 }
1087 }
1088 }
1089
1090 /* 1. Close the PCM devices */
1091 if (adev->voice_call_rx) {
1092 pcm_close(adev->voice_call_rx);
1093 adev->voice_call_rx = NULL;
1094 }
1095 if (adev->voice_call_tx) {
1096 pcm_close(adev->voice_call_tx);
1097 adev->voice_call_tx = NULL;
1098 }
1099
1100 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1101 if (uc_info == NULL) {
1102 ALOGE("%s: Could not find the usecase (%d) in the list",
1103 __func__, USECASE_VOICE_CALL);
1104 return -EINVAL;
1105 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001106
1107 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001108 disable_audio_route(adev, uc_info, true);
1109
1110 /* 3. Disable the rx and tx devices */
1111 disable_snd_device(adev, uc_info->out_snd_device, false);
1112 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001114 list_remove(&uc_info->list);
1115 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001117 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118 return ret;
1119}
1120
1121static int start_voice_call(struct audio_device *adev)
1122{
1123 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 struct audio_usecase *uc_info;
1125 int pcm_dev_rx_id, pcm_dev_tx_id;
1126
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001127 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001128
1129 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1130 uc_info->id = USECASE_VOICE_CALL;
1131 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001132 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001133 uc_info->devices = adev->primary_output->devices;
1134 uc_info->in_snd_device = SND_DEVICE_NONE;
1135 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001137 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001139 select_devices(adev, USECASE_VOICE_CALL);
1140
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1142 PCM_PLAYBACK);
1143 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1144 PCM_CAPTURE);
1145
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001146 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1147 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1148 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001149 ret = -EIO;
1150 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 }
1152
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001153 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154 __func__, SOUND_CARD, pcm_dev_rx_id);
1155 adev->voice_call_rx = pcm_open(SOUND_CARD,
1156 pcm_dev_rx_id,
1157 PCM_OUT, &pcm_config_voice_call);
1158 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1159 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001160 ret = -EIO;
1161 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001162 }
1163
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001164 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001165 __func__, SOUND_CARD, pcm_dev_tx_id);
1166 adev->voice_call_tx = pcm_open(SOUND_CARD,
1167 pcm_dev_tx_id,
1168 PCM_IN, &pcm_config_voice_call);
1169 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1170 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001171 ret = -EIO;
1172 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173 }
1174 pcm_start(adev->voice_call_rx);
1175 pcm_start(adev->voice_call_tx);
1176
1177 if (adev->csd_client) {
1178 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001179 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001180 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001181 } else {
1182 ret = adev->csd_start_voice();
1183 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001184 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1185 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186 }
1187 }
1188 }
1189
1190 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001191 return 0;
1192
1193error_start_voice:
1194 stop_voice_call(adev);
1195
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001196 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001197 return ret;
1198}
1199
1200static int check_input_parameters(uint32_t sample_rate,
1201 audio_format_t format,
1202 int channel_count)
1203{
1204 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1205
1206 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1207
1208 switch (sample_rate) {
1209 case 8000:
1210 case 11025:
1211 case 12000:
1212 case 16000:
1213 case 22050:
1214 case 24000:
1215 case 32000:
1216 case 44100:
1217 case 48000:
1218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222
1223 return 0;
1224}
1225
1226static size_t get_input_buffer_size(uint32_t sample_rate,
1227 audio_format_t format,
1228 int channel_count)
1229{
1230 size_t size = 0;
1231
1232 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1233
1234 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1235 size = (sample_rate * 20) / 1000;
1236 } else if (sample_rate == 11025 || sample_rate == 12000) {
1237 size = 256;
1238 } else if (sample_rate == 22050 || sample_rate == 24000) {
1239 size = 512;
1240 } else if (sample_rate == 44100 || sample_rate == 48000) {
1241 size = 1024;
1242 }
1243
1244 return size * sizeof(short) * channel_count;
1245}
1246
1247static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1248{
1249 struct stream_out *out = (struct stream_out *)stream;
1250
1251 return out->config.rate;
1252}
1253
1254static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1255{
1256 return -ENOSYS;
1257}
1258
1259static size_t out_get_buffer_size(const struct audio_stream *stream)
1260{
1261 struct stream_out *out = (struct stream_out *)stream;
1262
1263 return out->config.period_size * audio_stream_frame_size(stream);
1264}
1265
1266static uint32_t out_get_channels(const struct audio_stream *stream)
1267{
1268 struct stream_out *out = (struct stream_out *)stream;
1269
1270 return out->channel_mask;
1271}
1272
1273static audio_format_t out_get_format(const struct audio_stream *stream)
1274{
1275 return AUDIO_FORMAT_PCM_16_BIT;
1276}
1277
1278static int out_set_format(struct audio_stream *stream, audio_format_t format)
1279{
1280 return -ENOSYS;
1281}
1282
1283static int out_standby(struct audio_stream *stream)
1284{
1285 struct stream_out *out = (struct stream_out *)stream;
1286 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001287 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1288 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289 pthread_mutex_lock(&out->lock);
1290
1291 if (!out->standby) {
1292 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001293 if (out->pcm) {
1294 pcm_close(out->pcm);
1295 out->pcm = NULL;
1296 }
1297 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001299 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001300 }
1301 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001302 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001303 return 0;
1304}
1305
1306static int out_dump(const struct audio_stream *stream, int fd)
1307{
1308 return 0;
1309}
1310
1311static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1312{
1313 struct stream_out *out = (struct stream_out *)stream;
1314 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001315 struct audio_usecase *usecase;
1316 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317 struct str_parms *parms;
1318 char value[32];
1319 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001320 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001321
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001322 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1323 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324 parms = str_parms_create_str(kvpairs);
1325 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1326 if (ret >= 0) {
1327 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001329 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001331 /*
1332 * When HDMI cable is unplugged the music playback is paused and
1333 * the policy manager sends routing=0. But the audioflinger
1334 * continues to write data until standby time (3sec).
1335 * As the HDMI core is turned off, the write gets blocked.
1336 * Avoid this by routing audio to speaker until standby.
1337 */
1338 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1339 val == AUDIO_DEVICE_NONE) {
1340 val = AUDIO_DEVICE_OUT_SPEAKER;
1341 }
1342
1343 /*
1344 * select_devices() call below switches all the usecases on the same
1345 * backend to the new device. Refer to check_usecases_codec_backend() in
1346 * the select_devices(). But how do we undo this?
1347 *
1348 * For example, music playback is active on headset (deep-buffer usecase)
1349 * and if we go to ringtones and select a ringtone, low-latency usecase
1350 * will be started on headset+speaker. As we can't enable headset+speaker
1351 * and headset devices at the same time, select_devices() switches the music
1352 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1353 * So when the ringtone playback is completed, how do we undo the same?
1354 *
1355 * We are relying on the out_set_parameters() call on deep-buffer output,
1356 * once the ringtone playback is ended.
1357 * NOTE: We should not check if the current devices are same as new devices.
1358 * Because select_devices() must be called to switch back the music
1359 * playback to headset.
1360 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001361 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001362 out->devices = val;
1363
1364 if (!out->standby)
1365 select_devices(adev, out->usecase);
1366
1367 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1368 (out == adev->primary_output)) {
1369 start_voice_call(adev);
1370 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1371 (out == adev->primary_output)) {
1372 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001373 }
1374 }
1375
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001376 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1377 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001382 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001383 }
1384 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001385 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386 return ret;
1387}
1388
1389static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1390{
1391 struct stream_out *out = (struct stream_out *)stream;
1392 struct str_parms *query = str_parms_create_str(keys);
1393 char *str;
1394 char value[256];
1395 struct str_parms *reply = str_parms_create();
1396 size_t i, j;
1397 int ret;
1398 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001399 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1401 if (ret >= 0) {
1402 value[0] = '\0';
1403 i = 0;
1404 while (out->supported_channel_masks[i] != 0) {
1405 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1406 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1407 if (!first) {
1408 strcat(value, "|");
1409 }
1410 strcat(value, out_channels_name_to_enum_table[j].name);
1411 first = false;
1412 break;
1413 }
1414 }
1415 i++;
1416 }
1417 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1418 str = str_parms_to_str(reply);
1419 } else {
1420 str = strdup(keys);
1421 }
1422 str_parms_destroy(query);
1423 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001424 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425 return str;
1426}
1427
1428static uint32_t out_get_latency(const struct audio_stream_out *stream)
1429{
1430 struct stream_out *out = (struct stream_out *)stream;
1431
1432 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1433}
1434
1435static int out_set_volume(struct audio_stream_out *stream, float left,
1436 float right)
1437{
Eric Laurenta9024de2013-04-04 09:19:12 -07001438 struct stream_out *out = (struct stream_out *)stream;
1439 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1440 /* only take left channel into account: the API is for stereo anyway */
1441 out->muted = (left == 0.0f);
1442 return 0;
1443 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444 return -ENOSYS;
1445}
1446
1447static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1448 size_t bytes)
1449{
1450 struct stream_out *out = (struct stream_out *)stream;
1451 struct audio_device *adev = out->dev;
1452 int i, ret = -1;
1453
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 pthread_mutex_lock(&out->lock);
1455 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001456 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001457 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001458 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001459 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001460 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001461 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001462 goto exit;
1463 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465
1466 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001467 if (out->muted)
1468 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001469 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1470 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1471 }
1472
1473exit:
1474 pthread_mutex_unlock(&out->lock);
1475
1476 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001477 if (out->pcm)
1478 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479 out_standby(&out->stream.common);
1480 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1481 out_get_sample_rate(&out->stream.common));
1482 }
1483 return bytes;
1484}
1485
1486static int out_get_render_position(const struct audio_stream_out *stream,
1487 uint32_t *dsp_frames)
1488{
1489 return -EINVAL;
1490}
1491
1492static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1493{
1494 return 0;
1495}
1496
1497static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1498{
1499 return 0;
1500}
1501
1502static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1503 int64_t *timestamp)
1504{
1505 return -EINVAL;
1506}
1507
1508/** audio_stream_in implementation **/
1509static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1510{
1511 struct stream_in *in = (struct stream_in *)stream;
1512
1513 return in->config.rate;
1514}
1515
1516static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1517{
1518 return -ENOSYS;
1519}
1520
1521static size_t in_get_buffer_size(const struct audio_stream *stream)
1522{
1523 struct stream_in *in = (struct stream_in *)stream;
1524
1525 return in->config.period_size * audio_stream_frame_size(stream);
1526}
1527
1528static uint32_t in_get_channels(const struct audio_stream *stream)
1529{
1530 struct stream_in *in = (struct stream_in *)stream;
1531
1532 return in->channel_mask;
1533}
1534
1535static audio_format_t in_get_format(const struct audio_stream *stream)
1536{
1537 return AUDIO_FORMAT_PCM_16_BIT;
1538}
1539
1540static int in_set_format(struct audio_stream *stream, audio_format_t format)
1541{
1542 return -ENOSYS;
1543}
1544
1545static int in_standby(struct audio_stream *stream)
1546{
1547 struct stream_in *in = (struct stream_in *)stream;
1548 struct audio_device *adev = in->dev;
1549 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001550 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 pthread_mutex_lock(&in->lock);
1552 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001554 if (in->pcm) {
1555 pcm_close(in->pcm);
1556 in->pcm = NULL;
1557 }
1558 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001560 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001561 }
1562 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001563 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 return status;
1565}
1566
1567static int in_dump(const struct audio_stream *stream, int fd)
1568{
1569 return 0;
1570}
1571
1572static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1573{
1574 struct stream_in *in = (struct stream_in *)stream;
1575 struct audio_device *adev = in->dev;
1576 struct str_parms *parms;
1577 char *str;
1578 char value[32];
1579 int ret, val = 0;
1580
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001581 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582 parms = str_parms_create_str(kvpairs);
1583
1584 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1585
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001587 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 if (ret >= 0) {
1589 val = atoi(value);
1590 /* no audio source uses val == 0 */
1591 if ((in->source != val) && (val != 0)) {
1592 in->source = val;
1593 }
1594 }
1595
1596 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1597 if (ret >= 0) {
1598 val = atoi(value);
1599 if ((in->device != val) && (val != 0)) {
1600 in->device = val;
1601 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001602 if (!in->standby)
1603 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001604 }
1605 }
1606
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001608 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609
1610 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001611 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001612 return ret;
1613}
1614
1615static char* in_get_parameters(const struct audio_stream *stream,
1616 const char *keys)
1617{
1618 return strdup("");
1619}
1620
1621static int in_set_gain(struct audio_stream_in *stream, float gain)
1622{
1623 return 0;
1624}
1625
1626static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1627 size_t bytes)
1628{
1629 struct stream_in *in = (struct stream_in *)stream;
1630 struct audio_device *adev = in->dev;
1631 int i, ret = -1;
1632
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001633 pthread_mutex_lock(&in->lock);
1634 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001635 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001636 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001637 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 goto exit;
1640 }
1641 in->standby = 0;
1642 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643
1644 if (in->pcm) {
1645 ret = pcm_read(in->pcm, buffer, bytes);
1646 }
1647
1648 /*
1649 * Instead of writing zeroes here, we could trust the hardware
1650 * to always provide zeroes when muted.
1651 */
1652 if (ret == 0 && adev->mic_mute)
1653 memset(buffer, 0, bytes);
1654
1655exit:
1656 pthread_mutex_unlock(&in->lock);
1657
1658 if (ret != 0) {
1659 in_standby(&in->stream.common);
1660 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1661 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1662 in_get_sample_rate(&in->stream.common));
1663 }
1664 return bytes;
1665}
1666
1667static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1668{
1669 return 0;
1670}
1671
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001672static int add_remove_audio_effect(const struct audio_stream *stream,
1673 effect_handle_t effect,
1674 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001676 struct stream_in *in = (struct stream_in *)stream;
1677 int status = 0;
1678 effect_descriptor_t desc;
1679
1680 status = (*effect)->get_descriptor(effect, &desc);
1681 if (status != 0)
1682 return status;
1683
1684 pthread_mutex_lock(&in->lock);
1685 pthread_mutex_lock(&in->dev->lock);
1686 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1687 in->enable_aec != enable &&
1688 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1689 in->enable_aec = enable;
1690 if (!in->standby)
1691 select_devices(in->dev, in->usecase);
1692 }
1693 pthread_mutex_unlock(&in->dev->lock);
1694 pthread_mutex_unlock(&in->lock);
1695
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001696 return 0;
1697}
1698
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001699static int in_add_audio_effect(const struct audio_stream *stream,
1700 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001701{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001702 ALOGD("%s: effect %p", __func__, effect);
1703 return add_remove_audio_effect(stream, effect, true);
1704}
1705
1706static int in_remove_audio_effect(const struct audio_stream *stream,
1707 effect_handle_t effect)
1708{
1709 ALOGD("%s: effect %p", __func__, effect);
1710 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711}
1712
1713static int adev_open_output_stream(struct audio_hw_device *dev,
1714 audio_io_handle_t handle,
1715 audio_devices_t devices,
1716 audio_output_flags_t flags,
1717 struct audio_config *config,
1718 struct audio_stream_out **stream_out)
1719{
1720 struct audio_device *adev = (struct audio_device *)dev;
1721 struct stream_out *out;
1722 int i, ret;
1723
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001724 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 __func__, config->sample_rate, config->channel_mask, devices, flags);
1726 *stream_out = NULL;
1727 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1728
1729 if (devices == AUDIO_DEVICE_NONE)
1730 devices = AUDIO_DEVICE_OUT_SPEAKER;
1731
1732 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1733 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1734 out->flags = flags;
1735 out->devices = devices;
1736
1737 /* Init use case and pcm_config */
1738 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1739 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001740 pthread_mutex_lock(&adev->lock);
1741 ret = read_hdmi_channel_masks(out);
1742 pthread_mutex_unlock(&adev->lock);
1743 if (ret != 0) {
1744 /* If HDMI does not support multi channel playback, set the default */
1745 out->config.channels = popcount(out->channel_mask);
1746 set_hdmi_channels(adev->mixer, out->config.channels);
1747 goto error_open;
1748 }
1749
1750 if (config->sample_rate == 0)
1751 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1752 if (config->channel_mask == 0)
1753 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1754
1755 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1757 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758 out->config.rate = config->sample_rate;
1759 out->config.channels = popcount(out->channel_mask);
1760 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1761 set_hdmi_channels(adev->mixer, out->config.channels);
1762 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1763 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1764 out->config = pcm_config_deep_buffer;
1765 } else {
1766 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1767 out->config = pcm_config_low_latency;
1768 }
1769
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001770 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1771 if(adev->primary_output == NULL)
1772 adev->primary_output = out;
1773 else {
1774 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001775 ret = -EEXIST;
1776 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001777 }
1778 }
1779
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780 /* Check if this usecase is already existing */
1781 pthread_mutex_lock(&adev->lock);
1782 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1783 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001785 ret = -EEXIST;
1786 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 }
1788 pthread_mutex_unlock(&adev->lock);
1789
1790 out->stream.common.get_sample_rate = out_get_sample_rate;
1791 out->stream.common.set_sample_rate = out_set_sample_rate;
1792 out->stream.common.get_buffer_size = out_get_buffer_size;
1793 out->stream.common.get_channels = out_get_channels;
1794 out->stream.common.get_format = out_get_format;
1795 out->stream.common.set_format = out_set_format;
1796 out->stream.common.standby = out_standby;
1797 out->stream.common.dump = out_dump;
1798 out->stream.common.set_parameters = out_set_parameters;
1799 out->stream.common.get_parameters = out_get_parameters;
1800 out->stream.common.add_audio_effect = out_add_audio_effect;
1801 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1802 out->stream.get_latency = out_get_latency;
1803 out->stream.set_volume = out_set_volume;
1804 out->stream.write = out_write;
1805 out->stream.get_render_position = out_get_render_position;
1806 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1807
1808 out->dev = adev;
1809 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001810 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001811
1812 config->format = out->stream.common.get_format(&out->stream.common);
1813 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1814 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1815
1816 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001817 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001818 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001819
1820error_open:
1821 free(out);
1822 *stream_out = NULL;
1823 ALOGD("%s: exit: ret %d", __func__, ret);
1824 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825}
1826
1827static void adev_close_output_stream(struct audio_hw_device *dev,
1828 struct audio_stream_out *stream)
1829{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001830 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001831 out_standby(&stream->common);
1832 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001833 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834}
1835
1836static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1837{
1838 struct audio_device *adev = (struct audio_device *)dev;
1839 struct str_parms *parms;
1840 char *str;
1841 char value[32];
1842 int ret;
1843
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001844 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001845
1846 parms = str_parms_create_str(kvpairs);
1847 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1848 if (ret >= 0) {
1849 int tty_mode;
1850
1851 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1852 tty_mode = TTY_MODE_OFF;
1853 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1854 tty_mode = TTY_MODE_VCO;
1855 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1856 tty_mode = TTY_MODE_HCO;
1857 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1858 tty_mode = TTY_MODE_FULL;
1859 else
1860 return -EINVAL;
1861
1862 pthread_mutex_lock(&adev->lock);
1863 if (tty_mode != adev->tty_mode) {
1864 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001865 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1866 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001867 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868 }
1869 pthread_mutex_unlock(&adev->lock);
1870 }
1871
1872 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1873 if (ret >= 0) {
1874 /* When set to false, HAL should disable EC and NS
1875 * But it is currently not supported.
1876 */
1877 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1878 adev->bluetooth_nrec = true;
1879 else
1880 adev->bluetooth_nrec = false;
1881 }
1882
1883 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1884 if (ret >= 0) {
1885 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1886 adev->screen_off = false;
1887 else
1888 adev->screen_off = true;
1889 }
1890
1891 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001892 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 return ret;
1894}
1895
1896static char* adev_get_parameters(const struct audio_hw_device *dev,
1897 const char *keys)
1898{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899 return strdup("");
1900}
1901
1902static int adev_init_check(const struct audio_hw_device *dev)
1903{
1904 return 0;
1905}
1906
1907static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1908{
1909 struct audio_device *adev = (struct audio_device *)dev;
1910 int vol, err = 0;
1911
1912 pthread_mutex_lock(&adev->lock);
1913 adev->voice_volume = volume;
1914 if (adev->mode == AUDIO_MODE_IN_CALL) {
1915 if (volume < 0.0) {
1916 volume = 0.0;
1917 } else if (volume > 1.0) {
1918 volume = 1.0;
1919 }
1920
1921 vol = lrint(volume * 100.0);
1922
1923 // Voice volume levels from android are mapped to driver volume levels as follows.
1924 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1925 // So adjust the volume to get the correct volume index in driver
1926 vol = 100 - vol;
1927
1928 if (adev->csd_client) {
1929 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001930 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001931 } else {
1932 err = adev->csd_volume(vol);
1933 if (err < 0) {
1934 ALOGE("%s: csd_client error %d", __func__, err);
1935 }
1936 }
1937 } else {
1938 ALOGE("%s: No CSD Client present", __func__);
1939 }
1940 }
1941 pthread_mutex_unlock(&adev->lock);
1942 return err;
1943}
1944
1945static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1946{
1947 return -ENOSYS;
1948}
1949
1950static int adev_get_master_volume(struct audio_hw_device *dev,
1951 float *volume)
1952{
1953 return -ENOSYS;
1954}
1955
1956static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1957{
1958 return -ENOSYS;
1959}
1960
1961static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1962{
1963 return -ENOSYS;
1964}
1965
1966static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1967{
1968 struct audio_device *adev = (struct audio_device *)dev;
1969
1970 pthread_mutex_lock(&adev->lock);
1971 if (adev->mode != mode) {
1972 adev->mode = mode;
1973 }
1974 pthread_mutex_unlock(&adev->lock);
1975 return 0;
1976}
1977
1978static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1979{
1980 struct audio_device *adev = (struct audio_device *)dev;
1981 int err = 0;
1982
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001983 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984 adev->mic_mute = state;
1985 if (adev->mode == AUDIO_MODE_IN_CALL) {
1986 if (adev->csd_client) {
1987 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001988 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 } else {
1990 err = adev->csd_mic_mute(state);
1991 if (err < 0) {
1992 ALOGE("%s: csd_client error %d", __func__, err);
1993 }
1994 }
1995 } else {
1996 ALOGE("%s: No CSD Client present", __func__);
1997 }
1998 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001999 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 return err;
2001}
2002
2003static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2004{
2005 struct audio_device *adev = (struct audio_device *)dev;
2006
2007 *state = adev->mic_mute;
2008
2009 return 0;
2010}
2011
2012static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2013 const struct audio_config *config)
2014{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 int channel_count = popcount(config->channel_mask);
2016
2017 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2018}
2019
2020static int adev_open_input_stream(struct audio_hw_device *dev,
2021 audio_io_handle_t handle,
2022 audio_devices_t devices,
2023 struct audio_config *config,
2024 struct audio_stream_in **stream_in)
2025{
2026 struct audio_device *adev = (struct audio_device *)dev;
2027 struct stream_in *in;
2028 int ret, buffer_size, frame_size;
2029 int channel_count = popcount(config->channel_mask);
2030
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002031 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032 *stream_in = NULL;
2033 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2034 return -EINVAL;
2035
2036 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2037
2038 in->stream.common.get_sample_rate = in_get_sample_rate;
2039 in->stream.common.set_sample_rate = in_set_sample_rate;
2040 in->stream.common.get_buffer_size = in_get_buffer_size;
2041 in->stream.common.get_channels = in_get_channels;
2042 in->stream.common.get_format = in_get_format;
2043 in->stream.common.set_format = in_set_format;
2044 in->stream.common.standby = in_standby;
2045 in->stream.common.dump = in_dump;
2046 in->stream.common.set_parameters = in_set_parameters;
2047 in->stream.common.get_parameters = in_get_parameters;
2048 in->stream.common.add_audio_effect = in_add_audio_effect;
2049 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2050 in->stream.set_gain = in_set_gain;
2051 in->stream.read = in_read;
2052 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2053
2054 in->device = devices;
2055 in->source = AUDIO_SOURCE_DEFAULT;
2056 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057 in->standby = 1;
2058 in->channel_mask = config->channel_mask;
2059
2060 /* Update config params with the requested sample rate and channels */
2061 in->usecase = USECASE_AUDIO_RECORD;
2062 in->config = pcm_config_audio_capture;
2063 in->config.channels = channel_count;
2064 in->config.rate = config->sample_rate;
2065
2066 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2067 buffer_size = get_input_buffer_size(config->sample_rate,
2068 config->format,
2069 channel_count);
2070 in->config.period_size = buffer_size / frame_size;
2071
2072 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002073 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074 return 0;
2075
2076err_open:
2077 free(in);
2078 *stream_in = NULL;
2079 return ret;
2080}
2081
2082static void adev_close_input_stream(struct audio_hw_device *dev,
2083 struct audio_stream_in *stream)
2084{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002085 ALOGD("%s", __func__);
2086
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002087 in_standby(&stream->common);
2088 free(stream);
2089
2090 return;
2091}
2092
2093static int adev_dump(const audio_hw_device_t *device, int fd)
2094{
2095 return 0;
2096}
2097
2098static int adev_close(hw_device_t *device)
2099{
2100 struct audio_device *adev = (struct audio_device *)device;
2101 audio_route_free(adev->audio_route);
2102 free(device);
2103 return 0;
2104}
2105
2106static void init_platform_data(struct audio_device *adev)
2107{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002108 char platform[PROPERTY_VALUE_MAX];
2109 char baseband[PROPERTY_VALUE_MAX];
2110 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002111
2112 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002113 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002114 adev->fluence_in_voice_call = false;
2115 adev->fluence_in_voice_rec = false;
2116 adev->mic_type_analog = false;
2117
2118 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002119 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002120 adev->mic_type_analog = true;
2121
2122 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002123 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002124 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2125 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002126 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002127 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2128 adev->acdb_settings |= DMIC_FLAG;
2129 }
2130
2131 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2132 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002133 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002134 adev->fluence_in_voice_call = true;
2135 }
2136
2137 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002138 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002139 adev->fluence_in_voice_rec = true;
2140 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002141
2142 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002143 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002144 adev->fluence_in_spkr_mode = true;
2145 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002146 }
2147
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002148 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2149 if (adev->acdb_handle == NULL) {
2150 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2151 } else {
2152 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002153 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2154 "acdb_loader_deallocate_ACDB");
2155 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2156 "acdb_loader_send_audio_cal");
2157 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2158 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002159 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2160 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002162 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163 else
2164 adev->acdb_init();
2165 }
2166
2167 /* If platform is Fusion3, load CSD Client specific symbols
2168 * Voice call is handled by MDM and apps processor talks to
2169 * MDM through CSD Client
2170 */
2171 property_get("ro.board.platform", platform, "");
2172 property_get("ro.baseband", baseband, "");
2173 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2174 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2175 if (adev->csd_client == NULL)
2176 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2177 }
2178
2179 if (adev->csd_client) {
2180 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2182 "csd_client_deinit");
2183 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2184 "csd_client_disable_device");
2185 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2186 "csd_client_enable_device");
2187 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2188 "csd_client_start_voice");
2189 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2190 "csd_client_stop_voice");
2191 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2192 "csd_client_volume");
2193 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2194 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002195 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2196 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002197
2198 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002199 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200 } else {
2201 adev->csd_client_init();
2202 }
2203 }
2204}
2205
2206static int adev_open(const hw_module_t *module, const char *name,
2207 hw_device_t **device)
2208{
2209 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002210 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002212 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002213 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2214
2215 adev = calloc(1, sizeof(struct audio_device));
2216
2217 adev->mixer = mixer_open(MIXER_CARD);
2218 if (!adev->mixer) {
2219 ALOGE("Unable to open the mixer, aborting.");
2220 return -ENOSYS;
2221 }
2222
2223 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2224 if (!adev->audio_route) {
2225 free(adev);
2226 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2227 *device = NULL;
2228 return -EINVAL;
2229 }
2230
2231 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2232 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2233 adev->device.common.module = (struct hw_module_t *)module;
2234 adev->device.common.close = adev_close;
2235
2236 adev->device.init_check = adev_init_check;
2237 adev->device.set_voice_volume = adev_set_voice_volume;
2238 adev->device.set_master_volume = adev_set_master_volume;
2239 adev->device.get_master_volume = adev_get_master_volume;
2240 adev->device.set_master_mute = adev_set_master_mute;
2241 adev->device.get_master_mute = adev_get_master_mute;
2242 adev->device.set_mode = adev_set_mode;
2243 adev->device.set_mic_mute = adev_set_mic_mute;
2244 adev->device.get_mic_mute = adev_get_mic_mute;
2245 adev->device.set_parameters = adev_set_parameters;
2246 adev->device.get_parameters = adev_get_parameters;
2247 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2248 adev->device.open_output_stream = adev_open_output_stream;
2249 adev->device.close_output_stream = adev_close_output_stream;
2250 adev->device.open_input_stream = adev_open_input_stream;
2251 adev->device.close_input_stream = adev_close_input_stream;
2252 adev->device.dump = adev_dump;
2253
2254 /* Set the default route before the PCM stream is opened */
2255 pthread_mutex_lock(&adev->lock);
2256 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002257 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002258 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002259 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002260 adev->voice_call_rx = NULL;
2261 adev->voice_call_tx = NULL;
2262 adev->voice_volume = 1.0f;
2263 adev->tty_mode = TTY_MODE_OFF;
2264 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002266 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002267 for (i = 0; i < SND_DEVICE_MAX; i++) {
2268 adev->snd_dev_ref_cnt[i] = 0;
2269 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002270 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002271 pthread_mutex_unlock(&adev->lock);
2272
2273 /* Loads platform specific libraries dynamically */
2274 init_platform_data(adev);
2275
2276 *device = &adev->device.common;
2277
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002278 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 return 0;
2280}
2281
2282static struct hw_module_methods_t hal_module_methods = {
2283 .open = adev_open,
2284};
2285
2286struct audio_module HAL_MODULE_INFO_SYM = {
2287 .common = {
2288 .tag = HARDWARE_MODULE_TAG,
2289 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2290 .hal_api_version = HARDWARE_HAL_API_VERSION,
2291 .id = AUDIO_HARDWARE_MODULE_ID,
2292 .name = "QCOM Audio HAL",
2293 .author = "Code Aurora Forum",
2294 .methods = &hal_module_methods,
2295 },
2296};