blob: 275272ab821041f4b765c2518bea6dcb9a9095ff [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 Alamanda518da372013-06-05 14:11:39 -0700429static void check_and_route_capture_usecases(struct audio_device *adev,
430 struct audio_usecase *uc_info,
431 snd_device_t snd_device)
432{
433 struct listnode *node;
434 struct audio_usecase *usecase;
435 bool switch_device[AUDIO_USECASE_MAX];
436 int i, num_uc_to_switch = 0;
437
438 /*
439 * This function is to make sure that all the active capture usecases
440 * are always routed to the same input sound device.
441 * For example, if audio-record and voice-call usecases are currently
442 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
443 * is received for voice call then we have to make sure that audio-record
444 * usecase is also switched to earpiece i.e. voice-dmic-ef,
445 * because of the limitation that two devices cannot be enabled
446 * at the same time if they share the same backend.
447 */
448 for (i = 0; i < AUDIO_USECASE_MAX; i++)
449 switch_device[i] = false;
450
451 list_for_each(node, &adev->usecase_list) {
452 usecase = node_to_item(node, struct audio_usecase, list);
453 if (usecase->type != PCM_PLAYBACK &&
454 usecase != uc_info &&
455 usecase->in_snd_device != snd_device) {
456 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
457 __func__, use_case_table[usecase->id],
458 device_table[usecase->in_snd_device]);
459 disable_audio_route(adev, usecase, false);
460 switch_device[usecase->id] = true;
461 num_uc_to_switch++;
462 }
463 }
464
465 if (num_uc_to_switch) {
466 /* Make sure all the streams are de-routed before disabling the device */
467 audio_route_update_mixer(adev->audio_route);
468
469 list_for_each(node, &adev->usecase_list) {
470 usecase = node_to_item(node, struct audio_usecase, list);
471 if (switch_device[usecase->id]) {
472 disable_snd_device(adev, usecase->in_snd_device, false);
473 enable_snd_device(adev, snd_device, false);
474 }
475 }
476
477 /* Make sure new snd device is enabled before re-routing the streams */
478 audio_route_update_mixer(adev->audio_route);
479
480 /* Re-route all the usecases on the shared backend other than the
481 specified usecase to new snd devices */
482 list_for_each(node, &adev->usecase_list) {
483 usecase = node_to_item(node, struct audio_usecase, list);
484 /* Update the in_snd_device only before enabling the audio route */
485 if (switch_device[usecase->id] ) {
486 usecase->in_snd_device = snd_device;
487 enable_audio_route(adev, usecase, false);
488 }
489 }
490
491 audio_route_update_mixer(adev->audio_route);
492 }
493}
494
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700495static int set_echo_reference(struct mixer *mixer, const char* ec_ref)
496{
497 struct mixer_ctl *ctl;
498 const char *mixer_ctl_name = "EC_REF_RX";
499
500 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
501 if (!ctl) {
502 ALOGE("%s: Could not get ctl for mixer cmd - %s",
503 __func__, mixer_ctl_name);
504 return -EINVAL;
505 }
506 ALOGV("Setting EC Reference: %s", ec_ref);
507 mixer_ctl_set_enum_by_string(ctl, ec_ref);
508 return 0;
509}
510
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511static int set_hdmi_channels(struct mixer *mixer,
512 int channel_count)
513{
514 struct mixer_ctl *ctl;
515 const char *channel_cnt_str = NULL;
516 const char *mixer_ctl_name = "HDMI_RX Channels";
517 switch (channel_count) {
518 case 8:
519 channel_cnt_str = "Eight"; break;
520 case 7:
521 channel_cnt_str = "Seven"; break;
522 case 6:
523 channel_cnt_str = "Six"; break;
524 case 5:
525 channel_cnt_str = "Five"; break;
526 case 4:
527 channel_cnt_str = "Four"; break;
528 case 3:
529 channel_cnt_str = "Three"; break;
530 default:
531 channel_cnt_str = "Two"; break;
532 }
533 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
534 if (!ctl) {
535 ALOGE("%s: Could not get ctl for mixer cmd - %s",
536 __func__, mixer_ctl_name);
537 return -EINVAL;
538 }
539 ALOGV("HDMI channel count: %s", channel_cnt_str);
540 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
541 return 0;
542}
543
544/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700545static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700547 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800548 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549
550 switch (channels) {
551 /*
552 * Do not handle stereo output in Multi-channel cases
553 * Stereo case is handled in normal playback path
554 */
555 case 6:
556 ALOGV("%s: HDMI supports 5.1", __func__);
557 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
558 break;
559 case 8:
560 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
561 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
562 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
563 break;
564 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700565 ALOGE("HDMI does not support multi channel playback");
566 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800567 break;
568 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700569 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800570}
571
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700572static snd_device_t get_output_snd_device(struct audio_device *adev,
573 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700575 audio_mode_t mode = adev->mode;
576 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800577
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800578 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579 if (devices == AUDIO_DEVICE_NONE ||
580 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800581 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800582 goto exit;
583 }
584
585 if (mode == AUDIO_MODE_IN_CALL) {
586 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
587 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800588 if (adev->tty_mode == TTY_MODE_FULL)
589 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
590 else if (adev->tty_mode == TTY_MODE_VCO)
591 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
592 else if (adev->tty_mode == TTY_MODE_HCO)
593 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
594 else
595 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800596 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
597 snd_device = SND_DEVICE_OUT_BT_SCO;
598 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
599 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
600 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800601 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800602 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
603 else
604 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800605 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800606 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800607 goto exit;
608 }
609 }
610
611 if (popcount(devices) == 2) {
612 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
613 AUDIO_DEVICE_OUT_SPEAKER)) {
614 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
615 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
616 AUDIO_DEVICE_OUT_SPEAKER)) {
617 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
618 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
619 AUDIO_DEVICE_OUT_SPEAKER)) {
620 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
621 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800622 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 goto exit;
624 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800625 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800626 goto exit;
627 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800628 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800629
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800630 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800631 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800632 goto exit;
633 }
634
635 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
636 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
637 snd_device = SND_DEVICE_OUT_HEADPHONES;
638 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -0700639 if (adev->speaker_lr_swap) {
640 snd_device = SND_DEVICE_OUT_SPEAKER_REVERSE;
641 } else {
642 snd_device = SND_DEVICE_OUT_SPEAKER;
643 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
645 snd_device = SND_DEVICE_OUT_BT_SCO;
646 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
647 snd_device = SND_DEVICE_OUT_HDMI ;
648 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
649 snd_device = SND_DEVICE_OUT_HANDSET;
650 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800651 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800652 }
653exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800654 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 return snd_device;
656}
657
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700658static snd_device_t get_input_snd_device(struct audio_device *adev,
659 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660{
Eric Laurentc8400632013-02-14 19:04:54 -0800661 audio_source_t source = (adev->active_input == NULL) ?
662 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
663
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800664 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800665 audio_devices_t in_device = ((adev->active_input == NULL) ?
666 AUDIO_DEVICE_NONE : adev->active_input->device)
667 & ~AUDIO_DEVICE_BIT_IN;
668 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
669 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800670 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800671
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800672 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673 __func__, out_device, in_device);
674 if (mode == AUDIO_MODE_IN_CALL) {
675 if (out_device == AUDIO_DEVICE_NONE) {
676 ALOGE("%s: No output device set for voice call", __func__);
677 goto exit;
678 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800679 if (adev->tty_mode != TTY_MODE_OFF) {
680 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
681 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
682 switch (adev->tty_mode) {
683 case TTY_MODE_FULL:
684 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
685 break;
686 case TTY_MODE_VCO:
687 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
688 break;
689 case TTY_MODE_HCO:
690 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
691 break;
692 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800693 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800694 }
695 goto exit;
696 }
697 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
699 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800700 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
701 snd_device = SND_DEVICE_IN_HANDSET_MIC;
702 } else {
703 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800704 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800705 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
706 else
707 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
708 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
709 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
710 else
711 snd_device = SND_DEVICE_IN_HANDSET_MIC;
712 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
714 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
715 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
716 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
717 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800718 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800719 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
720 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800721 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800722 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
723 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
724 } else {
725 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
726 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727 }
728 } else if (source == AUDIO_SOURCE_CAMCORDER) {
729 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
730 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
731 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
732 }
733 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
734 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800735 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
736 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
737 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
738 else if (adev->fluence_in_voice_rec)
739 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
Eric Laurentc8400632013-02-14 19:04:54 -0800740 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
741 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
742 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
743 else if (adev->fluence_in_voice_rec)
744 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700745 }
746
747 if (snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800748 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700749 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800751 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
752 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
753 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700754 if (adev->active_input) {
755 if (adev->active_input->enable_aec) {
756 if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
757 if (adev->mic_type_analog)
758 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
759 else
760 snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC;
761 } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
762 snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC;
763 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
764 snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC;
765 }
766 set_echo_reference(adev->mixer, "SLIM_RX");
767 } else
768 set_echo_reference(adev->mixer, "NONE");
769 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800770 } else if (source == AUDIO_SOURCE_DEFAULT) {
771 goto exit;
772 }
773
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -0700774
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800775 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776 goto exit;
777 }
778
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800779 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800780 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
781 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800783 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800785 if (adev->mic_type_analog)
786 snd_device = SND_DEVICE_IN_HANDSET_MIC;
787 else
788 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
790 snd_device = SND_DEVICE_IN_HEADSET_MIC;
791 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
792 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
793 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
794 snd_device = SND_DEVICE_IN_HDMI_MIC;
795 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800796 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800797 ALOGW("%s: Using default handset-mic", __func__);
798 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800799 }
800 } else {
801 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800802 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800803 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
804 snd_device = SND_DEVICE_IN_HEADSET_MIC;
805 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
806 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
807 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800808 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800810 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800811 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
812 snd_device = SND_DEVICE_IN_HDMI_MIC;
813 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800814 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800815 ALOGW("%s: Using default handset-mic", __func__);
816 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800817 }
818 }
819exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800820 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800821 return snd_device;
822}
823
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700824static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
825 audio_usecase_t uc_id)
826{
827 struct audio_usecase *usecase;
828 struct listnode *node;
829
830 list_for_each(node, &adev->usecase_list) {
831 usecase = node_to_item(node, struct audio_usecase, list);
832 if (usecase->id == uc_id)
833 return usecase;
834 }
835 return NULL;
836}
837
838static int select_devices(struct audio_device *adev,
839 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800840{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800841 snd_device_t out_snd_device = SND_DEVICE_NONE;
842 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700843 struct audio_usecase *usecase = NULL;
844 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800845 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800846 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700847 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800848
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700849 usecase = get_usecase_from_list(adev, uc_id);
850 if (usecase == NULL) {
851 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
852 return -EINVAL;
853 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800854
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700855 if (usecase->type == VOICE_CALL) {
856 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
857 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
858 usecase->devices = usecase->stream.out->devices;
859 } else {
860 /*
861 * If the voice call is active, use the sound devices of voice call usecase
862 * so that it would not result any device switch. All the usecases will
863 * be switched to new device when select_devices() is called for voice call
864 * usecase. This is to avoid switching devices for voice call when
865 * check_usecases_codec_backend() is called below.
866 */
867 if (adev->in_call) {
868 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
869 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
870 in_snd_device = vc_usecase->in_snd_device;
871 out_snd_device = vc_usecase->out_snd_device;
872 }
873 }
874 if (usecase->type == PCM_PLAYBACK) {
875 usecase->devices = usecase->stream.out->devices;
876 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700877 if (out_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700878 out_snd_device = get_output_snd_device(adev,
879 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700880 if (usecase->stream.out == adev->primary_output &&
881 adev->active_input &&
882 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
883 select_devices(adev, adev->active_input->usecase);
884 }
885 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700886 } else if (usecase->type == PCM_CAPTURE) {
887 usecase->devices = usecase->stream.in->device;
888 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700889 if (in_snd_device == SND_DEVICE_NONE) {
890 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
891 adev->primary_output && !adev->primary_output->standby) {
892 in_snd_device = get_input_snd_device(adev,
893 adev->primary_output->devices);
894 } else {
895 in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE);
896 }
897 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700898 }
899 }
900
901 if (out_snd_device == usecase->out_snd_device &&
902 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800903 return 0;
904 }
905
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800906 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
907 out_snd_device, device_table[out_snd_device],
908 in_snd_device, device_table[in_snd_device]);
909
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 /*
911 * Limitation: While in call, to do a device switch we need to disable
912 * and enable both RX and TX devices though one of them is same as current
913 * device.
914 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700915 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
916 usecase->in_snd_device != SND_DEVICE_NONE &&
917 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800918 /* This must be called before disabling the mixer controls on APQ side */
919 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800920 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800921 } else {
922 status = adev->csd_disable_device();
923 if (status < 0) {
924 ALOGE("%s: csd_client_disable_device, failed, error %d",
925 __func__, status);
926 }
927 }
928 }
929
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700930 /* Disable current sound devices */
931 if (usecase->out_snd_device != SND_DEVICE_NONE) {
932 disable_audio_route(adev, usecase, true);
933 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800934 }
935
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700936 if (usecase->in_snd_device != SND_DEVICE_NONE) {
937 disable_audio_route(adev, usecase, true);
938 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939 }
940
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700941 /* Enable new sound devices */
942 if (out_snd_device != SND_DEVICE_NONE) {
943 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
944 check_usecases_codec_backend(adev, usecase, out_snd_device);
945 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800946 }
947
Ravi Kumar Alamanda518da372013-06-05 14:11:39 -0700948 if (in_snd_device != SND_DEVICE_NONE) {
949 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700950 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamanda518da372013-06-05 14:11:39 -0700951 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700952
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800953 audio_route_update_mixer(adev->audio_route);
954
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700955 usecase->in_snd_device = in_snd_device;
956 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800957
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700958 enable_audio_route(adev, usecase, true);
959
960 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800961 if (adev->csd_enable_device == NULL) {
962 ALOGE("%s: dlsym error for csd_client_enable_device",
963 __func__);
964 } else {
965 acdb_rx_id = get_acdb_device_id(out_snd_device);
966 acdb_tx_id = get_acdb_device_id(in_snd_device);
967
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700968 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
969 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
970 adev->acdb_settings);
971 if (status < 0) {
972 ALOGE("%s: csd_client_enable_device, failed, error %d",
973 __func__, status);
974 }
975 } else {
976 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
977 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800978 }
979 }
980 }
981
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800982 return status;
983}
984
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800985static int stop_input_stream(struct stream_in *in)
986{
987 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988 struct audio_usecase *uc_info;
989 struct audio_device *adev = in->dev;
990
Eric Laurentc8400632013-02-14 19:04:54 -0800991 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700993 ALOGD("%s: enter: usecase(%d: %s)", __func__,
994 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800995 uc_info = get_usecase_from_list(adev, in->usecase);
996 if (uc_info == NULL) {
997 ALOGE("%s: Could not find the usecase (%d) in the list",
998 __func__, in->usecase);
999 return -EINVAL;
1000 }
1001
Eric Laurent150dbfe2013-02-27 14:31:02 -08001002 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001003 disable_audio_route(adev, uc_info, true);
1004
1005 /* 2. Disable the tx device */
1006 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001007
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001008 list_remove(&uc_info->list);
1009 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001011 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 return ret;
1013}
1014
1015int start_input_stream(struct stream_in *in)
1016{
1017 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -08001018 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 struct audio_usecase *uc_info;
1020 struct audio_device *adev = in->dev;
1021
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001022 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001023 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
1024 in->usecase,
1025 PCM_CAPTURE);
1026 if (in->pcm_device_id < 0) {
1027 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
1028 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -08001029 ret = -EINVAL;
1030 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001032
1033 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001034 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1035 uc_info->id = in->usecase;
1036 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001037 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001038 uc_info->devices = in->device;
1039 uc_info->in_snd_device = SND_DEVICE_NONE;
1040 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001042 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001043 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044
Eric Laurentc8400632013-02-14 19:04:54 -08001045 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
1046 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
1048 PCM_IN, &in->config);
1049 if (in->pcm && !pcm_is_ready(in->pcm)) {
1050 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
1051 pcm_close(in->pcm);
1052 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -08001053 ret = -EIO;
1054 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001055 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001056 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -08001057 return ret;
1058
1059error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -08001061
1062error_config:
1063 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001064 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -08001065
1066 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067}
1068
1069static int stop_output_stream(struct stream_out *out)
1070{
1071 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 struct audio_usecase *uc_info;
1073 struct audio_device *adev = out->dev;
1074
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001075 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1076 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 uc_info = get_usecase_from_list(adev, out->usecase);
1078 if (uc_info == NULL) {
1079 ALOGE("%s: Could not find the usecase (%d) in the list",
1080 __func__, out->usecase);
1081 return -EINVAL;
1082 }
1083
Eric Laurent150dbfe2013-02-27 14:31:02 -08001084 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001085 disable_audio_route(adev, uc_info, true);
1086
1087 /* 2. Disable the rx device */
1088 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001090 list_remove(&uc_info->list);
1091 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001092
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001093 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094 return ret;
1095}
1096
1097int start_output_stream(struct stream_out *out)
1098{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100 struct audio_usecase *uc_info;
1101 struct audio_device *adev = out->dev;
1102
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001103 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
1104 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
1106 out->usecase,
1107 PCM_PLAYBACK);
1108 if (out->pcm_device_id < 0) {
1109 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1110 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001111 ret = -EINVAL;
1112 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113 }
1114
1115 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1116 uc_info->id = out->usecase;
1117 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001118 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001119 uc_info->devices = out->devices;
1120 uc_info->in_snd_device = SND_DEVICE_NONE;
1121 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001122
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001123 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001125 select_devices(adev, out->usecase);
1126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1128 __func__, 0, out->pcm_device_id);
1129 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1130 PCM_OUT, &out->config);
1131 if (out->pcm && !pcm_is_ready(out->pcm)) {
1132 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1133 pcm_close(out->pcm);
1134 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001135 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001136 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001138 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001140error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001142error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001143 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001144}
1145
1146static int stop_voice_call(struct audio_device *adev)
1147{
1148 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 struct audio_usecase *uc_info;
1150
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001151 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001152 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 if (adev->csd_client) {
1154 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001155 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001156 } else {
1157 ret = adev->csd_stop_voice();
1158 if (ret < 0) {
1159 ALOGE("%s: csd_client error %d\n", __func__, ret);
1160 }
1161 }
1162 }
1163
1164 /* 1. Close the PCM devices */
1165 if (adev->voice_call_rx) {
1166 pcm_close(adev->voice_call_rx);
1167 adev->voice_call_rx = NULL;
1168 }
1169 if (adev->voice_call_tx) {
1170 pcm_close(adev->voice_call_tx);
1171 adev->voice_call_tx = NULL;
1172 }
1173
1174 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1175 if (uc_info == NULL) {
1176 ALOGE("%s: Could not find the usecase (%d) in the list",
1177 __func__, USECASE_VOICE_CALL);
1178 return -EINVAL;
1179 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180
1181 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001182 disable_audio_route(adev, uc_info, true);
1183
1184 /* 3. Disable the rx and tx devices */
1185 disable_snd_device(adev, uc_info->out_snd_device, false);
1186 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001188 list_remove(&uc_info->list);
1189 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001191 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192 return ret;
1193}
1194
1195static int start_voice_call(struct audio_device *adev)
1196{
1197 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198 struct audio_usecase *uc_info;
1199 int pcm_dev_rx_id, pcm_dev_tx_id;
1200
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001201 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202
1203 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1204 uc_info->id = USECASE_VOICE_CALL;
1205 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001206 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001207 uc_info->devices = adev->primary_output->devices;
1208 uc_info->in_snd_device = SND_DEVICE_NONE;
1209 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001210
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001211 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001213 select_devices(adev, USECASE_VOICE_CALL);
1214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001215 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1216 PCM_PLAYBACK);
1217 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1218 PCM_CAPTURE);
1219
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1221 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1222 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001223 ret = -EIO;
1224 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225 }
1226
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001227 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228 __func__, SOUND_CARD, pcm_dev_rx_id);
1229 adev->voice_call_rx = pcm_open(SOUND_CARD,
1230 pcm_dev_rx_id,
1231 PCM_OUT, &pcm_config_voice_call);
1232 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1233 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001234 ret = -EIO;
1235 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 }
1237
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001238 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 __func__, SOUND_CARD, pcm_dev_tx_id);
1240 adev->voice_call_tx = pcm_open(SOUND_CARD,
1241 pcm_dev_tx_id,
1242 PCM_IN, &pcm_config_voice_call);
1243 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1244 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001245 ret = -EIO;
1246 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001247 }
1248 pcm_start(adev->voice_call_rx);
1249 pcm_start(adev->voice_call_tx);
1250
1251 if (adev->csd_client) {
1252 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001253 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001254 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255 } else {
1256 ret = adev->csd_start_voice();
1257 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001258 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1259 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 }
1261 }
1262 }
1263
1264 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001265 return 0;
1266
1267error_start_voice:
1268 stop_voice_call(adev);
1269
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001270 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271 return ret;
1272}
1273
1274static int check_input_parameters(uint32_t sample_rate,
1275 audio_format_t format,
1276 int channel_count)
1277{
1278 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1279
1280 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1281
1282 switch (sample_rate) {
1283 case 8000:
1284 case 11025:
1285 case 12000:
1286 case 16000:
1287 case 22050:
1288 case 24000:
1289 case 32000:
1290 case 44100:
1291 case 48000:
1292 break;
1293 default:
1294 return -EINVAL;
1295 }
1296
1297 return 0;
1298}
1299
1300static size_t get_input_buffer_size(uint32_t sample_rate,
1301 audio_format_t format,
1302 int channel_count)
1303{
1304 size_t size = 0;
1305
1306 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1307
1308 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1309 size = (sample_rate * 20) / 1000;
1310 } else if (sample_rate == 11025 || sample_rate == 12000) {
1311 size = 256;
1312 } else if (sample_rate == 22050 || sample_rate == 24000) {
1313 size = 512;
1314 } else if (sample_rate == 44100 || sample_rate == 48000) {
1315 size = 1024;
1316 }
1317
1318 return size * sizeof(short) * channel_count;
1319}
1320
1321static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1322{
1323 struct stream_out *out = (struct stream_out *)stream;
1324
1325 return out->config.rate;
1326}
1327
1328static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1329{
1330 return -ENOSYS;
1331}
1332
1333static size_t out_get_buffer_size(const struct audio_stream *stream)
1334{
1335 struct stream_out *out = (struct stream_out *)stream;
1336
1337 return out->config.period_size * audio_stream_frame_size(stream);
1338}
1339
1340static uint32_t out_get_channels(const struct audio_stream *stream)
1341{
1342 struct stream_out *out = (struct stream_out *)stream;
1343
1344 return out->channel_mask;
1345}
1346
1347static audio_format_t out_get_format(const struct audio_stream *stream)
1348{
1349 return AUDIO_FORMAT_PCM_16_BIT;
1350}
1351
1352static int out_set_format(struct audio_stream *stream, audio_format_t format)
1353{
1354 return -ENOSYS;
1355}
1356
1357static int out_standby(struct audio_stream *stream)
1358{
1359 struct stream_out *out = (struct stream_out *)stream;
1360 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001361 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1362 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363 pthread_mutex_lock(&out->lock);
1364
1365 if (!out->standby) {
1366 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001367 if (out->pcm) {
1368 pcm_close(out->pcm);
1369 out->pcm = NULL;
1370 }
1371 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001372 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001373 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374 }
1375 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001376 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377 return 0;
1378}
1379
1380static int out_dump(const struct audio_stream *stream, int fd)
1381{
1382 return 0;
1383}
1384
1385static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1386{
1387 struct stream_out *out = (struct stream_out *)stream;
1388 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001389 struct audio_usecase *usecase;
1390 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 struct str_parms *parms;
1392 char value[32];
1393 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001394 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001396 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1397 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398 parms = str_parms_create_str(kvpairs);
1399 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1400 if (ret >= 0) {
1401 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001403 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001405 /*
1406 * When HDMI cable is unplugged the music playback is paused and
1407 * the policy manager sends routing=0. But the audioflinger
1408 * continues to write data until standby time (3sec).
1409 * As the HDMI core is turned off, the write gets blocked.
1410 * Avoid this by routing audio to speaker until standby.
1411 */
1412 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1413 val == AUDIO_DEVICE_NONE) {
1414 val = AUDIO_DEVICE_OUT_SPEAKER;
1415 }
1416
1417 /*
1418 * select_devices() call below switches all the usecases on the same
1419 * backend to the new device. Refer to check_usecases_codec_backend() in
1420 * the select_devices(). But how do we undo this?
1421 *
1422 * For example, music playback is active on headset (deep-buffer usecase)
1423 * and if we go to ringtones and select a ringtone, low-latency usecase
1424 * will be started on headset+speaker. As we can't enable headset+speaker
1425 * and headset devices at the same time, select_devices() switches the music
1426 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1427 * So when the ringtone playback is completed, how do we undo the same?
1428 *
1429 * We are relying on the out_set_parameters() call on deep-buffer output,
1430 * once the ringtone playback is ended.
1431 * NOTE: We should not check if the current devices are same as new devices.
1432 * Because select_devices() must be called to switch back the music
1433 * playback to headset.
1434 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001435 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001436 out->devices = val;
1437
1438 if (!out->standby)
1439 select_devices(adev, out->usecase);
1440
1441 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1442 (out == adev->primary_output)) {
1443 start_voice_call(adev);
1444 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1445 (out == adev->primary_output)) {
1446 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001447 }
1448 }
1449
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001450 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1451 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001452 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001456 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001457 }
1458 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001459 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001460 return ret;
1461}
1462
1463static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1464{
1465 struct stream_out *out = (struct stream_out *)stream;
1466 struct str_parms *query = str_parms_create_str(keys);
1467 char *str;
1468 char value[256];
1469 struct str_parms *reply = str_parms_create();
1470 size_t i, j;
1471 int ret;
1472 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001473 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001474 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1475 if (ret >= 0) {
1476 value[0] = '\0';
1477 i = 0;
1478 while (out->supported_channel_masks[i] != 0) {
1479 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1480 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1481 if (!first) {
1482 strcat(value, "|");
1483 }
1484 strcat(value, out_channels_name_to_enum_table[j].name);
1485 first = false;
1486 break;
1487 }
1488 }
1489 i++;
1490 }
1491 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1492 str = str_parms_to_str(reply);
1493 } else {
1494 str = strdup(keys);
1495 }
1496 str_parms_destroy(query);
1497 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001498 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499 return str;
1500}
1501
1502static uint32_t out_get_latency(const struct audio_stream_out *stream)
1503{
1504 struct stream_out *out = (struct stream_out *)stream;
1505
1506 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1507}
1508
1509static int out_set_volume(struct audio_stream_out *stream, float left,
1510 float right)
1511{
Eric Laurenta9024de2013-04-04 09:19:12 -07001512 struct stream_out *out = (struct stream_out *)stream;
1513 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1514 /* only take left channel into account: the API is for stereo anyway */
1515 out->muted = (left == 0.0f);
1516 return 0;
1517 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001518 return -ENOSYS;
1519}
1520
1521static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1522 size_t bytes)
1523{
1524 struct stream_out *out = (struct stream_out *)stream;
1525 struct audio_device *adev = out->dev;
1526 int i, ret = -1;
1527
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001528 pthread_mutex_lock(&out->lock);
1529 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001530 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001531 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001533 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001535 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 goto exit;
1537 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001538 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001539
1540 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001541 if (out->muted)
1542 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001543 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1544 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1545 }
1546
1547exit:
1548 pthread_mutex_unlock(&out->lock);
1549
1550 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001551 if (out->pcm)
1552 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553 out_standby(&out->stream.common);
1554 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1555 out_get_sample_rate(&out->stream.common));
1556 }
1557 return bytes;
1558}
1559
1560static int out_get_render_position(const struct audio_stream_out *stream,
1561 uint32_t *dsp_frames)
1562{
1563 return -EINVAL;
1564}
1565
1566static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1567{
1568 return 0;
1569}
1570
1571static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1572{
1573 return 0;
1574}
1575
1576static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1577 int64_t *timestamp)
1578{
1579 return -EINVAL;
1580}
1581
1582/** audio_stream_in implementation **/
1583static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1584{
1585 struct stream_in *in = (struct stream_in *)stream;
1586
1587 return in->config.rate;
1588}
1589
1590static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1591{
1592 return -ENOSYS;
1593}
1594
1595static size_t in_get_buffer_size(const struct audio_stream *stream)
1596{
1597 struct stream_in *in = (struct stream_in *)stream;
1598
1599 return in->config.period_size * audio_stream_frame_size(stream);
1600}
1601
1602static uint32_t in_get_channels(const struct audio_stream *stream)
1603{
1604 struct stream_in *in = (struct stream_in *)stream;
1605
1606 return in->channel_mask;
1607}
1608
1609static audio_format_t in_get_format(const struct audio_stream *stream)
1610{
1611 return AUDIO_FORMAT_PCM_16_BIT;
1612}
1613
1614static int in_set_format(struct audio_stream *stream, audio_format_t format)
1615{
1616 return -ENOSYS;
1617}
1618
1619static int in_standby(struct audio_stream *stream)
1620{
1621 struct stream_in *in = (struct stream_in *)stream;
1622 struct audio_device *adev = in->dev;
1623 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001624 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625 pthread_mutex_lock(&in->lock);
1626 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001627 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001628 if (in->pcm) {
1629 pcm_close(in->pcm);
1630 in->pcm = NULL;
1631 }
1632 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001633 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001634 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635 }
1636 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001637 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 return status;
1639}
1640
1641static int in_dump(const struct audio_stream *stream, int fd)
1642{
1643 return 0;
1644}
1645
1646static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1647{
1648 struct stream_in *in = (struct stream_in *)stream;
1649 struct audio_device *adev = in->dev;
1650 struct str_parms *parms;
1651 char *str;
1652 char value[32];
1653 int ret, val = 0;
1654
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001655 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001656 parms = str_parms_create_str(kvpairs);
1657
1658 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001660 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001661 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001662 if (ret >= 0) {
1663 val = atoi(value);
1664 /* no audio source uses val == 0 */
1665 if ((in->source != val) && (val != 0)) {
1666 in->source = val;
1667 }
1668 }
1669
1670 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1671 if (ret >= 0) {
1672 val = atoi(value);
1673 if ((in->device != val) && (val != 0)) {
1674 in->device = val;
1675 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001676 if (!in->standby)
1677 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 }
1679 }
1680
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001682 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001683
1684 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001685 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001686 return ret;
1687}
1688
1689static char* in_get_parameters(const struct audio_stream *stream,
1690 const char *keys)
1691{
1692 return strdup("");
1693}
1694
1695static int in_set_gain(struct audio_stream_in *stream, float gain)
1696{
1697 return 0;
1698}
1699
1700static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1701 size_t bytes)
1702{
1703 struct stream_in *in = (struct stream_in *)stream;
1704 struct audio_device *adev = in->dev;
1705 int i, ret = -1;
1706
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707 pthread_mutex_lock(&in->lock);
1708 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001709 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001711 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713 goto exit;
1714 }
1715 in->standby = 0;
1716 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717
1718 if (in->pcm) {
1719 ret = pcm_read(in->pcm, buffer, bytes);
1720 }
1721
1722 /*
1723 * Instead of writing zeroes here, we could trust the hardware
1724 * to always provide zeroes when muted.
1725 */
1726 if (ret == 0 && adev->mic_mute)
1727 memset(buffer, 0, bytes);
1728
1729exit:
1730 pthread_mutex_unlock(&in->lock);
1731
1732 if (ret != 0) {
1733 in_standby(&in->stream.common);
1734 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1735 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1736 in_get_sample_rate(&in->stream.common));
1737 }
1738 return bytes;
1739}
1740
1741static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1742{
1743 return 0;
1744}
1745
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001746static int add_remove_audio_effect(const struct audio_stream *stream,
1747 effect_handle_t effect,
1748 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001749{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001750 struct stream_in *in = (struct stream_in *)stream;
1751 int status = 0;
1752 effect_descriptor_t desc;
1753
1754 status = (*effect)->get_descriptor(effect, &desc);
1755 if (status != 0)
1756 return status;
1757
1758 pthread_mutex_lock(&in->lock);
1759 pthread_mutex_lock(&in->dev->lock);
1760 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1761 in->enable_aec != enable &&
1762 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1763 in->enable_aec = enable;
1764 if (!in->standby)
1765 select_devices(in->dev, in->usecase);
1766 }
1767 pthread_mutex_unlock(&in->dev->lock);
1768 pthread_mutex_unlock(&in->lock);
1769
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770 return 0;
1771}
1772
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001773static int in_add_audio_effect(const struct audio_stream *stream,
1774 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001776 ALOGD("%s: effect %p", __func__, effect);
1777 return add_remove_audio_effect(stream, effect, true);
1778}
1779
1780static int in_remove_audio_effect(const struct audio_stream *stream,
1781 effect_handle_t effect)
1782{
1783 ALOGD("%s: effect %p", __func__, effect);
1784 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785}
1786
1787static int adev_open_output_stream(struct audio_hw_device *dev,
1788 audio_io_handle_t handle,
1789 audio_devices_t devices,
1790 audio_output_flags_t flags,
1791 struct audio_config *config,
1792 struct audio_stream_out **stream_out)
1793{
1794 struct audio_device *adev = (struct audio_device *)dev;
1795 struct stream_out *out;
1796 int i, ret;
1797
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001798 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 __func__, config->sample_rate, config->channel_mask, devices, flags);
1800 *stream_out = NULL;
1801 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1802
1803 if (devices == AUDIO_DEVICE_NONE)
1804 devices = AUDIO_DEVICE_OUT_SPEAKER;
1805
1806 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1807 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1808 out->flags = flags;
1809 out->devices = devices;
1810
1811 /* Init use case and pcm_config */
1812 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1813 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001814 pthread_mutex_lock(&adev->lock);
1815 ret = read_hdmi_channel_masks(out);
1816 pthread_mutex_unlock(&adev->lock);
1817 if (ret != 0) {
1818 /* If HDMI does not support multi channel playback, set the default */
1819 out->config.channels = popcount(out->channel_mask);
1820 set_hdmi_channels(adev->mixer, out->config.channels);
1821 goto error_open;
1822 }
1823
1824 if (config->sample_rate == 0)
1825 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1826 if (config->channel_mask == 0)
1827 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1828
1829 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1831 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001832 out->config.rate = config->sample_rate;
1833 out->config.channels = popcount(out->channel_mask);
1834 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1835 set_hdmi_channels(adev->mixer, out->config.channels);
1836 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1837 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1838 out->config = pcm_config_deep_buffer;
1839 } else {
1840 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1841 out->config = pcm_config_low_latency;
1842 }
1843
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001844 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1845 if(adev->primary_output == NULL)
1846 adev->primary_output = out;
1847 else {
1848 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001849 ret = -EEXIST;
1850 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001851 }
1852 }
1853
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854 /* Check if this usecase is already existing */
1855 pthread_mutex_lock(&adev->lock);
1856 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1857 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001859 ret = -EEXIST;
1860 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001861 }
1862 pthread_mutex_unlock(&adev->lock);
1863
1864 out->stream.common.get_sample_rate = out_get_sample_rate;
1865 out->stream.common.set_sample_rate = out_set_sample_rate;
1866 out->stream.common.get_buffer_size = out_get_buffer_size;
1867 out->stream.common.get_channels = out_get_channels;
1868 out->stream.common.get_format = out_get_format;
1869 out->stream.common.set_format = out_set_format;
1870 out->stream.common.standby = out_standby;
1871 out->stream.common.dump = out_dump;
1872 out->stream.common.set_parameters = out_set_parameters;
1873 out->stream.common.get_parameters = out_get_parameters;
1874 out->stream.common.add_audio_effect = out_add_audio_effect;
1875 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1876 out->stream.get_latency = out_get_latency;
1877 out->stream.set_volume = out_set_volume;
1878 out->stream.write = out_write;
1879 out->stream.get_render_position = out_get_render_position;
1880 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1881
1882 out->dev = adev;
1883 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001884 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001885
1886 config->format = out->stream.common.get_format(&out->stream.common);
1887 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1888 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1889
1890 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001891 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001893
1894error_open:
1895 free(out);
1896 *stream_out = NULL;
1897 ALOGD("%s: exit: ret %d", __func__, ret);
1898 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899}
1900
1901static void adev_close_output_stream(struct audio_hw_device *dev,
1902 struct audio_stream_out *stream)
1903{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001904 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905 out_standby(&stream->common);
1906 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001907 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001908}
1909
1910static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1911{
1912 struct audio_device *adev = (struct audio_device *)dev;
1913 struct str_parms *parms;
1914 char *str;
1915 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001916 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001917 int ret;
1918
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001919 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920
1921 parms = str_parms_create_str(kvpairs);
1922 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1923 if (ret >= 0) {
1924 int tty_mode;
1925
1926 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1927 tty_mode = TTY_MODE_OFF;
1928 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1929 tty_mode = TTY_MODE_VCO;
1930 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1931 tty_mode = TTY_MODE_HCO;
1932 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1933 tty_mode = TTY_MODE_FULL;
1934 else
1935 return -EINVAL;
1936
1937 pthread_mutex_lock(&adev->lock);
1938 if (tty_mode != adev->tty_mode) {
1939 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001940 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1941 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001942 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001943 }
1944 pthread_mutex_unlock(&adev->lock);
1945 }
1946
1947 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1948 if (ret >= 0) {
1949 /* When set to false, HAL should disable EC and NS
1950 * But it is currently not supported.
1951 */
1952 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1953 adev->bluetooth_nrec = true;
1954 else
1955 adev->bluetooth_nrec = false;
1956 }
1957
1958 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1959 if (ret >= 0) {
1960 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1961 adev->screen_off = false;
1962 else
1963 adev->screen_off = true;
1964 }
1965
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001966 ret = str_parms_get_int(parms, "rotation", &val);
1967 if (ret >= 0) {
1968 bool reverse_speakers = false;
1969 switch(val) {
1970 // FIXME: note that the code below assumes that the speakers are in the correct placement
1971 // relative to the user when the device is rotated 90deg from its default rotation. This
1972 // assumption is device-specific, not platform-specific like this code.
1973 case 270:
1974 reverse_speakers = true;
1975 break;
1976 case 0:
1977 case 90:
1978 case 180:
1979 break;
1980 default:
1981 ALOGE("%s: unexpected rotation of %d", __func__, val);
1982 }
1983 pthread_mutex_lock(&adev->lock);
1984 if (adev->speaker_lr_swap != reverse_speakers) {
1985 adev->speaker_lr_swap = reverse_speakers;
1986 // only update the selected device if there is active pcm playback
1987 struct audio_usecase *usecase;
1988 struct listnode *node;
1989 list_for_each(node, &adev->usecase_list) {
1990 usecase = node_to_item(node, struct audio_usecase, list);
1991 if (usecase->type == PCM_PLAYBACK) {
1992 select_devices(adev, usecase->id);
1993 break;
1994 }
1995 }
1996 }
1997 pthread_mutex_unlock(&adev->lock);
1998 }
1999
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002001 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002 return ret;
2003}
2004
2005static char* adev_get_parameters(const struct audio_hw_device *dev,
2006 const char *keys)
2007{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008 return strdup("");
2009}
2010
2011static int adev_init_check(const struct audio_hw_device *dev)
2012{
2013 return 0;
2014}
2015
2016static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2017{
2018 struct audio_device *adev = (struct audio_device *)dev;
2019 int vol, err = 0;
2020
2021 pthread_mutex_lock(&adev->lock);
2022 adev->voice_volume = volume;
2023 if (adev->mode == AUDIO_MODE_IN_CALL) {
2024 if (volume < 0.0) {
2025 volume = 0.0;
2026 } else if (volume > 1.0) {
2027 volume = 1.0;
2028 }
2029
2030 vol = lrint(volume * 100.0);
2031
2032 // Voice volume levels from android are mapped to driver volume levels as follows.
2033 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2034 // So adjust the volume to get the correct volume index in driver
2035 vol = 100 - vol;
2036
2037 if (adev->csd_client) {
2038 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002039 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040 } else {
2041 err = adev->csd_volume(vol);
2042 if (err < 0) {
2043 ALOGE("%s: csd_client error %d", __func__, err);
2044 }
2045 }
2046 } else {
2047 ALOGE("%s: No CSD Client present", __func__);
2048 }
2049 }
2050 pthread_mutex_unlock(&adev->lock);
2051 return err;
2052}
2053
2054static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2055{
2056 return -ENOSYS;
2057}
2058
2059static int adev_get_master_volume(struct audio_hw_device *dev,
2060 float *volume)
2061{
2062 return -ENOSYS;
2063}
2064
2065static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2066{
2067 return -ENOSYS;
2068}
2069
2070static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2071{
2072 return -ENOSYS;
2073}
2074
2075static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2076{
2077 struct audio_device *adev = (struct audio_device *)dev;
2078
2079 pthread_mutex_lock(&adev->lock);
2080 if (adev->mode != mode) {
2081 adev->mode = mode;
2082 }
2083 pthread_mutex_unlock(&adev->lock);
2084 return 0;
2085}
2086
2087static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2088{
2089 struct audio_device *adev = (struct audio_device *)dev;
2090 int err = 0;
2091
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002092 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002093 adev->mic_mute = state;
2094 if (adev->mode == AUDIO_MODE_IN_CALL) {
2095 if (adev->csd_client) {
2096 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002097 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002098 } else {
2099 err = adev->csd_mic_mute(state);
2100 if (err < 0) {
2101 ALOGE("%s: csd_client error %d", __func__, err);
2102 }
2103 }
2104 } else {
2105 ALOGE("%s: No CSD Client present", __func__);
2106 }
2107 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002108 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002109 return err;
2110}
2111
2112static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2113{
2114 struct audio_device *adev = (struct audio_device *)dev;
2115
2116 *state = adev->mic_mute;
2117
2118 return 0;
2119}
2120
2121static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2122 const struct audio_config *config)
2123{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124 int channel_count = popcount(config->channel_mask);
2125
2126 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2127}
2128
2129static int adev_open_input_stream(struct audio_hw_device *dev,
2130 audio_io_handle_t handle,
2131 audio_devices_t devices,
2132 struct audio_config *config,
2133 struct audio_stream_in **stream_in)
2134{
2135 struct audio_device *adev = (struct audio_device *)dev;
2136 struct stream_in *in;
2137 int ret, buffer_size, frame_size;
2138 int channel_count = popcount(config->channel_mask);
2139
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002140 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 *stream_in = NULL;
2142 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2143 return -EINVAL;
2144
2145 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2146
2147 in->stream.common.get_sample_rate = in_get_sample_rate;
2148 in->stream.common.set_sample_rate = in_set_sample_rate;
2149 in->stream.common.get_buffer_size = in_get_buffer_size;
2150 in->stream.common.get_channels = in_get_channels;
2151 in->stream.common.get_format = in_get_format;
2152 in->stream.common.set_format = in_set_format;
2153 in->stream.common.standby = in_standby;
2154 in->stream.common.dump = in_dump;
2155 in->stream.common.set_parameters = in_set_parameters;
2156 in->stream.common.get_parameters = in_get_parameters;
2157 in->stream.common.add_audio_effect = in_add_audio_effect;
2158 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2159 in->stream.set_gain = in_set_gain;
2160 in->stream.read = in_read;
2161 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2162
2163 in->device = devices;
2164 in->source = AUDIO_SOURCE_DEFAULT;
2165 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166 in->standby = 1;
2167 in->channel_mask = config->channel_mask;
2168
2169 /* Update config params with the requested sample rate and channels */
2170 in->usecase = USECASE_AUDIO_RECORD;
2171 in->config = pcm_config_audio_capture;
2172 in->config.channels = channel_count;
2173 in->config.rate = config->sample_rate;
2174
2175 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2176 buffer_size = get_input_buffer_size(config->sample_rate,
2177 config->format,
2178 channel_count);
2179 in->config.period_size = buffer_size / frame_size;
2180
2181 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002182 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 return 0;
2184
2185err_open:
2186 free(in);
2187 *stream_in = NULL;
2188 return ret;
2189}
2190
2191static void adev_close_input_stream(struct audio_hw_device *dev,
2192 struct audio_stream_in *stream)
2193{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002194 ALOGD("%s", __func__);
2195
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 in_standby(&stream->common);
2197 free(stream);
2198
2199 return;
2200}
2201
2202static int adev_dump(const audio_hw_device_t *device, int fd)
2203{
2204 return 0;
2205}
2206
2207static int adev_close(hw_device_t *device)
2208{
2209 struct audio_device *adev = (struct audio_device *)device;
2210 audio_route_free(adev->audio_route);
2211 free(device);
2212 return 0;
2213}
2214
2215static void init_platform_data(struct audio_device *adev)
2216{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002217 char platform[PROPERTY_VALUE_MAX];
2218 char baseband[PROPERTY_VALUE_MAX];
2219 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002220
2221 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002222 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002223 adev->fluence_in_voice_call = false;
2224 adev->fluence_in_voice_rec = false;
2225 adev->mic_type_analog = false;
2226
2227 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002228 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002229 adev->mic_type_analog = true;
2230
2231 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002232 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002233 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2234 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002235 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002236 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2237 adev->acdb_settings |= DMIC_FLAG;
2238 }
2239
2240 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2241 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002242 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002243 adev->fluence_in_voice_call = true;
2244 }
2245
2246 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002247 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002248 adev->fluence_in_voice_rec = true;
2249 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002250
2251 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002252 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002253 adev->fluence_in_spkr_mode = true;
2254 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002255 }
2256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002257 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2258 if (adev->acdb_handle == NULL) {
2259 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2260 } else {
2261 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2263 "acdb_loader_deallocate_ACDB");
2264 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2265 "acdb_loader_send_audio_cal");
2266 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2267 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002268 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2269 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002270 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002271 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002272 else
2273 adev->acdb_init();
2274 }
2275
2276 /* If platform is Fusion3, load CSD Client specific symbols
2277 * Voice call is handled by MDM and apps processor talks to
2278 * MDM through CSD Client
2279 */
2280 property_get("ro.board.platform", platform, "");
2281 property_get("ro.baseband", baseband, "");
2282 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2283 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2284 if (adev->csd_client == NULL)
2285 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2286 }
2287
2288 if (adev->csd_client) {
2289 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002290 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2291 "csd_client_deinit");
2292 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2293 "csd_client_disable_device");
2294 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2295 "csd_client_enable_device");
2296 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2297 "csd_client_start_voice");
2298 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2299 "csd_client_stop_voice");
2300 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2301 "csd_client_volume");
2302 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2303 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002304 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2305 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306
2307 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002308 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309 } else {
2310 adev->csd_client_init();
2311 }
2312 }
2313}
2314
2315static int adev_open(const hw_module_t *module, const char *name,
2316 hw_device_t **device)
2317{
2318 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002319 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002320
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002321 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002322 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2323
2324 adev = calloc(1, sizeof(struct audio_device));
2325
2326 adev->mixer = mixer_open(MIXER_CARD);
2327 if (!adev->mixer) {
2328 ALOGE("Unable to open the mixer, aborting.");
2329 return -ENOSYS;
2330 }
2331
2332 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2333 if (!adev->audio_route) {
2334 free(adev);
2335 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2336 *device = NULL;
2337 return -EINVAL;
2338 }
2339
2340 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2341 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2342 adev->device.common.module = (struct hw_module_t *)module;
2343 adev->device.common.close = adev_close;
2344
2345 adev->device.init_check = adev_init_check;
2346 adev->device.set_voice_volume = adev_set_voice_volume;
2347 adev->device.set_master_volume = adev_set_master_volume;
2348 adev->device.get_master_volume = adev_get_master_volume;
2349 adev->device.set_master_mute = adev_set_master_mute;
2350 adev->device.get_master_mute = adev_get_master_mute;
2351 adev->device.set_mode = adev_set_mode;
2352 adev->device.set_mic_mute = adev_set_mic_mute;
2353 adev->device.get_mic_mute = adev_get_mic_mute;
2354 adev->device.set_parameters = adev_set_parameters;
2355 adev->device.get_parameters = adev_get_parameters;
2356 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2357 adev->device.open_output_stream = adev_open_output_stream;
2358 adev->device.close_output_stream = adev_close_output_stream;
2359 adev->device.open_input_stream = adev_open_input_stream;
2360 adev->device.close_input_stream = adev_close_input_stream;
2361 adev->device.dump = adev_dump;
2362
2363 /* Set the default route before the PCM stream is opened */
2364 pthread_mutex_lock(&adev->lock);
2365 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002366 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002367 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369 adev->voice_call_rx = NULL;
2370 adev->voice_call_tx = NULL;
2371 adev->voice_volume = 1.0f;
2372 adev->tty_mode = TTY_MODE_OFF;
2373 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002375 adev->acdb_settings = TTY_MODE_OFF;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002376 adev->speaker_lr_swap = false;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002377 for (i = 0; i < SND_DEVICE_MAX; i++) {
2378 adev->snd_dev_ref_cnt[i] = 0;
2379 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002380 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002381 pthread_mutex_unlock(&adev->lock);
2382
2383 /* Loads platform specific libraries dynamically */
2384 init_platform_data(adev);
2385
2386 *device = &adev->device.common;
2387
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002388 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002389 return 0;
2390}
2391
2392static struct hw_module_methods_t hal_module_methods = {
2393 .open = adev_open,
2394};
2395
2396struct audio_module HAL_MODULE_INFO_SYM = {
2397 .common = {
2398 .tag = HARDWARE_MODULE_TAG,
2399 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2400 .hal_api_version = HARDWARE_HAL_API_VERSION,
2401 .id = AUDIO_HARDWARE_MODULE_ID,
2402 .name = "QCOM Audio HAL",
2403 .author = "Code Aurora Forum",
2404 .methods = &hal_module_methods,
2405 },
2406};