blob: 55ab055d5f66a851f6339621b11d916fef9afe2b [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},
73 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
74 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
75 [USECASE_AUDIO_RECORD] = {0, 0},
76 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
77 [USECASE_VOICE_CALL] = {12, 12},
78};
79
80/* Array to store sound devices */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080081static const char * const device_table[SND_DEVICE_MAX] = {
82 [SND_DEVICE_NONE] = "none",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080083 /* Playback sound devices */
84 [SND_DEVICE_OUT_HANDSET] = "handset",
85 [SND_DEVICE_OUT_SPEAKER] = "speaker",
86 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
87 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
88 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
89 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080090 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080091 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
92 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080093 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080094 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
95 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
96 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080097
98 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080099 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800100 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
101 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
102 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
103 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
104 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800105 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800106 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800107 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
108 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
109 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
110 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
111 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800112 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
113 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
114 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800115 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800116 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
117 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800118 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
119 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800120};
121
Eric Laurentc8400632013-02-14 19:04:54 -0800122/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800123static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700124 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800125 [SND_DEVICE_OUT_HANDSET] = 7,
126 [SND_DEVICE_OUT_SPEAKER] = 14,
127 [SND_DEVICE_OUT_HEADPHONES] = 10,
128 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
129 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
130 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800131 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800132 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
133 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800134 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800135 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
136 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
137 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800138
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800139 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800140 [SND_DEVICE_IN_SPEAKER_MIC] = 4,
141 [SND_DEVICE_IN_HEADSET_MIC] = 8,
142 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
143 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
144 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800145 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800146 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800147 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
148 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
149 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
150 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
151 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800152 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
153 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
154 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800155 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800156 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
157 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800158 /* TODO: Update with proper acdb ids */
Eric Laurentc8400632013-02-14 19:04:54 -0800159 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 62,
160 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 62,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800161};
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163int edid_get_max_channels(void);
164
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800165static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
166static bool is_tmus = false;
167
168static void check_operator()
169{
170 char value[PROPERTY_VALUE_MAX];
171 int mccmnc;
172 property_get("gsm.sim.operator.numeric",value,"0");
173 mccmnc = atoi(value);
174 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
175 switch(mccmnc) {
176 /* TMUS MCC(310), MNC(490, 260, 026) */
177 case 310490:
178 case 310260:
179 case 310026:
180 is_tmus = true;
181 break;
182 }
183}
184
185static bool is_operator_tmus()
186{
187 pthread_once(&check_op_once_ctl, check_operator);
188 return is_tmus;
189}
190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191static int get_pcm_device_id(struct audio_route *ar,
192 audio_usecase_t usecase,
193 int device_type)
194{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800195 int device_id;
196 if (device_type == PCM_PLAYBACK)
197 device_id = pcm_device_table[usecase][0];
198 else
199 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800200 return device_id;
201}
202
203static int get_acdb_device_id(snd_device_t snd_device)
204{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700205 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800206}
207
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800208static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700209 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800210{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700211 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
212 strcat(mixer_path, " bt-sco");
213 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
214 strcat(mixer_path, " bt-sco");
215 else if (snd_device == SND_DEVICE_OUT_HDMI)
216 strcat(mixer_path, " hdmi");
217 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
218 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800219}
220
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221static int enable_audio_route(struct audio_device *adev,
222 struct audio_usecase *usecase,
223 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700225 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800226 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800227
228 if (usecase == NULL)
229 return -EINVAL;
230
231 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
232
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800233 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700234 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800235 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700236 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800237
238 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800240 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700241 audio_route_apply_path(adev->audio_route, mixer_path);
242 if (update_mixer)
243 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800244
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800245 ALOGV("%s: exit", __func__);
246 return 0;
247}
248
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249static int disable_audio_route(struct audio_device *adev,
250 struct audio_usecase *usecase,
251 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800252{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800254 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800255
256 if (usecase == NULL)
257 return -EINVAL;
258
259 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700260 if (usecase->type == PCM_CAPTURE)
261 snd_device = usecase->in_snd_device;
262 else
263 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800264 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700265 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800266 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700267 audio_route_reset_path(adev->audio_route, mixer_path);
268 if (update_mixer)
269 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800270
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800271 ALOGV("%s: exit", __func__);
272 return 0;
273}
274
275static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700276 snd_device_t snd_device,
277 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800278{
279 int acdb_dev_id, acdb_dev_type;
280
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800281 if (snd_device < SND_DEVICE_MIN ||
282 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800283 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800284 return -EINVAL;
285 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700286
287 adev->snd_dev_ref_cnt[snd_device]++;
288 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
289 ALOGD("%s: snd_device(%d: %s) is already active",
290 __func__, snd_device, device_table[snd_device]);
291 return 0;
292 }
293
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800294 acdb_dev_id = get_acdb_device_id(snd_device);
295 if (acdb_dev_id < 0) {
296 ALOGE("%s: Could not find acdb id for device(%d)",
297 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700298 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800299 return -EINVAL;
300 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800301 if (adev->acdb_send_audio_cal) {
302 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
303 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700304 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
305 snd_device < SND_DEVICE_OUT_END)
306 acdb_dev_type = ACDB_DEV_TYPE_OUT;
307 else
308 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
310 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700311 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800312 __func__, LIB_ACDB_LOADER);
313 }
314
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700315 ALOGD("%s: snd_device(%d: %s)", __func__,
316 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800317 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700318 if (update_mixer)
319 audio_route_update_mixer(adev->audio_route);
320
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800321 return 0;
322}
323
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700324static int disable_snd_device(struct audio_device *adev,
325 snd_device_t snd_device,
326 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800327{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800328 if (snd_device < SND_DEVICE_MIN ||
329 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800330 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800331 return -EINVAL;
332 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700333 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
334 ALOGE("%s: device ref cnt is already 0", __func__);
335 return -EINVAL;
336 }
337 adev->snd_dev_ref_cnt[snd_device]--;
338 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
339 ALOGD("%s: snd_device(%d: %s)", __func__,
340 snd_device, device_table[snd_device]);
341 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
342 if (update_mixer)
343 audio_route_update_mixer(adev->audio_route);
344 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800345 return 0;
346}
347
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700348static void check_usecases_codec_backend(struct audio_device *adev,
349 struct audio_usecase *uc_info,
350 snd_device_t snd_device)
351{
352 struct listnode *node;
353 struct audio_usecase *usecase;
354 bool switch_device[AUDIO_USECASE_MAX];
355 int i, num_uc_to_switch = 0;
356
357 /*
358 * This function is to make sure that all the usecases that are active on
359 * the hardware codec backend are always routed to any one device that is
360 * handled by the hardware codec.
361 * For example, if low-latency and deep-buffer usecases are currently active
362 * on speaker and out_set_parameters(headset) is received on low-latency
363 * output, then we have to make sure deep-buffer is also switched to headset,
364 * because of the limitation that both the devices cannot be enabled
365 * at the same time as they share the same backend.
366 */
367 /* Disable all the usecases on the shared backend other than the
368 specified usecase */
369 for (i = 0; i < AUDIO_USECASE_MAX; i++)
370 switch_device[i] = false;
371
372 list_for_each(node, &adev->usecase_list) {
373 usecase = node_to_item(node, struct audio_usecase, list);
374 if (usecase->type != PCM_CAPTURE &&
375 usecase != uc_info &&
376 usecase->out_snd_device != snd_device &&
377 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
378 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
379 __func__, use_case_table[usecase->id],
380 device_table[usecase->out_snd_device]);
381 disable_audio_route(adev, usecase, false);
382 switch_device[usecase->id] = true;
383 num_uc_to_switch++;
384 }
385 }
386
387 if (num_uc_to_switch) {
388 /* Make sure all the streams are de-routed before disabling the device */
389 audio_route_update_mixer(adev->audio_route);
390
391 list_for_each(node, &adev->usecase_list) {
392 usecase = node_to_item(node, struct audio_usecase, list);
393 if (switch_device[usecase->id]) {
394 disable_snd_device(adev, usecase->out_snd_device, false);
395 enable_snd_device(adev, snd_device, false);
396 }
397 }
398
399 /* Make sure new snd device is enabled before re-routing the streams */
400 audio_route_update_mixer(adev->audio_route);
401
402 /* Re-route all the usecases on the shared backend other than the
403 specified usecase to new snd devices */
404 list_for_each(node, &adev->usecase_list) {
405 usecase = node_to_item(node, struct audio_usecase, list);
406 /* Update the out_snd_device only before enabling the audio route */
407 if (switch_device[usecase->id] ) {
408 usecase->out_snd_device = snd_device;
409 enable_audio_route(adev, usecase, false);
410 }
411 }
412
413 audio_route_update_mixer(adev->audio_route);
414 }
415}
416
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417static int set_hdmi_channels(struct mixer *mixer,
418 int channel_count)
419{
420 struct mixer_ctl *ctl;
421 const char *channel_cnt_str = NULL;
422 const char *mixer_ctl_name = "HDMI_RX Channels";
423 switch (channel_count) {
424 case 8:
425 channel_cnt_str = "Eight"; break;
426 case 7:
427 channel_cnt_str = "Seven"; break;
428 case 6:
429 channel_cnt_str = "Six"; break;
430 case 5:
431 channel_cnt_str = "Five"; break;
432 case 4:
433 channel_cnt_str = "Four"; break;
434 case 3:
435 channel_cnt_str = "Three"; break;
436 default:
437 channel_cnt_str = "Two"; break;
438 }
439 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
440 if (!ctl) {
441 ALOGE("%s: Could not get ctl for mixer cmd - %s",
442 __func__, mixer_ctl_name);
443 return -EINVAL;
444 }
445 ALOGV("HDMI channel count: %s", channel_cnt_str);
446 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
447 return 0;
448}
449
450/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700451static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800452{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700453 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800454 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800455
456 switch (channels) {
457 /*
458 * Do not handle stereo output in Multi-channel cases
459 * Stereo case is handled in normal playback path
460 */
461 case 6:
462 ALOGV("%s: HDMI supports 5.1", __func__);
463 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
464 break;
465 case 8:
466 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
467 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
468 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
469 break;
470 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700471 ALOGE("HDMI does not support multi channel playback");
472 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800473 break;
474 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700475 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800476}
477
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700478static snd_device_t get_output_snd_device(struct audio_device *adev,
479 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700481 audio_mode_t mode = adev->mode;
482 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800483
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800484 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800485 if (devices == AUDIO_DEVICE_NONE ||
486 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800487 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800488 goto exit;
489 }
490
491 if (mode == AUDIO_MODE_IN_CALL) {
492 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
493 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800494 if (adev->tty_mode == TTY_MODE_FULL)
495 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
496 else if (adev->tty_mode == TTY_MODE_VCO)
497 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
498 else if (adev->tty_mode == TTY_MODE_HCO)
499 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
500 else
501 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800502 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
503 snd_device = SND_DEVICE_OUT_BT_SCO;
504 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
505 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
506 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800507 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800508 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
509 else
510 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800512 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800513 goto exit;
514 }
515 }
516
517 if (popcount(devices) == 2) {
518 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
519 AUDIO_DEVICE_OUT_SPEAKER)) {
520 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
521 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
522 AUDIO_DEVICE_OUT_SPEAKER)) {
523 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
524 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
525 AUDIO_DEVICE_OUT_SPEAKER)) {
526 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
527 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800528 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800529 goto exit;
530 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800531 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800532 goto exit;
533 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800535
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800537 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800538 goto exit;
539 }
540
541 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
542 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
543 snd_device = SND_DEVICE_OUT_HEADPHONES;
544 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
545 snd_device = SND_DEVICE_OUT_SPEAKER;
546 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
547 snd_device = SND_DEVICE_OUT_BT_SCO;
548 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
549 snd_device = SND_DEVICE_OUT_HDMI ;
550 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
551 snd_device = SND_DEVICE_OUT_HANDSET;
552 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800553 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800554 }
555exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800556 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800557 return snd_device;
558}
559
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700560static snd_device_t get_input_snd_device(struct audio_device *adev,
561 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562{
Eric Laurentc8400632013-02-14 19:04:54 -0800563 audio_source_t source = (adev->active_input == NULL) ?
564 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
565
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800567 audio_devices_t in_device = ((adev->active_input == NULL) ?
568 AUDIO_DEVICE_NONE : adev->active_input->device)
569 & ~AUDIO_DEVICE_BIT_IN;
570 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
571 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800572 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800573
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800574 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800575 __func__, out_device, in_device);
576 if (mode == AUDIO_MODE_IN_CALL) {
577 if (out_device == AUDIO_DEVICE_NONE) {
578 ALOGE("%s: No output device set for voice call", __func__);
579 goto exit;
580 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800581 if (adev->tty_mode != TTY_MODE_OFF) {
582 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
583 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
584 switch (adev->tty_mode) {
585 case TTY_MODE_FULL:
586 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
587 break;
588 case TTY_MODE_VCO:
589 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
590 break;
591 case TTY_MODE_HCO:
592 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
593 break;
594 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800595 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800596 }
597 goto exit;
598 }
599 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800600 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
601 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800602 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
603 snd_device = SND_DEVICE_IN_HANDSET_MIC;
604 } else {
605 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800606 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800607 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
608 else
609 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
610 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
611 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
612 else
613 snd_device = SND_DEVICE_IN_HANDSET_MIC;
614 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800615 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
616 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
617 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
618 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
619 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800620 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800621 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
622 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800623 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800624 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
625 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
626 } else {
627 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
628 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800629 }
630 } else if (source == AUDIO_SOURCE_CAMCORDER) {
631 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
632 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
633 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
634 }
635 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
636 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800637 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
638 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
639 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
640 else if (adev->fluence_in_voice_rec)
641 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
642 else
643 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
644 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
645 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
646 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
647 else if (adev->fluence_in_voice_rec)
648 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
649 else
650 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
651 } else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800652 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800653 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800654 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
655 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
656 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 } else if (source == AUDIO_SOURCE_DEFAULT) {
658 goto exit;
659 }
660
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800661 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 goto exit;
663 }
664
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800665 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800666 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
667 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800668 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800669 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800670 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800671 if (adev->mic_type_analog)
672 snd_device = SND_DEVICE_IN_HANDSET_MIC;
673 else
674 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800675 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
676 snd_device = SND_DEVICE_IN_HEADSET_MIC;
677 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
678 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
679 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
680 snd_device = SND_DEVICE_IN_HDMI_MIC;
681 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800682 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800683 ALOGW("%s: Using default handset-mic", __func__);
684 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 }
686 } else {
687 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800688 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
690 snd_device = SND_DEVICE_IN_HEADSET_MIC;
691 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
692 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
693 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800694 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800695 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800696 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
698 snd_device = SND_DEVICE_IN_HDMI_MIC;
699 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800700 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800701 ALOGW("%s: Using default handset-mic", __func__);
702 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703 }
704 }
705exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800706 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 return snd_device;
708}
709
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700710static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
711 audio_usecase_t uc_id)
712{
713 struct audio_usecase *usecase;
714 struct listnode *node;
715
716 list_for_each(node, &adev->usecase_list) {
717 usecase = node_to_item(node, struct audio_usecase, list);
718 if (usecase->id == uc_id)
719 return usecase;
720 }
721 return NULL;
722}
723
724static int select_devices(struct audio_device *adev,
725 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800726{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800727 snd_device_t out_snd_device = SND_DEVICE_NONE;
728 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729 struct audio_usecase *usecase = NULL;
730 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800731 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700733 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700735 usecase = get_usecase_from_list(adev, uc_id);
736 if (usecase == NULL) {
737 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
738 return -EINVAL;
739 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 if (usecase->type == VOICE_CALL) {
742 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
743 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
744 usecase->devices = usecase->stream.out->devices;
745 } else {
746 /*
747 * If the voice call is active, use the sound devices of voice call usecase
748 * so that it would not result any device switch. All the usecases will
749 * be switched to new device when select_devices() is called for voice call
750 * usecase. This is to avoid switching devices for voice call when
751 * check_usecases_codec_backend() is called below.
752 */
753 if (adev->in_call) {
754 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
755 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
756 in_snd_device = vc_usecase->in_snd_device;
757 out_snd_device = vc_usecase->out_snd_device;
758 }
759 }
760 if (usecase->type == PCM_PLAYBACK) {
761 usecase->devices = usecase->stream.out->devices;
762 in_snd_device = SND_DEVICE_NONE;
763 if (out_snd_device == SND_DEVICE_NONE)
764 out_snd_device = get_output_snd_device(adev,
765 usecase->stream.out->devices);
766 } else if (usecase->type == PCM_CAPTURE) {
767 usecase->devices = usecase->stream.in->device;
768 out_snd_device = SND_DEVICE_NONE;
769 if (in_snd_device == SND_DEVICE_NONE)
770 in_snd_device = get_input_snd_device(adev, SND_DEVICE_NONE);
771 }
772 }
773
774 if (out_snd_device == usecase->out_snd_device &&
775 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776 return 0;
777 }
778
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800779 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
780 out_snd_device, device_table[out_snd_device],
781 in_snd_device, device_table[in_snd_device]);
782
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800783 /*
784 * Limitation: While in call, to do a device switch we need to disable
785 * and enable both RX and TX devices though one of them is same as current
786 * device.
787 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700788 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
789 usecase->in_snd_device != SND_DEVICE_NONE &&
790 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800791 /* This must be called before disabling the mixer controls on APQ side */
792 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800793 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800794 } else {
795 status = adev->csd_disable_device();
796 if (status < 0) {
797 ALOGE("%s: csd_client_disable_device, failed, error %d",
798 __func__, status);
799 }
800 }
801 }
802
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700803 /* Disable current sound devices */
804 if (usecase->out_snd_device != SND_DEVICE_NONE) {
805 disable_audio_route(adev, usecase, true);
806 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800807 }
808
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700809 if (usecase->in_snd_device != SND_DEVICE_NONE) {
810 disable_audio_route(adev, usecase, true);
811 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812 }
813
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700814 /* Enable new sound devices */
815 if (out_snd_device != SND_DEVICE_NONE) {
816 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
817 check_usecases_codec_backend(adev, usecase, out_snd_device);
818 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800819 }
820
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700821 if (in_snd_device != SND_DEVICE_NONE)
822 enable_snd_device(adev, in_snd_device, false);
823
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800824 audio_route_update_mixer(adev->audio_route);
825
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700826 usecase->in_snd_device = in_snd_device;
827 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800828
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829 enable_audio_route(adev, usecase, true);
830
831 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800832 if (adev->csd_enable_device == NULL) {
833 ALOGE("%s: dlsym error for csd_client_enable_device",
834 __func__);
835 } else {
836 acdb_rx_id = get_acdb_device_id(out_snd_device);
837 acdb_tx_id = get_acdb_device_id(in_snd_device);
838
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700839 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
840 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
841 adev->acdb_settings);
842 if (status < 0) {
843 ALOGE("%s: csd_client_enable_device, failed, error %d",
844 __func__, status);
845 }
846 } else {
847 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
848 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800849 }
850 }
851 }
852
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800853 return status;
854}
855
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800856static int stop_input_stream(struct stream_in *in)
857{
858 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800859 struct audio_usecase *uc_info;
860 struct audio_device *adev = in->dev;
861
Eric Laurentc8400632013-02-14 19:04:54 -0800862 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700864 ALOGD("%s: enter: usecase(%d: %s)", __func__,
865 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800866 uc_info = get_usecase_from_list(adev, in->usecase);
867 if (uc_info == NULL) {
868 ALOGE("%s: Could not find the usecase (%d) in the list",
869 __func__, in->usecase);
870 return -EINVAL;
871 }
872
Eric Laurent150dbfe2013-02-27 14:31:02 -0800873 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700874 disable_audio_route(adev, uc_info, true);
875
876 /* 2. Disable the tx device */
877 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800878
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800879 list_remove(&uc_info->list);
880 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800881
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800882 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 return ret;
884}
885
886int start_input_stream(struct stream_in *in)
887{
888 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800889 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800890 struct audio_usecase *uc_info;
891 struct audio_device *adev = in->dev;
892
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800893 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800894 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
895 in->usecase,
896 PCM_CAPTURE);
897 if (in->pcm_device_id < 0) {
898 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
899 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800900 ret = -EINVAL;
901 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700903
904 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800905 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
906 uc_info->id = in->usecase;
907 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800908 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700909 uc_info->devices = in->device;
910 uc_info->in_snd_device = SND_DEVICE_NONE;
911 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800912
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800913 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700914 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800915
Eric Laurentc8400632013-02-14 19:04:54 -0800916 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
917 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800918 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
919 PCM_IN, &in->config);
920 if (in->pcm && !pcm_is_ready(in->pcm)) {
921 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
922 pcm_close(in->pcm);
923 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800924 ret = -EIO;
925 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800926 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800927 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800928 return ret;
929
930error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800931 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800932
933error_config:
934 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700935 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800936
937 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800938}
939
940static int stop_output_stream(struct stream_out *out)
941{
942 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800943 struct audio_usecase *uc_info;
944 struct audio_device *adev = out->dev;
945
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700946 ALOGD("%s: enter: usecase(%d: %s)", __func__,
947 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948 uc_info = get_usecase_from_list(adev, out->usecase);
949 if (uc_info == NULL) {
950 ALOGE("%s: Could not find the usecase (%d) in the list",
951 __func__, out->usecase);
952 return -EINVAL;
953 }
954
Eric Laurent150dbfe2013-02-27 14:31:02 -0800955 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700956 disable_audio_route(adev, uc_info, true);
957
958 /* 2. Disable the rx device */
959 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800961 list_remove(&uc_info->list);
962 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800963
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700964 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800965 return ret;
966}
967
968int start_output_stream(struct stream_out *out)
969{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800970 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971 struct audio_usecase *uc_info;
972 struct audio_device *adev = out->dev;
973
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700974 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
975 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800976 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
977 out->usecase,
978 PCM_PLAYBACK);
979 if (out->pcm_device_id < 0) {
980 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
981 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800982 ret = -EINVAL;
983 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800984 }
985
986 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
987 uc_info->id = out->usecase;
988 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800989 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700990 uc_info->devices = out->devices;
991 uc_info->in_snd_device = SND_DEVICE_NONE;
992 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800993
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800994 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800995
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700996 select_devices(adev, out->usecase);
997
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800998 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
999 __func__, 0, out->pcm_device_id);
1000 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1001 PCM_OUT, &out->config);
1002 if (out->pcm && !pcm_is_ready(out->pcm)) {
1003 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1004 pcm_close(out->pcm);
1005 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001006 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001007 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001008 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001009 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001011error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001013error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001014 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015}
1016
1017static int stop_voice_call(struct audio_device *adev)
1018{
1019 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 struct audio_usecase *uc_info;
1021
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001022 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001023 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 if (adev->csd_client) {
1025 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001026 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027 } else {
1028 ret = adev->csd_stop_voice();
1029 if (ret < 0) {
1030 ALOGE("%s: csd_client error %d\n", __func__, ret);
1031 }
1032 }
1033 }
1034
1035 /* 1. Close the PCM devices */
1036 if (adev->voice_call_rx) {
1037 pcm_close(adev->voice_call_rx);
1038 adev->voice_call_rx = NULL;
1039 }
1040 if (adev->voice_call_tx) {
1041 pcm_close(adev->voice_call_tx);
1042 adev->voice_call_tx = NULL;
1043 }
1044
1045 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1046 if (uc_info == NULL) {
1047 ALOGE("%s: Could not find the usecase (%d) in the list",
1048 __func__, USECASE_VOICE_CALL);
1049 return -EINVAL;
1050 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051
1052 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001053 disable_audio_route(adev, uc_info, true);
1054
1055 /* 3. Disable the rx and tx devices */
1056 disable_snd_device(adev, uc_info->out_snd_device, false);
1057 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001059 list_remove(&uc_info->list);
1060 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001062 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 return ret;
1064}
1065
1066static int start_voice_call(struct audio_device *adev)
1067{
1068 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 struct audio_usecase *uc_info;
1070 int pcm_dev_rx_id, pcm_dev_tx_id;
1071
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001072 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073
1074 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1075 uc_info->id = USECASE_VOICE_CALL;
1076 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001077 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001078 uc_info->devices = adev->primary_output->devices;
1079 uc_info->in_snd_device = SND_DEVICE_NONE;
1080 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001082 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001084 select_devices(adev, USECASE_VOICE_CALL);
1085
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1087 PCM_PLAYBACK);
1088 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1089 PCM_CAPTURE);
1090
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1092 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1093 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001094 ret = -EIO;
1095 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001096 }
1097
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001098 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 __func__, SOUND_CARD, pcm_dev_rx_id);
1100 adev->voice_call_rx = pcm_open(SOUND_CARD,
1101 pcm_dev_rx_id,
1102 PCM_OUT, &pcm_config_voice_call);
1103 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1104 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001105 ret = -EIO;
1106 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107 }
1108
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001109 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110 __func__, SOUND_CARD, pcm_dev_tx_id);
1111 adev->voice_call_tx = pcm_open(SOUND_CARD,
1112 pcm_dev_tx_id,
1113 PCM_IN, &pcm_config_voice_call);
1114 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1115 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001116 ret = -EIO;
1117 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118 }
1119 pcm_start(adev->voice_call_rx);
1120 pcm_start(adev->voice_call_tx);
1121
1122 if (adev->csd_client) {
1123 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001124 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001125 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001126 } else {
1127 ret = adev->csd_start_voice();
1128 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001129 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1130 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001131 }
1132 }
1133 }
1134
1135 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001136 return 0;
1137
1138error_start_voice:
1139 stop_voice_call(adev);
1140
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001141 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142 return ret;
1143}
1144
1145static int check_input_parameters(uint32_t sample_rate,
1146 audio_format_t format,
1147 int channel_count)
1148{
1149 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1150
1151 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1152
1153 switch (sample_rate) {
1154 case 8000:
1155 case 11025:
1156 case 12000:
1157 case 16000:
1158 case 22050:
1159 case 24000:
1160 case 32000:
1161 case 44100:
1162 case 48000:
1163 break;
1164 default:
1165 return -EINVAL;
1166 }
1167
1168 return 0;
1169}
1170
1171static size_t get_input_buffer_size(uint32_t sample_rate,
1172 audio_format_t format,
1173 int channel_count)
1174{
1175 size_t size = 0;
1176
1177 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1178
1179 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1180 size = (sample_rate * 20) / 1000;
1181 } else if (sample_rate == 11025 || sample_rate == 12000) {
1182 size = 256;
1183 } else if (sample_rate == 22050 || sample_rate == 24000) {
1184 size = 512;
1185 } else if (sample_rate == 44100 || sample_rate == 48000) {
1186 size = 1024;
1187 }
1188
1189 return size * sizeof(short) * channel_count;
1190}
1191
1192static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1193{
1194 struct stream_out *out = (struct stream_out *)stream;
1195
1196 return out->config.rate;
1197}
1198
1199static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1200{
1201 return -ENOSYS;
1202}
1203
1204static size_t out_get_buffer_size(const struct audio_stream *stream)
1205{
1206 struct stream_out *out = (struct stream_out *)stream;
1207
1208 return out->config.period_size * audio_stream_frame_size(stream);
1209}
1210
1211static uint32_t out_get_channels(const struct audio_stream *stream)
1212{
1213 struct stream_out *out = (struct stream_out *)stream;
1214
1215 return out->channel_mask;
1216}
1217
1218static audio_format_t out_get_format(const struct audio_stream *stream)
1219{
1220 return AUDIO_FORMAT_PCM_16_BIT;
1221}
1222
1223static int out_set_format(struct audio_stream *stream, audio_format_t format)
1224{
1225 return -ENOSYS;
1226}
1227
1228static int out_standby(struct audio_stream *stream)
1229{
1230 struct stream_out *out = (struct stream_out *)stream;
1231 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001232 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1233 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001234 pthread_mutex_lock(&out->lock);
1235
1236 if (!out->standby) {
1237 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001238 if (out->pcm) {
1239 pcm_close(out->pcm);
1240 out->pcm = NULL;
1241 }
1242 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001243 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001244 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001245 }
1246 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001247 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001248 return 0;
1249}
1250
1251static int out_dump(const struct audio_stream *stream, int fd)
1252{
1253 return 0;
1254}
1255
1256static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1257{
1258 struct stream_out *out = (struct stream_out *)stream;
1259 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001260 struct audio_usecase *usecase;
1261 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 struct str_parms *parms;
1263 char value[32];
1264 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001265 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001267 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1268 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 parms = str_parms_create_str(kvpairs);
1270 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1271 if (ret >= 0) {
1272 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001274 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001276 /*
1277 * When HDMI cable is unplugged the music playback is paused and
1278 * the policy manager sends routing=0. But the audioflinger
1279 * continues to write data until standby time (3sec).
1280 * As the HDMI core is turned off, the write gets blocked.
1281 * Avoid this by routing audio to speaker until standby.
1282 */
1283 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1284 val == AUDIO_DEVICE_NONE) {
1285 val = AUDIO_DEVICE_OUT_SPEAKER;
1286 }
1287
1288 /*
1289 * select_devices() call below switches all the usecases on the same
1290 * backend to the new device. Refer to check_usecases_codec_backend() in
1291 * the select_devices(). But how do we undo this?
1292 *
1293 * For example, music playback is active on headset (deep-buffer usecase)
1294 * and if we go to ringtones and select a ringtone, low-latency usecase
1295 * will be started on headset+speaker. As we can't enable headset+speaker
1296 * and headset devices at the same time, select_devices() switches the music
1297 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1298 * So when the ringtone playback is completed, how do we undo the same?
1299 *
1300 * We are relying on the out_set_parameters() call on deep-buffer output,
1301 * once the ringtone playback is ended.
1302 * NOTE: We should not check if the current devices are same as new devices.
1303 * Because select_devices() must be called to switch back the music
1304 * playback to headset.
1305 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001306 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001307 out->devices = val;
1308
1309 if (!out->standby)
1310 select_devices(adev, out->usecase);
1311
1312 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1313 (out == adev->primary_output)) {
1314 start_voice_call(adev);
1315 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1316 (out == adev->primary_output)) {
1317 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001318 }
1319 }
1320
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001321 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1322 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001327 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 }
1329 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001330 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 return ret;
1332}
1333
1334static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1335{
1336 struct stream_out *out = (struct stream_out *)stream;
1337 struct str_parms *query = str_parms_create_str(keys);
1338 char *str;
1339 char value[256];
1340 struct str_parms *reply = str_parms_create();
1341 size_t i, j;
1342 int ret;
1343 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001344 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1346 if (ret >= 0) {
1347 value[0] = '\0';
1348 i = 0;
1349 while (out->supported_channel_masks[i] != 0) {
1350 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1351 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1352 if (!first) {
1353 strcat(value, "|");
1354 }
1355 strcat(value, out_channels_name_to_enum_table[j].name);
1356 first = false;
1357 break;
1358 }
1359 }
1360 i++;
1361 }
1362 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1363 str = str_parms_to_str(reply);
1364 } else {
1365 str = strdup(keys);
1366 }
1367 str_parms_destroy(query);
1368 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001369 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370 return str;
1371}
1372
1373static uint32_t out_get_latency(const struct audio_stream_out *stream)
1374{
1375 struct stream_out *out = (struct stream_out *)stream;
1376
1377 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1378}
1379
1380static int out_set_volume(struct audio_stream_out *stream, float left,
1381 float right)
1382{
Eric Laurenta9024de2013-04-04 09:19:12 -07001383 struct stream_out *out = (struct stream_out *)stream;
1384 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1385 /* only take left channel into account: the API is for stereo anyway */
1386 out->muted = (left == 0.0f);
1387 return 0;
1388 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 return -ENOSYS;
1390}
1391
1392static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1393 size_t bytes)
1394{
1395 struct stream_out *out = (struct stream_out *)stream;
1396 struct audio_device *adev = out->dev;
1397 int i, ret = -1;
1398
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001399 pthread_mutex_lock(&out->lock);
1400 if (out->standby) {
Eric Laurent150dbfe2013-02-27 14:31:02 -08001401 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001403 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 goto exit;
1406 }
1407 out->standby = false;
1408 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409
1410 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001411 if (out->muted)
1412 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1414 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1415 }
1416
1417exit:
1418 pthread_mutex_unlock(&out->lock);
1419
1420 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001421 if (out->pcm)
1422 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423 out_standby(&out->stream.common);
1424 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1425 out_get_sample_rate(&out->stream.common));
1426 }
1427 return bytes;
1428}
1429
1430static int out_get_render_position(const struct audio_stream_out *stream,
1431 uint32_t *dsp_frames)
1432{
1433 return -EINVAL;
1434}
1435
1436static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1437{
1438 return 0;
1439}
1440
1441static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1442{
1443 return 0;
1444}
1445
1446static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1447 int64_t *timestamp)
1448{
1449 return -EINVAL;
1450}
1451
1452/** audio_stream_in implementation **/
1453static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1454{
1455 struct stream_in *in = (struct stream_in *)stream;
1456
1457 return in->config.rate;
1458}
1459
1460static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1461{
1462 return -ENOSYS;
1463}
1464
1465static size_t in_get_buffer_size(const struct audio_stream *stream)
1466{
1467 struct stream_in *in = (struct stream_in *)stream;
1468
1469 return in->config.period_size * audio_stream_frame_size(stream);
1470}
1471
1472static uint32_t in_get_channels(const struct audio_stream *stream)
1473{
1474 struct stream_in *in = (struct stream_in *)stream;
1475
1476 return in->channel_mask;
1477}
1478
1479static audio_format_t in_get_format(const struct audio_stream *stream)
1480{
1481 return AUDIO_FORMAT_PCM_16_BIT;
1482}
1483
1484static int in_set_format(struct audio_stream *stream, audio_format_t format)
1485{
1486 return -ENOSYS;
1487}
1488
1489static int in_standby(struct audio_stream *stream)
1490{
1491 struct stream_in *in = (struct stream_in *)stream;
1492 struct audio_device *adev = in->dev;
1493 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001494 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495 pthread_mutex_lock(&in->lock);
1496 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001498 if (in->pcm) {
1499 pcm_close(in->pcm);
1500 in->pcm = NULL;
1501 }
1502 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001503 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001504 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001505 }
1506 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001507 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508 return status;
1509}
1510
1511static int in_dump(const struct audio_stream *stream, int fd)
1512{
1513 return 0;
1514}
1515
1516static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1517{
1518 struct stream_in *in = (struct stream_in *)stream;
1519 struct audio_device *adev = in->dev;
1520 struct str_parms *parms;
1521 char *str;
1522 char value[32];
1523 int ret, val = 0;
1524
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001525 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526 parms = str_parms_create_str(kvpairs);
1527
1528 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1529
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001530 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001531 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 if (ret >= 0) {
1533 val = atoi(value);
1534 /* no audio source uses val == 0 */
1535 if ((in->source != val) && (val != 0)) {
1536 in->source = val;
1537 }
1538 }
1539
1540 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1541 if (ret >= 0) {
1542 val = atoi(value);
1543 if ((in->device != val) && (val != 0)) {
1544 in->device = val;
1545 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001546 if (!in->standby)
1547 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 }
1549 }
1550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001552 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553
1554 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001555 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001556 return ret;
1557}
1558
1559static char* in_get_parameters(const struct audio_stream *stream,
1560 const char *keys)
1561{
1562 return strdup("");
1563}
1564
1565static int in_set_gain(struct audio_stream_in *stream, float gain)
1566{
1567 return 0;
1568}
1569
1570static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1571 size_t bytes)
1572{
1573 struct stream_in *in = (struct stream_in *)stream;
1574 struct audio_device *adev = in->dev;
1575 int i, ret = -1;
1576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001577 pthread_mutex_lock(&in->lock);
1578 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001579 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001580 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001581 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583 goto exit;
1584 }
1585 in->standby = 0;
1586 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001587
1588 if (in->pcm) {
1589 ret = pcm_read(in->pcm, buffer, bytes);
1590 }
1591
1592 /*
1593 * Instead of writing zeroes here, we could trust the hardware
1594 * to always provide zeroes when muted.
1595 */
1596 if (ret == 0 && adev->mic_mute)
1597 memset(buffer, 0, bytes);
1598
1599exit:
1600 pthread_mutex_unlock(&in->lock);
1601
1602 if (ret != 0) {
1603 in_standby(&in->stream.common);
1604 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1605 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1606 in_get_sample_rate(&in->stream.common));
1607 }
1608 return bytes;
1609}
1610
1611static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1612{
1613 return 0;
1614}
1615
1616static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1617{
1618 return 0;
1619}
1620
1621static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1622{
1623 return 0;
1624}
1625
1626static int adev_open_output_stream(struct audio_hw_device *dev,
1627 audio_io_handle_t handle,
1628 audio_devices_t devices,
1629 audio_output_flags_t flags,
1630 struct audio_config *config,
1631 struct audio_stream_out **stream_out)
1632{
1633 struct audio_device *adev = (struct audio_device *)dev;
1634 struct stream_out *out;
1635 int i, ret;
1636
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001637 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 __func__, config->sample_rate, config->channel_mask, devices, flags);
1639 *stream_out = NULL;
1640 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1641
1642 if (devices == AUDIO_DEVICE_NONE)
1643 devices = AUDIO_DEVICE_OUT_SPEAKER;
1644
1645 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1646 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1647 out->flags = flags;
1648 out->devices = devices;
1649
1650 /* Init use case and pcm_config */
1651 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1652 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001653 pthread_mutex_lock(&adev->lock);
1654 ret = read_hdmi_channel_masks(out);
1655 pthread_mutex_unlock(&adev->lock);
1656 if (ret != 0) {
1657 /* If HDMI does not support multi channel playback, set the default */
1658 out->config.channels = popcount(out->channel_mask);
1659 set_hdmi_channels(adev->mixer, out->config.channels);
1660 goto error_open;
1661 }
1662
1663 if (config->sample_rate == 0)
1664 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1665 if (config->channel_mask == 0)
1666 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1667
1668 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001669 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1670 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671 out->config.rate = config->sample_rate;
1672 out->config.channels = popcount(out->channel_mask);
1673 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1674 set_hdmi_channels(adev->mixer, out->config.channels);
1675 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1676 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1677 out->config = pcm_config_deep_buffer;
1678 } else {
1679 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1680 out->config = pcm_config_low_latency;
1681 }
1682
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001683 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1684 if(adev->primary_output == NULL)
1685 adev->primary_output = out;
1686 else {
1687 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001688 ret = -EEXIST;
1689 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001690 }
1691 }
1692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001693 /* Check if this usecase is already existing */
1694 pthread_mutex_lock(&adev->lock);
1695 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1696 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001698 ret = -EEXIST;
1699 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700 }
1701 pthread_mutex_unlock(&adev->lock);
1702
1703 out->stream.common.get_sample_rate = out_get_sample_rate;
1704 out->stream.common.set_sample_rate = out_set_sample_rate;
1705 out->stream.common.get_buffer_size = out_get_buffer_size;
1706 out->stream.common.get_channels = out_get_channels;
1707 out->stream.common.get_format = out_get_format;
1708 out->stream.common.set_format = out_set_format;
1709 out->stream.common.standby = out_standby;
1710 out->stream.common.dump = out_dump;
1711 out->stream.common.set_parameters = out_set_parameters;
1712 out->stream.common.get_parameters = out_get_parameters;
1713 out->stream.common.add_audio_effect = out_add_audio_effect;
1714 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1715 out->stream.get_latency = out_get_latency;
1716 out->stream.set_volume = out_set_volume;
1717 out->stream.write = out_write;
1718 out->stream.get_render_position = out_get_render_position;
1719 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1720
1721 out->dev = adev;
1722 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001723 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724
1725 config->format = out->stream.common.get_format(&out->stream.common);
1726 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1727 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1728
1729 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001730 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001731 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001732
1733error_open:
1734 free(out);
1735 *stream_out = NULL;
1736 ALOGD("%s: exit: ret %d", __func__, ret);
1737 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738}
1739
1740static void adev_close_output_stream(struct audio_hw_device *dev,
1741 struct audio_stream_out *stream)
1742{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001743 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001744 out_standby(&stream->common);
1745 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001746 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747}
1748
1749static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1750{
1751 struct audio_device *adev = (struct audio_device *)dev;
1752 struct str_parms *parms;
1753 char *str;
1754 char value[32];
1755 int ret;
1756
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001757 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758
1759 parms = str_parms_create_str(kvpairs);
1760 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1761 if (ret >= 0) {
1762 int tty_mode;
1763
1764 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1765 tty_mode = TTY_MODE_OFF;
1766 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1767 tty_mode = TTY_MODE_VCO;
1768 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1769 tty_mode = TTY_MODE_HCO;
1770 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1771 tty_mode = TTY_MODE_FULL;
1772 else
1773 return -EINVAL;
1774
1775 pthread_mutex_lock(&adev->lock);
1776 if (tty_mode != adev->tty_mode) {
1777 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001778 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1779 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001780 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781 }
1782 pthread_mutex_unlock(&adev->lock);
1783 }
1784
1785 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1786 if (ret >= 0) {
1787 /* When set to false, HAL should disable EC and NS
1788 * But it is currently not supported.
1789 */
1790 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1791 adev->bluetooth_nrec = true;
1792 else
1793 adev->bluetooth_nrec = false;
1794 }
1795
1796 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1797 if (ret >= 0) {
1798 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1799 adev->screen_off = false;
1800 else
1801 adev->screen_off = true;
1802 }
1803
1804 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001805 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001806 return ret;
1807}
1808
1809static char* adev_get_parameters(const struct audio_hw_device *dev,
1810 const char *keys)
1811{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001812 return strdup("");
1813}
1814
1815static int adev_init_check(const struct audio_hw_device *dev)
1816{
1817 return 0;
1818}
1819
1820static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1821{
1822 struct audio_device *adev = (struct audio_device *)dev;
1823 int vol, err = 0;
1824
1825 pthread_mutex_lock(&adev->lock);
1826 adev->voice_volume = volume;
1827 if (adev->mode == AUDIO_MODE_IN_CALL) {
1828 if (volume < 0.0) {
1829 volume = 0.0;
1830 } else if (volume > 1.0) {
1831 volume = 1.0;
1832 }
1833
1834 vol = lrint(volume * 100.0);
1835
1836 // Voice volume levels from android are mapped to driver volume levels as follows.
1837 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1838 // So adjust the volume to get the correct volume index in driver
1839 vol = 100 - vol;
1840
1841 if (adev->csd_client) {
1842 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001843 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844 } else {
1845 err = adev->csd_volume(vol);
1846 if (err < 0) {
1847 ALOGE("%s: csd_client error %d", __func__, err);
1848 }
1849 }
1850 } else {
1851 ALOGE("%s: No CSD Client present", __func__);
1852 }
1853 }
1854 pthread_mutex_unlock(&adev->lock);
1855 return err;
1856}
1857
1858static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1859{
1860 return -ENOSYS;
1861}
1862
1863static int adev_get_master_volume(struct audio_hw_device *dev,
1864 float *volume)
1865{
1866 return -ENOSYS;
1867}
1868
1869static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1870{
1871 return -ENOSYS;
1872}
1873
1874static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1875{
1876 return -ENOSYS;
1877}
1878
1879static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1880{
1881 struct audio_device *adev = (struct audio_device *)dev;
1882
1883 pthread_mutex_lock(&adev->lock);
1884 if (adev->mode != mode) {
1885 adev->mode = mode;
1886 }
1887 pthread_mutex_unlock(&adev->lock);
1888 return 0;
1889}
1890
1891static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1892{
1893 struct audio_device *adev = (struct audio_device *)dev;
1894 int err = 0;
1895
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001896 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897 adev->mic_mute = state;
1898 if (adev->mode == AUDIO_MODE_IN_CALL) {
1899 if (adev->csd_client) {
1900 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001901 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001902 } else {
1903 err = adev->csd_mic_mute(state);
1904 if (err < 0) {
1905 ALOGE("%s: csd_client error %d", __func__, err);
1906 }
1907 }
1908 } else {
1909 ALOGE("%s: No CSD Client present", __func__);
1910 }
1911 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001912 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 return err;
1914}
1915
1916static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1917{
1918 struct audio_device *adev = (struct audio_device *)dev;
1919
1920 *state = adev->mic_mute;
1921
1922 return 0;
1923}
1924
1925static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1926 const struct audio_config *config)
1927{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001928 int channel_count = popcount(config->channel_mask);
1929
1930 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1931}
1932
1933static int adev_open_input_stream(struct audio_hw_device *dev,
1934 audio_io_handle_t handle,
1935 audio_devices_t devices,
1936 struct audio_config *config,
1937 struct audio_stream_in **stream_in)
1938{
1939 struct audio_device *adev = (struct audio_device *)dev;
1940 struct stream_in *in;
1941 int ret, buffer_size, frame_size;
1942 int channel_count = popcount(config->channel_mask);
1943
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001944 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001945 *stream_in = NULL;
1946 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1947 return -EINVAL;
1948
1949 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1950
1951 in->stream.common.get_sample_rate = in_get_sample_rate;
1952 in->stream.common.set_sample_rate = in_set_sample_rate;
1953 in->stream.common.get_buffer_size = in_get_buffer_size;
1954 in->stream.common.get_channels = in_get_channels;
1955 in->stream.common.get_format = in_get_format;
1956 in->stream.common.set_format = in_set_format;
1957 in->stream.common.standby = in_standby;
1958 in->stream.common.dump = in_dump;
1959 in->stream.common.set_parameters = in_set_parameters;
1960 in->stream.common.get_parameters = in_get_parameters;
1961 in->stream.common.add_audio_effect = in_add_audio_effect;
1962 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1963 in->stream.set_gain = in_set_gain;
1964 in->stream.read = in_read;
1965 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1966
1967 in->device = devices;
1968 in->source = AUDIO_SOURCE_DEFAULT;
1969 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001970 in->standby = 1;
1971 in->channel_mask = config->channel_mask;
1972
1973 /* Update config params with the requested sample rate and channels */
1974 in->usecase = USECASE_AUDIO_RECORD;
1975 in->config = pcm_config_audio_capture;
1976 in->config.channels = channel_count;
1977 in->config.rate = config->sample_rate;
1978
1979 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1980 buffer_size = get_input_buffer_size(config->sample_rate,
1981 config->format,
1982 channel_count);
1983 in->config.period_size = buffer_size / frame_size;
1984
1985 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001986 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987 return 0;
1988
1989err_open:
1990 free(in);
1991 *stream_in = NULL;
1992 return ret;
1993}
1994
1995static void adev_close_input_stream(struct audio_hw_device *dev,
1996 struct audio_stream_in *stream)
1997{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001998 ALOGD("%s", __func__);
1999
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 in_standby(&stream->common);
2001 free(stream);
2002
2003 return;
2004}
2005
2006static int adev_dump(const audio_hw_device_t *device, int fd)
2007{
2008 return 0;
2009}
2010
2011static int adev_close(hw_device_t *device)
2012{
2013 struct audio_device *adev = (struct audio_device *)device;
2014 audio_route_free(adev->audio_route);
2015 free(device);
2016 return 0;
2017}
2018
2019static void init_platform_data(struct audio_device *adev)
2020{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002021 char platform[PROPERTY_VALUE_MAX];
2022 char baseband[PROPERTY_VALUE_MAX];
2023 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002024
2025 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002026 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002027 adev->fluence_in_voice_call = false;
2028 adev->fluence_in_voice_rec = false;
2029 adev->mic_type_analog = false;
2030
2031 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002032 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002033 adev->mic_type_analog = true;
2034
2035 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002036 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002037 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2038 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002039 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002040 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2041 adev->acdb_settings |= DMIC_FLAG;
2042 }
2043
2044 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2045 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002046 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002047 adev->fluence_in_voice_call = true;
2048 }
2049
2050 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002051 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002052 adev->fluence_in_voice_rec = true;
2053 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002054
2055 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002056 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002057 adev->fluence_in_spkr_mode = true;
2058 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002059 }
2060
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2062 if (adev->acdb_handle == NULL) {
2063 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2064 } else {
2065 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002066 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2067 "acdb_loader_deallocate_ACDB");
2068 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2069 "acdb_loader_send_audio_cal");
2070 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2071 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002072 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2073 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002075 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002076 else
2077 adev->acdb_init();
2078 }
2079
2080 /* If platform is Fusion3, load CSD Client specific symbols
2081 * Voice call is handled by MDM and apps processor talks to
2082 * MDM through CSD Client
2083 */
2084 property_get("ro.board.platform", platform, "");
2085 property_get("ro.baseband", baseband, "");
2086 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2087 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2088 if (adev->csd_client == NULL)
2089 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2090 }
2091
2092 if (adev->csd_client) {
2093 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002094 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2095 "csd_client_deinit");
2096 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2097 "csd_client_disable_device");
2098 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2099 "csd_client_enable_device");
2100 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2101 "csd_client_start_voice");
2102 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2103 "csd_client_stop_voice");
2104 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2105 "csd_client_volume");
2106 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2107 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002108 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2109 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002110
2111 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002112 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002113 } else {
2114 adev->csd_client_init();
2115 }
2116 }
2117}
2118
2119static int adev_open(const hw_module_t *module, const char *name,
2120 hw_device_t **device)
2121{
2122 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002123 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002125 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002126 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2127
2128 adev = calloc(1, sizeof(struct audio_device));
2129
2130 adev->mixer = mixer_open(MIXER_CARD);
2131 if (!adev->mixer) {
2132 ALOGE("Unable to open the mixer, aborting.");
2133 return -ENOSYS;
2134 }
2135
2136 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2137 if (!adev->audio_route) {
2138 free(adev);
2139 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2140 *device = NULL;
2141 return -EINVAL;
2142 }
2143
2144 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2145 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2146 adev->device.common.module = (struct hw_module_t *)module;
2147 adev->device.common.close = adev_close;
2148
2149 adev->device.init_check = adev_init_check;
2150 adev->device.set_voice_volume = adev_set_voice_volume;
2151 adev->device.set_master_volume = adev_set_master_volume;
2152 adev->device.get_master_volume = adev_get_master_volume;
2153 adev->device.set_master_mute = adev_set_master_mute;
2154 adev->device.get_master_mute = adev_get_master_mute;
2155 adev->device.set_mode = adev_set_mode;
2156 adev->device.set_mic_mute = adev_set_mic_mute;
2157 adev->device.get_mic_mute = adev_get_mic_mute;
2158 adev->device.set_parameters = adev_set_parameters;
2159 adev->device.get_parameters = adev_get_parameters;
2160 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2161 adev->device.open_output_stream = adev_open_output_stream;
2162 adev->device.close_output_stream = adev_close_output_stream;
2163 adev->device.open_input_stream = adev_open_input_stream;
2164 adev->device.close_input_stream = adev_close_input_stream;
2165 adev->device.dump = adev_dump;
2166
2167 /* Set the default route before the PCM stream is opened */
2168 pthread_mutex_lock(&adev->lock);
2169 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002170 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002171 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002172 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173 adev->voice_call_rx = NULL;
2174 adev->voice_call_tx = NULL;
2175 adev->voice_volume = 1.0f;
2176 adev->tty_mode = TTY_MODE_OFF;
2177 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002179 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002180 for (i = 0; i < SND_DEVICE_MAX; i++) {
2181 adev->snd_dev_ref_cnt[i] = 0;
2182 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002183 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184 pthread_mutex_unlock(&adev->lock);
2185
2186 /* Loads platform specific libraries dynamically */
2187 init_platform_data(adev);
2188
2189 *device = &adev->device.common;
2190
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002191 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192 return 0;
2193}
2194
2195static struct hw_module_methods_t hal_module_methods = {
2196 .open = adev_open,
2197};
2198
2199struct audio_module HAL_MODULE_INFO_SYM = {
2200 .common = {
2201 .tag = HARDWARE_MODULE_TAG,
2202 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2203 .hal_api_version = HARDWARE_HAL_API_VERSION,
2204 .id = AUDIO_HARDWARE_MODULE_ID,
2205 .name = "QCOM Audio HAL",
2206 .author = "Code Aurora Forum",
2207 .methods = &hal_module_methods,
2208 },
2209};