blob: 3782c32c4bedc1ede23b5fbff3eeb5579ca75d08 [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{
1383 return -ENOSYS;
1384}
1385
1386static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1387 size_t bytes)
1388{
1389 struct stream_out *out = (struct stream_out *)stream;
1390 struct audio_device *adev = out->dev;
1391 int i, ret = -1;
1392
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 pthread_mutex_lock(&out->lock);
1394 if (out->standby) {
Eric Laurent150dbfe2013-02-27 14:31:02 -08001395 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001396 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001397 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001399 goto exit;
1400 }
1401 out->standby = false;
1402 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403
1404 if (out->pcm) {
1405 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1406 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1407 }
1408
1409exit:
1410 pthread_mutex_unlock(&out->lock);
1411
1412 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001413 if (out->pcm)
1414 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001415 out_standby(&out->stream.common);
1416 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1417 out_get_sample_rate(&out->stream.common));
1418 }
1419 return bytes;
1420}
1421
1422static int out_get_render_position(const struct audio_stream_out *stream,
1423 uint32_t *dsp_frames)
1424{
1425 return -EINVAL;
1426}
1427
1428static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1429{
1430 return 0;
1431}
1432
1433static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1434{
1435 return 0;
1436}
1437
1438static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1439 int64_t *timestamp)
1440{
1441 return -EINVAL;
1442}
1443
1444/** audio_stream_in implementation **/
1445static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1446{
1447 struct stream_in *in = (struct stream_in *)stream;
1448
1449 return in->config.rate;
1450}
1451
1452static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1453{
1454 return -ENOSYS;
1455}
1456
1457static size_t in_get_buffer_size(const struct audio_stream *stream)
1458{
1459 struct stream_in *in = (struct stream_in *)stream;
1460
1461 return in->config.period_size * audio_stream_frame_size(stream);
1462}
1463
1464static uint32_t in_get_channels(const struct audio_stream *stream)
1465{
1466 struct stream_in *in = (struct stream_in *)stream;
1467
1468 return in->channel_mask;
1469}
1470
1471static audio_format_t in_get_format(const struct audio_stream *stream)
1472{
1473 return AUDIO_FORMAT_PCM_16_BIT;
1474}
1475
1476static int in_set_format(struct audio_stream *stream, audio_format_t format)
1477{
1478 return -ENOSYS;
1479}
1480
1481static int in_standby(struct audio_stream *stream)
1482{
1483 struct stream_in *in = (struct stream_in *)stream;
1484 struct audio_device *adev = in->dev;
1485 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001486 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001487 pthread_mutex_lock(&in->lock);
1488 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001489 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001490 if (in->pcm) {
1491 pcm_close(in->pcm);
1492 in->pcm = NULL;
1493 }
1494 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001496 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 }
1498 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001499 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001500 return status;
1501}
1502
1503static int in_dump(const struct audio_stream *stream, int fd)
1504{
1505 return 0;
1506}
1507
1508static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1509{
1510 struct stream_in *in = (struct stream_in *)stream;
1511 struct audio_device *adev = in->dev;
1512 struct str_parms *parms;
1513 char *str;
1514 char value[32];
1515 int ret, val = 0;
1516
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001517 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001518 parms = str_parms_create_str(kvpairs);
1519
1520 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001523 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001524 if (ret >= 0) {
1525 val = atoi(value);
1526 /* no audio source uses val == 0 */
1527 if ((in->source != val) && (val != 0)) {
1528 in->source = val;
1529 }
1530 }
1531
1532 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1533 if (ret >= 0) {
1534 val = atoi(value);
1535 if ((in->device != val) && (val != 0)) {
1536 in->device = val;
1537 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001538 if (!in->standby)
1539 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540 }
1541 }
1542
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001543 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001544 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001545
1546 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001547 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 return ret;
1549}
1550
1551static char* in_get_parameters(const struct audio_stream *stream,
1552 const char *keys)
1553{
1554 return strdup("");
1555}
1556
1557static int in_set_gain(struct audio_stream_in *stream, float gain)
1558{
1559 return 0;
1560}
1561
1562static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1563 size_t bytes)
1564{
1565 struct stream_in *in = (struct stream_in *)stream;
1566 struct audio_device *adev = in->dev;
1567 int i, ret = -1;
1568
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 pthread_mutex_lock(&in->lock);
1570 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001571 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001572 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001573 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001574 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575 goto exit;
1576 }
1577 in->standby = 0;
1578 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001579
1580 if (in->pcm) {
1581 ret = pcm_read(in->pcm, buffer, bytes);
1582 }
1583
1584 /*
1585 * Instead of writing zeroes here, we could trust the hardware
1586 * to always provide zeroes when muted.
1587 */
1588 if (ret == 0 && adev->mic_mute)
1589 memset(buffer, 0, bytes);
1590
1591exit:
1592 pthread_mutex_unlock(&in->lock);
1593
1594 if (ret != 0) {
1595 in_standby(&in->stream.common);
1596 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1597 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1598 in_get_sample_rate(&in->stream.common));
1599 }
1600 return bytes;
1601}
1602
1603static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1604{
1605 return 0;
1606}
1607
1608static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1609{
1610 return 0;
1611}
1612
1613static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1614{
1615 return 0;
1616}
1617
1618static int adev_open_output_stream(struct audio_hw_device *dev,
1619 audio_io_handle_t handle,
1620 audio_devices_t devices,
1621 audio_output_flags_t flags,
1622 struct audio_config *config,
1623 struct audio_stream_out **stream_out)
1624{
1625 struct audio_device *adev = (struct audio_device *)dev;
1626 struct stream_out *out;
1627 int i, ret;
1628
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001629 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001630 __func__, config->sample_rate, config->channel_mask, devices, flags);
1631 *stream_out = NULL;
1632 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1633
1634 if (devices == AUDIO_DEVICE_NONE)
1635 devices = AUDIO_DEVICE_OUT_SPEAKER;
1636
1637 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1638 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1639 out->flags = flags;
1640 out->devices = devices;
1641
1642 /* Init use case and pcm_config */
1643 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1644 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001645 pthread_mutex_lock(&adev->lock);
1646 ret = read_hdmi_channel_masks(out);
1647 pthread_mutex_unlock(&adev->lock);
1648 if (ret != 0) {
1649 /* If HDMI does not support multi channel playback, set the default */
1650 out->config.channels = popcount(out->channel_mask);
1651 set_hdmi_channels(adev->mixer, out->config.channels);
1652 goto error_open;
1653 }
1654
1655 if (config->sample_rate == 0)
1656 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1657 if (config->channel_mask == 0)
1658 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1659
1660 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001661 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1662 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001663 out->config.rate = config->sample_rate;
1664 out->config.channels = popcount(out->channel_mask);
1665 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1666 set_hdmi_channels(adev->mixer, out->config.channels);
1667 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1668 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1669 out->config = pcm_config_deep_buffer;
1670 } else {
1671 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1672 out->config = pcm_config_low_latency;
1673 }
1674
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001675 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1676 if(adev->primary_output == NULL)
1677 adev->primary_output = out;
1678 else {
1679 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001680 ret = -EEXIST;
1681 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001682 }
1683 }
1684
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001685 /* Check if this usecase is already existing */
1686 pthread_mutex_lock(&adev->lock);
1687 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1688 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001690 ret = -EEXIST;
1691 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001692 }
1693 pthread_mutex_unlock(&adev->lock);
1694
1695 out->stream.common.get_sample_rate = out_get_sample_rate;
1696 out->stream.common.set_sample_rate = out_set_sample_rate;
1697 out->stream.common.get_buffer_size = out_get_buffer_size;
1698 out->stream.common.get_channels = out_get_channels;
1699 out->stream.common.get_format = out_get_format;
1700 out->stream.common.set_format = out_set_format;
1701 out->stream.common.standby = out_standby;
1702 out->stream.common.dump = out_dump;
1703 out->stream.common.set_parameters = out_set_parameters;
1704 out->stream.common.get_parameters = out_get_parameters;
1705 out->stream.common.add_audio_effect = out_add_audio_effect;
1706 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1707 out->stream.get_latency = out_get_latency;
1708 out->stream.set_volume = out_set_volume;
1709 out->stream.write = out_write;
1710 out->stream.get_render_position = out_get_render_position;
1711 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1712
1713 out->dev = adev;
1714 out->standby = 1;
1715
1716 config->format = out->stream.common.get_format(&out->stream.common);
1717 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1718 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1719
1720 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001721 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001723
1724error_open:
1725 free(out);
1726 *stream_out = NULL;
1727 ALOGD("%s: exit: ret %d", __func__, ret);
1728 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001729}
1730
1731static void adev_close_output_stream(struct audio_hw_device *dev,
1732 struct audio_stream_out *stream)
1733{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001734 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735 out_standby(&stream->common);
1736 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001737 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738}
1739
1740static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1741{
1742 struct audio_device *adev = (struct audio_device *)dev;
1743 struct str_parms *parms;
1744 char *str;
1745 char value[32];
1746 int ret;
1747
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001748 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001749
1750 parms = str_parms_create_str(kvpairs);
1751 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1752 if (ret >= 0) {
1753 int tty_mode;
1754
1755 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1756 tty_mode = TTY_MODE_OFF;
1757 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1758 tty_mode = TTY_MODE_VCO;
1759 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1760 tty_mode = TTY_MODE_HCO;
1761 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1762 tty_mode = TTY_MODE_FULL;
1763 else
1764 return -EINVAL;
1765
1766 pthread_mutex_lock(&adev->lock);
1767 if (tty_mode != adev->tty_mode) {
1768 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001769 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1770 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001771 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 }
1773 pthread_mutex_unlock(&adev->lock);
1774 }
1775
1776 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1777 if (ret >= 0) {
1778 /* When set to false, HAL should disable EC and NS
1779 * But it is currently not supported.
1780 */
1781 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1782 adev->bluetooth_nrec = true;
1783 else
1784 adev->bluetooth_nrec = false;
1785 }
1786
1787 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1788 if (ret >= 0) {
1789 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1790 adev->screen_off = false;
1791 else
1792 adev->screen_off = true;
1793 }
1794
1795 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001796 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797 return ret;
1798}
1799
1800static char* adev_get_parameters(const struct audio_hw_device *dev,
1801 const char *keys)
1802{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001803 return strdup("");
1804}
1805
1806static int adev_init_check(const struct audio_hw_device *dev)
1807{
1808 return 0;
1809}
1810
1811static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1812{
1813 struct audio_device *adev = (struct audio_device *)dev;
1814 int vol, err = 0;
1815
1816 pthread_mutex_lock(&adev->lock);
1817 adev->voice_volume = volume;
1818 if (adev->mode == AUDIO_MODE_IN_CALL) {
1819 if (volume < 0.0) {
1820 volume = 0.0;
1821 } else if (volume > 1.0) {
1822 volume = 1.0;
1823 }
1824
1825 vol = lrint(volume * 100.0);
1826
1827 // Voice volume levels from android are mapped to driver volume levels as follows.
1828 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1829 // So adjust the volume to get the correct volume index in driver
1830 vol = 100 - vol;
1831
1832 if (adev->csd_client) {
1833 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001834 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 } else {
1836 err = adev->csd_volume(vol);
1837 if (err < 0) {
1838 ALOGE("%s: csd_client error %d", __func__, err);
1839 }
1840 }
1841 } else {
1842 ALOGE("%s: No CSD Client present", __func__);
1843 }
1844 }
1845 pthread_mutex_unlock(&adev->lock);
1846 return err;
1847}
1848
1849static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1850{
1851 return -ENOSYS;
1852}
1853
1854static int adev_get_master_volume(struct audio_hw_device *dev,
1855 float *volume)
1856{
1857 return -ENOSYS;
1858}
1859
1860static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1861{
1862 return -ENOSYS;
1863}
1864
1865static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1866{
1867 return -ENOSYS;
1868}
1869
1870static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1871{
1872 struct audio_device *adev = (struct audio_device *)dev;
1873
1874 pthread_mutex_lock(&adev->lock);
1875 if (adev->mode != mode) {
1876 adev->mode = mode;
1877 }
1878 pthread_mutex_unlock(&adev->lock);
1879 return 0;
1880}
1881
1882static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1883{
1884 struct audio_device *adev = (struct audio_device *)dev;
1885 int err = 0;
1886
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001887 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001888 adev->mic_mute = state;
1889 if (adev->mode == AUDIO_MODE_IN_CALL) {
1890 if (adev->csd_client) {
1891 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001892 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 } else {
1894 err = adev->csd_mic_mute(state);
1895 if (err < 0) {
1896 ALOGE("%s: csd_client error %d", __func__, err);
1897 }
1898 }
1899 } else {
1900 ALOGE("%s: No CSD Client present", __func__);
1901 }
1902 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001903 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001904 return err;
1905}
1906
1907static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1908{
1909 struct audio_device *adev = (struct audio_device *)dev;
1910
1911 *state = adev->mic_mute;
1912
1913 return 0;
1914}
1915
1916static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1917 const struct audio_config *config)
1918{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 int channel_count = popcount(config->channel_mask);
1920
1921 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1922}
1923
1924static int adev_open_input_stream(struct audio_hw_device *dev,
1925 audio_io_handle_t handle,
1926 audio_devices_t devices,
1927 struct audio_config *config,
1928 struct audio_stream_in **stream_in)
1929{
1930 struct audio_device *adev = (struct audio_device *)dev;
1931 struct stream_in *in;
1932 int ret, buffer_size, frame_size;
1933 int channel_count = popcount(config->channel_mask);
1934
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001935 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001936 *stream_in = NULL;
1937 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1938 return -EINVAL;
1939
1940 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1941
1942 in->stream.common.get_sample_rate = in_get_sample_rate;
1943 in->stream.common.set_sample_rate = in_set_sample_rate;
1944 in->stream.common.get_buffer_size = in_get_buffer_size;
1945 in->stream.common.get_channels = in_get_channels;
1946 in->stream.common.get_format = in_get_format;
1947 in->stream.common.set_format = in_set_format;
1948 in->stream.common.standby = in_standby;
1949 in->stream.common.dump = in_dump;
1950 in->stream.common.set_parameters = in_set_parameters;
1951 in->stream.common.get_parameters = in_get_parameters;
1952 in->stream.common.add_audio_effect = in_add_audio_effect;
1953 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1954 in->stream.set_gain = in_set_gain;
1955 in->stream.read = in_read;
1956 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1957
1958 in->device = devices;
1959 in->source = AUDIO_SOURCE_DEFAULT;
1960 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001961 in->standby = 1;
1962 in->channel_mask = config->channel_mask;
1963
1964 /* Update config params with the requested sample rate and channels */
1965 in->usecase = USECASE_AUDIO_RECORD;
1966 in->config = pcm_config_audio_capture;
1967 in->config.channels = channel_count;
1968 in->config.rate = config->sample_rate;
1969
1970 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1971 buffer_size = get_input_buffer_size(config->sample_rate,
1972 config->format,
1973 channel_count);
1974 in->config.period_size = buffer_size / frame_size;
1975
1976 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001977 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 return 0;
1979
1980err_open:
1981 free(in);
1982 *stream_in = NULL;
1983 return ret;
1984}
1985
1986static void adev_close_input_stream(struct audio_hw_device *dev,
1987 struct audio_stream_in *stream)
1988{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001989 ALOGD("%s", __func__);
1990
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991 in_standby(&stream->common);
1992 free(stream);
1993
1994 return;
1995}
1996
1997static int adev_dump(const audio_hw_device_t *device, int fd)
1998{
1999 return 0;
2000}
2001
2002static int adev_close(hw_device_t *device)
2003{
2004 struct audio_device *adev = (struct audio_device *)device;
2005 audio_route_free(adev->audio_route);
2006 free(device);
2007 return 0;
2008}
2009
2010static void init_platform_data(struct audio_device *adev)
2011{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002012 char platform[PROPERTY_VALUE_MAX];
2013 char baseband[PROPERTY_VALUE_MAX];
2014 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002015
2016 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002017 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002018 adev->fluence_in_voice_call = false;
2019 adev->fluence_in_voice_rec = false;
2020 adev->mic_type_analog = false;
2021
2022 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002023 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002024 adev->mic_type_analog = true;
2025
2026 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002027 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002028 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2029 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002030 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002031 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2032 adev->acdb_settings |= DMIC_FLAG;
2033 }
2034
2035 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2036 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002037 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002038 adev->fluence_in_voice_call = true;
2039 }
2040
2041 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002042 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002043 adev->fluence_in_voice_rec = true;
2044 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002045
2046 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002047 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002048 adev->fluence_in_spkr_mode = true;
2049 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002050 }
2051
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002052 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2053 if (adev->acdb_handle == NULL) {
2054 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2055 } else {
2056 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2058 "acdb_loader_deallocate_ACDB");
2059 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2060 "acdb_loader_send_audio_cal");
2061 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2062 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002063 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2064 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002065 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002066 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 else
2068 adev->acdb_init();
2069 }
2070
2071 /* If platform is Fusion3, load CSD Client specific symbols
2072 * Voice call is handled by MDM and apps processor talks to
2073 * MDM through CSD Client
2074 */
2075 property_get("ro.board.platform", platform, "");
2076 property_get("ro.baseband", baseband, "");
2077 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2078 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2079 if (adev->csd_client == NULL)
2080 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2081 }
2082
2083 if (adev->csd_client) {
2084 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002085 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2086 "csd_client_deinit");
2087 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2088 "csd_client_disable_device");
2089 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2090 "csd_client_enable_device");
2091 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2092 "csd_client_start_voice");
2093 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2094 "csd_client_stop_voice");
2095 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2096 "csd_client_volume");
2097 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2098 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002099 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2100 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002101
2102 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002103 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002104 } else {
2105 adev->csd_client_init();
2106 }
2107 }
2108}
2109
2110static int adev_open(const hw_module_t *module, const char *name,
2111 hw_device_t **device)
2112{
2113 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002114 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002115
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002116 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002117 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2118
2119 adev = calloc(1, sizeof(struct audio_device));
2120
2121 adev->mixer = mixer_open(MIXER_CARD);
2122 if (!adev->mixer) {
2123 ALOGE("Unable to open the mixer, aborting.");
2124 return -ENOSYS;
2125 }
2126
2127 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2128 if (!adev->audio_route) {
2129 free(adev);
2130 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2131 *device = NULL;
2132 return -EINVAL;
2133 }
2134
2135 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2136 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2137 adev->device.common.module = (struct hw_module_t *)module;
2138 adev->device.common.close = adev_close;
2139
2140 adev->device.init_check = adev_init_check;
2141 adev->device.set_voice_volume = adev_set_voice_volume;
2142 adev->device.set_master_volume = adev_set_master_volume;
2143 adev->device.get_master_volume = adev_get_master_volume;
2144 adev->device.set_master_mute = adev_set_master_mute;
2145 adev->device.get_master_mute = adev_get_master_mute;
2146 adev->device.set_mode = adev_set_mode;
2147 adev->device.set_mic_mute = adev_set_mic_mute;
2148 adev->device.get_mic_mute = adev_get_mic_mute;
2149 adev->device.set_parameters = adev_set_parameters;
2150 adev->device.get_parameters = adev_get_parameters;
2151 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2152 adev->device.open_output_stream = adev_open_output_stream;
2153 adev->device.close_output_stream = adev_close_output_stream;
2154 adev->device.open_input_stream = adev_open_input_stream;
2155 adev->device.close_input_stream = adev_close_input_stream;
2156 adev->device.dump = adev_dump;
2157
2158 /* Set the default route before the PCM stream is opened */
2159 pthread_mutex_lock(&adev->lock);
2160 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002161 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002162 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164 adev->voice_call_rx = NULL;
2165 adev->voice_call_tx = NULL;
2166 adev->voice_volume = 1.0f;
2167 adev->tty_mode = TTY_MODE_OFF;
2168 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002170 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002171 for (i = 0; i < SND_DEVICE_MAX; i++) {
2172 adev->snd_dev_ref_cnt[i] = 0;
2173 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002174 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175 pthread_mutex_unlock(&adev->lock);
2176
2177 /* Loads platform specific libraries dynamically */
2178 init_platform_data(adev);
2179
2180 *device = &adev->device.common;
2181
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002182 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 return 0;
2184}
2185
2186static struct hw_module_methods_t hal_module_methods = {
2187 .open = adev_open,
2188};
2189
2190struct audio_module HAL_MODULE_INFO_SYM = {
2191 .common = {
2192 .tag = HARDWARE_MODULE_TAG,
2193 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2194 .hal_api_version = HARDWARE_HAL_API_VERSION,
2195 .id = AUDIO_HARDWARE_MODULE_ID,
2196 .name = "QCOM Audio HAL",
2197 .author = "Code Aurora Forum",
2198 .methods = &hal_module_methods,
2199 },
2200};