blob: 7eef1f7a82bfa10276c904e8dd4f71ffe02df9b3 [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",
Jean-Michel Trivic56336b2013-05-24 16:55:17 -070090 [SND_DEVICE_OUT_SPEAKER_REVERSE] = "speaker-reverse",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080091 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
92 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
93 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
94 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080095 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080096 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
97 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080098 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080099 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
100 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
101 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800102
103 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800104 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800105 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
106 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700107 [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic",
108 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic",
109 [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800110 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
111 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
112 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800113 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800114 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800115 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
116 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
117 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
118 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
119 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800120 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
121 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
122 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800123 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800124 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
125 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800126 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
127 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800128};
129
Eric Laurentc8400632013-02-14 19:04:54 -0800130/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800131static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700132 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800133 [SND_DEVICE_OUT_HANDSET] = 7,
134 [SND_DEVICE_OUT_SPEAKER] = 14,
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700135 [SND_DEVICE_OUT_SPEAKER_REVERSE] = 14,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800136 [SND_DEVICE_OUT_HEADPHONES] = 10,
137 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
138 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
139 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800140 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800141 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
142 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800143 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800144 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
145 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
146 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800147
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800148 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800149 [SND_DEVICE_IN_SPEAKER_MIC] = 4,
150 [SND_DEVICE_IN_HEADSET_MIC] = 8,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700151 [SND_DEVICE_IN_HANDSET_MIC_AEC] = 40,
152 [SND_DEVICE_IN_SPEAKER_MIC_AEC] = 42,
153 [SND_DEVICE_IN_HEADSET_MIC_AEC] = 47,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800154 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
155 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
156 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800157 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800158 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800159 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
160 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
161 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
162 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
163 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800164 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
165 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
166 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800167 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700168 /* TODO: Update with proper acdb ids */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800169 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
170 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700171 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 6,
172 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 5,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800173};
174
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800175int edid_get_max_channels(void);
176
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800177static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
178static bool is_tmus = false;
179
180static void check_operator()
181{
182 char value[PROPERTY_VALUE_MAX];
183 int mccmnc;
184 property_get("gsm.sim.operator.numeric",value,"0");
185 mccmnc = atoi(value);
186 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
187 switch(mccmnc) {
188 /* TMUS MCC(310), MNC(490, 260, 026) */
189 case 310490:
190 case 310260:
191 case 310026:
192 is_tmus = true;
193 break;
194 }
195}
196
197static bool is_operator_tmus()
198{
199 pthread_once(&check_op_once_ctl, check_operator);
200 return is_tmus;
201}
202
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800203static int get_pcm_device_id(struct audio_route *ar,
204 audio_usecase_t usecase,
205 int device_type)
206{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800207 int device_id;
208 if (device_type == PCM_PLAYBACK)
209 device_id = pcm_device_table[usecase][0];
210 else
211 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800212 return device_id;
213}
214
215static int get_acdb_device_id(snd_device_t snd_device)
216{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700217 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218}
219
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800220static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800222{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
224 strcat(mixer_path, " bt-sco");
225 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
226 strcat(mixer_path, " bt-sco");
227 else if (snd_device == SND_DEVICE_OUT_HDMI)
228 strcat(mixer_path, " hdmi");
229 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
230 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800231}
232
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233static int enable_audio_route(struct audio_device *adev,
234 struct audio_usecase *usecase,
235 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800236{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700237 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800238 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800239
240 if (usecase == NULL)
241 return -EINVAL;
242
243 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
244
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800245 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800247 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700248 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800249
250 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700251 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800252 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 audio_route_apply_path(adev->audio_route, mixer_path);
254 if (update_mixer)
255 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800257 ALOGV("%s: exit", __func__);
258 return 0;
259}
260
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261static int disable_audio_route(struct audio_device *adev,
262 struct audio_usecase *usecase,
263 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700265 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800267
268 if (usecase == NULL)
269 return -EINVAL;
270
271 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700272 if (usecase->type == PCM_CAPTURE)
273 snd_device = usecase->in_snd_device;
274 else
275 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800276 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700277 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800278 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 audio_route_reset_path(adev->audio_route, mixer_path);
280 if (update_mixer)
281 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800282
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800283 ALOGV("%s: exit", __func__);
284 return 0;
285}
286
287static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700288 snd_device_t snd_device,
289 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800290{
291 int acdb_dev_id, acdb_dev_type;
292
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800293 if (snd_device < SND_DEVICE_MIN ||
294 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800295 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800296 return -EINVAL;
297 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700298
299 adev->snd_dev_ref_cnt[snd_device]++;
300 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
301 ALOGD("%s: snd_device(%d: %s) is already active",
302 __func__, snd_device, device_table[snd_device]);
303 return 0;
304 }
305
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800306 acdb_dev_id = get_acdb_device_id(snd_device);
307 if (acdb_dev_id < 0) {
308 ALOGE("%s: Could not find acdb id for device(%d)",
309 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700310 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800311 return -EINVAL;
312 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800313 if (adev->acdb_send_audio_cal) {
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700314 ALOGD("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800315 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700316 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
317 snd_device < SND_DEVICE_OUT_END)
318 acdb_dev_type = ACDB_DEV_TYPE_OUT;
319 else
320 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800321 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
322 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700323 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800324 __func__, LIB_ACDB_LOADER);
325 }
326
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700327 ALOGD("%s: snd_device(%d: %s)", __func__,
328 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800329 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700330 if (update_mixer)
331 audio_route_update_mixer(adev->audio_route);
332
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800333 return 0;
334}
335
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336static int disable_snd_device(struct audio_device *adev,
337 snd_device_t snd_device,
338 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800339{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800340 if (snd_device < SND_DEVICE_MIN ||
341 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800342 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800343 return -EINVAL;
344 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
346 ALOGE("%s: device ref cnt is already 0", __func__);
347 return -EINVAL;
348 }
349 adev->snd_dev_ref_cnt[snd_device]--;
350 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
351 ALOGD("%s: snd_device(%d: %s)", __func__,
352 snd_device, device_table[snd_device]);
353 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
354 if (update_mixer)
355 audio_route_update_mixer(adev->audio_route);
356 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800357 return 0;
358}
359
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700360static void check_usecases_codec_backend(struct audio_device *adev,
361 struct audio_usecase *uc_info,
362 snd_device_t snd_device)
363{
364 struct listnode *node;
365 struct audio_usecase *usecase;
366 bool switch_device[AUDIO_USECASE_MAX];
367 int i, num_uc_to_switch = 0;
368
369 /*
370 * This function is to make sure that all the usecases that are active on
371 * the hardware codec backend are always routed to any one device that is
372 * handled by the hardware codec.
373 * For example, if low-latency and deep-buffer usecases are currently active
374 * on speaker and out_set_parameters(headset) is received on low-latency
375 * output, then we have to make sure deep-buffer is also switched to headset,
376 * because of the limitation that both the devices cannot be enabled
377 * at the same time as they share the same backend.
378 */
379 /* Disable all the usecases on the shared backend other than the
380 specified usecase */
381 for (i = 0; i < AUDIO_USECASE_MAX; i++)
382 switch_device[i] = false;
383
384 list_for_each(node, &adev->usecase_list) {
385 usecase = node_to_item(node, struct audio_usecase, list);
386 if (usecase->type != PCM_CAPTURE &&
387 usecase != uc_info &&
388 usecase->out_snd_device != snd_device &&
389 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
390 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
391 __func__, use_case_table[usecase->id],
392 device_table[usecase->out_snd_device]);
393 disable_audio_route(adev, usecase, false);
394 switch_device[usecase->id] = true;
395 num_uc_to_switch++;
396 }
397 }
398
399 if (num_uc_to_switch) {
400 /* Make sure all the streams are de-routed before disabling the device */
401 audio_route_update_mixer(adev->audio_route);
402
403 list_for_each(node, &adev->usecase_list) {
404 usecase = node_to_item(node, struct audio_usecase, list);
405 if (switch_device[usecase->id]) {
406 disable_snd_device(adev, usecase->out_snd_device, false);
407 enable_snd_device(adev, snd_device, false);
408 }
409 }
410
411 /* Make sure new snd device is enabled before re-routing the streams */
412 audio_route_update_mixer(adev->audio_route);
413
414 /* Re-route all the usecases on the shared backend other than the
415 specified usecase to new snd devices */
416 list_for_each(node, &adev->usecase_list) {
417 usecase = node_to_item(node, struct audio_usecase, list);
418 /* Update the out_snd_device only before enabling the audio route */
419 if (switch_device[usecase->id] ) {
420 usecase->out_snd_device = snd_device;
421 enable_audio_route(adev, usecase, false);
422 }
423 }
424
425 audio_route_update_mixer(adev->audio_route);
426 }
427}
428
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700429static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
430{
431 struct mixer_ctl *ctl;
432 const char *mixer_ctl_name = "EC_REF_RX";
433
434 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
435 if (!ctl) {
436 ALOGE("%s: Could not get ctl for mixer cmd - %s",
437 __func__, mixer_ctl_name);
438 return -EINVAL;
439 }
440 ALOGV("Setting EC Reference: %s", ec_ref);
441 mixer_ctl_set_enum_by_string(ctl, ec_ref);
442 return 0;
443}
444
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800445static int set_hdmi_channels(struct mixer *mixer,
446 int channel_count)
447{
448 struct mixer_ctl *ctl;
449 const char *channel_cnt_str = NULL;
450 const char *mixer_ctl_name = "HDMI_RX Channels";
451 switch (channel_count) {
452 case 8:
453 channel_cnt_str = "Eight"; break;
454 case 7:
455 channel_cnt_str = "Seven"; break;
456 case 6:
457 channel_cnt_str = "Six"; break;
458 case 5:
459 channel_cnt_str = "Five"; break;
460 case 4:
461 channel_cnt_str = "Four"; break;
462 case 3:
463 channel_cnt_str = "Three"; break;
464 default:
465 channel_cnt_str = "Two"; break;
466 }
467 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
468 if (!ctl) {
469 ALOGE("%s: Could not get ctl for mixer cmd - %s",
470 __func__, mixer_ctl_name);
471 return -EINVAL;
472 }
473 ALOGV("HDMI channel count: %s", channel_cnt_str);
474 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
475 return 0;
476}
477
478/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700479static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700481 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800482 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800483
484 switch (channels) {
485 /*
486 * Do not handle stereo output in Multi-channel cases
487 * Stereo case is handled in normal playback path
488 */
489 case 6:
490 ALOGV("%s: HDMI supports 5.1", __func__);
491 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
492 break;
493 case 8:
494 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
495 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
496 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
497 break;
498 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700499 ALOGE("HDMI does not support multi channel playback");
500 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800501 break;
502 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700503 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800504}
505
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700506static snd_device_t get_output_snd_device(struct audio_device *adev,
507 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800508{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700509 audio_mode_t mode = adev->mode;
510 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800512 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800513 if (devices == AUDIO_DEVICE_NONE ||
514 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800515 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800516 goto exit;
517 }
518
519 if (mode == AUDIO_MODE_IN_CALL) {
520 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
521 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800522 if (adev->tty_mode == TTY_MODE_FULL)
523 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
524 else if (adev->tty_mode == TTY_MODE_VCO)
525 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
526 else if (adev->tty_mode == TTY_MODE_HCO)
527 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
528 else
529 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800530 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
531 snd_device = SND_DEVICE_OUT_BT_SCO;
532 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
533 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
534 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800535 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800536 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
537 else
538 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800540 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800541 goto exit;
542 }
543 }
544
545 if (popcount(devices) == 2) {
546 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
547 AUDIO_DEVICE_OUT_SPEAKER)) {
548 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
549 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
550 AUDIO_DEVICE_OUT_SPEAKER)) {
551 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
552 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
553 AUDIO_DEVICE_OUT_SPEAKER)) {
554 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
555 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800556 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800557 goto exit;
558 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800559 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800560 goto exit;
561 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800563
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800565 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566 goto exit;
567 }
568
569 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
570 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
571 snd_device = SND_DEVICE_OUT_HEADPHONES;
572 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700573 if (adev->speaker_lr_swap) {
574 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
575 } else {
576 snd_device = SND_DEVICE_OUT_SPEAKER;
577 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800578 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
579 snd_device = SND_DEVICE_OUT_BT_SCO;
580 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
581 snd_device = SND_DEVICE_OUT_HDMI ;
582 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
583 snd_device = SND_DEVICE_OUT_HANDSET;
584 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800585 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800586 }
587exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800588 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800589 return snd_device;
590}
591
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700592static snd_device_t get_input_snd_device(struct audio_device *adev,
593 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594{
Eric Laurentc8400632013-02-14 19:04:54 -0800595 audio_source_t source = (adev->active_input == NULL) ?
596 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
597
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800599 audio_devices_t in_device = ((adev->active_input == NULL) ?
600 AUDIO_DEVICE_NONE : adev->active_input->device)
601 & ~AUDIO_DEVICE_BIT_IN;
602 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
603 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800604 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800605
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800606 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800607 __func__, out_device, in_device);
608 if (mode == AUDIO_MODE_IN_CALL) {
609 if (out_device == AUDIO_DEVICE_NONE) {
610 ALOGE("%s: No output device set for voice call", __func__);
611 goto exit;
612 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800613 if (adev->tty_mode != TTY_MODE_OFF) {
614 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
615 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
616 switch (adev->tty_mode) {
617 case TTY_MODE_FULL:
618 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
619 break;
620 case TTY_MODE_VCO:
621 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
622 break;
623 case TTY_MODE_HCO:
624 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
625 break;
626 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800627 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800628 }
629 goto exit;
630 }
631 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800632 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
633 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800634 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
635 snd_device = SND_DEVICE_IN_HANDSET_MIC;
636 } else {
637 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800638 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800639 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
640 else
641 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
642 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
643 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
644 else
645 snd_device = SND_DEVICE_IN_HANDSET_MIC;
646 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800647 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
648 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
649 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
650 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
651 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800652 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800653 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
654 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800655 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800656 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
657 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
658 } else {
659 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
660 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800661 }
662 } else if (source == AUDIO_SOURCE_CAMCORDER) {
663 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
664 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
665 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
666 }
667 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
668 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800669 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
670 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
671 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
672 else if (adev->fluence_in_voice_rec)
673 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
Eric Laurentc8400632013-02-14 19:04:54 -0800674 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
675 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
676 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
677 else if (adev->fluence_in_voice_rec)
678 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700679 }
680
681 if (snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800682 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700683 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800685 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
686 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
687 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700688 if (adev->active_input) {
689 if (adev->active_input->enable_aec) {
690 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
691 if (adev->mic_type_analog)
692 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
693 else
694 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
695 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
696 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
697 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
698 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
699 }
700 set_echo_reference(adev->mixer, "SLIM_RX");
701 } else
702 set_echo_reference(adev->mixer, "NONE");
703 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 } else if (source == AUDIO_SOURCE_DEFAULT) {
705 goto exit;
706 }
707
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700708
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800709 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710 goto exit;
711 }
712
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800713 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800714 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
715 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800717 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800718 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800719 if (adev->mic_type_analog)
720 snd_device = SND_DEVICE_IN_HANDSET_MIC;
721 else
722 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800723 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
724 snd_device = SND_DEVICE_IN_HEADSET_MIC;
725 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
726 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
727 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
728 snd_device = SND_DEVICE_IN_HDMI_MIC;
729 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800730 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800731 ALOGW("%s: Using default handset-mic", __func__);
732 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733 }
734 } else {
735 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
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_WIRED_HEADSET) {
738 snd_device = SND_DEVICE_IN_HEADSET_MIC;
739 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
740 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
741 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800742 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800744 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
746 snd_device = SND_DEVICE_IN_HDMI_MIC;
747 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800748 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800749 ALOGW("%s: Using default handset-mic", __func__);
750 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 }
752 }
753exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800754 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800755 return snd_device;
756}
757
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700758static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
759 audio_usecase_t uc_id)
760{
761 struct audio_usecase *usecase;
762 struct listnode *node;
763
764 list_for_each(node, &adev->usecase_list) {
765 usecase = node_to_item(node, struct audio_usecase, list);
766 if (usecase->id == uc_id)
767 return usecase;
768 }
769 return NULL;
770}
771
772static int select_devices(struct audio_device *adev,
773 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800775 snd_device_t out_snd_device = SND_DEVICE_NONE;
776 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700777 struct audio_usecase *usecase = NULL;
778 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800779 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700781 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700783 usecase = get_usecase_from_list(adev, uc_id);
784 if (usecase == NULL) {
785 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
786 return -EINVAL;
787 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700789 if (usecase->type == VOICE_CALL) {
790 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
791 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
792 usecase->devices = usecase->stream.out->devices;
793 } else {
794 /*
795 * If the voice call is active, use the sound devices of voice call usecase
796 * so that it would not result any device switch. All the usecases will
797 * be switched to new device when select_devices() is called for voice call
798 * usecase. This is to avoid switching devices for voice call when
799 * check_usecases_codec_backend() is called below.
800 */
801 if (adev->in_call) {
802 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
803 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
804 in_snd_device = vc_usecase->in_snd_device;
805 out_snd_device = vc_usecase->out_snd_device;
806 }
807 }
808 if (usecase->type == PCM_PLAYBACK) {
809 usecase->devices = usecase->stream.out->devices;
810 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700811 if (out_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700812 out_snd_device = get_output_snd_device(adev,
813 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700814 if (usecase->stream.out == adev->primary_output &&
815 adev->active_input &&
816 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
817 select_devices(adev, adev->active_input->usecase);
818 }
819 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700820 } else if (usecase->type == PCM_CAPTURE) {
821 usecase->devices = usecase->stream.in->device;
822 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700823 if (in_snd_device == SND_DEVICE_NONE) {
824 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
825 adev->primary_output && !adev->primary_output->standby) {
826 in_snd_device = get_input_snd_device(adev,
827 adev->primary_output->devices);
828 } else {
829 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
830 }
831 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700832 }
833 }
834
835 if (out_snd_device == usecase->out_snd_device &&
836 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800837 return 0;
838 }
839
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800840 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
841 out_snd_device, device_table[out_snd_device],
842 in_snd_device, device_table[in_snd_device]);
843
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800844 /*
845 * Limitation: While in call, to do a device switch we need to disable
846 * and enable both RX and TX devices though one of them is same as current
847 * device.
848 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700849 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
850 usecase->in_snd_device != SND_DEVICE_NONE &&
851 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800852 /* This must be called before disabling the mixer controls on APQ side */
853 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800854 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800855 } else {
856 status = adev->csd_disable_device();
857 if (status < 0) {
858 ALOGE("%s: csd_client_disable_device, failed, error %d",
859 __func__, status);
860 }
861 }
862 }
863
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700864 /* Disable current sound devices */
865 if (usecase->out_snd_device != SND_DEVICE_NONE) {
866 disable_audio_route(adev, usecase, true);
867 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800868 }
869
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700870 if (usecase->in_snd_device != SND_DEVICE_NONE) {
871 disable_audio_route(adev, usecase, true);
872 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 }
874
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700875 /* Enable new sound devices */
876 if (out_snd_device != SND_DEVICE_NONE) {
877 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
878 check_usecases_codec_backend(adev, usecase, out_snd_device);
879 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880 }
881
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700882 if (in_snd_device != SND_DEVICE_NONE)
883 enable_snd_device(adev, in_snd_device, false);
884
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800885 audio_route_update_mixer(adev->audio_route);
886
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700887 usecase->in_snd_device = in_snd_device;
888 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800889
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700890 enable_audio_route(adev, usecase, true);
891
892 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800893 if (adev->csd_enable_device == NULL) {
894 ALOGE("%s: dlsym error for csd_client_enable_device",
895 __func__);
896 } else {
897 acdb_rx_id = get_acdb_device_id(out_snd_device);
898 acdb_tx_id = get_acdb_device_id(in_snd_device);
899
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700900 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
901 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
902 adev->acdb_settings);
903 if (status < 0) {
904 ALOGE("%s: csd_client_enable_device, failed, error %d",
905 __func__, status);
906 }
907 } else {
908 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
909 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800910 }
911 }
912 }
913
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914 return status;
915}
916
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800917static int stop_input_stream(struct stream_in *in)
918{
919 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800920 struct audio_usecase *uc_info;
921 struct audio_device *adev = in->dev;
922
Eric Laurentc8400632013-02-14 19:04:54 -0800923 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800924
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700925 ALOGD("%s: enter: usecase(%d: %s)", __func__,
926 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800927 uc_info = get_usecase_from_list(adev, in->usecase);
928 if (uc_info == NULL) {
929 ALOGE("%s: Could not find the usecase (%d) in the list",
930 __func__, in->usecase);
931 return -EINVAL;
932 }
933
Eric Laurent150dbfe2013-02-27 14:31:02 -0800934 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700935 disable_audio_route(adev, uc_info, true);
936
937 /* 2. Disable the tx device */
938 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800940 list_remove(&uc_info->list);
941 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800942
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800943 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 return ret;
945}
946
947int start_input_stream(struct stream_in *in)
948{
949 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800950 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951 struct audio_usecase *uc_info;
952 struct audio_device *adev = in->dev;
953
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800954 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800955 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
956 in->usecase,
957 PCM_CAPTURE);
958 if (in->pcm_device_id < 0) {
959 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
960 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800961 ret = -EINVAL;
962 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800963 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700964
965 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
967 uc_info->id = in->usecase;
968 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800969 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700970 uc_info->devices = in->device;
971 uc_info->in_snd_device = SND_DEVICE_NONE;
972 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800974 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700975 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800976
Eric Laurentc8400632013-02-14 19:04:54 -0800977 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
978 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800979 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
980 PCM_IN, &in->config);
981 if (in->pcm && !pcm_is_ready(in->pcm)) {
982 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
983 pcm_close(in->pcm);
984 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800985 ret = -EIO;
986 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800988 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800989 return ret;
990
991error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800993
994error_config:
995 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700996 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800997
998 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800999}
1000
1001static int stop_output_stream(struct stream_out *out)
1002{
1003 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001004 struct audio_usecase *uc_info;
1005 struct audio_device *adev = out->dev;
1006
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001007 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1008 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001009 uc_info = get_usecase_from_list(adev, out->usecase);
1010 if (uc_info == NULL) {
1011 ALOGE("%s: Could not find the usecase (%d) in the list",
1012 __func__, out->usecase);
1013 return -EINVAL;
1014 }
1015
Eric Laurent150dbfe2013-02-27 14:31:02 -08001016 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001017 disable_audio_route(adev, uc_info, true);
1018
1019 /* 2. Disable the rx device */
1020 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001022 list_remove(&uc_info->list);
1023 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001025 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 return ret;
1027}
1028
1029int start_output_stream(struct stream_out *out)
1030{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 struct audio_usecase *uc_info;
1033 struct audio_device *adev = out->dev;
1034
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001035 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1036 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
1038 out->usecase,
1039 PCM_PLAYBACK);
1040 if (out->pcm_device_id < 0) {
1041 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1042 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001043 ret = -EINVAL;
1044 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045 }
1046
1047 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1048 uc_info->id = out->usecase;
1049 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001050 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001051 uc_info->devices = out->devices;
1052 uc_info->in_snd_device = SND_DEVICE_NONE;
1053 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001055 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001057 select_devices(adev, out->usecase);
1058
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1060 __func__, 0, out->pcm_device_id);
1061 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1062 PCM_OUT, &out->config);
1063 if (out->pcm && !pcm_is_ready(out->pcm)) {
1064 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1065 pcm_close(out->pcm);
1066 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001067 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001068 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001070 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001072error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001074error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001075 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001076}
1077
1078static int stop_voice_call(struct audio_device *adev)
1079{
1080 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 struct audio_usecase *uc_info;
1082
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001083 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001084 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085 if (adev->csd_client) {
1086 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001087 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 } else {
1089 ret = adev->csd_stop_voice();
1090 if (ret < 0) {
1091 ALOGE("%s: csd_client error %d\n", __func__, ret);
1092 }
1093 }
1094 }
1095
1096 /* 1. Close the PCM devices */
1097 if (adev->voice_call_rx) {
1098 pcm_close(adev->voice_call_rx);
1099 adev->voice_call_rx = NULL;
1100 }
1101 if (adev->voice_call_tx) {
1102 pcm_close(adev->voice_call_tx);
1103 adev->voice_call_tx = NULL;
1104 }
1105
1106 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1107 if (uc_info == NULL) {
1108 ALOGE("%s: Could not find the usecase (%d) in the list",
1109 __func__, USECASE_VOICE_CALL);
1110 return -EINVAL;
1111 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112
1113 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001114 disable_audio_route(adev, uc_info, true);
1115
1116 /* 3. Disable the rx and tx devices */
1117 disable_snd_device(adev, uc_info->out_snd_device, false);
1118 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001119
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001120 list_remove(&uc_info->list);
1121 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001122
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001123 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 return ret;
1125}
1126
1127static int start_voice_call(struct audio_device *adev)
1128{
1129 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130 struct audio_usecase *uc_info;
1131 int pcm_dev_rx_id, pcm_dev_tx_id;
1132
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001133 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134
1135 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1136 uc_info->id = USECASE_VOICE_CALL;
1137 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001138 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001139 uc_info->devices = adev->primary_output->devices;
1140 uc_info->in_snd_device = SND_DEVICE_NONE;
1141 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001143 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001144
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001145 select_devices(adev, USECASE_VOICE_CALL);
1146
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1148 PCM_PLAYBACK);
1149 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1150 PCM_CAPTURE);
1151
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001152 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1153 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1154 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001155 ret = -EIO;
1156 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001157 }
1158
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001159 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160 __func__, SOUND_CARD, pcm_dev_rx_id);
1161 adev->voice_call_rx = pcm_open(SOUND_CARD,
1162 pcm_dev_rx_id,
1163 PCM_OUT, &pcm_config_voice_call);
1164 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1165 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001166 ret = -EIO;
1167 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168 }
1169
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001170 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171 __func__, SOUND_CARD, pcm_dev_tx_id);
1172 adev->voice_call_tx = pcm_open(SOUND_CARD,
1173 pcm_dev_tx_id,
1174 PCM_IN, &pcm_config_voice_call);
1175 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1176 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001177 ret = -EIO;
1178 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001179 }
1180 pcm_start(adev->voice_call_rx);
1181 pcm_start(adev->voice_call_tx);
1182
1183 if (adev->csd_client) {
1184 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001185 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001186 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187 } else {
1188 ret = adev->csd_start_voice();
1189 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001190 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1191 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192 }
1193 }
1194 }
1195
1196 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001197 return 0;
1198
1199error_start_voice:
1200 stop_voice_call(adev);
1201
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001202 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203 return ret;
1204}
1205
1206static int check_input_parameters(uint32_t sample_rate,
1207 audio_format_t format,
1208 int channel_count)
1209{
1210 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1211
1212 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1213
1214 switch (sample_rate) {
1215 case 8000:
1216 case 11025:
1217 case 12000:
1218 case 16000:
1219 case 22050:
1220 case 24000:
1221 case 32000:
1222 case 44100:
1223 case 48000:
1224 break;
1225 default:
1226 return -EINVAL;
1227 }
1228
1229 return 0;
1230}
1231
1232static size_t get_input_buffer_size(uint32_t sample_rate,
1233 audio_format_t format,
1234 int channel_count)
1235{
1236 size_t size = 0;
1237
1238 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1239
1240 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1241 size = (sample_rate * 20) / 1000;
1242 } else if (sample_rate == 11025 || sample_rate == 12000) {
1243 size = 256;
1244 } else if (sample_rate == 22050 || sample_rate == 24000) {
1245 size = 512;
1246 } else if (sample_rate == 44100 || sample_rate == 48000) {
1247 size = 1024;
1248 }
1249
1250 return size * sizeof(short) * channel_count;
1251}
1252
1253static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1254{
1255 struct stream_out *out = (struct stream_out *)stream;
1256
1257 return out->config.rate;
1258}
1259
1260static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1261{
1262 return -ENOSYS;
1263}
1264
1265static size_t out_get_buffer_size(const struct audio_stream *stream)
1266{
1267 struct stream_out *out = (struct stream_out *)stream;
1268
1269 return out->config.period_size * audio_stream_frame_size(stream);
1270}
1271
1272static uint32_t out_get_channels(const struct audio_stream *stream)
1273{
1274 struct stream_out *out = (struct stream_out *)stream;
1275
1276 return out->channel_mask;
1277}
1278
1279static audio_format_t out_get_format(const struct audio_stream *stream)
1280{
1281 return AUDIO_FORMAT_PCM_16_BIT;
1282}
1283
1284static int out_set_format(struct audio_stream *stream, audio_format_t format)
1285{
1286 return -ENOSYS;
1287}
1288
1289static int out_standby(struct audio_stream *stream)
1290{
1291 struct stream_out *out = (struct stream_out *)stream;
1292 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001293 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1294 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001295 pthread_mutex_lock(&out->lock);
1296
1297 if (!out->standby) {
1298 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001299 if (out->pcm) {
1300 pcm_close(out->pcm);
1301 out->pcm = NULL;
1302 }
1303 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001304 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001305 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001306 }
1307 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001308 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001309 return 0;
1310}
1311
1312static int out_dump(const struct audio_stream *stream, int fd)
1313{
1314 return 0;
1315}
1316
1317static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1318{
1319 struct stream_out *out = (struct stream_out *)stream;
1320 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001321 struct audio_usecase *usecase;
1322 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 struct str_parms *parms;
1324 char value[32];
1325 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001326 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001328 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1329 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330 parms = str_parms_create_str(kvpairs);
1331 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1332 if (ret >= 0) {
1333 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001335 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001337 /*
1338 * When HDMI cable is unplugged the music playback is paused and
1339 * the policy manager sends routing=0. But the audioflinger
1340 * continues to write data until standby time (3sec).
1341 * As the HDMI core is turned off, the write gets blocked.
1342 * Avoid this by routing audio to speaker until standby.
1343 */
1344 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1345 val == AUDIO_DEVICE_NONE) {
1346 val = AUDIO_DEVICE_OUT_SPEAKER;
1347 }
1348
1349 /*
1350 * select_devices() call below switches all the usecases on the same
1351 * backend to the new device. Refer to check_usecases_codec_backend() in
1352 * the select_devices(). But how do we undo this?
1353 *
1354 * For example, music playback is active on headset (deep-buffer usecase)
1355 * and if we go to ringtones and select a ringtone, low-latency usecase
1356 * will be started on headset+speaker. As we can't enable headset+speaker
1357 * and headset devices at the same time, select_devices() switches the music
1358 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1359 * So when the ringtone playback is completed, how do we undo the same?
1360 *
1361 * We are relying on the out_set_parameters() call on deep-buffer output,
1362 * once the ringtone playback is ended.
1363 * NOTE: We should not check if the current devices are same as new devices.
1364 * Because select_devices() must be called to switch back the music
1365 * playback to headset.
1366 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001367 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001368 out->devices = val;
1369
1370 if (!out->standby)
1371 select_devices(adev, out->usecase);
1372
1373 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1374 (out == adev->primary_output)) {
1375 start_voice_call(adev);
1376 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1377 (out == adev->primary_output)) {
1378 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001379 }
1380 }
1381
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001382 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1383 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001388 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 }
1390 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001391 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392 return ret;
1393}
1394
1395static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1396{
1397 struct stream_out *out = (struct stream_out *)stream;
1398 struct str_parms *query = str_parms_create_str(keys);
1399 char *str;
1400 char value[256];
1401 struct str_parms *reply = str_parms_create();
1402 size_t i, j;
1403 int ret;
1404 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001405 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1407 if (ret >= 0) {
1408 value[0] = '\0';
1409 i = 0;
1410 while (out->supported_channel_masks[i] != 0) {
1411 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1412 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1413 if (!first) {
1414 strcat(value, "|");
1415 }
1416 strcat(value, out_channels_name_to_enum_table[j].name);
1417 first = false;
1418 break;
1419 }
1420 }
1421 i++;
1422 }
1423 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1424 str = str_parms_to_str(reply);
1425 } else {
1426 str = strdup(keys);
1427 }
1428 str_parms_destroy(query);
1429 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001430 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 return str;
1432}
1433
1434static uint32_t out_get_latency(const struct audio_stream_out *stream)
1435{
1436 struct stream_out *out = (struct stream_out *)stream;
1437
1438 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1439}
1440
1441static int out_set_volume(struct audio_stream_out *stream, float left,
1442 float right)
1443{
Eric Laurenta9024de2013-04-04 09:19:12 -07001444 struct stream_out *out = (struct stream_out *)stream;
1445 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1446 /* only take left channel into account: the API is for stereo anyway */
1447 out->muted = (left == 0.0f);
1448 return 0;
1449 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001450 return -ENOSYS;
1451}
1452
1453static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1454 size_t bytes)
1455{
1456 struct stream_out *out = (struct stream_out *)stream;
1457 struct audio_device *adev = out->dev;
1458 int i, ret = -1;
1459
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001460 pthread_mutex_lock(&out->lock);
1461 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001462 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001463 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001465 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001466 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001467 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001468 goto exit;
1469 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001470 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001471
1472 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001473 if (out->muted)
1474 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001475 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1476 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1477 }
1478
1479exit:
1480 pthread_mutex_unlock(&out->lock);
1481
1482 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001483 if (out->pcm)
1484 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001485 out_standby(&out->stream.common);
1486 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1487 out_get_sample_rate(&out->stream.common));
1488 }
1489 return bytes;
1490}
1491
1492static int out_get_render_position(const struct audio_stream_out *stream,
1493 uint32_t *dsp_frames)
1494{
1495 return -EINVAL;
1496}
1497
1498static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1499{
1500 return 0;
1501}
1502
1503static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1504{
1505 return 0;
1506}
1507
1508static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1509 int64_t *timestamp)
1510{
1511 return -EINVAL;
1512}
1513
1514/** audio_stream_in implementation **/
1515static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1516{
1517 struct stream_in *in = (struct stream_in *)stream;
1518
1519 return in->config.rate;
1520}
1521
1522static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1523{
1524 return -ENOSYS;
1525}
1526
1527static size_t in_get_buffer_size(const struct audio_stream *stream)
1528{
1529 struct stream_in *in = (struct stream_in *)stream;
1530
1531 return in->config.period_size * audio_stream_frame_size(stream);
1532}
1533
1534static uint32_t in_get_channels(const struct audio_stream *stream)
1535{
1536 struct stream_in *in = (struct stream_in *)stream;
1537
1538 return in->channel_mask;
1539}
1540
1541static audio_format_t in_get_format(const struct audio_stream *stream)
1542{
1543 return AUDIO_FORMAT_PCM_16_BIT;
1544}
1545
1546static int in_set_format(struct audio_stream *stream, audio_format_t format)
1547{
1548 return -ENOSYS;
1549}
1550
1551static int in_standby(struct audio_stream *stream)
1552{
1553 struct stream_in *in = (struct stream_in *)stream;
1554 struct audio_device *adev = in->dev;
1555 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001556 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001557 pthread_mutex_lock(&in->lock);
1558 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001560 if (in->pcm) {
1561 pcm_close(in->pcm);
1562 in->pcm = NULL;
1563 }
1564 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001566 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567 }
1568 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001569 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001570 return status;
1571}
1572
1573static int in_dump(const struct audio_stream *stream, int fd)
1574{
1575 return 0;
1576}
1577
1578static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1579{
1580 struct stream_in *in = (struct stream_in *)stream;
1581 struct audio_device *adev = in->dev;
1582 struct str_parms *parms;
1583 char *str;
1584 char value[32];
1585 int ret, val = 0;
1586
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001587 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 parms = str_parms_create_str(kvpairs);
1589
1590 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1591
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001592 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001593 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001594 if (ret >= 0) {
1595 val = atoi(value);
1596 /* no audio source uses val == 0 */
1597 if ((in->source != val) && (val != 0)) {
1598 in->source = val;
1599 }
1600 }
1601
1602 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1603 if (ret >= 0) {
1604 val = atoi(value);
1605 if ((in->device != val) && (val != 0)) {
1606 in->device = val;
1607 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001608 if (!in->standby)
1609 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001610 }
1611 }
1612
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001613 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001614 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001615
1616 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001617 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 return ret;
1619}
1620
1621static char* in_get_parameters(const struct audio_stream *stream,
1622 const char *keys)
1623{
1624 return strdup("");
1625}
1626
1627static int in_set_gain(struct audio_stream_in *stream, float gain)
1628{
1629 return 0;
1630}
1631
1632static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1633 size_t bytes)
1634{
1635 struct stream_in *in = (struct stream_in *)stream;
1636 struct audio_device *adev = in->dev;
1637 int i, ret = -1;
1638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 pthread_mutex_lock(&in->lock);
1640 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001641 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001643 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645 goto exit;
1646 }
1647 in->standby = 0;
1648 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649
1650 if (in->pcm) {
1651 ret = pcm_read(in->pcm, buffer, bytes);
1652 }
1653
1654 /*
1655 * Instead of writing zeroes here, we could trust the hardware
1656 * to always provide zeroes when muted.
1657 */
1658 if (ret == 0 && adev->mic_mute)
1659 memset(buffer, 0, bytes);
1660
1661exit:
1662 pthread_mutex_unlock(&in->lock);
1663
1664 if (ret != 0) {
1665 in_standby(&in->stream.common);
1666 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1667 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1668 in_get_sample_rate(&in->stream.common));
1669 }
1670 return bytes;
1671}
1672
1673static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1674{
1675 return 0;
1676}
1677
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001678static int add_remove_audio_effect(const struct audio_stream *stream,
1679 effect_handle_t effect,
1680 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001682 struct stream_in *in = (struct stream_in *)stream;
1683 int status = 0;
1684 effect_descriptor_t desc;
1685
1686 status = (*effect)->get_descriptor(effect, &desc);
1687 if (status != 0)
1688 return status;
1689
1690 pthread_mutex_lock(&in->lock);
1691 pthread_mutex_lock(&in->dev->lock);
1692 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1693 in->enable_aec != enable &&
1694 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1695 in->enable_aec = enable;
1696 if (!in->standby)
1697 select_devices(in->dev, in->usecase);
1698 }
1699 pthread_mutex_unlock(&in->dev->lock);
1700 pthread_mutex_unlock(&in->lock);
1701
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001702 return 0;
1703}
1704
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001705static int in_add_audio_effect(const struct audio_stream *stream,
1706 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001708 ALOGD("%s: effect %p", __func__, effect);
1709 return add_remove_audio_effect(stream, effect, true);
1710}
1711
1712static int in_remove_audio_effect(const struct audio_stream *stream,
1713 effect_handle_t effect)
1714{
1715 ALOGD("%s: effect %p", __func__, effect);
1716 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717}
1718
1719static int adev_open_output_stream(struct audio_hw_device *dev,
1720 audio_io_handle_t handle,
1721 audio_devices_t devices,
1722 audio_output_flags_t flags,
1723 struct audio_config *config,
1724 struct audio_stream_out **stream_out)
1725{
1726 struct audio_device *adev = (struct audio_device *)dev;
1727 struct stream_out *out;
1728 int i, ret;
1729
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001730 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001731 __func__, config->sample_rate, config->channel_mask, devices, flags);
1732 *stream_out = NULL;
1733 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1734
1735 if (devices == AUDIO_DEVICE_NONE)
1736 devices = AUDIO_DEVICE_OUT_SPEAKER;
1737
1738 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1739 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1740 out->flags = flags;
1741 out->devices = devices;
1742
1743 /* Init use case and pcm_config */
1744 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1745 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001746 pthread_mutex_lock(&adev->lock);
1747 ret = read_hdmi_channel_masks(out);
1748 pthread_mutex_unlock(&adev->lock);
1749 if (ret != 0) {
1750 /* If HDMI does not support multi channel playback, set the default */
1751 out->config.channels = popcount(out->channel_mask);
1752 set_hdmi_channels(adev->mixer, out->config.channels);
1753 goto error_open;
1754 }
1755
1756 if (config->sample_rate == 0)
1757 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1758 if (config->channel_mask == 0)
1759 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1760
1761 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1763 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001764 out->config.rate = config->sample_rate;
1765 out->config.channels = popcount(out->channel_mask);
1766 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1767 set_hdmi_channels(adev->mixer, out->config.channels);
1768 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1769 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1770 out->config = pcm_config_deep_buffer;
1771 } else {
1772 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1773 out->config = pcm_config_low_latency;
1774 }
1775
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001776 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1777 if(adev->primary_output == NULL)
1778 adev->primary_output = out;
1779 else {
1780 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001781 ret = -EEXIST;
1782 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001783 }
1784 }
1785
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001786 /* Check if this usecase is already existing */
1787 pthread_mutex_lock(&adev->lock);
1788 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1789 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001791 ret = -EEXIST;
1792 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 }
1794 pthread_mutex_unlock(&adev->lock);
1795
1796 out->stream.common.get_sample_rate = out_get_sample_rate;
1797 out->stream.common.set_sample_rate = out_set_sample_rate;
1798 out->stream.common.get_buffer_size = out_get_buffer_size;
1799 out->stream.common.get_channels = out_get_channels;
1800 out->stream.common.get_format = out_get_format;
1801 out->stream.common.set_format = out_set_format;
1802 out->stream.common.standby = out_standby;
1803 out->stream.common.dump = out_dump;
1804 out->stream.common.set_parameters = out_set_parameters;
1805 out->stream.common.get_parameters = out_get_parameters;
1806 out->stream.common.add_audio_effect = out_add_audio_effect;
1807 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1808 out->stream.get_latency = out_get_latency;
1809 out->stream.set_volume = out_set_volume;
1810 out->stream.write = out_write;
1811 out->stream.get_render_position = out_get_render_position;
1812 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1813
1814 out->dev = adev;
1815 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001816 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817
1818 config->format = out->stream.common.get_format(&out->stream.common);
1819 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1820 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1821
1822 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001823 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001824 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001825
1826error_open:
1827 free(out);
1828 *stream_out = NULL;
1829 ALOGD("%s: exit: ret %d", __func__, ret);
1830 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001831}
1832
1833static void adev_close_output_stream(struct audio_hw_device *dev,
1834 struct audio_stream_out *stream)
1835{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001836 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837 out_standby(&stream->common);
1838 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001839 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001840}
1841
1842static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1843{
1844 struct audio_device *adev = (struct audio_device *)dev;
1845 struct str_parms *parms;
1846 char *str;
1847 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001848 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 int ret;
1850
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001851 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852
1853 parms = str_parms_create_str(kvpairs);
1854 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1855 if (ret >= 0) {
1856 int tty_mode;
1857
1858 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1859 tty_mode = TTY_MODE_OFF;
1860 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1861 tty_mode = TTY_MODE_VCO;
1862 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1863 tty_mode = TTY_MODE_HCO;
1864 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1865 tty_mode = TTY_MODE_FULL;
1866 else
1867 return -EINVAL;
1868
1869 pthread_mutex_lock(&adev->lock);
1870 if (tty_mode != adev->tty_mode) {
1871 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001872 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1873 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001874 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001875 }
1876 pthread_mutex_unlock(&adev->lock);
1877 }
1878
1879 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1880 if (ret >= 0) {
1881 /* When set to false, HAL should disable EC and NS
1882 * But it is currently not supported.
1883 */
1884 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1885 adev->bluetooth_nrec = true;
1886 else
1887 adev->bluetooth_nrec = false;
1888 }
1889
1890 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1891 if (ret >= 0) {
1892 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1893 adev->screen_off = false;
1894 else
1895 adev->screen_off = true;
1896 }
1897
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001898 ret = str_parms_get_int(parms, "rotation", &val);
1899 if (ret >= 0) {
1900 bool reverse_speakers = false;
1901 switch(val) {
1902 // FIXME: note that the code below assumes that the speakers are in the correct placement
1903 // relative to the user when the device is rotated 90deg from its default rotation. This
1904 // assumption is device-specific, not platform-specific like this code.
1905 case 270:
1906 reverse_speakers = true;
1907 break;
1908 case 0:
1909 case 90:
1910 case 180:
1911 break;
1912 default:
1913 ALOGE("%s: unexpected rotation of %d", __func__, val);
1914 }
1915 pthread_mutex_lock(&adev->lock);
1916 if (adev->speaker_lr_swap != reverse_speakers) {
1917 adev->speaker_lr_swap = reverse_speakers;
1918 // only update the selected device if there is active pcm playback
1919 struct audio_usecase *usecase;
1920 struct listnode *node;
1921 list_for_each(node, &adev->usecase_list) {
1922 usecase = node_to_item(node, struct audio_usecase, list);
1923 if (usecase->type == PCM_PLAYBACK) {
1924 select_devices(adev, usecase->id);
1925 break;
1926 }
1927 }
1928 }
1929 pthread_mutex_unlock(&adev->lock);
1930 }
1931
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001933 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001934 return ret;
1935}
1936
1937static char* adev_get_parameters(const struct audio_hw_device *dev,
1938 const char *keys)
1939{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001940 return strdup("");
1941}
1942
1943static int adev_init_check(const struct audio_hw_device *dev)
1944{
1945 return 0;
1946}
1947
1948static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1949{
1950 struct audio_device *adev = (struct audio_device *)dev;
1951 int vol, err = 0;
1952
1953 pthread_mutex_lock(&adev->lock);
1954 adev->voice_volume = volume;
1955 if (adev->mode == AUDIO_MODE_IN_CALL) {
1956 if (volume < 0.0) {
1957 volume = 0.0;
1958 } else if (volume > 1.0) {
1959 volume = 1.0;
1960 }
1961
1962 vol = lrint(volume * 100.0);
1963
1964 // Voice volume levels from android are mapped to driver volume levels as follows.
1965 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1966 // So adjust the volume to get the correct volume index in driver
1967 vol = 100 - vol;
1968
1969 if (adev->csd_client) {
1970 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001971 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972 } else {
1973 err = adev->csd_volume(vol);
1974 if (err < 0) {
1975 ALOGE("%s: csd_client error %d", __func__, err);
1976 }
1977 }
1978 } else {
1979 ALOGE("%s: No CSD Client present", __func__);
1980 }
1981 }
1982 pthread_mutex_unlock(&adev->lock);
1983 return err;
1984}
1985
1986static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1987{
1988 return -ENOSYS;
1989}
1990
1991static int adev_get_master_volume(struct audio_hw_device *dev,
1992 float *volume)
1993{
1994 return -ENOSYS;
1995}
1996
1997static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1998{
1999 return -ENOSYS;
2000}
2001
2002static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2003{
2004 return -ENOSYS;
2005}
2006
2007static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2008{
2009 struct audio_device *adev = (struct audio_device *)dev;
2010
2011 pthread_mutex_lock(&adev->lock);
2012 if (adev->mode != mode) {
2013 adev->mode = mode;
2014 }
2015 pthread_mutex_unlock(&adev->lock);
2016 return 0;
2017}
2018
2019static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2020{
2021 struct audio_device *adev = (struct audio_device *)dev;
2022 int err = 0;
2023
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002024 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 adev->mic_mute = state;
2026 if (adev->mode == AUDIO_MODE_IN_CALL) {
2027 if (adev->csd_client) {
2028 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002029 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 } else {
2031 err = adev->csd_mic_mute(state);
2032 if (err < 0) {
2033 ALOGE("%s: csd_client error %d", __func__, err);
2034 }
2035 }
2036 } else {
2037 ALOGE("%s: No CSD Client present", __func__);
2038 }
2039 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002040 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 return err;
2042}
2043
2044static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2045{
2046 struct audio_device *adev = (struct audio_device *)dev;
2047
2048 *state = adev->mic_mute;
2049
2050 return 0;
2051}
2052
2053static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2054 const struct audio_config *config)
2055{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056 int channel_count = popcount(config->channel_mask);
2057
2058 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2059}
2060
2061static int adev_open_input_stream(struct audio_hw_device *dev,
2062 audio_io_handle_t handle,
2063 audio_devices_t devices,
2064 struct audio_config *config,
2065 struct audio_stream_in **stream_in)
2066{
2067 struct audio_device *adev = (struct audio_device *)dev;
2068 struct stream_in *in;
2069 int ret, buffer_size, frame_size;
2070 int channel_count = popcount(config->channel_mask);
2071
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002072 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002073 *stream_in = NULL;
2074 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2075 return -EINVAL;
2076
2077 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2078
2079 in->stream.common.get_sample_rate = in_get_sample_rate;
2080 in->stream.common.set_sample_rate = in_set_sample_rate;
2081 in->stream.common.get_buffer_size = in_get_buffer_size;
2082 in->stream.common.get_channels = in_get_channels;
2083 in->stream.common.get_format = in_get_format;
2084 in->stream.common.set_format = in_set_format;
2085 in->stream.common.standby = in_standby;
2086 in->stream.common.dump = in_dump;
2087 in->stream.common.set_parameters = in_set_parameters;
2088 in->stream.common.get_parameters = in_get_parameters;
2089 in->stream.common.add_audio_effect = in_add_audio_effect;
2090 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2091 in->stream.set_gain = in_set_gain;
2092 in->stream.read = in_read;
2093 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2094
2095 in->device = devices;
2096 in->source = AUDIO_SOURCE_DEFAULT;
2097 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002098 in->standby = 1;
2099 in->channel_mask = config->channel_mask;
2100
2101 /* Update config params with the requested sample rate and channels */
2102 in->usecase = USECASE_AUDIO_RECORD;
2103 in->config = pcm_config_audio_capture;
2104 in->config.channels = channel_count;
2105 in->config.rate = config->sample_rate;
2106
2107 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2108 buffer_size = get_input_buffer_size(config->sample_rate,
2109 config->format,
2110 channel_count);
2111 in->config.period_size = buffer_size / frame_size;
2112
2113 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002114 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002115 return 0;
2116
2117err_open:
2118 free(in);
2119 *stream_in = NULL;
2120 return ret;
2121}
2122
2123static void adev_close_input_stream(struct audio_hw_device *dev,
2124 struct audio_stream_in *stream)
2125{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002126 ALOGD("%s", __func__);
2127
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128 in_standby(&stream->common);
2129 free(stream);
2130
2131 return;
2132}
2133
2134static int adev_dump(const audio_hw_device_t *device, int fd)
2135{
2136 return 0;
2137}
2138
2139static int adev_close(hw_device_t *device)
2140{
2141 struct audio_device *adev = (struct audio_device *)device;
2142 audio_route_free(adev->audio_route);
2143 free(device);
2144 return 0;
2145}
2146
2147static void init_platform_data(struct audio_device *adev)
2148{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002149 char platform[PROPERTY_VALUE_MAX];
2150 char baseband[PROPERTY_VALUE_MAX];
2151 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002152
2153 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002154 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002155 adev->fluence_in_voice_call = false;
2156 adev->fluence_in_voice_rec = false;
2157 adev->mic_type_analog = false;
2158
2159 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002160 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002161 adev->mic_type_analog = true;
2162
2163 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002164 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002165 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2166 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002167 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002168 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2169 adev->acdb_settings |= DMIC_FLAG;
2170 }
2171
2172 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2173 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002174 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002175 adev->fluence_in_voice_call = true;
2176 }
2177
2178 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002179 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002180 adev->fluence_in_voice_rec = true;
2181 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002182
2183 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002184 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002185 adev->fluence_in_spkr_mode = true;
2186 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002187 }
2188
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2190 if (adev->acdb_handle == NULL) {
2191 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2192 } else {
2193 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2195 "acdb_loader_deallocate_ACDB");
2196 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2197 "acdb_loader_send_audio_cal");
2198 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2199 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002200 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2201 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002203 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 else
2205 adev->acdb_init();
2206 }
2207
2208 /* If platform is Fusion3, load CSD Client specific symbols
2209 * Voice call is handled by MDM and apps processor talks to
2210 * MDM through CSD Client
2211 */
2212 property_get("ro.board.platform", platform, "");
2213 property_get("ro.baseband", baseband, "");
2214 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2215 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2216 if (adev->csd_client == NULL)
2217 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2218 }
2219
2220 if (adev->csd_client) {
2221 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002222 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2223 "csd_client_deinit");
2224 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2225 "csd_client_disable_device");
2226 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2227 "csd_client_enable_device");
2228 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2229 "csd_client_start_voice");
2230 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2231 "csd_client_stop_voice");
2232 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2233 "csd_client_volume");
2234 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2235 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002236 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2237 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238
2239 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002240 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002241 } else {
2242 adev->csd_client_init();
2243 }
2244 }
2245}
2246
2247static int adev_open(const hw_module_t *module, const char *name,
2248 hw_device_t **device)
2249{
2250 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002251 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002253 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002254 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2255
2256 adev = calloc(1, sizeof(struct audio_device));
2257
2258 adev->mixer = mixer_open(MIXER_CARD);
2259 if (!adev->mixer) {
2260 ALOGE("Unable to open the mixer, aborting.");
2261 return -ENOSYS;
2262 }
2263
2264 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2265 if (!adev->audio_route) {
2266 free(adev);
2267 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2268 *device = NULL;
2269 return -EINVAL;
2270 }
2271
2272 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2273 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2274 adev->device.common.module = (struct hw_module_t *)module;
2275 adev->device.common.close = adev_close;
2276
2277 adev->device.init_check = adev_init_check;
2278 adev->device.set_voice_volume = adev_set_voice_volume;
2279 adev->device.set_master_volume = adev_set_master_volume;
2280 adev->device.get_master_volume = adev_get_master_volume;
2281 adev->device.set_master_mute = adev_set_master_mute;
2282 adev->device.get_master_mute = adev_get_master_mute;
2283 adev->device.set_mode = adev_set_mode;
2284 adev->device.set_mic_mute = adev_set_mic_mute;
2285 adev->device.get_mic_mute = adev_get_mic_mute;
2286 adev->device.set_parameters = adev_set_parameters;
2287 adev->device.get_parameters = adev_get_parameters;
2288 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2289 adev->device.open_output_stream = adev_open_output_stream;
2290 adev->device.close_output_stream = adev_close_output_stream;
2291 adev->device.open_input_stream = adev_open_input_stream;
2292 adev->device.close_input_stream = adev_close_input_stream;
2293 adev->device.dump = adev_dump;
2294
2295 /* Set the default route before the PCM stream is opened */
2296 pthread_mutex_lock(&adev->lock);
2297 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002298 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002299 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002300 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301 adev->voice_call_rx = NULL;
2302 adev->voice_call_tx = NULL;
2303 adev->voice_volume = 1.0f;
2304 adev->tty_mode = TTY_MODE_OFF;
2305 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002307 adev->acdb_settings = TTY_MODE_OFF;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002308 adev->speaker_lr_swap = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002309 for (i = 0; i < SND_DEVICE_MAX; i++) {
2310 adev->snd_dev_ref_cnt[i] = 0;
2311 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002312 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313 pthread_mutex_unlock(&adev->lock);
2314
2315 /* Loads platform specific libraries dynamically */
2316 init_platform_data(adev);
2317
2318 *device = &adev->device.common;
2319
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002320 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002321 return 0;
2322}
2323
2324static struct hw_module_methods_t hal_module_methods = {
2325 .open = adev_open,
2326};
2327
2328struct audio_module HAL_MODULE_INFO_SYM = {
2329 .common = {
2330 .tag = HARDWARE_MODULE_TAG,
2331 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2332 .hal_api_version = HARDWARE_HAL_API_VERSION,
2333 .id = AUDIO_HARDWARE_MODULE_ID,
2334 .name = "QCOM Audio HAL",
2335 .author = "Code Aurora Forum",
2336 .methods = &hal_module_methods,
2337 },
2338};