blob: 64c313c1f8cb104bceae48ca1f40ec482f1be082 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080019#define LOG_NDDEBUG 0
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080020
21#include <errno.h>
22#include <pthread.h>
23#include <stdint.h>
24#include <sys/time.h>
25#include <stdlib.h>
26#include <dlfcn.h>
27#include <math.h>
28
29#include <cutils/log.h>
30#include <cutils/str_parms.h>
31#include <cutils/properties.h>
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -080032#include <cutils/list.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080033
34#include "audio_hw.h"
35
36#define LIB_ACDB_LOADER "/system/lib/libacdbloader.so"
37#define LIB_CSD_CLIENT "/system/lib/libcsd-client.so"
38#define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
39#define MIXER_CARD 0
40
41#define STRING_TO_ENUM(string) { #string, string }
42
43/* Flags used to initialize acdb_settings variable that goes to ACDB library */
44#define DMIC_FLAG 0x00000002
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -080045#define TTY_MODE_OFF 0x00000010
46#define TTY_MODE_FULL 0x00000020
47#define TTY_MODE_VCO 0x00000040
48#define TTY_MODE_HCO 0x00000080
49#define TTY_MODE_CLEAR 0xFFFFFF0F
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050
51struct string_to_enum {
52 const char *name;
53 uint32_t value;
54};
55
56static const struct string_to_enum out_channels_name_to_enum_table[] = {
57 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
58 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
59 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
60};
61
62static const char * const use_case_table[AUDIO_USECASE_MAX] = {
63 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
64 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
65 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
66 [USECASE_AUDIO_RECORD] = "audio-record",
67 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
68 [USECASE_VOICE_CALL] = "voice-call",
69};
70
71static const int pcm_device_table[AUDIO_USECASE_MAX][2] = {
72 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070073#ifdef MSM8974
74 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
75#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080076 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {14, 14},
Sungmin Choi5195a4b2013-04-03 21:54:22 -070077#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080078 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
79 [USECASE_AUDIO_RECORD] = {0, 0},
80 [USECASE_AUDIO_RECORD_LOW_LATENCY] = {14, 14},
Shashank Mittald2f3c3b2013-05-02 20:27:47 -070081#ifdef MSM8974
82 [USECASE_VOICE_CALL] = {2, 2},
83#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080084 [USECASE_VOICE_CALL] = {12, 12},
Shashank Mittald2f3c3b2013-05-02 20:27:47 -070085#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080086};
87
88/* Array to store sound devices */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080089static const char * const device_table[SND_DEVICE_MAX] = {
90 [SND_DEVICE_NONE] = "none",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080091 /* Playback sound devices */
92 [SND_DEVICE_OUT_HANDSET] = "handset",
93 [SND_DEVICE_OUT_SPEAKER] = "speaker",
94 [SND_DEVICE_OUT_HEADPHONES] = "headphones",
95 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones",
96 [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker",
97 [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -080098 [SND_DEVICE_OUT_HDMI] = "hdmi",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080099 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi",
100 [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800101 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = "voice-handset-tmus",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800102 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones",
103 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones",
104 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800105
106 /* Capture sound devices */
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800107 [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800108 [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic",
109 [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic",
110 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic",
111 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic",
112 [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800113 [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800114 [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800115 [SND_DEVICE_IN_VOICE_DMIC_EF] = "voice-dmic-ef",
116 [SND_DEVICE_IN_VOICE_DMIC_BS] = "voice-dmic-bs",
117 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = "voice-dmic-ef-tmus",
118 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = "voice-speaker-dmic-ef",
119 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = "voice-speaker-dmic-bs",
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800120 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic",
121 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic",
122 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800123 [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic",
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800124 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = "voice-rec-dmic-ef",
125 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = "voice-rec-dmic-bs",
Eric Laurentc8400632013-02-14 19:04:54 -0800126 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = "voice-rec-dmic-ef-fluence",
127 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = "voice-rec-dmic-bs-fluence",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800128};
129
Eric Laurentc8400632013-02-14 19:04:54 -0800130/* ACDB IDs (audio DSP path configuration IDs) for each sound device */
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800131static const int acdb_device_table[SND_DEVICE_MAX] = {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700132 [SND_DEVICE_NONE] = -1,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800133 [SND_DEVICE_OUT_HANDSET] = 7,
134 [SND_DEVICE_OUT_SPEAKER] = 14,
135 [SND_DEVICE_OUT_HEADPHONES] = 10,
136 [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = 10,
137 [SND_DEVICE_OUT_VOICE_SPEAKER] = 14,
138 [SND_DEVICE_OUT_VOICE_HEADPHONES] = 10,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800139 [SND_DEVICE_OUT_HDMI] = 18,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800140 [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = 14,
141 [SND_DEVICE_OUT_BT_SCO] = 22,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800142 [SND_DEVICE_OUT_VOICE_HANDSET_TMUS] = 81,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800143 [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = 17,
144 [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = 17,
145 [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = 37,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800146
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800147 [SND_DEVICE_IN_HANDSET_MIC] = 4,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800148 [SND_DEVICE_IN_SPEAKER_MIC] = 4,
149 [SND_DEVICE_IN_HEADSET_MIC] = 8,
150 [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = 11,
151 [SND_DEVICE_IN_VOICE_HEADSET_MIC] = 8,
152 [SND_DEVICE_IN_HDMI_MIC] = 4,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800153 [SND_DEVICE_IN_BT_SCO_MIC] = 21,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800154 [SND_DEVICE_IN_CAMCORDER_MIC] = 61,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800155 [SND_DEVICE_IN_VOICE_DMIC_EF] = 6,
156 [SND_DEVICE_IN_VOICE_DMIC_BS] = 5,
157 [SND_DEVICE_IN_VOICE_DMIC_EF_TMUS] = 91,
158 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF] = 13,
159 [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS] = 12,
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800160 [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = 16,
161 [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = 36,
162 [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = 16,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163 [SND_DEVICE_IN_VOICE_REC_MIC] = 62,
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800164 [SND_DEVICE_IN_VOICE_REC_DMIC_EF] = 62,
165 [SND_DEVICE_IN_VOICE_REC_DMIC_BS] = 62,
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800166 /* TODO: Update with proper acdb ids */
Eric Laurentc8400632013-02-14 19:04:54 -0800167 [SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE] = 62,
168 [SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE] = 62,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800169};
170
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171int edid_get_max_channels(void);
172
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800173static pthread_once_t check_op_once_ctl = PTHREAD_ONCE_INIT;
174static bool is_tmus = false;
175
176static void check_operator()
177{
178 char value[PROPERTY_VALUE_MAX];
179 int mccmnc;
180 property_get("gsm.sim.operator.numeric",value,"0");
181 mccmnc = atoi(value);
182 ALOGD("%s: tmus mccmnc %d", __func__, mccmnc);
183 switch(mccmnc) {
184 /* TMUS MCC(310), MNC(490, 260, 026) */
185 case 310490:
186 case 310260:
187 case 310026:
188 is_tmus = true;
189 break;
190 }
191}
192
193static bool is_operator_tmus()
194{
195 pthread_once(&check_op_once_ctl, check_operator);
196 return is_tmus;
197}
198
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199static int get_pcm_device_id(struct audio_route *ar,
200 audio_usecase_t usecase,
201 int device_type)
202{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800203 int device_id;
204 if (device_type == PCM_PLAYBACK)
205 device_id = pcm_device_table[usecase][0];
206 else
207 device_id = pcm_device_table[usecase][1];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800208 return device_id;
209}
210
211static int get_acdb_device_id(snd_device_t snd_device)
212{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 return acdb_device_table[snd_device];
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800214}
215
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800216static void add_backend_name(char *mixer_path,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700217 snd_device_t snd_device)
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800218{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 if (snd_device == SND_DEVICE_IN_BT_SCO_MIC)
220 strcat(mixer_path, " bt-sco");
221 else if(snd_device == SND_DEVICE_OUT_BT_SCO)
222 strcat(mixer_path, " bt-sco");
223 else if (snd_device == SND_DEVICE_OUT_HDMI)
224 strcat(mixer_path, " hdmi");
225 else if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HDMI)
226 strcat(mixer_path, " speaker-and-hdmi");
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800227}
228
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229static int enable_audio_route(struct audio_device *adev,
230 struct audio_usecase *usecase,
231 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800232{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800234 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800235
236 if (usecase == NULL)
237 return -EINVAL;
238
239 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
240
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800241 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700242 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800243 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800245
246 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700247 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800248 ALOGD("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 audio_route_apply_path(adev->audio_route, mixer_path);
250 if (update_mixer)
251 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800252
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800253 ALOGV("%s: exit", __func__);
254 return 0;
255}
256
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700257static int disable_audio_route(struct audio_device *adev,
258 struct audio_usecase *usecase,
259 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800260{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800262 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800263
264 if (usecase == NULL)
265 return -EINVAL;
266
267 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700268 if (usecase->type == PCM_CAPTURE)
269 snd_device = usecase->in_snd_device;
270 else
271 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800272 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273 add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800274 ALOGD("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700275 audio_route_reset_path(adev->audio_route, mixer_path);
276 if (update_mixer)
277 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800278
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800279 ALOGV("%s: exit", __func__);
280 return 0;
281}
282
283static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700284 snd_device_t snd_device,
285 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800286{
287 int acdb_dev_id, acdb_dev_type;
288
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800289 if (snd_device < SND_DEVICE_MIN ||
290 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800291 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800292 return -EINVAL;
293 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700294
295 adev->snd_dev_ref_cnt[snd_device]++;
296 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
297 ALOGD("%s: snd_device(%d: %s) is already active",
298 __func__, snd_device, device_table[snd_device]);
299 return 0;
300 }
301
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800302 acdb_dev_id = get_acdb_device_id(snd_device);
303 if (acdb_dev_id < 0) {
304 ALOGE("%s: Could not find acdb id for device(%d)",
305 __func__, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700306 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800307 return -EINVAL;
308 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 if (adev->acdb_send_audio_cal) {
310 ALOGV("%s: sending audio calibration for snd_device(%d) acdb_id(%d)",
311 __func__, snd_device, acdb_dev_id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700312 if (snd_device >= SND_DEVICE_OUT_BEGIN &&
313 snd_device < SND_DEVICE_OUT_END)
314 acdb_dev_type = ACDB_DEV_TYPE_OUT;
315 else
316 acdb_dev_type = ACDB_DEV_TYPE_IN;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800317 adev->acdb_send_audio_cal(acdb_dev_id, acdb_dev_type);
318 } else {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700319 ALOGW("%s: Could not find the symbol acdb_send_audio_cal from %s",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320 __func__, LIB_ACDB_LOADER);
321 }
322
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700323 ALOGD("%s: snd_device(%d: %s)", __func__,
324 snd_device, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800325 audio_route_apply_path(adev->audio_route, device_table[snd_device]);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 if (update_mixer)
327 audio_route_update_mixer(adev->audio_route);
328
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800329 return 0;
330}
331
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700332static int disable_snd_device(struct audio_device *adev,
333 snd_device_t snd_device,
334 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800335{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800336 if (snd_device < SND_DEVICE_MIN ||
337 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800338 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800339 return -EINVAL;
340 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
342 ALOGE("%s: device ref cnt is already 0", __func__);
343 return -EINVAL;
344 }
345 adev->snd_dev_ref_cnt[snd_device]--;
346 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
347 ALOGD("%s: snd_device(%d: %s)", __func__,
348 snd_device, device_table[snd_device]);
349 audio_route_reset_path(adev->audio_route, device_table[snd_device]);
350 if (update_mixer)
351 audio_route_update_mixer(adev->audio_route);
352 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800353 return 0;
354}
355
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700356static void check_usecases_codec_backend(struct audio_device *adev,
357 struct audio_usecase *uc_info,
358 snd_device_t snd_device)
359{
360 struct listnode *node;
361 struct audio_usecase *usecase;
362 bool switch_device[AUDIO_USECASE_MAX];
363 int i, num_uc_to_switch = 0;
364
365 /*
366 * This function is to make sure that all the usecases that are active on
367 * the hardware codec backend are always routed to any one device that is
368 * handled by the hardware codec.
369 * For example, if low-latency and deep-buffer usecases are currently active
370 * on speaker and out_set_parameters(headset) is received on low-latency
371 * output, then we have to make sure deep-buffer is also switched to headset,
372 * because of the limitation that both the devices cannot be enabled
373 * at the same time as they share the same backend.
374 */
375 /* Disable all the usecases on the shared backend other than the
376 specified usecase */
377 for (i = 0; i < AUDIO_USECASE_MAX; i++)
378 switch_device[i] = false;
379
380 list_for_each(node, &adev->usecase_list) {
381 usecase = node_to_item(node, struct audio_usecase, list);
382 if (usecase->type != PCM_CAPTURE &&
383 usecase != uc_info &&
384 usecase->out_snd_device != snd_device &&
385 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
386 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
387 __func__, use_case_table[usecase->id],
388 device_table[usecase->out_snd_device]);
389 disable_audio_route(adev, usecase, false);
390 switch_device[usecase->id] = true;
391 num_uc_to_switch++;
392 }
393 }
394
395 if (num_uc_to_switch) {
396 /* Make sure all the streams are de-routed before disabling the device */
397 audio_route_update_mixer(adev->audio_route);
398
399 list_for_each(node, &adev->usecase_list) {
400 usecase = node_to_item(node, struct audio_usecase, list);
401 if (switch_device[usecase->id]) {
402 disable_snd_device(adev, usecase->out_snd_device, false);
403 enable_snd_device(adev, snd_device, false);
404 }
405 }
406
407 /* Make sure new snd device is enabled before re-routing the streams */
408 audio_route_update_mixer(adev->audio_route);
409
410 /* Re-route all the usecases on the shared backend other than the
411 specified usecase to new snd devices */
412 list_for_each(node, &adev->usecase_list) {
413 usecase = node_to_item(node, struct audio_usecase, list);
414 /* Update the out_snd_device only before enabling the audio route */
415 if (switch_device[usecase->id] ) {
416 usecase->out_snd_device = snd_device;
417 enable_audio_route(adev, usecase, false);
418 }
419 }
420
421 audio_route_update_mixer(adev->audio_route);
422 }
423}
424
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800425static int set_hdmi_channels(struct mixer *mixer,
426 int channel_count)
427{
428 struct mixer_ctl *ctl;
429 const char *channel_cnt_str = NULL;
430 const char *mixer_ctl_name = "HDMI_RX Channels";
431 switch (channel_count) {
432 case 8:
433 channel_cnt_str = "Eight"; break;
434 case 7:
435 channel_cnt_str = "Seven"; break;
436 case 6:
437 channel_cnt_str = "Six"; break;
438 case 5:
439 channel_cnt_str = "Five"; break;
440 case 4:
441 channel_cnt_str = "Four"; break;
442 case 3:
443 channel_cnt_str = "Three"; break;
444 default:
445 channel_cnt_str = "Two"; break;
446 }
447 ctl = mixer_get_ctl_by_name(mixer, mixer_ctl_name);
448 if (!ctl) {
449 ALOGE("%s: Could not get ctl for mixer cmd - %s",
450 __func__, mixer_ctl_name);
451 return -EINVAL;
452 }
453 ALOGV("HDMI channel count: %s", channel_cnt_str);
454 mixer_ctl_set_enum_by_string(ctl, channel_cnt_str);
455 return 0;
456}
457
458/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700459static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800460{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700461 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800462 int channels = edid_get_max_channels();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800463
464 switch (channels) {
465 /*
466 * Do not handle stereo output in Multi-channel cases
467 * Stereo case is handled in normal playback path
468 */
469 case 6:
470 ALOGV("%s: HDMI supports 5.1", __func__);
471 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
472 break;
473 case 8:
474 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
475 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
476 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
477 break;
478 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700479 ALOGE("HDMI does not support multi channel playback");
480 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800481 break;
482 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700483 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800484}
485
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700486static snd_device_t get_output_snd_device(struct audio_device *adev,
487 audio_devices_t devices)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800488{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700489 audio_mode_t mode = adev->mode;
490 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800491
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800492 ALOGV("%s: enter: output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800493 if (devices == AUDIO_DEVICE_NONE ||
494 devices & AUDIO_DEVICE_BIT_IN) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800495 ALOGV("%s: Invalid output devices (%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496 goto exit;
497 }
498
499 if (mode == AUDIO_MODE_IN_CALL) {
500 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
501 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800502 if (adev->tty_mode == TTY_MODE_FULL)
503 snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES;
504 else if (adev->tty_mode == TTY_MODE_VCO)
505 snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES;
506 else if (adev->tty_mode == TTY_MODE_HCO)
507 snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET;
508 else
509 snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800510 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
511 snd_device = SND_DEVICE_OUT_BT_SCO;
512 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
513 snd_device = SND_DEVICE_OUT_VOICE_SPEAKER;
514 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800515 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800516 snd_device = SND_DEVICE_OUT_VOICE_HANDSET_TMUS;
517 else
518 snd_device = SND_DEVICE_OUT_HANDSET;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800520 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800521 goto exit;
522 }
523 }
524
525 if (popcount(devices) == 2) {
526 if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
527 AUDIO_DEVICE_OUT_SPEAKER)) {
528 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
529 } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET |
530 AUDIO_DEVICE_OUT_SPEAKER)) {
531 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES;
532 } else if (devices == (AUDIO_DEVICE_OUT_AUX_DIGITAL |
533 AUDIO_DEVICE_OUT_SPEAKER)) {
534 snd_device = SND_DEVICE_OUT_SPEAKER_AND_HDMI;
535 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800536 ALOGE("%s: Invalid combo device(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537 goto exit;
538 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800539 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800540 goto exit;
541 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800542 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800543
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800544 if (popcount(devices) != 1) {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800545 ALOGE("%s: Invalid output devices(%#x)", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546 goto exit;
547 }
548
549 if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
550 devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
551 snd_device = SND_DEVICE_OUT_HEADPHONES;
552 } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) {
553 snd_device = SND_DEVICE_OUT_SPEAKER;
554 } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) {
555 snd_device = SND_DEVICE_OUT_BT_SCO;
556 } else if (devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
557 snd_device = SND_DEVICE_OUT_HDMI ;
558 } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) {
559 snd_device = SND_DEVICE_OUT_HANDSET;
560 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800561 ALOGE("%s: Unknown device(s) %#x", __func__, devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562 }
563exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800564 ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565 return snd_device;
566}
567
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568static snd_device_t get_input_snd_device(struct audio_device *adev,
569 audio_devices_t out_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800570{
Eric Laurentc8400632013-02-14 19:04:54 -0800571 audio_source_t source = (adev->active_input == NULL) ?
572 AUDIO_SOURCE_DEFAULT : adev->active_input->source;
573
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574 audio_mode_t mode = adev->mode;
Eric Laurentc8400632013-02-14 19:04:54 -0800575 audio_devices_t in_device = ((adev->active_input == NULL) ?
576 AUDIO_DEVICE_NONE : adev->active_input->device)
577 & ~AUDIO_DEVICE_BIT_IN;
578 audio_channel_mask_t channel_mask = (adev->active_input == NULL) ?
579 AUDIO_CHANNEL_IN_MONO : adev->active_input->channel_mask;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800580 snd_device_t snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 ALOGV("%s: enter: out_device(%#x) in_device(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800583 __func__, out_device, in_device);
584 if (mode == AUDIO_MODE_IN_CALL) {
585 if (out_device == AUDIO_DEVICE_NONE) {
586 ALOGE("%s: No output device set for voice call", __func__);
587 goto exit;
588 }
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800589 if (adev->tty_mode != TTY_MODE_OFF) {
590 if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
591 out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
592 switch (adev->tty_mode) {
593 case TTY_MODE_FULL:
594 snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC;
595 break;
596 case TTY_MODE_VCO:
597 snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC;
598 break;
599 case TTY_MODE_HCO:
600 snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC;
601 break;
602 default:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800603 ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode);
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800604 }
605 goto exit;
606 }
607 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800608 if (out_device & AUDIO_DEVICE_OUT_EARPIECE ||
609 out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800610 if (adev->mic_type_analog || adev->fluence_in_voice_call == false) {
611 snd_device = SND_DEVICE_IN_HANDSET_MIC;
612 } else {
613 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
Ravi Kumar Alamanda37718842013-02-22 18:44:45 -0800614 if (is_operator_tmus())
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800615 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF_TMUS;
616 else
617 snd_device = SND_DEVICE_IN_VOICE_DMIC_EF;
618 } else if(adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE)
619 snd_device = SND_DEVICE_IN_VOICE_DMIC_BS;
620 else
621 snd_device = SND_DEVICE_IN_HANDSET_MIC;
622 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
624 snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC;
625 } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) {
626 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
627 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800628 if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800629 adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
630 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_EF;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -0800631 } else if (adev->fluence_in_voice_call && adev->fluence_in_spkr_mode &&
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800632 adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
633 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_DMIC_BS;
634 } else {
635 snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC;
636 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800637 }
638 } else if (source == AUDIO_SOURCE_CAMCORDER) {
639 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC ||
640 in_device & AUDIO_DEVICE_IN_BACK_MIC) {
641 snd_device = SND_DEVICE_IN_CAMCORDER_MIC;
642 }
643 } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) {
644 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Eric Laurentc8400632013-02-14 19:04:54 -0800645 if (adev->dualmic_config == DUALMIC_CONFIG_ENDFIRE) {
646 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
647 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF;
648 else if (adev->fluence_in_voice_rec)
649 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_EF_FLUENCE;
650 else
651 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
652 } else if (adev->dualmic_config == DUALMIC_CONFIG_BROADSIDE) {
653 if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)
654 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS;
655 else if (adev->fluence_in_voice_rec)
656 snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_BS_FLUENCE;
657 else
658 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
659 } else
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800660 snd_device = SND_DEVICE_IN_VOICE_REC_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800661 }
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800662 } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
663 if (out_device & AUDIO_DEVICE_OUT_SPEAKER)
664 in_device = AUDIO_DEVICE_IN_BACK_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 } else if (source == AUDIO_SOURCE_DEFAULT) {
666 goto exit;
667 }
668
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800669 if (snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800670 goto exit;
671 }
672
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800673 if (in_device != AUDIO_DEVICE_NONE &&
Ravi Kumar Alamanda8455fa72013-02-19 14:53:19 -0800674 !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) &&
675 !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800676 if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800677 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800678 } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800679 if (adev->mic_type_analog)
680 snd_device = SND_DEVICE_IN_HANDSET_MIC;
681 else
682 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800683 } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) {
684 snd_device = SND_DEVICE_IN_HEADSET_MIC;
685 } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
686 snd_device = SND_DEVICE_IN_BT_SCO_MIC ;
687 } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) {
688 snd_device = SND_DEVICE_IN_HDMI_MIC;
689 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800690 ALOGE("%s: Unknown input device(s) %#x", __func__, in_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800691 ALOGW("%s: Using default handset-mic", __func__);
692 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800693 }
694 } else {
695 if (out_device & AUDIO_DEVICE_OUT_EARPIECE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800696 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) {
698 snd_device = SND_DEVICE_IN_HEADSET_MIC;
699 } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) {
700 snd_device = SND_DEVICE_IN_SPEAKER_MIC;
701 } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800702 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703 } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800704 snd_device = SND_DEVICE_IN_BT_SCO_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800705 } else if (out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
706 snd_device = SND_DEVICE_IN_HDMI_MIC;
707 } else {
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800708 ALOGE("%s: Unknown output device(s) %#x", __func__, out_device);
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -0800709 ALOGW("%s: Using default handset-mic", __func__);
710 snd_device = SND_DEVICE_IN_HANDSET_MIC;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711 }
712 }
713exit:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800714 ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800715 return snd_device;
716}
717
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700718static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
719 audio_usecase_t uc_id)
720{
721 struct audio_usecase *usecase;
722 struct listnode *node;
723
724 list_for_each(node, &adev->usecase_list) {
725 usecase = node_to_item(node, struct audio_usecase, list);
726 if (usecase->id == uc_id)
727 return usecase;
728 }
729 return NULL;
730}
731
732static int select_devices(struct audio_device *adev,
733 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800735 snd_device_t out_snd_device = SND_DEVICE_NONE;
736 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700737 struct audio_usecase *usecase = NULL;
738 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800739 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740 int acdb_rx_id, acdb_tx_id;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700743 usecase = get_usecase_from_list(adev, uc_id);
744 if (usecase == NULL) {
745 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
746 return -EINVAL;
747 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700749 if (usecase->type == VOICE_CALL) {
750 out_snd_device = get_output_snd_device(adev, usecase->stream.out->devices);
751 in_snd_device = get_input_snd_device(adev, usecase->stream.out->devices);
752 usecase->devices = usecase->stream.out->devices;
753 } else {
754 /*
755 * If the voice call is active, use the sound devices of voice call usecase
756 * so that it would not result any device switch. All the usecases will
757 * be switched to new device when select_devices() is called for voice call
758 * usecase. This is to avoid switching devices for voice call when
759 * check_usecases_codec_backend() is called below.
760 */
761 if (adev->in_call) {
762 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
763 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
764 in_snd_device = vc_usecase->in_snd_device;
765 out_snd_device = vc_usecase->out_snd_device;
766 }
767 }
768 if (usecase->type == PCM_PLAYBACK) {
769 usecase->devices = usecase->stream.out->devices;
770 in_snd_device = SND_DEVICE_NONE;
771 if (out_snd_device == SND_DEVICE_NONE)
772 out_snd_device = get_output_snd_device(adev,
773 usecase->stream.out->devices);
774 } else if (usecase->type == PCM_CAPTURE) {
775 usecase->devices = usecase->stream.in->device;
776 out_snd_device = SND_DEVICE_NONE;
777 if (in_snd_device == SND_DEVICE_NONE)
778 in_snd_device = get_input_snd_device(adev, SND_DEVICE_NONE);
779 }
780 }
781
782 if (out_snd_device == usecase->out_snd_device &&
783 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 return 0;
785 }
786
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800787 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
788 out_snd_device, device_table[out_snd_device],
789 in_snd_device, device_table[in_snd_device]);
790
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 /*
792 * Limitation: While in call, to do a device switch we need to disable
793 * and enable both RX and TX devices though one of them is same as current
794 * device.
795 */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700796 if (usecase->type == VOICE_CALL && adev->csd_client != NULL &&
797 usecase->in_snd_device != SND_DEVICE_NONE &&
798 usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800799 /* This must be called before disabling the mixer controls on APQ side */
800 if (adev->csd_disable_device == NULL) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800801 ALOGE("%s: dlsym error for csd_client_disable_device", __func__);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800802 } else {
803 status = adev->csd_disable_device();
804 if (status < 0) {
805 ALOGE("%s: csd_client_disable_device, failed, error %d",
806 __func__, status);
807 }
808 }
809 }
810
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700811 /* Disable current sound devices */
812 if (usecase->out_snd_device != SND_DEVICE_NONE) {
813 disable_audio_route(adev, usecase, true);
814 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815 }
816
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700817 if (usecase->in_snd_device != SND_DEVICE_NONE) {
818 disable_audio_route(adev, usecase, true);
819 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800820 }
821
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700822 /* Enable new sound devices */
823 if (out_snd_device != SND_DEVICE_NONE) {
824 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
825 check_usecases_codec_backend(adev, usecase, out_snd_device);
826 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800827 }
828
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829 if (in_snd_device != SND_DEVICE_NONE)
830 enable_snd_device(adev, in_snd_device, false);
831
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832 audio_route_update_mixer(adev->audio_route);
833
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700834 usecase->in_snd_device = in_snd_device;
835 usecase->out_snd_device = out_snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800836
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700837 enable_audio_route(adev, usecase, true);
838
839 if (usecase->type == VOICE_CALL && adev->csd_client) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800840 if (adev->csd_enable_device == NULL) {
841 ALOGE("%s: dlsym error for csd_client_enable_device",
842 __func__);
843 } else {
844 acdb_rx_id = get_acdb_device_id(out_snd_device);
845 acdb_tx_id = get_acdb_device_id(in_snd_device);
846
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700847 if (acdb_rx_id > 0 || acdb_tx_id > 0) {
848 status = adev->csd_enable_device(acdb_rx_id, acdb_tx_id,
849 adev->acdb_settings);
850 if (status < 0) {
851 ALOGE("%s: csd_client_enable_device, failed, error %d",
852 __func__, status);
853 }
854 } else {
855 ALOGE("%s: Incorrect ACDB IDs (rx: %d tx: %d)", __func__,
856 acdb_rx_id, acdb_tx_id);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800857 }
858 }
859 }
860
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800861 return status;
862}
863
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800864static int stop_input_stream(struct stream_in *in)
865{
866 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867 struct audio_usecase *uc_info;
868 struct audio_device *adev = in->dev;
869
Eric Laurentc8400632013-02-14 19:04:54 -0800870 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800871
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700872 ALOGD("%s: enter: usecase(%d: %s)", __func__,
873 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800874 uc_info = get_usecase_from_list(adev, in->usecase);
875 if (uc_info == NULL) {
876 ALOGE("%s: Could not find the usecase (%d) in the list",
877 __func__, in->usecase);
878 return -EINVAL;
879 }
880
Eric Laurent150dbfe2013-02-27 14:31:02 -0800881 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700882 disable_audio_route(adev, uc_info, true);
883
884 /* 2. Disable the tx device */
885 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800886
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800887 list_remove(&uc_info->list);
888 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800889
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800890 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800891 return ret;
892}
893
894int start_input_stream(struct stream_in *in)
895{
896 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800897 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800898 struct audio_usecase *uc_info;
899 struct audio_device *adev = in->dev;
900
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800901 ALOGD("%s: enter: usecase(%d)", __func__, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902 in->pcm_device_id = get_pcm_device_id(adev->audio_route,
903 in->usecase,
904 PCM_CAPTURE);
905 if (in->pcm_device_id < 0) {
906 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
907 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800908 ret = -EINVAL;
909 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800910 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700911
912 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
914 uc_info->id = in->usecase;
915 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800916 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700917 uc_info->devices = in->device;
918 uc_info->in_snd_device = SND_DEVICE_NONE;
919 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800920
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800921 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700922 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800923
Eric Laurentc8400632013-02-14 19:04:54 -0800924 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
925 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800926 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
927 PCM_IN, &in->config);
928 if (in->pcm && !pcm_is_ready(in->pcm)) {
929 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
930 pcm_close(in->pcm);
931 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800932 ret = -EIO;
933 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800934 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800935 ALOGD("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800936 return ret;
937
938error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800940
941error_config:
942 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700943 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800944
945 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800946}
947
948static int stop_output_stream(struct stream_out *out)
949{
950 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951 struct audio_usecase *uc_info;
952 struct audio_device *adev = out->dev;
953
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700954 ALOGD("%s: enter: usecase(%d: %s)", __func__,
955 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800956 uc_info = get_usecase_from_list(adev, out->usecase);
957 if (uc_info == NULL) {
958 ALOGE("%s: Could not find the usecase (%d) in the list",
959 __func__, out->usecase);
960 return -EINVAL;
961 }
962
Eric Laurent150dbfe2013-02-27 14:31:02 -0800963 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700964 disable_audio_route(adev, uc_info, true);
965
966 /* 2. Disable the rx device */
967 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800968
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800969 list_remove(&uc_info->list);
970 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700972 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973 return ret;
974}
975
976int start_output_stream(struct stream_out *out)
977{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800978 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800979 struct audio_usecase *uc_info;
980 struct audio_device *adev = out->dev;
981
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700982 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
983 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800984 out->pcm_device_id = get_pcm_device_id(adev->audio_route,
985 out->usecase,
986 PCM_PLAYBACK);
987 if (out->pcm_device_id < 0) {
988 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
989 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800990 ret = -EINVAL;
991 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992 }
993
994 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
995 uc_info->id = out->usecase;
996 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800997 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700998 uc_info->devices = out->devices;
999 uc_info->in_snd_device = SND_DEVICE_NONE;
1000 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001001
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001002 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001004 select_devices(adev, out->usecase);
1005
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1007 __func__, 0, out->pcm_device_id);
1008 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
1009 PCM_OUT, &out->config);
1010 if (out->pcm && !pcm_is_ready(out->pcm)) {
1011 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1012 pcm_close(out->pcm);
1013 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001014 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001015 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016 }
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001017 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001019error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001021error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001022 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001023}
1024
1025static int stop_voice_call(struct audio_device *adev)
1026{
1027 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 struct audio_usecase *uc_info;
1029
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001030 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001031 adev->in_call = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 if (adev->csd_client) {
1033 if (adev->csd_stop_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001034 ALOGE("dlsym error for csd_client_disable_device");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035 } else {
1036 ret = adev->csd_stop_voice();
1037 if (ret < 0) {
1038 ALOGE("%s: csd_client error %d\n", __func__, ret);
1039 }
1040 }
1041 }
1042
1043 /* 1. Close the PCM devices */
1044 if (adev->voice_call_rx) {
1045 pcm_close(adev->voice_call_rx);
1046 adev->voice_call_rx = NULL;
1047 }
1048 if (adev->voice_call_tx) {
1049 pcm_close(adev->voice_call_tx);
1050 adev->voice_call_tx = NULL;
1051 }
1052
1053 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1054 if (uc_info == NULL) {
1055 ALOGE("%s: Could not find the usecase (%d) in the list",
1056 __func__, USECASE_VOICE_CALL);
1057 return -EINVAL;
1058 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059
1060 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001061 disable_audio_route(adev, uc_info, true);
1062
1063 /* 3. Disable the rx and tx devices */
1064 disable_snd_device(adev, uc_info->out_snd_device, false);
1065 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001067 list_remove(&uc_info->list);
1068 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001070 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071 return ret;
1072}
1073
1074static int start_voice_call(struct audio_device *adev)
1075{
1076 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 struct audio_usecase *uc_info;
1078 int pcm_dev_rx_id, pcm_dev_tx_id;
1079
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001080 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081
1082 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1083 uc_info->id = USECASE_VOICE_CALL;
1084 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001085 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001086 uc_info->devices = adev->primary_output->devices;
1087 uc_info->in_snd_device = SND_DEVICE_NONE;
1088 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001090 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001092 select_devices(adev, USECASE_VOICE_CALL);
1093
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094 pcm_dev_rx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1095 PCM_PLAYBACK);
1096 pcm_dev_tx_id = get_pcm_device_id(adev->audio_route, uc_info->id,
1097 PCM_CAPTURE);
1098
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1100 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1101 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001102 ret = -EIO;
1103 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001104 }
1105
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001106 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107 __func__, SOUND_CARD, pcm_dev_rx_id);
1108 adev->voice_call_rx = pcm_open(SOUND_CARD,
1109 pcm_dev_rx_id,
1110 PCM_OUT, &pcm_config_voice_call);
1111 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1112 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001113 ret = -EIO;
1114 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115 }
1116
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001117 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118 __func__, SOUND_CARD, pcm_dev_tx_id);
1119 adev->voice_call_tx = pcm_open(SOUND_CARD,
1120 pcm_dev_tx_id,
1121 PCM_IN, &pcm_config_voice_call);
1122 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1123 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001124 ret = -EIO;
1125 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001126 }
1127 pcm_start(adev->voice_call_rx);
1128 pcm_start(adev->voice_call_tx);
1129
1130 if (adev->csd_client) {
1131 if (adev->csd_start_voice == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001132 ALOGE("dlsym error for csd_client_start_voice");
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001133 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 } else {
1135 ret = adev->csd_start_voice();
1136 if (ret < 0) {
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001137 ALOGE("%s: csd_start_voice error %d\n", __func__, ret);
1138 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139 }
1140 }
1141 }
1142
1143 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001144 return 0;
1145
1146error_start_voice:
1147 stop_voice_call(adev);
1148
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001149 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150 return ret;
1151}
1152
1153static int check_input_parameters(uint32_t sample_rate,
1154 audio_format_t format,
1155 int channel_count)
1156{
1157 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1158
1159 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1160
1161 switch (sample_rate) {
1162 case 8000:
1163 case 11025:
1164 case 12000:
1165 case 16000:
1166 case 22050:
1167 case 24000:
1168 case 32000:
1169 case 44100:
1170 case 48000:
1171 break;
1172 default:
1173 return -EINVAL;
1174 }
1175
1176 return 0;
1177}
1178
1179static size_t get_input_buffer_size(uint32_t sample_rate,
1180 audio_format_t format,
1181 int channel_count)
1182{
1183 size_t size = 0;
1184
1185 if (check_input_parameters(sample_rate, format, channel_count) != 0) return 0;
1186
1187 if (sample_rate == 8000 || sample_rate == 16000 || sample_rate == 32000) {
1188 size = (sample_rate * 20) / 1000;
1189 } else if (sample_rate == 11025 || sample_rate == 12000) {
1190 size = 256;
1191 } else if (sample_rate == 22050 || sample_rate == 24000) {
1192 size = 512;
1193 } else if (sample_rate == 44100 || sample_rate == 48000) {
1194 size = 1024;
1195 }
1196
1197 return size * sizeof(short) * channel_count;
1198}
1199
1200static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1201{
1202 struct stream_out *out = (struct stream_out *)stream;
1203
1204 return out->config.rate;
1205}
1206
1207static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1208{
1209 return -ENOSYS;
1210}
1211
1212static size_t out_get_buffer_size(const struct audio_stream *stream)
1213{
1214 struct stream_out *out = (struct stream_out *)stream;
1215
1216 return out->config.period_size * audio_stream_frame_size(stream);
1217}
1218
1219static uint32_t out_get_channels(const struct audio_stream *stream)
1220{
1221 struct stream_out *out = (struct stream_out *)stream;
1222
1223 return out->channel_mask;
1224}
1225
1226static audio_format_t out_get_format(const struct audio_stream *stream)
1227{
1228 return AUDIO_FORMAT_PCM_16_BIT;
1229}
1230
1231static int out_set_format(struct audio_stream *stream, audio_format_t format)
1232{
1233 return -ENOSYS;
1234}
1235
1236static int out_standby(struct audio_stream *stream)
1237{
1238 struct stream_out *out = (struct stream_out *)stream;
1239 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001240 ALOGD("%s: enter: usecase(%d: %s)", __func__,
1241 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242 pthread_mutex_lock(&out->lock);
1243
1244 if (!out->standby) {
1245 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001246 if (out->pcm) {
1247 pcm_close(out->pcm);
1248 out->pcm = NULL;
1249 }
1250 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001252 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001253 }
1254 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001255 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001256 return 0;
1257}
1258
1259static int out_dump(const struct audio_stream *stream, int fd)
1260{
1261 return 0;
1262}
1263
1264static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1265{
1266 struct stream_out *out = (struct stream_out *)stream;
1267 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001268 struct audio_usecase *usecase;
1269 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001270 struct str_parms *parms;
1271 char value[32];
1272 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001273 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001275 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
1276 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 parms = str_parms_create_str(kvpairs);
1278 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1279 if (ret >= 0) {
1280 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001282 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001283
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001284 /*
1285 * When HDMI cable is unplugged the music playback is paused and
1286 * the policy manager sends routing=0. But the audioflinger
1287 * continues to write data until standby time (3sec).
1288 * As the HDMI core is turned off, the write gets blocked.
1289 * Avoid this by routing audio to speaker until standby.
1290 */
1291 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1292 val == AUDIO_DEVICE_NONE) {
1293 val = AUDIO_DEVICE_OUT_SPEAKER;
1294 }
1295
1296 /*
1297 * select_devices() call below switches all the usecases on the same
1298 * backend to the new device. Refer to check_usecases_codec_backend() in
1299 * the select_devices(). But how do we undo this?
1300 *
1301 * For example, music playback is active on headset (deep-buffer usecase)
1302 * and if we go to ringtones and select a ringtone, low-latency usecase
1303 * will be started on headset+speaker. As we can't enable headset+speaker
1304 * and headset devices at the same time, select_devices() switches the music
1305 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1306 * So when the ringtone playback is completed, how do we undo the same?
1307 *
1308 * We are relying on the out_set_parameters() call on deep-buffer output,
1309 * once the ringtone playback is ended.
1310 * NOTE: We should not check if the current devices are same as new devices.
1311 * Because select_devices() must be called to switch back the music
1312 * playback to headset.
1313 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001314 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001315 out->devices = val;
1316
1317 if (!out->standby)
1318 select_devices(adev, out->usecase);
1319
1320 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1321 (out == adev->primary_output)) {
1322 start_voice_call(adev);
1323 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1324 (out == adev->primary_output)) {
1325 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001326 }
1327 }
1328
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001329 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1330 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001335 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336 }
1337 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001338 ALOGD("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 return ret;
1340}
1341
1342static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1343{
1344 struct stream_out *out = (struct stream_out *)stream;
1345 struct str_parms *query = str_parms_create_str(keys);
1346 char *str;
1347 char value[256];
1348 struct str_parms *reply = str_parms_create();
1349 size_t i, j;
1350 int ret;
1351 bool first = true;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001352 ALOGD("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1354 if (ret >= 0) {
1355 value[0] = '\0';
1356 i = 0;
1357 while (out->supported_channel_masks[i] != 0) {
1358 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1359 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1360 if (!first) {
1361 strcat(value, "|");
1362 }
1363 strcat(value, out_channels_name_to_enum_table[j].name);
1364 first = false;
1365 break;
1366 }
1367 }
1368 i++;
1369 }
1370 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1371 str = str_parms_to_str(reply);
1372 } else {
1373 str = strdup(keys);
1374 }
1375 str_parms_destroy(query);
1376 str_parms_destroy(reply);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001377 ALOGD("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 return str;
1379}
1380
1381static uint32_t out_get_latency(const struct audio_stream_out *stream)
1382{
1383 struct stream_out *out = (struct stream_out *)stream;
1384
1385 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
1386}
1387
1388static int out_set_volume(struct audio_stream_out *stream, float left,
1389 float right)
1390{
Eric Laurenta9024de2013-04-04 09:19:12 -07001391 struct stream_out *out = (struct stream_out *)stream;
1392 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1393 /* only take left channel into account: the API is for stereo anyway */
1394 out->muted = (left == 0.0f);
1395 return 0;
1396 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001397 return -ENOSYS;
1398}
1399
1400static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1401 size_t bytes)
1402{
1403 struct stream_out *out = (struct stream_out *)stream;
1404 struct audio_device *adev = out->dev;
1405 int i, ret = -1;
1406
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 pthread_mutex_lock(&out->lock);
1408 if (out->standby) {
Eric Laurent150dbfe2013-02-27 14:31:02 -08001409 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001411 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413 goto exit;
1414 }
1415 out->standby = false;
1416 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417
1418 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001419 if (out->muted)
1420 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001421 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1422 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1423 }
1424
1425exit:
1426 pthread_mutex_unlock(&out->lock);
1427
1428 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001429 if (out->pcm)
1430 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 out_standby(&out->stream.common);
1432 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1433 out_get_sample_rate(&out->stream.common));
1434 }
1435 return bytes;
1436}
1437
1438static int out_get_render_position(const struct audio_stream_out *stream,
1439 uint32_t *dsp_frames)
1440{
1441 return -EINVAL;
1442}
1443
1444static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1445{
1446 return 0;
1447}
1448
1449static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1450{
1451 return 0;
1452}
1453
1454static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1455 int64_t *timestamp)
1456{
1457 return -EINVAL;
1458}
1459
1460/** audio_stream_in implementation **/
1461static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1462{
1463 struct stream_in *in = (struct stream_in *)stream;
1464
1465 return in->config.rate;
1466}
1467
1468static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1469{
1470 return -ENOSYS;
1471}
1472
1473static size_t in_get_buffer_size(const struct audio_stream *stream)
1474{
1475 struct stream_in *in = (struct stream_in *)stream;
1476
1477 return in->config.period_size * audio_stream_frame_size(stream);
1478}
1479
1480static uint32_t in_get_channels(const struct audio_stream *stream)
1481{
1482 struct stream_in *in = (struct stream_in *)stream;
1483
1484 return in->channel_mask;
1485}
1486
1487static audio_format_t in_get_format(const struct audio_stream *stream)
1488{
1489 return AUDIO_FORMAT_PCM_16_BIT;
1490}
1491
1492static int in_set_format(struct audio_stream *stream, audio_format_t format)
1493{
1494 return -ENOSYS;
1495}
1496
1497static int in_standby(struct audio_stream *stream)
1498{
1499 struct stream_in *in = (struct stream_in *)stream;
1500 struct audio_device *adev = in->dev;
1501 int status = 0;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001502 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001503 pthread_mutex_lock(&in->lock);
1504 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001505 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001506 if (in->pcm) {
1507 pcm_close(in->pcm);
1508 in->pcm = NULL;
1509 }
1510 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001511 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001512 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513 }
1514 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001515 ALOGD("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001516 return status;
1517}
1518
1519static int in_dump(const struct audio_stream *stream, int fd)
1520{
1521 return 0;
1522}
1523
1524static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1525{
1526 struct stream_in *in = (struct stream_in *)stream;
1527 struct audio_device *adev = in->dev;
1528 struct str_parms *parms;
1529 char *str;
1530 char value[32];
1531 int ret, val = 0;
1532
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001533 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 parms = str_parms_create_str(kvpairs);
1535
1536 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1537
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001538 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001539 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540 if (ret >= 0) {
1541 val = atoi(value);
1542 /* no audio source uses val == 0 */
1543 if ((in->source != val) && (val != 0)) {
1544 in->source = val;
1545 }
1546 }
1547
1548 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1549 if (ret >= 0) {
1550 val = atoi(value);
1551 if ((in->device != val) && (val != 0)) {
1552 in->device = val;
1553 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001554 if (!in->standby)
1555 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001556 }
1557 }
1558
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001560 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001561
1562 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001563 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 return ret;
1565}
1566
1567static char* in_get_parameters(const struct audio_stream *stream,
1568 const char *keys)
1569{
1570 return strdup("");
1571}
1572
1573static int in_set_gain(struct audio_stream_in *stream, float gain)
1574{
1575 return 0;
1576}
1577
1578static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1579 size_t bytes)
1580{
1581 struct stream_in *in = (struct stream_in *)stream;
1582 struct audio_device *adev = in->dev;
1583 int i, ret = -1;
1584
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585 pthread_mutex_lock(&in->lock);
1586 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001587 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001589 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001590 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591 goto exit;
1592 }
1593 in->standby = 0;
1594 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001595
1596 if (in->pcm) {
1597 ret = pcm_read(in->pcm, buffer, bytes);
1598 }
1599
1600 /*
1601 * Instead of writing zeroes here, we could trust the hardware
1602 * to always provide zeroes when muted.
1603 */
1604 if (ret == 0 && adev->mic_mute)
1605 memset(buffer, 0, bytes);
1606
1607exit:
1608 pthread_mutex_unlock(&in->lock);
1609
1610 if (ret != 0) {
1611 in_standby(&in->stream.common);
1612 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1613 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1614 in_get_sample_rate(&in->stream.common));
1615 }
1616 return bytes;
1617}
1618
1619static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1620{
1621 return 0;
1622}
1623
1624static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1625{
1626 return 0;
1627}
1628
1629static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1630{
1631 return 0;
1632}
1633
1634static int adev_open_output_stream(struct audio_hw_device *dev,
1635 audio_io_handle_t handle,
1636 audio_devices_t devices,
1637 audio_output_flags_t flags,
1638 struct audio_config *config,
1639 struct audio_stream_out **stream_out)
1640{
1641 struct audio_device *adev = (struct audio_device *)dev;
1642 struct stream_out *out;
1643 int i, ret;
1644
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001645 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001646 __func__, config->sample_rate, config->channel_mask, devices, flags);
1647 *stream_out = NULL;
1648 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1649
1650 if (devices == AUDIO_DEVICE_NONE)
1651 devices = AUDIO_DEVICE_OUT_SPEAKER;
1652
1653 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1654 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1655 out->flags = flags;
1656 out->devices = devices;
1657
1658 /* Init use case and pcm_config */
1659 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1660 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001661 pthread_mutex_lock(&adev->lock);
1662 ret = read_hdmi_channel_masks(out);
1663 pthread_mutex_unlock(&adev->lock);
1664 if (ret != 0) {
1665 /* If HDMI does not support multi channel playback, set the default */
1666 out->config.channels = popcount(out->channel_mask);
1667 set_hdmi_channels(adev->mixer, out->config.channels);
1668 goto error_open;
1669 }
1670
1671 if (config->sample_rate == 0)
1672 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1673 if (config->channel_mask == 0)
1674 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1675
1676 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001677 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1678 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001679 out->config.rate = config->sample_rate;
1680 out->config.channels = popcount(out->channel_mask);
1681 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
1682 set_hdmi_channels(adev->mixer, out->config.channels);
1683 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1684 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1685 out->config = pcm_config_deep_buffer;
1686 } else {
1687 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1688 out->config = pcm_config_low_latency;
1689 }
1690
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001691 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1692 if(adev->primary_output == NULL)
1693 adev->primary_output = out;
1694 else {
1695 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001696 ret = -EEXIST;
1697 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001698 }
1699 }
1700
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001701 /* Check if this usecase is already existing */
1702 pthread_mutex_lock(&adev->lock);
1703 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1704 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001705 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001706 ret = -EEXIST;
1707 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001708 }
1709 pthread_mutex_unlock(&adev->lock);
1710
1711 out->stream.common.get_sample_rate = out_get_sample_rate;
1712 out->stream.common.set_sample_rate = out_set_sample_rate;
1713 out->stream.common.get_buffer_size = out_get_buffer_size;
1714 out->stream.common.get_channels = out_get_channels;
1715 out->stream.common.get_format = out_get_format;
1716 out->stream.common.set_format = out_set_format;
1717 out->stream.common.standby = out_standby;
1718 out->stream.common.dump = out_dump;
1719 out->stream.common.set_parameters = out_set_parameters;
1720 out->stream.common.get_parameters = out_get_parameters;
1721 out->stream.common.add_audio_effect = out_add_audio_effect;
1722 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1723 out->stream.get_latency = out_get_latency;
1724 out->stream.set_volume = out_set_volume;
1725 out->stream.write = out_write;
1726 out->stream.get_render_position = out_get_render_position;
1727 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1728
1729 out->dev = adev;
1730 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001731 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001732
1733 config->format = out->stream.common.get_format(&out->stream.common);
1734 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1735 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1736
1737 *stream_out = &out->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001738 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001740
1741error_open:
1742 free(out);
1743 *stream_out = NULL;
1744 ALOGD("%s: exit: ret %d", __func__, ret);
1745 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746}
1747
1748static void adev_close_output_stream(struct audio_hw_device *dev,
1749 struct audio_stream_out *stream)
1750{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001751 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001752 out_standby(&stream->common);
1753 free(stream);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001754 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755}
1756
1757static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1758{
1759 struct audio_device *adev = (struct audio_device *)dev;
1760 struct str_parms *parms;
1761 char *str;
1762 char value[32];
1763 int ret;
1764
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001765 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001766
1767 parms = str_parms_create_str(kvpairs);
1768 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1769 if (ret >= 0) {
1770 int tty_mode;
1771
1772 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1773 tty_mode = TTY_MODE_OFF;
1774 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1775 tty_mode = TTY_MODE_VCO;
1776 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1777 tty_mode = TTY_MODE_HCO;
1778 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1779 tty_mode = TTY_MODE_FULL;
1780 else
1781 return -EINVAL;
1782
1783 pthread_mutex_lock(&adev->lock);
1784 if (tty_mode != adev->tty_mode) {
1785 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001786 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1787 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001788 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789 }
1790 pthread_mutex_unlock(&adev->lock);
1791 }
1792
1793 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1794 if (ret >= 0) {
1795 /* When set to false, HAL should disable EC and NS
1796 * But it is currently not supported.
1797 */
1798 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1799 adev->bluetooth_nrec = true;
1800 else
1801 adev->bluetooth_nrec = false;
1802 }
1803
1804 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1805 if (ret >= 0) {
1806 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1807 adev->screen_off = false;
1808 else
1809 adev->screen_off = true;
1810 }
1811
1812 str_parms_destroy(parms);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001813 ALOGD("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814 return ret;
1815}
1816
1817static char* adev_get_parameters(const struct audio_hw_device *dev,
1818 const char *keys)
1819{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001820 return strdup("");
1821}
1822
1823static int adev_init_check(const struct audio_hw_device *dev)
1824{
1825 return 0;
1826}
1827
1828static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1829{
1830 struct audio_device *adev = (struct audio_device *)dev;
1831 int vol, err = 0;
1832
1833 pthread_mutex_lock(&adev->lock);
1834 adev->voice_volume = volume;
1835 if (adev->mode == AUDIO_MODE_IN_CALL) {
1836 if (volume < 0.0) {
1837 volume = 0.0;
1838 } else if (volume > 1.0) {
1839 volume = 1.0;
1840 }
1841
1842 vol = lrint(volume * 100.0);
1843
1844 // Voice volume levels from android are mapped to driver volume levels as follows.
1845 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
1846 // So adjust the volume to get the correct volume index in driver
1847 vol = 100 - vol;
1848
1849 if (adev->csd_client) {
1850 if (adev->csd_volume == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001851 ALOGE("%s: dlsym error for csd_client_volume", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852 } else {
1853 err = adev->csd_volume(vol);
1854 if (err < 0) {
1855 ALOGE("%s: csd_client error %d", __func__, err);
1856 }
1857 }
1858 } else {
1859 ALOGE("%s: No CSD Client present", __func__);
1860 }
1861 }
1862 pthread_mutex_unlock(&adev->lock);
1863 return err;
1864}
1865
1866static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1867{
1868 return -ENOSYS;
1869}
1870
1871static int adev_get_master_volume(struct audio_hw_device *dev,
1872 float *volume)
1873{
1874 return -ENOSYS;
1875}
1876
1877static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1878{
1879 return -ENOSYS;
1880}
1881
1882static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1883{
1884 return -ENOSYS;
1885}
1886
1887static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1888{
1889 struct audio_device *adev = (struct audio_device *)dev;
1890
1891 pthread_mutex_lock(&adev->lock);
1892 if (adev->mode != mode) {
1893 adev->mode = mode;
1894 }
1895 pthread_mutex_unlock(&adev->lock);
1896 return 0;
1897}
1898
1899static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1900{
1901 struct audio_device *adev = (struct audio_device *)dev;
1902 int err = 0;
1903
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001904 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905 adev->mic_mute = state;
1906 if (adev->mode == AUDIO_MODE_IN_CALL) {
1907 if (adev->csd_client) {
1908 if (adev->csd_mic_mute == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08001909 ALOGE("%s: dlsym error for csd_mic_mute", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 } else {
1911 err = adev->csd_mic_mute(state);
1912 if (err < 0) {
1913 ALOGE("%s: csd_client error %d", __func__, err);
1914 }
1915 }
1916 } else {
1917 ALOGE("%s: No CSD Client present", __func__);
1918 }
1919 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001920 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001921 return err;
1922}
1923
1924static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1925{
1926 struct audio_device *adev = (struct audio_device *)dev;
1927
1928 *state = adev->mic_mute;
1929
1930 return 0;
1931}
1932
1933static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1934 const struct audio_config *config)
1935{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001936 int channel_count = popcount(config->channel_mask);
1937
1938 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1939}
1940
1941static int adev_open_input_stream(struct audio_hw_device *dev,
1942 audio_io_handle_t handle,
1943 audio_devices_t devices,
1944 struct audio_config *config,
1945 struct audio_stream_in **stream_in)
1946{
1947 struct audio_device *adev = (struct audio_device *)dev;
1948 struct stream_in *in;
1949 int ret, buffer_size, frame_size;
1950 int channel_count = popcount(config->channel_mask);
1951
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001952 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001953 *stream_in = NULL;
1954 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1955 return -EINVAL;
1956
1957 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1958
1959 in->stream.common.get_sample_rate = in_get_sample_rate;
1960 in->stream.common.set_sample_rate = in_set_sample_rate;
1961 in->stream.common.get_buffer_size = in_get_buffer_size;
1962 in->stream.common.get_channels = in_get_channels;
1963 in->stream.common.get_format = in_get_format;
1964 in->stream.common.set_format = in_set_format;
1965 in->stream.common.standby = in_standby;
1966 in->stream.common.dump = in_dump;
1967 in->stream.common.set_parameters = in_set_parameters;
1968 in->stream.common.get_parameters = in_get_parameters;
1969 in->stream.common.add_audio_effect = in_add_audio_effect;
1970 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1971 in->stream.set_gain = in_set_gain;
1972 in->stream.read = in_read;
1973 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1974
1975 in->device = devices;
1976 in->source = AUDIO_SOURCE_DEFAULT;
1977 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 in->standby = 1;
1979 in->channel_mask = config->channel_mask;
1980
1981 /* Update config params with the requested sample rate and channels */
1982 in->usecase = USECASE_AUDIO_RECORD;
1983 in->config = pcm_config_audio_capture;
1984 in->config.channels = channel_count;
1985 in->config.rate = config->sample_rate;
1986
1987 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1988 buffer_size = get_input_buffer_size(config->sample_rate,
1989 config->format,
1990 channel_count);
1991 in->config.period_size = buffer_size / frame_size;
1992
1993 *stream_in = &in->stream;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001994 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 return 0;
1996
1997err_open:
1998 free(in);
1999 *stream_in = NULL;
2000 return ret;
2001}
2002
2003static void adev_close_input_stream(struct audio_hw_device *dev,
2004 struct audio_stream_in *stream)
2005{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002006 ALOGD("%s", __func__);
2007
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008 in_standby(&stream->common);
2009 free(stream);
2010
2011 return;
2012}
2013
2014static int adev_dump(const audio_hw_device_t *device, int fd)
2015{
2016 return 0;
2017}
2018
2019static int adev_close(hw_device_t *device)
2020{
2021 struct audio_device *adev = (struct audio_device *)device;
2022 audio_route_free(adev->audio_route);
2023 free(device);
2024 return 0;
2025}
2026
2027static void init_platform_data(struct audio_device *adev)
2028{
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002029 char platform[PROPERTY_VALUE_MAX];
2030 char baseband[PROPERTY_VALUE_MAX];
2031 char value[PROPERTY_VALUE_MAX];
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002032
2033 adev->dualmic_config = DUALMIC_CONFIG_NONE;
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002034 adev->fluence_in_spkr_mode = false;
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002035 adev->fluence_in_voice_call = false;
2036 adev->fluence_in_voice_rec = false;
2037 adev->mic_type_analog = false;
2038
2039 property_get("persist.audio.handset.mic.type",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002040 if (!strcmp("analog", value))
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002041 adev->mic_type_analog = true;
2042
2043 property_get("persist.audio.dualmic.config",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002044 if (!strcmp("broadside", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002045 adev->dualmic_config = DUALMIC_CONFIG_BROADSIDE;
2046 adev->acdb_settings |= DMIC_FLAG;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002047 } else if (!strcmp("endfire", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002048 adev->dualmic_config = DUALMIC_CONFIG_ENDFIRE;
2049 adev->acdb_settings |= DMIC_FLAG;
2050 }
2051
2052 if (adev->dualmic_config != DUALMIC_CONFIG_NONE) {
2053 property_get("persist.audio.fluence.voicecall",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002054 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002055 adev->fluence_in_voice_call = true;
2056 }
2057
2058 property_get("persist.audio.fluence.voicerec",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002059 if (!strcmp("true", value)) {
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002060 adev->fluence_in_voice_rec = true;
2061 }
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002062
2063 property_get("persist.audio.fluence.speaker",value,"");
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002064 if (!strcmp("true", value)) {
Ravi Kumar Alamanda02317792013-03-04 20:56:50 -08002065 adev->fluence_in_spkr_mode = true;
2066 }
Ravi Kumar Alamanda72c411f2013-02-12 02:09:33 -08002067 }
2068
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 adev->acdb_handle = dlopen(LIB_ACDB_LOADER, RTLD_NOW);
2070 if (adev->acdb_handle == NULL) {
2071 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_ACDB_LOADER);
2072 } else {
2073 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_ACDB_LOADER);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074 adev->acdb_deallocate = (acdb_deallocate_t)dlsym(adev->acdb_handle,
2075 "acdb_loader_deallocate_ACDB");
2076 adev->acdb_send_audio_cal = (acdb_send_audio_cal_t)dlsym(adev->acdb_handle,
2077 "acdb_loader_send_audio_cal");
2078 adev->acdb_send_voice_cal = (acdb_send_voice_cal_t)dlsym(adev->acdb_handle,
2079 "acdb_loader_send_voice_cal");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002080 adev->acdb_init = (acdb_init_t)dlsym(adev->acdb_handle,
2081 "acdb_loader_init_ACDB");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002082 if (adev->acdb_init == NULL)
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002083 ALOGE("%s: dlsym error %s for acdb_loader_init_ACDB", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002084 else
2085 adev->acdb_init();
2086 }
2087
2088 /* If platform is Fusion3, load CSD Client specific symbols
2089 * Voice call is handled by MDM and apps processor talks to
2090 * MDM through CSD Client
2091 */
2092 property_get("ro.board.platform", platform, "");
2093 property_get("ro.baseband", baseband, "");
2094 if (!strcmp("msm8960", platform) && !strcmp("mdm", baseband)) {
2095 adev->csd_client = dlopen(LIB_CSD_CLIENT, RTLD_NOW);
2096 if (adev->csd_client == NULL)
2097 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_CSD_CLIENT);
2098 }
2099
2100 if (adev->csd_client) {
2101 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_CSD_CLIENT);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002102 adev->csd_client_deinit = (csd_client_deinit_t)dlsym(adev->csd_client,
2103 "csd_client_deinit");
2104 adev->csd_disable_device = (csd_disable_device_t)dlsym(adev->csd_client,
2105 "csd_client_disable_device");
2106 adev->csd_enable_device = (csd_enable_device_t)dlsym(adev->csd_client,
2107 "csd_client_enable_device");
2108 adev->csd_start_voice = (csd_start_voice_t)dlsym(adev->csd_client,
2109 "csd_client_start_voice");
2110 adev->csd_stop_voice = (csd_stop_voice_t)dlsym(adev->csd_client,
2111 "csd_client_stop_voice");
2112 adev->csd_volume = (csd_volume_t)dlsym(adev->csd_client,
2113 "csd_client_volume");
2114 adev->csd_mic_mute = (csd_mic_mute_t)dlsym(adev->csd_client,
2115 "csd_client_mic_mute");
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002116 adev->csd_client_init = (csd_client_init_t)dlsym(adev->csd_client,
2117 "csd_client_init");
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118
2119 if (adev->csd_client_init == NULL) {
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -08002120 ALOGE("%s: dlsym error %s for csd_client_init", __func__, dlerror());
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121 } else {
2122 adev->csd_client_init();
2123 }
2124 }
2125}
2126
2127static int adev_open(const hw_module_t *module, const char *name,
2128 hw_device_t **device)
2129{
2130 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002131 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002133 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002134 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2135
2136 adev = calloc(1, sizeof(struct audio_device));
2137
2138 adev->mixer = mixer_open(MIXER_CARD);
2139 if (!adev->mixer) {
2140 ALOGE("Unable to open the mixer, aborting.");
2141 return -ENOSYS;
2142 }
2143
2144 adev->audio_route = audio_route_init(MIXER_CARD, MIXER_XML_PATH);
2145 if (!adev->audio_route) {
2146 free(adev);
2147 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
2148 *device = NULL;
2149 return -EINVAL;
2150 }
2151
2152 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2153 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2154 adev->device.common.module = (struct hw_module_t *)module;
2155 adev->device.common.close = adev_close;
2156
2157 adev->device.init_check = adev_init_check;
2158 adev->device.set_voice_volume = adev_set_voice_volume;
2159 adev->device.set_master_volume = adev_set_master_volume;
2160 adev->device.get_master_volume = adev_get_master_volume;
2161 adev->device.set_master_mute = adev_set_master_mute;
2162 adev->device.get_master_mute = adev_get_master_mute;
2163 adev->device.set_mode = adev_set_mode;
2164 adev->device.set_mic_mute = adev_set_mic_mute;
2165 adev->device.get_mic_mute = adev_get_mic_mute;
2166 adev->device.set_parameters = adev_set_parameters;
2167 adev->device.get_parameters = adev_get_parameters;
2168 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2169 adev->device.open_output_stream = adev_open_output_stream;
2170 adev->device.close_output_stream = adev_close_output_stream;
2171 adev->device.open_input_stream = adev_open_input_stream;
2172 adev->device.close_input_stream = adev_close_input_stream;
2173 adev->device.dump = adev_dump;
2174
2175 /* Set the default route before the PCM stream is opened */
2176 pthread_mutex_lock(&adev->lock);
2177 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002178 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002179 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002180 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 adev->voice_call_rx = NULL;
2182 adev->voice_call_tx = NULL;
2183 adev->voice_volume = 1.0f;
2184 adev->tty_mode = TTY_MODE_OFF;
2185 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002187 adev->acdb_settings = TTY_MODE_OFF;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002188 for (i = 0; i < SND_DEVICE_MAX; i++) {
2189 adev->snd_dev_ref_cnt[i] = 0;
2190 }
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002191 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192 pthread_mutex_unlock(&adev->lock);
2193
2194 /* Loads platform specific libraries dynamically */
2195 init_platform_data(adev);
2196
2197 *device = &adev->device.common;
2198
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002199 ALOGD("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200 return 0;
2201}
2202
2203static struct hw_module_methods_t hal_module_methods = {
2204 .open = adev_open,
2205};
2206
2207struct audio_module HAL_MODULE_INFO_SYM = {
2208 .common = {
2209 .tag = HARDWARE_MODULE_TAG,
2210 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2211 .hal_api_version = HARDWARE_HAL_API_VERSION,
2212 .id = AUDIO_HARDWARE_MODULE_ID,
2213 .name = "QCOM Audio HAL",
2214 .author = "Code Aurora Forum",
2215 .methods = &hal_module_methods,
2216 },
2217};